fixed grep
This commit is contained in:
parent
f26953c707
commit
c4f4bf3ca3
24
src/grep.c
24
src/grep.c
@ -54,6 +54,7 @@ static int addpattern(const char *str, const size_t size) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char reg_error_flag = 0;
|
||||||
struct pattern **bckp = realloc(ptrns, sizeof(struct pattern *) * (ptrns_size + 1));
|
struct pattern **bckp = realloc(ptrns, sizeof(struct pattern *) * (ptrns_size + 1));
|
||||||
if (bckp == NULL)
|
if (bckp == NULL)
|
||||||
goto ADDP_ERROR;
|
goto ADDP_ERROR;
|
||||||
@ -89,25 +90,25 @@ static int addpattern(const char *str, const size_t size) {
|
|||||||
else if (w_flag)
|
else if (w_flag)
|
||||||
snprintf(reg_str, rs_size + 4, "%s\\<%s%.*s%s\\>%s", (bol) ? "^" : "", (E_flag) ? "(" : "\\(", (int)size - bol - eol, ptrns[ptrns_size]->str + bol, (E_flag) ? ")" : "\\)", (eol) ? "$" : "");
|
snprintf(reg_str, rs_size + 4, "%s\\<%s%.*s%s\\>%s", (bol) ? "^" : "", (E_flag) ? "(" : "\\(", (int)size - bol - eol, ptrns[ptrns_size]->str + bol, (E_flag) ? ")" : "\\)", (eol) ? "$" : "");
|
||||||
|
|
||||||
char flag = 0;
|
if (regcomp(&ptrns[ptrns_size]->reg, reg_str, reg_flag) != 0)
|
||||||
if (regcomp(&ptrns[ptrns_size]->reg, reg_str, reg_flag) < 0)
|
reg_error_flag = 1;
|
||||||
flag = 1;
|
|
||||||
|
|
||||||
ptrns[ptrns_size]->reg_set = 1;
|
ptrns[ptrns_size]->reg_set = 1;
|
||||||
if (x_flag || w_flag)
|
if (x_flag || w_flag)
|
||||||
free(reg_str);
|
free(reg_str);
|
||||||
|
|
||||||
if (flag)
|
|
||||||
goto ADDP_ERROR;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (reg_error_flag)
|
||||||
|
goto ADDP_ERROR;
|
||||||
|
|
||||||
ptrns_size++;
|
ptrns_size++;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
ADDP_ERROR:
|
ADDP_ERROR:
|
||||||
ptrns_size++;
|
ptrns_size++;
|
||||||
free_patterns();
|
free_patterns();
|
||||||
fprintf(stderr, "grep: %s\n", strerror(errno));
|
fprintf(stderr, "grep: %s\n", (reg_error_flag) ? "bad regex" : strerror(errno));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,12 +142,21 @@ static int addpattern_file(const char *file) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char *my_strcasestr(const char *s1, const char *s2) {
|
||||||
|
size_t len = strlen(s2);
|
||||||
|
for (; *s1; s1++)
|
||||||
|
if (!strncasecmp(s1, s2, len))
|
||||||
|
return (char *)s1;
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
static int cmp(const char *str1, const char *str2) {
|
static int cmp(const char *str1, const char *str2) {
|
||||||
if (x_flag)
|
if (x_flag)
|
||||||
return !((i_flag) ? strcasecmp : strcmp)(str1, str2);
|
return !((i_flag) ? strcasecmp : strcmp)(str1, str2);
|
||||||
|
|
||||||
else
|
else
|
||||||
return ((i_flag) ? strcasestr : strstr)(str1, str2) != NULL;
|
return ((i_flag) ? my_strcasestr : strstr)(str1, str2) != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int grep(FILE *fp, const char *file) {
|
static int grep(FILE *fp, const char *file) {
|
||||||
|
Loading…
Reference in New Issue
Block a user