int main(int argc, char *argv[]) { char *w, *s[50]; int n_case; get_int(&n_case); for (int i_case = 0; i_case < n_case; i_case++) { w = malloc(MAX_STR_LEN + 1); get_string(w); int n_str; get_int(&n_str); for (int i = 0; i < n_str; i++) { char tmp_str[MAX_STR_LEN + 1]; get_string(tmp_str); int j; for (j = i; (j > 0) && (strcmp(s[j - 1], tmp_str) > 0); j--) s[j] = s[j - 1]; s[j] = malloc(MAX_STR_LEN + 1); strcpy(s[j], tmp_str); } for (int i = 0; i < n_str; i++) if (is_matched(w, s[i])) printf("%s\n", s[i]); for (int i = 0; i < n_str; i++) free(s[i]); free(w); } return 0; }
static int grep(const char *find, const char *file) { char line_buf[MAX_LINE_BUF_SIZE]; FILE *fp; if( find == NULL || find[0] == 0 || file == NULL || file[0] == 0) return 1; if(!is_can_grepfile(file)) return 0; if((fp = fopen(file, "r")) == NULL) { fprintf(stderr, "fopen [%s] error [%s]\n", file, strerror(errno)); return 1; } while(fgets(line_buf, MAX_LINE_BUF_SIZE, fp) != NULL) { if(is_matched(find, line_buf)) printf("%s:\t%s", file, line_buf); } if(ferror(fp)) { fprintf(stderr, "fgets [%s] error [%s]\n", file, strerror(errno)); return 1; } return 0; }