remove OSF1 support
[oweals/cde.git] / cde / programs / dtksh / ksh93 / src / cmd / ksh93 / bltins / sleep.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: sleep.c /main/3 1995/11/01 16:29:36 rswiston $ */
24 /***************************************************************
25 *                                                              *
26 *                      AT&T - PROPRIETARY                      *
27 *                                                              *
28 *        THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF        *
29 *                    AT&T BELL LABORATORIES                    *
30 *         AND IS NOT TO BE DISCLOSED OR USED EXCEPT IN         *
31 *            ACCORDANCE WITH APPLICABLE AGREEMENTS             *
32 *                                                              *
33 *                Copyright (c) 1995 AT&T Corp.                 *
34 *              Unpublished & Not for Publication               *
35 *                     All Rights Reserved                      *
36 *                                                              *
37 *       The copyright notice above does not evidence any       *
38 *      actual or intended publication of such source code      *
39 *                                                              *
40 *               This software was created by the               *
41 *           Advanced Software Technology Department            *
42 *                    AT&T Bell Laboratories                    *
43 *                                                              *
44 *               For further information contact                *
45 *                    {research,attmail}!dgk                    *
46 *                                                              *
47 ***************************************************************/
48
49 /* : : generated by proto : : */
50
51 #if !defined(__PROTO__)
52 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
53 #if defined(__cplusplus)
54 #define __MANGLE__      "C"
55 #else
56 #define __MANGLE__
57 #endif
58 #define __STDARG__
59 #define __PROTO__(x)    x
60 #define __OTORP__(x)
61 #define __PARAM__(n,o)  n
62 #if !defined(__STDC__) && !defined(__cplusplus)
63 #if !defined(c_plusplus)
64 #define const
65 #endif
66 #define signed
67 #define void            int
68 #define volatile
69 #define __V_            char
70 #else
71 #define __V_            void
72 #endif
73 #else
74 #define __PROTO__(x)    ()
75 #define __OTORP__(x)    x
76 #define __PARAM__(n,o)  o
77 #define __MANGLE__
78 #define __V_            char
79 #define const
80 #define signed
81 #define void            int
82 #define volatile
83 #endif
84 #if defined(__cplusplus) || defined(c_plusplus)
85 #define __VARARG__      ...
86 #else
87 #define __VARARG__
88 #endif
89 #if defined(__STDARG__)
90 #define __VA_START__(p,a)       va_start(p,a)
91 #else
92 #define __VA_START__(p,a)       va_start(p)
93 #endif
94 #endif
95 #include        "defs.h"
96 #include        <error.h>
97 #include        <errno.h>
98 #include        "builtins.h"
99 #include        "FEATURE/time"
100 #include        "FEATURE/poll"
101 #ifdef _NEXT_SOURCE
102 #   define sleep        _ast_sleep
103 #endif /* _NEXT_SOURCE */
104 #ifdef _lib_poll_notimer
105 #   undef _lib_poll
106 #endif /* _lib_poll_notimer */
107
108 int     b_sleep __PARAM__((register int argc,char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(register int argc;char *argv[];__V_ *extra;){
109         register char *cp;
110         register double d;
111         time_t tloc = 0;
112         NOT_USED(extra);
113         while((argc = optget(argv,sh_optsleep))) switch(argc)
114         {
115                 case ':':
116                         error(2, opt_arg);
117                         break;
118                 case '?':
119                         error(ERROR_usage(2), opt_arg);
120                         break;
121         }
122         argv += opt_index;
123         if(error_info.errors || !(cp= *argv) || !(strmatch(cp,e_numeric)))
124                 error(ERROR_usage(2),optusage((char*)0));
125         if((d=atof(cp)) > 1.0)
126         {
127                 sfsync(sh.outpool);
128                 time(&tloc);
129                 tloc += (time_t)(d+.5);
130         }
131         while(1)
132         {
133                 time_t now;
134                 errno = 0;
135                 sh.lastsig=0;
136                 sh_delay(d);
137                 if(tloc==0 || errno!=EINTR || sh.lastsig)
138                         break;
139                 sh_sigcheck();
140                 if(tloc < (now=time(NIL(time_t*))))
141                         break;
142                 d = (double)(tloc-now);
143                 if(sh.sigflag[SIGALRM]&SH_SIGTRAP)
144                         sh_timetraps();
145         }
146         return(0);
147 }
148
149 static char expired;
150 static void completed __PARAM__((__V_ * handle), (handle)) __OTORP__(__V_ * handle;){
151         NOT_USED(handle);
152         expired = 1;
153 }
154
155 unsigned sleep __PARAM__((unsigned sec), (sec)) __OTORP__(unsigned sec;){
156         pid_t newpid, curpid=getpid();
157         __V_ *tp;
158         expired = 0;
159         sh.lastsig = 0;
160         tp = (__V_*)kshtimeradd(1000*sec, 0, completed, (__V_*)0);
161         do
162         {
163                 pause();
164                 if(sh.sigflag[SIGALRM]&SH_SIGTRAP)
165                         sh_timetraps();
166                 if((newpid=getpid()) != curpid)
167                 {
168                         curpid = newpid;
169                         alarm(1);
170                 }
171         }
172         while(!expired && sh.lastsig==0);
173         if(!expired)
174                 timerdel(tp);
175         sh_sigcheck();
176         return(0);
177 }
178
179 /*
180  * delay execution for time <t>
181  */
182
183 void    sh_delay __PARAM__((double t), (t)) __OTORP__(double t;){
184         register int n = (int)t;
185 #ifdef _lib_poll
186         struct pollfd fd;
187         if(t<=0)
188                 return;
189         else if(n > 30)
190         {
191                 sleep(n);
192                 t -= n;
193         }
194         if(n=(int)(1000*t))
195                 poll(&fd,0,n);
196 #else
197 #   if defined(_lib_select) && defined(_mem_tv_usec_timeval)
198         struct timeval timeloc;
199         if(t<=0)
200                 return;
201         timeloc.tv_sec = n;
202         timeloc.tv_usec = 1000000*(t-(double)n);
203         select(0,(fd_set*)0,(fd_set*)0,(fd_set*)0,&timeloc);
204 #   else
205 #       ifdef _lib_select
206                 /* for 9th edition machines */
207                 if(t<=0)
208                         return;
209                 if(n > 30)
210                 {
211                         sleep(n);
212                         t -= n;
213                 }
214                 if(n=(int)(1000*t))
215                         select(0,(fd_set*)0,(fd_set*)0,n);
216 #       else
217                 struct tms tt;
218                 if(t<=0)
219                         return;
220                 sleep(n);
221                 t -= n;
222                 if(t)
223                 {
224                         clock_t begin = times(&tt);
225                         if(begin==0)
226                                 return;
227                         t *= sh.lim.clk_tck;
228                         n += (t+.5);
229                         while((times(&tt)-begin) < n);
230                 }
231 #       endif
232 #   endif
233 #endif /* _lib_poll */
234 }