/* * UNG's Not GNU * * Copyright (c) 2011,2022, Jakob Kaivo * * 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. */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include struct grep_list { struct grep_list *next; char *string; regex_t re; }; static enum { NORMAL, COUNT, LIST, QUIET } grep_display = NORMAL; static int grep_filenames = 0; static int grep_linenumbers = 0; static int grep_inverse = 0; static int grep_silent = 0; static int grep_exact = 0; static int grep_match(struct grep_list *head, const char *buf) { for (struct grep_list *c = head; c != NULL; c = c->next) { if (c->string) { if (grep_exact && strcmp(buf, c->string) == 0) { return !grep_inverse; } if (!grep_exact && strstr(buf, c->string)) { return !grep_inverse; } continue; } if (regexec(&(c->re), buf, 0, NULL, 0) == 0) { return !grep_inverse; } } return grep_inverse; } static uintmax_t grep(struct grep_list *head, const char *path) { FILE *f = stdin; if (path && strcmp(path, "-")) { f = fopen(path, "r"); if (f == NULL) { if (!grep_silent || (errno != ENOENT && errno != EPERM)) { fprintf(stderr, "grep: %s: %s\n", path, strerror(errno)); } if (grep_display == QUIET) { return 1; } return 2; } } uintmax_t found = 0; char *buf = NULL; size_t len = 0; uintmax_t line = 0; while (getline(&buf, &len, f) != -1) { line++; char *nl = strrchr(buf, '\n'); if (nl) { *nl = '\0'; } if (grep_match(head, buf)) { if (grep_display == NORMAL) { if (grep_filenames) { printf("%s:", path); } if (grep_linenumbers) { printf("%ju:", line); } printf("%s\n", buf); } found++; } } fclose(f); if (grep_display == COUNT) { if (grep_filenames) { printf("%s:", path); } printf("%ju\n", found); } if (grep_display == LIST && found > 0) { printf("%s\n", path); } return found == 0; } static struct grep_list * grep_add_list(struct grep_list *head, char *s) { char *pattern = strtok(s, "\n"); while (pattern) { struct grep_list *node = calloc(1, sizeof(*node)); if (node == NULL) { perror("grep"); exit(2); } if (head == NULL) { head = node; } else { struct grep_list *tmp = head; while (tmp->next != NULL) { tmp = tmp->next; } tmp->next = node; } node->string = strdup(pattern); pattern = strtok(NULL, "\n"); } return head; } static struct grep_list * grep_add_file(struct grep_list *head, char *path) { FILE *f = fopen(path, "r"); if (f == NULL) { fprintf(stderr, "grep: %s: %s\n", path, strerror(errno)); exit(2); } char *buf = NULL; size_t len = 0; while (getline(&buf, &len, f) != -1) { head = grep_add_list(head, buf); } free(buf); fclose(f); return head; } int main(int argc, char *argv[]) { setlocale(LC_ALL, ""); int flags = REG_NOSUB; struct grep_list *head = NULL; enum {REGEX, FIXED } type = REGEX; int c; while ((c = getopt(argc, argv, "EFce:f:ilnqsvx")) != -1) { switch (c) { case 'E': flags |= REG_EXTENDED; break; case 'F': type = FIXED; break; case 'c': grep_display = COUNT; break; case 'e': head = grep_add_list(head, optarg); break; case 'f': head = grep_add_file(head, optarg); break; case 'i': flags |= REG_ICASE; break; case 'l': grep_display = LIST; break; case 'n': grep_linenumbers = 1; break; case 'q': grep_display = QUIET; break; case 's': grep_silent = 1; break; case 'v': grep_inverse = 1; break; case 'x': grep_exact = 1; break; default: return 2; } } if (head == NULL) { if (optind > argc - 1) { perror("grep: missing operands\n"); return 2; } head = grep_add_list(head, argv[optind++]); } if (type != FIXED) { for (struct grep_list *c = head; c != NULL; c = c->next) { int e = regcomp(&(c->re), c->string, flags); if (e == 0) { free(c->string); c->string = NULL; continue; } char err[512]; regerror(e, &(c->re), err, sizeof(err)); fprintf(stderr, "grep: %s: %s\n", c->string, err); return 2; } } grep_filenames = (argc > optind + 1); int ret = 1; do { int r = grep(head, argv[optind++]); if (r == 2) { ret = 2; } if (r == 0 && ret == 1) { ret = 0; } } while (optind < argc); return ret; }