😄
This commit is contained in:
104
A2/fauxgrep.c
104
A2/fauxgrep.c
@@ -16,67 +16,67 @@
|
||||
#include <err.h>
|
||||
|
||||
int fauxgrep_file(char const *needle, char const *path) {
|
||||
FILE *f = fopen(path, "r");
|
||||
FILE *f = fopen(path, "r");
|
||||
|
||||
if (f == NULL) {
|
||||
warn("failed to open %s", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *line = NULL;
|
||||
size_t linelen = 0;
|
||||
int lineno = 1;
|
||||
|
||||
while (getline(&line, &linelen, f) != -1) {
|
||||
if (strstr(line, needle) != NULL) {
|
||||
printf("%s:%d: %s", path, lineno, line);
|
||||
if (f == NULL) {
|
||||
warn("failed to open %s", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
lineno++;
|
||||
}
|
||||
char *line = NULL;
|
||||
size_t linelen = 0;
|
||||
int lineno = 1;
|
||||
|
||||
free(line);
|
||||
fclose(f);
|
||||
while (getline(&line, &linelen, f) != -1) {
|
||||
if (strstr(line, needle) != NULL) {
|
||||
printf("%s:%d: %s", path, lineno, line);
|
||||
}
|
||||
|
||||
return 0;
|
||||
lineno++;
|
||||
}
|
||||
|
||||
free(line);
|
||||
fclose(f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char * const *argv) {
|
||||
if (argc < 2) {
|
||||
err(1, "usage: STRING paths...");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char const *needle = argv[1];
|
||||
char * const *paths = &argv[2];
|
||||
|
||||
// FTS_LOGICAL = follow symbolic links
|
||||
// FTS_NOCHDIR = do not change the working directory of the process
|
||||
//
|
||||
// (These are not particularly important distinctions for our simple
|
||||
// uses.)
|
||||
int fts_options = FTS_LOGICAL | FTS_NOCHDIR;
|
||||
|
||||
FTS *ftsp;
|
||||
if ((ftsp = fts_open(paths, fts_options, NULL)) == NULL) {
|
||||
err(1, "fts_open() failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
FTSENT *p;
|
||||
while ((p = fts_read(ftsp)) != NULL) {
|
||||
switch (p->fts_info) {
|
||||
case FTS_D:
|
||||
break;
|
||||
case FTS_F:
|
||||
fauxgrep_file(needle, p->fts_path);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
if (argc < 2) {
|
||||
err(1, "usage: STRING paths...");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fts_close(ftsp);
|
||||
char const *needle = argv[1];
|
||||
char * const *paths = &argv[2];
|
||||
|
||||
return 0;
|
||||
// FTS_LOGICAL = follow symbolic links
|
||||
// FTS_NOCHDIR = do not change the working directory of the process
|
||||
//
|
||||
// (These are not particularly important distinctions for our simple
|
||||
// uses.)
|
||||
int fts_options = FTS_LOGICAL | FTS_NOCHDIR;
|
||||
|
||||
FTS *ftsp;
|
||||
if ((ftsp = fts_open(paths, fts_options, NULL)) == NULL) {
|
||||
err(1, "fts_open() failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
FTSENT *p;
|
||||
while ((p = fts_read(ftsp)) != NULL) {
|
||||
switch (p->fts_info) {
|
||||
case FTS_D:
|
||||
break;
|
||||
case FTS_F:
|
||||
fauxgrep_file(needle, p->fts_path);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fts_close(ftsp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user