Initial commit

This commit is contained in:
g1n 2022-05-29 09:23:50 +03:00
commit 39895e13c5
Signed by: g1n
GPG Key ID: 8D352193D65D4E2C
5 changed files with 179 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
vgcore*
build

10
LICENSE Normal file
View File

@ -0,0 +1,10 @@
MIT License
Copyright (c) 2021-2022 GRU
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1
README.org Normal file
View File

@ -0,0 +1 @@
#+TITLE: GRU txtutils

26
src/Makefile Normal file
View File

@ -0,0 +1,26 @@
SRCFILES!=ls -d */ | sed 's/include\///' | awk 'NF' | sed 's/.*/&\*.c/g'
BUILDDIR=../build
CFILES=$(wildcard $(SRCFILES) *.c)
OFILES=$(patsubst %.c, $(BUILDDIR)/%.o, $(CFILES))
TXTUTILS=$(BUILDDIR)/grep
INCLUDEFLAGS=-Iinclude/
DEBUGFLAGS=-fsanitize=address -fsanitize=leak -fsanitize=undefined -fsanitize=pointer-compare -lasan
CFLAGS=-Wall -Wextra $(INCLUDEFLAGS) -g
LIBFLAGS=
.PHONY: all clean test
.SUFFIXES: .o .c
all: $(BUILDDIR) $(TXTUTILS)
$(BUILDDIR):
mkdir -p $(BUILDDIR)
$(TXTUTILS): $(CFILES)
$(CC) $< -o $@ $(CFLAGS) $(LIBFLAGS)
clean:
rm -rf $(BUILDDIR)

140
src/grep.c Normal file
View File

@ -0,0 +1,140 @@
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <regex.h>
struct file {
FILE *file;
char *filename;
} file_t;
bool show_filenames = false;
bool invert_match = false;
bool line_number = false;
bool ignore_case = false;
bool extended_regex = false;
int usage() {
printf("Usage: %s [OPTION]... PATTERNS [FILE]...\n", "grep");
printf("Try '%s --help' for more information.\n", "grep");
return 2;
}
int main(int argc, char *argv[]) {
struct file **infiles = malloc(sizeof(FILE));
infiles[0] = malloc(sizeof(FILE));
infiles[0]->file = stdin;
int files_index = 0;
char **regex = malloc(sizeof(char*));
regex[0] = NULL;
int regex_index = 0;
int opt;
char line[256];
if (argc < 2) {
return usage();
} else {
while ((opt = getopt(argc, argv, "::e:vniyE")) != -1) {
switch (opt) {
case 'v':
invert_match = true;
break;
case 'n':
line_number = true;
break;
case 'i':
case 'y':
ignore_case = true;
break;
case 'E':
extended_regex = true;
break;
case 'e':
regex[regex_index] = malloc(strlen(optarg));
regex[regex_index] = optarg;
regex_index++;
break;
case '?':
printf("%s: invalid option -- '%c'\n", "grep", optopt);
return usage();
}
}
for (; optind < argc; optind++) {
if (regex[0] == NULL) {
regex[0] = malloc(strlen(argv[optind]));
regex[0] = argv[optind];
regex_index++;
} else {
if (strcmp(argv[optind], "-")) {
infiles[files_index] = malloc(sizeof(FILE) + strlen(argv[optind]));
infiles[files_index]->file = fopen(argv[optind], "r");
infiles[files_index]->filename = argv[optind];
if (infiles[files_index]->file == NULL) {
perror(argv[optind]);
return -1;
}
} else {
infiles[files_index] = malloc(sizeof(FILE) + strlen("(standard input)"));
infiles[files_index]->file = stdin;
infiles[files_index]->filename = "(standard input)";
}
files_index++;
}
}
}
if (files_index > 1) {
show_filenames = true;
}
int curline = 1;
int i = 0;
int j = 0;
int cur_line_has_regex = 1;
int regflags = 0;
if (extended_regex) {
regflags |= REG_EXTENDED;
}
if (ignore_case) {
regflags |= REG_ICASE;
}
regex_t exp;
regmatch_t matches[1];
do {
while (fgets(line, sizeof line, infiles[i]->file) != NULL) {
do {
regcomp(&exp, regex[j], regflags);
cur_line_has_regex = regexec(&exp, line, 1, matches, 0);
if (cur_line_has_regex == 0 && !invert_match) {
curline++;
if (line_number) printf("%d:", curline);
if (show_filenames) {
printf("%s:%s", infiles[i]->filename, line);
} else {
printf("%s", line);
}
} else if (cur_line_has_regex != 0 && invert_match) {
curline++;
if (line_number) printf("%d:", curline);
if (show_filenames) {
printf("%s:%s", infiles[i]->filename, line);
} else {
printf("%s", line);
}
}
j++;
regfree(&exp);
} while (j < regex_index);
j = 0;
}
i++;
} while (i < files_index);
free(infiles);
}