svn commit: r208170 - head/usr.bin/wc

Pawel Jakub Dawidek pjd at FreeBSD.org
Sun May 16 21:06:27 UTC 2010


Author: pjd
Date: Sun May 16 21:06:26 2010
New Revision: 208170
URL: http://svn.freebsd.org/changeset/base/208170

Log:
  On SIGINFO print current values on stderr.

Modified:
  head/usr.bin/wc/wc.c

Modified: head/usr.bin/wc/wc.c
==============================================================================
--- head/usr.bin/wc/wc.c	Sun May 16 20:43:17 2010	(r208169)
+++ head/usr.bin/wc/wc.c	Sun May 16 21:06:26 2010	(r208170)
@@ -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;
-		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");
+	} else {
+		do {
+			if (cnt(*argv) != 0)
+				++errors;
+			++total;
+		} while(*++argv);
 	}
+
+	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);
 }


More information about the svn-src-all mailing list