diff options
author | Ted Unangst <tedu@cvs.openbsd.org> | 2003-08-06 19:09:10 +0000 |
---|---|---|
committer | Ted Unangst <tedu@cvs.openbsd.org> | 2003-08-06 19:09:10 +0000 |
commit | d92a7531e29e5c40b09b72f2ed1bf69182cce1e2 (patch) | |
tree | dd030c7f4f25dab76dd28b01077b773eeee38057 | |
parent | fb87035a8dfc1d956ee9b28618912110c4a5b4ed (diff) |
-h, human readable sizes. from a jonathon gray mail to tech@
ok ian@ millert@
-rw-r--r-- | bin/ls/Makefile | 4 | ||||
-rw-r--r-- | bin/ls/ls.1 | 9 | ||||
-rw-r--r-- | bin/ls/ls.c | 10 | ||||
-rw-r--r-- | bin/ls/ls.h | 3 | ||||
-rw-r--r-- | bin/ls/print.c | 20 | ||||
-rw-r--r-- | bin/ls/util.c | 8 |
6 files changed, 40 insertions, 14 deletions
diff --git a/bin/ls/Makefile b/bin/ls/Makefile index 09fa966184d..defd6071b95 100644 --- a/bin/ls/Makefile +++ b/bin/ls/Makefile @@ -1,6 +1,8 @@ -# $OpenBSD: Makefile,v 1.6 2000/07/19 19:27:35 mickey Exp $ +# $OpenBSD: Makefile,v 1.7 2003/08/06 19:09:09 tedu Exp $ PROG= ls SRCS= cmp.c ls.c main.c print.c util.c +DPADD= ${LIBUTIL} +LDADD= -lutil .include <bsd.prog.mk> diff --git a/bin/ls/ls.1 b/bin/ls/ls.1 index 5456626b6ca..f1e3badec04 100644 --- a/bin/ls/ls.1 +++ b/bin/ls/ls.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: ls.1,v 1.36 2003/06/02 23:32:08 millert Exp $ +.\" $OpenBSD: ls.1,v 1.37 2003/08/06 19:09:09 tedu Exp $ .\" $NetBSD: ls.1,v 1.14 1995/12/05 02:44:01 jtc Exp $ .\" .\" Copyright (c) 1980, 1990, 1991, 1993, 1994 @@ -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,11 @@ Output is not sorted. .It Fl g Does nothing; kept for compatibility with older versions of .Nm ls . +.It Fl h +When used with a long format +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 powers of 2 for sizes (K=1024, M=1048576, etc.). .It Fl i For each file, print its inode number. .It Fl k diff --git a/bin/ls/ls.c b/bin/ls/ls.c index 0e3ce3c9682..51098067b1d 100644 --- a/bin/ls/ls.c +++ b/bin/ls/ls.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ls.c,v 1.20 2003/06/11 23:42:12 deraadt Exp $ */ +/* $OpenBSD: ls.c,v 1.21 2003/08/06 19:09:09 tedu Exp $ */ /* $NetBSD: ls.c,v 1.18 1996/07/09 09:16:29 mycroft Exp $ */ /* @@ -43,7 +43,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)ls.c 8.7 (Berkeley) 8/5/94"; #else -static char rcsid[] = "$OpenBSD: ls.c,v 1.20 2003/06/11 23:42:12 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: ls.c,v 1.21 2003/08/06 19:09:09 tedu Exp $"; #endif #endif /* not lint */ @@ -85,6 +85,7 @@ int f_accesstime; /* use time of last access */ 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 . */ @@ -132,7 +133,7 @@ ls_main(int argc, char *argv[]) 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 @@ -200,6 +201,9 @@ ls_main(int argc, char *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 a68a66f1833..2c1c65310d1 100644 --- a/bin/ls/ls.h +++ b/bin/ls/ls.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ls.h,v 1.6 2003/07/02 21:19:33 deraadt Exp $ */ +/* $OpenBSD: ls.h,v 1.7 2003/08/06 19:09:09 tedu Exp $ */ /* $NetBSD: ls.h,v 1.7 1995/03/21 09:06:33 cgd Exp $ */ /* @@ -41,6 +41,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_inode; /* print inode */ extern int f_longform; /* long listing format */ extern int f_nonprint; /* show unprintables as ? */ diff --git a/bin/ls/print.c b/bin/ls/print.c index 90b65f751a8..30a05eaf6e9 100644 --- a/bin/ls/print.c +++ b/bin/ls/print.c @@ -1,4 +1,4 @@ -/* $OpenBSD: print.c,v 1.19 2003/06/11 23:42:12 deraadt Exp $ */ +/* $OpenBSD: print.c,v 1.20 2003/08/06 19:09:09 tedu Exp $ */ /* $NetBSD: print.c,v 1.15 1996/12/11 03:25:39 thorpej Exp $ */ /* @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94"; #else -static char rcsid[] = "$OpenBSD: print.c,v 1.19 2003/06/11 23:42:12 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: print.c,v 1.20 2003/08/06 19:09:09 tedu Exp $"; #endif #endif /* not lint */ @@ -55,12 +55,14 @@ static char rcsid[] = "$OpenBSD: print.c,v 1.19 2003/06/11 23:42:12 deraadt Exp #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 *); @@ -114,7 +116,7 @@ printlong(DISPLAY *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) @@ -363,3 +365,15 @@ printlink(FTSENT *p) (void)printf(" -> "); (void)putname(path); } + +static void +printsize(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); +} diff --git a/bin/ls/util.c b/bin/ls/util.c index 0ce468231d0..f99600a284b 100644 --- a/bin/ls/util.c +++ b/bin/ls/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.10 2003/07/29 00:24:15 deraadt Exp $ */ +/* $OpenBSD: util.c,v 1.11 2003/08/06 19:09:09 tedu Exp $ */ /* $NetBSD: util.c,v 1.12 1995/09/07 06:43:02 jtc Exp $ */ /* @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)util.c 8.5 (Berkeley) 4/28/95"; #else -static char rcsid[] = "$OpenBSD: util.c,v 1.10 2003/07/29 00:24:15 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: util.c,v 1.11 2003/08/06 19:09:09 tedu Exp $"; #endif #endif /* not lint */ @@ -67,7 +67,7 @@ void usage(void) { (void)fprintf(stderr, - "usage: %s [-1ACFLRSTWacdfiklmnopqrstux] [file ...]\n", - __progname); + "usage: %s [-1ACFLRSTWacdfhiklmnopqrstux] [file ...]\n", + __progname); exit(1); } |