113952Ssam #ifndef lint 2*18128Sralph static char sccsid[] = "@(#)common.c 4.9 (Berkeley) 02/27/85"; 313952Ssam #endif 413952Ssam 512117Sralph /* 612117Sralph * Routines and data common to all the line printer functions. 712117Sralph */ 812117Sralph 912117Sralph #include "lp.h" 1012117Sralph 1112117Sralph int DU; /* daeomon user-id */ 1212117Sralph int MX; /* maximum number of blocks to copy */ 1313170Sralph int MC; /* maximum number of copies allowed */ 1412117Sralph char *LP; /* line printer device name */ 1512117Sralph char *RM; /* remote machine name */ 1612117Sralph char *RP; /* remote printer name */ 1712117Sralph char *LO; /* lock file name */ 1812117Sralph char *ST; /* status file name */ 1912117Sralph char *SD; /* spool directory */ 2012117Sralph char *AF; /* accounting file */ 2112117Sralph char *LF; /* log file for error messages */ 2212117Sralph char *OF; /* name of output filter (created once) */ 2312117Sralph char *IF; /* name of input filter (created per job) */ 2412433Sralph char *RF; /* name of fortran text filter (per job) */ 2512117Sralph char *TF; /* name of troff filter (per job) */ 2613235Sralph char *NF; /* name of ditroff filter (per job) */ 2712117Sralph char *DF; /* name of tex filter (per job) */ 2812117Sralph char *GF; /* name of graph(1G) filter (per job) */ 2912117Sralph char *VF; /* name of vplot filter (per job) */ 3012117Sralph char *CF; /* name of cifplot filter (per job) */ 3112117Sralph char *PF; /* name of vrast filter (per job) */ 3212117Sralph char *FF; /* form feed string */ 3312117Sralph char *TR; /* trailer string to be output when Q empties */ 3413170Sralph short SC; /* suppress multiple copies */ 3512117Sralph short SF; /* suppress FF on each print job */ 3612117Sralph short SH; /* suppress header page */ 3712117Sralph short SB; /* short banner instead of normal header */ 38*18128Sralph short HL; /* print header last */ 3912117Sralph short RW; /* open LP for reading and writing */ 4012117Sralph short PW; /* page width */ 4112117Sralph short PL; /* page length */ 4212433Sralph short PX; /* page width in pixels */ 4312433Sralph short PY; /* page length in pixels */ 4412117Sralph short BR; /* baud rate if lp is a tty */ 4513170Sralph int FC; /* flags to clear if lp is a tty */ 4613170Sralph int FS; /* flags to set if lp is a tty */ 4713170Sralph int XC; /* flags to clear for local mode */ 4813170Sralph int XS; /* flags to set for local mode */ 4912433Sralph short RS; /* restricted to those with local accounts */ 5012117Sralph 5112117Sralph char line[BUFSIZ]; 5212117Sralph char pbuf[BUFSIZ/2]; /* buffer for printcap strings */ 5312117Sralph char *bp = pbuf; /* pointer into pbuf for pgetent() */ 5412117Sralph char *name; /* program name */ 5512117Sralph char *printer; /* printer name */ 5612117Sralph char host[32]; /* host machine name */ 5712117Sralph char *from = host; /* client's machine name */ 5812117Sralph 5912117Sralph /* 6012117Sralph * Create a connection to the remote printer server. 6112117Sralph * Most of this code comes from rcmd.c. 6212117Sralph */ 6312528Sralph getport(rhost) 6412528Sralph char *rhost; 6512117Sralph { 6612117Sralph struct hostent *hp; 6712117Sralph struct servent *sp; 6812117Sralph struct sockaddr_in sin; 6912117Sralph int s, timo = 1, lport = IPPORT_RESERVED - 1; 7012874Sralph int err; 7112117Sralph 7212117Sralph /* 7312117Sralph * Get the host address and port number to connect to. 7412117Sralph */ 7512528Sralph if (rhost == NULL) 7612117Sralph fatal("no remote host to connect to"); 7712528Sralph hp = gethostbyname(rhost); 7812117Sralph if (hp == NULL) 7912528Sralph fatal("unknown host %s", rhost); 8012117Sralph sp = getservbyname("printer", "tcp"); 8112117Sralph if (sp == NULL) 8212117Sralph fatal("printer/tcp: unknown service"); 8312117Sralph bzero((char *)&sin, sizeof(sin)); 8412117Sralph bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length); 8512117Sralph sin.sin_family = hp->h_addrtype; 8612117Sralph sin.sin_port = sp->s_port; 8712117Sralph 8812117Sralph /* 8912117Sralph * Try connecting to the server. 9012117Sralph */ 9112117Sralph retry: 9212117Sralph s = rresvport(&lport); 9312117Sralph if (s < 0) 9412117Sralph return(-1); 9512117Sralph if (connect(s, (caddr_t)&sin, sizeof(sin), 0) < 0) { 9612874Sralph err = errno; 9712874Sralph (void) close(s); 9812874Sralph errno = err; 9912117Sralph if (errno == EADDRINUSE) { 10012117Sralph lport--; 10112117Sralph goto retry; 10212117Sralph } 10312117Sralph if (errno == ECONNREFUSED && timo <= 16) { 10412117Sralph sleep(timo); 10512117Sralph timo *= 2; 10612117Sralph goto retry; 10712117Sralph } 10812117Sralph return(-1); 10912117Sralph } 11012117Sralph return(s); 11112117Sralph } 11212117Sralph 11312117Sralph rresvport(alport) 11412117Sralph int *alport; 11512117Sralph { 11612117Sralph struct sockaddr_in sin; 11712117Sralph int s; 11812117Sralph 11912117Sralph sin.sin_family = AF_INET; 12012117Sralph sin.sin_addr.s_addr = 0; 12112117Sralph s = socket(AF_INET, SOCK_STREAM, 0); 12212117Sralph if (s < 0) 12312117Sralph return(-1); 12412874Sralph for (; *alport > IPPORT_RESERVED/2; (*alport)--) { 12512117Sralph sin.sin_port = htons((u_short) *alport); 12612117Sralph if (bind(s, (caddr_t)&sin, sizeof(sin), 0) >= 0) 12712117Sralph return(s); 12812117Sralph if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) 12912874Sralph break; 13012117Sralph } 13112874Sralph (void) close(s); 13212874Sralph return(-1); 13312117Sralph } 13412117Sralph 13512117Sralph /* 13612117Sralph * Getline reads a line from the control file cfp, removes tabs, converts 13712117Sralph * new-line to null and leaves it in line. 13812117Sralph * Returns 0 at EOF or the number of characters read. 13912117Sralph */ 14012117Sralph getline(cfp) 14112117Sralph FILE *cfp; 14212117Sralph { 14312117Sralph register int linel = 0; 14412117Sralph register char *lp = line; 14512117Sralph register c; 14612117Sralph 14712117Sralph while ((c = getc(cfp)) != '\n') { 14812117Sralph if (c == EOF) 14912117Sralph return(0); 15012117Sralph if (c == '\t') { 15112117Sralph do { 15212117Sralph *lp++ = ' '; 15312117Sralph linel++; 15412117Sralph } while ((linel & 07) != 0); 15512117Sralph continue; 15612117Sralph } 15712117Sralph *lp++ = c; 15812117Sralph linel++; 15912117Sralph } 16012117Sralph *lp++ = '\0'; 16112117Sralph return(linel); 16212117Sralph } 16312117Sralph 16412117Sralph /* 16512117Sralph * Scan the current directory and make a list of daemon files sorted by 16612117Sralph * creation time. 16712117Sralph * Return the number of entries and a pointer to the list. 16812117Sralph */ 16912117Sralph getq(namelist) 17012117Sralph struct queue *(*namelist[]); 17112117Sralph { 17212117Sralph register struct direct *d; 17312117Sralph register struct queue *q, **queue; 17412117Sralph register int nitems; 17512117Sralph struct stat stbuf; 17612117Sralph int arraysz, compar(); 17712117Sralph DIR *dirp; 17812117Sralph 17913170Sralph if ((dirp = opendir(SD)) == NULL) 18012117Sralph return(-1); 18112117Sralph if (fstat(dirp->dd_fd, &stbuf) < 0) 18212874Sralph goto errdone; 18312117Sralph 18412117Sralph /* 18512117Sralph * Estimate the array size by taking the size of the directory file 18612117Sralph * and dividing it by a multiple of the minimum size entry. 18712117Sralph */ 18812117Sralph arraysz = (stbuf.st_size / 24); 18912117Sralph queue = (struct queue **)malloc(arraysz * sizeof(struct queue *)); 19012117Sralph if (queue == NULL) 19112874Sralph goto errdone; 19212117Sralph 19312117Sralph nitems = 0; 19412117Sralph while ((d = readdir(dirp)) != NULL) { 19512117Sralph if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 19612117Sralph continue; /* daemon control files only */ 19712117Sralph if (stat(d->d_name, &stbuf) < 0) 19812117Sralph continue; /* Doesn't exist */ 19912117Sralph q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1); 20012117Sralph if (q == NULL) 20112874Sralph goto errdone; 20212117Sralph q->q_time = stbuf.st_mtime; 20312117Sralph strcpy(q->q_name, d->d_name); 20412117Sralph /* 20512117Sralph * Check to make sure the array has space left and 20612117Sralph * realloc the maximum size. 20712117Sralph */ 20812117Sralph if (++nitems > arraysz) { 20912117Sralph queue = (struct queue **)realloc((char *)queue, 21012117Sralph (stbuf.st_size/12) * sizeof(struct queue *)); 21112117Sralph if (queue == NULL) 21212874Sralph goto errdone; 21312117Sralph } 21412117Sralph queue[nitems-1] = q; 21512117Sralph } 21612117Sralph closedir(dirp); 21712117Sralph if (nitems) 21812117Sralph qsort(queue, nitems, sizeof(struct queue *), compar); 21912117Sralph *namelist = queue; 22012117Sralph return(nitems); 22112874Sralph 22212874Sralph errdone: 22312874Sralph closedir(dirp); 22412874Sralph return(-1); 22512117Sralph } 22612117Sralph 22712117Sralph /* 22812117Sralph * Compare modification times. 22912117Sralph */ 23012117Sralph static 23112117Sralph compar(p1, p2) 23212117Sralph register struct queue **p1, **p2; 23312117Sralph { 23412117Sralph if ((*p1)->q_time < (*p2)->q_time) 23512117Sralph return(-1); 23612117Sralph if ((*p1)->q_time > (*p2)->q_time) 23712117Sralph return(1); 23812117Sralph return(0); 23912117Sralph } 24012117Sralph 24112117Sralph /*VARARGS1*/ 24212117Sralph fatal(msg, a1, a2, a3) 24312117Sralph char *msg; 24412117Sralph { 24512117Sralph if (from != host) 24612117Sralph printf("%s: ", host); 24712117Sralph printf("%s: ", name); 24812117Sralph if (printer) 24912117Sralph printf("%s: ", printer); 25012117Sralph printf(msg, a1, a2, a3); 25112117Sralph putchar('\n'); 25212117Sralph exit(1); 25312117Sralph } 254