blob: 736c8af1a7757629bbbf8b24406c553e378e8bf9 [file] [log] [blame]
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001/*
2 * sleep -- sleep for fractions of a second
3 *
4 * usage: sleep seconds[.fraction]
5 */
Jari Aalto31859422009-01-12 13:36:28 +00006
7/*
8 Copyright (C) 1999-2009 Free Software Foundation, Inc.
9
10 This file is part of GNU Bash.
11 Bash is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 Bash is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23*/
24
Jari Aaltoccc6cda1996-12-23 17:02:34 +000025#include "config.h"
26
27#include "bashtypes.h"
28
29#if defined (TIME_WITH_SYS_TIME)
30# include <sys/time.h>
31# include <time.h>
32#else
33# if defined (HAVE_SYS_TIME_H)
34# include <sys/time.h>
35# else
36# include <time.h>
37# endif
38#endif
39
40#if defined (HAVE_UNISTD_H)
41#include <unistd.h>
42#endif
43
44#include <stdio.h>
Jari Aaltof73dda02001-11-13 17:56:06 +000045#include "chartypes.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000046
47#include "shell.h"
48#include "builtins.h"
Jari Aalto31859422009-01-12 13:36:28 +000049#include "common.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000050
51#define RETURN(x) \
52 do { \
53 if (sp) *sp = sec; \
54 if (usp) *usp = usec; \
55 return (x); \
56 } while (0)
57
Jari Aaltoccc6cda1996-12-23 17:02:34 +000058int
59sleep_builtin (list)
60WORD_LIST *list;
61{
62 long sec, usec;
63
64 if (list == 0) {
65 builtin_usage();
66 return(EX_USAGE);
67 }
68
Chet Rameyac50fba2014-02-26 09:36:43 -050069 /* Skip over `--' */
70 if (list->word && ISOPTION (list->word->word, '-'))
71 list = list->next;
72
Jari Aaltoccc6cda1996-12-23 17:02:34 +000073 if (*list->word->word == '-' || list->next) {
74 builtin_usage ();
75 return (EX_USAGE);
76 }
77
Jari Aalto31859422009-01-12 13:36:28 +000078 if (uconvert(list->word->word, &sec, &usec)) {
Jari Aaltoccc6cda1996-12-23 17:02:34 +000079 fsleep(sec, usec);
80 return(EXECUTION_SUCCESS);
81 }
82
83 builtin_error("%s: bad sleep interval", list->word->word);
84 return (EXECUTION_FAILURE);
85}
86
87static char *sleep_doc[] = {
Jari Aalto31859422009-01-12 13:36:28 +000088 "Suspend execution for specified period.",
89 ""
Jari Aaltoccc6cda1996-12-23 17:02:34 +000090 "sleep suspends execution for a minimum of SECONDS[.FRACTION] seconds.",
91 (char *)NULL
92};
93
94struct builtin sleep_struct = {
95 "sleep",
96 sleep_builtin,
97 BUILTIN_ENABLED,
98 sleep_doc,
99 "sleep seconds[.fraction]",
100 0
101};