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

@@ -30,13 +30,13 @@
 /*        All Rights Reserved   */
 
 /*      Copyright (c) 1987, 1988 Microsoft Corporation  */
 /*        All Rights Reserved   */
 
-%{
-#pragma ident   "%Z%%M% %I%     %E% SMI"
-%}
+/*
+ * Copyright 2013 Damian Bogel. All rights reserved.
+ */
 
 /*
  * egrep -- print lines containing (or not containing) a regular expression
  *
  *      status returns:

@@ -59,10 +59,12 @@
 #include <widec.h>
 #include <stdlib.h>
 #include <limits.h>
 #include <locale.h>
 
+#define STDIN_FILENAME gettext("(standard input)")
+
 #define BLKSIZE 512     /* size of reported disk blocks */
 #define EBUFSIZ 8192
 #define MAXLIN 350
 #define NCHARS 256
 #define MAXPOS 4000

@@ -108,15 +110,16 @@
 long long lnum;
 int     bflag;
 int     cflag;
 int     eflag;
 int     fflag;
+int     Hflag;
 int     hflag;
 int     iflag;
 int     lflag;
 int     nflag;
-int     sflag;
+int     qflag;
 int     vflag;
 int     nfile;
 long long blkno;
 long long tln;
 int     nsucc;

@@ -658,11 +661,11 @@
 #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, "ybcie:f:hlnvs")) != -1)
+        while((c = getopt(argc, argv, "ybcie:f:Hhlnvs")) != -1)
                 switch(c) {
 
                 case 'b':
                         bflag++;
                         continue;

@@ -684,29 +687,38 @@
                                   gettext("egrep: can't open %s\n"), optarg);
                                 exit(2);
                         }
                         continue;
 
+                case 'H':
+                        if (!lflag) /* H is excluded by l as in GNU grep */
+                                Hflag++;
+                        hflag = 0; /* H excludes h */
+                        continue;
+
                 case 'h':
                         hflag++;
+                        Hflag = 0; /* h excludes H */
                         continue;
 
                 case 'y':
                 case 'i':
                         iflag++;
                         continue;
 
                 case 'l':
                         lflag++;
+                        Hflag = 0; /* l excludes H */
                         continue;
 
                 case 'n':
                         nflag++;
                         continue;
 
-                case 's':
-                        sflag++;
+                case 'q':
+                case 's': /* Solaris: legacy option */
+                        qflag++;
                         continue;
 
                 case 'v':
                         vflag++;
                         continue;

@@ -797,20 +809,20 @@
                                 gettext("egrep: can't open %s\n"), file);
                         badbotch=1;
                         return;
                 }
         } else {
-                file = "<stdin>";
                 f = stdin;
+                file = STDIN_FILENAME;
         }
         lnum = 1;
         tln = 0;
         if((count = read(fileno(f), buf, EBUFSIZ)) <= 0) {
                 fclose(f);
 
-                if (cflag) {
-                        if (nfile>1 && !hflag)
+                if (cflag && !qflag) {
+                        if (Hflag || (nfile > 1 && !hflag))
                                 fprintf(stdout, "%s:", file);
                         fprintf(stdout, "%lld\n", tln);
                 }
                 return;
         }

@@ -900,23 +912,23 @@
                                 }
                                 succ = vflag;
                                 break;
                         }
                 }
-                if(succ) {
+                if (succ) {
                         nsucc = 1;
-                        if (cflag) tln++;
-                        else if (sflag)
-                                ;       /* ugh */
-                        else if (lflag) {
-                                printf("%s\n", file);
+                        if (lflag || qflag) {
+                                if (!qflag)
+                                        (void) printf("%s\n", file);
                                 fclose(f);
                                 return;
                         }
-                        else {
-                                if (nfile > 1 && !hflag) 
-                                        printf(gettext("%s:"), file);
+                        if (cflag) {
+                                tln++;
+                        } else {
+                                if (Hflag || (nfile > 1 && !hflag))
+                                        printf("%s:", file);
                                 if (bflag) {
                                         nchars = blkno - (buf + count - ptrend) - 2;
                                         if(nlflag)
                                                 nchars++;
                                         printf("%lld:", nchars/BLKSIZE);

@@ -942,13 +954,13 @@
                 lnum++;
                 if (reinit == 1) 
                         clearg();
         }
         fclose(f);
-        if (cflag) {
-                if (nfile > 1 && !hflag)
-                        printf(gettext("%s:"), file);
+        if (cflag && !qflag) {
+                if (Hflag || (nfile > 1 && !hflag))
+                        printf("%s:", file);
                 printf("%lld\n", tln);
         }
 }
 
 void