From cb7d5c9d0949b8b80ac516488b8fc58032f0d8df Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 5 Dec 2024 08:30:30 +0200 Subject: [PATCH] userspace: Implement `dirname` utility --- userspace/programs/CMakeLists.txt | 1 + userspace/programs/dirname/CMakeLists.txt | 8 ++++++++ userspace/programs/dirname/main.cpp | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 userspace/programs/dirname/CMakeLists.txt create mode 100644 userspace/programs/dirname/main.cpp diff --git a/userspace/programs/CMakeLists.txt b/userspace/programs/CMakeLists.txt index ec6ce9ce..93c98441 100644 --- a/userspace/programs/CMakeLists.txt +++ b/userspace/programs/CMakeLists.txt @@ -7,6 +7,7 @@ set(USERSPACE_PROGRAMS cp dd dhcp-client + dirname DynamicLoader echo env diff --git a/userspace/programs/dirname/CMakeLists.txt b/userspace/programs/dirname/CMakeLists.txt new file mode 100644 index 00000000..f8b22d47 --- /dev/null +++ b/userspace/programs/dirname/CMakeLists.txt @@ -0,0 +1,8 @@ +set(SOURCES + main.cpp +) + +add_executable(dirname ${SOURCES}) +banan_link_library(dirname libc) + +install(TARGETS dirname OPTIONAL) diff --git a/userspace/programs/dirname/main.cpp b/userspace/programs/dirname/main.cpp new file mode 100644 index 00000000..1fe01ab7 --- /dev/null +++ b/userspace/programs/dirname/main.cpp @@ -0,0 +1,18 @@ +#include +#include + +int usage(const char* argv0, int ret) +{ + FILE* fout = ret ? stderr : stdout; + fprintf(fout, "usage: %s STRING\n", argv0); + return ret; +} + +int main(int argc, const char* argv[]) +{ + if (argc != 2) + return usage(argv[0], 1); + const char* result = dirname(const_cast(argv[1])); + printf("%s\n", result); + return 0; +}