add Berkeley header; format a little better
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Sun, 27 Dec 1987 14:04:40 +0000 (06:04 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Sun, 27 Dec 1987 14:04:40 +0000 (06:04 -0800)
SCCS-vsn: usr.bin/wc/wc.c 5.3

usr/src/usr.bin/wc/wc.c

index 11aa226..c64cae1 100644 (file)
@@ -1,18 +1,24 @@
 /*
 /*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * 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 this notice is preserved and that due credit is given
+ * to the University of California at 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'' without express or implied warranty.
  */
 
 #ifndef lint
 char copyright[] =
  */
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+"@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\
  All rights reserved.\n";
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)wc.c       5.2 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)wc.c       5.3 (Berkeley) %G%";
+#endif /* not lint */
 
 /* wc line, word and char count */
 
 
 /* wc line, word and char count */
 
@@ -22,28 +28,19 @@ static char sccsid[] = "@(#)wc.c    5.2 (Berkeley) %G%";
 #include <stdio.h>
 
 #define DEL    0177                    /* del char */
 #include <stdio.h>
 
 #define DEL    0177                    /* del char */
-#define ERR    1                       /* error exit */
 #define NL     012                     /* newline char */
 #define NL     012                     /* newline char */
-#define NO     0                       /* no/false */
-#define OK     0                       /* okay exit */
 #define SPACE  040                     /* space char */
 #define TAB    011                     /* tab char */
 #define SPACE  040                     /* space char */
 #define TAB    011                     /* tab char */
-#define YES    1                       /* yes/true */
 
 
-static long    tlinect,                /* total line count */
-               twordct,                /* total word count */
-               tcharct;                /* total character count */
-static short   doline,                 /* if want line count */
-               doword,                 /* if want word count */
-               dochar;                 /* if want character count */
+static long    tlinect, twordct, tcharct;
+static int     doline, doword, dochar;
 
 main(argc,argv)
 
 main(argc,argv)
-int    argc;
-char   **argv;
+       int argc;
+       char **argv;
 {
 {
-       extern char     *optarg;        /* getopt arguments */
-       extern int      optind;
-       register int    ch;             /* getopt character */
+       extern int optind;
+       register int ch;
 
        /*
         * 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,19 +50,19 @@ char        **argv;
        if (argc > 1 && argv[1][0] == '-' && argv[1][1]) {
                while ((ch = getopt(argc,argv,"lwc")) != EOF)
                        switch((char)ch) {
        if (argc > 1 && argv[1][0] == '-' && argv[1][1]) {
                while ((ch = getopt(argc,argv,"lwc")) != EOF)
                        switch((char)ch) {
-                               case 'l':
-                                       doline = YES;
-                                       break;
-                               case 'w':
-                                       doword = YES;
-                                       break;
-                               case 'c':
-                                       dochar = YES;
-                                       break;
-                               case '?':
-                               default:
-                                       fputs("Usage: wc [-lwc] [files]\n",stderr);
-                                       exit(ERR);
+                       case 'l':
+                               doline = 1;
+                               break;
+                       case 'w':
+                               doword = 1;
+                               break;
+                       case 'c':
+                               dochar = 1;
+                               break;
+                       case '?':
+                       default:
+                               fputs("usage: wc [-lwc] [files]\n",stderr);
+                               exit(1);
                        }
                argv += optind;
                argc -= optind;
                        }
                argv += optind;
                argc -= optind;
@@ -73,20 +70,20 @@ char        **argv;
        else {
                ++argv;
                --argc;
        else {
                ++argv;
                --argc;
-               doline = doword = dochar = YES;
+               doline = doword = dochar = 1;
        }
 
        /* should print "stdin" as the file name, here */
        if (argc <= 1) {
        }
 
        /* should print "stdin" as the file name, here */
        if (argc <= 1) {
-               if (!*argv || !strcmp(*argv,"-")) {
+               if (!*argv || !strcmp(*argv, "-")) {
                        cnt((char *)NULL);
                        putchar('\n');
                }
                else {
                        cnt(*argv);
                        cnt((char *)NULL);
                        putchar('\n');
                }
                else {
                        cnt(*argv);
-                       printf(" %s\n",*argv);
+                       printf(" %s\n", *argv);
                }
                }
-               exit(OK);
+               exit(0);
        }
 
        /*
        }
 
        /*
@@ -95,45 +92,43 @@ char        **argv;
         * as the file name.
         */
        do {
         * as the file name.
         */
        do {
-               if (!strcmp(*argv,"-")) {
+               if (!strcmp(*argv, "-")) {
                        cnt((char *)NULL);
                        putchar('\n');
                }
                else {
                        cnt(*argv);
                        cnt((char *)NULL);
                        putchar('\n');
                }
                else {
                        cnt(*argv);
-                       printf(" %s\n",*argv);
+                       printf(" %s\n", *argv);
                }
        } while(*++argv);
 
        if (doline)
                }
        } while(*++argv);
 
        if (doline)
-               printf(" %7ld",tlinect);
+               printf(" %7ld", tlinect);
        if (doword)
        if (doword)
-               printf(" %7ld",twordct);
+               printf(" %7ld", twordct);
        if (dochar)
        if (dochar)
-               printf(" %7ld",tcharct);
+               printf(" %7ld", tcharct);
        puts(" total");
        puts(" total");
-       exit(OK);
+       exit(0);
 }
 
 static
 cnt(file)
 }
 
 static
 cnt(file)
-char   *file;
+       char *file;
 {
 {
-       register u_char *C;             /* traveling pointer */
-       register short  gotsp;          /* space toggle */
-       register int    len;            /* length of read */
-       register long   linect,         /* line count */
-                       wordct,         /* word count */
-                       charct;         /* character count */
-       struct stat     sbuf;           /* stat buffer */
-       int     fd;                     /* file descriptor */
-       u_char  buf[MAXBSIZE];          /* read buffer */
+       register u_char *C;
+       register short gotsp;
+       register int len;
+       register long linect, wordct,charct;    
+       struct stat sbuf;
+       int fd;
+       u_char buf[MAXBSIZE];
 
        linect = wordct = charct = 0;
        if (file) {
 
        linect = wordct = charct = 0;
        if (file) {
-               if ((fd = open(file,O_RDONLY)) < 0) {
+               if ((fd = open(file, O_RDONLY, 0)) < 0) {
                        perror(file);
                        perror(file);
-                       exit(ERR);
+                       exit(1);
                }
                if (!doword) {
                        /*
                }
                if (!doword) {
                        /*
@@ -142,21 +137,21 @@ char      *file;
                         * the word count requires some logic.
                         */
                        if (doline) {
                         * the word count requires some logic.
                         */
                        if (doline) {
-                               while(len = read(fd,buf,MAXBSIZE)) {
+                               while(len = read(fd, buf, MAXBSIZE)) {
                                        if (len == -1) {
                                                perror(file);
                                        if (len == -1) {
                                                perror(file);
-                                               exit(ERR);
+                                               exit(1);
                                        }
                                        charct += len;
                                        }
                                        charct += len;
-                                       for (C = buf;len--;++C)
+                                       for (C = buf; len--; ++C)
                                                if (*C == '\n')
                                                        ++linect;
                                }
                                tlinect += linect;
                                                if (*C == '\n')
                                                        ++linect;
                                }
                                tlinect += linect;
-                               printf(" %7ld",linect);
+                               printf(" %7ld", linect);
                                if (dochar) {
                                        tcharct += charct;
                                if (dochar) {
                                        tcharct += charct;
-                                       printf(" %7ld",sbuf.st_size);
+                                       printf(" %7ld", sbuf.st_size);
                                }
                                close(fd);
                                return;
                                }
                                close(fd);
                                return;
@@ -169,12 +164,12 @@ char      *file;
                         * of inode.
                         */
                        if (dochar) {
                         * of inode.
                         */
                        if (dochar) {
-                               if (fstat(fd,&sbuf)) {
+                               if (fstat(fd, &sbuf)) {
                                        perror(file);
                                        perror(file);
-                                       exit(ERR);
+                                       exit(1);
                                }
                                if (sbuf.st_mode & (S_IFREG | S_IFLNK | S_IFDIR)) {
                                }
                                if (sbuf.st_mode & (S_IFREG | S_IFLNK | S_IFDIR)) {
-                                       printf(" %7ld",sbuf.st_size);
+                                       printf(" %7ld", sbuf.st_size);
                                        tcharct += sbuf.st_size;
                                        close(fd);
                                        return;
                                        tcharct += sbuf.st_size;
                                        close(fd);
                                        return;
@@ -185,22 +180,22 @@ char      *file;
        else
                fd = 0;
        /* do it the hard way... */
        else
                fd = 0;
        /* do it the hard way... */
-       for (gotsp = YES;len = read(fd,buf,MAXBSIZE);) {
+       for (gotsp = 1; len = read(fd, buf, MAXBSIZE);) {
                if (len == -1) {
                        perror(file);
                if (len == -1) {
                        perror(file);
-                       exit(ERR);
+                       exit(1);
                }
                charct += len;
                }
                charct += len;
-               for (C = buf;len--;++C)
+               for (C = buf; len--; ++C)
                        switch(*C) {
                                case NL:
                                        ++linect;
                                case TAB:
                                case SPACE:
                        switch(*C) {
                                case NL:
                                        ++linect;
                                case TAB:
                                case SPACE:
-                                       gotsp = YES;
+                                       gotsp = 1;
                                        continue;
                                default:
                                        continue;
                                default:
-#ifdef NOT_DEFINED
+#ifdef notdef
                                        /*
                                         * This line of code implements the
                                         * original V7 wc algorithm, i.e.
                                        /*
                                         * This line of code implements the
                                         * original V7 wc algorithm, i.e.
@@ -215,7 +210,7 @@ char        *file;
                                         * first
                                         */
                                        if (gotsp && *C > SPACE && *C < DEL) {
                                         * first
                                         */
                                        if (gotsp && *C > SPACE && *C < DEL) {
-#endif NOT_DEFINED
+#endif
                                        /*
                                         * This line implements the manual
                                         * page, i.e. a word is a "maximal
                                        /*
                                         * This line implements the manual
                                         * page, i.e. a word is a "maximal
@@ -225,22 +220,22 @@ char      *file;
                                         * being printing or non-printing.
                                         */
                                        if (gotsp) {
                                         * being printing or non-printing.
                                         */
                                        if (gotsp) {
-                                               gotsp = NO;
+                                               gotsp = 0;
                                                ++wordct;
                                        }
                        }
        }
        if (doline) {
                tlinect += linect;
                                                ++wordct;
                                        }
                        }
        }
        if (doline) {
                tlinect += linect;
-               printf(" %7ld",linect);
+               printf(" %7ld", linect);
        }
        if (doword) {
                twordct += wordct;
        }
        if (doword) {
                twordct += wordct;
-               printf(" %7ld",wordct);
+               printf(" %7ld", wordct);
        }
        if (dochar) {
                tcharct += charct;
        }
        if (dochar) {
                tcharct += charct;
-               printf(" %7ld",charct);
+               printf(" %7ld", charct);
        }
        close(fd);
 }
        }
        close(fd);
 }