Skip to content
Closed
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
45 changes: 27 additions & 18 deletions src/cobra_prim.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// clang-format off
/*
* This file is part of the public release of Cobra. It is subject to the
* terms in the License file that is included in this source directory.
Expand Down Expand Up @@ -138,29 +139,37 @@ fhash(const char *v)
}

void
dogrep(const char *s)
dogrep(const char *search_str)
{ Files *f;
int n;
char cmd[MAXYYTEXT];

for (n = 0; n < NHASH; n++)
for (f = files[n]; f; f = f->nxt)
{ if (strlen(s)
+ strlen(f->s)
+ strlen("grep -n -e -q \"\"")
+ 1 >= sizeof(cmd))
{ printf("search pattern too long\n");
return;
}
snprintf(cmd, sizeof(cmd), "grep -q -n -e \"%s\" %s",
s, f->s);
if (system(cmd) == 0) // dogrep
{ printf("%s:\n", f->s);
const char* pattern = "grep -n -e -q ";
const char* pattern_noline = "grep -e -q ";

int search_str_len = strlen(search_str);
int pattern_len = strlen(pattern);

for (n = 0; n < NHASH; n++) {
for (f = files[n]; f; f = f->nxt) {
// Accounts for the size of the quotes around the search pattern (2)
int cmd_size = search_str_len + pattern_len + strlen(f->s) + 2 + 1;
if (cmd_size > sizeof(cmd)) {
printf("search pattern too long\n");
return;
}

snprintf(cmd, sizeof(cmd), "%s\"%s\" %s", pattern, search_str, f->s);
if (system(cmd) == 0) { // dogrep
printf("%s:\n", f->s);
}

snprintf(cmd, sizeof(cmd), "%s\"%s\" %s", pattern_noline, search_str, f->s);
if (system(cmd) < 0) { // dogrep
printf("cmd '%s' failed\n", cmd);
}
}
cmd[5] = cmd[6] = ' ';
if (system(cmd) < 0) // dogrep
{ printf("cmd '%s' failed\n", cmd);
} }
}
}

char *
Expand Down