blob: e987df1b1fdfba0f27450918d701c731da0d91a9 [file] [log] [blame]
Victor Stinner31a83932017-12-04 13:39:15 +01001/* Path configuration like module_search_path (sys.path) */
2
3#include "Python.h"
4#include "osdefs.h"
5#include "internal/pystate.h"
Nick Coghland5d9e022018-03-25 23:03:10 +10006#include <wchar.h>
Victor Stinner31a83932017-12-04 13:39:15 +01007
8#ifdef __cplusplus
9extern "C" {
10#endif
11
12
13_PyPathConfig _Py_path_config = _PyPathConfig_INIT;
14
15
Victor Stinnerb1147e42018-07-21 02:06:16 +020016static int
17copy_wstr(wchar_t **dst, const wchar_t *src)
18{
19 if (src != NULL) {
20 *dst = _PyMem_RawWcsdup(src);
21 if (*dst == NULL) {
22 return -1;
23 }
24 }
25 else {
26 *dst = NULL;
27 }
28 return 0;
29}
30
31
32static void
Victor Stinner31a83932017-12-04 13:39:15 +010033_PyPathConfig_Clear(_PyPathConfig *config)
34{
35 /* _PyMem_SetDefaultAllocator() is needed to get a known memory allocator,
36 since Py_SetPath(), Py_SetPythonHome() and Py_SetProgramName() can be
37 called before Py_Initialize() which can changes the memory allocator. */
38 PyMemAllocatorEx old_alloc;
39 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
40
41#define CLEAR(ATTR) \
42 do { \
43 PyMem_RawFree(ATTR); \
44 ATTR = NULL; \
45 } while (0)
46
47 CLEAR(config->prefix);
48 CLEAR(config->program_full_path);
49#ifdef MS_WINDOWS
50 CLEAR(config->dll_path);
51#else
52 CLEAR(config->exec_prefix);
53#endif
54 CLEAR(config->module_search_path);
55 CLEAR(config->home);
56 CLEAR(config->program_name);
57#undef CLEAR
58
59 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
60}
61
62
Victor Stinnerb1147e42018-07-21 02:06:16 +020063/* Calculate the path configuration: initialize path_config from core_config */
64static _PyInitError
65_PyPathConfig_Calculate(_PyPathConfig *path_config,
66 const _PyCoreConfig *core_config)
Victor Stinner31a83932017-12-04 13:39:15 +010067{
Victor Stinner31a83932017-12-04 13:39:15 +010068 _PyInitError err;
69 _PyPathConfig new_config = _PyPathConfig_INIT;
70
71 PyMemAllocatorEx old_alloc;
72 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
73
74 /* Calculate program_full_path, prefix, exec_prefix (Unix)
75 or dll_path (Windows), and module_search_path */
Victor Stinnerb1147e42018-07-21 02:06:16 +020076 err = _PyPathConfig_Calculate_impl(&new_config, core_config);
Victor Stinner31a83932017-12-04 13:39:15 +010077 if (_Py_INIT_FAILED(err)) {
Victor Stinnerb1147e42018-07-21 02:06:16 +020078 goto err;
Victor Stinner31a83932017-12-04 13:39:15 +010079 }
80
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +010081 /* Copy home and program_name from core_config */
Victor Stinnerb1147e42018-07-21 02:06:16 +020082 if (copy_wstr(&new_config.home, core_config->home) < 0) {
83 err = _Py_INIT_NO_MEMORY();
84 goto err;
Victor Stinner31a83932017-12-04 13:39:15 +010085 }
Victor Stinnerb1147e42018-07-21 02:06:16 +020086 if (copy_wstr(&new_config.program_name, core_config->program_name) < 0) {
87 err = _Py_INIT_NO_MEMORY();
88 goto err;
Victor Stinner31a83932017-12-04 13:39:15 +010089 }
90
Victor Stinnerb1147e42018-07-21 02:06:16 +020091 _PyPathConfig_Clear(path_config);
92 *path_config = new_config;
93
94 err = _Py_INIT_OK();
95 goto done;
96
97err:
98 _PyPathConfig_Clear(&new_config);
99
100done:
101 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
102 return err;
103}
104
105
106_PyInitError
107_PyPathConfig_SetGlobal(const _PyPathConfig *config)
108{
109 _PyInitError err;
110 _PyPathConfig new_config = _PyPathConfig_INIT;
111
112 PyMemAllocatorEx old_alloc;
113 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
114
115#define COPY_ATTR(ATTR) \
116 do { \
117 if (copy_wstr(&new_config.ATTR, config->ATTR) < 0) { \
118 _PyPathConfig_Clear(&new_config); \
119 err = _Py_INIT_NO_MEMORY(); \
120 goto done; \
121 } \
122 } while (0)
123
124 COPY_ATTR(program_full_path);
125 COPY_ATTR(prefix);
126#ifdef MS_WINDOWS
127 COPY_ATTR(dll_path);
128#else
129 COPY_ATTR(exec_prefix);
130#endif
131 COPY_ATTR(module_search_path);
132 COPY_ATTR(program_name);
133 COPY_ATTR(home);
Victor Stinner31a83932017-12-04 13:39:15 +0100134
135 _PyPathConfig_Clear(&_Py_path_config);
Victor Stinnerb1147e42018-07-21 02:06:16 +0200136 /* Steal new_config strings; don't clear new_config */
Victor Stinner31a83932017-12-04 13:39:15 +0100137 _Py_path_config = new_config;
138
139 err = _Py_INIT_OK();
140
141done:
142 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
143 return err;
144}
145
146
Victor Stinnerb1147e42018-07-21 02:06:16 +0200147void
148_PyPathConfig_ClearGlobal(void)
149{
150 _PyPathConfig_Clear(&_Py_path_config);
151}
152
153
154static wchar_t*
155wstrlist_join(wchar_t sep, int count, wchar_t **list)
156{
157 size_t len = 1; /* NUL terminator */
158 for (int i=0; i < count; i++) {
159 if (i != 0) {
160 len++;
161 }
162 len += wcslen(list[i]);
163 }
164
165 wchar_t *text = PyMem_RawMalloc(len * sizeof(wchar_t));
166 if (text == NULL) {
167 return NULL;
168 }
169 wchar_t *str = text;
170 for (int i=0; i < count; i++) {
171 wchar_t *path = list[i];
172 if (i != 0) {
173 *str++ = SEP;
174 }
175 len = wcslen(path);
176 memcpy(str, path, len * sizeof(wchar_t));
177 str += len;
178 }
179 *str = L'\0';
180
181 return text;
182}
183
184
185/* Set the global path configuration from core_config. */
186_PyInitError
187_PyCoreConfig_SetPathConfig(const _PyCoreConfig *core_config)
188{
189 PyMemAllocatorEx old_alloc;
190 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
191
192 _PyInitError err;
193 _PyPathConfig path_config = _PyPathConfig_INIT;
194
195 path_config.module_search_path = wstrlist_join(DELIM,
196 core_config->nmodule_search_path,
197 core_config->module_search_paths);
198 if (path_config.module_search_path == NULL) {
199 goto no_memory;
200 }
201
202 if (copy_wstr(&path_config.program_full_path, core_config->executable) < 0) {
203 goto no_memory;
204 }
205 if (copy_wstr(&path_config.prefix, core_config->prefix) < 0) {
206 goto no_memory;
207 }
208#ifdef MS_WINDOWS
209 if (copy_wstr(&path_config.dll_path, core_config->dll_path) < 0) {
210 goto no_memory;
211 }
212#else
213 if (copy_wstr(&path_config.exec_prefix, core_config->exec_prefix) < 0) {
214 goto no_memory;
215 }
216#endif
217 if (copy_wstr(&path_config.program_name, core_config->program_name) < 0) {
218 goto no_memory;
219 }
220 if (copy_wstr(&path_config.home, core_config->home) < 0) {
221 goto no_memory;
222 }
223
224 err = _PyPathConfig_SetGlobal(&path_config);
225 if (_Py_INIT_FAILED(err)) {
226 goto done;
227 }
228
229 err = _Py_INIT_OK();
230 goto done;
231
232no_memory:
233 err = _Py_INIT_NO_MEMORY();
234
235done:
236 _PyPathConfig_Clear(&path_config);
237 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
238 return err;
239}
240
241
242static _PyInitError
243core_config_init_module_search_paths(_PyCoreConfig *config,
244 _PyPathConfig *path_config)
245{
246 assert(config->module_search_paths == NULL);
247 assert(config->nmodule_search_path < 0);
248
249 config->nmodule_search_path = 0;
250
251 const wchar_t *sys_path = path_config->module_search_path;
252 const wchar_t delim = DELIM;
253 const wchar_t *p = sys_path;
254 while (1) {
255 p = wcschr(sys_path, delim);
256 if (p == NULL) {
257 p = sys_path + wcslen(sys_path); /* End of string */
258 }
259
260 size_t path_len = (p - sys_path);
261 wchar_t *path = PyMem_RawMalloc((path_len + 1) * sizeof(wchar_t));
262 if (path == NULL) {
263 return _Py_INIT_NO_MEMORY();
264 }
265 memcpy(path, sys_path, path_len * sizeof(wchar_t));
266 path[path_len] = L'\0';
267
268 _PyInitError err = _Py_wstrlist_append(&config->nmodule_search_path,
269 &config->module_search_paths,
270 path);
271 PyMem_RawFree(path);
272 if (_Py_INIT_FAILED(err)) {
273 return err;
274 }
275
276 if (*p == '\0') {
277 break;
278 }
279 sys_path = p + 1;
280 }
281 return _Py_INIT_OK();
282}
283
284
285_PyInitError
286_PyCoreConfig_InitPathConfig(_PyCoreConfig *config)
287{
288 _PyPathConfig path_config = _PyPathConfig_INIT;
289 _PyInitError err;
290
291 err = _PyPathConfig_Calculate(&path_config, config);
292 if (_Py_INIT_FAILED(err)) {
293 goto error;
294 }
295
296 if (config->nmodule_search_path < 0) {
297 err = core_config_init_module_search_paths(config, &path_config);
298 if (_Py_INIT_FAILED(err)) {
299 goto error;
300 }
301 }
302
303 if (config->executable == NULL) {
304 if (copy_wstr(&config->executable,
305 path_config.program_full_path) < 0) {
306 goto no_memory;
307 }
308 }
309
310 if (config->prefix == NULL) {
311 if (copy_wstr(&config->prefix, path_config.prefix) < 0) {
312 goto no_memory;
313 }
314 }
315
316 if (config->exec_prefix == NULL) {
317#ifdef MS_WINDOWS
318 wchar_t *exec_prefix = path_config.prefix;
319#else
320 wchar_t *exec_prefix = path_config.exec_prefix;
321#endif
322 if (copy_wstr(&config->exec_prefix, exec_prefix) < 0) {
323 goto no_memory;
324 }
325 }
326
327#ifdef MS_WINDOWS
328 if (config->dll_path == NULL) {
329 if (copy_wstr(&config->dll_path, path_config.dll_path) < 0) {
330 goto no_memory;
331 }
332 }
333#endif
334
335 if (config->base_prefix == NULL) {
336 if (copy_wstr(&config->base_prefix, config->prefix) < 0) {
337 goto no_memory;
338 }
339 }
340
341 if (config->base_exec_prefix == NULL) {
342 if (copy_wstr(&config->base_exec_prefix, config->exec_prefix) < 0) {
343 goto no_memory;
344 }
345 }
346
347 _PyPathConfig_Clear(&path_config);
348 return _Py_INIT_OK();
349
350no_memory:
351 err = _Py_INIT_NO_MEMORY();
352
353error:
354 _PyPathConfig_Clear(&path_config);
355 return err;
356}
357
358
Victor Stinner31a83932017-12-04 13:39:15 +0100359static void
360pathconfig_global_init(void)
361{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200362 if (_Py_path_config.module_search_path != NULL) {
Victor Stinner31a83932017-12-04 13:39:15 +0100363 /* Already initialized */
364 return;
365 }
366
367 _PyInitError err;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200368 _PyPathConfig path_config = _PyPathConfig_INIT;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100369 _PyCoreConfig config = _PyCoreConfig_INIT;
Victor Stinner31a83932017-12-04 13:39:15 +0100370
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100371 err = _PyCoreConfig_Read(&config);
Victor Stinner31a83932017-12-04 13:39:15 +0100372 if (_Py_INIT_FAILED(err)) {
373 goto error;
374 }
375
Victor Stinnerb1147e42018-07-21 02:06:16 +0200376 err = _PyPathConfig_Calculate(&path_config, &config);
Victor Stinner31a83932017-12-04 13:39:15 +0100377 if (_Py_INIT_FAILED(err)) {
378 goto error;
379 }
380
Victor Stinnerb1147e42018-07-21 02:06:16 +0200381 err = _PyPathConfig_SetGlobal(&path_config);
382 if (_Py_INIT_FAILED(err)) {
383 goto error;
384 }
385
386 _PyPathConfig_Clear(&path_config);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100387 _PyCoreConfig_Clear(&config);
Victor Stinner31a83932017-12-04 13:39:15 +0100388 return;
389
390error:
Victor Stinnerb1147e42018-07-21 02:06:16 +0200391 _PyPathConfig_Clear(&path_config);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100392 _PyCoreConfig_Clear(&config);
Victor Stinner31a83932017-12-04 13:39:15 +0100393 _Py_FatalInitError(err);
394}
395
396
397/* External interface */
398
399void
400Py_SetPath(const wchar_t *path)
401{
402 if (path == NULL) {
403 _PyPathConfig_Clear(&_Py_path_config);
404 return;
405 }
406
407 PyMemAllocatorEx old_alloc;
408 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
409
410 _PyPathConfig new_config;
411 new_config.program_full_path = _PyMem_RawWcsdup(Py_GetProgramName());
Victor Stinnerb1147e42018-07-21 02:06:16 +0200412 int alloc_error = (new_config.program_full_path == NULL);
Victor Stinner31a83932017-12-04 13:39:15 +0100413 new_config.prefix = _PyMem_RawWcsdup(L"");
Victor Stinnerb1147e42018-07-21 02:06:16 +0200414 alloc_error |= (new_config.prefix == NULL);
Victor Stinner31a83932017-12-04 13:39:15 +0100415#ifdef MS_WINDOWS
416 new_config.dll_path = _PyMem_RawWcsdup(L"");
Victor Stinnerb1147e42018-07-21 02:06:16 +0200417 alloc_error |= (new_config.dll_path == NULL);
Victor Stinner31a83932017-12-04 13:39:15 +0100418#else
419 new_config.exec_prefix = _PyMem_RawWcsdup(L"");
Victor Stinnerb1147e42018-07-21 02:06:16 +0200420 alloc_error |= (new_config.exec_prefix == NULL);
Victor Stinner31a83932017-12-04 13:39:15 +0100421#endif
422 new_config.module_search_path = _PyMem_RawWcsdup(path);
Victor Stinnerb1147e42018-07-21 02:06:16 +0200423 alloc_error |= (new_config.module_search_path == NULL);
Victor Stinner31a83932017-12-04 13:39:15 +0100424
425 /* steal the home and program_name values (to leave them unchanged) */
426 new_config.home = _Py_path_config.home;
427 _Py_path_config.home = NULL;
428 new_config.program_name = _Py_path_config.program_name;
429 _Py_path_config.program_name = NULL;
430
431 _PyPathConfig_Clear(&_Py_path_config);
432 _Py_path_config = new_config;
433
434 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerb1147e42018-07-21 02:06:16 +0200435
436 if (alloc_error) {
437 Py_FatalError("Py_SetPath() failed: out of memory");
438 }
Victor Stinner31a83932017-12-04 13:39:15 +0100439}
440
441
442void
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200443Py_SetPythonHome(const wchar_t *home)
Victor Stinner31a83932017-12-04 13:39:15 +0100444{
445 if (home == NULL) {
446 return;
447 }
448
449 PyMemAllocatorEx old_alloc;
450 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
451
452 PyMem_RawFree(_Py_path_config.home);
453 _Py_path_config.home = _PyMem_RawWcsdup(home);
454
455 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
456
457 if (_Py_path_config.home == NULL) {
458 Py_FatalError("Py_SetPythonHome() failed: out of memory");
459 }
460}
461
462
463void
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200464Py_SetProgramName(const wchar_t *program_name)
Victor Stinner31a83932017-12-04 13:39:15 +0100465{
466 if (program_name == NULL || program_name[0] == L'\0') {
467 return;
468 }
469
470 PyMemAllocatorEx old_alloc;
471 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
472
473 PyMem_RawFree(_Py_path_config.program_name);
474 _Py_path_config.program_name = _PyMem_RawWcsdup(program_name);
475
476 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
477
478 if (_Py_path_config.program_name == NULL) {
479 Py_FatalError("Py_SetProgramName() failed: out of memory");
480 }
481}
482
483
484wchar_t *
485Py_GetPath(void)
486{
487 pathconfig_global_init();
488 return _Py_path_config.module_search_path;
489}
490
491
492wchar_t *
493Py_GetPrefix(void)
494{
495 pathconfig_global_init();
496 return _Py_path_config.prefix;
497}
498
499
500wchar_t *
501Py_GetExecPrefix(void)
502{
503#ifdef MS_WINDOWS
504 return Py_GetPrefix();
505#else
506 pathconfig_global_init();
507 return _Py_path_config.exec_prefix;
508#endif
509}
510
511
512wchar_t *
513Py_GetProgramFullPath(void)
514{
515 pathconfig_global_init();
516 return _Py_path_config.program_full_path;
517}
518
519
520wchar_t*
521Py_GetPythonHome(void)
522{
523 pathconfig_global_init();
524 return _Py_path_config.home;
525}
526
527
528wchar_t *
529Py_GetProgramName(void)
530{
531 pathconfig_global_init();
532 return _Py_path_config.program_name;
533}
534
Victor Stinner11a247d2017-12-13 21:05:57 +0100535/* Compute argv[0] which will be prepended to sys.argv */
536PyObject*
537_PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
538{
539 wchar_t *argv0;
540 wchar_t *p = NULL;
541 Py_ssize_t n = 0;
Nick Coghland5d9e022018-03-25 23:03:10 +1000542 int have_script_arg = 0;
543 int have_module_arg = 0;
Victor Stinner11a247d2017-12-13 21:05:57 +0100544#ifdef HAVE_READLINK
545 wchar_t link[MAXPATHLEN+1];
546 wchar_t argv0copy[2*MAXPATHLEN+1];
547 int nr = 0;
548#endif
549#if defined(HAVE_REALPATH)
550 wchar_t fullpath[MAXPATHLEN];
551#elif defined(MS_WINDOWS)
552 wchar_t fullpath[MAX_PATH];
553#endif
554
Victor Stinner11a247d2017-12-13 21:05:57 +0100555 argv0 = argv[0];
Nick Coghland5d9e022018-03-25 23:03:10 +1000556 if (argc > 0 && argv0 != NULL) {
557 have_module_arg = (wcscmp(argv0, L"-m") == 0);
558 have_script_arg = !have_module_arg && (wcscmp(argv0, L"-c") != 0);
559 }
560
561 if (have_module_arg) {
562 #if defined(HAVE_REALPATH) || defined(MS_WINDOWS)
563 _Py_wgetcwd(fullpath, Py_ARRAY_LENGTH(fullpath));
564 argv0 = fullpath;
565 n = wcslen(argv0);
566 #else
567 argv0 = L".";
568 n = 1;
569 #endif
570 }
Victor Stinner11a247d2017-12-13 21:05:57 +0100571
572#ifdef HAVE_READLINK
Nick Coghland5d9e022018-03-25 23:03:10 +1000573 if (have_script_arg)
Victor Stinner11a247d2017-12-13 21:05:57 +0100574 nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
575 if (nr > 0) {
576 /* It's a symlink */
577 link[nr] = '\0';
578 if (link[0] == SEP)
579 argv0 = link; /* Link to absolute path */
580 else if (wcschr(link, SEP) == NULL)
581 ; /* Link without path */
582 else {
583 /* Must join(dirname(argv0), link) */
584 wchar_t *q = wcsrchr(argv0, SEP);
585 if (q == NULL)
586 argv0 = link; /* argv0 without path */
587 else {
588 /* Must make a copy, argv0copy has room for 2 * MAXPATHLEN */
589 wcsncpy(argv0copy, argv0, MAXPATHLEN);
590 q = wcsrchr(argv0copy, SEP);
591 wcsncpy(q+1, link, MAXPATHLEN);
592 q[MAXPATHLEN + 1] = L'\0';
593 argv0 = argv0copy;
594 }
595 }
596 }
597#endif /* HAVE_READLINK */
598
599#if SEP == '\\'
600 /* Special case for Microsoft filename syntax */
Nick Coghland5d9e022018-03-25 23:03:10 +1000601 if (have_script_arg) {
Victor Stinner11a247d2017-12-13 21:05:57 +0100602 wchar_t *q;
603#if defined(MS_WINDOWS)
604 /* Replace the first element in argv with the full path. */
605 wchar_t *ptemp;
606 if (GetFullPathNameW(argv0,
607 Py_ARRAY_LENGTH(fullpath),
608 fullpath,
609 &ptemp)) {
610 argv0 = fullpath;
611 }
612#endif
613 p = wcsrchr(argv0, SEP);
614 /* Test for alternate separator */
615 q = wcsrchr(p ? p : argv0, '/');
616 if (q != NULL)
617 p = q;
618 if (p != NULL) {
619 n = p + 1 - argv0;
620 if (n > 1 && p[-1] != ':')
621 n--; /* Drop trailing separator */
622 }
623 }
624#else /* All other filename syntaxes */
Nick Coghland5d9e022018-03-25 23:03:10 +1000625 if (have_script_arg) {
Victor Stinner11a247d2017-12-13 21:05:57 +0100626#if defined(HAVE_REALPATH)
627 if (_Py_wrealpath(argv0, fullpath, Py_ARRAY_LENGTH(fullpath))) {
628 argv0 = fullpath;
629 }
630#endif
631 p = wcsrchr(argv0, SEP);
632 }
633 if (p != NULL) {
634 n = p + 1 - argv0;
635#if SEP == '/' /* Special case for Unix filename syntax */
636 if (n > 1)
637 n--; /* Drop trailing separator */
638#endif /* Unix */
639 }
640#endif /* All others */
641
642 return PyUnicode_FromWideChar(argv0, n);
643}
644
Victor Stinner9bee3292017-12-21 16:49:13 +0100645
646/* Search for a prefix value in an environment file (pyvenv.cfg).
647 If found, copy it into the provided buffer. */
648int
649_Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key,
650 wchar_t *value, size_t value_size)
651{
652 int result = 0; /* meaning not found */
653 char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */
654
655 fseek(env_file, 0, SEEK_SET);
656 while (!feof(env_file)) {
657 char * p = fgets(buffer, MAXPATHLEN*2, env_file);
Victor Stinner9bee3292017-12-21 16:49:13 +0100658
659 if (p == NULL) {
660 break;
661 }
Victor Stinner05d68a82018-01-18 11:15:25 +0100662
663 size_t n = strlen(p);
Victor Stinner9bee3292017-12-21 16:49:13 +0100664 if (p[n - 1] != '\n') {
665 /* line has overflowed - bail */
666 break;
667 }
668 if (p[0] == '#') {
669 /* Comment - skip */
670 continue;
671 }
Victor Stinner05d68a82018-01-18 11:15:25 +0100672
673 wchar_t *tmpbuffer = _Py_DecodeUTF8_surrogateescape(buffer, n);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100674 if (tmpbuffer) {
Victor Stinner9bee3292017-12-21 16:49:13 +0100675 wchar_t * state;
676 wchar_t * tok = wcstok(tmpbuffer, L" \t\r\n", &state);
677 if ((tok != NULL) && !wcscmp(tok, key)) {
678 tok = wcstok(NULL, L" \t", &state);
679 if ((tok != NULL) && !wcscmp(tok, L"=")) {
680 tok = wcstok(NULL, L"\r\n", &state);
681 if (tok != NULL) {
682 wcsncpy(value, tok, MAXPATHLEN);
683 result = 1;
684 PyMem_RawFree(tmpbuffer);
685 break;
686 }
687 }
688 }
689 PyMem_RawFree(tmpbuffer);
690 }
691 }
692 return result;
693}
694
Victor Stinner31a83932017-12-04 13:39:15 +0100695#ifdef __cplusplus
696}
697#endif