xref: /original-bsd/old/vpr/vprm.c (revision 08eb28af)
1 static char vprmSCCSid[] = "@(#)vprm.c	1.4\t11/04/87";
2 
3 #include <sys/param.h>
4 #include <sys/dir.h>
5 #include <sys/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 	DIR *dir;		/* The spool dir. */
37 	struct direct *dirp;	/* 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 = opendir(".")) == 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 		rewinddir(dir);
61 		while ((dirp = readdir(dir)) != NULL) {
62 			if (dirp->d_name[0] == 'd' &&
63 			    dirp->d_name[1] == 'f' &&
64 			    delcheck(dirp->d_name, *argv)) {
65 				printf(" removing %s", &(dirp->d_name[3]));
66 				deletion = 1;
67 			}
68 		}
69 		argv++;
70 	}
71 	closedir(dir);
72 	if (!deletion)
73 		printf(" nothing to remove\n");
74 	else
75 		putchar('\n');
76 }
77 
78 
79 /*
80  * delcheck tries to match the given arg against the given command file in
81  * various ways.  For instance, if dfname = 'dfa12345', then there is a match if
82  * arg == 'dfa12345', or
83  * arg == '12345', or
84  * arg == the name given on the L line of the file (the owner), or
85  * arg == the basename of a file given on a command line in the file.
86  * If there is a match, all the U (unlink) commands in the command file
87  * are executed, and then the command file itself is unlinked.
88  */
89 
90 int
91 delcheck(dfname, arg)
92 	char *dfname, *arg;
93 {
94 	FILE *df = NULL;	/* The command file. */
95 	int delfile = 0;	/* A match was found, so do a deletion. */
96 	char line[256];		/* Command line in command file. */
97 
98 	if (strcmp(arg, dfname) == 0)
99 		delfile = 1;	/* arg == 'dfa12345'. */
100 	else if (strcmp(arg, dfname+3) == 0)
101 		delfile = 1;	/* arg == '12345' (skip 'dfa'). */
102 	else {			/* No match; look inside on command lines. */
103 		if ((df = fopen(dfname, "r")) == NULL)
104 			return(0);
105 		while (!delfile && getline(df, line)) {
106 			switch (line[0]) {
107 				case 'L':
108 					/* Check owner name. */
109 					if (strcmp(arg, line+1) == 0)
110 						delfile = 1;
111 					break;
112 
113 				case 'C':
114 				case 'V':
115 				case 'F':
116 				case 'G':
117 				case 'P':
118 				case 'T':
119 					/* Check command line arg. */
120 					if (strcmp (basename(arg), basename(line)) == 0)
121 						delfile = 1;
122 					break;
123 			}
124 		}
125 	}
126 
127 	if (delfile) {
128 		if (df == NULL)		/* File not open yet. */
129 			df = fopen(dfname, "r");
130 		if (df == NULL)
131 			return(0);
132 
133 		/* Run through the command file, executing Unlink commands. */
134 		rewind(df);
135 		while (getline(df, line)) {
136 			if (line[0] == 'U')
137 				unlink(line+1);
138 		}
139 
140 		unlink(dfname);		/* Now unlink the command file itself. */
141 	}
142 
143 	if (df != NULL)
144 		fclose(df);
145 	return(delfile);
146 }
147 
148 
149 
150 
151 getline(file, line)
152 	FILE *file;
153 	char *line;
154 {
155 	register int i, c;
156 
157 	i = 0;
158 	while ((c = getc(file)) != '\n') {
159 		if (c <= 0)
160 			return(0);
161 		if (i < 256)
162 			line[i++] = c;
163 	}
164 	line[i++] = 0;
165 	return (1);
166 }
167 
168 
169 /*
170  * basename gets the final component of the given pathname. E.g. "c" in
171  * /a/b/c.
172  */
173 
174 char *
175 basename(pathname)
176 	char *pathname;
177 {
178 	register char *lastname;
179 
180 	lastname = pathname + strlen(pathname)-1; /* Move to last char in name. */
181 	while (lastname >= pathname) {
182 		if (*lastname == '/')
183 			return(lastname + 1);
184 		lastname--;
185 	}
186 	return(pathname);		/* No /'s at all. */
187 }
188