Commit | Line | Data |
---|---|---|
bce5be06 | 1 | static char vprmSCCSid[] = "@(#)vprm.c 1.3\t%G%"; |
8aa49a34 | 2 | |
bce5be06 | 3 | #include <sys/param.h> |
58e0be1c BJ |
4 | #include <dir.h> |
5 | #include <stat.h> | |
6 | #include <stdio.h> | |
7 | ||
8aa49a34 DH |
8 | char *basename(); |
9 | ||
58e0be1c BJ |
10 | extern char _sobuf[]; |
11 | ||
12 | main(argc, argv) | |
13 | int argc; | |
14 | char *argv[]; | |
15 | { | |
58e0be1c BJ |
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 | ||
8aa49a34 DH |
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 | \f | |
28 | /* | |
29 | * Look through the argv list for things to delete. | |
30 | */ | |
31 | ||
32 | delete(devname, spooldir, argc, argv) | |
bce5be06 KM |
33 | char *devname, *spooldir, *argv[]; |
34 | int argc; | |
8aa49a34 | 35 | { |
bce5be06 KM |
36 | DIR *dir; /* The spool dir. */ |
37 | struct direct *dirp; /* An entry read from the spool dir.*/ | |
8aa49a34 DH |
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); | |
58e0be1c BJ |
44 | } |
45 | ||
8aa49a34 | 46 | /* Open it. */ |
bce5be06 | 47 | if ((dir = opendir(".")) == NULL) { |
8aa49a34 DH |
48 | perror(spooldir); |
49 | return(1); | |
58e0be1c | 50 | } |
58e0be1c | 51 | |
8aa49a34 DH |
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) { | |
bce5be06 KM |
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; | |
8aa49a34 | 67 | } |
58e0be1c | 68 | } |
8aa49a34 | 69 | argv++; |
58e0be1c | 70 | } |
bce5be06 | 71 | closedir(dir); |
8aa49a34 DH |
72 | if (!deletion) |
73 | printf(" nothing to remove\n"); | |
74 | else | |
75 | putchar('\n'); | |
58e0be1c BJ |
76 | } |
77 | ||
8aa49a34 DH |
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) | |
bce5be06 | 92 | char *dfname, *arg; |
58e0be1c | 93 | { |
8aa49a34 DH |
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 | } | |
58e0be1c | 126 | |
8aa49a34 DH |
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); | |
58e0be1c | 138 | } |
8aa49a34 DH |
139 | |
140 | unlink(dfname); /* Now unlink the command file itself. */ | |
58e0be1c | 141 | } |
8aa49a34 DH |
142 | |
143 | if (df != NULL) | |
144 | fclose(df); | |
145 | return(delfile); | |
58e0be1c BJ |
146 | } |
147 | ||
58e0be1c | 148 | |
58e0be1c | 149 | |
8aa49a34 DH |
150 | |
151 | getline(file, line) | |
bce5be06 KM |
152 | FILE *file; |
153 | char *line; | |
58e0be1c BJ |
154 | { |
155 | register int i, c; | |
156 | ||
157 | i = 0; | |
8aa49a34 | 158 | while ((c = getc(file)) != '\n') { |
58e0be1c BJ |
159 | if (c <= 0) |
160 | return(0); | |
8aa49a34 | 161 | if (i < 256) |
58e0be1c BJ |
162 | line[i++] = c; |
163 | } | |
164 | line[i++] = 0; | |
165 | return (1); | |
166 | } | |
8aa49a34 DH |
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) | |
bce5be06 | 176 | char *pathname; |
8aa49a34 DH |
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 | } |