| 1 | /* $Id: mscfakes.c 1321 2007-12-02 10:28:19Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * Fake Unix stuff for MSC.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2005-2007 knut st. osmundsen <bird-kBuild-spam@anduin.net>
|
|---|
| 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 | char *dirname(char *path)
|
|---|
| 43 | {
|
|---|
| 44 | /** @todo later */
|
|---|
| 45 | return path;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | int link(const char *pszDst, const char *pszLink)
|
|---|
| 50 | {
|
|---|
| 51 | errno = ENOSYS;
|
|---|
| 52 | err(1, "link() is not implemented on windows!");
|
|---|
| 53 | return -1;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | int mkdir_msc(const char *path, mode_t mode)
|
|---|
| 58 | {
|
|---|
| 59 | int rc = (mkdir)(path);
|
|---|
| 60 | if (rc)
|
|---|
| 61 | {
|
|---|
| 62 | size_t len = strlen(path);
|
|---|
| 63 | if (len > 0 && (path[len - 1] == '/' || path[len - 1] == '\\'))
|
|---|
| 64 | {
|
|---|
| 65 | char *str = strdup(path);
|
|---|
| 66 | while (len > 0 && (str[len - 1] == '/' || str[len - 1] == '\\'))
|
|---|
| 67 | str[--len] = '\0';
|
|---|
| 68 | rc = (mkdir)(str);
|
|---|
| 69 | free(str);
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 | return rc;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | int rmdir_msc(const char *path)
|
|---|
| 76 | {
|
|---|
| 77 | int rc = (rmdir)(path);
|
|---|
| 78 | if (rc)
|
|---|
| 79 | {
|
|---|
| 80 | size_t len = strlen(path);
|
|---|
| 81 | if (len > 0 && (path[len - 1] == '/' || path[len - 1] == '\\'))
|
|---|
| 82 | {
|
|---|
| 83 | char *str = strdup(path);
|
|---|
| 84 | while (len > 0 && (str[len - 1] == '/' || str[len - 1] == '\\'))
|
|---|
| 85 | str[--len] = '\0';
|
|---|
| 86 | rc = (rmdir)(str);
|
|---|
| 87 | free(str);
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 | return rc;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 | static int doname(char *pszX, char *pszEnd)
|
|---|
| 95 | {
|
|---|
| 96 | static char s_szChars[] = "Xabcdefghijklmnopqrstuwvxyz1234567890";
|
|---|
| 97 | int rc = 0;
|
|---|
| 98 | do
|
|---|
| 99 | {
|
|---|
| 100 | char ch;
|
|---|
| 101 |
|
|---|
| 102 | pszEnd++;
|
|---|
| 103 | ch = *(strchr(s_szChars, *pszEnd) + 1);
|
|---|
| 104 | if (ch)
|
|---|
| 105 | {
|
|---|
| 106 | *pszEnd = ch;
|
|---|
| 107 | return 0;
|
|---|
| 108 | }
|
|---|
| 109 | *pszEnd = 'a';
|
|---|
| 110 | } while (pszEnd != pszX);
|
|---|
| 111 | return 1;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 | int mkstemp(char *temp)
|
|---|
| 116 | {
|
|---|
| 117 | char *pszX = strchr(temp, 'X');
|
|---|
| 118 | char *pszEnd = strchr(pszX, '\0');
|
|---|
| 119 | int cTries = 1000;
|
|---|
| 120 | while (--cTries > 0)
|
|---|
| 121 | {
|
|---|
| 122 | int fd;
|
|---|
| 123 | if (doname(pszX, pszEnd))
|
|---|
| 124 | return -1;
|
|---|
| 125 | fd = open(temp, _O_EXCL | _O_CREAT | _O_BINARY | _O_RDWR, 0777);
|
|---|
| 126 | if (fd >= 0)
|
|---|
| 127 | return fd;
|
|---|
| 128 | }
|
|---|
| 129 | return -1;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 | /** Unix to DOS. */
|
|---|
| 134 | static char *fix_slashes(char *psz)
|
|---|
| 135 | {
|
|---|
| 136 | char *pszRet = psz;
|
|---|
| 137 | for (; *psz; psz++)
|
|---|
| 138 | if (*psz == '/')
|
|---|
| 139 | *psz = '\\';
|
|---|
| 140 | return pszRet;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 | /** Calcs the SYMBOLIC_LINK_FLAG_DIRECTORY flag for CreatesymbolcLink. */
|
|---|
| 145 | static DWORD is_directory(const char *pszPath, const char *pszRelativeTo)
|
|---|
| 146 | {
|
|---|
| 147 | size_t cchPath = strlen(pszPath);
|
|---|
| 148 | struct stat st;
|
|---|
| 149 | if (cchPath > 0 && pszPath[cchPath - 1] == '\\' || pszPath[cchPath - 1] == '/')
|
|---|
| 150 | return 1; /* SYMBOLIC_LINK_FLAG_DIRECTORY */
|
|---|
| 151 |
|
|---|
| 152 | if (stat(pszPath, &st))
|
|---|
| 153 | {
|
|---|
| 154 | size_t cchRelativeTo = strlen(pszRelativeTo);
|
|---|
| 155 | char *psz = malloc(cchPath + cchRelativeTo + 4);
|
|---|
| 156 | memcpy(psz, pszRelativeTo, cchRelativeTo);
|
|---|
| 157 | memcpy(psz + cchRelativeTo, "\\", 1);
|
|---|
| 158 | memcpy(psz + cchRelativeTo + 1, pszPath, cchPath + 1);
|
|---|
| 159 | if (stat(pszPath, &st))
|
|---|
| 160 | st.st_mode = _S_IFREG;
|
|---|
| 161 | free(psz);
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | return (st.st_mode & _S_IFMT) == _S_IFDIR ? 1 : 0;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 | int symlink(const char *pszDst, const char *pszLink)
|
|---|
| 169 | {
|
|---|
| 170 | static BOOL (WINAPI *s_pfnCreateSymbolicLinkA)(LPCSTR, LPCSTR, DWORD) = 0;
|
|---|
| 171 | static BOOL s_fTried = FALSE;
|
|---|
| 172 |
|
|---|
| 173 | if (!s_fTried)
|
|---|
| 174 | {
|
|---|
| 175 | HMODULE hmod = LoadLibrary("KERNEL32.DLL");
|
|---|
| 176 | if (hmod)
|
|---|
| 177 | *(FARPROC *)&s_pfnCreateSymbolicLinkA = GetProcAddress(hmod, "CreateSymbolicLinkA");
|
|---|
| 178 | s_fTried = TRUE;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | if (s_pfnCreateSymbolicLinkA)
|
|---|
| 182 | {
|
|---|
| 183 | char *pszDstCopy = fix_slashes(strdup(pszDst));
|
|---|
| 184 | char *pszLinkCopy = fix_slashes(strdup(pszLink));
|
|---|
| 185 | BOOL fRc = s_pfnCreateSymbolicLinkA(pszLinkCopy, pszDstCopy,
|
|---|
| 186 | is_directory(pszDstCopy, pszLinkCopy));
|
|---|
| 187 | DWORD err = GetLastError();
|
|---|
| 188 | free(pszDstCopy);
|
|---|
| 189 | free(pszLinkCopy);
|
|---|
| 190 | if (fRc)
|
|---|
| 191 | return 0;
|
|---|
| 192 | switch (err)
|
|---|
| 193 | {
|
|---|
| 194 | case ERROR_NOT_SUPPORTED: errno = ENOSYS; break;
|
|---|
| 195 | case ERROR_ALREADY_EXISTS:
|
|---|
| 196 | case ERROR_FILE_EXISTS: errno = EEXIST; break;
|
|---|
| 197 | case ERROR_DIRECTORY: errno = ENOTDIR; break;
|
|---|
| 198 | case ERROR_ACCESS_DENIED:
|
|---|
| 199 | case ERROR_PRIVILEGE_NOT_HELD: errno = EPERM; break;
|
|---|
| 200 | default: errno = EINVAL; break;
|
|---|
| 201 | }
|
|---|
| 202 | return -1;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | errno = ENOSYS;
|
|---|
| 206 | err(1, "symlink() is not implemented on windows!");
|
|---|
| 207 | return -1;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 | #if _MSC_VER < 1400
|
|---|
| 212 | int snprintf(char *buf, size_t size, const char *fmt, ...)
|
|---|
| 213 | {
|
|---|
| 214 | int cch;
|
|---|
| 215 | va_list args;
|
|---|
| 216 | va_start(args, fmt);
|
|---|
| 217 | cch = vsprintf(buf, fmt, args);
|
|---|
| 218 | va_end(args);
|
|---|
| 219 | return cch;
|
|---|
| 220 | }
|
|---|
| 221 | #endif
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 | int utimes(const char *pszPath, const struct timeval *paTimes)
|
|---|
| 225 | {
|
|---|
| 226 | /** @todo implement me! */
|
|---|
| 227 | return 0;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 | int writev(int fd, const struct iovec *vector, int count)
|
|---|
| 232 | {
|
|---|
| 233 | int size = 0;
|
|---|
| 234 | int i;
|
|---|
| 235 | for (i = 0; i < count; i++)
|
|---|
| 236 | {
|
|---|
| 237 | int cb = (int)write(fd, vector[i].iov_base, (int)vector[i].iov_len);
|
|---|
| 238 | if (cb < 0)
|
|---|
| 239 | return -1;
|
|---|
| 240 | size += cb;
|
|---|
| 241 | }
|
|---|
| 242 | return size;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 | intmax_t strtoimax(const char *nptr, char **endptr, int base)
|
|---|
| 247 | {
|
|---|
| 248 | return strtol(nptr, endptr, base); /** @todo fix this. */
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 | uintmax_t strtoumax(const char *nptr, char **endptr, int base)
|
|---|
| 253 | {
|
|---|
| 254 | return strtoul(nptr, endptr, base); /** @todo fix this. */
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 | int asprintf(char **strp, const char *fmt, ...)
|
|---|
| 259 | {
|
|---|
| 260 | int rc;
|
|---|
| 261 | va_list va;
|
|---|
| 262 | va_start(va, fmt);
|
|---|
| 263 | rc = vasprintf(strp, fmt, va);
|
|---|
| 264 | va_end(va);
|
|---|
| 265 | return rc;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 | int vasprintf(char **strp, const char *fmt, va_list va)
|
|---|
| 270 | {
|
|---|
| 271 | int rc;
|
|---|
| 272 | char *psz;
|
|---|
| 273 | size_t cb = 1024;
|
|---|
| 274 |
|
|---|
| 275 | *strp = NULL;
|
|---|
| 276 | for (;;)
|
|---|
| 277 | {
|
|---|
| 278 | va_list va2;
|
|---|
| 279 |
|
|---|
| 280 | psz = malloc(cb);
|
|---|
| 281 | if (!psz)
|
|---|
| 282 | return -1;
|
|---|
| 283 |
|
|---|
| 284 | #ifdef va_copy
|
|---|
| 285 | va_copy(va2, va);
|
|---|
| 286 | rc = snprintf(psz, cb, fmt, va2);
|
|---|
| 287 | va_end(vaCopy);
|
|---|
| 288 | #else
|
|---|
| 289 | va2 = va;
|
|---|
| 290 | rc = snprintf(psz, cb, fmt, va2);
|
|---|
| 291 | #endif
|
|---|
| 292 | if (rc < 0 || (size_t)rc < cb)
|
|---|
| 293 | break;
|
|---|
| 294 | cb *= 2;
|
|---|
| 295 | free(psz);
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | *strp = psz;
|
|---|
| 299 | return rc;
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 | #undef stat
|
|---|
| 304 | /*
|
|---|
| 305 | * Workaround for directory names with trailing slashes.
|
|---|
| 306 | * Added by bird reasons stated.
|
|---|
| 307 | */
|
|---|
| 308 | int
|
|---|
| 309 | my_other_stat(const char *path, struct stat *st)
|
|---|
| 310 | {
|
|---|
| 311 | int rc = stat(path, st);
|
|---|
| 312 | if ( rc != 0
|
|---|
| 313 | && errno == ENOENT
|
|---|
| 314 | && *path != '\0')
|
|---|
| 315 | {
|
|---|
| 316 | char *slash = strchr(path, '\0') - 1;
|
|---|
| 317 | if (*slash == '/' || *slash == '\\')
|
|---|
| 318 | {
|
|---|
| 319 | size_t len_path = slash - path + 1;
|
|---|
| 320 | char *tmp = alloca(len_path + 4);
|
|---|
| 321 | memcpy(tmp, path, len_path);
|
|---|
| 322 | tmp[len_path] = '.';
|
|---|
| 323 | tmp[len_path + 1] = '\0';
|
|---|
| 324 | errno = 0;
|
|---|
| 325 | rc = stat(tmp, st);
|
|---|
| 326 | if ( rc == 0
|
|---|
| 327 | && !S_ISDIR(st->st_mode))
|
|---|
| 328 | {
|
|---|
| 329 | errno = ENOTDIR;
|
|---|
| 330 | rc = -1;
|
|---|
| 331 | }
|
|---|
| 332 | }
|
|---|
| 333 | }
|
|---|
| 334 | return rc;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|