Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ex26/logfind.5/.logfind
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.c
*.h
Makefile
fail.txt
nonewline.txt
1 change: 1 addition & 0 deletions ex26/logfind.5/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ all: logfind
./logfind MAX_LINE
./logfind error MAX LINE
./logfind -o error MAX LINE
./logfind find me

clean:
rm -f logfind
3 changes: 3 additions & 0 deletions ex26/logfind.5/fail.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
find
me
please
13 changes: 4 additions & 9 deletions ex26/logfind.5/logfind.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ const size_t MAX_LINE = 1024;

int list_files(glob_t *pglob)
{
char *line = calloc(MAX_LINE, 1);
char line[MAX_LINE];
FILE *file = fopen(".logfind", "r");
int glob_flags = GLOB_TILDE;
int i = 0;
int rc = -1;

check(pglob != NULL, "Invalid glob_t given.");
check_mem(line);
check(file, "Failed to open .logfind. Make that first.");

while(fgets(line, MAX_LINE-1, file) != NULL) {
while(fgets(line, MAX_LINE, file) != NULL) {
line[strlen(line) - 1] = '\0'; // drop the \n ending
debug("Globbing %s", line);

Expand All @@ -38,7 +37,6 @@ int list_files(glob_t *pglob)
rc = 0; // all good

error: // fallthrough
if(line) free(line);
return rc;
}

Expand All @@ -57,16 +55,15 @@ int found_it(int use_or, int found_count, int search_len)

int scan_file(const char *filename, int use_or, int search_len, char *search_for[])
{
char *line = calloc(MAX_LINE, 1);
char line[MAX_LINE];
FILE *file = fopen(filename, "r");
int found_count = 0;
int i = 0;

check_mem(line);
check(file, "Failed to open file: %s", filename);

// read each line of the file and search that line for the contents
while(fgets(line, MAX_LINE-1, file) != NULL)
while(fgets(line, MAX_LINE, file) != NULL)
{
for(i = 0; i < search_len; i++) {
if(strcasestr(line, search_for[i]) != NULL) {
Expand All @@ -84,12 +81,10 @@ int scan_file(const char *filename, int use_or, int search_len, char *search_for
}


free(line);
fclose(file);
return 0;

error:
if(line) free(line);
if(file) fclose(file);

return -1;
Expand Down