summaryrefslogtreecommitdiffstats
path: root/bin/ls
diff options
context:
space:
mode:
authorjoe <joe@FreeBSD.org>2001-12-28 20:50:12 +0000
committerjoe <joe@FreeBSD.org>2001-12-28 20:50:12 +0000
commitdfee532b860ae8392d001e56b14faf954a365fb8 (patch)
tree95cb44773e27fe8bfcef9a8f79767b90422dc3d9 /bin/ls
parentdde2b7683ece45d306f0d20e281e8ee844196689 (diff)
downloadFreeBSD-src-dfee532b860ae8392d001e56b14faf954a365fb8.zip
FreeBSD-src-dfee532b860ae8392d001e56b14faf954a365fb8.tar.gz
Add a new flag, -h which when combined with the -l option causes
file sizes to be displayed with unit suffixes; Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte in order to reduce the number of digits to three or less. Submitted by: nik
Diffstat (limited to 'bin/ls')
-rw-r--r--bin/ls/Makefile1
-rw-r--r--bin/ls/ls.128
-rw-r--r--bin/ls/ls.c6
-rw-r--r--bin/ls/ls.h1
-rw-r--r--bin/ls/print.c72
-rw-r--r--bin/ls/util.c4
6 files changed, 107 insertions, 5 deletions
diff --git a/bin/ls/Makefile b/bin/ls/Makefile
index 65286ef..1aa0b4e 100644
--- a/bin/ls/Makefile
+++ b/bin/ls/Makefile
@@ -4,6 +4,7 @@
PROG= ls
SRCS= cmp.c ls.c print.c util.c lomac.c
+LDADD= -lm
WARNS= 0
.if !defined(RELEASE_CRUNCH)
diff --git a/bin/ls/ls.1 b/bin/ls/ls.1
index be37844..4e892b0 100644
--- a/bin/ls/ls.1
+++ b/bin/ls/ls.1
@@ -43,7 +43,7 @@
.Nd list directory contents
.Sh SYNOPSIS
.Nm
-.Op Fl ABCFGHLPRTWZabcdfgiklnoqrstu1
+.Op Fl ABCFGHLPRTWZabcdfghiklnoqrstu1
.Op Ar
.Sh DESCRIPTION
For each operand that names a
@@ -146,6 +146,12 @@ with
it was used to display the group name in the long
.Pq Fl l
format output.
+.It Fl h
+When used wih the
+.Fl l
+option, use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte
+and Petabyte in order to reduce the number of digits to three or less
+using base 2 for sizes.
.It Fl i
For each file, print the file's file serial number (inode number).
.It Fl k
@@ -586,3 +592,23 @@ specification.
.Sh BUGS
To maintain backward compatibility, the relationships between the many
options are quite complex.
+
+
+
+
+***************
+*** 43,49 ****
+ .Nd list directory contents
+ .Sh SYNOPSIS
+ .Nm
+- .Op Fl ABCFGHLPRTWabcdfgiklnoqrstu1
+ .Op Ar
+ .Sh DESCRIPTION
+ For each operand that names a
+--- 43,49 ----
+ .Nd list directory contents
+ .Sh SYNOPSIS
+ .Nm
+ .Op Ar
+ .Sh DESCRIPTION
+ For each operand that names a
diff --git a/bin/ls/ls.c b/bin/ls/ls.c
index 7791d20..e4afb61 100644
--- a/bin/ls/ls.c
+++ b/bin/ls/ls.c
@@ -94,6 +94,7 @@ int termwidth = 80; /* default terminal width */
int f_accesstime; /* use time of last access */
int f_column; /* columnated format */
int f_flags; /* show flags associated with a file */
+int f_humanval; /* show human-readable file sizes */
int f_inode; /* print inode */
int f_kblocks; /* print size in kilobytes */
int f_listdir; /* list actual directory, not contents */
@@ -167,7 +168,7 @@ main(argc, argv)
f_listdot = 1;
fts_options = FTS_PHYSICAL;
- while ((ch = getopt(argc, argv, "1ABCFGHLPRTWZabcdfgiklnoqrstu")) != -1) {
+ while ((ch = getopt(argc, argv, "1ABCFGHLPRTWZabcdfghiklnoqrstu")) != -1) {
switch (ch) {
/*
* The -1, -C and -l options all override each other so shell
@@ -236,6 +237,9 @@ main(argc, argv)
break;
case 'g': /* Compatibility with 4.3BSD. */
break;
+ case 'h':
+ f_humanval = 1;
+ break;
case 'i':
f_inode = 1;
break;
diff --git a/bin/ls/ls.h b/bin/ls/ls.h
index fd75306..dfb54ea 100644
--- a/bin/ls/ls.h
+++ b/bin/ls/ls.h
@@ -44,6 +44,7 @@ extern long blocksize; /* block size units */
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_lomac; /* show LOMAC attributes */
extern int f_inode; /* print inode */
extern int f_longform; /* long listing format */
diff --git a/bin/ls/print.c b/bin/ls/print.c
index 209f620..74d30c5 100644
--- a/bin/ls/print.c
+++ b/bin/ls/print.c
@@ -50,6 +50,7 @@ static const char rcsid[] =
#include <errno.h>
#include <fts.h>
#include <grp.h>
+#include <math.h>
#include <langinfo.h>
#include <pwd.h>
#include <stdio.h>
@@ -70,12 +71,33 @@ static int printaname __P((FTSENT *, u_long, u_long));
static void printlink __P((FTSENT *));
static void printtime __P((time_t));
static int printtype __P((u_int));
+static void printsize __P((size_t, off_t));
#ifdef COLORLS
static void endcolor __P((int));
static int colortype __P((mode_t));
#endif
#define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
+#define UNITS_2 2
+
+#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))
+
+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 __P((off_t *));
+
+int unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
#ifdef COLORLS
/* Most of these are taken from <sys/stat.h> */
@@ -178,7 +200,7 @@ printlong(dp)
(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)
@@ -550,3 +572,51 @@ printlink(p)
(void)printf(" -> ");
printname(path);
}
+
+static void
+printsize(width, bytes)
+ size_t width;
+ off_t bytes;
+{
+ unit_t unit;
+
+ if (f_humanval) {
+ unit = unit_adjust(&bytes);
+
+ if (bytes == 0)
+ (void)printf("%*s ", width, "0B");
+ else
+ (void)printf("%*qd%c ", width - 1, bytes,
+ "BKMGTPE"[unit]);
+ }
+ else
+ (void)printf("%*qd ", 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.
+ *
+ */
+unit_t
+unit_adjust(val)
+ off_t *val;
+{
+ double abval;
+ unit_t unit;
+ unsigned int unit_sz;
+
+ abval = fabs(*val);
+
+ unit_sz = abval ? ilogb(abval) / 10 : 0;
+
+ if (unit_sz >= UNIT_MAX) {
+ unit = NONE;
+ } else {
+ unit = unitp[unit_sz];
+ *val /= (double)vals_base2[unit_sz];
+ }
+
+ return (unit);
+}
diff --git a/bin/ls/util.c b/bin/ls/util.c
index 84d106e..668d8e4 100644
--- a/bin/ls/util.c
+++ b/bin/ls/util.c
@@ -162,9 +162,9 @@ usage()
{
(void)fprintf(stderr,
#ifdef COLORLS
- "usage: ls [-ABCFGHLPRTWZabcdfgiklnoqrstu1]"
+ "usage: ls [-ABCFGHLPRTWZabcdfghiklnoqrstu1]"
#else
- "usage: ls [-ABCFHLPRTWZabcdfgiklnoqrstu1]"
+ "usage: ls [-ABCFHLPRTWZabcdfghiklnoqrstu1]"
#endif
" [file ...]\n");
exit(1);
OpenPOWER on IntegriCloud