1 /* sleep - delay for a specified amount of time.
2 Copyright (C) 1984-2024 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 <https://www.gnu.org/licenses/>. */
19 #include <sys/types.h>
22 #include "cl-strtod.h"
23 #include "long-options.h"
25 #include "xnanosleep.h"
28 /* The official name of this program (e.g., no 'g' prefix). */
29 #define PROGRAM_NAME "sleep"
32 proper_name ("Jim Meyering"), \
33 proper_name ("Paul Eggert")
38 if (status
!= EXIT_SUCCESS
)
43 Usage: %s NUMBER[SUFFIX]...\n\
45 Pause for NUMBER seconds, where NUMBER is an integer or floating-point.\n\
46 SUFFIX may be 's','m','h', or 'd', for seconds, minutes, hours, days.\n\
47 With multiple arguments, pause for the sum of their values.\n\
50 program_name
, program_name
);
51 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
52 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
53 emit_ancillary_info (PROGRAM_NAME
);
58 /* Given a floating point value *X, and a suffix character, SUFFIX_CHAR,
59 scale *X by the multiplier implied by SUFFIX_CHAR. SUFFIX_CHAR may
60 be the NUL byte or 's' to denote seconds, 'm' for minutes, 'h' for
61 hours, or 'd' for days. If SUFFIX_CHAR is invalid, don't modify *X
62 and return false. Otherwise return true. */
65 apply_suffix (double *x
, char suffix_char
)
82 multiplier
= 60 * 60 * 24;
94 main (int argc
, char **argv
)
99 initialize_main (&argc
, &argv
);
100 set_program_name (argv
[0]);
101 setlocale (LC_ALL
, "");
102 bindtextdomain (PACKAGE
, LOCALEDIR
);
103 textdomain (PACKAGE
);
105 atexit (close_stdout
);
107 parse_gnu_standard_options_only (argc
, argv
, PROGRAM_NAME
, PACKAGE_NAME
,
108 Version
, true, usage
, AUTHORS
,
109 (char const *) nullptr);
113 error (0, 0, _("missing operand"));
114 usage (EXIT_FAILURE
);
117 for (int i
= optind
; i
< argc
; i
++)
121 if (! (xstrtod (argv
[i
], &p
, &s
, cl_strtod
) || errno
== ERANGE
)
122 /* Nonnegative interval. */
124 /* No extra chars after the number and an optional s,m,h,d char. */
126 /* Check any suffix char and update S based on the suffix. */
127 || ! apply_suffix (&s
, *p
))
129 error (0, 0, _("invalid time interval %s"), quote (argv
[i
]));
137 usage (EXIT_FAILURE
);
139 if (xnanosleep (seconds
))
140 error (EXIT_FAILURE
, errno
, _("cannot read realtime clock"));