Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wildcard auditing #178

Merged
merged 6 commits into from
Dec 27, 2017
Merged
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
32 changes: 30 additions & 2 deletions grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,38 @@ int countlineswith(const char* str, const char* fname)
}

// Search through file
// getline reads a string from the specified file up to either a newline character or EOF
// getline reads a string from the specified file up to either a
// newline character or EOF
while(getline(&buffer, &size, fp) != -1)
if((strstr(buffer, str)) != NULL)
{
// Strip potential newline character at the end of line we just read
if(buffer[strlen(buffer)-1] == '\n')
buffer[strlen(buffer)-1] = '\0';

// Search for exact match
if(strcmp(buffer, str) == 0)
{
found++;
continue;
}

// If line starts with *, search for partial match of
// needle "buffer+1" in haystack "str"
if(buffer[0] == '*')
{
char * buf = strstr(str, buffer+1);
// The strstr() function finds the first occurrence of
// the substring buffer+1 in the string str.
// These functions return a pointer to the beginning of
// the located substring, or NULL if the substring is not
// found. Hence, we compare the length of the substring to
// the wildcard entry to rule out the possiblity that
// there is anything behind the wildcard. This avoids that given
// "*example.com" "example.com.xxxxx" would also match.
if(buf != NULL && strlen(buf) == strlen(buffer+1))
found++;
}
}

// Free allocated memory
if(buffer != NULL)
Expand Down