Print this page
3737 grep does not support -H option

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