Userspace: implement basic sleep command
This commit is contained in:
parent
56008869d6
commit
ff8b3be8dc
|
@ -19,6 +19,7 @@ set(USERSPACE_PROJECTS
|
||||||
poweroff
|
poweroff
|
||||||
rm
|
rm
|
||||||
Shell
|
Shell
|
||||||
|
sleep
|
||||||
snake
|
snake
|
||||||
stat
|
stat
|
||||||
sync
|
sync
|
||||||
|
|
|
@ -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
|
||||||
|
)
|
|
@ -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);
|
||||||
|
}
|
Loading…
Reference in New Issue