Fix for missing total blocks when using -l with ls (TRY THIS PATCH INSTEAD)

jorgen at antistaten.se jorgen at antistaten.se
Fri Oct 2 10:14:56 UTC 2009


I've changed the patch a bit.
Now it's even tinyer(removed not needed code)
and it will work with '-R' option as well.
Since BusyBox 'ls' has taken the '-k' option for use with SELINUX
I haven't cared for the POSIX/GNU meaning of the '-k' option.
On my system it shows blocks in 1024 bytes as default.

Anyway, Anyone think I should add the [PATCH] word to the subject now? :-)
Or someone like to test it?

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-02 11:01:05.000000000 +0200
@@ -17,8 +17,7 @@
  * it more portable.
  *
  * KNOWN BUGS:
- * 1. ls -l of a directory doesn't give "total <blocks>" header
- * 2. hidden files can make column width too large
+ * 1. hidden files can make column width too large
  *
  * NON-OPTIMAL BEHAVIOUR:
  * 1. autowidth reads directories twice
@@ -245,7 +244,7 @@
 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);

 struct globals {
 #if ENABLE_FEATURE_LS_COLOR
@@ -617,6 +616,9 @@
 		}
 		subdnp = list_dir(dn[i]->fullname);
 		nfiles = countfiles(subdnp);
+		if (all_fmt & STYLE_LONG) {
+			printf("total %lu\n", (unsigned long)files_calculate_blocks(subdnp,
nfiles));
+		}
 		if (nfiles > 0) {
 			/* list all files at this level */
 			dnsort(subdnp, nfiles);
@@ -1059,3 +1061,17 @@
 		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;
+}
+


> POSIX says:
>
>    If any of the -l, -n, -s options is specified, each list
>    of files within the directory shall be preceded by a
>    status line indicating the number of file system blocks
>    occupied by files in the directory in 512-byte units if
>    the -k option is not specified, or 1024-byte units if the
>    -k option is specified, rounded up to the next integral
>    number of units, if necessary. In the POSIX locale, the
>    format shall be:
>
>    "total %u\n", <number of units in the directory>
>
> http://www.opengroup.org/onlinepubs/9699919799/utilities/ls.html
>
>
> HTH
> Johannes
>
>
> On Thu, Oct 01, 2009 at 07:46:17PM +0200, Jörgen Overgaard wrote:
>> Yes it will be doing a 'du' job.
>>
>> I do not know if that is some BSD or GNU extension.
>> All I know, is that the application I was trying to get running under
>> BusyBox, expected 'ls -l' to return the total stuff.
>> Also in the source for ls.c it says
>>
>> * KNOWN BUGS:
>> * 1. ls -l of a directory doesn't give "total <blocks>" header
>>
>> So I decided to "fix" that and put that patch on the mailing list.
>>
>> Wrong or right? I do not know. I am used to see that total header
>> when telling ls to do a long list format but never use it my self.
>>
>> Perhaps remove that BUG notice in the source of ls.c?
>> Or make it optional during compile to support totals or not from
>> configure? -DLS_LONG_WITH_TOTAL ;-)
>>
>> Regards
>> Jorgen Overgaard
>>
>> Cathey, Jim skrev:
>> >> I had some software that used ls -l and expected the total blocks
>> >> to be on the first line of the output from ls.
>> >
>> > That's a new one on me.  Is that some kind of BSD
>> > or GNU extension?  I'm not really in favor of it
>> > doing a 'du' job as well.
>> >
>> > -- Jim
>> >
>> >
>> >
>> >
>> > _______________________________________________
>> > busybox mailing list
>> > busybox at busybox.net
>> > http://lists.busybox.net/mailman/listinfo/busybox
>> >
>> > __________ NOD32 4473 (20091001) Information __________
>> >
>> > Detta meddelande är genomsökt av NOD32 Antivirus.
>> > http://www.nod32.com
>> >
>> >
>> >
>>
>> _______________________________________________
>> busybox mailing list
>> busybox at busybox.net
>> http://lists.busybox.net/mailman/listinfo/busybox
>>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: busybox-1.15.1-ls.patch
Type: application/octet-stream
Size: 1315 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20091002/65f4173b/attachment-0001.obj>


More information about the busybox mailing list