/* * 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 #include #include struct grep_list { struct grep_list *next; char *string; regex_t re; }; #ifndef ARG_MAX #define ARG_MAX _POSIX_ARG_MAX #endif #define BASIC 0 #define EXTENDED 1 #define FIXED 2 #define NORMAL 0 #define COUNT 1 #define LIST 2 #define QUIET 3 #define IGNORECASE 1 << 0 #define LINENUMBERS 1 << 1 #define SUPPRESS 1 << 2 #define INVERT 1 << 3 #define WHOLELINE 1 << 4 #define FILENAMES 1 << 5 #if 0 static int grep(int patterns, char **p, int type, int display, int flags, char *path) { FILE *f = stdin; char *buf = NULL; int line = 0; size_t len = 0; ssize_t nread = 0; regmatch_t pmatch[1]; regex_t re; int i; int count = 0; struct stat st; stat(path, &st); if (S_ISDIR(st.st_mode)) { return 0; } if (strcmp("-", path)) { f = fopen(path, "r"); } if (f == NULL) { if (!(flags & SUPPRESS)) { perror(path); } return 0; } while ((nread = getline(&buf, &len, f)) != -1) { line++; for (i = 0; i < patterns; i++) { // FIXME: fixed pattern if (regcomp (&re, p[i], (type == BASIC ? 0 : REG_EXTENDED) | (flags & IGNORECASE ? REG_ICASE : 0)) != 0) { printf("Bad regex: %s\n", p[i]); exit(2); } else if (regexec(&re, buf, 1, pmatch, 0) == (flags & INVERT ? REG_NOMATCH : 0)) { if (!(flags & WHOLELINE) || (flags & WHOLELINE && pmatch[0].rm_so == 0 && pmatch[0].rm_eo == nread - 1)) { count++; if (NORMAL == display) { if (flags & FILENAMES) printf("%s:", path); if (flags & LINENUMBERS) printf("%d:", line); fwrite(buf, sizeof(char), nread, stdout); } } } regfree(&re); } if (buf != NULL) { free(buf); buf = NULL; } len = 0; } if (strcmp("-", path)) { fclose(f); } if (count > 0 && LIST == display) { if (strcmp("-", path)) { printf("%s\n", path); } else { printf("(standard input)\n"); } } else if (COUNT == display) { if (flags & FILENAMES) { printf("%s:", path); } printf("%d\n", count); } return count; } #endif 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 c; int flags = REG_NOSUB; struct grep_list *head = NULL; while ((c = getopt(argc, argv, "EFce:f:ilnqsvx")) != -1) { switch (c) { case 'E': flags |= REG_EXTENDED; break; case 'F': //type = FIXED; // FIXME break; case 'c': //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': //display = LIST; break; case 'n': //flags |= LINENUMBERS; break; case 'q': //display = QUIET; break; case 's': //flags |= SUPPRESS; break; case 'v': //flags |= INVERT; break; case 'x': //flags |= WHOLELINE; 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++]); } for (struct grep_list *c = head; c != NULL; c = c->next) { printf("pattern '%s'\n", c->string); } }