ls: reorder includes to work around broken <sys/capability.h>
[coreutils.git] / src / sleep.c
blob699edbd7323d5b46085404b97958bb2069b62ab8
1 /* sleep - delay for a specified amount of time.
2 Copyright (C) 1984, 1991-1997, 1999-2005, 2007-2010 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 fprintf (stderr, _("Try `%s --help' for more information.\n"),
43 program_name);
44 else
46 printf (_("\
47 Usage: %s NUMBER[SUFFIX]...\n\
48 or: %s OPTION\n\
49 Pause for NUMBER seconds. SUFFIX may be `s' for seconds (the default),\n\
50 `m' for minutes, `h' for hours or `d' for days. Unlike most implementations\n\
51 that require NUMBER be an integer, here NUMBER may be an arbitrary floating\n\
52 point number. Given two or more arguments, pause for the amount of time\n\
53 specified by the sum of their values.\n\
54 \n\
55 "),
56 program_name, program_name);
57 fputs (HELP_OPTION_DESCRIPTION, stdout);
58 fputs (VERSION_OPTION_DESCRIPTION, stdout);
59 emit_ancillary_info ();
61 exit (status);
64 /* Given a floating point value *X, and a suffix character, SUFFIX_CHAR,
65 scale *X by the multiplier implied by SUFFIX_CHAR. SUFFIX_CHAR may
66 be the NUL byte or `s' to denote seconds, `m' for minutes, `h' for
67 hours, or `d' for days. If SUFFIX_CHAR is invalid, don't modify *X
68 and return false. Otherwise return true. */
70 static bool
71 apply_suffix (double *x, char suffix_char)
73 int multiplier;
75 switch (suffix_char)
77 case 0:
78 case 's':
79 multiplier = 1;
80 break;
81 case 'm':
82 multiplier = 60;
83 break;
84 case 'h':
85 multiplier = 60 * 60;
86 break;
87 case 'd':
88 multiplier = 60 * 60 * 24;
89 break;
90 default:
91 return false;
94 *x *= multiplier;
96 return true;
99 int
100 main (int argc, char **argv)
102 int i;
103 double seconds = 0.0;
104 bool ok = true;
106 initialize_main (&argc, &argv);
107 set_program_name (argv[0]);
108 setlocale (LC_ALL, "");
109 bindtextdomain (PACKAGE, LOCALEDIR);
110 textdomain (PACKAGE);
112 atexit (close_stdout);
114 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
115 usage, AUTHORS, (char const *) NULL);
116 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
117 usage (EXIT_FAILURE);
119 if (argc == 1)
121 error (0, 0, _("missing operand"));
122 usage (EXIT_FAILURE);
125 for (i = optind; i < argc; i++)
127 double s;
128 const char *p;
129 if (! xstrtod (argv[i], &p, &s, c_strtod)
130 /* Nonnegative interval. */
131 || ! (0 <= s)
132 /* No extra chars after the number and an optional s,m,h,d char. */
133 || (*p && *(p+1))
134 /* Check any suffix char and update S based on the suffix. */
135 || ! apply_suffix (&s, *p))
137 error (0, 0, _("invalid time interval %s"), quote (argv[i]));
138 ok = false;
141 seconds += s;
144 if (!ok)
145 usage (EXIT_FAILURE);
147 if (xnanosleep (seconds))
148 error (EXIT_FAILURE, errno, _("cannot read realtime clock"));
150 exit (EXIT_SUCCESS);