split: port ‘split -n N /dev/null’ better to macOS
[coreutils.git] / src / sleep.c
blob5aeeb98e85b531d66c293fd8cbb6f205d6ccf97e
1 /* sleep - delay for a specified amount of time.
2 Copyright (C) 1984-2023 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 "die.h"
24 #include "error.h"
25 #include "long-options.h"
26 #include "quote.h"
27 #include "xnanosleep.h"
28 #include "xstrtod.h"
30 /* The official name of this program (e.g., no 'g' prefix). */
31 #define PROGRAM_NAME "sleep"
33 #define AUTHORS \
34 proper_name ("Jim Meyering"), \
35 proper_name ("Paul Eggert")
37 void
38 usage (int status)
40 if (status != EXIT_SUCCESS)
41 emit_try_help ();
42 else
44 printf (_("\
45 Usage: %s NUMBER[SUFFIX]...\n\
46 or: %s OPTION\n\
47 Pause for NUMBER seconds. SUFFIX may be 's' for seconds (the default),\n\
48 'm' for minutes, 'h' for hours or 'd' for days. NUMBER need not be an\n\
49 integer. Given two or more arguments, pause for the amount of time\n\
50 specified by the sum of their values.\n\
51 \n\
52 "),
53 program_name, program_name);
54 fputs (HELP_OPTION_DESCRIPTION, stdout);
55 fputs (VERSION_OPTION_DESCRIPTION, stdout);
56 emit_ancillary_info (PROGRAM_NAME);
58 exit (status);
61 /* Given a floating point value *X, and a suffix character, SUFFIX_CHAR,
62 scale *X by the multiplier implied by SUFFIX_CHAR. SUFFIX_CHAR may
63 be the NUL byte or 's' to denote seconds, 'm' for minutes, 'h' for
64 hours, or 'd' for days. If SUFFIX_CHAR is invalid, don't modify *X
65 and return false. Otherwise return true. */
67 static bool
68 apply_suffix (double *x, char suffix_char)
70 int multiplier;
72 switch (suffix_char)
74 case 0:
75 case 's':
76 multiplier = 1;
77 break;
78 case 'm':
79 multiplier = 60;
80 break;
81 case 'h':
82 multiplier = 60 * 60;
83 break;
84 case 'd':
85 multiplier = 60 * 60 * 24;
86 break;
87 default:
88 return false;
91 *x *= multiplier;
93 return true;
96 int
97 main (int argc, char **argv)
99 double seconds = 0.0;
100 bool ok = true;
102 initialize_main (&argc, &argv);
103 set_program_name (argv[0]);
104 setlocale (LC_ALL, "");
105 bindtextdomain (PACKAGE, LOCALEDIR);
106 textdomain (PACKAGE);
108 atexit (close_stdout);
110 parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
111 Version, true, usage, AUTHORS,
112 (char const *) NULL);
114 if (argc == 1)
116 error (0, 0, _("missing operand"));
117 usage (EXIT_FAILURE);
120 for (int i = optind; i < argc; i++)
122 double s;
123 char const *p;
124 if (! (xstrtod (argv[i], &p, &s, cl_strtod) || errno == ERANGE)
125 /* Nonnegative interval. */
126 || ! (0 <= s)
127 /* No extra chars after the number and an optional s,m,h,d char. */
128 || (*p && *(p + 1))
129 /* Check any suffix char and update S based on the suffix. */
130 || ! apply_suffix (&s, *p))
132 error (0, 0, _("invalid time interval %s"), quote (argv[i]));
133 ok = false;
136 seconds += s;
139 if (!ok)
140 usage (EXIT_FAILURE);
142 if (xnanosleep (seconds))
143 die (EXIT_FAILURE, errno, _("cannot read realtime clock"));
145 return EXIT_SUCCESS;