Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 5 Dec 2008 18:46:00 -0800
From:      "Sheldon Givens" <sheldon@sigsegv.ca>
To:        "Giorgos Keramidas" <keramida@ceid.upatras.gr>, freebsd-hackers@freebsd.org
Subject:   Re: Small change to wc
Message-ID:  <f4ecc0930812051846n27308fbdue6ea50a909d8d6ab@mail.gmail.com>
In-Reply-To: <871vwmtawz.fsf@kobe.laptop>
References:  <f4ecc0930812051414n17867e1fi80d6ed458e879bde@mail.gmail.com> <871vwmtawz.fsf@kobe.laptop>

next in thread | previous in thread | raw e-mail | index | archive | help
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 <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 @@
        (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
<keramida@ceid.upatras.gr>wrote:

> On Fri, 5 Dec 2008 14:14:32 -0800, "Sheldon Givens" <sheldon@sigsegv.ca>
> 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.
>



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?f4ecc0930812051846n27308fbdue6ea50a909d8d6ab>