1*13170Sralph /* common.c 4.6 83/06/17 */ 212117Sralph /* 312117Sralph * Routines and data common to all the line printer functions. 412117Sralph */ 512117Sralph 612117Sralph #include "lp.h" 712117Sralph 812117Sralph int DU; /* daeomon user-id */ 912117Sralph int MX; /* maximum number of blocks to copy */ 10*13170Sralph int MC; /* maximum number of copies allowed */ 1112117Sralph char *LP; /* line printer device name */ 1212117Sralph char *RM; /* remote machine name */ 1312117Sralph char *RP; /* remote printer name */ 1412117Sralph char *LO; /* lock file name */ 1512117Sralph char *ST; /* status file name */ 1612117Sralph char *SD; /* spool directory */ 1712117Sralph char *AF; /* accounting file */ 1812117Sralph char *LF; /* log file for error messages */ 1912117Sralph char *OF; /* name of output filter (created once) */ 2012117Sralph char *IF; /* name of input filter (created per job) */ 2112433Sralph char *RF; /* name of fortran text filter (per job) */ 2212117Sralph char *TF; /* name of troff filter (per job) */ 2312117Sralph char *DF; /* name of tex filter (per job) */ 2412117Sralph char *GF; /* name of graph(1G) filter (per job) */ 2512117Sralph char *VF; /* name of vplot filter (per job) */ 2612117Sralph char *CF; /* name of cifplot filter (per job) */ 2712117Sralph char *PF; /* name of vrast filter (per job) */ 2812117Sralph char *FF; /* form feed string */ 2912117Sralph char *TR; /* trailer string to be output when Q empties */ 30*13170Sralph short SC; /* suppress multiple copies */ 3112117Sralph short SF; /* suppress FF on each print job */ 3212117Sralph short SH; /* suppress header page */ 3312117Sralph short SB; /* short banner instead of normal header */ 3412117Sralph short RW; /* open LP for reading and writing */ 3512117Sralph short PW; /* page width */ 3612117Sralph short PL; /* page length */ 3712433Sralph short PX; /* page width in pixels */ 3812433Sralph short PY; /* page length in pixels */ 3912117Sralph short BR; /* baud rate if lp is a tty */ 40*13170Sralph int FC; /* flags to clear if lp is a tty */ 41*13170Sralph int FS; /* flags to set if lp is a tty */ 42*13170Sralph int XC; /* flags to clear for local mode */ 43*13170Sralph int XS; /* flags to set for local mode */ 4412433Sralph short RS; /* restricted to those with local accounts */ 4512117Sralph 4612117Sralph char line[BUFSIZ]; 4712117Sralph char pbuf[BUFSIZ/2]; /* buffer for printcap strings */ 4812117Sralph char *bp = pbuf; /* pointer into pbuf for pgetent() */ 4912117Sralph char *name; /* program name */ 5012117Sralph char *printer; /* printer name */ 5112117Sralph char host[32]; /* host machine name */ 5212117Sralph char *from = host; /* client's machine name */ 5312117Sralph 5412117Sralph /* 5512117Sralph * Create a connection to the remote printer server. 5612117Sralph * Most of this code comes from rcmd.c. 5712117Sralph */ 5812528Sralph getport(rhost) 5912528Sralph char *rhost; 6012117Sralph { 6112117Sralph struct hostent *hp; 6212117Sralph struct servent *sp; 6312117Sralph struct sockaddr_in sin; 6412117Sralph int s, timo = 1, lport = IPPORT_RESERVED - 1; 6512874Sralph int err; 6612117Sralph 6712117Sralph /* 6812117Sralph * Get the host address and port number to connect to. 6912117Sralph */ 7012528Sralph if (rhost == NULL) 7112117Sralph fatal("no remote host to connect to"); 7212528Sralph hp = gethostbyname(rhost); 7312117Sralph if (hp == NULL) 7412528Sralph fatal("unknown host %s", rhost); 7512117Sralph sp = getservbyname("printer", "tcp"); 7612117Sralph if (sp == NULL) 7712117Sralph fatal("printer/tcp: unknown service"); 7812117Sralph bzero((char *)&sin, sizeof(sin)); 7912117Sralph bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length); 8012117Sralph sin.sin_family = hp->h_addrtype; 8112117Sralph sin.sin_port = sp->s_port; 8212117Sralph 8312117Sralph /* 8412117Sralph * Try connecting to the server. 8512117Sralph */ 8612117Sralph retry: 8712117Sralph s = rresvport(&lport); 8812117Sralph if (s < 0) 8912117Sralph return(-1); 9012117Sralph if (connect(s, (caddr_t)&sin, sizeof(sin), 0) < 0) { 9112874Sralph err = errno; 9212874Sralph (void) close(s); 9312874Sralph errno = err; 9412117Sralph if (errno == EADDRINUSE) { 9512117Sralph lport--; 9612117Sralph goto retry; 9712117Sralph } 9812117Sralph if (errno == ECONNREFUSED && timo <= 16) { 9912117Sralph sleep(timo); 10012117Sralph timo *= 2; 10112117Sralph goto retry; 10212117Sralph } 10312117Sralph return(-1); 10412117Sralph } 10512117Sralph return(s); 10612117Sralph } 10712117Sralph 10812117Sralph rresvport(alport) 10912117Sralph int *alport; 11012117Sralph { 11112117Sralph struct sockaddr_in sin; 11212117Sralph int s; 11312117Sralph 11412117Sralph sin.sin_family = AF_INET; 11512117Sralph sin.sin_addr.s_addr = 0; 11612117Sralph s = socket(AF_INET, SOCK_STREAM, 0); 11712117Sralph if (s < 0) 11812117Sralph return(-1); 11912874Sralph for (; *alport > IPPORT_RESERVED/2; (*alport)--) { 12012117Sralph sin.sin_port = htons((u_short) *alport); 12112117Sralph if (bind(s, (caddr_t)&sin, sizeof(sin), 0) >= 0) 12212117Sralph return(s); 12312117Sralph if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) 12412874Sralph break; 12512117Sralph } 12612874Sralph (void) close(s); 12712874Sralph return(-1); 12812117Sralph } 12912117Sralph 13012117Sralph /* 13112117Sralph * Getline reads a line from the control file cfp, removes tabs, converts 13212117Sralph * new-line to null and leaves it in line. 13312117Sralph * Returns 0 at EOF or the number of characters read. 13412117Sralph */ 13512117Sralph getline(cfp) 13612117Sralph FILE *cfp; 13712117Sralph { 13812117Sralph register int linel = 0; 13912117Sralph register char *lp = line; 14012117Sralph register c; 14112117Sralph 14212117Sralph while ((c = getc(cfp)) != '\n') { 14312117Sralph if (c == EOF) 14412117Sralph return(0); 14512117Sralph if (c == '\t') { 14612117Sralph do { 14712117Sralph *lp++ = ' '; 14812117Sralph linel++; 14912117Sralph } while ((linel & 07) != 0); 15012117Sralph continue; 15112117Sralph } 15212117Sralph *lp++ = c; 15312117Sralph linel++; 15412117Sralph } 15512117Sralph *lp++ = '\0'; 15612117Sralph return(linel); 15712117Sralph } 15812117Sralph 15912117Sralph /* 16012117Sralph * Scan the current directory and make a list of daemon files sorted by 16112117Sralph * creation time. 16212117Sralph * Return the number of entries and a pointer to the list. 16312117Sralph */ 16412117Sralph getq(namelist) 16512117Sralph struct queue *(*namelist[]); 16612117Sralph { 16712117Sralph register struct direct *d; 16812117Sralph register struct queue *q, **queue; 16912117Sralph register int nitems; 17012117Sralph struct stat stbuf; 17112117Sralph int arraysz, compar(); 17212117Sralph DIR *dirp; 17312117Sralph 174*13170Sralph if ((dirp = opendir(SD)) == NULL) 17512117Sralph return(-1); 17612117Sralph if (fstat(dirp->dd_fd, &stbuf) < 0) 17712874Sralph goto errdone; 17812117Sralph 17912117Sralph /* 18012117Sralph * Estimate the array size by taking the size of the directory file 18112117Sralph * and dividing it by a multiple of the minimum size entry. 18212117Sralph */ 18312117Sralph arraysz = (stbuf.st_size / 24); 18412117Sralph queue = (struct queue **)malloc(arraysz * sizeof(struct queue *)); 18512117Sralph if (queue == NULL) 18612874Sralph goto errdone; 18712117Sralph 18812117Sralph nitems = 0; 18912117Sralph while ((d = readdir(dirp)) != NULL) { 19012117Sralph if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 19112117Sralph continue; /* daemon control files only */ 19212117Sralph if (stat(d->d_name, &stbuf) < 0) 19312117Sralph continue; /* Doesn't exist */ 19412117Sralph q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1); 19512117Sralph if (q == NULL) 19612874Sralph goto errdone; 19712117Sralph q->q_time = stbuf.st_mtime; 19812117Sralph strcpy(q->q_name, d->d_name); 19912117Sralph /* 20012117Sralph * Check to make sure the array has space left and 20112117Sralph * realloc the maximum size. 20212117Sralph */ 20312117Sralph if (++nitems > arraysz) { 20412117Sralph queue = (struct queue **)realloc((char *)queue, 20512117Sralph (stbuf.st_size/12) * sizeof(struct queue *)); 20612117Sralph if (queue == NULL) 20712874Sralph goto errdone; 20812117Sralph } 20912117Sralph queue[nitems-1] = q; 21012117Sralph } 21112117Sralph closedir(dirp); 21212117Sralph if (nitems) 21312117Sralph qsort(queue, nitems, sizeof(struct queue *), compar); 21412117Sralph *namelist = queue; 21512117Sralph return(nitems); 21612874Sralph 21712874Sralph errdone: 21812874Sralph closedir(dirp); 21912874Sralph return(-1); 22012117Sralph } 22112117Sralph 22212117Sralph /* 22312117Sralph * Compare modification times. 22412117Sralph */ 22512117Sralph static 22612117Sralph compar(p1, p2) 22712117Sralph register struct queue **p1, **p2; 22812117Sralph { 22912117Sralph if ((*p1)->q_time < (*p2)->q_time) 23012117Sralph return(-1); 23112117Sralph if ((*p1)->q_time > (*p2)->q_time) 23212117Sralph return(1); 23312117Sralph return(0); 23412117Sralph } 23512117Sralph 23612117Sralph /*VARARGS1*/ 23712117Sralph fatal(msg, a1, a2, a3) 23812117Sralph char *msg; 23912117Sralph { 24012117Sralph if (from != host) 24112117Sralph printf("%s: ", host); 24212117Sralph printf("%s: ", name); 24312117Sralph if (printer) 24412117Sralph printf("%s: ", printer); 24512117Sralph printf(msg, a1, a2, a3); 24612117Sralph putchar('\n'); 24712117Sralph exit(1); 24812117Sralph } 249