blob: 1f6177faec4eca8942a6244beb993744704df55a [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
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200286_PyCoreConfig_InitPathConfig(_PyCoreConfig *config,
287 int *isolated, int *no_site_import)
Victor Stinnerb1147e42018-07-21 02:06:16 +0200288{
289 _PyPathConfig path_config = _PyPathConfig_INIT;
290 _PyInitError err;
291
292 err = _PyPathConfig_Calculate(&path_config, config);
293 if (_Py_INIT_FAILED(err)) {
294 goto error;
295 }
296
297 if (config->nmodule_search_path < 0) {
298 err = core_config_init_module_search_paths(config, &path_config);
299 if (_Py_INIT_FAILED(err)) {
300 goto error;
301 }
302 }
303
304 if (config->executable == NULL) {
305 if (copy_wstr(&config->executable,
306 path_config.program_full_path) < 0) {
307 goto no_memory;
308 }
309 }
310
311 if (config->prefix == NULL) {
312 if (copy_wstr(&config->prefix, path_config.prefix) < 0) {
313 goto no_memory;
314 }
315 }
316
317 if (config->exec_prefix == NULL) {
318#ifdef MS_WINDOWS
319 wchar_t *exec_prefix = path_config.prefix;
320#else
321 wchar_t *exec_prefix = path_config.exec_prefix;
322#endif
323 if (copy_wstr(&config->exec_prefix, exec_prefix) < 0) {
324 goto no_memory;
325 }
326 }
327
328#ifdef MS_WINDOWS
329 if (config->dll_path == NULL) {
330 if (copy_wstr(&config->dll_path, path_config.dll_path) < 0) {
331 goto no_memory;
332 }
333 }
334#endif
335
336 if (config->base_prefix == NULL) {
337 if (copy_wstr(&config->base_prefix, config->prefix) < 0) {
338 goto no_memory;
339 }
340 }
341
342 if (config->base_exec_prefix == NULL) {
343 if (copy_wstr(&config->base_exec_prefix, config->exec_prefix) < 0) {
344 goto no_memory;
345 }
346 }
347
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200348 if (path_config.isolated != -1 && isolated != NULL) {
349 *isolated = path_config.isolated;
350 }
351 if (path_config.no_site_import != -1 && no_site_import != NULL) {
352 *no_site_import = path_config.no_site_import;
353 }
354
Victor Stinnerb1147e42018-07-21 02:06:16 +0200355 _PyPathConfig_Clear(&path_config);
356 return _Py_INIT_OK();
357
358no_memory:
359 err = _Py_INIT_NO_MEMORY();
360
361error:
362 _PyPathConfig_Clear(&path_config);
363 return err;
364}
365
366
Victor Stinner31a83932017-12-04 13:39:15 +0100367static void
368pathconfig_global_init(void)
369{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200370 if (_Py_path_config.module_search_path != NULL) {
Victor Stinner31a83932017-12-04 13:39:15 +0100371 /* Already initialized */
372 return;
373 }
374
375 _PyInitError err;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100376 _PyCoreConfig config = _PyCoreConfig_INIT;
Victor Stinner31a83932017-12-04 13:39:15 +0100377
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200378 /* Py_IsolatedFlag and Py_NoSiteFlag are left unchanged: pass NULL.
379 _PyCoreConfig_InitPathConfig() will be called later and will set
380 these flags. */
381 err = _PyCoreConfig_Read(&config, NULL, NULL);
Victor Stinner31a83932017-12-04 13:39:15 +0100382 if (_Py_INIT_FAILED(err)) {
383 goto error;
384 }
385
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200386 err = _PyCoreConfig_SetPathConfig(&config);
Victor Stinner31a83932017-12-04 13:39:15 +0100387 if (_Py_INIT_FAILED(err)) {
388 goto error;
389 }
390
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100391 _PyCoreConfig_Clear(&config);
Victor Stinner31a83932017-12-04 13:39:15 +0100392 return;
393
394error:
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100395 _PyCoreConfig_Clear(&config);
Victor Stinner31a83932017-12-04 13:39:15 +0100396 _Py_FatalInitError(err);
397}
398
399
400/* External interface */
401
402void
403Py_SetPath(const wchar_t *path)
404{
405 if (path == NULL) {
406 _PyPathConfig_Clear(&_Py_path_config);
407 return;
408 }
409
410 PyMemAllocatorEx old_alloc;
411 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
412
413 _PyPathConfig new_config;
414 new_config.program_full_path = _PyMem_RawWcsdup(Py_GetProgramName());
Victor Stinnerb1147e42018-07-21 02:06:16 +0200415 int alloc_error = (new_config.program_full_path == NULL);
Victor Stinner31a83932017-12-04 13:39:15 +0100416 new_config.prefix = _PyMem_RawWcsdup(L"");
Victor Stinnerb1147e42018-07-21 02:06:16 +0200417 alloc_error |= (new_config.prefix == NULL);
Victor Stinner31a83932017-12-04 13:39:15 +0100418#ifdef MS_WINDOWS
419 new_config.dll_path = _PyMem_RawWcsdup(L"");
Victor Stinnerb1147e42018-07-21 02:06:16 +0200420 alloc_error |= (new_config.dll_path == NULL);
Victor Stinner31a83932017-12-04 13:39:15 +0100421#else
422 new_config.exec_prefix = _PyMem_RawWcsdup(L"");
Victor Stinnerb1147e42018-07-21 02:06:16 +0200423 alloc_error |= (new_config.exec_prefix == NULL);
Victor Stinner31a83932017-12-04 13:39:15 +0100424#endif
425 new_config.module_search_path = _PyMem_RawWcsdup(path);
Victor Stinnerb1147e42018-07-21 02:06:16 +0200426 alloc_error |= (new_config.module_search_path == NULL);
Victor Stinner31a83932017-12-04 13:39:15 +0100427
428 /* steal the home and program_name values (to leave them unchanged) */
429 new_config.home = _Py_path_config.home;
430 _Py_path_config.home = NULL;
431 new_config.program_name = _Py_path_config.program_name;
432 _Py_path_config.program_name = NULL;
433
434 _PyPathConfig_Clear(&_Py_path_config);
435 _Py_path_config = new_config;
436
437 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerb1147e42018-07-21 02:06:16 +0200438
439 if (alloc_error) {
440 Py_FatalError("Py_SetPath() failed: out of memory");
441 }
Victor Stinner31a83932017-12-04 13:39:15 +0100442}
443
444
445void
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200446Py_SetPythonHome(const wchar_t *home)
Victor Stinner31a83932017-12-04 13:39:15 +0100447{
448 if (home == NULL) {
449 return;
450 }
451
452 PyMemAllocatorEx old_alloc;
453 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
454
455 PyMem_RawFree(_Py_path_config.home);
456 _Py_path_config.home = _PyMem_RawWcsdup(home);
457
458 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
459
460 if (_Py_path_config.home == NULL) {
461 Py_FatalError("Py_SetPythonHome() failed: out of memory");
462 }
463}
464
465
466void
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200467Py_SetProgramName(const wchar_t *program_name)
Victor Stinner31a83932017-12-04 13:39:15 +0100468{
469 if (program_name == NULL || program_name[0] == L'\0') {
470 return;
471 }
472
473 PyMemAllocatorEx old_alloc;
474 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
475
476 PyMem_RawFree(_Py_path_config.program_name);
477 _Py_path_config.program_name = _PyMem_RawWcsdup(program_name);
478
479 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
480
481 if (_Py_path_config.program_name == NULL) {
482 Py_FatalError("Py_SetProgramName() failed: out of memory");
483 }
484}
485
486
487wchar_t *
488Py_GetPath(void)
489{
490 pathconfig_global_init();
491 return _Py_path_config.module_search_path;
492}
493
494
495wchar_t *
496Py_GetPrefix(void)
497{
498 pathconfig_global_init();
499 return _Py_path_config.prefix;
500}
501
502
503wchar_t *
504Py_GetExecPrefix(void)
505{
506#ifdef MS_WINDOWS
507 return Py_GetPrefix();
508#else
509 pathconfig_global_init();
510 return _Py_path_config.exec_prefix;
511#endif
512}
513
514
515wchar_t *
516Py_GetProgramFullPath(void)
517{
518 pathconfig_global_init();
519 return _Py_path_config.program_full_path;
520}
521
522
523wchar_t*
524Py_GetPythonHome(void)
525{
526 pathconfig_global_init();
527 return _Py_path_config.home;
528}
529
530
531wchar_t *
532Py_GetProgramName(void)
533{
534 pathconfig_global_init();
535 return _Py_path_config.program_name;
536}
537
Victor Stinner11a247d2017-12-13 21:05:57 +0100538/* Compute argv[0] which will be prepended to sys.argv */
539PyObject*
540_PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
541{
542 wchar_t *argv0;
543 wchar_t *p = NULL;
544 Py_ssize_t n = 0;
Nick Coghland5d9e022018-03-25 23:03:10 +1000545 int have_script_arg = 0;
546 int have_module_arg = 0;
Victor Stinner11a247d2017-12-13 21:05:57 +0100547#ifdef HAVE_READLINK
548 wchar_t link[MAXPATHLEN+1];
549 wchar_t argv0copy[2*MAXPATHLEN+1];
550 int nr = 0;
551#endif
552#if defined(HAVE_REALPATH)
553 wchar_t fullpath[MAXPATHLEN];
554#elif defined(MS_WINDOWS)
555 wchar_t fullpath[MAX_PATH];
556#endif
557
Victor Stinner11a247d2017-12-13 21:05:57 +0100558 argv0 = argv[0];
Nick Coghland5d9e022018-03-25 23:03:10 +1000559 if (argc > 0 && argv0 != NULL) {
560 have_module_arg = (wcscmp(argv0, L"-m") == 0);
561 have_script_arg = !have_module_arg && (wcscmp(argv0, L"-c") != 0);
562 }
563
564 if (have_module_arg) {
565 #if defined(HAVE_REALPATH) || defined(MS_WINDOWS)
566 _Py_wgetcwd(fullpath, Py_ARRAY_LENGTH(fullpath));
567 argv0 = fullpath;
568 n = wcslen(argv0);
569 #else
570 argv0 = L".";
571 n = 1;
572 #endif
573 }
Victor Stinner11a247d2017-12-13 21:05:57 +0100574
575#ifdef HAVE_READLINK
Nick Coghland5d9e022018-03-25 23:03:10 +1000576 if (have_script_arg)
Victor Stinner11a247d2017-12-13 21:05:57 +0100577 nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
578 if (nr > 0) {
579 /* It's a symlink */
580 link[nr] = '\0';
581 if (link[0] == SEP)
582 argv0 = link; /* Link to absolute path */
583 else if (wcschr(link, SEP) == NULL)
584 ; /* Link without path */
585 else {
586 /* Must join(dirname(argv0), link) */
587 wchar_t *q = wcsrchr(argv0, SEP);
588 if (q == NULL)
589 argv0 = link; /* argv0 without path */
590 else {
591 /* Must make a copy, argv0copy has room for 2 * MAXPATHLEN */
592 wcsncpy(argv0copy, argv0, MAXPATHLEN);
593 q = wcsrchr(argv0copy, SEP);
594 wcsncpy(q+1, link, MAXPATHLEN);
595 q[MAXPATHLEN + 1] = L'\0';
596 argv0 = argv0copy;
597 }
598 }
599 }
600#endif /* HAVE_READLINK */
601
602#if SEP == '\\'
603 /* Special case for Microsoft filename syntax */
Nick Coghland5d9e022018-03-25 23:03:10 +1000604 if (have_script_arg) {
Victor Stinner11a247d2017-12-13 21:05:57 +0100605 wchar_t *q;
606#if defined(MS_WINDOWS)
607 /* Replace the first element in argv with the full path. */
608 wchar_t *ptemp;
609 if (GetFullPathNameW(argv0,
610 Py_ARRAY_LENGTH(fullpath),
611 fullpath,
612 &ptemp)) {
613 argv0 = fullpath;
614 }
615#endif
616 p = wcsrchr(argv0, SEP);
617 /* Test for alternate separator */
618 q = wcsrchr(p ? p : argv0, '/');
619 if (q != NULL)
620 p = q;
621 if (p != NULL) {
622 n = p + 1 - argv0;
623 if (n > 1 && p[-1] != ':')
624 n--; /* Drop trailing separator */
625 }
626 }
627#else /* All other filename syntaxes */
Nick Coghland5d9e022018-03-25 23:03:10 +1000628 if (have_script_arg) {
Victor Stinner11a247d2017-12-13 21:05:57 +0100629#if defined(HAVE_REALPATH)
630 if (_Py_wrealpath(argv0, fullpath, Py_ARRAY_LENGTH(fullpath))) {
631 argv0 = fullpath;
632 }
633#endif
634 p = wcsrchr(argv0, SEP);
635 }
636 if (p != NULL) {
637 n = p + 1 - argv0;
638#if SEP == '/' /* Special case for Unix filename syntax */
639 if (n > 1)
640 n--; /* Drop trailing separator */
641#endif /* Unix */
642 }
643#endif /* All others */
644
645 return PyUnicode_FromWideChar(argv0, n);
646}
647
Victor Stinner9bee3292017-12-21 16:49:13 +0100648
649/* Search for a prefix value in an environment file (pyvenv.cfg).
650 If found, copy it into the provided buffer. */
651int
652_Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key,
653 wchar_t *value, size_t value_size)
654{
655 int result = 0; /* meaning not found */
656 char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */
657
658 fseek(env_file, 0, SEEK_SET);
659 while (!feof(env_file)) {
660 char * p = fgets(buffer, MAXPATHLEN*2, env_file);
Victor Stinner9bee3292017-12-21 16:49:13 +0100661
662 if (p == NULL) {
663 break;
664 }
Victor Stinner05d68a82018-01-18 11:15:25 +0100665
666 size_t n = strlen(p);
Victor Stinner9bee3292017-12-21 16:49:13 +0100667 if (p[n - 1] != '\n') {
668 /* line has overflowed - bail */
669 break;
670 }
671 if (p[0] == '#') {
672 /* Comment - skip */
673 continue;
674 }
Victor Stinner05d68a82018-01-18 11:15:25 +0100675
676 wchar_t *tmpbuffer = _Py_DecodeUTF8_surrogateescape(buffer, n);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100677 if (tmpbuffer) {
Victor Stinner9bee3292017-12-21 16:49:13 +0100678 wchar_t * state;
679 wchar_t * tok = wcstok(tmpbuffer, L" \t\r\n", &state);
680 if ((tok != NULL) && !wcscmp(tok, key)) {
681 tok = wcstok(NULL, L" \t", &state);
682 if ((tok != NULL) && !wcscmp(tok, L"=")) {
683 tok = wcstok(NULL, L"\r\n", &state);
684 if (tok != NULL) {
685 wcsncpy(value, tok, MAXPATHLEN);
686 result = 1;
687 PyMem_RawFree(tmpbuffer);
688 break;
689 }
690 }
691 }
692 PyMem_RawFree(tmpbuffer);
693 }
694 }
695 return result;
696}
697
Victor Stinner31a83932017-12-04 13:39:15 +0100698#ifdef __cplusplus
699}
700#endif