introduce LONE_CHAR (optimized strcmp with one-char string)
[oweals/busybox.git] / coreutils / sleep.c
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
10 /* BB_AUDIT SUSv3 compliant */
11 /* BB_AUDIT GNU issues -- fancy version matches except args must be ints. */
12 /* http://www.opengroup.org/onlinepubs/007904975/utilities/sleep.html */
13
14 /* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
15  *
16  * Rewritten to do proper arg and error checking.
17  * Also, added a 'fancy' configuration to accept multiple args with
18  * time suffixes for seconds, minutes, hours, and days.
19  */
20
21 #include <stdlib.h>
22 #include <limits.h>
23 #include <unistd.h>
24 #include "busybox.h"
25
26 #ifdef CONFIG_FEATURE_FANCY_SLEEP
27 static const struct suffix_mult sfx[] = {
28         { "s", 1 },
29         { "m", 60 },
30         { "h", 60*60 },
31         { "d", 24*60*60 },
32         { NULL, 0 }
33 };
34 #endif
35
36 int sleep_main(int argc, char **argv)
37 {
38         unsigned int duration;
39
40 #ifdef CONFIG_FEATURE_FANCY_SLEEP
41
42         if (argc < 2) {
43                 bb_show_usage();
44         }
45
46         ++argv;
47         duration = 0;
48         do {
49                 duration += xatoul_range_sfx(*argv, 0, UINT_MAX-duration, sfx);
50         } while (*++argv);
51
52 #else  /* CONFIG_FEATURE_FANCY_SLEEP */
53
54         if (argc != 2) {
55                 bb_show_usage();
56         }
57
58         duration = xatou(argv[1]);
59
60 #endif /* CONFIG_FEATURE_FANCY_SLEEP */
61
62         if (sleep(duration)) {
63                 bb_perror_nomsg_and_die();
64         }
65
66         return EXIT_SUCCESS;
67 }