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)) |
---|
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 $"; |
---|
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> |
---|
23 | #include "col.h" |
---|
24 | #include "util.h" |
---|
25 | #include "directories.h" |
---|
26 | #include "pattern.h" |
---|
27 | #include "lsdel.h" |
---|
28 | #include "mit-copyright.h" |
---|
29 | |
---|
30 | char *malloc(), *realloc(); |
---|
31 | extern int current_time; |
---|
32 | |
---|
33 | int block_total = 0; |
---|
34 | int dirsonly, recursive, timev, yield; |
---|
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 | } |
---|
51 | dirsonly = recursive = timev = yield = 0; |
---|
52 | while ((arg = getopt(argc, argv, "drt:y")) != -1) { |
---|
53 | switch (arg) { |
---|
54 | case 'd': |
---|
55 | dirsonly++; |
---|
56 | break; |
---|
57 | case 'r': |
---|
58 | recursive++; |
---|
59 | break; |
---|
60 | case 't': |
---|
61 | timev = atoi(optarg); |
---|
62 | break; |
---|
63 | case 'y': |
---|
64 | yield++; |
---|
65 | break; |
---|
66 | default: |
---|
67 | usage(); |
---|
68 | exit(1); |
---|
69 | } |
---|
70 | } |
---|
71 | if (optind == argc) { |
---|
72 | char *cwd; |
---|
73 | |
---|
74 | cwd = "."; |
---|
75 | exit(ls(&cwd, 1)); |
---|
76 | } |
---|
77 | exit(ls(&argv[optind], argc - optind)); |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | |
---|
84 | |
---|
85 | usage() |
---|
86 | { |
---|
87 | printf("Usage: %s [ options ] [ filename [ ...]]\n", whoami); |
---|
88 | printf("Options are:\n"); |
---|
89 | printf(" -d list directory names, not contents\n"); |
---|
90 | printf(" -r recursive\n"); |
---|
91 | printf(" -t n list n-day-or-older files only\n"); |
---|
92 | printf(" -y report total space taken up by files\n"); |
---|
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 = ""; |
---|
118 | if ((*args[num - 1] == '.') && (! *(args[num - 1] + 1))) |
---|
119 | file_re = parse_pattern("*"); |
---|
120 | else |
---|
121 | file_re = parse_pattern(args[num - 1]); |
---|
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) |
---|
130 | num_found = process_files(found_files, num_found); |
---|
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 | } |
---|
152 | } |
---|
153 | } |
---|
154 | if (total) { |
---|
155 | list_files(); |
---|
156 | } |
---|
157 | if (yield) |
---|
158 | printf("\nTotal space taken up by file%s: %dk\n", |
---|
159 | (total == 1 ? "" : "s"), blk_to_k(block_total)); |
---|
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; |
---|
207 | if ((stat_buf.st_mode & S_IFMT) == S_IFDIR) { |
---|
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; |
---|
230 | filerec *leaf; |
---|
231 | |
---|
232 | for (i = 0; i < num; i++) { |
---|
233 | if (! (leaf = add_path_to_tree(files[i]))) { |
---|
234 | fprintf(stderr, "%s: error adding path to filename tree\n", |
---|
235 | whoami); |
---|
236 | exit(1); |
---|
237 | } |
---|
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; |
---|
246 | } |
---|
247 | free(files); |
---|
248 | return(num); |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | |
---|
255 | list_files() |
---|
256 | { |
---|
257 | filerec *current; |
---|
258 | char **strings; |
---|
259 | int num; |
---|
260 | |
---|
261 | strings = (char **) malloc(sizeof(char *)); |
---|
262 | num = 0; |
---|
263 | if (! strings) { |
---|
264 | perror(sprintf(error_buf, "%s: list_files", whoami)); |
---|
265 | exit(1); |
---|
266 | } |
---|
267 | current = get_root_tree(); |
---|
268 | strings = accumulate_names(current, strings, &num); |
---|
269 | current = get_cwd_tree(); |
---|
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); |
---|
275 | return(0); |
---|
276 | } |
---|