From 3867a29a105ac1979bff9780a6d32e2c20a4c5c6 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 8 Apr 2023 14:47:58 +0200 Subject: [PATCH] apps: Add mkdir, chown and chmod --- apps/CMakeLists.txt | 16 ++++------------ apps/chmod.cpp | 23 +++++++++++++++++++++++ apps/chown.cpp | 23 +++++++++++++++++++++++ apps/mkdir.cpp | 23 +++++++++++++++++++++++ 4 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 apps/chmod.cpp create mode 100644 apps/chown.cpp create mode 100644 apps/mkdir.cpp diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 288cb943..0b2cdc10 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -3,26 +3,18 @@ function(luna_app SOURCE_FILE APP_NAME) target_compile_options(${APP_NAME} PRIVATE -Os ${COMMON_FLAGS} -Wno-write-strings) add_dependencies(${APP_NAME} libc) target_include_directories(${APP_NAME} PRIVATE ${LUNA_BASE}/usr/include) + target_link_libraries(${APP_NAME} PRIVATE os) install(TARGETS ${APP_NAME} DESTINATION ${LUNA_ROOT}/initrd/bin) endfunction() luna_app(init.cpp init) luna_app(env.cpp env) - luna_app(su.cpp su) -target_link_libraries(su PRIVATE os) - luna_app(sh.cpp sh) -target_link_libraries(sh PRIVATE os) - luna_app(cat.cpp cat) -target_link_libraries(cat PRIVATE os) - luna_app(date.cpp date) -target_link_libraries(date PRIVATE os) - luna_app(edit.cpp edit) -target_link_libraries(edit PRIVATE os) - luna_app(ls.cpp ls) -target_link_libraries(ls PRIVATE os) +luna_app(chown.cpp chown) +luna_app(chmod.cpp chmod) +luna_app(mkdir.cpp mkdir) diff --git a/apps/chmod.cpp b/apps/chmod.cpp new file mode 100644 index 00000000..8440c8bf --- /dev/null +++ b/apps/chmod.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + StringView mode_string; + StringView path; + + os::ArgumentParser parser; + parser.add_positional_argument(mode_string, "mode"_sv, true); + parser.add_positional_argument(path, "path"_sv, true); + parser.parse(argc, argv); + + mode_t mode = (mode_t)strtoul(mode_string.chars(), NULL, 8); + + if (chmod(path.chars(), mode) < 0) + { + perror("chmod"); + return 1; + } +} diff --git a/apps/chown.cpp b/apps/chown.cpp new file mode 100644 index 00000000..cf7e64c2 --- /dev/null +++ b/apps/chown.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + StringView user; + StringView path; + + os::ArgumentParser parser; + parser.add_positional_argument(user, "user"_sv, true); + parser.add_positional_argument(path, "path"_sv, true); + parser.parse(argc, argv); + + u32 id = (u32)strtoul(user.chars(), NULL, 0); + + if (chown(path.chars(), id, id) < 0) + { + perror("chown"); + return 1; + } +} diff --git a/apps/mkdir.cpp b/apps/mkdir.cpp new file mode 100644 index 00000000..bd385c9c --- /dev/null +++ b/apps/mkdir.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + StringView path; + StringView mode_string; + + os::ArgumentParser parser; + parser.add_positional_argument(path, "path"_sv, true); + parser.add_positional_argument(mode_string, "mode"_sv, "755"_sv); + parser.parse(argc, argv); + + mode_t mode = (mode_t)strtoul(mode_string.chars(), NULL, 8); + + if (mkdir(path.chars(), mode) < 0) + { + perror("mkdir"); + return 1; + } +}