# 2.11BSD#431 - /usr/src/usr.sbin/lpr/common.c

##### Functions

    fatal             X
    getline           X
    getport           X
    getq              X

##### Variables

    AF                X
    BR                X
    CF                X
    DF                X
    DU                X
    FC                X
    FF                X
    FS                X
    GF                X
    HL                X
    IF                X
    LF                X
    LO                X
    LP                X
    MC                X
    MX                X
    NF                X
    OF                X
    PF                X
    PL                X
    PW                X
    PX                X
    PY                X
    RF                X
    RM                X
    RP                X
    RS                X
    RW                X
    SB                X
    SC                X
    SD                X
    SF                X
    SH                X
    ST                X
    TF                X
    TR                X
    VF                X
    XC                X
    XS                X
    bp                X
    from              X
    host              X
    line              X
    name              X
    pbuf              X
    printer           X

       1: /*
       2:  * Copyright (c) 1983 Regents of the University of California.
       3:  * All rights reserved.  The Berkeley software License Agreement
       4:  * specifies the terms and conditions for redistribution.
       5:  */
       6:
       7: #ifndef lint
       8: static char sccsid[] = "@(#)common.c	5.2 (Berkeley) 5/6/86";
       9: #endif not lint
      10:
      11: /*
      12:  * Routines and data common to all the line printer functions.
      13:  */
      14:
      15: #include "lp.h"
      16:
      17: int DU;     /* daeomon user-id */
      18: int MX;     /* maximum number of blocks to copy */
      19: int MC;     /* maximum number of copies allowed */
      20: char    *LP;        /* line printer device name */
      21: char    *RM;        /* remote machine name */
      22: char    *RP;        /* remote printer name */
      23: char    *LO;        /* lock file name */
      24: char    *ST;        /* status file name */
      25: char    *SD;        /* spool directory */
      26: char    *AF;        /* accounting file */
      27: char    *LF;        /* log file for error messages */
      28: char    *OF;        /* name of output filter (created once) */
      29: char    *IF;        /* name of input filter (created per job) */
      30: char    *RF;        /* name of fortran text filter (per job) */
      31: char    *TF;        /* name of troff filter (per job) */
      32: char    *NF;        /* name of ditroff filter (per job) */
      33: char    *DF;        /* name of tex filter (per job) */
      34: char    *GF;        /* name of graph(1G) filter (per job) */
      35: char    *VF;        /* name of vplot filter (per job) */
      36: char    *CF;        /* name of cifplot filter (per job) */
      37: char    *PF;        /* name of vrast filter (per job) */
      38: char    *FF;        /* form feed string */
      39: char    *TR;        /* trailer string to be output when Q empties */
      40: short   SC;     /* suppress multiple copies */
      41: short   SF;     /* suppress FF on each print job */
      42: short   SH;     /* suppress header page */
      43: short   SB;     /* short banner instead of normal header */
      44: short   HL;     /* print header last */
      45: short   RW;     /* open LP for reading and writing */
      46: short   PW;     /* page width */
      47: short   PL;     /* page length */
      48: short   PX;     /* page width in pixels */
      49: short   PY;     /* page length in pixels */
      50: short   BR;     /* baud rate if lp is a tty */
      51: int FC;     /* flags to clear if lp is a tty */
      52: int FS;     /* flags to set if lp is a tty */
      53: int XC;     /* flags to clear for local mode */
      54: int XS;     /* flags to set for local mode */
      55: short   RS;     /* restricted to those with local accounts */
      56:
      57: char    line[BUFSIZ];
      58: char    pbuf[BUFSIZ/2]; /* buffer for printcap strings */
      59: char    *bp = pbuf; /* pointer into pbuf for pgetent() */
      60: char    *name;      /* program name */
      61: char    *printer;   /* printer name */
      62: char    host[32];   /* host machine name */
      63: char    *from = host;   /* client's machine name */
      64:
      65: /*
      66:  * Create a connection to the remote printer server.
      67:  * Most of this code comes from rcmd.c.
      68:  */
      69: getport(rhost)
      70:     char *rhost;
      71: {
      72:     struct hostent *hp;
      73:     struct servent *sp;
      74:     struct sockaddr_in sin;
      75:     int s, timo = 1, lport = IPPORT_RESERVED - 1;
      76:     int err;
      77:
      78:     /*
      79: 	 * Get the host address and port number to connect to.
      80: 	 */
      81:     if (rhost == NULL)
      82:         fatal("no remote host to connect to");
      83:     hp = gethostbyname(rhost);
      84:     if (hp == NULL)
      85:         fatal("unknown host %s", rhost);
      86:     sp = getservbyname("printer", "tcp");
      87:     if (sp == NULL)
      88:         fatal("printer/tcp: unknown service");
      89:     bzero((char *)&sin, sizeof(sin));
      90:     bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
      91:     sin.sin_family = hp->h_addrtype;
      92:     sin.sin_port = sp->s_port;
      93:
      94:     /*
      95: 	 * Try connecting to the server.
      96: 	 */
      97: retry:
      98:     s = rresvport(&lport);
      99:     if (s < 0)
     100:         return(-1);
     101:     if (connect(s, (caddr_t)&sin, sizeof(sin), 0) < 0) {
     102:         err = errno;
     103:         (void) close(s);
     104:         errno = err;
     105:         if (errno == EADDRINUSE) {
     106:             lport--;
     107:             goto retry;
     108:         }
     109:         if (errno == ECONNREFUSED && timo <= 16) {
     110:             sleep(timo);
     111:             timo *= 2;
     112:             goto retry;
     113:         }
     114:         return(-1);
     115:     }
     116:     return(s);
     117: }
     118:
     119: /*
     120:  * Getline reads a line from the control file cfp, removes tabs, converts
     121:  *  new-line to null and leaves it in line.
     122:  * Returns 0 at EOF or the number of characters read.
     123:  */
     124: getline(cfp)
     125:     FILE *cfp;
     126: {
     127:     register int linel = 0;
     128:     register char *lp = line;
     129:     register c;
     130:
     131:     while ((c = getc(cfp)) != '\n') {
     132:         if (c == EOF)
     133:             return(0);
     134:         if (c == '\t') {
     135:             do {
     136:                 *lp++ = ' ';
     137:                 linel++;
     138:             } while ((linel & 07) != 0);
     139:             continue;
     140:         }
     141:         *lp++ = c;
     142:         linel++;
     143:     }
     144:     *lp++ = '\0';
     145:     return(linel);
     146: }
     147:
     148: /*
     149:  * Scan the current directory and make a list of daemon files sorted by
     150:  * creation time.
     151:  * Return the number of entries and a pointer to the list.
     152:  */
     153: getq(namelist)
     154:     struct queue *(*namelist[]);
     155: {
     156:     register struct direct *d;
     157:     register struct queue *q, **queue;
     158:     register int nitems;
     159:     struct stat stbuf;
     160:     int arraysz, compar();
     161:     DIR *dirp;
     162:
     163:     if ((dirp = opendir(SD)) == NULL)
     164:         return(-1);
     165:     if (fstat(dirp->dd_fd, &stbuf) < 0)
     166:         goto errdone;
     167:
     168:     /*
     169: 	 * Estimate the array size by taking the size of the directory file
     170: 	 * and dividing it by a multiple of the minimum size entry.
     171: 	 */
     172:     arraysz = (stbuf.st_size / 24);
     173:     queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
     174:     if (queue == NULL)
     175:         goto errdone;
     176:
     177:     nitems = 0;
     178:     while ((d = readdir(dirp)) != NULL) {
     179:         if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
     180:             continue;   /* daemon control files only */
     181:         if (stat(d->d_name, &stbuf) < 0)
     182:             continue;   /* Doesn't exist */
     183:         q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
     184:         if (q == NULL)
     185:             goto errdone;
     186:         q->q_time = stbuf.st_mtime;
     187:         strcpy(q->q_name, d->d_name);
     188:         /*
     189: 		 * Check to make sure the array has space left and
     190: 		 * realloc the maximum size.
     191: 		 */
     192:         if (++nitems > arraysz) {
     193:             queue = (struct queue **)realloc((char *)queue,
     194:                 (stbuf.st_size/12) * sizeof(struct queue *));
     195:             if (queue == NULL)
     196:                 goto errdone;
     197:         }
     198:         queue[nitems-1] = q;
     199:     }
     200:     closedir(dirp);
     201:     if (nitems)
     202:         qsort(queue, nitems, sizeof(struct queue *), compar);
     203:     *namelist = queue;
     204:     return(nitems);
     205:
     206: errdone:
     207:     closedir(dirp);
     208:     return(-1);
     209: }
     210:
     211: /*
     212:  * Compare modification times.
     213:  */
     214: static
     215: compar(p1, p2)
     216:     register struct queue **p1, **p2;
     217: {
     218:     if ((*p1)->q_time < (*p2)->q_time)
     219:         return(-1);
     220:     if ((*p1)->q_time > (*p2)->q_time)
     221:         return(1);
     222:     return(0);
     223: }
     224:
     225: /*VARARGS1*/
     226: fatal(msg, a1, a2, a3)
     227:     char *msg;
     228: {
     229:     if (from != host)
     230:         printf("%s: ", host);
     231:     printf("%s: ", name);
     232:     if (printer)
     233:         printf("%s: ", printer);
     234:     printf(msg, a1, a2, a3);
     235:     putchar('\n');
     236:     exit(1);
     237: }

#### Defined functions

compar defined in line 214; used 2 times

- in line 160, 202

fatal defined in line 226; used 34 times

- in line 82-88(3)
- in /usr/src/usr.sbin/lpr/displayq.c line 64-66(2), 141, 151-153(2)
- in /usr/src/usr.sbin/lpr/lpd.c line 274-277(2), 315-319(2), 345-349(2), 356, 411-414(2), 448
- in /usr/src/usr.sbin/lpr/lpq.c line 94-98(2)
- in /usr/src/usr.sbin/lpr/lprm.c line 55-57(2), 84-88(2)
- in /usr/src/usr.sbin/lpr/rmjob.c line 40-42(2), 67, 73-75(2), 86, 99, 116, 151, 276

getline defined in line 124; used 11 times

- in /usr/src/usr.sbin/lpr/cmds.c line 101, 904
- in /usr/src/usr.sbin/lpr/displayq.c line 273
- in /usr/src/usr.sbin/lpr/printjob.c line 282, 380, 647, 664, 691
- in /usr/src/usr.sbin/lpr/rmjob.c line 119, 152, 191

getport defined in line 69; used 3 times

- in /usr/src/usr.sbin/lpr/displayq.c line 133
- in /usr/src/usr.sbin/lpr/printjob.c line 1138
- in /usr/src/usr.sbin/lpr/rmjob.c line 268

getq defined in line 153; used 4 times

- in /usr/src/usr.sbin/lpr/cmds.c line 808
- in /usr/src/usr.sbin/lpr/displayq.c line 152
- in /usr/src/usr.sbin/lpr/printjob.c line 120, 191

#### Defined variables

AF defined in line 26; used 2 times

- in /usr/src/usr.sbin/lpr/printjob.c line 554, 1082

BR defined in line 50; used 4 times

- in /usr/src/usr.sbin/lpr/printjob.c line 1099, 1231-1236(3)

CF defined in line 36; used 2 times

- in /usr/src/usr.sbin/lpr/printjob.c line 523, 1091

DF defined in line 33; used 2 times

- in /usr/src/usr.sbin/lpr/printjob.c line 517, 1088

DU defined in line 17; used 3 times

- in /usr/src/usr.sbin/lpr/printjob.c line 978, 1032-1033(2)

FC defined in line 51; used 3 times

- in /usr/src/usr.sbin/lpr/printjob.c line 1100-1101(2), 1241

FF defined in line 38; used 10 times

- in /usr/src/usr.sbin/lpr/printjob.c line 199(2), 436(2), 793(2), 821(2), 1034-1035(2)

FS defined in line 52; used 3 times

- in /usr/src/usr.sbin/lpr/printjob.c line 1102-1103(2), 1242

GF defined in line 34; used 2 times

- in /usr/src/usr.sbin/lpr/printjob.c line 529, 1089

HL defined in line 44; used 3 times

- in /usr/src/usr.sbin/lpr/printjob.c line 333, 383, 1097

IF defined in line 29; used 5 times

- in /usr/src/usr.sbin/lpr/printjob.c line 439, 451, 481, 488, 1084

LF defined in line 27; used 8 times

- in /usr/src/usr.sbin/lpr/printjob.c line 73-74(2), 1028-1029(2)
- in /usr/src/usr.sbin/lpr/recvjob.c line 44-45(2), 52-53(2)

LO defined in line 23; used 17 times

- in /usr/src/usr.sbin/lpr/printjob.c line 94-98(3), 104, 114, 128, 146, 165, 182, 1024-1025(2)
- in /usr/src/usr.sbin/lpr/recvjob.c line 48-49(2), 59
- in /usr/src/usr.sbin/lpr/rmjob.c line 45-46(2), 83

LP defined in line 20; used 9 times

- in /usr/src/usr.sbin/lpr/printjob.c line 1020-1021(2), 1078, 1119-1125(3)
- in /usr/src/usr.sbin/lpr/rmjob.c line 47-48(2), 247

MC defined in line 19; never used
MX defined in line 18; never used
NF defined in line 32; used 2 times

- in /usr/src/usr.sbin/lpr/printjob.c line 517, 1087

OF defined in line 28; used 6 times

- in /usr/src/usr.sbin/lpr/printjob.c line 1083, 1167, 1177-1182(4)

PF defined in line 37; never used
PL defined in line 47; used 3 times

- in /usr/src/usr.sbin/lpr/printjob.c line 1039-1041(3)

PW defined in line 46; used 5 times

- in /usr/src/usr.sbin/lpr/printjob.c line 241, 861, 1036-1038(3)

PX defined in line 48; used 3 times

- in /usr/src/usr.sbin/lpr/printjob.c line 1042-1044(3)

PY defined in line 49; used 3 times

- in /usr/src/usr.sbin/lpr/printjob.c line 1045-1047(3)

RF defined in line 30; used 2 times

- in /usr/src/usr.sbin/lpr/printjob.c line 496, 1085

RM defined in line 21; used 15 times

- in /usr/src/usr.sbin/lpr/printjob.c line 738-742(2), 1048, 1054, 1070-1073(2), 1135-1138(2), 1149-1151(2), 1157
- in /usr/src/usr.sbin/lpr/rmjob.c line 51, 247, 268-272(2)

RP defined in line 22; used 6 times

- in /usr/src/usr.sbin/lpr/printjob.c line 1022-1023(2), 1140
- in /usr/src/usr.sbin/lpr/rmjob.c line 49-50(2), 256

RS defined in line 55; used 2 times

- in /usr/src/usr.sbin/lpr/printjob.c line 292, 1093

RW defined in line 45; used 2 times

- in /usr/src/usr.sbin/lpr/printjob.c line 1098, 1121

SB defined in line 43; used 2 times

- in /usr/src/usr.sbin/lpr/printjob.c line 794, 1096

SC defined in line 40; never used
SD defined in line 25; used 16 times

- in line 163
- in /usr/src/usr.sbin/lpr/printjob.c line 90-91(2), 121, 192, 1030-1031(2)
- in /usr/src/usr.sbin/lpr/recvjob.c line 46-47(2), 57-58(2), 65-66(2)
- in /usr/src/usr.sbin/lpr/rmjob.c line 43-44(2), 72

SF defined in line 41; used 5 times

- in /usr/src/usr.sbin/lpr/printjob.c line 198, 435, 792, 820, 1094

SH defined in line 42; used 3 times

- in /usr/src/usr.sbin/lpr/printjob.c line 333, 383, 1095

ST defined in line 24; used 4 times

- in /usr/src/usr.sbin/lpr/printjob.c line 1026-1027(2), 1277-1279(2)

TF defined in line 31; used 2 times

- in /usr/src/usr.sbin/lpr/printjob.c line 517, 1086

TR defined in line 39; used 4 times

- in /usr/src/usr.sbin/lpr/printjob.c line 200-201(3), 1092

VF defined in line 35; used 2 times

- in /usr/src/usr.sbin/lpr/printjob.c line 535, 1090

XC defined in line 53; used 5 times

- in /usr/src/usr.sbin/lpr/printjob.c line 1104-1105(2), 1247, 1255-1256(2)

XS defined in line 54; used 5 times

- in /usr/src/usr.sbin/lpr/printjob.c line 1106-1107(2), 1247, 1261-1262(2)

bp defined in line 59; used 38 times

- in /usr/src/usr.sbin/lpr/lpq.c line 186, 193-194(2)
- in /usr/src/usr.sbin/lpr/printjob.c line 1020-1034(7), 1048, 1082-1092(11), 1221, 1232-1239(6)
- in /usr/src/usr.sbin/lpr/recvjob.c line 34, 44-48(3)
- in /usr/src/usr.sbin/lpr/rmjob.c line 43-51(5)

from defined in line 63; used 16 times

- in line 229
- in /usr/src/usr.sbin/lpr/lpd.c line 286-289(2), 418-419(2)
- in /usr/src/usr.sbin/lpr/recvjob.c line 151
- in /usr/src/usr.sbin/lpr/rmjob.c line 66, 155, 162, 183(2), 227-231(4), 270

host defined in line 62; used 22 times

- in line 63, 229-230(2)
- in /usr/src/usr.sbin/lpr/lpd.c line 63(2), 419
- in /usr/src/usr.sbin/lpr/lpq.c line 65(2)
- in /usr/src/usr.sbin/lpr/lprm.c line 52(2)
- in /usr/src/usr.sbin/lpr/printjob.c line 937
- in /usr/src/usr.sbin/lpr/rmjob.c line 66, 155-156(2), 162-163(2), 183, 227-232(3), 270-271(2)

line defined in line 57; used 59 times

- in line 128
- in /usr/src/usr.sbin/lpr/lpd.c line 114-116(3)
- in /usr/src/usr.sbin/lpr/printjob.c line 111-113(3), 143-145(3), 283-295(5), 302, 315-316(2), 322-323(2), 329-334(2), 341-354(7), 381-393(4), 649-650(2), 662-665(4), 692-693(2), 1013, 1140-1142(3)
- in /usr/src/usr.sbin/lpr/recvjob.c line 40, 124, 133
- in /usr/src/usr.sbin/lpr/rmjob.c line 39, 123, 153-158(3), 192-200(3), 207, 213(2)

name defined in line 60; used 13 times

- in line 231
- in /usr/src/usr.sbin/lpr/lpd.c line 64
- in /usr/src/usr.sbin/lpr/lpq.c line 64
- in /usr/src/usr.sbin/lpr/lprm.c line 51
- in /usr/src/usr.sbin/lpr/printjob.c line 1055-1067(8), 1078

pbuf defined in line 58; used 2 times

- in line 59
- in /usr/src/usr.sbin/lpr/recvjob.c line 34

printer defined in line 61; used 58 times

- in line 232-233(2)
- in /usr/src/usr.sbin/lpr/lpd.c line 293-302(3), 326, 370
- in /usr/src/usr.sbin/lpr/lpq.c line 78-81(2), 103-104(3)
- in /usr/src/usr.sbin/lpr/lprm.c line 65-68(2), 93-94(3)
- in /usr/src/usr.sbin/lpr/printjob.c line 98, 104, 114, 121, 128, 146, 165, 172, 182, 192, 233, 506, 543, 564, 599, 610, 738, 757, 777, 1013-1017(2), 1129-1134(2), 1161, 1182, 1224-1228(2), 1236, 1244, 1251, 1257, 1263, 1279
- in /usr/src/usr.sbin/lpr/recvjob.c line 40-43(2), 58, 66-70(2), 128
- in /usr/src/usr.sbin/lpr/rmjob.c line 39, 98

sccsid defined in line 8; never used

Last modified: 1987-02-18 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 1930 |