xref: /original-bsd/old/vpr/vprm.c (revision 6c57d260)
1 static char vprmSCCSid[] = "@(#)vprm.c	1.2\t02/12/81";
2 
3 #include <sys/types.h>
4 #include <dir.h>
5 #include <stat.h>
6 #include <stdio.h>
7 
8 char *basename();
9 
10 extern	char _sobuf[];
11 
12 main(argc, argv)
13 	int argc;
14 	char *argv[];
15 {
16 	setbuf(stdout, _sobuf);
17 	argc--, argv++;
18 	if (argc == 0) {
19 		fprintf(stderr, "usage: vprm [ id ... ] [ filename ... ] [ user ... ]\n");
20 		exit(1);
21 	}
22 
23 	/* Look for something to delete both in Varian and Versatec spool dirs. */
24 	delete("Varian", "/usr/spool/vad", argc, argv);
25 	delete("Versatec", "/usr/spool/vpd", argc, argv);
26 }
27 
28 /*
29  * Look through the argv list for things to delete.
30  */
31 
32 delete(devname, spooldir, argc, argv)
33 char *devname, *spooldir, *argv[];
34 int argc;
35 {
36 	FILE *dir;		/* The spool dir. */
37 	struct dir dirent;	/* An entry read from the spool dir.*/
38 	int deletion = 0;	/* Flag noting something has been deleted. */
39 
40 	/* Change to the spool directory. */
41 	if (chdir(spooldir) < 0) {
42 		perror(spooldir);
43 		return(1);
44 	}
45 
46 	/* Open it. */
47 	if ((dir = fopen(".", "r")) == NULL) {
48 		perror(spooldir);
49 		return(1);
50 	}
51 
52 	printf("%s -", devname);
53 
54 	/*
55 	 * Loop through the args and the spool dir, looking for a spool
56 	 * command file (has a prefix of 'df'),
57 	 * and trying to match it with the argument.
58 	 */
59 	while (argc-- > 0) {
60 		rewind(dir);
61 		while (fread(&dirent, sizeof dirent, 1, dir) == 1) {
62 			if (dirent.d_ino == 0)
63 				continue;
64 			if (dirent.d_name[0] == 'd' &&
65 			    dirent.d_name[1] == 'f') {
66 				if (delcheck(dirent.d_name, *argv)) {
67 					printf(" removing %s", dirent.d_name+3);
68 					deletion = 1;
69 				}
70 			}
71 		}
72 		argv++;
73 	}
74 
75 	if (!deletion)
76 		printf(" nothing to remove\n");
77 	else
78 		putchar('\n');
79 }
80 
81 
82 /*
83  * delcheck tries to match the given arg against the given command file in
84  * various ways.  For instance, if dfname = 'dfa12345', then there is a match if
85  * arg == 'dfa12345', or
86  * arg == '12345', or
87  * arg == the name given on the L line of the file (the owner), or
88  * arg == the basename of a file given on a command line in the file.
89  * If there is a match, all the U (unlink) commands in the command file
90  * are executed, and then the command file itself is unlinked.
91  */
92 
93 int
94 delcheck(dfname, arg)
95 char *dfname, *arg;
96 {
97 	FILE *df = NULL;	/* The command file. */
98 	int delfile = 0;	/* A match was found, so do a deletion. */
99 	char line[256];		/* Command line in command file. */
100 
101 	if (strcmp(arg, dfname) == 0)
102 		delfile = 1;	/* arg == 'dfa12345'. */
103 	else if (strcmp(arg, dfname+3) == 0)
104 		delfile = 1;	/* arg == '12345' (skip 'dfa'). */
105 	else {			/* No match; look inside on command lines. */
106 		if ((df = fopen(dfname, "r")) == NULL)
107 			return(0);
108 		while (!delfile && getline(df, line)) {
109 			switch (line[0]) {
110 				case 'L':
111 					/* Check owner name. */
112 					if (strcmp(arg, line+1) == 0)
113 						delfile = 1;
114 					break;
115 
116 				case 'C':
117 				case 'V':
118 				case 'F':
119 				case 'G':
120 				case 'P':
121 				case 'T':
122 					/* Check command line arg. */
123 					if (strcmp (basename(arg), basename(line)) == 0)
124 						delfile = 1;
125 					break;
126 			}
127 		}
128 	}
129 
130 	if (delfile) {
131 		if (df == NULL)		/* File not open yet. */
132 			df = fopen(dfname, "r");
133 		if (df == NULL)
134 			return(0);
135 
136 		/* Run through the command file, executing Unlink commands. */
137 		rewind(df);
138 		while (getline(df, line)) {
139 			if (line[0] == 'U')
140 				unlink(line+1);
141 		}
142 
143 		unlink(dfname);		/* Now unlink the command file itself. */
144 	}
145 
146 	if (df != NULL)
147 		fclose(df);
148 	return(delfile);
149 }
150 
151 
152 
153 
154 getline(file, line)
155 FILE *file;
156 char *line;
157 {
158 	register int i, c;
159 
160 	i = 0;
161 	while ((c = getc(file)) != '\n') {
162 		if (c <= 0)
163 			return(0);
164 		if (i < 256)
165 			line[i++] = c;
166 	}
167 	line[i++] = 0;
168 	return (1);
169 }
170 
171 
172 /*
173  * basename gets the final component of the given pathname. E.g. "c" in
174  * /a/b/c.
175  */
176 
177 char *
178 basename(pathname)
179 char *pathname;
180 {
181 	register char *lastname;
182 
183 	lastname = pathname + strlen(pathname)-1; /* Move to last char in name. */
184 	while (lastname >= pathname) {
185 		if (*lastname == '/')
186 			return(lastname + 1);
187 		lastname--;
188 	}
189 	return(pathname);		/* No /'s at all. */
190 }
191