tests: remove crufty test=test_name code from old tests
[coreutils/ericb.git] / src / sleep.c
blob7aa1d6cb7bb8634e668920fb4c5ac12226e879e4
1 /* sleep - delay for a specified amount of time.
2 Copyright (C) 1984, 1991-1997, 1999-2005, 2007-2012 Free Software
3 Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include <config.h>
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <getopt.h>
23 #include "system.h"
24 #include "c-strtod.h"
25 #include "error.h"
26 #include "long-options.h"
27 #include "quote.h"
28 #include "xnanosleep.h"
29 #include "xstrtod.h"
31 /* The official name of this program (e.g., no 'g' prefix). */
32 #define PROGRAM_NAME "sleep"
34 #define AUTHORS \
35 proper_name ("Jim Meyering"), \
36 proper_name ("Paul Eggert")
38 void
39 usage (int status)
41 if (status != EXIT_SUCCESS)
42 emit_try_help ();
43 else
45 printf (_("\
46 Usage: %s NUMBER[SUFFIX]...\n\
47 or: %s OPTION\n\
48 Pause for NUMBER seconds. SUFFIX may be 's' for seconds (the default),\n\
49 'm' for minutes, 'h' for hours or 'd' for days. Unlike most implementations\n\
50 that require NUMBER be an integer, here NUMBER may be an arbitrary floating\n\
51 point number. Given two or more arguments, pause for the amount of time\n\
52 specified by the sum of their values.\n\
53 \n\
54 "),
55 program_name, program_name);
56 fputs (HELP_OPTION_DESCRIPTION, stdout);
57 fputs (VERSION_OPTION_DESCRIPTION, stdout);
58 emit_ancillary_info ();
60 exit (status);
63 /* Given a floating point value *X, and a suffix character, SUFFIX_CHAR,
64 scale *X by the multiplier implied by SUFFIX_CHAR. SUFFIX_CHAR may
65 be the NUL byte or 's' to denote seconds, 'm' for minutes, 'h' for
66 hours, or 'd' for days. If SUFFIX_CHAR is invalid, don't modify *X
67 and return false. Otherwise return true. */
69 static bool
70 apply_suffix (double *x, char suffix_char)
72 int multiplier;
74 switch (suffix_char)
76 case 0:
77 case 's':
78 multiplier = 1;
79 break;
80 case 'm':
81 multiplier = 60;
82 break;
83 case 'h':
84 multiplier = 60 * 60;
85 break;
86 case 'd':
87 multiplier = 60 * 60 * 24;
88 break;
89 default:
90 return false;
93 *x *= multiplier;
95 return true;
98 int
99 main (int argc, char **argv)
101 int i;
102 double seconds = 0.0;
103 bool ok = true;
105 initialize_main (&argc, &argv);
106 set_program_name (argv[0]);
107 setlocale (LC_ALL, "");
108 bindtextdomain (PACKAGE, LOCALEDIR);
109 textdomain (PACKAGE);
111 atexit (close_stdout);
113 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
114 usage, AUTHORS, (char const *) NULL);
115 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
116 usage (EXIT_FAILURE);
118 if (argc == 1)
120 error (0, 0, _("missing operand"));
121 usage (EXIT_FAILURE);
124 for (i = optind; i < argc; i++)
126 double s;
127 const char *p;
128 if (! xstrtod (argv[i], &p, &s, c_strtod)
129 /* Nonnegative interval. */
130 || ! (0 <= s)
131 /* No extra chars after the number and an optional s,m,h,d char. */
132 || (*p && *(p+1))
133 /* Check any suffix char and update S based on the suffix. */
134 || ! apply_suffix (&s, *p))
136 error (0, 0, _("invalid time interval %s"), quote (argv[i]));
137 ok = false;
140 seconds += s;
143 if (!ok)
144 usage (EXIT_FAILURE);
146 if (xnanosleep (seconds))
147 error (EXIT_FAILURE, errno, _("cannot read realtime clock"));
149 exit (EXIT_SUCCESS);