home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 19 Printer / 19-Printer.zip / LPR.ZIP / LPR.C < prev    next >
Text File  |  1991-03-10  |  4KB  |  151 lines

  1. /*
  2.  * lpr.c
  3.  *
  4.  * This program will print one or more files to the PRT: device
  5.  * separated by Form Feeds.
  6.  *
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <time.h>
  12.  
  13. int   i, j, c, num_lines, page_no, page_len;
  14. char  title1[80], title2[80], *xtime, *p;
  15. FILE  *fdi, *fdo;
  16. char  FF = '\f';
  17. long  tloc;
  18.  
  19. char *documentation[] = {
  20. "lpr prints files to standard out.  Execute by",
  21. "    lpr [flags] filen ...",
  22. "",
  23. "Flags are single characters preceeded by '-'",
  24. "    -lnn      number of lines per page, default is 60",
  25. "    -pn       n is the printer number, default is 1",
  26. "    -tstring  string is the title to use for the listing instead of",
  27. "              'stdin' for input from stdin.",
  28. "",
  29. "where filen is a list of files to print.",
  30. "If no files are specified, input is from stdin.",
  31. "",
  32. 0 };
  33.  
  34. main(int argc, char *argv[])
  35. {
  36.     int nfile;
  37.     char *prt, title[81];
  38.  
  39.     prt = "lpt1";
  40.     strcpy( title, "stdin" );
  41.     fdo = NULL;
  42.     page_len = 60;
  43.     j = 0;
  44.     if ( argc > 1 && *argv[1] == '?' ) {
  45.         help( documentation );
  46.         exit(4);
  47.     }
  48.     tloc = time(&tloc);
  49.     xtime = ctime(&tloc);
  50.  
  51.     nfile = argc;
  52.     for (i = 1;i < argc; i++) {
  53.         p = argv[i];
  54.         if ( *p == '-' ) {
  55.             nfile--;
  56.             ++p;
  57.             while ( c = *p++ ) {
  58.                 switch ( tolower(c) ) {
  59.                 case '?':
  60.                     help( documentation );
  61.                     break;
  62.                 case 'l':
  63.                     page_len = atoi( p );
  64.                     *p = '\0';
  65.                     break;
  66.                 case 'p':
  67.                     strcpy( prt, "lpt" );
  68.                     strcat( prt, p );
  69.                     *p = '\0';
  70.                     break;
  71.                 case 't':
  72.                     strcpy( title, p );
  73.                     *p = '\0';
  74.                     break;
  75.                 default:
  76.                     printf( "Unknown flag: %c\n", c );
  77.                 }
  78.             }
  79.         }
  80.         else {
  81.             if (( fdi = fopen( p, "r" ) ) == NULL) {
  82.                 printf("Can't OPEN input file: %s.\n",argv[i]);
  83.             }
  84.             else {
  85.                 num_lines = page_no = 0;
  86.                 if ( fdo == NULL ) {
  87.                     if ((fdo = fopen( prt, "w" )) == NULL) {
  88.                         printf("Can't OPEN printer.\n");
  89.                         exit(8);
  90.                     }
  91.                 }
  92.                 sprintf(title2,"File: %s  \n\n",argv[i]);
  93.                 prt_file();
  94.             }
  95.         }
  96.     }
  97.     if ( nfile == 1 ) {
  98.         if ( fdo == NULL ) {
  99.             if ((fdo = fopen( prt, "w" )) == NULL) {
  100.                 printf("Can't OPEN printer.\n");
  101.                 exit(8);
  102.             }
  103.         }
  104.         fdi = stdin;
  105.         sprintf( title2, "File: %s \n\n", title );
  106.         prt_file();
  107.     }
  108.     if ( fdo != NULL ) {
  109.         putc(FF,fdo);
  110.         fclose(fdo);
  111.     }
  112.     exit(0);
  113. }
  114.  
  115. prt_file()
  116. {
  117.  
  118.     num_lines = page_no = 0;
  119.     header();
  120.     while (( c = getc(fdi)) != EOF ) {
  121.         putc( c, fdo );
  122.         if ( c == '\n' ) num_lines++;
  123.         if ( num_lines > page_len ) {
  124.             num_lines = 0;
  125.             header();
  126.         }
  127.     }
  128.     if ( fdi != stdin ) fclose(fdi);
  129. }
  130.  
  131. header()
  132. {
  133.     sprintf(title1,"\nDate: %.24s                \
  134.                  Page:%3d\n", xtime, ++page_no);
  135.     if (j) putc ( '\f', fdo );
  136.     j++;
  137.     fprintf( fdo, "%s", title1 );
  138.     fprintf( fdo, "%s", title2 );
  139.     num_lines = 3;
  140. }
  141.  
  142. int help( char **hp )
  143. {
  144.     register char **dp;
  145.  
  146.     for ( dp = hp; *dp; dp++ ) printf ( "%s\n", *dp );
  147. }
  148.  
  149.  
  150.  
  151.