ls: omit unnecessary test
[coreutils.git] / src / sleep.c
blobad2b349cb13dac4d49f71a418729f5b908539968
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/>. */
17 #include <config.h>
18 #include <stdio.h>
19 #include <sys/types.h>
21 #include "system.h"
22 #include "cl-strtod.h"
23 #include "long-options.h"
24 #include "quote.h"
25 #include "xnanosleep.h"
26 #include "xstrtod.h"
28 /* The official name of this program (e.g., no 'g' prefix). */
29 #define PROGRAM_NAME "sleep"
31 #define AUTHORS \
32 proper_name ("Jim Meyering"), \
33 proper_name ("Paul Eggert")
35 void
36 usage (int status)
38 if (status != EXIT_SUCCESS)
39 emit_try_help ();
40 else
42 printf (_("\
43 Usage: %s NUMBER[SUFFIX]...\n\
44 or: %s OPTION\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\
48 \n\
49 "),
50 program_name, program_name);
51 fputs (HELP_OPTION_DESCRIPTION, stdout);
52 fputs (VERSION_OPTION_DESCRIPTION, stdout);
53 emit_ancillary_info (PROGRAM_NAME);
55 exit (status);
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. */
64 static bool
65 apply_suffix (double *x, char suffix_char)
67 int multiplier;
69 switch (suffix_char)
71 case 0:
72 case 's':
73 multiplier = 1;
74 break;
75 case 'm':
76 multiplier = 60;
77 break;
78 case 'h':
79 multiplier = 60 * 60;
80 break;
81 case 'd':
82 multiplier = 60 * 60 * 24;
83 break;
84 default:
85 return false;
88 *x *= multiplier;
90 return true;
93 int
94 main (int argc, char **argv)
96 double seconds = 0.0;
97 bool ok = true;
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);
111 if (argc == 1)
113 error (0, 0, _("missing operand"));
114 usage (EXIT_FAILURE);
117 for (int i = optind; i < argc; i++)
119 double s;
120 char const *p;
121 if (! (xstrtod (argv[i], &p, &s, cl_strtod) || errno == ERANGE)
122 /* Nonnegative interval. */
123 || ! (0 <= s)
124 /* No extra chars after the number and an optional s,m,h,d char. */
125 || (*p && *(p + 1))
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]));
130 ok = false;
133 seconds += s;
136 if (!ok)
137 usage (EXIT_FAILURE);
139 if (xnanosleep (seconds))
140 error (EXIT_FAILURE, errno, _("cannot read realtime clock"));
142 return EXIT_SUCCESS;