a98ce9da903de2a2ac655733258b8e29f7d868dc
[platform/upstream/coreutils.git] / src / sleep.c
1 /* sleep - delay for a specified amount of time.
2    Copyright (C) 1984, 1991 Free Software Foundation, Inc.
3
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.
8
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.
13
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
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 #ifdef HAVE_CONFIG_H
19 #if defined (CONFIG_BROKETS)
20 /* We use <config.h> instead of "config.h" so that a compilation
21    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
22    (which it would do because it found this file in $srcdir).  */
23 #include <config.h>
24 #else
25 #include "config.h"
26 #endif
27 #endif
28
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <getopt.h>
32
33 #include "version.h"
34 #include "system.h"
35
36 void error ();
37
38 static long argdecode ();
39
40 /* The name by which this program was run. */
41 char *program_name;
42
43 /* If non-zero, display usage information and exit.  */
44 static int show_help;
45
46 /* If non-zero, print the version on standard output and exit.  */
47 static int show_version;
48
49 static struct option const long_options[] =
50 {
51   {"help", no_argument, &show_help, 1},
52   {"version", no_argument, &show_version, 1},
53   {0, 0, 0, 0}
54 };
55
56 static void
57 usage (status)
58      int status;
59 {
60   fprintf (status == 0 ? stdout : stderr, "\
61 Usage: %s [OPTION]... NUMBER[SUFFIX]\n\
62 ",
63            program_name);
64
65   if (status != 0)
66     fprintf (stderr, "Try `%s --help' for more information.\n",
67              program_name);
68   else
69
70     printf ("\
71 \n\
72   --help      display this help and exit\n\
73   --version   output version information and exit\n\
74 \n\
75 SUFFIX may be s for seconds, m for minutes, h for hours or d for days.\n\
76 ");
77
78   exit (status);
79 }
80
81 void
82 main (argc, argv)
83      int argc;
84      char **argv;
85 {
86   int i;
87   unsigned seconds = 0;
88   int c;
89
90   program_name = argv[0];
91
92   while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
93     {
94       switch (c)
95         {
96         case 0:
97           break;
98
99         default:
100           usage (1);
101         }
102     }
103
104   if (show_version)
105     {
106       printf ("%s\n", version_string);
107       exit (0);
108     }
109
110   if (show_help)
111     usage (0);
112
113   if (argc == 1)
114     {
115       fprintf (stderr, "Usage: %s number[smhd]...\n", argv[0]);
116       exit (1);
117     }
118
119   for (i = 1; i < argc; i++)
120     seconds += argdecode (argv[i]);
121
122   sleep (seconds);
123
124   exit (0);
125 }
126
127 static long
128 argdecode (s)
129      char *s;
130 {
131   long value;
132   register char *p = s;
133   register char c;
134
135   value = 0;
136   while ((c = *p++) >= '0' && c <= '9')
137     value = value * 10 + c - '0';
138
139   switch (c)
140     {
141     case 's':
142       break;
143     case 'm':
144       value *= 60;
145       break;
146     case 'h':
147       value *= 60 * 60;
148       break;
149     case 'd':
150       value *= 60 * 60 * 24;
151       break;
152     default:
153       p--;
154     }
155
156   if (*p)
157     error (1, 0, "invalid time interval `%s'", s);
158   return value;
159 }