all files: make most variables static and const when possible.
[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 #include <stdio.h>
19 #include <sys/types.h>
20 #include "system.h"
21
22 void error ();
23
24 static long argdecode ();
25
26 /* The name by which this program was run. */
27 char *program_name;
28
29 void
30 main (argc, argv)
31      int argc;
32      char **argv;
33 {
34   int i;
35   unsigned seconds = 0;
36
37   program_name = argv[0];
38
39   if (argc == 1)
40     {
41       fprintf (stderr, "Usage: %s number[smhd]...\n", argv[0]);
42       exit (1);
43     }
44
45   for (i = 1; i < argc; i++)
46     seconds += argdecode (argv[i]);
47
48   sleep (seconds);
49
50   exit (0);
51 }
52
53 static long
54 argdecode (s)
55      char *s;
56 {
57   long value;
58   register char *p = s;
59   register char c;
60
61   value = 0;
62   while ((c = *p++) >= '0' && c <= '9')
63     value = value * 10 + c - '0';
64
65   switch (c)
66     {
67     case 's':
68       break;
69     case 'm':
70       value *= 60;
71       break;
72     case 'h':
73       value *= 60 * 60;
74       break;
75     case 'd':
76       value *= 60 * 60 * 24;
77       break;
78     default:
79       p--;
80     }
81
82   if (*p)
83     error (1, 0, "invalid time interval `%s'", s);
84   return value;
85 }