source: trunk/src/kmk/kmkbuiltin/mscfakes.c@ 3686

Last change on this file since 3686 was 3682, checked in by bird, 3 months ago

lib/nt,kmk: Fixed around rm/unlink semantics and general support for long file names in lib/nt.

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