| 1 | /* $Id: mscfakes.c 2413 2010-09-11 17:43:04Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * Fake Unix stuff for MSC.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2005-2010 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 <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 | }
|
|---|
| 161 |
|
|---|
| 162 | return -1;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | char *dirname(char *path)
|
|---|
| 166 | {
|
|---|
| 167 | /** @todo later */
|
|---|
| 168 | return path;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 | int lchmod(const char *pszPath, mode_t mode)
|
|---|
| 173 | {
|
|---|
| 174 | int rc = 0;
|
|---|
| 175 | int fMustBeDir;
|
|---|
| 176 | char *pszPathFree = msc_fix_path(&pszPath, &fMustBeDir);
|
|---|
| 177 |
|
|---|
| 178 | /*
|
|---|
| 179 | * Get the current attributes
|
|---|
| 180 | */
|
|---|
| 181 | DWORD fAttr = GetFileAttributes(pszPath);
|
|---|
| 182 | if (fAttr == INVALID_FILE_ATTRIBUTES)
|
|---|
| 183 | rc = msc_set_errno(GetLastError());
|
|---|
| 184 | else if (fMustBeDir & !(fAttr & FILE_ATTRIBUTE_DIRECTORY))
|
|---|
| 185 | {
|
|---|
| 186 | errno = ENOTDIR;
|
|---|
| 187 | rc = -1;
|
|---|
| 188 | }
|
|---|
| 189 | else
|
|---|
| 190 | {
|
|---|
| 191 | /*
|
|---|
| 192 | * Modify the attributes and try set them.
|
|---|
| 193 | */
|
|---|
| 194 | if (mode & _S_IWRITE)
|
|---|
| 195 | fAttr &= ~FILE_ATTRIBUTE_READONLY;
|
|---|
| 196 | else
|
|---|
| 197 | fAttr |= FILE_ATTRIBUTE_READONLY;
|
|---|
| 198 | if (!SetFileAttributes(pszPath, fAttr))
|
|---|
| 199 | rc = msc_set_errno(GetLastError());
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | if (pszPathFree)
|
|---|
| 203 | {
|
|---|
| 204 | int saved_errno = errno;
|
|---|
| 205 | free(pszPathFree);
|
|---|
| 206 | errno = saved_errno;
|
|---|
| 207 | }
|
|---|
| 208 | return rc;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 | int msc_chmod(const char *pszPath, mode_t mode)
|
|---|
| 213 | {
|
|---|
| 214 | int rc = 0;
|
|---|
| 215 | int saved_errno;
|
|---|
| 216 | int fMustBeDir;
|
|---|
| 217 | char *pszPathFree = msc_fix_path(&pszPath, &fMustBeDir);
|
|---|
| 218 |
|
|---|
| 219 | /*
|
|---|
| 220 | * Get the current attributes.
|
|---|
| 221 | */
|
|---|
| 222 | DWORD fAttr = GetFileAttributes(pszPath);
|
|---|
| 223 | if (fAttr == INVALID_FILE_ATTRIBUTES)
|
|---|
| 224 | rc = msc_set_errno(GetLastError());
|
|---|
| 225 | else if (fMustBeDir & !(fAttr & FILE_ATTRIBUTE_DIRECTORY))
|
|---|
| 226 | {
|
|---|
| 227 | errno = ENOTDIR;
|
|---|
| 228 | rc = -1;
|
|---|
| 229 | }
|
|---|
| 230 | else if (fAttr & FILE_ATTRIBUTE_REPARSE_POINT)
|
|---|
| 231 | {
|
|---|
| 232 | errno = ENOSYS; /** @todo resolve symbolic link / rewrite to NtSetInformationFile. */
|
|---|
| 233 | rc = -1;
|
|---|
| 234 | }
|
|---|
| 235 | else
|
|---|
| 236 | {
|
|---|
| 237 | /*
|
|---|
| 238 | * Modify the attributes and try set them.
|
|---|
| 239 | */
|
|---|
| 240 | if (mode & _S_IWRITE)
|
|---|
| 241 | fAttr &= ~FILE_ATTRIBUTE_READONLY;
|
|---|
| 242 | else
|
|---|
| 243 | fAttr |= FILE_ATTRIBUTE_READONLY;
|
|---|
| 244 | if (!SetFileAttributes(pszPath, fAttr))
|
|---|
| 245 | rc = msc_set_errno(GetLastError());
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | if (pszPathFree)
|
|---|
| 249 | {
|
|---|
| 250 | int saved_errno = errno;
|
|---|
| 251 | free(pszPathFree);
|
|---|
| 252 | errno = saved_errno;
|
|---|
| 253 | }
|
|---|
| 254 | return rc;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 | int link(const char *pszDst, const char *pszLink)
|
|---|
| 259 | {
|
|---|
| 260 | errno = ENOSYS;
|
|---|
| 261 | err(1, "link() is not implemented on windows!");
|
|---|
| 262 | return -1;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 | int mkdir_msc(const char *path, mode_t mode)
|
|---|
| 267 | {
|
|---|
| 268 | int rc = (mkdir)(path);
|
|---|
| 269 | if (rc)
|
|---|
| 270 | {
|
|---|
| 271 | size_t len = strlen(path);
|
|---|
| 272 | if (len > 0 && (path[len - 1] == '/' || path[len - 1] == '\\'))
|
|---|
| 273 | {
|
|---|
| 274 | char *str = strdup(path);
|
|---|
| 275 | while (len > 0 && (str[len - 1] == '/' || str[len - 1] == '\\'))
|
|---|
| 276 | str[--len] = '\0';
|
|---|
| 277 | rc = (mkdir)(str);
|
|---|
| 278 | free(str);
|
|---|
| 279 | }
|
|---|
| 280 | }
|
|---|
| 281 | return rc;
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | int rmdir_msc(const char *path)
|
|---|
| 285 | {
|
|---|
| 286 | int rc = (rmdir)(path);
|
|---|
| 287 | if (rc)
|
|---|
| 288 | {
|
|---|
| 289 | size_t len = strlen(path);
|
|---|
| 290 | if (len > 0 && (path[len - 1] == '/' || path[len - 1] == '\\'))
|
|---|
| 291 | {
|
|---|
| 292 | char *str = strdup(path);
|
|---|
| 293 | while (len > 0 && (str[len - 1] == '/' || str[len - 1] == '\\'))
|
|---|
| 294 | str[--len] = '\0';
|
|---|
| 295 | rc = (rmdir)(str);
|
|---|
| 296 | free(str);
|
|---|
| 297 | }
|
|---|
| 298 | }
|
|---|
| 299 | return rc;
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 | static int doname(char *pszX, char *pszEnd)
|
|---|
| 304 | {
|
|---|
| 305 | static char s_szChars[] = "Xabcdefghijklmnopqrstuwvxyz1234567890";
|
|---|
| 306 | int rc = 0;
|
|---|
| 307 | do
|
|---|
| 308 | {
|
|---|
| 309 | char ch;
|
|---|
| 310 |
|
|---|
| 311 | pszEnd++;
|
|---|
| 312 | ch = *(strchr(s_szChars, *pszEnd) + 1);
|
|---|
| 313 | if (ch)
|
|---|
| 314 | {
|
|---|
| 315 | *pszEnd = ch;
|
|---|
| 316 | return 0;
|
|---|
| 317 | }
|
|---|
| 318 | *pszEnd = 'a';
|
|---|
| 319 | } while (pszEnd != pszX);
|
|---|
| 320 | return 1;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 | int mkstemp(char *temp)
|
|---|
| 325 | {
|
|---|
| 326 | char *pszX = strchr(temp, 'X');
|
|---|
| 327 | char *pszEnd = strchr(pszX, '\0');
|
|---|
| 328 | int cTries = 1000;
|
|---|
| 329 | while (--cTries > 0)
|
|---|
| 330 | {
|
|---|
| 331 | int fd;
|
|---|
| 332 | if (doname(pszX, pszEnd))
|
|---|
| 333 | return -1;
|
|---|
| 334 | fd = open(temp, _O_EXCL | _O_CREAT | _O_BINARY | _O_RDWR, 0777);
|
|---|
| 335 | if (fd >= 0)
|
|---|
| 336 | return fd;
|
|---|
| 337 | }
|
|---|
| 338 | return -1;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 | /** Unix to DOS. */
|
|---|
| 343 | static char *fix_slashes(char *psz)
|
|---|
| 344 | {
|
|---|
| 345 | char *pszRet = psz;
|
|---|
| 346 | for (; *psz; psz++)
|
|---|
| 347 | if (*psz == '/')
|
|---|
| 348 | *psz = '\\';
|
|---|
| 349 | return pszRet;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 | /** Calcs the SYMBOLIC_LINK_FLAG_DIRECTORY flag for CreatesymbolcLink. */
|
|---|
| 354 | static DWORD is_directory(const char *pszPath, const char *pszRelativeTo)
|
|---|
| 355 | {
|
|---|
| 356 | size_t cchPath = strlen(pszPath);
|
|---|
| 357 | struct stat st;
|
|---|
| 358 | if (cchPath > 0 && pszPath[cchPath - 1] == '\\' || pszPath[cchPath - 1] == '/')
|
|---|
| 359 | return 1; /* SYMBOLIC_LINK_FLAG_DIRECTORY */
|
|---|
| 360 |
|
|---|
| 361 | if (stat(pszPath, &st))
|
|---|
| 362 | {
|
|---|
| 363 | size_t cchRelativeTo = strlen(pszRelativeTo);
|
|---|
| 364 | char *psz = malloc(cchPath + cchRelativeTo + 4);
|
|---|
| 365 | memcpy(psz, pszRelativeTo, cchRelativeTo);
|
|---|
| 366 | memcpy(psz + cchRelativeTo, "\\", 1);
|
|---|
| 367 | memcpy(psz + cchRelativeTo + 1, pszPath, cchPath + 1);
|
|---|
| 368 | if (stat(pszPath, &st))
|
|---|
| 369 | st.st_mode = _S_IFREG;
|
|---|
| 370 | free(psz);
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | return (st.st_mode & _S_IFMT) == _S_IFDIR ? 1 : 0;
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 | int symlink(const char *pszDst, const char *pszLink)
|
|---|
| 378 | {
|
|---|
| 379 | static BOOLEAN (WINAPI *s_pfnCreateSymbolicLinkA)(LPCSTR, LPCSTR, DWORD) = 0;
|
|---|
| 380 | static BOOL s_fTried = FALSE;
|
|---|
| 381 |
|
|---|
| 382 | if (!s_fTried)
|
|---|
| 383 | {
|
|---|
| 384 | HMODULE hmod = LoadLibrary("KERNEL32.DLL");
|
|---|
| 385 | if (hmod)
|
|---|
| 386 | *(FARPROC *)&s_pfnCreateSymbolicLinkA = GetProcAddress(hmod, "CreateSymbolicLinkA");
|
|---|
| 387 | s_fTried = TRUE;
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | if (s_pfnCreateSymbolicLinkA)
|
|---|
| 391 | {
|
|---|
| 392 | char *pszDstCopy = fix_slashes(strdup(pszDst));
|
|---|
| 393 | char *pszLinkCopy = fix_slashes(strdup(pszLink));
|
|---|
| 394 | BOOLEAN fRc = s_pfnCreateSymbolicLinkA(pszLinkCopy, pszDstCopy,
|
|---|
| 395 | is_directory(pszDstCopy, pszLinkCopy));
|
|---|
| 396 | DWORD err = GetLastError();
|
|---|
| 397 | free(pszDstCopy);
|
|---|
| 398 | free(pszLinkCopy);
|
|---|
| 399 | if (fRc)
|
|---|
| 400 | return 0;
|
|---|
| 401 | switch (err)
|
|---|
| 402 | {
|
|---|
| 403 | case ERROR_NOT_SUPPORTED: errno = ENOSYS; break;
|
|---|
| 404 | case ERROR_ALREADY_EXISTS:
|
|---|
| 405 | case ERROR_FILE_EXISTS: errno = EEXIST; break;
|
|---|
| 406 | case ERROR_DIRECTORY: errno = ENOTDIR; break;
|
|---|
| 407 | case ERROR_ACCESS_DENIED:
|
|---|
| 408 | case ERROR_PRIVILEGE_NOT_HELD: errno = EPERM; break;
|
|---|
| 409 | default: errno = EINVAL; break;
|
|---|
| 410 | }
|
|---|
| 411 | return -1;
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | errno = ENOSYS;
|
|---|
| 415 | err(1, "symlink() is not implemented on windows!");
|
|---|
| 416 | return -1;
|
|---|
| 417 | }
|
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 | #if _MSC_VER < 1400
|
|---|
| 421 | int snprintf(char *buf, size_t size, const char *fmt, ...)
|
|---|
| 422 | {
|
|---|
| 423 | int cch;
|
|---|
| 424 | va_list args;
|
|---|
| 425 | va_start(args, fmt);
|
|---|
| 426 | cch = vsprintf(buf, fmt, args);
|
|---|
| 427 | va_end(args);
|
|---|
| 428 | return cch;
|
|---|
| 429 | }
|
|---|
| 430 | #endif
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 | int utimes(const char *pszPath, const struct timeval *paTimes)
|
|---|
| 434 | {
|
|---|
| 435 | /** @todo implement me! */
|
|---|
| 436 | return 0;
|
|---|
| 437 | }
|
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 | int writev(int fd, const struct iovec *vector, int count)
|
|---|
| 441 | {
|
|---|
| 442 | int size = 0;
|
|---|
| 443 | int i;
|
|---|
| 444 | for (i = 0; i < count; i++)
|
|---|
| 445 | {
|
|---|
| 446 | int cb = (int)write(fd, vector[i].iov_base, (int)vector[i].iov_len);
|
|---|
| 447 | if (cb < 0)
|
|---|
| 448 | return -1;
|
|---|
| 449 | size += cb;
|
|---|
| 450 | }
|
|---|
| 451 | return size;
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 | intmax_t strtoimax(const char *nptr, char **endptr, int base)
|
|---|
| 456 | {
|
|---|
| 457 | return strtol(nptr, endptr, base); /** @todo fix this. */
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 | uintmax_t strtoumax(const char *nptr, char **endptr, int base)
|
|---|
| 462 | {
|
|---|
| 463 | return strtoul(nptr, endptr, base); /** @todo fix this. */
|
|---|
| 464 | }
|
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| 467 | int asprintf(char **strp, const char *fmt, ...)
|
|---|
| 468 | {
|
|---|
| 469 | int rc;
|
|---|
| 470 | va_list va;
|
|---|
| 471 | va_start(va, fmt);
|
|---|
| 472 | rc = vasprintf(strp, fmt, va);
|
|---|
| 473 | va_end(va);
|
|---|
| 474 | return rc;
|
|---|
| 475 | }
|
|---|
| 476 |
|
|---|
| 477 |
|
|---|
| 478 | int vasprintf(char **strp, const char *fmt, va_list va)
|
|---|
| 479 | {
|
|---|
| 480 | int rc;
|
|---|
| 481 | char *psz;
|
|---|
| 482 | size_t cb = 1024;
|
|---|
| 483 |
|
|---|
| 484 | *strp = NULL;
|
|---|
| 485 | for (;;)
|
|---|
| 486 | {
|
|---|
| 487 | va_list va2;
|
|---|
| 488 |
|
|---|
| 489 | psz = malloc(cb);
|
|---|
| 490 | if (!psz)
|
|---|
| 491 | return -1;
|
|---|
| 492 |
|
|---|
| 493 | #ifdef va_copy
|
|---|
| 494 | va_copy(va2, va);
|
|---|
| 495 | rc = snprintf(psz, cb, fmt, va2);
|
|---|
| 496 | va_end(vaCopy);
|
|---|
| 497 | #else
|
|---|
| 498 | va2 = va;
|
|---|
| 499 | rc = snprintf(psz, cb, fmt, va2);
|
|---|
| 500 | #endif
|
|---|
| 501 | if (rc < 0 || (size_t)rc < cb)
|
|---|
| 502 | break;
|
|---|
| 503 | cb *= 2;
|
|---|
| 504 | free(psz);
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | *strp = psz;
|
|---|
| 508 | return rc;
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 |
|
|---|
| 512 | #undef stat
|
|---|
| 513 | /*
|
|---|
| 514 | * Workaround for directory names with trailing slashes.
|
|---|
| 515 | * Added by bird reasons stated.
|
|---|
| 516 | */
|
|---|
| 517 | int
|
|---|
| 518 | my_other_stat(const char *path, struct stat *st)
|
|---|
| 519 | {
|
|---|
| 520 | int rc = stat(path, st);
|
|---|
| 521 | if ( rc != 0
|
|---|
| 522 | && errno == ENOENT
|
|---|
| 523 | && *path != '\0')
|
|---|
| 524 | {
|
|---|
| 525 | char *slash = strchr(path, '\0') - 1;
|
|---|
| 526 | if (*slash == '/' || *slash == '\\')
|
|---|
| 527 | {
|
|---|
| 528 | size_t len_path = slash - path + 1;
|
|---|
| 529 | char *tmp = alloca(len_path + 4);
|
|---|
| 530 | memcpy(tmp, path, len_path);
|
|---|
| 531 | tmp[len_path] = '.';
|
|---|
| 532 | tmp[len_path + 1] = '\0';
|
|---|
| 533 | errno = 0;
|
|---|
| 534 | rc = stat(tmp, st);
|
|---|
| 535 | if ( rc == 0
|
|---|
| 536 | && !S_ISDIR(st->st_mode))
|
|---|
| 537 | {
|
|---|
| 538 | errno = ENOTDIR;
|
|---|
| 539 | rc = -1;
|
|---|
| 540 | }
|
|---|
| 541 | }
|
|---|
| 542 | }
|
|---|
| 543 | return rc;
|
|---|
| 544 | }
|
|---|
| 545 |
|
|---|