From a61c120b3fc0259627f22d5415402200f1eaf6e0 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Sat, 11 Mar 2023 14:30:17 +0100 Subject: [PATCH] Utilities: Add an `xzcat` utility --- Userland/Utilities/CMakeLists.txt | 3 ++- Userland/Utilities/xzcat.cpp | 35 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 Userland/Utilities/xzcat.cpp diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index 0cb5171c88..001dcfc31c 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -8,7 +8,7 @@ list(APPEND REQUIRED_TARGETS ) list(APPEND RECOMMENDED_TARGETS adjtime aplay abench asctl bt checksum chres cksum copy fortune gunzip gzip init install keymap lsirq lsof lspci lzcat man mknod mktemp - nc netstat notify ntpquery open passwd pls printf pro shot strings tar tt unzip wallpaper zip + nc netstat notify ntpquery open passwd pls printf pro shot strings tar tt unzip wallpaper xzcat zip ) # FIXME: Support specifying component dependencies for utilities (e.g. WebSocket for telws) @@ -138,6 +138,7 @@ target_link_libraries(wallpaper PRIVATE LibGfx LibGUI) target_link_libraries(wasm PRIVATE LibWasm LibLine LibJS) target_link_libraries(wsctl PRIVATE LibGUI LibIPC) target_link_libraries(xml PRIVATE LibXML) +target_link_libraries(xzcat PRIVATE LibCompress) target_link_libraries(zip PRIVATE LibArchive LibCompress LibCrypto) # FIXME: Link this file into headless-browser without compiling it again. diff --git a/Userland/Utilities/xzcat.cpp b/Userland/Utilities/xzcat.cpp new file mode 100644 index 0000000000..81ffe61a45 --- /dev/null +++ b/Userland/Utilities/xzcat.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023, Tim Schumacher + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include + +ErrorOr serenity_main(Main::Arguments arguments) +{ + TRY(Core::System::pledge("rpath stdio")); + + StringView filename; + + Core::ArgsParser args_parser; + args_parser.set_general_help("Decompress and print an XZ archive"); + args_parser.add_positional_argument(filename, "File to decompress", "file"); + args_parser.parse(arguments); + + auto file = TRY(Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read)); + auto stream = TRY(Compress::XzDecompressor::create(move(file))); + + // Arbitrarily chosen buffer size. + Array buffer; + while (!stream->is_eof()) { + auto slice = TRY(stream->read_some(buffer)); + out("{:s}", slice); + } + + return 0; +}