From owner-freebsd-hackers@FreeBSD.ORG Sat Dec 6 02:46:01 2008 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 7890F1065672 for ; Sat, 6 Dec 2008 02:46:01 +0000 (UTC) (envelope-from sheldon@sigsegv.ca) Received: from wf-out-1314.google.com (wf-out-1314.google.com [209.85.200.168]) by mx1.freebsd.org (Postfix) with ESMTP id 53C058FC13 for ; Sat, 6 Dec 2008 02:46:01 +0000 (UTC) (envelope-from sheldon@sigsegv.ca) Received: by wf-out-1314.google.com with SMTP id 24so269012wfg.7 for ; Fri, 05 Dec 2008 18:46:01 -0800 (PST) Received: by 10.142.128.15 with SMTP id a15mr295620wfd.260.1228531560962; Fri, 05 Dec 2008 18:46:00 -0800 (PST) Received: by 10.142.136.4 with HTTP; Fri, 5 Dec 2008 18:46:00 -0800 (PST) Message-ID: Date: Fri, 5 Dec 2008 18:46:00 -0800 From: "Sheldon Givens" To: "Giorgos Keramidas" , freebsd-hackers@freebsd.org In-Reply-To: <871vwmtawz.fsf@kobe.laptop> MIME-Version: 1.0 References: <871vwmtawz.fsf@kobe.laptop> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Cc: Subject: Re: Small change to wc X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 06 Dec 2008 02:46:01 -0000 New diff -u: --- /usr/src/usr.bin/wc/wc.c 2004-12-27 14:27:56.000000000 -0800 +++ wc/wc.c 2008-12-05 14:33:21.000000000 -0800 @@ -62,8 +62,8 @@ #include #include -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 @@ (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; @@ -91,6 +91,9 @@ domulti = 1; dochar = 0; break; + case 'L': + dolongline = 1; + break; case '?': default: usage(); @@ -125,6 +128,8 @@ (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 @@ cnt(const char *file) { struct stat sb; - uintmax_t linect, wordct, charct; + uintmax_t linect, wordct, charct, llcnt, tmpll; int fd, len, warned; size_t clen; short gotsp; @@ -143,7 +148,7 @@ wchar_t wch; mbstate_t mbs; - linect = wordct = charct = 0; + linect = wordct = charct = llcnt = tmpll = 0; if (file == NULL) { file = "stdin"; fd = STDIN_FILENO; @@ -167,9 +172,13 @@ return (1); } charct += len; - for (p = buf; len--; ++p) - if (*p == '\n') + for (p = buf; len--; ++p) + if (*p == '\n') { + if (tmpll > llcnt) + llcnt = tmpll; + tmpll = 0; ++linect; + } else {tmpll++;} } tlinect += linect; (void)printf(" %7ju", linect); @@ -177,6 +186,10 @@ tcharct += charct; (void)printf(" %7ju", charct); } + if (dolongline) { + tlongline = llcnt; + (void)printf(" %7ju", tlongline); + } (void)close(fd); return (0); } @@ -194,7 +207,7 @@ (void)printf(" %7lld", (long long)sb.st_size); tcharct += sb.st_size; (void)close(fd); - return (0); + return (0); } } } @@ -229,10 +242,15 @@ else if (clen == 0) clen = 1; charct++; + tmpll++; len -= clen; p += clen; - if (wch == L'\n') + if (wch == L'\n') { + if (tmpll > llcnt) + llcnt = tmpll; + tmpll = 0; ++linect; + } if (iswspace(wch)) gotsp = 1; else if (gotsp) { @@ -256,6 +274,10 @@ tcharct += charct; (void)printf(" %7ju", charct); } + if (dolongline) { + tlongline = llcnt; + (void)printf(" %7ju", llcnt - 1); + } (void)close(fd); return (0); } @@ -263,6 +285,6 @@ static void usage() { - (void)fprintf(stderr, "usage: wc [-clmw] [file ...]\n"); + (void)fprintf(stderr, "usage: wc [-clmwL] [file ...]\n"); exit(1); } On Fri, Dec 5, 2008 at 4:17 PM, Giorgos Keramidas wrote: > On Fri, 5 Dec 2008 14:14:32 -0800, "Sheldon Givens" > wrote: > > Hello everyone, > > In the process of migrating the last of a few Linux servers to > > FreeBSD, we ran in to a bit of a snag with one of our scripts when BSD > > wc didn't have an equivalent to the Linux -L. This flag tells wc to > > keep track of the longest line in the input. > > > > Here's a little diff to add this functionality to BSD wc. > > > > With this patch, an additional parameter is added to output that shows > > the length of the longest line > > Adding the option to increase finger-compatibility and make shell > scripts a bit easier to port over sounds fine by me :) > > > My apologies if this is in the wrong format. I don't often post here. > > ---snip--- > > [patch] > > ---unsnip--- > > Can you post a `diff -u' or `diff -c' version of the patch? I like the > idea of the new option but it would be easier to read in -u/-c format. >