Add a -L option to wc(1), for finger compatibility with the GNU

wc utility.  The -L option can be used to report the length of
the longest line wc has seen in one or more files.  It is
disabled by default, and wc uses the standard `-lwc'.

Submitted by:	Sheldon Givens, sheldon at sigsegv.ca
Reviewed by:	kib
MFC after:	1 week
This commit is contained in:
Giorgos Keramidas 2008-12-06 19:21:56 +00:00
parent 4e57bc3338
commit f45dd01002
2 changed files with 60 additions and 11 deletions

View File

@ -35,7 +35,7 @@
.\" @(#)wc.1 8.2 (Berkeley) 4/19/94
.\" $FreeBSD$
.\"
.Dd February 23, 2005
.Dd December 6, 2008
.Dt WC 1
.Os
.Sh NAME
@ -43,7 +43,7 @@
.Nd word, line, character, and byte count
.Sh SYNOPSIS
.Nm
.Op Fl clmw
.Op Fl Lclmw
.Op Ar
.Sh DESCRIPTION
The
@ -71,6 +71,15 @@ the last file.
.Pp
The following options are available:
.Bl -tag -width indent
.It Fl L
The number of characters in the longest input line
is written to the standard output.
When more then one
.Ar file
argument is specified, the longest input line of
.Em all
files is reported as the value of the final
.Dq total .
.It Fl c
The number of bytes in each input file
is written to the standard output.
@ -129,6 +138,10 @@ and
as well as the totals for both:
.Pp
.Dl "wc -mlw report1 report2"
.Pp
Find the longest line in a list of files:
.Pp
.Dl "wc -L file1 file2 file3 | fgrep total"
.Sh COMPATIBILITY
Historically, the
.Nm
@ -154,6 +167,16 @@ in terms of the
.Xr iswspace 3
function, as required by
.St -p1003.2 .
.Pp
The
.Fl L
option is a non-standard
.Fx
extension, compatible with the
.Fl L
option of the GNU
.Nm
utility.
.Sh SEE ALSO
.Xr iswspace 3
.Sh STANDARDS

View File

@ -62,8 +62,8 @@ __FBSDID("$FreeBSD$");
#include <wchar.h>
#include <wctype.h>
uintmax_t tlinect, twordct, tcharct;
int doline, doword, dochar, domulti;
uintmax_t tlinect, twordct, tcharct, tlongline;
int doline, doword, dochar, domulti, dolongline;
static int cnt(const char *);
static void usage(void);
@ -75,7 +75,7 @@ main(int argc, char *argv[])
(void) setlocale(LC_CTYPE, "");
while ((ch = getopt(argc, argv, "clmw")) != -1)
while ((ch = getopt(argc, argv, "clmwL")) != -1)
switch((char)ch) {
case 'l':
doline = 1;
@ -87,6 +87,9 @@ main(int argc, char *argv[])
dochar = 1;
domulti = 0;
break;
case 'L':
dolongline = 1;
break;
case 'm':
domulti = 1;
dochar = 0;
@ -99,7 +102,7 @@ main(int argc, char *argv[])
argc -= optind;
/* Wc's flags are on by default. */
if (doline + doword + dochar + domulti == 0)
if (doline + doword + dochar + domulti + dolongline == 0)
doline = doword = dochar = 1;
errors = 0;
@ -125,6 +128,8 @@ main(int argc, char *argv[])
(void)printf(" %7ju", twordct);
if (dochar || domulti)
(void)printf(" %7ju", tcharct);
if (dolongline)
(void)printf(" %7ju", tlongline);
(void)printf(" total\n");
}
exit(errors == 0 ? 0 : 1);
@ -134,7 +139,7 @@ static int
cnt(const char *file)
{
struct stat sb;
uintmax_t linect, wordct, charct;
uintmax_t linect, wordct, charct, llct, tmpll;
int fd, len, warned;
size_t clen;
short gotsp;
@ -143,7 +148,7 @@ cnt(const char *file)
wchar_t wch;
mbstate_t mbs;
linect = wordct = charct = 0;
linect = wordct = charct = llct = tmpll = 0;
if (file == NULL) {
file = "stdin";
fd = STDIN_FILENO;
@ -168,8 +173,13 @@ cnt(const char *file)
}
charct += len;
for (p = buf; len--; ++p)
if (*p == '\n')
if (*p == '\n') {
if (tmpll > llct)
llct = tmpll;
tmpll = 0;
++linect;
} else
tmpll++;
}
tlinect += linect;
(void)printf(" %7ju", linect);
@ -177,6 +187,11 @@ cnt(const char *file)
tcharct += charct;
(void)printf(" %7ju", charct);
}
if (dolongline) {
if (llct > tlongline)
tlongline = llct;
(void)printf(" %7ju", tlongline);
}
(void)close(fd);
return (0);
}
@ -229,10 +244,16 @@ word: gotsp = 1;
else if (clen == 0)
clen = 1;
charct++;
if (wch != L'\n')
tmpll++;
len -= clen;
p += clen;
if (wch == L'\n')
if (wch == L'\n') {
if (tmpll > llct)
llct = tmpll;
tmpll = 0;
++linect;
}
if (iswspace(wch))
gotsp = 1;
else if (gotsp) {
@ -256,6 +277,11 @@ word: gotsp = 1;
tcharct += charct;
(void)printf(" %7ju", charct);
}
if (dolongline) {
if (llct > tlongline)
tlongline = llct;
(void)printf(" %7ju", llct);
}
(void)close(fd);
return (0);
}
@ -263,6 +289,6 @@ word: gotsp = 1;
static void
usage()
{
(void)fprintf(stderr, "usage: wc [-clmw] [file ...]\n");
(void)fprintf(stderr, "usage: wc [-Lclmw] [file ...]\n");
exit(1);
}