14887Schin /*********************************************************************** 24887Schin * * 34887Schin * This software is part of the ast package * 4*8462SApril.Chin@Sun.COM * Copyright (c) 1992-2008 AT&T Intellectual Property * 54887Schin * and is licensed under the * 64887Schin * Common Public License, Version 1.0 * 7*8462SApril.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[] = 304887Schin "[-?\n@(#)$Id: wc (AT&T Research) 2006-08-25 $\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.]" 534887Schin "[L:longest-line|max-line-length?List the longest line length.]" 544887Schin "\n" 554887Schin "\n[file ...]\n" 564887Schin "\n" 574887Schin "[+EXIT STATUS?]{" 584887Schin "[+0?All files processed successfully.]" 594887Schin "[+>0?One or more files failed to open or could not be read.]" 604887Schin "}" 614887Schin "[+SEE ALSO?\bcat\b(1), \bisspace\b(3)]" 624887Schin ; 634887Schin 644887Schin 654887Schin #include <cmd.h> 664887Schin #include <wc.h> 674887Schin #include <ls.h> 684887Schin 694887Schin #define ERRORMAX 125 704887Schin 714887Schin static void printout(register Wc_t *wp, register char *name,register int mode) 724887Schin { 734887Schin if(mode&WC_LINES) 744887Schin sfprintf(sfstdout," %7I*d",sizeof(wp->lines),wp->lines); 754887Schin if(mode&WC_WORDS) 764887Schin sfprintf(sfstdout," %7I*d",sizeof(wp->words),wp->words); 774887Schin if(mode&WC_CHARS) 784887Schin sfprintf(sfstdout," %7I*d",sizeof(wp->chars),wp->chars); 794887Schin if(mode&WC_LONGEST) 804887Schin sfprintf(sfstdout," %7I*d",sizeof(wp->chars),wp->longest); 814887Schin if(name) 824887Schin sfprintf(sfstdout," %s",name); 834887Schin sfputc(sfstdout,'\n'); 844887Schin } 854887Schin 864887Schin int 874887Schin b_wc(int argc,register char **argv, void* context) 884887Schin { 894887Schin register char *cp; 904887Schin register int mode=0, n; 914887Schin register Wc_t *wp; 924887Schin Sfio_t *fp; 934887Schin Sfoff_t tlines=0, twords=0, tchars=0; 944887Schin struct stat statb; 954887Schin 964887Schin cmdinit(argc, argv, context, ERROR_CATALOG, 0); 974887Schin while (n = optget(argv,usage)) switch (n) 984887Schin { 994887Schin case 'c': 1004887Schin mode |= WC_CHARS; 1014887Schin break; 1024887Schin case 'l': 1034887Schin mode |= WC_LINES; 1044887Schin break; 1054887Schin case 'L': 1064887Schin mode |= WC_LONGEST; 1074887Schin break; 1084887Schin case 'm': 1094887Schin case 'C': 1104887Schin mode |= WC_MBYTE; 1114887Schin break; 1124887Schin case 'q': 1134887Schin mode |= WC_QUIET; 1144887Schin break; 1154887Schin case 'w': 1164887Schin mode |= WC_WORDS; 1174887Schin break; 1184887Schin case ':': 1194887Schin error(2, "%s", opt_info.arg); 1204887Schin break; 1214887Schin case '?': 1224887Schin error(ERROR_usage(2), "%s", opt_info.arg); 1234887Schin break; 1244887Schin } 1254887Schin argv += opt_info.index; 1264887Schin if (error_info.errors) 1274887Schin error(ERROR_usage(2), "%s", optusage(NiL)); 1284887Schin if(mode&WC_MBYTE) 1294887Schin { 1304887Schin if(mode&WC_CHARS) 1314887Schin error(2, "-c and -C are mutually exclusive"); 1324887Schin mode |= WC_CHARS; 1334887Schin if(!mbwide()) 1344887Schin { 1354887Schin mode &= ~WC_MBYTE; 1364887Schin setlocale(LC_CTYPE, "C"); 1374887Schin } 1384887Schin } 1394887Schin if(!(mode&(WC_WORDS|WC_CHARS|WC_LINES|WC_MBYTE|WC_LONGEST))) 1404887Schin mode |= (WC_WORDS|WC_CHARS|WC_LINES); 1414887Schin if(!(wp = wc_init(mode))) 1424887Schin error(3,"internal error"); 1434887Schin if(!(mode&WC_WORDS)) 1444887Schin { 1454887Schin memzero(wp->space, (1<<CHAR_BIT)); 1464887Schin wp->space['\n'] = -1; 1474887Schin } 1484887Schin if(cp = *argv) 1494887Schin argv++; 1504887Schin do 1514887Schin { 1524887Schin if(!cp || streq(cp,"-")) 1534887Schin fp = sfstdin; 1544887Schin else if(!(fp = sfopen(NiL,cp,"r"))) 1554887Schin { 1564887Schin error(ERROR_system(0),"%s: cannot open",cp); 1574887Schin continue; 1584887Schin } 1594887Schin if(cp) 1604887Schin n++; 1614887Schin if(!(mode&(WC_WORDS|WC_LINES|WC_MBYTE|WC_LONGEST)) && fstat(sffileno(fp),&statb)>=0 1624887Schin && S_ISREG(statb.st_mode)) 1634887Schin { 1644887Schin wp->chars = statb.st_size - lseek(sffileno(fp),0L,1); 1654887Schin lseek(sffileno(fp),0L,2); 1664887Schin } 1674887Schin else 1684887Schin wc_count(wp, fp, cp); 1694887Schin if(fp!=sfstdin) 1704887Schin sfclose(fp); 1714887Schin tchars += wp->chars; 1724887Schin twords += wp->words; 1734887Schin tlines += wp->lines; 1744887Schin printout(wp,cp,mode); 1754887Schin } 1764887Schin while(cp= *argv++); 1774887Schin if(n>1) 1784887Schin { 1794887Schin wp->lines = tlines; 1804887Schin wp->chars = tchars; 1814887Schin wp->words = twords; 1824887Schin printout(wp,"total",mode); 1834887Schin } 1844887Schin return(error_info.errors<ERRORMAX?error_info.errors:ERRORMAX); 1854887Schin } 1864887Schin 187