source: rtems/cpukit/libmisc/shell/main_ls.c @ 3421e520

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