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