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. SUFFIX may be 's' for seconds (the default),\n\
46 'm' for minutes, 'h' for hours or 'd' for days. NUMBER need not be an\n\
47 integer. Given two or more arguments, pause for the amount of time\n\
48 specified by the sum of their values.\n\
51 program_name
, program_name
);
52 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
53 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
54 emit_ancillary_info (PROGRAM_NAME
);
59 /* Given a floating point value *X, and a suffix character, SUFFIX_CHAR,
60 scale *X by the multiplier implied by SUFFIX_CHAR. SUFFIX_CHAR may
61 be the NUL byte or 's' to denote seconds, 'm' for minutes, 'h' for
62 hours, or 'd' for days. If SUFFIX_CHAR is invalid, don't modify *X
63 and return false. Otherwise return true. */
66 apply_suffix (double *x
, char suffix_char
)
83 multiplier
= 60 * 60 * 24;
95 main (int argc
, char **argv
)
100 initialize_main (&argc
, &argv
);
101 set_program_name (argv
[0]);
102 setlocale (LC_ALL
, "");
103 bindtextdomain (PACKAGE
, LOCALEDIR
);
104 textdomain (PACKAGE
);
106 atexit (close_stdout
);
108 parse_gnu_standard_options_only (argc
, argv
, PROGRAM_NAME
, PACKAGE_NAME
,
109 Version
, true, usage
, AUTHORS
,
110 (char const *) nullptr);
114 error (0, 0, _("missing operand"));
115 usage (EXIT_FAILURE
);
118 for (int i
= optind
; i
< argc
; i
++)
122 if (! (xstrtod (argv
[i
], &p
, &s
, cl_strtod
) || errno
== ERANGE
)
123 /* Nonnegative interval. */
125 /* No extra chars after the number and an optional s,m,h,d char. */
127 /* Check any suffix char and update S based on the suffix. */
128 || ! apply_suffix (&s
, *p
))
130 error (0, 0, _("invalid time interval %s"), quote (argv
[i
]));
138 usage (EXIT_FAILURE
);
140 if (xnanosleep (seconds
))
141 error (EXIT_FAILURE
, errno
, _("cannot read realtime clock"));