Annotation of 43BSD/ucb/wc.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1980 Regents of the University of California.
                      3:  * All rights reserved.  The Berkeley software License Agreement
                      4:  * specifies the terms and conditions for redistribution.
                      5:  */
                      6: 
                      7: #ifndef lint
                      8: char copyright[] =
                      9: "@(#) Copyright (c) 1980 Regents of the University of California.\n\
                     10:  All rights reserved.\n";
                     11: #endif not lint
                     12: 
                     13: #ifndef lint
                     14: static char sccsid[] = "@(#)wc.c       5.1 (Berkeley) 5/31/85";
                     15: #endif not lint
                     16: 
                     17: /* wc line and word count */
                     18: 
                     19: #include <stdio.h>
                     20: long   linect, wordct, charct, pagect;
                     21: long   tlinect, twordct, tcharct, tpagect;
                     22: char   *wd = "lwc";
                     23: 
                     24: main(argc, argv)
                     25: char **argv;
                     26: {
                     27:        int i, token;
                     28:        register FILE *fp;
                     29:        register int c;
                     30:        char *p;
                     31: 
                     32:        while (argc > 1 && *argv[1] == '-') {
                     33:                switch (argv[1][1]) {
                     34:                case 'l': case 'w': case 'c': 
                     35:                        wd = argv[1]+1;
                     36:                        break;
                     37:                default:
                     38:                usage:
                     39:                        fprintf(stderr, "Usage: wc [-lwc] [files]\n");
                     40:                        exit(1);
                     41:                }
                     42:                argc--;
                     43:                argv++;
                     44:        }
                     45: 
                     46:        i = 1;
                     47:        fp = stdin;
                     48:        do {
                     49:                if(argc>1 && (fp=fopen(argv[i], "r")) == NULL) {
                     50:                        perror(argv[i]);
                     51:                        continue;
                     52:                }
                     53:                linect = 0;
                     54:                wordct = 0;
                     55:                charct = 0;
                     56:                token = 0;
                     57:                for(;;) {
                     58:                        c = getc(fp);
                     59:                        if (c == EOF)
                     60:                                break;
                     61:                        charct++;
                     62:                        if(' '<c&&c<0177) {
                     63:                                if(!token) {
                     64:                                        wordct++;
                     65:                                        token++;
                     66:                                }
                     67:                                continue;
                     68:                        }
                     69:                        if(c=='\n') {
                     70:                                linect++;
                     71:                        }
                     72:                        else if(c!=' '&&c!='\t')
                     73:                                continue;
                     74:                        token = 0;
                     75:                }
                     76:                /* print lines, words, chars */
                     77:                wcp(wd, charct, wordct, linect);
                     78:                if(argc>1) {
                     79:                        printf(" %s\n", argv[i]);
                     80:                } else
                     81:                        printf("\n");
                     82:                fclose(fp);
                     83:                tlinect += linect;
                     84:                twordct += wordct;
                     85:                tcharct += charct;
                     86:        } while(++i<argc);
                     87:        if(argc > 2) {
                     88:                wcp(wd, tcharct, twordct, tlinect);
                     89:                printf(" total\n");
                     90:        }
                     91:        exit(0);
                     92: }
                     93: 
                     94: wcp(wd, charct, wordct, linect)
                     95: register char *wd;
                     96: long charct; long wordct; long linect;
                     97: {
                     98:        while (*wd) switch (*wd++) {
                     99:        case 'l':
                    100:                ipr(linect);
                    101:                break;
                    102: 
                    103:        case 'w':
                    104:                ipr(wordct);
                    105:                break;
                    106: 
                    107:        case 'c':
                    108:                ipr(charct);
                    109:                break;
                    110: 
                    111:        }
                    112: }
                    113: 
                    114: ipr(num)
                    115: long num;
                    116: {
                    117:        printf(" %7ld", num);
                    118: }
                    119: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.