home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1986 / 05 / letters.may < prev    next >
Text File  |  1986-05-31  |  4KB  |  171 lines

  1. /**
  2. **
  3. **      LJ.C  -- A printing utility for the HP LaserJet
  4. **
  5. **  This program prints a series of files on the LaserJet 
  6. **  printer.  The files are printed in a ``landscape'' font at
  7. **  17 characters to the inch.  To take advantage of this
  8. **  density, two ``pages'' of information from the file are 
  9. **  printed on each piece of paper (left and right halves).
  10. **
  11. **  Usage is:  LJ file1 file2 file3 ...
  12. **
  13. **  Where file# is a valid MS-DOS filename, included on the 
  14. **  command line.  This program is compatible with Lattice C
  15. **  on the IBM PC and the HP Touchscreen computers.
  16. **
  17. **  Joe Barnhart  original version  May 5, 1985
  18. **  Ray Duncan  date and time stamping  May 22, 1985
  19. **  Joe Barnhart  revised date stamping  June 6, 1985
  20. **  Ray Moon  modified for CI86  December 13, 1985
  21. **      & revised EOF test  
  22. **
  23. **/
  24.  
  25. #define CI86  1  /* Remove this #define => Lattice C version */
  26.  
  27. #ifdef CI86
  28. #include <stdio.h>
  29. #else
  30. #include <h\stdio.h>
  31. #endif
  32. #define MAXLINE 56      /* maximum lines per page */
  33. #define Page  `/f'      /* for compilers without `/f' */
  34. #define TAB   8      /* width of one tab stop */
  35.  
  36. #ifdef CI86
  37. typedef struct {
  38.     unsigned short ax,bx,cx,dx,si,di,ds,es;
  39.   } REGSET;
  40. #else
  41. typedef struct {
  42.     int ax, bx, cx, dx, si, di;
  43.   } REGSET;
  44. #endif
  45.  
  46. main(argc, argv)
  47.   int argc;
  48.   char *argv[];
  49. {
  50.   int filenum;
  51.   FILE *fp, *prn, *fopen();
  52.   
  53.   if( (prn = fopen(``PRN:'', ``w'') ) == NULL )
  54.     printf(``Error opening printer as file.\n'');
  55.   else {
  56.     /* initialize the LaserJet for landscape printing */
  57.     fprintf(prn, ``\033E\033&l1o\033(s17H\033&l8d6E'' );
  58.     for(filenum=1; filenum < argc; filenum++) {
  59.       fp = fopen(argv[filenum], ``r'');
  60.       if (fp == NULL)
  61.         printf(``file %s doesn't exist.\n'', argv [filenum])
  62.       else {
  63.         printf(``Now printing %s\n'', argv[filenum]);
  64.         printfile(fp, prn, argv[filenum]);
  65.         fclose(fp);
  66.       }
  67.     }
  68.     fprintf(prn, ``\015\033E'');  /* clear LaserJet */
  69.   }
  70. }
  71.  
  72. printfile(fp,prn,filename)
  73.   FILE *fp,*prn;
  74.   char *filename;
  75. {
  76.   int pagenum = 1;
  77.   
  78.   while( !feof(fp)) {
  79.     fprintf(prn, ``\033&a0r85m5L\015'');  /* set left half */
  80.     printpage(fp,prn);      /* print page */
  81.     if( !feof(fp)) {      /*if more .. */
  82.       fprintf(prn, ``\033&a0r171m91L''); /*set right half */
  83.       printpage(fp,prn);    /* print another */
  84.  
  85.     }
  86.     stamp(prn, filename, pagenum++);  /* title */
  87.     fputc(PAGE, prn);      /* kick paper */
  88.   }
  89. }
  90.  
  91. printpage(fp,prn)
  92.   FILE *fp, *prn;
  93. {
  94.   int c,line,col;
  95.  
  96.   line = col = 0;
  97.   while(line < MAXLINE)
  98.     switch(c = fgetc(fp)) {
  99.       case `\n':      /* newline found */
  100.         col = 0;    /* zero column */
  101.         line++;      /* adv line cnt */
  102.         fputc(`\n',prn);
  103.         break;
  104.       case `\t`:      /* TAB found */
  105.         do
  106.           fputc(`\040',prn);
  107.         while ((++col % TAB) != 0);
  108.         break;
  109.       case PAGE:      /* page break or */
  110.       case EOF:      /* EOF found */
  111.         line = MAXLINE;    /* force terminate */
  112.         break;
  113.       default:      /* no special case */
  114.         fputc(c,prn);    /* print character */
  115.         col++;
  116.         break;
  117.     }
  118. }
  119.  
  120. stamp(prn, filename,pagenum)
  121.   FILE *prn;
  122.   char *filename;
  123.   int pagenum;
  124. {
  125.   char datestr[10], timestr[10];
  126.  
  127.   fprintf(prn, ``\033&a5l171M'');      /* widen margins */
  128.   fprintf(prn, ``\015\033&a58R'');    /* move to row 58 */
  129.   fprintf(prn, ``File:  %-113s'', filename);
  130.   fprintf(prn, ``Page %-3d'', pagenum);
  131.   timestamp(timestr);
  132.   datestamp(datestr);
  133.   fprintf(prn, ``   %s    %s'', datestr, timestr);
  134. }
  135.  
  136. datestamp(datestr)
  137.   char *datestr;
  138. {
  139.   REGSET regs;
  140.   int month, day, year;
  141.  
  142.   regs.ax = 0x2a00;
  143. #ifdef CI86
  144.   sysint21(®s,®s);
  145. #else
  146.   int86(0x21,®s,®s);
  147. #endif
  148.   month = (regs.dx >> 8) & 255;
  149.   day = regs.dx & 255;
  150.   year = regs.cx - 1900;
  151.   sprintf(datestr, ``%02d/%02d/%02d'', month, day, year);
  152. }
  153.  
  154. timestamp(timestr)
  155.   char *timestr;
  156. {
  157.   REGSET regs;
  158.   int hours,mins;
  159.  
  160.   regs.ax = 0x2c00;
  161. #ifdef CI86
  162.   sysint21(®s,®s);
  163. #else
  164.   int86(0x21,®s,®s);
  165. #endif
  166.   hours = (regs.cx >> 8) & 255;
  167.   mins = regs.cx & 255;
  168.   sprintf(timestr, ``%02d:%02d'', hours, mins);
  169. }
  170.                      [EOF]
  171.