1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1992-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * David Korn
1N/A * AT&T Bell Laboratories
1N/A *
1N/A * count the number of bytes, words, and lines in a file
1N/A */
1N/A
1N/Astatic const char usage[] =
1N/A"[-?\n@(#)$Id: wc (AT&T Research) 2009-11-28 $\n]"
1N/AUSAGE_LICENSE
1N/A"[+NAME?wc - print the number of bytes, words, and lines in files]"
1N/A"[+DESCRIPTION?\bwc\b reads one or more input files and, by default, "
1N/A "for each file writes a line containing the number of newlines, "
1N/A "\aword\as, and bytes contained in each file followed by the "
1N/A "file name to standard output in that order. A \aword\a is "
1N/A "defined to be a non-zero length string delimited by \bisspace\b(3) "
1N/A "characters.]"
1N/A"[+?If more than one file is specified, \bwc\b writes a total count "
1N/A "for all of the named files with \btotal\b written instead "
1N/A "of the file name.]"
1N/A"[+?By default, \bwc\b writes all three counts. Options can specified "
1N/A "so that only certain counts are written. The options \b-c\b "
1N/A "and \b-m\b are mutually exclusive.]"
1N/A"[+?If no \afile\a is given, or if the \afile\a is \b-\b, \bwc\b "
1N/A "reads from standard input and no filename is written to standard "
1N/A "output. The start of the file is defined as the current offset.]"
1N/A"[l:lines?List the line counts.]"
1N/A"[w:words?List the word counts.]"
1N/A"[c:bytes|chars:chars?List the byte counts.]"
1N/A"[m|C:multibyte-chars?List the character counts.]"
1N/A"[q:quiet?Suppress invalid multibyte character warnings.]"
1N/A"[L:longest-line|max-line-length?List the longest line length; the newline,"
1N/A "if any, is not counted in the length.]"
1N/A"[N!:utf8?For \bUTF-8\b locales \b--noutf8\b disables \bUTF-8\b "
1N/A "optimzations and relies on the native \bmbtowc\b(3).]"
1N/A"\n"
1N/A"\n[file ...]\n"
1N/A"\n"
1N/A"[+EXIT STATUS?]{"
1N/A "[+0?All files processed successfully.]"
1N/A "[+>0?One or more files failed to open or could not be read.]"
1N/A"}"
1N/A"[+SEE ALSO?\bcat\b(1), \bisspace\b(3)]"
1N/A;
1N/A
1N/A
1N/A#include <cmd.h>
1N/A#include <wc.h>
1N/A#include <ls.h>
1N/A
1N/A#define ERRORMAX 125
1N/A
1N/Astatic void printout(register Wc_t *wp, register char *name,register int mode)
1N/A{
1N/A if (mode&WC_LINES)
1N/A sfprintf(sfstdout," %7I*d",sizeof(wp->lines),wp->lines);
1N/A if (mode&WC_WORDS)
1N/A sfprintf(sfstdout," %7I*d",sizeof(wp->words),wp->words);
1N/A if (mode&WC_CHARS)
1N/A sfprintf(sfstdout," %7I*d",sizeof(wp->chars),wp->chars);
1N/A if (mode&WC_LONGEST)
1N/A sfprintf(sfstdout," %7I*d",sizeof(wp->chars),wp->longest);
1N/A if (name)
1N/A sfprintf(sfstdout," %s",name);
1N/A sfputc(sfstdout,'\n');
1N/A}
1N/A
1N/Aint
1N/Ab_wc(int argc,register char **argv, void* context)
1N/A{
1N/A register char *cp;
1N/A register int mode=0, n;
1N/A register Wc_t *wp;
1N/A Sfio_t *fp;
1N/A Sfoff_t tlines=0, twords=0, tchars=0;
1N/A struct stat statb;
1N/A
1N/A cmdinit(argc, argv, context, ERROR_CATALOG, 0);
1N/A for (;;)
1N/A {
1N/A switch (optget(argv, usage))
1N/A {
1N/A case 'c':
1N/A mode |= WC_CHARS;
1N/A continue;
1N/A case 'l':
1N/A mode |= WC_LINES;
1N/A continue;
1N/A case 'L':
1N/A mode |= WC_LONGEST;
1N/A continue;
1N/A case 'N':
1N/A if (!opt_info.num)
1N/A mode |= WC_NOUTF8;
1N/A continue;
1N/A case 'm':
1N/A case 'C':
1N/A mode |= WC_MBYTE;
1N/A continue;
1N/A case 'q':
1N/A mode |= WC_QUIET;
1N/A continue;
1N/A case 'w':
1N/A mode |= WC_WORDS;
1N/A continue;
1N/A case ':':
1N/A error(2, "%s", opt_info.arg);
1N/A break;
1N/A case '?':
1N/A error(ERROR_usage(2), "%s", opt_info.arg);
1N/A break;
1N/A }
1N/A break;
1N/A }
1N/A argv += opt_info.index;
1N/A if (error_info.errors)
1N/A error(ERROR_usage(2), "%s", optusage(NiL));
1N/A if (mode&WC_MBYTE)
1N/A {
1N/A if (mode&WC_CHARS)
1N/A error(2, "-c and -C are mutually exclusive");
1N/A if (!mbwide())
1N/A mode &= ~WC_MBYTE;
1N/A mode |= WC_CHARS;
1N/A }
1N/A if (!(mode&(WC_WORDS|WC_CHARS|WC_LINES|WC_MBYTE|WC_LONGEST)))
1N/A mode |= (WC_WORDS|WC_CHARS|WC_LINES);
1N/A if (!(wp = wc_init(mode)))
1N/A error(3,"internal error");
1N/A if (cp = *argv)
1N/A argv++;
1N/A n = 0;
1N/A do
1N/A {
1N/A if (!cp || streq(cp,"-"))
1N/A fp = sfstdin;
1N/A else if (!(fp = sfopen(NiL,cp,"r")))
1N/A {
1N/A error(ERROR_system(0),"%s: cannot open",cp);
1N/A continue;
1N/A }
1N/A if (cp)
1N/A n++;
1N/A if (!(mode&(WC_WORDS|WC_LINES|WC_MBYTE|WC_LONGEST)) && fstat(sffileno(fp),&statb)>=0
1N/A && S_ISREG(statb.st_mode))
1N/A {
1N/A wp->chars = statb.st_size - lseek(sffileno(fp),0L,1);
1N/A lseek(sffileno(fp),0L,2);
1N/A }
1N/A else
1N/A wc_count(wp, fp, cp);
1N/A if (fp!=sfstdin)
1N/A sfclose(fp);
1N/A tchars += wp->chars;
1N/A twords += wp->words;
1N/A tlines += wp->lines;
1N/A printout(wp,cp,mode);
1N/A } while (cp= *argv++);
1N/A if (n > 1)
1N/A {
1N/A wp->lines = tlines;
1N/A wp->chars = tchars;
1N/A wp->words = twords;
1N/A printout(wp,"total",mode);
1N/A }
1N/A return error_info.errors<ERRORMAX?error_info.errors:ERRORMAX;
1N/A}