Print this page
3737 grep does not support -H option
Reviewed by: Andy Stormont <andyjstormont@gmail.com>

@@ -31,10 +31,14 @@
 /*        All Rights Reserved   */
 
 /* Copyright 2012 Nexenta Systems, Inc.  All rights reserved. */
 
 /*
+ * Copyright 2013 Damian Bogel. All rights reserved.
+ */
+
+/*
  * grep -- print lines matching (or not matching) a pattern
  *
  *      status returns:
  *              0 - ok, and some matches
  *              1 - ok, but no matches

@@ -71,10 +75,12 @@
         "Illegal byte sequence.",
         "Unknown regexp error code!!",
         NULL
 };
 
+#define STDIN_FILENAME  gettext("(standard input)")
+
 #define errmsg(msg, arg)        (void) fprintf(stderr, gettext(msg), arg)
 #define BLKSIZE 512
 #define GBUFSIZ 8192
 #define MAX_DEPTH       1000
 

@@ -92,10 +98,11 @@
 static int      vflag;
 static int      sflag;
 static int      iflag;
 static int      wflag;
 static int      hflag;
+static int      Hflag;
 static int      qflag;
 static int      errflg;
 static int      nfile;
 static long long        tln;
 static int      nsucc;

@@ -121,15 +128,22 @@
 #if !defined(TEXT_DOMAIN)       /* Should be defined by cc -D */
 #define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
 #endif
         (void) textdomain(TEXT_DOMAIN);
 
-        while ((c = getopt(argc, argv, "hqblcnRrsviyw")) != -1)
+        while ((c = getopt(argc, argv, "hHqblcnRrsviyw")) != -1)
                 switch (c) {
+                /* based on options order h or H is set as in GNU grep */
                 case 'h':
                         hflag++;
+                        Hflag = 0; /* h excludes H */
                         break;
+                case 'H':
+                        if (!lflag) /* H is excluded by l */
+                                Hflag++;
+                        hflag = 0; /* H excludes h */
+                        break;
                 case 'q':       /* POSIX: quiet: status only */
                         qflag++;
                         break;
                 case 'v':
                         vflag++;

@@ -152,10 +166,11 @@
                 case 's':
                         sflag++;
                         break;
                 case 'l':
                         lflag++;
+                        Hflag = 0; /* l excludes H */
                         break;
                 case 'y':
                 case 'i':
                         iflag++;
                         break;

@@ -165,11 +180,11 @@
                 case '?':
                         errflg++;
                 }
 
         if (errflg || (optind >= argc)) {
-                errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hbnsviw "
+                errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hHbnsviw "
                     "pattern file . . .\n",
                     (char *)NULL);
                 exit(2);
         }
 

@@ -297,13 +312,14 @@
                 if ((linebuf = malloc(fw_lPrntBufLen)) == NULL) {
                         exit(2); /* out of memory - BAIL */
                 }
         }
 
-        if (file == NULL)
+        if (file == NULL) {
                 temp = 0;
-        else if ((temp = open(file + base, O_RDONLY)) == -1) {
+                file = STDIN_FILENAME;
+        } else if ((temp = open(file + base, O_RDONLY)) == -1) {
                 if (!sflag)
                         errmsg("grep: can't open %s\n", file);
                 nsucc = 2;
                 return;
         }

@@ -311,11 +327,11 @@
         /* read in first block of bytes */
         if ((count = read(temp, prntbuf, GBUFSIZ)) <= 0) {
                 (void) close(temp);
 
                 if (cflag && !qflag) {
-                        if (nfile > 1 && !hflag && file)
+                        if (Hflag || (nfile > 1 && !hflag))
                                 (void) fprintf(stdout, "%s:", file);
                         if (!rflag)
                         (void) fprintf(stdout, "%lld\n", tln);
                 }
                 return;

@@ -408,12 +424,12 @@
                 offset = 0;
         }
         (void) close(temp);
 
         if (cflag && !qflag) {
-                if (!hflag && file && (nfile > 1 ||
-                    (rflag && outfn)))
+                if (Hflag || (!hflag && ((nfile > 1) ||
+                    (rflag && outfn))))
                         (void) fprintf(stdout, "%s:", file);
                 (void) fprintf(stdout, "%lld\n", tln);
         }
 }
 

@@ -421,13 +437,10 @@
 succeed(const char *f)
 {
         int nchars;
         nsucc = (nsucc == 2) ? 2 : 1;
 
-        if (f == NULL)
-                f = "<stdin>";
-
         if (qflag) {
                 /* no need to continue */
                 return (1);
         }
 

@@ -439,11 +452,11 @@
         if (lflag) {
                 (void) fprintf(stdout, "%s\n", f);
                 return (1);
         }
 
-        if (!hflag && (nfile > 1 || (rflag && outfn))) {
+        if (Hflag || (!hflag && (nfile > 1 || (rflag && outfn)))) {
                 /* print filename */
                 (void) fprintf(stdout, "%s:", f);
         }
 
         if (bflag)