Print this page
3737 grep does not support -H option

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/fgrep/fgrep.c
          +++ new/usr/src/cmd/fgrep/fgrep.c
↓ open down ↓ 22 lines elided ↑ open up ↑
  23   23   * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
  24   24   * Use is subject to license terms.
  25   25   */
  26   26  
  27   27  /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
  28   28  /*        All Rights Reserved   */
  29   29  
  30   30  /*      Copyright (c) 1987, 1988 Microsoft Corporation  */
  31   31  /*        All Rights Reserved   */
  32   32  
  33      -#pragma ident   "%Z%%M% %I%     %E% SMI"
       33 +/*
       34 + * Copyright 2013 Damian Bogel. All rights reserved.
       35 + */
  34   36  
  35   37  /*
  36   38   * fgrep -- print all lines containing any of a set of keywords
  37   39   *
  38   40   *      status returns:
  39   41   *              0 - ok, and some matches
  40   42   *              1 - ok, but no matches
  41   43   *              2 - some error
  42   44   */
  43   45  
↓ open down ↓ 56 lines elided ↑ open up ↑
 100  102  
 101  103  /*
 102  104   * The same() macro and letter() function were inserted to allow for
 103  105   * the -i option work for the multi-byte environment.
 104  106   */
 105  107  wchar_t letter();
 106  108  #define same(a, b) \
 107  109          (a == b || iflag && (!MULTI_BYTE || ISASCII(a)) && (a ^ b) == ' ' && \
 108  110          letter(a) == letter(b))
 109  111  
      112 +#define STDIN_FILENAME gettext("(standard input)")
 110  113  
 111  114  #define QSIZE 400
 112  115  struct words {
 113  116          wchar_t inp;
 114  117          char    out;
 115  118          struct  words *nst;
 116  119          struct  words *link;
 117  120          struct  words *fail;
 118  121  } *w = NULL, *smax, *q;
 119  122  
 120  123  FILE *fptr;
 121  124  long long lnum;
 122      -int     bflag, cflag, lflag, fflag, nflag, vflag, xflag, eflag, sflag;
 123      -int     hflag, iflag;
      125 +int     bflag, cflag, lflag, fflag, nflag, vflag, xflag, eflag, qflag;
      126 +int     Hflag, hflag, iflag;
 124  127  int     retcode = 0;
 125  128  int     nfile;
 126  129  long long blkno;
 127  130  int     nsucc;
 128  131  long long tln;
 129  132  FILE    *wordf;
 130  133  char    *argptr;
 131  134  off_t input_size = 0;
 132  135  
 133  136  void    execute(char *);
↓ open down ↓ 9 lines elided ↑ open up ↑
 143  146          int c;
 144  147          int errflg = 0;
 145  148          struct stat file_stat;
 146  149  
 147  150          (void) setlocale(LC_ALL, "");
 148  151  #if !defined(TEXT_DOMAIN)       /* Should be defined by cc -D */
 149  152  #define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
 150  153  #endif
 151  154          (void) textdomain(TEXT_DOMAIN);
 152  155  
 153      -        while ((c = getopt(argc, argv, "hybcie:f:lnvxs")) != EOF)
      156 +        while ((c = getopt(argc, argv, "Hhybcie:f:lnvxqs")) != EOF)
 154  157                  switch (c) {
 155  158  
 156      -                case 's':
 157      -                        sflag++;
      159 +                case 'q':
      160 +                case 's': /* Solaris: legacy option */
      161 +                        qflag++;
      162 +                        continue;
      163 +                case 'H':
      164 +                        Hflag++;
      165 +                        hflag = 0;
 158  166                          continue;
 159  167                  case 'h':
 160  168                          hflag++;
      169 +                        Hflag = 0;
 161  170                          continue;
 162  171                  case 'b':
 163  172                          bflag++;
 164  173                          continue;
 165  174  
 166  175                  case 'i':
 167  176                  case 'y':
 168  177                          iflag++;
 169  178                          continue;
 170  179  
↓ open down ↓ 43 lines elided ↑ open up ↑
 214  223                  case 'x':
 215  224                          xflag++;
 216  225                          continue;
 217  226  
 218  227                  case '?':
 219  228                          errflg++;
 220  229          }
 221  230  
 222  231          argc -= optind;
 223  232          if (errflg || ((argc <= 0) && !fflag && !eflag)) {
 224      -                (void) printf(gettext("usage: fgrep [ -bchilnsvx ] "
      233 +                (void) printf(gettext("usage: fgrep [ -bcHhilnqsvx ] "
 225  234                          "[ -e exp ] [ -f file ] [ strings ] [ file ] ...\n"));
 226  235                  exit(2);
 227  236          }
 228  237          if (!eflag && !fflag) {
 229  238                  argptr = argv[optind];
 230  239                  input_size = strlen(argptr);
 231  240                  input_size++;
 232  241                  optind++;
 233  242                  argc--;
 234  243          }
↓ open down ↓ 71 lines elided ↑ open up ↑
 306  315          }
 307  316  
 308  317          if (file) {
 309  318                  if ((fptr = fopen(file, "r")) == NULL) {
 310  319                          (void) fprintf(stderr,
 311  320                                  gettext("fgrep: can't open %s\n"), file);
 312  321                          retcode = 2;
 313  322                          return;
 314  323                  }
 315  324          } else {
 316      -                file = "<stdin>";
 317  325                  fptr = stdin;
      326 +                file = STDIN_FILENAME;
 318  327          }
 319  328          ccount = 0;
 320  329          failed = 0;
 321  330          lnum = 1;
 322  331          tln = 0;
 323  332          blkno = 0;
 324  333          p = buf;
 325  334          nlp = p;
 326  335          c = w;
 327  336          for (;;) {
↓ open down ↓ 82 lines elided ↑ open up ↑
 410  419                  fptr)) <= 0) break;
 411  420                  blkno += (long long)ccount;
 412  421          }
 413  422          GETONE(lc, p);
 414  423  }
 415  424                          if ((vflag && (failed == 0 || xflag == 0)) ||
 416  425                                  (vflag == 0 && xflag && failed))
 417  426                                  goto nomatch;
 418  427  succeed:
 419  428                          nsucc = 1;
 420      -                        if (cflag)
 421      -                                tln++;
 422      -                        else if (lflag && !sflag) {
 423      -                                (void) printf("%s\n", file);
      429 +                        if (lflag || qflag) {
      430 +                                if (!qflag)
      431 +                                        (void) printf("%s\n", file);
 424  432                                  (void) fclose(fptr);
 425  433                                  return;
 426      -                        } else if (!sflag) {
 427      -                                if (nfile > 1 && !hflag)
      434 +                        }
      435 +                        if (cflag) {
      436 +                                tln++;
      437 +                        } else {
      438 +                                if (Hflag || (nfile > 1 && !hflag))
 428  439                                          (void) printf("%s:", file);
 429  440                                  if (bflag)
 430  441                                          (void) printf("%lld:",
 431  442                                                  (blkno - (long long)(ccount-1))
 432  443                                                  / BUFSIZ);
 433  444                                  if (nflag)
 434  445                                          (void) printf("%lld:", lnum);
 435  446                                  if (p <= nlp) {
 436  447                                          while (nlp < &buf[fw_lBufsiz + BUFSIZ])
 437  448                                                  (void) putchar(*nlp++);
↓ open down ↓ 13 lines elided ↑ open up ↑
 451  462                          if (vflag)
 452  463                                  goto succeed;
 453  464                          else {
 454  465                                  lnum++;
 455  466                                  nlp = p;
 456  467                                  c = w;
 457  468                                  failed = 0;
 458  469                          }
 459  470          }
 460  471          (void) fclose(fptr);
 461      -        if (cflag) {
 462      -                if ((nfile > 1) && !hflag)
      472 +        if (cflag && !qflag) {
      473 +                if (Hflag || (nfile > 1 && !hflag))
 463  474                          (void) printf("%s:", file);
 464  475                  (void) printf("%lld\n", tln);
 465  476          }
 466  477  }
 467  478  
 468  479  
 469  480  wchar_t
 470  481  getargc(void)
 471  482  {
 472  483          /* appends a newline to shell quoted argument list so */
↓ open down ↓ 222 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX