summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Espie <espie@cvs.openbsd.org>2005-10-19 21:49:03 +0000
committerMarc Espie <espie@cvs.openbsd.org>2005-10-19 21:49:03 +0000
commit7cc95b102041316892accf5739dc60268f4bb330 (patch)
tree8c736f3d0dc5f59b3ce9bcfa21b2573417954dfd
parent707c5ec4585e3bb51404500092f7e9ae49cc7766 (diff)
Add -h option to display human-readable numbers.
okay otto@, deraadt@, jmc@. (note that is mostly useless from scripts, hence okay as a non-standard option).
-rw-r--r--usr.bin/wc/Makefile4
-rw-r--r--usr.bin/wc/wc.114
-rw-r--r--usr.bin/wc/wc.c34
3 files changed, 40 insertions, 12 deletions
diff --git a/usr.bin/wc/Makefile b/usr.bin/wc/Makefile
index 657cf724cfa..3f3c619362a 100644
--- a/usr.bin/wc/Makefile
+++ b/usr.bin/wc/Makefile
@@ -1,5 +1,7 @@
-# $OpenBSD: Makefile,v 1.2 1996/06/26 05:42:49 deraadt Exp $
+# $OpenBSD: Makefile,v 1.3 2005/10/19 21:49:02 espie Exp $
PROG= wc
+DPADD= ${LIBUTIL}
+LDADD= -lutil
.include <bsd.prog.mk>
diff --git a/usr.bin/wc/wc.1 b/usr.bin/wc/wc.1
index c32e07e74c6..c841add4ba5 100644
--- a/usr.bin/wc/wc.1
+++ b/usr.bin/wc/wc.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: wc.1,v 1.15 2004/12/19 11:51:30 jmc Exp $
+.\" $OpenBSD: wc.1,v 1.16 2005/10/19 21:49:02 espie Exp $
.\"
.\" Copyright (c) 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -41,7 +41,7 @@
.Sh SYNOPSIS
.Nm wc
.Op Fl c | m
-.Op Fl lw
+.Op Fl hlw
.Op Ar file ...
.Sh DESCRIPTION
The
@@ -64,6 +64,10 @@ The options are as follows:
.It Fl c
The number of bytes in each input file
is written to the standard output.
+.It Fl h
+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 l
The number of lines in each input file
is written to the standard output.
@@ -139,6 +143,12 @@ The
.Nm
utility conforms to
.St -p1003.2-92 .
+.Pp
+The
+.Fl h
+option is an
+.Ox
+extension.
.Sh HISTORY
A
.Nm
diff --git a/usr.bin/wc/wc.c b/usr.bin/wc/wc.c
index b4d0329ffa8..bbfc7909964 100644
--- a/usr.bin/wc/wc.c
+++ b/usr.bin/wc/wc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: wc.c,v 1.10 2005/04/11 07:04:47 deraadt Exp $ */
+/* $OpenBSD: wc.c,v 1.11 2005/10/19 21:49:02 espie Exp $ */
/*
* Copyright (c) 1980, 1987, 1991, 1993
@@ -39,7 +39,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)wc.c 8.2 (Berkeley) 5/2/95";
#else
-static char rcsid[] = "$OpenBSD: wc.c,v 1.10 2005/04/11 07:04:47 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: wc.c,v 1.11 2005/10/19 21:49:02 espie Exp $";
#endif
#endif /* not lint */
@@ -53,9 +53,10 @@ static char rcsid[] = "$OpenBSD: wc.c,v 1.10 2005/04/11 07:04:47 deraadt Exp $";
#include <sys/stat.h>
#include <sys/file.h>
#include <unistd.h>
+#include <util.h>
int64_t tlinect, twordct, tcharct;
-int doline, doword, dochar;
+int doline, doword, dochar, humanchar;
int rval;
extern char *__progname;
@@ -69,7 +70,7 @@ main(int argc, char *argv[])
setlocale(LC_ALL, "");
- while ((ch = getopt(argc, argv, "lwcm")) != -1)
+ while ((ch = getopt(argc, argv, "lwchm")) != -1)
switch((char)ch) {
case 'l':
doline = 1;
@@ -81,10 +82,13 @@ main(int argc, char *argv[])
case 'm':
dochar = 1;
break;
+ case 'h':
+ humanchar = 1;
+ break;
case '?':
default:
(void)fprintf(stderr,
- "usage: %s [-c | -m] [-lw] [file ...]\n",
+ "usage: %s [-c | -m] [-hlw] [file ...]\n",
__progname);
exit(1);
}
@@ -236,16 +240,28 @@ cnt(char *file)
}
}
+void
+format_and_print(long long v)
+{
+ if (humanchar) {
+ char result[FMT_SCALED_STRSIZE];
+
+ (void)fmt_scaled(v, result);
+ (void)printf("%7s", result);
+ } else {
+ (void)printf(" %7lld", v);
+ }
+}
+
void
print_counts(int64_t lines, int64_t words, int64_t chars, char *name)
{
-
if (doline)
- (void)printf(" %7lld", (long long)lines);
+ format_and_print((long long)lines);
if (doword)
- (void)printf(" %7lld", (long long)words);
+ format_and_print((long long)words);
if (dochar)
- (void)printf(" %7lld", (long long)chars);
+ format_and_print((long long)chars);
(void)printf(" %s\n", name);
}