blob: 42a532d9fa98acf05ba74cff90b1cb08f3619cad [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 Stinnere2623772012-11-12 23:04:02 +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 Stinnere2623772012-11-12 23:04:02 +010067#ifdef __APPLE__
68 wchar_t *wstr;
69 wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg));
70 if (wstr == NULL)
71 return NULL;
72 if (size != NULL)
73 *size = wcslen(wstr);
74 return wstr;
75#else
Victor Stinner4e314432010-10-07 21:45:39 +000076 wchar_t *res;
77#ifdef HAVE_BROKEN_MBSTOWCS
78 /* Some platforms have a broken implementation of
79 * mbstowcs which does not count the characters that
80 * would result from conversion. Use an upper bound.
81 */
82 size_t argsize = strlen(arg);
83#else
84 size_t argsize = mbstowcs(NULL, arg, 0);
85#endif
86 size_t count;
87 unsigned char *in;
88 wchar_t *out;
89#ifdef HAVE_MBRTOWC
90 mbstate_t mbs;
91#endif
92 if (argsize != (size_t)-1) {
93 res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
94 if (!res)
95 goto oom;
96 count = mbstowcs(res, arg, argsize+1);
97 if (count != (size_t)-1) {
98 wchar_t *tmp;
99 /* Only use the result if it contains no
100 surrogate characters. */
101 for (tmp = res; *tmp != 0 &&
Victor Stinner76df43d2012-10-30 01:42:39 +0100102 !Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
Victor Stinner4e314432010-10-07 21:45:39 +0000103 ;
Victor Stinner168e1172010-10-16 23:16:16 +0000104 if (*tmp == 0) {
105 if (size != NULL)
106 *size = count;
Victor Stinner4e314432010-10-07 21:45:39 +0000107 return res;
Victor Stinner168e1172010-10-16 23:16:16 +0000108 }
Victor Stinner4e314432010-10-07 21:45:39 +0000109 }
110 PyMem_Free(res);
111 }
112 /* Conversion failed. Fall back to escaping with surrogateescape. */
113#ifdef HAVE_MBRTOWC
114 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
115
116 /* Overallocate; as multi-byte characters are in the argument, the
117 actual output could use less memory. */
118 argsize = strlen(arg) + 1;
119 res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t));
Victor Stinner19de4c32010-11-08 23:30:46 +0000120 if (!res)
121 goto oom;
Victor Stinner4e314432010-10-07 21:45:39 +0000122 in = (unsigned char*)arg;
123 out = res;
124 memset(&mbs, 0, sizeof mbs);
125 while (argsize) {
126 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
127 if (converted == 0)
128 /* Reached end of string; null char stored. */
129 break;
130 if (converted == (size_t)-2) {
131 /* Incomplete character. This should never happen,
132 since we provide everything that we have -
133 unless there is a bug in the C library, or I
134 misunderstood how mbrtowc works. */
Victor Stinner19de4c32010-11-08 23:30:46 +0000135 PyMem_Free(res);
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100136 if (size != NULL)
137 *size = (size_t)-2;
Victor Stinner4e314432010-10-07 21:45:39 +0000138 return NULL;
139 }
140 if (converted == (size_t)-1) {
141 /* Conversion error. Escape as UTF-8b, and start over
142 in the initial shift state. */
143 *out++ = 0xdc00 + *in++;
144 argsize--;
145 memset(&mbs, 0, sizeof mbs);
146 continue;
147 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100148 if (Py_UNICODE_IS_SURROGATE(*out)) {
Victor Stinner4e314432010-10-07 21:45:39 +0000149 /* Surrogate character. Escape the original
150 byte sequence with surrogateescape. */
151 argsize -= converted;
152 while (converted--)
153 *out++ = 0xdc00 + *in++;
154 continue;
155 }
156 /* successfully converted some bytes */
157 in += converted;
158 argsize -= converted;
159 out++;
160 }
Victor Stinnere2623772012-11-12 23:04:02 +0100161#else /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000162 /* Cannot use C locale for escaping; manually escape as if charset
163 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
164 correctly in the locale's charset, which must be an ASCII superset. */
165 res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t));
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100166 if (!res)
167 goto oom;
Victor Stinner4e314432010-10-07 21:45:39 +0000168 in = (unsigned char*)arg;
169 out = res;
170 while(*in)
171 if(*in < 128)
172 *out++ = *in++;
173 else
174 *out++ = 0xdc00 + *in++;
175 *out = 0;
Victor Stinnere2623772012-11-12 23:04:02 +0100176#endif /* HAVE_MBRTOWC */
Victor Stinner168e1172010-10-16 23:16:16 +0000177 if (size != NULL)
178 *size = out - res;
Victor Stinner4e314432010-10-07 21:45:39 +0000179 return res;
180oom:
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100181 if (size != NULL)
182 *size = (size_t)-1;
Victor Stinner4e314432010-10-07 21:45:39 +0000183 return NULL;
Victor Stinnere2623772012-11-12 23:04:02 +0100184#endif /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000185}
186
187/* Encode a (wide) character string to the locale encoding with the
188 surrogateescape error handler (characters in range U+DC80..U+DCFF are
189 converted to bytes 0x80..0xFF).
190
191 This function is the reverse of _Py_char2wchar().
192
193 Return a pointer to a newly allocated byte string (use PyMem_Free() to free
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100194 the memory), or NULL on encoding or memory allocation error.
Victor Stinner2f02a512010-11-08 22:43:46 +0000195
196 If error_pos is not NULL: *error_pos is the index of the invalid character
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100197 on encoding error, or (size_t)-1 otherwise. */
Victor Stinner4e314432010-10-07 21:45:39 +0000198char*
Victor Stinner2f02a512010-11-08 22:43:46 +0000199_Py_wchar2char(const wchar_t *text, size_t *error_pos)
Victor Stinner4e314432010-10-07 21:45:39 +0000200{
Victor Stinnere2623772012-11-12 23:04:02 +0100201#ifdef __APPLE__
202 Py_ssize_t len;
203 PyObject *unicode, *bytes = NULL;
204 char *cpath;
205
206 unicode = PyUnicode_FromWideChar(text, wcslen(text));
207 if (unicode == NULL) {
208 Py_DECREF(unicode);
209 return NULL;
210 }
211
212 bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape");
213 Py_DECREF(unicode);
214 if (bytes == NULL) {
215 PyErr_Clear();
216 return NULL;
217 }
218
219 len = PyBytes_GET_SIZE(bytes);
220 cpath = PyMem_Malloc(len+1);
221 if (cpath == NULL) {
222 Py_DECREF(bytes);
223 return NULL;
224 }
225 memcpy(cpath, PyBytes_AsString(bytes), len + 1);
226 Py_DECREF(bytes);
227 return cpath;
228#else /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000229 const size_t len = wcslen(text);
230 char *result = NULL, *bytes = NULL;
231 size_t i, size, converted;
232 wchar_t c, buf[2];
233
Victor Stinner2f02a512010-11-08 22:43:46 +0000234 if (error_pos != NULL)
235 *error_pos = (size_t)-1;
236
Victor Stinner4e314432010-10-07 21:45:39 +0000237 /* The function works in two steps:
238 1. compute the length of the output buffer in bytes (size)
239 2. outputs the bytes */
240 size = 0;
241 buf[1] = 0;
242 while (1) {
243 for (i=0; i < len; i++) {
244 c = text[i];
245 if (c >= 0xdc80 && c <= 0xdcff) {
246 /* UTF-8b surrogate */
247 if (bytes != NULL) {
248 *bytes++ = c - 0xdc00;
249 size--;
250 }
251 else
252 size++;
253 continue;
254 }
255 else {
256 buf[0] = c;
257 if (bytes != NULL)
258 converted = wcstombs(bytes, buf, size);
259 else
260 converted = wcstombs(NULL, buf, 0);
261 if (converted == (size_t)-1) {
262 if (result != NULL)
263 PyMem_Free(result);
Victor Stinner2f02a512010-11-08 22:43:46 +0000264 if (error_pos != NULL)
265 *error_pos = i;
Victor Stinner4e314432010-10-07 21:45:39 +0000266 return NULL;
267 }
268 if (bytes != NULL) {
269 bytes += converted;
270 size -= converted;
271 }
272 else
273 size += converted;
274 }
275 }
276 if (result != NULL) {
277 *bytes = 0;
278 break;
279 }
280
281 size += 1; /* nul byte at the end */
282 result = PyMem_Malloc(size);
283 if (result == NULL)
284 return NULL;
285 bytes = result;
286 }
287 return result;
Victor Stinnere2623772012-11-12 23:04:02 +0100288#endif /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000289}
290
Victor Stinner4e314432010-10-07 21:45:39 +0000291/* In principle, this should use HAVE__WSTAT, and _wstat
292 should be detected by autoconf. However, no current
293 POSIX system provides that function, so testing for
294 it is pointless.
295 Not sure whether the MS_WINDOWS guards are necessary:
296 perhaps for cygwin/mingw builds?
297*/
Victor Stinnerb306d752010-10-07 22:09:40 +0000298#if defined(HAVE_STAT) && !defined(MS_WINDOWS)
Victor Stinner6672d0c2010-10-07 22:53:43 +0000299
300/* Get file status. Encode the path to the locale encoding. */
301
Victor Stinnerb306d752010-10-07 22:09:40 +0000302int
303_Py_wstat(const wchar_t* path, struct stat *buf)
304{
Victor Stinner4e314432010-10-07 21:45:39 +0000305 int err;
306 char *fname;
Victor Stinner2f02a512010-11-08 22:43:46 +0000307 fname = _Py_wchar2char(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +0000308 if (fname == NULL) {
309 errno = EINVAL;
310 return -1;
311 }
312 err = stat(fname, buf);
313 PyMem_Free(fname);
314 return err;
Victor Stinner4e314432010-10-07 21:45:39 +0000315}
316#endif
317
Victor Stinner6672d0c2010-10-07 22:53:43 +0000318/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
319 call stat() otherwise. Only fill st_mode attribute on Windows.
320
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100321 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
322 raised. */
Victor Stinner4e314432010-10-07 21:45:39 +0000323
324int
Victor Stinnera4a75952010-10-07 22:23:10 +0000325_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000326{
327#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000328 int err;
329 struct _stat wstatbuf;
Victor Stinneree587ea2011-11-17 00:51:38 +0100330 wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000331
Victor Stinneree587ea2011-11-17 00:51:38 +0100332 wpath = PyUnicode_AsUnicode(path);
333 if (wpath == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100334 return -2;
Victor Stinneree587ea2011-11-17 00:51:38 +0100335 err = _wstat(wpath, &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000336 if (!err)
337 statbuf->st_mode = wstatbuf.st_mode;
338 return err;
339#else
340 int ret;
Victor Stinnera4a75952010-10-07 22:23:10 +0000341 PyObject *bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +0000342 if (bytes == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100343 return -2;
Victor Stinner4e314432010-10-07 21:45:39 +0000344 ret = stat(PyBytes_AS_STRING(bytes), statbuf);
345 Py_DECREF(bytes);
346 return ret;
347#endif
348}
349
Victor Stinner6672d0c2010-10-07 22:53:43 +0000350/* Open a file. Use _wfopen() on Windows, encode the path to the locale
351 encoding and use fopen() otherwise. */
352
Victor Stinner4e314432010-10-07 21:45:39 +0000353FILE *
354_Py_wfopen(const wchar_t *path, const wchar_t *mode)
355{
356#ifndef MS_WINDOWS
357 FILE *f;
358 char *cpath;
359 char cmode[10];
360 size_t r;
361 r = wcstombs(cmode, mode, 10);
362 if (r == (size_t)-1 || r >= 10) {
363 errno = EINVAL;
364 return NULL;
365 }
Victor Stinner2f02a512010-11-08 22:43:46 +0000366 cpath = _Py_wchar2char(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +0000367 if (cpath == NULL)
368 return NULL;
369 f = fopen(cpath, cmode);
370 PyMem_Free(cpath);
371 return f;
372#else
373 return _wfopen(path, mode);
374#endif
375}
376
Victor Stinner6672d0c2010-10-07 22:53:43 +0000377/* Call _wfopen() on Windows, or encode the path to the filesystem encoding and
378 call fopen() otherwise.
379
380 Return the new file object on success, or NULL if the file cannot be open or
381 (if PyErr_Occurred()) on unicode error */
Victor Stinner4e314432010-10-07 21:45:39 +0000382
383FILE*
Victor Stinnera4a75952010-10-07 22:23:10 +0000384_Py_fopen(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +0000385{
386#ifdef MS_WINDOWS
Victor Stinneree587ea2011-11-17 00:51:38 +0100387 wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000388 wchar_t wmode[10];
389 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +0000390
Antoine Pitrou0e576f12011-12-22 10:03:38 +0100391 if (!PyUnicode_Check(path)) {
392 PyErr_Format(PyExc_TypeError,
393 "str file path expected under Windows, got %R",
394 Py_TYPE(path));
395 return NULL;
396 }
Victor Stinneree587ea2011-11-17 00:51:38 +0100397 wpath = PyUnicode_AsUnicode(path);
398 if (wpath == NULL)
399 return NULL;
400
Victor Stinner4e314432010-10-07 21:45:39 +0000401 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
402 if (usize == 0)
403 return NULL;
404
Victor Stinneree587ea2011-11-17 00:51:38 +0100405 return _wfopen(wpath, wmode);
Victor Stinner4e314432010-10-07 21:45:39 +0000406#else
407 FILE *f;
Antoine Pitrou2b1cc892011-12-19 18:19:06 +0100408 PyObject *bytes;
409 if (!PyUnicode_FSConverter(path, &bytes))
Victor Stinner4e314432010-10-07 21:45:39 +0000410 return NULL;
411 f = fopen(PyBytes_AS_STRING(bytes), mode);
412 Py_DECREF(bytes);
413 return f;
414#endif
415}
416
417#ifdef HAVE_READLINK
Victor Stinner6672d0c2010-10-07 22:53:43 +0000418
419/* Read value of symbolic link. Encode the path to the locale encoding, decode
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100420 the result from the locale encoding. Return -1 on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +0000421
Victor Stinner4e314432010-10-07 21:45:39 +0000422int
423_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
424{
425 char *cpath;
426 char cbuf[PATH_MAX];
Victor Stinner3f711f42010-10-16 22:47:37 +0000427 wchar_t *wbuf;
Victor Stinner4e314432010-10-07 21:45:39 +0000428 int res;
429 size_t r1;
430
Victor Stinner2f02a512010-11-08 22:43:46 +0000431 cpath = _Py_wchar2char(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +0000432 if (cpath == NULL) {
433 errno = EINVAL;
434 return -1;
435 }
436 res = (int)readlink(cpath, cbuf, PATH_MAX);
437 PyMem_Free(cpath);
438 if (res == -1)
439 return -1;
440 if (res == PATH_MAX) {
441 errno = EINVAL;
442 return -1;
443 }
444 cbuf[res] = '\0'; /* buf will be null terminated */
Victor Stinner168e1172010-10-16 23:16:16 +0000445 wbuf = _Py_char2wchar(cbuf, &r1);
Victor Stinner350147b2010-10-16 22:52:09 +0000446 if (wbuf == NULL) {
447 errno = EINVAL;
448 return -1;
449 }
Victor Stinner3f711f42010-10-16 22:47:37 +0000450 if (bufsiz <= r1) {
451 PyMem_Free(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000452 errno = EINVAL;
453 return -1;
454 }
Victor Stinner3f711f42010-10-16 22:47:37 +0000455 wcsncpy(buf, wbuf, bufsiz);
456 PyMem_Free(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000457 return (int)r1;
458}
459#endif
460
461#ifdef HAVE_REALPATH
Victor Stinner6672d0c2010-10-07 22:53:43 +0000462
463/* Return the canonicalized absolute pathname. Encode path to the locale
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100464 encoding, decode the result from the locale encoding.
465 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +0000466
Victor Stinner4e314432010-10-07 21:45:39 +0000467wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +0000468_Py_wrealpath(const wchar_t *path,
469 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +0000470{
471 char *cpath;
472 char cresolved_path[PATH_MAX];
Victor Stinner0a1b8cb2010-10-16 22:55:47 +0000473 wchar_t *wresolved_path;
Victor Stinner4e314432010-10-07 21:45:39 +0000474 char *res;
475 size_t r;
Victor Stinner2f02a512010-11-08 22:43:46 +0000476 cpath = _Py_wchar2char(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +0000477 if (cpath == NULL) {
478 errno = EINVAL;
479 return NULL;
480 }
481 res = realpath(cpath, cresolved_path);
482 PyMem_Free(cpath);
483 if (res == NULL)
484 return NULL;
Victor Stinner0a1b8cb2010-10-16 22:55:47 +0000485
Victor Stinner168e1172010-10-16 23:16:16 +0000486 wresolved_path = _Py_char2wchar(cresolved_path, &r);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +0000487 if (wresolved_path == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +0000488 errno = EINVAL;
489 return NULL;
490 }
Victor Stinner0a1b8cb2010-10-16 22:55:47 +0000491 if (resolved_path_size <= r) {
492 PyMem_Free(wresolved_path);
493 errno = EINVAL;
494 return NULL;
495 }
496 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
497 PyMem_Free(wresolved_path);
Victor Stinner4e314432010-10-07 21:45:39 +0000498 return resolved_path;
499}
500#endif
501
Victor Stinnerf4061da2010-10-14 12:37:19 +0000502/* Get the current directory. size is the buffer size in wide characters
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100503 including the null character. Decode the path from the locale encoding.
504 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +0000505
Victor Stinner4e314432010-10-07 21:45:39 +0000506wchar_t*
507_Py_wgetcwd(wchar_t *buf, size_t size)
508{
509#ifdef MS_WINDOWS
510 return _wgetcwd(buf, size);
511#else
512 char fname[PATH_MAX];
Victor Stinnerf4061da2010-10-14 12:37:19 +0000513 wchar_t *wname;
Victor Stinner168e1172010-10-16 23:16:16 +0000514 size_t len;
Victor Stinnerf4061da2010-10-14 12:37:19 +0000515
Victor Stinner4e314432010-10-07 21:45:39 +0000516 if (getcwd(fname, PATH_MAX) == NULL)
517 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +0000518 wname = _Py_char2wchar(fname, &len);
Victor Stinnerf4061da2010-10-14 12:37:19 +0000519 if (wname == NULL)
520 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +0000521 if (size <= len) {
Victor Stinnerf4061da2010-10-14 12:37:19 +0000522 PyMem_Free(wname);
Victor Stinner4e314432010-10-07 21:45:39 +0000523 return NULL;
524 }
Victor Stinnerf4061da2010-10-14 12:37:19 +0000525 wcsncpy(buf, wname, size);
526 PyMem_Free(wname);
Victor Stinner4e314432010-10-07 21:45:39 +0000527 return buf;
528#endif
529}
530
531#endif