diff --git a/userspace/CMakeLists.txt b/userspace/CMakeLists.txt index ad24eafe7e..a53eac0415 100644 --- a/userspace/CMakeLists.txt +++ b/userspace/CMakeLists.txt @@ -19,6 +19,7 @@ set(USERSPACE_PROJECTS poweroff rm Shell + sleep snake stat sync diff --git a/userspace/sleep/CMakeLists.txt b/userspace/sleep/CMakeLists.txt new file mode 100644 index 0000000000..38299e583f --- /dev/null +++ b/userspace/sleep/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.26) + +project(sleep CXX) + +set(SOURCES + main.cpp +) + +add_executable(sleep ${SOURCES}) +target_compile_options(sleep PUBLIC -O2 -g) +target_link_libraries(sleep PUBLIC libc) + +add_custom_target(sleep-install + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/sleep ${BANAN_BIN}/ + DEPENDS sleep +) diff --git a/userspace/sleep/main.cpp b/userspace/sleep/main.cpp new file mode 100644 index 0000000000..6593bce0fc --- /dev/null +++ b/userspace/sleep/main.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +int usage(char* argv0, int ret) +{ + FILE* fp = (ret == 0) ? stdout : stderr; + fprintf(fp, "usage: %s SECONDS\n", argv0); + return ret; +} + +int main(int argc, char** argv) +{ + if (argc != 2) + return usage(argv[0], 1); + + if (strlen(argv[1]) > 9) + { + fprintf(stderr, "SECONDS argument too large\n"); + return usage(argv[0], 1); + } + + int seconds = 0; + const char* ptr = argv[1]; + while (*ptr) + { + if (!isdigit(*ptr)) + { + fprintf(stderr, "invalid SECONDS argument\n"); + return usage(argv[0], 1); + } + seconds = (seconds * 10) + (*ptr - '0'); + ptr++; + } + + sleep(seconds); +}