sleep.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 source tree.
  8. */
  9. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  10. *
  11. * Rewritten to do proper arg and error checking.
  12. * Also, added a 'fancy' configuration to accept multiple args with
  13. * time suffixes for seconds, minutes, hours, and days.
  14. */
  15. //config:config SLEEP
  16. //config: bool "sleep (2 kb)"
  17. //config: default y
  18. //config: help
  19. //config: sleep is used to pause for a specified number of seconds.
  20. //config: It comes in 3 versions:
  21. //config: - small: takes one integer parameter
  22. //config: - fancy: takes multiple integer arguments with suffixes:
  23. //config: sleep 1d 2h 3m 15s
  24. //config: - fancy with fractional numbers:
  25. //config: sleep 2.3s 4.5h sleeps for 16202.3 seconds
  26. //config: Last one is "the most compatible" with coreutils sleep,
  27. //config: but it adds around 1k of code.
  28. //config:
  29. //config:config FEATURE_FANCY_SLEEP
  30. //config: bool "Enable multiple arguments and s/m/h/d suffixes"
  31. //config: default y
  32. //config: depends on SLEEP
  33. //config: help
  34. //config: Allow sleep to pause for specified minutes, hours, and days.
  35. /* Do not make this applet NOFORK. It breaks ^C-ing of pauses in shells */
  36. //applet:IF_SLEEP(APPLET(sleep, BB_DIR_BIN, BB_SUID_DROP))
  37. //kbuild:lib-$(CONFIG_SLEEP) += sleep.o
  38. //kbuild:lib-$(CONFIG_ASH_SLEEP) += sleep.o
  39. /* BB_AUDIT SUSv3 compliant */
  40. /* BB_AUDIT GNU issues -- fancy version matches except args must be ints. */
  41. /* http://www.opengroup.org/onlinepubs/007904975/utilities/sleep.html */
  42. //usage:#define sleep_trivial_usage
  43. //usage: IF_FEATURE_FANCY_SLEEP("[") "N" IF_FEATURE_FANCY_SLEEP("]...")
  44. //usage:#define sleep_full_usage "\n\n"
  45. //usage: IF_NOT_FEATURE_FANCY_SLEEP("Pause for N seconds")
  46. //usage: IF_FEATURE_FANCY_SLEEP(
  47. //usage: "Pause for a time equal to the total of the args given, where each arg can\n"
  48. //usage: "have an optional suffix of (s)econds, (m)inutes, (h)ours, or (d)ays")
  49. //usage:
  50. //usage:#define sleep_example_usage
  51. //usage: "$ sleep 2\n"
  52. //usage: "[2 second delay results]\n"
  53. //usage: IF_FEATURE_FANCY_SLEEP(
  54. //usage: "$ sleep 1d 3h 22m 8s\n"
  55. //usage: "[98528 second delay results]\n")
  56. #include "libbb.h"
  57. int sleep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  58. int sleep_main(int argc UNUSED_PARAM, char **argv)
  59. {
  60. duration_t duration;
  61. /* Note: sleep_main may be directly called from ash as a builtin.
  62. * This brings some complications:
  63. * + we can't use xfunc here
  64. * + we can't use bb_show_usage
  65. * + applet_name can be the name of the shell
  66. */
  67. ++argv;
  68. if (!*argv) {
  69. /* Without this, bare "sleep" in ash shows _ash_ --help */
  70. /* (ash can be the "sh" applet as well, so check 2nd char) */
  71. if (ENABLE_ASH_SLEEP && applet_name[1] != 'l') {
  72. bb_simple_error_msg("sleep: missing operand");
  73. return EXIT_FAILURE;
  74. }
  75. bb_show_usage();
  76. }
  77. /* GNU sleep accepts "inf", "INF", "infinity" and "INFINITY" */
  78. if (strncasecmp(argv[0], "inf", 3) == 0)
  79. for (;;)
  80. sleep(INT_MAX);
  81. //FIXME: in ash, "sleep 123qwerty" as a builtin aborts the shell
  82. #if ENABLE_FEATURE_FANCY_SLEEP
  83. duration = 0;
  84. do {
  85. duration += parse_duration_str(*argv);
  86. } while (*++argv);
  87. sleep_for_duration(duration);
  88. #else /* simple */
  89. duration = xatou(*argv);
  90. sleep(duration);
  91. #endif
  92. return EXIT_SUCCESS;
  93. }