1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/*
* Copyright (C) 2005 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Ports of standard functions that don't exist on a specific platform.
//
// Note these are NOT in the "android" namespace.
//
#include <utils/ported.h>
#if defined(NEED_GETTIMEOFDAY) || defined(NEED_USLEEP)
# include <sys/time.h>
# include <windows.h>
#endif
#if defined(NEED_GETTIMEOFDAY)
/*
* Replacement gettimeofday() for Windows environments (primarily MinGW).
*
* Ignores "tz".
*/
int gettimeofday(struct timeval* ptv, struct timezone* tz)
{
long long nsTime; // time in 100ns units since Jan 1 1601
FILETIME ft;
if (tz != NULL) {
// oh well
}
::GetSystemTimeAsFileTime(&ft);
nsTime = (long long) ft.dwHighDateTime << 32 |
(long long) ft.dwLowDateTime;
// convert to time in usec since Jan 1 1970
ptv->tv_usec = (long) ((nsTime / 10LL) % 1000000LL);
ptv->tv_sec = (long) ((nsTime - 116444736000000000LL) / 10000000LL);
return 0;
}
#endif
#if defined(NEED_USLEEP)
//
// Replacement usleep for Windows environments (primarily MinGW).
//
void usleep(unsigned long usec)
{
// Win32 API function Sleep() takes milliseconds
::Sleep((usec + 500) / 1000);
}
#endif
#if 0 //defined(NEED_PIPE)
//
// Replacement pipe() command for MinGW
//
// The _O_NOINHERIT flag sets bInheritHandle to FALSE in the
// SecurityAttributes argument to CreatePipe(). This means the handles
// aren't inherited when a new process is created. The examples I've seen
// use it, possibly because there's a lot of junk going on behind the
// scenes. (I'm assuming "process" and "thread" are different here, so
// we should be okay spinning up a thread.) The recommended practice is
// to dup() the descriptor you want the child to have.
//
// It appears that unnamed pipes can't do non-blocking ("overlapped") I/O.
// You can't use select() either, since that only works on sockets. The
// Windows API calls that are useful here all operate on a HANDLE, not
// an integer file descriptor, and I don't think you can get there from
// here. The "named pipe" stuff is insane.
//
int pipe(int filedes[2])
{
return _pipe(filedes, 0, _O_BINARY | _O_NOINHERIT);
}
#endif
#if defined(NEED_SETENV)
/*
* MinGW lacks these. For now, just stub them out so the code compiles.
*/
int setenv(const char* name, const char* value, int overwrite)
{
return 0;
}
void unsetenv(const char* name)
{
}
char* getenv(const char* name)
{
return NULL;
}
#endif
|