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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2d1195c0 was 26c02b22, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

Fix printf format error

error: format not a string literal and no format arguments [-Werror=format-security]

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