1 | /* |
---|
2 | * $Source: /afs/dev.mit.edu/source/repository/athena/bin/delete/util.c,v $ |
---|
3 | * $Author: jik $ |
---|
4 | * |
---|
5 | * This program is a replacement for rm. Instead of actually deleting |
---|
6 | * files, it marks them for deletion by prefixing them with a ".#" |
---|
7 | * prefix. |
---|
8 | * |
---|
9 | * Copyright (c) 1989 by the Massachusetts Institute of Technology. |
---|
10 | * For copying and distribution information, see the file "mit-copyright.h." |
---|
11 | */ |
---|
12 | |
---|
13 | #if (!defined(lint) && !defined(SABER)) |
---|
14 | static char rcsid_util_c[] = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/delete/util.c,v 1.11 1989-11-06 21:56:17 jik Exp $"; |
---|
15 | #endif |
---|
16 | |
---|
17 | #ifdef AFS_MOUNTPOINTS |
---|
18 | /* |
---|
19 | * I assume there's a "right" header file to include to get the symbol |
---|
20 | * VICE defined, but I can't find it, and if VICE isn't defined then |
---|
21 | * certain header files don't load properly, so I'm doing it by hand. |
---|
22 | */ |
---|
23 | #define VICE |
---|
24 | #endif |
---|
25 | #include <stdio.h> |
---|
26 | #include <sys/param.h> |
---|
27 | #include <sys/types.h> |
---|
28 | #include <sys/stat.h> |
---|
29 | #include <sys/dir.h> |
---|
30 | #include <strings.h> |
---|
31 | #include <pwd.h> |
---|
32 | #include <errno.h> |
---|
33 | #ifdef AFS_MOUNTPOINTS |
---|
34 | #include <sys/ioctl.h> |
---|
35 | #include <sys/vice.h> |
---|
36 | /* |
---|
37 | * there has to be a global header file that contains this |
---|
38 | * information, but once again, I can't find it, so this is the best I |
---|
39 | * can do. Sigh. |
---|
40 | */ |
---|
41 | #include <venus/uvenus.h> |
---|
42 | #endif |
---|
43 | #include "delete_errs.h" |
---|
44 | #include "directories.h" |
---|
45 | #include "util.h" |
---|
46 | #include "mit-copyright.h" |
---|
47 | #include "errors.h" |
---|
48 | |
---|
49 | extern char *getenv(); |
---|
50 | extern uid_t getuid(); |
---|
51 | extern int errno; |
---|
52 | |
---|
53 | char *convert_to_user_name(real_name, user_name) |
---|
54 | char real_name[]; |
---|
55 | char user_name[]; /* RETURN */ |
---|
56 | { |
---|
57 | char *ptr, *q; |
---|
58 | |
---|
59 | (void) strcpy(user_name, real_name); |
---|
60 | while (ptr = strrindex(user_name, ".#")) { |
---|
61 | for (q = ptr; *(q + 2); q++) |
---|
62 | *q = *(q + 2); |
---|
63 | *q = '\0'; |
---|
64 | } |
---|
65 | return (user_name); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | |
---|
71 | |
---|
72 | char *strindex(str, sub_str) |
---|
73 | char *str, *sub_str; |
---|
74 | { |
---|
75 | char *ptr = str; |
---|
76 | while (ptr = index(ptr, *sub_str)) { |
---|
77 | if (! strncmp(ptr, sub_str, strlen(sub_str))) |
---|
78 | return(ptr); |
---|
79 | ptr++; |
---|
80 | } |
---|
81 | return ((char *) NULL); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | char *strrindex(str, sub_str) |
---|
87 | char *str, *sub_str; |
---|
88 | { |
---|
89 | char *ptr; |
---|
90 | |
---|
91 | if (strlen(str)) |
---|
92 | ptr = &str[strlen(str) - 1]; |
---|
93 | else |
---|
94 | return((char *) NULL); |
---|
95 | while ((*ptr != *sub_str) && (ptr != str)) ptr--; |
---|
96 | while (ptr != str) { |
---|
97 | if (! strncmp(ptr, sub_str, strlen(sub_str))) |
---|
98 | return(ptr); |
---|
99 | ptr--; |
---|
100 | while ((*ptr != *sub_str) && (ptr != str)) ptr--; |
---|
101 | } |
---|
102 | if (! strncmp(ptr, sub_str, strlen(sub_str))) |
---|
103 | return(str); |
---|
104 | else |
---|
105 | return ((char *) NULL); |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | /* |
---|
110 | * NOTE: Append uses a static array, so its return value must be |
---|
111 | * copied immediately. |
---|
112 | */ |
---|
113 | char *append(filepath, filename) |
---|
114 | char *filepath, *filename; |
---|
115 | { |
---|
116 | static char buf[MAXPATHLEN]; |
---|
117 | |
---|
118 | (void) strcpy(buf, filepath); |
---|
119 | if ((! *filename) || (! *filepath)) { |
---|
120 | (void) strcpy(buf, filename); |
---|
121 | return(buf); |
---|
122 | } |
---|
123 | if (buf[strlen(buf) - 1] == '/') |
---|
124 | buf[strlen(buf) - 1] = '\0'; |
---|
125 | if (strlen(buf) + strlen(filename) + 2 > MAXPATHLEN) { |
---|
126 | set_error(ENAMETOOLONG); |
---|
127 | strncat(buf, "/", MAXPATHLEN - strlen(buf) - 1); |
---|
128 | strncat(buf, filename, MAXPATHLEN - strlen(buf) - 1); |
---|
129 | error(buf); |
---|
130 | *buf = '\0'; |
---|
131 | return buf; |
---|
132 | } |
---|
133 | (void) strcat(buf, "/"); |
---|
134 | (void) strcat(buf, filename); |
---|
135 | return buf; |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | yes() { |
---|
142 | char buf[BUFSIZ]; |
---|
143 | char *val; |
---|
144 | |
---|
145 | val = fgets(buf, BUFSIZ, stdin); |
---|
146 | if (! val) { |
---|
147 | printf("\n"); |
---|
148 | exit(1); |
---|
149 | } |
---|
150 | if (! index(buf, '\n')) do |
---|
151 | (void) fgets(buf + 1, BUFSIZ - 1, stdin); |
---|
152 | while (! index(buf + 1, '\n')); |
---|
153 | return(*buf == 'y'); |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | char *lastpart(filename) |
---|
160 | char *filename; |
---|
161 | { |
---|
162 | char *part; |
---|
163 | |
---|
164 | part = rindex(filename, '/'); |
---|
165 | |
---|
166 | if (! part) |
---|
167 | part = filename; |
---|
168 | else if (part == filename) |
---|
169 | part++; |
---|
170 | else if (part - filename + 1 == strlen(filename)) { |
---|
171 | part = rindex(--part, '/'); |
---|
172 | if (! part) |
---|
173 | part = filename; |
---|
174 | else |
---|
175 | part++; |
---|
176 | } |
---|
177 | else |
---|
178 | part++; |
---|
179 | |
---|
180 | return(part); |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | char *firstpart(filename, rest) |
---|
187 | char *filename; |
---|
188 | char *rest; /* RETURN */ |
---|
189 | { |
---|
190 | char *part; |
---|
191 | static char buf[MAXPATHLEN]; |
---|
192 | |
---|
193 | (void) strcpy(buf, filename); |
---|
194 | part = index(buf, '/'); |
---|
195 | if (! part) { |
---|
196 | *rest = '\0'; |
---|
197 | return(buf); |
---|
198 | } |
---|
199 | (void) strcpy(rest, part + 1); |
---|
200 | *part = '\0'; |
---|
201 | return(buf); |
---|
202 | } |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | |
---|
207 | |
---|
208 | get_home(buf) |
---|
209 | char *buf; |
---|
210 | { |
---|
211 | char *user; |
---|
212 | struct passwd *psw; |
---|
213 | |
---|
214 | (void) strcpy(buf, getenv("HOME")); |
---|
215 | |
---|
216 | if (*buf) |
---|
217 | return(0); |
---|
218 | |
---|
219 | user = getenv("USER"); |
---|
220 | psw = getpwnam(user); |
---|
221 | |
---|
222 | if (psw) { |
---|
223 | (void) strcpy(buf, psw->pw_dir); |
---|
224 | return(0); |
---|
225 | } |
---|
226 | |
---|
227 | psw = getpwuid((int) getuid()); |
---|
228 | |
---|
229 | if (psw) { |
---|
230 | (void) strcpy(buf, psw->pw_dir); |
---|
231 | return(0); |
---|
232 | } |
---|
233 | |
---|
234 | set_error(NO_HOME_DIR); |
---|
235 | error("get_home"); |
---|
236 | return error_code; |
---|
237 | } |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | |
---|
242 | timed_out(file_ent, current_time, min_days) |
---|
243 | filerec *file_ent; |
---|
244 | time_t current_time, min_days; |
---|
245 | { |
---|
246 | if ((current_time - file_ent->specs.st_mtime) / 86400 >= min_days) |
---|
247 | return(1); |
---|
248 | else |
---|
249 | return(0); |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | int directory_exists(dirname) |
---|
255 | char *dirname; |
---|
256 | { |
---|
257 | struct stat stat_buf; |
---|
258 | |
---|
259 | if (stat(dirname, &stat_buf)) |
---|
260 | return(0); |
---|
261 | else if ((stat_buf.st_mode & S_IFMT) == S_IFDIR) |
---|
262 | return(1); |
---|
263 | else |
---|
264 | return(0); |
---|
265 | } |
---|
266 | |
---|
267 | |
---|
268 | |
---|
269 | is_link(name, oldbuf) |
---|
270 | char *name; |
---|
271 | struct stat *oldbuf; |
---|
272 | { |
---|
273 | struct stat statbuf; |
---|
274 | |
---|
275 | if (oldbuf) |
---|
276 | statbuf = *oldbuf; |
---|
277 | else if (lstat(name, &statbuf) < 0) { |
---|
278 | set_error(errno); |
---|
279 | error("is_link"); |
---|
280 | return(0); |
---|
281 | } |
---|
282 | |
---|
283 | if ((statbuf.st_mode & S_IFMT) == S_IFLNK) |
---|
284 | return 1; |
---|
285 | else |
---|
286 | return 0; |
---|
287 | } |
---|
288 | |
---|
289 | |
---|
290 | |
---|
291 | /* |
---|
292 | * This is one of the few procedures that is allowed to break the |
---|
293 | * rule of always returning an error value if an error occurs. That's |
---|
294 | * because it's stupid to expect a boolean function to do that, and |
---|
295 | * because defaulting to not being a mountpoint if there is an error |
---|
296 | * is a reasonable thing to do. |
---|
297 | */ |
---|
298 | /* |
---|
299 | * The second parameter is optional -- if it is non-NULL< it is |
---|
300 | * presumed to be a stat structure fo the file being passed in. |
---|
301 | */ |
---|
302 | int is_mountpoint(name, oldbuf) |
---|
303 | char *name; |
---|
304 | struct stat *oldbuf; |
---|
305 | { |
---|
306 | struct stat statbuf; |
---|
307 | dev_t device; |
---|
308 | char buf[MAXPATHLEN]; |
---|
309 | #ifdef AFS_MOUNTPOINTS |
---|
310 | struct ViceIoctl blob; |
---|
311 | char retbuf[MAXPATHLEN]; |
---|
312 | int retval; |
---|
313 | char *shortname; |
---|
314 | #endif |
---|
315 | |
---|
316 | /* First way to check for a mount point -- if the device number */ |
---|
317 | /* of name is different from the device number of name/.. */ |
---|
318 | if (oldbuf) |
---|
319 | statbuf = *oldbuf; |
---|
320 | else if (lstat(name, &statbuf) < 0) { |
---|
321 | set_error(errno); |
---|
322 | error("is_mountpoint"); |
---|
323 | return 0; |
---|
324 | } |
---|
325 | |
---|
326 | device = statbuf.st_dev; |
---|
327 | |
---|
328 | if (strlen(name) + 4 /* length of "/.." + a NULL */ > MAXPATHLEN) { |
---|
329 | set_error(ENAMETOOLONG); |
---|
330 | error("is_mountpoint"); |
---|
331 | return 0; |
---|
332 | } |
---|
333 | |
---|
334 | strcpy(buf, name); |
---|
335 | strcat(buf, "/.."); |
---|
336 | if (lstat(buf, &statbuf) < 0) { |
---|
337 | set_error(errno); |
---|
338 | error("is_mountpoint"); |
---|
339 | return 0; |
---|
340 | } |
---|
341 | |
---|
342 | if (statbuf.st_dev != device) |
---|
343 | return 1; |
---|
344 | |
---|
345 | #ifdef AFS_MOUNTPOINTS |
---|
346 | /* Check for AFS mountpoint using the AFS pioctl call. */ |
---|
347 | if ((shortname = lastpart(name)) == name) { |
---|
348 | strcpy(buf, "."); |
---|
349 | blob.in = name; |
---|
350 | blob.in_size = strlen(name) + 1; |
---|
351 | blob.out = retbuf; |
---|
352 | blob.out_size = MAXPATHLEN; |
---|
353 | bzero(retbuf, MAXPATHLEN); |
---|
354 | } |
---|
355 | else { |
---|
356 | strncpy(buf, name, shortname - name - 1); |
---|
357 | buf[shortname - name - 1] = '\0'; |
---|
358 | if (*buf == '\0') |
---|
359 | strcpy(buf, "/"); |
---|
360 | blob.in = shortname; |
---|
361 | blob.in_size = strlen(shortname) + 1; |
---|
362 | blob.out = retbuf; |
---|
363 | blob.out_size = MAXPATHLEN; |
---|
364 | bzero(retbuf, MAXPATHLEN); |
---|
365 | } |
---|
366 | |
---|
367 | retval = pioctl(buf, VIOC_AFS_STAT_MT_PT, &blob, 0); |
---|
368 | |
---|
369 | if (retval == 0) { |
---|
370 | #ifdef DEBUG |
---|
371 | printf("%s is an AFS mountpoint, is_mountpoint returning true.\n", |
---|
372 | name); |
---|
373 | #endif |
---|
374 | return 1; |
---|
375 | } |
---|
376 | else { |
---|
377 | if (errno != EINVAL) { |
---|
378 | set_error(errno); |
---|
379 | error("is_mountpoint"); |
---|
380 | } |
---|
381 | } |
---|
382 | #endif /* AFS_MOUNTPOINTS */ |
---|
383 | |
---|
384 | return 0; |
---|
385 | } |
---|