home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / mint / mntutl93 / lpd.c < prev    next >
C/C++ Source or Header  |  1993-08-03  |  3KB  |  164 lines

  1. /* a simple line printer daemon */
  2.  
  3. #include <osbind.h>
  4. #ifdef __GNUC__
  5. #include <minimal.h>
  6. #endif
  7. #include "mintbind.h"
  8.  
  9. #define PRINTERFILE "U:\\DEV\\PRN"
  10. #define MSGQUEUE "U:\\PIPE\\LPD.MSG"
  11. #define MSGSIZ    128
  12. char msgbuf[MSGSIZ];
  13.  
  14. #define BUFSIZ 2048
  15.  
  16. int msgfd;    /* requests to print files come here */
  17. int printerfd;
  18.  
  19. /* make a copy of a file. the name of the copy is kept in local
  20.    storage, and must be of the form XXXXXXXX.LPR. Moreover, it had
  21.    better be unique.
  22.    The string "from" is overwritten with an error message if an error
  23.    occurs, otherwise from[0] is set to 0.
  24.  */
  25.  
  26. void
  27. copy_file(from)
  28.     char *from;
  29. {
  30.     static char buf[BUFSIZ];
  31.     static char topat[] = "AAAAAAA.LPR";
  32.     char *to;
  33.     int  infd, outfd;
  34.     long r;
  35.  
  36.     if ((infd = Fopen(from, 0)) < 0) {
  37.         strcpy(from, "File couldn't be opened for input.");
  38.         return;
  39.     }
  40.     for(;;) {
  41.         to = topat;
  42.         while (*to == 'Z')
  43.             *to++ = 'A';
  44.         if (*to != '.')
  45.             *to += 1;
  46.         outfd = Fopen(topat, 1);
  47.         if (outfd < 0) break;
  48.         Fclose(outfd);
  49.     }
  50.  
  51.     outfd = Fcreate(topat, 0);
  52.     
  53.     if (outfd < 0) {
  54.         Fclose(infd);
  55.         strcpy(from, "Error creating spool file.");
  56.         return;
  57.     }
  58.     while ((r = Fread(infd, (long)BUFSIZ, buf)) > 0) {
  59.         if (r != Fwrite(outfd, r, buf)) {
  60.             strcpy(from, "Write error on spool file (disk full?)");
  61.             break;
  62.         }
  63.     }
  64.  
  65.     Fclose(infd);
  66.     Fclose(outfd);
  67.  
  68.     if (r == 0)
  69.         from[0] = 0;
  70. }
  71.  
  72. /*
  73.  * get a file name to be printed, and queue it. The queueing is done
  74.  * simply by making a copy of the file (see copyfile(), above) in the
  75.  * current directory.
  76.  */
  77.  
  78. void
  79. get_request()
  80. {
  81.     Fread(msgfd, (long)MSGSIZ, msgbuf); /* read the request */
  82.     copy_file(msgbuf);
  83. /* on error, copy_file puts an error message in msgbuf */
  84.  
  85.     Fwrite(msgfd, (long)MSGSIZ, msgbuf); /* reply to the request */
  86. }
  87.  
  88. /*
  89.  * print a file. periodically checks for incoming requests to print,
  90.  * and calls get_request if there are any.
  91.  */
  92.  
  93. void
  94. print_file(name)
  95.     char *name;
  96. {
  97.     int fd;
  98.     long r;
  99.     static char inbuf[80];
  100.  
  101.     fd = Fopen(name, 0);
  102.     if (fd < 0) {
  103.         Cconws("lpd: couldn't open a file???\r\n");
  104.         return;
  105.     }
  106.     for(;;) {
  107.         r = Fread(fd, 80L, inbuf);
  108.         if (r <= 0)
  109.             break;
  110.         Fwrite(printerfd, r, inbuf);
  111. /* check for incoming messages */
  112.         if (Finstat(msgfd))
  113.             get_request();
  114.     }
  115.     Fclose(fd);
  116.     Fdelete(name);
  117. }
  118.  
  119. int
  120. main(argc, argv)
  121.     int argc;
  122.     char **argv;
  123. {
  124.     extern char *getenv();
  125.     char *tmpdir;
  126.     long r;
  127.     struct _dta p;
  128.     char printfile[80];
  129.  
  130.     if ((tmpdir = getenv("TEMP")))
  131.         Dsetpath(tmpdir);
  132.  
  133.     msgfd = Fcreate(MSGQUEUE, 0);
  134.     if (msgfd == -36) {    /* access denied */
  135.         Cconws("lpd: a printer daemon already exists\r\n");
  136.         Pterm0();
  137.     }
  138.     else if (msgfd < 0) {
  139.         Cconws("lpd: unable to create message queue\r\n");
  140.         Pterm(1);
  141.     }
  142.  
  143.     if (argv[1])
  144.         strcpy(printfile, argv[1]);
  145.     else
  146.         strcpy(printfile, PRINTERFILE);
  147.  
  148.     printerfd = Fcreate(printfile, 0);
  149.     if (printerfd < 0) {
  150.         Cconws("lpd: unable to open printer\r\n");
  151.         Pterm(1);
  152.     }
  153.  
  154.     Fsetdta(&p);
  155.     for(;;) {
  156.         r = Fsfirst("*.LPR", 0);
  157.         while (r >= 0) {
  158.             print_file(p.dta_name);
  159.             r = Fsnext();
  160.         }
  161.         get_request();    /* get outstanding requests */
  162.     }
  163. }
  164.