OSDN Git Service

libuuid: Windows portability fixes
authorTheodore Ts'o <tytso@mit.edu>
Fri, 22 Aug 2008 16:15:56 +0000 (12:15 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 22 Aug 2008 16:15:56 +0000 (12:15 -0400)
Make the uuid library (more) portable for Windows.

Addresses-Sourceforge-Feature-Request: #1937287

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
configure
configure.in
lib/uuid/gen_uuid.c
lib/uuid/tst_uuid.c
lib/uuid/uuid.h
lib/uuid/uuid_time.c

index 74ec761..db058bd 100755 (executable)
--- a/configure
+++ b/configure
@@ -11883,7 +11883,8 @@ fi
 
 
 
-for ac_header in dirent.h errno.h getopt.h malloc.h mntent.h paths.h setjmp.h signal.h stdarg.h stdint.h stdlib.h termios.h termio.h unistd.h utime.h linux/fd.h linux/major.h net/if_dl.h netinet/in.h sys/disklabel.h sys/ioctl.h sys/mkdev.h sys/mman.h sys/prctl.h sys/queue.h sys/resource.h sys/select.h sys/socket.h sys/sockio.h sys/stat.h sys/syscall.h sys/sysmacros.h sys/time.h sys/types.h sys/un.h sys/wait.h
+
+for ac_header in dirent.h errno.h getopt.h malloc.h mntent.h paths.h setjmp.h signal.h stdarg.h stdint.h stdlib.h termios.h termio.h unistd.h utime.h linux/fd.h linux/major.h net/if_dl.h netinet/in.h sys/disklabel.h sys/file.h sys/ioctl.h sys/mkdev.h sys/mman.h sys/prctl.h sys/queue.h sys/resource.h sys/select.h sys/socket.h sys/sockio.h sys/stat.h sys/syscall.h sys/sysmacros.h sys/time.h sys/types.h sys/un.h sys/wait.h
 do
 as_ac_Header=`echo "ac_cv_header_$ac_header" | $as_tr_sh`
 if { as_var=$as_ac_Header; eval "test \"\${$as_var+set}\" = set"; }; then
index 3b9c53f..2e0f935 100644 (file)
@@ -623,7 +623,7 @@ if test $cross_compiling = no; then
 else
   AC_CHECK_PROGS(BUILD_CC, gcc cc)
 fi
-AC_CHECK_HEADERS(dirent.h errno.h getopt.h malloc.h mntent.h paths.h setjmp.h signal.h stdarg.h stdint.h stdlib.h termios.h termio.h unistd.h utime.h linux/fd.h linux/major.h net/if_dl.h netinet/in.h sys/disklabel.h sys/ioctl.h sys/mkdev.h sys/mman.h sys/prctl.h sys/queue.h sys/resource.h sys/select.h sys/socket.h sys/sockio.h sys/stat.h sys/syscall.h sys/sysmacros.h sys/time.h sys/types.h sys/un.h sys/wait.h)
+AC_CHECK_HEADERS(dirent.h errno.h getopt.h malloc.h mntent.h paths.h setjmp.h signal.h stdarg.h stdint.h stdlib.h termios.h termio.h unistd.h utime.h linux/fd.h linux/major.h net/if_dl.h netinet/in.h sys/disklabel.h sys/file.h sys/ioctl.h sys/mkdev.h sys/mman.h sys/prctl.h sys/queue.h sys/resource.h sys/select.h sys/socket.h sys/sockio.h sys/stat.h sys/syscall.h sys/sysmacros.h sys/time.h sys/types.h sys/un.h sys/wait.h)
 AC_CHECK_HEADERS(sys/disk.h sys/mount.h,,,
 [[
 #if HAVE_SYS_QUEUE_H
index fd9314d..152a6c0 100644 (file)
  */
 #define _SVID_SOURCE
 
+#ifdef _WIN32
+#define _WIN32_WINNT 0x0500
+#include <windows.h>
+#define UUID MYUUID
+#endif
 #include <stdio.h>
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <sys/types.h>
+#ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
+#endif
 #include <sys/wait.h>
 #include <sys/stat.h>
+#ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
+#endif
 #ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
 #endif
 THREAD_LOCAL unsigned short jrand_seed[3];
 #endif
 
+#ifdef _WIN32
+static void gettimeofday (struct timeval *tv, void *dummy)
+{
+       FILETIME        ftime;
+       uint64_t        n;
+
+       GetSystemTimeAsFileTime (&ftime);
+       n = (((uint64_t) ftime.dwHighDateTime << 32)
+            + (uint64_t) ftime.dwLowDateTime);
+       if (n) {
+               n /= 10;
+               n -= ((369 * 365 + 89) * (uint64_t) 86400) * 1000000;
+       }
+
+       tv->tv_sec = n / 1000000;
+       tv->tv_usec = n % 1000000;
+}
+
+static int getuid (void)
+{
+       return 1;
+}
+#endif
+
 static int get_random_fd(void)
 {
        struct timeval  tv;
@@ -105,6 +138,7 @@ static int get_random_fd(void)
 
        if (fd == -2) {
                gettimeofday(&tv, 0);
+#ifndef _WIN32
                fd = open("/dev/urandom", O_RDONLY);
                if (fd == -1)
                        fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
@@ -113,6 +147,7 @@ static int get_random_fd(void)
                        if (i >= 0) 
                                fcntl(fd, F_SETFD, i | FD_CLOEXEC);
                }
+#endif
                srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
 #ifdef DO_JRAND_MIX
                jrand_seed[0] = getpid() ^ (tv.tv_sec & 0xFFFF);
@@ -173,6 +208,11 @@ static void get_random_bytes(void *buf, int nbytes)
 
 /*
  * Get the ethernet hardware address, if we can find it...
+ *
+ * XXX for a windows version, probably should use GetAdaptersInfo:
+ * http://www.codeguru.com/cpp/i-n/network/networkinformation/article.php/c5451
+ * commenting out get_node_id just to get gen_uuid to compile under windows
+ * is not the right way to go!
  */
 static int get_node_id(unsigned char *node_id)
 {
@@ -269,7 +309,7 @@ static int get_clock(uint32_t *clock_high, uint32_t *clock_low,
        THREAD_LOCAL uint16_t           clock_seq;
        struct timeval                  tv;
        struct flock                    fl;
-       unsigned long long              clock_reg;
+       uint64_t                        clock_reg;
        mode_t                          save_umask;
 
        if (state_fd == -2) {
@@ -339,8 +379,8 @@ try_again:
        }
                
        clock_reg = tv.tv_usec*10 + adjustment;
-       clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
-       clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
+       clock_reg += ((uint64_t) tv.tv_sec)*10000000;
+       clock_reg += (((uint64_t) 0x01B21DD2) << 32) + 0x13814000;
 
        if (num && (*num > 1)) {
                adjustment += *num - 1;
index 086d35c..01606d7 100644 (file)
  * %End-Header%
  */
 
+#ifdef _WIN32
+#define _WIN32_WINNT 0x0500
+#include <windows.h>
+#define UUID MYUUID
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
 
index e9cf889..1c241e3 100644 (file)
@@ -36,7 +36,9 @@
 #define _UUID_UUID_H
 
 #include <sys/types.h>
+#ifndef _WIN32
 #include <sys/time.h>
+#endif
 #include <time.h>
 
 typedef unsigned char uuid_t[16];
index d5f992b..fc1d266 100644 (file)
  * %End-Header%
  */
 
+#ifdef _WIN32
+#define _WIN32_WINNT 0x0500
+#include <windows.h>
+#define UUID MYUUID
+#endif
+
 #include <stdio.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 #include <stdlib.h>
 #include <sys/types.h>
+#ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
+#endif
 #include <time.h>
 
 #include "uuidP.h"
 
 time_t uuid_time(const uuid_t uu, struct timeval *ret_tv)
 {
-       struct uuid             uuid;
-       uint32_t                        high;
        struct timeval          tv;
-       unsigned long long      clock_reg;
+       struct uuid             uuid;
+       uint32_t                high;
+       uint64_t                clock_reg;
 
        uuid_unpack(uu, &uuid);
        
        high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16);
-       clock_reg = uuid.time_low | ((unsigned long long) high << 32);
+       clock_reg = uuid.time_low | ((uint64_t) high << 32);
 
-       clock_reg -= (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
+       clock_reg -= (((uint64_t) 0x01B21DD2) << 32) + 0x13814000;
        tv.tv_sec = clock_reg / 10000000;
        tv.tv_usec = (clock_reg % 10000000) / 10;