[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
ls -h patch
- To: tech_(_at_)_openbsd_(_dot_)_org
- Subject: ls -h patch
- From: Jonathan Gray <khalek_(_at_)_scummvm_(_dot_)_org>
- Date: Fri, 06 Jun 2003 19:20:25 +1000
hi,
Below is a patch to ls that adds the -h (human readable flag).
Its based on the version of ls in FreeBSD.
The patch can also be found here:
http://users.bigpond.net.au/tgray2/ls-humanval.patch
thanks
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/06 07:57:56
@@ -2,5 +2,7 @@
PROG= ls
SRCS= cmp.c ls.c main.c print.c util.c
+DPADD= ${LIBM}
+LDADD= -lm
.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/06 07:57:57
@@ -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
+and Petabyte 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/06 07:57:58
@@ -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/06 07:57:58
@@ -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/06 07:57:58
@@ -48,6 +48,7 @@
#include <errno.h>
#include <fts.h>
#include <grp.h>
+#include <math.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
@@ -61,12 +62,33 @@
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 *);
#define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
+#define KILO_SZ(n) (n)
+#define MEGA_SZ(n) ((n) * (n))
+#define GIGA_SZ(n) ((n) * (n) * (n))
+#define TERA_SZ(n) ((n) * (n) * (n) * (n))
+#define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
+
+#define KILO_2_SZ (KILO_SZ(1024ULL))
+#define MEGA_2_SZ (MEGA_SZ(1024ULL))
+#define GIGA_2_SZ (GIGA_SZ(1024ULL))
+#define TERA_2_SZ (TERA_SZ(1024ULL))
+#define PETA_2_SZ (PETA_SZ(1024ULL))
+
+static unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
+
+typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
+
+static unit_t unit_adjust(double *);
+
+static unit_t unitp[] = {NONE, KILO, MEGA, GIGA, TERA, PETA};
+
void
printscol(dp)
DISPLAY *dp;
@@ -116,7 +138,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 +396,53 @@
path[lnklen] = '\0';
(void)printf(" -> ");
(void)putname(path);
+}
+
+static void
+printsize(width, bytes)
+ size_t width;
+ off_t bytes;
+{
+ double dbytes;
+ unit_t unit;
+
+ if (f_humanval) {
+ dbytes = bytes;
+ unit = unit_adjust(&dbytes);
+
+ if (dbytes == 0)
+ (void)printf("%*s ", (u_int)width, "0B");
+ else
+ (void)printf("%*.*f%c ", (u_int)width - 1,
+ dbytes > 10 ? 0 : 1, dbytes, "BKMGTPE"[unit]);
+ } else
+ (void)printf("%*qd ", (u_int)width, bytes);
+}
+
+/*
+ * Output in "human-readable" format. Uses 3 digits max and puts
+ * unit suffixes at the end. Makes output compact and easy to read,
+ * especially on huge disks.
+ *
+ */
+static unit_t
+unit_adjust(val)
+ double *val;
+{
+ double abval;
+ unit_t unit;
+ u_int unit_sz;
+
+ abval = fabs(*val);
+
+ unit_sz = abval ? (u_int)ilogb(abval) / 10 : 0;
+
+ if (unit_sz >= (u_int)UNIT_MAX)
+ unit = NONE;
+ else {
+ unit = unitp[unit_sz];
+ *val /= (double)vals_base2[unit_sz];
+ }
+
+ return (unit);
}
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/06 07:57:59
@@ -68,7 +68,7 @@
usage()
{
(void)fprintf(stderr,
- "usage: %s [-1ACFLRSTWacdfiklmnopqrstux] [file ...]\n",
+ "usage: %s [-1ACFLRSTWacdfhiklmnopqrstux] [file ...]\n",
__progname);
exit(1);
}
Visit your host, monkey.org