[1691] | 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)) |
---|
[1853] | 14 | static char rcsid_util_c[] = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/delete/util.c,v 1.8 1989-05-04 14:28:11 jik Exp $"; |
---|
[1691] | 15 | #endif |
---|
| 16 | |
---|
| 17 | #include <stdio.h> |
---|
| 18 | #include <sys/param.h> |
---|
| 19 | #include <sys/types.h> |
---|
[1732] | 20 | #include <sys/stat.h> |
---|
[1691] | 21 | #include <sys/dir.h> |
---|
| 22 | #include <strings.h> |
---|
[1695] | 23 | #include <pwd.h> |
---|
[1732] | 24 | #include "directories.h" |
---|
[1691] | 25 | #include "util.h" |
---|
[1811] | 26 | #include "mit-copyright.h" |
---|
[1691] | 27 | |
---|
[1695] | 28 | char *getenv(); |
---|
[1691] | 29 | |
---|
| 30 | |
---|
| 31 | char *convert_to_user_name(real_name, user_name) |
---|
| 32 | char real_name[]; |
---|
| 33 | char user_name[]; /* RETURN */ |
---|
| 34 | { |
---|
| 35 | char *ptr, *q; |
---|
| 36 | |
---|
| 37 | strcpy(user_name, real_name); |
---|
| 38 | while (ptr = strrindex(user_name, ".#")) { |
---|
| 39 | for (q = ptr; *(q + 2); q++) |
---|
| 40 | *q = *(q + 2); |
---|
| 41 | *q = '\0'; |
---|
| 42 | } |
---|
| 43 | return (user_name); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | char *strindex(str, sub_str) |
---|
| 51 | char *str, *sub_str; |
---|
| 52 | { |
---|
| 53 | char *ptr = str; |
---|
| 54 | while (ptr = index(ptr, *sub_str)) { |
---|
| 55 | if (! strncmp(ptr, sub_str, strlen(sub_str))) |
---|
| 56 | return(ptr); |
---|
| 57 | ptr++; |
---|
| 58 | } |
---|
| 59 | return ((char *) NULL); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | char *strrindex(str, sub_str) |
---|
| 65 | char *str, *sub_str; |
---|
| 66 | { |
---|
| 67 | char *ptr; |
---|
| 68 | |
---|
| 69 | if (strlen(str)) |
---|
| 70 | ptr = &str[strlen(str) - 1]; |
---|
| 71 | else |
---|
| 72 | return((char *) NULL); |
---|
| 73 | while ((*ptr != *sub_str) && (ptr != str)) ptr--; |
---|
| 74 | while (ptr != str) { |
---|
| 75 | if (! strncmp(ptr, sub_str, strlen(sub_str))) |
---|
| 76 | return(ptr); |
---|
| 77 | ptr--; |
---|
| 78 | while ((*ptr != *sub_str) && (ptr != str)) ptr--; |
---|
| 79 | } |
---|
| 80 | if (! strncmp(ptr, sub_str, strlen(sub_str))) |
---|
| 81 | return(str); |
---|
| 82 | else |
---|
| 83 | return ((char *) NULL); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | /* |
---|
| 88 | * NOTE: Append uses a static array, so its return value must be |
---|
| 89 | * copied immediately. |
---|
| 90 | */ |
---|
| 91 | char *append(filepath, filename) |
---|
| 92 | char *filepath, *filename; |
---|
| 93 | { |
---|
| 94 | static char buf[MAXPATHLEN]; |
---|
| 95 | |
---|
| 96 | strcpy(buf, filepath); |
---|
| 97 | if ((! *filename) || (! *filepath)) { |
---|
| 98 | strcpy(buf, filename); |
---|
| 99 | return(buf); |
---|
| 100 | } |
---|
| 101 | if (buf[strlen(buf) - 1] == '/') |
---|
| 102 | buf[strlen(buf) - 1] = '\0'; |
---|
| 103 | if (strlen(buf) + strlen(filename) + 2 > MAXPATHLEN) { |
---|
| 104 | *buf = '\0'; |
---|
| 105 | return(buf); |
---|
| 106 | } |
---|
| 107 | strcat(buf, "/"); |
---|
| 108 | strcat(buf, filename); |
---|
| 109 | return(buf); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | yes() { |
---|
| 116 | char buf[BUFSIZ]; |
---|
| 117 | char *val; |
---|
| 118 | |
---|
| 119 | val = fgets(buf, BUFSIZ, stdin); |
---|
[1732] | 120 | if (! val) { |
---|
| 121 | printf("\n"); |
---|
[1691] | 122 | exit(1); |
---|
[1732] | 123 | } |
---|
[1691] | 124 | if (! index(buf, '\n')) do |
---|
| 125 | fgets(buf + 1, BUFSIZ - 1, stdin); |
---|
| 126 | while (! index(buf + 1, '\n')); |
---|
| 127 | return(*buf == 'y'); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | char *lastpart(filename) |
---|
| 134 | char *filename; |
---|
| 135 | { |
---|
| 136 | char *part; |
---|
| 137 | |
---|
| 138 | part = rindex(filename, '/'); |
---|
| 139 | |
---|
| 140 | if (! part) |
---|
| 141 | part = filename; |
---|
| 142 | else if (part == filename) |
---|
| 143 | part++; |
---|
| 144 | else if (part - filename + 1 == strlen(filename)) { |
---|
| 145 | part = rindex(--part, '/'); |
---|
| 146 | if (! part) |
---|
| 147 | part = filename; |
---|
| 148 | else |
---|
| 149 | part++; |
---|
| 150 | } |
---|
| 151 | else |
---|
| 152 | part++; |
---|
| 153 | |
---|
| 154 | return(part); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | char *firstpart(filename, rest) |
---|
| 161 | char *filename; |
---|
| 162 | char *rest; /* RETURN */ |
---|
| 163 | { |
---|
| 164 | char *part; |
---|
| 165 | static char buf[MAXPATHLEN]; |
---|
| 166 | |
---|
| 167 | strcpy(buf, filename); |
---|
| 168 | part = index(buf, '/'); |
---|
| 169 | if (! part) { |
---|
| 170 | *rest = '\0'; |
---|
| 171 | return(buf); |
---|
| 172 | } |
---|
| 173 | strcpy(rest, part + 1); |
---|
| 174 | *part = '\0'; |
---|
| 175 | return(buf); |
---|
| 176 | } |
---|
[1695] | 177 | |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | |
---|
[1705] | 181 | char *reg_firstpart(filename, rest) |
---|
| 182 | char *filename; |
---|
| 183 | char *rest; /* RETURN */ |
---|
| 184 | { |
---|
| 185 | static char first[MAXNAMLEN]; |
---|
| 186 | |
---|
| 187 | sprintf(first, "^%s$", firstpart(filename, rest)); |
---|
| 188 | return(first); |
---|
| 189 | } |
---|
[1695] | 190 | |
---|
[1705] | 191 | |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | |
---|
[1695] | 196 | get_home(buf) |
---|
| 197 | char *buf; |
---|
| 198 | { |
---|
| 199 | char *user; |
---|
[1699] | 200 | struct passwd *psw; |
---|
[1695] | 201 | |
---|
| 202 | strcpy(buf, getenv("HOME")); |
---|
| 203 | |
---|
| 204 | if (*buf) |
---|
| 205 | return(0); |
---|
| 206 | |
---|
| 207 | user = getenv("USER"); |
---|
| 208 | psw = getpwnam(user); |
---|
| 209 | |
---|
| 210 | if (psw) { |
---|
| 211 | strcpy(buf, psw->pw_dir); |
---|
| 212 | return(0); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | psw = getpwuid(getuid()); |
---|
| 216 | |
---|
| 217 | if (psw) { |
---|
| 218 | strcpy(buf, psw->pw_dir); |
---|
| 219 | return(0); |
---|
| 220 | } |
---|
| 221 | return(1); |
---|
| 222 | } |
---|
[1732] | 223 | |
---|
| 224 | |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | timed_out(file_ent, current_time, min_days) |
---|
| 228 | filerec *file_ent; |
---|
| 229 | int current_time, min_days; |
---|
| 230 | { |
---|
| 231 | if ((current_time - file_ent->specs.st_mtime) / 86400 >= min_days) |
---|
| 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 | |
---|
| 252 | |
---|