+ if (total > 1) {
+ if (doline)
+ (void)printf(" %7ld", tlinect);
+ if (doword)
+ (void)printf(" %7ld", twordct);
+ if (dochar)
+ (void)printf(" %7ld", tcharct);
+ (void)printf(" total\n");
+ }
+ exit(0);
+}
+
+void
+cnt(file)
+ char *file;
+{
+ register u_char *p;
+ register short gotsp;
+ register int ch, len;
+ register u_long linect, wordct, charct;
+ struct stat sb;
+ int fd;
+ u_char buf[MAXBSIZE];
+
+ fd = STDIN_FILENO;
+ linect = wordct = charct = 0;
+ if (file) {
+ if ((fd = open(file, O_RDONLY, 0)) < 0)
+ err("%s: %s", file, strerror(errno));
+ if (doword)
+ goto word;
+ /*
+ * Line counting is split out because it's a lot faster to get
+ * lines than to get words, since the word count requires some
+ * logic.
+ */
+ if (doline) {
+ while (len = read(fd, buf, MAXBSIZE)) {
+ if (len == -1)
+ err("%s: %s", file, strerror(errno));
+ charct += len;
+ for (p = buf; len--; ++p)
+ if (*p == '\n')
+ ++linect;
+ }
+ tlinect += linect;
+ (void)printf(" %7lu", linect);
+ if (dochar) {
+ tcharct += charct;
+ (void)printf(" %7lu", charct);
+ }
+ (void)close(fd);
+ return;