home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume23 / mlpd / mlp.c < prev    next >
C/C++ Source or Header  |  1991-01-08  |  4KB  |  155 lines

  1. /*
  2.  * Copyright (c) 1990 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Riverside. 
  11.  *
  12.  * NOTE : That's Riverside.  Not Berkeley, not Santa Cruz, not even
  13.  *        Irvine.  Riverside.  Ri - ver - side.
  14.  *
  15.  * The name of the University may not be used to endorse or promote 
  16.  * products derived from this software without specific prior written
  17.  * permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  21.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  22.  * 
  23.  * MLPD -- Multiple Line Printer Daemon (Version 1.3)
  24.  * SCCS keywords: @(#)mlp.c    1.3 12/1/90
  25.  */
  26.  
  27. #include "config.h"
  28. char *LP_SPOOL_DIRECTORY;
  29. int  TIMEOUT;
  30. int  errno;
  31.  
  32. /*
  33. // main() -- The main program.
  34. //
  35. // This will start off looking to create the printer structure,
  36. // initialize the process so that it is running as a daemon,
  37. // find the total number of printers to check, and start up a
  38. // select/test loop that looks for print jobs in the main queue
  39. // and moves/prints the new files in new queues.
  40. //
  41. // Arguments : None
  42. */
  43. int main(argc, argv)
  44. int argc;
  45. char *argv[];
  46. {
  47.     int count, clt, nready, pready, no_of_printers;
  48.     struct timeval timeout;
  49.     printerstruct *printers;
  50. #ifndef lint
  51.     FILE *myfp, *fopen();
  52. #endif
  53.     int fprintf();
  54.     int write();
  55.     void exit();
  56.  
  57.     /* 
  58.     // First, initialize the printer structure.
  59.     */
  60. #ifdef lint
  61.     printers = NULL;
  62. #else
  63.     printers = (printerstruct *) malloc(sizeof(printerstruct));
  64. #endif
  65.     /*
  66.     // Next, parse through all of the arguments to find information
  67.     // that we need, and set all variables necessary.
  68.     */
  69.     no_of_printers = parse_arguments(argc, argv, printers);
  70.     /* 
  71.     // Set the timeout values for the select process to be 2 seconds,
  72.     // which keeps the CPU usage down.
  73.     */
  74.     timeout.tv_sec = 2;
  75.     timeout.tv_usec = 0;
  76.     count = 0;
  77.     while(TRUE)
  78.     {
  79.         /*
  80.         // Do a select() for about 2 seconds on nothing, so that
  81.         // all we do is a time wait without causing a lot of CPU
  82.         // usage.  I'm hoping that this isn't too much bother for
  83.         // the users.  
  84.         */
  85.         if (count == 0)
  86.         {
  87.             (void)restart_all_printers(no_of_printers, printers);
  88.             /* 
  89.             // (15 * 2 seconds) + 2 seconds;
  90.             */
  91.             count = 16;            
  92.         }
  93.         --count;
  94.         if ((nready = select((int) 0, (fd_set *) NULL, (fd_set *) NULL, 
  95.             (fd_set *) NULL, (struct timeval *) &timeout)) < 0)
  96.         {
  97.             if (errno != EINTR)
  98.             {
  99.                 bomb("select");
  100.             }
  101.             else
  102.             {
  103.                 nready = 0;
  104.             }
  105.         }
  106.         /*
  107.         // Well, from here, we want to go through the printers
  108.         // to make sure we either don't have anything to print,
  109.         // or all of the printers are busy.  We check the base
  110.         // printer (Which ever one that is), and see if it has
  111.         // any files to print.  If it does, then we want to print
  112.         // out the file to one of the other printers, if any
  113.         // of them are available.
  114.         */
  115.         pready = 0;
  116.         while ((nready == 0) && 
  117.             (check_base_printer() > 0) &&
  118.             (pready == 0))
  119.         {
  120.             debug("Found a file to print.\n");
  121.             /*
  122.             // Begin to check through the list of printers
  123.             // and see if we have one free.  If we do, print
  124.             // to that printer, otherwise, skip it.  We do 
  125.             // NOT want to queue files on printers...We want
  126.             // to maintain one queue.  (See README).
  127.             */
  128.             for (clt = 0; clt < no_of_printers; ++clt)
  129.             {
  130.                 /*
  131.                 // This checks to see if the printer is
  132.                 // available to print on.
  133.                 */
  134.                 if ((pready = check_prs(printers, clt)) < 0)
  135.                 {
  136.                     bomb("check_prs");
  137.                 }
  138.                 else if (pready == 0)
  139.                 {
  140.                     debug("I can print out a file.\n");
  141.                     /*
  142.                     // If we get here, we can print
  143.                     // out a file on this printer.
  144.                     */
  145.                     if (print_file(printers, clt) < 0)
  146.                     {
  147.                         bomb("print_file");
  148.                     }
  149.                     clt = no_of_printers;
  150.                 }
  151.             }
  152.         }
  153.     }
  154. }
  155.