home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume23 / mlpd / pfile.c < prev    next >
C/C++ Source or Header  |  1991-01-08  |  5KB  |  197 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: @(#)pfile.c    1.3 12/1/90
  25.  */
  26.  
  27. #include "config.h"
  28. extern char *LP_SPOOL_DIRECTORY; 
  29. extern int TIMEOUT;
  30.  
  31. /*
  32. // parse_arguments() -- Get printer information from argv.
  33. //
  34. // This function when called will return a structure containing
  35. // the printers and their respective spooling directories in
  36. // a structure.  
  37. //
  38. // Arguments : printerstruct *printers (The printer structure)
  39. */
  40. int parse_arguments(argc, argv, printers)
  41. int argc;
  42. char *argv[];
  43. printerstruct *printers;
  44. {
  45.     int i, origargc;
  46.     int no_of_printers, ncnt, can_exit;
  47.     char base[1024];
  48.     
  49.     can_exit = FALSE;
  50.     i = 0;
  51.     origargc = argc;
  52.     if (argc < 5)
  53.     {
  54.         usage(argv[0], "Not enough arguments.\n");
  55.     }
  56.     while (--argc)
  57.     {
  58.         if (!strcmp("-t", argv[i]))
  59.         {
  60.             if ((atoi(argv[i+1])) && (i+1 != argc))
  61.             {
  62.                 TIMEOUT = atoi(argv[i + 1]);
  63.             }
  64.             else
  65.             {
  66.                 usage(argv[0], "Bad timeout value.\n");
  67.             }
  68.         }
  69.         if (!strcmp("-p", argv[i]))
  70.         {
  71.             can_exit = TRUE;
  72.             no_of_printers = 0;
  73.             for (ncnt = i + 1; ncnt < origargc; ++ncnt)
  74.             {
  75.                 if (!strcmp(argv[ncnt], "-t"))
  76.                 {
  77.                     ncnt = origargc;
  78.                 }
  79.                 else if (ncnt == i + 1)
  80.                 {
  81.                     (void)strcpy(base, argv[ncnt]);
  82.                     find_pr(argv[ncnt]);
  83.                 }
  84.                 else
  85.                 {
  86.                     add_pr(printers, argv[ncnt],
  87.                         no_of_printers);
  88.                     ++no_of_printers;
  89.                 }
  90.             }
  91.             if (no_of_printers < 2)
  92.             {
  93.                 usage(argv[0], "Less than 2 printers specified.\n");
  94.             }
  95.         }
  96.         ++i;
  97.     }
  98.     if (!TIMEOUT) TIMEOUT = 2;
  99.     if (can_exit == FALSE)
  100.     {
  101.         usage(argv[0], "Bad arguments used.  Must use -p option.\n");
  102.     }
  103.     init_daemon(base);
  104.     return(no_of_printers);
  105. }
  106.  
  107. usage(arg, str)
  108. char *arg, *str;
  109. {
  110.     (void)fprintf(stderr, "%sUsage : %s [-p <main printer> <pr1> <pr2> ...] [-t <timeout>]\n", str, arg);
  111.     exit(1);
  112. }
  113.  
  114. /* 
  115. // add_pr() - add a printer to the list of printers.
  116. // 
  117. // Arguments :  printerstruct *printers -- the printer structure.
  118. //        char *str -- the name of the printer specified on command line.
  119. //              int nop -- the number of the printer item;
  120. */
  121. int add_pr(printers, str, nop)
  122. printerstruct *printers;
  123. char *str;
  124. int nop;
  125. {
  126.     char line[BUFSIZ];
  127.     char *tmpbuf;
  128.     int status;
  129.     char *bp;
  130.  
  131. #ifndef lint
  132.     printers->directory[nop] = (char *) malloc(512);
  133.     printers->name[nop] = (char *) malloc(512);
  134.     tmpbuf = (char *)malloc(BUFSIZ);
  135.     bp = (char *)malloc(BUFSIZ/2);
  136. #else
  137.     printers->directory[nop] = NULL;
  138.     printers->name[nop] = NULL;
  139.     tmpbuf = NULL;
  140.     bp = NULL;
  141. #endif
  142.     if ((status = pgetent(line, str)) < 0)
  143.     {
  144.         bomb("pgetent");
  145.     }
  146.     if (status == 0)
  147.     {
  148.         (void)fprintf(stderr, "No such printer (%s) exists on this system.\n", str);
  149.         exit(1);
  150.     }
  151.     if ((tmpbuf = (char *)pgetstr((char *)"sd", (char *)&bp)) == NULL)
  152.     {
  153.         bomb("get_prdir_from_printcap:pgetstr");
  154.     }
  155.     (void)strcpy((char *)printers->directory[nop], (char *)tmpbuf);
  156.     (void)strcpy((char *)printers->name[nop], (char *)str);
  157. }
  158.  
  159. /* 
  160. // find_pr() - find the base printer in the printcap file.
  161. // 
  162. // This function will set the LP_SPOOL_DIRECTORY to the base printer
  163. // spooling directory specified in /etc/printcap.
  164. //
  165. // Arguments : char *str -- the name of the printer specified on command line.
  166. */
  167.  
  168. find_pr(str)
  169. char *str;
  170. {
  171.     char line[BUFSIZ];
  172. #ifndef lint
  173.     char *newdir;
  174.     char *bp;
  175. #endif
  176.     int status;
  177.  
  178. #ifndef lint
  179.     newdir = (char *)malloc(BUFSIZ);
  180.     bp = (char *)malloc(BUFSIZ/2);
  181. #endif
  182.     if ((status = pgetent(line, str)) < 0)
  183.     {
  184.         bomb("pgetent");
  185.     }
  186.     if (status == 0)
  187.     {
  188.         (void)fprintf(stderr, "No such printer (%s) exists on this system.\n", str);
  189.         exit(1);
  190.     }
  191.     if ((newdir = (char *)pgetstr("sd", &bp)) == NULL)
  192.     {
  193.         bomb("get_prdir_from_printcap:pgetstr");
  194.     }
  195.     LP_SPOOL_DIRECTORY = (char *)strdup(newdir);
  196. }
  197.