blob: 502868f98c8d2eb3ccd8a4f201ee43278fef9eb5 [file] [log] [blame]
Victor Stinner4e314432010-10-07 21:45:39 +00001#include "Python.h"
Victor Stinnerb306d752010-10-07 22:09:40 +00002#ifdef MS_WINDOWS
3# include <windows.h>
4#endif
Victor Stinner4e314432010-10-07 21:45:39 +00005
6#ifdef HAVE_STAT
7
8/* Decode a byte string from the locale encoding with the
9 surrogateescape error handler (undecodable bytes are decoded as characters
10 in range U+DC80..U+DCFF). If a byte sequence can be decoded as a surrogate
11 character, escape the bytes using the surrogateescape error handler instead
12 of decoding them.
13
14 Use _Py_wchar2char() to encode the character string back to a byte string.
15
16 Return a pointer to a newly allocated (wide) character string (use
17 PyMem_Free() to free the memory), or NULL on error (conversion error or
18 memory error). */
19wchar_t*
20_Py_char2wchar(char* arg)
21{
22 wchar_t *res;
23#ifdef HAVE_BROKEN_MBSTOWCS
24 /* Some platforms have a broken implementation of
25 * mbstowcs which does not count the characters that
26 * would result from conversion. Use an upper bound.
27 */
28 size_t argsize = strlen(arg);
29#else
30 size_t argsize = mbstowcs(NULL, arg, 0);
31#endif
32 size_t count;
33 unsigned char *in;
34 wchar_t *out;
35#ifdef HAVE_MBRTOWC
36 mbstate_t mbs;
37#endif
38 if (argsize != (size_t)-1) {
39 res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
40 if (!res)
41 goto oom;
42 count = mbstowcs(res, arg, argsize+1);
43 if (count != (size_t)-1) {
44 wchar_t *tmp;
45 /* Only use the result if it contains no
46 surrogate characters. */
47 for (tmp = res; *tmp != 0 &&
48 (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
49 ;
50 if (*tmp == 0)
51 return res;
52 }
53 PyMem_Free(res);
54 }
55 /* Conversion failed. Fall back to escaping with surrogateescape. */
56#ifdef HAVE_MBRTOWC
57 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
58
59 /* Overallocate; as multi-byte characters are in the argument, the
60 actual output could use less memory. */
61 argsize = strlen(arg) + 1;
62 res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t));
63 if (!res) goto oom;
64 in = (unsigned char*)arg;
65 out = res;
66 memset(&mbs, 0, sizeof mbs);
67 while (argsize) {
68 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
69 if (converted == 0)
70 /* Reached end of string; null char stored. */
71 break;
72 if (converted == (size_t)-2) {
73 /* Incomplete character. This should never happen,
74 since we provide everything that we have -
75 unless there is a bug in the C library, or I
76 misunderstood how mbrtowc works. */
77 fprintf(stderr, "unexpected mbrtowc result -2\n");
78 return NULL;
79 }
80 if (converted == (size_t)-1) {
81 /* Conversion error. Escape as UTF-8b, and start over
82 in the initial shift state. */
83 *out++ = 0xdc00 + *in++;
84 argsize--;
85 memset(&mbs, 0, sizeof mbs);
86 continue;
87 }
88 if (*out >= 0xd800 && *out <= 0xdfff) {
89 /* Surrogate character. Escape the original
90 byte sequence with surrogateescape. */
91 argsize -= converted;
92 while (converted--)
93 *out++ = 0xdc00 + *in++;
94 continue;
95 }
96 /* successfully converted some bytes */
97 in += converted;
98 argsize -= converted;
99 out++;
100 }
101#else
102 /* Cannot use C locale for escaping; manually escape as if charset
103 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
104 correctly in the locale's charset, which must be an ASCII superset. */
105 res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t));
106 if (!res) goto oom;
107 in = (unsigned char*)arg;
108 out = res;
109 while(*in)
110 if(*in < 128)
111 *out++ = *in++;
112 else
113 *out++ = 0xdc00 + *in++;
114 *out = 0;
115#endif
116 return res;
117oom:
118 fprintf(stderr, "out of memory\n");
119 return NULL;
120}
121
122/* Encode a (wide) character string to the locale encoding with the
123 surrogateescape error handler (characters in range U+DC80..U+DCFF are
124 converted to bytes 0x80..0xFF).
125
126 This function is the reverse of _Py_char2wchar().
127
128 Return a pointer to a newly allocated byte string (use PyMem_Free() to free
129 the memory), or NULL on error (conversion error or memory error). */
130char*
131_Py_wchar2char(const wchar_t *text)
132{
133 const size_t len = wcslen(text);
134 char *result = NULL, *bytes = NULL;
135 size_t i, size, converted;
136 wchar_t c, buf[2];
137
138 /* The function works in two steps:
139 1. compute the length of the output buffer in bytes (size)
140 2. outputs the bytes */
141 size = 0;
142 buf[1] = 0;
143 while (1) {
144 for (i=0; i < len; i++) {
145 c = text[i];
146 if (c >= 0xdc80 && c <= 0xdcff) {
147 /* UTF-8b surrogate */
148 if (bytes != NULL) {
149 *bytes++ = c - 0xdc00;
150 size--;
151 }
152 else
153 size++;
154 continue;
155 }
156 else {
157 buf[0] = c;
158 if (bytes != NULL)
159 converted = wcstombs(bytes, buf, size);
160 else
161 converted = wcstombs(NULL, buf, 0);
162 if (converted == (size_t)-1) {
163 if (result != NULL)
164 PyMem_Free(result);
165 return NULL;
166 }
167 if (bytes != NULL) {
168 bytes += converted;
169 size -= converted;
170 }
171 else
172 size += converted;
173 }
174 }
175 if (result != NULL) {
176 *bytes = 0;
177 break;
178 }
179
180 size += 1; /* nul byte at the end */
181 result = PyMem_Malloc(size);
182 if (result == NULL)
183 return NULL;
184 bytes = result;
185 }
186 return result;
187}
188
Victor Stinner4e314432010-10-07 21:45:39 +0000189/* In principle, this should use HAVE__WSTAT, and _wstat
190 should be detected by autoconf. However, no current
191 POSIX system provides that function, so testing for
192 it is pointless.
193 Not sure whether the MS_WINDOWS guards are necessary:
194 perhaps for cygwin/mingw builds?
195*/
Victor Stinnerb306d752010-10-07 22:09:40 +0000196#if defined(HAVE_STAT) && !defined(MS_WINDOWS)
197int
198_Py_wstat(const wchar_t* path, struct stat *buf)
199{
Victor Stinner4e314432010-10-07 21:45:39 +0000200 int err;
201 char *fname;
202 fname = _Py_wchar2char(path);
203 if (fname == NULL) {
204 errno = EINVAL;
205 return -1;
206 }
207 err = stat(fname, buf);
208 PyMem_Free(fname);
209 return err;
Victor Stinner4e314432010-10-07 21:45:39 +0000210}
211#endif
212
213/* Call _wstat() on Windows, or stat() otherwise. Only fill st_mode
214 attribute on Windows. Return 0 on success, -1 on stat error or (if
215 PyErr_Occurred()) unicode error. */
216
217int
Victor Stinnera4a75952010-10-07 22:23:10 +0000218_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000219{
220#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000221 int err;
222 struct _stat wstatbuf;
223
Victor Stinnera4a75952010-10-07 22:23:10 +0000224 err = _wstat(PyUnicode_AS_UNICODE(path), &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000225 if (!err)
226 statbuf->st_mode = wstatbuf.st_mode;
227 return err;
228#else
229 int ret;
Victor Stinnera4a75952010-10-07 22:23:10 +0000230 PyObject *bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +0000231 if (bytes == NULL)
232 return -1;
233 ret = stat(PyBytes_AS_STRING(bytes), statbuf);
234 Py_DECREF(bytes);
235 return ret;
236#endif
237}
238
239FILE *
240_Py_wfopen(const wchar_t *path, const wchar_t *mode)
241{
242#ifndef MS_WINDOWS
243 FILE *f;
244 char *cpath;
245 char cmode[10];
246 size_t r;
247 r = wcstombs(cmode, mode, 10);
248 if (r == (size_t)-1 || r >= 10) {
249 errno = EINVAL;
250 return NULL;
251 }
252 cpath = _Py_wchar2char(path);
253 if (cpath == NULL)
254 return NULL;
255 f = fopen(cpath, cmode);
256 PyMem_Free(cpath);
257 return f;
258#else
259 return _wfopen(path, mode);
260#endif
261}
262
263/* Call _wfopen() on Windows, or fopen() otherwise. Return the new file
264 object on success, or NULL if the file cannot be open or (if
265 PyErr_Occurred()) on unicode error */
266
267FILE*
Victor Stinnera4a75952010-10-07 22:23:10 +0000268_Py_fopen(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +0000269{
270#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000271 wchar_t wmode[10];
272 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +0000273
274 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
275 if (usize == 0)
276 return NULL;
277
Victor Stinnera4a75952010-10-07 22:23:10 +0000278 return _wfopen(PyUnicode_AS_UNICODE(path), wmode);
Victor Stinner4e314432010-10-07 21:45:39 +0000279#else
280 FILE *f;
Victor Stinnera4a75952010-10-07 22:23:10 +0000281 PyObject *bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +0000282 if (bytes == NULL)
283 return NULL;
284 f = fopen(PyBytes_AS_STRING(bytes), mode);
285 Py_DECREF(bytes);
286 return f;
287#endif
288}
289
290#ifdef HAVE_READLINK
291int
292_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
293{
294 char *cpath;
295 char cbuf[PATH_MAX];
296 int res;
297 size_t r1;
298
299 cpath = _Py_wchar2char(path);
300 if (cpath == NULL) {
301 errno = EINVAL;
302 return -1;
303 }
304 res = (int)readlink(cpath, cbuf, PATH_MAX);
305 PyMem_Free(cpath);
306 if (res == -1)
307 return -1;
308 if (res == PATH_MAX) {
309 errno = EINVAL;
310 return -1;
311 }
312 cbuf[res] = '\0'; /* buf will be null terminated */
313 r1 = mbstowcs(buf, cbuf, bufsiz);
314 if (r1 == -1) {
315 errno = EINVAL;
316 return -1;
317 }
318 return (int)r1;
319}
320#endif
321
322#ifdef HAVE_REALPATH
323wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +0000324_Py_wrealpath(const wchar_t *path,
325 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +0000326{
327 char *cpath;
328 char cresolved_path[PATH_MAX];
329 char *res;
330 size_t r;
331 cpath = _Py_wchar2char(path);
332 if (cpath == NULL) {
333 errno = EINVAL;
334 return NULL;
335 }
336 res = realpath(cpath, cresolved_path);
337 PyMem_Free(cpath);
338 if (res == NULL)
339 return NULL;
Victor Stinner015f4d82010-10-07 22:29:53 +0000340 r = mbstowcs(resolved_path, cresolved_path, resolved_path_size);
Victor Stinner4e314432010-10-07 21:45:39 +0000341 if (r == (size_t)-1 || r >= PATH_MAX) {
342 errno = EINVAL;
343 return NULL;
344 }
345 return resolved_path;
346}
347#endif
348
349wchar_t*
350_Py_wgetcwd(wchar_t *buf, size_t size)
351{
352#ifdef MS_WINDOWS
353 return _wgetcwd(buf, size);
354#else
355 char fname[PATH_MAX];
356 if (getcwd(fname, PATH_MAX) == NULL)
357 return NULL;
358 if (mbstowcs(buf, fname, size) >= size) {
359 errno = ERANGE;
360 return NULL;
361 }
362 return buf;
363#endif
364}
365
366#endif