build: avoid compile warnings in factor.c on some systems
[coreutils.git] / src / sleep.c
blob3aec9379179ac86b46be144d68e3fb59616dcbf8
1 /* sleep - delay for a specified amount of time.
2 Copyright (C) 1984-2012 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 <http://www.gnu.org/licenses/>. */
17 #include <config.h>
18 #include <stdio.h>
19 #include <sys/types.h>
20 #include <getopt.h>
22 #include "system.h"
23 #include "c-strtod.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. Unlike most implementations\n\
49 that require NUMBER be an integer, here NUMBER may be an arbitrary floating\n\
50 point number. Given two or more arguments, pause for the amount of time\n\
51 specified by the sum of their values.\n\
52 \n\
53 "),
54 program_name, program_name);
55 fputs (HELP_OPTION_DESCRIPTION, stdout);
56 fputs (VERSION_OPTION_DESCRIPTION, stdout);
57 emit_ancillary_info ();
59 exit (status);
62 /* Given a floating point value *X, and a suffix character, SUFFIX_CHAR,
63 scale *X by the multiplier implied by SUFFIX_CHAR. SUFFIX_CHAR may
64 be the NUL byte or 's' to denote seconds, 'm' for minutes, 'h' for
65 hours, or 'd' for days. If SUFFIX_CHAR is invalid, don't modify *X
66 and return false. Otherwise return true. */
68 static bool
69 apply_suffix (double *x, char suffix_char)
71 int multiplier;
73 switch (suffix_char)
75 case 0:
76 case 's':
77 multiplier = 1;
78 break;
79 case 'm':
80 multiplier = 60;
81 break;
82 case 'h':
83 multiplier = 60 * 60;
84 break;
85 case 'd':
86 multiplier = 60 * 60 * 24;
87 break;
88 default:
89 return false;
92 *x *= multiplier;
94 return true;
97 int
98 main (int argc, char **argv)
100 int i;
101 double seconds = 0.0;
102 bool ok = true;
104 initialize_main (&argc, &argv);
105 set_program_name (argv[0]);
106 setlocale (LC_ALL, "");
107 bindtextdomain (PACKAGE, LOCALEDIR);
108 textdomain (PACKAGE);
110 atexit (close_stdout);
112 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
113 usage, AUTHORS, (char const *) NULL);
114 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
115 usage (EXIT_FAILURE);
117 if (argc == 1)
119 error (0, 0, _("missing operand"));
120 usage (EXIT_FAILURE);
123 for (i = optind; i < argc; i++)
125 double s;
126 const char *p;
127 if (! xstrtod (argv[i], &p, &s, c_strtod)
128 /* Nonnegative interval. */
129 || ! (0 <= s)
130 /* No extra chars after the number and an optional s,m,h,d char. */
131 || (*p && *(p+1))
132 /* Check any suffix char and update S based on the suffix. */
133 || ! apply_suffix (&s, *p))
135 error (0, 0, _("invalid time interval %s"), quote (argv[i]));
136 ok = false;
139 seconds += s;
142 if (!ok)
143 usage (EXIT_FAILURE);
145 if (xnanosleep (seconds))
146 error (EXIT_FAILURE, errno, _("cannot read realtime clock"));
148 exit (EXIT_SUCCESS);