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>
*** 31,40 ****
--- 31,44 ----
/* All Rights Reserved */
/* Copyright 2012 Nexenta Systems, Inc. All rights reserved. */
/*
+ * Copyright 2013 Damian Bogel. All rights reserved.
+ */
+
+ /*
* grep -- print lines matching (or not matching) a pattern
*
* status returns:
* 0 - ok, and some matches
* 1 - ok, but no matches
*** 71,80 ****
--- 75,86 ----
"Illegal byte sequence.",
"Unknown regexp error code!!",
NULL
};
+ #define STDIN_FILENAME gettext("(standard input)")
+
#define errmsg(msg, arg) (void) fprintf(stderr, gettext(msg), arg)
#define BLKSIZE 512
#define GBUFSIZ 8192
#define MAX_DEPTH 1000
*** 92,101 ****
--- 98,108 ----
static int vflag;
static int sflag;
static int iflag;
static int wflag;
static int hflag;
+ static int Hflag;
static int qflag;
static int errflg;
static int nfile;
static long long tln;
static int nsucc;
*** 121,135 ****
#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, "hqblcnRrsviyw")) != -1)
switch (c) {
case 'h':
hflag++;
break;
case 'q': /* POSIX: quiet: status only */
qflag++;
break;
case 'v':
vflag++;
--- 128,149 ----
#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, "hHqblcnRrsviyw")) != -1)
switch (c) {
+ /* based on options order h or H is set as in GNU grep */
case 'h':
hflag++;
+ Hflag = 0; /* h excludes H */
break;
+ case 'H':
+ if (!lflag) /* H is excluded by l */
+ Hflag++;
+ hflag = 0; /* H excludes h */
+ break;
case 'q': /* POSIX: quiet: status only */
qflag++;
break;
case 'v':
vflag++;
*** 152,161 ****
--- 166,176 ----
case 's':
sflag++;
break;
case 'l':
lflag++;
+ Hflag = 0; /* l excludes H */
break;
case 'y':
case 'i':
iflag++;
break;
*** 165,175 ****
case '?':
errflg++;
}
if (errflg || (optind >= argc)) {
! errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hbnsviw "
"pattern file . . .\n",
(char *)NULL);
exit(2);
}
--- 180,190 ----
case '?':
errflg++;
}
if (errflg || (optind >= argc)) {
! errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hHbnsviw "
"pattern file . . .\n",
(char *)NULL);
exit(2);
}
*** 297,309 ****
if ((linebuf = malloc(fw_lPrntBufLen)) == NULL) {
exit(2); /* out of memory - BAIL */
}
}
! if (file == NULL)
temp = 0;
! else if ((temp = open(file + base, O_RDONLY)) == -1) {
if (!sflag)
errmsg("grep: can't open %s\n", file);
nsucc = 2;
return;
}
--- 312,325 ----
if ((linebuf = malloc(fw_lPrntBufLen)) == NULL) {
exit(2); /* out of memory - BAIL */
}
}
! if (file == NULL) {
temp = 0;
! file = STDIN_FILENAME;
! } else if ((temp = open(file + base, O_RDONLY)) == -1) {
if (!sflag)
errmsg("grep: can't open %s\n", file);
nsucc = 2;
return;
}
*** 311,321 ****
/* read in first block of bytes */
if ((count = read(temp, prntbuf, GBUFSIZ)) <= 0) {
(void) close(temp);
if (cflag && !qflag) {
! if (nfile > 1 && !hflag && file)
(void) fprintf(stdout, "%s:", file);
if (!rflag)
(void) fprintf(stdout, "%lld\n", tln);
}
return;
--- 327,337 ----
/* read in first block of bytes */
if ((count = read(temp, prntbuf, GBUFSIZ)) <= 0) {
(void) close(temp);
if (cflag && !qflag) {
! if (Hflag || (nfile > 1 && !hflag))
(void) fprintf(stdout, "%s:", file);
if (!rflag)
(void) fprintf(stdout, "%lld\n", tln);
}
return;
*** 408,419 ****
offset = 0;
}
(void) close(temp);
if (cflag && !qflag) {
! if (!hflag && file && (nfile > 1 ||
! (rflag && outfn)))
(void) fprintf(stdout, "%s:", file);
(void) fprintf(stdout, "%lld\n", tln);
}
}
--- 424,435 ----
offset = 0;
}
(void) close(temp);
if (cflag && !qflag) {
! if (Hflag || (!hflag && ((nfile > 1) ||
! (rflag && outfn))))
(void) fprintf(stdout, "%s:", file);
(void) fprintf(stdout, "%lld\n", tln);
}
}
*** 421,433 ****
succeed(const char *f)
{
int nchars;
nsucc = (nsucc == 2) ? 2 : 1;
- if (f == NULL)
- f = "<stdin>";
-
if (qflag) {
/* no need to continue */
return (1);
}
--- 437,446 ----
*** 439,449 ****
if (lflag) {
(void) fprintf(stdout, "%s\n", f);
return (1);
}
! if (!hflag && (nfile > 1 || (rflag && outfn))) {
/* print filename */
(void) fprintf(stdout, "%s:", f);
}
if (bflag)
--- 452,462 ----
if (lflag) {
(void) fprintf(stdout, "%s\n", f);
return (1);
}
! if (Hflag || (!hflag && (nfile > 1 || (rflag && outfn)))) {
/* print filename */
(void) fprintf(stdout, "%s:", f);
}
if (bflag)