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:
parent
4e57bc3338
commit
f45dd01002
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=185714
@ -35,7 +35,7 @@
|
|||||||
.\" @(#)wc.1 8.2 (Berkeley) 4/19/94
|
.\" @(#)wc.1 8.2 (Berkeley) 4/19/94
|
||||||
.\" $FreeBSD$
|
.\" $FreeBSD$
|
||||||
.\"
|
.\"
|
||||||
.Dd February 23, 2005
|
.Dd December 6, 2008
|
||||||
.Dt WC 1
|
.Dt WC 1
|
||||||
.Os
|
.Os
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
@ -43,7 +43,7 @@
|
|||||||
.Nd word, line, character, and byte count
|
.Nd word, line, character, and byte count
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.Nm
|
.Nm
|
||||||
.Op Fl clmw
|
.Op Fl Lclmw
|
||||||
.Op Ar
|
.Op Ar
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
||||||
The
|
The
|
||||||
@ -71,6 +71,15 @@ the last file.
|
|||||||
.Pp
|
.Pp
|
||||||
The following options are available:
|
The following options are available:
|
||||||
.Bl -tag -width indent
|
.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
|
.It Fl c
|
||||||
The number of bytes in each input file
|
The number of bytes in each input file
|
||||||
is written to the standard output.
|
is written to the standard output.
|
||||||
@ -129,6 +138,10 @@ and
|
|||||||
as well as the totals for both:
|
as well as the totals for both:
|
||||||
.Pp
|
.Pp
|
||||||
.Dl "wc -mlw report1 report2"
|
.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
|
.Sh COMPATIBILITY
|
||||||
Historically, the
|
Historically, the
|
||||||
.Nm
|
.Nm
|
||||||
@ -154,6 +167,16 @@ in terms of the
|
|||||||
.Xr iswspace 3
|
.Xr iswspace 3
|
||||||
function, as required by
|
function, as required by
|
||||||
.St -p1003.2 .
|
.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
|
.Sh SEE ALSO
|
||||||
.Xr iswspace 3
|
.Xr iswspace 3
|
||||||
.Sh STANDARDS
|
.Sh STANDARDS
|
||||||
|
@ -62,8 +62,8 @@ __FBSDID("$FreeBSD$");
|
|||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
#include <wctype.h>
|
#include <wctype.h>
|
||||||
|
|
||||||
uintmax_t tlinect, twordct, tcharct;
|
uintmax_t tlinect, twordct, tcharct, tlongline;
|
||||||
int doline, doword, dochar, domulti;
|
int doline, doword, dochar, domulti, dolongline;
|
||||||
|
|
||||||
static int cnt(const char *);
|
static int cnt(const char *);
|
||||||
static void usage(void);
|
static void usage(void);
|
||||||
@ -75,7 +75,7 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
(void) setlocale(LC_CTYPE, "");
|
(void) setlocale(LC_CTYPE, "");
|
||||||
|
|
||||||
while ((ch = getopt(argc, argv, "clmw")) != -1)
|
while ((ch = getopt(argc, argv, "clmwL")) != -1)
|
||||||
switch((char)ch) {
|
switch((char)ch) {
|
||||||
case 'l':
|
case 'l':
|
||||||
doline = 1;
|
doline = 1;
|
||||||
@ -87,6 +87,9 @@ main(int argc, char *argv[])
|
|||||||
dochar = 1;
|
dochar = 1;
|
||||||
domulti = 0;
|
domulti = 0;
|
||||||
break;
|
break;
|
||||||
|
case 'L':
|
||||||
|
dolongline = 1;
|
||||||
|
break;
|
||||||
case 'm':
|
case 'm':
|
||||||
domulti = 1;
|
domulti = 1;
|
||||||
dochar = 0;
|
dochar = 0;
|
||||||
@ -99,7 +102,7 @@ main(int argc, char *argv[])
|
|||||||
argc -= optind;
|
argc -= optind;
|
||||||
|
|
||||||
/* Wc's flags are on by default. */
|
/* Wc's flags are on by default. */
|
||||||
if (doline + doword + dochar + domulti == 0)
|
if (doline + doword + dochar + domulti + dolongline == 0)
|
||||||
doline = doword = dochar = 1;
|
doline = doword = dochar = 1;
|
||||||
|
|
||||||
errors = 0;
|
errors = 0;
|
||||||
@ -125,6 +128,8 @@ main(int argc, char *argv[])
|
|||||||
(void)printf(" %7ju", twordct);
|
(void)printf(" %7ju", twordct);
|
||||||
if (dochar || domulti)
|
if (dochar || domulti)
|
||||||
(void)printf(" %7ju", tcharct);
|
(void)printf(" %7ju", tcharct);
|
||||||
|
if (dolongline)
|
||||||
|
(void)printf(" %7ju", tlongline);
|
||||||
(void)printf(" total\n");
|
(void)printf(" total\n");
|
||||||
}
|
}
|
||||||
exit(errors == 0 ? 0 : 1);
|
exit(errors == 0 ? 0 : 1);
|
||||||
@ -134,7 +139,7 @@ static int
|
|||||||
cnt(const char *file)
|
cnt(const char *file)
|
||||||
{
|
{
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
uintmax_t linect, wordct, charct;
|
uintmax_t linect, wordct, charct, llct, tmpll;
|
||||||
int fd, len, warned;
|
int fd, len, warned;
|
||||||
size_t clen;
|
size_t clen;
|
||||||
short gotsp;
|
short gotsp;
|
||||||
@ -143,7 +148,7 @@ cnt(const char *file)
|
|||||||
wchar_t wch;
|
wchar_t wch;
|
||||||
mbstate_t mbs;
|
mbstate_t mbs;
|
||||||
|
|
||||||
linect = wordct = charct = 0;
|
linect = wordct = charct = llct = tmpll = 0;
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
file = "stdin";
|
file = "stdin";
|
||||||
fd = STDIN_FILENO;
|
fd = STDIN_FILENO;
|
||||||
@ -168,8 +173,13 @@ cnt(const char *file)
|
|||||||
}
|
}
|
||||||
charct += len;
|
charct += len;
|
||||||
for (p = buf; len--; ++p)
|
for (p = buf; len--; ++p)
|
||||||
if (*p == '\n')
|
if (*p == '\n') {
|
||||||
|
if (tmpll > llct)
|
||||||
|
llct = tmpll;
|
||||||
|
tmpll = 0;
|
||||||
++linect;
|
++linect;
|
||||||
|
} else
|
||||||
|
tmpll++;
|
||||||
}
|
}
|
||||||
tlinect += linect;
|
tlinect += linect;
|
||||||
(void)printf(" %7ju", linect);
|
(void)printf(" %7ju", linect);
|
||||||
@ -177,6 +187,11 @@ cnt(const char *file)
|
|||||||
tcharct += charct;
|
tcharct += charct;
|
||||||
(void)printf(" %7ju", charct);
|
(void)printf(" %7ju", charct);
|
||||||
}
|
}
|
||||||
|
if (dolongline) {
|
||||||
|
if (llct > tlongline)
|
||||||
|
tlongline = llct;
|
||||||
|
(void)printf(" %7ju", tlongline);
|
||||||
|
}
|
||||||
(void)close(fd);
|
(void)close(fd);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
@ -229,10 +244,16 @@ word: gotsp = 1;
|
|||||||
else if (clen == 0)
|
else if (clen == 0)
|
||||||
clen = 1;
|
clen = 1;
|
||||||
charct++;
|
charct++;
|
||||||
|
if (wch != L'\n')
|
||||||
|
tmpll++;
|
||||||
len -= clen;
|
len -= clen;
|
||||||
p += clen;
|
p += clen;
|
||||||
if (wch == L'\n')
|
if (wch == L'\n') {
|
||||||
|
if (tmpll > llct)
|
||||||
|
llct = tmpll;
|
||||||
|
tmpll = 0;
|
||||||
++linect;
|
++linect;
|
||||||
|
}
|
||||||
if (iswspace(wch))
|
if (iswspace(wch))
|
||||||
gotsp = 1;
|
gotsp = 1;
|
||||||
else if (gotsp) {
|
else if (gotsp) {
|
||||||
@ -256,6 +277,11 @@ word: gotsp = 1;
|
|||||||
tcharct += charct;
|
tcharct += charct;
|
||||||
(void)printf(" %7ju", charct);
|
(void)printf(" %7ju", charct);
|
||||||
}
|
}
|
||||||
|
if (dolongline) {
|
||||||
|
if (llct > tlongline)
|
||||||
|
tlongline = llct;
|
||||||
|
(void)printf(" %7ju", llct);
|
||||||
|
}
|
||||||
(void)close(fd);
|
(void)close(fd);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
@ -263,6 +289,6 @@ word: gotsp = 1;
|
|||||||
static void
|
static void
|
||||||
usage()
|
usage()
|
||||||
{
|
{
|
||||||
(void)fprintf(stderr, "usage: wc [-clmw] [file ...]\n");
|
(void)fprintf(stderr, "usage: wc [-Lclmw] [file ...]\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user