📄 lpr.c
字号:
} (void) close(tfd); } if (link(tfname, cfname) < 0) { printf("%s: cannot rename %s\n", name, cfname); tfname[inchar]++; cleanup(0); } unlink(tfname); if (qflag) /* just q things up */ exit(0); if (!startdaemon(printer)) printf("jobs queued, but cannot start daemon.\n"); exit(0); } cleanup(0); /* NOTREACHED */}/* * Create the file n and copy from file descriptor f. */static voidcopy(f, n) int f; char n[];{ register int fd, i, nr, nc; char buf[BUFSIZ]; if (format == 'p') card('T', title ? title : n); for (i = 0; i < ncopies; i++) card(format, &dfname[inchar-2]); card('U', &dfname[inchar-2]); card('N', n); fd = nfile(dfname); nr = nc = 0; while ((i = read(f, buf, BUFSIZ)) > 0) { if (write(fd, buf, i) != i) { printf("%s: %s: temp file write error\n", name, n); break; } nc += i; if (nc >= BUFSIZ) { nc -= BUFSIZ; nr++; if (MX > 0 && nr > MX) { printf("%s: %s: copy file is too large\n", name, n); break; } } } (void) close(fd); if (nc==0 && nr==0) printf("%s: %s: empty input file\n", name, f ? n : "stdin"); else nact++;}/* * Try and link the file to dfname. Return a pointer to the full * path name if successful. */static char *linked(file) register char *file;{ register char *cp; static char buf[BUFSIZ]; if (*file != '/') { if (getwd(buf) == NULL) return(NULL); while (file[0] == '.') { switch (file[1]) { case '/': file += 2; continue; case '.': if (file[2] == '/') { if ((cp = rindex(buf, '/')) != NULL) *cp = '\0'; file += 3; continue; } } break; } strcat(buf, "/"); strcat(buf, file); file = buf; } return(symlink(file, dfname) ? NULL : file);}/* * Put a line into the control file. */static voidcard(c, p2) register int c; register char *p2;{ char buf[BUFSIZ]; register char *p1 = buf; register int len = 2; *p1++ = c; while ((c = *p2++) != '\0') { *p1++ = (c == '\n') ? ' ' : c; len++; } *p1++ = '\n'; write(tfd, buf, len);}/* * Create a new file in the spool directory. */static intnfile(n) char *n;{ register int f; int oldumask = umask(0); /* should block signals */ f = open(n, O_WRONLY | O_EXCL | O_CREAT, FILMOD); (void) umask(oldumask); if (f < 0) { printf("%s: cannot create %s\n", name, n); cleanup(0); } if (fchown(f, userid, -1) < 0) { printf("%s: cannot chown %s\n", name, n); cleanup(0); } if (++n[inchar] > 'z') { if (++n[inchar-2] == 't') { printf("too many files - break up the job\n"); cleanup(0); } n[inchar] = 'A'; } else if (n[inchar] == '[') n[inchar] = 'a'; return(f);}/* * Cleanup after interrupts and errors. */static voidcleanup(signo) int signo;{ register i; signal(SIGHUP, SIG_IGN); signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); signal(SIGTERM, SIG_IGN); i = inchar; if (tfname) do unlink(tfname); while (tfname[i]-- != 'A'); if (cfname) do unlink(cfname); while (cfname[i]-- != 'A'); if (dfname) do { do unlink(dfname); while (dfname[i]-- != 'A'); dfname[i] = 'z'; } while (dfname[i-2]-- != 'd'); exit(1);}/* * Test to see if this is a printable file. * Return -1 if it is not, 0 if its printable, and 1 if * we should remove it after printing. */static inttest(file) char *file;{ struct exec execb; register int fd; register char *cp; if (access(file, 4) < 0) { printf("%s: cannot access %s\n", name, file); return(-1); } if (stat(file, &statb) < 0) { printf("%s: cannot stat %s\n", name, file); return(-1); } if ((statb.st_mode & S_IFMT) == S_IFDIR) { printf("%s: %s is a directory\n", name, file); return(-1); } if (statb.st_size == 0) { printf("%s: %s is an empty file\n", name, file); return(-1); } if ((fd = open(file, O_RDONLY)) < 0) { printf("%s: cannot open %s\n", name, file); return(-1); } if (read(fd, &execb, sizeof(execb)) == sizeof(execb) && !N_BADMAG(execb)) { printf("%s: %s is an executable program", name, file); goto error1; } (void) close(fd); if (rflag) { if ((cp = rindex(file, '/')) == NULL) { if (access(".", 2) == 0) return(1); } else { if (cp == file) { fd = access("/", 2); } else { *cp = '\0'; fd = access(file, 2); *cp = '/'; } if (fd == 0) return(1); } printf("%s: %s: is not removable by you\n", name, file); } return(0);error1: printf(" and is unprintable\n"); (void) close(fd); return(-1);}/* * itoa - integer to string conversion */static char *itoa(i) register int i;{ static char b[10] = "########"; register char *p; p = &b[8]; do *p-- = i%10 + '0'; while (i /= 10); return(++p);}/* * Perform lookup for printer name or abbreviation -- */static voidchkprinter(s) char *s;{ int status; if ((status = cgetent(&bp, printcapdb, s)) == -2) fatal2("cannot open printer description file"); else if (status == -1) fatal2("%s: unknown printer", s); if (cgetstr(bp, "sd", &SD) == -1) SD = _PATH_DEFSPOOL; if (cgetstr(bp, "lo", &LO) == -1) LO = DEFLOCK; cgetstr(bp, "rg", &RG); if (cgetnum(bp, "mx", &MX) < 0) MX = DEFMX; if (cgetnum(bp,"mc", &MC) < 0) MC = DEFMAXCOPIES; if (cgetnum(bp, "du", &DU) < 0) DU = DEFUID; SC = (cgetcap(bp, "sc", ':') != NULL);}/* * Make the temp files. */static voidmktemps(){ register int len, fd, n; register char *cp; char buf[BUFSIZ]; char *lmktemp(); (void) sprintf(buf, "%s/.seq", SD); if ((fd = open(buf, O_RDWR|O_CREAT, 0661)) < 0) { printf("%s: cannot create %s\n", name, buf); exit(1); } if (flock(fd, LOCK_EX)) { printf("%s: cannot lock %s\n", name, buf); exit(1); } n = 0; if ((len = read(fd, buf, sizeof(buf))) > 0) { for (cp = buf; len--; ) { if (*cp < '0' || *cp > '9') break; n = n * 10 + (*cp++ - '0'); } } len = strlen(SD) + strlen(host) + 8; tfname = lmktemp("tf", n, len); cfname = lmktemp("cf", n, len); dfname = lmktemp("df", n, len); inchar = strlen(SD) + 3; n = (n + 1) % 1000; (void) lseek(fd, (off_t)0, 0); sprintf(buf, "%03d\n", n); (void) write(fd, buf, strlen(buf)); (void) close(fd); /* unlocks as well */}/* * Make a temp file name. */static char *lmktemp(id, num, len) char *id; int num, len;{ register char *s; if ((s = malloc(len)) == NULL) fatal2("out of memory"); (void) sprintf(s, "%s/%sA%03d%s", SD, id, num, host); return(s);}#if __STDC__#include <stdarg.h>#else#include <varargs.h>#endifstatic void#if __STDC__fatal2(const char *msg, ...)#elsefatal2(msg, va_alist) char *msg; va_dcl#endif{ va_list ap;#if __STDC__ va_start(ap, msg);#else va_start(ap);#endif printf("%s: ", name); vprintf(msg, ap); putchar('\n'); va_end(ap); exit(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -