Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Stefan Krah | 6df5cae | 2012-11-12 20:14:36 +0100 | [diff] [blame] | 2 | #include "osdefs.h" |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 3 | #ifdef MS_WINDOWS |
| 4 | # include <windows.h> |
| 5 | #endif |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 6 | |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 7 | #ifdef HAVE_LANGINFO_H |
| 8 | #include <langinfo.h> |
| 9 | #endif |
| 10 | |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame^] | 11 | #ifdef __APPLE__ |
| 12 | extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size); |
| 13 | #endif |
| 14 | |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 15 | PyObject * |
| 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 Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 45 | #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 Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 55 | 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 Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 58 | 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 Stinner | 19de4c3 | 2010-11-08 23:30:46 +0000 | [diff] [blame] | 61 | |
| 62 | Conversion errors should never happen, unless there is a bug in the C |
| 63 | library. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 64 | wchar_t* |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 65 | _Py_char2wchar(const char* arg, size_t *size) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 66 | { |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame^] | 67 | #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 Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 76 | 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 Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 102 | !Py_UNICODE_IS_SURROGATE(*tmp); tmp++) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 103 | ; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 104 | if (*tmp == 0) { |
| 105 | if (size != NULL) |
| 106 | *size = count; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 107 | return res; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 108 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 109 | } |
| 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 Stinner | 19de4c3 | 2010-11-08 23:30:46 +0000 | [diff] [blame] | 120 | if (!res) |
| 121 | goto oom; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 122 | 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 Stinner | 19de4c3 | 2010-11-08 23:30:46 +0000 | [diff] [blame] | 135 | PyMem_Free(res); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 136 | if (size != NULL) |
| 137 | *size = (size_t)-2; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 138 | 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 Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 148 | if (Py_UNICODE_IS_SURROGATE(*out)) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 149 | /* 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 Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame^] | 161 | #else /* HAVE_MBRTOWC */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 162 | /* 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 Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 166 | if (!res) |
| 167 | goto oom; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 168 | 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 Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame^] | 176 | #endif /* HAVE_MBRTOWC */ |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 177 | if (size != NULL) |
| 178 | *size = out - res; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 179 | return res; |
| 180 | oom: |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 181 | if (size != NULL) |
| 182 | *size = (size_t)-1; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 183 | return NULL; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame^] | 184 | #endif /* __APPLE__ */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 185 | } |
| 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 Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 194 | the memory), or NULL on encoding or memory allocation error. |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 195 | |
| 196 | If error_pos is not NULL: *error_pos is the index of the invalid character |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 197 | on encoding error, or (size_t)-1 otherwise. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 198 | char* |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 199 | _Py_wchar2char(const wchar_t *text, size_t *error_pos) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 200 | { |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame^] | 201 | #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 Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 229 | 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 Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 234 | if (error_pos != NULL) |
| 235 | *error_pos = (size_t)-1; |
| 236 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 237 | /* 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 Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 264 | if (error_pos != NULL) |
| 265 | *error_pos = i; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 266 | 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 Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame^] | 288 | #endif /* __APPLE__ */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 291 | /* 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 Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 298 | #if defined(HAVE_STAT) && !defined(MS_WINDOWS) |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 299 | |
| 300 | /* Get file status. Encode the path to the locale encoding. */ |
| 301 | |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 302 | int |
| 303 | _Py_wstat(const wchar_t* path, struct stat *buf) |
| 304 | { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 305 | int err; |
| 306 | char *fname; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 307 | fname = _Py_wchar2char(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 308 | if (fname == NULL) { |
| 309 | errno = EINVAL; |
| 310 | return -1; |
| 311 | } |
| 312 | err = stat(fname, buf); |
| 313 | PyMem_Free(fname); |
| 314 | return err; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 315 | } |
| 316 | #endif |
| 317 | |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 318 | /* 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 Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 321 | Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was |
| 322 | raised. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 323 | |
| 324 | int |
Victor Stinner | a4a7595 | 2010-10-07 22:23:10 +0000 | [diff] [blame] | 325 | _Py_stat(PyObject *path, struct stat *statbuf) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 326 | { |
| 327 | #ifdef MS_WINDOWS |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 328 | int err; |
| 329 | struct _stat wstatbuf; |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 330 | wchar_t *wpath; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 331 | |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 332 | wpath = PyUnicode_AsUnicode(path); |
| 333 | if (wpath == NULL) |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 334 | return -2; |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 335 | err = _wstat(wpath, &wstatbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 336 | if (!err) |
| 337 | statbuf->st_mode = wstatbuf.st_mode; |
| 338 | return err; |
| 339 | #else |
| 340 | int ret; |
Victor Stinner | a4a7595 | 2010-10-07 22:23:10 +0000 | [diff] [blame] | 341 | PyObject *bytes = PyUnicode_EncodeFSDefault(path); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 342 | if (bytes == NULL) |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 343 | return -2; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 344 | ret = stat(PyBytes_AS_STRING(bytes), statbuf); |
| 345 | Py_DECREF(bytes); |
| 346 | return ret; |
| 347 | #endif |
| 348 | } |
| 349 | |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 350 | /* Open a file. Use _wfopen() on Windows, encode the path to the locale |
| 351 | encoding and use fopen() otherwise. */ |
| 352 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 353 | FILE * |
| 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 Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 366 | cpath = _Py_wchar2char(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 367 | 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 Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 377 | /* 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 Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 382 | |
| 383 | FILE* |
Victor Stinner | a4a7595 | 2010-10-07 22:23:10 +0000 | [diff] [blame] | 384 | _Py_fopen(PyObject *path, const char *mode) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 385 | { |
| 386 | #ifdef MS_WINDOWS |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 387 | wchar_t *wpath; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 388 | wchar_t wmode[10]; |
| 389 | int usize; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 390 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 391 | 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 Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 397 | wpath = PyUnicode_AsUnicode(path); |
| 398 | if (wpath == NULL) |
| 399 | return NULL; |
| 400 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 401 | usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode)); |
| 402 | if (usize == 0) |
| 403 | return NULL; |
| 404 | |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 405 | return _wfopen(wpath, wmode); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 406 | #else |
| 407 | FILE *f; |
Antoine Pitrou | 2b1cc89 | 2011-12-19 18:19:06 +0100 | [diff] [blame] | 408 | PyObject *bytes; |
| 409 | if (!PyUnicode_FSConverter(path, &bytes)) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 410 | 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 Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 418 | |
| 419 | /* Read value of symbolic link. Encode the path to the locale encoding, decode |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 420 | the result from the locale encoding. Return -1 on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 421 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 422 | int |
| 423 | _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz) |
| 424 | { |
| 425 | char *cpath; |
| 426 | char cbuf[PATH_MAX]; |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 427 | wchar_t *wbuf; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 428 | int res; |
| 429 | size_t r1; |
| 430 | |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 431 | cpath = _Py_wchar2char(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 432 | 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 Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 445 | wbuf = _Py_char2wchar(cbuf, &r1); |
Victor Stinner | 350147b | 2010-10-16 22:52:09 +0000 | [diff] [blame] | 446 | if (wbuf == NULL) { |
| 447 | errno = EINVAL; |
| 448 | return -1; |
| 449 | } |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 450 | if (bufsiz <= r1) { |
| 451 | PyMem_Free(wbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 452 | errno = EINVAL; |
| 453 | return -1; |
| 454 | } |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 455 | wcsncpy(buf, wbuf, bufsiz); |
| 456 | PyMem_Free(wbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 457 | return (int)r1; |
| 458 | } |
| 459 | #endif |
| 460 | |
| 461 | #ifdef HAVE_REALPATH |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 462 | |
| 463 | /* Return the canonicalized absolute pathname. Encode path to the locale |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 464 | encoding, decode the result from the locale encoding. |
| 465 | Return NULL on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 466 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 467 | wchar_t* |
Victor Stinner | 015f4d8 | 2010-10-07 22:29:53 +0000 | [diff] [blame] | 468 | _Py_wrealpath(const wchar_t *path, |
| 469 | wchar_t *resolved_path, size_t resolved_path_size) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 470 | { |
| 471 | char *cpath; |
| 472 | char cresolved_path[PATH_MAX]; |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 473 | wchar_t *wresolved_path; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 474 | char *res; |
| 475 | size_t r; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 476 | cpath = _Py_wchar2char(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 477 | 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 Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 485 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 486 | wresolved_path = _Py_char2wchar(cresolved_path, &r); |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 487 | if (wresolved_path == NULL) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 488 | errno = EINVAL; |
| 489 | return NULL; |
| 490 | } |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 491 | 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 Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 498 | return resolved_path; |
| 499 | } |
| 500 | #endif |
| 501 | |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 502 | /* Get the current directory. size is the buffer size in wide characters |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 503 | including the null character. Decode the path from the locale encoding. |
| 504 | Return NULL on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 505 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 506 | wchar_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 Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 513 | wchar_t *wname; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 514 | size_t len; |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 515 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 516 | if (getcwd(fname, PATH_MAX) == NULL) |
| 517 | return NULL; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 518 | wname = _Py_char2wchar(fname, &len); |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 519 | if (wname == NULL) |
| 520 | return NULL; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 521 | if (size <= len) { |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 522 | PyMem_Free(wname); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 523 | return NULL; |
| 524 | } |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 525 | wcsncpy(buf, wname, size); |
| 526 | PyMem_Free(wname); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 527 | return buf; |
| 528 | #endif |
| 529 | } |
| 530 | |
| 531 | #endif |