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

  1. /*
  2.  * a print program; this one interfaces with the line printer
  3.  * daemon in lpd.c. All we do is send a request to lpd's port
  4.  * identifying the file we want printed. lpd sends back either
  5.  * a null string (everything's OK) or an error message.
  6.  * The only tricky part about the whole process is that we have
  7.  * to lock the port once we've opened it, so as to be sure
  8.  * that we don't interleave messages. Also: lpd expects the full
  9.  * pathname of the file to be printed.
  10.  */
  11.  
  12. #include <osbind.h>
  13. #ifdef __GNUC__
  14. #include <minimal.h>
  15. #endif
  16. #include "mintbind.h"
  17.  
  18. #define F_SETLK    6
  19. struct flock {
  20.     short l_type;
  21. #define F_RDLCK        0
  22. #define F_WRLCK        1
  23. #define F_UNLCK        3
  24.     short l_whence;
  25.     long l_start;
  26.     long l_len;
  27.     short l_pid;
  28. } mylock;
  29.  
  30. #define MSGQUEUE "U:\\PIPE\\LPD.MSG"
  31. #define MSGSIZ    128
  32. char msgbuf[MSGSIZ];
  33. int msgfd;
  34.  
  35. /*
  36.  * return the full pathname of the file "foo"
  37.  */
  38.  
  39. char *
  40. fullpath(foo)
  41.     char *foo;
  42. {
  43.     static char ans[128];
  44.     char *s;
  45.  
  46.     s = ans;
  47.     if (*foo && foo[1] == ':')    /* already a full path? */
  48.         return foo;
  49.  
  50.     *s++ = Dgetdrv()+'A';        /* put in the drive letter */
  51.     *s++ = ':';
  52.     if (*foo == '\\') {
  53.         strcpy(s, foo);
  54.         return ans;
  55.     }
  56.     *s = 0;
  57.     Dgetpath(s, 0);            /* put in the current directory */
  58.     strcat(s, "\\");        /* plus a path seperator */
  59.     strcat(s, foo);            /* and the file name */
  60.     return ans;
  61. }
  62.  
  63. main(argc, argv)
  64.     int argc;
  65.     char **argv;
  66. {
  67.     long r;
  68.  
  69.     if (argc != 2) {
  70.         Cconws("Usage: lpr file\r\n");
  71.         Pterm(2);
  72.     }
  73.  
  74.     msgfd = Fopen(MSGQUEUE, 2);    /* read/write access needed */
  75.     if (msgfd < 0) {
  76.         Cconws("lpr: couldn't open lpd's message queue\r\n");
  77.         Pterm(1);
  78.     }
  79.  
  80. /*
  81.  * acquire a lock on the message queue
  82.  */
  83.     mylock.l_type = F_WRLCK;
  84.     mylock.l_whence = 0;
  85.     mylock.l_start = 0;
  86.     mylock.l_len = 0;
  87.  
  88.     do {
  89.         r = Fcntl(msgfd, &mylock, F_SETLK);
  90.         if (r == -36 || r == -58) {
  91.             Cconws("lpr: waiting to acquire lock...\r\n");
  92.             Syield();
  93.             Syield();
  94.         } else if (r < 0) {
  95.             Cconws("lpr: fcntl failed??\r\n");
  96.             Pterm(3);
  97.         }
  98.     } while (r < 0);
  99.  
  100. /*
  101.  * queue the file to be printed
  102.  */
  103.     strcpy(msgbuf, fullpath(argv[1]));
  104.  
  105. /* write the request */
  106.     Fwrite(msgfd, (long)MSGSIZ, msgbuf);
  107.  
  108. /* read the server's reply */
  109.     Fread(msgfd, (long)MSGSIZ, msgbuf);
  110.  
  111. /*
  112.  * Normally, we should release the lock on msgfs. However, we're going
  113.  * to exit now, and the kernel will release the lock for us.
  114.  */
  115.  
  116. /* check for errors from the server */
  117.  
  118.     if (msgbuf[0]) {
  119.         Cconws("lpd said: ");
  120.         Cconws(msgbuf);
  121.         Cconws("\r\n");
  122.         Pterm(1);
  123.     }
  124.  
  125.     Pterm0();
  126. }
  127.