1/***********************************************************************
2*                                                                      *
3*               This software is part of the ast package               *
4*          Copyright (c) 1992-2012 AT&T Intellectual Property          *
5*                      and is licensed under the                       *
6*                 Eclipse Public License, Version 1.0                  *
7*                    by AT&T Intellectual Property                     *
8*                                                                      *
9*                A copy of the License is available at                 *
10*          http://www.eclipse.org/org/documents/epl-v10.html           *
11*         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12*                                                                      *
13*              Information and Software Systems Research               *
14*                            AT&T Research                             *
15*                           Florham Park NJ                            *
16*                                                                      *
17*                 Glenn Fowler <gsf@research.att.com>                  *
18*                  David Korn <dgk@research.att.com>                   *
19*                                                                      *
20***********************************************************************/
21#pragma prototyped
22/*
23 * David Korn
24 * AT&T Bell Laboratories
25 *
26 * count the number of bytes, words, and lines in a file
27 */
28
29static const char usage[] =
30"[-?\n@(#)$Id: wc (AT&T Research) 2009-11-28 $\n]"
31USAGE_LICENSE
32"[+NAME?wc - print the number of bytes, words, and lines in files]"
33"[+DESCRIPTION?\bwc\b reads one or more input files and, by default, "
34	"for each file writes a line containing the number of newlines, "
35	"\aword\as, and bytes contained in each file followed by the "
36	"file name to standard output in that order.  A \aword\a is "
37	"defined to be a non-zero length string delimited by \bisspace\b(3) "
38	"characters.]"
39"[+?If more than one file is specified, \bwc\b writes a total count "
40	"for all of the named files with \btotal\b written instead "
41	"of the file name.]"
42"[+?By default, \bwc\b writes all three counts.  Options can specified "
43	"so that only certain counts are written.  The options \b-c\b "
44	"and \b-m\b are mutually exclusive.]"
45"[+?If no \afile\a is given, or if the \afile\a is \b-\b, \bwc\b "
46        "reads from standard input and no filename is written to standard "
47	"output.  The start of the file is defined as the current offset.]"
48"[l:lines?List the line counts.]"
49"[w:words?List the word counts.]"
50"[c:bytes|chars:chars?List the byte counts.]"
51"[m|C:multibyte-chars?List the character counts.]"
52"[q:quiet?Suppress invalid multibyte character warnings.]"
53"[L:longest-line|max-line-length?List the longest line length; the newline,"
54    "if any, is not counted in the length.]"
55"[N!:utf8?For \bUTF-8\b locales \b--noutf8\b disables \bUTF-8\b "
56    "optimzations and relies on the native \bmbtowc\b(3).]"
57"\n"
58"\n[file ...]\n"
59"\n"
60"[+EXIT STATUS?]{"
61        "[+0?All files processed successfully.]"
62        "[+>0?One or more files failed to open or could not be read.]"
63"}"
64"[+SEE ALSO?\bcat\b(1), \bisspace\b(3)]"
65;
66
67
68#include <cmd.h>
69#include <wc.h>
70#include <ls.h>
71
72#define ERRORMAX	125
73
74static void printout(register Wc_t *wp, register char *name,register int mode)
75{
76	if (mode&WC_LINES)
77		sfprintf(sfstdout," %7I*d",sizeof(wp->lines),wp->lines);
78	if (mode&WC_WORDS)
79		sfprintf(sfstdout," %7I*d",sizeof(wp->words),wp->words);
80	if (mode&WC_CHARS)
81		sfprintf(sfstdout," %7I*d",sizeof(wp->chars),wp->chars);
82	if (mode&WC_LONGEST)
83		sfprintf(sfstdout," %7I*d",sizeof(wp->chars),wp->longest);
84	if (name)
85		sfprintf(sfstdout," %s",name);
86	sfputc(sfstdout,'\n');
87}
88
89int
90b_wc(int argc,register char **argv, Shbltin_t* context)
91{
92	register char	*cp;
93	register int	mode=0, n;
94	register Wc_t	*wp;
95	Sfio_t		*fp;
96	Sfoff_t		tlines=0, twords=0, tchars=0;
97	struct stat	statb;
98
99	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
100	for (;;)
101	{
102		switch (optget(argv, usage))
103		{
104		case 'c':
105			mode |= WC_CHARS;
106			continue;
107		case 'l':
108			mode |= WC_LINES;
109			continue;
110		case 'L':
111			mode |= WC_LONGEST;
112			continue;
113		case 'N':
114			if (!opt_info.num)
115				mode |= WC_NOUTF8;
116			continue;
117		case 'm':
118		case 'C':
119			mode |= WC_MBYTE;
120			continue;
121		case 'q':
122			mode |= WC_QUIET;
123			continue;
124		case 'w':
125			mode |= WC_WORDS;
126			continue;
127		case ':':
128			error(2, "%s", opt_info.arg);
129			break;
130		case '?':
131			error(ERROR_usage(2), "%s", opt_info.arg);
132			break;
133		}
134		break;
135	}
136	argv += opt_info.index;
137	if (error_info.errors)
138		error(ERROR_usage(2), "%s", optusage(NiL));
139	if (mode&WC_MBYTE)
140	{
141		if (mode&WC_CHARS)
142			error(2, "-c and -C are mutually exclusive");
143		if (!mbwide())
144			mode &= ~WC_MBYTE;
145		mode |= WC_CHARS;
146	}
147	if (!(mode&(WC_WORDS|WC_CHARS|WC_LINES|WC_MBYTE|WC_LONGEST)))
148		mode |= (WC_WORDS|WC_CHARS|WC_LINES);
149	if (!(wp = wc_init(mode)))
150		error(3,"internal error");
151	if (cp = *argv)
152		argv++;
153	n = 0;
154	do
155	{
156		if (!cp || streq(cp,"-"))
157			fp = sfstdin;
158		else if (!(fp = sfopen(NiL,cp,"r")))
159		{
160			error(ERROR_system(0),"%s: cannot open",cp);
161			continue;
162		}
163		if (cp)
164			n++;
165		if (!(mode&(WC_WORDS|WC_LINES|WC_MBYTE|WC_LONGEST)) && fstat(sffileno(fp),&statb)>=0
166			 && S_ISREG(statb.st_mode))
167		{
168			wp->chars = statb.st_size - lseek(sffileno(fp),0L,1);
169			lseek(sffileno(fp),0L,2);
170		}
171		else
172			wc_count(wp, fp, cp);
173		if (fp!=sfstdin)
174			sfclose(fp);
175		tchars += wp->chars;
176		twords += wp->words;
177		tlines += wp->lines;
178		printout(wp,cp,mode);
179	} while (cp= *argv++);
180	if (n > 1)
181	{
182		wp->lines = tlines;
183		wp->chars = tchars;
184		wp->words = twords;
185		printout(wp,"total",mode);
186	}
187	return error_info.errors<ERRORMAX?error_info.errors:ERRORMAX;
188}
189