1 /* sleep - delay for a specified amount of time.
2 Copyright (C) 1984-2016 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 3 of the License, or
7 (at your option) 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, see <http://www.gnu.org/licenses/>. */
19 #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
)
46 Usage: %s NUMBER[SUFFIX]...\n\
48 Pause for NUMBER seconds. SUFFIX may be 's' for seconds (the default),\n\
49 'm' for minutes, 'h' for hours or 'd' for days. Unlike most implementations\n\
50 that require NUMBER be an integer, here NUMBER may be an arbitrary floating\n\
51 point number. Given two or more arguments, pause for the amount of time\n\
52 specified by the sum of their values.\n\
55 program_name
, program_name
);
56 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
57 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
58 emit_ancillary_info (PROGRAM_NAME
);
63 /* Given a floating point value *X, and a suffix character, SUFFIX_CHAR,
64 scale *X by the multiplier implied by SUFFIX_CHAR. SUFFIX_CHAR may
65 be the NUL byte or 's' to denote seconds, 'm' for minutes, 'h' for
66 hours, or 'd' for days. If SUFFIX_CHAR is invalid, don't modify *X
67 and return false. Otherwise return true. */
70 apply_suffix (double *x
, char suffix_char
)
87 multiplier
= 60 * 60 * 24;
99 main (int argc
, char **argv
)
102 double seconds
= 0.0;
105 initialize_main (&argc
, &argv
);
106 set_program_name (argv
[0]);
107 setlocale (LC_ALL
, "");
108 bindtextdomain (PACKAGE
, LOCALEDIR
);
109 textdomain (PACKAGE
);
111 atexit (close_stdout
);
113 parse_long_options (argc
, argv
, PROGRAM_NAME
, PACKAGE_NAME
, Version
,
114 usage
, AUTHORS
, (char const *) NULL
);
115 if (getopt_long (argc
, argv
, "", NULL
, NULL
) != -1)
116 usage (EXIT_FAILURE
);
120 error (0, 0, _("missing operand"));
121 usage (EXIT_FAILURE
);
124 for (i
= optind
; i
< argc
; i
++)
128 if (! (xstrtod (argv
[i
], &p
, &s
, c_strtod
) || errno
== ERANGE
)
129 /* Nonnegative interval. */
131 /* No extra chars after the number and an optional s,m,h,d char. */
133 /* Check any suffix char and update S based on the suffix. */
134 || ! apply_suffix (&s
, *p
))
136 error (0, 0, _("invalid time interval %s"), quote (argv
[i
]));
144 usage (EXIT_FAILURE
);
146 if (xnanosleep (seconds
))
147 die (EXIT_FAILURE
, errno
, _("cannot read realtime clock"));