1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1982-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * David Korn <dgk@research.att.com> *
19 ***********************************************************************/
29 #define sleep ______sleep
36 #include "FEATURE/time"
37 #include "FEATURE/poll"
39 # define sleep _ast_sleep
40 #endif /* _NEXT_SOURCE */
41 #ifdef _lib_poll_notimer
43 #endif /* _lib_poll_notimer */
45 int b_sleep(register int argc
,char *argv
[],void *extra
)
49 register Shell_t
*shp
= ((Shbltin_t
*)extra
)->shp
;
53 if(!(shp
->sigflag
[SIGALRM
]&(SH_SIGFAULT
|SH_SIGOFF
)))
55 while((argc
= optget(argv
,sh_optsleep
))) switch(argc
)
61 errormsg(SH_DICT
,2, "%s", opt_info
.arg
);
64 errormsg(SH_DICT
,ERROR_usage(2), "%s", opt_info
.arg
);
67 argv
+= opt_info
.index
;
70 d
= strtod(cp
, &last
);
76 if(*cp
== 'P' || *cp
== 'p')
77 ns
= tmxdate(cp
, &last
, now
);
80 if(pp
= sfprints("exact %s", cp
))
81 ns
= tmxdate(pp
, &last
, now
);
82 if(*last
&& (pp
= sfprints("p%s", cp
)))
83 ns
= tmxdate(pp
, &last
, now
);
86 errormsg(SH_DICT
,ERROR_exit(1),e_number
,*argv
);
91 errormsg(SH_DICT
,ERROR_exit(1),e_oneoperand
);
94 errormsg(SH_DICT
,ERROR_exit(1),e_oneoperand
);
98 tloc
+= (time_t)(d
+.5);
108 if(sflag
|| tloc
==0 || errno
!=EINTR
|| shp
->lastsig
)
111 if(tloc
< (now
=time(NIL(time_t*))))
113 d
= (double)(tloc
-now
);
114 if(shp
->sigflag
[SIGALRM
]&SH_SIGTRAP
)
120 static void completed(void * handle
)
122 char *expired
= (char*)handle
;
126 unsigned int sleep(unsigned int sec
)
129 pid_t newpid
, curpid
=getpid();
133 tp
= (void*)sh_timeradd(1000*sec
, 0, completed
, (void*)&expired
);
136 if(!shp
->waitevent
|| (*shp
->waitevent
)(-1,-1L,0)==0)
138 if(shp
->sigflag
[SIGALRM
]&SH_SIGTRAP
)
140 if((newpid
=getpid()) != curpid
)
144 shp
->trapnote
&= ~SH_SIGSET
;
149 tp
= (void*)sh_timeradd(1000*sec
, 0, completed
, (void*)&expired
);
152 while(!expired
&& shp
->lastsig
==0);
160 * delay execution for time <t>
163 void sh_delay(double t
)
165 register int n
= (int)t
;
178 if(!shp
->waitevent
|| (*shp
->waitevent
)(-1,(long)n
,0)==0)
182 # if defined(_lib_select) && defined(_mem_tv_usec_timeval)
183 struct timeval timeloc
;
186 if(n
=(int)(1000*t
) && shp
->waitevent
&& (*shp
->waitevent
)(-1,(long)n
,0))
190 timeloc
.tv_usec
= 1000000*(t
-(double)n
);
191 select(0,(fd_set
*)0,(fd_set
*)0,(fd_set
*)0,&timeloc
);
194 /* for 9th edition machines */
204 if(!shp
->waitevent
|| (*shp
->waitevent
)(-1,(long)n
,0)==0)
205 select(0,(fd_set
*)0,(fd_set
*)0,n
);
215 clock_t begin
= times(&tt
);
218 t
*= shp
->lim
.clk_tck
;
220 while((times(&tt
)-begin
) < n
);
224 #endif /* _lib_poll */