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