home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 19 Printer
/
19-Printer.zip
/
LPR.ZIP
/
LPR.C
< prev
next >
Wrap
Text File
|
1991-03-10
|
4KB
|
151 lines
/*
* lpr.c
*
* This program will print one or more files to the PRT: device
* separated by Form Feeds.
*
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
int i, j, c, num_lines, page_no, page_len;
char title1[80], title2[80], *xtime, *p;
FILE *fdi, *fdo;
char FF = '\f';
long tloc;
char *documentation[] = {
"lpr prints files to standard out. Execute by",
" lpr [flags] filen ...",
"",
"Flags are single characters preceeded by '-'",
" -lnn number of lines per page, default is 60",
" -pn n is the printer number, default is 1",
" -tstring string is the title to use for the listing instead of",
" 'stdin' for input from stdin.",
"",
"where filen is a list of files to print.",
"If no files are specified, input is from stdin.",
"",
0 };
main(int argc, char *argv[])
{
int nfile;
char *prt, title[81];
prt = "lpt1";
strcpy( title, "stdin" );
fdo = NULL;
page_len = 60;
j = 0;
if ( argc > 1 && *argv[1] == '?' ) {
help( documentation );
exit(4);
}
tloc = time(&tloc);
xtime = ctime(&tloc);
nfile = argc;
for (i = 1;i < argc; i++) {
p = argv[i];
if ( *p == '-' ) {
nfile--;
++p;
while ( c = *p++ ) {
switch ( tolower(c) ) {
case '?':
help( documentation );
break;
case 'l':
page_len = atoi( p );
*p = '\0';
break;
case 'p':
strcpy( prt, "lpt" );
strcat( prt, p );
*p = '\0';
break;
case 't':
strcpy( title, p );
*p = '\0';
break;
default:
printf( "Unknown flag: %c\n", c );
}
}
}
else {
if (( fdi = fopen( p, "r" ) ) == NULL) {
printf("Can't OPEN input file: %s.\n",argv[i]);
}
else {
num_lines = page_no = 0;
if ( fdo == NULL ) {
if ((fdo = fopen( prt, "w" )) == NULL) {
printf("Can't OPEN printer.\n");
exit(8);
}
}
sprintf(title2,"File: %s \n\n",argv[i]);
prt_file();
}
}
}
if ( nfile == 1 ) {
if ( fdo == NULL ) {
if ((fdo = fopen( prt, "w" )) == NULL) {
printf("Can't OPEN printer.\n");
exit(8);
}
}
fdi = stdin;
sprintf( title2, "File: %s \n\n", title );
prt_file();
}
if ( fdo != NULL ) {
putc(FF,fdo);
fclose(fdo);
}
exit(0);
}
prt_file()
{
num_lines = page_no = 0;
header();
while (( c = getc(fdi)) != EOF ) {
putc( c, fdo );
if ( c == '\n' ) num_lines++;
if ( num_lines > page_len ) {
num_lines = 0;
header();
}
}
if ( fdi != stdin ) fclose(fdi);
}
header()
{
sprintf(title1,"\nDate: %.24s \
Page:%3d\n", xtime, ++page_no);
if (j) putc ( '\f', fdo );
j++;
fprintf( fdo, "%s", title1 );
fprintf( fdo, "%s", title2 );
num_lines = 3;
}
int help( char **hp )
{
register char **dp;
for ( dp = hp; *dp; dp++ ) printf ( "%s\n", *dp );
}