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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d96d9bc was d96d9bc, checked in by Jakub Jermar <jakub@…>, 9 years ago

Rename chdir() to vfs_cwd_set() and getcwd() to vfs_cwd_get()

  • Property mode set to 100644
File size: 10.3 KB
Line 
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/* 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
33#include <errno.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <dirent.h>
37#include <getopt.h>
38#include <sys/types.h>
39#include <vfs/vfs.h>
40#include <str.h>
41#include <sort.h>
42
43#include "ls.h"
44#include "errors.h"
45#include "config.h"
46#include "util.h"
47#include "entry.h"
48#include "cmds.h"
49
50static const char *cmdname = "ls";
51
52static ls_job_t ls;
53
54static struct option const long_options[] = {
55 { "help", no_argument, 0, 'h' },
56 { "unsort", no_argument, 0, 'u' },
57 { "recursive", no_argument, 0, 'r' },
58 { 0, 0, 0, 0 }
59};
60
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 *);
64static int ls_cmp(void *, void *, void *);
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
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
98/** Compare 2 directory elements.
99 *
100 * It compares 2 elements of a directory : a file is considered
101 * as bigger than a directory, and if they have the same type,
102 * they are compared alphabetically.
103 *
104 * @param a Pointer to the structure of the first element.
105 * @param b Pointer to the structure of the second element.
106 * @param arg Pointer for an other and optionnal argument.
107 *
108 * @return -1 if a < b, 1 otherwise.
109 */
110static int ls_cmp(void *a, void *b, void *arg)
111{
112 struct dir_elem_t *da = a;
113 struct dir_elem_t *db = b;
114
115 if ((da->s.is_directory && db->s.is_file) ||
116 ((da->s.is_directory == db->s.is_directory) &&
117 str_cmp(da->name, db->name) < 0))
118 return -1;
119 else
120 return 1;
121}
122
123/** Scan a directory.
124 *
125 * Scan the content of a directory and print it.
126 *
127 * @param d Name of the directory.
128 * @param dirp Directory stream.
129 * @param sort 1 if the output must be sorted,
130 * 0 otherwise.
131 */
132static signed int ls_scan_dir(const char *d, DIR *dirp,
133 struct dir_elem_t **dir_list_ptr)
134{
135 int alloc_blocks = 20;
136 int i;
137 int nbdirs = 0;
138 int rc;
139 int len;
140 char *buff;
141 struct dir_elem_t *tmp;
142 struct dir_elem_t *tosort;
143 struct dirent *dp;
144
145 if (!dirp)
146 return -1;
147
148 buff = (char *) malloc(PATH_MAX);
149 if (!buff) {
150 cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
151 return -1;
152 }
153
154 tosort = (struct dir_elem_t *) malloc(alloc_blocks * sizeof(*tosort));
155 if (!tosort) {
156 cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
157 free(buff);
158 return -1;
159 }
160
161 while ((dp = readdir(dirp))) {
162 if (nbdirs + 1 > alloc_blocks) {
163 alloc_blocks += alloc_blocks;
164
165 tmp = (struct dir_elem_t *) realloc(tosort,
166 alloc_blocks * sizeof(struct dir_elem_t));
167 if (!tmp) {
168 cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
169 goto out;
170 }
171 tosort = tmp;
172 }
173
174 /* fill the name field */
175 tosort[nbdirs].name = (char *) malloc(str_size(dp->d_name) + 1);
176 if (!tosort[nbdirs].name) {
177 cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
178 goto out;
179 }
180
181 str_cpy(tosort[nbdirs].name, str_size(dp->d_name) + 1, dp->d_name);
182 len = snprintf(buff, PATH_MAX - 1, "%s/%s", d, tosort[nbdirs].name);
183 buff[len] = '\0';
184
185 rc = vfs_stat_path(buff, &tosort[nbdirs++].s);
186 if (rc != EOK) {
187 printf("ls: skipping bogus node %s\n", buff);
188 printf("error=%d\n", rc);
189 goto out;
190 }
191 }
192
193 if (ls.sort) {
194 if (!qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t),
195 ls_cmp, NULL)) {
196 printf("Sorting error.\n");
197 }
198 }
199
200 for (i = 0; i < nbdirs; i++)
201 ls_print(&tosort[i]);
202
203 /* Populate the directory list. */
204 if (ls.recursive) {
205 tmp = (struct dir_elem_t *) realloc(*dir_list_ptr,
206 nbdirs * sizeof(struct dir_elem_t));
207 if (!tmp) {
208 cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
209 goto out;
210 }
211 *dir_list_ptr = tmp;
212
213 for (i = 0; i < nbdirs; i++) {
214 (*dir_list_ptr)[i].name = str_dup(tosort[i].name);
215 if (!(*dir_list_ptr)[i].name) {
216 cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
217 goto out;
218 }
219 }
220 }
221
222out:
223 for(i = 0; i < nbdirs; i++)
224 free(tosort[i].name);
225 free(tosort);
226 free(buff);
227
228 return nbdirs;
229}
230
231/** Visit a directory recursively.
232 *
233 * ls_recursive visits all the subdirectories recursively and
234 * prints the files and directories in them.
235 *
236 * @param path Path the current directory being visited.
237 * @param dirp Directory stream.
238 */
239static unsigned int ls_recursive(const char *path, DIR *dirp)
240{
241 int i, nbdirs, ret;
242 unsigned int scope;
243 char *subdir_path;
244 DIR *subdirp;
245 struct dir_elem_t *dir_list;
246
247 const char * const trailing_slash = "/";
248
249 nbdirs = 0;
250 dir_list = (struct dir_elem_t *) malloc(sizeof(struct dir_elem_t));
251
252 printf("\n%s:\n", path);
253
254 subdir_path = (char *) malloc(PATH_MAX);
255 if (!subdir_path) {
256 ret = CMD_FAILURE;
257 goto out;
258 }
259
260 nbdirs = ls_scan_dir(path, dirp, &dir_list);
261 if (nbdirs == -1) {
262 ret = CMD_FAILURE;
263 goto out;
264 }
265
266 for (i = 0; i < nbdirs; ++i) {
267 memset(subdir_path, 0, PATH_MAX);
268
269 if (str_size(subdir_path) + str_size(path) + 1 <= PATH_MAX)
270 str_append(subdir_path, PATH_MAX, path);
271 if (path[str_size(path)-1] != '/' &&
272 str_size(subdir_path) + str_size(trailing_slash) + 1 <= PATH_MAX)
273 str_append(subdir_path, PATH_MAX, trailing_slash);
274 if (str_size(subdir_path) +
275 str_size(dir_list[i].name) + 1 <= PATH_MAX)
276 str_append(subdir_path, PATH_MAX, dir_list[i].name);
277
278 scope = ls_scope(subdir_path, &dir_list[i]);
279 switch (scope) {
280 case LS_FILE:
281 break;
282 case LS_DIR:
283 subdirp = opendir(subdir_path);
284 if (!subdirp) {
285 /* May have been deleted between scoping it and opening it */
286 cli_error(CL_EFAIL, "Could not stat %s", dir_list[i].name);
287 ret = CMD_FAILURE;
288 goto out;
289 }
290
291 ret = ls_recursive(subdir_path, subdirp);
292 closedir(subdirp);
293 if (ret == CMD_FAILURE)
294 goto out;
295 break;
296 case LS_BOGUS:
297 ret = CMD_FAILURE;
298 goto out;
299 }
300 }
301
302 ret = CMD_SUCCESS;
303
304out:
305 for (i = 0; i < nbdirs; i++)
306 free(dir_list[i].name);
307 free(dir_list);
308 free(subdir_path);
309
310 return ret;
311}
312
313static unsigned int ls_scope(const char *path, struct dir_elem_t *de)
314{
315 if (vfs_stat_path(path, &de->s) != EOK) {
316 cli_error(CL_ENOENT, "%s", path);
317 return LS_BOGUS;
318 }
319
320 if (de->s.is_file)
321 return LS_FILE;
322 else if (de->s.is_directory)
323 return LS_DIR;
324
325 return LS_BOGUS;
326}
327
328void help_cmd_ls(unsigned int level)
329{
330 if (level == HELP_SHORT) {
331 printf("`%s' lists files and directories.\n", cmdname);
332 } else {
333 help_cmd_ls(HELP_SHORT);
334 printf(
335 "Usage: %s [options] [path]\n"
336 "If not path is given, the current working directory is used.\n"
337 "Options:\n"
338 " -h, --help A short option summary\n"
339 " -u, --unsort Do not sort directory entries\n"
340 " -r, --recursive List subdirectories recursively\n",
341 cmdname);
342 }
343
344 return;
345}
346
347int cmd_ls(char **argv)
348{
349 unsigned int argc;
350 struct dir_elem_t de;
351 DIR *dirp;
352 int c, opt_ind;
353 int ret = 0;
354 unsigned int scope;
355
356 if (!ls_start(&ls)) {
357 cli_error(CL_EFAIL, "%s: Could not initialize", cmdname);
358 return CMD_FAILURE;
359 }
360
361 argc = cli_count_args(argv);
362
363 for (c = 0, optreset = 1, optind = 0, opt_ind = 0; c != -1;) {
364 c = getopt_long(argc, argv, "hur", long_options, &opt_ind);
365 switch (c) {
366 case 'h':
367 help_cmd_ls(HELP_LONG);
368 return CMD_SUCCESS;
369 case 'u':
370 ls.sort = 0;
371 break;
372 case 'r':
373 ls.recursive = 1;
374 break;
375 }
376 }
377
378 argc -= optind;
379
380 de.name = (char *) malloc(PATH_MAX);
381 if (!de.name) {
382 cli_error(CL_ENOMEM, "%s: Out of memory", cmdname);
383 return CMD_FAILURE;
384 }
385 memset(de.name, 0, PATH_MAX);
386
387 if (argc == 0) {
388 if (vfs_cwd_get(de.name, PATH_MAX) != EOK) {
389 cli_error(CL_EFAIL, "%s: Failed determining working "
390 "directory", cmdname);
391 return CMD_FAILURE;
392 }
393 } else {
394 str_cpy(de.name, PATH_MAX, argv[optind]);
395 }
396
397 scope = ls_scope(de.name, &de);
398 switch (scope) {
399 case LS_FILE:
400 ls_print(&de);
401 break;
402 case LS_DIR:
403 dirp = opendir(de.name);
404 if (!dirp) {
405 /* May have been deleted between scoping it and opening it */
406 cli_error(CL_EFAIL, "Could not stat %s", de.name);
407 free(de.name);
408 return CMD_FAILURE;
409 }
410 if (ls.recursive)
411 ret = ls_recursive(de.name, dirp);
412 else
413 ret = ls_scan_dir(de.name, dirp, NULL);
414
415 closedir(dirp);
416 break;
417 case LS_BOGUS:
418 return CMD_FAILURE;
419 }
420
421 free(de.name);
422
423 if (ret == -1 || ret == CMD_FAILURE)
424 return CMD_FAILURE;
425 else
426 return CMD_SUCCESS;
427}
428
Note: See TracBrowser for help on using the repository browser.