home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Professional
/
OS2PRO194.ISO
/
os2
/
prgramer
/
minix
/
wc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-12-26
|
5KB
|
217 lines
/*
* wc - count lines, words and characters Author: David
* Messer
*/
/* $Header: d:/rcs/D:/RCS/RCS/wc.c 1.1 89/12/27 03:07:23 RCA Exp $
* $Log: wc.c $
* Revision 1.1 89/12/27 03:07:23 RCA
* Initial revision
*
*/
#include "stdio.h"
#define isdigit(c) (c >= '0' && c <= '9')
#define isspace(c) (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r')
/*
* Usage: wc [-lwc] [names]
*
* Flags: l - count lines. w - count words. c - count characters.
*
* Flags l, w, and c are default. Words are delimited by any
* non-alphabetic character.
*
* Released into the PUBLIC-DOMAIN 02/10/86
*
* If you find this program to be of use to you, a donation of
* whatever you think it is worth will be cheerfully accepted.
*
* Written by: David L. Messer P.O. Box 19130, Mpls, MN, 55119
* Program (heavily) modified by Andy Tanenbaum */
int lflag; /* Count lines */
int wflag; /* Count words */
int cflag; /* Count characters */
long lcount; /* Count of lines */
long wcount; /* Count of words */
long ccount; /* Count of characters */
long ltotal; /* Total count of lines */
long wtotal; /* Total count of words */
long ctotal; /* Total count of characters */
main(argc, argv)
int argc;
char *argv[];
{
int k;
char *cp;
int tflag, files;
int i;
if (argc == 1)
{
printf ("\n █▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█");
printf ("\n █ WC (Count words, lines, chars) $Author: RCA $ █");
printf ("\n █ $Date: 89/12/27 03:07:23 $ $Revision: 1.1 $ █");
printf ("\n █ Usage: WC [-lwc] filename(s) █");
printf ("\n █ Note: Words are delimited by any non-alphanumeric █");
printf ("\n █ character. █");
printf ("\n █ Options: -l: Print number of lines. █");
printf ("\n █ -w: Print number of words. █");
printf ("\n █ -c: Print number of characters. █");
printf ("\n █ [ -lwc is the default ] █");
printf ("\n █ OS: Works under OS/2 or DOS. █");
printf ("\n █ Credits: David Messer, Andy Tannenbaum, K&R. █");
printf ("\n █▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█\n");
exit(1);
}
/* Get flags. */
files = argc - 1;
k = 1;
cp = argv[1];
if (argc > 1 && *cp++ == '-')
{
files--;
k++; /* points to first file */
while (*cp != 0)
{
switch (*cp)
{
case 'l':
lflag++;
break;
case 'w':
wflag++;
break;
case 'c':
cflag++;
break;
default:
usage();
}
cp++;
}
}
/* If no flags are set, treat as wc -lwc. */
if (!lflag && !wflag && !cflag)
{
lflag = 1;
wflag = 1;
cflag = 1;
}
/* Process files. */
tflag = files >= 2; /* set if # files > 1 */
/* Check to see if input comes from std input. */
if (k >= argc)
{
count();
/*
* if(lflag) printf(" %6D", lcount); if(wflag) printf(" %6D",
* wcount); if(cflag) printf(" %6D", ccount); zxzxzx (edit
* tag)
*/
if (lflag)
printf(" %6d", lcount);
if (wflag)
printf(" %6d", wcount);
if (cflag)
printf(" %6d", ccount);
printf(" \n");
fflush(stdout);
exit(0);
}
/* There is an explicit list of files. Loop on files. */
while (k < argc)
{
fclose(stdin);
if (fopen(argv[k], "r") == NULL)
{
/*
* std_err("wc: cannot open "); std_err(argv[k]);
* std_err("\n");
*/
printf("wc: cannot open ");
printf(argv[k]);
printf("\n");
k++;
continue;
} else
{
/* Next file has been opened as std input. */
count();
/*
* if(lflag) printf(" %6D", lcount); if(wflag) printf("
* %6D", wcount); if(cflag) printf(" %6D", ccount);
* printf(" %s\n", argv[k]);
*/
if (lflag)
printf(" %6d", lcount);
if (wflag)
printf(" %6d", wcount);
if (cflag)
printf(" %6d", ccount);
printf(" %s\n", argv[k]);
}
k++;
}
if (tflag)
{
/*
* if(lflag) printf(" %6D", ltotal); if(wflag) printf(" %6D",
* wtotal); if(cflag) printf(" %6D", ctotal); printf("
* total\n");
*/
if (lflag)
printf(" %6d", ltotal);
if (wflag)
printf(" %6d", wtotal);
if (cflag)
printf(" %6d", ctotal);
printf(" total\n");
}
fflush(stdout);
exit(0);
}
count()
{
register int c;
register int word = 0;
lcount = 0;
wcount = 0;
ccount = 0L;
while ((c = getc(stdin)) > 0)
{
ccount++;
if (isspace(c))
{
if (word)
wcount++;
word = 0;
} else
{
word = 1;
}
if (c == '\n' || c == '\f')
lcount++;
}
ltotal += lcount;
wtotal += wcount;
ctotal += ccount;
}
usage()
{
/*
* std_err("Usage: wc [-lwc] [name ...]\n");
*/
printf("Usage: wc [-lwc] [name ...]\n");
exit(1);
}