home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / lpr / common_source / common.c,v next >
Encoding:
Text File  |  1994-05-23  |  8.7 KB  |  336 lines

  1. head    1.1;
  2. access;
  3. symbols;
  4. locks
  5.     rzsfl:1.1; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.1
  10. date    94.05.23.09.04.57;    author rzsfl;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @Original
  17. @
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/*
  26.  * Copyright (c) 1983 Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * Redistribution and use in source and binary forms, with or without
  30.  * modification, are permitted provided that the following conditions
  31.  * are met:
  32.  * 1. Redistributions of source code must retain the above copyright
  33.  *    notice, this list of conditions and the following disclaimer.
  34.  * 2. Redistributions in binary form must reproduce the above copyright
  35.  *    notice, this list of conditions and the following disclaimer in the
  36.  *    documentation and/or other materials provided with the distribution.
  37.  * 3. All advertising materials mentioning features or use of this software
  38.  *    must display the following acknowledgement:
  39.  *    This product includes software developed by the University of
  40.  *    California, Berkeley and its contributors.
  41.  * 4. Neither the name of the University nor the names of its contributors
  42.  *    may be used to endorse or promote products derived from this software
  43.  *    without specific prior written permission.
  44.  *
  45.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  46.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  48.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  49.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  50.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  51.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  52.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  53.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  54.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  55.  * SUCH DAMAGE.
  56.  */
  57.  
  58. #ifndef lint
  59. /*static char sccsid[] = "from: @@(#)common.c    5.7 (Berkeley) 3/2/91";*/
  60. static char rcsid[] = "$Id: common.c,v 1.3 1993/12/08 00:47:01 jtc Exp $";
  61. #endif /* not lint */
  62.  
  63. /*
  64.  * Routines and data common to all the line printer functions.
  65.  */
  66.  
  67. #include "lp.h"
  68.  
  69. int    DU;        /* daeomon user-id */
  70. int    MX;        /* maximum number of blocks to copy */
  71. int    MC;        /* maximum number of copies allowed */
  72. char    *LP;        /* line printer device name */
  73. char    *RM;        /* remote machine name */
  74. char    *RP;        /* remote printer name */
  75. char    *LO;        /* lock file name */
  76. char    *ST;        /* status file name */
  77. char    *SD;        /* spool directory */
  78. char    *AF;        /* accounting file */
  79. char    *LF;        /* log file for error messages */
  80. char    *OF;        /* name of output filter (created once) */
  81. char    *IF;        /* name of input filter (created per job) */
  82. char    *RF;        /* name of fortran text filter (per job) */
  83. char    *TF;        /* name of troff filter (per job) */
  84. char    *NF;        /* name of ditroff filter (per job) */
  85. char    *DF;        /* name of tex filter (per job) */
  86. char    *GF;        /* name of graph(1G) filter (per job) */
  87. char    *VF;        /* name of vplot filter (per job) */
  88. char    *CF;        /* name of cifplot filter (per job) */
  89. char    *PF;        /* name of vrast filter (per job) */
  90. char    *FF;        /* form feed string */
  91. char    *TR;        /* trailer string to be output when Q empties */
  92. short    SC;        /* suppress multiple copies */
  93. short    SF;        /* suppress FF on each print job */
  94. short    SH;        /* suppress header page */
  95. short    SB;        /* short banner instead of normal header */
  96. short    HL;        /* print header last */
  97. short    RW;        /* open LP for reading and writing */
  98. short    PW;        /* page width */
  99. short    PL;        /* page length */
  100. short    PX;        /* page width in pixels */
  101. short    PY;        /* page length in pixels */
  102. short    BR;        /* baud rate if lp is a tty */
  103. int    FC;        /* flags to clear if lp is a tty */
  104. int    FS;        /* flags to set if lp is a tty */
  105. int    XC;        /* flags to clear for local mode */
  106. int    XS;        /* flags to set for local mode */
  107. short    RS;        /* restricted to those with local accounts */
  108.  
  109. char    line[BUFSIZ];
  110. char    pbuf[BUFSIZ/2];    /* buffer for printcap strings */
  111. char    *bp = pbuf;    /* pointer into pbuf for pgetent() */
  112. char    *name;        /* program name */
  113. char    *printer;    /* printer name */
  114. char    host[32];    /* host machine name */
  115. char    *from = host;    /* client's machine name */
  116. int    sendtorem;    /* are we sending to a remote? */
  117.  
  118. /*
  119.  * Create a connection to the remote printer server.
  120.  * Most of this code comes from rcmd.c.
  121.  */
  122. getport(rhost)
  123.     char *rhost;
  124. {
  125.     struct hostent *hp;
  126.     struct servent *sp;
  127.     struct sockaddr_in sin;
  128.     int s, timo = 1, lport = IPPORT_RESERVED - 1;
  129.     int err;
  130.  
  131.     /*
  132.      * Get the host address and port number to connect to.
  133.      */
  134.     if (rhost == NULL)
  135.         fatal("no remote host to connect to");
  136.     hp = gethostbyname(rhost);
  137.     if (hp == NULL)
  138.         fatal("unknown host %s", rhost);
  139.     sp = getservbyname("printer", "tcp");
  140.     if (sp == NULL)
  141.         fatal("printer/tcp: unknown service");
  142.     bzero((char *)&sin, sizeof(sin));
  143.     bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
  144.     sin.sin_family = hp->h_addrtype;
  145.     sin.sin_port = sp->s_port;
  146.  
  147.     /*
  148.      * Try connecting to the server.
  149.      */
  150. retry:
  151.     s = rresvport(&lport);
  152.     if (s < 0)
  153.         return(-1);
  154.     if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  155.         err = errno;
  156.         (void) close(s);
  157.         errno = err;
  158.         if (errno == EADDRINUSE) {
  159.             lport--;
  160.             goto retry;
  161.         }
  162.         if (errno == ECONNREFUSED && timo <= 16) {
  163.             sleep(timo);
  164.             timo *= 2;
  165.             goto retry;
  166.         }
  167.         return(-1);
  168.     }
  169.     return(s);
  170. }
  171.  
  172. /*
  173.  * Getline reads a line from the control file cfp, removes tabs, converts
  174.  *  new-line to null and leaves it in line.
  175.  * Returns 0 at EOF or the number of characters read.
  176.  */
  177. getline(cfp)
  178.     FILE *cfp;
  179. {
  180.     register int linel = 0;
  181.     register char *lp = line;
  182.     register c;
  183.  
  184.     while ((c = getc(cfp)) != '\n') {
  185.         if (c == EOF)
  186.             return(0);
  187.         if (c == '\t') {
  188.             do {
  189.                 *lp++ = ' ';
  190.                 linel++;
  191.             } while ((linel & 07) != 0);
  192.             continue;
  193.         }
  194.         *lp++ = c;
  195.         linel++;
  196.     }
  197.     *lp++ = '\0';
  198.     return(linel);
  199. }
  200.  
  201. /*
  202.  * Scan the current directory and make a list of daemon files sorted by
  203.  * creation time.
  204.  * Return the number of entries and a pointer to the list.
  205.  */
  206. getq(namelist)
  207.     struct queue *(*namelist[]);
  208. {
  209.     register struct dirent *d;
  210.     register struct queue *q, **queue;
  211.     register int nitems;
  212.     struct stat stbuf;
  213.     DIR *dirp;
  214.     int arraysz;
  215.     static int compar();
  216.  
  217.     if ((dirp = opendir(SD)) == NULL)
  218.         return(-1);
  219.     if (fstat(dirp->dd_fd, &stbuf) < 0)
  220.         goto errdone;
  221.  
  222.     /*
  223.      * Estimate the array size by taking the size of the directory file
  224.      * and dividing it by a multiple of the minimum size entry. 
  225.      */
  226.     arraysz = (stbuf.st_size / 24);
  227.     queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
  228.     if (queue == NULL)
  229.         goto errdone;
  230.  
  231.     nitems = 0;
  232.     while ((d = readdir(dirp)) != NULL) {
  233.         if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
  234.             continue;    /* daemon control files only */
  235.         if (stat(d->d_name, &stbuf) < 0)
  236.             continue;    /* Doesn't exist */
  237.         q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
  238.         if (q == NULL)
  239.             goto errdone;
  240.         q->q_time = stbuf.st_mtime;
  241.         strcpy(q->q_name, d->d_name);
  242.         /*
  243.          * Check to make sure the array has space left and
  244.          * realloc the maximum size.
  245.          */
  246.         if (++nitems > arraysz) {
  247.             queue = (struct queue **)realloc((char *)queue,
  248.                 (stbuf.st_size/12) * sizeof(struct queue *));
  249.             if (queue == NULL)
  250.                 goto errdone;
  251.         }
  252.         queue[nitems-1] = q;
  253.     }
  254.     closedir(dirp);
  255.     if (nitems)
  256.         qsort(queue, nitems, sizeof(struct queue *), compar);
  257.     *namelist = queue;
  258.     return(nitems);
  259.  
  260. errdone:
  261.     closedir(dirp);
  262.     return(-1);
  263. }
  264.  
  265. /*
  266.  * Compare modification times.
  267.  */
  268. static
  269. compar(p1, p2)
  270.     register struct queue **p1, **p2;
  271. {
  272.     if ((*p1)->q_time < (*p2)->q_time)
  273.         return(-1);
  274.     if ((*p1)->q_time > (*p2)->q_time)
  275.         return(1);
  276.     return(0);
  277. }
  278.  
  279. /*
  280.  * Figure out whether the local machine is the same
  281.  * as the remote machine (RM) entry (if it exists).
  282.  */
  283. char *
  284. checkremote()
  285. {
  286.     char name[MAXHOSTNAMELEN];
  287.     register struct hostent *hp;
  288.     static char errbuf[128];
  289.  
  290.     sendtorem = 0;    /* assume printer is local */
  291.     if (RM != (char *)NULL) {
  292.         /* get the official name of the local host */
  293.         gethostname(name, sizeof(name));
  294.         name[sizeof(name)-1] = '\0';
  295.         hp = gethostbyname(name);
  296.         if (hp == (struct hostent *) NULL) {
  297.             (void) sprintf(errbuf,
  298.             "unable to get official name for local machine %s",
  299.             name);
  300.             return errbuf;
  301.         } else (void) strcpy(name, hp->h_name);
  302.  
  303.         /* get the official name of RM */
  304.         hp = gethostbyname(RM);
  305.         if (hp == (struct hostent *) NULL) {
  306.             (void) sprintf(errbuf,
  307.             "unable to get official name for remote machine %s",
  308.             RM);
  309.             return errbuf;
  310.         }
  311.  
  312.         /*
  313.          * if the two hosts are not the same,
  314.          * then the printer must be remote.
  315.          */
  316.         if (strcmp(name, hp->h_name) != 0)
  317.             sendtorem = 1;
  318.     }
  319.     return (char *)0;
  320. }
  321.  
  322. /*VARARGS1*/
  323. fatal(msg, a1, a2, a3)
  324.     char *msg;
  325. {
  326.     if (from != host)
  327.         printf("%s: ", host);
  328.     printf("%s: ", name);
  329.     if (printer)
  330.         printf("%s: ", printer);
  331.     printf(msg, a1, a2, a3);
  332.     putchar('\n');
  333.     exit(1);
  334. }
  335. @
  336.