[BusyBox] df, add -P and -B

Bernhard Fischer rep.nop at aon.at
Mon Aug 15 22:13:17 UTC 2005


Hi,

still needs some love, i know.

Comments?

-------------- next part --------------
Index: coreutils/df.c
===================================================================
--- coreutils/df.c	(revision 11159)
+++ coreutils/df.c	(working copy)
@@ -21,7 +21,7 @@
  *
  */
 
-/* BB_AUDIT SUSv3 _NOT_ compliant -- options -P and -t missing.  Also blocksize. */
+/* BB_AUDIT SUSv3 _NOT_ compliant -- option -t missing. */
 /* http://www.opengroup.org/onlinepubs/007904975/utilities/df.html */
 
 /* Mar 16, 2003      Manuel Novoa III   (mjn3 at codepoet.org)
@@ -29,6 +29,10 @@
  * Size reduction.  Removed floating point dependency.  Added error checking
  * on output.  Output stats on 0-sized filesystems if specifically listed on
  * the command line.  Properly round *-blocks, Used, and Available quantities.
+ *
+ * Aug 15, 2005      Bernhard Fischer   (busybox at busybox.net)
+ *
+ * Implement -P and -B.
  */
 
 #include <stdio.h>
@@ -40,9 +44,9 @@
 #include "busybox.h"
 
 #ifndef CONFIG_FEATURE_HUMAN_READABLE
-static long kscale(long b, long bs)
+static unsigned long long kscale(long b, long bs, unsigned long df_disp_bs)
 {
-	return ( b * (long long) bs + KILOBYTE/2 ) / KILOBYTE;
+	return ( b * (long long) bs + df_disp_bs/2 ) / df_disp_bs;
 }
 #endif
 
@@ -50,36 +54,72 @@ extern int df_main(int argc, char **argv
 {
 	long blocks_used;
 	long blocks_percent_used;
-#ifdef CONFIG_FEATURE_HUMAN_READABLE
-	unsigned long df_disp_hr = KILOBYTE;
-#endif
-	int status = EXIT_SUCCESS;
+	unsigned long df_disp_bs = KILOBYTE;
+	char *df_opt_bs;
+	short status = EXIT_SUCCESS;
 	unsigned long opt;
-	FILE *mount_table;
+	FILE *mount_table = NULL;
 	struct mntent *mount_entry;
 	struct statfs s;
-	static const char hdr_1k[] = "1k-blocks"; /* default display is kilobytes */
-	const char *disp_units_hdr = hdr_1k;
+	char *disp_units_hdr = " 1k-blocks"; /* default is kilobytes */
+	char *disp_use_hdr = "Use%%";
 
 #ifdef CONFIG_FEATURE_HUMAN_READABLE
-	bb_opt_complementaly = "h-km:k-hm:m-hk";
-	opt = bb_getopt_ulflags(argc, argv, "hmk");
-	if(opt & 1) {
-				df_disp_hr = 0;
-				disp_units_hdr = "     Size";
+	bb_opt_complementaly = "h-km:k-hm:m-hk:P-hm:B-mk";
+#define OPT_h (opt & 1)
+#define OPT_m (opt & 2)
+#define OPT_k (opt & 4)
+#define OPT_P (opt & 8)
+#define OPT_B (opt & 16)
+#else
+	bb_opt_complementaly = "B-k";
+#define OPT_k (opt & 1)
+#define OPT_P (opt & 2)
+#define OPT_B (opt & 4)
+#endif
+
+#ifdef CONFIG_FEATURE_HUMAN_READABLE
+	opt = bb_getopt_ulflags(argc, argv, "hmkPB:", &df_opt_bs);
+
+	if (OPT_h) {
+				df_disp_bs = KILOBYTE;
+				disp_units_hdr = "      Size";
 	}
-	if(opt & 2) {
-				df_disp_hr = MEGABYTE;
-				disp_units_hdr = "1M-blocks";
+	if (OPT_m) {
+				df_disp_bs = MEGABYTE;
+				disp_units_hdr = " 1M-blocks";
 	}
 #else
-	opt = bb_getopt_ulflags(argc, argv, "k");
+	opt = bb_getopt_ulflags(argc, argv, "kPB:",&df_opt_bs);
 #endif
+	/* first see if -P was given... */
+	if (OPT_P) {
+				df_disp_bs = 512;
+				disp_units_hdr = "512-blocks";
+				disp_use_hdr = "Capacity";
+	}
+	/* ... then see if -P should print in 512 or in 1024 byte blocks. */
+	if (OPT_k) {
+				df_disp_bs = KILOBYTE;
+				disp_units_hdr = " 1k-blocks";
+	}
+	/* finally see if a blocksize was given */
+	if (OPT_B) {
+				df_disp_bs = bb_xgetularg10_bnd(df_opt_bs, 0, INT_MAX);
+				bb_xasprintf(&disp_units_hdr,
+#ifndef SOMEONE_FIXED_make_human_readable_str
+						"%ld-blocks", df_disp_bs);
+#else
+						"%s-blocks",
+						make_human_readable_str(df_disp_bs,1,1));
+// XXX these should work, but they don't :
+//						make_human_readable_str(df_disp_bs,1,0));
+//						  should give e.g. 512B (i.e. 'B')
+//						make_human_readable_str(df_disp_bs,df_disp_bs,df_disp_bs));
+//						  should probably give zero_and_units too.
+#endif
+	}
 
-	bb_printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n",
-			  "", disp_units_hdr);
-
-	mount_table = NULL;
 	argv += optind;
 	if (optind >= argc) {
 		if (!(mount_table = setmntent(bb_path_mtab_file, "r"))) {
@@ -87,6 +127,9 @@ extern int df_main(int argc, char **argv
 		}
 	}
 
+	bb_printf("Filesystem%10s%-16sUsed Available %s Mounted on\n",
+			  "", disp_units_hdr, disp_use_hdr);
+
 	do {
 		const char *device;
 		const char *mount_point;
@@ -117,6 +160,7 @@ extern int df_main(int argc, char **argv
 		}
 
 		if ((s.f_blocks > 0) || !mount_table){
+
 			blocks_used = s.f_blocks - s.f_bfree;
 			blocks_percent_used = 0;
 			if (blocks_used + s.f_bavail) {
@@ -137,28 +181,30 @@ extern int df_main(int argc, char **argv
 
 #ifdef CONFIG_FEATURE_HUMAN_READABLE
 			bb_printf("%-21s%9s ", device,
-					  make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
+					  make_human_readable_str(s.f_blocks, s.f_bsize,
+											  df_disp_bs));
 
 			bb_printf("%9s ",
 					  make_human_readable_str( (s.f_blocks - s.f_bfree),
-											  s.f_bsize, df_disp_hr));
+											  s.f_bsize, df_disp_bs));
 
 			bb_printf("%9s %3ld%% %s\n",
-					  make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
+					  make_human_readable_str(s.f_bavail, s.f_bsize,
+											  df_disp_bs),
 					  blocks_percent_used, mount_point);
 #else
-			bb_printf("%-21s%9ld %9ld %9ld %3ld%% %s\n",
+			bb_printf("%-21s%9lld %9lld %9lld %3ld%% %s\n",
 					  device,
-					  kscale(s.f_blocks, s.f_bsize),
-					  kscale(s.f_blocks-s.f_bfree, s.f_bsize),
-					  kscale(s.f_bavail, s.f_bsize),
+					  kscale(s.f_blocks, s.f_bsize, df_disp_bs),
+					  kscale(s.f_blocks-s.f_bfree, s.f_bsize, df_disp_bs),
+					  kscale(s.f_bavail, s.f_bsize, df_disp_bs),
 					  blocks_percent_used, mount_point);
 #endif
 		}
 
 	} while (1);
 
-	bb_fflush_stdout_and_exit(status);
+	bb_fflush_stdout_and_exit((int)status);
 }
 
 /*
Index: include/usage.h
===================================================================
--- include/usage.h	(revision 11159)
+++ include/usage.h	(working copy)
@@ -416,15 +416,17 @@
 #  define USAGE_NOT_HUMAN_READABLE(a) a
 #endif
 #define df_trivial_usage \
-	"[-" USAGE_HUMAN_READABLE("hm") USAGE_NOT_HUMAN_READABLE("") "k] [FILESYSTEM ...]"
+	"[-" USAGE_HUMAN_READABLE("hm") USAGE_NOT_HUMAN_READABLE("") "kPB] [FILESYSTEM ...]"
 #define df_full_usage \
 	"Print the filesystem space used and space available.\n\n" \
 	"Options:\n" \
 	USAGE_HUMAN_READABLE( \
-	"\n\t-h\tprint sizes in human readable format (e.g., 1K 243M 2G )\n" \
+	"\n\t-h\tprint sizes in human readable format (e.g., 1K 243M 2G)\n" \
 	"\t-m\tprint sizes in megabytes\n" \
-	"\t-k\tprint sizes in kilobytes(default)") USAGE_NOT_HUMAN_READABLE( \
-	"\n\t-k\tprint sizes in kilobytes(compatibility)")
+	"\t-k\tprint sizes in kilobytes (default)") USAGE_NOT_HUMAN_READABLE( \
+	"\n\t-k\tprint sizes in kilobytes(compatibility)") \
+	"\n\t-P\tuse the POSIX output format" \
+	"\n\t-B SIZE\tuse SIZE-byte blocks"
 #define df_example_usage \
 	"$ df\n" \
 	"Filesystem           1k-blocks      Used Available Use% Mounted on\n" \



More information about the busybox mailing list