53 #if !defined(HAVE_ASPRINTF) 58 #include <sys/types.h> 62 #if defined(AIX) && !defined(DONT_HOOK_MALLOC) 65 aix_malloc (
size_t size)
75 #define malloc(a) aix_malloc(a) 86 realpath (
const char *path,
char *resolved_path)
89 char *tmp_str = _fullpath (resolved_path, path, _MAX_PATH);
90 char tmp_path[_MAX_PATH] = { 0 };
95 strncpy (tmp_path, tmp_str, _MAX_PATH);
105 if (len > 0 && tmp_path[len - 1] ==
'\\')
107 tmp_path[len - 1] =
'\0';
110 if (stat (tmp_path, &stat_buf) == 0)
127 poll (
struct pollfd *fds, nfds_t nfds,
int timeout)
129 struct timeval to, *tp;
131 fd_set *rp, *wp, *ep;
134 unsigned long long max_fd;
139 to.tv_sec = timeout / 1000;
140 to.tv_usec = (timeout % 1000) * 1000;
150 for (i = 0; i < nfds; i++)
152 if (fds[i].events & POLLIN)
158 FD_SET (fds[i].fd, rp);
159 max_fd = MAX (fds[i].fd, max_fd);
161 if (fds[i].events & POLLOUT)
167 FD_SET (fds[i].fd, wp);
168 max_fd = MAX (fds[i].fd, max_fd);
170 if (fds[i].events & POLLPRI)
176 FD_SET (fds[i].fd, ep);
177 max_fd = MAX (fds[i].fd, max_fd);
181 r = select ((
int) max_fd + 1, rp, wp, ep, tp);
182 for (i = 0; i < nfds; i++)
185 if ((fds[i].events & POLLIN) && FD_ISSET (fds[i].fd, rp))
187 fds[
i].revents |= POLLIN;
189 if ((fds[i].events & POLLOUT) && FD_ISSET (fds[i].fd, wp))
191 fds[
i].revents |= POLLOUT;
193 if ((fds[i].events & POLLPRI) && FD_ISSET (fds[i].fd, ep))
195 fds[
i].revents |= POLLPRI;
203 #define EPOCH_BIAS_IN_100NANOSECS 116444736000000000LL 212 gettimeofday (
struct timeval *tp,
void *tzp)
218 #define RAPID_CALC_DIVISOR 78125 222 unsigned __int64 nsec100;
226 GetSystemTimeAsFileTime (&now.ft);
232 tp->tv_sec = (long) (((now.nsec100 - EPOCH_BIAS_IN_100NANOSECS) >> 7) / RAPID_CALC_DIVISOR);
241 ((long) (now.nsec100 - EPOCH_BIAS_IN_100NANOSECS - (((
unsigned __int64) (tp->tv_sec * RAPID_CALC_DIVISOR)) << 7))) /
247 #define LOCKING_SIZE 2000 256 lockf (
int fd,
int cmd,
long size)
261 return (_locking (fd, _LK_UNLCK, (size ? size : LOCKING_SIZE)));
264 return (_locking (fd, _LK_LOCK, (size ? size : LOCKING_SIZE)));
267 return (_locking (fd, _LK_NBLCK, (size ? size : LOCKING_SIZE)));
290 cuserid (
char *
string)
297 env = getenv (
"USERNAME");
300 strcpy (
string,
"noname");
314 getlogin_r (
char *buf,
size_t bufsize)
316 DWORD wd = static_cast < DWORD > (bufsize);
317 return GetUserName (buf, &wd);
357 pathconf (
char *path,
int name)
373 return ((MAX_PATH - 1));
376 if (GetVolumeInformation (
NULL,
NULL, 0,
NULL, (LPDWORD) & namelen, (LPDWORD) & filesysflags,
NULL, 0))
401 #define SIG_UNBLOCK 1 402 #define SIG_SETMASK 2 403 #define SIGABRT_BIT 1 407 #define SIGSEGV_BIT 16 408 #define SIGTERM_BIT 64 416 sigfillset (sigset_t *
set)
430 static int setmask (sigset_t *
set, sigset_t * oldset);
431 static int block_signals (sigset_t *
set, sigset_t * oldset);
432 static int unblock_signals (sigset_t *
set, sigset_t * oldset);
433 static void sync_mask (sigset_t *
set);
442 setmask (sigset_t *
set, sigset_t * oldset)
456 tmp.mask =
set->mask;
458 tmp.abrt_state = signal (SIGABRT, (tmp.mask |= SIGABRT_BIT) ? SIG_IGN : SIG_DFL);
459 if (tmp.abrt_state < 0)
462 (void) signal (SIGABRT, tmp.abrt_state);
464 tmp.fpe_state = signal (SIGFPE, (tmp.mask |= SIGFPE_BIT) ? SIG_IGN : SIG_DFL);
465 if (tmp.fpe_state < 0)
468 (void) signal (SIGFPE, tmp.fpe_state);
470 tmp.ill_state = signal (SIGILL, (tmp.mask |= SIGILL_BIT) ? SIG_IGN : SIG_DFL);
471 if (tmp.ill_state < 0)
474 (void) signal (SIGILL, tmp.ill_state);
476 tmp.int_state = signal (SIGINT, (tmp.mask |= SIGINT_BIT) ? SIG_IGN : SIG_DFL);
477 if (tmp.int_state < 0)
480 (void) signal (SIGINT, tmp.int_state);
482 tmp.sev_state = signal (SIGSEGV, (tmp.mask |= SIGSEGV_BIT) ? SIG_IGN : SIG_DFL);
483 if (tmp.sev_state < 0)
486 (void) signal (SIGSEGV, tmp.sev_state);
488 tmp.term_state = signal (SIGTERM, (tmp.mask |= SIGTERM_BIT) ? SIG_IGN : SIG_DFL);
489 if (tmp.term_state < 0)
492 (void) signal (SIGTERM, tmp.term_state);
496 oldset->term_state = tmp.term_state;
497 oldset->sev_state = tmp.sev_state;
498 oldset->int_state = tmp.int_state;
499 oldset->ill_state = tmp.ill_state;
500 oldset->fpe_state = tmp.fpe_state;
501 oldset->abrt_state = tmp.abrt_state;
523 block_signals (sigset_t *
set, sigset_t * oldset)
539 if (test & SIGABRT_BIT)
541 tmp.mask |= SIGABRT_BIT;
542 tmp.abrt_state = signal (SIGABRT, SIG_IGN);
543 if (tmp.abrt_state < 0)
546 (void) signal (SIGABRT, tmp.abrt_state);
549 if (test & SIGFPE_BIT)
551 tmp.mask |= SIGFPE_BIT;
552 tmp.fpe_state = signal (SIGFPE, SIG_IGN);
553 if (tmp.fpe_state < 0)
556 (void) signal (SIGFPE, tmp.fpe_state);
559 if (test & SIGILL_BIT)
561 tmp.mask |= SIGILL_BIT;
562 tmp.ill_state = signal (SIGILL, SIG_IGN);
563 if (tmp.ill_state < 0)
566 (void) signal (SIGILL, tmp.ill_state);
569 if (test & SIGINT_BIT)
571 tmp.mask |= SIGINT_BIT;
572 tmp.int_state = signal (SIGINT, SIG_IGN);
573 if (tmp.int_state < 0)
576 (void) signal (SIGINT, tmp.int_state);
579 if (test & SIGSEGV_BIT)
581 tmp.mask |= SIGSEGV_BIT;
582 tmp.sev_state = signal (SIGSEGV, SIG_IGN);
583 if (tmp.sev_state < 0)
586 (void) signal (SIGSEGV, tmp.sev_state);
589 if (test & SIGTERM_BIT)
591 tmp.mask |= SIGTERM_BIT;
592 tmp.term_state = signal (SIGTERM, SIG_IGN);
593 if (tmp.term_state < 0)
596 (void) signal (SIGTERM, tmp.term_state);
601 oldset->term_state = tmp.term_state;
602 oldset->sev_state = tmp.sev_state;
603 oldset->int_state = tmp.int_state;
604 oldset->ill_state = tmp.ill_state;
605 oldset->fpe_state = tmp.fpe_state;
606 oldset->abrt_state = tmp.abrt_state;
628 unblock_signals (sigset_t *
set, sigset_t * oldset)
644 if (test & SIGABRT_BIT)
646 tmp.mask |= SIGABRT_BIT;
647 tmp.abrt_state = signal (SIGABRT, set->abrt_state);
648 if (tmp.abrt_state < 0)
651 (void) signal (SIGABRT, tmp.abrt_state);
654 if (test & SIGFPE_BIT)
656 tmp.mask |= SIGFPE_BIT;
657 tmp.fpe_state = signal (SIGFPE, set->fpe_state);
658 if (tmp.fpe_state < 0)
661 (void) signal (SIGFPE, tmp.fpe_state);
664 if (test & SIGILL_BIT)
666 tmp.mask |= SIGILL_BIT;
667 tmp.ill_state = signal (SIGILL, set->ill_state);
668 if (tmp.ill_state < 0)
671 (void) signal (SIGILL, tmp.ill_state);
674 if (test & SIGINT_BIT)
676 tmp.mask |= SIGINT_BIT;
677 tmp.int_state = signal (SIGINT, set->int_state);
678 if (tmp.int_state < 0)
681 (void) signal (SIGINT, tmp.int_state);
684 if (test & SIGSEGV_BIT)
686 tmp.mask |= SIGSEGV_BIT;
687 tmp.sev_state = signal (SIGSEGV, set->sev_state);
688 if (tmp.sev_state < 0)
691 (void) signal (SIGSEGV, tmp.sev_state);
694 if (test & SIGTERM_BIT)
696 tmp.mask |= SIGTERM_BIT;
697 tmp.term_state = signal (SIGTERM, set->term_state);
698 if (tmp.term_state < 0)
701 (void) signal (SIGTERM, tmp.term_state);
706 oldset->term_state = tmp.term_state;
707 oldset->sev_state = tmp.sev_state;
708 oldset->int_state = tmp.int_state;
709 oldset->ill_state = tmp.ill_state;
710 oldset->fpe_state = tmp.fpe_state;
711 oldset->abrt_state = tmp.abrt_state;
732 sync_mask (sigset_t *
set)
734 set->mask |= (
set->term_state == SIG_IGN) ? SIGTERM_BIT : set->mask;
735 set->mask |= (set->sev_state == SIG_IGN) ? SIGSEGV_BIT :
set->mask;
736 set->mask |= (
set->int_state == SIG_IGN) ? SIGINT_BIT : set->mask;
737 set->mask |= (set->ill_state == SIG_IGN) ? SIGILL_BIT :
set->mask;
738 set->mask |= (
set->fpe_state == SIG_IGN) ? SIGFPE_BIT : set->mask;
739 set->mask |= (set->abrt_state == SIG_IGN) ? SIGABRT_BIT :
set->mask;
752 sigprocmask (
int how, sigset_t *
set, sigset_t * oldset)
757 return (block_signals (
set, oldset));
760 return (unblock_signals (
set, oldset));
763 return (setmask (
set, oldset));
776 static DWORD NT_PageSize = 0;
779 if (NT_PageSize == 0)
781 GetSystemInfo (&sysinfo);
782 NT_PageSize = sysinfo.dwPageSize;
784 return (NT_PageSize);
795 stat (
const char *path,
struct stat *buf)
800 rc = _stat (path, &_buf);
816 fpbits = _EM_OVERFLOW | _EM_UNDERFLOW | _EM_ZERODIVIDE;
817 (void) _control87 (fpbits, fpbits);
829 #if defined (ENABLE_UNUSED_FUNCTION) 839 lock_region (
int fd,
int cmd,
long offset,
long size)
841 if (lseek (fd, offset, SEEK_SET) != offset)
845 return lockf (fd, cmd, size);
862 free_space (
const char *path,
int page_size)
864 ULARGE_INTEGER freebytes_user, total_bytes, freebytes_system;
865 TCHAR disk[PATH_MAX];
869 _tcsncpy (disk, (TCHAR *) path, PATH_MAX);
871 temp = _tcschr (disk, __TEXT (
':'));
875 if (*temp == __TEXT (
'\\') || *temp == __TEXT (
'/'))
879 *temp = __TEXT (
'\0');
883 if (!GetDiskFreeSpaceEx ((temp) ? disk :
NULL, &freebytes_user, &total_bytes, &freebytes_system))
889 return ((
int) (freebytes_user.QuadPart / page_size));
894 #if !defined(HAVE_STRDUP) 907 size_t len =
strlen (str) + 1;
908 sdup = (
char *) malloc (len);
911 memcpy (sdup, str, len);
918 #if !defined(HAVE_VASPRINTF) 921 vasprintf (
char **ptr,
const char *format, va_list ap)
925 len = _vscprintf_p (format, ap) + 1;
926 *ptr = (
char *) malloc (len *
sizeof (
char));
932 return _vsprintf_p (*ptr, len, format, ap);
942 va_copy (ap_copy, ap);
943 count = vsnprintf (NULL, 0, format, ap);
946 buffer = (
char *) malloc (count + 1);
949 count = vsnprintf (buffer, count + 1, format, ap_copy);
971 #if !defined(HAVE_ASPRINTF) 980 va_start (ap, format);
995 return (errno = ERANGE);
998 if (path == NULL || *path ==
'\0')
1006 endp = path +
strlen (path) - 1;
1033 len = (ptrdiff_t) (endp - path) + 1;
1034 if (len + 1 > PATH_MAX)
1036 return (errno = ENAMETOOLONG);
1038 if (len + 1 > (
int) buflen)
1040 return (errno = ERANGE);
1042 (void) strncpy (pathbuf, path, len);
1043 pathbuf[len] =
'\0';
1052 double result = ceil (x);
1054 if ((x < 0) && (result == 0))
1061 #define ceil(x) aix_ceil(x) 1064 #if !defined(HAVE_DIRNAME) 1068 static char *bname =
NULL;
1072 bname = (
char *) malloc (PATH_MAX);
1077 return (
cub_dirname_r (path, bname, PATH_MAX) < 0) ? NULL : bname;
1084 const char *endp, *startp;
1088 return (errno = ERANGE);
1091 if (path == NULL || *path ==
'\0')
1099 endp = path +
strlen (path) - 1;
1116 len = (ptrdiff_t) (endp - startp) + 1;
1117 if (len + 1 > PATH_MAX)
1119 return (errno = ENAMETOOLONG);
1121 if (len + 1 > (
int) buflen)
1123 return (errno = ERANGE);
1125 (void) strncpy (pathbuf, startp, len);
1126 pathbuf[len] =
'\0';
1130 #if !defined(HAVE_BASENAME) 1134 static char *bname =
NULL;
1138 bname = (
char *) malloc (PATH_MAX);
1143 return (
basename_r (path, bname, PATH_MAX) < 0) ? NULL : bname;
1147 #if defined(WINDOWS) 1149 ctime_r (
const time_t * time,
char *time_buf)
1152 assert (time != NULL && time_buf != NULL);
1154 err = ctime_s (time_buf,
CTIME_MAX, time);
1163 #if defined(WINDOWS) 1165 localtime_r (
const time_t * time,
struct tm *tm_val)
1168 assert (time != NULL && tm_val != NULL);
1170 err = localtime_s (tm_val, time);
1180 #if defined (ENABLE_UNUSED_FUNCTION) 1182 utona (
unsigned int u,
char *s,
size_t n)
1184 char nbuf[10], *
p, *t;
1186 if (s == NULL || n == 0)
1199 *p++ = u % 10 +
'0';
1201 while ((u /= 10) > 0);
1209 while (p >= nbuf && --n > 1);
1216 itona (
int i,
char *s,
size_t n)
1218 if (s == NULL || n == 0)
1232 return utona (-i, s, n) + 1;
1236 return utona (i, s, n);
1247 if ((c = *find++) !=
'0')
1254 if ((sc = *s++) ==
'\0')
1259 while (toupper (sc) != toupper (c));
1261 while (strncasecmp (s, find, len) != 0);
1273 if (cuserid (
string) == NULL)
1279 string[size - 1] =
'\0';
1296 #if defined(WINDOWS) 1298 if (MoveFileEx (src_path, dest_path, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
1309 return rename (src_path, dest_path);
1335 #if defined(WINDOWS) 1336 return signal (sig_no, sig_handler);
1338 struct sigaction act;
1339 struct sigaction oact;
1341 act.sa_handler = sig_handler;
1344 if (sigemptyset (&act.sa_mask) < 0)
1352 #if defined(SA_INTERRUPT) 1353 act.sa_flags |= SA_INTERRUPT;
1357 #if defined(SA_RESTART) 1358 act.sa_flags |= SA_RESTART;
1363 if (sigaction (sig_no, &act, &oact) < 0)
1368 return (oact.sa_handler);
1380 #if defined(WINDOWS) 1383 kill (getpid (), sig_no);
1387 #if defined(WINDOWS) 1388 #if !defined(HAVE_STRSEP) 1390 strsep (
char **stringp,
const char *delim)
1394 if (*stringp == NULL)
1399 p = strstr (*stringp, delim);
1407 *stringp = p +
strlen (delim);
1420 getpass (
const char *prompt)
1424 static char password_buffer[80];
1426 fprintf (stdout, prompt);
1431 if (c ==
'\r' || c ==
'\n')
1439 if (pwlen <
sizeof (password_buffer) - 1)
1440 password_buffer[pwlen++] = c;
1442 password_buffer[pwlen] =
'\0';
1443 return password_buffer;
1448 #if defined(WINDOWS) 1451 setenv (
const char *name,
const char *val,
int overwrite)
1457 char *ptr = getenv (name);
1464 ret = _putenv_s (name, val);
1474 cub_vsnprintf (
char *buffer,
size_t count,
const char *format, va_list argptr)
1476 int len = _vscprintf_p (format, argptr) + 1;
1478 if (len > (
int) count)
1480 char *cp = (
char *) malloc (len);
1486 len = _vsprintf_p (cp, len, format, argptr);
1493 memcpy (buffer, cp, count - 1);
1494 buffer[count - 1] = 0;
1500 return _vsprintf_p (buffer, count, format, argptr);
1503 #if !defined(_MSC_VER) || _MSC_VER < 1800 1507 return d >= 0 ? floor (d + 0.5) : ceil (d - 0.5);
1514 if (mutex->csp == &mutex->cs && mutex->watermark == WATERMARK_MUTEX_INITIALIZED)
1521 mutex->csp = &mutex->cs;
1522 mutex->watermark = WATERMARK_MUTEX_INITIALIZED;
1523 InitializeCriticalSection (mutex->csp);
1531 if (mutex->csp != &mutex->cs || mutex->watermark != WATERMARK_MUTEX_INITIALIZED)
1533 if (mutex->csp == NULL)
1535 mutex->watermark = 0;
1542 mutex->watermark = 0;
1546 DeleteCriticalSection (mutex->csp);
1548 mutex->watermark = 0;
1553 pthread_mutexattr_init (pthread_mutexattr_t * attr)
1559 pthread_mutexattr_settype (pthread_mutexattr_t * attr,
int type)
1565 pthread_mutexattr_destroy (pthread_mutexattr_t * attr)
1571 pthread_mutex_t css_Internal_mutex_for_mutex_initialize = PTHREAD_MUTEX_INITIALIZER;
1574 port_win_mutex_init_and_lock (pthread_mutex_t * mutex)
1576 if (css_Internal_mutex_for_mutex_initialize.csp != &css_Internal_mutex_for_mutex_initialize.cs
1577 || css_Internal_mutex_for_mutex_initialize.watermark != WATERMARK_MUTEX_INITIALIZED)
1582 EnterCriticalSection (css_Internal_mutex_for_mutex_initialize.csp);
1583 if (mutex->csp != &mutex->cs || mutex->watermark != WATERMARK_MUTEX_INITIALIZED)
1589 assert (mutex->csp == NULL);
1592 LeaveCriticalSection (css_Internal_mutex_for_mutex_initialize.csp);
1594 EnterCriticalSection (mutex->csp);
1598 port_win_mutex_init_and_trylock (pthread_mutex_t * mutex)
1602 if (css_Internal_mutex_for_mutex_initialize.csp != &css_Internal_mutex_for_mutex_initialize.cs
1603 || css_Internal_mutex_for_mutex_initialize.watermark != WATERMARK_MUTEX_INITIALIZED)
1608 EnterCriticalSection (css_Internal_mutex_for_mutex_initialize.csp);
1609 if (mutex->csp != &mutex->cs || mutex->watermark != WATERMARK_MUTEX_INITIALIZED)
1615 assert (mutex->csp == NULL);
1618 LeaveCriticalSection (css_Internal_mutex_for_mutex_initialize.csp);
1620 r = TryEnterCriticalSection (mutex->csp);
1621 if (mutex->csp->RecursionCount > 1)
1623 LeaveCriticalSection (mutex->csp);
1627 return r ? 0 : EBUSY;
1631 typedef void (WINAPI * InitializeConditionVariable_t) (CONDITION_VARIABLE *);
1632 typedef bool (WINAPI * SleepConditionVariableCS_t) (CONDITION_VARIABLE *, CRITICAL_SECTION *, DWORD dwMilliseconds);
1634 typedef void (WINAPI * WakeAllConditionVariable_t) (CONDITION_VARIABLE *);
1635 typedef void (WINAPI * WakeConditionVariable_t) (CONDITION_VARIABLE *);
1637 InitializeConditionVariable_t fp_InitializeConditionVariable;
1638 SleepConditionVariableCS_t fp_SleepConditionVariableCS;
1639 WakeAllConditionVariable_t fp_WakeAllConditionVariable;
1640 WakeConditionVariable_t fp_WakeConditionVariable;
1642 static bool have_CONDITION_VARIABLE =
false;
1646 check_CONDITION_VARIABLE (
void)
1648 HMODULE kernel32 = GetModuleHandle (
"kernel32");
1650 have_CONDITION_VARIABLE =
true;
1651 fp_InitializeConditionVariable =
1652 (InitializeConditionVariable_t) GetProcAddress (kernel32,
"InitializeConditionVariable");
1653 if (fp_InitializeConditionVariable == NULL)
1655 have_CONDITION_VARIABLE =
false;
1659 fp_SleepConditionVariableCS = (SleepConditionVariableCS_t) GetProcAddress (kernel32,
"SleepConditionVariableCS");
1660 fp_WakeAllConditionVariable = (WakeAllConditionVariable_t) GetProcAddress (kernel32,
"WakeAllConditionVariable");
1661 fp_WakeConditionVariable = (WakeConditionVariable_t) GetProcAddress (kernel32,
"WakeConditionVariable");
1665 timespec_to_msec (
const struct timespec *abstime)
1670 if (abstime == NULL)
1675 gettimeofday (&tv, NULL);
1676 msec = (abstime->tv_sec - tv.tv_sec) * 1000;
1677 msec += (abstime->tv_nsec / 1000 - tv.tv_usec) / 1000;
1693 win_custom_cond_init (pthread_cond_t * cond,
const pthread_condattr_t * attr)
1695 cond->initialized =
true;
1697 InitializeCriticalSection (&cond->lock_waiting);
1699 cond->events[COND_SIGNAL] = CreateEvent (NULL,
FALSE,
FALSE, NULL);
1701 cond->broadcast_block_event = CreateEvent (NULL,
TRUE,
TRUE, NULL);
1703 if (cond->events[COND_SIGNAL] == NULL || cond->events[
COND_BROADCAST] == NULL || cond->broadcast_block_event == NULL)
1712 win_custom_cond_destroy (pthread_cond_t * cond)
1714 if (!cond->initialized)
1719 DeleteCriticalSection (&cond->lock_waiting);
1721 if (CloseHandle (cond->events[COND_SIGNAL]) == 0 || CloseHandle (cond->events[
COND_BROADCAST]) == 0
1722 || CloseHandle (cond->broadcast_block_event) == 0)
1727 cond->initialized =
false;
1732 win_custom_cond_timedwait (pthread_cond_t * cond, pthread_mutex_t * mutex,
struct timespec *abstime)
1737 assert (cond->initialized ==
true);
1739 msec = timespec_to_msec (abstime);
1740 WaitForSingleObject (cond->broadcast_block_event, INFINITE);
1742 EnterCriticalSection (&cond->lock_waiting);
1744 LeaveCriticalSection (&cond->lock_waiting);
1746 LeaveCriticalSection (mutex->csp);
1747 result = WaitForMultipleObjects (2, cond->events,
FALSE, msec);
1748 assert (result == WAIT_TIMEOUT || result <= 2);
1752 EnterCriticalSection (&cond->lock_waiting);
1755 if (cond->waiting == 0)
1758 SetEvent (cond->broadcast_block_event);
1764 WaitForSingleObject (cond->events[COND_SIGNAL], 0);
1767 LeaveCriticalSection (&cond->lock_waiting);
1768 EnterCriticalSection (mutex->csp);
1770 return result == WAIT_TIMEOUT ? ETIMEDOUT : 0;
1774 win_custom_cond_signal (pthread_cond_t * cond)
1776 assert (cond->initialized ==
true);
1778 EnterCriticalSection (&cond->lock_waiting);
1780 if (cond->waiting > 0)
1782 SetEvent (cond->events[COND_SIGNAL]);
1785 LeaveCriticalSection (&cond->lock_waiting);
1791 win_custom_cond_broadcast (pthread_cond_t * cond)
1793 assert (cond->initialized ==
true);
1795 EnterCriticalSection (&cond->lock_waiting);
1797 if (cond->waiting > 0)
1799 ResetEvent (cond->broadcast_block_event);
1803 LeaveCriticalSection (&cond->lock_waiting);
1809 pthread_cond_init (pthread_cond_t * cond,
const pthread_condattr_t * attr)
1811 static bool checked =
false;
1812 if (checked ==
false)
1814 check_CONDITION_VARIABLE ();
1818 if (have_CONDITION_VARIABLE)
1820 fp_InitializeConditionVariable (&cond->native_cond);
1824 return win_custom_cond_init (cond, attr);
1828 pthread_cond_destroy (pthread_cond_t * cond)
1830 if (have_CONDITION_VARIABLE)
1835 return win_custom_cond_destroy (cond);
1839 pthread_cond_broadcast (pthread_cond_t * cond)
1841 if (have_CONDITION_VARIABLE)
1843 fp_WakeAllConditionVariable (&cond->native_cond);
1847 return win_custom_cond_broadcast (cond);
1851 pthread_cond_signal (pthread_cond_t * cond)
1853 if (have_CONDITION_VARIABLE)
1855 fp_WakeConditionVariable (&cond->native_cond);
1859 return win_custom_cond_signal (cond);
1863 pthread_cond_timedwait (pthread_cond_t * cond, pthread_mutex_t * mutex,
struct timespec *abstime)
1865 if (have_CONDITION_VARIABLE)
1867 int msec = timespec_to_msec (abstime);
1868 if (fp_SleepConditionVariableCS (&cond->native_cond, mutex->csp, msec) ==
false)
1876 return win_custom_cond_timedwait (cond, mutex, abstime);
1880 pthread_cond_wait (pthread_cond_t * cond, pthread_mutex_t * mutex)
1882 return pthread_cond_timedwait (cond, mutex, NULL);
1887 pthread_create (pthread_t * thread,
const pthread_attr_t * attr,
1891 *thread = (pthread_t) _beginthreadex (NULL, 0, start_routine, arg, 0, &tid);
1892 return (*thread <= 0) ? -1 : 0;
1904 return (pthread_t) GetCurrentThreadId ();
1908 pthread_join (pthread_t thread,
void **value_ptr)
1910 return WaitForSingleObject (thread, INFINITE);
1914 pthread_key_create (pthread_key_t * key,
void (*destructor) (
void *))
1916 return (*key = TlsAlloc ()) != 0xFFFFFFFF ? 0 : -1;
1920 pthread_key_delete (pthread_key_t key)
1922 return TlsFree (key) != 0 ? 0 : -1;
1926 pthread_setspecific (pthread_key_t key,
const void *value)
1928 return TlsSetValue (key, (LPVOID) value) != 0 ? 0 : -1;
1932 pthread_getspecific (pthread_key_t key)
1934 return TlsGetValue (key);
1937 #if !defined(_WIN64) 1943 win32_compare_exchange64 (UINT64
volatile *val_ptr, UINT64 swap_val, UINT64 cmp_val)
1949 mov ebx, dword ptr[swap_val]
1950 mov ecx, dword ptr[swap_val + 4]
1951 mov eax, dword ptr[cmp_val]
1952 mov edx, dword ptr[cmp_val + 4]
1959 win32_exchange_add64 (UINT64
volatile *ptr, UINT64 amount)
1966 while (win32_compare_exchange64 (ptr, old + amount, old) != old);
1971 win32_exchange64 (UINT64
volatile *ptr, UINT64 new_val)
1978 while (win32_compare_exchange64 (ptr, new_val, old) != old);
1985 #if defined(WINDOWS) 1993 strtod_win (
const char *str,
char **end_ptr)
1995 bool is_hex =
false;
1996 double result = 0.0, int_d = 0.0, float_d = 0.0;
2001 if (str == NULL || *str ==
'\0')
2003 if (end_ptr != NULL)
2005 *end_ptr = (
char *) str;
2022 if (*p ==
'0' && (*(p + 1) ==
'x' || *(p + 1) ==
'X'))
2038 if (
'0' <= *p && *p <=
'9')
2040 tmp_d = (double) (*p -
'0');
2042 else if (
'A' <= *p && *p <=
'F')
2044 tmp_d = (double) (*p -
'A' + 10);
2046 else if (
'a' <= *p && *p <=
'f')
2048 tmp_d = (double) (*p -
'a' + 10);
2056 int_d = int_d * 16.0 + tmp_d;
2067 while (*++p !=
'\0')
2074 if (
'0' <= *p && *p <=
'9')
2076 tmp_d = (double) (*p -
'0');
2078 else if (
'A' <= *p && *p <=
'F')
2080 tmp_d = (double) (*p -
'A' + 10);
2082 else if (
'a' <= *p && *p <=
'f')
2084 tmp_d = (double) (*p -
'a' + 10);
2092 float_d = (float_d + tmp_d) / 16.0;
2098 result = int_d + float_d;
2099 if (sign_flag == -1)
2105 if (result > DBL_MAX || (-result) > DBL_MAX)
2112 result = strtod (str, end_ptr);
2117 if (is_hex && end_ptr != NULL)
2119 *end_ptr = (
char *) end_p;
2137 msec = (end_time->tv_sec - start_time->tv_sec) * 1000LL;
2138 msec += (end_time->tv_usec - start_time->tv_usec) / 1000LL;
2156 added_time->tv_sec = start_time->tv_sec + msec / 1000LL;
2157 usec = (msec % 1000LL) * 1000LL;
2159 added_time->tv_sec += (start_time->tv_usec + usec) / 1000000LL;
2160 added_time->tv_usec = (start_time->tv_usec + usec) % 1000000LL;
2178 to->tv_sec = from->tv_sec;
2179 to->tv_nsec = from->tv_usec * 1000LL;
2200 #ifdef HAVE_OPEN_MEMSTREAM 2201 return open_memstream (ptr, sizeloc);
2203 *ptr = tempnam (NULL,
"cubrid_");
2204 return fopen (*ptr,
"w+");
2226 #ifdef HAVE_OPEN_MEMSTREAM 2230 struct stat stat_buf;
2232 if (fstat (fileno (fp), &stat_buf) == 0)
2234 *sizeloc = stat_buf.st_size;
2236 buff = (
char *) malloc (*sizeloc + 1);
2241 fseek (fp, 0, SEEK_SET);
2242 n = fread (buff, 1, *sizeloc, fp);
2268 for (s = str; *s !=
'\0' && (*s ==
' ' || *s ==
'\t' || *s ==
'\n' || *s ==
'\r'); s++)
2277 for (p = s; *p !=
'\0'; p++)
2279 for (p--; *p ==
' ' || *p ==
'\t' || *p ==
'\n' || *p ==
'\r'; p--)
2284 memmove (str, s,
strlen (s) + 1);
2358 val = strtol (str_p, end_p, base);
2360 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0))
2365 if (*end_p == str_p)
2371 if (val < INT_MIN || val > INT_MAX)
2384 unsigned long val = 0;
2394 val = strtoul (str_p, end_p, base);
2396 if ((errno == ERANGE && val == ULONG_MAX) || (errno != 0 && val == 0))
2401 if (*end_p == str_p)
2412 *ret_p = (
unsigned int) val;
2431 val = strtoll (str_p, end_p, base);
2433 if ((errno == ERANGE && (val ==
LLONG_MAX || val ==
LLONG_MIN)) || (errno != 0 && val == 0))
2438 if (*end_p == str_p)
2461 val = strtoull (str_p, end_p, base);
2463 if ((errno == ERANGE && val ==
ULLONG_MAX) || (errno != 0 && val == 0))
2468 if (*end_p == str_p)
2491 val = strtod (str_p, end_p);
2493 if (errno == ERANGE || errno != 0)
2498 if (*end_p == str_p)
2521 val = strtof (str_p, end_p);
2523 if (errno == ERANGE || errno != 0)
2528 if (*end_p == str_p)
2538 #if defined(WINDOWS) 2540 strtof_win (
const char *nptr,
char **endptr)
2547 d_val = strtod (nptr, endptr);
2548 if (errno == ERANGE)
2553 if (d_val > FLT_MAX)
2556 *endptr = const_cast <
char *>(nptr);
2559 else if (d_val < (-FLT_MAX))
2562 *endptr = const_cast <
char *>(nptr);
2565 else if (((d_val > 0) && (d_val < FLT_MIN)) || ((d_val < 0) && (d_val > (-FLT_MIN))))
2568 *endptr = const_cast <
char *>(nptr);
2572 f_val = (float) d_val;
2577 #ifndef HAVE_STRLCPY 2587 const char *s = src;
2594 if (n != 0 && --n != 0)
2598 if ((*d++ = *s++) == 0)
2613 return (s - src - 1);
2617 #if (defined(WINDOWS) && defined(_WIN32)) 2619 mktime_for_win32 (
struct tm * tm)
2627 t_32 = _mktime32 (tm);
2630 return (time_t) t_32;
2635 t_64 = _mktime64 (tm);
2637 if (t_64 >= 0x00 && t_64 <= 0x7FFFFFFF)
2639 return (time_t) t_64;
2647 return (time_t) (-1);
2663 #if defined (WINDOWS) 2668 tv.tv_sec = msec / 1000L;
2669 tv.tv_usec = msec % 1000L * 1000L;
2672 select (0, NULL, NULL, NULL, &tv);
2676 assert (error == 0 || error == EINTR);
int os_rename_file(const char *src_path, const char *dest_path)
char * dirname(const char *path)
int str_to_double(double *ret_p, char **end_p, const char *str_p)
#define COND_BROADCAST(a)
#define pthread_mutex_init(a, b)
int parse_int(int *ret_p, const char *str_p, int base)
int vasprintf(char **ptr, const char *format, va_list ap)
int str_to_uint32(unsigned int *ret_p, char **end_p, const char *str_p, int base)
int str_to_int64(INT64 *ret_p, char **end_p, const char *str_p, int base)
void os_send_signal(const int sig_no)
char * getuserid(char *string, int size)
INT64 timeval_diff_in_msec(const struct timeval *end_time, const struct timeval *start_time)
char * stristr(const char *s, const char *find)
void port_close_memstream(FILE *fp, char **ptr, size_t *sizeloc)
int str_to_uint64(UINT64 *ret_p, char **end_p, const char *str_p, int base)
int asprintf(char **ptr, const char *format,...)
int cub_dirname_r(const char *path, char *pathbuf, size_t buflen)
int str_to_int32(int *ret_p, char **end_p, const char *str_p, int base)
static struct timeval start_time
int count(int &result, const cub_regex_object ®, const std::string &src, const int position, const INTL_CODESET codeset)
int timeval_to_timespec(struct timespec *to, const struct timeval *from)
static void error(const char *msg)
size_t strlcpy(char *dst, const char *src, size_t siz)
char * basename(const char *path)
char * strdup(const char *str)
int parse_bigint(INT64 *ret_p, const char *str_p, int base)
int msleep(const long msec)
void(* SIGNAL_HANDLER_FUNCTION)(int sig_no)
int timeval_add_msec(struct timeval *added_time, const struct timeval *start_time, int msec)
int str_to_float(float *ret_p, char **end_p, const char *str_p)
SIGNAL_HANDLER_FUNCTION os_set_signal_handler(const int sig_no, SIGNAL_HANDLER_FUNCTION sig_handler)
#define THREAD_CALLING_CONVENTION
int basename_r(const char *path, char *pathbuf, size_t buflen)
#define pthread_mutex_destroy(a)
FILE * port_open_memstream(char **ptr, size_t *sizeloc)