]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/sh/ufuncs.c
fix for SIGINT in sourced script
[thirdparty/bash.git] / lib / sh / ufuncs.c
CommitLineData
3185942a
JA
1/* ufuncs - sleep and alarm functions that understand fractional values */
2
3/* Copyright (C) 2008,2009 Free Software Foundation, Inc.
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include "config.h"
22
23#include "bashtypes.h"
24
25#if defined (TIME_WITH_SYS_TIME)
26# include <sys/time.h>
27# include <time.h>
28#else
29# if defined (HAVE_SYS_TIME_H)
30# include <sys/time.h>
31# else
32# include <time.h>
33# endif
34#endif
35
36#if defined (HAVE_UNISTD_H)
37#include <unistd.h>
38#endif
39
a0c0a00f
CR
40#if defined (HAVE_SELECT)
41# include "posixselect.h"
42#endif
43
3185942a
JA
44/* A version of `alarm' using setitimer if it's available. */
45
46#if defined (HAVE_SETITIMER)
47unsigned int
48falarm(secs, usecs)
49 unsigned int secs, usecs;
50{
51 struct itimerval it, oit;
52
53 it.it_interval.tv_sec = 0;
54 it.it_interval.tv_usec = 0;
55
56 it.it_value.tv_sec = secs;
57 it.it_value.tv_usec = usecs;
58
59 if (setitimer(ITIMER_REAL, &it, &oit) < 0)
60 return (-1); /* XXX will be converted to unsigned */
61
62 /* Backwards compatibility with alarm(3) */
63 if (oit.it_value.tv_usec)
64 oit.it_value.tv_sec++;
65 return (oit.it_value.tv_sec);
66}
67#else
68int
69falarm (secs, usecs)
70 unsigned int secs, usecs;
71{
72 if (secs == 0 && usecs == 0)
73 return (alarm (0));
74
75 if (secs == 0 || usecs >= 500000)
76 {
77 secs++;
78 usecs = 0;
79 }
80 return (alarm (secs));
81}
82#endif /* !HAVE_SETITIMER */
83
84/* A version of sleep using fractional seconds and select. I'd like to use
85 `usleep', but it's already taken */
86
87#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
88int
89fsleep(sec, usec)
90 unsigned int sec, usec;
91{
92 struct timeval tv;
93
94 tv.tv_sec = sec;
95 tv.tv_usec = usec;
96
97 return select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv);
98}
99#else /* !HAVE_TIMEVAL || !HAVE_SELECT */
100int
101fsleep(sec, usec)
102 long sec, usec;
103{
104 if (usec >= 500000) /* round */
105 sec++;
106 return (sleep(sec));
107}
108#endif /* !HAVE_TIMEVAL || !HAVE_SELECT */