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