Blame src/lib/libcmd/wc.c

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