122426Sdist /* 261840Sbostic * Copyright (c) 1983, 1993 361840Sbostic * The Regents of the University of California. All rights reserved. 465779Sbostic * (c) UNIX System Laboratories, Inc. 565779Sbostic * All or some portions of this file are derived from material licensed 665779Sbostic * to the University of California by American Telephone and Telegraph 765779Sbostic * Co. or Unix System Laboratories, Inc. and are reproduced herein with 865779Sbostic * the permission of UNIX System Laboratories, Inc. 934203Sbostic * 1056248Selan * %sccs.include.redist.c% 1122426Sdist */ 1222426Sdist 1313952Ssam #ifndef lint 14*68971Stef static char sccsid[] = "@(#)common.c 8.3 (Berkeley) 04/27/95"; 1534203Sbostic #endif /* not lint */ 1613952Ssam 1755470Sbostic #include <sys/param.h> 1855470Sbostic #include <sys/stat.h> 1955470Sbostic 2055470Sbostic #include <sys/socket.h> 2155470Sbostic #include <netinet/in.h> 2255470Sbostic #include <netdb.h> 2355470Sbostic 2455470Sbostic #include <dirent.h> 2555470Sbostic #include <errno.h> 2655470Sbostic #include <unistd.h> 2755470Sbostic #include <stdlib.h> 2855470Sbostic #include <stdio.h> 2955470Sbostic #include <string.h> 3055470Sbostic #include "lp.h" 3156120Selan #include "pathnames.h" 3255470Sbostic 3312117Sralph /* 3412117Sralph * Routines and data common to all the line printer functions. 3512117Sralph */ 3612117Sralph 3756120Selan char *AF; /* accounting file */ 3856120Selan long BR; /* baud rate if lp is a tty */ 3956120Selan char *CF; /* name of cifplot filter (per job) */ 4056120Selan char *DF; /* name of tex filter (per job) */ 4156120Selan long DU; /* daeomon user-id */ 4256120Selan long FC; /* flags to clear if lp is a tty */ 4356120Selan char *FF; /* form feed string */ 4456120Selan long FS; /* flags to set if lp is a tty */ 4556120Selan char *GF; /* name of graph(1G) filter (per job) */ 4656120Selan long HL; /* print header last */ 4756120Selan char *IF; /* name of input filter (created per job) */ 4856120Selan char *LF; /* log file for error messages */ 4956120Selan char *LO; /* lock file name */ 5012117Sralph char *LP; /* line printer device name */ 5156120Selan long MC; /* maximum number of copies allowed */ 5256120Selan long MX; /* maximum number of blocks to copy */ 5356120Selan char *NF; /* name of ditroff filter (per job) */ 5456120Selan char *OF; /* name of output filter (created once) */ 5556120Selan char *PF; /* name of vrast filter (per job) */ 5656120Selan long PL; /* page length */ 5756120Selan long PW; /* page width */ 5856120Selan long PX; /* page width in pixels */ 5956120Selan long PY; /* page length in pixels */ 6056120Selan char *RF; /* name of fortran text filter (per job) */ 6156120Selan char *RG; /* resricted group */ 6212117Sralph char *RM; /* remote machine name */ 6312117Sralph char *RP; /* remote printer name */ 6456120Selan long RS; /* restricted to those with local accounts */ 6556120Selan long RW; /* open LP for reading and writing */ 6656120Selan long SB; /* short banner instead of normal header */ 6756120Selan long SC; /* suppress multiple copies */ 6856120Selan char *SD; /* spool directory */ 6956120Selan long SF; /* suppress FF on each print job */ 7056120Selan long SH; /* suppress header page */ 7112117Sralph char *ST; /* status file name */ 7212117Sralph char *TF; /* name of troff filter (per job) */ 7356120Selan char *TR; /* trailer string to be output when Q empties */ 7412117Sralph char *VF; /* name of vplot filter (per job) */ 7556120Selan long XC; /* flags to clear for local mode */ 7656120Selan long XS; /* flags to set for local mode */ 7712117Sralph 7812117Sralph char line[BUFSIZ]; 7956120Selan char *bp; /* pointer into printcap buffer. */ 8012117Sralph char *name; /* program name */ 8112117Sralph char *printer; /* printer name */ 8255470Sbostic /* host machine name */ 8355470Sbostic char host[MAXHOSTNAMELEN]; 8412117Sralph char *from = host; /* client's machine name */ 8538736Stef int sendtorem; /* are we sending to a remote? */ 8656120Selan char *printcapdb[2] = { _PATH_PRINTCAP, 0 }; 8712117Sralph 8855470Sbostic static int compar __P((const void *, const void *)); 8955470Sbostic 9012117Sralph /* 9112117Sralph * Create a connection to the remote printer server. 9212117Sralph * Most of this code comes from rcmd.c. 9312117Sralph */ 9455470Sbostic int 9512528Sralph getport(rhost) 9612528Sralph char *rhost; 9712117Sralph { 9812117Sralph struct hostent *hp; 9912117Sralph struct servent *sp; 10012117Sralph struct sockaddr_in sin; 10112117Sralph int s, timo = 1, lport = IPPORT_RESERVED - 1; 10212874Sralph int err; 10312117Sralph 10412117Sralph /* 10512117Sralph * Get the host address and port number to connect to. 10612117Sralph */ 10712528Sralph if (rhost == NULL) 10812117Sralph fatal("no remote host to connect to"); 10912528Sralph hp = gethostbyname(rhost); 11012117Sralph if (hp == NULL) 11112528Sralph fatal("unknown host %s", rhost); 11212117Sralph sp = getservbyname("printer", "tcp"); 11312117Sralph if (sp == NULL) 11412117Sralph fatal("printer/tcp: unknown service"); 11512117Sralph bzero((char *)&sin, sizeof(sin)); 11612117Sralph bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length); 11712117Sralph sin.sin_family = hp->h_addrtype; 11812117Sralph sin.sin_port = sp->s_port; 11912117Sralph 12012117Sralph /* 12112117Sralph * Try connecting to the server. 12212117Sralph */ 12312117Sralph retry: 12412117Sralph s = rresvport(&lport); 12512117Sralph if (s < 0) 12612117Sralph return(-1); 12746910Sbostic if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 12812874Sralph err = errno; 12912874Sralph (void) close(s); 13012874Sralph errno = err; 13112117Sralph if (errno == EADDRINUSE) { 13212117Sralph lport--; 13312117Sralph goto retry; 13412117Sralph } 13512117Sralph if (errno == ECONNREFUSED && timo <= 16) { 13612117Sralph sleep(timo); 13712117Sralph timo *= 2; 13812117Sralph goto retry; 13912117Sralph } 14012117Sralph return(-1); 14112117Sralph } 14212117Sralph return(s); 14312117Sralph } 14412117Sralph 14512117Sralph /* 14612117Sralph * Getline reads a line from the control file cfp, removes tabs, converts 14712117Sralph * new-line to null and leaves it in line. 14812117Sralph * Returns 0 at EOF or the number of characters read. 14912117Sralph */ 15055470Sbostic int 15112117Sralph getline(cfp) 15212117Sralph FILE *cfp; 15312117Sralph { 15412117Sralph register int linel = 0; 15512117Sralph register char *lp = line; 15612117Sralph register c; 15712117Sralph 15812117Sralph while ((c = getc(cfp)) != '\n') { 15912117Sralph if (c == EOF) 16012117Sralph return(0); 16112117Sralph if (c == '\t') { 16212117Sralph do { 16312117Sralph *lp++ = ' '; 16412117Sralph linel++; 16512117Sralph } while ((linel & 07) != 0); 16612117Sralph continue; 16712117Sralph } 16812117Sralph *lp++ = c; 16912117Sralph linel++; 17012117Sralph } 17112117Sralph *lp++ = '\0'; 17212117Sralph return(linel); 17312117Sralph } 17412117Sralph 17512117Sralph /* 17612117Sralph * Scan the current directory and make a list of daemon files sorted by 17712117Sralph * creation time. 17812117Sralph * Return the number of entries and a pointer to the list. 17912117Sralph */ 18055470Sbostic int 18112117Sralph getq(namelist) 18212117Sralph struct queue *(*namelist[]); 18312117Sralph { 18455470Sbostic register struct dirent *d; 18512117Sralph register struct queue *q, **queue; 18612117Sralph register int nitems; 18712117Sralph struct stat stbuf; 18812117Sralph DIR *dirp; 18946910Sbostic int arraysz; 19012117Sralph 19113170Sralph if ((dirp = opendir(SD)) == NULL) 19212117Sralph return(-1); 19312117Sralph if (fstat(dirp->dd_fd, &stbuf) < 0) 19412874Sralph goto errdone; 19512117Sralph 19612117Sralph /* 19712117Sralph * Estimate the array size by taking the size of the directory file 19812117Sralph * and dividing it by a multiple of the minimum size entry. 19912117Sralph */ 20012117Sralph arraysz = (stbuf.st_size / 24); 20112117Sralph queue = (struct queue **)malloc(arraysz * sizeof(struct queue *)); 20212117Sralph if (queue == NULL) 20312874Sralph goto errdone; 20412117Sralph 20512117Sralph nitems = 0; 20612117Sralph while ((d = readdir(dirp)) != NULL) { 20712117Sralph if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 20812117Sralph continue; /* daemon control files only */ 20912117Sralph if (stat(d->d_name, &stbuf) < 0) 21012117Sralph continue; /* Doesn't exist */ 21112117Sralph q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1); 21212117Sralph if (q == NULL) 21312874Sralph goto errdone; 21412117Sralph q->q_time = stbuf.st_mtime; 21512117Sralph strcpy(q->q_name, d->d_name); 21612117Sralph /* 21712117Sralph * Check to make sure the array has space left and 21812117Sralph * realloc the maximum size. 21912117Sralph */ 22012117Sralph if (++nitems > arraysz) { 221*68971Stef arraysz *= 2; 22212117Sralph queue = (struct queue **)realloc((char *)queue, 223*68971Stef arraysz * sizeof(struct queue *)); 22412117Sralph if (queue == NULL) 22512874Sralph goto errdone; 22612117Sralph } 22712117Sralph queue[nitems-1] = q; 22812117Sralph } 22912117Sralph closedir(dirp); 23012117Sralph if (nitems) 23112117Sralph qsort(queue, nitems, sizeof(struct queue *), compar); 23212117Sralph *namelist = queue; 23312117Sralph return(nitems); 23412874Sralph 23512874Sralph errdone: 23612874Sralph closedir(dirp); 23712874Sralph return(-1); 23812117Sralph } 23912117Sralph 24012117Sralph /* 24112117Sralph * Compare modification times. 24212117Sralph */ 24355470Sbostic static int 24412117Sralph compar(p1, p2) 24555470Sbostic const void *p1, *p2; 24612117Sralph { 24755470Sbostic if ((*(struct queue **)p1)->q_time < (*(struct queue **)p2)->q_time) 24812117Sralph return(-1); 24955470Sbostic if ((*(struct queue **)p1)->q_time > (*(struct queue **)p2)->q_time) 25012117Sralph return(1); 25112117Sralph return(0); 25212117Sralph } 25312117Sralph 25438736Stef /* 25538736Stef * Figure out whether the local machine is the same 25638736Stef * as the remote machine (RM) entry (if it exists). 25738736Stef */ 25838736Stef char * 25938736Stef checkremote() 26038736Stef { 26138736Stef char name[MAXHOSTNAMELEN]; 26238736Stef register struct hostent *hp; 26338736Stef static char errbuf[128]; 26438736Stef 26538736Stef sendtorem = 0; /* assume printer is local */ 26638736Stef if (RM != (char *)NULL) { 26738736Stef /* get the official name of the local host */ 26838736Stef gethostname(name, sizeof(name)); 26938736Stef name[sizeof(name)-1] = '\0'; 27038736Stef hp = gethostbyname(name); 27138736Stef if (hp == (struct hostent *) NULL) { 27255470Sbostic (void) snprintf(errbuf, sizeof(errbuf), 27338736Stef "unable to get official name for local machine %s", 27438736Stef name); 27538736Stef return errbuf; 27638736Stef } else (void) strcpy(name, hp->h_name); 27738736Stef 27838736Stef /* get the official name of RM */ 27938736Stef hp = gethostbyname(RM); 28038736Stef if (hp == (struct hostent *) NULL) { 28155470Sbostic (void) snprintf(errbuf, sizeof(errbuf), 28238736Stef "unable to get official name for remote machine %s", 28338736Stef RM); 28438736Stef return errbuf; 28538736Stef } 28638736Stef 28738736Stef /* 28838736Stef * if the two hosts are not the same, 28938736Stef * then the printer must be remote. 29038736Stef */ 291*68971Stef if (strcasecmp(name, hp->h_name) != 0) 29238736Stef sendtorem = 1; 29338736Stef } 294*68971Stef return NULL; 29538736Stef } 29638736Stef 29755470Sbostic #if __STDC__ 29855470Sbostic #include <stdarg.h> 29955470Sbostic #else 30055470Sbostic #include <varargs.h> 30155470Sbostic #endif 30255470Sbostic 30355470Sbostic void 30455470Sbostic #if __STDC__ 30555470Sbostic fatal(const char *msg, ...) 30655470Sbostic #else 30755470Sbostic fatal(msg, va_alist) 30812117Sralph char *msg; 30955470Sbostic va_dcl 31055470Sbostic #endif 31112117Sralph { 31255470Sbostic va_list ap; 31355470Sbostic #if __STDC__ 31455470Sbostic va_start(ap, msg); 31555470Sbostic #else 31655470Sbostic va_start(ap); 31755470Sbostic #endif 31812117Sralph if (from != host) 31955470Sbostic (void)printf("%s: ", host); 32055470Sbostic (void)printf("%s: ", name); 32112117Sralph if (printer) 32255470Sbostic (void)printf("%s: ", printer); 32355470Sbostic (void)vprintf(msg, ap); 32455470Sbostic va_end(ap); 32555470Sbostic (void)putchar('\n'); 32612117Sralph exit(1); 32712117Sralph } 328