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