1*41fbaed0Stron /* $NetBSD: doze.c,v 1.1.1.1 2009/06/23 10:08:59 tron Exp $ */
2*41fbaed0Stron
3*41fbaed0Stron /*++
4*41fbaed0Stron /* NAME
5*41fbaed0Stron /* doze 3
6*41fbaed0Stron /* SUMMARY
7*41fbaed0Stron /* take a nap
8*41fbaed0Stron /* SYNOPSIS
9*41fbaed0Stron /* #include <iostuff.h>
10*41fbaed0Stron /*
11*41fbaed0Stron /* void doze(microseconds)
12*41fbaed0Stron /* unsigned microseconds;
13*41fbaed0Stron /* DESCRIPTION
14*41fbaed0Stron /* doze() sleeps for the specified number of microseconds. It is
15*41fbaed0Stron /* a simple alternative for systems that lack usleep().
16*41fbaed0Stron /* DIAGNOSTICS
17*41fbaed0Stron /* All errors are fatal.
18*41fbaed0Stron /* LICENSE
19*41fbaed0Stron /* .ad
20*41fbaed0Stron /* .fi
21*41fbaed0Stron /* The Secure Mailer license must be distributed with this software.
22*41fbaed0Stron /* AUTHOR(S)
23*41fbaed0Stron /* Wietse Venema
24*41fbaed0Stron /* IBM T.J. Watson Research
25*41fbaed0Stron /* P.O. Box 704
26*41fbaed0Stron /* Yorktown Heights, NY 10598, USA
27*41fbaed0Stron /*--*/
28*41fbaed0Stron
29*41fbaed0Stron /* System library. */
30*41fbaed0Stron
31*41fbaed0Stron #include "sys_defs.h"
32*41fbaed0Stron #include <sys/time.h>
33*41fbaed0Stron #include <unistd.h>
34*41fbaed0Stron #include <errno.h>
35*41fbaed0Stron #ifdef USE_SYS_SELECT_H
36*41fbaed0Stron #include <sys/select.h>
37*41fbaed0Stron #endif
38*41fbaed0Stron
39*41fbaed0Stron /* Utility library. */
40*41fbaed0Stron
41*41fbaed0Stron #include <msg.h>
42*41fbaed0Stron #include <iostuff.h>
43*41fbaed0Stron
44*41fbaed0Stron /* doze - sleep a while */
45*41fbaed0Stron
doze(unsigned delay)46*41fbaed0Stron void doze(unsigned delay)
47*41fbaed0Stron {
48*41fbaed0Stron struct timeval tv;
49*41fbaed0Stron
50*41fbaed0Stron #define MILLION 1000000
51*41fbaed0Stron
52*41fbaed0Stron tv.tv_sec = delay / MILLION;
53*41fbaed0Stron tv.tv_usec = delay % MILLION;
54*41fbaed0Stron while (select(0, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, &tv) < 0)
55*41fbaed0Stron if (errno != EINTR)
56*41fbaed0Stron msg_fatal("doze: select: %m");
57*41fbaed0Stron }
58*41fbaed0Stron
59*41fbaed0Stron #ifdef TEST
60*41fbaed0Stron
61*41fbaed0Stron #include <stdlib.h>
62*41fbaed0Stron
main(int argc,char ** argv)63*41fbaed0Stron int main(int argc, char **argv)
64*41fbaed0Stron {
65*41fbaed0Stron unsigned delay;
66*41fbaed0Stron
67*41fbaed0Stron if (argc != 2 || (delay = atol(argv[1])) == 0)
68*41fbaed0Stron msg_fatal("usage: %s microseconds", argv[0]);
69*41fbaed0Stron doze(delay);
70*41fbaed0Stron exit(0);
71*41fbaed0Stron }
72*41fbaed0Stron
73*41fbaed0Stron #endif
74