Annotation of /trunk/mkinitrd-magellan/busybox/coreutils/sleep.c
Parent Directory | Revision Log
Revision 984 -
(hide annotations)
(download)
Sun May 30 11:32:42 2010 UTC (13 years, 11 months ago) by niro
File MIME type: text/plain
File size: 2273 byte(s)
Sun May 30 11:32:42 2010 UTC (13 years, 11 months ago) by niro
File MIME type: text/plain
File size: 2273 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 | niro | 532 | /* 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 | niro | 816 | #include "libbb.h" |
22 | niro | 532 | |
23 | niro | 816 | /* This is a NOFORK applet. Be very careful! */ |
24 | |||
25 | |||
26 | #if ENABLE_FEATURE_FANCY_SLEEP || ENABLE_FEATURE_FLOAT_SLEEP | ||
27 | niro | 532 | static const struct suffix_mult sfx[] = { |
28 | { "s", 1 }, | ||
29 | { "m", 60 }, | ||
30 | { "h", 60*60 }, | ||
31 | { "d", 24*60*60 }, | ||
32 | niro | 984 | { "", 0 } |
33 | niro | 532 | }; |
34 | #endif | ||
35 | |||
36 | niro | 816 | int sleep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
37 | int sleep_main(int argc UNUSED_PARAM, char **argv) | ||
38 | niro | 532 | { |
39 | niro | 816 | #if ENABLE_FEATURE_FLOAT_SLEEP |
40 | double duration; | ||
41 | struct timespec ts; | ||
42 | #else | ||
43 | unsigned duration; | ||
44 | #endif | ||
45 | niro | 532 | |
46 | niro | 816 | ++argv; |
47 | if (!*argv) | ||
48 | niro | 532 | bb_show_usage(); |
49 | |||
50 | niro | 816 | #if ENABLE_FEATURE_FLOAT_SLEEP |
51 | |||
52 | niro | 532 | duration = 0; |
53 | do { | ||
54 | niro | 816 | char *arg = *argv; |
55 | if (strchr(arg, '.')) { | ||
56 | double d; | ||
57 | niro | 984 | char *pp; |
58 | niro | 816 | int len = strspn(arg, "0123456789."); |
59 | char sv = arg[len]; | ||
60 | arg[len] = '\0'; | ||
61 | niro | 984 | errno = 0; |
62 | d = strtod(arg, &pp); | ||
63 | if (errno || *pp) | ||
64 | niro | 816 | bb_show_usage(); |
65 | arg[len] = sv; | ||
66 | len--; | ||
67 | sv = arg[len]; | ||
68 | arg[len] = '1'; | ||
69 | duration += d * xatoul_sfx(&arg[len], sfx); | ||
70 | arg[len] = sv; | ||
71 | } else | ||
72 | duration += xatoul_sfx(arg, sfx); | ||
73 | niro | 532 | } while (*++argv); |
74 | |||
75 | niro | 816 | ts.tv_sec = MAXINT(typeof(ts.tv_sec)); |
76 | ts.tv_nsec = 0; | ||
77 | if (duration >= 0 && duration < ts.tv_sec) { | ||
78 | ts.tv_sec = duration; | ||
79 | ts.tv_nsec = (duration - ts.tv_sec) * 1000000000; | ||
80 | niro | 532 | } |
81 | niro | 816 | do { |
82 | errno = 0; | ||
83 | nanosleep(&ts, &ts); | ||
84 | } while (errno == EINTR); | ||
85 | niro | 532 | |
86 | niro | 816 | #elif ENABLE_FEATURE_FANCY_SLEEP |
87 | niro | 532 | |
88 | niro | 816 | duration = 0; |
89 | do { | ||
90 | duration += xatou_range_sfx(*argv, 0, UINT_MAX - duration, sfx); | ||
91 | } while (*++argv); | ||
92 | sleep(duration); | ||
93 | niro | 532 | |
94 | niro | 816 | #else /* simple */ |
95 | niro | 532 | |
96 | niro | 816 | duration = xatou(*argv); |
97 | sleep(duration); | ||
98 | // Off. If it's really needed, provide example why | ||
99 | //if (sleep(duration)) { | ||
100 | // bb_perror_nomsg_and_die(); | ||
101 | //} | ||
102 | |||
103 | #endif | ||
104 | |||
105 | niro | 532 | return EXIT_SUCCESS; |
106 | } |