xref: /csrg-svn/old/vpr/vprm.c (revision 2415)
1*2415Shalbert static char vprmSCCSid[] = "@(#)vprm.c	1.2\t02/12/81";
2*2415Shalbert 
31854Sroot #include <sys/types.h>
41854Sroot #include <dir.h>
51854Sroot #include <stat.h>
61854Sroot #include <stdio.h>
71854Sroot 
8*2415Shalbert char *basename();
9*2415Shalbert 
101854Sroot extern	char _sobuf[];
111854Sroot 
121854Sroot main(argc, argv)
131854Sroot 	int argc;
141854Sroot 	char *argv[];
151854Sroot {
161854Sroot 	setbuf(stdout, _sobuf);
171854Sroot 	argc--, argv++;
181854Sroot 	if (argc == 0) {
191854Sroot 		fprintf(stderr, "usage: vprm [ id ... ] [ filename ... ] [ user ... ]\n");
201854Sroot 		exit(1);
211854Sroot 	}
221854Sroot 
23*2415Shalbert 	/* Look for something to delete both in Varian and Versatec spool dirs. */
24*2415Shalbert 	delete("Varian", "/usr/spool/vad", argc, argv);
25*2415Shalbert 	delete("Versatec", "/usr/spool/vpd", argc, argv);
26*2415Shalbert }
27*2415Shalbert 
28*2415Shalbert /*
29*2415Shalbert  * Look through the argv list for things to delete.
30*2415Shalbert  */
311854Sroot 
32*2415Shalbert delete(devname, spooldir, argc, argv)
33*2415Shalbert char *devname, *spooldir, *argv[];
34*2415Shalbert int argc;
35*2415Shalbert {
36*2415Shalbert 	FILE *dir;		/* The spool dir. */
37*2415Shalbert 	struct dir dirent;	/* An entry read from the spool dir.*/
38*2415Shalbert 	int deletion = 0;	/* Flag noting something has been deleted. */
391854Sroot 
40*2415Shalbert 	/* Change to the spool directory. */
41*2415Shalbert 	if (chdir(spooldir) < 0) {
42*2415Shalbert 		perror(spooldir);
43*2415Shalbert 		return(1);
441854Sroot 	}
451854Sroot 
46*2415Shalbert 	/* Open it. */
47*2415Shalbert 	if ((dir = fopen(".", "r")) == NULL) {
48*2415Shalbert 		perror(spooldir);
49*2415Shalbert 		return(1);
501854Sroot 	}
511854Sroot 
52*2415Shalbert 	printf("%s -", devname);
531854Sroot 
54*2415Shalbert 	/*
55*2415Shalbert 	 * Loop through the args and the spool dir, looking for a spool
56*2415Shalbert 	 * command file (has a prefix of 'df'),
57*2415Shalbert 	 * and trying to match it with the argument.
58*2415Shalbert 	 */
59*2415Shalbert 	while (argc-- > 0) {
60*2415Shalbert 		rewind(dir);
61*2415Shalbert 		while (fread(&dirent, sizeof dirent, 1, dir) == 1) {
62*2415Shalbert 			if (dirent.d_ino == 0)
63*2415Shalbert 				continue;
64*2415Shalbert 			if (dirent.d_name[0] == 'd' &&
65*2415Shalbert 			    dirent.d_name[1] == 'f') {
66*2415Shalbert 				if (delcheck(dirent.d_name, *argv)) {
67*2415Shalbert 					printf(" removing %s", dirent.d_name+3);
68*2415Shalbert 					deletion = 1;
69*2415Shalbert 				}
70*2415Shalbert 			}
711854Sroot 		}
72*2415Shalbert 		argv++;
731854Sroot 	}
74*2415Shalbert 
75*2415Shalbert 	if (!deletion)
76*2415Shalbert 		printf(" nothing to remove\n");
77*2415Shalbert 	else
78*2415Shalbert 		putchar('\n');
791854Sroot }
801854Sroot 
81*2415Shalbert 
82*2415Shalbert /*
83*2415Shalbert  * delcheck tries to match the given arg against the given command file in
84*2415Shalbert  * various ways.  For instance, if dfname = 'dfa12345', then there is a match if
85*2415Shalbert  * arg == 'dfa12345', or
86*2415Shalbert  * arg == '12345', or
87*2415Shalbert  * arg == the name given on the L line of the file (the owner), or
88*2415Shalbert  * arg == the basename of a file given on a command line in the file.
89*2415Shalbert  * If there is a match, all the U (unlink) commands in the command file
90*2415Shalbert  * are executed, and then the command file itself is unlinked.
91*2415Shalbert  */
92*2415Shalbert 
93*2415Shalbert int
94*2415Shalbert delcheck(dfname, arg)
95*2415Shalbert char *dfname, *arg;
961854Sroot {
97*2415Shalbert 	FILE *df = NULL;	/* The command file. */
98*2415Shalbert 	int delfile = 0;	/* A match was found, so do a deletion. */
99*2415Shalbert 	char line[256];		/* Command line in command file. */
1001854Sroot 
101*2415Shalbert 	if (strcmp(arg, dfname) == 0)
102*2415Shalbert 		delfile = 1;	/* arg == 'dfa12345'. */
103*2415Shalbert 	else if (strcmp(arg, dfname+3) == 0)
104*2415Shalbert 		delfile = 1;	/* arg == '12345' (skip 'dfa'). */
105*2415Shalbert 	else {			/* No match; look inside on command lines. */
106*2415Shalbert 		if ((df = fopen(dfname, "r")) == NULL)
107*2415Shalbert 			return(0);
108*2415Shalbert 		while (!delfile && getline(df, line)) {
109*2415Shalbert 			switch (line[0]) {
110*2415Shalbert 				case 'L':
111*2415Shalbert 					/* Check owner name. */
112*2415Shalbert 					if (strcmp(arg, line+1) == 0)
113*2415Shalbert 						delfile = 1;
114*2415Shalbert 					break;
1151854Sroot 
116*2415Shalbert 				case 'C':
117*2415Shalbert 				case 'V':
118*2415Shalbert 				case 'F':
119*2415Shalbert 				case 'G':
120*2415Shalbert 				case 'P':
121*2415Shalbert 				case 'T':
122*2415Shalbert 					/* Check command line arg. */
123*2415Shalbert 					if (strcmp (basename(arg), basename(line)) == 0)
124*2415Shalbert 						delfile = 1;
125*2415Shalbert 					break;
126*2415Shalbert 			}
1271854Sroot 		}
1281854Sroot 	}
1291854Sroot 
130*2415Shalbert 	if (delfile) {
131*2415Shalbert 		if (df == NULL)		/* File not open yet. */
132*2415Shalbert 			df = fopen(dfname, "r");
133*2415Shalbert 		if (df == NULL)
134*2415Shalbert 			return(0);
135*2415Shalbert 
136*2415Shalbert 		/* Run through the command file, executing Unlink commands. */
137*2415Shalbert 		rewind(df);
138*2415Shalbert 		while (getline(df, line)) {
139*2415Shalbert 			if (line[0] == 'U')
140*2415Shalbert 				unlink(line+1);
141*2415Shalbert 		}
1421854Sroot 
143*2415Shalbert 		unlink(dfname);		/* Now unlink the command file itself. */
144*2415Shalbert 	}
145*2415Shalbert 
146*2415Shalbert 	if (df != NULL)
147*2415Shalbert 		fclose(df);
148*2415Shalbert 	return(delfile);
1491854Sroot }
1501854Sroot 
151*2415Shalbert 
152*2415Shalbert 
153*2415Shalbert 
154*2415Shalbert getline(file, line)
155*2415Shalbert FILE *file;
156*2415Shalbert char *line;
1571854Sroot {
1581854Sroot 	register int i, c;
1591854Sroot 
1601854Sroot 	i = 0;
161*2415Shalbert 	while ((c = getc(file)) != '\n') {
1621854Sroot 		if (c <= 0)
1631854Sroot 			return(0);
164*2415Shalbert 		if (i < 256)
1651854Sroot 			line[i++] = c;
1661854Sroot 	}
1671854Sroot 	line[i++] = 0;
1681854Sroot 	return (1);
1691854Sroot }
170*2415Shalbert 
171*2415Shalbert 
172*2415Shalbert /*
173*2415Shalbert  * basename gets the final component of the given pathname. E.g. "c" in
174*2415Shalbert  * /a/b/c.
175*2415Shalbert  */
176*2415Shalbert 
177*2415Shalbert char *
178*2415Shalbert basename(pathname)
179*2415Shalbert char *pathname;
180*2415Shalbert {
181*2415Shalbert 	register char *lastname;
182*2415Shalbert 
183*2415Shalbert 	lastname = pathname + strlen(pathname)-1; /* Move to last char in name. */
184*2415Shalbert 	while (lastname >= pathname) {
185*2415Shalbert 		if (*lastname == '/')
186*2415Shalbert 			return(lastname + 1);
187*2415Shalbert 		lastname--;
188*2415Shalbert 	}
189*2415Shalbert 	return(pathname);		/* No /'s at all. */
190*2415Shalbert }
191