1 | /* $Id: mscfakes.c 2645 2012-09-09 02:29:23Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * Fake Unix stuff for MSC.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2005-2010 knut st. osmundsen <[email protected]>
|
---|
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 <stdarg.h>
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <string.h>
|
---|
34 | #include <errno.h>
|
---|
35 | #include <io.h>
|
---|
36 | #include <fcntl.h>
|
---|
37 | #include <sys/stat.h>
|
---|
38 | #include "err.h"
|
---|
39 | #include "mscfakes.h"
|
---|
40 |
|
---|
41 | #define timeval windows_timeval
|
---|
42 | #include <Windows.h>
|
---|
43 | #undef timeval
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Makes corrections to a directory path that ends with a trailing slash.
|
---|
48 | *
|
---|
49 | * @returns temporary buffer to free.
|
---|
50 | * @param ppszPath The path pointer. This is updated when necessary.
|
---|
51 | * @param pfMustBeDir This is set if it must be a directory, otherwise it's cleared.
|
---|
52 | */
|
---|
53 | static char *
|
---|
54 | msc_fix_path(const char **ppszPath, int *pfMustBeDir)
|
---|
55 | {
|
---|
56 | const char *pszPath = *ppszPath;
|
---|
57 | const char *psz;
|
---|
58 | char *pszNew;
|
---|
59 | *pfMustBeDir = 0;
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * Skip any compusory trailing slashes
|
---|
63 | */
|
---|
64 | if (pszPath[0] == '/' || pszPath[0] == '\\')
|
---|
65 | {
|
---|
66 | if ( (pszPath[1] == '/' || pszPath[1] == '\\')
|
---|
67 | && pszPath[2] != '/'
|
---|
68 | && pszPath[2] != '\\')
|
---|
69 | /* unc */
|
---|
70 | pszPath += 2;
|
---|
71 | else
|
---|
72 | /* root slash(es) */
|
---|
73 | pszPath++;
|
---|
74 | }
|
---|
75 | else if ( isalpha(pszPath[0])
|
---|
76 | && pszPath[1] == ':')
|
---|
77 | {
|
---|
78 | if (pszPath[2] == '/' || pszPath[2] == '\\')
|
---|
79 | /* drive w/ slash */
|
---|
80 | pszPath += 3;
|
---|
81 | else
|
---|
82 | /* drive relative path. */
|
---|
83 | pszPath += 2;
|
---|
84 | }
|
---|
85 | /* else: relative path, no skipping necessary. */
|
---|
86 |
|
---|
87 | /*
|
---|
88 | * Any trailing slashes to drop off?
|
---|
89 | */
|
---|
90 | psz = strchr(pszPath, '\0');
|
---|
91 | if (pszPath <= psz)
|
---|
92 | return NULL;
|
---|
93 | if ( psz[-1] != '/'
|
---|
94 | || psz[-1] != '\\')
|
---|
95 | return NULL;
|
---|
96 |
|
---|
97 | /* figure how many, make a copy and strip them off. */
|
---|
98 | while ( psz > pszPath
|
---|
99 | && ( psz[-1] == '/'
|
---|
100 | || psz[-1] == '\\'))
|
---|
101 | psz--;
|
---|
102 | pszNew = strdup(pszPath);
|
---|
103 | pszNew[psz - pszPath] = '\0';
|
---|
104 |
|
---|
105 | *pfMustBeDir = 1;
|
---|
106 | *ppszPath = pszNew; /* use this one */
|
---|
107 | return pszNew;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | static int
|
---|
112 | msc_set_errno(DWORD dwErr)
|
---|
113 | {
|
---|
114 | switch (dwErr)
|
---|
115 | {
|
---|
116 | default:
|
---|
117 | case ERROR_INVALID_FUNCTION: errno = EINVAL; break;
|
---|
118 | case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
|
---|
119 | case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
|
---|
120 | case ERROR_TOO_MANY_OPEN_FILES: errno = EMFILE; break;
|
---|
121 | case ERROR_ACCESS_DENIED: errno = EACCES; break;
|
---|
122 | case ERROR_INVALID_HANDLE: errno = EBADF; break;
|
---|
123 | case ERROR_ARENA_TRASHED: errno = ENOMEM; break;
|
---|
124 | case ERROR_NOT_ENOUGH_MEMORY: errno = ENOMEM; break;
|
---|
125 | case ERROR_INVALID_BLOCK: errno = ENOMEM; break;
|
---|
126 | case ERROR_BAD_ENVIRONMENT: errno = E2BIG; break;
|
---|
127 | case ERROR_BAD_FORMAT: errno = ENOEXEC; break;
|
---|
128 | case ERROR_INVALID_ACCESS: errno = EINVAL; break;
|
---|
129 | case ERROR_INVALID_DATA: errno = EINVAL; break;
|
---|
130 | case ERROR_INVALID_DRIVE: errno = ENOENT; break;
|
---|
131 | case ERROR_CURRENT_DIRECTORY: errno = EACCES; break;
|
---|
132 | case ERROR_NOT_SAME_DEVICE: errno = EXDEV; break;
|
---|
133 | case ERROR_NO_MORE_FILES: errno = ENOENT; break;
|
---|
134 | case ERROR_LOCK_VIOLATION: errno = EACCES; break;
|
---|
135 | case ERROR_BAD_NETPATH: errno = ENOENT; break;
|
---|
136 | case ERROR_NETWORK_ACCESS_DENIED: errno = EACCES; break;
|
---|
137 | case ERROR_BAD_NET_NAME: errno = ENOENT; break;
|
---|
138 | case ERROR_FILE_EXISTS: errno = EEXIST; break;
|
---|
139 | case ERROR_CANNOT_MAKE: errno = EACCES; break;
|
---|
140 | case ERROR_FAIL_I24: errno = EACCES; break;
|
---|
141 | case ERROR_INVALID_PARAMETER: errno = EINVAL; break;
|
---|
142 | case ERROR_NO_PROC_SLOTS: errno = EAGAIN; break;
|
---|
143 | case ERROR_DRIVE_LOCKED: errno = EACCES; break;
|
---|
144 | case ERROR_BROKEN_PIPE: errno = EPIPE; break;
|
---|
145 | case ERROR_DISK_FULL: errno = ENOSPC; break;
|
---|
146 | case ERROR_INVALID_TARGET_HANDLE: errno = EBADF; break;
|
---|
147 | case ERROR_WAIT_NO_CHILDREN: errno = ECHILD; break;
|
---|
148 | case ERROR_CHILD_NOT_COMPLETE: errno = ECHILD; break;
|
---|
149 | case ERROR_DIRECT_ACCESS_HANDLE: errno = EBADF; break;
|
---|
150 | case ERROR_NEGATIVE_SEEK: errno = EINVAL; break;
|
---|
151 | case ERROR_SEEK_ON_DEVICE: errno = EACCES; break;
|
---|
152 | case ERROR_DIR_NOT_EMPTY: errno = ENOTEMPTY; break;
|
---|
153 | case ERROR_NOT_LOCKED: errno = EACCES; break;
|
---|
154 | case ERROR_BAD_PATHNAME: errno = ENOENT; break;
|
---|
155 | case ERROR_MAX_THRDS_REACHED: errno = EAGAIN; break;
|
---|
156 | case ERROR_LOCK_FAILED: errno = EACCES; break;
|
---|
157 | case ERROR_ALREADY_EXISTS: errno = EEXIST; break;
|
---|
158 | case ERROR_FILENAME_EXCED_RANGE: errno = ENOENT; break;
|
---|
159 | case ERROR_NESTING_NOT_ALLOWED: errno = EAGAIN; break;
|
---|
160 | #ifdef EMLINK
|
---|
161 | case ERROR_TOO_MANY_LINKS: errno = EMLINK; break;
|
---|
162 | #endif
|
---|
163 | }
|
---|
164 |
|
---|
165 | return -1;
|
---|
166 | }
|
---|
167 |
|
---|
168 | char *dirname(char *path)
|
---|
169 | {
|
---|
170 | /** @todo later */
|
---|
171 | return path;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | int lchmod(const char *pszPath, mode_t mode)
|
---|
176 | {
|
---|
177 | int rc = 0;
|
---|
178 | int fMustBeDir;
|
---|
179 | char *pszPathFree = msc_fix_path(&pszPath, &fMustBeDir);
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * Get the current attributes
|
---|
183 | */
|
---|
184 | DWORD fAttr = GetFileAttributes(pszPath);
|
---|
185 | if (fAttr == INVALID_FILE_ATTRIBUTES)
|
---|
186 | rc = msc_set_errno(GetLastError());
|
---|
187 | else if (fMustBeDir & !(fAttr & FILE_ATTRIBUTE_DIRECTORY))
|
---|
188 | {
|
---|
189 | errno = ENOTDIR;
|
---|
190 | rc = -1;
|
---|
191 | }
|
---|
192 | else
|
---|
193 | {
|
---|
194 | /*
|
---|
195 | * Modify the attributes and try set them.
|
---|
196 | */
|
---|
197 | if (mode & _S_IWRITE)
|
---|
198 | fAttr &= ~FILE_ATTRIBUTE_READONLY;
|
---|
199 | else
|
---|
200 | fAttr |= FILE_ATTRIBUTE_READONLY;
|
---|
201 | if (!SetFileAttributes(pszPath, fAttr))
|
---|
202 | rc = msc_set_errno(GetLastError());
|
---|
203 | }
|
---|
204 |
|
---|
205 | if (pszPathFree)
|
---|
206 | {
|
---|
207 | int saved_errno = errno;
|
---|
208 | free(pszPathFree);
|
---|
209 | errno = saved_errno;
|
---|
210 | }
|
---|
211 | return rc;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | int msc_chmod(const char *pszPath, mode_t mode)
|
---|
216 | {
|
---|
217 | int rc = 0;
|
---|
218 | int fMustBeDir;
|
---|
219 | char *pszPathFree = msc_fix_path(&pszPath, &fMustBeDir);
|
---|
220 |
|
---|
221 | /*
|
---|
222 | * Get the current attributes.
|
---|
223 | */
|
---|
224 | DWORD fAttr = GetFileAttributes(pszPath);
|
---|
225 | if (fAttr == INVALID_FILE_ATTRIBUTES)
|
---|
226 | rc = msc_set_errno(GetLastError());
|
---|
227 | else if (fMustBeDir & !(fAttr & FILE_ATTRIBUTE_DIRECTORY))
|
---|
228 | {
|
---|
229 | errno = ENOTDIR;
|
---|
230 | rc = -1;
|
---|
231 | }
|
---|
232 | else if (fAttr & FILE_ATTRIBUTE_REPARSE_POINT)
|
---|
233 | {
|
---|
234 | errno = ENOSYS; /** @todo resolve symbolic link / rewrite to NtSetInformationFile. */
|
---|
235 | rc = -1;
|
---|
236 | }
|
---|
237 | else
|
---|
238 | {
|
---|
239 | /*
|
---|
240 | * Modify the attributes and try set them.
|
---|
241 | */
|
---|
242 | if (mode & _S_IWRITE)
|
---|
243 | fAttr &= ~FILE_ATTRIBUTE_READONLY;
|
---|
244 | else
|
---|
245 | fAttr |= FILE_ATTRIBUTE_READONLY;
|
---|
246 | if (!SetFileAttributes(pszPath, fAttr))
|
---|
247 | rc = msc_set_errno(GetLastError());
|
---|
248 | }
|
---|
249 |
|
---|
250 | if (pszPathFree)
|
---|
251 | {
|
---|
252 | int saved_errno = errno;
|
---|
253 | free(pszPathFree);
|
---|
254 | errno = saved_errno;
|
---|
255 | }
|
---|
256 | return rc;
|
---|
257 | }
|
---|
258 |
|
---|
259 |
|
---|
260 | typedef BOOL (WINAPI *PFNCREATEHARDLINKA)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
|
---|
261 | int link(const char *pszDst, const char *pszLink)
|
---|
262 | {
|
---|
263 | static PFNCREATEHARDLINKA s_pfnCreateHardLinkA = NULL;
|
---|
264 | static int s_fTried = FALSE;
|
---|
265 |
|
---|
266 | /* The API was introduced in Windows 2000, so resolve it dynamically. */
|
---|
267 | if (!s_pfnCreateHardLinkA)
|
---|
268 | {
|
---|
269 | if (!s_fTried)
|
---|
270 | {
|
---|
271 | HMODULE hmod = LoadLibrary("KERNEL32.DLL");
|
---|
272 | if (hmod)
|
---|
273 | *(FARPROC *)&s_pfnCreateHardLinkA = GetProcAddress(hmod, "CreateHardLinkA");
|
---|
274 | s_fTried = TRUE;
|
---|
275 | }
|
---|
276 | if (!s_pfnCreateHardLinkA)
|
---|
277 | {
|
---|
278 | errno = ENOSYS;
|
---|
279 | return -1;
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | if (s_pfnCreateHardLinkA(pszLink, pszDst, NULL))
|
---|
284 | return 0;
|
---|
285 | return msc_set_errno(GetLastError());
|
---|
286 | }
|
---|
287 |
|
---|
288 |
|
---|
289 | int mkdir_msc(const char *path, mode_t mode)
|
---|
290 | {
|
---|
291 | int rc = (mkdir)(path);
|
---|
292 | if (rc)
|
---|
293 | {
|
---|
294 | size_t len = strlen(path);
|
---|
295 | if (len > 0 && (path[len - 1] == '/' || path[len - 1] == '\\'))
|
---|
296 | {
|
---|
297 | char *str = strdup(path);
|
---|
298 | while (len > 0 && (str[len - 1] == '/' || str[len - 1] == '\\'))
|
---|
299 | str[--len] = '\0';
|
---|
300 | rc = (mkdir)(str);
|
---|
301 | free(str);
|
---|
302 | }
|
---|
303 | }
|
---|
304 | return rc;
|
---|
305 | }
|
---|
306 |
|
---|
307 | int rmdir_msc(const char *path)
|
---|
308 | {
|
---|
309 | int rc = (rmdir)(path);
|
---|
310 | if (rc)
|
---|
311 | {
|
---|
312 | size_t len = strlen(path);
|
---|
313 | if (len > 0 && (path[len - 1] == '/' || path[len - 1] == '\\'))
|
---|
314 | {
|
---|
315 | char *str = strdup(path);
|
---|
316 | while (len > 0 && (str[len - 1] == '/' || str[len - 1] == '\\'))
|
---|
317 | str[--len] = '\0';
|
---|
318 | rc = (rmdir)(str);
|
---|
319 | free(str);
|
---|
320 | }
|
---|
321 | }
|
---|
322 | return rc;
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|
326 | static int doname(char *pszX, char *pszEnd)
|
---|
327 | {
|
---|
328 | static char s_szChars[] = "Xabcdefghijklmnopqrstuwvxyz1234567890";
|
---|
329 | int rc = 0;
|
---|
330 | do
|
---|
331 | {
|
---|
332 | char ch;
|
---|
333 |
|
---|
334 | pszEnd++;
|
---|
335 | ch = *(strchr(s_szChars, *pszEnd) + 1);
|
---|
336 | if (ch)
|
---|
337 | {
|
---|
338 | *pszEnd = ch;
|
---|
339 | return 0;
|
---|
340 | }
|
---|
341 | *pszEnd = 'a';
|
---|
342 | } while (pszEnd != pszX);
|
---|
343 | return 1;
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 | int mkstemp(char *temp)
|
---|
348 | {
|
---|
349 | char *pszX = strchr(temp, 'X');
|
---|
350 | char *pszEnd = strchr(pszX, '\0');
|
---|
351 | int cTries = 1000;
|
---|
352 | while (--cTries > 0)
|
---|
353 | {
|
---|
354 | int fd;
|
---|
355 | if (doname(pszX, pszEnd))
|
---|
356 | return -1;
|
---|
357 | fd = open(temp, _O_EXCL | _O_CREAT | _O_BINARY | _O_RDWR, 0777);
|
---|
358 | if (fd >= 0)
|
---|
359 | return fd;
|
---|
360 | }
|
---|
361 | return -1;
|
---|
362 | }
|
---|
363 |
|
---|
364 |
|
---|
365 | /** Unix to DOS. */
|
---|
366 | static char *fix_slashes(char *psz)
|
---|
367 | {
|
---|
368 | char *pszRet = psz;
|
---|
369 | for (; *psz; psz++)
|
---|
370 | if (*psz == '/')
|
---|
371 | *psz = '\\';
|
---|
372 | return pszRet;
|
---|
373 | }
|
---|
374 |
|
---|
375 |
|
---|
376 | /** Calcs the SYMBOLIC_LINK_FLAG_DIRECTORY flag for CreatesymbolcLink. */
|
---|
377 | static DWORD is_directory(const char *pszPath, const char *pszRelativeTo)
|
---|
378 | {
|
---|
379 | size_t cchPath = strlen(pszPath);
|
---|
380 | struct stat st;
|
---|
381 | if (cchPath > 0 && pszPath[cchPath - 1] == '\\' || pszPath[cchPath - 1] == '/')
|
---|
382 | return 1; /* SYMBOLIC_LINK_FLAG_DIRECTORY */
|
---|
383 |
|
---|
384 | if (stat(pszPath, &st))
|
---|
385 | {
|
---|
386 | size_t cchRelativeTo = strlen(pszRelativeTo);
|
---|
387 | char *psz = malloc(cchPath + cchRelativeTo + 4);
|
---|
388 | memcpy(psz, pszRelativeTo, cchRelativeTo);
|
---|
389 | memcpy(psz + cchRelativeTo, "\\", 1);
|
---|
390 | memcpy(psz + cchRelativeTo + 1, pszPath, cchPath + 1);
|
---|
391 | if (stat(pszPath, &st))
|
---|
392 | st.st_mode = _S_IFREG;
|
---|
393 | free(psz);
|
---|
394 | }
|
---|
395 |
|
---|
396 | return (st.st_mode & _S_IFMT) == _S_IFDIR ? 1 : 0;
|
---|
397 | }
|
---|
398 |
|
---|
399 |
|
---|
400 | int symlink(const char *pszDst, const char *pszLink)
|
---|
401 | {
|
---|
402 | static BOOLEAN (WINAPI *s_pfnCreateSymbolicLinkA)(LPCSTR, LPCSTR, DWORD) = 0;
|
---|
403 | static BOOL s_fTried = FALSE;
|
---|
404 |
|
---|
405 | if (!s_fTried)
|
---|
406 | {
|
---|
407 | HMODULE hmod = LoadLibrary("KERNEL32.DLL");
|
---|
408 | if (hmod)
|
---|
409 | *(FARPROC *)&s_pfnCreateSymbolicLinkA = GetProcAddress(hmod, "CreateSymbolicLinkA");
|
---|
410 | s_fTried = TRUE;
|
---|
411 | }
|
---|
412 |
|
---|
413 | if (s_pfnCreateSymbolicLinkA)
|
---|
414 | {
|
---|
415 | char *pszDstCopy = fix_slashes(strdup(pszDst));
|
---|
416 | char *pszLinkCopy = fix_slashes(strdup(pszLink));
|
---|
417 | BOOLEAN fRc = s_pfnCreateSymbolicLinkA(pszLinkCopy, pszDstCopy,
|
---|
418 | is_directory(pszDstCopy, pszLinkCopy));
|
---|
419 | DWORD err = GetLastError();
|
---|
420 | free(pszDstCopy);
|
---|
421 | free(pszLinkCopy);
|
---|
422 | if (fRc)
|
---|
423 | return 0;
|
---|
424 | switch (err)
|
---|
425 | {
|
---|
426 | case ERROR_NOT_SUPPORTED: errno = ENOSYS; break;
|
---|
427 | case ERROR_ALREADY_EXISTS:
|
---|
428 | case ERROR_FILE_EXISTS: errno = EEXIST; break;
|
---|
429 | case ERROR_DIRECTORY: errno = ENOTDIR; break;
|
---|
430 | case ERROR_ACCESS_DENIED:
|
---|
431 | case ERROR_PRIVILEGE_NOT_HELD: errno = EPERM; break;
|
---|
432 | default: errno = EINVAL; break;
|
---|
433 | }
|
---|
434 | return -1;
|
---|
435 | }
|
---|
436 |
|
---|
437 | errno = ENOSYS;
|
---|
438 | err(1, "symlink() is not implemented on windows!");
|
---|
439 | return -1;
|
---|
440 | }
|
---|
441 |
|
---|
442 |
|
---|
443 | #if _MSC_VER < 1400
|
---|
444 | int snprintf(char *buf, size_t size, const char *fmt, ...)
|
---|
445 | {
|
---|
446 | int cch;
|
---|
447 | va_list args;
|
---|
448 | va_start(args, fmt);
|
---|
449 | cch = vsprintf(buf, fmt, args);
|
---|
450 | va_end(args);
|
---|
451 | return cch;
|
---|
452 | }
|
---|
453 | #endif
|
---|
454 |
|
---|
455 |
|
---|
456 | int utimes(const char *pszPath, const struct timeval *paTimes)
|
---|
457 | {
|
---|
458 | /** @todo implement me! */
|
---|
459 | return 0;
|
---|
460 | }
|
---|
461 |
|
---|
462 |
|
---|
463 | int writev(int fd, const struct iovec *vector, int count)
|
---|
464 | {
|
---|
465 | int size = 0;
|
---|
466 | int i;
|
---|
467 | for (i = 0; i < count; i++)
|
---|
468 | {
|
---|
469 | int cb = (int)write(fd, vector[i].iov_base, (int)vector[i].iov_len);
|
---|
470 | if (cb < 0)
|
---|
471 | return -1;
|
---|
472 | size += cb;
|
---|
473 | }
|
---|
474 | return size;
|
---|
475 | }
|
---|
476 |
|
---|
477 |
|
---|
478 | intmax_t strtoimax(const char *nptr, char **endptr, int base)
|
---|
479 | {
|
---|
480 | if (*nptr != '-')
|
---|
481 | return _strtoui64(nptr, endptr, base);
|
---|
482 | return -(intmax_t)_strtoui64(nptr + 1, endptr, base);
|
---|
483 | }
|
---|
484 |
|
---|
485 |
|
---|
486 | uintmax_t strtoumax(const char *nptr, char **endptr, int base)
|
---|
487 | {
|
---|
488 | return _strtoui64(nptr, endptr, base);
|
---|
489 | }
|
---|
490 |
|
---|
491 |
|
---|
492 | int asprintf(char **strp, const char *fmt, ...)
|
---|
493 | {
|
---|
494 | int rc;
|
---|
495 | va_list va;
|
---|
496 | va_start(va, fmt);
|
---|
497 | rc = vasprintf(strp, fmt, va);
|
---|
498 | va_end(va);
|
---|
499 | return rc;
|
---|
500 | }
|
---|
501 |
|
---|
502 |
|
---|
503 | int vasprintf(char **strp, const char *fmt, va_list va)
|
---|
504 | {
|
---|
505 | int rc;
|
---|
506 | char *psz;
|
---|
507 | size_t cb = 1024;
|
---|
508 |
|
---|
509 | *strp = NULL;
|
---|
510 | for (;;)
|
---|
511 | {
|
---|
512 | va_list va2;
|
---|
513 |
|
---|
514 | psz = malloc(cb);
|
---|
515 | if (!psz)
|
---|
516 | return -1;
|
---|
517 |
|
---|
518 | #ifdef va_copy
|
---|
519 | va_copy(va2, va);
|
---|
520 | rc = snprintf(psz, cb, fmt, va2);
|
---|
521 | va_end(vaCopy);
|
---|
522 | #else
|
---|
523 | va2 = va;
|
---|
524 | rc = snprintf(psz, cb, fmt, va2);
|
---|
525 | #endif
|
---|
526 | if (rc < 0 || (size_t)rc < cb)
|
---|
527 | break;
|
---|
528 | cb *= 2;
|
---|
529 | free(psz);
|
---|
530 | }
|
---|
531 |
|
---|
532 | *strp = psz;
|
---|
533 | return rc;
|
---|
534 | }
|
---|
535 |
|
---|
536 |
|
---|
537 | /*
|
---|
538 | * Workaround for directory names with trailing slashes.
|
---|
539 | */
|
---|
540 | #undef stat
|
---|
541 | int
|
---|
542 | bird_w32_stat(const char *path, struct stat *st)
|
---|
543 | {
|
---|
544 | int rc = stat(path, st);
|
---|
545 | if ( rc != 0
|
---|
546 | && errno == ENOENT
|
---|
547 | && *path != '\0')
|
---|
548 | {
|
---|
549 | char *slash = strchr(path, '\0') - 1;
|
---|
550 | if (*slash == '/' || *slash == '\\')
|
---|
551 | {
|
---|
552 | size_t len_path = slash - path + 1;
|
---|
553 | char *tmp = alloca(len_path + 4);
|
---|
554 | memcpy(tmp, path, len_path);
|
---|
555 | tmp[len_path] = '.';
|
---|
556 | tmp[len_path + 1] = '\0';
|
---|
557 | errno = 0;
|
---|
558 | rc = stat(tmp, st);
|
---|
559 | if ( rc == 0
|
---|
560 | && !S_ISDIR(st->st_mode))
|
---|
561 | {
|
---|
562 | errno = ENOTDIR;
|
---|
563 | rc = -1;
|
---|
564 | }
|
---|
565 | }
|
---|
566 | }
|
---|
567 | #ifdef KMK_PRF
|
---|
568 | {
|
---|
569 | int err = errno;
|
---|
570 | fprintf(stderr, "stat(%s,) -> %d/%d\n", path, rc, errno);
|
---|
571 | errno = err;
|
---|
572 | }
|
---|
573 | #endif
|
---|
574 | return rc;
|
---|
575 | }
|
---|
576 |
|
---|