summaryrefslogtreecommitdiffstats
path: root/examples/loadables/sleep.c
blob: 204601f5282eda3091e278654b71e99ace331623 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * sleep -- sleep for fractions of a second
 *
 * usage: sleep seconds[.fraction]
 *
 * as an extension, we support the GNU time interval format (2m20s)
 */

/*
   Copyright (C) 1999-2021 Free Software Foundation, Inc.

   This file is part of GNU Bash.
   Bash is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Bash is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "config.h"

#include "bashtypes.h"

#if defined (TIME_WITH_SYS_TIME)
#  include <sys/time.h>
#  include <time.h>
#else
#  if defined (HAVE_SYS_TIME_H)
#    include <sys/time.h>
#  else
#    include <time.h>
#  endif
#endif

#if defined (HAVE_UNISTD_H)
#include <unistd.h>
#endif

#include <stdio.h>
#include "chartypes.h"

#include "loadables.h"

#define S_SEC	1
#define S_MIN	(60*S_SEC)
#define S_HOUR	(60*S_MIN)
#define S_DAY	(24*S_HOUR)

static int
parse_gnutimefmt (char *string, long *sp, long *up)
{
	int	c, r;
	char	*s, *ep;
	long	tsec, tusec, accumsec, accumusec, t;
	int	mult;

	tsec = tusec = 0;
	accumsec = accumusec = 0;
	mult = 1;

	for (s = string; s && *s; s++) {
	    	r = uconvert(s, &accumsec, &accumusec, &ep);
		if (r == 0 && *ep == 0)
			return r;
		c = *ep;
		mult = 1;
		switch (c) {
			case '\0':
			case 's':
				mult = S_SEC;
				break;
			case 'm':
				mult = S_MIN;
				break;
			case 'h':
				mult = S_HOUR;
				break;
			case 'd':
				mult = S_DAY;
				break;
			default:
				return 0;
		}

		/* multiply the accumulated value by the multiplier */
		t = accumusec * mult;
		accumsec = accumsec * mult + (t / 1000000);
		accumusec = t % 1000000;

		/* add to running total */
		tsec += accumsec;
		tusec += accumusec;
		if (tusec >= 1000000) {
			tsec++;
			tusec -= 1000000;
		}

		/* reset and continue */
		accumsec = accumusec = 0;
		mult = 1;
		if (c == 0)
			break;
		s = ep;
	}

	if (sp)
		*sp = tsec;
	if (up)
		*up = tusec;

	return 1;
}

int
sleep_builtin (WORD_LIST *list)
{
	long	sec, usec;
	char	*ep;
	int	r, mul;
	time_t	t;

	if (list == 0) {
		builtin_usage();
		return(EX_USAGE);
	}

	/* Skip over `--' */
	if (list->word && ISOPTION (list->word->word, '-'))
		list = list->next;

	if (*list->word->word == '-' || list->next) {
		builtin_usage ();
		return (EX_USAGE);
	}

    	r = uconvert(list->word->word, &sec, &usec, &ep);
	/*
	 * Maybe postprocess conversion failures here based on EP
	 *
	 * A heuristic: if the conversion failed, but the argument appears to
	 * contain a GNU-like interval specifier (e.g. "1m30s"), try to parse
	 * it. If we can't, return the right exit code to tell
	 * execute_builtin to try and execute a disk command instead.
	 */
	if (r == 0 && (strchr ("dhms", *ep) || strpbrk (list->word->word, "dhms")))
		r = parse_gnutimefmt (list->word->word, &sec, &usec);
    		
	if (r) {
		fsleep(sec, usec);
		QUIT;
		return(EXECUTION_SUCCESS);
	}
	builtin_error("%s: bad sleep interval", list->word->word);
	return (EXECUTION_FAILURE);
}

static char *sleep_doc[] = {
	"Suspend execution for specified period.",
	""
	"sleep suspends execution for a minimum of SECONDS[.FRACTION] seconds.",
	"As an extension, sleep accepts GNU-style time intervals (e.g., 2m30s).",
	(char *)NULL
};

struct builtin sleep_struct = {
	"sleep",
	sleep_builtin,
	BUILTIN_ENABLED,
	sleep_doc,
	"sleep seconds[.fraction]",
	0
};