sleep.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <stdlib.h>
  19. #include <limits.h>
  20. #include <unistd.h>
  21. #include "busybox.h"
  22. #ifdef CONFIG_FEATURE_FANCY_SLEEP
  23. static const struct suffix_mult sfx[] = {
  24. { "s", 1 },
  25. { "m", 60 },
  26. { "h", 60*60 },
  27. { "d", 24*60*60 },
  28. { NULL, 0 }
  29. };
  30. #endif
  31. int sleep_main(int argc, char **argv)
  32. {
  33. unsigned int duration;
  34. #ifdef CONFIG_FEATURE_FANCY_SLEEP
  35. if (argc < 2) {
  36. bb_show_usage();
  37. }
  38. ++argv;
  39. duration = 0;
  40. do {
  41. duration += xatoul_range_sfx(*argv, 0, UINT_MAX-duration, sfx);
  42. } while (*++argv);
  43. #else /* CONFIG_FEATURE_FANCY_SLEEP */
  44. if (argc != 2) {
  45. bb_show_usage();
  46. }
  47. duration = xatou(argv[1]);
  48. #endif /* CONFIG_FEATURE_FANCY_SLEEP */
  49. if (sleep(duration)) {
  50. bb_perror_nomsg_and_die();
  51. }
  52. return EXIT_SUCCESS;
  53. }