1 /* $OpenBSD: wc.c,v 1.16 2013/11/27 13:32:02 okan Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1987, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 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. 15 * 3. Neither the name of the University nor the names of its contributors 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 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <locale.h> 36 #include <ctype.h> 37 #include <err.h> 38 #include <sys/param.h> 39 #include <sys/stat.h> 40 #include <sys/file.h> 41 #include <unistd.h> 42 #include <util.h> 43 44 int64_t tlinect, twordct, tcharct; 45 int doline, doword, dochar, humanchar; 46 int rval; 47 extern char *__progname; 48 49 void print_counts(int64_t, int64_t, int64_t, char *); 50 void format_and_print(long long); 51 void cnt(char *); 52 53 int 54 main(int argc, char *argv[]) 55 { 56 int ch; 57 58 setlocale(LC_ALL, ""); 59 60 while ((ch = getopt(argc, argv, "lwchm")) != -1) 61 switch(ch) { 62 case 'l': 63 doline = 1; 64 break; 65 case 'w': 66 doword = 1; 67 break; 68 case 'c': 69 case 'm': 70 dochar = 1; 71 break; 72 case 'h': 73 humanchar = 1; 74 break; 75 case '?': 76 default: 77 (void)fprintf(stderr, 78 "usage: %s [-c | -m] [-hlw] [file ...]\n", 79 __progname); 80 exit(1); 81 } 82 argv += optind; 83 argc -= optind; 84 85 /* 86 * wc is unusual in that its flags are on by default, so, 87 * if you don't get any arguments, you have to turn them 88 * all on. 89 */ 90 if (!doline && !doword && !dochar) 91 doline = doword = dochar = 1; 92 93 if (!*argv) { 94 cnt((char *)NULL); 95 } else { 96 int dototal = (argc > 1); 97 98 do { 99 cnt(*argv); 100 } while(*++argv); 101 102 if (dototal) 103 print_counts(tlinect, twordct, tcharct, "total"); 104 } 105 106 exit(rval); 107 } 108 109 void 110 cnt(char *file) 111 { 112 u_char *C; 113 short gotsp; 114 int len; 115 int64_t linect, wordct, charct; 116 struct stat sbuf; 117 int fd; 118 u_char buf[MAXBSIZE]; 119 120 linect = wordct = charct = 0; 121 if (file) { 122 if ((fd = open(file, O_RDONLY, 0)) < 0) { 123 warn("%s", file); 124 rval = 1; 125 return; 126 } 127 } else { 128 fd = STDIN_FILENO; 129 } 130 131 if (!doword) { 132 /* 133 * Line counting is split out because it's a lot 134 * faster to get lines than to get words, since 135 * the word count requires some logic. 136 */ 137 if (doline) { 138 while ((len = read(fd, buf, MAXBSIZE)) > 0) { 139 charct += len; 140 for (C = buf; len--; ++C) 141 if (*C == '\n') 142 ++linect; 143 } 144 if (len == -1) { 145 warn("%s", file); 146 rval = 1; 147 } 148 } 149 /* 150 * If all we need is the number of characters and 151 * it's a directory or a regular or linked file, just 152 * stat the puppy. We avoid testing for it not being 153 * a special device in case someone adds a new type 154 * of inode. 155 */ 156 else if (dochar) { 157 mode_t ifmt; 158 159 if (fstat(fd, &sbuf)) { 160 warn("%s", file); 161 rval = 1; 162 } else { 163 ifmt = sbuf.st_mode & S_IFMT; 164 if (ifmt == S_IFREG || ifmt == S_IFLNK 165 || ifmt == S_IFDIR) { 166 charct = sbuf.st_size; 167 } else { 168 while ((len = read(fd, buf, MAXBSIZE)) > 0) 169 charct += len; 170 if (len == -1) { 171 warn("%s", file); 172 rval = 1; 173 } 174 } 175 } 176 } 177 } else { 178 /* Do it the hard way... */ 179 gotsp = 1; 180 while ((len = read(fd, buf, MAXBSIZE)) > 0) { 181 /* 182 * This loses in the presence of multi-byte characters. 183 * To do it right would require a function to return a 184 * character while knowing how many bytes it consumed. 185 */ 186 charct += len; 187 for (C = buf; len--; ++C) { 188 if (isspace(*C)) { 189 gotsp = 1; 190 if (*C == '\n') 191 ++linect; 192 } else { 193 /* 194 * This line implements the POSIX 195 * spec, i.e. a word is a "maximal 196 * string of characters delimited by 197 * whitespace." Notice nothing was 198 * said about a character being 199 * printing or non-printing. 200 */ 201 if (gotsp) { 202 gotsp = 0; 203 ++wordct; 204 } 205 } 206 } 207 } 208 if (len == -1) { 209 warn("%s", file); 210 rval = 1; 211 } 212 } 213 214 print_counts(linect, wordct, charct, file); 215 216 /* 217 * Don't bother checking doline, doword, or dochar -- speeds 218 * up the common case 219 */ 220 tlinect += linect; 221 twordct += wordct; 222 tcharct += charct; 223 224 if (close(fd) != 0) { 225 warn("%s", file); 226 rval = 1; 227 } 228 } 229 230 void 231 format_and_print(long long v) 232 { 233 if (humanchar) { 234 char result[FMT_SCALED_STRSIZE]; 235 236 (void)fmt_scaled(v, result); 237 (void)printf("%7s", result); 238 } else { 239 (void)printf(" %7lld", v); 240 } 241 } 242 243 void 244 print_counts(int64_t lines, int64_t words, int64_t chars, char *name) 245 { 246 if (doline) 247 format_and_print((long long)lines); 248 if (doword) 249 format_and_print((long long)words); 250 if (dochar) 251 format_and_print((long long)chars); 252 253 if (name) 254 (void)printf(" %s\n", name); 255 else 256 (void)printf("\n"); 257 } 258