X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=coreutils%2Fsleep.c;h=de18dd0db36ea28e29efddab153b67c9de7c7651;hb=e40e76f3cd3702b1891231f585e5f38867a52b29;hp=592005babf7f377f885b5a412db6ce036cf5adae;hpb=99912ca733dd960f5589227fd999c86e73c8e894;p=oweals%2Fbusybox.git diff --git a/coreutils/sleep.c b/coreutils/sleep.c index 592005bab..de18dd0db 100644 --- a/coreutils/sleep.c +++ b/coreutils/sleep.c @@ -18,51 +18,87 @@ * time suffixes for seconds, minutes, hours, and days. */ -#include "busybox.h" +#include "libbb.h" /* This is a NOFORK applet. Be very careful! */ -#if ENABLE_FEATURE_FANCY_SLEEP +#if ENABLE_FEATURE_FANCY_SLEEP || ENABLE_FEATURE_FLOAT_SLEEP static const struct suffix_mult sfx[] = { { "s", 1 }, { "m", 60 }, { "h", 60*60 }, { "d", 24*60*60 }, - { NULL, 0 } + { } }; #endif -int sleep_main(int argc, char **argv); -int sleep_main(int argc, char **argv) +int sleep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int sleep_main(int argc UNUSED_PARAM, char **argv) { +#if ENABLE_FEATURE_FLOAT_SLEEP + double duration; + struct timespec ts; +#else unsigned duration; +#endif -#if ENABLE_FEATURE_FANCY_SLEEP - - if (argc < 2) { + ++argv; + if (!*argv) bb_show_usage(); - } - ++argv; +#if ENABLE_FEATURE_FLOAT_SLEEP + duration = 0; do { - duration += xatoul_range_sfx(*argv, 0, UINT_MAX-duration, sfx); + char *arg = *argv; + if (strchr(arg, '.')) { + double d; + int len = strspn(arg, "0123456789."); + char sv = arg[len]; + arg[len] = '\0'; + d = bb_strtod(arg, NULL); + if (errno) + bb_show_usage(); + arg[len] = sv; + len--; + sv = arg[len]; + arg[len] = '1'; + duration += d * xatoul_sfx(&arg[len], sfx); + arg[len] = sv; + } else + duration += xatoul_sfx(arg, sfx); } while (*++argv); -#else /* FEATURE_FANCY_SLEEP */ - - if (argc != 2) { - bb_show_usage(); + ts.tv_sec = MAXINT(typeof(ts.tv_sec)); + ts.tv_nsec = 0; + if (duration >= 0 && duration < ts.tv_sec) { + ts.tv_sec = duration; + ts.tv_nsec = (duration - ts.tv_sec) * 1000000000; } + do { + errno = 0; + nanosleep(&ts, &ts); + } while (errno == EINTR); + +#elif ENABLE_FEATURE_FANCY_SLEEP + + duration = 0; + do { + duration += xatou_range_sfx(*argv, 0, UINT_MAX - duration, sfx); + } while (*++argv); + sleep(duration); - duration = xatou(argv[1]); +#else /* simple */ -#endif /* FEATURE_FANCY_SLEEP */ + duration = xatou(*argv); + sleep(duration); + // Off. If it's really needed, provide example why + //if (sleep(duration)) { + // bb_perror_nomsg_and_die(); + //} - if (sleep(duration)) { - bb_perror_nomsg_and_die(); - } +#endif return EXIT_SUCCESS; }