home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.barnyard.co.uk
/
2015.02.ftp.barnyard.co.uk.tar
/
ftp.barnyard.co.uk
/
cpm
/
walnut-creek-CDROM
/
BEEHIVE
/
ZCAT
/
WORK-VMN.LBR
/
WC.C
< prev
next >
Wrap
Text File
|
2000-06-30
|
4KB
|
197 lines
/* wc.c -- text analysis utility -- written by leor zolman, 03/16/82.
'wc' prints out total number of characters, words, and lines in files
declared from the command line. 'words' are defined as groups of
characters delimited by blanks, tabs, or newlines. maximum number of
words and lines are each 65535, but character count is virtually
unlimited.
usage: >wc <fn1.ft1><cr>
>wc <fn1.ft1> [fn2.ft2] [etc.]<cr>
>wc <*.ft><cr>
>wc <fn.*><cr>
>wc <fn.*> [fn1.ft1] [etc.]<cr>
09/15/83 modified to compile with c/80 from software toolworks. fg */
#include "tprintf.c" /* from c/80 version 3.0 environment */
#define EOF -1
#define NULL 0
static int f1;
static unsigned lo_totchars, hi_totchars, totwords, totlines;
main(argc,argv)
int argc;
char **argv;
{
command(&argc,&argv);
if (argc < 2)
{
printf("Usage: >wc <fn1.ft1> [fn2.ft2] [etc.]\n");
printf(" >wc <*.ft> [fn.*] [etc.]\n");
exit(1);
}
lo_totchars = hi_totchars = totwords = totlines = 0;
printf("\n\t\tChars\tWords\tLines\n");
while (--argc)
dofile(*++argv);
printf("Totals:");
if (hi_totchars)
printf("\t\t%d%04d",hi_totchars,lo_totchars);
else
printf("\t\t%d",lo_totchars);
printf("\t%d\t%d\n",totwords,totlines);
}
dofile(name)
char *name;
{
char inword;
register int c;
static unsigned lo_tch, hi_tch, twords, tlines;
if ((f1 = fopen(name, "r")) == NULL)
{
printf("Can't open %s\n",name);
return;
}
printf("%s:\t",name);
if (strlen(name) < 7)
putchar('\t');
inword = lo_tch = hi_tch = twords = tlines = 0;
while ((c = getc(f1)) != EOF)
{
if (++lo_tch == 10000)
{
lo_tch = 0;
hi_tch++;
}
if (isspace(c))
{
if (inword)
{
inword = 0;
twords++;
}
}
else if (!inword)
inword = 1;
if (c == '\n')
tlines++;
}
fclose(f1);
if (hi_tch)
printf("%d%04d",hi_tch,lo_tch);
else
printf("%d",lo_tch);
printf("\t%d\t%d\n",twords,tlines);
if ((lo_totchars += lo_tch) >= 10000)
{
lo_totchars -= 10000;
hi_totchars++;
}
hi_totchars += hi_tch;
totwords += twords;
totlines += tlines;
}
/* command -- expand wildcard filenames in the command line.
usage: 'command(&argc, &argv)' modifies argc and argv as necessary.
uses 'sbrk' to create new arg list. also uses 'makfcb()' and 'bdos()'
from c/80 'stdlib.c' source file. written by dr. jim gillogly;
modified for cp/m by walt bilofsky. (7/25/83) */
#define MAXFILES 255 /* max number of expanded files */
#define FNSIZE 15 /* filename length is 2(d:) + 8 + 1 + 3 + null */
int COMnf, *COMfn, COMc, *COMv;
char *COMarg, *COMs;
static expand();
command(argcp, argvp)
int *argcp, *argvp;
{
int f_alloc[MAXFILES];
COMfn = f_alloc;
COMc = *argcp;
COMv = *argvp;
COMfn[0] = *COMv++; /* don't expand first one */
COMnf = 1;
for (COMarg = *COMv; --COMc; COMarg = *++COMv)
{
for (COMs = COMarg; *COMs; COMs++)
if (*COMs == '?' || *COMs == '*')
{
expand();
goto contn; /* expand each name once */
}
COMfn[COMnf++] = COMarg; /* no expansion */
contn: ;
}
*argcp = COMnf;
COMfn[COMnf++] = -1;
COMv = *argvp = sbrk(2 * COMnf);
while (COMnf--)
COMv[COMnf] = COMfn[COMnf];
}
static expand()
{
char fcb[36];
static char *p, *q;
static int i, flg;
makfcb(COMarg, fcb);
if (fcb[0] == -1)
fcb[0] = '?'; /* check for all users */
for (i = flg = 1; i <= 11; ++i) /* expand *'s to ?'s */
{
if (i == 9)
flg = 1;
if (fcb[i] == '*')
flg = 0;
if (flg == 0)
fcb[i] = '?';
}
flg = 17;
bdos(26, 0x80); /* make sure dma address ok */
while ((i = bdos(flg, fcb)) != -1)
{
COMfn[COMnf++] = q = sbrk(FNSIZE);
if (COMnf >= MAXFILES-1)
{
for (p = "Too many file names.\n";
putchar(*p++); )
;
exit(0);
}
p = 0x81 + i * 32; /* where to find directory field */
if (COMarg[1] == ':' && COMarg[0] != '?')
{
*q++ = COMarg[0];
*q++ = ':';
}
for (i = 12; --i; )
{
if (i == 3)
*q++ = '.';
if ((*q = *p++ & 0177) != ' ')
++q;
}
*q = 0;
flg = 18;
}
}
#include "stdlib.c" /* required here for c/80 environment */
7) != ' ')
++q;
}
*q = 0;
flg = 18;
}
}
#include "stdlib.c" /* requir