1 /*
2 **  OSSP uuid - Universally Unique Identifier
3 **  Copyright (c) 2004-2008 Ralf S. Engelschall <rse@engelschall.com>
4 **  Copyright (c) 2004-2008 The OSSP Project <http://www.ossp.org/>
5 **
6 **  This file is part of OSSP uuid, a library for the generation
7 **  of UUIDs which can found at http://www.ossp.org/pkg/lib/uuid/
8 **
9 **  Permission to use, copy, modify, and distribute this software for
10 **  any purpose with or without fee is hereby granted, provided that
11 **  the above copyright notice and this permission notice appear in all
12 **  copies.
13 **
14 **  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
15 **  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 **  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
18 **  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 **  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 **  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
21 **  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 **  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 **  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24 **  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 **  SUCH DAMAGE.
26 **
27 **  uuid_time.c: Time Management
28 */
29 
30 /* own headers (part (1/2) */
31 #include "uuid_ac.h"
32 
33 /* system headers */
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <time.h>
37 #ifdef HAVE_SYS_TIME_H
38 #include <sys/time.h>
39 #endif
40 #ifdef HAVE_SYS_TYPES_H
41 #include <sys/types.h>
42 #endif
43 #ifdef HAVE_SYS_SELECT_H
44 #include <sys/select.h>
45 #endif
46 
47 /* own headers (part (1/2) */
48 #include "uuid_time.h"
49 
50 /* POSIX gettimeofday(2) abstraction (without timezone) */
time_gettimeofday(struct timeval * tv)51 int time_gettimeofday(struct timeval *tv)
52 {
53 #if defined(HAVE_GETTIMEOFDAY)
54     /* Unix/POSIX pass-through */
55     return gettimeofday(tv, NULL);
56 #elif defined(HAVE_CLOCK_GETTIME)
57     /* POSIX emulation */
58     struct timespec ts;
59     if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
60         return -1;
61     if (tv != NULL) {
62         tv->tv_sec = (long)ts.tv_sec;
63         tv->tv_usec = (long)ts.tv_nsec / 1000;
64     }
65     return 0;
66 #elif defined(WIN32)
67     /* Windows emulation */
68     FILETIME ft;
69     LARGE_INTEGER li;
70     __int64 t;
71     static int tzflag;
72 #if !defined(__GNUC__)
73 #define EPOCHFILETIME 116444736000000000i64
74 #else
75 #define EPOCHFILETIME 116444736000000000LL
76 #endif
77     if (tv != NULL) {
78         GetSystemTimeAsFileTime(&ft);
79         li.LowPart  = ft.dwLowDateTime;
80         li.HighPart = ft.dwHighDateTime;
81         t  = li.QuadPart;
82         t -= EPOCHFILETIME;
83         t /= 10;
84         tv->tv_sec  = (long)(t / 1000000);
85         tv->tv_usec = (long)(t % 1000000);
86     }
87     return 0;
88 #else
89 #error neither Win32 GetSystemTimeAsFileTime() nor Unix gettimeofday() nor POSIX clock_gettime() available!
90 #endif
91 }
92 
93 /* BSD usleep(3) abstraction */
time_usleep(long usec)94 int time_usleep(long usec)
95 {
96 #if defined(WIN32) && defined(HAVE_SLEEP)
97     /* Win32 newer Sleep(3) variant */
98     Sleep(usec / 1000);
99 #elif defined(WIN32)
100     /* Win32 older _sleep(3) variant */
101     _sleep(usec / 1000);
102 #elif defined(HAVE_NANOSLEEP)
103     /* POSIX newer nanosleep(3) variant */
104     struct timespec ts;
105     ts.tv_sec  = 0;
106     ts.tv_nsec = 1000 * usec;
107     nanosleep(&ts, NULL);
108 #else
109     /* POSIX older select(2) variant */
110     struct timeval tv;
111     tv.tv_sec  = 0;
112     tv.tv_usec = usec;
113     select(0, NULL, NULL, NULL, &tv);
114 #endif
115     return 0;
116 }
117 
118