wc.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/ArgsParser.h>
  29. #include <ctype.h>
  30. #include <stdio.h>
  31. #include <sys/stat.h>
  32. struct Count {
  33. String name;
  34. bool exists = true;
  35. unsigned int lines = 0;
  36. unsigned int characters = 0;
  37. unsigned int words = 0;
  38. size_t bytes = 0;
  39. };
  40. bool output_line = false;
  41. bool output_byte = false;
  42. bool output_word = false;
  43. static 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. printf("%14s\n", count.name.characters());
  52. }
  53. static Count get_count(const String& file_name)
  54. {
  55. Count count;
  56. FILE* file_pointer = nullptr;
  57. if (file_name == "-") {
  58. count.name = "";
  59. file_pointer = stdin;
  60. } else {
  61. count.name = file_name;
  62. if ((file_pointer = fopen(file_name.characters(), "r")) == NULL) {
  63. fprintf(stderr, "wc: unable to open %s\n", file_name.characters());
  64. count.exists = false;
  65. return count;
  66. }
  67. }
  68. bool start_a_new_word = true;
  69. for (int ch = fgetc(file_pointer); ch != EOF; ch = fgetc(file_pointer)) {
  70. count.bytes++;
  71. if (isspace(ch)) {
  72. start_a_new_word = true;
  73. } else if (start_a_new_word) {
  74. start_a_new_word = false;
  75. count.words++;
  76. }
  77. if (ch == '\n')
  78. count.lines++;
  79. }
  80. return count;
  81. }
  82. static Count get_total_count(Vector<Count>& counts)
  83. {
  84. Count total_count { "total" };
  85. for (auto& count : counts) {
  86. total_count.lines += count.lines;
  87. total_count.words += count.words;
  88. total_count.characters += count.characters;
  89. total_count.bytes += count.bytes;
  90. }
  91. return total_count;
  92. }
  93. int main(int argc, char** argv)
  94. {
  95. if (pledge("stdio rpath", nullptr) < 0) {
  96. perror("pledge");
  97. return 1;
  98. }
  99. Vector<const char*> files;
  100. Core::ArgsParser args_parser;
  101. args_parser.add_option(output_line, "Output line count", "lines", 'l');
  102. args_parser.add_option(output_byte, "Output byte count", "bytes", 'c');
  103. args_parser.add_option(output_word, "Output word count", "words", 'w');
  104. args_parser.add_positional_argument(files, "File to process", "file", Core::ArgsParser::Required::No);
  105. args_parser.parse(argc, argv);
  106. if (!output_line && !output_byte && !output_word)
  107. output_line = output_byte = output_word = true;
  108. Vector<Count> counts;
  109. for (auto& file : files) {
  110. Count count = get_count(file);
  111. counts.append(count);
  112. }
  113. if (pledge("stdio", nullptr) < 0) {
  114. perror("pledge");
  115. return 1;
  116. }
  117. if (files.size() > 1) {
  118. Count total_count = get_total_count(counts);
  119. counts.append(total_count);
  120. }
  121. if (files.is_empty()) {
  122. Count count = get_count("-");
  123. counts.append(count);
  124. }
  125. for (auto& count : counts)
  126. if (count.exists)
  127. wc_out(count);
  128. return 0;
  129. }