浏览代码

wc: Count last line even if it doesn't end in newline

Rodrigo Tobar 3 年之前
父节点
当前提交
f356c4ab91
共有 1 个文件被更改,包括 4 次插入0 次删除
  1. 4 0
      Userland/Utilities/wc.cpp

+ 4 - 0
Userland/Utilities/wc.cpp

@@ -55,7 +55,9 @@ static Count get_count(const String& file_specifier)
     }
 
     bool start_a_new_word = true;
+    int last_ch = EOF;
     for (int ch = fgetc(file_pointer); ch != EOF; ch = fgetc(file_pointer)) {
+        last_ch = ch;
         count.bytes++;
         if (isspace(ch)) {
             start_a_new_word = true;
@@ -66,6 +68,8 @@ static Count get_count(const String& file_specifier)
             count.words++;
         }
     }
+    if (last_ch != '\n')
+        count.lines++;
 
     if (file_pointer != stdin)
         fclose(file_pointer);