1 /* sleep - delay for a specified amount of time.
2    Copyright (C) 1984-2023 Free Software Foundation, Inc.
3 
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.
8 
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.
13 
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/>.  */
16 
17 #include <config.h>
18 #include <stdio.h>
19 #include <sys/types.h>
20 
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"
27 
28 /* The official name of this program (e.g., no 'g' prefix).  */
29 #define PROGRAM_NAME "sleep"
30 
31 #define AUTHORS \
32   proper_name ("Jim Meyering"), \
33   proper_name ("Paul Eggert")
34 
35 void
usage(int status)36 usage (int status)
37 {
38   if (status != EXIT_SUCCESS)
39     emit_try_help ();
40   else
41     {
42       printf (_("\
43 Usage: %s NUMBER[SUFFIX]...\n\
44   or:  %s OPTION\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\
49 \n\
50 "),
51               program_name, program_name);
52       fputs (HELP_OPTION_DESCRIPTION, stdout);
53       fputs (VERSION_OPTION_DESCRIPTION, stdout);
54       emit_ancillary_info (PROGRAM_NAME);
55     }
56   exit (status);
57 }
58 
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.  */
64 
65 static bool
apply_suffix(double * x,char suffix_char)66 apply_suffix (double *x, char suffix_char)
67 {
68   int multiplier;
69 
70   switch (suffix_char)
71     {
72     case 0:
73     case 's':
74       multiplier = 1;
75       break;
76     case 'm':
77       multiplier = 60;
78       break;
79     case 'h':
80       multiplier = 60 * 60;
81       break;
82     case 'd':
83       multiplier = 60 * 60 * 24;
84       break;
85     default:
86       return false;
87     }
88 
89   *x *= multiplier;
90 
91   return true;
92 }
93 
94 int
main(int argc,char ** argv)95 main (int argc, char **argv)
96 {
97   double seconds = 0.0;
98   bool ok = true;
99 
100   initialize_main (&argc, &argv);
101   set_program_name (argv[0]);
102   setlocale (LC_ALL, "");
103   bindtextdomain (PACKAGE, LOCALEDIR);
104   textdomain (PACKAGE);
105 
106   atexit (close_stdout);
107 
108   parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
109                                    Version, true, usage, AUTHORS,
110                                    (char const *) nullptr);
111 
112   if (argc == 1)
113     {
114       error (0, 0, _("missing operand"));
115       usage (EXIT_FAILURE);
116     }
117 
118   for (int i = optind; i < argc; i++)
119     {
120       double s;
121       char const *p;
122       if (! (xstrtod (argv[i], &p, &s, cl_strtod) || errno == ERANGE)
123           /* Nonnegative interval.  */
124           || ! (0 <= s)
125           /* No extra chars after the number and an optional s,m,h,d char.  */
126           || (*p && *(p + 1))
127           /* Check any suffix char and update S based on the suffix.  */
128           || ! apply_suffix (&s, *p))
129         {
130           error (0, 0, _("invalid time interval %s"), quote (argv[i]));
131           ok = false;
132         }
133 
134       seconds += s;
135     }
136 
137   if (!ok)
138     usage (EXIT_FAILURE);
139 
140   if (xnanosleep (seconds))
141     error (EXIT_FAILURE, errno, _("cannot read realtime clock"));
142 
143   return EXIT_SUCCESS;
144 }
145