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