From 39895e13c5a5a7f242bc0faf4f01d0eec80a15b5 Mon Sep 17 00:00:00 2001 From: g1n Date: Sun, 29 May 2022 09:23:50 +0300 Subject: [PATCH] Initial commit --- .gitignore | 2 + LICENSE | 10 ++++ README.org | 1 + src/Makefile | 26 ++++++++++ src/grep.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 179 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.org create mode 100644 src/Makefile create mode 100644 src/grep.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d0ee085 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vgcore* +build diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4d1842a --- /dev/null +++ b/LICENSE @@ -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. + diff --git a/README.org b/README.org new file mode 100644 index 0000000..383d2fe --- /dev/null +++ b/README.org @@ -0,0 +1 @@ +#+TITLE: GRU txtutils diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..89318da --- /dev/null +++ b/src/Makefile @@ -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) diff --git a/src/grep.c b/src/grep.c new file mode 100644 index 0000000..c5ec063 --- /dev/null +++ b/src/grep.c @@ -0,0 +1,140 @@ +#include +#include +#include +#include +#include +#include +#include + +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); +}