*** empty log message ***
[coreutils.git] / src / sleep.c
bloba55f301bd54a11a500f1af00b5680e74a5eb5647
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 (_("Usage: %s [OPTION]... NUMBER[SUFFIX]\n"), program_name);
51 printf (_("\
52 Pause for NUMBER seconds.\n\
53 SUFFIX may be s to keep seconds, m for minutes, h for hours or d for days.\n\
54 \n\
55 --help display this help and exit\n\
56 --version output version information and exit\n"));
57 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
59 exit (status);
62 int
63 main (int argc, char **argv)
65 int i;
66 unsigned seconds = 0;
67 int c;
69 program_name = argv[0];
70 setlocale (LC_ALL, "");
71 bindtextdomain (PACKAGE, LOCALEDIR);
72 textdomain (PACKAGE);
74 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
75 AUTHORS, usage);
77 while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
79 switch (c)
81 case 0:
82 break;
84 default:
85 usage (1);
89 if (argc == 1)
91 error (0, 0, _("too few arguments"));
92 usage (1);
95 for (i = 1; i < argc; i++)
96 seconds += argdecode (argv[i]);
98 sleep (seconds);
100 exit (0);
103 static long
104 argdecode (const char *s)
106 long value;
107 register const char *p = s;
108 register char c;
110 value = 0;
111 while ((c = *p++) >= '0' && c <= '9')
112 value = value * 10 + c - '0';
114 switch (c)
116 case 's':
117 break;
118 case 'm':
119 value *= 60;
120 break;
121 case 'h':
122 value *= 60 * 60;
123 break;
124 case 'd':
125 value *= 60 * 60 * 24;
126 break;
127 default:
128 p--;
131 if (*p)
132 error (1, 0, _("invalid time interval `%s'"), s);
133 return value;