1 /* sleep - delay for a specified amount of time.
2 Copyright (C) 84, 1991-1997, 1999-2005, 2007-2008
3 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <sys/types.h>
26 #include "long-options.h"
28 #include "xnanosleep.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "sleep"
35 proper_name ("Jim Meyering"), \
36 proper_name ("Paul Eggert")
41 if (status != EXIT_SUCCESS)
42 fprintf (stderr, _("Try `%s --help' for more information.\n"),
47 Usage: %s NUMBER[SUFFIX]...\n\
49 Pause for NUMBER seconds. SUFFIX may be `s' for seconds (the default),\n\
50 `m' for minutes, `h' for hours or `d' for days. Unlike most implementations\n\
51 that require NUMBER be an integer, here NUMBER may be an arbitrary floating\n\
52 point number. Given two or more arguments, pause for the amount of time\n\
53 specified by the sum of their values.\n\
56 program_name, program_name);
57 fputs (HELP_OPTION_DESCRIPTION, stdout);
58 fputs (VERSION_OPTION_DESCRIPTION, stdout);
59 emit_bug_reporting_address ();
64 /* Given a floating point value *X, and a suffix character, SUFFIX_CHAR,
65 scale *X by the multiplier implied by SUFFIX_CHAR. SUFFIX_CHAR may
66 be the NUL byte or `s' to denote seconds, `m' for minutes, `h' for
67 hours, or `d' for days. If SUFFIX_CHAR is invalid, don't modify *X
68 and return false. Otherwise return true. */
71 apply_suffix (double *x, char suffix_char)
88 multiplier = 60 * 60 * 24;
100 main (int argc, char **argv)
103 double seconds = 0.0;
106 initialize_main (&argc, &argv);
107 set_program_name (argv[0]);
108 setlocale (LC_ALL, "");
109 bindtextdomain (PACKAGE, LOCALEDIR);
110 textdomain (PACKAGE);
112 atexit (close_stdout);
114 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, VERSION,
115 usage, AUTHORS, (char const *) NULL);
116 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
117 usage (EXIT_FAILURE);
121 error (0, 0, _("missing operand"));
122 usage (EXIT_FAILURE);
125 for (i = optind; i < argc; i++)
129 if (! xstrtod (argv[i], &p, &s, c_strtod)
130 /* Nonnegative interval. */
132 /* No extra chars after the number and an optional s,m,h,d char. */
134 /* Check any suffix char and update S based on the suffix. */
135 || ! apply_suffix (&s, *p))
137 error (0, 0, _("invalid time interval %s"), quote (argv[i]));
145 usage (EXIT_FAILURE);
147 if (xnanosleep (seconds))
148 error (EXIT_FAILURE, errno, _("cannot read realtime clock"));