From 5bb43bd708142d686ec8ee8164b2690b8860e531 Mon Sep 17 00:00:00 2001 From: Eike Flath Date: Sat, 20 Apr 2024 13:59:47 +0200 Subject: [PATCH 1/4] implemented sleep --- Makefile | 1 + src/sleep/sleep.c | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 264a4dc..983cbfe 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ BINS += ln BINS += mv BINS += chmod BINS += sh +BINS += sleep BINARIES := $(foreach b, $(BINS), src/$b/$b) BINS-COPY := $(foreach b, $(BINS), bin/$b) diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c index 6e6d777..7672e38 100644 --- a/src/sleep/sleep.c +++ b/src/sleep/sleep.c @@ -1,6 +1,41 @@ #include +#include +#include +#include +#include -int main(void) { - printf("Hello, World!\n"); - return 0; -} \ No newline at end of file +#define NAME "sleep (canoutils)" +#define VERSION "1.0.0" +#define AUTHOR "Eike Flath" + +#include "version_info.h" + +static unsigned int parse_int(char *arg) { + unsigned int n = 0; + for (int i = 0; arg[i]; i++) { + if (!isdigit(arg[i])) { + fprintf(stderr, "sleep: invalid time interval '%s'\n", arg); + exit(EXIT_FAILURE); + } + unsigned int new_n = n * 10 + (arg[i] - '0'); + if (new_n < n) { // overflow + fprintf(stderr, "sleep: time interval '%s' is too big\n", arg); + exit(EXIT_FAILURE); + } + n = new_n; + } + return n; +} + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, "sleep: expected exactly one operand, got %d\n", argc - 1); + return EXIT_FAILURE; + } + if (!strcmp(argv[1], "--version")) { + print_version(); + return EXIT_SUCCESS; + } + sleep(parse_int(argv[1])); + return EXIT_SUCCESS; +} From e03d477cf54a0c1fe3c56200d23be3daa3aea400 Mon Sep 17 00:00:00 2001 From: Eike Flath Date: Sat, 20 Apr 2024 14:16:17 +0200 Subject: [PATCH 2/4] made sleep handle SIGALRM correctly --- src/sleep/sleep.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c index 7672e38..74241ba 100644 --- a/src/sleep/sleep.c +++ b/src/sleep/sleep.c @@ -3,6 +3,7 @@ #include #include #include +#include #define NAME "sleep (canoutils)" #define VERSION "1.0.0" @@ -27,6 +28,11 @@ static unsigned int parse_int(char *arg) { return n; } +static void handle_sigalrm(int sig) { + (void) sig; + exit(EXIT_SUCCESS); +} + int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "sleep: expected exactly one operand, got %d\n", argc - 1); @@ -36,6 +42,7 @@ int main(int argc, char **argv) { print_version(); return EXIT_SUCCESS; } - sleep(parse_int(argv[1])); - return EXIT_SUCCESS; + unsigned int n = parse_int(argv[1]); + signal(SIGALRM, handle_sigalrm); + return sleep(n) == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } From 830824551032414b978932daf26bbad3289babfd Mon Sep 17 00:00:00 2001 From: Eike Flath Date: Sat, 20 Apr 2024 14:33:01 +0200 Subject: [PATCH 3/4] fixed format --- src/sleep/sleep.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c index 74241ba..80a6fc0 100644 --- a/src/sleep/sleep.c +++ b/src/sleep/sleep.c @@ -1,9 +1,9 @@ +#include +#include #include #include #include -#include #include -#include #define NAME "sleep (canoutils)" #define VERSION "1.0.0" @@ -29,7 +29,7 @@ static unsigned int parse_int(char *arg) { } static void handle_sigalrm(int sig) { - (void) sig; + (void)sig; exit(EXIT_SUCCESS); } From c0772569b45a1840c4d1fca71ab311887aa45a6b Mon Sep 17 00:00:00 2001 From: Eike Flath Date: Sat, 20 Apr 2024 20:33:37 +0200 Subject: [PATCH 4/4] used strtol instead of custom integer parsing function --- src/sleep/sleep.c | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c index 80a6fc0..30abe87 100644 --- a/src/sleep/sleep.c +++ b/src/sleep/sleep.c @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -11,23 +11,6 @@ #include "version_info.h" -static unsigned int parse_int(char *arg) { - unsigned int n = 0; - for (int i = 0; arg[i]; i++) { - if (!isdigit(arg[i])) { - fprintf(stderr, "sleep: invalid time interval '%s'\n", arg); - exit(EXIT_FAILURE); - } - unsigned int new_n = n * 10 + (arg[i] - '0'); - if (new_n < n) { // overflow - fprintf(stderr, "sleep: time interval '%s' is too big\n", arg); - exit(EXIT_FAILURE); - } - n = new_n; - } - return n; -} - static void handle_sigalrm(int sig) { (void)sig; exit(EXIT_SUCCESS); @@ -42,7 +25,20 @@ int main(int argc, char **argv) { print_version(); return EXIT_SUCCESS; } - unsigned int n = parse_int(argv[1]); + char *endptr = NULL; + long n = strtol(argv[1], &endptr, 10); + if (argv[1][0] == '\0' || *endptr != '\0') { + fprintf(stderr, "sleep: invalid time interval '%s'\n", argv[1]); + return EXIT_FAILURE; + } + if (n < 0) { + fprintf(stderr, "sleep: please provide a non-negative time interval\n"); + return EXIT_FAILURE; + } + if (n > UINT_MAX) { + fprintf(stderr, "sleep: time interval '%s' is too big\n", argv[1]); + return EXIT_FAILURE; + } signal(SIGALRM, handle_sigalrm); - return sleep(n) == 0 ? EXIT_SUCCESS : EXIT_FAILURE; + return sleep((unsigned int)n) == 0 ? EXIT_SUCCESS : EXIT_FAILURE; }