wc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* `wc' word count utility.
  2. Copyright 1985 Copyright by Paul Rubin and Richard Stallman
  3. Permission is granted to anyone to make or distribute
  4. verbatim copies of this program
  5. provided that the copyright notice and this permission notice are preserved;
  6. and provided that the recipient is not asked to waive or limit his right to
  7. redistribute copies as permitted by this permission notice;
  8. and provided that anyone possessing a machine-executable copy
  9. is granted access to copy the source code, in machine-readable form,
  10. in some reasonable manner.
  11. Permission is granted to distribute derived works or enhanced versions of
  12. this program under the above conditions with the additional condition
  13. that the entire derivative or enhanced work
  14. must be covered by a permission notice identical to this one.
  15. Anything distributed as part of a package containing portions derived
  16. from this program, which cannot in current practice perform its function
  17. usefully in the absence of what was derived directly from this program,
  18. is to be considered as forming, together with the latter,
  19. a single work derived from this program,
  20. which must be entirely covered by a permission notice identical to this one
  21. in order for distribution of the package to be permitted.
  22. In other words, you are welcome to use, share and improve this program.
  23. You are forbidden to forbid anyone else to use, share and improve
  24. what you give them. Help stamp out software-hoarding! */
  25. #define NULL 0
  26. #define BUFSIZE 16384
  27. char *flags = "lwc";
  28. long lines, words, chars;
  29. long tlines = 0, twords = 0, tchars = 0;
  30. main(argc, argv)
  31. int argc;
  32. char **argv;
  33. {
  34. int error = 0;
  35. int i, fd;
  36. if (argc > 1 && argv[1][0] == '-') {
  37. flags = &argv[1][1];
  38. for (i = 0; flags[i] != '\0'; i++) {
  39. if (index("lwc", flags[i]) == NULL) {
  40. printf("Usage: wc [-lwc] [files]\n");
  41. exit(1);
  42. }
  43. }
  44. argc--; argv++;
  45. }
  46. if (argc > 1) {
  47. for (i = 1; i < argc; i++) {
  48. if ((fd = open(argv[i], 0)) < 0) {
  49. perror(argv[i]);
  50. error++;
  51. } else {
  52. wc(fd, argv[i]);
  53. close(fd);
  54. tlines += lines;
  55. twords += words;
  56. tchars += chars;
  57. }
  58. }
  59. if (argc > 2)
  60. output(tlines, twords, tchars, "total");
  61. } else
  62. wc(0, "");
  63. exit (error);
  64. }
  65. wc(fd, fname)
  66. int fd;
  67. char *fname;
  68. {
  69. char buf[BUFSIZE];
  70. register int n;
  71. register int state = 0;
  72. lines = words = chars = 0;
  73. while ((n = read(fd, buf, BUFSIZE)) > 0) {
  74. register char *p = buf;
  75. do {
  76. chars++;
  77. switch (*p++) {
  78. case '\n':
  79. lines++;
  80. /* fall through */
  81. case ' ':
  82. case '\t':
  83. if (state == 0) {
  84. state++;
  85. words++;
  86. }
  87. break;
  88. default:
  89. state = 0;
  90. break;
  91. }
  92. } while (--n);
  93. }
  94. output(lines, words, chars, fname);
  95. }
  96. output(l, w, c, fname)
  97. int l, w, c;
  98. char *fname;
  99. {
  100. if (index(flags, 'l') != NULL)
  101. printf("%8ld", l);
  102. if (index(flags, 'w') != NULL)
  103. printf("%8ld", w);
  104. if (index(flags, 'c') != NULL)
  105. printf("%8ld", c);
  106. printf(" %s\n", fname);
  107. }