VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/mscfakes.c@ 3219

Last change on this file since 3219 was 3219, checked in by bird, 6 years ago

kmkbuiltin: Added KMK_OPEN_NO_INHERIT to all open calls.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.5 KB
Line 
1/* $Id: mscfakes.c 3219 2018-03-30 22:30:15Z bird $ */
2/** @file
3 * Fake Unix stuff for MSC.
4 */
5
6/*
7 * Copyright (c) 2005-2015 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
8 *
9 * This file is part of kBuild.
10 *
11 * kBuild is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * kBuild is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kBuild. If not, see <http://www.gnu.org/licenses/>
23 *
24 */
25
26/*******************************************************************************
27* Header Files *
28*******************************************************************************/
29#include "config.h"
30#include <assert.h>
31#include <stdarg.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <errno.h>
36#include <io.h>
37#include <fcntl.h>
38#include <sys/stat.h>
39#include <sys/timeb.h>
40#include "err.h"
41#include "mscfakes.h"
42
43#include "nt/ntutimes.h"
44#undef utimes
45#undef lutimes
46
47#include "console.h"
48
49
50
51/*******************************************************************************
52* Internal Functions *
53*******************************************************************************/
54static BOOL isPipeFd(int fd);
55
56
57/**
58 * Makes corrections to a directory path that ends with a trailing slash.
59 *
60 * @returns temporary buffer to free.
61 * @param ppszPath The path pointer. This is updated when necessary.
62 * @param pfMustBeDir This is set if it must be a directory, otherwise it's cleared.
63 */
64static char *
65msc_fix_path(const char **ppszPath, int *pfMustBeDir)
66{
67 const char *pszPath = *ppszPath;
68 const char *psz;
69 char *pszNew;
70 *pfMustBeDir = 0;
71
72 /*
73 * Skip any compusory trailing slashes
74 */
75 if (pszPath[0] == '/' || pszPath[0] == '\\')
76 {
77 if ( (pszPath[1] == '/' || pszPath[1] == '\\')
78 && pszPath[2] != '/'
79 && pszPath[2] != '\\')
80 /* unc */
81 pszPath += 2;
82 else
83 /* root slash(es) */
84 pszPath++;
85 }
86 else if ( isalpha(pszPath[0])
87 && pszPath[1] == ':')
88 {
89 if (pszPath[2] == '/' || pszPath[2] == '\\')
90 /* drive w/ slash */
91 pszPath += 3;
92 else
93 /* drive relative path. */
94 pszPath += 2;
95 }
96 /* else: relative path, no skipping necessary. */
97
98 /*
99 * Any trailing slashes to drop off?
100 */
101 psz = strchr(pszPath, '\0');
102 if (pszPath <= psz)
103 return NULL;
104 if ( psz[-1] != '/'
105 || psz[-1] != '\\')
106 return NULL;
107
108 /* figure how many, make a copy and strip them off. */
109 while ( psz > pszPath
110 && ( psz[-1] == '/'
111 || psz[-1] == '\\'))
112 psz--;
113 pszNew = strdup(pszPath);
114 pszNew[psz - pszPath] = '\0';
115
116 *pfMustBeDir = 1;
117 *ppszPath = pszNew; /* use this one */
118 return pszNew;
119}
120
121
122int
123birdSetErrno(unsigned dwErr)
124{
125 switch (dwErr)
126 {
127 default:
128 case ERROR_INVALID_FUNCTION: errno = EINVAL; break;
129 case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
130 case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
131 case ERROR_TOO_MANY_OPEN_FILES: errno = EMFILE; break;
132 case ERROR_ACCESS_DENIED: errno = EACCES; break;
133 case ERROR_INVALID_HANDLE: errno = EBADF; break;
134 case ERROR_ARENA_TRASHED: errno = ENOMEM; break;
135 case ERROR_NOT_ENOUGH_MEMORY: errno = ENOMEM; break;
136 case ERROR_INVALID_BLOCK: errno = ENOMEM; break;
137 case ERROR_BAD_ENVIRONMENT: errno = E2BIG; break;
138 case ERROR_BAD_FORMAT: errno = ENOEXEC; break;
139 case ERROR_INVALID_ACCESS: errno = EINVAL; break;
140 case ERROR_INVALID_DATA: errno = EINVAL; break;
141 case ERROR_INVALID_DRIVE: errno = ENOENT; break;
142 case ERROR_CURRENT_DIRECTORY: errno = EACCES; break;
143 case ERROR_NOT_SAME_DEVICE: errno = EXDEV; break;
144 case ERROR_NO_MORE_FILES: errno = ENOENT; break;
145 case ERROR_LOCK_VIOLATION: errno = EACCES; break;
146 case ERROR_BAD_NETPATH: errno = ENOENT; break;
147 case ERROR_NETWORK_ACCESS_DENIED: errno = EACCES; break;
148 case ERROR_BAD_NET_NAME: errno = ENOENT; break;
149 case ERROR_FILE_EXISTS: errno = EEXIST; break;
150 case ERROR_CANNOT_MAKE: errno = EACCES; break;
151 case ERROR_FAIL_I24: errno = EACCES; break;
152 case ERROR_INVALID_PARAMETER: errno = EINVAL; break;
153 case ERROR_NO_PROC_SLOTS: errno = EAGAIN; break;
154 case ERROR_DRIVE_LOCKED: errno = EACCES; break;
155 case ERROR_BROKEN_PIPE: errno = EPIPE; break;
156 case ERROR_DISK_FULL: errno = ENOSPC; break;
157 case ERROR_INVALID_TARGET_HANDLE: errno = EBADF; break;
158 case ERROR_WAIT_NO_CHILDREN: errno = ECHILD; break;
159 case ERROR_CHILD_NOT_COMPLETE: errno = ECHILD; break;
160 case ERROR_DIRECT_ACCESS_HANDLE: errno = EBADF; break;
161 case ERROR_NEGATIVE_SEEK: errno = EINVAL; break;
162 case ERROR_SEEK_ON_DEVICE: errno = EACCES; break;
163 case ERROR_DIR_NOT_EMPTY: errno = ENOTEMPTY; break;
164 case ERROR_NOT_LOCKED: errno = EACCES; break;
165 case ERROR_BAD_PATHNAME: errno = ENOENT; break;
166 case ERROR_MAX_THRDS_REACHED: errno = EAGAIN; break;
167 case ERROR_LOCK_FAILED: errno = EACCES; break;
168 case ERROR_ALREADY_EXISTS: errno = EEXIST; break;
169 case ERROR_FILENAME_EXCED_RANGE: errno = ENOENT; break;
170 case ERROR_NESTING_NOT_ALLOWED: errno = EAGAIN; break;
171#ifdef EMLINK
172 case ERROR_TOO_MANY_LINKS: errno = EMLINK; break;
173#endif
174 }
175
176 return -1;
177}
178
179char *dirname(char *path)
180{
181 /** @todo later */
182 return path;
183}
184
185
186int lchmod(const char *pszPath, mode_t mode)
187{
188 int rc = 0;
189 int fMustBeDir;
190 char *pszPathFree = msc_fix_path(&pszPath, &fMustBeDir);
191
192 /*
193 * Get the current attributes
194 */
195 DWORD fAttr = GetFileAttributes(pszPath);
196 if (fAttr == INVALID_FILE_ATTRIBUTES)
197 rc = birdSetErrno(GetLastError());
198 else if (fMustBeDir & !(fAttr & FILE_ATTRIBUTE_DIRECTORY))
199 {
200 errno = ENOTDIR;
201 rc = -1;
202 }
203 else
204 {
205 /*
206 * Modify the attributes and try set them.
207 */
208 if (mode & _S_IWRITE)
209 fAttr &= ~FILE_ATTRIBUTE_READONLY;
210 else
211 fAttr |= FILE_ATTRIBUTE_READONLY;
212 if (!SetFileAttributes(pszPath, fAttr))
213 rc = birdSetErrno(GetLastError());
214 }
215
216 if (pszPathFree)
217 {
218 int saved_errno = errno;
219 free(pszPathFree);
220 errno = saved_errno;
221 }
222 return rc;
223}
224
225
226int msc_chmod(const char *pszPath, mode_t mode)
227{
228 int rc = 0;
229 int fMustBeDir;
230 char *pszPathFree = msc_fix_path(&pszPath, &fMustBeDir);
231
232 /*
233 * Get the current attributes.
234 */
235 DWORD fAttr = GetFileAttributes(pszPath);
236 if (fAttr == INVALID_FILE_ATTRIBUTES)
237 rc = birdSetErrno(GetLastError());
238 else if (fMustBeDir & !(fAttr & FILE_ATTRIBUTE_DIRECTORY))
239 {
240 errno = ENOTDIR;
241 rc = -1;
242 }
243 else if (fAttr & FILE_ATTRIBUTE_REPARSE_POINT)
244 {
245 errno = ENOSYS; /** @todo resolve symbolic link / rewrite to NtSetInformationFile. */
246 rc = -1;
247 }
248 else
249 {
250 /*
251 * Modify the attributes and try set them.
252 */
253 if (mode & _S_IWRITE)
254 fAttr &= ~FILE_ATTRIBUTE_READONLY;
255 else
256 fAttr |= FILE_ATTRIBUTE_READONLY;
257 if (!SetFileAttributes(pszPath, fAttr))
258 rc = birdSetErrno(GetLastError());
259 }
260
261 if (pszPathFree)
262 {
263 int saved_errno = errno;
264 free(pszPathFree);
265 errno = saved_errno;
266 }
267 return rc;
268}
269
270
271typedef BOOL (WINAPI *PFNCREATEHARDLINKA)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
272int link(const char *pszDst, const char *pszLink)
273{
274 static PFNCREATEHARDLINKA s_pfnCreateHardLinkA = NULL;
275 static int s_fTried = FALSE;
276
277 /* The API was introduced in Windows 2000, so resolve it dynamically. */
278 if (!s_pfnCreateHardLinkA)
279 {
280 if (!s_fTried)
281 {
282 HMODULE hmod = LoadLibrary("KERNEL32.DLL");
283 if (hmod)
284 *(FARPROC *)&s_pfnCreateHardLinkA = GetProcAddress(hmod, "CreateHardLinkA");
285 s_fTried = TRUE;
286 }
287 if (!s_pfnCreateHardLinkA)
288 {
289 errno = ENOSYS;
290 return -1;
291 }
292 }
293
294 if (s_pfnCreateHardLinkA(pszLink, pszDst, NULL))
295 return 0;
296 return birdSetErrno(GetLastError());
297}
298
299
300int mkdir_msc(const char *path, mode_t mode)
301{
302 int rc = (mkdir)(path);
303 if (rc)
304 {
305 size_t len = strlen(path);
306 if (len > 0 && (path[len - 1] == '/' || path[len - 1] == '\\'))
307 {
308 char *str = strdup(path);
309 while (len > 0 && (str[len - 1] == '/' || str[len - 1] == '\\'))
310 str[--len] = '\0';
311 rc = (mkdir)(str);
312 free(str);
313 }
314 }
315 return rc;
316}
317
318int rmdir_msc(const char *path)
319{
320 int rc = (rmdir)(path);
321 if (rc)
322 {
323 size_t len = strlen(path);
324 if (len > 0 && (path[len - 1] == '/' || path[len - 1] == '\\'))
325 {
326 char *str = strdup(path);
327 while (len > 0 && (str[len - 1] == '/' || str[len - 1] == '\\'))
328 str[--len] = '\0';
329 rc = (rmdir)(str);
330 free(str);
331 }
332 }
333 return rc;
334}
335
336
337static int doname(char *pszX, char *pszEnd)
338{
339 static char s_szChars[] = "Xabcdefghijklmnopqrstuwvxyz1234567890";
340 int rc = 0;
341 do
342 {
343 char ch;
344
345 pszEnd++;
346 ch = *(strchr(s_szChars, *pszEnd) + 1);
347 if (ch)
348 {
349 *pszEnd = ch;
350 return 0;
351 }
352 *pszEnd = 'a';
353 } while (pszEnd != pszX);
354 return 1;
355}
356
357
358int mkstemp(char *temp)
359{
360 char *pszX = strchr(temp, 'X');
361 char *pszEnd = strchr(pszX, '\0');
362 int cTries = 1000;
363 while (--cTries > 0)
364 {
365 int fd;
366 if (doname(pszX, pszEnd))
367 return -1;
368 fd = open(temp, _O_EXCL | _O_CREAT | _O_BINARY | _O_RDWR | KMK_OPEN_NO_INHERIT, 0777);
369 if (fd >= 0)
370 return fd;
371 }
372 return -1;
373}
374
375
376/** Unix to DOS. */
377static char *fix_slashes(char *psz)
378{
379 char *pszRet = psz;
380 for (; *psz; psz++)
381 if (*psz == '/')
382 *psz = '\\';
383 return pszRet;
384}
385
386
387/** Calcs the SYMBOLIC_LINK_FLAG_DIRECTORY flag for CreatesymbolcLink. */
388static DWORD is_directory(const char *pszPath, const char *pszRelativeTo)
389{
390 size_t cchPath = strlen(pszPath);
391 struct stat st;
392 if (cchPath > 0 && pszPath[cchPath - 1] == '\\' || pszPath[cchPath - 1] == '/')
393 return 1; /* SYMBOLIC_LINK_FLAG_DIRECTORY */
394
395 if (stat(pszPath, &st))
396 {
397 size_t cchRelativeTo = strlen(pszRelativeTo);
398 char *psz = malloc(cchPath + cchRelativeTo + 4);
399 memcpy(psz, pszRelativeTo, cchRelativeTo);
400 memcpy(psz + cchRelativeTo, "\\", 1);
401 memcpy(psz + cchRelativeTo + 1, pszPath, cchPath + 1);
402 if (stat(pszPath, &st))
403 st.st_mode = _S_IFREG;
404 free(psz);
405 }
406
407 return (st.st_mode & _S_IFMT) == _S_IFDIR ? 1 : 0;
408}
409
410
411int symlink(const char *pszDst, const char *pszLink)
412{
413 static BOOLEAN (WINAPI *s_pfnCreateSymbolicLinkA)(LPCSTR, LPCSTR, DWORD) = 0;
414 static BOOL s_fTried = FALSE;
415
416 if (!s_fTried)
417 {
418 HMODULE hmod = LoadLibrary("KERNEL32.DLL");
419 if (hmod)
420 *(FARPROC *)&s_pfnCreateSymbolicLinkA = GetProcAddress(hmod, "CreateSymbolicLinkA");
421 s_fTried = TRUE;
422 }
423
424 if (s_pfnCreateSymbolicLinkA)
425 {
426 char *pszDstCopy = fix_slashes(strdup(pszDst));
427 char *pszLinkCopy = fix_slashes(strdup(pszLink));
428 BOOLEAN fRc = s_pfnCreateSymbolicLinkA(pszLinkCopy, pszDstCopy,
429 is_directory(pszDstCopy, pszLinkCopy));
430 DWORD err = GetLastError();
431 free(pszDstCopy);
432 free(pszLinkCopy);
433 if (fRc)
434 return 0;
435 switch (err)
436 {
437 case ERROR_NOT_SUPPORTED: errno = ENOSYS; break;
438 case ERROR_ALREADY_EXISTS:
439 case ERROR_FILE_EXISTS: errno = EEXIST; break;
440 case ERROR_DIRECTORY: errno = ENOTDIR; break;
441 case ERROR_ACCESS_DENIED:
442 case ERROR_PRIVILEGE_NOT_HELD: errno = EPERM; break;
443 default: errno = EINVAL; break;
444 }
445 return -1;
446 }
447
448 fprintf(stderr, "warning: symlink() is available on this version of Windows!\n");
449 errno = ENOSYS;
450 return -1;
451}
452
453
454#if _MSC_VER < 1400
455int snprintf(char *buf, size_t size, const char *fmt, ...)
456{
457 int cch;
458 va_list args;
459 va_start(args, fmt);
460 cch = vsprintf(buf, fmt, args);
461 va_end(args);
462 return cch;
463}
464#endif
465
466
467/* We override the libc write function (in our modules only, unfortunately) so
468 we can kludge our way around a ENOSPC problem observed on build servers
469 capturing STDOUT and STDERR via pipes. Apparently this may happen when the
470 pipe buffer is full, even with the mscfake_init hack in place.
471
472 XXX: Probably need to hook into fwrite as well. */
473ssize_t msc_write(int fd, const void *pvSrc, size_t cbSrc)
474{
475#define MSC_WRITE_MAX_CHUNK (UINT_MAX / 32)
476 ssize_t cbRet;
477 if (cbSrc <= MSC_WRITE_MAX_CHUNK)
478 {
479 /* Console output optimization: */
480 if (cbSrc > 0 && is_console(fd))
481 return maybe_con_write(fd, pvSrc, cbSrc);
482
483#ifndef MSC_WRITE_TEST
484 cbRet = _write(fd, pvSrc, (unsigned int)cbSrc);
485#else
486 cbRet = -1; errno = ENOSPC;
487#endif
488 if (cbRet < 0)
489 {
490 /* ENOSPC on pipe kludge. */
491 unsigned int cbLimit;
492 int cSinceLastSuccess;
493
494 if (cbSrc == 0)
495 return 0;
496 if (errno != ENOSPC)
497 return -1;
498#ifndef MSC_WRITE_TEST
499 if (!isPipeFd(fd))
500 {
501 errno = ENOSPC;
502 return -1;
503 }
504#endif
505
506 /* Likely a full pipe buffer, try write smaller amounts and do some
507 sleeping inbetween each unsuccessful one. */
508 cbLimit = (unsigned)(cbSrc / 4);
509 if (cbLimit < 4)
510 cbLimit = 4;
511 else if (cbLimit > 512)
512 cbLimit = 512;
513 cSinceLastSuccess = 0;
514 cbRet = 0;
515#ifdef MSC_WRITE_TEST
516 cbLimit = 4;
517#endif
518
519 while ((ssize_t)cbSrc > 0)
520 {
521 unsigned int cbAttempt = cbSrc > cbLimit ? cbLimit : (unsigned int)cbSrc;
522 ssize_t cbActual = _write(fd, pvSrc, cbAttempt);
523 if (cbActual > 0)
524 {
525 /* For some reason, it seems like we cannot trust _write to return
526 a number that's less or equal to the number of bytes we passed
527 in to the call. (Also reason for signed check in loop.) */
528 if (cbActual > cbAttempt)
529 cbActual = cbAttempt;
530
531 pvSrc = (char *)pvSrc + cbActual;
532 cbSrc -= cbActual;
533 cbRet += cbActual;
534#ifndef MSC_WRITE_TEST
535 if (cbLimit < 32)
536 cbLimit = 32;
537#endif
538 cSinceLastSuccess = 0;
539 }
540 else if (errno != ENOSPC)
541 return -1;
542 else
543 {
544 /* Delay for about 30 seconds, then just give up. */
545 cSinceLastSuccess++;
546 if (cSinceLastSuccess > 1860)
547 return -1;
548 if (cSinceLastSuccess <= 2)
549 Sleep(0);
550 else if (cSinceLastSuccess <= 66)
551 {
552 if (cbLimit >= 8)
553 cbLimit /= 2; /* Just in case the pipe buffer is very very small. */
554 Sleep(1);
555 }
556 else
557 Sleep(16);
558 }
559 }
560 }
561 }
562 else
563 {
564 /*
565 * Type limit exceeded. Split the job up.
566 */
567 cbRet = 0;
568 while (cbSrc > 0)
569 {
570 size_t cbToWrite = cbSrc > MSC_WRITE_MAX_CHUNK ? MSC_WRITE_MAX_CHUNK : cbSrc;
571 ssize_t cbWritten = msc_write(fd, pvSrc, cbToWrite);
572 if (cbWritten > 0)
573 {
574 pvSrc = (char *)pvSrc + (size_t)cbWritten;
575 cbSrc -= (size_t)cbWritten;
576 cbRet += (size_t)cbWritten;
577 }
578 else if (cbWritten == 0 || cbRet > 0)
579 break;
580 else
581 return -1;
582 }
583 }
584 return cbRet;
585}
586
587ssize_t writev(int fd, const struct iovec *vector, int count)
588{
589 ssize_t size = 0;
590 if (count > 0)
591 {
592 int i;
593
594 /* To get consistent console output, we must try combine the segments
595 when outputing to the console. */
596 if (count > 1 && is_console(fd))
597 {
598 char *pbTmp;
599 ssize_t cbTotal;
600 if (count == 1)
601 return maybe_con_write(fd, vector[0].iov_base, (int)vector[0].iov_len);
602
603 cbTotal = 0;
604 for (i = 0; i < count; i++)
605 cbTotal += vector[i].iov_len;
606 pbTmp = malloc(cbTotal);
607 if (pbTmp)
608 {
609 char *pbCur = pbTmp;
610 for (i = 0; i < count; i++)
611 {
612 memcpy(pbCur, vector[i].iov_base, vector[i].iov_len);
613 pbCur += vector[i].iov_len;
614 }
615 size = maybe_con_write(fd, pbTmp, cbTotal);
616 free(pbTmp);
617 return size;
618 }
619
620 /* fall back on segment by segment output. */
621 }
622
623 for (i = 0; i < count; i++)
624 {
625 int cb = msc_write(fd, vector[i].iov_base, (int)vector[i].iov_len);
626 if (cb < 0)
627 return cb;
628 size += cb;
629 }
630 }
631 return size;
632}
633
634
635intmax_t strtoimax(const char *nptr, char **endptr, int base)
636{
637 if (*nptr != '-')
638 return _strtoui64(nptr, endptr, base);
639 return -(intmax_t)_strtoui64(nptr + 1, endptr, base);
640}
641
642
643uintmax_t strtoumax(const char *nptr, char **endptr, int base)
644{
645 return _strtoui64(nptr, endptr, base);
646}
647
648
649int asprintf(char **strp, const char *fmt, ...)
650{
651 int rc;
652 va_list va;
653 va_start(va, fmt);
654 rc = vasprintf(strp, fmt, va);
655 va_end(va);
656 return rc;
657}
658
659
660int vasprintf(char **strp, const char *fmt, va_list va)
661{
662 int rc;
663 char *psz;
664 size_t cb = 1024;
665
666 *strp = NULL;
667 for (;;)
668 {
669 va_list va2;
670
671 psz = malloc(cb);
672 if (!psz)
673 return -1;
674
675#ifdef va_copy
676 va_copy(va2, va);
677 rc = vsnprintf(psz, cb, fmt, va2);
678 va_end(vaCopy);
679#else
680 va2 = va;
681 rc = vsnprintf(psz, cb, fmt, va2);
682#endif
683 if (rc < 0 || (size_t)rc < cb)
684 break;
685 cb *= 2;
686 free(psz);
687 }
688
689 *strp = psz;
690 return rc;
691}
692
693
694int utimes(const char *pszPath, const struct msc_timeval *paTimes)
695{
696 if (paTimes)
697 {
698 BirdTimeVal_T aTimes[2];
699 aTimes[0].tv_sec = paTimes[0].tv_sec;
700 aTimes[0].tv_usec = paTimes[0].tv_usec;
701 aTimes[1].tv_sec = paTimes[1].tv_sec;
702 aTimes[1].tv_usec = paTimes[1].tv_usec;
703 return birdUtimes(pszPath, aTimes);
704 }
705 return birdUtimes(pszPath, NULL);
706}
707
708
709int lutimes(const char *pszPath, const struct msc_timeval *paTimes)
710{
711 if (paTimes)
712 {
713 BirdTimeVal_T aTimes[2];
714 aTimes[0].tv_sec = paTimes[0].tv_sec;
715 aTimes[0].tv_usec = paTimes[0].tv_usec;
716 aTimes[1].tv_sec = paTimes[1].tv_sec;
717 aTimes[1].tv_usec = paTimes[1].tv_usec;
718 return birdUtimes(pszPath, aTimes);
719 }
720 return birdUtimes(pszPath, NULL);
721}
722
723
724int gettimeofday(struct msc_timeval *pNow, void *pvIgnored)
725{
726 struct __timeb64 Now;
727 int rc = _ftime64_s(&Now);
728 if (rc == 0)
729 {
730 pNow->tv_sec = Now.time;
731 pNow->tv_usec = Now.millitm * 1000;
732 return 0;
733 }
734 errno = rc;
735 return -1;
736}
737
738
739struct tm *localtime_r(const __time64_t *pNow, struct tm *pResult)
740{
741 int rc = _localtime64_s(pResult, pNow);
742 if (rc == 0)
743 return pResult;
744 errno = rc;
745 return NULL;
746}
747
748
749__time64_t timegm(struct tm *pNow)
750{
751 return _mkgmtime64(pNow);
752}
753
754
755/**
756 * Checks if the given file descriptor is a pipe or not.
757 *
758 * @returns TRUE if pipe, FALSE if not.
759 * @param fd The libc file descriptor number.
760 */
761static BOOL isPipeFd(int fd)
762{
763 /* Is pipe? */
764 HANDLE hFile = (HANDLE)_get_osfhandle(fd);
765 if (hFile != INVALID_HANDLE_VALUE)
766 {
767 DWORD fType = GetFileType(hFile);
768 fType &= ~FILE_TYPE_REMOTE;
769 if (fType == FILE_TYPE_PIPE)
770 return TRUE;
771 }
772 return FALSE;
773}
774
775
776/**
777 * This is a kludge to make pipe handles blocking.
778 *
779 * @returns TRUE if it's now blocking, FALSE if not a pipe or we failed to fix
780 * the blocking mode.
781 * @param fd The libc file descriptor number.
782 */
783static BOOL makePipeBlocking(int fd)
784{
785 if (isPipeFd(fd))
786 {
787 /* Try fix it. */
788 HANDLE hFile = (HANDLE)_get_osfhandle(fd);
789 DWORD fState = 0;
790 if (GetNamedPipeHandleState(hFile, &fState, NULL, NULL, NULL, NULL, 0))
791 {
792 fState &= ~PIPE_NOWAIT;
793 fState |= PIPE_WAIT;
794 if (SetNamedPipeHandleState(hFile, &fState, NULL, NULL))
795 return TRUE;
796 }
797 }
798 return FALSE;
799}
800
801
802/**
803 * Initializes the msc fake stuff.
804 * @returns 0 on success (non-zero would indicate failure, see rterr.h).
805 */
806int mscfake_init(void)
807{
808 /*
809 * Kludge against _write returning ENOSPC on non-blocking pipes.
810 */
811 makePipeBlocking(STDOUT_FILENO);
812 makePipeBlocking(STDERR_FILENO);
813
814 return 0;
815}
816
817/*
818 * Do this before main is called.
819 */
820#pragma section(".CRT$XIA", read)
821#pragma section(".CRT$XIU", read)
822#pragma section(".CRT$XIZ", read)
823typedef int (__cdecl *PFNCRTINIT)(void);
824static __declspec(allocate(".CRT$XIU")) PFNCRTINIT g_MscFakeInitVectorEntry = mscfake_init;
825
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use