1 | /*
|
---|
2 | * Copyright (c) 2008 Tim Post
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /*
|
---|
30 | * NOTE:
|
---|
31 | * This is a bit of an ugly hack, working around the absence of fstat / etc.
|
---|
32 | * As more stuff is completed and exposed in libc, this will improve
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <errno.h>
|
---|
36 | #include <str_error.h>
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <stdint.h>
|
---|
40 | #include <dirent.h>
|
---|
41 | #include <getopt.h>
|
---|
42 | #include <vfs/vfs.h>
|
---|
43 | #include <str.h>
|
---|
44 | #include <cap.h>
|
---|
45 |
|
---|
46 | #include "ls.h"
|
---|
47 | #include "errors.h"
|
---|
48 | #include "config.h"
|
---|
49 | #include "util.h"
|
---|
50 | #include "entry.h"
|
---|
51 | #include "cmds.h"
|
---|
52 |
|
---|
53 | static const char *cmdname = "ls";
|
---|
54 |
|
---|
55 | static ls_job_t ls;
|
---|
56 |
|
---|
57 | static struct option const long_options[] = {
|
---|
58 | { "help", no_argument, 0, 'h' },
|
---|
59 | { "unsort", no_argument, 0, 'u' },
|
---|
60 | { "recursive", no_argument, 0, 'r' },
|
---|
61 | { "exact-size", no_argument, 0, 'e' },
|
---|
62 | { "single-column", no_argument, 0, '1' },
|
---|
63 | { 0, 0, 0, 0 }
|
---|
64 | };
|
---|
65 |
|
---|
66 | /* Prototypes for the ls command, excluding entry points. */
|
---|
67 | static unsigned int ls_start(ls_job_t *);
|
---|
68 | static bool ls_print(struct dir_elem_t *);
|
---|
69 | static bool ls_print_single_column(struct dir_elem_t *);
|
---|
70 | static int ls_cmp_type_name(const void *, const void *);
|
---|
71 | static int ls_cmp_name(const void *, const void *);
|
---|
72 | static signed int ls_scan_dir(const char *, DIR *, struct dir_elem_t **);
|
---|
73 | static unsigned int ls_recursive(const char *, DIR *);
|
---|
74 | static unsigned int ls_scope(const char *, struct dir_elem_t *);
|
---|
75 |
|
---|
76 | static unsigned int ls_start(ls_job_t *ls)
|
---|
77 | {
|
---|
78 | ls->recursive = 0;
|
---|
79 | ls->sort = 1;
|
---|
80 |
|
---|
81 | ls->exact_size = false;
|
---|
82 | ls->single_column = false;
|
---|
83 | ls->printer = ls_print;
|
---|
84 | return 1;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /** Print an entry.
|
---|
88 | *
|
---|
89 | * ls_print currently does nothing more than print the entry.
|
---|
90 | * In the future, we will likely pass the absolute path, and
|
---|
91 | * some sort of ls_options structure that controls how each
|
---|
92 | * entry is printed and what is printed about it.
|
---|
93 | *
|
---|
94 | * Now we just print basic DOS style lists.
|
---|
95 | *
|
---|
96 | * @param de Directory element.
|
---|
97 | */
|
---|
98 | static bool ls_print(struct dir_elem_t *de)
|
---|
99 | {
|
---|
100 | int width = 13;
|
---|
101 |
|
---|
102 | if (de->s.is_file) {
|
---|
103 | if (ls.exact_size) {
|
---|
104 | printf("%-40s\t%*llu\n", de->name, width, (long long) de->s.size);
|
---|
105 | } else {
|
---|
106 | cap_spec_t cap;
|
---|
107 |
|
---|
108 | cap_from_blocks(de->s.size, 1, &cap);
|
---|
109 | cap_simplify(&cap);
|
---|
110 |
|
---|
111 | char *rptr;
|
---|
112 | if (cap_format(&cap, &rptr) == EOK) {
|
---|
113 | char bytes[9], suffix[3];
|
---|
114 | sscanf(rptr, "%s %s", bytes, suffix);
|
---|
115 | free(rptr);
|
---|
116 |
|
---|
117 | printf("%-40s\t%*s %2s\n", de->name, width - 3, bytes, suffix);
|
---|
118 | } else
|
---|
119 | return false;
|
---|
120 | }
|
---|
121 | } else if (de->s.is_directory)
|
---|
122 | printf("%-40s\t%*s\n", de->name, width, "<dir>");
|
---|
123 | else
|
---|
124 | printf("%-40s\n", de->name);
|
---|
125 |
|
---|
126 | return true;
|
---|
127 | }
|
---|
128 |
|
---|
129 | static bool ls_print_single_column(struct dir_elem_t *de)
|
---|
130 | {
|
---|
131 | if (de->s.is_file) {
|
---|
132 | printf("%s\n", de->name);
|
---|
133 | } else {
|
---|
134 | printf("%s/\n", de->name);
|
---|
135 | }
|
---|
136 |
|
---|
137 | return true;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** Compare 2 directory elements.
|
---|
141 | *
|
---|
142 | * It compares 2 elements of a directory : a file is considered
|
---|
143 | * as bigger than a directory, and if they have the same type,
|
---|
144 | * they are compared alphabetically.
|
---|
145 | *
|
---|
146 | * @param a Pointer to the structure of the first element.
|
---|
147 | * @param b Pointer to the structure of the second element.
|
---|
148 | *
|
---|
149 | * @return -1 if a < b, 1 otherwise.
|
---|
150 | */
|
---|
151 | static int ls_cmp_type_name(const void *a, const void *b)
|
---|
152 | {
|
---|
153 | struct dir_elem_t const *da = a;
|
---|
154 | struct dir_elem_t const *db = b;
|
---|
155 |
|
---|
156 | if ((da->s.is_directory && db->s.is_file) ||
|
---|
157 | ((da->s.is_directory == db->s.is_directory) &&
|
---|
158 | str_cmp(da->name, db->name) < 0))
|
---|
159 | return -1;
|
---|
160 | else
|
---|
161 | return 1;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /** Compare directories/files per name
|
---|
165 | *
|
---|
166 | * This comparision ignores the type of
|
---|
167 | * the node. Sorted will strictly by name.
|
---|
168 | *
|
---|
169 | */
|
---|
170 | static int ls_cmp_name(const void *a, const void *b)
|
---|
171 | {
|
---|
172 | struct dir_elem_t const *da = a;
|
---|
173 | struct dir_elem_t const *db = b;
|
---|
174 |
|
---|
175 | return str_cmp(da->name, db->name);
|
---|
176 | }
|
---|
177 |
|
---|
178 | /** Scan a directory.
|
---|
179 | *
|
---|
180 | * Scan the content of a directory and print it.
|
---|
181 | *
|
---|
182 | * @param d Name of the directory.
|
---|
183 | * @param dirp Directory stream.
|
---|
184 | * @param sort 1 if the output must be sorted,
|
---|
185 | * 0 otherwise.
|
---|
186 | */
|
---|
187 | static signed int ls_scan_dir(const char *d, DIR *dirp,
|
---|
188 | struct dir_elem_t **dir_list_ptr)
|
---|
189 | {
|
---|
190 | int alloc_blocks = 20;
|
---|
191 | int i;
|
---|
192 | int nbdirs = 0;
|
---|
193 | errno_t rc;
|
---|
194 | int len;
|
---|
195 | char *buff;
|
---|
196 | struct dir_elem_t *tmp;
|
---|
197 | struct dir_elem_t *tosort;
|
---|
198 | struct dirent *dp;
|
---|
199 |
|
---|
200 | if (!dirp)
|
---|
201 | return -1;
|
---|
202 |
|
---|
203 | buff = (char *) malloc(PATH_MAX);
|
---|
204 | if (!buff) {
|
---|
205 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
---|
206 | return -1;
|
---|
207 | }
|
---|
208 |
|
---|
209 | tosort = (struct dir_elem_t *) malloc(alloc_blocks * sizeof(*tosort));
|
---|
210 | if (!tosort) {
|
---|
211 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
---|
212 | free(buff);
|
---|
213 | return -1;
|
---|
214 | }
|
---|
215 |
|
---|
216 | while ((dp = readdir(dirp))) {
|
---|
217 | if (nbdirs + 1 > alloc_blocks) {
|
---|
218 | alloc_blocks += alloc_blocks;
|
---|
219 |
|
---|
220 | tmp = (struct dir_elem_t *) realloc(tosort,
|
---|
221 | alloc_blocks * sizeof(struct dir_elem_t));
|
---|
222 | if (!tmp) {
|
---|
223 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
---|
224 | goto out;
|
---|
225 | }
|
---|
226 | tosort = tmp;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /* fill the name field */
|
---|
230 | tosort[nbdirs].name = (char *) malloc(str_size(dp->d_name) + 1);
|
---|
231 | if (!tosort[nbdirs].name) {
|
---|
232 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
---|
233 | goto out;
|
---|
234 | }
|
---|
235 |
|
---|
236 | str_cpy(tosort[nbdirs].name, str_size(dp->d_name) + 1, dp->d_name);
|
---|
237 | len = snprintf(buff, PATH_MAX - 1, "%s/%s", d, tosort[nbdirs].name);
|
---|
238 | buff[len] = '\0';
|
---|
239 |
|
---|
240 | rc = vfs_stat_path(buff, &tosort[nbdirs++].s);
|
---|
241 | if (rc != EOK) {
|
---|
242 | printf("ls: skipping bogus node %s\n", buff);
|
---|
243 | printf("error=%s\n", str_error_name(rc));
|
---|
244 | goto out;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | if (ls.sort) {
|
---|
249 | int (*compar)(const void *, const void *);
|
---|
250 | compar = ls.single_column ? ls_cmp_name : ls_cmp_type_name;
|
---|
251 | qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t), compar);
|
---|
252 | }
|
---|
253 |
|
---|
254 | for (i = 0; i < nbdirs; i++) {
|
---|
255 | if (!ls.printer(&tosort[i])) {
|
---|
256 | cli_error(CL_ENOMEM, "%s: Out of memory", cmdname);
|
---|
257 | goto out;
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | /* Populate the directory list. */
|
---|
262 | if (ls.recursive) {
|
---|
263 | tmp = (struct dir_elem_t *) realloc(*dir_list_ptr,
|
---|
264 | nbdirs * sizeof(struct dir_elem_t));
|
---|
265 | if (!tmp) {
|
---|
266 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
---|
267 | goto out;
|
---|
268 | }
|
---|
269 | *dir_list_ptr = tmp;
|
---|
270 |
|
---|
271 | for (i = 0; i < nbdirs; i++) {
|
---|
272 | (*dir_list_ptr)[i].name = str_dup(tosort[i].name);
|
---|
273 | if (!(*dir_list_ptr)[i].name) {
|
---|
274 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
---|
275 | goto out;
|
---|
276 | }
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | out:
|
---|
281 | for (i = 0; i < nbdirs; i++)
|
---|
282 | free(tosort[i].name);
|
---|
283 | free(tosort);
|
---|
284 | free(buff);
|
---|
285 |
|
---|
286 | return nbdirs;
|
---|
287 | }
|
---|
288 |
|
---|
289 | /** Visit a directory recursively.
|
---|
290 | *
|
---|
291 | * ls_recursive visits all the subdirectories recursively and
|
---|
292 | * prints the files and directories in them.
|
---|
293 | *
|
---|
294 | * @param path Path the current directory being visited.
|
---|
295 | * @param dirp Directory stream.
|
---|
296 | */
|
---|
297 | static unsigned int ls_recursive(const char *path, DIR *dirp)
|
---|
298 | {
|
---|
299 | int i, nbdirs, ret;
|
---|
300 | unsigned int scope;
|
---|
301 | char *subdir_path;
|
---|
302 | DIR *subdirp;
|
---|
303 | struct dir_elem_t *dir_list;
|
---|
304 |
|
---|
305 | const char *const trailing_slash = "/";
|
---|
306 |
|
---|
307 | nbdirs = 0;
|
---|
308 | dir_list = (struct dir_elem_t *) malloc(sizeof(struct dir_elem_t));
|
---|
309 |
|
---|
310 | printf("\n%s:\n", path);
|
---|
311 |
|
---|
312 | subdir_path = (char *) malloc(PATH_MAX);
|
---|
313 | if (!subdir_path) {
|
---|
314 | ret = CMD_FAILURE;
|
---|
315 | goto out;
|
---|
316 | }
|
---|
317 |
|
---|
318 | nbdirs = ls_scan_dir(path, dirp, &dir_list);
|
---|
319 | if (nbdirs == -1) {
|
---|
320 | ret = CMD_FAILURE;
|
---|
321 | goto out;
|
---|
322 | }
|
---|
323 |
|
---|
324 | for (i = 0; i < nbdirs; ++i) {
|
---|
325 | memset(subdir_path, 0, PATH_MAX);
|
---|
326 |
|
---|
327 | if (str_size(subdir_path) + str_size(path) + 1 <= PATH_MAX)
|
---|
328 | str_append(subdir_path, PATH_MAX, path);
|
---|
329 | if (path[str_size(path) - 1] != '/' &&
|
---|
330 | str_size(subdir_path) + str_size(trailing_slash) + 1 <= PATH_MAX)
|
---|
331 | str_append(subdir_path, PATH_MAX, trailing_slash);
|
---|
332 | if (str_size(subdir_path) +
|
---|
333 | str_size(dir_list[i].name) + 1 <= PATH_MAX)
|
---|
334 | str_append(subdir_path, PATH_MAX, dir_list[i].name);
|
---|
335 |
|
---|
336 | scope = ls_scope(subdir_path, &dir_list[i]);
|
---|
337 | switch (scope) {
|
---|
338 | case LS_FILE:
|
---|
339 | break;
|
---|
340 | case LS_DIR:
|
---|
341 | subdirp = opendir(subdir_path);
|
---|
342 | if (!subdirp) {
|
---|
343 | /* May have been deleted between scoping it and opening it */
|
---|
344 | cli_error(CL_EFAIL, "Could not stat %s", dir_list[i].name);
|
---|
345 | ret = CMD_FAILURE;
|
---|
346 | goto out;
|
---|
347 | }
|
---|
348 |
|
---|
349 | ret = ls_recursive(subdir_path, subdirp);
|
---|
350 | closedir(subdirp);
|
---|
351 | if (ret == CMD_FAILURE)
|
---|
352 | goto out;
|
---|
353 | break;
|
---|
354 | case LS_BOGUS:
|
---|
355 | ret = CMD_FAILURE;
|
---|
356 | goto out;
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | ret = CMD_SUCCESS;
|
---|
361 |
|
---|
362 | out:
|
---|
363 | for (i = 0; i < nbdirs; i++)
|
---|
364 | free(dir_list[i].name);
|
---|
365 | free(dir_list);
|
---|
366 | free(subdir_path);
|
---|
367 |
|
---|
368 | return ret;
|
---|
369 | }
|
---|
370 |
|
---|
371 | static unsigned int ls_scope(const char *path, struct dir_elem_t *de)
|
---|
372 | {
|
---|
373 | if (vfs_stat_path(path, &de->s) != EOK) {
|
---|
374 | cli_error(CL_ENOENT, "%s", path);
|
---|
375 | return LS_BOGUS;
|
---|
376 | }
|
---|
377 |
|
---|
378 | if (de->s.is_file)
|
---|
379 | return LS_FILE;
|
---|
380 | else if (de->s.is_directory)
|
---|
381 | return LS_DIR;
|
---|
382 |
|
---|
383 | return LS_BOGUS;
|
---|
384 | }
|
---|
385 |
|
---|
386 | void help_cmd_ls(unsigned int level)
|
---|
387 | {
|
---|
388 | if (level == HELP_SHORT) {
|
---|
389 | printf("`%s' lists files and directories.\n", cmdname);
|
---|
390 | } else {
|
---|
391 | help_cmd_ls(HELP_SHORT);
|
---|
392 | printf(
|
---|
393 | "Usage: %s [options] [path]\n"
|
---|
394 | "If not path is given, the current working directory is used.\n"
|
---|
395 | "Options:\n"
|
---|
396 | " -h, --help A short option summary\n"
|
---|
397 | " -u, --unsort Do not sort directory entries\n"
|
---|
398 | " -r, --recursive List subdirectories recursively\n"
|
---|
399 | " -e, --exact-size File sizes will be unformatted (raw bytes count)\n"
|
---|
400 | " -1, --single-column Only the names will be returned\n",
|
---|
401 | cmdname);
|
---|
402 | }
|
---|
403 |
|
---|
404 | return;
|
---|
405 | }
|
---|
406 |
|
---|
407 | int cmd_ls(char **argv)
|
---|
408 | {
|
---|
409 | unsigned int argc;
|
---|
410 | struct dir_elem_t de;
|
---|
411 | DIR *dirp;
|
---|
412 | int c, opt_ind;
|
---|
413 | int ret = 0;
|
---|
414 | unsigned int scope;
|
---|
415 |
|
---|
416 | if (!ls_start(&ls)) {
|
---|
417 | cli_error(CL_EFAIL, "%s: Could not initialize", cmdname);
|
---|
418 | return CMD_FAILURE;
|
---|
419 | }
|
---|
420 |
|
---|
421 | argc = cli_count_args(argv);
|
---|
422 |
|
---|
423 | c = 0;
|
---|
424 | optreset = 1;
|
---|
425 | optind = 0;
|
---|
426 | opt_ind = 0;
|
---|
427 |
|
---|
428 | while (c != -1) {
|
---|
429 | c = getopt_long(argc, argv, "hure1", long_options, &opt_ind);
|
---|
430 | switch (c) {
|
---|
431 | case 'h':
|
---|
432 | help_cmd_ls(HELP_LONG);
|
---|
433 | return CMD_SUCCESS;
|
---|
434 | case 'u':
|
---|
435 | ls.sort = 0;
|
---|
436 | break;
|
---|
437 | case 'r':
|
---|
438 | ls.recursive = 1;
|
---|
439 | break;
|
---|
440 | case 'e':
|
---|
441 | ls.exact_size = true;
|
---|
442 | break;
|
---|
443 | case '1':
|
---|
444 | ls.single_column = true;
|
---|
445 | ls.printer = ls_print_single_column;
|
---|
446 | break;
|
---|
447 | }
|
---|
448 | }
|
---|
449 |
|
---|
450 | argc -= optind;
|
---|
451 |
|
---|
452 | de.name = (char *) malloc(PATH_MAX);
|
---|
453 | if (!de.name) {
|
---|
454 | cli_error(CL_ENOMEM, "%s: Out of memory", cmdname);
|
---|
455 | return CMD_FAILURE;
|
---|
456 | }
|
---|
457 | memset(de.name, 0, PATH_MAX);
|
---|
458 |
|
---|
459 | if (argc == 0) {
|
---|
460 | if (vfs_cwd_get(de.name, PATH_MAX) != EOK) {
|
---|
461 | cli_error(CL_EFAIL, "%s: Failed determining working "
|
---|
462 | "directory", cmdname);
|
---|
463 | return CMD_FAILURE;
|
---|
464 | }
|
---|
465 | } else {
|
---|
466 | str_cpy(de.name, PATH_MAX, argv[optind]);
|
---|
467 | }
|
---|
468 |
|
---|
469 | scope = ls_scope(de.name, &de);
|
---|
470 | switch (scope) {
|
---|
471 | case LS_FILE:
|
---|
472 | if (!ls.printer(&de)) {
|
---|
473 | cli_error(CL_ENOMEM, "%s: Out of memory", cmdname);
|
---|
474 | return CMD_FAILURE;
|
---|
475 | }
|
---|
476 | break;
|
---|
477 | case LS_DIR:
|
---|
478 | dirp = opendir(de.name);
|
---|
479 | if (!dirp) {
|
---|
480 | /* May have been deleted between scoping it and opening it */
|
---|
481 | cli_error(CL_EFAIL, "Could not stat %s", de.name);
|
---|
482 | free(de.name);
|
---|
483 | return CMD_FAILURE;
|
---|
484 | }
|
---|
485 | if (ls.recursive)
|
---|
486 | ret = ls_recursive(de.name, dirp);
|
---|
487 | else
|
---|
488 | ret = ls_scan_dir(de.name, dirp, NULL);
|
---|
489 |
|
---|
490 | closedir(dirp);
|
---|
491 | break;
|
---|
492 | case LS_BOGUS:
|
---|
493 | return CMD_FAILURE;
|
---|
494 | }
|
---|
495 |
|
---|
496 | free(de.name);
|
---|
497 |
|
---|
498 | if (ret == -1 || ret == CMD_FAILURE)
|
---|
499 | return CMD_FAILURE;
|
---|
500 | else
|
---|
501 | return CMD_SUCCESS;
|
---|
502 | }
|
---|