3
0

sleep.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * sleep implementation for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 compliant */
  10. /* BB_AUDIT GNU issues -- fancy version matches except args must be ints. */
  11. /* http://www.opengroup.org/onlinepubs/007904975/utilities/sleep.html */
  12. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  13. *
  14. * Rewritten to do proper arg and error checking.
  15. * Also, added a 'fancy' configuration to accept multiple args with
  16. * time suffixes for seconds, minutes, hours, and days.
  17. */
  18. #include "libbb.h"
  19. /* This is a NOFORK applet. Be very careful! */
  20. #if ENABLE_FEATURE_FANCY_SLEEP
  21. static const struct suffix_mult sfx[] = {
  22. { "s", 1 },
  23. { "m", 60 },
  24. { "h", 60*60 },
  25. { "d", 24*60*60 },
  26. { }
  27. };
  28. #endif
  29. int sleep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  30. int sleep_main(int argc ATTRIBUTE_UNUSED, char **argv)
  31. {
  32. unsigned duration;
  33. ++argv;
  34. if (!*argv)
  35. bb_show_usage();
  36. #if ENABLE_FEATURE_FANCY_SLEEP
  37. duration = 0;
  38. do {
  39. duration += xatoul_range_sfx(*argv, 0, UINT_MAX-duration, sfx);
  40. } while (*++argv);
  41. #else /* FEATURE_FANCY_SLEEP */
  42. duration = xatou(*argv);
  43. #endif /* FEATURE_FANCY_SLEEP */
  44. if (sleep(duration)) {
  45. bb_perror_nomsg_and_die();
  46. }
  47. return EXIT_SUCCESS;
  48. }