1 | /* $NetBSD: ls.c,v 1.58 2005/10/26 02:24:22 jschauma Exp $ */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (c) 1989, 1993, 1994 |
---|
5 | * The Regents of the University of California. All rights reserved. |
---|
6 | * |
---|
7 | * This code is derived from software contributed to Berkeley by |
---|
8 | * Michael Fischbein. |
---|
9 | * |
---|
10 | * Redistribution and use in source and binary forms, with or without |
---|
11 | * modification, are permitted provided that the following conditions |
---|
12 | * are met: |
---|
13 | * 1. Redistributions of source code must retain the above copyright |
---|
14 | * notice, this list of conditions and the following disclaimer. |
---|
15 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
16 | * notice, this list of conditions and the following disclaimer in the |
---|
17 | * documentation and/or other materials provided with the distribution. |
---|
18 | * 3. Neither the name of the University nor the names of its contributors |
---|
19 | * may be used to endorse or promote products derived from this software |
---|
20 | * without specific prior written permission. |
---|
21 | * |
---|
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
32 | * SUCH DAMAGE. |
---|
33 | */ |
---|
34 | |
---|
35 | #ifdef HAVE_CONFIG_H |
---|
36 | #include "config.h" |
---|
37 | #endif |
---|
38 | |
---|
39 | #if 0 |
---|
40 | #include <sys/cdefs.h> |
---|
41 | #ifndef lint |
---|
42 | __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\ |
---|
43 | The Regents of the University of California. All rights reserved.\n"); |
---|
44 | #endif /* not lint */ |
---|
45 | |
---|
46 | #ifndef lint |
---|
47 | #if 0 |
---|
48 | static char sccsid[] = "@(#)ls.c 8.7 (Berkeley) 8/5/94"; |
---|
49 | #else |
---|
50 | __RCSID("$NetBSD: ls.c,v 1.58 2005/10/26 02:24:22 jschauma Exp $"); |
---|
51 | #endif |
---|
52 | #endif /* not lint */ |
---|
53 | #endif |
---|
54 | |
---|
55 | #include <rtems.h> |
---|
56 | #include <rtems/shell.h> |
---|
57 | #include <rtems/shellconfig.h> |
---|
58 | #include <getopt.h> |
---|
59 | |
---|
60 | #include <sys/types.h> |
---|
61 | #include <sys/stat.h> |
---|
62 | #include <sys/ioctl.h> |
---|
63 | |
---|
64 | #include <dirent.h> |
---|
65 | #include <err.h> |
---|
66 | #include <errno.h> |
---|
67 | #include <fts.h> |
---|
68 | #include <locale.h> |
---|
69 | #include <stdio.h> |
---|
70 | #include <stdlib.h> |
---|
71 | #include <string.h> |
---|
72 | #include <unistd.h> |
---|
73 | #include <termios.h> |
---|
74 | #include <pwd.h> |
---|
75 | #include <grp.h> |
---|
76 | |
---|
77 | #include "extern-ls.h" |
---|
78 | |
---|
79 | static void display(rtems_shell_ls_globals* globals, FTSENT *, FTSENT *); |
---|
80 | static int mastercmp_listdir(const FTSENT **, const FTSENT **); |
---|
81 | static int mastercmp_no_listdir(const FTSENT **, const FTSENT **); |
---|
82 | static void traverse(rtems_shell_ls_globals* globals, int, char **, int); |
---|
83 | |
---|
84 | static void (*printfcn)(rtems_shell_ls_globals* globals, DISPLAY *); |
---|
85 | static int (*sortfcn)(const FTSENT *, const FTSENT *); |
---|
86 | |
---|
87 | #define BY_NAME 0 |
---|
88 | #define BY_SIZE 1 |
---|
89 | #define BY_TIME 2 |
---|
90 | |
---|
91 | #if RTEMS_REMOVED |
---|
92 | long blocksize; /* block size units */ |
---|
93 | int termwidth = 80; /* default terminal width */ |
---|
94 | int sortkey = BY_NAME; |
---|
95 | int rval = EXIT_SUCCESS; /* exit value - set if error encountered */ |
---|
96 | |
---|
97 | /* flags */ |
---|
98 | int f_accesstime; /* use time of last access */ |
---|
99 | int f_column; /* columnated format */ |
---|
100 | int f_columnacross; /* columnated format, sorted across */ |
---|
101 | int f_flags; /* show flags associated with a file */ |
---|
102 | int f_grouponly; /* long listing without owner */ |
---|
103 | int f_humanize; /* humanize the size field */ |
---|
104 | int f_inode; /* print inode */ |
---|
105 | int f_listdir; /* list actual directory, not contents */ |
---|
106 | int f_listdot; /* list files beginning with . */ |
---|
107 | int f_longform; /* long listing format */ |
---|
108 | int f_nonprint; /* show unprintables as ? */ |
---|
109 | int f_nosort; /* don't sort output */ |
---|
110 | int f_numericonly; /* don't convert uid/gid to name */ |
---|
111 | int f_octal; /* print octal escapes for nongraphic characters */ |
---|
112 | int f_octal_escape; /* like f_octal but use C escapes if possible */ |
---|
113 | int f_recursive; /* ls subdirectories also */ |
---|
114 | int f_reversesort; /* reverse whatever sort is used */ |
---|
115 | int f_sectime; /* print the real time for all files */ |
---|
116 | int f_singlecol; /* use single column output */ |
---|
117 | int f_size; /* list size in short listing */ |
---|
118 | int f_statustime; /* use time of last mode change */ |
---|
119 | int f_stream; /* stream format */ |
---|
120 | int f_type; /* add type character for non-regular files */ |
---|
121 | int f_typedir; /* add type character for directories */ |
---|
122 | int f_whiteout; /* show whiteout entries */ |
---|
123 | #endif |
---|
124 | |
---|
125 | void |
---|
126 | rtems_shell_ls_exit (rtems_shell_ls_globals* globals, int code) |
---|
127 | { |
---|
128 | globals->exit_code = code; |
---|
129 | longjmp (globals->exit_jmp, 1); |
---|
130 | } |
---|
131 | |
---|
132 | static int main_ls(rtems_shell_ls_globals* globals, int argc, char *argv[]); |
---|
133 | |
---|
134 | int |
---|
135 | rtems_shell_main_ls(int argc, char *argv[]) |
---|
136 | { |
---|
137 | rtems_shell_ls_globals ls_globals; |
---|
138 | rtems_shell_ls_globals* globals = &ls_globals; |
---|
139 | memset (globals, 0, sizeof (ls_globals)); |
---|
140 | termwidth = 80; |
---|
141 | sortkey = BY_NAME; |
---|
142 | rval = EXIT_SUCCESS; |
---|
143 | ls_globals.exit_code = 1; |
---|
144 | if (setjmp (ls_globals.exit_jmp) == 0) |
---|
145 | return main_ls (globals, argc, argv); |
---|
146 | return ls_globals.exit_code; |
---|
147 | } |
---|
148 | |
---|
149 | int |
---|
150 | main_ls(rtems_shell_ls_globals* globals, int argc, char *argv[]) |
---|
151 | { |
---|
152 | static char dot[] = ".", *dotav[] = { dot, NULL }; |
---|
153 | //struct winsize win; |
---|
154 | int ch, fts_options; |
---|
155 | int kflag = 0; |
---|
156 | const char *p; |
---|
157 | |
---|
158 | struct getopt_data getopt_reent; |
---|
159 | memset(&getopt_reent, 0, sizeof(getopt_data)); |
---|
160 | |
---|
161 | #if RTEMS_REMOVED |
---|
162 | setprogname(argv[0]); |
---|
163 | #endif |
---|
164 | setlocale(LC_ALL, ""); |
---|
165 | |
---|
166 | /* Terminal defaults to -Cq, non-terminal defaults to -1. */ |
---|
167 | if (isatty(STDOUT_FILENO)) { |
---|
168 | #if RTEMS_REMOVED |
---|
169 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 && |
---|
170 | win.ws_col > 0) |
---|
171 | termwidth = win.ws_col; |
---|
172 | f_column = f_nonprint = 1; |
---|
173 | #endif |
---|
174 | } else |
---|
175 | f_singlecol = 1; |
---|
176 | |
---|
177 | /* Root is -A automatically. */ |
---|
178 | if (!getuid()) |
---|
179 | f_listdot = 1; |
---|
180 | |
---|
181 | fts_options = FTS_PHYSICAL; |
---|
182 | while ((ch = getopt_r(argc, argv, |
---|
183 | "1ABCFLRSTWabcdfghiklmnopqrstuwx", &getopt_reent)) != -1) { |
---|
184 | switch (ch) { |
---|
185 | /* |
---|
186 | * The -1, -C, -l, -m and -x options all override each other so |
---|
187 | * shell aliasing works correctly. |
---|
188 | */ |
---|
189 | case '1': |
---|
190 | f_singlecol = 1; |
---|
191 | f_column = f_columnacross = f_longform = f_stream = 0; |
---|
192 | break; |
---|
193 | case 'C': |
---|
194 | f_column = 1; |
---|
195 | f_columnacross = f_longform = f_singlecol = f_stream = |
---|
196 | 0; |
---|
197 | break; |
---|
198 | case 'g': |
---|
199 | if (f_grouponly != -1) |
---|
200 | f_grouponly = 1; |
---|
201 | f_longform = 1; |
---|
202 | f_column = f_columnacross = f_singlecol = f_stream = 0; |
---|
203 | break; |
---|
204 | case 'l': |
---|
205 | f_longform = 1; |
---|
206 | f_column = f_columnacross = f_singlecol = f_stream = 0; |
---|
207 | /* Never let -g take precedence over -l. */ |
---|
208 | f_grouponly = -1; |
---|
209 | break; |
---|
210 | case 'm': |
---|
211 | f_stream = 1; |
---|
212 | f_column = f_columnacross = f_longform = f_singlecol = |
---|
213 | 0; |
---|
214 | break; |
---|
215 | case 'x': |
---|
216 | f_columnacross = 1; |
---|
217 | f_column = f_longform = f_singlecol = f_stream = 0; |
---|
218 | break; |
---|
219 | /* The -c and -u options override each other. */ |
---|
220 | case 'c': |
---|
221 | f_statustime = 1; |
---|
222 | f_accesstime = 0; |
---|
223 | break; |
---|
224 | case 'u': |
---|
225 | f_accesstime = 1; |
---|
226 | f_statustime = 0; |
---|
227 | break; |
---|
228 | case 'F': |
---|
229 | f_type = 1; |
---|
230 | break; |
---|
231 | case 'L': |
---|
232 | fts_options &= ~FTS_PHYSICAL; |
---|
233 | fts_options |= FTS_LOGICAL; |
---|
234 | break; |
---|
235 | case 'R': |
---|
236 | f_recursive = 1; |
---|
237 | break; |
---|
238 | case 'a': |
---|
239 | fts_options |= FTS_SEEDOT; |
---|
240 | /* FALLTHROUGH */ |
---|
241 | case 'A': |
---|
242 | f_listdot = 1; |
---|
243 | break; |
---|
244 | /* The -B option turns off the -b, -q and -w options. */ |
---|
245 | case 'B': |
---|
246 | f_nonprint = 0; |
---|
247 | f_octal = 1; |
---|
248 | f_octal_escape = 0; |
---|
249 | break; |
---|
250 | /* The -b option turns off the -B, -q and -w options. */ |
---|
251 | case 'b': |
---|
252 | f_nonprint = 0; |
---|
253 | f_octal = 0; |
---|
254 | f_octal_escape = 1; |
---|
255 | break; |
---|
256 | /* The -d option turns off the -R option. */ |
---|
257 | case 'd': |
---|
258 | f_listdir = 1; |
---|
259 | f_recursive = 0; |
---|
260 | break; |
---|
261 | case 'f': |
---|
262 | f_nosort = 1; |
---|
263 | break; |
---|
264 | case 'i': |
---|
265 | f_inode = 1; |
---|
266 | break; |
---|
267 | case 'k': |
---|
268 | blocksize = 1024; |
---|
269 | kflag = 1; |
---|
270 | break; |
---|
271 | /* The -h option forces all sizes to be measured in bytes. */ |
---|
272 | case 'h': |
---|
273 | f_humanize = 1; |
---|
274 | break; |
---|
275 | case 'n': |
---|
276 | f_numericonly = 1; |
---|
277 | break; |
---|
278 | case 'o': |
---|
279 | f_flags = 1; |
---|
280 | break; |
---|
281 | case 'p': |
---|
282 | f_typedir = 1; |
---|
283 | break; |
---|
284 | /* The -q option turns off the -B, -b and -w options. */ |
---|
285 | case 'q': |
---|
286 | f_nonprint = 1; |
---|
287 | f_octal = 0; |
---|
288 | f_octal_escape = 0; |
---|
289 | break; |
---|
290 | case 'r': |
---|
291 | f_reversesort = 1; |
---|
292 | break; |
---|
293 | case 'S': |
---|
294 | sortkey = BY_SIZE; |
---|
295 | break; |
---|
296 | case 's': |
---|
297 | f_size = 1; |
---|
298 | break; |
---|
299 | case 'T': |
---|
300 | f_sectime = 1; |
---|
301 | break; |
---|
302 | case 't': |
---|
303 | sortkey = BY_TIME; |
---|
304 | break; |
---|
305 | case 'W': |
---|
306 | f_whiteout = 1; |
---|
307 | break; |
---|
308 | /* The -w option turns off the -B, -b and -q options. */ |
---|
309 | case 'w': |
---|
310 | f_nonprint = 0; |
---|
311 | f_octal = 0; |
---|
312 | f_octal_escape = 0; |
---|
313 | break; |
---|
314 | default: |
---|
315 | case '?': |
---|
316 | usage(globals); |
---|
317 | } |
---|
318 | } |
---|
319 | argc -= getopt_reent.optind; |
---|
320 | argv += getopt_reent.optind; |
---|
321 | |
---|
322 | if (f_column || f_columnacross || f_stream) { |
---|
323 | if ((p = getenv("COLUMNS")) != NULL) |
---|
324 | termwidth = atoi(p); |
---|
325 | } |
---|
326 | |
---|
327 | /* |
---|
328 | * If both -g and -l options, let -l take precedence. |
---|
329 | */ |
---|
330 | if (f_grouponly == -1) |
---|
331 | f_grouponly = 0; |
---|
332 | |
---|
333 | /* |
---|
334 | * If not -F, -i, -l, -p, -S, -s or -t options, don't require stat |
---|
335 | * information. |
---|
336 | */ |
---|
337 | if (!f_inode && !f_longform && !f_size && !f_type && !f_typedir && |
---|
338 | sortkey == BY_NAME) |
---|
339 | fts_options |= FTS_NOSTAT; |
---|
340 | |
---|
341 | /* |
---|
342 | * If not -F, -d or -l options, follow any symbolic links listed on |
---|
343 | * the command line. |
---|
344 | */ |
---|
345 | if (!f_longform && !f_listdir && !f_type) |
---|
346 | fts_options |= FTS_COMFOLLOW; |
---|
347 | |
---|
348 | /* |
---|
349 | * If -W, show whiteout entries |
---|
350 | */ |
---|
351 | #ifdef FTS_WHITEOUT |
---|
352 | if (f_whiteout) |
---|
353 | fts_options |= FTS_WHITEOUT; |
---|
354 | #endif |
---|
355 | |
---|
356 | /* If -l or -s, figure out block size. */ |
---|
357 | if (f_inode || f_longform || f_size) { |
---|
358 | #if RTEMS_REMOVED |
---|
359 | if (!kflag) |
---|
360 | (void)getbsize(NULL, &blocksize); |
---|
361 | #else |
---|
362 | blocksize = 1024; |
---|
363 | #endif |
---|
364 | blocksize /= 512; |
---|
365 | } |
---|
366 | |
---|
367 | /* Select a sort function. */ |
---|
368 | if (f_reversesort) { |
---|
369 | switch (sortkey) { |
---|
370 | case BY_NAME: |
---|
371 | sortfcn = revnamecmp; |
---|
372 | break; |
---|
373 | case BY_SIZE: |
---|
374 | sortfcn = revsizecmp; |
---|
375 | break; |
---|
376 | case BY_TIME: |
---|
377 | if (f_accesstime) |
---|
378 | sortfcn = revacccmp; |
---|
379 | else if (f_statustime) |
---|
380 | sortfcn = revstatcmp; |
---|
381 | else /* Use modification time. */ |
---|
382 | sortfcn = revmodcmp; |
---|
383 | break; |
---|
384 | } |
---|
385 | } else { |
---|
386 | switch (sortkey) { |
---|
387 | case BY_NAME: |
---|
388 | sortfcn = namecmp; |
---|
389 | break; |
---|
390 | case BY_SIZE: |
---|
391 | sortfcn = sizecmp; |
---|
392 | break; |
---|
393 | case BY_TIME: |
---|
394 | if (f_accesstime) |
---|
395 | sortfcn = acccmp; |
---|
396 | else if (f_statustime) |
---|
397 | sortfcn = statcmp; |
---|
398 | else /* Use modification time. */ |
---|
399 | sortfcn = modcmp; |
---|
400 | break; |
---|
401 | } |
---|
402 | } |
---|
403 | |
---|
404 | /* Select a print function. */ |
---|
405 | if (f_singlecol) |
---|
406 | printfcn = printscol; |
---|
407 | else if (f_columnacross) |
---|
408 | printfcn = printacol; |
---|
409 | else if (f_longform) |
---|
410 | printfcn = printlong; |
---|
411 | else if (f_stream) |
---|
412 | printfcn = printstream; |
---|
413 | else |
---|
414 | printfcn = printcol; |
---|
415 | |
---|
416 | if (argc) |
---|
417 | traverse(globals, argc, argv, fts_options); |
---|
418 | else |
---|
419 | traverse(globals, 1, dotav, fts_options); |
---|
420 | exit(rval); |
---|
421 | /* NOTREACHED */ |
---|
422 | return 0; |
---|
423 | } |
---|
424 | |
---|
425 | #if RTEMS_REMOVED |
---|
426 | static int output; /* If anything output. */ |
---|
427 | #endif |
---|
428 | |
---|
429 | /* |
---|
430 | * Traverse() walks the logical directory structure specified by the argv list |
---|
431 | * in the order specified by the mastercmp() comparison function. During the |
---|
432 | * traversal it passes linked lists of structures to display() which represent |
---|
433 | * a superset (may be exact set) of the files to be displayed. |
---|
434 | */ |
---|
435 | static void |
---|
436 | traverse(rtems_shell_ls_globals* globals, int argc, char *argv[], int options) |
---|
437 | { |
---|
438 | FTS *ftsp; |
---|
439 | FTSENT *p, *chp; |
---|
440 | int ch_options; |
---|
441 | |
---|
442 | if ((ftsp = |
---|
443 | fts_open(argv, options, |
---|
444 | f_nosort ? NULL : f_listdir ? |
---|
445 | mastercmp_listdir : mastercmp_no_listdir)) == NULL) |
---|
446 | err(exit_jump, EXIT_FAILURE, NULL); |
---|
447 | |
---|
448 | display(globals, NULL, fts_children(ftsp, 0)); |
---|
449 | if (f_listdir) |
---|
450 | { |
---|
451 | fts_close(ftsp); |
---|
452 | return; |
---|
453 | } |
---|
454 | |
---|
455 | /* |
---|
456 | * If not recursing down this tree and don't need stat info, just get |
---|
457 | * the names. |
---|
458 | */ |
---|
459 | ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0; |
---|
460 | |
---|
461 | while ((p = fts_read(ftsp)) != NULL) |
---|
462 | switch (p->fts_info) { |
---|
463 | case FTS_DC: |
---|
464 | warnx("%s: directory causes a cycle", p->fts_name); |
---|
465 | break; |
---|
466 | case FTS_DNR: |
---|
467 | case FTS_ERR: |
---|
468 | warnx("%s: %s", p->fts_name, strerror(p->fts_errno)); |
---|
469 | rval = EXIT_FAILURE; |
---|
470 | break; |
---|
471 | case FTS_D: |
---|
472 | if (p->fts_level != FTS_ROOTLEVEL && |
---|
473 | p->fts_name[0] == '.' && !f_listdot) |
---|
474 | break; |
---|
475 | |
---|
476 | /* |
---|
477 | * If already output something, put out a newline as |
---|
478 | * a separator. If multiple arguments, precede each |
---|
479 | * directory with its name. |
---|
480 | */ |
---|
481 | if (output) |
---|
482 | (void)printf("\n%s:\n", p->fts_path); |
---|
483 | else if (argc > 1) { |
---|
484 | (void)printf("%s:\n", p->fts_path); |
---|
485 | output = 1; |
---|
486 | } |
---|
487 | |
---|
488 | chp = fts_children(ftsp, ch_options); |
---|
489 | display(globals, p, chp); |
---|
490 | |
---|
491 | if (!f_recursive && chp != NULL) |
---|
492 | (void)fts_set(ftsp, p, FTS_SKIP); |
---|
493 | break; |
---|
494 | } |
---|
495 | fts_close(ftsp); |
---|
496 | if (errno) |
---|
497 | err(exit_jump, EXIT_FAILURE, "fts_read"); |
---|
498 | } |
---|
499 | |
---|
500 | /* |
---|
501 | * Display() takes a linked list of FTSENT structures and passes the list |
---|
502 | * along with any other necessary information to the print function. P |
---|
503 | * points to the parent directory of the display list. |
---|
504 | */ |
---|
505 | static void |
---|
506 | display(rtems_shell_ls_globals* globals, FTSENT *p, FTSENT *list) |
---|
507 | { |
---|
508 | struct stat *sp; |
---|
509 | DISPLAY d; |
---|
510 | FTSENT *cur; |
---|
511 | NAMES *np; |
---|
512 | u_int64_t btotal, stotal, maxblock, maxsize; |
---|
513 | int maxinode, maxnlink, maxmajor, maxminor; |
---|
514 | int bcfile, entries, flen, glen, ulen, maxflags, maxgroup, maxlen; |
---|
515 | int maxuser, needstats; |
---|
516 | const char *user, *group; |
---|
517 | char buf[21]; /* 64 bits == 20 digits, +1 for NUL */ |
---|
518 | char nuser[12], ngroup[12]; |
---|
519 | char *flags = NULL; |
---|
520 | |
---|
521 | #ifdef __GNUC__ |
---|
522 | /* This outrageous construct just to shut up a GCC warning. */ |
---|
523 | (void) &maxsize; |
---|
524 | #endif |
---|
525 | |
---|
526 | /* |
---|
527 | * If list is NULL there are two possibilities: that the parent |
---|
528 | * directory p has no children, or that fts_children() returned an |
---|
529 | * error. We ignore the error case since it will be replicated |
---|
530 | * on the next call to fts_read() on the post-order visit to the |
---|
531 | * directory p, and will be signalled in traverse(). |
---|
532 | */ |
---|
533 | if (list == NULL) |
---|
534 | return; |
---|
535 | |
---|
536 | needstats = f_inode || f_longform || f_size; |
---|
537 | flen = 0; |
---|
538 | maxinode = maxnlink = 0; |
---|
539 | bcfile = 0; |
---|
540 | maxuser = maxgroup = maxflags = maxlen = 0; |
---|
541 | btotal = stotal = maxblock = maxsize = 0; |
---|
542 | maxmajor = maxminor = 0; |
---|
543 | for (cur = list, entries = 0; cur; cur = cur->fts_link) { |
---|
544 | if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) { |
---|
545 | warnx("%s: %s", |
---|
546 | cur->fts_name, strerror(cur->fts_errno)); |
---|
547 | cur->fts_number = NO_PRINT; |
---|
548 | rval = EXIT_FAILURE; |
---|
549 | continue; |
---|
550 | } |
---|
551 | |
---|
552 | /* |
---|
553 | * P is NULL if list is the argv list, to which different rules |
---|
554 | * apply. |
---|
555 | */ |
---|
556 | if (p == NULL) { |
---|
557 | /* Directories will be displayed later. */ |
---|
558 | if (cur->fts_info == FTS_D && !f_listdir) { |
---|
559 | cur->fts_number = NO_PRINT; |
---|
560 | continue; |
---|
561 | } |
---|
562 | } else { |
---|
563 | /* Only display dot file if -a/-A set. */ |
---|
564 | if (cur->fts_name[0] == '.' && !f_listdot) { |
---|
565 | cur->fts_number = NO_PRINT; |
---|
566 | continue; |
---|
567 | } |
---|
568 | } |
---|
569 | if (cur->fts_namelen > maxlen) |
---|
570 | maxlen = cur->fts_namelen; |
---|
571 | if (needstats) { |
---|
572 | sp = cur->fts_statp; |
---|
573 | if (sp->st_blocks > maxblock) |
---|
574 | maxblock = sp->st_blocks; |
---|
575 | if (sp->st_ino > maxinode) |
---|
576 | maxinode = sp->st_ino; |
---|
577 | if (sp->st_nlink > maxnlink) |
---|
578 | maxnlink = sp->st_nlink; |
---|
579 | if (sp->st_size > maxsize) |
---|
580 | maxsize = sp->st_size; |
---|
581 | if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) { |
---|
582 | bcfile = 1; |
---|
583 | if (major(sp->st_rdev) > maxmajor) |
---|
584 | maxmajor = major(sp->st_rdev); |
---|
585 | if (minor(sp->st_rdev) > maxminor) |
---|
586 | maxminor = minor(sp->st_rdev); |
---|
587 | } |
---|
588 | |
---|
589 | btotal += sp->st_blocks; |
---|
590 | stotal += sp->st_size; |
---|
591 | if (f_longform) { |
---|
592 | if (f_numericonly || |
---|
593 | (user = user_from_uid(sp->st_uid, 0)) == |
---|
594 | NULL) { |
---|
595 | (void)snprintf(nuser, sizeof(nuser), |
---|
596 | "%u", sp->st_uid); |
---|
597 | user = nuser; |
---|
598 | } |
---|
599 | if (f_numericonly || |
---|
600 | (group = group_from_gid(sp->st_gid, 0)) == |
---|
601 | NULL) { |
---|
602 | (void)snprintf(ngroup, sizeof(ngroup), |
---|
603 | "%u", sp->st_gid); |
---|
604 | group = ngroup; |
---|
605 | } |
---|
606 | if ((ulen = strlen(user)) > maxuser) |
---|
607 | maxuser = ulen; |
---|
608 | if ((glen = strlen(group)) > maxgroup) |
---|
609 | maxgroup = glen; |
---|
610 | #if RTEMS_REMOVED |
---|
611 | if (f_flags) { |
---|
612 | flags = |
---|
613 | flags_to_string(sp->st_flags, "-"); |
---|
614 | if ((flen = strlen(flags)) > maxflags) |
---|
615 | maxflags = flen; |
---|
616 | } else |
---|
617 | #endif |
---|
618 | flen = 0; |
---|
619 | |
---|
620 | if ((np = malloc(sizeof(NAMES) + |
---|
621 | ulen + glen + flen + 3)) == NULL) |
---|
622 | err(exit_jump, EXIT_FAILURE, NULL); |
---|
623 | |
---|
624 | np->user = &np->data[0]; |
---|
625 | (void)strcpy(np->user, user); |
---|
626 | np->group = &np->data[ulen + 1]; |
---|
627 | (void)strcpy(np->group, group); |
---|
628 | |
---|
629 | if (f_flags) { |
---|
630 | np->flags = &np->data[ulen + glen + 2]; |
---|
631 | (void)strcpy(np->flags, flags); |
---|
632 | } |
---|
633 | cur->fts_pointer = np; |
---|
634 | } |
---|
635 | } |
---|
636 | ++entries; |
---|
637 | } |
---|
638 | |
---|
639 | if (!entries) |
---|
640 | return; |
---|
641 | |
---|
642 | d.list = list; |
---|
643 | d.entries = entries; |
---|
644 | d.maxlen = maxlen; |
---|
645 | if (needstats) { |
---|
646 | d.btotal = btotal; |
---|
647 | d.stotal = stotal; |
---|
648 | if (f_humanize) { |
---|
649 | d.s_block = 4; /* min buf length for humanize_number */ |
---|
650 | } else { |
---|
651 | (void)snprintf(buf, sizeof(buf), "%llu", |
---|
652 | (long long)howmany(maxblock, blocksize)); |
---|
653 | d.s_block = strlen(buf); |
---|
654 | } |
---|
655 | d.s_flags = maxflags; |
---|
656 | d.s_group = maxgroup; |
---|
657 | (void)snprintf(buf, sizeof(buf), "%u", maxinode); |
---|
658 | d.s_inode = strlen(buf); |
---|
659 | (void)snprintf(buf, sizeof(buf), "%u", maxnlink); |
---|
660 | d.s_nlink = strlen(buf); |
---|
661 | if (f_humanize) { |
---|
662 | d.s_size = 4; /* min buf length for humanize_number */ |
---|
663 | } else { |
---|
664 | (void)snprintf(buf, sizeof(buf), "%llu", |
---|
665 | (long long)maxsize); |
---|
666 | d.s_size = strlen(buf); |
---|
667 | } |
---|
668 | d.s_user = maxuser; |
---|
669 | if (bcfile) { |
---|
670 | (void)snprintf(buf, sizeof(buf), "%u", maxmajor); |
---|
671 | d.s_major = strlen(buf); |
---|
672 | (void)snprintf(buf, sizeof(buf), "%u", maxminor); |
---|
673 | d.s_minor = strlen(buf); |
---|
674 | if (d.s_major + d.s_minor + 2 > d.s_size) |
---|
675 | d.s_size = d.s_major + d.s_minor + 2; |
---|
676 | else if (d.s_size - d.s_minor - 2 > d.s_major) |
---|
677 | d.s_major = d.s_size - d.s_minor - 2; |
---|
678 | } else { |
---|
679 | d.s_major = 0; |
---|
680 | d.s_minor = 0; |
---|
681 | } |
---|
682 | } |
---|
683 | |
---|
684 | printfcn(globals, &d); |
---|
685 | output = 1; |
---|
686 | |
---|
687 | if (f_longform) |
---|
688 | for (cur = list; cur; cur = cur->fts_link) |
---|
689 | free(cur->fts_pointer); |
---|
690 | } |
---|
691 | |
---|
692 | /* |
---|
693 | * Ordering for mastercmp: |
---|
694 | * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories |
---|
695 | * as larger than directories. Within either group, use the sort function. |
---|
696 | * All other levels use the sort function. Error entries remain unsorted. |
---|
697 | */ |
---|
698 | static int |
---|
699 | mastercmp_no_listdir(const FTSENT **a, const FTSENT **b) |
---|
700 | { |
---|
701 | int a_info, b_info; |
---|
702 | int l_f_listdir = 0; |
---|
703 | |
---|
704 | a_info = (*a)->fts_info; |
---|
705 | if (a_info == FTS_ERR) |
---|
706 | return (0); |
---|
707 | b_info = (*b)->fts_info; |
---|
708 | if (b_info == FTS_ERR) |
---|
709 | return (0); |
---|
710 | |
---|
711 | if (a_info == FTS_NS || b_info == FTS_NS) { |
---|
712 | if (b_info != FTS_NS) |
---|
713 | return (1); |
---|
714 | else if (a_info != FTS_NS) |
---|
715 | return (-1); |
---|
716 | else |
---|
717 | return (namecmp(*a, *b)); |
---|
718 | } |
---|
719 | |
---|
720 | if (a_info != b_info && !l_f_listdir && |
---|
721 | (*a)->fts_level == FTS_ROOTLEVEL) { |
---|
722 | if (a_info == FTS_D) |
---|
723 | return (1); |
---|
724 | else if (b_info == FTS_D) |
---|
725 | return (-1); |
---|
726 | } |
---|
727 | return (sortfcn(*a, *b)); |
---|
728 | } |
---|
729 | |
---|
730 | static int |
---|
731 | mastercmp_listdir(const FTSENT **a, const FTSENT **b) |
---|
732 | { |
---|
733 | int a_info, b_info; |
---|
734 | int l_f_listdir = 1; |
---|
735 | |
---|
736 | a_info = (*a)->fts_info; |
---|
737 | if (a_info == FTS_ERR) |
---|
738 | return (0); |
---|
739 | b_info = (*b)->fts_info; |
---|
740 | if (b_info == FTS_ERR) |
---|
741 | return (0); |
---|
742 | |
---|
743 | if (a_info == FTS_NS || b_info == FTS_NS) { |
---|
744 | if (b_info != FTS_NS) |
---|
745 | return (1); |
---|
746 | else if (a_info != FTS_NS) |
---|
747 | return (-1); |
---|
748 | else |
---|
749 | return (namecmp(*a, *b)); |
---|
750 | } |
---|
751 | |
---|
752 | if (a_info != b_info && !l_f_listdir && |
---|
753 | (*a)->fts_level == FTS_ROOTLEVEL) { |
---|
754 | if (a_info == FTS_D) |
---|
755 | return (1); |
---|
756 | else if (b_info == FTS_D) |
---|
757 | return (-1); |
---|
758 | } |
---|
759 | return (sortfcn(*a, *b)); |
---|
760 | } |
---|
761 | |
---|
762 | rtems_shell_cmd_t rtems_shell_LS_Command = { |
---|
763 | "ls", /* name */ |
---|
764 | "ls [dir] # list files in the directory", /* usage */ |
---|
765 | "files", /* topic */ |
---|
766 | rtems_shell_main_ls, /* command */ |
---|
767 | NULL, /* alias */ |
---|
768 | NULL /* next */ |
---|
769 | }; |
---|