[BACK]Return to wc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / wc

Annotation of src/usr.bin/wc/wc.c, Revision 1.19

1.19    ! deraadt     1: /*     $OpenBSD: wc.c,v 1.18 2015/10/03 14:39:25 deraadt Exp $ */
1.2       deraadt     2:
1.1       deraadt     3: /*
1.3       millert     4:  * Copyright (c) 1980, 1987, 1991, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt     6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.9       millert    15:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
1.17      deraadt    32: #include <sys/param.h> /* MAXBSIZE */
                     33: #include <sys/stat.h>
                     34: #include <sys/file.h>
1.1       deraadt    35: #include <stdio.h>
                     36: #include <stdlib.h>
                     37: #include <string.h>
                     38: #include <locale.h>
                     39: #include <ctype.h>
1.3       millert    40: #include <err.h>
1.1       deraadt    41: #include <unistd.h>
1.11      espie      42: #include <util.h>
1.1       deraadt    43:
1.3       millert    44: int64_t        tlinect, twordct, tcharct;
1.11      espie      45: int    doline, doword, dochar, humanchar;
1.3       millert    46: int    rval;
                     47: extern char *__progname;
                     48:
1.7       millert    49: void   print_counts(int64_t, int64_t, int64_t, char *);
1.14      deraadt    50: void   format_and_print(long long);
1.7       millert    51: void   cnt(char *);
1.1       deraadt    52:
                     53: int
1.8       deraadt    54: main(int argc, char *argv[])
1.1       deraadt    55: {
1.6       mpech      56:        int ch;
1.1       deraadt    57:
                     58:        setlocale(LC_ALL, "");
1.18      deraadt    59:
1.19    ! deraadt    60:        if (pledge("stdio rpath", NULL) == -1)
        !            61:                err(1, "pledge");
1.1       deraadt    62:
1.11      espie      63:        while ((ch = getopt(argc, argv, "lwchm")) != -1)
1.16      okan       64:                switch(ch) {
1.1       deraadt    65:                case 'l':
                     66:                        doline = 1;
                     67:                        break;
                     68:                case 'w':
                     69:                        doword = 1;
                     70:                        break;
                     71:                case 'c':
                     72:                case 'm':
                     73:                        dochar = 1;
                     74:                        break;
1.11      espie      75:                case 'h':
                     76:                        humanchar = 1;
                     77:                        break;
1.1       deraadt    78:                case '?':
                     79:                default:
1.3       millert    80:                        (void)fprintf(stderr,
1.11      espie      81:                            "usage: %s [-c | -m] [-hlw] [file ...]\n",
1.3       millert    82:                            __progname);
1.1       deraadt    83:                        exit(1);
                     84:                }
                     85:        argv += optind;
                     86:        argc -= optind;
                     87:
                     88:        /*
                     89:         * wc is unusual in that its flags are on by default, so,
                     90:         * if you don't get any arguments, you have to turn them
                     91:         * all on.
                     92:         */
1.3       millert    93:        if (!doline && !doword && !dochar)
1.1       deraadt    94:                doline = doword = dochar = 1;
                     95:
                     96:        if (!*argv) {
                     97:                cnt((char *)NULL);
                     98:        } else {
                     99:                int dototal = (argc > 1);
                    100:
                    101:                do {
                    102:                        cnt(*argv);
                    103:                } while(*++argv);
                    104:
1.3       millert   105:                if (dototal)
1.10      deraadt   106:                        print_counts(tlinect, twordct, tcharct, "total");
1.1       deraadt   107:        }
                    108:
                    109:        exit(rval);
                    110: }
                    111:
1.3       millert   112: void
1.8       deraadt   113: cnt(char *file)
1.1       deraadt   114: {
1.6       mpech     115:        u_char *C;
                    116:        short gotsp;
                    117:        int len;
                    118:        int64_t linect, wordct, charct;
1.1       deraadt   119:        struct stat sbuf;
                    120:        int fd;
                    121:        u_char buf[MAXBSIZE];
                    122:
                    123:        linect = wordct = charct = 0;
                    124:        if (file) {
                    125:                if ((fd = open(file, O_RDONLY, 0)) < 0) {
1.3       millert   126:                        warn("%s", file);
1.1       deraadt   127:                        rval = 1;
                    128:                        return;
                    129:                }
                    130:        } else  {
                    131:                fd = STDIN_FILENO;
                    132:        }
1.10      deraadt   133:
1.1       deraadt   134:        if (!doword) {
                    135:                /*
1.3       millert   136:                 * Line counting is split out because it's a lot
1.1       deraadt   137:                 * faster to get lines than to get words, since
                    138:                 * the word count requires some logic.
                    139:                 */
                    140:                if (doline) {
1.3       millert   141:                        while ((len = read(fd, buf, MAXBSIZE)) > 0) {
1.1       deraadt   142:                                charct += len;
                    143:                                for (C = buf; len--; ++C)
                    144:                                        if (*C == '\n')
                    145:                                                ++linect;
                    146:                        }
                    147:                        if (len == -1) {
1.3       millert   148:                                warn("%s", file);
1.1       deraadt   149:                                rval = 1;
                    150:                        }
                    151:                }
                    152:                /*
1.3       millert   153:                 * If all we need is the number of characters and
1.1       deraadt   154:                 * it's a directory or a regular or linked file, just
                    155:                 * stat the puppy.  We avoid testing for it not being
                    156:                 * a special device in case someone adds a new type
                    157:                 * of inode.
                    158:                 */
                    159:                else if (dochar) {
1.3       millert   160:                        mode_t ifmt;
1.1       deraadt   161:
                    162:                        if (fstat(fd, &sbuf)) {
1.3       millert   163:                                warn("%s", file);
1.1       deraadt   164:                                rval = 1;
                    165:                        } else {
                    166:                                ifmt = sbuf.st_mode & S_IFMT;
                    167:                                if (ifmt == S_IFREG || ifmt == S_IFLNK
1.3       millert   168:                                    || ifmt == S_IFDIR) {
1.1       deraadt   169:                                        charct = sbuf.st_size;
                    170:                                } else {
1.3       millert   171:                                        while ((len = read(fd, buf, MAXBSIZE)) > 0)
1.1       deraadt   172:                                                charct += len;
                    173:                                        if (len == -1) {
1.3       millert   174:                                                warn("%s", file);
1.1       deraadt   175:                                                rval = 1;
                    176:                                        }
                    177:                                }
                    178:                        }
                    179:                }
1.3       millert   180:        } else {
                    181:                /* Do it the hard way... */
1.1       deraadt   182:                gotsp = 1;
                    183:                while ((len = read(fd, buf, MAXBSIZE)) > 0) {
1.3       millert   184:                        /*
                    185:                         * This loses in the presence of multi-byte characters.
                    186:                         * To do it right would require a function to return a
                    187:                         * character while knowing how many bytes it consumed.
                    188:                         */
1.1       deraadt   189:                        charct += len;
                    190:                        for (C = buf; len--; ++C) {
1.15      deraadt   191:                                if (isspace(*C)) {
1.1       deraadt   192:                                        gotsp = 1;
1.3       millert   193:                                        if (*C == '\n')
1.1       deraadt   194:                                                ++linect;
                    195:                                } else {
                    196:                                        /*
                    197:                                         * This line implements the POSIX
                    198:                                         * spec, i.e. a word is a "maximal
                    199:                                         * string of characters delimited by
                    200:                                         * whitespace."  Notice nothing was
                    201:                                         * said about a character being
                    202:                                         * printing or non-printing.
                    203:                                         */
                    204:                                        if (gotsp) {
                    205:                                                gotsp = 0;
                    206:                                                ++wordct;
                    207:                                        }
                    208:                                }
                    209:                        }
                    210:                }
                    211:                if (len == -1) {
1.3       millert   212:                        warn("%s", file);
1.1       deraadt   213:                        rval = 1;
                    214:                }
                    215:        }
                    216:
1.12      otto      217:        print_counts(linect, wordct, charct, file);
1.1       deraadt   218:
1.3       millert   219:        /*
                    220:         * Don't bother checking doline, doword, or dochar -- speeds
1.10      deraadt   221:         * up the common case
1.3       millert   222:         */
1.1       deraadt   223:        tlinect += linect;
                    224:        twordct += wordct;
                    225:        tcharct += charct;
                    226:
1.3       millert   227:        if (close(fd) != 0) {
                    228:                warn("%s", file);
1.1       deraadt   229:                rval = 1;
                    230:        }
                    231: }
                    232:
1.11      espie     233: void
                    234: format_and_print(long long v)
                    235: {
                    236:        if (humanchar) {
                    237:                char result[FMT_SCALED_STRSIZE];
                    238:
                    239:                (void)fmt_scaled(v, result);
                    240:                (void)printf("%7s", result);
                    241:        } else {
                    242:                (void)printf(" %7lld", v);
                    243:        }
                    244: }
                    245:
1.1       deraadt   246: void
1.8       deraadt   247: print_counts(int64_t lines, int64_t words, int64_t chars, char *name)
1.1       deraadt   248: {
                    249:        if (doline)
1.11      espie     250:                format_and_print((long long)lines);
1.1       deraadt   251:        if (doword)
1.11      espie     252:                format_and_print((long long)words);
1.1       deraadt   253:        if (dochar)
1.11      espie     254:                format_and_print((long long)chars);
1.1       deraadt   255:
1.12      otto      256:        if (name)
                    257:                (void)printf(" %s\n", name);
                    258:        else
                    259:                (void)printf("\n");
1.1       deraadt   260: }