Home | History | Annotate | Line # | Download | only in common_source
common.c revision 1.36
      1  1.36  christos /*	$NetBSD: common.c,v 1.36 2006/05/25 00:19:27 christos Exp $	*/
      2   1.8       jtc 
      3   1.4       cgd /*
      4   1.5       cgd  * Copyright (c) 1983, 1993
      5   1.5       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.4       cgd  * (c) UNIX System Laboratories, Inc.
      7   1.4       cgd  * All or some portions of this file are derived from material licensed
      8   1.4       cgd  * to the University of California by American Telephone and Telegraph
      9   1.4       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10   1.4       cgd  * the permission of UNIX System Laboratories, Inc.
     11   1.4       cgd  *
     12   1.4       cgd  * Redistribution and use in source and binary forms, with or without
     13   1.4       cgd  * modification, are permitted provided that the following conditions
     14   1.4       cgd  * are met:
     15   1.4       cgd  * 1. Redistributions of source code must retain the above copyright
     16   1.4       cgd  *    notice, this list of conditions and the following disclaimer.
     17   1.4       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.4       cgd  *    notice, this list of conditions and the following disclaimer in the
     19   1.4       cgd  *    documentation and/or other materials provided with the distribution.
     20  1.25       agc  * 3. Neither the name of the University nor the names of its contributors
     21   1.4       cgd  *    may be used to endorse or promote products derived from this software
     22   1.4       cgd  *    without specific prior written permission.
     23   1.4       cgd  *
     24   1.4       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.4       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.4       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.4       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.4       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.4       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.4       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.4       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.4       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.4       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.4       cgd  * SUCH DAMAGE.
     35   1.4       cgd  */
     36   1.4       cgd 
     37  1.11       mrg #include <sys/cdefs.h>
     38   1.4       cgd #ifndef lint
     39  1.11       mrg #if 0
     40  1.10       mrg static char sccsid[] = "@(#)common.c	8.5 (Berkeley) 4/28/95";
     41  1.11       mrg #else
     42  1.36  christos __RCSID("$NetBSD: common.c,v 1.36 2006/05/25 00:19:27 christos Exp $");
     43  1.11       mrg #endif
     44   1.4       cgd #endif /* not lint */
     45   1.4       cgd 
     46   1.5       cgd #include <sys/param.h>
     47   1.5       cgd #include <sys/stat.h>
     48  1.10       mrg #include <sys/time.h>
     49   1.5       cgd 
     50   1.5       cgd #include <sys/socket.h>
     51   1.5       cgd #include <netinet/in.h>
     52  1.11       mrg #include <arpa/inet.h>
     53   1.5       cgd #include <netdb.h>
     54   1.5       cgd 
     55   1.5       cgd #include <dirent.h>
     56   1.5       cgd #include <errno.h>
     57   1.5       cgd #include <unistd.h>
     58   1.5       cgd #include <stdlib.h>
     59   1.5       cgd #include <stdio.h>
     60   1.5       cgd #include <string.h>
     61  1.21    itojun #include <ifaddrs.h>
     62   1.5       cgd #include "lp.h"
     63  1.28  christos #include "lp.local.h"
     64   1.5       cgd #include "pathnames.h"
     65   1.5       cgd 
     66   1.4       cgd /*
     67   1.4       cgd  * Routines and data common to all the line printer functions.
     68   1.4       cgd  */
     69   1.4       cgd 
     70  1.28  christos const char	*AF;		/* accounting file */
     71  1.28  christos long		 BR;		/* baud rate if lp is a tty */
     72  1.28  christos const char	*CF;		/* name of cifplot filter (per job) */
     73  1.28  christos const char	*DF;		/* name of tex filter (per job) */
     74  1.28  christos long		 DU;		/* daeomon user-id */
     75  1.28  christos long		 FC;		/* flags to clear if lp is a tty */
     76  1.28  christos const char	*FF;		/* form feed string */
     77  1.28  christos long		 FS;		/* flags to set if lp is a tty */
     78  1.28  christos const char	*GF;		/* name of graph(1G) filter (per job) */
     79  1.28  christos long		 HL;		/* print header last */
     80  1.28  christos const char	*IF;		/* name of input filter (created per job) */
     81  1.28  christos const char	*LF;		/* log file for error messages */
     82  1.28  christos const char	*LO;		/* lock file name */
     83  1.28  christos const char	*LP;		/* line printer device name */
     84  1.28  christos long		 MC;		/* maximum number of copies allowed */
     85  1.28  christos const char	*MS;		/* stty flags to set if lp is a tty */
     86  1.28  christos long		 MX;		/* maximum number of blocks to copy */
     87  1.28  christos const char	*NF;		/* name of ditroff filter (per job) */
     88  1.28  christos const char	*OF;		/* name of output filter (created once) */
     89  1.31   garbled const char	*PF;		/* name of postscript filter (per job) */
     90  1.28  christos long		 PL;		/* page length */
     91  1.28  christos long		 PW;		/* page width */
     92  1.28  christos long		 PX;		/* page width in pixels */
     93  1.28  christos long		 PY;		/* page length in pixels */
     94  1.28  christos const char	*RF;		/* name of fortran text filter (per job) */
     95  1.28  christos const char	*RG;		/* resricted group */
     96  1.28  christos const char	*RM;		/* remote machine name */
     97  1.28  christos const char	*RP;		/* remote printer name */
     98  1.28  christos long		 RS;		/* restricted to those with local accounts */
     99  1.28  christos long		 RW;		/* open LP for reading and writing */
    100  1.28  christos long		 SB;		/* short banner instead of normal header */
    101  1.28  christos long		 SC;		/* suppress multiple copies */
    102  1.28  christos const char	*SD;		/* spool directory */
    103  1.28  christos long		 SF;		/* suppress FF on each print job */
    104  1.28  christos long		 SH;		/* suppress header page */
    105  1.28  christos const char	*ST;		/* status file name */
    106  1.28  christos const char	*TF;		/* name of troff filter (per job) */
    107  1.28  christos const char	*TR;		/* trailer string to be output when Q empties */
    108  1.31   garbled const char	*VF;		/* name of vplot/vrast filter (per job) */
    109  1.28  christos long		 XC;		/* flags to clear for local mode */
    110  1.28  christos long		 XS;		/* flags to set for local mode */
    111   1.4       cgd 
    112   1.4       cgd char	line[BUFSIZ];
    113  1.10       mrg int	remote;		/* true if sending files to a remote host */
    114   1.5       cgd 
    115   1.6   hpeyerl extern uid_t	uid, euid;
    116   1.6   hpeyerl 
    117  1.23       wiz static int compar(const void *, const void *);
    118   1.4       cgd 
    119  1.33  christos const char *
    120  1.33  christos gethost(const char *hname)
    121  1.33  christos {
    122  1.33  christos 	const char *p = strchr(hname, '@');
    123  1.33  christos 	return p ? ++p : hname;
    124  1.33  christos }
    125  1.33  christos 
    126   1.4       cgd /*
    127  1.33  christos  * Create a TCP connection to host "rhost". If "rhost" is of the
    128  1.33  christos  * form port@host, use the specified port. Otherwise use the
    129  1.33  christos  * default printer port. Most of this code comes from rcmd.c.
    130   1.4       cgd  */
    131   1.5       cgd int
    132  1.33  christos getport(const char *rhost)
    133   1.4       cgd {
    134  1.17    itojun 	struct addrinfo hints, *res, *r;
    135  1.15       mrg 	u_int timo = 1;
    136  1.15       mrg 	int s, lport = IPPORT_RESERVED - 1;
    137  1.17    itojun 	int error;
    138  1.18    itojun 	int refuse, trial;
    139  1.33  christos 	char hbuf[NI_MAXSERV], *ptr;
    140  1.33  christos 	const char *port = "printer";
    141  1.33  christos 	const char *hostname = rhost;
    142   1.4       cgd 
    143   1.4       cgd 	/*
    144   1.4       cgd 	 * Get the host address and port number to connect to.
    145   1.4       cgd 	 */
    146   1.4       cgd 	if (rhost == NULL)
    147   1.4       cgd 		fatal("no remote host to connect to");
    148  1.33  christos 	(void)strlcpy(hbuf, rhost, sizeof(hbuf));
    149  1.33  christos 	for (ptr = hbuf; *ptr; ptr++)
    150  1.33  christos 		if (*ptr == '@') {
    151  1.33  christos 			*ptr++ = '\0';
    152  1.33  christos 			port = hbuf;
    153  1.33  christos 			hostname = ptr;
    154  1.33  christos 			break;
    155  1.33  christos 		}
    156  1.33  christos 	(void)memset(&hints, 0, sizeof(hints));
    157  1.17    itojun 	hints.ai_family = PF_UNSPEC;
    158  1.17    itojun 	hints.ai_socktype = SOCK_STREAM;
    159  1.33  christos 	error = getaddrinfo(hostname, port, &hints, &res);
    160  1.17    itojun 	if (error)
    161  1.17    itojun 		fatal("printer/tcp: %s", gai_strerror(error));
    162   1.4       cgd 
    163   1.4       cgd 	/*
    164   1.4       cgd 	 * Try connecting to the server.
    165   1.4       cgd 	 */
    166  1.18    itojun retry:
    167  1.17    itojun 	s = -1;
    168  1.18    itojun 	refuse = trial = 0;
    169  1.17    itojun 	for (r = res; r; r = r->ai_next) {
    170  1.18    itojun 		trial++;
    171  1.18    itojun retryport:
    172  1.17    itojun 		seteuid(euid);
    173  1.17    itojun 		s = rresvport_af(&lport, r->ai_family);
    174  1.17    itojun 		seteuid(uid);
    175  1.17    itojun 		if (s < 0)
    176  1.17    itojun 			return(-1);
    177  1.17    itojun 		if (connect(s, r->ai_addr, r->ai_addrlen) < 0) {
    178  1.17    itojun 			error = errno;
    179  1.17    itojun 			(void)close(s);
    180  1.17    itojun 			s = -1;
    181  1.17    itojun 			errno = error;
    182  1.17    itojun 			if (errno == EADDRINUSE) {
    183  1.17    itojun 				lport--;
    184  1.18    itojun 				goto retryport;
    185  1.18    itojun 			} else if (errno == ECONNREFUSED)
    186  1.18    itojun 				refuse++;
    187  1.17    itojun 			continue;
    188  1.17    itojun 		} else
    189  1.17    itojun 			break;
    190  1.18    itojun 	}
    191  1.18    itojun 	if (s < 0 && trial == refuse && timo <= 16) {
    192  1.18    itojun 		sleep(timo);
    193  1.18    itojun 		timo *= 2;
    194  1.18    itojun 		goto retry;
    195   1.4       cgd 	}
    196  1.17    itojun 	if (res)
    197  1.17    itojun 		freeaddrinfo(res);
    198   1.4       cgd 	return(s);
    199   1.4       cgd }
    200   1.4       cgd 
    201   1.4       cgd /*
    202   1.4       cgd  * Getline reads a line from the control file cfp, removes tabs, converts
    203   1.4       cgd  *  new-line to null and leaves it in line.
    204   1.4       cgd  * Returns 0 at EOF or the number of characters read.
    205   1.4       cgd  */
    206   1.5       cgd int
    207  1.23       wiz getline(FILE *cfp)
    208   1.4       cgd {
    209  1.11       mrg 	int linel = 0, c;
    210  1.11       mrg 	char *lp = line;
    211   1.4       cgd 
    212  1.11       mrg 	while ((c = getc(cfp)) != '\n' && linel+1<sizeof(line)) {
    213   1.4       cgd 		if (c == EOF)
    214   1.4       cgd 			return(0);
    215   1.4       cgd 		if (c == '\t') {
    216   1.4       cgd 			do {
    217   1.4       cgd 				*lp++ = ' ';
    218   1.4       cgd 				linel++;
    219  1.11       mrg 			} while ((linel & 07) != 0 && linel+1 < sizeof(line));
    220   1.4       cgd 			continue;
    221   1.4       cgd 		}
    222   1.4       cgd 		*lp++ = c;
    223   1.4       cgd 		linel++;
    224   1.4       cgd 	}
    225   1.4       cgd 	*lp++ = '\0';
    226   1.4       cgd 	return(linel);
    227   1.4       cgd }
    228   1.4       cgd 
    229   1.4       cgd /*
    230   1.4       cgd  * Scan the current directory and make a list of daemon files sorted by
    231   1.4       cgd  * creation time.
    232   1.4       cgd  * Return the number of entries and a pointer to the list.
    233   1.4       cgd  */
    234   1.5       cgd int
    235  1.23       wiz getq(struct queue **namelist[])
    236   1.4       cgd {
    237  1.11       mrg 	struct dirent *d;
    238  1.35  christos 	struct queue *q, **queue = NULL, **nqueue;
    239   1.4       cgd 	struct stat stbuf;
    240   1.4       cgd 	DIR *dirp;
    241  1.35  christos 	u_int nitems = 0, arraysz;
    242   1.4       cgd 
    243   1.6   hpeyerl 	seteuid(euid);
    244  1.22       wiz 	dirp = opendir(SD);
    245  1.22       wiz 	seteuid(uid);
    246  1.22       wiz 	if (dirp == NULL)
    247   1.4       cgd 		return(-1);
    248   1.4       cgd 	if (fstat(dirp->dd_fd, &stbuf) < 0)
    249   1.4       cgd 		goto errdone;
    250   1.4       cgd 
    251   1.4       cgd 	/*
    252   1.4       cgd 	 * Estimate the array size by taking the size of the directory file
    253   1.4       cgd 	 * and dividing it by a multiple of the minimum size entry.
    254   1.4       cgd 	 */
    255  1.15       mrg 	arraysz = (int)(stbuf.st_size / 24);
    256  1.35  christos 	queue = calloc(arraysz, sizeof(struct queue *));
    257   1.4       cgd 	if (queue == NULL)
    258   1.4       cgd 		goto errdone;
    259   1.4       cgd 
    260   1.4       cgd 	while ((d = readdir(dirp)) != NULL) {
    261   1.4       cgd 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
    262   1.4       cgd 			continue;	/* daemon control files only */
    263   1.6   hpeyerl 		seteuid(euid);
    264  1.22       wiz 		if (stat(d->d_name, &stbuf) < 0) {
    265  1.22       wiz 			seteuid(uid);
    266   1.4       cgd 			continue;	/* Doesn't exist */
    267  1.22       wiz 		}
    268   1.6   hpeyerl 		seteuid(uid);
    269   1.4       cgd 		q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
    270   1.4       cgd 		if (q == NULL)
    271   1.4       cgd 			goto errdone;
    272   1.4       cgd 		q->q_time = stbuf.st_mtime;
    273   1.9       mrg 		strcpy(q->q_name, d->d_name);	/* XXX: strcpy is safe */
    274   1.4       cgd 		/*
    275   1.4       cgd 		 * Check to make sure the array has space left and
    276   1.4       cgd 		 * realloc the maximum size.
    277   1.4       cgd 		 */
    278   1.4       cgd 		if (++nitems > arraysz) {
    279  1.26    itojun 			nqueue = (struct queue **)realloc(queue,
    280  1.26    itojun 				arraysz * 2 * sizeof(struct queue *));
    281  1.36  christos 			if (nqueue == NULL) {
    282  1.36  christos 				free(q)
    283  1.26    itojun 				goto errdone;
    284  1.36  christos 			}
    285  1.35  christos 			(void)memset(&nqueue[arraysz], 0,
    286  1.35  christos 			    arraysz * sizeof(struct queueue *));
    287  1.26    itojun 			queue = nqueue;
    288  1.10       mrg 			arraysz *= 2;
    289   1.4       cgd 		}
    290   1.4       cgd 		queue[nitems-1] = q;
    291   1.4       cgd 	}
    292   1.4       cgd 	closedir(dirp);
    293   1.4       cgd 	if (nitems)
    294   1.4       cgd 		qsort(queue, nitems, sizeof(struct queue *), compar);
    295   1.4       cgd 	*namelist = queue;
    296   1.4       cgd 	return(nitems);
    297   1.4       cgd 
    298   1.4       cgd errdone:
    299  1.35  christos 	freeq(queue, nitems);
    300   1.4       cgd 	closedir(dirp);
    301   1.4       cgd 	return(-1);
    302   1.4       cgd }
    303   1.4       cgd 
    304  1.35  christos void
    305  1.35  christos freeq(struct queue **namelist, u_int nitems)
    306  1.35  christos {
    307  1.35  christos 	u_int i;
    308  1.35  christos 	if (namelist == NULL)
    309  1.35  christos 		return;
    310  1.35  christos 	for (i = 0; i < nitems; i++)
    311  1.35  christos 		if (namelist[i])
    312  1.35  christos 			free(namelist[i]);
    313  1.35  christos 	free(namelist);
    314  1.35  christos }
    315  1.35  christos 
    316   1.4       cgd /*
    317   1.4       cgd  * Compare modification times.
    318   1.4       cgd  */
    319   1.5       cgd static int
    320  1.23       wiz compar(const void *p1, const void *p2)
    321   1.4       cgd {
    322  1.28  christos 	const struct queue *const *q1 = p1;
    323  1.28  christos 	const struct queue *const *q2 = p2;
    324  1.28  christos 
    325  1.28  christos 	if ((*q1)->q_time < (*q2)->q_time)
    326  1.28  christos 		return -1;
    327  1.28  christos 	if ((*q1)->q_time > (*q2)->q_time)
    328  1.28  christos 		return 1;
    329  1.28  christos 	return 0;
    330   1.4       cgd }
    331   1.4       cgd 
    332   1.4       cgd /*
    333   1.4       cgd  * Figure out whether the local machine is the same
    334   1.4       cgd  * as the remote machine (RM) entry (if it exists).
    335   1.4       cgd  */
    336  1.28  christos const char *
    337  1.23       wiz checkremote(void)
    338   1.4       cgd {
    339  1.21    itojun 	char lname[NI_MAXHOST], rname[NI_MAXHOST];
    340  1.21    itojun 	struct addrinfo hints, *res, *res0;
    341   1.4       cgd 	static char errbuf[128];
    342  1.17    itojun 	int error;
    343  1.21    itojun 	struct ifaddrs *ifap, *ifa;
    344  1.21    itojun 	const int niflags = NI_NUMERICHOST;
    345  1.21    itojun #ifdef __KAME__
    346  1.21    itojun 	struct sockaddr_in6 sin6;
    347  1.21    itojun 	struct sockaddr_in6 *sin6p;
    348  1.21    itojun #endif
    349  1.21    itojun 
    350  1.21    itojun 	remote = 0;	/* assume printer is local on failure */
    351   1.4       cgd 
    352  1.21    itojun 	if (RM == NULL)
    353  1.21    itojun 		return NULL;
    354  1.21    itojun 
    355  1.21    itojun 	/* get the local interface addresses */
    356  1.21    itojun 	if (getifaddrs(&ifap) < 0) {
    357  1.21    itojun 		(void)snprintf(errbuf, sizeof(errbuf),
    358  1.21    itojun 		    "unable to get local interface address: %s",
    359  1.21    itojun 		    strerror(errno));
    360  1.21    itojun 		return errbuf;
    361  1.21    itojun 	}
    362   1.4       cgd 
    363  1.21    itojun 	/* get the remote host addresses (RM) */
    364  1.21    itojun 	memset(&hints, 0, sizeof(hints));
    365  1.21    itojun 	hints.ai_flags = AI_CANONNAME;
    366  1.21    itojun 	hints.ai_family = PF_UNSPEC;
    367  1.21    itojun 	hints.ai_socktype = SOCK_STREAM;
    368  1.21    itojun 	res = NULL;
    369  1.33  christos 	error = getaddrinfo(gethost(RM), NULL, &hints, &res0);
    370  1.21    itojun 	if (error) {
    371  1.21    itojun 		(void)snprintf(errbuf, sizeof(errbuf),
    372  1.21    itojun 		    "unable to resolve remote machine %s: %s",
    373  1.21    itojun 		    RM, gai_strerror(error));
    374  1.21    itojun 		freeifaddrs(ifap);
    375  1.21    itojun 		return errbuf;
    376  1.21    itojun 	}
    377   1.4       cgd 
    378  1.21    itojun 	remote = 1;	/* assume printer is remote */
    379  1.17    itojun 
    380  1.21    itojun 	for (res = res0; res; res = res->ai_next) {
    381  1.21    itojun 		if (getnameinfo(res->ai_addr, res->ai_addrlen,
    382  1.21    itojun 		    rname, sizeof(rname), NULL, 0, niflags) != 0)
    383  1.21    itojun 			continue;
    384  1.21    itojun 		for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    385  1.21    itojun #ifdef __KAME__
    386  1.21    itojun 			sin6p = (struct sockaddr_in6 *)ifa->ifa_addr;
    387  1.21    itojun 			if (ifa->ifa_addr->sa_family == AF_INET6 &&
    388  1.21    itojun 			    ifa->ifa_addr->sa_len == sizeof(sin6) &&
    389  1.21    itojun 			    IN6_IS_ADDR_LINKLOCAL(&sin6p->sin6_addr) &&
    390  1.21    itojun 			    *(u_int16_t *)&sin6p->sin6_addr.s6_addr[2]) {
    391  1.21    itojun 				/* kame scopeid hack */
    392  1.21    itojun 				memcpy(&sin6, ifa->ifa_addr, sizeof(sin6));
    393  1.21    itojun 				sin6.sin6_scope_id =
    394  1.21    itojun 				    ntohs(*(u_int16_t *)&sin6p->sin6_addr.s6_addr[2]);
    395  1.21    itojun 				sin6.sin6_addr.s6_addr[2] = 0;
    396  1.21    itojun 				sin6.sin6_addr.s6_addr[3] = 0;
    397  1.21    itojun 				if (getnameinfo((struct sockaddr *)&sin6,
    398  1.21    itojun 				    sin6.sin6_len, lname, sizeof(lname),
    399  1.21    itojun 				    NULL, 0, niflags) != 0)
    400  1.21    itojun 					continue;
    401  1.21    itojun 			} else
    402  1.21    itojun #endif
    403  1.21    itojun 			if (getnameinfo(ifa->ifa_addr, ifa->ifa_addr->sa_len,
    404  1.21    itojun 			    lname, sizeof(lname), NULL, 0, niflags) != 0)
    405  1.21    itojun 				continue;
    406  1.21    itojun 
    407  1.21    itojun 			if (strcmp(rname, lname) == 0) {
    408  1.21    itojun 				remote = 0;
    409  1.21    itojun 				goto done;
    410  1.21    itojun 			}
    411  1.21    itojun 		}
    412   1.4       cgd 	}
    413  1.21    itojun done:
    414  1.21    itojun 	freeaddrinfo(res0);
    415  1.21    itojun 	freeifaddrs(ifap);
    416  1.10       mrg 	return NULL;
    417  1.10       mrg }
    418  1.10       mrg 
    419  1.10       mrg /* sleep n milliseconds */
    420  1.10       mrg void
    421  1.23       wiz delay(int n)
    422  1.10       mrg {
    423  1.24   mycroft 	struct timespec tdelay;
    424  1.10       mrg 
    425  1.10       mrg 	if (n <= 0 || n > 10000)
    426  1.10       mrg 		fatal("unreasonable delay period (%d)", n);
    427  1.10       mrg 	tdelay.tv_sec = n / 1000;
    428  1.24   mycroft 	tdelay.tv_nsec = (n % 1000) * 1000000;
    429  1.24   mycroft 	nanosleep(&tdelay, NULL);
    430   1.4       cgd }
    431  1.28  christos 
    432  1.28  christos void
    433  1.28  christos getprintcap(const char *pr)
    434  1.28  christos {
    435  1.28  christos 	char *cp;
    436  1.28  christos 	const char *dp;
    437  1.28  christos 	int i;
    438  1.28  christos 
    439  1.28  christos 	if ((i = cgetent(&bp, printcapdb, pr)) == -2)
    440  1.28  christos 		fatal("can't open printer description file");
    441  1.28  christos 	else if (i == -1)
    442  1.28  christos 		fatal("unknown printer: %s", pr);
    443  1.28  christos 	else if (i == -3)
    444  1.28  christos 		fatal("potential reference loop detected in printcap file");
    445  1.28  christos 
    446  1.28  christos 	LP = cgetstr(bp, DEFLP, &cp) == -1 ? _PATH_DEFDEVLP : cp;
    447  1.28  christos 	RP = cgetstr(bp, "rp", &cp) == -1 ? DEFLP : cp;
    448  1.28  christos 	SD = cgetstr(bp, "sd", &cp) == -1 ? _PATH_DEFSPOOL : cp;
    449  1.28  christos 	LO = cgetstr(bp, "lo", &cp) == -1 ? DEFLOCK : cp;
    450  1.28  christos 	ST = cgetstr(bp, "st", &cp) == -1 ? DEFSTAT : cp;
    451  1.28  christos 	RM = cgetstr(bp, "rm", &cp) == -1 ? NULL : cp;
    452  1.28  christos 	if ((dp = checkremote()) != NULL)
    453  1.28  christos 		printf("Warning: %s\n", dp);
    454  1.28  christos 	LF = cgetstr(bp, "lf", &cp) == -1 ? _PATH_CONSOLE : cp;
    455  1.28  christos }
    456  1.28  christos 
    457  1.28  christos /*
    458  1.28  christos  * Make sure there's some work to do before forking off a child
    459  1.28  christos  */
    460  1.28  christos int
    461  1.28  christos ckqueue(char *cap)
    462  1.28  christos {
    463  1.28  christos 	struct dirent *d;
    464  1.28  christos 	DIR *dirp;
    465  1.28  christos 	const char *spooldir;
    466  1.30   garbled 	char *sd = NULL;
    467  1.28  christos 	int rv = 0;
    468  1.28  christos 
    469  1.28  christos 	spooldir = cgetstr(cap, "sd", &sd) == -1 ? _PATH_DEFSPOOL : sd;
    470  1.28  christos 	if ((dirp = opendir(spooldir)) == NULL) {
    471  1.28  christos 		rv = -1;
    472  1.28  christos 		goto out;
    473  1.28  christos 	}
    474  1.28  christos 	while ((d = readdir(dirp)) != NULL) {
    475  1.28  christos 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
    476  1.28  christos 			continue;	/* daemon control files only */
    477  1.28  christos 		rv = 1;
    478  1.28  christos 		break;
    479  1.28  christos 	}
    480  1.28  christos out:
    481  1.28  christos 	if (dirp != NULL)
    482  1.28  christos 		closedir(dirp);
    483  1.28  christos 	if (spooldir != sd)
    484  1.28  christos 		free(sd);
    485  1.32   garbled 	return (rv);
    486  1.28  christos }
    487