blob: 8f3fdca7557507fe4c6a7fdb4423b6ba050ff50e [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"
Stefan Krah6c01e382014-01-20 15:31:08 +01003#include <locale.h>
4
Victor Stinnerb306d752010-10-07 22:09:40 +00005#ifdef MS_WINDOWS
6# include <windows.h>
7#endif
Victor Stinner4e314432010-10-07 21:45:39 +00008
Brett Cannonefb00c02012-02-29 18:31:31 -05009#ifdef HAVE_LANGINFO_H
10#include <langinfo.h>
11#endif
12
Victor Stinnerdaf45552013-08-28 00:53:59 +020013#ifdef HAVE_SYS_IOCTL_H
14#include <sys/ioctl.h>
15#endif
16
17#ifdef HAVE_FCNTL_H
18#include <fcntl.h>
19#endif /* HAVE_FCNTL_H */
20
Victor Stinnere2623772012-11-12 23:04:02 +010021#ifdef __APPLE__
22extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
23#endif
24
Victor Stinnerdaf45552013-08-28 00:53:59 +020025#ifdef O_CLOEXEC
Victor Stinnerb034eee2013-09-07 10:36:04 +020026/* Does open() support the O_CLOEXEC flag? Possible values:
Victor Stinnerdaf45552013-08-28 00:53:59 +020027
28 -1: unknown
29 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23
30 1: open() supports O_CLOEXEC flag, close-on-exec is set
31
32 The flag is used by _Py_open(), io.FileIO and os.open() */
33int _Py_open_cloexec_works = -1;
34#endif
35
Brett Cannonefb00c02012-02-29 18:31:31 -050036PyObject *
37_Py_device_encoding(int fd)
38{
Victor Stinner14b9b112013-06-25 00:37:25 +020039#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050040 UINT cp;
41#endif
42 if (!_PyVerify_fd(fd) || !isatty(fd)) {
43 Py_RETURN_NONE;
44 }
Victor Stinner14b9b112013-06-25 00:37:25 +020045#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050046 if (fd == 0)
47 cp = GetConsoleCP();
48 else if (fd == 1 || fd == 2)
49 cp = GetConsoleOutputCP();
50 else
51 cp = 0;
52 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
53 has no console */
54 if (cp != 0)
55 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
56#elif defined(CODESET)
57 {
58 char *codeset = nl_langinfo(CODESET);
59 if (codeset != NULL && codeset[0] != 0)
60 return PyUnicode_FromString(codeset);
61 }
62#endif
63 Py_RETURN_NONE;
64}
65
Victor Stinnerd45c7f82012-12-04 01:34:47 +010066#if !defined(__APPLE__) && !defined(MS_WINDOWS)
67extern int _Py_normalize_encoding(const char *, char *, size_t);
68
69/* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale.
70 On these operating systems, nl_langinfo(CODESET) announces an alias of the
71 ASCII encoding, whereas mbstowcs() and wcstombs() functions use the
72 ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use
73 locale.getpreferredencoding() codec. For example, if command line arguments
74 are decoded by mbstowcs() and encoded back by os.fsencode(), we get a
75 UnicodeEncodeError instead of retrieving the original byte string.
76
77 The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C",
78 nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least
79 one byte in range 0x80-0xff can be decoded from the locale encoding. The
80 workaround is also enabled on error, for example if getting the locale
81 failed.
82
Philip Jenvey215c49a2013-01-15 13:24:12 -080083 Values of force_ascii:
Victor Stinnerd45c7f82012-12-04 01:34:47 +010084
Victor Stinnerf6a271a2014-08-01 12:28:48 +020085 1: the workaround is used: Py_EncodeLocale() uses
86 encode_ascii_surrogateescape() and Py_DecodeLocale() uses
Victor Stinnerd45c7f82012-12-04 01:34:47 +010087 decode_ascii_surrogateescape()
Victor Stinnerf6a271a2014-08-01 12:28:48 +020088 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and
89 Py_DecodeLocale() uses mbstowcs()
Victor Stinnerd45c7f82012-12-04 01:34:47 +010090 -1: unknown, need to call check_force_ascii() to get the value
91*/
92static int force_ascii = -1;
93
94static int
95check_force_ascii(void)
96{
97 char *loc;
98#if defined(HAVE_LANGINFO_H) && defined(CODESET)
99 char *codeset, **alias;
100 char encoding[100];
101 int is_ascii;
102 unsigned int i;
103 char* ascii_aliases[] = {
104 "ascii",
105 "646",
106 "ansi-x3.4-1968",
107 "ansi-x3-4-1968",
108 "ansi-x3.4-1986",
109 "cp367",
110 "csascii",
111 "ibm367",
112 "iso646-us",
113 "iso-646.irv-1991",
114 "iso-ir-6",
115 "us",
116 "us-ascii",
117 NULL
118 };
119#endif
120
121 loc = setlocale(LC_CTYPE, NULL);
122 if (loc == NULL)
123 goto error;
124 if (strcmp(loc, "C") != 0) {
125 /* the LC_CTYPE locale is different than C */
126 return 0;
127 }
128
129#if defined(HAVE_LANGINFO_H) && defined(CODESET)
130 codeset = nl_langinfo(CODESET);
131 if (!codeset || codeset[0] == '\0') {
132 /* CODESET is not set or empty */
133 goto error;
134 }
135 if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding)))
136 goto error;
137
138 is_ascii = 0;
139 for (alias=ascii_aliases; *alias != NULL; alias++) {
140 if (strcmp(encoding, *alias) == 0) {
141 is_ascii = 1;
142 break;
143 }
144 }
145 if (!is_ascii) {
146 /* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */
147 return 0;
148 }
149
150 for (i=0x80; i<0xff; i++) {
151 unsigned char ch;
152 wchar_t wch;
153 size_t res;
154
155 ch = (unsigned char)i;
156 res = mbstowcs(&wch, (char*)&ch, 1);
157 if (res != (size_t)-1) {
158 /* decoding a non-ASCII character from the locale encoding succeed:
159 the locale encoding is not ASCII, force ASCII */
160 return 1;
161 }
162 }
163 /* None of the bytes in the range 0x80-0xff can be decoded from the locale
164 encoding: the locale encoding is really ASCII */
165 return 0;
166#else
167 /* nl_langinfo(CODESET) is not available: always force ASCII */
168 return 1;
169#endif
170
171error:
172 /* if an error occured, force the ASCII encoding */
173 return 1;
174}
175
176static char*
177encode_ascii_surrogateescape(const wchar_t *text, size_t *error_pos)
178{
179 char *result = NULL, *out;
180 size_t len, i;
181 wchar_t ch;
182
183 if (error_pos != NULL)
184 *error_pos = (size_t)-1;
185
186 len = wcslen(text);
187
188 result = PyMem_Malloc(len + 1); /* +1 for NUL byte */
189 if (result == NULL)
190 return NULL;
191
192 out = result;
193 for (i=0; i<len; i++) {
194 ch = text[i];
195
196 if (ch <= 0x7f) {
197 /* ASCII character */
198 *out++ = (char)ch;
199 }
200 else if (0xdc80 <= ch && ch <= 0xdcff) {
201 /* UTF-8b surrogate */
202 *out++ = (char)(ch - 0xdc00);
203 }
204 else {
205 if (error_pos != NULL)
206 *error_pos = i;
207 PyMem_Free(result);
208 return NULL;
209 }
210 }
211 *out = '\0';
212 return result;
213}
214#endif /* !defined(__APPLE__) && !defined(MS_WINDOWS) */
215
216#if !defined(__APPLE__) && (!defined(MS_WINDOWS) || !defined(HAVE_MBRTOWC))
217static wchar_t*
218decode_ascii_surrogateescape(const char *arg, size_t *size)
219{
220 wchar_t *res;
221 unsigned char *in;
222 wchar_t *out;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600223 size_t argsize = strlen(arg) + 1;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100224
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600225 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
226 return NULL;
Benjamin Peterson10ecaa22015-01-04 16:05:39 -0600227 res = PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100228 if (!res)
229 return NULL;
230
231 in = (unsigned char*)arg;
232 out = res;
233 while(*in)
234 if(*in < 128)
235 *out++ = *in++;
236 else
237 *out++ = 0xdc00 + *in++;
238 *out = 0;
239 if (size != NULL)
240 *size = out - res;
241 return res;
242}
243#endif
244
Victor Stinner4e314432010-10-07 21:45:39 +0000245
246/* Decode a byte string from the locale encoding with the
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200247 surrogateescape error handler: undecodable bytes are decoded as characters
248 in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate
Victor Stinner4e314432010-10-07 21:45:39 +0000249 character, escape the bytes using the surrogateescape error handler instead
250 of decoding them.
251
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200252 Return a pointer to a newly allocated wide character string, use
253 PyMem_RawFree() to free the memory. If size is not NULL, write the number of
254 wide characters excluding the null character into *size
Victor Stinner4e314432010-10-07 21:45:39 +0000255
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200256 Return NULL on decoding error or memory allocation error. If *size* is not
257 NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on
258 decoding error.
Victor Stinner19de4c32010-11-08 23:30:46 +0000259
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200260 Decoding errors should never happen, unless there is a bug in the C
261 library.
262
263 Use the Py_EncodeLocale() function to encode the character string back to a
264 byte string. */
Victor Stinner4e314432010-10-07 21:45:39 +0000265wchar_t*
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200266Py_DecodeLocale(const char* arg, size_t *size)
Victor Stinner4e314432010-10-07 21:45:39 +0000267{
Victor Stinnere2623772012-11-12 23:04:02 +0100268#ifdef __APPLE__
269 wchar_t *wstr;
270 wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg));
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100271 if (size != NULL) {
272 if (wstr != NULL)
273 *size = wcslen(wstr);
274 else
275 *size = (size_t)-1;
276 }
Victor Stinnere2623772012-11-12 23:04:02 +0100277 return wstr;
278#else
Victor Stinner4e314432010-10-07 21:45:39 +0000279 wchar_t *res;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100280 size_t argsize;
Victor Stinner4e314432010-10-07 21:45:39 +0000281 size_t count;
Victor Stinner313f10c2013-05-07 23:48:56 +0200282#ifdef HAVE_MBRTOWC
Victor Stinner4e314432010-10-07 21:45:39 +0000283 unsigned char *in;
284 wchar_t *out;
Victor Stinner4e314432010-10-07 21:45:39 +0000285 mbstate_t mbs;
286#endif
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100287
288#ifndef MS_WINDOWS
289 if (force_ascii == -1)
290 force_ascii = check_force_ascii();
291
292 if (force_ascii) {
293 /* force ASCII encoding to workaround mbstowcs() issue */
294 res = decode_ascii_surrogateescape(arg, size);
295 if (res == NULL)
296 goto oom;
297 return res;
298 }
299#endif
300
301#ifdef HAVE_BROKEN_MBSTOWCS
302 /* Some platforms have a broken implementation of
303 * mbstowcs which does not count the characters that
304 * would result from conversion. Use an upper bound.
305 */
306 argsize = strlen(arg);
307#else
308 argsize = mbstowcs(NULL, arg, 0);
309#endif
Victor Stinner4e314432010-10-07 21:45:39 +0000310 if (argsize != (size_t)-1) {
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600311 if (argsize == PY_SSIZE_T_MAX)
312 goto oom;
313 argsize += 1;
314 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
315 goto oom;
Benjamin Peterson10ecaa22015-01-04 16:05:39 -0600316 res = (wchar_t *)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner4e314432010-10-07 21:45:39 +0000317 if (!res)
318 goto oom;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600319 count = mbstowcs(res, arg, argsize);
Victor Stinner4e314432010-10-07 21:45:39 +0000320 if (count != (size_t)-1) {
321 wchar_t *tmp;
322 /* Only use the result if it contains no
323 surrogate characters. */
324 for (tmp = res; *tmp != 0 &&
Victor Stinner76df43d2012-10-30 01:42:39 +0100325 !Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
Victor Stinner4e314432010-10-07 21:45:39 +0000326 ;
Victor Stinner168e1172010-10-16 23:16:16 +0000327 if (*tmp == 0) {
328 if (size != NULL)
329 *size = count;
Victor Stinner4e314432010-10-07 21:45:39 +0000330 return res;
Victor Stinner168e1172010-10-16 23:16:16 +0000331 }
Victor Stinner4e314432010-10-07 21:45:39 +0000332 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200333 PyMem_RawFree(res);
Victor Stinner4e314432010-10-07 21:45:39 +0000334 }
335 /* Conversion failed. Fall back to escaping with surrogateescape. */
336#ifdef HAVE_MBRTOWC
337 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
338
339 /* Overallocate; as multi-byte characters are in the argument, the
340 actual output could use less memory. */
341 argsize = strlen(arg) + 1;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600342 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
343 goto oom;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200344 res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner19de4c32010-11-08 23:30:46 +0000345 if (!res)
346 goto oom;
Victor Stinner4e314432010-10-07 21:45:39 +0000347 in = (unsigned char*)arg;
348 out = res;
349 memset(&mbs, 0, sizeof mbs);
350 while (argsize) {
351 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
352 if (converted == 0)
353 /* Reached end of string; null char stored. */
354 break;
355 if (converted == (size_t)-2) {
356 /* Incomplete character. This should never happen,
357 since we provide everything that we have -
358 unless there is a bug in the C library, or I
359 misunderstood how mbrtowc works. */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200360 PyMem_RawFree(res);
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100361 if (size != NULL)
362 *size = (size_t)-2;
Victor Stinner4e314432010-10-07 21:45:39 +0000363 return NULL;
364 }
365 if (converted == (size_t)-1) {
366 /* Conversion error. Escape as UTF-8b, and start over
367 in the initial shift state. */
368 *out++ = 0xdc00 + *in++;
369 argsize--;
370 memset(&mbs, 0, sizeof mbs);
371 continue;
372 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100373 if (Py_UNICODE_IS_SURROGATE(*out)) {
Victor Stinner4e314432010-10-07 21:45:39 +0000374 /* Surrogate character. Escape the original
375 byte sequence with surrogateescape. */
376 argsize -= converted;
377 while (converted--)
378 *out++ = 0xdc00 + *in++;
379 continue;
380 }
381 /* successfully converted some bytes */
382 in += converted;
383 argsize -= converted;
384 out++;
385 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100386 if (size != NULL)
387 *size = out - res;
Victor Stinnere2623772012-11-12 23:04:02 +0100388#else /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000389 /* Cannot use C locale for escaping; manually escape as if charset
390 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
391 correctly in the locale's charset, which must be an ASCII superset. */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100392 res = decode_ascii_surrogateescape(arg, size);
393 if (res == NULL)
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100394 goto oom;
Victor Stinnere2623772012-11-12 23:04:02 +0100395#endif /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000396 return res;
397oom:
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100398 if (size != NULL)
399 *size = (size_t)-1;
Victor Stinner4e314432010-10-07 21:45:39 +0000400 return NULL;
Victor Stinnere2623772012-11-12 23:04:02 +0100401#endif /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000402}
403
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200404/* Encode a wide character string to the locale encoding with the
405 surrogateescape error handler: surrogate characters in the range
406 U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
Victor Stinner4e314432010-10-07 21:45:39 +0000407
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200408 Return a pointer to a newly allocated byte string, use PyMem_Free() to free
409 the memory. Return NULL on encoding or memory allocation error.
Victor Stinner4e314432010-10-07 21:45:39 +0000410
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200411 If error_pos is not NULL, *error_pos is set to the index of the invalid
412 character on encoding error, or set to (size_t)-1 otherwise.
Victor Stinner2f02a512010-11-08 22:43:46 +0000413
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200414 Use the Py_DecodeLocale() function to decode the bytes string back to a wide
415 character string. */
Victor Stinner4e314432010-10-07 21:45:39 +0000416char*
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200417Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
Victor Stinner4e314432010-10-07 21:45:39 +0000418{
Victor Stinnere2623772012-11-12 23:04:02 +0100419#ifdef __APPLE__
420 Py_ssize_t len;
421 PyObject *unicode, *bytes = NULL;
422 char *cpath;
423
424 unicode = PyUnicode_FromWideChar(text, wcslen(text));
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100425 if (unicode == NULL)
Victor Stinnere2623772012-11-12 23:04:02 +0100426 return NULL;
Victor Stinnere2623772012-11-12 23:04:02 +0100427
428 bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape");
429 Py_DECREF(unicode);
430 if (bytes == NULL) {
431 PyErr_Clear();
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100432 if (error_pos != NULL)
433 *error_pos = (size_t)-1;
Victor Stinnere2623772012-11-12 23:04:02 +0100434 return NULL;
435 }
436
437 len = PyBytes_GET_SIZE(bytes);
438 cpath = PyMem_Malloc(len+1);
439 if (cpath == NULL) {
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100440 PyErr_Clear();
Victor Stinnere2623772012-11-12 23:04:02 +0100441 Py_DECREF(bytes);
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100442 if (error_pos != NULL)
443 *error_pos = (size_t)-1;
Victor Stinnere2623772012-11-12 23:04:02 +0100444 return NULL;
445 }
446 memcpy(cpath, PyBytes_AsString(bytes), len + 1);
447 Py_DECREF(bytes);
448 return cpath;
449#else /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000450 const size_t len = wcslen(text);
451 char *result = NULL, *bytes = NULL;
452 size_t i, size, converted;
453 wchar_t c, buf[2];
454
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100455#ifndef MS_WINDOWS
456 if (force_ascii == -1)
457 force_ascii = check_force_ascii();
458
459 if (force_ascii)
460 return encode_ascii_surrogateescape(text, error_pos);
461#endif
462
Victor Stinner4e314432010-10-07 21:45:39 +0000463 /* The function works in two steps:
464 1. compute the length of the output buffer in bytes (size)
465 2. outputs the bytes */
466 size = 0;
467 buf[1] = 0;
468 while (1) {
469 for (i=0; i < len; i++) {
470 c = text[i];
471 if (c >= 0xdc80 && c <= 0xdcff) {
472 /* UTF-8b surrogate */
473 if (bytes != NULL) {
474 *bytes++ = c - 0xdc00;
475 size--;
476 }
477 else
478 size++;
479 continue;
480 }
481 else {
482 buf[0] = c;
483 if (bytes != NULL)
484 converted = wcstombs(bytes, buf, size);
485 else
486 converted = wcstombs(NULL, buf, 0);
487 if (converted == (size_t)-1) {
488 if (result != NULL)
489 PyMem_Free(result);
Victor Stinner2f02a512010-11-08 22:43:46 +0000490 if (error_pos != NULL)
491 *error_pos = i;
Victor Stinner4e314432010-10-07 21:45:39 +0000492 return NULL;
493 }
494 if (bytes != NULL) {
495 bytes += converted;
496 size -= converted;
497 }
498 else
499 size += converted;
500 }
501 }
502 if (result != NULL) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100503 *bytes = '\0';
Victor Stinner4e314432010-10-07 21:45:39 +0000504 break;
505 }
506
507 size += 1; /* nul byte at the end */
508 result = PyMem_Malloc(size);
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100509 if (result == NULL) {
510 if (error_pos != NULL)
511 *error_pos = (size_t)-1;
Victor Stinner4e314432010-10-07 21:45:39 +0000512 return NULL;
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100513 }
Victor Stinner4e314432010-10-07 21:45:39 +0000514 bytes = result;
515 }
516 return result;
Victor Stinnere2623772012-11-12 23:04:02 +0100517#endif /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000518}
519
Victor Stinner4e314432010-10-07 21:45:39 +0000520/* In principle, this should use HAVE__WSTAT, and _wstat
521 should be detected by autoconf. However, no current
522 POSIX system provides that function, so testing for
523 it is pointless.
524 Not sure whether the MS_WINDOWS guards are necessary:
525 perhaps for cygwin/mingw builds?
526*/
Victor Stinnerb306d752010-10-07 22:09:40 +0000527#if defined(HAVE_STAT) && !defined(MS_WINDOWS)
Victor Stinner6672d0c2010-10-07 22:53:43 +0000528
529/* Get file status. Encode the path to the locale encoding. */
530
Victor Stinnerb306d752010-10-07 22:09:40 +0000531int
532_Py_wstat(const wchar_t* path, struct stat *buf)
533{
Victor Stinner4e314432010-10-07 21:45:39 +0000534 int err;
535 char *fname;
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200536 fname = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +0000537 if (fname == NULL) {
538 errno = EINVAL;
539 return -1;
540 }
541 err = stat(fname, buf);
542 PyMem_Free(fname);
543 return err;
Victor Stinner4e314432010-10-07 21:45:39 +0000544}
545#endif
546
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100547
Steve Dowerf2f373f2015-02-21 08:44:05 -0800548#if defined(HAVE_FSTAT) || defined(MS_WINDOWS)
549
550#ifdef MS_WINDOWS
551static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
552
553static void
554FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
555{
556 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
557 /* Cannot simply cast and dereference in_ptr,
558 since it might not be aligned properly */
559 __int64 in;
560 memcpy(&in, in_ptr, sizeof(in));
561 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
562 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
563}
564
565void
566time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
567{
568 /* XXX endianness */
569 __int64 out;
570 out = time_in + secs_between_epochs;
571 out = out * 10000000 + nsec_in / 100;
572 memcpy(out_ptr, &out, sizeof(out));
573}
574
575/* Below, we *know* that ugo+r is 0444 */
576#if _S_IREAD != 0400
577#error Unsupported C library
578#endif
579static int
580attributes_to_mode(DWORD attr)
581{
582 int m = 0;
583 if (attr & FILE_ATTRIBUTE_DIRECTORY)
584 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
585 else
586 m |= _S_IFREG;
587 if (attr & FILE_ATTRIBUTE_READONLY)
588 m |= 0444;
589 else
590 m |= 0666;
591 return m;
592}
593
594int
595attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct _Py_stat_struct *result)
596{
597 memset(result, 0, sizeof(*result));
598 result->st_mode = attributes_to_mode(info->dwFileAttributes);
599 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
600 result->st_dev = info->dwVolumeSerialNumber;
601 result->st_rdev = result->st_dev;
602 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
603 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
604 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
605 result->st_nlink = info->nNumberOfLinks;
606 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
607 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
608 /* first clear the S_IFMT bits */
609 result->st_mode ^= (result->st_mode & S_IFMT);
610 /* now set the bits that make this a symlink */
611 result->st_mode |= S_IFLNK;
612 }
613 result->st_file_attributes = info->dwFileAttributes;
614
615 return 0;
616}
617#endif
618
619/* Return information about a file.
620
621 On POSIX, use fstat().
622
623 On Windows, use GetFileType() and GetFileInformationByHandle() which support
624 files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger
625 than 2 GB because the file size type is an signed 32-bit integer: see issue
626 #23152.
627 */
628int
629_Py_fstat(int fd, struct _Py_stat_struct *result)
630{
631#ifdef MS_WINDOWS
632 BY_HANDLE_FILE_INFORMATION info;
633 HANDLE h;
634 int type;
635
636 if (!_PyVerify_fd(fd))
637 h = INVALID_HANDLE_VALUE;
638 else
639 h = (HANDLE)_get_osfhandle(fd);
640
641 /* Protocol violation: we explicitly clear errno, instead of
642 setting it to a POSIX error. Callers should use GetLastError. */
643 errno = 0;
644
645 if (h == INVALID_HANDLE_VALUE) {
646 /* This is really a C library error (invalid file handle).
647 We set the Win32 error to the closes one matching. */
648 SetLastError(ERROR_INVALID_HANDLE);
649 return -1;
650 }
651 memset(result, 0, sizeof(*result));
652
653 type = GetFileType(h);
654 if (type == FILE_TYPE_UNKNOWN) {
655 DWORD error = GetLastError();
656 if (error != 0) {
657 return -1;
658 }
659 /* else: valid but unknown file */
660 }
661
662 if (type != FILE_TYPE_DISK) {
663 if (type == FILE_TYPE_CHAR)
664 result->st_mode = _S_IFCHR;
665 else if (type == FILE_TYPE_PIPE)
666 result->st_mode = _S_IFIFO;
667 return 0;
668 }
669
670 if (!GetFileInformationByHandle(h, &info)) {
671 return -1;
672 }
673
674 attribute_data_to_stat(&info, 0, result);
675 /* specific to fstat() */
676 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
677 return 0;
678#else
679 return fstat(fd, result);
680#endif
681}
682#endif /* HAVE_FSTAT || MS_WINDOWS */
683
684
685#ifdef HAVE_STAT
Victor Stinner6672d0c2010-10-07 22:53:43 +0000686/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
687 call stat() otherwise. Only fill st_mode attribute on Windows.
688
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100689 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
690 raised. */
Victor Stinner4e314432010-10-07 21:45:39 +0000691
692int
Victor Stinnera4a75952010-10-07 22:23:10 +0000693_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000694{
695#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000696 int err;
697 struct _stat wstatbuf;
Victor Stinneree587ea2011-11-17 00:51:38 +0100698 wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000699
Victor Stinneree587ea2011-11-17 00:51:38 +0100700 wpath = PyUnicode_AsUnicode(path);
701 if (wpath == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100702 return -2;
Victor Stinneree587ea2011-11-17 00:51:38 +0100703 err = _wstat(wpath, &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000704 if (!err)
705 statbuf->st_mode = wstatbuf.st_mode;
706 return err;
707#else
708 int ret;
Victor Stinnera4a75952010-10-07 22:23:10 +0000709 PyObject *bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +0000710 if (bytes == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100711 return -2;
Victor Stinner4e314432010-10-07 21:45:39 +0000712 ret = stat(PyBytes_AS_STRING(bytes), statbuf);
713 Py_DECREF(bytes);
714 return ret;
715#endif
716}
717
Steve Dowerf2f373f2015-02-21 08:44:05 -0800718#endif /* HAVE_STAT */
719
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100720
Antoine Pitrou409b5382013-10-12 22:41:17 +0200721static int
Victor Stinnerdaf45552013-08-28 00:53:59 +0200722get_inheritable(int fd, int raise)
723{
724#ifdef MS_WINDOWS
725 HANDLE handle;
726 DWORD flags;
Victor Stinner6672d0c2010-10-07 22:53:43 +0000727
Victor Stinnerdaf45552013-08-28 00:53:59 +0200728 if (!_PyVerify_fd(fd)) {
729 if (raise)
730 PyErr_SetFromErrno(PyExc_OSError);
731 return -1;
732 }
733
734 handle = (HANDLE)_get_osfhandle(fd);
735 if (handle == INVALID_HANDLE_VALUE) {
736 if (raise)
737 PyErr_SetFromWindowsErr(0);
738 return -1;
739 }
740
741 if (!GetHandleInformation(handle, &flags)) {
742 if (raise)
743 PyErr_SetFromWindowsErr(0);
744 return -1;
745 }
746
747 return (flags & HANDLE_FLAG_INHERIT);
748#else
749 int flags;
750
751 flags = fcntl(fd, F_GETFD, 0);
752 if (flags == -1) {
753 if (raise)
754 PyErr_SetFromErrno(PyExc_OSError);
755 return -1;
756 }
757 return !(flags & FD_CLOEXEC);
758#endif
759}
760
761/* Get the inheritable flag of the specified file descriptor.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200762 Return 1 if the file descriptor can be inherited, 0 if it cannot,
Victor Stinnerdaf45552013-08-28 00:53:59 +0200763 raise an exception and return -1 on error. */
764int
765_Py_get_inheritable(int fd)
766{
767 return get_inheritable(fd, 1);
768}
769
770static int
771set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
772{
773#ifdef MS_WINDOWS
774 HANDLE handle;
775 DWORD flags;
Victor Stinner282124b2014-09-02 11:41:04 +0200776#else
777#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
778 static int ioctl_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200779 int request;
780 int err;
Victor Stinner282124b2014-09-02 11:41:04 +0200781#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +0200782 int flags;
783 int res;
784#endif
785
786 /* atomic_flag_works can only be used to make the file descriptor
787 non-inheritable */
788 assert(!(atomic_flag_works != NULL && inheritable));
789
790 if (atomic_flag_works != NULL && !inheritable) {
791 if (*atomic_flag_works == -1) {
792 int inheritable = get_inheritable(fd, raise);
793 if (inheritable == -1)
794 return -1;
795 *atomic_flag_works = !inheritable;
796 }
797
798 if (*atomic_flag_works)
799 return 0;
800 }
801
802#ifdef MS_WINDOWS
803 if (!_PyVerify_fd(fd)) {
804 if (raise)
805 PyErr_SetFromErrno(PyExc_OSError);
806 return -1;
807 }
808
809 handle = (HANDLE)_get_osfhandle(fd);
810 if (handle == INVALID_HANDLE_VALUE) {
811 if (raise)
812 PyErr_SetFromWindowsErr(0);
813 return -1;
814 }
815
816 if (inheritable)
817 flags = HANDLE_FLAG_INHERIT;
818 else
819 flags = 0;
820 if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
821 if (raise)
822 PyErr_SetFromWindowsErr(0);
823 return -1;
824 }
825 return 0;
826
Victor Stinnerdaf45552013-08-28 00:53:59 +0200827#else
Victor Stinner282124b2014-09-02 11:41:04 +0200828
829#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
830 if (ioctl_works != 0) {
831 /* fast-path: ioctl() only requires one syscall */
832 if (inheritable)
833 request = FIONCLEX;
834 else
835 request = FIOCLEX;
836 err = ioctl(fd, request, NULL);
837 if (!err) {
838 ioctl_works = 1;
839 return 0;
840 }
841
842 if (errno != ENOTTY) {
843 if (raise)
844 PyErr_SetFromErrno(PyExc_OSError);
845 return -1;
846 }
847 else {
848 /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for
849 device". The ioctl is declared but not supported by the kernel.
850 Remember that ioctl() doesn't work. It is the case on
851 Illumos-based OS for example. */
852 ioctl_works = 0;
853 }
854 /* fallback to fcntl() if ioctl() does not work */
855 }
856#endif
857
858 /* slow-path: fcntl() requires two syscalls */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200859 flags = fcntl(fd, F_GETFD);
860 if (flags < 0) {
861 if (raise)
862 PyErr_SetFromErrno(PyExc_OSError);
863 return -1;
864 }
865
866 if (inheritable)
867 flags &= ~FD_CLOEXEC;
868 else
869 flags |= FD_CLOEXEC;
870 res = fcntl(fd, F_SETFD, flags);
871 if (res < 0) {
872 if (raise)
873 PyErr_SetFromErrno(PyExc_OSError);
874 return -1;
875 }
876 return 0;
877#endif
878}
879
880/* Make the file descriptor non-inheritable.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200881 Return 0 on success, set errno and return -1 on error. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200882static int
883make_non_inheritable(int fd)
884{
885 return set_inheritable(fd, 0, 0, NULL);
886}
887
888/* Set the inheritable flag of the specified file descriptor.
889 On success: return 0, on error: raise an exception if raise is nonzero
890 and return -1.
891
892 If atomic_flag_works is not NULL:
893
894 * if *atomic_flag_works==-1, check if the inheritable is set on the file
895 descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and
896 set the inheritable flag
897 * if *atomic_flag_works==1: do nothing
898 * if *atomic_flag_works==0: set inheritable flag to False
899
900 Set atomic_flag_works to NULL if no atomic flag was used to create the
901 file descriptor.
902
903 atomic_flag_works can only be used to make a file descriptor
904 non-inheritable: atomic_flag_works must be NULL if inheritable=1. */
905int
906_Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works)
907{
908 return set_inheritable(fd, inheritable, 1, atomic_flag_works);
909}
910
911/* Open a file with the specified flags (wrapper to open() function).
912 The file descriptor is created non-inheritable. */
913int
914_Py_open(const char *pathname, int flags)
915{
916 int fd;
917#ifdef MS_WINDOWS
918 fd = open(pathname, flags | O_NOINHERIT);
919 if (fd < 0)
920 return fd;
921#else
922
923 int *atomic_flag_works;
924#ifdef O_CLOEXEC
925 atomic_flag_works = &_Py_open_cloexec_works;
926 flags |= O_CLOEXEC;
927#else
928 atomic_flag_works = NULL;
929#endif
930 fd = open(pathname, flags);
931 if (fd < 0)
932 return fd;
933
934 if (set_inheritable(fd, 0, 0, atomic_flag_works) < 0) {
935 close(fd);
936 return -1;
937 }
938#endif /* !MS_WINDOWS */
939 return fd;
940}
941
942/* Open a file. Use _wfopen() on Windows, encode the path to the locale
943 encoding and use fopen() otherwise. The file descriptor is created
944 non-inheritable. */
Victor Stinner4e314432010-10-07 21:45:39 +0000945FILE *
946_Py_wfopen(const wchar_t *path, const wchar_t *mode)
947{
Victor Stinner4e314432010-10-07 21:45:39 +0000948 FILE *f;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200949#ifndef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000950 char *cpath;
951 char cmode[10];
952 size_t r;
953 r = wcstombs(cmode, mode, 10);
954 if (r == (size_t)-1 || r >= 10) {
955 errno = EINVAL;
956 return NULL;
957 }
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200958 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +0000959 if (cpath == NULL)
960 return NULL;
961 f = fopen(cpath, cmode);
962 PyMem_Free(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +0000963#else
Victor Stinnerdaf45552013-08-28 00:53:59 +0200964 f = _wfopen(path, mode);
Victor Stinner4e314432010-10-07 21:45:39 +0000965#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +0200966 if (f == NULL)
967 return NULL;
968 if (make_non_inheritable(fileno(f)) < 0) {
969 fclose(f);
970 return NULL;
971 }
972 return f;
Victor Stinner4e314432010-10-07 21:45:39 +0000973}
974
Victor Stinnerdaf45552013-08-28 00:53:59 +0200975/* Wrapper to fopen(). The file descriptor is created non-inheritable. */
976FILE*
977_Py_fopen(const char *pathname, const char *mode)
978{
979 FILE *f = fopen(pathname, mode);
980 if (f == NULL)
981 return NULL;
982 if (make_non_inheritable(fileno(f)) < 0) {
983 fclose(f);
984 return NULL;
985 }
986 return f;
987}
988
989/* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem
990 encoding and call fopen() otherwise. The file descriptor is created
991 non-inheritable.
Victor Stinner6672d0c2010-10-07 22:53:43 +0000992
993 Return the new file object on success, or NULL if the file cannot be open or
Victor Stinnerdaf45552013-08-28 00:53:59 +0200994 (if PyErr_Occurred()) on unicode error. */
Victor Stinner4e314432010-10-07 21:45:39 +0000995FILE*
Victor Stinnerdaf45552013-08-28 00:53:59 +0200996_Py_fopen_obj(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +0000997{
Victor Stinnerdaf45552013-08-28 00:53:59 +0200998 FILE *f;
Victor Stinner4e314432010-10-07 21:45:39 +0000999#ifdef MS_WINDOWS
Victor Stinneree587ea2011-11-17 00:51:38 +01001000 wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +00001001 wchar_t wmode[10];
1002 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +00001003
Antoine Pitrou0e576f12011-12-22 10:03:38 +01001004 if (!PyUnicode_Check(path)) {
1005 PyErr_Format(PyExc_TypeError,
1006 "str file path expected under Windows, got %R",
1007 Py_TYPE(path));
1008 return NULL;
1009 }
Victor Stinneree587ea2011-11-17 00:51:38 +01001010 wpath = PyUnicode_AsUnicode(path);
1011 if (wpath == NULL)
1012 return NULL;
1013
Victor Stinner4e314432010-10-07 21:45:39 +00001014 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
1015 if (usize == 0)
1016 return NULL;
1017
Victor Stinnerdaf45552013-08-28 00:53:59 +02001018 f = _wfopen(wpath, wmode);
Victor Stinner4e314432010-10-07 21:45:39 +00001019#else
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001020 PyObject *bytes;
1021 if (!PyUnicode_FSConverter(path, &bytes))
Victor Stinner4e314432010-10-07 21:45:39 +00001022 return NULL;
1023 f = fopen(PyBytes_AS_STRING(bytes), mode);
1024 Py_DECREF(bytes);
Victor Stinner4e314432010-10-07 21:45:39 +00001025#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001026 if (f == NULL)
1027 return NULL;
1028 if (make_non_inheritable(fileno(f)) < 0) {
1029 fclose(f);
1030 return NULL;
1031 }
1032 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001033}
1034
1035#ifdef HAVE_READLINK
Victor Stinner6672d0c2010-10-07 22:53:43 +00001036
1037/* Read value of symbolic link. Encode the path to the locale encoding, decode
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001038 the result from the locale encoding. Return -1 on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001039
Victor Stinner4e314432010-10-07 21:45:39 +00001040int
1041_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
1042{
1043 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001044 char cbuf[MAXPATHLEN];
Victor Stinner3f711f42010-10-16 22:47:37 +00001045 wchar_t *wbuf;
Victor Stinner4e314432010-10-07 21:45:39 +00001046 int res;
1047 size_t r1;
1048
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001049 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001050 if (cpath == NULL) {
1051 errno = EINVAL;
1052 return -1;
1053 }
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001054 res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));
Victor Stinner4e314432010-10-07 21:45:39 +00001055 PyMem_Free(cpath);
1056 if (res == -1)
1057 return -1;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001058 if (res == Py_ARRAY_LENGTH(cbuf)) {
Victor Stinner4e314432010-10-07 21:45:39 +00001059 errno = EINVAL;
1060 return -1;
1061 }
1062 cbuf[res] = '\0'; /* buf will be null terminated */
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001063 wbuf = Py_DecodeLocale(cbuf, &r1);
Victor Stinner350147b2010-10-16 22:52:09 +00001064 if (wbuf == NULL) {
1065 errno = EINVAL;
1066 return -1;
1067 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001068 if (bufsiz <= r1) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001069 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001070 errno = EINVAL;
1071 return -1;
1072 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001073 wcsncpy(buf, wbuf, bufsiz);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001074 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001075 return (int)r1;
1076}
1077#endif
1078
1079#ifdef HAVE_REALPATH
Victor Stinner6672d0c2010-10-07 22:53:43 +00001080
1081/* Return the canonicalized absolute pathname. Encode path to the locale
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001082 encoding, decode the result from the locale encoding.
1083 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001084
Victor Stinner4e314432010-10-07 21:45:39 +00001085wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +00001086_Py_wrealpath(const wchar_t *path,
1087 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +00001088{
1089 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001090 char cresolved_path[MAXPATHLEN];
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001091 wchar_t *wresolved_path;
Victor Stinner4e314432010-10-07 21:45:39 +00001092 char *res;
1093 size_t r;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001094 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001095 if (cpath == NULL) {
1096 errno = EINVAL;
1097 return NULL;
1098 }
1099 res = realpath(cpath, cresolved_path);
1100 PyMem_Free(cpath);
1101 if (res == NULL)
1102 return NULL;
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001103
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001104 wresolved_path = Py_DecodeLocale(cresolved_path, &r);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001105 if (wresolved_path == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001106 errno = EINVAL;
1107 return NULL;
1108 }
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001109 if (resolved_path_size <= r) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001110 PyMem_RawFree(wresolved_path);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001111 errno = EINVAL;
1112 return NULL;
1113 }
1114 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001115 PyMem_RawFree(wresolved_path);
Victor Stinner4e314432010-10-07 21:45:39 +00001116 return resolved_path;
1117}
1118#endif
1119
Victor Stinnerf4061da2010-10-14 12:37:19 +00001120/* Get the current directory. size is the buffer size in wide characters
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001121 including the null character. Decode the path from the locale encoding.
1122 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001123
Victor Stinner4e314432010-10-07 21:45:39 +00001124wchar_t*
1125_Py_wgetcwd(wchar_t *buf, size_t size)
1126{
1127#ifdef MS_WINDOWS
Victor Stinner56785ea2013-06-05 00:46:29 +02001128 int isize = (int)Py_MIN(size, INT_MAX);
1129 return _wgetcwd(buf, isize);
Victor Stinner4e314432010-10-07 21:45:39 +00001130#else
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001131 char fname[MAXPATHLEN];
Victor Stinnerf4061da2010-10-14 12:37:19 +00001132 wchar_t *wname;
Victor Stinner168e1172010-10-16 23:16:16 +00001133 size_t len;
Victor Stinnerf4061da2010-10-14 12:37:19 +00001134
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001135 if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)
Victor Stinner4e314432010-10-07 21:45:39 +00001136 return NULL;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001137 wname = Py_DecodeLocale(fname, &len);
Victor Stinnerf4061da2010-10-14 12:37:19 +00001138 if (wname == NULL)
1139 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +00001140 if (size <= len) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001141 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001142 return NULL;
1143 }
Victor Stinnerf4061da2010-10-14 12:37:19 +00001144 wcsncpy(buf, wname, size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001145 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001146 return buf;
1147#endif
1148}
1149
Victor Stinnerdaf45552013-08-28 00:53:59 +02001150/* Duplicate a file descriptor. The new file descriptor is created as
1151 non-inheritable. Return a new file descriptor on success, raise an OSError
1152 exception and return -1 on error.
1153
1154 The GIL is released to call dup(). The caller must hold the GIL. */
1155int
1156_Py_dup(int fd)
1157{
1158#ifdef MS_WINDOWS
1159 HANDLE handle;
1160 DWORD ftype;
1161#endif
1162
1163 if (!_PyVerify_fd(fd)) {
1164 PyErr_SetFromErrno(PyExc_OSError);
1165 return -1;
1166 }
1167
1168#ifdef MS_WINDOWS
1169 handle = (HANDLE)_get_osfhandle(fd);
1170 if (handle == INVALID_HANDLE_VALUE) {
1171 PyErr_SetFromWindowsErr(0);
1172 return -1;
1173 }
1174
1175 /* get the file type, ignore the error if it failed */
1176 ftype = GetFileType(handle);
1177
1178 Py_BEGIN_ALLOW_THREADS
1179 fd = dup(fd);
1180 Py_END_ALLOW_THREADS
1181 if (fd < 0) {
1182 PyErr_SetFromErrno(PyExc_OSError);
1183 return -1;
1184 }
1185
1186 /* Character files like console cannot be make non-inheritable */
1187 if (ftype != FILE_TYPE_CHAR) {
1188 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
1189 close(fd);
1190 return -1;
1191 }
1192 }
1193#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
1194 Py_BEGIN_ALLOW_THREADS
1195 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1196 Py_END_ALLOW_THREADS
1197 if (fd < 0) {
1198 PyErr_SetFromErrno(PyExc_OSError);
1199 return -1;
1200 }
1201
1202#else
1203 Py_BEGIN_ALLOW_THREADS
1204 fd = dup(fd);
1205 Py_END_ALLOW_THREADS
1206 if (fd < 0) {
1207 PyErr_SetFromErrno(PyExc_OSError);
1208 return -1;
1209 }
1210
1211 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
1212 close(fd);
1213 return -1;
1214 }
1215#endif
1216 return fd;
1217}
1218
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001219#ifndef MS_WINDOWS
1220/* Get the blocking mode of the file descriptor.
1221 Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared,
1222 raise an exception and return -1 on error. */
1223int
1224_Py_get_blocking(int fd)
1225{
1226 int flags = fcntl(fd, F_GETFL, 0);
1227 if (flags < 0) {
1228 PyErr_SetFromErrno(PyExc_OSError);
1229 return -1;
1230 }
1231
1232 return !(flags & O_NONBLOCK);
1233}
1234
1235/* Set the blocking mode of the specified file descriptor.
1236
1237 Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag
1238 otherwise.
1239
1240 Return 0 on success, raise an exception and return -1 on error. */
1241int
1242_Py_set_blocking(int fd, int blocking)
1243{
1244#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)
1245 int arg = !blocking;
1246 if (ioctl(fd, FIONBIO, &arg) < 0)
1247 goto error;
1248#else
1249 int flags, res;
1250
1251 flags = fcntl(fd, F_GETFL, 0);
1252 if (flags < 0)
1253 goto error;
1254
1255 if (blocking)
1256 flags = flags & (~O_NONBLOCK);
1257 else
1258 flags = flags | O_NONBLOCK;
1259
1260 res = fcntl(fd, F_SETFL, flags);
1261 if (res < 0)
1262 goto error;
1263#endif
1264 return 0;
1265
1266error:
1267 PyErr_SetFromErrno(PyExc_OSError);
1268 return -1;
1269}
1270#endif
1271