Userspace: implement basic sleep command

This commit is contained in:
Bananymous 2023-12-06 13:05:53 +02:00
parent 56008869d6
commit ff8b3be8dc
3 changed files with 55 additions and 0 deletions

View File

@ -19,6 +19,7 @@ set(USERSPACE_PROJECTS
poweroff
rm
Shell
sleep
snake
stat
sync

View File

@ -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
)

38
userspace/sleep/main.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
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);
}