home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
CMDS
/
cowen_tools.lzh
/
l.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-03-06
|
5KB
|
216 lines
/*
** l
** lists one or more files with wordwrap
**
**
** copyright 1991 by Cowen Software Ltd
** 23 Bristol Ave, Levenshulme, Manchester,
** GB-M19 3NU, England, UK
** released to the public domain for non-commercial use
*/
#include <stdio.h>
#include <modes.h>
#include <ctype.h>
#define TRUE 1
#define FALSE 0
#define trace FALSE
#define maxwidth 256
#define defwidth 79
#define deftab 4
#define zlen 80
extern int errno;
char fflag = FALSE; /* file specified */
char lflag = FALSE; /* line feed */
char pflag = FALSE; /* form feed */
char zflag = FALSE; /* read filelist */
int tval = 0;
int zpath;
int width = 0;
int i, j;
char fline[maxwidth+2];
char zbuff[zlen];
main(argc,argv)
int argc;
char **argv;
{
char *p;
FILE *fp;
int zread;
getflags(argc,argv);
if (width == 0) width = defwidth;
if (width > maxwidth) error ("width too great",0);
if ( fflag )
{
while (--argc)
if (*(p = *++argv) != '-')
{
if ( (fp = fopen(p,"r") ) == NULL)
printf("\n File %s not accessable, error: %d\n ",p,errno);
else { printf("\n\n List of File %s\n\n",p);
dolist(fp);
}
}
}
if ( zflag )
{
while ( (zread = readln(zpath,zbuff,zlen) ) > 0)
{
if (zread < 2) continue;
if ( (fp = fopen(zbuff,"r") ) == NULL)
printf("\n File %s not accessable, error: %d\n ",p,errno);
else { printf("\n\n List of File %s\n\n",zbuff);
dolist(fp);
}
}
if (zread < 0) error("error on input z-file",0);
}
if (!fflag & !zflag)
dolist(stdin);
}
getflags(carg,varg)
int carg;
char **varg;
{
char *p, c;
while ( (--carg) )
{
if (*(p = *++varg) == '-')
{
while (c = *++p)
{
if (isdigit(c)) { width = atoi(p); break;};
if (c == 'f') {pflag = TRUE; continue;};
if (c == 'l') {lflag = TRUE; continue;};
if (c == 't') {
if (*++p == '=') tval = atoi(++p);
else tval = deftab;
break;
};
if (c == 'z') {
zflag = TRUE;
if (*++p != '=') {zpath = 0; break;};
zpath = open(++p,S_IREAD);
if (zpath == -1)
error ("Can't open file ",zbuff);
break;
};
if (c == '?') {help(); break;};
error("bad option",p);
}
}
else
fflag = TRUE;
}
}
dolist(fp)
FILE *fp;
/* This procedure is called for each file to be printed
*/
{
int f, tf;
char pline[maxwidth+2];
i = j = 0;
do { f = fgetc(fp);
if (f == EOF) break;
if (f == '\n') {printlast(); continue;}
if ((f == '\l') & lflag) {printlast(); continue;}
if ((f == '\t') & (tval > 0))
{
tf = ( tval - (i % tval) -1);
while (tf-- > 0) fline[i++] = ' ';
}
if (f < ' ') {f = ' ';}
if (isspace(f) ) { if (i == 0) continue;
if (i >= width) continue;
j = i;
}
fline[i++] = f;
if (i >= width)
{ if (j == 0) j = width;
else j = j + 1;
strncpy (pline, fline,j);
pline[j] = 0;
printf("%s\n",pline);
strcpy(&fline[0], &fline[j]);
i = i - j;
j = 0;
}
} while (f > 0);
if (i > 0) printlast();
if (pflag) printf("\f\n");
}
printlast()
{
fline[i] = 0;
printf("%s\n",fline);
i = j = 0;
}
error(s1, s2)
char *s1, *s2;
/*
** write the two strings passed and then a <cr>
*/
{
if (s1)
fprintf(stderr, "%s", s1 );
if (s2)
fprintf(stderr, "%s", s2 );
fprintf(stderr, "\n");
fflush(stderr);
exit (1);
}
help()
/*
** provide usage info for this command
*/
{
fprintf(stderr, "Usage: l [-options] [file] [file] [-options]\n" );
fprintf(stderr, " -no = width (default 79, max 256) \n" );
fprintf(stderr, " file = list of files to be listed\n" );
fprintf(stderr, " -f = form feed after each file \n" );
fprintf(stderr, " -l = end line at line feed \n" );
fprintf(stderr, " -t=n = tab-stop every 'n' characters (default = 4)\n" );
fprintf(stderr, " -z=xx= read filenames from xx (default = stdin)\n" );
fprintf(stderr, " -? = report help \n" );
fprintf(stderr, "\n");
fflush(stderr);
exit (0);
}