Print this page
3737 grep does not support -H option


  90                             &buf[fw_lBufsiz + BUFSIZ] - p, fptr))\
  91                                 <= 0) break;                         \
  92                 } else if ((ccount = fread(p,                \
  93                         sizeof (char),  BUFSIZ, fptr)) <= 0)     \
  94                         break;                                   \
  95                 blkno += (long long)ccount;                  \
  96         }                                                \
  97         ccount -= cw;                                    \
  98         while (cw--)                                     \
  99                 lc = (lc << 7) | ((*p++) & 0177)
 100 
 101 /*
 102  * The same() macro and letter() function were inserted to allow for
 103  * the -i option work for the multi-byte environment.
 104  */
 105 wchar_t letter();
 106 #define same(a, b) \
 107         (a == b || iflag && (!MULTI_BYTE || ISASCII(a)) && (a ^ b) == ' ' && \
 108         letter(a) == letter(b))
 109 

 110 
 111 #define QSIZE 400
 112 struct words {
 113         wchar_t inp;
 114         char    out;
 115         struct  words *nst;
 116         struct  words *link;
 117         struct  words *fail;
 118 } *w = NULL, *smax, *q;
 119 
 120 FILE *fptr;
 121 long long lnum;
 122 int     bflag, cflag, lflag, fflag, nflag, vflag, xflag, eflag, sflag;
 123 int     hflag, iflag;
 124 int     retcode = 0;
 125 int     nfile;
 126 long long blkno;
 127 int     nsucc;
 128 long long tln;
 129 FILE    *wordf;
 130 char    *argptr;
 131 off_t input_size = 0;
 132 
 133 void    execute(char *);
 134 void    cgotofn(void);
 135 void    overflo(void);
 136 void    cfail(void);
 137 
 138 static long fw_lBufsiz = 0;
 139 
 140 int
 141 main(int argc, char **argv)
 142 {
 143         int c;
 144         int errflg = 0;
 145         struct stat file_stat;
 146 
 147         (void) setlocale(LC_ALL, "");
 148 #if !defined(TEXT_DOMAIN)       /* Should be defined by cc -D */
 149 #define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
 150 #endif
 151         (void) textdomain(TEXT_DOMAIN);
 152 
 153         while ((c = getopt(argc, argv, "hybcie:f:lnvxs")) != EOF)
 154                 switch (c) {
 155 
 156                 case 's':
 157                         sflag++;





 158                         continue;
 159                 case 'h':
 160                         hflag++;

 161                         continue;
 162                 case 'b':
 163                         bflag++;
 164                         continue;
 165 
 166                 case 'i':
 167                 case 'y':
 168                         iflag++;
 169                         continue;
 170 
 171                 case 'c':
 172                         cflag++;
 173                         continue;
 174 
 175                 case 'e':
 176                         eflag++;
 177                         argptr = optarg;
 178                         input_size = strlen(argptr);
 179                         continue;
 180 


 204                         continue;
 205 
 206                 case 'n':
 207                         nflag++;
 208                         continue;
 209 
 210                 case 'v':
 211                         vflag++;
 212                         continue;
 213 
 214                 case 'x':
 215                         xflag++;
 216                         continue;
 217 
 218                 case '?':
 219                         errflg++;
 220         }
 221 
 222         argc -= optind;
 223         if (errflg || ((argc <= 0) && !fflag && !eflag)) {
 224                 (void) printf(gettext("usage: fgrep [ -bchilnsvx ] "
 225                         "[ -e exp ] [ -f file ] [ strings ] [ file ] ...\n"));
 226                 exit(2);
 227         }
 228         if (!eflag && !fflag) {
 229                 argptr = argv[optind];
 230                 input_size = strlen(argptr);
 231                 input_size++;
 232                 optind++;
 233                 argc--;
 234         }
 235 
 236 /*
 237  * Normally we need one struct words for each letter in the pattern
 238  * plus one terminating struct words with outp = 1, but when -x option
 239  * is specified we require one more struct words for `\n` character so we
 240  * calculate the input_size as below. We add extra 1 because
 241  * (input_size/2) rounds off odd numbers
 242  */
 243 
 244         if (xflag) {


 296         int failed;
 297         char *nlp;
 298         wchar_t lc;
 299         int cw;
 300 
 301         if (buf == NULL) {
 302                 fw_lBufsiz = BUFSIZ;
 303                 if ((buf = malloc(fw_lBufsiz + BUFSIZ)) == NULL) {
 304                         exit(2); /* out of memory */
 305                 }
 306         }
 307 
 308         if (file) {
 309                 if ((fptr = fopen(file, "r")) == NULL) {
 310                         (void) fprintf(stderr,
 311                                 gettext("fgrep: can't open %s\n"), file);
 312                         retcode = 2;
 313                         return;
 314                 }
 315         } else {
 316                 file = "<stdin>";
 317                 fptr = stdin;

 318         }
 319         ccount = 0;
 320         failed = 0;
 321         lnum = 1;
 322         tln = 0;
 323         blkno = 0;
 324         p = buf;
 325         nlp = p;
 326         c = w;
 327         for (;;) {
 328                 if (c == 0)
 329                         break;
 330                 if (ccount <= 0) {
 331                         if (p >= &buf[fw_lBufsiz + BUFSIZ]) {
 332                                 if (nlp == buf) {
 333                                         /* increase the buffer size */
 334                                         fw_lBufsiz += BUFSIZ;
 335                                         if ((buf = realloc(buf,
 336                                                 fw_lBufsiz + BUFSIZ)) == NULL) {
 337                                                 exit(2); /* out of memory */


 400                 /* shift buffer down */
 401                 (void) memmove(buf, nlp, &buf[fw_lBufsiz + BUFSIZ] - nlp);
 402                 p -= nlp - buf;
 403                 nlp = buf;
 404         }
 405 }
 406 if (p > &buf[fw_lBufsiz]) {
 407         if ((ccount = fread(p, sizeof (char),
 408                 &buf[fw_lBufsiz + BUFSIZ] - p, fptr)) <= 0) break;
 409         } else if ((ccount = fread(p, sizeof (char), BUFSIZ,
 410                 fptr)) <= 0) break;
 411                 blkno += (long long)ccount;
 412         }
 413         GETONE(lc, p);
 414 }
 415                         if ((vflag && (failed == 0 || xflag == 0)) ||
 416                                 (vflag == 0 && xflag && failed))
 417                                 goto nomatch;
 418 succeed:
 419                         nsucc = 1;
 420                         if (cflag)
 421                                 tln++;
 422                         else if (lflag && !sflag) {
 423                                 (void) printf("%s\n", file);
 424                                 (void) fclose(fptr);
 425                                 return;
 426                         } else if (!sflag) {
 427                                 if (nfile > 1 && !hflag)



 428                                         (void) printf("%s:", file);
 429                                 if (bflag)
 430                                         (void) printf("%lld:",
 431                                                 (blkno - (long long)(ccount-1))
 432                                                 / BUFSIZ);
 433                                 if (nflag)
 434                                         (void) printf("%lld:", lnum);
 435                                 if (p <= nlp) {
 436                                         while (nlp < &buf[fw_lBufsiz + BUFSIZ])
 437                                                 (void) putchar(*nlp++);
 438                                         nlp = buf;
 439                                 }
 440                                 while (nlp < p)
 441                                         (void) putchar(*nlp++);
 442                         }
 443 nomatch:
 444                         lnum++;
 445                         nlp = p;
 446                         c = w;
 447                         failed = 0;
 448                         continue;
 449                 }
 450                 if (lc == '\n')
 451                         if (vflag)
 452                                 goto succeed;
 453                         else {
 454                                 lnum++;
 455                                 nlp = p;
 456                                 c = w;
 457                                 failed = 0;
 458                         }
 459         }
 460         (void) fclose(fptr);
 461         if (cflag) {
 462                 if ((nfile > 1) && !hflag)
 463                         (void) printf("%s:", file);
 464                 (void) printf("%lld\n", tln);
 465         }
 466 }
 467 
 468 
 469 wchar_t
 470 getargc(void)
 471 {
 472         /* appends a newline to shell quoted argument list so */
 473         /* the list looks like it came from an ed style file  */
 474         wchar_t c;
 475         int cw;
 476         int b;
 477         static int endflg;
 478 
 479 
 480         if (wordf) {
 481                 if ((b = getc(wordf)) == EOF)
 482                         return (EOF);




  90                             &buf[fw_lBufsiz + BUFSIZ] - p, fptr))\
  91                                 <= 0) break;                         \
  92                 } else if ((ccount = fread(p,                \
  93                         sizeof (char),  BUFSIZ, fptr)) <= 0)     \
  94                         break;                                   \
  95                 blkno += (long long)ccount;                  \
  96         }                                                \
  97         ccount -= cw;                                    \
  98         while (cw--)                                     \
  99                 lc = (lc << 7) | ((*p++) & 0177)
 100 
 101 /*
 102  * The same() macro and letter() function were inserted to allow for
 103  * the -i option work for the multi-byte environment.
 104  */
 105 wchar_t letter();
 106 #define same(a, b) \
 107         (a == b || iflag && (!MULTI_BYTE || ISASCII(a)) && (a ^ b) == ' ' && \
 108         letter(a) == letter(b))
 109 
 110 #define STDIN_FILENAME gettext("(standard input)")
 111 
 112 #define QSIZE 400
 113 struct words {
 114         wchar_t inp;
 115         char    out;
 116         struct  words *nst;
 117         struct  words *link;
 118         struct  words *fail;
 119 } *w = NULL, *smax, *q;
 120 
 121 FILE *fptr;
 122 long long lnum;
 123 int     bflag, cflag, lflag, fflag, nflag, vflag, xflag, eflag, qflag;
 124 int     Hflag, hflag, iflag;
 125 int     retcode = 0;
 126 int     nfile;
 127 long long blkno;
 128 int     nsucc;
 129 long long tln;
 130 FILE    *wordf;
 131 char    *argptr;
 132 off_t input_size = 0;
 133 
 134 void    execute(char *);
 135 void    cgotofn(void);
 136 void    overflo(void);
 137 void    cfail(void);
 138 
 139 static long fw_lBufsiz = 0;
 140 
 141 int
 142 main(int argc, char **argv)
 143 {
 144         int c;
 145         int errflg = 0;
 146         struct stat file_stat;
 147 
 148         (void) setlocale(LC_ALL, "");
 149 #if !defined(TEXT_DOMAIN)       /* Should be defined by cc -D */
 150 #define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
 151 #endif
 152         (void) textdomain(TEXT_DOMAIN);
 153 
 154         while ((c = getopt(argc, argv, "Hhybcie:f:lnvxqs")) != EOF)
 155                 switch (c) {
 156 
 157                 case 'q':
 158                 case 's': /* Solaris: legacy option */
 159                         qflag++;
 160                         continue;
 161                 case 'H':
 162                         Hflag++;
 163                         hflag = 0;
 164                         continue;
 165                 case 'h':
 166                         hflag++;
 167                         Hflag = 0;
 168                         continue;
 169                 case 'b':
 170                         bflag++;
 171                         continue;
 172 
 173                 case 'i':
 174                 case 'y':
 175                         iflag++;
 176                         continue;
 177 
 178                 case 'c':
 179                         cflag++;
 180                         continue;
 181 
 182                 case 'e':
 183                         eflag++;
 184                         argptr = optarg;
 185                         input_size = strlen(argptr);
 186                         continue;
 187 


 211                         continue;
 212 
 213                 case 'n':
 214                         nflag++;
 215                         continue;
 216 
 217                 case 'v':
 218                         vflag++;
 219                         continue;
 220 
 221                 case 'x':
 222                         xflag++;
 223                         continue;
 224 
 225                 case '?':
 226                         errflg++;
 227         }
 228 
 229         argc -= optind;
 230         if (errflg || ((argc <= 0) && !fflag && !eflag)) {
 231                 (void) printf(gettext("usage: fgrep [ -bcHhilnqsvx ] "
 232                         "[ -e exp ] [ -f file ] [ strings ] [ file ] ...\n"));
 233                 exit(2);
 234         }
 235         if (!eflag && !fflag) {
 236                 argptr = argv[optind];
 237                 input_size = strlen(argptr);
 238                 input_size++;
 239                 optind++;
 240                 argc--;
 241         }
 242 
 243 /*
 244  * Normally we need one struct words for each letter in the pattern
 245  * plus one terminating struct words with outp = 1, but when -x option
 246  * is specified we require one more struct words for `\n` character so we
 247  * calculate the input_size as below. We add extra 1 because
 248  * (input_size/2) rounds off odd numbers
 249  */
 250 
 251         if (xflag) {


 303         int failed;
 304         char *nlp;
 305         wchar_t lc;
 306         int cw;
 307 
 308         if (buf == NULL) {
 309                 fw_lBufsiz = BUFSIZ;
 310                 if ((buf = malloc(fw_lBufsiz + BUFSIZ)) == NULL) {
 311                         exit(2); /* out of memory */
 312                 }
 313         }
 314 
 315         if (file) {
 316                 if ((fptr = fopen(file, "r")) == NULL) {
 317                         (void) fprintf(stderr,
 318                                 gettext("fgrep: can't open %s\n"), file);
 319                         retcode = 2;
 320                         return;
 321                 }
 322         } else {

 323                 fptr = stdin;
 324                 file = STDIN_FILENAME;
 325         }
 326         ccount = 0;
 327         failed = 0;
 328         lnum = 1;
 329         tln = 0;
 330         blkno = 0;
 331         p = buf;
 332         nlp = p;
 333         c = w;
 334         for (;;) {
 335                 if (c == 0)
 336                         break;
 337                 if (ccount <= 0) {
 338                         if (p >= &buf[fw_lBufsiz + BUFSIZ]) {
 339                                 if (nlp == buf) {
 340                                         /* increase the buffer size */
 341                                         fw_lBufsiz += BUFSIZ;
 342                                         if ((buf = realloc(buf,
 343                                                 fw_lBufsiz + BUFSIZ)) == NULL) {
 344                                                 exit(2); /* out of memory */


 407                 /* shift buffer down */
 408                 (void) memmove(buf, nlp, &buf[fw_lBufsiz + BUFSIZ] - nlp);
 409                 p -= nlp - buf;
 410                 nlp = buf;
 411         }
 412 }
 413 if (p > &buf[fw_lBufsiz]) {
 414         if ((ccount = fread(p, sizeof (char),
 415                 &buf[fw_lBufsiz + BUFSIZ] - p, fptr)) <= 0) break;
 416         } else if ((ccount = fread(p, sizeof (char), BUFSIZ,
 417                 fptr)) <= 0) break;
 418                 blkno += (long long)ccount;
 419         }
 420         GETONE(lc, p);
 421 }
 422                         if ((vflag && (failed == 0 || xflag == 0)) ||
 423                                 (vflag == 0 && xflag && failed))
 424                                 goto nomatch;
 425 succeed:
 426                         nsucc = 1;
 427                         if (lflag || qflag) {
 428                                 if (!qflag)

 429                                         (void) printf("%s\n", file);
 430                                 (void) fclose(fptr);
 431                                 return;
 432                         }
 433                         if (cflag) {
 434                                 tln++;
 435                         } else {
 436                                 if (Hflag || (nfile > 1 && !hflag))
 437                                         (void) printf("%s:", file);
 438                                 if (bflag)
 439                                         (void) printf("%lld:",
 440                                                 (blkno - (long long)(ccount-1))
 441                                                 / BUFSIZ);
 442                                 if (nflag)
 443                                         (void) printf("%lld:", lnum);
 444                                 if (p <= nlp) {
 445                                         while (nlp < &buf[fw_lBufsiz + BUFSIZ])
 446                                                 (void) putchar(*nlp++);
 447                                         nlp = buf;
 448                                 }
 449                                 while (nlp < p)
 450                                         (void) putchar(*nlp++);
 451                         }
 452 nomatch:
 453                         lnum++;
 454                         nlp = p;
 455                         c = w;
 456                         failed = 0;
 457                         continue;
 458                 }
 459                 if (lc == '\n')
 460                         if (vflag)
 461                                 goto succeed;
 462                         else {
 463                                 lnum++;
 464                                 nlp = p;
 465                                 c = w;
 466                                 failed = 0;
 467                         }
 468         }
 469         (void) fclose(fptr);
 470         if (cflag && !qflag) {
 471                 if (Hflag || (nfile > 1 && !hflag))
 472                         (void) printf("%s:", file);
 473                 (void) printf("%lld\n", tln);
 474         }
 475 }
 476 
 477 
 478 wchar_t
 479 getargc(void)
 480 {
 481         /* appends a newline to shell quoted argument list so */
 482         /* the list looks like it came from an ed style file  */
 483         wchar_t c;
 484         int cw;
 485         int b;
 486         static int endflg;
 487 
 488 
 489         if (wordf) {
 490                 if ((b = getc(wordf)) == EOF)
 491                         return (EOF);