ANSI
[unix-history] / usr / src / usr.bin / wc / wc.c
index 5b9eeb0..64666d2 100644 (file)
@@ -2,17 +2,7 @@
  * Copyright (c) 1980, 1987 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1980, 1987 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -22,7 +12,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)wc.c       5.4 (Berkeley) %G%";
+static char sccsid[] = "@(#)wc.c       5.6 (Berkeley) %G%";
 #endif /* not lint */
 
 /* wc line, word and char count */
 #endif /* not lint */
 
 /* wc line, word and char count */
@@ -38,14 +28,15 @@ static char sccsid[] = "@(#)wc.c    5.4 (Berkeley) %G%";
 #define TAB    011                     /* tab char */
 
 static long    tlinect, twordct, tcharct;
 #define TAB    011                     /* tab char */
 
 static long    tlinect, twordct, tcharct;
-static int     doline, doword, dochar;
+static int     doline, doword, dochar;
 
 
-main(argc,argv)
+main(argc, argv)
        int argc;
        char **argv;
 {
        extern int optind;
        register int ch;
        int argc;
        char **argv;
 {
        extern int optind;
        register int ch;
+       int total;
 
        /*
         * wc is unusual in that its flags are on by default, so,
 
        /*
         * wc is unusual in that its flags are on by default, so,
@@ -53,7 +44,7 @@ main(argc,argv)
         * all on.
         */
        if (argc > 1 && argv[1][0] == '-' && argv[1][1]) {
         * all on.
         */
        if (argc > 1 && argv[1][0] == '-' && argv[1][1]) {
-               while ((ch = getopt(argc,argv,"lwc")) != EOF)
+               while ((ch = getopt(argc, argv, "lwc")) != EOF)
                        switch((char)ch) {
                        case 'l':
                                doline = 1;
                        switch((char)ch) {
                        case 'l':
                                doline = 1;
@@ -66,7 +57,7 @@ main(argc,argv)
                                break;
                        case '?':
                        default:
                                break;
                        case '?':
                        default:
-                               fputs("usage: wc [-lwc] [files]\n",stderr);
+                               fputs("usage: wc [-lwc] [files]\n", stderr);
                                exit(1);
                        }
                argv += optind;
                                exit(1);
                        }
                argv += optind;
@@ -78,42 +69,26 @@ main(argc,argv)
                doline = doword = dochar = 1;
        }
 
                doline = doword = dochar = 1;
        }
 
-       /* should print "stdin" as the file name, here */
-       if (argc <= 1) {
-               if (!*argv || !strcmp(*argv, "-")) {
-                       cnt((char *)NULL);
-                       putchar('\n');
-               }
-               else {
-                       cnt(*argv);
-                       printf(" %s\n", *argv);
-               }
-               exit(0);
+       total = 0;
+       if (!*argv) {
+               cnt((char *)NULL);
+               putchar('\n');
        }
        }
-
-       /*
-        * cat allows "-" as stdin anywhere in the arg list,
-        * might as well here, too.  Again, should use "stdin"
-        * as the file name.
-        */
-       do {
-               if (!strcmp(*argv, "-")) {
-                       cnt((char *)NULL);
-                       putchar('\n');
-               }
-               else {
-                       cnt(*argv);
-                       printf(" %s\n", *argv);
-               }
+       else do {
+               cnt(*argv);
+               printf(" %s\n", *argv);
+               ++total;
        } while(*++argv);
 
        } while(*++argv);
 
-       if (doline)
-               printf(" %7ld", tlinect);
-       if (doword)
-               printf(" %7ld", twordct);
-       if (dochar)
-               printf(" %7ld", tcharct);
-       puts(" total");
+       if (total > 1) {
+               if (doline)
+                       printf(" %7ld", tlinect);
+               if (doword)
+                       printf(" %7ld", twordct);
+               if (dochar)
+                       printf(" %7ld", tcharct);
+               puts(" total");
+       }
        exit(0);
 }
 
        exit(0);
 }
 
@@ -124,7 +99,7 @@ cnt(file)
        register u_char *C;
        register short gotsp;
        register int len;
        register u_char *C;
        register short gotsp;
        register int len;
-       register long linect, wordct,charct;    
+       register long linect, wordct, charct;
        struct stat sbuf;
        int fd;
        u_char buf[MAXBSIZE];
        struct stat sbuf;
        int fd;
        u_char buf[MAXBSIZE];
@@ -156,7 +131,7 @@ cnt(file)
                                printf(" %7ld", linect);
                                if (dochar) {
                                        tcharct += charct;
                                printf(" %7ld", linect);
                                if (dochar) {
                                        tcharct += charct;
-                                       printf(" %7ld", sbuf.st_size);
+                                       printf(" %7ld", charct);
                                }
                                close(fd);
                                return;
                                }
                                close(fd);
                                return;