seteuid(uid);
main(argc, argv)	int argc;	char *argv[];{	struct passwd *pw;	struct group *gptr;	extern char *itoa();	register char *arg, *cp;	char buf[BUFSIZ];	int i, f;	struct stat stb;	euid = geteuid();	uid = getuid();	seteuid(uid);	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)		signal(SIGHUP, cleanup);	if (signal(SIGINT, SIG_IGN) != SIG_IGN)		signal(SIGINT, cleanup);	if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)		signal(SIGQUIT, cleanup);	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)		signal(SIGTERM, cleanup);	name = argv[0];	gethostname(host, sizeof (host));	openlog("lpd", 0, LOG_LPR);	while (argc > 1 && argv[1][0] == '-') {		argc--;		arg = *++argv;		switch (arg[1]) {		case 'P':		/* specifiy printer name */			if (arg[2])				printer = &arg[2];			else if (argc > 1) {				argc--;				printer = *++argv;			}			break;		case 'C':		/* classification spec */			hdr++;			if (arg[2])				class = &arg[2];			else if (argc > 1) {				argc--;				class = *++argv;			}			break;		case 'U':		/* user name */			hdr++;			if (arg[2])				person = &arg[2];			else if (argc > 1) {				argc--;				person = *++argv;			}			break;		case 'J':		/* job name */			hdr++;			if (arg[2])				jobname = &arg[2];			else if (argc > 1) {				argc--;				jobname = *++argv;			}			break;		case 'T':		/* pr's title line */			if (arg[2])				title = &arg[2];			else if (argc > 1) {				argc--;				title = *++argv;			}			break;		case 'l':		/* literal output */		case 'p':		/* print using ``pr'' */		case 't':		/* print troff output (cat files) */		case 'n':		/* print ditroff output */		case 'd':		/* print tex output (dvi files) */		case 'g':		/* print graph(1G) output */		case 'c':		/* print cifplot output */		case 'v':		/* print vplot output */			format = arg[1];			break;		case 'f':		/* print fortran output */			format = 'r';			break;		case '4':		/* troff fonts */		case '3':		case '2':		case '1':			if (argc > 1) {				argc--;				fonts[arg[1] - '1'] = *++argv;			}			break;		case 'w':		/* versatec page width */			width = arg+2;			break;		case 'r':		/* remove file when done */			rflag++;			break;		case 'm':		/* send mail when done */			mailflg++;			break;		case 'h':		/* toggle want of header page */			hdr = !hdr;			break;		case 's':		/* try to link files */			sflag++;			break;		case 'q':		/* just q job */			qflag++;			break;		case 'i':		/* indent output */			iflag++;			indent = arg[2] ? atoi(&arg[2]) : 8;			break;		case '#':		/* n copies */			if (isdigit(arg[2])) {				i = atoi(&arg[2]);				if (i > 0)					ncopies = i;			}		}	}	if (printer == NULL) {		char *p;		printer = DEFLP;		if ((p = getenv("PRINTER")) != NULL)			printer = p;	}	chkprinter(printer);	if (SC && ncopies > 1)		fatal2("multiple copies are not allowed");	if (MC > 0 && ncopies > MC)		fatal2("only %d copies are allowed", MC);	/*	 * Get the identity of the person doing the lpr using the same	 * algorithm as lprm. 	 */	userid = getuid();	if (userid != DU || person == 0) {		if ((pw = getpwuid(userid)) == NULL)			fatal2("Who are you?");		person = pw->pw_name;	}	/*	 * Check for restricted group access.	 */	if (RG != NULL && userid != DU) {		if ((gptr = getgrnam(RG)) == NULL)			fatal2("Restricted group specified incorrectly");		if (gptr->gr_gid != getgid()) {			while (*gptr->gr_mem != NULL) {				if ((strcmp(person, *gptr->gr_mem)) == 0)					break;				gptr->gr_mem++;			}			if (*gptr->gr_mem == NULL)				fatal2("Not a member of the restricted group");		}	}	/*	 * Check to make sure queuing is enabled if userid is not root.	 */	(void) snprintf(buf, sizeof(buf), "%s/%s", SD, LO);	if (userid && stat(buf, &stb) == 0 && (stb.st_mode & 010))		fatal2("Printer queue is disabled");	/*	 * Initialize the control file.	 */	mktemps();	tfd = nfile(tfname);	seteuid(euid);	(void) fchown(tfd, DU, -1);	/* owned by daemon for protection */	seteuid(uid);	card('H', host);	card('P', person);	if (hdr) {		if (jobname == NULL) {			if (argc == 1)				jobname = "stdin";			else				jobname = (arg = rindex(argv[1], '/')) ? arg+1 : argv[1];		}		card('J', jobname);		card('C', class);		card('L', person);	}	if (iflag)		card('I', itoa(indent));	if (mailflg)		card('M', person);	if (format == 't' || format == 'n' || format == 'd')		for (i = 0; i < 4; i++)			if (fonts[i] != NULL)				card('1'+i, fonts[i]);	if (width != NULL)		card('W', width);	/*	 * Read the files and spool them.	 */	if (argc == 1)		copy(0, " ");	else while (--argc) {		if ((f = test(arg = *++argv)) < 0)			continue;	/* file unreasonable */		if (sflag && (cp = linked(arg)) != NULL) {			(void) snprintf(buf, sizeof(buf), "%d %d", statb.st_dev,					statb.st_ino);			card('S', buf);			if (format == 'p')				card('T', title ? title : arg);			for (i = 0; i < ncopies; i++)				card(format, &dfname[inchar-2]);			card('U', &dfname[inchar-2]);			if (f)				card('U', cp);			card('N', arg);			dfname[inchar]++;			nact++;			continue;		}		if (sflag)			printf("%s: %s: not linked, copying instead\n", name, arg);		if ((i = open(arg, O_RDONLY)) < 0) {			seteuid(uid);			printf("%s: cannot open %s\n", name, arg);			continue;		}		copy(i, arg);		(void) close(i);		if (f && unlink(arg) < 0)			printf("%s: %s: not removed\n", name, arg);	}	if (nact) {		(void) close(tfd);		tfname[inchar]--;		/*		 * Touch the control file to fix position in the queue.		 */		seteuid(euid);		if ((tfd = open(tfname, O_RDWR)) >= 0) {			char c;			if (read(tfd, &c, 1) == 1 &&			    lseek(tfd, (off_t)0, 0) == 0 &&			    write(tfd, &c, 1) != 1) {				printf("%s: cannot touch %s\n", name, tfname);				tfname[inchar]++;				cleanup(0);			}			(void) close(tfd);		}		if (link(tfname, cfname) < 0) {			printf("%s: cannot rename %s\n", name, cfname);			tfname[inchar]++;			cleanup(0);		}		unlink(tfname);		seteuid(uid);		if (qflag)		/* just q things up */			exit(0);		if (!startdaemon(printer))			printf("jobs queued, but cannot start daemon.\n");		exit(0);	}	cleanup(0);	/* NOTREACHED */}