From 8cd91f5a6a4069f5b09fd459c0959ea00839323c Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 10 Jul 2023 16:24:03 +0300 Subject: [PATCH] Userspace: add basic 'touch' command --- userspace/CMakeLists.txt | 1 + userspace/touch/CMakeLists.txt | 16 ++++++++++++++++ userspace/touch/main.cpp | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 userspace/touch/CMakeLists.txt create mode 100644 userspace/touch/main.cpp diff --git a/userspace/CMakeLists.txt b/userspace/CMakeLists.txt index d0c3bf9e80..f71e801724 100644 --- a/userspace/CMakeLists.txt +++ b/userspace/CMakeLists.txt @@ -11,6 +11,7 @@ set(USERSPACE_PROJECTS Shell tee test + touch u8sum yes ) diff --git a/userspace/touch/CMakeLists.txt b/userspace/touch/CMakeLists.txt new file mode 100644 index 0000000000..34d61f6a8c --- /dev/null +++ b/userspace/touch/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.26) + +project(touch CXX) + +set(SOURCES + main.cpp +) + +add_executable(touch ${SOURCES}) +target_compile_options(touch PUBLIC -O2 -g) +target_link_libraries(touch PUBLIC libc) + +add_custom_target(touch-install + COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/touch ${BANAN_BIN}/ + DEPENDS touch +) diff --git a/userspace/touch/main.cpp b/userspace/touch/main.cpp new file mode 100644 index 0000000000..23ddd8ac93 --- /dev/null +++ b/userspace/touch/main.cpp @@ -0,0 +1,21 @@ +#include +#include +#include + +int main(int argc, char** argv) +{ + for (int i = 1; i < argc; i++) + { + int fd = open(argv[i], O_WRONLY | O_CREAT, 0644); + if (fd == -1) + { + if (errno != EEXISTS) + perror(argv[i]); + } + else + { + close(fd); + } + } + return 0; +}