wc.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/String.h>
  27. #include <AK/Vector.h>
  28. #include <LibCore/CArgsParser.h>
  29. #include <stdio.h>
  30. #include <sys/stat.h>
  31. struct Count {
  32. String name;
  33. bool exists = true;
  34. unsigned int lines = 0;
  35. unsigned int characters = 0;
  36. unsigned int words = 0;
  37. size_t bytes = 0;
  38. };
  39. bool output_line = false;
  40. bool output_byte = false;
  41. bool output_character = false;
  42. bool output_word = false;
  43. void wc_out(Count& count)
  44. {
  45. if (output_line)
  46. printf("%7i ", count.lines);
  47. if (output_word)
  48. printf("%7i ", count.words);
  49. if (output_byte)
  50. printf("%7lu ", count.bytes);
  51. if (output_character)
  52. printf("%7i ", count.characters);
  53. printf("%14s\n", count.name.characters());
  54. }
  55. Count get_count(const String& file_name)
  56. {
  57. Count count;
  58. FILE* file_pointer = nullptr;
  59. if (file_name == "-") {
  60. count.name = "";
  61. file_pointer = stdin;
  62. } else {
  63. count.name = file_name;
  64. if ((file_pointer = fopen(file_name.characters(), "r")) == NULL) {
  65. fprintf(stderr, "wc: unable to open %s\n", file_name.characters());
  66. count.exists = false;
  67. return count;
  68. }
  69. }
  70. bool tab_flag = false;
  71. bool space_flag = false;
  72. bool line_flag = true;
  73. int current_character;
  74. while ((current_character = fgetc(file_pointer)) != EOF) {
  75. count.characters++;
  76. if (current_character >= 'A' && current_character <= 'z' && (space_flag || line_flag || tab_flag)) {
  77. count.words++;
  78. space_flag = false;
  79. line_flag = false;
  80. tab_flag = false;
  81. }
  82. switch (current_character) {
  83. case '\n':
  84. count.lines++;
  85. line_flag = true;
  86. break;
  87. case ' ':
  88. space_flag = true;
  89. break;
  90. case '\t':
  91. tab_flag = true;
  92. break;
  93. }
  94. }
  95. fclose(file_pointer);
  96. if (file_pointer != stdin) {
  97. struct stat st;
  98. stat(file_name.characters(), &st);
  99. count.bytes = st.st_size;
  100. }
  101. return count;
  102. }
  103. Count get_total_count(Vector<Count>& counts)
  104. {
  105. Count total_count { "total" };
  106. for (auto& count : counts) {
  107. total_count.lines += count.lines;
  108. total_count.words += count.words;
  109. total_count.characters += count.characters;
  110. total_count.bytes += count.bytes;
  111. }
  112. return total_count;
  113. }
  114. int main(int argc, char** argv)
  115. {
  116. Vector<const char*> files;
  117. CArgsParser args_parser;
  118. args_parser.add_option(output_line, "Output line count", "lines", 'l');
  119. args_parser.add_option(output_byte, "Output byte count", "bytes", 'c');
  120. args_parser.add_option(output_word, "Output word count", "words", 'w');
  121. args_parser.add_positional_argument(files, "File to process", "file", CArgsParser::Required::No);
  122. args_parser.parse(argc, argv);
  123. if (!output_line && !output_byte && !output_word)
  124. output_line = output_byte = output_word = true;
  125. Vector<Count> counts;
  126. for (auto& file : files) {
  127. Count count = get_count(file);
  128. counts.append(count);
  129. }
  130. if (files.size() > 1) {
  131. Count total_count = get_total_count(counts);
  132. counts.append(total_count);
  133. }
  134. if (files.is_empty()) {
  135. Count count = get_count("-");
  136. counts.append(count);
  137. }
  138. for (auto& count : counts)
  139. if (count.exists)
  140. wc_out(count);
  141. return 0;
  142. }