| 1 | /* $Id: mscfakes.c 785 2007-01-24 22:21:56Z 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 "err.h"
|
|---|
| 34 | #include "mscfakes.h"
|
|---|
| 35 | #undef mkdir
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | char *dirname(char *path)
|
|---|
| 39 | {
|
|---|
| 40 | /** @todo later */
|
|---|
| 41 | return path;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | int link(const char *pszDst, const char *pszLink)
|
|---|
| 46 | {
|
|---|
| 47 | errno = ENOSYS;
|
|---|
| 48 | err(1, "link() is not implemented on windows!");
|
|---|
| 49 | return -1;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | int mkdir_msc(const char *path, mode_t mode)
|
|---|
| 54 | {
|
|---|
| 55 | int rc = mkdir(path);
|
|---|
| 56 | if (rc)
|
|---|
| 57 | {
|
|---|
| 58 | int len = strlen(path);
|
|---|
| 59 | if (len > 0 && (path[len - 1] == '/' || path[len - 1] == '\\'))
|
|---|
| 60 | {
|
|---|
| 61 | char *str = strdup(path);
|
|---|
| 62 | while (len > 0 && (str[len - 1] == '/' || str[len - 1] == '\\'))
|
|---|
| 63 | str[--len] = '\0';
|
|---|
| 64 | rc = mkdir(str);
|
|---|
| 65 | free(str);
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 | return rc;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 | static int doname(char *pszX, char *pszEnd)
|
|---|
| 73 | {
|
|---|
| 74 | static char s_szChars[] = "Xabcdefghijklmnopqrstuwvxyz1234567890";
|
|---|
| 75 | int rc = 0;
|
|---|
| 76 | do
|
|---|
| 77 | {
|
|---|
| 78 | char ch;
|
|---|
| 79 |
|
|---|
| 80 | pszEnd++;
|
|---|
| 81 | ch = *(strchr(s_szChars, *pszEnd) + 1);
|
|---|
| 82 | if (ch)
|
|---|
| 83 | {
|
|---|
| 84 | *pszEnd = ch;
|
|---|
| 85 | return 0;
|
|---|
| 86 | }
|
|---|
| 87 | *pszEnd = 'a';
|
|---|
| 88 | } while (pszEnd != pszX);
|
|---|
| 89 | return 1;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 | int mkstemp(char *temp)
|
|---|
| 94 | {
|
|---|
| 95 | char *pszX = strchr(temp, 'X');
|
|---|
| 96 | char *pszEnd = strchr(pszX, '\0');
|
|---|
| 97 | int cTries = 1000;
|
|---|
| 98 | while (--cTries > 0)
|
|---|
| 99 | {
|
|---|
| 100 | int fd;
|
|---|
| 101 | if (doname(pszX, pszEnd))
|
|---|
| 102 | return -1;
|
|---|
| 103 | fd = open(temp, _O_EXCL | _O_CREAT | _O_BINARY | _O_RDWR, 0777);
|
|---|
| 104 | if (fd >= 0)
|
|---|
| 105 | return fd;
|
|---|
| 106 | }
|
|---|
| 107 | return -1;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 | int symlink(const char *pszDst, const char *pszLink)
|
|---|
| 112 | {
|
|---|
| 113 | errno = ENOSYS;
|
|---|
| 114 | err(1, "symlink() is not implemented on windows!");
|
|---|
| 115 | return -1;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 | #if _MSC_VER < 1400
|
|---|
| 120 | int snprintf(char *buf, size_t size, const char *fmt, ...)
|
|---|
| 121 | {
|
|---|
| 122 | int cch;
|
|---|
| 123 | va_list args;
|
|---|
| 124 | va_start(args, fmt);
|
|---|
| 125 | cch = vsprintf(buf, fmt, args);
|
|---|
| 126 | va_end(args);
|
|---|
| 127 | return cch;
|
|---|
| 128 | }
|
|---|
| 129 | #endif
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 | int utimes(const char *pszPath, const struct timeval *paTimes)
|
|---|
| 133 | {
|
|---|
| 134 | /** @todo implement me! */
|
|---|
| 135 | return 0;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 | int writev(int fd, const struct iovec *vector, int count)
|
|---|
| 140 | {
|
|---|
| 141 | int size = 0;
|
|---|
| 142 | int i;
|
|---|
| 143 | for (i = 0; i < count; i++)
|
|---|
| 144 | {
|
|---|
| 145 | int cb = write(fd, vector[i].iov_base, vector[i].iov_len);
|
|---|
| 146 | if (cb < 0)
|
|---|
| 147 | return -1;
|
|---|
| 148 | size += cb;
|
|---|
| 149 | }
|
|---|
| 150 | return size;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 | intmax_t strtoimax(const char *nptr, char **endptr, int base)
|
|---|
| 155 | {
|
|---|
| 156 | return strtol(nptr, endptr, base); /** @todo fix this. */
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 | uintmax_t strtoumax(const char *nptr, char **endptr, int base)
|
|---|
| 161 | {
|
|---|
| 162 | return strtoul(nptr, endptr, base); /** @todo fix this. */
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | int asprintf(char **strp, const char *fmt, ...)
|
|---|
| 167 | {
|
|---|
| 168 | int rc;
|
|---|
| 169 | va_list va;
|
|---|
| 170 | va_start(va, fmt);
|
|---|
| 171 | rc = vasprintf(strp, fmt, va);
|
|---|
| 172 | va_end(va);
|
|---|
| 173 | return rc;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 | int vasprintf(char **strp, const char *fmt, va_list va)
|
|---|
| 178 | {
|
|---|
| 179 | int rc;
|
|---|
| 180 | char *psz;
|
|---|
| 181 | size_t cb = 1024;
|
|---|
| 182 |
|
|---|
| 183 | *strp = NULL;
|
|---|
| 184 | for (;;)
|
|---|
| 185 | {
|
|---|
| 186 | va_list va2;
|
|---|
| 187 |
|
|---|
| 188 | psz = malloc(cb);
|
|---|
| 189 | if (!psz)
|
|---|
| 190 | return -1;
|
|---|
| 191 |
|
|---|
| 192 | #ifdef va_copy
|
|---|
| 193 | va_copy(va2, va);
|
|---|
| 194 | rc = snprintf(psz, cb, fmt, va2);
|
|---|
| 195 | va_end(vaCopy);
|
|---|
| 196 | #else
|
|---|
| 197 | va2 = va;
|
|---|
| 198 | rc = snprintf(psz, cb, fmt, va2);
|
|---|
| 199 | #endif
|
|---|
| 200 | if (rc < 0 || (size_t)rc < cb)
|
|---|
| 201 | break;
|
|---|
| 202 | cb *= 2;
|
|---|
| 203 | free(psz);
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | *strp = psz;
|
|---|
| 207 | return rc;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|