+ linect = wordct = charct = 0;
+ if (file) {
+ if ((fd = open(file,O_RDONLY)) < 0) {
+ perror(file);
+ exit(ERR);
+ }
+ if (!doword) {
+ /*
+ * 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) {
+ perror(file);
+ exit(ERR);
+ }
+ charct += len;
+ for (C = buf;len--;++C)
+ if (*C == '\n')
+ ++linect;
+ }
+ tlinect += linect;
+ printf(" %7ld",linect);
+ if (dochar) {
+ tcharct += charct;
+ printf(" %7ld",sbuf.st_size);
+ }
+ close(fd);
+ return;
+ }
+ /*
+ * if all we need is the number of characters and
+ * it's a directory or a regular or linked file, just
+ * stat the puppy. We avoid testing for it not being
+ * a special device in case someone adds a new type
+ * of inode.
+ */
+ if (dochar) {
+ if (fstat(fd,&sbuf)) {
+ perror(file);
+ exit(ERR);
+ }
+ if (sbuf.st_mode & (S_IFREG | S_IFLNK | S_IFDIR)) {
+ printf(" %7ld",sbuf.st_size);
+ tcharct += sbuf.st_size;
+ close(fd);
+ return;
+ }
+ }
+ }
+ }
+ else
+ fd = 0;
+ /* do it the hard way... */
+ for (gotsp = YES;len = read(fd,buf,MAXBSIZE);) {
+ if (len == -1) {
+ perror(file);
+ exit(ERR);
+ }
+ charct += len;
+ for (C = buf;len--;++C)
+ switch(*C) {
+ case NL:
+ ++linect;
+ case TAB:
+ case SPACE:
+ gotsp = YES;
+ continue;
+ default:
+#ifdef NOT_DEFINED
+ /*
+ * This line of code implements the
+ * original V7 wc algorithm, i.e.
+ * a non-printing character doesn't
+ * toggle the "word" count, so that
+ * " ^D^F " counts as 6 spaces,
+ * while "foo^D^Fbar" counts as 8
+ * characters.
+ *
+ * test order is important -- gotsp
+ * will normally be NO, so test it
+ * first
+ */
+ if (gotsp && *C > SPACE && *C < DEL) {
+#endif NOT_DEFINED
+ /*
+ * This line implements the manual
+ * page, i.e. a word is a "maximal
+ * string of characters delimited by
+ * spaces, tabs or newlines." Notice
+ * nothing was said about a character
+ * being printing or non-printing.
+ */
+ if (gotsp) {
+ gotsp = NO;
+ ++wordct;
+ }
+ }
+ }
+ if (doline) {
+ tlinect += linect;
+ printf(" %7ld",linect);
+ }
+ if (doword) {
+ twordct += wordct;
+ printf(" %7ld",wordct);
+ }
+ if (dochar) {
+ tcharct += charct;
+ printf(" %7ld",charct);