wc.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* wc -- word and line count
*/
#include <stdio.h>
#include <limits.h>
#include <locale.h>
#include <wctype.h>
#include <stdlib.h>
#include <euc.h>
#define BUFSIZ 4096
unsigned char b[BUFSIZ];
unsigned long long wordct;
unsigned long long twordct;
unsigned long long linect;
unsigned long long tlinect;
unsigned long long charct;
unsigned long long tcharct;
unsigned long long real_charct;
unsigned long long real_tcharct;
static void wcp(unsigned long long, unsigned long long,
unsigned long long, unsigned long long);
static void usage(void);
int
{
unsigned int c;
int flag;
int i, token;
int status = 0;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
switch (flag) {
case 'c':
if (mflag)
usage();
cflag++;
break;
case 'C':
case 'm': /* POSIX.2 */
if (cflag)
usage();
mflag++;
break;
case 'l':
lflag++;
break;
case 'w':
wflag++;
break;
default:
usage();
break;
}
}
/*
* If no flags set, use defaults
*/
cflag = 1;
lflag = 1;
wflag = 1;
}
i = 0;
do {
"wc: cannot open %s\n"), argv[i]);
status = 2;
continue;
}
linect = 0;
wordct = 0;
charct = 0;
real_charct = 0;
token = 0;
errflag = 0;
for (;;) {
p1 = b;
if (c == 0) {
break;
/*
* skip the file and generate error
* message when failed to read the
* file.
*/
"wc: cannot read %s: "),
argv[i]);
perror("");
status = 2;
errflag = 1;
break;
}
}
charct += c;
}
c = *p1++;
real_charct++;
if (ISASCII(c)) {
if (isspace(c)) {
if (c == '\n')
linect++;
token = 0;
continue;
}
if (!token) {
wordct++;
token++;
}
} else {
p1--;
(unsigned int)MB_CUR_MAX) {
for (n = 0; n < len; n++)
b[n] = *p1++;
p1 = b;
if ((int)c > 0) {
charct += c;
p2 += c;
}
}
(unsigned int)MB_CUR_MAX)
len = (unsigned int)MB_CUR_MAX;
token = 0;
continue;
}
} else
p1++;
if (!token) {
wordct++;
token++;
}
}
}
/* print lines, words, chars */
if (errflag)
continue;
if (argc > 0) {
}
else
(void) printf("\n");
} while (++i < argc);
if (argc > 1) {
(void) printf(" total\n");
}
return (status);
}
static void
wcp(
unsigned long long charct,
unsigned long long wordct,
unsigned long long linect,
unsigned long long real_charct)
{
if (lflag)
" %llu", linect);
if (wflag)
" %llu", wordct);
if (cflag)
" %llu", charct);
else if (mflag)
" %llu", real_charct);
}
static void
usage()
{
"usage: wc [-c | -m | -C] [-lw] [file ...]\n"));
exit(2);
}