convert the rest to use proper_name -- manually
[coreutils.git] / src / sleep.c
blob39bb0c91c80ccaae25f8c15e6c29d680eaf26540
1 /* sleep - delay for a specified amount of time.
2 Copyright (C) 84, 1991-1997, 1999-2005, 2007 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 /* The name by which this program was run. */
38 char *program_name;
40 void
41 usage (int status)
43 if (status != EXIT_SUCCESS)
44 fprintf (stderr, _("Try `%s --help' for more information.\n"),
45 program_name);
46 else
48 printf (_("\
49 Usage: %s NUMBER[SUFFIX]...\n\
50 or: %s OPTION\n\
51 Pause for NUMBER seconds. SUFFIX may be `s' for seconds (the default),\n\
52 `m' for minutes, `h' for hours or `d' for days. Unlike most implementations\n\
53 that require NUMBER be an integer, here NUMBER may be an arbitrary floating\n\
54 point number. Given two or more arguments, pause for the amount of time\n\
55 specified by the sum of their values.\n\
56 \n\
57 "),
58 program_name, program_name);
59 fputs (HELP_OPTION_DESCRIPTION, stdout);
60 fputs (VERSION_OPTION_DESCRIPTION, stdout);
61 emit_bug_reporting_address ();
63 exit (status);
66 /* Given a floating point value *X, and a suffix character, SUFFIX_CHAR,
67 scale *X by the multiplier implied by SUFFIX_CHAR. SUFFIX_CHAR may
68 be the NUL byte or `s' to denote seconds, `m' for minutes, `h' for
69 hours, or `d' for days. If SUFFIX_CHAR is invalid, don't modify *X
70 and return false. Otherwise return true. */
72 static bool
73 apply_suffix (double *x, char suffix_char)
75 int multiplier;
77 switch (suffix_char)
79 case 0:
80 case 's':
81 multiplier = 1;
82 break;
83 case 'm':
84 multiplier = 60;
85 break;
86 case 'h':
87 multiplier = 60 * 60;
88 break;
89 case 'd':
90 multiplier = 60 * 60 * 24;
91 break;
92 default:
93 return false;
96 *x *= multiplier;
98 return true;
102 main (int argc, char **argv)
104 int i;
105 double seconds = 0.0;
106 bool ok = true;
108 initialize_main (&argc, &argv);
109 program_name = argv[0];
110 setlocale (LC_ALL, "");
111 bindtextdomain (PACKAGE, LOCALEDIR);
112 textdomain (PACKAGE);
114 atexit (close_stdout);
116 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, VERSION,
117 usage, AUTHORS, (char const *) NULL);
118 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
119 usage (EXIT_FAILURE);
121 if (argc == 1)
123 error (0, 0, _("missing operand"));
124 usage (EXIT_FAILURE);
127 for (i = optind; i < argc; i++)
129 double s;
130 const char *p;
131 if (! xstrtod (argv[i], &p, &s, c_strtod)
132 /* Nonnegative interval. */
133 || ! (0 <= s)
134 /* No extra chars after the number and an optional s,m,h,d char. */
135 || (*p && *(p+1))
136 /* Check any suffix char and update S based on the suffix. */
137 || ! apply_suffix (&s, *p))
139 error (0, 0, _("invalid time interval %s"), quote (argv[i]));
140 ok = false;
143 seconds += s;
146 if (!ok)
147 usage (EXIT_FAILURE);
149 if (xnanosleep (seconds))
150 error (EXIT_FAILURE, errno, _("cannot read realtime clock"));
152 exit (EXIT_SUCCESS);