home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / laser / lj20.arc / LJ.C next >
Text File  |  1986-09-11  |  5KB  |  213 lines

  1. /**
  2.  **
  3.  **    LJ -- A printing utility for the HP LaserJet
  4.  **
  5.  **    Version 2.0
  6.  **    Copyright (C) 1986 Chip Rabinowitz
  7.  **    Released to the public domain for use without profit.
  8.  **
  9.  **    ********************************************************
  10.  **    *!!!! Commercial users must register this product. !!!!*
  11.  **    ********************************************************
  12.  **
  13.  **    This program prints a series of files on the LaserJet printer.  The
  14.  **    files are printed in a "landscape" font at 17 characters to the inch.
  15.  **    To take advantage of this density, two "pages" of information from
  16.  **    the file are printed on each piece of paper (left and right halves).
  17.  **
  18.  **    Usage is:       LJ  file1 file2 file3 ...
  19.  **
  20.  **    Where file# is a valid MS-DOS filename, included on the command line.
  21.  **
  22.  **    Joe Barnhart    original version for Lattice C        May 5, 1985
  23.  **    Ray Duncan    date and time stamping            May 22, 1985
  24.  **    Joe Barnhart    revised date stamping            June 6, 1985
  25.  **    Ray Duncan    modified for Microsoft C        Oct. 18, 1985
  26.  **    Chip Rabinowitz modified for Aztec C86
  27.  **            Vertical Line separation added
  28.  **            Lines > 80 continued on next line
  29.  **            Now allows 66 text lines per page    Sept. 11, 1986
  30.  **
  31.  **    Planned enhancements:
  32.  **        Wildcard file entry
  33.  **        Command Line Switches
  34.  **
  35.  **    Comments or suggestions??
  36.  **        Chip Rabinowitz
  37.  **        51 East Rogues Path
  38.  **        Huntington Station, NY 11746
  39.  **        1-516-427-5725
  40.  **
  41.  **        Registration entitles user to source code and LJ.COM file 
  42.  **        on diskette, along with printed copy of source code.  A
  43.  **        $15 registration fee is appreciated, and entitles the user
  44.  **        to all future updates.
  45.  **
  46.  **/
  47.  
  48. #include "stdio.h"
  49. #include "time.h"
  50.  
  51. #define MAXLINE 66        /* maximum lines per page on LaserJet */
  52. #define PAGE    '\014'        /* form feed */
  53. #define TAB    8        /* width of one tab stop */
  54.  
  55. FILE *lp;
  56.  
  57. main(argc, argv)
  58. int argc;
  59. char *argv[];
  60. {
  61.     int filenum;
  62.     FILE *fp, *fopen();
  63.  
  64.     lp = fopen("prn","w");            /* open the printer file */
  65.  
  66.     fprintf( lp, "\033E\033&l1O\033(s17H\033&l5.14C\033&l70F\033&l7E" );    /* this is a test */
  67.     /* initialize the LaserJet for landscape printing */
  68.  
  69.     for(filenum = 1; filenum < argc; filenum++ ){
  70.         fp = fopen( argv[filenum] ,"r+" );
  71.         if( fp == NULL )
  72.             printf( "File %s doesn't exist.\n", argv[filenum] );
  73.         else {
  74.         printf( "Now printing %s\n", argv[filenum] );
  75.         printfile( fp, argv[filenum] );
  76.         fclose( fp );
  77.         }
  78.     }
  79.     fprintf( lp, "\015\033E" );            /* clear LaserJet */
  80.     fclose(lp);
  81.  
  82. }
  83.  
  84. printfile(fp,filename)
  85. FILE *fp;
  86. char *filename;
  87. {
  88.     int pagenum = 1;
  89.     int retval = 0;
  90.  
  91.     while( (feof(fp)==0) && (retval==0) ) {
  92.         fprintf(lp, "\033&a0r85m5L\015");     /* set LaserJet to left half */
  93.         stamp(filename, pagenum++);        /* title top of page */
  94.         retval = printpage( fp );        /* print one page */
  95.         if(feof(fp)==0 && retval==0){            /* if more to print... */
  96.             dovert();
  97.             fprintf(lp, "\033&a0r171m91L\015");    /* LaserJet to right half */
  98.             stamp(filename, pagenum++);        /* title top of page */
  99.             retval = printpage( fp );        /* and print another page */
  100.         }
  101.         fputc(PAGE, lp);             /* kick out paper */
  102.     }
  103. }
  104.  
  105. dovert()
  106. {
  107.     int line = 1;
  108.     fprintf(lp,"\033&a0r90m88L\015|");
  109.     while(line++ < MAXLINE+2)
  110.         fprintf(lp,"\n|");
  111. }
  112.  
  113. printpage(fp)
  114. FILE *fp;
  115. {
  116.     char c;
  117.     int line,col;
  118.     static int cont = 0;
  119.     static char *cont1 = "---->";
  120.  
  121.     line = col = 0;
  122.     if(cont){
  123.         fprintf(lp,cont1);
  124.         col = 5;
  125.         cont = 0;
  126.     }
  127.  
  128.     while( line < MAXLINE ){
  129.         c = fgetc(fp);
  130.         if(col>=80){
  131.             line++;
  132.             switch(c){
  133.                 case '\012':
  134.                 case '\015':
  135.                 case PAGE:
  136.                 case EOF:
  137.                 case '\377':
  138.                 case '\032':
  139.                     break;
  140.                 default:
  141.                     if(line >= MAXLINE){
  142.                         cont = 1;
  143.                         ungetc(c,fp);
  144.                         return(0);
  145.                     }
  146.                     fprintf(lp,"\n%s",cont1);
  147.                     col = 5;
  148.                     break;
  149.             }
  150.         }
  151.         switch(c){
  152.             case '\012':            /* newline found */
  153.                 col = 0;        /* zero column and */
  154.                 line++;            /* advance line count */
  155.                 if( line < MAXLINE )
  156.                     fprintf(lp,"\n");
  157.                 break;
  158.             case '\015':            /* CR found */
  159.                 break;            /* discard it */
  160.             case '\011':        /* TAB found */
  161.                 do
  162.                     fputc('\040',lp);
  163.                 while ( (++col % TAB) != 0 );
  164.                 break;
  165.             case PAGE:            /* Page break or */
  166.                 line = MAXLINE;        /* force termination of loop */
  167.                 break;
  168.             default:            /* no special case */
  169.                 fputc(c,lp);        /* print character */
  170.                 col++;
  171.                 break;
  172.             case EOF:            /* EOF mark */
  173.             case '\377':            /* EOF mark */
  174.             case '\032':            /* EOF mark */
  175.                 return(-1);
  176.         }
  177.     }
  178.     return(0);
  179. }
  180.  
  181. stamp( filename, pagenum )
  182. char *filename;
  183. int pagenum;
  184. {
  185.     char datestr[10], timestr[10];
  186.  
  187.     timestamp(timestr);
  188.     datestamp(datestr);
  189.     fprintf(lp, "File: %-43s%s   %s  --  Page: %03d\n\n", filename,datestr,timestr,pagenum);
  190. }
  191.  
  192. timestamp( timestr )
  193. char    *timestr;
  194. {
  195.     struct tm buf;
  196.  
  197.     dostime(&buf);
  198.  
  199.     sprintf(timestr,"%02d:%02d",buf.tm_hour,buf.tm_min);
  200.     return;
  201. }
  202.  
  203. datestamp( datestr )
  204. char  *datestr;
  205. {
  206.     struct tm buf;
  207.  
  208.     dostime(&buf);
  209.     sprintf(datestr,"%02d/%02d/%02d",buf.tm_mon+1,buf.tm_mday,buf.tm_year);
  210.  
  211.     return;
  212. }
  213.