Added CVS id.
[platform/upstream/c-ares.git] / windows_port.c
1 #include "setup.h"
2
3 /* $Id: */
4
5 /* only do the following on windows
6  */
7 #if (defined(WIN32) || defined(WATT32)) && !defined(MSDOS)
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <ctype.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <malloc.h>
14
15 #ifdef WATT32
16 #include <sys/socket.h>
17 #else
18 #include "nameser.h"
19 #endif
20 #include "ares.h"
21 #include "ares_private.h"
22
23 #ifndef __MINGW32__
24 int
25 ares_strncasecmp(const char *a, const char *b, int n)
26 {
27     int i;
28
29     for (i = 0; i < n; i++) {
30         int c1 = isupper(a[i]) ? tolower(a[i]) : a[i];
31         int c2 = isupper(b[i]) ? tolower(b[i]) : b[i];
32         if (c1 != c2) return c1-c2;
33     }
34     return 0;
35 }
36
37 int
38 ares_strcasecmp(const char *a, const char *b)
39 {
40     return strncasecmp(a, b, strlen(a)+1);
41 }
42 #endif
43
44 /*
45  * Number of micro-seconds between the beginning of the Windows epoch
46  * (Jan. 1, 1601) and the Unix epoch (Jan. 1, 1970).
47  */
48 #if defined(_MSC_VER) || defined(__WATCOMC__)
49 #define EPOCH_FILETIME 11644473600000000Ui64
50 #else
51 #define EPOCH_FILETIME 11644473600000000ULL
52 #endif
53
54 int
55 ares_gettimeofday(struct timeval *tv, struct timezone *tz)
56 {
57     FILETIME        ft;
58     LARGE_INTEGER   li;
59     __int64         t;
60
61     if (tv)
62     {
63         GetSystemTimeAsFileTime(&ft);
64         li.LowPart  = ft.dwLowDateTime;
65         li.HighPart = ft.dwHighDateTime;
66         t  = li.QuadPart / 10;   /* In micro-second intervals */
67         t -= EPOCH_FILETIME;     /* Offset to the Epoch time */
68         tv->tv_sec  = (long)(t / 1000000);
69         tv->tv_usec = (long)(t % 1000000);
70     }
71     (void) tz;
72     return 0;
73 }
74
75 int
76 ares_writev (ares_socket_t s, const struct iovec *vector, size_t count)
77 {
78   char *buffer, *bp;
79   size_t i, bytes = 0;
80
81   /* Find the total number of bytes to write
82    */
83   for (i = 0; i < count; i++)
84       bytes += vector[i].iov_len;
85
86   if (bytes == 0)   /* not an error */
87      return (0);
88
89   /* Allocate a temporary buffer to hold the data
90    */
91   buffer = bp = (char*) alloca (bytes);
92   if (!buffer)
93   {
94     errno = ENOMEM;
95     return (-1);
96   }
97
98   /* Copy the data into buffer.
99    */
100   for (i = 0; i < count; ++i)
101   {
102     memcpy (bp, vector[i].iov_base, vector[i].iov_len);
103     bp += vector[i].iov_len;
104   }
105   return send (s, (const void*)buffer, bytes, 0);
106 }
107 #endif /* WIN32 builds only */