1/**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6
7#include <stdint.h>
8#include <errno.h>
9#include <sys/timeb.h>
10
11int __cdecl ftime32(struct __timeb32 *tb32);
12int __cdecl ftime32(struct __timeb32 *tb32)
13{
14 /*
15 * Both 32-bit and 64-bit MS _ftime functions have void return value and do not signal overflow.
16 * So if 32-bit POSIX ftime function wants to detect overflow it has to call 64-bit _ftime function.
17 * msvc defines ftime as alias to _ftime, which always fills all members even if they overflow.
18 * So for compatibility with application code written for msvc ftime function, always fill
19 * in our mingw-w64 POSIX ftime function all __timeb32 members, even if they are overflowed.
20 * And if overflow happens, correctly sets errno to EOVERFLOW and returns negative value.
21 */
22 struct __timeb64 tb64;
23 _ftime64(&tb64);
24 tb32->time = (__time32_t)tb64.time; /* truncate */
25 tb32->millitm = tb64.millitm;
26 tb32->timezone = tb64.timezone;
27 tb32->dstflag = tb64.dstflag;
28 if (tb64.time < INT32_MIN || tb64.time > INT32_MAX) {
29 errno = EOVERFLOW;
30 return -1;
31 }
32 return 0;
33}
34
35/* On 32-bit systems is ftime ABI using 32-bit time_t */
36#ifndef _WIN64
37int __attribute__ ((alias("ftime32"))) __cdecl ftime(struct timeb *);
38#endif