[1714] | 1 | /* |
---|
| 2 | * $Source: /afs/dev.mit.edu/source/repository/athena/bin/delete/lsdel.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)) |
---|
[1851] | 14 | static char rcsid_lsdel_c[] = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/delete/lsdel.c,v 1.5 1989-05-04 14:18:18 jik Exp $"; |
---|
[1714] | 15 | #endif |
---|
| 16 | |
---|
| 17 | #include <stdio.h> |
---|
| 18 | #include <sys/types.h> |
---|
| 19 | #include <sys/dir.h> |
---|
| 20 | #include <sys/param.h> |
---|
| 21 | #include <sys/stat.h> |
---|
| 22 | #include <strings.h> |
---|
[1732] | 23 | #include "col.h" |
---|
[1714] | 24 | #include "util.h" |
---|
| 25 | #include "directories.h" |
---|
| 26 | #include "pattern.h" |
---|
[1732] | 27 | #include "lsdel.h" |
---|
[1811] | 28 | #include "mit-copyright.h" |
---|
[1714] | 29 | |
---|
| 30 | char *malloc(), *realloc(); |
---|
[1732] | 31 | extern int current_time; |
---|
[1714] | 32 | |
---|
[1732] | 33 | int block_total = 0; |
---|
| 34 | int dirsonly, recursive, timev, yield; |
---|
[1714] | 35 | char *whoami, *error_buf; |
---|
| 36 | |
---|
| 37 | main(argc, argv) |
---|
| 38 | int argc; |
---|
| 39 | char *argv[]; |
---|
| 40 | { |
---|
| 41 | extern char *optarg; |
---|
| 42 | extern int optind; |
---|
| 43 | int arg; |
---|
| 44 | |
---|
| 45 | whoami = lastpart(argv[0]); |
---|
| 46 | error_buf = malloc(strlen(whoami) + MAXPATHLEN + 3); |
---|
| 47 | if (! error_buf) { |
---|
| 48 | perror(whoami); |
---|
| 49 | exit(1); |
---|
| 50 | } |
---|
[1732] | 51 | dirsonly = recursive = timev = yield = 0; |
---|
| 52 | while ((arg = getopt(argc, argv, "drt:y")) != -1) { |
---|
[1714] | 53 | switch (arg) { |
---|
| 54 | case 'd': |
---|
| 55 | dirsonly++; |
---|
| 56 | break; |
---|
| 57 | case 'r': |
---|
| 58 | recursive++; |
---|
| 59 | break; |
---|
[1732] | 60 | case 't': |
---|
| 61 | timev = atoi(optarg); |
---|
| 62 | break; |
---|
| 63 | case 'y': |
---|
| 64 | yield++; |
---|
| 65 | break; |
---|
[1714] | 66 | default: |
---|
| 67 | usage(); |
---|
| 68 | exit(1); |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | if (optind == argc) { |
---|
[1732] | 72 | char *cwd; |
---|
| 73 | |
---|
| 74 | cwd = "."; |
---|
| 75 | exit(ls(&cwd, 1)); |
---|
[1714] | 76 | } |
---|
| 77 | exit(ls(&argv[optind], argc - optind)); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | usage() |
---|
| 86 | { |
---|
[1732] | 87 | printf("Usage: %s [ options ] [ filename [ ...]]\n", whoami); |
---|
[1714] | 88 | printf("Options are:\n"); |
---|
| 89 | printf(" -d list directory names, not contents\n"); |
---|
| 90 | printf(" -r recursive\n"); |
---|
[1732] | 91 | printf(" -t n list n-day-or-older files only\n"); |
---|
| 92 | printf(" -y report total space taken up by files\n"); |
---|
[1714] | 93 | } |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | ls(args, num) |
---|
| 99 | char **args; |
---|
| 100 | int num; |
---|
| 101 | { |
---|
| 102 | char *start_dir; |
---|
| 103 | char **found_files; |
---|
| 104 | int num_found, total = 0; |
---|
| 105 | char *file_re; |
---|
| 106 | int status = 0; |
---|
| 107 | |
---|
| 108 | if (initialize_tree()) |
---|
| 109 | exit(1); |
---|
| 110 | |
---|
| 111 | for ( ; num; num--) { |
---|
| 112 | if (*args[num - 1] == '/') { |
---|
| 113 | start_dir = "/"; |
---|
| 114 | file_re = parse_pattern(args[num - 1] + 1); |
---|
| 115 | } |
---|
| 116 | else { |
---|
| 117 | start_dir = ""; |
---|
[1851] | 118 | if ((*args[num - 1] == '.') && (! *(args[num - 1] + 1))) |
---|
| 119 | file_re = parse_pattern("*"); |
---|
| 120 | else |
---|
| 121 | file_re = parse_pattern(args[num - 1]); |
---|
[1714] | 122 | } |
---|
| 123 | if (! file_re) |
---|
| 124 | return(ERROR_MASK); |
---|
| 125 | |
---|
| 126 | found_files = get_the_files(start_dir, file_re, &num_found); |
---|
| 127 | free(file_re); |
---|
| 128 | total += num_found; |
---|
| 129 | if (num_found) |
---|
[1732] | 130 | num_found = process_files(found_files, num_found); |
---|
[1754] | 131 | else { |
---|
| 132 | /* What we do at this point depends on exactly what the |
---|
| 133 | * file_re is. There are three possible conditions: |
---|
| 134 | * 1. It's an existing directory. Print nothing. |
---|
| 135 | * 2. It doesn't exist in deleted form, and there are |
---|
| 136 | * no wildcards in it. Then we print "not found." |
---|
| 137 | * 3. It does't exist, but there are wildcards in it. |
---|
| 138 | * Then we print "no match." |
---|
| 139 | * None of these are considered error conditions, so we |
---|
| 140 | * don't set the error flag. |
---|
| 141 | */ |
---|
| 142 | if (no_wildcards(file_re)) { |
---|
| 143 | if (! directory_exists(args[num - 1])) { |
---|
| 144 | fprintf(stderr, "%s: %s: not found\n", |
---|
| 145 | whoami, args[num - 1]); |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | else { |
---|
| 149 | fprintf(stderr, "%s: %s: no match\n", |
---|
| 150 | whoami, args[num-1]); |
---|
| 151 | } |
---|
[1714] | 152 | } |
---|
| 153 | } |
---|
| 154 | if (total) { |
---|
| 155 | list_files(); |
---|
| 156 | } |
---|
[1732] | 157 | if (yield) |
---|
| 158 | printf("\nTotal space taken up by file%s: %dk\n", |
---|
| 159 | (total == 1 ? "" : "s"), blk_to_k(block_total)); |
---|
[1714] | 160 | return(status); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | char **get_the_files(start_dir, file_re, number_found) |
---|
| 167 | char *start_dir, *file_re; |
---|
| 168 | int *number_found; |
---|
| 169 | { |
---|
| 170 | char **matches; |
---|
| 171 | int num_matches; |
---|
| 172 | char **found; |
---|
| 173 | int num; |
---|
| 174 | int i; |
---|
| 175 | |
---|
| 176 | found = (char **) malloc(0); |
---|
| 177 | num = 0; |
---|
| 178 | |
---|
| 179 | matches = find_matches(start_dir, file_re, &num_matches); |
---|
| 180 | if (recursive) { |
---|
| 181 | char **recurs_found; |
---|
| 182 | int recurs_num; |
---|
| 183 | |
---|
| 184 | for (i = 0; i < num_matches; free(matches[i]), i++) { |
---|
| 185 | if (is_deleted(lastpart(matches[i]))) { |
---|
| 186 | found = add_str(found, num, matches[i]); |
---|
| 187 | num++; |
---|
| 188 | } |
---|
| 189 | recurs_found = find_deleted_recurses(matches[i], &recurs_num); |
---|
| 190 | add_arrays(&found, &num, &recurs_found, &recurs_num); |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | else { |
---|
| 194 | struct stat stat_buf; |
---|
| 195 | char **contents_found; |
---|
| 196 | int num_contents; |
---|
| 197 | |
---|
| 198 | for (i = 0; i < num_matches; free(matches[i]), i++) { |
---|
| 199 | if (is_deleted(lastpart(matches[i]))) { |
---|
| 200 | found = add_str(found, num, matches[i]); |
---|
| 201 | num++; |
---|
| 202 | } |
---|
| 203 | if (dirsonly) |
---|
| 204 | continue; |
---|
| 205 | if (lstat(matches[i], &stat_buf)) |
---|
| 206 | continue; |
---|
[1754] | 207 | if ((stat_buf.st_mode & S_IFMT) == S_IFDIR) { |
---|
[1714] | 208 | contents_found = find_deleted_contents_recurs(matches[i], |
---|
| 209 | &num_contents); |
---|
| 210 | add_arrays(&found, &num, &contents_found, &num_contents); |
---|
| 211 | |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | free(matches); |
---|
| 216 | *number_found = num; |
---|
| 217 | return(found); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | process_files(files, num) |
---|
| 226 | char **files; |
---|
| 227 | int num; |
---|
| 228 | { |
---|
| 229 | int i; |
---|
[1732] | 230 | filerec *leaf; |
---|
| 231 | |
---|
[1714] | 232 | for (i = 0; i < num; i++) { |
---|
[1732] | 233 | if (! (leaf = add_path_to_tree(files[i]))) { |
---|
[1714] | 234 | fprintf(stderr, "%s: error adding path to filename tree\n", |
---|
| 235 | whoami); |
---|
| 236 | exit(1); |
---|
| 237 | } |
---|
[1732] | 238 | |
---|
| 239 | free(files[i]); |
---|
| 240 | if (! timed_out(leaf, current_time, timev)) { |
---|
| 241 | free_leaf(leaf); |
---|
| 242 | num--; |
---|
| 243 | continue; |
---|
| 244 | } |
---|
| 245 | block_total += leaf->specs.st_blocks; |
---|
[1714] | 246 | } |
---|
| 247 | free(files); |
---|
[1732] | 248 | return(num); |
---|
[1714] | 249 | } |
---|
| 250 | |
---|
| 251 | |
---|
| 252 | |
---|
| 253 | |
---|
| 254 | |
---|
| 255 | list_files() |
---|
| 256 | { |
---|
| 257 | filerec *current; |
---|
| 258 | char **strings; |
---|
| 259 | int num; |
---|
[1732] | 260 | |
---|
| 261 | strings = (char **) malloc(sizeof(char *)); |
---|
[1714] | 262 | num = 0; |
---|
[1732] | 263 | if (! strings) { |
---|
| 264 | perror(sprintf(error_buf, "%s: list_files", whoami)); |
---|
| 265 | exit(1); |
---|
| 266 | } |
---|
[1714] | 267 | current = get_root_tree(); |
---|
[1732] | 268 | strings = accumulate_names(current, strings, &num); |
---|
[1714] | 269 | current = get_cwd_tree(); |
---|
[1732] | 270 | strings = accumulate_names(current, strings, &num); |
---|
| 271 | column_array(strings, num, DEF_SCR_WIDTH, 0, 0, 2, 1, 0, 1, stdout); |
---|
| 272 | for ( ; num; num--) |
---|
| 273 | free(strings[num - 1]); |
---|
| 274 | free(strings); |
---|
[1714] | 275 | return(0); |
---|
| 276 | } |
---|