Print this page
3737 grep does not support -H option
3759 egrep(1) and fgrep(1) -s flag does not hide -c output
Reviewed by: Albert Lee <trisk@nexenta.com>
Reviewed by: Andy Stormont <andyjstormont@gmail.com>

@@ -28,11 +28,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.
+ */
 
 /*
  * fgrep -- print all lines containing any of a set of keywords
  *
  *      status returns:

@@ -105,10 +107,11 @@
 wchar_t letter();
 #define same(a, b) \
         (a == b || iflag && (!MULTI_BYTE || ISASCII(a)) && (a ^ b) == ' ' && \
         letter(a) == letter(b))
 
+#define STDIN_FILENAME gettext("(standard input)")
 
 #define QSIZE 400
 struct words {
         wchar_t inp;
         char    out;

@@ -117,12 +120,12 @@
         struct  words *fail;
 } *w = NULL, *smax, *q;
 
 FILE *fptr;
 long long lnum;
-int     bflag, cflag, lflag, fflag, nflag, vflag, xflag, eflag, sflag;
-int     hflag, iflag;
+int     bflag, cflag, lflag, fflag, nflag, vflag, xflag, eflag, qflag;
+int     Hflag, hflag, iflag;
 int     retcode = 0;
 int     nfile;
 long long blkno;
 int     nsucc;
 long long tln;

@@ -148,18 +151,24 @@
 #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, "hybcie:f:lnvxs")) != EOF)
+        while ((c = getopt(argc, argv, "Hhybcie:f:lnvxqs")) != EOF)
                 switch (c) {
 
-                case 's':
-                        sflag++;
+                case 'q':
+                case 's': /* Solaris: legacy option */
+                        qflag++;
                         continue;
+                case 'H':
+                        Hflag++;
+                        hflag = 0;
+                        continue;
                 case 'h':
                         hflag++;
+                        Hflag = 0;
                         continue;
                 case 'b':
                         bflag++;
                         continue;
 

@@ -219,11 +228,11 @@
                         errflg++;
         }
 
         argc -= optind;
         if (errflg || ((argc <= 0) && !fflag && !eflag)) {
-                (void) printf(gettext("usage: fgrep [ -bchilnsvx ] "
+                (void) printf(gettext("usage: fgrep [ -bcHhilnqsvx ] "
                         "[ -e exp ] [ -f file ] [ strings ] [ file ] ...\n"));
                 exit(2);
         }
         if (!eflag && !fflag) {
                 argptr = argv[optind];

@@ -311,12 +320,12 @@
                                 gettext("fgrep: can't open %s\n"), file);
                         retcode = 2;
                         return;
                 }
         } else {
-                file = "<stdin>";
                 fptr = stdin;
+                file = STDIN_FILENAME;
         }
         ccount = 0;
         failed = 0;
         lnum = 1;
         tln = 0;

@@ -415,18 +424,20 @@
                         if ((vflag && (failed == 0 || xflag == 0)) ||
                                 (vflag == 0 && xflag && failed))
                                 goto nomatch;
 succeed:
                         nsucc = 1;
-                        if (cflag)
-                                tln++;
-                        else if (lflag && !sflag) {
+                        if (lflag || qflag) {
+                                if (!qflag)
                                 (void) printf("%s\n", file);
                                 (void) fclose(fptr);
                                 return;
-                        } else if (!sflag) {
-                                if (nfile > 1 && !hflag)
+                        }
+                        if (cflag) {
+                                tln++;
+                        } else {
+                                if (Hflag || (nfile > 1 && !hflag))
                                         (void) printf("%s:", file);
                                 if (bflag)
                                         (void) printf("%lld:",
                                                 (blkno - (long long)(ccount-1))
                                                 / BUFSIZ);

@@ -456,12 +467,12 @@
                                 c = w;
                                 failed = 0;
                         }
         }
         (void) fclose(fptr);
-        if (cflag) {
-                if ((nfile > 1) && !hflag)
+        if (cflag && !qflag) {
+                if (Hflag || (nfile > 1 && !hflag))
                         (void) printf("%s:", file);
                 (void) printf("%lld\n", tln);
         }
 }