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


  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  28 /*        All Rights Reserved   */
  29 
  30 /*      Copyright (c) 1987, 1988 Microsoft Corporation  */
  31 /*        All Rights Reserved   */
  32 
  33 /* Copyright 2012 Nexenta Systems, Inc.  All rights reserved. */
  34 
  35 /*




  36  * grep -- print lines matching (or not matching) a pattern
  37  *
  38  *      status returns:
  39  *              0 - ok, and some matches
  40  *              1 - ok, but no matches
  41  *              2 - some error
  42  */
  43 
  44 #include <sys/types.h>
  45 
  46 #include <ctype.h>
  47 #include <fcntl.h>
  48 #include <locale.h>
  49 #include <memory.h>
  50 #include <regexpr.h>
  51 #include <stdio.h>
  52 #include <stdlib.h>
  53 #include <string.h>
  54 #include <unistd.h>
  55 #include <ftw.h>
  56 #include <limits.h>
  57 #include <sys/param.h>
  58 
  59 static const char *errstr[] = {
  60         "Range endpoint too large.",
  61         "Bad number.",
  62         "``\\digit'' out of range.",
  63         "No remembered search string.",
  64         "\\( \\) imbalance.",
  65         "Too many \\(.",
  66         "More than 2 numbers given in \\{ \\}.",
  67         "} expected after \\.",
  68         "First number exceeds second in \\{ \\}.",
  69         "[ ] imbalance.",
  70         "Regular expression overflow.",
  71         "Illegal byte sequence.",
  72         "Unknown regexp error code!!",
  73         NULL
  74 };
  75 


  76 #define errmsg(msg, arg)        (void) fprintf(stderr, gettext(msg), arg)
  77 #define BLKSIZE 512
  78 #define GBUFSIZ 8192
  79 #define MAX_DEPTH       1000
  80 
  81 static int      temp;
  82 static long long        lnum;
  83 static char     *linebuf;
  84 static char     *prntbuf = NULL;
  85 static long     fw_lPrntBufLen = 0;
  86 static int      nflag;
  87 static int      bflag;
  88 static int      lflag;
  89 static int      cflag;
  90 static int      rflag;
  91 static int      Rflag;
  92 static int      vflag;
  93 static int      sflag;
  94 static int      iflag;
  95 static int      wflag;
  96 static int      hflag;

  97 static int      qflag;
  98 static int      errflg;
  99 static int      nfile;
 100 static long long        tln;
 101 static int      nsucc;
 102 static int      outfn = 0;
 103 static int      nlflag;
 104 static char     *ptr, *ptrend;
 105 static char     *expbuf;
 106 
 107 static void     execute(const char *, int);
 108 static void     regerr(int);
 109 static void     prepare(const char *);
 110 static int      recursive(const char *, const struct stat *, int, struct FTW *);
 111 static int      succeed(const char *);
 112 
 113 int
 114 main(int argc, char **argv)
 115 {
 116         int     c;
 117         char    *arg;
 118         extern int      optind;
 119 
 120         (void) setlocale(LC_ALL, "");
 121 #if !defined(TEXT_DOMAIN)       /* Should be defined by cc -D */
 122 #define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
 123 #endif
 124         (void) textdomain(TEXT_DOMAIN);
 125 
 126         while ((c = getopt(argc, argv, "hqblcnRrsviyw")) != -1)
 127                 switch (c) {

 128                 case 'h':
 129                         hflag++;

 130                         break;





 131                 case 'q':       /* POSIX: quiet: status only */
 132                         qflag++;
 133                         break;
 134                 case 'v':
 135                         vflag++;
 136                         break;
 137                 case 'c':
 138                         cflag++;
 139                         break;
 140                 case 'n':
 141                         nflag++;
 142                         break;
 143                 case 'R':
 144                         Rflag++;
 145                         /* FALLTHROUGH */
 146                 case 'r':
 147                         rflag++;
 148                         break;
 149                 case 'b':
 150                         bflag++;
 151                         break;
 152                 case 's':
 153                         sflag++;
 154                         break;
 155                 case 'l':
 156                         lflag++;

 157                         break;
 158                 case 'y':
 159                 case 'i':
 160                         iflag++;
 161                         break;
 162                 case 'w':
 163                         wflag++;
 164                         break;
 165                 case '?':
 166                         errflg++;
 167                 }
 168 
 169         if (errflg || (optind >= argc)) {
 170                 errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hbnsviw "
 171                     "pattern file . . .\n",
 172                     (char *)NULL);
 173                 exit(2);
 174         }
 175 
 176         argv = &argv[optind];
 177         argc -= optind;
 178         nfile = argc - 1;
 179 
 180         if (strrchr(*argv, '\n') != NULL)
 181                 regerr(41);
 182 
 183         if (iflag) {
 184                 for (arg = *argv; *arg != NULL; ++arg)
 185                         *arg = (char)tolower((int)((unsigned char)*arg));
 186         }
 187 
 188         if (wflag) {
 189                 unsigned int    wordlen;
 190                 char            *wordbuf;


 282 execute(const char *file, int base)
 283 {
 284         char    *lbuf, *p;
 285         long    count;
 286         long    offset = 0;
 287         char    *next_ptr = NULL;
 288         long    next_count = 0;
 289 
 290         tln = 0;
 291 
 292         if (prntbuf == NULL) {
 293                 fw_lPrntBufLen = GBUFSIZ + 1;
 294                 if ((prntbuf = malloc(fw_lPrntBufLen)) == NULL) {
 295                         exit(2); /* out of memory - BAIL */
 296                 }
 297                 if ((linebuf = malloc(fw_lPrntBufLen)) == NULL) {
 298                         exit(2); /* out of memory - BAIL */
 299                 }
 300         }
 301 
 302         if (file == NULL)
 303                 temp = 0;
 304         else if ((temp = open(file + base, O_RDONLY)) == -1) {

 305                 if (!sflag)
 306                         errmsg("grep: can't open %s\n", file);
 307                 nsucc = 2;
 308                 return;
 309         }
 310 
 311         /* read in first block of bytes */
 312         if ((count = read(temp, prntbuf, GBUFSIZ)) <= 0) {
 313                 (void) close(temp);
 314 
 315                 if (cflag && !qflag) {
 316                         if (nfile > 1 && !hflag && file)
 317                                 (void) fprintf(stdout, "%s:", file);
 318                         if (!rflag)
 319                         (void) fprintf(stdout, "%lld\n", tln);
 320                 }
 321                 return;
 322         }
 323 
 324         lnum = 0;
 325         ptr = prntbuf;
 326         for (;;) {
 327                 /* look for next newline */
 328                 if ((ptrend = memchr(ptr + offset, '\n', count)) == NULL) {
 329                         offset += count;
 330 
 331                         /*
 332                          * shift unused data to the beginning of the buffer
 333                          */
 334                         if (ptr > prntbuf) {
 335                                 (void) memmove(prntbuf, ptr, offset);
 336                                 ptr = prntbuf;


 393                 } else
 394                         /*
 395                          * Use record as is
 396                          */
 397                         lbuf = ptr;
 398 
 399                 /* lflag only once */
 400                 if ((step(lbuf, expbuf) ^ vflag) && succeed(file) == 1)
 401                         break;
 402 
 403                 if (!nlflag)
 404                         break;
 405 
 406                 ptr = next_ptr;
 407                 count = next_count;
 408                 offset = 0;
 409         }
 410         (void) close(temp);
 411 
 412         if (cflag && !qflag) {
 413                 if (!hflag && file && (nfile > 1 ||
 414                     (rflag && outfn)))
 415                         (void) fprintf(stdout, "%s:", file);
 416                 (void) fprintf(stdout, "%lld\n", tln);
 417         }
 418 }
 419 
 420 static int
 421 succeed(const char *f)
 422 {
 423         int nchars;
 424         nsucc = (nsucc == 2) ? 2 : 1;
 425 
 426         if (f == NULL)
 427                 f = "<stdin>";
 428 
 429         if (qflag) {
 430                 /* no need to continue */
 431                 return (1);
 432         }
 433 
 434         if (cflag) {
 435                 tln++;
 436                 return (0);
 437         }
 438 
 439         if (lflag) {
 440                 (void) fprintf(stdout, "%s\n", f);
 441                 return (1);
 442         }
 443 
 444         if (!hflag && (nfile > 1 || (rflag && outfn))) {
 445                 /* print filename */
 446                 (void) fprintf(stdout, "%s:", f);
 447         }
 448 
 449         if (bflag)
 450                 /* print block number */
 451                 (void) fprintf(stdout, "%lld:", (offset_t)
 452                     ((lseek(temp, (off_t)0, SEEK_CUR) - 1) / BLKSIZE));
 453 
 454         if (nflag)
 455                 /* print line number */
 456                 (void) fprintf(stdout, "%lld:", lnum);
 457 
 458         if (nlflag) {
 459                 /* newline at end of line */
 460                 *ptrend = '\n';
 461                 nchars = ptrend - ptr + 1;
 462         } else {
 463                 /* don't write sentinel \0 */
 464                 nchars = ptrend - ptr;




  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  28 /*        All Rights Reserved   */
  29 
  30 /*      Copyright (c) 1987, 1988 Microsoft Corporation  */
  31 /*        All Rights Reserved   */
  32 
  33 /* Copyright 2012 Nexenta Systems, Inc.  All rights reserved. */
  34 
  35 /*
  36  * Copyright 2013 Damian Bogel. All rights reserved.
  37  */
  38 
  39 /*
  40  * grep -- print lines matching (or not matching) a pattern
  41  *
  42  *      status returns:
  43  *              0 - ok, and some matches
  44  *              1 - ok, but no matches
  45  *              2 - some error
  46  */
  47 
  48 #include <sys/types.h>
  49 
  50 #include <ctype.h>
  51 #include <fcntl.h>
  52 #include <locale.h>
  53 #include <memory.h>
  54 #include <regexpr.h>
  55 #include <stdio.h>
  56 #include <stdlib.h>
  57 #include <string.h>
  58 #include <unistd.h>
  59 #include <ftw.h>
  60 #include <limits.h>
  61 #include <sys/param.h>
  62 
  63 static const char *errstr[] = {
  64         "Range endpoint too large.",
  65         "Bad number.",
  66         "``\\digit'' out of range.",
  67         "No remembered search string.",
  68         "\\( \\) imbalance.",
  69         "Too many \\(.",
  70         "More than 2 numbers given in \\{ \\}.",
  71         "} expected after \\.",
  72         "First number exceeds second in \\{ \\}.",
  73         "[ ] imbalance.",
  74         "Regular expression overflow.",
  75         "Illegal byte sequence.",
  76         "Unknown regexp error code!!",
  77         NULL
  78 };
  79 
  80 #define STDIN_FILENAME  gettext("(standard input)")
  81 
  82 #define errmsg(msg, arg)        (void) fprintf(stderr, gettext(msg), arg)
  83 #define BLKSIZE 512
  84 #define GBUFSIZ 8192
  85 #define MAX_DEPTH       1000
  86 
  87 static int      temp;
  88 static long long        lnum;
  89 static char     *linebuf;
  90 static char     *prntbuf = NULL;
  91 static long     fw_lPrntBufLen = 0;
  92 static int      nflag;
  93 static int      bflag;
  94 static int      lflag;
  95 static int      cflag;
  96 static int      rflag;
  97 static int      Rflag;
  98 static int      vflag;
  99 static int      sflag;
 100 static int      iflag;
 101 static int      wflag;
 102 static int      hflag;
 103 static int      Hflag;
 104 static int      qflag;
 105 static int      errflg;
 106 static int      nfile;
 107 static long long        tln;
 108 static int      nsucc;
 109 static int      outfn = 0;
 110 static int      nlflag;
 111 static char     *ptr, *ptrend;
 112 static char     *expbuf;
 113 
 114 static void     execute(const char *, int);
 115 static void     regerr(int);
 116 static void     prepare(const char *);
 117 static int      recursive(const char *, const struct stat *, int, struct FTW *);
 118 static int      succeed(const char *);
 119 
 120 int
 121 main(int argc, char **argv)
 122 {
 123         int     c;
 124         char    *arg;
 125         extern int      optind;
 126 
 127         (void) setlocale(LC_ALL, "");
 128 #if !defined(TEXT_DOMAIN)       /* Should be defined by cc -D */
 129 #define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
 130 #endif
 131         (void) textdomain(TEXT_DOMAIN);
 132 
 133         while ((c = getopt(argc, argv, "hHqblcnRrsviyw")) != -1)
 134                 switch (c) {
 135                 /* based on options order h or H is set as in GNU grep */
 136                 case 'h':
 137                         hflag++;
 138                         Hflag = 0; /* h excludes H */
 139                         break;
 140                 case 'H':
 141                         if (!lflag) /* H is excluded by l */
 142                                 Hflag++;
 143                         hflag = 0; /* H excludes h */
 144                         break;
 145                 case 'q':       /* POSIX: quiet: status only */
 146                         qflag++;
 147                         break;
 148                 case 'v':
 149                         vflag++;
 150                         break;
 151                 case 'c':
 152                         cflag++;
 153                         break;
 154                 case 'n':
 155                         nflag++;
 156                         break;
 157                 case 'R':
 158                         Rflag++;
 159                         /* FALLTHROUGH */
 160                 case 'r':
 161                         rflag++;
 162                         break;
 163                 case 'b':
 164                         bflag++;
 165                         break;
 166                 case 's':
 167                         sflag++;
 168                         break;
 169                 case 'l':
 170                         lflag++;
 171                         Hflag = 0; /* l excludes H */
 172                         break;
 173                 case 'y':
 174                 case 'i':
 175                         iflag++;
 176                         break;
 177                 case 'w':
 178                         wflag++;
 179                         break;
 180                 case '?':
 181                         errflg++;
 182                 }
 183 
 184         if (errflg || (optind >= argc)) {
 185                 errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hHbnsviw "
 186                     "pattern file . . .\n",
 187                     (char *)NULL);
 188                 exit(2);
 189         }
 190 
 191         argv = &argv[optind];
 192         argc -= optind;
 193         nfile = argc - 1;
 194 
 195         if (strrchr(*argv, '\n') != NULL)
 196                 regerr(41);
 197 
 198         if (iflag) {
 199                 for (arg = *argv; *arg != NULL; ++arg)
 200                         *arg = (char)tolower((int)((unsigned char)*arg));
 201         }
 202 
 203         if (wflag) {
 204                 unsigned int    wordlen;
 205                 char            *wordbuf;


 297 execute(const char *file, int base)
 298 {
 299         char    *lbuf, *p;
 300         long    count;
 301         long    offset = 0;
 302         char    *next_ptr = NULL;
 303         long    next_count = 0;
 304 
 305         tln = 0;
 306 
 307         if (prntbuf == NULL) {
 308                 fw_lPrntBufLen = GBUFSIZ + 1;
 309                 if ((prntbuf = malloc(fw_lPrntBufLen)) == NULL) {
 310                         exit(2); /* out of memory - BAIL */
 311                 }
 312                 if ((linebuf = malloc(fw_lPrntBufLen)) == NULL) {
 313                         exit(2); /* out of memory - BAIL */
 314                 }
 315         }
 316 
 317         if (file == NULL) {
 318                 temp = 0;
 319                 file = STDIN_FILENAME;
 320         } else if ((temp = open(file + base, O_RDONLY)) == -1) {
 321                 if (!sflag)
 322                         errmsg("grep: can't open %s\n", file);
 323                 nsucc = 2;
 324                 return;
 325         }
 326 
 327         /* read in first block of bytes */
 328         if ((count = read(temp, prntbuf, GBUFSIZ)) <= 0) {
 329                 (void) close(temp);
 330 
 331                 if (cflag && !qflag) {
 332                         if (Hflag || (nfile > 1 && !hflag))
 333                                 (void) fprintf(stdout, "%s:", file);
 334                         if (!rflag)
 335                         (void) fprintf(stdout, "%lld\n", tln);
 336                 }
 337                 return;
 338         }
 339 
 340         lnum = 0;
 341         ptr = prntbuf;
 342         for (;;) {
 343                 /* look for next newline */
 344                 if ((ptrend = memchr(ptr + offset, '\n', count)) == NULL) {
 345                         offset += count;
 346 
 347                         /*
 348                          * shift unused data to the beginning of the buffer
 349                          */
 350                         if (ptr > prntbuf) {
 351                                 (void) memmove(prntbuf, ptr, offset);
 352                                 ptr = prntbuf;


 409                 } else
 410                         /*
 411                          * Use record as is
 412                          */
 413                         lbuf = ptr;
 414 
 415                 /* lflag only once */
 416                 if ((step(lbuf, expbuf) ^ vflag) && succeed(file) == 1)
 417                         break;
 418 
 419                 if (!nlflag)
 420                         break;
 421 
 422                 ptr = next_ptr;
 423                 count = next_count;
 424                 offset = 0;
 425         }
 426         (void) close(temp);
 427 
 428         if (cflag && !qflag) {
 429                 if (Hflag || (!hflag && ((nfile > 1) ||
 430                     (rflag && outfn))))
 431                         (void) fprintf(stdout, "%s:", file);
 432                 (void) fprintf(stdout, "%lld\n", tln);
 433         }
 434 }
 435 
 436 static int
 437 succeed(const char *f)
 438 {
 439         int nchars;
 440         nsucc = (nsucc == 2) ? 2 : 1;
 441 



 442         if (qflag) {
 443                 /* no need to continue */
 444                 return (1);
 445         }
 446 
 447         if (cflag) {
 448                 tln++;
 449                 return (0);
 450         }
 451 
 452         if (lflag) {
 453                 (void) fprintf(stdout, "%s\n", f);
 454                 return (1);
 455         }
 456 
 457         if (Hflag || (!hflag && (nfile > 1 || (rflag && outfn)))) {
 458                 /* print filename */
 459                 (void) fprintf(stdout, "%s:", f);
 460         }
 461 
 462         if (bflag)
 463                 /* print block number */
 464                 (void) fprintf(stdout, "%lld:", (offset_t)
 465                     ((lseek(temp, (off_t)0, SEEK_CUR) - 1) / BLKSIZE));
 466 
 467         if (nflag)
 468                 /* print line number */
 469                 (void) fprintf(stdout, "%lld:", lnum);
 470 
 471         if (nlflag) {
 472                 /* newline at end of line */
 473                 *ptrend = '\n';
 474                 nchars = ptrend - ptr + 1;
 475         } else {
 476                 /* don't write sentinel \0 */
 477                 nchars = ptrend - ptr;