14887Schin /***********************************************************************
24887Schin * *
34887Schin * This software is part of the ast package *
4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1992-2010 AT&T Intellectual Property *
54887Schin * and is licensed under the *
64887Schin * Common Public License, Version 1.0 *
78462SApril.Chin@Sun.COM * by AT&T Intellectual Property *
84887Schin * *
94887Schin * A copy of the License is available at *
104887Schin * http://www.opensource.org/licenses/cpl1.0.txt *
114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
124887Schin * *
134887Schin * Information and Software Systems Research *
144887Schin * AT&T Research *
154887Schin * Florham Park NJ *
164887Schin * *
174887Schin * Glenn Fowler <gsf@research.att.com> *
184887Schin * David Korn <dgk@research.att.com> *
194887Schin * *
204887Schin ***********************************************************************/
214887Schin #pragma prototyped
224887Schin /*
234887Schin * David Korn
244887Schin * AT&T Bell Laboratories
254887Schin *
264887Schin * count the number of bytes, words, and lines in a file
274887Schin */
284887Schin
294887Schin static const char usage[] =
30*12068SRoger.Faulkner@Oracle.COM "[-?\n@(#)$Id: wc (AT&T Research) 2009-11-28 $\n]"
314887Schin USAGE_LICENSE
324887Schin "[+NAME?wc - print the number of bytes, words, and lines in files]"
334887Schin "[+DESCRIPTION?\bwc\b reads one or more input files and, by default, "
344887Schin "for each file writes a line containing the number of newlines, "
354887Schin "\aword\as, and bytes contained in each file followed by the "
364887Schin "file name to standard output in that order. A \aword\a is "
374887Schin "defined to be a non-zero length string delimited by \bisspace\b(3) "
384887Schin "characters.]"
394887Schin "[+?If more than one file is specified, \bwc\b writes a total count "
404887Schin "for all of the named files with \btotal\b written instead "
414887Schin "of the file name.]"
424887Schin "[+?By default, \bwc\b writes all three counts. Options can specified "
434887Schin "so that only certain counts are written. The options \b-c\b "
444887Schin "and \b-m\b are mutually exclusive.]"
454887Schin "[+?If no \afile\a is given, or if the \afile\a is \b-\b, \bwc\b "
464887Schin "reads from standard input and no filename is written to standard "
474887Schin "output. The start of the file is defined as the current offset.]"
484887Schin "[l:lines?List the line counts.]"
494887Schin "[w:words?List the word counts.]"
504887Schin "[c:bytes|chars:chars?List the byte counts.]"
514887Schin "[m|C:multibyte-chars?List the character counts.]"
524887Schin "[q:quiet?Suppress invalid multibyte character warnings.]"
5310898Sroland.mainz@nrubsig.org "[L:longest-line|max-line-length?List the longest line length; the newline,"
5410898Sroland.mainz@nrubsig.org "if any, is not counted in the length.]"
5510898Sroland.mainz@nrubsig.org "[N!:utf8?For \bUTF-8\b locales \b--noutf8\b disables \bUTF-8\b "
5610898Sroland.mainz@nrubsig.org "optimzations and relies on the native \bmbtowc\b(3).]"
574887Schin "\n"
584887Schin "\n[file ...]\n"
594887Schin "\n"
604887Schin "[+EXIT STATUS?]{"
614887Schin "[+0?All files processed successfully.]"
624887Schin "[+>0?One or more files failed to open or could not be read.]"
634887Schin "}"
644887Schin "[+SEE ALSO?\bcat\b(1), \bisspace\b(3)]"
654887Schin ;
664887Schin
674887Schin
684887Schin #include <cmd.h>
694887Schin #include <wc.h>
704887Schin #include <ls.h>
714887Schin
724887Schin #define ERRORMAX 125
734887Schin
printout(register Wc_t * wp,register char * name,register int mode)744887Schin static void printout(register Wc_t *wp, register char *name,register int mode)
754887Schin {
7610898Sroland.mainz@nrubsig.org if (mode&WC_LINES)
774887Schin sfprintf(sfstdout," %7I*d",sizeof(wp->lines),wp->lines);
7810898Sroland.mainz@nrubsig.org if (mode&WC_WORDS)
794887Schin sfprintf(sfstdout," %7I*d",sizeof(wp->words),wp->words);
8010898Sroland.mainz@nrubsig.org if (mode&WC_CHARS)
814887Schin sfprintf(sfstdout," %7I*d",sizeof(wp->chars),wp->chars);
8210898Sroland.mainz@nrubsig.org if (mode&WC_LONGEST)
834887Schin sfprintf(sfstdout," %7I*d",sizeof(wp->chars),wp->longest);
8410898Sroland.mainz@nrubsig.org if (name)
854887Schin sfprintf(sfstdout," %s",name);
864887Schin sfputc(sfstdout,'\n');
874887Schin }
884887Schin
894887Schin int
b_wc(int argc,register char ** argv,void * context)904887Schin b_wc(int argc,register char **argv, void* context)
914887Schin {
924887Schin register char *cp;
934887Schin register int mode=0, n;
944887Schin register Wc_t *wp;
954887Schin Sfio_t *fp;
964887Schin Sfoff_t tlines=0, twords=0, tchars=0;
974887Schin struct stat statb;
984887Schin
994887Schin cmdinit(argc, argv, context, ERROR_CATALOG, 0);
10010898Sroland.mainz@nrubsig.org for (;;)
1014887Schin {
10210898Sroland.mainz@nrubsig.org switch (optget(argv, usage))
10310898Sroland.mainz@nrubsig.org {
10410898Sroland.mainz@nrubsig.org case 'c':
10510898Sroland.mainz@nrubsig.org mode |= WC_CHARS;
10610898Sroland.mainz@nrubsig.org continue;
10710898Sroland.mainz@nrubsig.org case 'l':
10810898Sroland.mainz@nrubsig.org mode |= WC_LINES;
10910898Sroland.mainz@nrubsig.org continue;
11010898Sroland.mainz@nrubsig.org case 'L':
11110898Sroland.mainz@nrubsig.org mode |= WC_LONGEST;
11210898Sroland.mainz@nrubsig.org continue;
11310898Sroland.mainz@nrubsig.org case 'N':
11410898Sroland.mainz@nrubsig.org if (!opt_info.num)
11510898Sroland.mainz@nrubsig.org mode |= WC_NOUTF8;
11610898Sroland.mainz@nrubsig.org continue;
11710898Sroland.mainz@nrubsig.org case 'm':
11810898Sroland.mainz@nrubsig.org case 'C':
11910898Sroland.mainz@nrubsig.org mode |= WC_MBYTE;
12010898Sroland.mainz@nrubsig.org continue;
12110898Sroland.mainz@nrubsig.org case 'q':
12210898Sroland.mainz@nrubsig.org mode |= WC_QUIET;
12310898Sroland.mainz@nrubsig.org continue;
12410898Sroland.mainz@nrubsig.org case 'w':
12510898Sroland.mainz@nrubsig.org mode |= WC_WORDS;
12610898Sroland.mainz@nrubsig.org continue;
12710898Sroland.mainz@nrubsig.org case ':':
12810898Sroland.mainz@nrubsig.org error(2, "%s", opt_info.arg);
12910898Sroland.mainz@nrubsig.org break;
13010898Sroland.mainz@nrubsig.org case '?':
13110898Sroland.mainz@nrubsig.org error(ERROR_usage(2), "%s", opt_info.arg);
13210898Sroland.mainz@nrubsig.org break;
13310898Sroland.mainz@nrubsig.org }
1344887Schin break;
1354887Schin }
1364887Schin argv += opt_info.index;
1374887Schin if (error_info.errors)
1384887Schin error(ERROR_usage(2), "%s", optusage(NiL));
13910898Sroland.mainz@nrubsig.org if (mode&WC_MBYTE)
1404887Schin {
14110898Sroland.mainz@nrubsig.org if (mode&WC_CHARS)
1424887Schin error(2, "-c and -C are mutually exclusive");
14310898Sroland.mainz@nrubsig.org if (!mbwide())
14410898Sroland.mainz@nrubsig.org mode &= ~WC_MBYTE;
1454887Schin mode |= WC_CHARS;
1464887Schin }
14710898Sroland.mainz@nrubsig.org if (!(mode&(WC_WORDS|WC_CHARS|WC_LINES|WC_MBYTE|WC_LONGEST)))
1484887Schin mode |= (WC_WORDS|WC_CHARS|WC_LINES);
14910898Sroland.mainz@nrubsig.org if (!(wp = wc_init(mode)))
1504887Schin error(3,"internal error");
15110898Sroland.mainz@nrubsig.org if (cp = *argv)
1524887Schin argv++;
15310898Sroland.mainz@nrubsig.org n = 0;
1544887Schin do
1554887Schin {
15610898Sroland.mainz@nrubsig.org if (!cp || streq(cp,"-"))
1574887Schin fp = sfstdin;
15810898Sroland.mainz@nrubsig.org else if (!(fp = sfopen(NiL,cp,"r")))
1594887Schin {
1604887Schin error(ERROR_system(0),"%s: cannot open",cp);
1614887Schin continue;
1624887Schin }
16310898Sroland.mainz@nrubsig.org if (cp)
1644887Schin n++;
16510898Sroland.mainz@nrubsig.org if (!(mode&(WC_WORDS|WC_LINES|WC_MBYTE|WC_LONGEST)) && fstat(sffileno(fp),&statb)>=0
1664887Schin && S_ISREG(statb.st_mode))
1674887Schin {
1684887Schin wp->chars = statb.st_size - lseek(sffileno(fp),0L,1);
1694887Schin lseek(sffileno(fp),0L,2);
1704887Schin }
1714887Schin else
1724887Schin wc_count(wp, fp, cp);
17310898Sroland.mainz@nrubsig.org if (fp!=sfstdin)
1744887Schin sfclose(fp);
1754887Schin tchars += wp->chars;
1764887Schin twords += wp->words;
1774887Schin tlines += wp->lines;
1784887Schin printout(wp,cp,mode);
17910898Sroland.mainz@nrubsig.org } while (cp= *argv++);
18010898Sroland.mainz@nrubsig.org if (n > 1)
1814887Schin {
1824887Schin wp->lines = tlines;
1834887Schin wp->chars = tchars;
1844887Schin wp->words = twords;
1854887Schin printout(wp,"total",mode);
1864887Schin }
18710898Sroland.mainz@nrubsig.org return error_info.errors<ERRORMAX?error_info.errors:ERRORMAX;
1884887Schin }
189