[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ls -h patch



On Fri, 6 Jun 2003, Ian Darwin wrote:

> Yes, but No. We have fmt_scaled and scan_scaled in libutil.
> Please use them. We do not need to have every utility
> have its own slightly different code for handling "-h"-like formatting.
>
> Ian
>

Ah I wasn't aware of those functions, here is a patch that uses
fmt_scaled.

Jonathan

Index: bin/ls/Makefile
===================================================================
RCS file: /cvs/src/bin/ls/Makefile,v
retrieving revision 1.6
diff -u -r1.6 Makefile
--- bin/ls/Makefile	2000/07/19 19:27:35	1.6
+++ bin/ls/Makefile	2003/06/07 04:06:50
@@ -2,5 +2,7 @@

 PROG=	ls
 SRCS=	cmp.c ls.c main.c print.c util.c
+DPADD=	${LIBUTIL}
+LDADD=	-lutil

 .include <bsd.prog.mk>
Index: bin/ls/ls.1
===================================================================
RCS file: /cvs/src/bin/ls/ls.1,v
retrieving revision 1.36
diff -u -r1.36 ls.1
--- bin/ls/ls.1	2003/06/02 23:32:08	1.36
+++ bin/ls/ls.1	2003/06/07 04:06:51
@@ -41,7 +41,7 @@
 .Nd list directory contents
 .Sh SYNOPSIS
 .Nm ls
-.Op Fl 1ACFLRSTWacdfgiklmnopqrstux
+.Op Fl 1ACFLRSTWacdfghiklmnopqrstux
 .Op Ar file ...
 .Sh DESCRIPTION
 For each operand that names a
@@ -125,6 +125,12 @@
 .It Fl g
 Does nothing; kept for compatibility with older versions of
 .Nm ls .
+.It Fl h
+When used with the
+.Fl l
+option, use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte,
+Petabyte and Exabyte in order to reduce the number of digits to four or fewer
+using base 2 for sizes.
 .It Fl i
 For each file, print its inode number.
 .It Fl k
Index: bin/ls/ls.c
===================================================================
RCS file: /cvs/src/bin/ls/ls.c,v
retrieving revision 1.19
diff -u -r1.19 ls.c
--- bin/ls/ls.c	2003/06/02 23:32:08	1.19
+++ bin/ls/ls.c	2003/06/07 04:06:53
@@ -85,6 +85,7 @@
 int f_column;			/* columnated format */
 int f_columnacross;		/* columnated format, sorted across */
 int f_flags;			/* show flags associated with a file */
+int f_humanval;		/* show human-readable file sizes */
 int f_inode;			/* print inode */
 int f_listdir;			/* list actual directory, not contents */
 int f_listdot;			/* list files beginning with . */
@@ -134,7 +135,7 @@
 		f_listdot = 1;

 	fts_options = FTS_PHYSICAL;
-	while ((ch = getopt(argc, argv, "1ACFLRSTWacdfgiklmnopqrstux")) != -1) {
+	while ((ch = getopt(argc, argv, "1ACFLRSTWacdfghiklmnopqrstux")) != -1) {
 		switch (ch) {
 		/*
 		 * The -1, -C and -l, -m and -x options all override each
@@ -201,6 +202,9 @@
 			f_nosort = 1;
 			break;
 		case 'g':		/* Compatibility with 4.3BSD. */
+			break;
+		case 'h':
+			f_humanval = 1;
 			break;
 		case 'i':
 			f_inode = 1;
Index: bin/ls/ls.h
===================================================================
RCS file: /cvs/src/bin/ls/ls.h,v
retrieving revision 1.5
diff -u -r1.5 ls.h
--- bin/ls/ls.h	2003/06/02 23:32:08	1.5
+++ bin/ls/ls.h	2003/06/07 04:06:53
@@ -41,6 +41,7 @@

 extern int f_accesstime;	/* use time of last access */
 extern int f_flags;		/* show flags associated with a file */
+extern int f_humanval;		/* show human-readable file sizes */
 extern int f_inode;		/* print inode */
 extern int f_longform;		/* long listing format */
 extern int f_nonprint;		/* show unprintables as ? */
Index: bin/ls/print.c
===================================================================
RCS file: /cvs/src/bin/ls/print.c,v
retrieving revision 1.18
diff -u -r1.18 print.c
--- bin/ls/print.c	2003/06/02 23:32:08	1.18
+++ bin/ls/print.c	2003/06/07 04:06:53
@@ -55,12 +55,14 @@
 #include <time.h>
 #include <tzfile.h>
 #include <unistd.h>
+#include <util.h>

 #include "ls.h"
 #include "extern.h"

 static int	printaname(FTSENT *, u_long, u_long);
 static void	printlink(FTSENT *);
+static void	printsize(size_t, off_t);
 static void	printtime(time_t);
 static int	printtype(u_int);
 static int	compute_columns(DISPLAY *, int *);
@@ -116,7 +118,7 @@
 			(void)printf("%*s%*qd ",
 			    8 - dp->s_size, "", dp->s_size, sp->st_size);
 		else
-			(void)printf("%*qd ", dp->s_size, sp->st_size);
+			printsize(dp->s_size, sp->st_size);
 		if (f_accesstime)
 			printtime(sp->st_atime);
 		else if (f_statustime)
@@ -374,4 +376,18 @@
 	path[lnklen] = '\0';
 	(void)printf(" -> ");
 	(void)putname(path);
+}
+
+static void
+printsize(width, bytes)
+	size_t width;
+	off_t bytes;
+{
+	char ret[FMT_SCALED_STRSIZE];
+
+	if ((f_humanval) && (fmt_scaled(bytes, ret) != -1)) {
+		(void)printf("%*s ", (u_int)width, ret);
+		return;
+	}
+	(void)printf("%*qd ", (u_int)width, bytes);
 }
Index: bin/ls/util.c
===================================================================
RCS file: /cvs/src/bin/ls/util.c,v
retrieving revision 1.8
diff -u -r1.8 util.c
--- bin/ls/util.c	2003/06/02 23:32:08	1.8
+++ bin/ls/util.c	2003/06/07 04:06:53
@@ -68,7 +68,7 @@
 usage()
 {
 	(void)fprintf(stderr,
-	    "usage: %s [-1ACFLRSTWacdfiklmnopqrstux] [file ...]\n",
+	    "usage: %s [-1ACFLRSTWacdfhiklmnopqrstux] [file ...]\n",
              __progname);
 	exit(1);
 }