📄 common.c
字号:
#ifndef lintstatic char *sccsid = "@(#)common.c 4.1 ULTRIX 7/2/90";#endif/************************************************************************ * * * Copyright (c) 1988-1990 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * This software is derived from software received from the * * University of California, Berkeley, and from Bell * * Laboratories. Use, duplication, or disclosure is subject to * * restrictions under license agreements with University of * * California and with AT&T. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************//* * common.c -- Routines and data common to all the line printer functions. *//* SCCS history beginning * *************************************************************** * -- Revision History -- * *************************************************************** * * 1.1 01/11/83 -- sccs * date and time created 83/11/01 20:54:02 by sccs * * *************************************************************** * * 1.2 12/12/84 -- root * added PP definition - lp * * * *************************************************************** * * 1.3 17/07/86 -- root * Comments taken from: 1.3: * *** Working Pool Statistics ***: 1.3 86/07/02 17:41:19 hoffman 00004/00000/00253 * * added LAT 5.1 host-initiated connect * * * * * *************************************************************** * * 1.4 02/10/86 -- root * Comments taken from: 1.4: * *** Working Pool Statistics ***: 1.4 86/07/08 17:17:53 hoffman 00001/00000/00257 * * Added pass-thru filter parameter xf * * * * * * * *************************************************************** * * 1.5 17/05/88 -- thoms * Increased size of strings area for printcap * * * *************************************************************** * * 1.6 20/05/88 -- thoms * Amended sizes to handle extra printcap entries. * Added limit pointer to check string area overflow (bp_lim) * * * *************************************************************** * * 1.7 11/07/88 -- thoms * Added copyright notice * 1.8 02/07/90 -- sue * Changed name of rresvport to rresvport_lp to not conflict with * the libc.a routine. * * SCCS history end */#include "lp.h"int DU; /* daeomon user-id */int MX; /* maximum number of blocks to copy */int MC; /* maximum number of copies allowed */char *LP; /* line printer device name */char *RM; /* remote machine name */char *RP; /* remote printer name */char *LO; /* lock file name */char *ST; /* status file name */char *SD; /* spool directory */char *AF; /* accounting file */char *LF; /* log file for error messages */char *OF; /* name of output filter (created once) */char *IF; /* name of input filter (created per job) */char *RF; /* name of fortran text filter (per job) */char *TF; /* name of troff filter (per job) */char *NF; /* name of ditroff filter (per job) */char *DF; /* name of tex filter (per job) */char *GF; /* name of graph(1G) filter (per job) */char *VF; /* name of vplot filter (per job) */char *CF; /* name of cifplot filter (per job) */char *PF; /* name of vrast filter (per job) */char *PP; /* name of print filter replacement */char *XF; /* name of pass-thru filter */char *FF; /* form feed string */char *TR; /* trailer string to be output when Q empties */char *TS; /* terminal server node name */char *OP; /* object port on terminal server */char *OS; /* object service on terminal server */char *DQ; /* notify DQS */short SC; /* suppress multiple copies */short SF; /* suppress FF on each print job */short SH; /* suppress header page */short SB; /* short banner instead of normal header */short RW; /* open LP for reading and writing */short PW; /* page width */short PL; /* page length */short PX; /* page width in pixels */short PY; /* page length in pixels */short BR; /* baud rate if lp is a tty */int FC; /* flags to clear if lp is a tty */int FS; /* flags to set if lp is a tty */int XC; /* flags to clear for local mode */int XS; /* flags to set for local mode */short RS; /* restricted to those with local accounts */char line[BUFSIZ];char pbuf[PRINTCAP_BUFSIZ]; /* buffer for printcap strings */char *bp = pbuf; /* pointer into pbuf for pgetent() */char *bp_lim = &pbuf[PRINTCAP_BUFSIZ]; /* limit for bp */char *name; /* program name */char *printer; /* printer name */char host[32]; /* host machine name */char *from = host; /* client's machine name *//* * Create a connection to the remote printer server. * Most of this code comes from rcmd.c. */getport(rhost) char *rhost;{ struct hostent *hp; struct servent *sp; struct sockaddr_in sin; int s, timo = 1, lport = IPPORT_RESERVED - 1; int err; /* * Get the host address and port number to connect to. */ if (rhost == NULL) fatal("no remote host to connect to"); hp = gethostbyname(rhost); if (hp == NULL) fatal("unknown host %s", rhost); sp = getservbyname("printer", "tcp"); if (sp == NULL) fatal("printer/tcp: unknown service"); bzero((char *)&sin, sizeof(sin)); bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length); sin.sin_family = hp->h_addrtype; sin.sin_port = sp->s_port; /* * Try connecting to the server. */retry: s = rresvport_lp(&lport); if (s < 0) return(-1); if (connect(s, (caddr_t)&sin, sizeof(sin), 0) < 0) { err = errno; (void) close(s); errno = err; if (errno == EADDRINUSE) { lport--; goto retry; } if (errno == ECONNREFUSED && timo <= 16) { sleep(timo); timo *= 2; goto retry; } return(-1); } return(s);}rresvport_lp(alport) int *alport;{ struct sockaddr_in sin; int s; sin.sin_family = AF_INET; sin.sin_addr.s_addr = 0; s = socket(AF_INET, SOCK_STREAM, 0); if (s < 0) return(-1); for (; *alport > IPPORT_RESERVED/2; (*alport)--) { sin.sin_port = htons((u_short) *alport); if (bind(s, (caddr_t)&sin, sizeof(sin), 0) >= 0) return(s); if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) break; } (void) close(s); return(-1);}/* * Getline reads a line from the control file cfp, removes tabs, converts * new-line to null and leaves it in line. * Returns 0 at EOF or the number of characters read. */getline(cfp) FILE *cfp;{ register int linel = 0; register char *lp = line; register c; while ((c = getc(cfp)) != '\n') { if (c == EOF) return(0); if (c == '\t') { do { *lp++ = ' '; linel++; } while ((linel & 07) != 0); continue; } *lp++ = c; linel++; } *lp++ = '\0'; return(linel);}/* * Scan the current directory and make a list of daemon files sorted by * creation time. * Return the number of entries and a pointer to the list. */getq(namelist) struct queue *(*namelist[]);{ register struct direct *d; register struct queue *q, **queue; register int nitems; struct stat stbuf; int arraysz, compar(); DIR *dirp; if ((dirp = opendir(SD)) == NULL) return(-1); if (fstat(dirp->dd_fd, &stbuf) < 0) goto errdone; /* * Estimate the array size by taking the size of the directory file * and dividing it by a multiple of the minimum size entry. */ arraysz = (stbuf.st_size / 24); queue = (struct queue **)malloc(arraysz * sizeof(struct queue *)); if (queue == NULL) goto errdone; nitems = 0; while ((d = readdir(dirp)) != NULL) { if (d->d_name[0] != 'c' || d->d_name[1] != 'f') continue; /* daemon control files only */ if (stat(d->d_name, &stbuf) < 0) continue; /* Doesn't exist */ q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1); if (q == NULL) goto errdone; q->q_time = stbuf.st_mtime; strcpy(q->q_name, d->d_name); /* * Check to make sure the array has space left and * realloc the maximum size. */ if (++nitems > arraysz) { queue = (struct queue **)realloc((char *)queue, (stbuf.st_size/12) * sizeof(struct queue *)); if (queue == NULL) goto errdone; } queue[nitems-1] = q; } closedir(dirp); if (nitems) qsort(queue, nitems, sizeof(struct queue *), compar); *namelist = queue; return(nitems);errdone: closedir(dirp); return(-1);}/* * Compare modification times. */staticcompar(p1, p2) register struct queue **p1, **p2;{ if ((*p1)->q_time < (*p2)->q_time) return(-1); if ((*p1)->q_time > (*p2)->q_time) return(1); return(0);}/*VARARGS1*/fatal(msg, a1, a2, a3) char *msg;{ if (from != host) printf("%s: ", host); printf("%s: ", name); if (printer) printf("%s: ", printer); printf(msg, a1, a2, a3); putchar('\n'); exit(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -