Index: Makefile =================================================================== RCS file: /home/ncvs/src/bin/ls/Makefile,v retrieving revision 1.9.2.2 diff -u -r1.9.2.2 Makefile --- Makefile 4 Jul 2000 18:45:27 -0000 1.9.2.2 +++ Makefile 20 Dec 2001 12:09:27 -0000 @@ -4,6 +4,7 @@ PROG= ls SRCS= cmp.c ls.c print.c util.c +LDADD= -lm .if !defined(RELEASE_BUILD_FIXIT) CFLAGS+= -DCOLORLS Index: ls.1 =================================================================== RCS file: /home/ncvs/src/bin/ls/ls.1,v retrieving revision 1.33.2.13 diff -u -r1.33.2.13 ls.1 --- ls.1 16 Aug 2001 10:01:05 -0000 1.33.2.13 +++ ls.1 20 Dec 2001 12:32:07 -0000 @@ -43,7 +43,7 @@ .Nd list directory contents .Sh SYNOPSIS .Nm -.Op Fl ABCFGHLPRTWabcdfgiklnoqrstu1 +.Op Fl ABCFGHLPRTWabcdfghiklnoqrstu1 .Op Ar .Sh DESCRIPTION For each operand that names a @@ -142,6 +142,12 @@ 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 Index: ls.c =================================================================== RCS file: /home/ncvs/src/bin/ls/ls.c,v retrieving revision 1.32.2.4 diff -u -r1.32.2.4 ls.c --- ls.c 16 Aug 2000 19:57:11 -0000 1.32.2.4 +++ ls.c 20 Dec 2001 11:57:46 -0000 @@ -93,6 +93,7 @@ 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 */ @@ -163,7 +164,7 @@ f_listdot = 1; fts_options = FTS_PHYSICAL; - while ((ch = getopt(argc, argv, "1ABCFGHLPRTWabcdfgiklnoqrstu")) != -1) { + while ((ch = getopt(argc, argv, "1ABCFGHLPRTWabcdfghiklnoqrstu")) != -1) { switch (ch) { /* * The -1, -C and -l options all override each other so shell @@ -231,6 +232,9 @@ f_nosort = 1; break; case 'g': /* Compatibility with 4.3BSD. */ + break; + case 'h': + f_humanval = 1; break; case 'i': f_inode = 1; Index: ls.h =================================================================== RCS file: /home/ncvs/src/bin/ls/ls.h,v retrieving revision 1.11.2.2 diff -u -r1.11.2.2 ls.h --- ls.h 12 Jul 2000 06:19:14 -0000 1.11.2.2 +++ ls.h 20 Dec 2001 11:58:59 -0000 @@ -44,6 +44,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_octal; /* print unprintables in octal */ Index: print.c =================================================================== RCS file: /home/ncvs/src/bin/ls/print.c,v retrieving revision 1.19.2.3 diff -u -r1.19.2.3 print.c --- print.c 12 Jul 2000 06:19:14 -0000 1.19.2.3 +++ print.c 20 Dec 2001 12:27:54 -0000 @@ -50,6 +50,7 @@ #include #include #include +#include #include #include #include @@ -69,12 +70,33 @@ 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 */ @@ -170,7 +192,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) @@ -509,4 +531,52 @@ path[lnklen] = '\0'; (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); } Index: util.c =================================================================== RCS file: /home/ncvs/src/bin/ls/util.c,v retrieving revision 1.20.2.3 diff -u -r1.20.2.3 util.c --- util.c 7 Aug 2000 08:05:08 -0000 1.20.2.3 +++ util.c 20 Dec 2001 12:28:58 -0000 @@ -162,9 +162,9 @@ { (void)fprintf(stderr, #ifdef COLORLS - "usage: ls [-ABCFGHLPRTWabcdfgiklnoqrstu1]" + "usage: ls [-ABCFGHLPRTWabcdfghiklnoqrstu1]" #else - "usage: ls [-ABCFHLPRTWabcdfgiklnoqrstu1]" + "usage: ls [-ABCFHLPRTWabcdfghiklnoqrstu1]" #endif " [file ...]\n"); exit(1);