ls: code shrink
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 13 May 2011 15:55:08 +0000 (17:55 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 13 May 2011 15:55:08 +0000 (17:55 +0200)
function                                             old     new   delta
sort_and_display_files                                 -     427    +427
scan_and_display_dirs_recur                          497     488      -9
ls_main                                              765     735     -30
display_files                                        422       -    -422
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 0/2 up/down: 427/-461)          Total: -34 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
coreutils/ls.c

index 90795507726b862927676a22042fbbec8a95573e..e7b2e39e1f9cf10af71a5784b8aa9943d6f06456 100644 (file)
@@ -910,8 +910,15 @@ static void dnsort(struct dnode **dn, int size)
 {
        qsort(dn, size, sizeof(*dn), sortcmp);
 }
+
+static void sort_and_display_files(struct dnode **dn, unsigned nfiles)
+{
+       dnsort(dn, nfiles);
+       display_files(dn, nfiles);
+}
 #else
-#define dnsort(dn, size) ((void)0)
+# define dnsort(dn, size) ((void)0)
+# define sort_and_display_files(dn, nfiles) display_files(dn, nfiles)
 #endif
 
 /* Returns NULL-terminated malloced vector of pointers (or NULL) */
@@ -1022,8 +1029,7 @@ static void scan_and_display_dirs_recur(struct dnode **dn, int first)
 #endif
                if (nfiles > 0) {
                        /* list all files at this level */
-                       dnsort(subdnp, nfiles);
-                       display_files(subdnp, nfiles);
+                       sort_and_display_files(subdnp, nfiles);
 
                        if (ENABLE_FEATURE_LS_RECURSIVE
                         && (G.all_fmt & DISP_RECURSIVE)
@@ -1216,16 +1222,14 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
        }
 
        if (G.all_fmt & DISP_NOLIST) {
-               dnsort(dnp, nfiles);
-               display_files(dnp, nfiles);
+               sort_and_display_files(dnp, nfiles);
        } else {
                dnd = splitdnarray(dnp, SPLIT_DIR);
                dnf = splitdnarray(dnp, SPLIT_FILE);
                dndirs = count_dirs(dnp, SPLIT_DIR);
                dnfiles = nfiles - dndirs;
                if (dnfiles > 0) {
-                       dnsort(dnf, dnfiles);
-                       display_files(dnf, dnfiles);
+                       sort_and_display_files(dnf, dnfiles);
                        if (ENABLE_FEATURE_CLEAN_UP)
                                free(dnf);
                }