neogrep: Init repo

This commit is contained in:
2026-02-28 14:27:52 -08:00
commit 264fe0a27f
7 changed files with 195 additions and 0 deletions

14
.clang-format Normal file
View File

@ -0,0 +1,14 @@
BasedOnStyle: WebKit
BreakBeforeBraces: Allman
AlwaysBreakAfterReturnType: All
BinPackArguments: true
BinPackParameters: true
IndentCaseLabels: true
IndentWidth: 2
TabWidth: 2
UseTab: Never
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignConsecutiveMacros: true
AlignTrailingComments: true
PointerAlignment: Left

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Files
*.o
ng

13
LICENSE Normal file
View File

@ -0,0 +1,13 @@
Copyright (c) 2026 Neo-Sekai Club
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

34
Makefile Normal file
View File

@ -0,0 +1,34 @@
.POSIX:
.SUFFIXES:
include config.mk
BIN_DIR = $(DESTDIR)$(PREFIX)bin
BIN_NAME = ng
SRC = main.c
OBJ = $(SRC:.c=.o)
CFLAGS = -std=c99 \
-Wall \
-Wextra \
-Wno-deprecated-declarations \
-O2
.SUFFIXES: .c .o
.c.o:
$(CC) $(CFLAGS) -c $<
ng: $(OBJ)
$(CC) -o $@ $(OBJ) $(CFLAGS)
.PHONY: clean install
clean:
rm -f $(BIN_NAME) $(OBJ)
install: ng
mkdir -p $(BIN_DIR)
cp -f $(BIN_NAME) $(BIN_DIR)
chmod 755 $(BIN_DIR)/$(BIN_NAME)

12
README.org Normal file
View File

@ -0,0 +1,12 @@
#+title: neogrep - The Recursive Line Checker That Sucks Less
#+author: Neo-Sekai Club
#+options: html-postamble:nil html-preable:nil toc:nil
It's cool to be able to grep your code to see where you may have made some sort of mistake, or when you're doing a debug or valgrind and go "Hey, this is like how many hundreds of lines of code! Where is this problematic function!?". If you're a psychopath, you pipe to grep. If you enjoy using garbage that eats your resources on compile-time, you use ripgrep. neogrep is the option that is meant to suck less and be far simpler.
Is it as robust as real grep? No, but it doesn't need to be. Does it fill the exact same needs as ripgrep? You bet it does. We managed to do this simple task in 115 lines of C code, with a more sane directory structure and build system. ripgrep spans across how many source files, but we narrowed neogrep down into just one. It doesn't need to do more, it needs to do what it's designed to do, and do that efficiently.
Due to neogrep's design, it's easily hackable if you are a C programmer yourself, allowing you (the end user) to add any features you may desire out of neogrep.
* Requirements
neogrep runs on any system that is POSIX compliant or allows for POSIX compliancy. We build neogrep with the intent that it will be used on everything that is a UNIX, UNIX-like, or Linux. This means that it does not run on Windows. Sorry, Wintoddlers.

4
config.mk Normal file
View File

@ -0,0 +1,4 @@
# Customize for your system
CC = clang
PREFIX = /usr/local/

115
main.c Normal file
View File

@ -0,0 +1,115 @@
/*
* main.c - Main entry point for neogrep
* $author: Buckwheat of Neo-Sekai Club
* $creation-date: 2026/02/28
* $version: 1.0.0
*
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#define NEWLINE '\n'
#define NULTERM '\0'
/* Function Declarations */
static int
err_msg(const char*);
static int
substr_check(char*, const char*, int, int, int);
/* Function Definitions */
static int
err_msg(const char* msg)
{
printf("ERROR: %s\n", msg);
return 1;
}
static int
substr_check(char* data, const char* substr, int start, int len, int line)
{
int newlen = len + 1;
char* str = (char*)malloc(sizeof(char) * newlen);
if (!str)
return -1;
for (int index = 0; index < newlen; index++)
if (index == len)
str[index] = NULTERM;
else
str[index] = data[index + start];
if (strstr(str, substr))
printf("\033[1;31mLine %d: \033[4;32m%s\033[0m", line, str);
free(str);
return 0;
}
int
main(int argc, char** argv)
{
if (argc > 3 || argc < 3)
return err_msg("Insufficient arg count\nUsage: ng <file> <string>");
struct stat file_info;
char* file_map;
int char_count = 0;
int file_size = 0;
int line_count = 0;
int src_file = -1;
const char* substr = argv[2];
const char* src_name = argv[1];
src_file = open(src_name, O_RDONLY, 0);
if (src_file == -1)
return err_msg("Could not open input file");
fstat(src_file, &file_info);
file_size = file_info.st_size;
file_map = (char*)mmap(NULL, file_size, PROT_READ,
MAP_SHARED, src_file, 0);
if (file_map == MAP_FAILED)
{
close(src_file);
return err_msg("mmap failed");
}
for (int index = 0; index < file_size; index++)
{
char current = file_map[index];
if (current == NEWLINE)
{
line_count++;
char_count = substr_check(file_map, substr,
index - char_count, char_count + 1, line_count);
}
else
char_count++;
if (char_count == -1)
return err_msg("Failed to malloc");
}
if (munmap(file_map, file_size) == -1)
return err_msg("munmap failed");
return 0;
}