Skip to content

Commit

Permalink
Merge pull request #37 from Eike-Flath/main
Browse files Browse the repository at this point in the history
Implementation of sleep
  • Loading branch information
proh14 authored Apr 20, 2024
2 parents 6c35700 + c077256 commit f490d95
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 42 additions & 4 deletions src/sleep/sleep.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void) {
printf("Hello, World!\n");
return 0;
}
#define NAME "sleep (canoutils)"
#define VERSION "1.0.0"
#define AUTHOR "Eike Flath"

#include "version_info.h"

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);
return EXIT_FAILURE;
}
if (!strcmp(argv[1], "--version")) {
print_version();
return EXIT_SUCCESS;
}
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((unsigned int)n) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

0 comments on commit f490d95

Please sign in to comment.