diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index c7d8e823..f3386de5 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -7,9 +7,11 @@ function(luna_app SOURCE_FILE APP_NAME) endfunction() luna_app(init.cpp init) -luna_app(cat.cpp cat) luna_app(sh.cpp sh) +luna_app(cat.cpp cat) +target_link_libraries(cat PRIVATE os) + luna_app(date.cpp date) target_link_libraries(date PRIVATE os) diff --git a/apps/cat.cpp b/apps/cat.cpp index b99c72ec..e91e226c 100644 --- a/apps/cat.cpp +++ b/apps/cat.cpp @@ -1,9 +1,24 @@ +#include + #include #include #include -static void do_cat(FILE* f) +static void do_cat(StringView path) { + FILE* f; + + if (path == "-") f = stdin; + else + { + f = fopen(path.chars(), "r"); + if (!f) + { + perror(path.chars()); + exit(1); + } + } + char buffer[4096]; while (1) @@ -12,30 +27,19 @@ static void do_cat(FILE* f) if (nread == 0) return; fwrite(buffer, 1, nread, stdout); } + + if (f != stdin) fclose(f); } int main(int argc, char** argv) { - FILE* f; + StringView filename; - if (argc < 2) { do_cat(stdin); } - else - { - for (int i = 1; i < argc; i++) - { - if (!strcmp(argv[i], "-")) f = stdin; - else - { - f = fopen(argv[i], "r"); - if (!f) - { - perror(argv[i]); - return 1; - } - } + ArgumentParser parser; + parser.add_positional_argument(filename, "file"_sv, "-"_sv); + Vector extra_files = parser.parse(argc, argv).value(); - do_cat(f); - if (f != stdin) fclose(f); - } - } + do_cat(filename); + + for (auto file : extra_files) { do_cat(file); } }