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