Tizen 2.0 Release
[external/tizen-coreutils.git] / src / sleep.c
1 /* sleep - delay for a specified amount of time.
2    Copyright (C) 84, 1991-1997, 1999-2005 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 Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 #include <config.h>
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <getopt.h>
22
23 #include "system.h"
24 #include "c-strtod.h"
25 #include "error.h"
26 #include "long-options.h"
27 #include "quote.h"
28 #include "xnanosleep.h"
29 #include "xstrtod.h"
30
31 /* The official name of this program (e.g., no `g' prefix).  */
32 #define PROGRAM_NAME "sleep"
33
34 #define AUTHORS "Jim Meyering", "Paul Eggert"
35
36 /* The name by which this program was run. */
37 char *program_name;
38
39 void
40 usage (int status)
41 {
42   if (status != EXIT_SUCCESS)
43     fprintf (stderr, _("Try `%s --help' for more information.\n"),
44              program_name);
45   else
46     {
47       printf (_("\
48 Usage: %s NUMBER[SUFFIX]...\n\
49   or:  %s OPTION\n\
50 Pause for NUMBER seconds.  SUFFIX may be `s' for seconds (the default),\n\
51 `m' for minutes, `h' for hours or `d' for days.  Unlike most implementations\n\
52 that require NUMBER be an integer, here NUMBER may be an arbitrary floating\n\
53 point number.  Given two or more arguments, pause for the amount of time\n\
54 specified by the sum of their values.\n\
55 \n\
56 "),
57               program_name, program_name);
58       fputs (HELP_OPTION_DESCRIPTION, stdout);
59       fputs (VERSION_OPTION_DESCRIPTION, stdout);
60       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
61     }
62   exit (status);
63 }
64
65 /* Given a floating point value *X, and a suffix character, SUFFIX_CHAR,
66    scale *X by the multiplier implied by SUFFIX_CHAR.  SUFFIX_CHAR may
67    be the NUL byte or `s' to denote seconds, `m' for minutes, `h' for
68    hours, or `d' for days.  If SUFFIX_CHAR is invalid, don't modify *X
69    and return false.  Otherwise return true.  */
70
71 static bool
72 apply_suffix (double *x, char suffix_char)
73 {
74   int multiplier;
75
76   switch (suffix_char)
77     {
78     case 0:
79     case 's':
80       multiplier = 1;
81       break;
82     case 'm':
83       multiplier = 60;
84       break;
85     case 'h':
86       multiplier = 60 * 60;
87       break;
88     case 'd':
89       multiplier = 60 * 60 * 24;
90       break;
91     default:
92       return false;
93     }
94
95   *x *= multiplier;
96
97   return true;
98 }
99
100 int
101 main (int argc, char **argv)
102 {
103   int i;
104   double seconds = 0.0;
105   bool ok = true;
106
107   initialize_main (&argc, &argv);
108   program_name = argv[0];
109   setlocale (LC_ALL, "");
110   bindtextdomain (PACKAGE, LOCALEDIR);
111   textdomain (PACKAGE);
112
113   atexit (close_stdout);
114
115   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
116                       usage, AUTHORS, (char const *) NULL);
117   if (getopt_long (argc, argv, "", NULL, NULL) != -1)
118     usage (EXIT_FAILURE);
119
120   if (argc == 1)
121     {
122       error (0, 0, _("missing operand"));
123       usage (EXIT_FAILURE);
124     }
125
126   for (i = optind; i < argc; i++)
127     {
128       double s;
129       const char *p;
130       if (! xstrtod (argv[i], &p, &s, c_strtod)
131           /* Nonnegative interval.  */
132           || ! (0 <= s)
133           /* No extra chars after the number and an optional s,m,h,d char.  */
134           || (*p && *(p+1))
135           /* Check any suffix char and update S based on the suffix.  */
136           || ! apply_suffix (&s, *p))
137         {
138           error (0, 0, _("invalid time interval %s"), quote (argv[i]));
139           ok = false;
140         }
141
142       seconds += s;
143     }
144
145   if (!ok)
146     usage (EXIT_FAILURE);
147
148   if (xnanosleep (seconds))
149     error (EXIT_FAILURE, errno, _("cannot read realtime clock"));
150
151   exit (EXIT_SUCCESS);
152 }