1 /* Copyright (C) 2014 InfiniDB, Inc.
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License
5    as published by the Free Software Foundation; version 2 of
6    the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16    MA 02110-1301, USA. */
17 
18 #include <time.h>
19 #include <string>
20 #include <sys/types.h>
21 #include <map>
22 #include <boost/filesystem/operations.hpp>
23 #include <boost/filesystem/path.hpp>
24 namespace fs = boost::filesystem;
25 
26 using namespace std;
27 
28 #include "unistd.h"
29 #include "stdint.h"
30 #include "sys/time.h"
31 #include "syslog.h"
32 #include "idbregistry.h"
33 #include "WinSyslog.h"
34 #include "io.h"
35 //This is the number of msecs between 1601 and 1970 (+/-)
36 //#define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
37 //This is the number of 100-nsec intvls btwn 1601 and 1970 (+/-)
38 const unsigned __int64 EPOCH_DELTA = ((1970Ui64 - 1601Ui64) * 365Ui64 +
39                                       ((1970Ui64 - 1601Ui64) / 4Ui64) -
40                                       ((1970Ui64 - 1601Ui64) / 100Ui64) +
41                                       ((1970Ui64 - 1601Ui64) / 400Ui64)) *
42                                      86400Ui64 * 1000Ui64 * 1000Ui64 * 10Ui64;
43 
44 // returns the secs+usecs since the epoch
gettimeofday(struct timeval * tvp,struct timezone * tzp)45 int gettimeofday(struct timeval* tvp, struct timezone* tzp)
46 {
47     FILETIME ft;
48     unsigned __int64 tmpres = 0;
49     static int tzflag;
50 
51     if (0 != tvp)
52     {
53         //returns the current time as the number of 100-nanosecond intervals since January 1, 1601 (UTC)
54         GetSystemTimeAsFileTime(&ft);
55 
56         tmpres |= ft.dwHighDateTime;
57         tmpres <<= 32;
58         tmpres |= ft.dwLowDateTime;
59 
60         /*converting file time to unix epoch*/
61         tmpres -= EPOCH_DELTA;
62         tmpres /= 10;  /*convert into microseconds*/
63         tvp->tv_sec = (long)(tmpres / 1000000UL);
64         tvp->tv_usec = (long)(tmpres % 1000000UL);
65     }
66 
67     if (0 != tzp)
68     {
69         if (!tzflag)
70         {
71             _tzset();
72             tzflag++;
73         }
74 
75         tzp->tz_minuteswest = _timezone / 60;
76         tzp->tz_dsttime = _daylight;
77     }
78 
79     return 0;
80 }
81 
closelog(...)82 int closelog(...)
83 {
84     return 0;
85 }
86 
openlog(...)87 int openlog(...)
88 {
89     return 0;
90 }
91 
syslog(int priority,const char * format,...)92 int syslog(int priority, const char* format, ...)
93 {
94     int rtn;
95     va_list args;
96     va_start(args, format);
97     rtn = WinSyslog::instance()->Log(priority, format, args);
98     va_end(args);
99     return rtn;
100 }
101 
fcntl(int i1,int i2,...)102 int fcntl(int i1, int i2, ...)
103 {
104     return 0;
105 }
106 
inet_aton(const char * c,struct in_addr * p)107 int inet_aton(const char* c, struct in_addr* p)
108 {
109     p->S_un.S_addr = inet_addr(c);
110     return 1;
111 }
112 
flock(int i1,int i2)113 int flock(int i1, int i2)
114 {
115     return 0;
116 }
117 
usleep(unsigned int usecs)118 int usleep(unsigned int usecs)
119 {
120     unsigned int msecs;
121     //cvt usecs to msecs
122     msecs = usecs / 1000;
123 
124     if (msecs == 0) msecs++;
125 
126     Sleep(msecs);
127     return 0;
128 }
129 
fork()130 int fork()
131 {
132     return -1;
133 }
134 
getpagesize()135 int getpagesize()
136 {
137     return 4096;
138 }
139 
idb_localtime_r(const time_t * tp,struct tm * tmp)140 struct tm* idb_localtime_r(const time_t* tp, struct tm* tmp)
141 {
142     time_t t = *tp;
143     errno_t p = 0;
144     p = localtime_s(tmp, &t);
145 
146     if (p != 0)
147         memset(tmp, 0, sizeof(struct tm));
148 
149     return tmp;
150 }
151 
152 //FIXME: need a better impl!
clock_gettime(clockid_t,struct timespec * tp)153 long clock_gettime(clockid_t, struct timespec* tp)
154 {
155     SYSTEMTIME st;
156     GetSystemTime(&st);
157     tp->tv_sec = st.wHour * 3600 + st.wMinute * 60 + st.wSecond;
158     tp->tv_nsec = st.wMilliseconds * 1000000;
159     return 0;
160 }
161 
162 #if _MSC_VER < 1600
lldiv(const long long numer,const long long denom)163 lldiv_t lldiv(const long long numer, const long long denom)
164 {
165     lldiv_t ret;
166     ret.quot = numer / denom;
167     ret.rem = numer % denom;
168     return ret;
169 }
170 #endif
171 
sleep(unsigned int secs)172 unsigned int sleep(unsigned int secs)
173 {
174     Sleep(secs * 1000);
175     return 0;
176 }
pipe(int fds[2])177 int pipe(int fds[2])
178 {
179     return -1;
180 }
181 
getppid()182 pid_t getppid()
183 {
184     return -1;
185 }
186 
waitpid(pid_t,int *,int)187 pid_t waitpid(pid_t, int*, int)
188 {
189     return -1;
190 }
191 
kill(pid_t,int)192 int kill(pid_t, int)
193 {
194     return -1;
195 }
196 
setuid(uid_t)197 int setuid(uid_t)
198 {
199     return -1;
200 }
201 
IDBSysErrorStr(DWORD err)202 string IDBSysErrorStr(DWORD err)
203 {
204     // Retrieve the system error message for the last-error code
205 
206     string errstr;
207 
208     LPVOID lpMsgBuf;
209 
210     FormatMessage(
211         FORMAT_MESSAGE_ALLOCATE_BUFFER |
212         FORMAT_MESSAGE_FROM_SYSTEM |
213         FORMAT_MESSAGE_IGNORE_INSERTS,
214         NULL,
215         err,
216         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
217         (LPTSTR) &lpMsgBuf,
218         0, NULL );
219 
220     // Display the error message and exit the process
221 
222     errstr = (LPCTSTR)lpMsgBuf;
223 
224     LocalFree(lpMsgBuf);
225 
226     return errstr;
227 }
228