*** empty log message ***
[coreutils.git] / src / sleep.c
blob57be102de9d54d4a920f5e0d20bc4e7e2555b955
1 /* sleep - delay for a specified amount of time.
2 Copyright (C) 84, 1991-1997, 1999-2002 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #include <config.h>
19 #include <stdio.h>
20 #include <assert.h>
21 #include <sys/types.h>
22 #include <time.h>
23 #include <getopt.h>
25 #define USE_CLOCK_GETTIME (defined CLOCK_REALTIME && HAVE_CLOCK_GETTIME)
26 #if ! USE_CLOCK_GETTIME
27 # include <sys/time.h>
28 #endif
30 #ifndef TIME_T_MAX
31 # define TIME_T_MAX TYPE_MAXIMUM (time_t)
32 #endif
34 #include "system.h"
35 #include "error.h"
36 #include "long-options.h"
37 #include "timespec.h"
38 #include "xstrtod.h"
39 #include "closeout.h"
41 #if HAVE_FENV_H
42 # include <fenv.h>
43 #endif
45 /* Tell the compiler that non-default rounding modes are used. */
46 #if 199901 <= __STDC_VERSION__
47 #pragma STDC FENV_ACCESS ON
48 #endif
50 /* The official name of this program (e.g., no `g' prefix). */
51 #define PROGRAM_NAME "sleep"
53 #define AUTHORS N_ ("Jim Meyering and Paul Eggert")
55 /* The name by which this program was run. */
56 char *program_name;
58 static struct option const long_options[] =
60 {0, 0, 0, 0}
63 void
64 usage (int status)
66 if (status != 0)
67 fprintf (stderr, _("Try `%s --help' for more information.\n"),
68 program_name);
69 else
71 printf (_("\
72 Usage: %s NUMBER[SUFFIX]...\n\
73 or: %s OPTION\n\
74 Pause for NUMBER seconds. SUFFIX may be `s' for seconds (the default),\n\
75 `m' for minutes, `h' for hours or `d' for days. Unlike most implementations\n\
76 that require NUMBER be an integer, here NUMBER may be an arbitrary floating\n\
77 point number.\n\
78 \n\
79 "),
80 program_name, program_name);
81 fputs (HELP_OPTION_DESCRIPTION, stdout);
82 fputs (VERSION_OPTION_DESCRIPTION, stdout);
83 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
85 exit (status);
88 /* Given a floating point value *X, and a suffix character, SUFFIX_CHAR,
89 scale *X by the multiplier implied by SUFFIX_CHAR. SUFFIX_CHAR may
90 be the NUL byte or `s' to denote seconds, `m' for minutes, `h' for
91 hours, or `d' for days. If SUFFIX_CHAR is invalid, don't modify *X
92 and return nonzero. Otherwise return zero. */
94 static int
95 apply_suffix (double *x, char suffix_char)
97 unsigned int multiplier;
99 switch (suffix_char)
101 case 0:
102 case 's':
103 multiplier = 1;
104 break;
105 case 'm':
106 multiplier = 60;
107 break;
108 case 'h':
109 multiplier = 60 * 60;
110 break;
111 case 'd':
112 multiplier = 60 * 60 * 24;
113 break;
114 default:
115 multiplier = 0;
118 if (multiplier == 0)
119 return 1;
121 *x *= multiplier;
123 return 0;
126 /* Subtract the `struct timespec' values X and Y,
127 storing the difference in DIFF.
128 Return 1 if the difference is positive, otherwise 0.
129 Derived from code in the GNU libc manual. */
131 static int
132 timespec_subtract (struct timespec *diff,
133 const struct timespec *x, struct timespec *y)
135 /* Perform the carry for the later subtraction by updating Y. */
136 if (x->tv_nsec < y->tv_nsec)
138 int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
139 y->tv_nsec -= 1000000000 * nsec;
140 y->tv_sec += nsec;
143 if (1000000000 < x->tv_nsec - y->tv_nsec)
145 int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000;
146 y->tv_nsec += 1000000000 * nsec;
147 y->tv_sec -= nsec;
150 /* Compute the time remaining to wait.
151 `tv_nsec' is certainly positive. */
152 diff->tv_sec = x->tv_sec - y->tv_sec;
153 diff->tv_nsec = x->tv_nsec - y->tv_nsec;
155 /* Return 1 if result is positive. */
156 return y->tv_sec < x->tv_sec;
159 static struct timespec *
160 clock_get_realtime (struct timespec *ts)
162 int fail;
163 #if USE_CLOCK_GETTIME
164 fail = clock_gettime (CLOCK_REALTIME, ts);
165 #else
166 struct timeval tv;
167 fail = gettimeofday (&tv, NULL);
168 if (!fail)
170 ts->tv_sec = tv.tv_sec;
171 ts->tv_nsec = 1000 * tv.tv_usec;
173 #endif
175 if (fail)
176 error (1, errno, _("cannot read realtime clock"));
178 return ts;
182 main (int argc, char **argv)
184 int i;
185 double seconds = 0.0;
186 double ns;
187 int c;
188 int fail = 0;
189 int forever;
190 struct timespec ts_start;
191 struct timespec ts_stop;
192 struct timespec ts_sleep;
194 /* Record start time. */
195 clock_get_realtime (&ts_start);
197 program_name = argv[0];
198 setlocale (LC_ALL, "");
199 bindtextdomain (PACKAGE, LOCALEDIR);
200 textdomain (PACKAGE);
202 atexit (close_stdout);
204 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
205 AUTHORS, usage);
207 while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
209 switch (c)
211 case 0:
212 break;
214 default:
215 usage (1);
219 if (argc == 1)
221 error (0, 0, _("too few arguments"));
222 usage (1);
225 #ifdef FE_UPWARD
226 /* Always round up, since we must sleep for at least the specified
227 interval. */
228 fesetround (FE_UPWARD);
229 #endif
231 for (i = optind; i < argc; i++)
233 double s;
234 const char *p;
235 if (xstrtod (argv[i], &p, &s)
236 /* Nonnegative interval. */
237 || ! (0 <= s)
238 /* No extra chars after the number and an optional s,m,h,d char. */
239 || (*p && *(p+1))
240 /* Check any suffix char and update S based on the suffix. */
241 || apply_suffix (&s, *p))
243 error (0, 0, _("invalid time interval `%s'"), argv[i]);
244 fail = 1;
247 seconds += s;
250 if (fail)
251 usage (1);
253 /* Separate whole seconds from nanoseconds.
254 Be careful to detect any overflow. */
255 ts_sleep.tv_sec = seconds;
256 ns = 1e9 * (seconds - ts_sleep.tv_sec);
257 forever = ! (ts_sleep.tv_sec <= seconds && 0 <= ns && ns <= 1e9);
258 ts_sleep.tv_nsec = ns;
260 /* Round up to the next whole number, if necessary, so that we
261 always sleep for at least the requested amount of time. */
262 ts_sleep.tv_nsec += (ts_sleep.tv_nsec < ns);
264 /* Normalize the interval length. nanosleep requires this. */
265 if (1000000000 <= ts_sleep.tv_nsec)
267 time_t t = ts_sleep.tv_sec + 1;
269 /* Detect integer overflow. */
270 forever |= (t < ts_sleep.tv_sec);
272 ts_sleep.tv_sec = t;
273 ts_sleep.tv_nsec -= 1000000000;
276 ts_stop.tv_sec = ts_start.tv_sec + ts_sleep.tv_sec;
277 ts_stop.tv_nsec = ts_start.tv_nsec + ts_sleep.tv_nsec;
278 if (1000000000 <= ts_stop.tv_nsec)
280 ++ts_stop.tv_sec;
281 ts_stop.tv_nsec -= 1000000000;
284 /* Detect integer overflow. */
285 forever |= (ts_stop.tv_sec < ts_start.tv_sec
286 || (ts_stop.tv_sec == ts_start.tv_sec
287 && ts_stop.tv_nsec < ts_start.tv_nsec));
289 if (forever)
291 /* Fix ts_sleep and ts_stop, which may be garbage due to overflow. */
292 ts_sleep.tv_sec = ts_stop.tv_sec = TIME_T_MAX;
293 ts_sleep.tv_nsec = ts_stop.tv_nsec = 999999999;
296 while (nanosleep (&ts_sleep, NULL) != 0
297 && timespec_subtract (&ts_sleep, &ts_stop,
298 clock_get_realtime (&ts_start)))
299 continue;
301 exit (0);