Changeset a716a75 in mainline


Ignore:
Timestamp:
2011-03-25T23:32:57Z (13 years ago)
Author:
Jerome Portal <jeromeportal1@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
99b65d2
Parents:
47f7adfc
Message:

add alphabetical sort for ls command

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

Legend:

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

    r47f7adfc ra716a75  
    5151static const char *cmdname = "ls";
    5252
     53/** Sort array of files/directories
     54 *
     55 * Sort an array containing files and directories,
     56 * in alphabetical order, with files first.
     57 *
     58 * @param d                     Current directory.
     59 * @param tab           Array of file/directories to sort.
     60 * @param nbdirs        Number of elements.
     61 *
     62 */
     63static void ls_sort_dir(const char *d, char ** tab, int nbdirs)
     64{
     65        int i = 0;
     66        int j = 0;
     67        int min = 0;
     68        int rc;
     69        char * buff1 = NULL;
     70        char * buff2 = NULL;
     71        char * tmp = NULL;
     72        struct stat s1;
     73        struct stat s2;
     74       
     75        buff1 = (char *)malloc(PATH_MAX);
     76        if (NULL == buff1) {
     77                cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
     78                return;
     79        }
     80       
     81        buff2 = (char *)malloc(PATH_MAX);
     82        if (NULL == buff2) {
     83                cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
     84                return;
     85        }
     86       
     87        for(i=0;i<nbdirs;i++) {
     88                min = i;
     89               
     90                for(j=i;j<nbdirs;j++) {
     91                        memset(buff1, 0, sizeof(buff1));
     92                        memset(buff2, 0, sizeof(buff2));
     93                        snprintf(buff1, PATH_MAX - 1, "%s/%s", d, tab[min]);
     94                        snprintf(buff2, PATH_MAX - 1, "%s/%s", d, tab[j]);
     95                       
     96                        rc = stat(buff1, &s1);
     97                        if (rc != 0) {
     98                                printf("ls: skipping bogus node %s\n", buff1);
     99                                printf("rc=%d\n", rc);
     100                                return;
     101                        }
     102                       
     103                        rc = stat(buff2, &s2);
     104                        if (rc != 0) {
     105                                printf("ls: skipping bogus node %s\n", buff2);
     106                                printf("rc=%d\n", rc);
     107                                return;
     108                        }
     109                       
     110                        if ((s2.is_file && s1.is_directory)
     111                                || (((s2.is_file && s1.is_file)
     112                                || (s2.is_directory && s1.is_directory))
     113                                && str_cmp(tab[min], tab[j]) > 0))
     114                            min = j;
     115                }
     116               
     117                tmp = tab[i];
     118                tab[i] = tab[min];
     119                tab[min] = tmp;
     120        }
     121       
     122        free(buff1);
     123        free(buff2);
     124       
     125        return;
     126}
     127
     128/** Scan a directory.
     129 *
     130 * Scan the content of a directory and print it.
     131 *
     132 * @param d             Name of the directory.
     133 * @param dirp  Directory stream.
     134 *
     135 */
    53136static void ls_scan_dir(const char *d, DIR *dirp)
    54137{
    55         struct dirent *dp;
    56         char *buff;
     138        int alloc_blocks = 20;
     139        int i = 0;
     140        int nbdirs = 0;
     141        char * buff = NULL;
     142        char ** tmp = NULL;
     143        char ** tosort = NULL;
     144        struct dirent * dp = NULL;
    57145
    58146        if (! dirp)
     
    64152                return;
    65153        }
    66 
     154       
     155        tosort = (char **)malloc(alloc_blocks*sizeof(char *));
     156        if (NULL == tosort) {
     157                cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
     158                return;
     159        }
     160        memset(tosort, 0, sizeof(tosort));
     161       
    67162        while ((dp = readdir(dirp))) {
     163                nbdirs++;
     164               
     165                if (nbdirs > alloc_blocks) {
     166                        alloc_blocks += alloc_blocks;
     167                       
     168                        tmp = (char **)realloc(tosort, (alloc_blocks)*sizeof(char *));
     169                        if (NULL == tmp) {
     170                                cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
     171                                return;
     172                        }
     173                       
     174                        tosort = tmp;
     175                }
     176               
     177                tosort[nbdirs-1] = (char *)malloc(str_length(dp->d_name)+1);
     178                if (NULL == tosort[nbdirs-1]) {
     179                                cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
     180                                return;
     181                }
     182                memset(tosort[nbdirs-1], 0, str_length(dp->d_name)+1);
     183               
     184                str_cpy(tosort[nbdirs-1], str_length(dp->d_name)+1, dp->d_name);
     185        }
     186       
     187        ls_sort_dir(d, tosort, nbdirs);
     188       
     189        for(i=0;i<nbdirs;i++) {
    68190                memset(buff, 0, sizeof(buff));
    69191                /* Don't worry if inserting a double slash, this will be fixed by
    70192                 * absolutize() later with subsequent calls to open() or readdir() */
    71                 snprintf(buff, PATH_MAX - 1, "%s/%s", d, dp->d_name);
    72                 ls_print(dp->d_name, buff);
    73         }
    74 
     193                snprintf(buff, PATH_MAX - 1, "%s/%s", d, tosort[i]);
     194                ls_print(tosort[i], buff);
     195                free(tosort[i]);
     196        }
     197       
     198        free(tosort);
    75199        free(buff);
    76200
  • uspace/app/bdsh/cmds/modules/ls/ls.h

    r47f7adfc ra716a75  
    77#define LS_DIR   2
    88
     9static void ls_sort_dir(const char *, char **, int);
    910static void ls_scan_dir(const char *, DIR *);
    1011static void ls_print(const char *, const char *);
Note: See TracChangeset for help on using the changeset viewer.