source: mainline/uspace/app/bdsh/cmds/modules/ls/ls.c@ 2c4e1cc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2c4e1cc was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 10.2 KB
RevLine 
[36ab7c7]1/*
2 * Copyright (c) 2008 Tim Post
[216d6fc]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
[36ab7c7]6 * modification, are permitted provided that the following conditions
7 * are met:
[216d6fc]8 *
[36ab7c7]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.
[216d6fc]16 *
[36ab7c7]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.
[216d6fc]27 */
28
29/* NOTE:
30 * This is a bit of an ugly hack, working around the absence of fstat / etc.
31 * As more stuff is completed and exposed in libc, this will improve */
32
[6afc9d7]33#include <errno.h>
[dd8ab1c]34#include <str_error.h>
[216d6fc]35#include <stdio.h>
36#include <stdlib.h>
[8d2dd7f2]37#include <stdint.h>
[216d6fc]38#include <dirent.h>
[e3a3e3ad]39#include <getopt.h>
[23a0368]40#include <vfs/vfs.h>
[19f857a]41#include <str.h>
[216d6fc]42
[4567b31c]43#include "ls.h"
[216d6fc]44#include "errors.h"
45#include "config.h"
46#include "util.h"
47#include "entry.h"
48#include "cmds.h"
49
[a000878c]50static const char *cmdname = "ls";
[216d6fc]51
[4567b31c]52static ls_job_t ls;
53
[e3a3e3ad]54static struct option const long_options[] = {
55 { "help", no_argument, 0, 'h' },
[cfc3027]56 { "unsort", no_argument, 0, 'u' },
[9bdf1f2a]57 { "recursive", no_argument, 0, 'r' },
[e3a3e3ad]58 { 0, 0, 0, 0 }
59};
60
[4567b31c]61/* Prototypes for the ls command, excluding entry points. */
62static unsigned int ls_start(ls_job_t *);
63static void ls_print(struct dir_elem_t *);
[f2460a50]64static int ls_cmp(const void *, const void *);
[4567b31c]65static signed int ls_scan_dir(const char *, DIR *, struct dir_elem_t **);
66static unsigned int ls_recursive(const char *, DIR *);
67static unsigned int ls_scope(const char *, struct dir_elem_t *);
68
69static unsigned int ls_start(ls_job_t *ls)
70{
71 ls->recursive = 0;
72 ls->sort = 1;
73
74 return 1;
75}
76
[ba63912]77/** Print an entry.
78 *
79 * ls_print currently does nothing more than print the entry.
80 * In the future, we will likely pass the absolute path, and
81 * some sort of ls_options structure that controls how each
82 * entry is printed and what is printed about it.
83 *
84 * Now we just print basic DOS style lists.
85 *
86 * @param de Directory element.
87 */
88static void ls_print(struct dir_elem_t *de)
89{
90 if (de->s.is_file)
91 printf("%-40s\t%llu\n", de->name, (long long) de->s.size);
92 else if (de->s.is_directory)
93 printf("%-40s\t<dir>\n", de->name);
94 else
95 printf("%-40s\n", de->name);
96}
97
[e3a3e3ad]98/** Compare 2 directory elements.
[a716a75]99 *
[e3a3e3ad]100 * It compares 2 elements of a directory : a file is considered
[ba63912]101 * as bigger than a directory, and if they have the same type,
[e3a3e3ad]102 * they are compared alphabetically.
[a716a75]103 *
[e3a3e3ad]104 * @param a Pointer to the structure of the first element.
105 * @param b Pointer to the structure of the second element.
[a716a75]106 *
[ba63912]107 * @return -1 if a < b, 1 otherwise.
[a716a75]108 */
[f2460a50]109static int ls_cmp(const void *a, const void *b)
[a716a75]110{
[f2460a50]111 struct dir_elem_t const *da = a;
112 struct dir_elem_t const *db = b;
[a35b458]113
[ba63912]114 if ((da->s.is_directory && db->s.is_file) ||
115 ((da->s.is_directory == db->s.is_directory) &&
116 str_cmp(da->name, db->name) < 0))
[e3a3e3ad]117 return -1;
118 else
119 return 1;
[a716a75]120}
121
122/** Scan a directory.
123 *
124 * Scan the content of a directory and print it.
125 *
126 * @param d Name of the directory.
127 * @param dirp Directory stream.
[1e2629f]128 * @param sort 1 if the output must be sorted,
129 * 0 otherwise.
[a716a75]130 */
[1b20da0]131static signed int ls_scan_dir(const char *d, DIR *dirp,
[4567b31c]132 struct dir_elem_t **dir_list_ptr)
[216d6fc]133{
[a716a75]134 int alloc_blocks = 20;
[ba63912]135 int i;
[a716a75]136 int nbdirs = 0;
[b7fd2a0]137 errno_t rc;
[ba63912]138 int len;
139 char *buff;
140 struct dir_elem_t *tmp;
141 struct dir_elem_t *tosort;
142 struct dirent *dp;
[a35b458]143
[ba63912]144 if (!dirp)
[4567b31c]145 return -1;
[216d6fc]146
[ba63912]147 buff = (char *) malloc(PATH_MAX);
148 if (!buff) {
[216d6fc]149 cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
[4567b31c]150 return -1;
[216d6fc]151 }
[a35b458]152
[ba63912]153 tosort = (struct dir_elem_t *) malloc(alloc_blocks * sizeof(*tosort));
154 if (!tosort) {
[a716a75]155 cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
[e3a3e3ad]156 free(buff);
[4567b31c]157 return -1;
[a716a75]158 }
[a35b458]159
[216d6fc]160 while ((dp = readdir(dirp))) {
[ba63912]161 if (nbdirs + 1 > alloc_blocks) {
[a716a75]162 alloc_blocks += alloc_blocks;
[a35b458]163
[ba63912]164 tmp = (struct dir_elem_t *) realloc(tosort,
165 alloc_blocks * sizeof(struct dir_elem_t));
166 if (!tmp) {
[a716a75]167 cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
[ba63912]168 goto out;
[a716a75]169 }
170 tosort = tmp;
171 }
[a35b458]172
[ba63912]173 /* fill the name field */
[a701812]174 tosort[nbdirs].name = (char *) malloc(str_size(dp->d_name) + 1);
[ba63912]175 if (!tosort[nbdirs].name) {
[e3a3e3ad]176 cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
[ba63912]177 goto out;
[e3a3e3ad]178 }
[a701812]179
180 str_cpy(tosort[nbdirs].name, str_size(dp->d_name) + 1, dp->d_name);
[ba63912]181 len = snprintf(buff, PATH_MAX - 1, "%s/%s", d, tosort[nbdirs].name);
182 buff[len] = '\0';
183
[23a0368]184 rc = vfs_stat_path(buff, &tosort[nbdirs++].s);
185 if (rc != EOK) {
[e3a3e3ad]186 printf("ls: skipping bogus node %s\n", buff);
[dd8ab1c]187 printf("error=%s\n", str_error_name(rc));
[ba63912]188 goto out;
[a716a75]189 }
190 }
[a35b458]191
[f2460a50]192 if (ls.sort)
193 qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t), ls_cmp);
[a35b458]194
[ba63912]195 for (i = 0; i < nbdirs; i++)
196 ls_print(&tosort[i]);
[4567b31c]197
198 /* Populate the directory list. */
199 if (ls.recursive) {
[1b20da0]200 tmp = (struct dir_elem_t *) realloc(*dir_list_ptr,
[4567b31c]201 nbdirs * sizeof(struct dir_elem_t));
202 if (!tmp) {
203 cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
204 goto out;
[1b20da0]205 }
[4567b31c]206 *dir_list_ptr = tmp;
207
208 for (i = 0; i < nbdirs; i++) {
209 (*dir_list_ptr)[i].name = str_dup(tosort[i].name);
210 if (!(*dir_list_ptr)[i].name) {
211 cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
212 goto out;
213 }
214 }
215 }
[a35b458]216
[ba63912]217out:
[1433ecda]218 for (i = 0; i < nbdirs; i++)
[ba63912]219 free(tosort[i].name);
[a716a75]220 free(tosort);
[216d6fc]221 free(buff);
[4567b31c]222
223 return nbdirs;
224}
225
226/** Visit a directory recursively.
227 *
228 * ls_recursive visits all the subdirectories recursively and
229 * prints the files and directories in them.
230 *
231 * @param path Path the current directory being visited.
232 * @param dirp Directory stream.
233 */
234static unsigned int ls_recursive(const char *path, DIR *dirp)
235{
236 int i, nbdirs, ret;
237 unsigned int scope;
238 char *subdir_path;
239 DIR *subdirp;
240 struct dir_elem_t *dir_list;
[a35b458]241
[1433ecda]242 const char *const trailing_slash = "/";
[4567b31c]243
244 nbdirs = 0;
245 dir_list = (struct dir_elem_t *) malloc(sizeof(struct dir_elem_t));
246
247 printf("\n%s:\n", path);
248
249 subdir_path = (char *) malloc(PATH_MAX);
250 if (!subdir_path) {
251 ret = CMD_FAILURE;
252 goto out;
253 }
254
[1b20da0]255 nbdirs = ls_scan_dir(path, dirp, &dir_list);
[4567b31c]256 if (nbdirs == -1) {
257 ret = CMD_FAILURE;
258 goto out;
259 }
260
261 for (i = 0; i < nbdirs; ++i) {
262 memset(subdir_path, 0, PATH_MAX);
263
264 if (str_size(subdir_path) + str_size(path) + 1 <= PATH_MAX)
265 str_append(subdir_path, PATH_MAX, path);
[1433ecda]266 if (path[str_size(path) - 1] != '/' &&
[4567b31c]267 str_size(subdir_path) + str_size(trailing_slash) + 1 <= PATH_MAX)
268 str_append(subdir_path, PATH_MAX, trailing_slash);
269 if (str_size(subdir_path) +
270 str_size(dir_list[i].name) + 1 <= PATH_MAX)
271 str_append(subdir_path, PATH_MAX, dir_list[i].name);
272
273 scope = ls_scope(subdir_path, &dir_list[i]);
274 switch (scope) {
275 case LS_FILE:
276 break;
277 case LS_DIR:
278 subdirp = opendir(subdir_path);
279 if (!subdirp) {
280 /* May have been deleted between scoping it and opening it */
281 cli_error(CL_EFAIL, "Could not stat %s", dir_list[i].name);
282 ret = CMD_FAILURE;
283 goto out;
284 }
285
286 ret = ls_recursive(subdir_path, subdirp);
287 closedir(subdirp);
288 if (ret == CMD_FAILURE)
289 goto out;
290 break;
291 case LS_BOGUS:
292 ret = CMD_FAILURE;
293 goto out;
[1b20da0]294 }
[4567b31c]295 }
[a35b458]296
[1b20da0]297 ret = CMD_SUCCESS;
[4567b31c]298
299out:
300 for (i = 0; i < nbdirs; i++)
301 free(dir_list[i].name);
302 free(dir_list);
303 free(subdir_path);
304
305 return ret;
306}
307
308static unsigned int ls_scope(const char *path, struct dir_elem_t *de)
309{
[23a0368]310 if (vfs_stat_path(path, &de->s) != EOK) {
[26c02b22]311 cli_error(CL_ENOENT, "%s", path);
[4567b31c]312 return LS_BOGUS;
313 }
314
315 if (de->s.is_file)
316 return LS_FILE;
317 else if (de->s.is_directory)
318 return LS_DIR;
319
320 return LS_BOGUS;
[216d6fc]321}
322
[809813d]323void help_cmd_ls(unsigned int level)
[216d6fc]324{
325 if (level == HELP_SHORT) {
326 printf("`%s' lists files and directories.\n", cmdname);
327 } else {
328 help_cmd_ls(HELP_SHORT);
[e3a3e3ad]329 printf(
[1433ecda]330 "Usage: %s [options] [path]\n"
331 "If not path is given, the current working directory is used.\n"
332 "Options:\n"
333 " -h, --help A short option summary\n"
334 " -u, --unsort Do not sort directory entries\n"
335 " -r, --recursive List subdirectories recursively\n",
336 cmdname);
[216d6fc]337 }
338
[809813d]339 return;
[216d6fc]340}
341
[809813d]342int cmd_ls(char **argv)
[216d6fc]343{
344 unsigned int argc;
[ba63912]345 struct dir_elem_t de;
[216d6fc]346 DIR *dirp;
[e3a3e3ad]347 int c, opt_ind;
[4567b31c]348 int ret = 0;
349 unsigned int scope;
350
351 if (!ls_start(&ls)) {
352 cli_error(CL_EFAIL, "%s: Could not initialize", cmdname);
353 return CMD_FAILURE;
354 }
[216d6fc]355
[43e02a6]356 argc = cli_count_args(argv);
[a35b458]357
[948222e4]358 c = 0;
359 optreset = 1;
360 optind = 0;
361 opt_ind = 0;
362
363 while (c != -1) {
[9bdf1f2a]364 c = getopt_long(argc, argv, "hur", long_options, &opt_ind);
[e3a3e3ad]365 switch (c) {
366 case 'h':
367 help_cmd_ls(HELP_LONG);
368 return CMD_SUCCESS;
369 case 'u':
[4567b31c]370 ls.sort = 0;
371 break;
[9bdf1f2a]372 case 'r':
[4567b31c]373 ls.recursive = 1;
[e3a3e3ad]374 break;
375 }
376 }
[6afc9d7]377
[cfc3027]378 argc -= optind;
[6afc9d7]379
[ba63912]380 de.name = (char *) malloc(PATH_MAX);
381 if (!de.name) {
[6afc9d7]382 cli_error(CL_ENOMEM, "%s: Out of memory", cmdname);
[216d6fc]383 return CMD_FAILURE;
384 }
[4567b31c]385 memset(de.name, 0, PATH_MAX);
[6afc9d7]386
387 if (argc == 0) {
[d96d9bc]388 if (vfs_cwd_get(de.name, PATH_MAX) != EOK) {
[6afc9d7]389 cli_error(CL_EFAIL, "%s: Failed determining working "
390 "directory", cmdname);
391 return CMD_FAILURE;
392 }
393 } else {
[cfc3027]394 str_cpy(de.name, PATH_MAX, argv[optind]);
[6afc9d7]395 }
[415c7e0d]396
[4567b31c]397 scope = ls_scope(de.name, &de);
398 switch (scope) {
399 case LS_FILE:
[ba63912]400 ls_print(&de);
[4567b31c]401 break;
402 case LS_DIR:
[ba63912]403 dirp = opendir(de.name);
[415c7e0d]404 if (!dirp) {
[216d6fc]405 /* May have been deleted between scoping it and opening it */
[ba63912]406 cli_error(CL_EFAIL, "Could not stat %s", de.name);
407 free(de.name);
[216d6fc]408 return CMD_FAILURE;
409 }
[4567b31c]410 if (ls.recursive)
411 ret = ls_recursive(de.name, dirp);
[1b20da0]412 else
[4567b31c]413 ret = ls_scan_dir(de.name, dirp, NULL);
414
[216d6fc]415 closedir(dirp);
[4567b31c]416 break;
417 case LS_BOGUS:
418 return CMD_FAILURE;
[216d6fc]419 }
420
[ba63912]421 free(de.name);
[216d6fc]422
[4567b31c]423 if (ret == -1 || ret == CMD_FAILURE)
424 return CMD_FAILURE;
425 else
426 return CMD_SUCCESS;
[216d6fc]427}
428
Note: See TracBrowser for help on using the repository browser.