Index: external/bsd/tre/dist/src/agrep.c =================================================================== RCS file: /cvsroot/src/external/bsd/tre/dist/src/agrep.c,v retrieving revision 1.4 diff -p -u -r1.4 agrep.c --- external/bsd/tre/dist/src/agrep.c 20 Aug 2020 15:54:11 -0000 1.4 +++ external/bsd/tre/dist/src/agrep.c 23 May 2024 20:44:57 -0000 @@ -179,6 +179,7 @@ static int next_delim_len; /* Length of static int delim_after = 1;/* If true, print the delimiter after the record. */ static int at_eof; static int have_matches; /* If true, matches have been found. */ +static int is_binary; /* -1 unknown, 0 ascii, 1 binary */ static int invert_match; /* Show only non-matching records. */ static int print_filename; /* Output filename. */ @@ -196,6 +197,12 @@ static int be_silent; /* Never outp static regaparams_t match_params; +static int +isbinaryfile(void) +{ + return buf != NULL && memchr(buf, '\0', data_len) != NULL; +} + /* The color string used with the --color option. If set, the environment variable GREP_COLOR overrides this default value. */ static const char *highlight = "01;31"; @@ -262,6 +269,9 @@ tre_agrep_get_next_record(int fd, const } data_len += r; next_record = buf; + + if (is_binary < 0) + is_binary = isbinaryfile(); } /* Find the next record delimiter. */ @@ -316,41 +326,6 @@ tre_agrep_get_next_record(int fd, const #include -static int -isbinaryfile(const char *filename) -{ - struct stat st; - size_t size; - size_t i; - char *mapped; - FILE *fp; - int isbin; - - if ((fp = fopen(filename, "r")) == NULL) { - return 1; - } - fstat(fileno(fp), &st); - isbin = 0; - if ((st.st_mode & S_IFMT) != S_IFREG) { - isbin = 1; - } else { - size = (size_t)st.st_size; - mapped = mmap(NULL, size, PROT_READ, MAP_SHARED, fileno(fp), 0); - if (mapped == MAP_FAILED) { - fclose(fp); - return 1; - } - for (i = 0 ; !isbin && i < size ; i++) { - if (mapped[i] == 0x0) { - isbin = 1; - } - } - munmap(mapped, size); - } - fclose(fp); - return isbin; -} - static int tre_agrep_handle_file(const char */*filename*/); static int @@ -398,6 +373,8 @@ tre_agrep_handle_file(const char *filena int count = 0; int recnum = 0; + is_binary = -1; + /* Allocate the initial buffer. */ if (buf == NULL) { @@ -488,10 +465,12 @@ tre_agrep_handle_file(const char *filena printf("%s\n", filename); break; } - else if (!count_matches && isbinaryfile(filename)) + else if (!count_matches && is_binary > 0) { if (print_filename) - printf("Binary file %s matches\n", filename); + printf("%s:", filename); + printf("Binary file matches\n"); + break; } else if (!count_matches) {