-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleep_msecs.c
48 lines (39 loc) · 1.17 KB
/
sleep_msecs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* $Id: //devel/tools/main/nbtscan/sleep_msecs.c#1 $
*
* Sleep for the given number of milliseconds in as portable
* manner as possible. This is really an important function,
* because when probing a remote network, blasting out a full
* class C address of UDP packets virtually guarantees packet
* loss.
*
* If you really can't figure out a way to do this, at least add
* the real UNIX sleep(1) function. This makes for very slow
* scanning, but at least it will work.
*
* My goal is to eliminate the use of this function entirely by
* doing proper select I/O with timeouts, but some of the tools
* have sleeps for legacy purposes. Sorry.
*
* Please find a way to send back portability changes to me!
* -- [email protected]
*/
#ifndef COMMONFILE
# define COMMONFILE "libcommon.h"
#endif
#include COMMONFILE
#include "penlib.h"
void __stdcall sleep_msecs(long msecs)
{
if (msecs <= 0) return;
#ifdef _WIN32
Sleep((unsigned long)msecs);
#elif defined(M_XENIX)
napms(msecs);
#else
struct timespec ts;
ts.tv_sec = msecs / 1000000ul; // whole seconds
ts.tv_nsec = (msecs % 1000000ul) * 1000; // remainder, in nanoseconds
nanosleep(&ts, NULL);
#endif
}