1992-11-01 05:44:29 +00:00
|
|
|
/* sleep - delay for a specified amount of time.
|
2005-06-16 21:35:40 +00:00
|
|
|
Copyright (C) 84, 1991-1997, 1999-2005 Free Software Foundation, Inc.
|
1992-11-01 05:44:29 +00:00
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
1996-03-24 18:33:12 +00:00
|
|
|
along with this program; if not, write to the Free Software Foundation,
|
2005-05-14 07:58:31 +00:00
|
|
|
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
1992-11-01 05:44:29 +00:00
|
|
|
|
1993-10-12 14:49:11 +00:00
|
|
|
#include <config.h>
|
1992-11-01 05:44:29 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <sys/types.h>
|
1993-10-12 01:52:24 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
|
|
1992-11-01 05:44:29 +00:00
|
|
|
#include "system.h"
|
2003-11-27 08:16:38 +00:00
|
|
|
#include "c-strtod.h"
|
1994-12-20 05:26:44 +00:00
|
|
|
#include "error.h"
|
1999-03-06 15:28:54 +00:00
|
|
|
#include "long-options.h"
|
2005-06-16 21:35:40 +00:00
|
|
|
#include "quote.h"
|
2002-09-12 12:01:02 +00:00
|
|
|
#include "xnanosleep.h"
|
1999-11-27 20:00:54 +00:00
|
|
|
#include "xstrtod.h"
|
2000-01-05 12:17:19 +00:00
|
|
|
|
1999-03-31 04:16:08 +00:00
|
|
|
/* The official name of this program (e.g., no `g' prefix). */
|
|
|
|
|
#define PROGRAM_NAME "sleep"
|
|
|
|
|
|
2003-10-18 10:05:47 +00:00
|
|
|
#define AUTHORS "Jim Meyering", "Paul Eggert"
|
1992-11-12 04:14:54 +00:00
|
|
|
|
1992-11-01 05:44:29 +00:00
|
|
|
/* The name by which this program was run. */
|
|
|
|
|
char *program_name;
|
|
|
|
|
|
1999-01-25 14:33:38 +00:00
|
|
|
void
|
1996-01-06 11:44:05 +00:00
|
|
|
usage (int status)
|
1993-10-12 01:52:24 +00:00
|
|
|
{
|
2004-01-21 23:44:47 +00:00
|
|
|
if (status != EXIT_SUCCESS)
|
1995-08-08 04:37:34 +00:00
|
|
|
fprintf (stderr, _("Try `%s --help' for more information.\n"),
|
1993-10-17 03:57:04 +00:00
|
|
|
program_name);
|
|
|
|
|
else
|
1993-10-26 00:11:14 +00:00
|
|
|
{
|
1995-08-08 04:37:34 +00:00
|
|
|
printf (_("\
|
1999-11-27 17:10:15 +00:00
|
|
|
Usage: %s NUMBER[SUFFIX]...\n\
|
1999-08-30 14:38:00 +00:00
|
|
|
or: %s OPTION\n\
|
2000-01-02 09:31:15 +00:00
|
|
|
Pause for NUMBER seconds. SUFFIX may be `s' for seconds (the default),\n\
|
|
|
|
|
`m' for minutes, `h' for hours or `d' for days. Unlike most implementations\n\
|
|
|
|
|
that require NUMBER be an integer, here NUMBER may be an arbitrary floating\n\
|
2005-12-04 09:58:34 +00:00
|
|
|
point number. Given two or more arguments, pause for the amount of time\n\
|
|
|
|
|
specified by the sum of their values.\n\
|
1993-10-17 03:57:04 +00:00
|
|
|
\n\
|
1999-08-30 14:38:00 +00:00
|
|
|
"),
|
|
|
|
|
program_name, program_name);
|
2001-12-15 20:46:30 +00:00
|
|
|
fputs (HELP_OPTION_DESCRIPTION, stdout);
|
|
|
|
|
fputs (VERSION_OPTION_DESCRIPTION, stdout);
|
2002-07-02 09:09:10 +00:00
|
|
|
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
|
1993-10-26 00:11:14 +00:00
|
|
|
}
|
1993-10-17 03:57:04 +00:00
|
|
|
exit (status);
|
1993-10-12 01:52:24 +00:00
|
|
|
}
|
2000-01-06 08:35:45 +00:00
|
|
|
|
2000-01-06 08:35:21 +00:00
|
|
|
/* Given a floating point value *X, and a suffix character, SUFFIX_CHAR,
|
|
|
|
|
scale *X by the multiplier implied by SUFFIX_CHAR. SUFFIX_CHAR may
|
|
|
|
|
be the NUL byte or `s' to denote seconds, `m' for minutes, `h' for
|
|
|
|
|
hours, or `d' for days. If SUFFIX_CHAR is invalid, don't modify *X
|
2004-08-02 22:19:27 +00:00
|
|
|
and return false. Otherwise return true. */
|
1999-11-27 20:00:54 +00:00
|
|
|
|
2004-08-02 22:19:27 +00:00
|
|
|
static bool
|
2000-01-06 08:35:21 +00:00
|
|
|
apply_suffix (double *x, char suffix_char)
|
1999-11-27 20:00:54 +00:00
|
|
|
{
|
2004-08-02 22:19:27 +00:00
|
|
|
int multiplier;
|
1999-11-27 20:00:54 +00:00
|
|
|
|
|
|
|
|
switch (suffix_char)
|
1999-11-27 15:02:27 +00:00
|
|
|
{
|
1999-11-27 20:00:54 +00:00
|
|
|
case 0:
|
1999-11-27 15:02:27 +00:00
|
|
|
case 's':
|
1999-11-27 20:00:54 +00:00
|
|
|
multiplier = 1;
|
1999-11-27 15:02:27 +00:00
|
|
|
break;
|
|
|
|
|
case 'm':
|
1999-11-27 20:00:54 +00:00
|
|
|
multiplier = 60;
|
1999-11-27 15:02:27 +00:00
|
|
|
break;
|
|
|
|
|
case 'h':
|
1999-11-27 20:00:54 +00:00
|
|
|
multiplier = 60 * 60;
|
1999-11-27 15:02:27 +00:00
|
|
|
break;
|
|
|
|
|
case 'd':
|
1999-11-27 20:00:54 +00:00
|
|
|
multiplier = 60 * 60 * 24;
|
1999-11-27 15:02:27 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
2004-08-02 22:19:27 +00:00
|
|
|
return false;
|
1999-11-27 15:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
2000-01-06 08:35:21 +00:00
|
|
|
*x *= multiplier;
|
1999-11-27 20:00:54 +00:00
|
|
|
|
2004-08-02 22:19:27 +00:00
|
|
|
return true;
|
1999-11-27 20:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
1996-03-21 22:47:02 +00:00
|
|
|
int
|
1996-01-06 11:44:05 +00:00
|
|
|
main (int argc, char **argv)
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
|
|
|
|
int i;
|
1999-11-27 20:00:54 +00:00
|
|
|
double seconds = 0.0;
|
2004-08-02 22:19:27 +00:00
|
|
|
bool ok = true;
|
1992-11-01 05:44:29 +00:00
|
|
|
|
2003-06-17 18:13:23 +00:00
|
|
|
initialize_main (&argc, &argv);
|
1992-11-01 05:44:29 +00:00
|
|
|
program_name = argv[0];
|
1996-03-12 23:49:29 +00:00
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
|
textdomain (PACKAGE);
|
1992-11-01 05:44:29 +00:00
|
|
|
|
2000-05-11 07:21:33 +00:00
|
|
|
atexit (close_stdout);
|
|
|
|
|
|
1999-03-31 04:16:08 +00:00
|
|
|
parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
|
2003-11-05 03:53:19 +00:00
|
|
|
usage, AUTHORS, (char const *) NULL);
|
2004-11-17 00:56:25 +00:00
|
|
|
if (getopt_long (argc, argv, "", NULL, NULL) != -1)
|
2004-09-21 22:09:09 +00:00
|
|
|
usage (EXIT_FAILURE);
|
1993-10-12 01:52:24 +00:00
|
|
|
|
1992-11-01 05:44:29 +00:00
|
|
|
if (argc == 1)
|
|
|
|
|
{
|
2004-06-21 15:03:35 +00:00
|
|
|
error (0, 0, _("missing operand"));
|
2002-08-31 08:52:10 +00:00
|
|
|
usage (EXIT_FAILURE);
|
1992-11-01 05:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
1999-11-27 20:00:54 +00:00
|
|
|
for (i = optind; i < argc; i++)
|
|
|
|
|
{
|
|
|
|
|
double s;
|
|
|
|
|
const char *p;
|
2004-08-02 22:19:27 +00:00
|
|
|
if (! xstrtod (argv[i], &p, &s, c_strtod)
|
2000-01-05 12:17:19 +00:00
|
|
|
/* Nonnegative interval. */
|
|
|
|
|
|| ! (0 <= s)
|
1999-12-19 15:21:19 +00:00
|
|
|
/* No extra chars after the number and an optional s,m,h,d char. */
|
1999-11-28 08:00:58 +00:00
|
|
|
|| (*p && *(p+1))
|
1999-11-28 19:59:18 +00:00
|
|
|
/* Check any suffix char and update S based on the suffix. */
|
2004-08-02 22:19:27 +00:00
|
|
|
|| ! apply_suffix (&s, *p))
|
1999-11-27 20:00:54 +00:00
|
|
|
{
|
2005-06-16 21:35:40 +00:00
|
|
|
error (0, 0, _("invalid time interval %s"), quote (argv[i]));
|
2004-08-02 22:19:27 +00:00
|
|
|
ok = false;
|
1999-11-27 20:00:54 +00:00
|
|
|
}
|
2000-01-05 12:17:19 +00:00
|
|
|
|
|
|
|
|
seconds += s;
|
1999-11-27 20:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
2004-08-02 22:19:27 +00:00
|
|
|
if (!ok)
|
2002-08-31 08:52:10 +00:00
|
|
|
usage (EXIT_FAILURE);
|
1999-11-27 20:00:54 +00:00
|
|
|
|
2002-09-12 12:01:02 +00:00
|
|
|
if (xnanosleep (seconds))
|
|
|
|
|
error (EXIT_FAILURE, errno, _("cannot read realtime clock"));
|
1992-11-01 05:44:29 +00:00
|
|
|
|
2002-08-31 08:52:10 +00:00
|
|
|
exit (EXIT_SUCCESS);
|
1992-11-01 05:44:29 +00:00
|
|
|
}
|