Changeset a27e370 in mainline


Ignore:
Timestamp:
2018-12-08T13:45:38Z (6 years ago)
Author:
Matthieu Riolo <matthieu.riolo@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0296ee1
Parents:
87ba3ceb
Message:

correct -1 according to feedback

This corrects the format for dirs vs files by adding a slash
Directories and files are simply sorted alphabetically

Location:
uspace/app/bdsh/cmds/modules/ls
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/cmds/modules/ls/ls.c

    r87ba3ceb ra27e370  
    6767static unsigned int ls_start(ls_job_t *);
    6868static void ls_print(struct dir_elem_t *);
    69 static int ls_cmp(const void *, const void *);
     69static void ls_print_single_column(struct dir_elem_t *);
     70static int ls_cmp_type_name(const void *, const void *);
     71static int ls_cmp_name(const void *, const void *);
    7072static signed int ls_scan_dir(const char *, DIR *, struct dir_elem_t **);
    7173static unsigned int ls_recursive(const char *, DIR *);
     
    7981        ls->well_formatted = false;
    8082        ls->single_column = false;
    81 
     83        ls->printer = ls_print;
    8284        return 1;
    8385}
     
    9698static void ls_print(struct dir_elem_t *de)
    9799{
    98         if (!ls.single_column && de->s.is_file) {
     100
     101        if (de->s.is_file) {
    99102                if (ls.well_formatted) {
    100103                        cap_spec_t cap;
     
    114117
    115118                printf("%-40s\t%llu\n", de->name, (long long) de->s.size);
    116         } else if (!ls.single_column && de->s.is_directory)
     119        } else if (de->s.is_directory)
    117120                printf("%-40s\t<dir>\n", de->name);
    118121        else
     
    120123}
    121124
     125static void ls_print_single_column(struct dir_elem_t *de)
     126{
     127        if (de->s.is_file) {
     128                printf("%s\n", de->name);
     129        } else {
     130                printf("%s/\n", de->name);
     131        }
     132}
     133
    122134/** Compare 2 directory elements.
    123135 *
     
    131143 * @return              -1 if a < b, 1 otherwise.
    132144 */
    133 static int ls_cmp(const void *a, const void *b)
     145static int ls_cmp_type_name(const void *a, const void *b)
    134146{
    135147        struct dir_elem_t const *da = a;
     
    142154        else
    143155                return 1;
     156}
     157
     158/** Compare directories/files per name
     159 *
     160 * This comparision ignores the type of
     161 * the node. Sorted will strictly by name.
     162 *
     163 */
     164static int ls_cmp_name(const void *a, const void *b)
     165{
     166        struct dir_elem_t const *da = a;
     167        struct dir_elem_t const *db = b;
     168
     169        return str_cmp(da->name, db->name);
    144170}
    145171
     
    214240        }
    215241
    216         if (ls.sort)
    217                 qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t), ls_cmp);
     242        if (ls.sort) {
     243                int (*compar)(const void *, const void *);
     244                compar = ls.single_column ? ls_cmp_name : ls_cmp_type_name;
     245                qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t), compar);
     246        }
    218247
    219248        for (i = 0; i < nbdirs; i++)
    220                 ls_print(&tosort[i]);
     249                ls.printer(&tosort[i]);
    221250
    222251        /* Populate the directory list. */
     
    404433                case '1':
    405434                        ls.single_column = true;
     435                        ls.printer = ls_print_single_column;
    406436                        break;
    407437                }
     
    430460        switch (scope) {
    431461        case LS_FILE:
    432                 ls_print(&de);
     462                ls.printer(&de);
    433463                break;
    434464        case LS_DIR:
  • uspace/app/bdsh/cmds/modules/ls/ls.h

    r87ba3ceb ra27e370  
    88#define LS_FILE  1
    99#define LS_DIR   2
    10 
    11 typedef struct {
    12         /* Options set at runtime. */
    13         unsigned int recursive;
    14         unsigned int sort;
    15 
    16         bool single_column;
    17         bool well_formatted;
    18 } ls_job_t;
    1910
    2011/** Structure to represent a directory entry.
     
    2819};
    2920
     21typedef struct {
     22        /* Options set at runtime. */
     23        unsigned int recursive;
     24        unsigned int sort;
     25
     26        bool single_column;
     27        bool well_formatted;
     28
     29        void (*printer)(struct dir_elem_t *);
     30} ls_job_t;
     31
    3032#endif
Note: See TracChangeset for help on using the changeset viewer.