Cross Reference of: WC.C;1 Fri May 02 17:29:51 1986 Page 1 1: /* 2: * wc [file ...] 3: */ 4: 5: /*)BUILD $(TKBOPTIONS) = { 6: TASK = ...WCX 7: } 8: */ 9: 10: #ifdef DOCUMENTATION 11: 12: title wc Word Count 13: index Count Words, Lines, and Bytes in Files 14: 15: synopsis 16: 17: wc [name ...] 18: 19: description 20: 21: Count the number of bytes, words, and lines in one or more files. 22: wc accepts wild-card file name arguments. 23: 24: diagnostics 25: 26: .lm +8 27: .s.i -8;"file name": cannot open 28: .s.i -8;"file name": illegal file name 29: .lm -8 30: 31: author 32: 33: Martin Minow 34: 35: bugs 36: 37: #endif 38: 39: #include 40: 41: long twords = 0; 42: long tlines = 0; 43: long tbytes = 0; 44: char file_name[81]; 45: 46: main(argc, argv) 47: char *argv[]; 48: { 49: register int i, nfiles; 50: register FILE *fp; 51: int gotcha; 52: 53: nfiles = 0; 54: if(argc < 2) { 55: ++nfiles; 56: count(stdin, NULL); 57: } Cross Reference of: WC.C;1 Fri May 02 17:29:51 1986 Page 2 58: else { 59: #ifdef unix 60: for (i = 1; i < argc; ++i) { 61: if ((fp = fopen(argv[i], "r")) == NULL) { 62: perror(argv[i]); 63: } 64: else { 65: ++nfiles; 66: count(fp, argv[i]); 67: fclose(fp); 68: } 69: } 70: #else 71: for (i = 1; i < argc; ++i) { 72: if ((fp = fwild(argv[i], "r")) == NULL) { 73: perror(argv[i]); 74: } 75: else { 76: for (gotcha = 0; fnext(fp) != NULL; gotcha++) { 77: ++nfiles; 78: fgetname(fp, file_name); 79: count(fp, file_name); 80: } 81: if (gotcha == 0) 82: fprintf(stderr, "\"%s\": no matching files\n", argv[i]); 83: } 84: } 85: #endif 86: } 87: if (nfiles > 1) 88: output(tlines, twords, tbytes, "total"); 89: } 90: 91: count(fp, filename) 92: FILE *fp; /* File pointer */ 93: char *filename; /* File name string */ 94: { 95: register int c, inword; 96: long lines; 97: long words; 98: long bytes; 99: 100: lines = 0; 101: words = 0; 102: bytes = 0; 103: inword = 0; 104: while((c = getc(fp)) != EOF) { 105: ++bytes; 106: if (c == ' ' || c == '\t' || c == '\n') { 107: inword = 0; 108: if (c == '\n') 109: ++lines; 110: } 111: else if (!inword) { 112: ++inword; 113: ++words; 114: } 115: } Cross Reference of: WC.C;1 Fri May 02 17:29:51 1986 Page 3 116: twords += words; 117: tlines += lines; 118: tbytes += bytes; 119: output(lines, words, bytes, filename); 120: } 121: 122: output(lines, words, bytes, filename) 123: long lines; 124: long words; 125: long bytes; 126: char *filename; 127: { 128: plural(lines, "line"); 129: plural(words, "word"); 130: plural(bytes, "byte"); 131: if (filename != NULL) 132: printf(" %s", filename); 133: printf("\n"); 134: } 135: 136: plural(value, what) 137: long value; 138: char *what; 139: { 140: #ifdef unix 141: printf(%8ld", value); 142: #else 143: printf("%8ld %s%c", value, what, (value == 1) ? ' ' : 's'); 144: #endif 145: } 146: Cross Reference of: WC.C;1 Fri May 02 17:29:51 1986 Page 4 Bytes 13 Count 12 13 21 DOCUMENT 10 EOF 104 FILE 50 92 Files 13 Lines 13 Martin 33 Minow 33 NULL 56 61 72 76 131 Word 12 Words 13 accepts 22 and 13 21 argc 46 54 60 71 argument 22 argv 46 47 61 62 66 72 73 82 author 31 bugs 35 bytes 21 98 102 105 118 119 122 125 130 c 95 104 106 106 106 108 cannot 27 card 22 count 56 66 79 91 descript 19 diagnost 24 endif 37 85 144 fclose 67 fgetname 78 file 22 28 file_nam 44 78 79 filename 91 93 119 122 126 131 132 files 21 fnext 76 fopen 61 fp 50 61 66 67 72 76 78 79 91 92 104 fprintf 82 fwild 72 getc 104 gotcha 51 76 76 81 h 39 i 27 28 49 60 60 60 61 62 66 71 71 71 72 73 82 ifdef 10 59 140 illegal 28 in 13 21 include 39 index 13 inword 95 103 107 111 112 lines 21 96 100 109 117 119 122 123 128 lm 26 29 main 46 more 21 name 17 22 28 nfiles 49 53 55 65 77 87 number 21 of 21 one 21 Cross Reference of: WC.C;1 Fri May 02 17:29:51 1986 Page 5 open 27 or 21 output 88 119 122 perror 62 73 plural 128 129 130 136 printf 132 133 141 143 s 27 28 stderr 82 stdin 56 stdio 39 synopsis 15 tbytes 43 88 118 the 21 title 12 tlines 42 88 117 twords 41 88 116 unix 59 140 value 136 137 143 143 wc 12 17 22 what 136 138 143 wild 22 words 21 97 101 113 116 119 122 124 129