summaryrefslogtreecommitdiffstats
path: root/usr.bin/wc
diff options
context:
space:
mode:
authorpjd <pjd@FreeBSD.org>2010-05-16 21:06:26 +0000
committerpjd <pjd@FreeBSD.org>2010-05-16 21:06:26 +0000
commitb447cf7e7fe64bed9520500b0b555c13f09a87b3 (patch)
tree367b2e19ad88ddf021a4dd91058c262a9f4ec93a /usr.bin/wc
parent02e2e7806f669c5da2bd50002bb58f54c31f28da (diff)
downloadFreeBSD-src-b447cf7e7fe64bed9520500b0b555c13f09a87b3.zip
FreeBSD-src-b447cf7e7fe64bed9520500b0b555c13f09a87b3.tar.gz
On SIGINFO print current values on stderr.
Diffstat (limited to 'usr.bin/wc')
-rw-r--r--usr.bin/wc/wc.c112
1 files changed, 68 insertions, 44 deletions
diff --git a/usr.bin/wc/wc.c b/usr.bin/wc/wc.c
index 5b02474..7c3e3e4 100644
--- a/usr.bin/wc/wc.c
+++ b/usr.bin/wc/wc.c
@@ -63,11 +63,20 @@ __FBSDID("$FreeBSD$");
#include <wctype.h>
uintmax_t tlinect, twordct, tcharct, tlongline;
-int doline, doword, dochar, domulti, dolongline;
+int doline, doword, dochar, domulti, dolongline, siginfo;
+static void show_cnt(const char *file, uintmax_t linect, uintmax_t wordct,
+ uintmax_t charct, uintmax_t llct);
static int cnt(const char *);
static void usage(void);
+static void
+siginfo_handler(int sig __unused)
+{
+
+ siginfo = 1;
+}
+
int
main(int argc, char *argv[])
{
@@ -101,6 +110,8 @@ main(int argc, char *argv[])
argv += optind;
argc -= optind;
+ (void)signal(SIGINFO, siginfo_handler);
+
/* Wc's flags are on by default. */
if (doline + doword + dochar + domulti + dolongline == 0)
doline = doword = dochar = 1;
@@ -110,31 +121,46 @@ main(int argc, char *argv[])
if (!*argv) {
if (cnt((char *)NULL) != 0)
++errors;
- else
- (void)printf("\n");
+ } else {
+ do {
+ if (cnt(*argv) != 0)
+ ++errors;
+ ++total;
+ } while(*++argv);
}
- else do {
- if (cnt(*argv) != 0)
- ++errors;
- else
- (void)printf(" %s\n", *argv);
- ++total;
- } while(*++argv);
- if (total > 1) {
- if (doline)
- (void)printf(" %7ju", tlinect);
- if (doword)
- (void)printf(" %7ju", twordct);
- if (dochar || domulti)
- (void)printf(" %7ju", tcharct);
- if (dolongline)
- (void)printf(" %7ju", tlongline);
- (void)printf(" total\n");
- }
+ if (total > 1)
+ show_cnt("total", tlinect, twordct, tcharct, tlongline);
exit(errors == 0 ? 0 : 1);
}
+static void
+show_cnt(const char *file, uintmax_t linect, uintmax_t wordct,
+ uintmax_t charct, uintmax_t llct)
+{
+ FILE *out;
+
+ if (!siginfo)
+ out = stdout;
+ else {
+ out = stderr;
+ siginfo = 0;
+ }
+
+ if (doline)
+ (void)fprintf(out, " %7ju", linect);
+ if (doword)
+ (void)fprintf(out, " %7ju", wordct);
+ if (dochar || domulti)
+ (void)fprintf(out, " %7ju", charct);
+ if (dolongline)
+ (void)fprintf(out, " %7ju", llct);
+ if (file != NULL)
+ (void)fprintf(out, " %s\n", file);
+ else
+ (void)fprintf(out, "\n");
+}
+
static int
cnt(const char *file)
{
@@ -149,10 +175,9 @@ cnt(const char *file)
mbstate_t mbs;
linect = wordct = charct = llct = tmpll = 0;
- if (file == NULL) {
- file = "stdin";
+ if (file == NULL)
fd = STDIN_FILENO;
- } else {
+ else {
if ((fd = open(file, O_RDONLY, 0)) < 0) {
warn("%s: open", file);
return (1);
@@ -171,6 +196,10 @@ cnt(const char *file)
(void)close(fd);
return (1);
}
+ if (siginfo) {
+ show_cnt(file, linect, wordct, charct,
+ llct);
+ }
charct += len;
for (p = buf; len--; ++p)
if (*p == '\n') {
@@ -182,16 +211,13 @@ cnt(const char *file)
tmpll++;
}
tlinect += linect;
- (void)printf(" %7ju", linect);
- if (dochar) {
+ if (dochar)
tcharct += charct;
- (void)printf(" %7ju", charct);
- }
if (dolongline) {
if (llct > tlongline)
tlongline = llct;
- (void)printf(" %7ju", tlongline);
}
+ show_cnt(file, linect, wordct, charct, llct);
(void)close(fd);
return (0);
}
@@ -206,8 +232,9 @@ cnt(const char *file)
return (1);
}
if (S_ISREG(sb.st_mode)) {
- (void)printf(" %7lld", (long long)sb.st_size);
- tcharct += sb.st_size;
+ charct = sb.st_size;
+ show_cnt(file, linect, wordct, charct, llct);
+ tcharct += charct;
(void)close(fd);
return (0);
}
@@ -220,12 +247,14 @@ word: gotsp = 1;
memset(&mbs, 0, sizeof(mbs));
while ((len = read(fd, buf, MAXBSIZE)) != 0) {
if (len == -1) {
- warn("%s: read", file);
+ warn("%s: read", file != NULL ? file : "stdin");
(void)close(fd);
return (1);
}
p = buf;
while (len > 0) {
+ if (siginfo)
+ show_cnt(file, linect, wordct, charct, llct);
if (!domulti || MB_CUR_MAX == 1) {
clen = 1;
wch = (unsigned char)*p;
@@ -233,7 +262,8 @@ word: gotsp = 1;
(size_t)-1) {
if (!warned) {
errno = EILSEQ;
- warn("%s", file);
+ warn("%s",
+ file != NULL ? file : "stdin");
warned = 1;
}
memset(&mbs, 0, sizeof(mbs));
@@ -264,24 +294,18 @@ word: gotsp = 1;
}
if (domulti && MB_CUR_MAX > 1)
if (mbrtowc(NULL, NULL, 0, &mbs) == (size_t)-1 && !warned)
- warn("%s", file);
- if (doline) {
+ warn("%s", file != NULL ? file : "stdin");
+ if (doline)
tlinect += linect;
- (void)printf(" %7ju", linect);
- }
- if (doword) {
+ if (doword)
twordct += wordct;
- (void)printf(" %7ju", wordct);
- }
- if (dochar || domulti) {
+ if (dochar || domulti)
tcharct += charct;
- (void)printf(" %7ju", charct);
- }
if (dolongline) {
if (llct > tlongline)
tlongline = llct;
- (void)printf(" %7ju", llct);
}
+ show_cnt(file, linect, wordct, charct, llct);
(void)close(fd);
return (0);
}
OpenPOWER on IntegriCloud