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