sleep.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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);
  30. int sleep_main(int argc, char **argv)
  31. {
  32. unsigned duration;
  33. #if ENABLE_FEATURE_FANCY_SLEEP
  34. if (argc < 2) {
  35. bb_show_usage();
  36. }
  37. ++argv;
  38. duration = 0;
  39. do {
  40. duration += xatoul_range_sfx(*argv, 0, UINT_MAX-duration, sfx);
  41. } while (*++argv);
  42. #else /* FEATURE_FANCY_SLEEP */
  43. if (argc != 2) {
  44. bb_show_usage();
  45. }
  46. duration = xatou(argv[1]);
  47. #endif /* FEATURE_FANCY_SLEEP */
  48. if (sleep(duration)) {
  49. bb_perror_nomsg_and_die();
  50. }
  51. return EXIT_SUCCESS;
  52. }