Print this page
3737 grep does not support -H option

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/grep_xpg4/grep.c
          +++ new/usr/src/cmd/grep_xpg4/grep.c
↓ open down ↓ 46 lines elided ↑ open up ↑
  47   47  #include <fcntl.h>
  48   48  #include <stdio.h>
  49   49  #include <locale.h>
  50   50  #include <wchar.h>
  51   51  #include <errno.h>
  52   52  #include <unistd.h>
  53   53  #include <wctype.h>
  54   54  #include <ftw.h>
  55   55  #include <sys/param.h>
  56   56  
       57 +#define STDIN_FILENAME gettext("(standard input)")
       58 +
  57   59  #define BSIZE           512             /* Size of block for -b */
  58   60  #define BUFSIZE         8192            /* Input buffer size */
  59   61  #define MAX_DEPTH       1000            /* how deep to recurse */
  60   62  
  61   63  #define M_CSETSIZE      256             /* singlebyte chars */
  62   64  static int      bmglen;                 /* length of BMG pattern */
  63   65  static char     *bmgpat;                /* BMG pattern */
  64   66  static int      bmgtab[M_CSETSIZE];     /* BMG delta1 table */
  65   67  
  66   68  typedef struct  _PATTERN        {
↓ open down ↓ 6 lines elided ↑ open up ↑
  73   75  static PATTERN  *patterns;
  74   76  static char     errstr[128];            /* regerror string buffer */
  75   77  static int      regflags = 0;           /* regcomp options */
  76   78  static int      matched = 0;            /* return of the grep() */
  77   79  static int      errors = 0;             /* count of errors */
  78   80  static uchar_t  fgrep = 0;              /* Invoked as fgrep */
  79   81  static uchar_t  egrep = 0;              /* Invoked as egrep */
  80   82  static uchar_t  nvflag = 1;             /* Print matching lines */
  81   83  static uchar_t  cflag;                  /* Count of matches */
  82   84  static uchar_t  iflag;                  /* Case insensitve matching */
       85 +static uchar_t  Hflag;                  /* Precede lines by file name */
  83   86  static uchar_t  hflag;                  /* Supress printing of filename */
  84   87  static uchar_t  lflag;                  /* Print file names of matches */
  85   88  static uchar_t  nflag;                  /* Precede lines by line number */
  86   89  static uchar_t  rflag;                  /* Search directories recursively */
  87   90  static uchar_t  bflag;                  /* Preccede matches by block number */
  88   91  static uchar_t  sflag;                  /* Suppress file error messages */
  89   92  static uchar_t  qflag;                  /* Suppress standard output */
  90   93  static uchar_t  wflag;                  /* Search for expression as a word */
  91   94  static uchar_t  xflag;                  /* Anchoring */
  92   95  static uchar_t  Eflag;                  /* Egrep or -E flag */
↓ open down ↓ 55 lines elided ↑ open up ↑
 148  151           */
 149  152          if (*ap == 'e' || *ap == 'E') {
 150  153                  regflags |= REG_EXTENDED;
 151  154                  egrep++;
 152  155          } else {
 153  156                  if (*ap == 'f' || *ap == 'F') {
 154  157                          fgrep++;
 155  158                  }
 156  159          }
 157  160  
 158      -        while ((c = getopt(argc, argv, "vwchilnrbse:f:qxEFIR")) != EOF) {
      161 +        while ((c = getopt(argc, argv, "vwchHilnrbse:f:qxEFIR")) != EOF) {
 159  162                  switch (c) {
 160  163                  case 'v':       /* POSIX: negate matches */
 161  164                          nvflag = 0;
 162  165                          break;
 163  166  
 164  167                  case 'c':       /* POSIX: write count */
 165  168                          cflag++;
 166  169                          break;
 167  170  
 168  171                  case 'i':       /* POSIX: ignore case */
↓ open down ↓ 40 lines elided ↑ open up ↑
 209  212                          file_list = realloc(file_list,
 210  213                              sizeof (char *) * n_file);
 211  214                          if (file_list == NULL) {
 212  215                                  (void) fprintf(stderr,
 213  216                                      gettext("%s: out of memory\n"),
 214  217                                      cmdname);
 215  218                                  exit(2);
 216  219                          }
 217  220                          *(file_list + n_file - 1) = optarg;
 218  221                          break;
      222 +
      223 +                /* based on options order h or H is set as in GNU grep */
 219  224                  case 'h':       /* Solaris: supress printing of file name */
 220  225                          hflag = 1;
      226 +                        Hflag = 0;
      227 +                        break;
      228 +                /* Solaris: precede every matching with file name */
      229 +                case 'H':
      230 +                        Hflag = 1;
      231 +                        hflag = 0;
 221  232                          break;
 222  233  
 223  234                  case 'q':       /* POSIX: quiet: status only */
 224  235                          qflag++;
 225  236                          break;
 226  237  
 227  238                  case 'w':       /* Solaris: treat pattern as word */
 228  239                          wflag++;
 229  240                          break;
 230  241  
↓ open down ↓ 56 lines elided ↑ open up ↑
 287  298                  usage();
 288  299          }
 289  300  
 290  301          /*
 291  302           * -E and -F flags are mutually exclusive - check for this
 292  303           */
 293  304          if (Eflag && Fflag)
 294  305                  usage();
 295  306  
 296  307          /*
      308 +         * -l overrides -H like in GNU grep
      309 +         */
      310 +        if (lflag)
      311 +                Hflag = 0;
      312 +
      313 +        /*
 297  314           * -c, -l and -q flags are mutually exclusive
 298  315           * We have -c override -l like in Solaris.
 299  316           * -q overrides -l & -c programmatically in grep() function.
 300  317           */
 301  318          if (cflag && lflag)
 302  319                  lflag = 0;
 303  320  
 304  321          argv += optind - 1;
 305  322          argc -= optind - 1;
 306  323  
↓ open down ↓ 32 lines elided ↑ open up ↑
 339  356           */
 340  357          use_wchar = Fflag && mblocale && (!xflag || iflag);
 341  358  
 342  359          /*
 343  360           * Compile Patterns and also decide if BMG can be used
 344  361           */
 345  362          fixpatterns();
 346  363  
 347  364          /* Process all files: stdin, or rest of arg list */
 348  365          if (argc < 2) {
 349      -                matched = grep(0, gettext("(standard input)"));
      366 +                matched = grep(0, STDIN_FILENAME);
 350  367          } else {
 351      -                if (argc > 2 && hflag == 0)
      368 +                if (Hflag || (argc > 2 && hflag == 0))
 352  369                          outfn = 1;      /* Print filename on match line */
 353  370                  for (argv++; *argv != NULL; argv++) {
 354  371                          process_path(*argv);
 355  372                  }
 356  373          }
 357  374          /*
 358  375           * Return() here is used instead of exit
 359  376           */
 360  377  
 361  378          (void) fflush(stdout);
↓ open down ↓ 743 lines elided ↑ open up ↑
1105 1122                                  off_t   pos;
1106 1123                                  pos = ptr + data_len - (ptrend + 1);
1107 1124                                  (void) lseek(fd, -pos, SEEK_CUR);
1108 1125                                  exit(0);
1109 1126                          }
1110 1127                          if (lflag) {
1111 1128                                  (void) printf("%s\n", fn);
1112 1129                                  break;
1113 1130                          }
1114 1131                          if (!cflag) {
1115      -                                if (outfn) {
     1132 +                                if (Hflag || outfn) {
1116 1133                                          (void) printf("%s:", fn);
1117 1134                                  }
1118 1135                                  if (bflag) {
1119 1136                                          (void) printf("%lld:", (offset_t)
1120 1137                                              (line_offset / BSIZE));
1121 1138                                  }
1122 1139                                  if (nflag) {
1123 1140                                          (void) printf("%lld:", lineno);
1124 1141                                  }
1125 1142                                  *ptrend = '\n';
↓ open down ↓ 6 lines elided ↑ open up ↑
1132 1149  L_skip_line:
1133 1150                  if (!newlinep)
1134 1151                          break;
1135 1152  
1136 1153                  data_len -= line_len + 1;
1137 1154                  line_offset += line_len + 1;
1138 1155                  ptr = ptrend + 1;
1139 1156          }
1140 1157  
1141 1158          if (cflag) {
1142      -                if (outfn) {
     1159 +                if (Hflag || outfn) {
1143 1160                          (void) printf("%s:", fn);
1144 1161                  }
1145 1162                  if (!qflag) {
1146 1163                          (void) printf("%lld\n", matches);
1147 1164                  }
1148 1165          }
1149 1166          return (matches != 0);
1150 1167  }
1151 1168  
1152 1169  /*
1153 1170   * usage message for grep
1154 1171   */
1155 1172  static void
1156 1173  usage(void)
1157 1174  {
1158 1175          if (egrep || fgrep) {
1159 1176                  (void) fprintf(stderr, gettext("Usage:\t%s"), cmdname);
1160 1177                  (void) fprintf(stderr,
1161      -                    gettext(" [-c|-l|-q] [-r|-R] [-bhinsvx] "
     1178 +                    gettext(" [-c|-l|-q] [-r|-R] [-bhHinsvx] "
1162 1179                      "pattern_list [file ...]\n"));
1163 1180  
1164 1181                  (void) fprintf(stderr, "\t%s", cmdname);
1165 1182                  (void) fprintf(stderr,
1166      -                    gettext(" [-c|-l|-q] [-r|-R] [-bhinsvx] "
     1183 +                    gettext(" [-c|-l|-q] [-r|-R] [-bhHinsvx] "
1167 1184                      "[-e pattern_list]... "
1168 1185                      "[-f pattern_file]... [file...]\n"));
1169 1186          } else {
1170 1187                  (void) fprintf(stderr, gettext("Usage:\t%s"), cmdname);
1171 1188                  (void) fprintf(stderr,
1172      -                    gettext(" [-c|-l|-q] [-r|-R] [-bhinsvwx] "
     1189 +                    gettext(" [-c|-l|-q] [-r|-R] [-bhHinsvwx] "
1173 1190                      "pattern_list [file ...]\n"));
1174 1191  
1175 1192                  (void) fprintf(stderr, "\t%s", cmdname);
1176 1193                  (void) fprintf(stderr,
1177      -                    gettext(" [-c|-l|-q] [-r|-R] [-bhinsvwx] "
     1194 +                    gettext(" [-c|-l|-q] [-r|-R] [-bhHinsvwx] "
1178 1195                      "[-e pattern_list]... "
1179 1196                      "[-f pattern_file]... [file...]\n"));
1180 1197  
1181 1198                  (void) fprintf(stderr, "\t%s", cmdname);
1182 1199                  (void) fprintf(stderr,
1183      -                    gettext(" -E [-c|-l|-q] [-r|-R] [-bhinsvx] "
     1200 +                    gettext(" -E [-c|-l|-q] [-r|-R] [-bhHinsvx] "
1184 1201                      "pattern_list [file ...]\n"));
1185 1202  
1186 1203                  (void) fprintf(stderr, "\t%s", cmdname);
1187 1204                  (void) fprintf(stderr,
1188      -                    gettext(" -E [-c|-l|-q] [-r|-R] [-bhinsvx] "
     1205 +                    gettext(" -E [-c|-l|-q] [-r|-R] [-bhHinsvx] "
1189 1206                      "[-e pattern_list]... "
1190 1207                      "[-f pattern_file]... [file...]\n"));
1191 1208  
1192 1209                  (void) fprintf(stderr, "\t%s", cmdname);
1193 1210                  (void) fprintf(stderr,
1194      -                    gettext(" -F [-c|-l|-q] [-r|-R] [-bhinsvx] "
     1211 +                    gettext(" -F [-c|-l|-q] [-r|-R] [-bhHinsvx] "
1195 1212                      "pattern_list [file ...]\n"));
1196 1213  
1197 1214                  (void) fprintf(stderr, "\t%s", cmdname);
1198 1215                  (void) fprintf(stderr,
1199      -                    gettext(" -F [-c|-l|-q] [-bhinsvx] [-e pattern_list]... "
     1216 +                    gettext(" -F [-c|-l|-q] [-bhHinsvx] [-e pattern_list]... "
1200 1217                      "[-f pattern_file]... [file...]\n"));
1201 1218          }
1202 1219          exit(2);
1203 1220          /* NOTREACHED */
1204 1221  }
1205 1222  
1206 1223  /*
1207 1224   * Compile literal pattern into BMG tables
1208 1225   */
1209 1226  static void
↓ open down ↓ 49 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX