⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmds.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
	printf("%s:\n", printer);	/*	 * Turn on the group execute bit of the lock file to disable queuing and	 * turn on the owner execute bit of the lock file to disable printing.	 */	(void) sprintf(line, "%s/%s", SD, LO);	if (stat(line, &stbuf) >= 0) {		if (chmod(line, (stbuf.st_mode & 0777) | 0110) < 0)			printf("\tcannot disable queuing\n");		else			printf("\tprinter and queuing disabled\n");	} else if (errno == ENOENT) {		if ((fd = open(line, O_WRONLY|O_CREAT, 0770)) < 0)			printf("\tcannot create lock file\n");		else {			(void) close(fd);			printf("\tprinter and queuing disabled\n");		}		return;	} else		printf("\tcannot stat lock file\n");	/*	 * Write the message into the status file.	 */	(void) sprintf(line, "%s/%s", SD, ST);	fd = open(line, O_WRONLY|O_CREAT, 0664);	if (fd < 0 || flock(fd, LOCK_EX) < 0) {		printf("\tcannot create status file\n");		return;	}	(void) ftruncate(fd, 0);	if (argc <= 0) {		(void) write(fd, "\n", 1);		(void) close(fd);		return;	}	cp1 = buf;	while (--argc >= 0) {		cp2 = *argv++;		while (*cp1++ = *cp2++)			;		cp1[-1] = ' ';	}	cp1[-1] = '\n';	*cp1 = '\0';	(void) write(fd, buf, strlen(buf));	(void) close(fd);}/* * Exit lpc */voidquit(argc, argv)	int argc;	char *argv[];{	exit(0);}/* * Kill and restart the daemon. */voidrestart(argc, argv)	int argc;	char *argv[];{	register int c, status;	register char *cp1, *cp2;	char prbuf[100];	if (argc == 1) {		printf("Usage: restart {all | printer ...}\n");		return;	}	if (argc == 2 && !strcmp(argv[1], "all")) {		printer = prbuf;		while (cgetnext(&bp, printcapdb) > 0) {			cp1 = prbuf;			cp2 = bp;			while ((c = *cp2++) && c != '|' && c != ':')				*cp1++ = c;			*cp1 = '\0';			abortpr(0);			startpr(0);		}		return;	}	while (--argc) {		printer = *++argv;		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {			printf("cannot open printer description file\n");			continue;		} else if (status == -1) {			printf("unknown printer %s\n", printer);			continue;		} else if (status == -3)			fatal("potential reference loop detected in printcap file");		abortpr(0);		startpr(0);	}}/* * Enable printing on the specified printer and startup the daemon. */voidstartcmd(argc, argv)	int argc;	char *argv[];{	register int c, status;	register char *cp1, *cp2;	char prbuf[100];	if (argc == 1) {		printf("Usage: start {all | printer ...}\n");		return;	}	if (argc == 2 && !strcmp(argv[1], "all")) {		printer = prbuf;		while (cgetnext(&bp, printcapdb) > 0) {			cp1 = prbuf;			cp2 = bp;			while ((c = *cp2++) && c != '|' && c != ':')				*cp1++ = c;			*cp1 = '\0';			startpr(1);		}		return;	}	while (--argc) {		printer = *++argv;		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {			printf("cannot open printer description file\n");			continue;		} else if (status == -1) {			printf("unknown printer %s\n", printer);			continue;		} else if (status == -3)			fatal("potential reference loop detected in printcap file");		startpr(1);	}}static voidstartpr(enable)	int enable;{	struct stat stbuf;	if (cgetstr(bp, "sd", &SD) == -1)		SD = _PATH_DEFSPOOL;	if (cgetstr(bp, "lo", &LO) == -1)		LO = DEFLOCK;	(void) sprintf(line, "%s/%s", SD, LO);	printf("%s:\n", printer);	/*	 * Turn off the owner execute bit of the lock file to enable printing.	 */	if (enable && stat(line, &stbuf) >= 0) {		if (chmod(line, stbuf.st_mode & (enable==2 ? 0666 : 0677)) < 0)			printf("\tcannot enable printing\n");		else			printf("\tprinting enabled\n");	}	if (!startdaemon(printer))		printf("\tcouldn't start daemon\n");	else		printf("\tdaemon started\n");}/* * Print the status of each queue listed or all the queues. */voidstatus(argc, argv)	int argc;	char *argv[];{	register int c, status;	register char *cp1, *cp2;	char prbuf[100];	if (argc == 1) {		printer = prbuf;		while (cgetnext(&bp, printcapdb) > 0) {			cp1 = prbuf;			cp2 = bp;			while ((c = *cp2++) && c != '|' && c != ':')				*cp1++ = c;			*cp1 = '\0';			prstat();		}		return;	}	while (--argc) {		printer = *++argv;		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {			printf("cannot open printer description file\n");			continue;		} else if (status == -1) {			printf("unknown printer %s\n", printer);			continue;		} else if (status == -3)			fatal("potential reference loop detected in printcap file");		prstat();	}}/* * Print the status of the printer queue. */static voidprstat(){	struct stat stbuf;	register int fd, i;	register struct dirent *dp;	DIR *dirp;	if (cgetstr(bp, "sd", &SD) == -1)		SD = _PATH_DEFSPOOL;	if (cgetstr(bp, "lo", &LO) == -1)		LO = DEFLOCK;	if (cgetstr(bp, "st", &ST) == -1)		ST = DEFSTAT;	printf("%s:\n", printer);	(void) sprintf(line, "%s/%s", SD, LO);	if (stat(line, &stbuf) >= 0) {		printf("\tqueuing is %s\n",			(stbuf.st_mode & 010) ? "disabled" : "enabled");		printf("\tprinting is %s\n",			(stbuf.st_mode & 0100) ? "disabled" : "enabled");	} else {		printf("\tqueuing is enabled\n");		printf("\tprinting is enabled\n");	}	if ((dirp = opendir(SD)) == NULL) {		printf("\tcannot examine spool directory\n");		return;	}	i = 0;	while ((dp = readdir(dirp)) != NULL) {		if (*dp->d_name == 'c' && dp->d_name[1] == 'f')			i++;	}	closedir(dirp);	if (i == 0)		printf("\tno entries\n");	else if (i == 1)		printf("\t1 entry in spool area\n");	else		printf("\t%d entries in spool area\n", i);	fd = open(line, O_RDONLY);	if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) {		(void) close(fd);	/* unlocks as well */		printf("\tno daemon present\n");		return;	}	(void) close(fd);	putchar('\t');	(void) sprintf(line, "%s/%s", SD, ST);	fd = open(line, O_RDONLY);	if (fd >= 0) {		(void) flock(fd, LOCK_SH);		while ((i = read(fd, line, sizeof(line))) > 0)			(void) fwrite(line, 1, i, stdout);		(void) close(fd);	/* unlocks as well */	}}/* * Stop the specified daemon after completing the current job and disable * printing. */voidstop(argc, argv)	int argc;	char *argv[];{	register int c, status;	register char *cp1, *cp2;	char prbuf[100];	if (argc == 1) {		printf("Usage: stop {all | printer ...}\n");		return;	}	if (argc == 2 && !strcmp(argv[1], "all")) {		printer = prbuf;		while (cgetnext(&bp, printcapdb) > 0) {			cp1 = prbuf;			cp2 = bp;			while ((c = *cp2++) && c != '|' && c != ':')				*cp1++ = c;			*cp1 = '\0';			stoppr();		}		return;	}	while (--argc) {		printer = *++argv;		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {			printf("cannot open printer description file\n");			continue;		} else if (status == -1) {			printf("unknown printer %s\n", printer);			continue;		} else if (status == -3)			fatal("potential reference loop detected in printcap file");		stoppr();	}}static voidstoppr(){	register int fd;	struct stat stbuf;	if (cgetstr(bp, "sd", &SD) == -1)		SD = _PATH_DEFSPOOL;	if (cgetstr(bp, "lo", &LO) == -1)		LO = DEFLOCK;	(void) sprintf(line, "%s/%s", SD, LO);	printf("%s:\n", printer);	/*	 * Turn on the owner execute bit of the lock file to disable printing.	 */	if (stat(line, &stbuf) >= 0) {		if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)			printf("\tcannot disable printing\n");		else {			upstat("printing disabled\n");			printf("\tprinting disabled\n");		}	} else if (errno == ENOENT) {		if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)			printf("\tcannot create lock file\n");		else {			(void) close(fd);			upstat("printing disabled\n");			printf("\tprinting disabled\n");		}	} else		printf("\tcannot stat lock file\n");}struct	queue **queue;int	nitems;time_t	mtime;/* * Put the specified jobs at the top of printer queue. */voidtopq(argc, argv)	int argc;	char *argv[];{	register int i;	struct stat stbuf;	int status, changed;	if (argc < 3) {		printf("Usage: topq printer [jobnum ...] [user ...]\n");		return;	}	--argc;	printer = *++argv;	status = cgetent(&bp, printcapdb, printer);	if (status == -2) {		printf("cannot open printer description file\n");		return;	} else if (status == -1) {		printf("%s: unknown printer\n", printer);		return;	} else if (status == -3)		fatal("potential reference loop detected in printcap file");	if (cgetstr(bp, "sd", &SD) == -1)		SD = _PATH_DEFSPOOL;	if (cgetstr(bp, "lo", &LO) == -1)		LO = DEFLOCK;	printf("%s:\n", printer);	if (chdir(SD) < 0) {		printf("\tcannot chdir to %s\n", SD);		return;	}	nitems = getq(&queue);	if (nitems == 0)		return;	changed = 0;	mtime = queue[0]->q_time;	for (i = argc; --i; ) {		if (doarg(argv[i]) == 0) {			printf("\tjob %s is not in the queue\n", argv[i]);			continue;		} else			changed++;	}	for (i = 0; i < nitems; i++)		free(queue[i]);	free(queue);	if (!changed) {		printf("\tqueue order unchanged\n");		return;	}	/*	 * Turn on the public execute bit of the lock file to	 * get lpd to rebuild the queue after the current job.	 */	if (changed && stat(LO, &stbuf) >= 0)		(void) chmod(LO, (stbuf.st_mode & 0777) | 01);} /* * Reposition the job by changing the modification time of * the control file. */static inttouch(q)	struct queue *q;{	struct timeval tvp[2];	tvp[0].tv_sec = tvp[1].tv_sec = --mtime;	tvp[0].tv_usec = tvp[1].tv_usec = 0;	return(utimes(q->q_name, tvp));}/* * Checks if specified job name is in the printer's queue. * Returns:  negative (-1) if argument name is not in the queue. */static intdoarg(job)	char *job;{	register struct queue **qq;	register int jobnum, n;	register char *cp, *machine;	int cnt = 0;	FILE *fp;	/*	 * Look for a job item consisting of system name, colon, number 	 * (example: ucbarpa:114)  	 */	if ((cp = index(job, ':')) != NULL) {		machine = job;		*cp++ = '\0';		job = cp;	} else		machine = NULL;	/*	 * Check for job specified by number (example: 112 or 235ucbarpa).	 */	if (isdigit(*job)) {		jobnum = 0;		do			jobnum = jobnum * 10 + (*job++ - '0');		while (isdigit(*job));		for (qq = queue + nitems; --qq >= queue; ) {			n = 0;			for (cp = (*qq)->q_name+3; isdigit(*cp); )				n = n * 10 + (*cp++ - '0');			if (jobnum != n)				continue;			if (*job && strcmp(job, cp) != 0)				continue;			if (machine != NULL && strcmp(machine, cp) != 0)				continue;			if (touch(*qq) == 0) {				printf("\tmoved %s\n", (*qq)->q_name);				cnt++;			}		}		return(cnt);	}	/*	 * Process item consisting of owner's name (example: henry).	 */	for (qq = queue + nitems; --qq >= queue; ) {		if ((fp = fopen((*qq)->q_name, "r")) == NULL)			continue;		while (getline(fp) > 0)			if (line[0] == 'P')				break;		(void) fclose(fp);		if (line[0] != 'P' || strcmp(job, line+1) != 0)			continue;		if (touch(*qq) == 0) {			printf("\tmoved %s\n", (*qq)->q_name);			cnt++;		}	}	return(cnt);}/* * Enable everything and start printer (undo `down'). */voidup(argc, argv)	int argc;	char *argv[];{	register int c, status;	register char *cp1, *cp2;	char prbuf[100];	if (argc == 1) {		printf("Usage: up {all | printer ...}\n");		return;	}	if (argc == 2 && !strcmp(argv[1], "all")) {		printer = prbuf;		while (cgetnext(&bp, printcapdb) > 0) {			cp1 = prbuf;			cp2 = bp;			while ((c = *cp2++) && c != '|' && c != ':')				*cp1++ = c;			*cp1 = '\0';			startpr(2);		}		return;	}	while (--argc) {		printer = *++argv;		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {			printf("cannot open printer description file\n");			continue;		} else if (status == -1) {			printf("unknown printer %s\n", printer);			continue;		} else if (status == -3)			fatal("potential reference loop detected in printcap file");		startpr(2);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -