Print this page
3737 grep does not support -H option

@@ -71,10 +71,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 +94,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,14 +124,21 @@
 #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':

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

@@ -165,11 +176,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 +308,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 +323,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 +420,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 +433,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 +448,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)