summaryrefslogtreecommitdiff
path: root/legacy/evil/src/lib/evil_unistd.c
blob: 32c233560ecb1cfbbbb10e8a4a50ea4a96009b97 (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif /* HAVE_ERRNO_H */

#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#undef WIN32_LEAN_AND_MEAN

# include <sys/time.h>

#ifdef _MSC_VER
# include <direct.h>   /* for _getcwd */
#endif

#include "Evil.h"
#include "evil_private.h"


LONGLONG _evil_time_freq;
LONGLONG _evil_time_count;
long     _evil_time_second;


long
_evil_systemtime_to_time(SYSTEMTIME st)
{
   int days[] = {
     -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364
   };
   int day;
   time_t time;

   st.wYear -= 1900;
   if ((st.wYear < 70) || (st.wYear > 138))
     return -1;

   day = st.wDay + days[st.wMonth - 1];

  if (!(st.wYear & 3) && (st.wMonth > 2) )
    day++;

  time = ((st.wYear - 70) * 365 + ((st.wYear - 1) >> 2) - 17 + day) * 24 + st.wHour;
  time = (time * 60 + st.wMinute) * 60 + st.wSecond;

  return (long)time;
}

/*
 * Time related functions
 *
 */

double
evil_time_get()
{
   LARGE_INTEGER count;

   QueryPerformanceCounter(&count);

   return (double)_evil_time_second + (double)(count.QuadPart - _evil_time_count)/ (double)_evil_time_freq;
}

int
evil_gettimeofday(struct timeval *tp, void *tzp __UNUSED__)
{
   LARGE_INTEGER count;
   LONGLONG      diff;

   QueryPerformanceCounter(&count);
   diff = count.QuadPart - _evil_time_count;
   tp->tv_sec = _evil_time_second + diff / _evil_time_freq;
   tp->tv_usec = (diff % _evil_time_freq) * 1000000000ll;

   return 1;
}


/*
 * Process identifer related functions
 *
 */

pid_t
getpid(void)
{
  return (pid_t)GetCurrentProcessId();
}

/*
 * File related functions
 *
 */

char *
evil_getcwd(char *buffer, size_t size)
{
#ifdef _WIN32_WCE
   wchar_t wpath[PATH_MAX];
   char   *cpath;
   char   *delim;
   DWORD   ret = 0;

   if (size <= 0)
     return NULL;

   ret = GetModuleFileName(GetModuleHandle(NULL), (LPWSTR)&wpath, PATH_MAX);

   if (!ret)
     {
        _evil_error_display(__FUNCTION__, ret);
        return NULL;
     }

   cpath = evil_wchar_to_char(wpath);
   if (!cpath)
     return NULL;

   if (strlen(cpath) >= (size - 1))
     {
        free(cpath);
        return NULL;
     }

   delim = strrchr(cpath, '\\');
   if (delim)
     *delim = '\0';

   if (!buffer)
     {
        buffer = (char *)malloc(sizeof(char) * size);
        if (!buffer)
          {
             free(cpath);
             return NULL;
          }
     }

   strcpy(buffer, cpath);
   free(cpath);

   return buffer;
#else
   return _getcwd(buffer, (int)size);
#endif /* ! _WIN32_WCE */
}

#if defined (_WIN32_WCE) && ! defined (__CEGCC__)

int
evil_stat(const char *file_name, struct stat *st)
{
   SYSTEMTIME      system_time;
   FILETIME        local_time;
   WIN32_FIND_DATA data;
   HANDLE          handle;
   char           *f;
   char           *tmp;
   wchar_t        *file;
   int             permission = 0;

   if (!file_name || !*file_name)
     return -1;

   f = strdup(file_name);
   if (!f)
     return -1;

   tmp = f;
   while (*tmp)
     {
        if (*tmp == '/') *tmp = '\\';
        tmp++;
     }

   if (!strcmp(file_name, "\\"))
     {
        st->st_size = 1024;
        st->st_mode = S_IFDIR;
        permission = S_IREAD|S_IWRITE|S_IEXEC;

        st->st_mode |= permission | (permission >> 3) | (permission >> 6);
        return 0;
     }

   if (*f != '\\')
     {
        char  buf[PATH_MAX];
        char *tmp;
        int   l1;
        int   l2;

        evil_getcwd(buf, PATH_MAX);
        l1 = strlen(buf);
        l2 = strlen(file_name);
        tmp = (char *)malloc(l1 + 1 + l2 + 1);
        if (!tmp)
          return -1;
        memcpy(tmp, buf, l1);
        tmp[l1] = '\\';
        memcpy(tmp + l1 + 1, file_name, l2);
        tmp[l1 + 1 + l2] = '\0';
        file = evil_char_to_wchar(tmp);
        free(tmp);
        if (!file)
          return -1;
     }
   else
     {
        file = evil_char_to_wchar(f);
        if (!file)
          return -1;
     }

   free(f);

   handle = FindFirstFile(file, &data);
   if (handle == INVALID_HANDLE_VALUE)
     {
        free(file);
        return -1;
     }

   free(file);

   if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
     {
        st->st_size = 1024;
        st->st_mode = S_IFDIR;
        st->st_nlink = 2;
     }
   else
     {
        st->st_size = data.nFileSizeLow;
        st->st_mode = S_IFREG;
        st->st_nlink = 1;
     }

   permission |= S_IREAD;

   if (!(data.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
     permission |= S_IWRITE;

   if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
     permission |= S_IEXEC;

   st->st_mode |= permission | (permission >> 3) | (permission >> 6);

   FileTimeToLocalFileTime(&data.ftLastWriteTime, &local_time);
   FileTimeToSystemTime(&local_time, &system_time);

   st->st_mtime = _evil_systemtime_to_time(system_time);

   FileTimeToLocalFileTime(&data.ftCreationTime, &local_time);
   FileTimeToSystemTime(&local_time, &system_time);

   st->st_ctime = _evil_systemtime_to_time(system_time);

   FileTimeToLocalFileTime(&data.ftLastAccessTime, &local_time);
   FileTimeToSystemTime(&local_time, &system_time);

   st->st_atime = _evil_systemtime_to_time(system_time);

   if(st->st_atime == 0)
     st->st_atime = st->st_mtime;
   if (st->st_ctime == 0)
     st->st_ctime = st->st_mtime;

   st->st_rdev = 1;
   st->st_ino = 0;

   FindClose(handle);

  return 0;
}

#endif /* _WIN32_WCE && ! __CEGCC__ */



/*
 * Sockets and pipe related functions
 *
 */

int
evil_sockets_init(void)
{
   WSADATA wsa_data;

   return (WSAStartup(MAKEWORD(2, 2), &wsa_data) == 0) ? 1 : 0;
}

void
evil_sockets_shutdown(void)
{
   WSACleanup();
}

/*
 * The code of the following functions has been kindly offered
 * by Tor Lillqvist.
 */
int
evil_pipe(int *fds)
{
   struct sockaddr_in saddr;
   struct timeval     tv;
   SOCKET             temp;
   SOCKET             socket1 = INVALID_SOCKET;
   SOCKET             socket2 = INVALID_SOCKET;
   u_long             arg;
   fd_set             read_set;
   fd_set             write_set;
   int                len;

   temp = socket (AF_INET, SOCK_STREAM, 0);

   if (temp == INVALID_SOCKET)
     goto out0;

   arg = 1;
   if (ioctlsocket (temp, FIONBIO, &arg) == SOCKET_ERROR)
     goto out0;

   memset (&saddr, 0, sizeof (saddr));
   saddr.sin_family = AF_INET;
   saddr.sin_port = 0;
   saddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);

   if (bind (temp, (struct sockaddr *)&saddr, sizeof (saddr)))
     goto out0;

   if (listen (temp, 1) == SOCKET_ERROR)
     goto out0;

   len = sizeof (saddr);
   if (getsockname (temp, (struct sockaddr *)&saddr, &len))
     goto out0;

   socket1 = socket (AF_INET, SOCK_STREAM, 0);

   if (socket1 == INVALID_SOCKET)
     goto out0;

   arg = 1;
   if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
      goto out1;

   if ((connect (socket1, (struct sockaddr  *)&saddr, len) == SOCKET_ERROR) &&
       (WSAGetLastError () != WSAEWOULDBLOCK))
     goto out1;

   FD_ZERO (&read_set);
   FD_SET (temp, &read_set);

   tv.tv_sec = 0;
   tv.tv_usec = 0;

   if (select (0, &read_set, NULL, NULL, NULL) == SOCKET_ERROR)
     goto out1;

   if (!FD_ISSET (temp, &read_set))
     goto out1;

   socket2 = accept (temp, (struct sockaddr *) &saddr, &len);
   if (socket2 == INVALID_SOCKET)
     goto out1;

   FD_ZERO (&write_set);
   FD_SET (socket1, &write_set);

   tv.tv_sec = 0;
   tv.tv_usec = 0;

   if (select (0, NULL, &write_set, NULL, NULL) == SOCKET_ERROR)
     goto out2;

   if (!FD_ISSET (socket1, &write_set))
     goto out2;

   arg = 0;
   if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
     goto out2;

   arg = 0;
   if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR)
     goto out2;

   fds[0] = socket1;
   fds[1] = socket2;

   closesocket (temp);

   return 0;

 out2:
   closesocket (socket2);
 out1:
   closesocket (socket1);
 out0:
   closesocket (temp);

   fds[0] = INVALID_SOCKET;
   fds[1] = INVALID_SOCKET;

   return -1;
}


/*
 * Exec related functions
 *
 */

#if defined (_WIN32_WCE) && ! defined (__CEGCC__)

int execvp (const char *file __UNUSED__, char *const argv[] __UNUSED__)
{
   return 1;
}

#endif /* _WIN32_WCE && ! __CEGCC__ */