source: rtems/cpukit/libmisc/shell/main_ls.c @ 42c4de8

4.104.115
Last change on this file since 42c4de8 was 42c4de8, checked in by Chris Johns <chrisj@…>, on 02/01/10 at 00:03:02

2010-02-01 Chris Johns <chrisj@…>

  • libmisc/shell/fts.c: Updated to the latest NetBSD version to resolve heap allocation bugs.
  • libmisc/shell/main_ls.c, libmisc/shell/print-ls.c: Fix printing size bugs.
  • libnetworking/rtems/mkrootfs.c: Fix byte order bug when creating the loopback interface address.
  • 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
135int
136rtems_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
150int
151main_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        /* Make equal to 1 so ls -l shows the actual blcok count */
364        blocksize = 512;
365#endif
366                blocksize /= 512;
367        }
368
369        /* Select a sort function. */
370        if (f_reversesort) {
371                switch (sortkey) {
372                case BY_NAME:
373                        sortfcn = revnamecmp;
374                        break;
375                case BY_SIZE:
376                        sortfcn = revsizecmp;
377                        break;
378                case BY_TIME:
379                        if (f_accesstime)
380                                sortfcn = revacccmp;
381                        else if (f_statustime)
382                                sortfcn = revstatcmp;
383                        else /* Use modification time. */
384                                sortfcn = revmodcmp;
385                        break;
386                }
387        } else {
388                switch (sortkey) {
389                case BY_NAME:
390                        sortfcn = namecmp;
391                        break;
392                case BY_SIZE:
393                        sortfcn = sizecmp;
394                        break;
395                case BY_TIME:
396                        if (f_accesstime)
397                                sortfcn = acccmp;
398                        else if (f_statustime)
399                                sortfcn = statcmp;
400                        else /* Use modification time. */
401                                sortfcn = modcmp;
402                        break;
403                }
404        }
405
406        /* Select a print function. */
407        if (f_singlecol)
408                printfcn = printscol;
409        else if (f_columnacross)
410                printfcn = printacol;
411        else if (f_longform)
412                printfcn = printlong;
413        else if (f_stream)
414                printfcn = printstream;
415        else
416                printfcn = printcol;
417
418        if (argc)
419                traverse(globals, argc, argv, fts_options);
420        else
421                traverse(globals, 1, dotav, fts_options);
422        exit(rval);
423        /* NOTREACHED */
424    return 0;
425}
426
427#if RTEMS_REMOVED
428static int output;                      /* If anything output. */
429#endif
430
431/*
432 * Traverse() walks the logical directory structure specified by the argv list
433 * in the order specified by the mastercmp() comparison function.  During the
434 * traversal it passes linked lists of structures to display() which represent
435 * a superset (may be exact set) of the files to be displayed.
436 */
437static void
438traverse(rtems_shell_ls_globals* globals, int argc, char *argv[], int options)
439{
440        FTS *ftsp;
441        FTSENT *p, *chp;
442        int ch_options;
443
444        if ((ftsp =
445            fts_open(argv, options,
446                 f_nosort ? NULL : f_listdir ?
447                 mastercmp_listdir : mastercmp_no_listdir)) == NULL)
448                err(exit_jump, EXIT_FAILURE, NULL);
449
450        display(globals, NULL, fts_children(ftsp, 0));
451        if (f_listdir)
452    {
453        fts_close(ftsp);
454                return;
455    }
456
457        /*
458         * If not recursing down this tree and don't need stat info, just get
459         * the names.
460         */
461        ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
462
463        while ((p = fts_read(ftsp)) != NULL)
464                switch (p->fts_info) {
465                case FTS_DC:
466                        warnx("%s: directory causes a cycle", p->fts_name);
467                        break;
468                case FTS_DNR:
469                case FTS_ERR:
470                        warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
471                        rval = EXIT_FAILURE;
472                        break;
473                case FTS_D:
474                        if (p->fts_level != FTS_ROOTLEVEL &&
475                            p->fts_name[0] == '.' && !f_listdot)
476                                break;
477
478                        /*
479                         * If already output something, put out a newline as
480                         * a separator.  If multiple arguments, precede each
481                         * directory with its name.
482                         */
483                        if (output)
484                                (void)printf("\n%s:\n", p->fts_path);
485                        else if (argc > 1) {
486                                (void)printf("%s:\n", p->fts_path);
487                                output = 1;
488                        }
489
490                        chp = fts_children(ftsp, ch_options);
491                        display(globals, p, chp);
492
493                        if (!f_recursive && chp != NULL)
494                                (void)fts_set(ftsp, p, FTS_SKIP);
495                        break;
496                }
497    fts_close(ftsp);
498        if (errno)
499                err(exit_jump, EXIT_FAILURE, "fts_read");
500}
501
502/*
503 * Display() takes a linked list of FTSENT structures and passes the list
504 * along with any other necessary information to the print function.  P
505 * points to the parent directory of the display list.
506 */
507static void
508display(rtems_shell_ls_globals* globals, FTSENT *p, FTSENT *list)
509{
510        struct stat *sp;
511        DISPLAY d;
512        FTSENT *cur;
513        NAMES *np;
514        u_int64_t btotal, stotal, maxblock, maxsize;
515        int maxinode, maxnlink, maxmajor, maxminor;
516        int bcfile, entries, flen, glen, ulen, maxflags, maxgroup, maxlen;
517        int maxuser, needstats;
518        const char *user, *group;
519        char buf[21];           /* 64 bits == 20 digits, +1 for NUL */
520        char nuser[12], ngroup[12];
521        char *flags = NULL;
522
523#ifdef __GNUC__
524        /* This outrageous construct just to shut up a GCC warning. */
525        (void) &maxsize;
526#endif
527
528        /*
529         * If list is NULL there are two possibilities: that the parent
530         * directory p has no children, or that fts_children() returned an
531         * error.  We ignore the error case since it will be replicated
532         * on the next call to fts_read() on the post-order visit to the
533         * directory p, and will be signalled in traverse().
534         */
535        if (list == NULL)
536                return;
537
538        needstats = f_inode || f_longform || f_size;
539        flen = 0;
540        maxinode = maxnlink = 0;
541        bcfile = 0;
542        maxuser = maxgroup = maxflags = maxlen = 0;
543        btotal = stotal = maxblock = maxsize = 0;
544        maxmajor = maxminor = 0;
545        for (cur = list, entries = 0; cur; cur = cur->fts_link) {
546    uint64_t size;
547                if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
548                        warnx("%s: %s",
549                            cur->fts_name, strerror(cur->fts_errno));
550                        cur->fts_number = NO_PRINT;
551                        rval = EXIT_FAILURE;
552                        continue;
553                }
554
555                /*
556                 * P is NULL if list is the argv list, to which different rules
557                 * apply.
558                 */
559                if (p == NULL) {
560                        /* Directories will be displayed later. */
561                        if (cur->fts_info == FTS_D && !f_listdir) {
562                                cur->fts_number = NO_PRINT;
563                                continue;
564                        }
565                } else {
566                        /* Only display dot file if -a/-A set. */
567                        if (cur->fts_name[0] == '.' && !f_listdot) {
568                                cur->fts_number = NO_PRINT;
569                                continue;
570                        }
571                }
572                if (cur->fts_namelen > maxlen)
573                        maxlen = cur->fts_namelen;
574                if (needstats) {
575                        sp = cur->fts_statp;
576      size = ((uint64_t) sp->st_blocks) * sp->st_blksize;
577      if (size < 0x100000000ULL)
578        size = sp->st_size;
579                        if (sp->st_blocks > maxblock)
580                                maxblock = sp->st_blocks;
581                        if (sp->st_ino > maxinode)
582                                maxinode = sp->st_ino;
583                        if (sp->st_nlink > maxnlink)
584                                maxnlink = sp->st_nlink;
585                        if (size > maxsize)
586                                maxsize = size;
587                        if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) {
588                                bcfile = 1;
589                                if (major(sp->st_rdev) > maxmajor)
590                                        maxmajor = major(sp->st_rdev);
591                                if (minor(sp->st_rdev) > maxminor)
592                                        maxminor = minor(sp->st_rdev);
593                        }
594
595                        btotal += sp->st_blocks;
596                        stotal += size;
597                        if (f_longform) {
598                                if (f_numericonly ||
599                                    (user = user_from_uid(sp->st_uid, 0)) ==
600                                    NULL) {
601                                        (void)snprintf(nuser, sizeof(nuser),
602                                            "%u", sp->st_uid);
603                                        user = nuser;
604                                }
605                                if (f_numericonly ||
606                                    (group = group_from_gid(sp->st_gid, 0)) ==
607                                    NULL) {
608                                        (void)snprintf(ngroup, sizeof(ngroup),
609                                            "%u", sp->st_gid);
610                                        group = ngroup;
611                                }
612                                if ((ulen = strlen(user)) > maxuser)
613                                        maxuser = ulen;
614                                if ((glen = strlen(group)) > maxgroup)
615                                        maxgroup = glen;
616#if RTEMS_REMOVED
617                                if (f_flags) {
618                                        flags =
619                                            flags_to_string(sp->st_flags, "-");
620                                        if ((flen = strlen(flags)) > maxflags)
621                                                maxflags = flen;
622                                } else
623#endif
624                                        flen = 0;
625
626                                if ((np = malloc(sizeof(NAMES) +
627                                    ulen + glen + flen + 3)) == NULL)
628                                        err(exit_jump, EXIT_FAILURE, NULL);
629
630                                np->user = &np->data[0];
631                                (void)strcpy(np->user, user);
632                                np->group = &np->data[ulen + 1];
633                                (void)strcpy(np->group, group);
634
635                                 if (f_flags && flags) {
636                                        np->flags = &np->data[ulen + glen + 2];
637                                        (void)strcpy(np->flags, flags);
638                                }
639                                cur->fts_pointer = np;
640                        }
641                }
642                ++entries;
643        }
644
645        if (!entries)
646                return;
647
648        d.list = list;
649        d.entries = entries;
650        d.maxlen = maxlen;
651        if (needstats) {
652                d.btotal = btotal;
653                d.stotal = stotal;
654                if (f_humanize) {
655                        d.s_block = 4; /* min buf length for humanize_number */
656                } else {
657                        (void)snprintf(buf, sizeof(buf), "%llu",
658                            (long long)howmany(maxblock, blocksize));
659                        d.s_block = strlen(buf);
660                }
661                d.s_flags = maxflags;
662                d.s_group = maxgroup;
663                (void)snprintf(buf, sizeof(buf), "%u", maxinode);
664                d.s_inode = strlen(buf);
665                (void)snprintf(buf, sizeof(buf), "%u", maxnlink);
666                d.s_nlink = strlen(buf);
667                if (f_humanize) {
668                        d.s_size = 4; /* min buf length for humanize_number */
669                } else {
670                        (void)snprintf(buf, sizeof(buf), "%llu",
671                            (long long)maxsize);
672                        d.s_size = strlen(buf);
673                }
674                d.s_user = maxuser;
675                if (bcfile) {
676                        (void)snprintf(buf, sizeof(buf), "%u", maxmajor);
677                        d.s_major = strlen(buf);
678                        (void)snprintf(buf, sizeof(buf), "%u", maxminor);
679                        d.s_minor = strlen(buf);
680                        if (d.s_major + d.s_minor + 2 > d.s_size)
681                                d.s_size = d.s_major + d.s_minor + 2;
682                        else if (d.s_size - d.s_minor - 2 > d.s_major)
683                                d.s_major = d.s_size - d.s_minor - 2;
684                } else {
685                        d.s_major = 0;
686                        d.s_minor = 0;
687                }
688        }
689
690        printfcn(globals, &d);
691        output = 1;
692
693        if (f_longform)
694                for (cur = list; cur; cur = cur->fts_link)
695                        free(cur->fts_pointer);
696}
697
698/*
699 * Ordering for mastercmp:
700 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
701 * as larger than directories.  Within either group, use the sort function.
702 * All other levels use the sort function.  Error entries remain unsorted.
703 */
704static int
705mastercmp_no_listdir(const FTSENT **a, const FTSENT **b)
706{
707        int a_info, b_info;
708    int l_f_listdir = 0;
709
710        a_info = (*a)->fts_info;
711        if (a_info == FTS_ERR)
712                return (0);
713        b_info = (*b)->fts_info;
714        if (b_info == FTS_ERR)
715                return (0);
716
717        if (a_info == FTS_NS || b_info == FTS_NS) {
718                if (b_info != FTS_NS)
719                        return (1);
720                else if (a_info != FTS_NS)
721                        return (-1);
722                else
723                        return (namecmp(*a, *b));
724        }
725
726        if (a_info != b_info && !l_f_listdir &&
727            (*a)->fts_level == FTS_ROOTLEVEL) {
728                if (a_info == FTS_D)
729                        return (1);
730                else if (b_info == FTS_D)
731                        return (-1);
732        }
733        return (sortfcn(*a, *b));
734}
735
736static int
737mastercmp_listdir(const FTSENT **a, const FTSENT **b)
738{
739        int a_info, b_info;
740    int l_f_listdir = 1;
741
742        a_info = (*a)->fts_info;
743        if (a_info == FTS_ERR)
744                return (0);
745        b_info = (*b)->fts_info;
746        if (b_info == FTS_ERR)
747                return (0);
748
749        if (a_info == FTS_NS || b_info == FTS_NS) {
750                if (b_info != FTS_NS)
751                        return (1);
752                else if (a_info != FTS_NS)
753                        return (-1);
754                else
755                        return (namecmp(*a, *b));
756        }
757
758        if (a_info != b_info && !l_f_listdir &&
759            (*a)->fts_level == FTS_ROOTLEVEL) {
760                if (a_info == FTS_D)
761                        return (1);
762                else if (b_info == FTS_D)
763                        return (-1);
764        }
765        return (sortfcn(*a, *b));
766}
767
768rtems_shell_cmd_t rtems_shell_LS_Command = {
769  "ls",                                         /* name */
770  "ls [dir]     # list files in the directory", /* usage */
771  "files",                                      /* topic */
772  rtems_shell_main_ls,                          /* command */
773  NULL,                                         /* alias */
774  NULL                                          /* next */
775};
Note: See TracBrowser for help on using the repository browser.