blob: 2e9ea359da0fcf322a87d14260fa1c431f357b15 [file] [log] [blame]
Victor Stinner4e314432010-10-07 21:45:39 +00001#include "Python.h"
Stefan Krah6df5cae2012-11-12 20:14:36 +01002#include "osdefs.h"
Victor Stinnerb306d752010-10-07 22:09:40 +00003#ifdef MS_WINDOWS
4# include <windows.h>
5#endif
Victor Stinner4e314432010-10-07 21:45:39 +00006
Brett Cannonefb00c02012-02-29 18:31:31 -05007#ifdef HAVE_LANGINFO_H
8#include <langinfo.h>
9#endif
10
Victor Stinner27b1ca22012-12-03 12:47:59 +010011#ifdef __APPLE__
12extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
13#endif
14
Brett Cannonefb00c02012-02-29 18:31:31 -050015PyObject *
16_Py_device_encoding(int fd)
17{
18#if defined(MS_WINDOWS) || defined(MS_WIN64)
19 UINT cp;
20#endif
21 if (!_PyVerify_fd(fd) || !isatty(fd)) {
22 Py_RETURN_NONE;
23 }
24#if defined(MS_WINDOWS) || defined(MS_WIN64)
25 if (fd == 0)
26 cp = GetConsoleCP();
27 else if (fd == 1 || fd == 2)
28 cp = GetConsoleOutputCP();
29 else
30 cp = 0;
31 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
32 has no console */
33 if (cp != 0)
34 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
35#elif defined(CODESET)
36 {
37 char *codeset = nl_langinfo(CODESET);
38 if (codeset != NULL && codeset[0] != 0)
39 return PyUnicode_FromString(codeset);
40 }
41#endif
42 Py_RETURN_NONE;
43}
44
Victor Stinner4e314432010-10-07 21:45:39 +000045#ifdef HAVE_STAT
46
47/* Decode a byte string from the locale encoding with the
48 surrogateescape error handler (undecodable bytes are decoded as characters
49 in range U+DC80..U+DCFF). If a byte sequence can be decoded as a surrogate
50 character, escape the bytes using the surrogateescape error handler instead
51 of decoding them.
52
53 Use _Py_wchar2char() to encode the character string back to a byte string.
54
Victor Stinner168e1172010-10-16 23:16:16 +000055 Return a pointer to a newly allocated wide character string (use
56 PyMem_Free() to free the memory) and write the number of written wide
57 characters excluding the null character into *size if size is not NULL, or
Victor Stinneraf02e1c2011-12-16 23:56:01 +010058 NULL on error (decoding or memory allocation error). If size is not NULL,
59 *size is set to (size_t)-1 on memory error and (size_t)-2 on decoding
60 error.
Victor Stinner19de4c32010-11-08 23:30:46 +000061
62 Conversion errors should never happen, unless there is a bug in the C
63 library. */
Victor Stinner4e314432010-10-07 21:45:39 +000064wchar_t*
Victor Stinner168e1172010-10-16 23:16:16 +000065_Py_char2wchar(const char* arg, size_t *size)
Victor Stinner4e314432010-10-07 21:45:39 +000066{
Victor Stinner27b1ca22012-12-03 12:47:59 +010067#ifdef __APPLE__
68 wchar_t *wstr;
69 wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg));
70 if (size != NULL) {
71 if (wstr != NULL)
72 *size = wcslen(wstr);
73 else
74 *size = (size_t)-1;
75 }
76 return wstr;
77#else
Victor Stinner4e314432010-10-07 21:45:39 +000078 wchar_t *res;
79#ifdef HAVE_BROKEN_MBSTOWCS
80 /* Some platforms have a broken implementation of
81 * mbstowcs which does not count the characters that
82 * would result from conversion. Use an upper bound.
83 */
84 size_t argsize = strlen(arg);
85#else
86 size_t argsize = mbstowcs(NULL, arg, 0);
87#endif
88 size_t count;
89 unsigned char *in;
90 wchar_t *out;
91#ifdef HAVE_MBRTOWC
92 mbstate_t mbs;
93#endif
94 if (argsize != (size_t)-1) {
95 res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
96 if (!res)
97 goto oom;
98 count = mbstowcs(res, arg, argsize+1);
99 if (count != (size_t)-1) {
100 wchar_t *tmp;
101 /* Only use the result if it contains no
102 surrogate characters. */
103 for (tmp = res; *tmp != 0 &&
104 (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
105 ;
Victor Stinner168e1172010-10-16 23:16:16 +0000106 if (*tmp == 0) {
107 if (size != NULL)
108 *size = count;
Victor Stinner4e314432010-10-07 21:45:39 +0000109 return res;
Victor Stinner168e1172010-10-16 23:16:16 +0000110 }
Victor Stinner4e314432010-10-07 21:45:39 +0000111 }
112 PyMem_Free(res);
113 }
114 /* Conversion failed. Fall back to escaping with surrogateescape. */
115#ifdef HAVE_MBRTOWC
116 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
117
118 /* Overallocate; as multi-byte characters are in the argument, the
119 actual output could use less memory. */
120 argsize = strlen(arg) + 1;
121 res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t));
Victor Stinner19de4c32010-11-08 23:30:46 +0000122 if (!res)
123 goto oom;
Victor Stinner4e314432010-10-07 21:45:39 +0000124 in = (unsigned char*)arg;
125 out = res;
126 memset(&mbs, 0, sizeof mbs);
127 while (argsize) {
128 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
129 if (converted == 0)
130 /* Reached end of string; null char stored. */
131 break;
132 if (converted == (size_t)-2) {
133 /* Incomplete character. This should never happen,
134 since we provide everything that we have -
135 unless there is a bug in the C library, or I
136 misunderstood how mbrtowc works. */
Victor Stinner19de4c32010-11-08 23:30:46 +0000137 PyMem_Free(res);
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100138 if (size != NULL)
139 *size = (size_t)-2;
Victor Stinner4e314432010-10-07 21:45:39 +0000140 return NULL;
141 }
142 if (converted == (size_t)-1) {
143 /* Conversion error. Escape as UTF-8b, and start over
144 in the initial shift state. */
145 *out++ = 0xdc00 + *in++;
146 argsize--;
147 memset(&mbs, 0, sizeof mbs);
148 continue;
149 }
150 if (*out >= 0xd800 && *out <= 0xdfff) {
151 /* Surrogate character. Escape the original
152 byte sequence with surrogateescape. */
153 argsize -= converted;
154 while (converted--)
155 *out++ = 0xdc00 + *in++;
156 continue;
157 }
158 /* successfully converted some bytes */
159 in += converted;
160 argsize -= converted;
161 out++;
162 }
Victor Stinner27b1ca22012-12-03 12:47:59 +0100163#else /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000164 /* Cannot use C locale for escaping; manually escape as if charset
165 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
166 correctly in the locale's charset, which must be an ASCII superset. */
167 res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t));
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100168 if (!res)
169 goto oom;
Victor Stinner4e314432010-10-07 21:45:39 +0000170 in = (unsigned char*)arg;
171 out = res;
172 while(*in)
173 if(*in < 128)
174 *out++ = *in++;
175 else
176 *out++ = 0xdc00 + *in++;
177 *out = 0;
Victor Stinner27b1ca22012-12-03 12:47:59 +0100178#endif /* HAVE_MBRTOWC */
Victor Stinner168e1172010-10-16 23:16:16 +0000179 if (size != NULL)
180 *size = out - res;
Victor Stinner4e314432010-10-07 21:45:39 +0000181 return res;
182oom:
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100183 if (size != NULL)
184 *size = (size_t)-1;
Victor Stinner4e314432010-10-07 21:45:39 +0000185 return NULL;
Victor Stinner27b1ca22012-12-03 12:47:59 +0100186#endif /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000187}
188
189/* Encode a (wide) character string to the locale encoding with the
190 surrogateescape error handler (characters in range U+DC80..U+DCFF are
191 converted to bytes 0x80..0xFF).
192
193 This function is the reverse of _Py_char2wchar().
194
195 Return a pointer to a newly allocated byte string (use PyMem_Free() to free
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100196 the memory), or NULL on encoding or memory allocation error.
Victor Stinner2f02a512010-11-08 22:43:46 +0000197
198 If error_pos is not NULL: *error_pos is the index of the invalid character
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100199 on encoding error, or (size_t)-1 otherwise. */
Victor Stinner4e314432010-10-07 21:45:39 +0000200char*
Victor Stinner2f02a512010-11-08 22:43:46 +0000201_Py_wchar2char(const wchar_t *text, size_t *error_pos)
Victor Stinner4e314432010-10-07 21:45:39 +0000202{
Victor Stinner27b1ca22012-12-03 12:47:59 +0100203#ifdef __APPLE__
204 Py_ssize_t len;
205 PyObject *unicode, *bytes = NULL;
206 char *cpath;
207
208 unicode = PyUnicode_FromWideChar(text, wcslen(text));
209 if (unicode == NULL)
210 return NULL;
211
212 bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape");
213 Py_DECREF(unicode);
214 if (bytes == NULL) {
215 PyErr_Clear();
216 if (error_pos != NULL)
217 *error_pos = (size_t)-1;
218 return NULL;
219 }
220
221 len = PyBytes_GET_SIZE(bytes);
222 cpath = PyMem_Malloc(len+1);
223 if (cpath == NULL) {
224 PyErr_Clear();
225 Py_DECREF(bytes);
226 if (error_pos != NULL)
227 *error_pos = (size_t)-1;
228 return NULL;
229 }
230 memcpy(cpath, PyBytes_AsString(bytes), len + 1);
231 Py_DECREF(bytes);
232 return cpath;
233#else /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000234 const size_t len = wcslen(text);
235 char *result = NULL, *bytes = NULL;
236 size_t i, size, converted;
237 wchar_t c, buf[2];
238
239 /* The function works in two steps:
240 1. compute the length of the output buffer in bytes (size)
241 2. outputs the bytes */
242 size = 0;
243 buf[1] = 0;
244 while (1) {
245 for (i=0; i < len; i++) {
246 c = text[i];
247 if (c >= 0xdc80 && c <= 0xdcff) {
248 /* UTF-8b surrogate */
249 if (bytes != NULL) {
250 *bytes++ = c - 0xdc00;
251 size--;
252 }
253 else
254 size++;
255 continue;
256 }
257 else {
258 buf[0] = c;
259 if (bytes != NULL)
260 converted = wcstombs(bytes, buf, size);
261 else
262 converted = wcstombs(NULL, buf, 0);
263 if (converted == (size_t)-1) {
264 if (result != NULL)
265 PyMem_Free(result);
Victor Stinner2f02a512010-11-08 22:43:46 +0000266 if (error_pos != NULL)
267 *error_pos = i;
Victor Stinner4e314432010-10-07 21:45:39 +0000268 return NULL;
269 }
270 if (bytes != NULL) {
271 bytes += converted;
272 size -= converted;
273 }
274 else
275 size += converted;
276 }
277 }
278 if (result != NULL) {
279 *bytes = 0;
280 break;
281 }
282
283 size += 1; /* nul byte at the end */
284 result = PyMem_Malloc(size);
Victor Stinner27b1ca22012-12-03 12:47:59 +0100285 if (result == NULL) {
286 if (error_pos != NULL)
287 *error_pos = (size_t)-1;
Victor Stinner4e314432010-10-07 21:45:39 +0000288 return NULL;
Victor Stinner27b1ca22012-12-03 12:47:59 +0100289 }
Victor Stinner4e314432010-10-07 21:45:39 +0000290 bytes = result;
291 }
292 return result;
Victor Stinner27b1ca22012-12-03 12:47:59 +0100293#endif /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000294}
295
Victor Stinner4e314432010-10-07 21:45:39 +0000296/* In principle, this should use HAVE__WSTAT, and _wstat
297 should be detected by autoconf. However, no current
298 POSIX system provides that function, so testing for
299 it is pointless.
300 Not sure whether the MS_WINDOWS guards are necessary:
301 perhaps for cygwin/mingw builds?
302*/
Victor Stinnerb306d752010-10-07 22:09:40 +0000303#if defined(HAVE_STAT) && !defined(MS_WINDOWS)
Victor Stinner6672d0c2010-10-07 22:53:43 +0000304
305/* Get file status. Encode the path to the locale encoding. */
306
Victor Stinnerb306d752010-10-07 22:09:40 +0000307int
308_Py_wstat(const wchar_t* path, struct stat *buf)
309{
Victor Stinner4e314432010-10-07 21:45:39 +0000310 int err;
311 char *fname;
Victor Stinner2f02a512010-11-08 22:43:46 +0000312 fname = _Py_wchar2char(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +0000313 if (fname == NULL) {
314 errno = EINVAL;
315 return -1;
316 }
317 err = stat(fname, buf);
318 PyMem_Free(fname);
319 return err;
Victor Stinner4e314432010-10-07 21:45:39 +0000320}
321#endif
322
Victor Stinner6672d0c2010-10-07 22:53:43 +0000323/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
324 call stat() otherwise. Only fill st_mode attribute on Windows.
325
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100326 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
327 raised. */
Victor Stinner4e314432010-10-07 21:45:39 +0000328
329int
Victor Stinnera4a75952010-10-07 22:23:10 +0000330_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000331{
332#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000333 int err;
334 struct _stat wstatbuf;
Victor Stinneree587ea2011-11-17 00:51:38 +0100335 wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000336
Victor Stinneree587ea2011-11-17 00:51:38 +0100337 wpath = PyUnicode_AsUnicode(path);
338 if (wpath == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100339 return -2;
Victor Stinneree587ea2011-11-17 00:51:38 +0100340 err = _wstat(wpath, &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000341 if (!err)
342 statbuf->st_mode = wstatbuf.st_mode;
343 return err;
344#else
345 int ret;
Victor Stinnera4a75952010-10-07 22:23:10 +0000346 PyObject *bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +0000347 if (bytes == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100348 return -2;
Victor Stinner4e314432010-10-07 21:45:39 +0000349 ret = stat(PyBytes_AS_STRING(bytes), statbuf);
350 Py_DECREF(bytes);
351 return ret;
352#endif
353}
354
Victor Stinner6672d0c2010-10-07 22:53:43 +0000355/* Open a file. Use _wfopen() on Windows, encode the path to the locale
356 encoding and use fopen() otherwise. */
357
Victor Stinner4e314432010-10-07 21:45:39 +0000358FILE *
359_Py_wfopen(const wchar_t *path, const wchar_t *mode)
360{
361#ifndef MS_WINDOWS
362 FILE *f;
363 char *cpath;
364 char cmode[10];
365 size_t r;
366 r = wcstombs(cmode, mode, 10);
367 if (r == (size_t)-1 || r >= 10) {
368 errno = EINVAL;
369 return NULL;
370 }
Victor Stinner2f02a512010-11-08 22:43:46 +0000371 cpath = _Py_wchar2char(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +0000372 if (cpath == NULL)
373 return NULL;
374 f = fopen(cpath, cmode);
375 PyMem_Free(cpath);
376 return f;
377#else
378 return _wfopen(path, mode);
379#endif
380}
381
Victor Stinner6672d0c2010-10-07 22:53:43 +0000382/* Call _wfopen() on Windows, or encode the path to the filesystem encoding and
383 call fopen() otherwise.
384
385 Return the new file object on success, or NULL if the file cannot be open or
386 (if PyErr_Occurred()) on unicode error */
Victor Stinner4e314432010-10-07 21:45:39 +0000387
388FILE*
Victor Stinnera4a75952010-10-07 22:23:10 +0000389_Py_fopen(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +0000390{
391#ifdef MS_WINDOWS
Victor Stinneree587ea2011-11-17 00:51:38 +0100392 wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000393 wchar_t wmode[10];
394 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +0000395
Antoine Pitrou0e576f12011-12-22 10:03:38 +0100396 if (!PyUnicode_Check(path)) {
397 PyErr_Format(PyExc_TypeError,
398 "str file path expected under Windows, got %R",
399 Py_TYPE(path));
400 return NULL;
401 }
Victor Stinneree587ea2011-11-17 00:51:38 +0100402 wpath = PyUnicode_AsUnicode(path);
403 if (wpath == NULL)
404 return NULL;
405
Victor Stinner4e314432010-10-07 21:45:39 +0000406 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
407 if (usize == 0)
408 return NULL;
409
Victor Stinneree587ea2011-11-17 00:51:38 +0100410 return _wfopen(wpath, wmode);
Victor Stinner4e314432010-10-07 21:45:39 +0000411#else
412 FILE *f;
Antoine Pitrou2b1cc892011-12-19 18:19:06 +0100413 PyObject *bytes;
414 if (!PyUnicode_FSConverter(path, &bytes))
Victor Stinner4e314432010-10-07 21:45:39 +0000415 return NULL;
416 f = fopen(PyBytes_AS_STRING(bytes), mode);
417 Py_DECREF(bytes);
418 return f;
419#endif
420}
421
422#ifdef HAVE_READLINK
Victor Stinner6672d0c2010-10-07 22:53:43 +0000423
424/* Read value of symbolic link. Encode the path to the locale encoding, decode
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100425 the result from the locale encoding. Return -1 on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +0000426
Victor Stinner4e314432010-10-07 21:45:39 +0000427int
428_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
429{
430 char *cpath;
431 char cbuf[PATH_MAX];
Victor Stinner3f711f42010-10-16 22:47:37 +0000432 wchar_t *wbuf;
Victor Stinner4e314432010-10-07 21:45:39 +0000433 int res;
434 size_t r1;
435
Victor Stinner2f02a512010-11-08 22:43:46 +0000436 cpath = _Py_wchar2char(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +0000437 if (cpath == NULL) {
438 errno = EINVAL;
439 return -1;
440 }
441 res = (int)readlink(cpath, cbuf, PATH_MAX);
442 PyMem_Free(cpath);
443 if (res == -1)
444 return -1;
445 if (res == PATH_MAX) {
446 errno = EINVAL;
447 return -1;
448 }
449 cbuf[res] = '\0'; /* buf will be null terminated */
Victor Stinner168e1172010-10-16 23:16:16 +0000450 wbuf = _Py_char2wchar(cbuf, &r1);
Victor Stinner350147b2010-10-16 22:52:09 +0000451 if (wbuf == NULL) {
452 errno = EINVAL;
453 return -1;
454 }
Victor Stinner3f711f42010-10-16 22:47:37 +0000455 if (bufsiz <= r1) {
456 PyMem_Free(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000457 errno = EINVAL;
458 return -1;
459 }
Victor Stinner3f711f42010-10-16 22:47:37 +0000460 wcsncpy(buf, wbuf, bufsiz);
461 PyMem_Free(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000462 return (int)r1;
463}
464#endif
465
466#ifdef HAVE_REALPATH
Victor Stinner6672d0c2010-10-07 22:53:43 +0000467
468/* Return the canonicalized absolute pathname. Encode path to the locale
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100469 encoding, decode the result from the locale encoding.
470 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +0000471
Victor Stinner4e314432010-10-07 21:45:39 +0000472wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +0000473_Py_wrealpath(const wchar_t *path,
474 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +0000475{
476 char *cpath;
477 char cresolved_path[PATH_MAX];
Victor Stinner0a1b8cb2010-10-16 22:55:47 +0000478 wchar_t *wresolved_path;
Victor Stinner4e314432010-10-07 21:45:39 +0000479 char *res;
480 size_t r;
Victor Stinner2f02a512010-11-08 22:43:46 +0000481 cpath = _Py_wchar2char(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +0000482 if (cpath == NULL) {
483 errno = EINVAL;
484 return NULL;
485 }
486 res = realpath(cpath, cresolved_path);
487 PyMem_Free(cpath);
488 if (res == NULL)
489 return NULL;
Victor Stinner0a1b8cb2010-10-16 22:55:47 +0000490
Victor Stinner168e1172010-10-16 23:16:16 +0000491 wresolved_path = _Py_char2wchar(cresolved_path, &r);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +0000492 if (wresolved_path == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +0000493 errno = EINVAL;
494 return NULL;
495 }
Victor Stinner0a1b8cb2010-10-16 22:55:47 +0000496 if (resolved_path_size <= r) {
497 PyMem_Free(wresolved_path);
498 errno = EINVAL;
499 return NULL;
500 }
501 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
502 PyMem_Free(wresolved_path);
Victor Stinner4e314432010-10-07 21:45:39 +0000503 return resolved_path;
504}
505#endif
506
Victor Stinnerf4061da2010-10-14 12:37:19 +0000507/* Get the current directory. size is the buffer size in wide characters
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100508 including the null character. Decode the path from the locale encoding.
509 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +0000510
Victor Stinner4e314432010-10-07 21:45:39 +0000511wchar_t*
512_Py_wgetcwd(wchar_t *buf, size_t size)
513{
514#ifdef MS_WINDOWS
515 return _wgetcwd(buf, size);
516#else
517 char fname[PATH_MAX];
Victor Stinnerf4061da2010-10-14 12:37:19 +0000518 wchar_t *wname;
Victor Stinner168e1172010-10-16 23:16:16 +0000519 size_t len;
Victor Stinnerf4061da2010-10-14 12:37:19 +0000520
Victor Stinner4e314432010-10-07 21:45:39 +0000521 if (getcwd(fname, PATH_MAX) == NULL)
522 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +0000523 wname = _Py_char2wchar(fname, &len);
Victor Stinnerf4061da2010-10-14 12:37:19 +0000524 if (wname == NULL)
525 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +0000526 if (size <= len) {
Victor Stinnerf4061da2010-10-14 12:37:19 +0000527 PyMem_Free(wname);
Victor Stinner4e314432010-10-07 21:45:39 +0000528 return NULL;
529 }
Victor Stinnerf4061da2010-10-14 12:37:19 +0000530 wcsncpy(buf, wname, size);
531 PyMem_Free(wname);
Victor Stinner4e314432010-10-07 21:45:39 +0000532 return buf;
533#endif
534}
535
536#endif