diff options
Diffstat (limited to 'grep.c')
| -rw-r--r-- | grep.c | 17 |
1 files changed, 12 insertions, 5 deletions
@@ -41,8 +41,8 @@ struct grep_list { enum { NORMAL, COUNT, LIST, QUIET } grep_display = NORMAL; static int grep_filenames = 0; +static int grep_linenumbers = 0; -#define LINENUMBERS 1 << 1 #define SUPPRESS 1 << 2 #define INVERT 1 << 3 #define WHOLELINE 1 << 4 @@ -84,13 +84,21 @@ static uintmax_t grep(struct grep_list *head, const char *path, int flags) 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++; @@ -200,11 +208,11 @@ int main(int argc, char *argv[]) break; case 'n': - //flags |= LINENUMBERS; + grep_linenumbers = 1; break; case 'q': - //display = QUIET; + grep_display = QUIET; break; case 's': @@ -248,8 +256,7 @@ int main(int argc, char *argv[]) } } - printf("argc %d, optind %d\n", argc, optind); - grep_filenames = (argc - optind < 2); + grep_filenames = (argc > optind + 1); uintmax_t found = 0; do { |
