source: rtems/cpukit/libmisc/shell/main_ls.c @ 68e1ccc4

5
Last change on this file since 68e1ccc4 was 6cdaa85, checked in by Sebastian Huber <sebastian.huber@…>, on 10/04/18 at 18:16:45

shell: Use #include "..." for local header files

Update #3375.

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