(__restrict): Define to `restrict' or to nothing.
[coreutils.git] / src / sleep.c
blob542c8f10fd9da8463ff9a277134e42e5905e1908
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 "closeout.h"
36 #include "error.h"
37 #include "long-options.h"
38 #include "timespec.h"
39 #include "xnanosleep.h"
40 #include "xstrtod.h"
42 /* The official name of this program (e.g., no `g' prefix). */
43 #define PROGRAM_NAME "sleep"
45 #define AUTHORS N_ ("Jim Meyering and Paul Eggert")
47 /* The name by which this program was run. */
48 char *program_name;
50 static struct option const long_options[] =
52 {0, 0, 0, 0}
55 void
56 usage (int status)
58 if (status != 0)
59 fprintf (stderr, _("Try `%s --help' for more information.\n"),
60 program_name);
61 else
63 printf (_("\
64 Usage: %s NUMBER[SUFFIX]...\n\
65 or: %s OPTION\n\
66 Pause for NUMBER seconds. SUFFIX may be `s' for seconds (the default),\n\
67 `m' for minutes, `h' for hours or `d' for days. Unlike most implementations\n\
68 that require NUMBER be an integer, here NUMBER may be an arbitrary floating\n\
69 point number.\n\
70 \n\
71 "),
72 program_name, program_name);
73 fputs (HELP_OPTION_DESCRIPTION, stdout);
74 fputs (VERSION_OPTION_DESCRIPTION, stdout);
75 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
77 exit (status);
80 /* Given a floating point value *X, and a suffix character, SUFFIX_CHAR,
81 scale *X by the multiplier implied by SUFFIX_CHAR. SUFFIX_CHAR may
82 be the NUL byte or `s' to denote seconds, `m' for minutes, `h' for
83 hours, or `d' for days. If SUFFIX_CHAR is invalid, don't modify *X
84 and return nonzero. Otherwise return zero. */
86 static int
87 apply_suffix (double *x, char suffix_char)
89 unsigned int multiplier;
91 switch (suffix_char)
93 case 0:
94 case 's':
95 multiplier = 1;
96 break;
97 case 'm':
98 multiplier = 60;
99 break;
100 case 'h':
101 multiplier = 60 * 60;
102 break;
103 case 'd':
104 multiplier = 60 * 60 * 24;
105 break;
106 default:
107 multiplier = 0;
110 if (multiplier == 0)
111 return 1;
113 *x *= multiplier;
115 return 0;
119 main (int argc, char **argv)
121 int i;
122 double seconds = 0.0;
123 int c;
124 int fail = 0;
126 program_name = argv[0];
127 setlocale (LC_ALL, "");
128 bindtextdomain (PACKAGE, LOCALEDIR);
129 textdomain (PACKAGE);
131 atexit (close_stdout);
133 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
134 AUTHORS, usage);
136 while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
138 switch (c)
140 case 0:
141 break;
143 default:
144 usage (EXIT_FAILURE);
148 if (argc == 1)
150 error (0, 0, _("too few arguments"));
151 usage (EXIT_FAILURE);
154 for (i = optind; i < argc; i++)
156 double s;
157 const char *p;
158 if (xstrtod (argv[i], &p, &s)
159 /* Nonnegative interval. */
160 || ! (0 <= s)
161 /* No extra chars after the number and an optional s,m,h,d char. */
162 || (*p && *(p+1))
163 /* Check any suffix char and update S based on the suffix. */
164 || apply_suffix (&s, *p))
166 error (0, 0, _("invalid time interval `%s'"), argv[i]);
167 fail = 1;
170 seconds += s;
173 if (fail)
174 usage (EXIT_FAILURE);
176 if (xnanosleep (seconds))
177 error (EXIT_FAILURE, errno, _("cannot read realtime clock"));
179 exit (EXIT_SUCCESS);