1*13952Ssam #ifndef lint 2*13952Ssam static char sccsid[] = "@(#)common.c 4.8 (Berkeley) 07/17/83"; 3*13952Ssam #endif 4*13952Ssam 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 */ 3812117Sralph short RW; /* open LP for reading and writing */ 3912117Sralph short PW; /* page width */ 4012117Sralph short PL; /* page length */ 4112433Sralph short PX; /* page width in pixels */ 4212433Sralph short PY; /* page length in pixels */ 4312117Sralph short BR; /* baud rate if lp is a tty */ 4413170Sralph int FC; /* flags to clear if lp is a tty */ 4513170Sralph int FS; /* flags to set if lp is a tty */ 4613170Sralph int XC; /* flags to clear for local mode */ 4713170Sralph int XS; /* flags to set for local mode */ 4812433Sralph short RS; /* restricted to those with local accounts */ 4912117Sralph 5012117Sralph char line[BUFSIZ]; 5112117Sralph char pbuf[BUFSIZ/2]; /* buffer for printcap strings */ 5212117Sralph char *bp = pbuf; /* pointer into pbuf for pgetent() */ 5312117Sralph char *name; /* program name */ 5412117Sralph char *printer; /* printer name */ 5512117Sralph char host[32]; /* host machine name */ 5612117Sralph char *from = host; /* client's machine name */ 5712117Sralph 5812117Sralph /* 5912117Sralph * Create a connection to the remote printer server. 6012117Sralph * Most of this code comes from rcmd.c. 6112117Sralph */ 6212528Sralph getport(rhost) 6312528Sralph char *rhost; 6412117Sralph { 6512117Sralph struct hostent *hp; 6612117Sralph struct servent *sp; 6712117Sralph struct sockaddr_in sin; 6812117Sralph int s, timo = 1, lport = IPPORT_RESERVED - 1; 6912874Sralph int err; 7012117Sralph 7112117Sralph /* 7212117Sralph * Get the host address and port number to connect to. 7312117Sralph */ 7412528Sralph if (rhost == NULL) 7512117Sralph fatal("no remote host to connect to"); 7612528Sralph hp = gethostbyname(rhost); 7712117Sralph if (hp == NULL) 7812528Sralph fatal("unknown host %s", rhost); 7912117Sralph sp = getservbyname("printer", "tcp"); 8012117Sralph if (sp == NULL) 8112117Sralph fatal("printer/tcp: unknown service"); 8212117Sralph bzero((char *)&sin, sizeof(sin)); 8312117Sralph bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length); 8412117Sralph sin.sin_family = hp->h_addrtype; 8512117Sralph sin.sin_port = sp->s_port; 8612117Sralph 8712117Sralph /* 8812117Sralph * Try connecting to the server. 8912117Sralph */ 9012117Sralph retry: 9112117Sralph s = rresvport(&lport); 9212117Sralph if (s < 0) 9312117Sralph return(-1); 9412117Sralph if (connect(s, (caddr_t)&sin, sizeof(sin), 0) < 0) { 9512874Sralph err = errno; 9612874Sralph (void) close(s); 9712874Sralph errno = err; 9812117Sralph if (errno == EADDRINUSE) { 9912117Sralph lport--; 10012117Sralph goto retry; 10112117Sralph } 10212117Sralph if (errno == ECONNREFUSED && timo <= 16) { 10312117Sralph sleep(timo); 10412117Sralph timo *= 2; 10512117Sralph goto retry; 10612117Sralph } 10712117Sralph return(-1); 10812117Sralph } 10912117Sralph return(s); 11012117Sralph } 11112117Sralph 11212117Sralph rresvport(alport) 11312117Sralph int *alport; 11412117Sralph { 11512117Sralph struct sockaddr_in sin; 11612117Sralph int s; 11712117Sralph 11812117Sralph sin.sin_family = AF_INET; 11912117Sralph sin.sin_addr.s_addr = 0; 12012117Sralph s = socket(AF_INET, SOCK_STREAM, 0); 12112117Sralph if (s < 0) 12212117Sralph return(-1); 12312874Sralph for (; *alport > IPPORT_RESERVED/2; (*alport)--) { 12412117Sralph sin.sin_port = htons((u_short) *alport); 12512117Sralph if (bind(s, (caddr_t)&sin, sizeof(sin), 0) >= 0) 12612117Sralph return(s); 12712117Sralph if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) 12812874Sralph break; 12912117Sralph } 13012874Sralph (void) close(s); 13112874Sralph return(-1); 13212117Sralph } 13312117Sralph 13412117Sralph /* 13512117Sralph * Getline reads a line from the control file cfp, removes tabs, converts 13612117Sralph * new-line to null and leaves it in line. 13712117Sralph * Returns 0 at EOF or the number of characters read. 13812117Sralph */ 13912117Sralph getline(cfp) 14012117Sralph FILE *cfp; 14112117Sralph { 14212117Sralph register int linel = 0; 14312117Sralph register char *lp = line; 14412117Sralph register c; 14512117Sralph 14612117Sralph while ((c = getc(cfp)) != '\n') { 14712117Sralph if (c == EOF) 14812117Sralph return(0); 14912117Sralph if (c == '\t') { 15012117Sralph do { 15112117Sralph *lp++ = ' '; 15212117Sralph linel++; 15312117Sralph } while ((linel & 07) != 0); 15412117Sralph continue; 15512117Sralph } 15612117Sralph *lp++ = c; 15712117Sralph linel++; 15812117Sralph } 15912117Sralph *lp++ = '\0'; 16012117Sralph return(linel); 16112117Sralph } 16212117Sralph 16312117Sralph /* 16412117Sralph * Scan the current directory and make a list of daemon files sorted by 16512117Sralph * creation time. 16612117Sralph * Return the number of entries and a pointer to the list. 16712117Sralph */ 16812117Sralph getq(namelist) 16912117Sralph struct queue *(*namelist[]); 17012117Sralph { 17112117Sralph register struct direct *d; 17212117Sralph register struct queue *q, **queue; 17312117Sralph register int nitems; 17412117Sralph struct stat stbuf; 17512117Sralph int arraysz, compar(); 17612117Sralph DIR *dirp; 17712117Sralph 17813170Sralph if ((dirp = opendir(SD)) == NULL) 17912117Sralph return(-1); 18012117Sralph if (fstat(dirp->dd_fd, &stbuf) < 0) 18112874Sralph goto errdone; 18212117Sralph 18312117Sralph /* 18412117Sralph * Estimate the array size by taking the size of the directory file 18512117Sralph * and dividing it by a multiple of the minimum size entry. 18612117Sralph */ 18712117Sralph arraysz = (stbuf.st_size / 24); 18812117Sralph queue = (struct queue **)malloc(arraysz * sizeof(struct queue *)); 18912117Sralph if (queue == NULL) 19012874Sralph goto errdone; 19112117Sralph 19212117Sralph nitems = 0; 19312117Sralph while ((d = readdir(dirp)) != NULL) { 19412117Sralph if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 19512117Sralph continue; /* daemon control files only */ 19612117Sralph if (stat(d->d_name, &stbuf) < 0) 19712117Sralph continue; /* Doesn't exist */ 19812117Sralph q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1); 19912117Sralph if (q == NULL) 20012874Sralph goto errdone; 20112117Sralph q->q_time = stbuf.st_mtime; 20212117Sralph strcpy(q->q_name, d->d_name); 20312117Sralph /* 20412117Sralph * Check to make sure the array has space left and 20512117Sralph * realloc the maximum size. 20612117Sralph */ 20712117Sralph if (++nitems > arraysz) { 20812117Sralph queue = (struct queue **)realloc((char *)queue, 20912117Sralph (stbuf.st_size/12) * sizeof(struct queue *)); 21012117Sralph if (queue == NULL) 21112874Sralph goto errdone; 21212117Sralph } 21312117Sralph queue[nitems-1] = q; 21412117Sralph } 21512117Sralph closedir(dirp); 21612117Sralph if (nitems) 21712117Sralph qsort(queue, nitems, sizeof(struct queue *), compar); 21812117Sralph *namelist = queue; 21912117Sralph return(nitems); 22012874Sralph 22112874Sralph errdone: 22212874Sralph closedir(dirp); 22312874Sralph return(-1); 22412117Sralph } 22512117Sralph 22612117Sralph /* 22712117Sralph * Compare modification times. 22812117Sralph */ 22912117Sralph static 23012117Sralph compar(p1, p2) 23112117Sralph register struct queue **p1, **p2; 23212117Sralph { 23312117Sralph if ((*p1)->q_time < (*p2)->q_time) 23412117Sralph return(-1); 23512117Sralph if ((*p1)->q_time > (*p2)->q_time) 23612117Sralph return(1); 23712117Sralph return(0); 23812117Sralph } 23912117Sralph 24012117Sralph /*VARARGS1*/ 24112117Sralph fatal(msg, a1, a2, a3) 24212117Sralph char *msg; 24312117Sralph { 24412117Sralph if (from != host) 24512117Sralph printf("%s: ", host); 24612117Sralph printf("%s: ", name); 24712117Sralph if (printer) 24812117Sralph printf("%s: ", printer); 24912117Sralph printf(msg, a1, a2, a3); 25012117Sralph putchar('\n'); 25112117Sralph exit(1); 25212117Sralph } 253