(recheck): Handle a race condition (including <dev,inode>
[coreutils.git] / src / sleep.c
blob433bc94014d29bfecb7eff1becd754383ea2662d
1 /* sleep - delay for a specified amount of time.
2 Copyright (C) 84, 1991-1997, 1999 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)
7 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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #include <config.h>
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <getopt.h>
23 #include "system.h"
24 #include "error.h"
25 #include "long-options.h"
27 /* The official name of this program (e.g., no `g' prefix). */
28 #define PROGRAM_NAME "sleep"
30 #define AUTHORS "FIXME: unknown"
32 static long argdecode PARAMS ((const char *s));
34 /* The name by which this program was run. */
35 char *program_name;
37 static struct option const long_options[] =
39 {0, 0, 0, 0}
42 void
43 usage (int status)
45 if (status != 0)
46 fprintf (stderr, _("Try `%s --help' for more information.\n"),
47 program_name);
48 else
50 printf (_("\
51 Usage: %s NUMBER[SUFFIX]\n\
52 or: %s OPTION\n\
53 Pause for NUMBER seconds.\n\
54 SUFFIX may be s for seconds (the default), m for minutes,\n\
55 h for hours or d for days.\n\
56 \n\
57 --help display this help and exit\n\
58 --version output version information and exit\n\
59 "),
60 program_name, program_name);
61 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
63 exit (status);
66 int
67 main (int argc, char **argv)
69 int i;
70 unsigned seconds = 0;
71 int c;
73 program_name = argv[0];
74 setlocale (LC_ALL, "");
75 bindtextdomain (PACKAGE, LOCALEDIR);
76 textdomain (PACKAGE);
78 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
79 AUTHORS, usage);
81 while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
83 switch (c)
85 case 0:
86 break;
88 default:
89 usage (1);
93 if (argc == 1)
95 error (0, 0, _("too few arguments"));
96 usage (1);
99 for (i = 1; i < argc; i++)
100 seconds += argdecode (argv[i]);
102 sleep (seconds);
104 exit (0);
107 static long
108 argdecode (const char *s)
110 long value;
111 register const char *p = s;
112 register char c;
114 value = 0;
115 while ((c = *p++) >= '0' && c <= '9')
116 value = value * 10 + c - '0';
118 switch (c)
120 case 's':
121 break;
122 case 'm':
123 value *= 60;
124 break;
125 case 'h':
126 value *= 60 * 60;
127 break;
128 case 'd':
129 value *= 60 * 60 * 24;
130 break;
131 default:
132 p--;
135 if (*p)
136 error (1, 0, _("invalid time interval `%s'"), s);
137 return value;