summaryrefslogtreecommitdiff
path: root/usr.bin/wc
diff options
context:
space:
mode:
authorSimon J. Gerraty <sjg@FreeBSD.org>2014-11-19 01:07:58 +0000
committerSimon J. Gerraty <sjg@FreeBSD.org>2014-11-19 01:07:58 +0000
commit9268022b74279434ed6300244e3f977e56a8ceb5 (patch)
tree377ac0ac449528621eb192cd245adadb5fd53668 /usr.bin/wc
parent29c34e9d2781cf25403647fb5af7d7ddb23be7e1 (diff)
parent8c3d6a4ab2a4a95d864d9a32d0157d7de90498a4 (diff)
downloadsrc-test-9268022b74279434ed6300244e3f977e56a8ceb5.tar.gz
src-test-9268022b74279434ed6300244e3f977e56a8ceb5.zip
Merge from head@274682
Notes
Notes: svn path=/projects/bmake/; revision=274683
Diffstat (limited to 'usr.bin/wc')
-rw-r--r--usr.bin/wc/Makefile3
-rw-r--r--usr.bin/wc/wc.17
-rw-r--r--usr.bin/wc/wc.c54
3 files changed, 45 insertions, 19 deletions
diff --git a/usr.bin/wc/Makefile b/usr.bin/wc/Makefile
index 4fa9f30f79121..6c671353be69c 100644
--- a/usr.bin/wc/Makefile
+++ b/usr.bin/wc/Makefile
@@ -2,4 +2,7 @@
# $FreeBSD$
PROG= wc
+DPADD= ${LIBXO}
+LDADD= -lxo
+
.include <bsd.prog.mk>
diff --git a/usr.bin/wc/wc.1 b/usr.bin/wc/wc.1
index afcccb00e22d5..ee81e1744ff29 100644
--- a/usr.bin/wc/wc.1
+++ b/usr.bin/wc/wc.1
@@ -31,7 +31,7 @@
.\" @(#)wc.1 8.2 (Berkeley) 4/19/94
.\" $FreeBSD$
.\"
-.Dd December 6, 2008
+.Dd November 4, 2014
.Dt WC 1
.Os
.Sh NAME
@@ -39,6 +39,7 @@
.Nd word, line, character, and byte count
.Sh SYNOPSIS
.Nm
+.Op Fl -libxo
.Op Fl Lclmw
.Op Ar
.Sh DESCRIPTION
@@ -174,7 +175,9 @@ option of the GNU
.Nm
utility.
.Sh SEE ALSO
-.Xr iswspace 3
+.Xr iswspace 3 ,
+.Xr libxo 3 ,
+.Xr xo_parse_args 3
.Sh STANDARDS
The
.Nm
diff --git a/usr.bin/wc/wc.c b/usr.bin/wc/wc.c
index 08823a132b699..79ac4a020f3d6 100644
--- a/usr.bin/wc/wc.c
+++ b/usr.bin/wc/wc.c
@@ -57,10 +57,12 @@ __FBSDID("$FreeBSD$");
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>
+#include <libxo/xo.h>
static uintmax_t tlinect, twordct, tcharct, tlongline;
static int doline, doword, dochar, domulti, dolongline;
static volatile sig_atomic_t siginfo;
+static xo_handle_t *stderr_handle;
static void show_cnt(const char *file, uintmax_t linect, uintmax_t wordct,
uintmax_t charct, uintmax_t llct);
@@ -81,6 +83,10 @@ main(int argc, char *argv[])
(void) setlocale(LC_CTYPE, "");
+ argc = xo_parse_args(argc, argv);
+ if (argc < 0)
+ return (argc);
+
while ((ch = getopt(argc, argv, "clmwL")) != -1)
switch((char)ch) {
case 'l':
@@ -113,21 +119,35 @@ main(int argc, char *argv[])
if (doline + doword + dochar + domulti + dolongline == 0)
doline = doword = dochar = 1;
+ stderr_handle = xo_create_to_file(stderr, XO_STYLE_TEXT, 0);
+ xo_open_container("wc");
+ xo_open_list("file");
+
errors = 0;
total = 0;
if (!*argv) {
+ xo_open_instance("file");
if (cnt((char *)NULL) != 0)
++errors;
+ xo_close_instance("file");
} else {
do {
+ xo_open_instance("file");
if (cnt(*argv) != 0)
++errors;
+ xo_close_instance("file");
++total;
} while(*++argv);
}
- if (total > 1)
+ if (total > 1) {
+ xo_open_container("total");
show_cnt("total", tlinect, twordct, tcharct, tlongline);
+ xo_close_container("total");
+ }
+ xo_close_list("file");
+ xo_close_container("wc");
+ xo_finish();
exit(errors == 0 ? 0 : 1);
}
@@ -135,27 +155,27 @@ static void
show_cnt(const char *file, uintmax_t linect, uintmax_t wordct,
uintmax_t charct, uintmax_t llct)
{
- FILE *out;
+ xo_handle_t *xop;
if (!siginfo)
- out = stdout;
+ xop = NULL;
else {
- out = stderr;
+ xop = stderr_handle;
siginfo = 0;
}
if (doline)
- (void)fprintf(out, " %7ju", linect);
+ xo_emit_h(xop, " {:lines/%7ju/%ju}", linect);
if (doword)
- (void)fprintf(out, " %7ju", wordct);
+ xo_emit_h(xop, " {:words/%7ju/%ju}", wordct);
if (dochar || domulti)
- (void)fprintf(out, " %7ju", charct);
+ xo_emit_h(xop, " {:characters/%7ju/%ju}", charct);
if (dolongline)
- (void)fprintf(out, " %7ju", llct);
+ xo_emit_h(xop, " {:long-lines/%7ju/%ju}", llct);
if (file != NULL)
- (void)fprintf(out, " %s\n", file);
+ xo_emit_h(xop, " {:filename/%s}\n", file);
else
- (void)fprintf(out, "\n");
+ xo_emit_h(xop, "\n");
}
static int
@@ -176,7 +196,7 @@ cnt(const char *file)
fd = STDIN_FILENO;
else {
if ((fd = open(file, O_RDONLY, 0)) < 0) {
- warn("%s: open", file);
+ xo_warn("%s: open", file);
return (1);
}
if (doword || (domulti && MB_CUR_MAX != 1))
@@ -189,7 +209,7 @@ cnt(const char *file)
if (doline) {
while ((len = read(fd, buf, MAXBSIZE))) {
if (len == -1) {
- warn("%s: read", file);
+ xo_warn("%s: read", file);
(void)close(fd);
return (1);
}
@@ -224,7 +244,7 @@ cnt(const char *file)
*/
if (dochar || domulti) {
if (fstat(fd, &sb)) {
- warn("%s: fstat", file);
+ xo_warn("%s: fstat", file);
(void)close(fd);
return (1);
}
@@ -244,7 +264,7 @@ word: gotsp = 1;
memset(&mbs, 0, sizeof(mbs));
while ((len = read(fd, buf, MAXBSIZE)) != 0) {
if (len == -1) {
- warn("%s: read", file != NULL ? file : "stdin");
+ xo_warn("%s: read", file != NULL ? file : "stdin");
(void)close(fd);
return (1);
}
@@ -259,7 +279,7 @@ word: gotsp = 1;
(size_t)-1) {
if (!warned) {
errno = EILSEQ;
- warn("%s",
+ xo_warn("%s",
file != NULL ? file : "stdin");
warned = 1;
}
@@ -291,7 +311,7 @@ word: gotsp = 1;
}
if (domulti && MB_CUR_MAX > 1)
if (mbrtowc(NULL, NULL, 0, &mbs) == (size_t)-1 && !warned)
- warn("%s", file != NULL ? file : "stdin");
+ xo_warn("%s", file != NULL ? file : "stdin");
if (doline)
tlinect += linect;
if (doword)
@@ -310,6 +330,6 @@ word: gotsp = 1;
static void
usage(void)
{
- (void)fprintf(stderr, "usage: wc [-Lclmw] [file ...]\n");
+ xo_error("usage: wc [-Lclmw] [file ...]\n");
exit(1);
}