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