source: trunk/athena/bin/delete/util.c @ 12350

Revision 12350, 7.6 KB checked in by ghudson, 26 years ago (diff)
Some RCS ID cleanup: delete $Log$ and replace other RCS keywords with $Id$.
RevLine 
[1691]1/*
[12350]2 * $Id: util.c,v 1.32 1999-01-22 23:09:09 ghudson Exp $
[1691]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.
[4505]9 * For copying and distribution information, see the file "mit-copying.h."
[1691]10 */
11
12#if (!defined(lint) && !defined(SABER))
[12350]13     static char rcsid_util_c[] = "$Id: util.c,v 1.32 1999-01-22 23:09:09 ghudson Exp $";
[1691]14#endif
15
16#include <stdio.h>
17#include <sys/param.h>
18#include <sys/types.h>
[5049]19#include <dirent.h>
[3049]20#include <string.h>
[1695]21#include <pwd.h>
[2169]22#include <errno.h>
[12144]23#ifdef HAVE_AFS
[2221]24#include <sys/ioctl.h>
[12144]25#include <rx/rx.h>
[4405]26#include <afs/param.h>
[2480]27#include <afs/vice.h>
[6343]28#include <netinet/in.h>
[2480]29#include <afs/venus.h>
[2221]30#endif
[2169]31#include "delete_errs.h"
[4416]32#include "util.h"
[1732]33#include "directories.h"
[4505]34#include "mit-copying.h"
[2169]35#include "errors.h"
[1691]36
37char *convert_to_user_name(real_name, user_name)
38char real_name[];
39char user_name[];  /* RETURN */
40{
41     char *ptr, *q;
42     
[2169]43     (void) strcpy(user_name, real_name);
[1691]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
56char *strindex(str, sub_str)
57char *str, *sub_str;
58{
59     char *ptr = str;
[10977]60     while (ptr = strchr(ptr, *sub_str)) {
[1691]61          if (! strncmp(ptr, sub_str, strlen(sub_str)))
62               return(ptr);
63          ptr++;
64     }
65     return ((char *) NULL);
66}
67
68
69
70char *strrindex(str, sub_str)
71char *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 */
97char *append(filepath, filename)
98char *filepath, *filename;
99{
100     static char buf[MAXPATHLEN];
101
[2169]102     (void) strcpy(buf, filepath);
[1691]103     if ((! *filename) || (! *filepath)) {
[2169]104          (void) strcpy(buf, filename);
[1691]105          return(buf);
106     }
107     if (buf[strlen(buf) - 1] == '/')
108          buf[strlen(buf) - 1] = '\0';
109     if (strlen(buf) + strlen(filename) + 2 > MAXPATHLEN) {
[2169]110          set_error(ENAMETOOLONG);
111          strncat(buf, "/", MAXPATHLEN - strlen(buf) - 1);
112          strncat(buf, filename, MAXPATHLEN - strlen(buf) - 1);
113          error(buf);
[1691]114          *buf = '\0';
[2169]115          return buf;
[1691]116     }
[2169]117     (void) strcat(buf, "/");
118     (void) strcat(buf, filename);
119     return buf;
[1691]120}
121
122
123
124
125yes() {
126     char buf[BUFSIZ];
127     char *val;
128     
129     val = fgets(buf, BUFSIZ, stdin);
[1732]130     if (! val) {
131          printf("\n");
[1691]132          exit(1);
[1732]133     }
[10977]134     if (! strchr(buf, '\n')) do
[2169]135          (void) fgets(buf + 1, BUFSIZ - 1, stdin);
[10977]136     while (! strchr(buf + 1, '\n'));
[1691]137     return(*buf == 'y');
138}
139
140
141
142
143char *lastpart(filename)
144char *filename;
145{
146     char *part;
147
[10977]148     part = strrchr(filename, '/');
[1691]149
150     if (! part)
151          part = filename;
152     else if (part == filename)
153          part++;
154     else if (part - filename + 1 == strlen(filename)) {
[10977]155          part = strrchr(--part, '/');
[1691]156          if (! part)
157               part = filename;
158          else
159               part++;
160     }
161     else
162          part++;
163
164     return(part);
165}
166
167
168
169
170char *firstpart(filename, rest)
171char *filename;
172char *rest; /* RETURN */
173{
174     char *part;
175     static char buf[MAXPATHLEN];
176
[2169]177     (void) strcpy(buf, filename);
[10977]178     part = strchr(buf, '/');
[1691]179     if (! part) {
180          *rest = '\0';
181          return(buf);
182     }
[2169]183     (void) strcpy(rest, part + 1);
[1691]184     *part = '\0';
185     return(buf);
186}
[1695]187
188
189
190
191
192get_home(buf)
193char *buf;
194{
195     char *user;
[1699]196     struct passwd *psw;
[1695]197     
[2169]198     (void) strcpy(buf, getenv("HOME"));
[1695]199     
200     if (*buf)
201          return(0);
202
203     user = getenv("USER");
204     psw = getpwnam(user);
205
206     if (psw) {
[2169]207          (void) strcpy(buf, psw->pw_dir);
[1695]208          return(0);
209     }
210     
[2169]211     psw = getpwuid((int) getuid());
[1695]212
213     if (psw) {
[2169]214          (void) strcpy(buf, psw->pw_dir);
[1695]215          return(0);
[2169]216     }
217
218     set_error(NO_HOME_DIR);
219     error("get_home");
220     return error_code;
[1695]221}
[1732]222
223
224
225
226timed_out(file_ent, current_time, min_days)
227filerec *file_ent;
[2221]228time_t current_time, min_days;
[1732]229{
[7979]230     if ((current_time - file_ent->specs.st_chtime) / 86400 >= min_days)
[1732]231          return(1);
232     else
233          return(0);
234}
[1755]235
236
237
238int directory_exists(dirname)
239char *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
[2221]251
252
253is_link(name, oldbuf)
254char *name;
255struct stat *oldbuf;
256{
[4416]257#ifdef S_IFLNK
[2221]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
[4416]271#endif
[2221]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/*
[4403]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.
[2221]287 */
288int is_mountpoint(name, oldbuf)
289char *name;
290struct stat *oldbuf;
291{
292     struct stat statbuf;
293     dev_t device;
294     char buf[MAXPATHLEN];
[12144]295#ifdef HAVE_AFS
[2221]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);
[2373]308          error(name);
[2221]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);
[2373]316          error(name);
[2221]317          return 0;
318     }
319
320     strcpy(buf, name);
321     strcat(buf, "/..");
322     if (lstat(buf, &statbuf) < 0) {
323          set_error(errno);
[2373]324          error(name);
[2221]325          return 0;
326     }
327
328     if (statbuf.st_dev != device)
329          return 1;
330
[12144]331#ifdef HAVE_AFS
[2221]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;
[7163]339          memset(retbuf, 0, MAXPATHLEN);
[2221]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;
[7163]350          memset(retbuf, 0, MAXPATHLEN);
[2221]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);
[2373]365               error(name);
[2221]366          }
367     }
[12144]368#endif /* HAVE_AFS */
[2221]369
370     return 0;
371}
[2362]372
[2581]373#ifdef MALLOC_DEBUG
[2362]374char *Malloc(size)
375unsigned 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         
Note: See TracBrowser for help on using the repository browser.