Hi all,

I had some software that used ls -l and expected the total blocks
to be on the first line of the output from ls.
Since BusyBox ls do not provide it (in ls.c it is mentioned as the
first of known bugs) I decided to put together something that provided
the "total <blocks>"

This is my first doing anything with the BusyBox source (except just
compiling it.. *lol*) so bare with me as I am not familiar with
BusyBox coding scheme/style... And of course, it might include bugs. :D

Anyway, my way of saying thanks for a great piece of software!

Regards
Jorgen Overgaard

--- busybox-1.15.1/coreutils/ls.c       2009-09-12 17:55:36.000000000 +0200
+++ busybox-1.15.1-mine/coreutils/ls.c  2009-10-01 16:18:55.000000000 +0200
@@ -245,7 +245,8 @@
 static struct dnode **list_dir(const char *);
 static struct dnode **dnalloc(int);
 static int list_single(const struct dnode *);
-
+static blkcnt_t files_calculate_blocks(struct dnode **dn, int nfiles);
+static blkcnt_t dirs_calculate_blocks(struct dnode **dn, int ndirs);

 struct globals {
 #if ENABLE_FEATURE_LS_COLOR
@@ -1049,6 +1050,9 @@
                                free(dnf);
                }
                if (dndirs > 0) {
+                       if (all_fmt & STYLE_LONG) {
+                               printf("total %ld\n", (long 
int)dirs_calculate_blocks(dnd, dndirs));
+                       }
                        dnsort(dnd, dndirs);
                        showdirs(dnd, dndirs, dnfiles == 0);
                        if (ENABLE_FEATURE_CLEAN_UP)
@@ -1059,3 +1063,35 @@
                dfree(dnp, nfiles);
        return exit_code;
 }
+
+static blkcnt_t files_calculate_blocks(struct dnode **dn, int nfiles)
+{
+       int i;
+       blkcnt_t blocks;
+
+       blocks = 0;
+       for (i = 0; i < nfiles; i++) {
+               blocks += (dn[i]->dstat.st_blocks >> 1);
+       }
+
+       return blocks;
+}
+
+static blkcnt_t dirs_calculate_blocks(struct dnode **dn, int ndirs)
+{
+       int i, nfiles;
+       blkcnt_t blocks;
+       struct dnode **subdnp;
+
+       blocks = 0;
+       for (i = 0; i < ndirs; i++) {
+               subdnp = list_dir(dn[i]->fullname);
+               nfiles = countfiles(subdnp);
+               if (nfiles > 0) {
+                       blocks += files_calculate_blocks(subdnp, nfiles);
+               }
+       }
+
+       return blocks;
+}
+

Attachment: busybox-1.15.1-ls.patch
Description: Binary data

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to