sleep.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* BB_AUDIT SUSv3 compliant */
  23. /* BB_AUDIT GNU issues -- fancy version matches except args must be ints. */
  24. /* http://www.opengroup.org/onlinepubs/007904975/utilities/sleep.html */
  25. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  26. *
  27. * Rewritten to do proper arg and error checking.
  28. * Also, added a 'fancy' configuration to accept multiple args with
  29. * time suffixes for seconds, minutes, hours, and days.
  30. */
  31. #include <stdlib.h>
  32. #include <limits.h>
  33. #include <unistd.h>
  34. #include "busybox.h"
  35. #ifdef CONFIG_FEATURE_FANCY_SLEEP
  36. static const struct suffix_mult sleep_suffixes[] = {
  37. { "s", 1 },
  38. { "m", 60 },
  39. { "h", 60*60 },
  40. { "d", 24*60*60 },
  41. { NULL, 0 }
  42. };
  43. #endif
  44. int sleep_main(int argc, char **argv)
  45. {
  46. unsigned int duration;
  47. #ifdef CONFIG_FEATURE_FANCY_SLEEP
  48. if (argc < 2) {
  49. bb_show_usage();
  50. }
  51. ++argv;
  52. duration = 0;
  53. do {
  54. duration += bb_xgetularg_bnd_sfx(*argv, 10,
  55. 0, UINT_MAX-duration,
  56. sleep_suffixes);
  57. } while (*++argv);
  58. #else /* CONFIG_FEATURE_FANCY_SLEEP */
  59. if (argc != 2) {
  60. bb_show_usage();
  61. }
  62. #if UINT_MAX == ULONG_MAX
  63. duration = bb_xgetularg10(argv[1]);
  64. #else
  65. duration = bb_xgetularg10_bnd(argv[1], 0, UINT_MAX);
  66. #endif
  67. #endif /* CONFIG_FEATURE_FANCY_SLEEP */
  68. if (sleep(duration)) {
  69. bb_perror_nomsg_and_die();
  70. }
  71. return EXIT_SUCCESS;
  72. }