PERFORCE change 143423 for review
Gabor Kovesdan
gabor at FreeBSD.org
Fri Jun 13 19:18:37 UTC 2008
http://perforce.freebsd.org/chv.cgi?CH=143423
Change 143423 by gabor at gabor_server on 2008/06/13 19:18:29
- Implement -m / --max-count
Affected files ...
.. //depot/projects/soc2008/gabor_textproc/grep/grep.1#9 edit
.. //depot/projects/soc2008/gabor_textproc/grep/grep.c#17 edit
.. //depot/projects/soc2008/gabor_textproc/grep/grep.h#13 edit
.. //depot/projects/soc2008/gabor_textproc/grep/util.c#14 edit
Differences ...
==== //depot/projects/soc2008/gabor_textproc/grep/grep.1#9 (text+ko) ====
@@ -29,7 +29,7 @@
.\"
.\" @(#)grep.1 8.3 (Berkeley) 4/18/94
.\"
-.Dd 6 Jun, 2008
+.Dd 13 Jun, 2008
.Dt GREP 1
.Os
.Sh NAME
@@ -39,7 +39,7 @@
.Sh SYNOPSIS
.Nm grep
.Bk -words
-.Op Fl abcdDEFGHhIiLlnOoPqRSsUVvwxZ
+.Op Fl abcdDEFGHhIiLlmnOoPqRSsUVvwxZ
.Op Fl A Ar num
.Op Fl B Ar num
.Op Fl C Ns Op Ar num
@@ -251,6 +251,10 @@
.Xr read 2
to read input, which can result in better performance under some
circumstances but can cause undefined behaiour.
+.It Fl m Ar num, Fl Fl max-count Ns = Ns Ar num
+Stop reading the file after
+.Ar num
+matches.
.It Fl n , Fl Fl line-number
Each output line is preceded by its relative line number in the file,
starting at line 1.
==== //depot/projects/soc2008/gabor_textproc/grep/grep.c#17 (text+ko) ====
@@ -84,6 +84,7 @@
int hflag; /* -h: don't print filename headers */
int iflag; /* -i: ignore case */
int lflag; /* -l: only show names of files with matches */
+int mflag; /* -m x: stop reading the files after x matches */
int nflag; /* -n: show line numbers in front of matching lines */
int oflag; /* -o: print only matching part */
int qflag; /* -q: quiet mode (don't output anything) */
@@ -95,6 +96,7 @@
int nullflag; /* --null */
char *label; /* --label */
char *color; /* --color */
+long long mcount; /* count for -m */
int binbehave = BIN_FILE_BIN;
@@ -124,7 +126,7 @@
usage(void)
{
fprintf(stderr,
- "usage: %s [-abcDEFGHhIiJLlnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]]\n"
+ "usage: %s [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]]\n"
"\t[-e pattern] [-f file] [--binary-files=value] [--color=when]\n"
"\t[--context[=num]] [--directories=action] [--label] [--line-buffered]\n"
"\t[--null] [pattern] [file ...]\n"
@@ -132,7 +134,7 @@
exit(2);
}
-static char *optstr = "0123456789A:B:CD:EFGHIJLOPSRUVZabcd:e:f:hilnoqrsuvwxy";
+static char *optstr = "0123456789A:B:CD:EFGHIJLOPSRUVZabcd:e:f:hilm:noqrsuvwxy";
struct option long_options[] =
{
@@ -163,8 +165,7 @@
{"bz2decompress", no_argument, NULL, 'J'},
{"files-with-matches", no_argument, NULL, 'l'},
{"files-without-match", no_argument, NULL, 'L'},
-/* XXX: UNIMPLEMENTED
- {"max-count", required_argument, NULL, 'm'}, */
+ {"max-count", required_argument, NULL, 'm'},
{"line-number", no_argument, NULL, 'n'},
{"only-matching", no_argument, NULL, 'o'},
{"quiet", no_argument, NULL, 'q'},
@@ -389,6 +390,10 @@
Lflag = 0;
lflag = qflag = 1;
break;
+ case 'm':
+ mflag++;
+ mcount = strtoll(optarg, (char **)NULL, 10);
+ break;
case 'n':
nflag = 1;
break;
==== //depot/projects/soc2008/gabor_textproc/grep/grep.h#13 (text+ko) ====
@@ -62,9 +62,10 @@
/* Command line flags */
extern int Aflag, Bflag, Dflag, Eflag, Fflag, Gflag, Hflag, Jflag, Lflag, Pflag,
Sflag, Rflag, Zflag,
- bflag, cflag, hflag, iflag, lflag, nflag, Oflag, oflag, qflag, sflag,
+ bflag, cflag, hflag, iflag, lflag, mflag, nflag, Oflag, oflag, qflag, sflag,
vflag, wflag, xflag,
nullflag;
+extern long long mcount;
extern char *color, *label;
extern int binbehave;
==== //depot/projects/soc2008/gabor_textproc/grep/util.c#14 (text+ko) ====
@@ -122,6 +122,9 @@
struct file *f;
int c, t, z, nottext;
+ if (mflag && (mcount <= 0))
+ return (0);
+
if (fn == NULL) {
if (label != NULL)
fn = label;
@@ -167,6 +170,12 @@
linesqueued++;
}
c += t;
+
+ if (mflag) {
+ mcount -= t;
+ if (mcount <= 0)
+ break;
+ }
}
if (Bflag > 0)
clearqueue();
More information about the p4-projects
mailing list