Print this page
3737 grep does not support -H option

@@ -52,10 +52,12 @@
 #include <unistd.h>
 #include <wctype.h>
 #include <ftw.h>
 #include <sys/param.h>
 
+#define STDIN_FILENAME gettext("(standard input)")
+
 #define BSIZE           512             /* Size of block for -b */
 #define BUFSIZE         8192            /* Input buffer size */
 #define MAX_DEPTH       1000            /* how deep to recurse */
 
 #define M_CSETSIZE      256             /* singlebyte chars */

@@ -78,10 +80,11 @@
 static uchar_t  fgrep = 0;              /* Invoked as fgrep */
 static uchar_t  egrep = 0;              /* Invoked as egrep */
 static uchar_t  nvflag = 1;             /* Print matching lines */
 static uchar_t  cflag;                  /* Count of matches */
 static uchar_t  iflag;                  /* Case insensitve matching */
+static uchar_t  Hflag;                  /* Precede lines by file name */
 static uchar_t  hflag;                  /* Supress printing of filename */
 static uchar_t  lflag;                  /* Print file names of matches */
 static uchar_t  nflag;                  /* Precede lines by line number */
 static uchar_t  rflag;                  /* Search directories recursively */
 static uchar_t  bflag;                  /* Preccede matches by block number */

@@ -153,11 +156,11 @@
                 if (*ap == 'f' || *ap == 'F') {
                         fgrep++;
                 }
         }
 
-        while ((c = getopt(argc, argv, "vwchilnrbse:f:qxEFIR")) != EOF) {
+        while ((c = getopt(argc, argv, "vwchHilnrbse:f:qxEFIR")) != EOF) {
                 switch (c) {
                 case 'v':       /* POSIX: negate matches */
                         nvflag = 0;
                         break;
 

@@ -214,12 +217,20 @@
                                     cmdname);
                                 exit(2);
                         }
                         *(file_list + n_file - 1) = optarg;
                         break;
+
+                /* based on options order h or H is set as in GNU grep */
                 case 'h':       /* Solaris: supress printing of file name */
                         hflag = 1;
+                        Hflag = 0;
+                        break;
+                /* Solaris: precede every matching with file name */
+                case 'H':
+                        Hflag = 1;
+                        hflag = 0;
                         break;
 
                 case 'q':       /* POSIX: quiet: status only */
                         qflag++;
                         break;

@@ -292,10 +303,16 @@
          */
         if (Eflag && Fflag)
                 usage();
 
         /*
+         * -l overrides -H like in GNU grep
+         */
+        if (lflag)
+                Hflag = 0;
+
+        /*
          * -c, -l and -q flags are mutually exclusive
          * We have -c override -l like in Solaris.
          * -q overrides -l & -c programmatically in grep() function.
          */
         if (cflag && lflag)

@@ -344,13 +361,13 @@
          */
         fixpatterns();
 
         /* Process all files: stdin, or rest of arg list */
         if (argc < 2) {
-                matched = grep(0, gettext("(standard input)"));
+                matched = grep(0, STDIN_FILENAME);
         } else {
-                if (argc > 2 && hflag == 0)
+                if (Hflag || (argc > 2 && hflag == 0))
                         outfn = 1;      /* Print filename on match line */
                 for (argv++; *argv != NULL; argv++) {
                         process_path(*argv);
                 }
         }

@@ -1110,11 +1127,11 @@
                         if (lflag) {
                                 (void) printf("%s\n", fn);
                                 break;
                         }
                         if (!cflag) {
-                                if (outfn) {
+                                if (Hflag || outfn) {
                                         (void) printf("%s:", fn);
                                 }
                                 if (bflag) {
                                         (void) printf("%lld:", (offset_t)
                                             (line_offset / BSIZE));

@@ -1137,11 +1154,11 @@
                 line_offset += line_len + 1;
                 ptr = ptrend + 1;
         }
 
         if (cflag) {
-                if (outfn) {
+                if (Hflag || outfn) {
                         (void) printf("%s:", fn);
                 }
                 if (!qflag) {
                         (void) printf("%lld\n", matches);
                 }

@@ -1156,49 +1173,49 @@
 usage(void)
 {
         if (egrep || fgrep) {
                 (void) fprintf(stderr, gettext("Usage:\t%s"), cmdname);
                 (void) fprintf(stderr,
-                    gettext(" [-c|-l|-q] [-r|-R] [-bhinsvx] "
+                    gettext(" [-c|-l|-q] [-r|-R] [-bhHinsvx] "
                     "pattern_list [file ...]\n"));
 
                 (void) fprintf(stderr, "\t%s", cmdname);
                 (void) fprintf(stderr,
-                    gettext(" [-c|-l|-q] [-r|-R] [-bhinsvx] "
+                    gettext(" [-c|-l|-q] [-r|-R] [-bhHinsvx] "
                     "[-e pattern_list]... "
                     "[-f pattern_file]... [file...]\n"));
         } else {
                 (void) fprintf(stderr, gettext("Usage:\t%s"), cmdname);
                 (void) fprintf(stderr,
-                    gettext(" [-c|-l|-q] [-r|-R] [-bhinsvwx] "
+                    gettext(" [-c|-l|-q] [-r|-R] [-bhHinsvwx] "
                     "pattern_list [file ...]\n"));
 
                 (void) fprintf(stderr, "\t%s", cmdname);
                 (void) fprintf(stderr,
-                    gettext(" [-c|-l|-q] [-r|-R] [-bhinsvwx] "
+                    gettext(" [-c|-l|-q] [-r|-R] [-bhHinsvwx] "
                     "[-e pattern_list]... "
                     "[-f pattern_file]... [file...]\n"));
 
                 (void) fprintf(stderr, "\t%s", cmdname);
                 (void) fprintf(stderr,
-                    gettext(" -E [-c|-l|-q] [-r|-R] [-bhinsvx] "
+                    gettext(" -E [-c|-l|-q] [-r|-R] [-bhHinsvx] "
                     "pattern_list [file ...]\n"));
 
                 (void) fprintf(stderr, "\t%s", cmdname);
                 (void) fprintf(stderr,
-                    gettext(" -E [-c|-l|-q] [-r|-R] [-bhinsvx] "
+                    gettext(" -E [-c|-l|-q] [-r|-R] [-bhHinsvx] "
                     "[-e pattern_list]... "
                     "[-f pattern_file]... [file...]\n"));
 
                 (void) fprintf(stderr, "\t%s", cmdname);
                 (void) fprintf(stderr,
-                    gettext(" -F [-c|-l|-q] [-r|-R] [-bhinsvx] "
+                    gettext(" -F [-c|-l|-q] [-r|-R] [-bhHinsvx] "
                     "pattern_list [file ...]\n"));
 
                 (void) fprintf(stderr, "\t%s", cmdname);
                 (void) fprintf(stderr,
-                    gettext(" -F [-c|-l|-q] [-bhinsvx] [-e pattern_list]... "
+                    gettext(" -F [-c|-l|-q] [-bhHinsvx] [-e pattern_list]... "
                     "[-f pattern_file]... [file...]\n"));
         }
         exit(2);
         /* NOTREACHED */
 }