xref: /titanic_51/usr/src/lib/libcmd/common/wc.c (revision 491f61a1e1c1fc54a47bbcf53dbbbe1293b93b27)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1992-2008 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
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 
29 static const char usage[] =
30 "[-?\n@(#)$Id: wc (AT&T Research) 2006-08-25 $\n]"
31 USAGE_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.]"
54 "\n"
55 "\n[file ...]\n"
56 "\n"
57 "[+EXIT STATUS?]{"
58         "[+0?All files processed successfully.]"
59         "[+>0?One or more files failed to open or could not be read.]"
60 "}"
61 "[+SEE ALSO?\bcat\b(1), \bisspace\b(3)]"
62 ;
63 
64 
65 #include <cmd.h>
66 #include <wc.h>
67 #include <ls.h>
68 
69 #define ERRORMAX	125
70 
71 static void printout(register Wc_t *wp, register char *name,register int mode)
72 {
73 	if(mode&WC_LINES)
74 		sfprintf(sfstdout," %7I*d",sizeof(wp->lines),wp->lines);
75 	if(mode&WC_WORDS)
76 		sfprintf(sfstdout," %7I*d",sizeof(wp->words),wp->words);
77 	if(mode&WC_CHARS)
78 		sfprintf(sfstdout," %7I*d",sizeof(wp->chars),wp->chars);
79 	if(mode&WC_LONGEST)
80 		sfprintf(sfstdout," %7I*d",sizeof(wp->chars),wp->longest);
81 	if(name)
82 		sfprintf(sfstdout," %s",name);
83 	sfputc(sfstdout,'\n');
84 }
85 
86 int
87 b_wc(int argc,register char **argv, void* context)
88 {
89 	register char	*cp;
90 	register int	mode=0, n;
91 	register Wc_t	*wp;
92 	Sfio_t		*fp;
93 	Sfoff_t		tlines=0, twords=0, tchars=0;
94 	struct stat	statb;
95 
96 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
97 	while (n = optget(argv,usage)) switch (n)
98 	{
99 	case 'c':
100 		mode |= WC_CHARS;
101 		break;
102 	case 'l':
103 		mode |= WC_LINES;
104 		break;
105 	case 'L':
106 		mode |= WC_LONGEST;
107 		break;
108 	case 'm':
109 	case 'C':
110 		mode |= WC_MBYTE;
111 		break;
112 	case 'q':
113 		mode |= WC_QUIET;
114 		break;
115 	case 'w':
116 		mode |= WC_WORDS;
117 		break;
118 	case ':':
119 		error(2, "%s", opt_info.arg);
120 		break;
121 	case '?':
122 		error(ERROR_usage(2), "%s", opt_info.arg);
123 		break;
124 	}
125 	argv += opt_info.index;
126 	if (error_info.errors)
127 		error(ERROR_usage(2), "%s", optusage(NiL));
128 	if(mode&WC_MBYTE)
129 	{
130 		if(mode&WC_CHARS)
131 			error(2, "-c and -C are mutually exclusive");
132 		mode |= WC_CHARS;
133 		if(!mbwide())
134 		{
135 			mode &= ~WC_MBYTE;
136 			setlocale(LC_CTYPE, "C");
137 		}
138 	}
139 	if(!(mode&(WC_WORDS|WC_CHARS|WC_LINES|WC_MBYTE|WC_LONGEST)))
140 		mode |= (WC_WORDS|WC_CHARS|WC_LINES);
141 	if(!(wp = wc_init(mode)))
142 		error(3,"internal error");
143 	if(!(mode&WC_WORDS))
144 	{
145 		memzero(wp->space, (1<<CHAR_BIT));
146 		wp->space['\n'] = -1;
147 	}
148 	if(cp = *argv)
149 		argv++;
150 	do
151 	{
152 		if(!cp || streq(cp,"-"))
153 			fp = sfstdin;
154 		else if(!(fp = sfopen(NiL,cp,"r")))
155 		{
156 			error(ERROR_system(0),"%s: cannot open",cp);
157 			continue;
158 		}
159 		if(cp)
160 			n++;
161 		if(!(mode&(WC_WORDS|WC_LINES|WC_MBYTE|WC_LONGEST)) && fstat(sffileno(fp),&statb)>=0
162 			 && S_ISREG(statb.st_mode))
163 		{
164 			wp->chars = statb.st_size - lseek(sffileno(fp),0L,1);
165 			lseek(sffileno(fp),0L,2);
166 		}
167 		else
168 			wc_count(wp, fp, cp);
169 		if(fp!=sfstdin)
170 			sfclose(fp);
171 		tchars += wp->chars;
172 		twords += wp->words;
173 		tlines += wp->lines;
174 		printout(wp,cp,mode);
175 	}
176 	while(cp= *argv++);
177 	if(n>1)
178 	{
179 		wp->lines = tlines;
180 		wp->chars = tchars;
181 		wp->words = twords;
182 		printout(wp,"total",mode);
183 	}
184 	return(error_info.errors<ERRORMAX?error_info.errors:ERRORMAX);
185 }
186 
187