From 6cb6e477799acada0c99a9343419857fedc35fac Mon Sep 17 00:00:00 2001 From: Matthew Olsson Date: Thu, 2 Jul 2020 17:59:25 -0700 Subject: [PATCH] cut: Accept input from stdin if no files are given --- Userland/cut.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Userland/cut.cpp b/Userland/cut.cpp index 59010e87e64..f3eee67e1cb 100644 --- a/Userland/cut.cpp +++ b/Userland/cut.cpp @@ -164,12 +164,13 @@ static void expand_list(Vector& tokens, Vector& indexes) static void cut_file(const String& file, const Vector& byte_vector) { - FILE* fp = nullptr; - fp = fopen(file.characters(), "r"); - - if (!fp) { - fprintf(stderr, "cut: Could not open file '%s'\n", file.characters()); - return; + FILE* fp = stdin; + if (!file.is_null()) { + fp = fopen(file.characters(), "r"); + if (!fp) { + fprintf(stderr, "cut: Could not open file '%s'\n", file.characters()); + return; + } } char* line = nullptr; @@ -195,7 +196,9 @@ static void cut_file(const String& file, const Vector& byte_vector) if (line) free(line); - fclose(fp); + + if (!file.is_null()) + fclose(fp); } int main(int argc, char** argv) @@ -227,12 +230,16 @@ int main(int argc, char** argv) } } - if (files.is_empty() || byte_list == "") + if (byte_list == "") print_usage_and_exit(1); Vector byte_vector; expand_list(tokens, byte_vector); quick_sort(byte_vector, [](auto& a, auto& b) { return a.m_from < b.m_from; }); + + if (files.is_empty()) + files.append(String()); + /* Process each file */ for (auto& file : files) cut_file(file, byte_vector);