1 /* sleep - delay for a specified amount of time.
2 Copyright (C) 84, 1991-1997, 1999-2003 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include <sys/types.h>
26 #include "long-options.h"
27 #include "xnanosleep.h"
30 /* The official name of this program (e.g., no `g' prefix). */
31 #define PROGRAM_NAME "sleep"
33 #define WRITTEN_BY _("Written by Jim Meyering and Paul Eggert.")
35 /* The name by which this program was run. */
38 static struct option const long_options[] =
47 fprintf (stderr, _("Try `%s --help' for more information.\n"),
52 Usage: %s NUMBER[SUFFIX]...\n\
54 Pause for NUMBER seconds. SUFFIX may be `s' for seconds (the default),\n\
55 `m' for minutes, `h' for hours or `d' for days. Unlike most implementations\n\
56 that require NUMBER be an integer, here NUMBER may be an arbitrary floating\n\
60 program_name, program_name);
61 fputs (HELP_OPTION_DESCRIPTION, stdout);
62 fputs (VERSION_OPTION_DESCRIPTION, stdout);
63 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
68 /* Given a floating point value *X, and a suffix character, SUFFIX_CHAR,
69 scale *X by the multiplier implied by SUFFIX_CHAR. SUFFIX_CHAR may
70 be the NUL byte or `s' to denote seconds, `m' for minutes, `h' for
71 hours, or `d' for days. If SUFFIX_CHAR is invalid, don't modify *X
72 and return nonzero. Otherwise return zero. */
75 apply_suffix (double *x, char suffix_char)
77 unsigned int multiplier;
92 multiplier = 60 * 60 * 24;
107 main (int argc, char **argv)
110 double seconds = 0.0;
114 initialize_main (&argc, &argv);
115 program_name = argv[0];
116 setlocale (LC_ALL, "");
117 bindtextdomain (PACKAGE, LOCALEDIR);
118 textdomain (PACKAGE);
120 atexit (close_stdout);
122 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
125 while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
133 usage (EXIT_FAILURE);
139 error (0, 0, _("too few arguments"));
140 usage (EXIT_FAILURE);
143 for (i = optind; i < argc; i++)
147 if (xstrtod (argv[i], &p, &s)
148 /* Nonnegative interval. */
150 /* No extra chars after the number and an optional s,m,h,d char. */
152 /* Check any suffix char and update S based on the suffix. */
153 || apply_suffix (&s, *p))
155 error (0, 0, _("invalid time interval `%s'"), argv[i]);
163 usage (EXIT_FAILURE);
165 if (xnanosleep (seconds))
166 error (EXIT_FAILURE, errno, _("cannot read realtime clock"));