30 #include <freetds/time.h>
32 #if defined(HAVE_GETUID) && defined(HAVE_GETPWUID)
53 #include <sys/socket.h>
57 #include <netinet/in.h>
61 #include <arpa/inet.h>
68 #if defined(_WIN32) || defined(_WIN64)
73 #include <freetds/tds.h>
74 #include <freetds/thread.h>
75 #include "replacements.h"
82 #if defined(_REENTRANT) && !defined(_WIN32)
83 #if HAVE_FUNC_LOCALTIME_R_TM
84 tm = localtime_r(timep,
result);
87 if (!localtime_r(timep,
result))
91 tm = localtime(timep);
103 #if !defined(_WIN32) && !defined(_WIN64)
108 #if HAVE_GETTIMEOFDAY
112 gettimeofday(&tv,
NULL);
125 strftime(
str, maxlen - 6,
"%H:%M:%S", tm);
127 #if HAVE_GETTIMEOFDAY
128 sprintf(usecs,
".%06lu", (
long) tv.tv_usec);
136 _snprintf(
str, maxlen - 1,
"%02u:%02u:%02u.%03u",
st.wHour,
st.wMinute,
st.wSecond,
st.wMilliseconds);
148 # undef NETDB_REENTRANT
149 # define NETDB_REENTRANT 1
152 #if !defined(HAVE_GETADDRINFO)
154 #if defined(NETDB_REENTRANT)
158 return gethostbyname(servername);
161 #elif defined(HAVE_GETIPNODEBYNAME) || defined(HAVE_GETIPNODEBYADDR)
167 tds_copy_hostent(
struct hostent *he,
struct hostent *
result,
char *
buffer,
int buflen)
169 #define CHECK_BUF(len) \
170 if (p + sizeof(struct hostent) - buffer > buflen) return -1;
171 #define ALIGN_P do { p += TDS_ALIGN_SIZE - 1; p -= (p-buffer) % TDS_ALIGN_SIZE; } while(0)
179 memcpy(he2, he,
sizeof(
struct hostent));
181 if (he->h_addr_list) {
186 for (
n = 0; he->h_addr_list[
n]; ++
n)
190 addresses = (
char **) p;
191 he2->h_addr_list = (
char **) p;
192 len =
sizeof(
char *) * (
n + 1);
196 for (
i = 0;
i <
n; ++
i) {
199 CHECK_BUF(he->h_length);
200 memcpy(p, he->h_addr_list[
i], he->h_length);
209 n = strlen(he->h_name) + 1;
212 memcpy(p, he->h_name,
n);
222 for (
n = 0; he->h_aliases[
n]; ++
n)
226 aliases = (
char **) p;
227 he2->h_aliases = (
char **) p;
228 len =
sizeof(
char *) * (
n + 1);
231 for (
i = 0;
i <
n; ++
i) {
232 len = strlen(he->h_aliases[
i]) + 1;
236 memcpy(p, he->h_aliases[
i],
len);
247 struct hostent *he = getipnodebyname(servername, AF_INET, 0, h_errnop);
254 *h_errnop = NETDB_INTERNAL;
262 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
271 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
279 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
283 struct hostent_data *
data = (
struct hostent_data *)
buffer;
285 memset(
buffer, 0, buflen);
286 if (gethostbyname_r(servername,
result,
data)) {
293 #elif defined(TDS_NO_THREADSAFE)
297 return gethostbyname(servername);
301 #error gethostbyname_r style unknown
308 #undef HAVE_GETADDRINFO
309 #undef NETDB_REENTRANT
310 #undef HAVE_FUNC_GETSERVBYNAME_R_6
311 #undef HAVE_FUNC_GETSERVBYNAME_R_5
312 #undef HAVE_FUNC_GETSERVBYNAME_R_4
313 #undef TDS_NO_THREADSAFE
316 # define HAVE_FUNC_GETSERVBYNAME_R_6 1
317 int test_getservbyname_r(
const char *name,
const char *proto,
318 struct servent *result_buf,
char *
buffer,
319 size_t buflen,
struct servent **
result);
320 # define getservbyname_r test_getservbyname_r
322 # define HAVE_FUNC_GETSERVBYNAME_R_5 1
324 test_getservbyname_r(
const char *name,
const char *proto,
325 struct servent *result_buf,
char *
buffer,
327 # define getservbyname_r test_getservbyname_r
329 # define HAVE_FUNC_GETSERVBYNAME_R_4 1
330 struct servent_data {
int dummy; };
332 test_getservbyname_r(
const char *name,
const char *proto,
333 struct servent *result_buf,
334 struct servent_data *
data);
335 # define getservbyname_r test_getservbyname_r
345 #if defined(HAVE_GETADDRINFO)
350 memset(&hints, 0,
sizeof(hints));
351 hints.ai_family = AF_INET;
352 hints.ai_socktype = SOCK_STREAM;
353 hints.ai_flags = AI_PASSIVE;
355 hints.ai_flags |= AI_CANONNAME;
360 if (res->ai_family != AF_INET || !res->ai_addr) {
364 result = ntohs(((
struct sockaddr_in *) res->ai_addr)->sin_port);
368 #elif defined(NETDB_REENTRANT)
370 struct servent *
result = getservbyname(name,
"tcp");
373 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_6)
376 struct servent result_buf;
380 return ntohs(
result->s_port);
383 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_5)
389 return ntohs(
result.s_port);
392 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_4)
395 struct servent_data
data;
397 if (!getservbyname_r(name,
"tcp", &
result, &
data))
398 return ntohs(
result.s_port);
401 #elif defined(TDS_NO_THREADSAFE)
402 struct servent *
result = getservbyname(name,
"tcp");
405 #error getservbyname_r style unknown
418 #if defined(HAVE_GETUID) && defined(HAVE_GETPWUID_R)
419 struct passwd *pw, bpw;
422 # if defined(HAVE_FUNC_GETPWUID_R_5)
425 if (getpwuid_r(getuid(), &bpw,
buf,
sizeof(
buf), &pw) || !pw)
428 # elif defined(HAVE_FUNC_GETPWUID_R_4_PW)
429 if (!(pw = getpwuid_r(getuid(), &bpw,
buf,
sizeof(
buf))))
432 if (getpwuid_r(getuid(), &bpw,
buf,
sizeof(
buf)))
437 return strdup(pw->pw_dir);
440 #elif defined(HAVE_GETUID) && defined(HAVE_GETPWUID) && !defined(_REENTRANT)
443 pw = getpwuid(getuid());
446 return strdup(pw->pw_dir);
450 home = getenv(
"HOME");
451 if (!home || !home[0])
466 char path[MAX_PATH] =
"";
468 LPMALLOC pMalloc =
NULL;
471 hr = SHGetMalloc(&pMalloc);
473 hr = SHGetSpecialFolderLocation(
NULL, CSIDL_APPDATA, &pidl);
475 if (SHGetPathFromIDListA(pidl, path))
477 (*pMalloc->lpVtbl->Free)(pMalloc, pidl);
479 (*pMalloc->lpVtbl->Release)(pMalloc);
static const char * str(char *buf, int n)
#define getaddrinfo(n, s, h, r)
struct tm * tds_localtime_r(const time_t *timep, struct tm *result)
int tds_getservice(const char *name)
Return service port given the name.
char * tds_get_homedir(void)
Get user home directory.
char * tds_timestamp_str(char *str, int maxlen)
struct hostent * tds_gethostbyname_r(const char *servername, struct hostent *result, char *buffer, int buflen, int *h_errnop)
static SLJIT_INLINE sljit_ins st(sljit_gpr r, sljit_s32 d, sljit_gpr x, sljit_gpr b)