Print this page
3737 grep does not support -H option
3759 egrep(1) and fgrep(1) -s flag does not hide -c output
Reviewed by: Albert Lee <trisk@nexenta.com>
Reviewed by: Andy Stormont <andyjstormont@gmail.com>

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/grep/grep.c
          +++ new/usr/src/cmd/grep/grep.c
↓ open down ↓ 25 lines elided ↑ open up ↑
  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   33  /* Copyright 2012 Nexenta Systems, Inc.  All rights reserved. */
  34   34  
  35   35  /*
       36 + * Copyright 2013 Damian Bogel. All rights reserved.
       37 + */
       38 +
       39 +/*
  36   40   * grep -- print lines matching (or not matching) a pattern
  37   41   *
  38   42   *      status returns:
  39   43   *              0 - ok, and some matches
  40   44   *              1 - ok, but no matches
  41   45   *              2 - some error
  42   46   */
  43   47  
  44   48  #include <sys/types.h>
  45   49  
↓ open down ↓ 20 lines elided ↑ open up ↑
  66   70          "More than 2 numbers given in \\{ \\}.",
  67   71          "} expected after \\.",
  68   72          "First number exceeds second in \\{ \\}.",
  69   73          "[ ] imbalance.",
  70   74          "Regular expression overflow.",
  71   75          "Illegal byte sequence.",
  72   76          "Unknown regexp error code!!",
  73   77          NULL
  74   78  };
  75   79  
       80 +#define STDIN_FILENAME  gettext("(standard input)")
       81 +
  76   82  #define errmsg(msg, arg)        (void) fprintf(stderr, gettext(msg), arg)
  77   83  #define BLKSIZE 512
  78   84  #define GBUFSIZ 8192
  79   85  #define MAX_DEPTH       1000
  80   86  
  81   87  static int      temp;
  82   88  static long long        lnum;
  83   89  static char     *linebuf;
  84   90  static char     *prntbuf = NULL;
  85   91  static long     fw_lPrntBufLen = 0;
↓ open down ↓ 1 lines elided ↑ open up ↑
  87   93  static int      bflag;
  88   94  static int      lflag;
  89   95  static int      cflag;
  90   96  static int      rflag;
  91   97  static int      Rflag;
  92   98  static int      vflag;
  93   99  static int      sflag;
  94  100  static int      iflag;
  95  101  static int      wflag;
  96  102  static int      hflag;
      103 +static int      Hflag;
  97  104  static int      qflag;
  98  105  static int      errflg;
  99  106  static int      nfile;
 100  107  static long long        tln;
 101  108  static int      nsucc;
 102  109  static int      outfn = 0;
 103  110  static int      nlflag;
 104  111  static char     *ptr, *ptrend;
 105  112  static char     *expbuf;
 106  113  
↓ open down ↓ 9 lines elided ↑ open up ↑
 116  123          int     c;
 117  124          char    *arg;
 118  125          extern int      optind;
 119  126  
 120  127          (void) setlocale(LC_ALL, "");
 121  128  #if !defined(TEXT_DOMAIN)       /* Should be defined by cc -D */
 122  129  #define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
 123  130  #endif
 124  131          (void) textdomain(TEXT_DOMAIN);
 125  132  
 126      -        while ((c = getopt(argc, argv, "hqblcnRrsviyw")) != -1)
      133 +        while ((c = getopt(argc, argv, "hHqblcnRrsviyw")) != -1)
 127  134                  switch (c) {
      135 +                /* based on options order h or H is set as in GNU grep */
 128  136                  case 'h':
 129  137                          hflag++;
      138 +                        Hflag = 0; /* h excludes H */
 130  139                          break;
      140 +                case 'H':
      141 +                        if (!lflag) /* H is excluded by l */
      142 +                                Hflag++;
      143 +                        hflag = 0; /* H excludes h */
      144 +                        break;
 131  145                  case 'q':       /* POSIX: quiet: status only */
 132  146                          qflag++;
 133  147                          break;
 134  148                  case 'v':
 135  149                          vflag++;
 136  150                          break;
 137  151                  case 'c':
 138  152                          cflag++;
 139  153                          break;
 140  154                  case 'n':
↓ open down ↓ 6 lines elided ↑ open up ↑
 147  161                          rflag++;
 148  162                          break;
 149  163                  case 'b':
 150  164                          bflag++;
 151  165                          break;
 152  166                  case 's':
 153  167                          sflag++;
 154  168                          break;
 155  169                  case 'l':
 156  170                          lflag++;
      171 +                        Hflag = 0; /* l excludes H */
 157  172                          break;
 158  173                  case 'y':
 159  174                  case 'i':
 160  175                          iflag++;
 161  176                          break;
 162  177                  case 'w':
 163  178                          wflag++;
 164  179                          break;
 165  180                  case '?':
 166  181                          errflg++;
 167  182                  }
 168  183  
 169  184          if (errflg || (optind >= argc)) {
 170      -                errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hbnsviw "
      185 +                errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hHbnsviw "
 171  186                      "pattern file . . .\n",
 172  187                      (char *)NULL);
 173  188                  exit(2);
 174  189          }
 175  190  
 176  191          argv = &argv[optind];
 177  192          argc -= optind;
 178  193          nfile = argc - 1;
 179  194  
 180  195          if (strrchr(*argv, '\n') != NULL)
↓ open down ↓ 111 lines elided ↑ open up ↑
 292  307          if (prntbuf == NULL) {
 293  308                  fw_lPrntBufLen = GBUFSIZ + 1;
 294  309                  if ((prntbuf = malloc(fw_lPrntBufLen)) == NULL) {
 295  310                          exit(2); /* out of memory - BAIL */
 296  311                  }
 297  312                  if ((linebuf = malloc(fw_lPrntBufLen)) == NULL) {
 298  313                          exit(2); /* out of memory - BAIL */
 299  314                  }
 300  315          }
 301  316  
 302      -        if (file == NULL)
      317 +        if (file == NULL) {
 303  318                  temp = 0;
 304      -        else if ((temp = open(file + base, O_RDONLY)) == -1) {
      319 +                file = STDIN_FILENAME;
      320 +        } else if ((temp = open(file + base, O_RDONLY)) == -1) {
 305  321                  if (!sflag)
 306  322                          errmsg("grep: can't open %s\n", file);
 307  323                  nsucc = 2;
 308  324                  return;
 309  325          }
 310  326  
 311  327          /* read in first block of bytes */
 312  328          if ((count = read(temp, prntbuf, GBUFSIZ)) <= 0) {
 313  329                  (void) close(temp);
 314  330  
 315  331                  if (cflag && !qflag) {
 316      -                        if (nfile > 1 && !hflag && file)
      332 +                        if (Hflag || (nfile > 1 && !hflag))
 317  333                                  (void) fprintf(stdout, "%s:", file);
 318  334                          if (!rflag)
 319  335                          (void) fprintf(stdout, "%lld\n", tln);
 320  336                  }
 321  337                  return;
 322  338          }
 323  339  
 324  340          lnum = 0;
 325  341          ptr = prntbuf;
 326  342          for (;;) {
↓ open down ↓ 76 lines elided ↑ open up ↑
 403  419                  if (!nlflag)
 404  420                          break;
 405  421  
 406  422                  ptr = next_ptr;
 407  423                  count = next_count;
 408  424                  offset = 0;
 409  425          }
 410  426          (void) close(temp);
 411  427  
 412  428          if (cflag && !qflag) {
 413      -                if (!hflag && file && (nfile > 1 ||
 414      -                    (rflag && outfn)))
      429 +                if (Hflag || (!hflag && ((nfile > 1) ||
      430 +                    (rflag && outfn))))
 415  431                          (void) fprintf(stdout, "%s:", file);
 416  432                  (void) fprintf(stdout, "%lld\n", tln);
 417  433          }
 418  434  }
 419  435  
 420  436  static int
 421  437  succeed(const char *f)
 422  438  {
 423  439          int nchars;
 424  440          nsucc = (nsucc == 2) ? 2 : 1;
 425  441  
 426      -        if (f == NULL)
 427      -                f = "<stdin>";
 428      -
 429  442          if (qflag) {
 430  443                  /* no need to continue */
 431  444                  return (1);
 432  445          }
 433  446  
 434  447          if (cflag) {
 435  448                  tln++;
 436  449                  return (0);
 437  450          }
 438  451  
 439  452          if (lflag) {
 440  453                  (void) fprintf(stdout, "%s\n", f);
 441  454                  return (1);
 442  455          }
 443  456  
 444      -        if (!hflag && (nfile > 1 || (rflag && outfn))) {
      457 +        if (Hflag || (!hflag && (nfile > 1 || (rflag && outfn)))) {
 445  458                  /* print filename */
 446  459                  (void) fprintf(stdout, "%s:", f);
 447  460          }
 448  461  
 449  462          if (bflag)
 450  463                  /* print block number */
 451  464                  (void) fprintf(stdout, "%lld:", (offset_t)
 452  465                      ((lseek(temp, (off_t)0, SEEK_CUR) - 1) / BLKSIZE));
 453  466  
 454  467          if (nflag)
↓ open down ↓ 65 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX