From e685f38fd150c565caad9d5213846e8f7b170d55 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 25 Oct 2023 02:36:09 +0300 Subject: [PATCH] Userspace: Add basic chmod command --- userspace/CMakeLists.txt | 1 + userspace/chmod/CMakeLists.txt | 17 ++++++++++++++ userspace/chmod/main.cpp | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 userspace/chmod/CMakeLists.txt create mode 100644 userspace/chmod/main.cpp diff --git a/userspace/CMakeLists.txt b/userspace/CMakeLists.txt index 28f7f574..cb9bf28c 100644 --- a/userspace/CMakeLists.txt +++ b/userspace/CMakeLists.txt @@ -5,6 +5,7 @@ project(userspace CXX) set(USERSPACE_PROJECTS cat cat-mmap + chmod cp dd echo diff --git a/userspace/chmod/CMakeLists.txt b/userspace/chmod/CMakeLists.txt new file mode 100644 index 00000000..29b3c28d --- /dev/null +++ b/userspace/chmod/CMakeLists.txt @@ -0,0 +1,17 @@ +cmake_minimum_required(VERSION 3.26) + +project(chmod CXX) + +set(SOURCES + main.cpp +) + +add_executable(chmod ${SOURCES}) +target_compile_options(chmod PUBLIC -O2 -g) +target_link_libraries(chmod PUBLIC libc) + +add_custom_target(chmod-install + COMMAND sudo cp ${CMAKE_CURRENT_BINARY_DIR}/chmod ${BANAN_BIN}/ + DEPENDS chmod + USES_TERMINAL +) diff --git a/userspace/chmod/main.cpp b/userspace/chmod/main.cpp new file mode 100644 index 00000000..522a674e --- /dev/null +++ b/userspace/chmod/main.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include + +void usage(const char* argv0, int ret) +{ + FILE* out = (ret == 0) ? stdout : stderr; + fprintf(out, "usage: %s MODE FILE...\n", argv0); + fprintf(out, " Change the mode of each FILE to MODE.\n"); + exit(ret); +} + +int main(int argc, char** argv) +{ + if (argc <= 2) + usage(argv[0], 1); + + int base = (argv[1][0] == '0') ? 8 : 10; + + mode_t mode = 0; + for (const char* ptr = argv[1]; *ptr; ptr++) + { + if (!isdigit(*ptr)) + { + fprintf(stderr, "Invalid MODE %s\n", argv[1]); + usage(argv[0], 1); + } + mode = (mode * base) + (*ptr - '0'); + } + + int ret = 0; + for (int i = 2; i < argc; i++) + { + if (chmod(argv[i], mode) == -1) + { + perror("chmod"); + ret = 1; + } + } + + return ret; +}