home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload
/
ShartewareOverload.cdr
/
progm
/
cpgms.zip
/
LIST.C
< prev
next >
Wrap
C/C++ Source or Header
|
1985-11-17
|
5KB
|
270 lines
#include "stdio.h"
char *documentation[] = {
"list prints a set of files with headings.",
" list [flags] file_list [>file]",
"",
"Flags are single characters preceeded by '-':",
" -w Wrap long lines (default is to truncate)",
" -h Print headings switch. Normally, on.",
" -v Verbose. Show file stats on stderr.",
" -n Each line is preceeded by its line number",
" -lnnn Line length (default 80).",
" -pnnn Page length (default 55)."
" -tnnn Tab stop every nnn columns (default 4).",
"The file_list is a list of files (wildcards are acceptable).",
"",
"Output is to stdout. To print add >PRN: to your command.",
0 };
#define DeSmet 1
#define MSDOS 1
#define LMAX 256
#ifndef stdin
#define stdin STDIN
#define stdout STDOUT
#define stderr STDERR
#endif
int wflag;
int nflag;
int hflag;
int vflag;
int nfile;
int pflag = 55;
int tflag = 4;
int lflag = 80;
char lbuf[LMAX];
int debug = 0; /* Set for debug code */
char *pp;
/*******************************************************/
main(argc, argv)
char *argv[];
{
register char *p;
register int c, i;
char *fexpnd();
FILE *f;
if (argc <= 1)
usage("No arguments");
if (argc == 2 && argv[1][0] == '?' && argv[1][1] == 0) {
help(documentation);
return;
}
nfile = argc-1;
for (i=1; i < argc; ++i) {
p = argv[i];
if (*p == '-') {
++p;
while (c = *p++) {
switch(tolower(c)) {
case '?':
help(documentation);
break;
case 'W':
case 'w':
++wflag;
break;
case 'D':
case 'd':
++debug;
break;
case 'N':
case 'n':
++nflag;
break;
case 'H':
case 'h':
++hflag;
break;
case 'V':
case 'v':
++vflag;
break;
case 'L':
case 'l':
lflag=atoi(p);
if (lflag > 132 | lflag < 9)
error("Line size must be 9 - 132\n","");
while (*p != '\0') /* skip value */
p++;
break;
case 'P':
case 'p':
pflag=atoi(p)-4; /* -4 to allow for heading */
if (pflag < 1)
error("Page size must be > 4","");
while (*p != '\0') /* skip value */
p++;
break;
case 'T':
case 't':
tflag=atoi(p);
while (*p != '\0') /* skip value */
p++;
break;
default:
usage("Unknown flag");
}
}
argv[i] = 0;
--nfile;
}
}
if (hflag)
pflag += 4; /* give back lines for headings if not necessary */
if (nfile == 0)
list(stdin, 0);
else {
for (i=1; i < argc; ++i) {
#if MSDOS
if (argv[i])
while (p = fexpnd(argv[i])) {
#else
if (p = argv[i]) {
#endif
if ((f=fopen(p, "r")) == NULL)
cant(p);
else {
list(f, p);
fclose(f);
}
}
}
}
}
/*******************************************************/
usage(s)
char *s;
{
puts("?LIST-E-");
puts(s);
puts("\n");
puts("Usage: list [-whnv] [-lnnn] [-pnnn] [-tnnn] [file ...]. \n");
exit(1);
}
/*******************************************************/
list(fp, fn)
FILE *fp; /* File to process */
char *fn; /* File name (for -f option) */
/*
* List the file with headings, etc.
*/
{
register int lno, count;
int pageno, llen, rlen, i;
char date[9], time[9], sbuf[LMAX];
dates(date); /* get date and time from operating system */
times(time);
pageno = 0;
count = 0;
lno = pflag + 1;
while (fgets(lbuf, LMAX, fp)) {
if (lno >= pflag) {
lno = 0;
if (!hflag) {
printf("\f %-12s ",fn);
for (i=1; i < lflag - 50; i++)
printf(" ");
printf("%s %s Page %d\n\n\n",date,time,++pageno);
}
else
printf("\f");
}
++count;
if (nflag) {
printf("%5d ", count);
llen = lflag - 8; /* -8 for the line number */
}
else
llen = lflag;
detabs(sbuf,lbuf,tflag,LMAX); /* expand tabs, if necessary */
if (wflag)
rlen = strlen(sbuf);
else
rlen = llen;
for (;rlen > 0;rlen-=llen) {
++lno;
strncpy(lbuf,sbuf,llen); /* limit output line length */
if (lbuf[llen-1] != '\0') {
lbuf[llen] = '\n'; /* in case len(sbuf) > llen */
lbuf[llen+1] = '\0';
}
printf("%s", lbuf);
if (rlen > llen) {
strncpy(sbuf,sbuf+llen,rlen); /* discard part printed */
printf(" ");
llen = lflag - 8;
}
}
}
if (vflag)
fprintf(stderr," %s: lines=%d, pages=%d\n", fn,count,pageno);
}
/*******************************************************/
detabs(bufo,bufi,tabsize,omax) /* remove tabs from input string */
char bufi[], bufo[];
int tabsize, omax;
{
char c;
int ii, io;
ii = io = 0;
while ((c = bufi[ii++]) && io < omax) /* till end of string */
if (c == '\t') {
bufo[io++] = ' ';
while ((io % tabsize) && io < omax) /* space to next tab */
bufo[io++] = ' ';
}
else if (c != '\f')
bufo[io++] = c;
bufo[io] = '\0';
}