Bug Summary

File:./Modules/posixmodule.c
Location:line 4715, column 9
Description:Both operands to '!=' always have the same value

Annotated Source Code

1
2/* POSIX module implementation */
3
4/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
7 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
8 of the compiler used. Different compilers define their own feature
9 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
13
14/* See also ../Dos/dosmodule.c */
15
16#ifdef __APPLE__1
17 /*
18 * Step 1 of support for weak-linking a number of symbols existing on
19 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
28#define PY_SSIZE_T_CLEAN
29
30#include "Python.h"
31
32#if defined(__VMS)
33# include <unixio.h>
34#endif /* defined(__VMS) */
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40PyDoc_STRVAR(posix__doc__,static char posix__doc__[] = "This module provides access to operating system functionality that is\nstandardized by the C Standard and the POSIX standard (a thinly\ndisguised Unix interface). Refer to the library manual and\ncorresponding Unix manual entries for more information on calls."
41"This module provides access to operating system functionality that is\n\static char posix__doc__[] = "This module provides access to operating system functionality that is\nstandardized by the C Standard and the POSIX standard (a thinly\ndisguised Unix interface). Refer to the library manual and\ncorresponding Unix manual entries for more information on calls."
42standardized by the C Standard and the POSIX standard (a thinly\n\static char posix__doc__[] = "This module provides access to operating system functionality that is\nstandardized by the C Standard and the POSIX standard (a thinly\ndisguised Unix interface). Refer to the library manual and\ncorresponding Unix manual entries for more information on calls."
43disguised Unix interface). Refer to the library manual and\n\static char posix__doc__[] = "This module provides access to operating system functionality that is\nstandardized by the C Standard and the POSIX standard (a thinly\ndisguised Unix interface). Refer to the library manual and\ncorresponding Unix manual entries for more information on calls."
44corresponding Unix manual entries for more information on calls.")static char posix__doc__[] = "This module provides access to operating system functionality that is\nstandardized by the C Standard and the POSIX standard (a thinly\ndisguised Unix interface). Refer to the library manual and\ncorresponding Unix manual entries for more information on calls.";
45
46
47#if defined(PYOS_OS2)
48#define INCL_DOS
49#define INCL_DOSERRORS
50#define INCL_DOSPROCESS
51#define INCL_NOPMAPI
52#include <os2.h>
53#if defined(PYCC_GCC)
54#include <ctype.h>
55#include <io.h>
56#include <stdio.h>
57#include <process.h>
58#endif
59#include "osdefs.h"
60#endif
61
62#ifdef HAVE_SYS_TYPES_H1
63#include <sys/types.h>
64#endif /* HAVE_SYS_TYPES_H */
65
66#ifdef HAVE_SYS_STAT_H1
67#include <sys/stat.h>
68#endif /* HAVE_SYS_STAT_H */
69
70#ifdef HAVE_SYS_WAIT_H1
71#include <sys/wait.h> /* For WNOHANG */
72#endif
73
74#ifdef HAVE_SIGNAL_H1
75#include <signal.h>
76#endif
77
78#ifdef HAVE_FCNTL_H1
79#include <fcntl.h>
80#endif /* HAVE_FCNTL_H */
81
82#ifdef HAVE_GRP_H1
83#include <grp.h>
84#endif
85
86#ifdef HAVE_SYSEXITS_H1
87#include <sysexits.h>
88#endif /* HAVE_SYSEXITS_H */
89
90#ifdef HAVE_SYS_LOADAVG_H
91#include <sys/loadavg.h>
92#endif
93
94#ifdef HAVE_LANGINFO_H1
95#include <langinfo.h>
96#endif
97
98/* Various compilers have only certain posix functions */
99/* XXX Gosh I wish these were all moved into pyconfig.h */
100#if defined(PYCC_VACPP) && defined(PYOS_OS2)
101#include <process.h>
102#else
103#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
104#define HAVE_GETCWD1 1
105#define HAVE_OPENDIR1 1
106#define HAVE_SYSTEM1 1
107#if defined(__OS2__)
108#define HAVE_EXECV1 1
109#define HAVE_WAIT1 1
110#endif
111#include <process.h>
112#else
113#ifdef __BORLANDC__ /* Borland compiler */
114#define HAVE_EXECV1 1
115#define HAVE_GETCWD1 1
116#define HAVE_OPENDIR1 1
117#define HAVE_PIPE1 1
118#define HAVE_SYSTEM1 1
119#define HAVE_WAIT1 1
120#else
121#ifdef _MSC_VER /* Microsoft compiler */
122#define HAVE_GETCWD1 1
123#define HAVE_GETPPID1 1
124#define HAVE_GETLOGIN1 1
125#define HAVE_SPAWNV 1
126#define HAVE_EXECV1 1
127#define HAVE_PIPE1 1
128#define HAVE_SYSTEM1 1
129#define HAVE_CWAIT 1
130#define HAVE_FSYNC1 1
131#define fsync _commit
132#else
133#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
134/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
135#else /* all other compilers */
136/* Unix functions that the configure script doesn't check for */
137#define HAVE_EXECV1 1
138#define HAVE_FORK1 1
139#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
140#define HAVE_FORK1 1
141#endif
142#define HAVE_GETCWD1 1
143#define HAVE_GETEGID1 1
144#define HAVE_GETEUID1 1
145#define HAVE_GETGID1 1
146#define HAVE_GETPPID1 1
147#define HAVE_GETUID1 1
148#define HAVE_KILL1 1
149#define HAVE_OPENDIR1 1
150#define HAVE_PIPE1 1
151#define HAVE_SYSTEM1 1
152#define HAVE_WAIT1 1
153#define HAVE_TTYNAME1 1
154#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
155#endif /* _MSC_VER */
156#endif /* __BORLANDC__ */
157#endif /* ! __WATCOMC__ || __QNX__ */
158#endif /* ! __IBMC__ */
159
160#ifndef _MSC_VER
161
162#if defined(__sgi)&&_COMPILER_VERSION>=700
163/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
164 (default) */
165extern char *ctermid_r(char *);
166#endif
167
168#ifndef HAVE_UNISTD_H1
169#if defined(PYCC_VACPP)
170extern int mkdir(char *);
171#else
172#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
173extern int mkdir(const char *);
174#else
175extern int mkdir(const char *, mode_t);
176#endif
177#endif
178#if defined(__IBMC__) || defined(__IBMCPP__)
179extern int chdir(char *);
180extern int rmdir(char *);
181#else
182extern int chdir(const char *);
183extern int rmdir(const char *);
184#endif
185#ifdef __BORLANDC__
186extern int chmod(const char *, int);
187#else
188extern int chmod(const char *, mode_t);
189#endif
190/*#ifdef HAVE_FCHMOD
191extern int fchmod(int, mode_t);
192#endif*/
193/*#ifdef HAVE_LCHMOD
194extern int lchmod(const char *, mode_t);
195#endif*/
196extern int chown(const char *, uid_t, gid_t);
197extern char *getcwd(char *, int);
198extern char *strerror(int);
199extern int link(const char *, const char *);
200extern int rename(const char *, const char *);
201extern int stat(const char *, struct stat *);
202extern int unlink(const char *);
203#ifdef HAVE_SYMLINK1
204extern int symlink(const char *, const char *);
205#endif /* HAVE_SYMLINK */
206#ifdef HAVE_LSTAT1
207extern int lstat(const char *, struct stat *);
208#endif /* HAVE_LSTAT */
209#endif /* !HAVE_UNISTD_H */
210
211#endif /* !_MSC_VER */
212
213#ifdef HAVE_UTIME_H1
214#include <utime.h>
215#endif /* HAVE_UTIME_H */
216
217#ifdef HAVE_SYS_UTIME_H
218#include <sys/utime.h>
219#define HAVE_UTIME_H1 /* pretend we do for the rest of this file */
220#endif /* HAVE_SYS_UTIME_H */
221
222#ifdef HAVE_SYS_TIMES_H1
223#include <sys/times.h>
224#endif /* HAVE_SYS_TIMES_H */
225
226#ifdef HAVE_SYS_PARAM_H1
227#include <sys/param.h>
228#endif /* HAVE_SYS_PARAM_H */
229
230#ifdef HAVE_SYS_UTSNAME_H1
231#include <sys/utsname.h>
232#endif /* HAVE_SYS_UTSNAME_H */
233
234#ifdef HAVE_DIRENT_H1
235#include <dirent.h>
236#define NAMLEN(dirent)strlen((dirent)->d_name) strlen((dirent)->d_name)
237#else
238#if defined(__WATCOMC__) && !defined(__QNX__)
239#include <direct.h>
240#define NAMLEN(dirent)strlen((dirent)->d_name) strlen((dirent)->d_name)
241#else
242#define dirent direct
243#define NAMLEN(dirent)strlen((dirent)->d_name) (dirent)->d_namlen
244#endif
245#ifdef HAVE_SYS_NDIR_H
246#include <sys/ndir.h>
247#endif
248#ifdef HAVE_SYS_DIR_H
249#include <sys/dir.h>
250#endif
251#ifdef HAVE_NDIR_H
252#include <ndir.h>
253#endif
254#endif
255
256#ifdef _MSC_VER
257#ifdef HAVE_DIRECT_H
258#include <direct.h>
259#endif
260#ifdef HAVE_IO_H
261#include <io.h>
262#endif
263#ifdef HAVE_PROCESS_H
264#include <process.h>
265#endif
266#ifndef VOLUME_NAME_DOS
267#define VOLUME_NAME_DOS 0x0
268#endif
269#ifndef VOLUME_NAME_NT
270#define VOLUME_NAME_NT 0x2
271#endif
272#ifndef IO_REPARSE_TAG_SYMLINK
273#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
274#endif
275#include "osdefs.h"
276#include <malloc.h>
277#include <windows.h>
278#include <shellapi.h> /* for ShellExecute() */
279#include <lmcons.h> /* for UNLEN */
280#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
281#define HAVE_SYMLINK1
282static int win32_can_symlink = 0;
283#endif
284#endif /* _MSC_VER */
285
286#if defined(PYCC_VACPP) && defined(PYOS_OS2)
287#include <io.h>
288#endif /* OS2 */
289
290#ifndef MAXPATHLEN1024
291#if defined(PATH_MAX1024) && PATH_MAX1024 > 1024
292#define MAXPATHLEN1024 PATH_MAX1024
293#else
294#define MAXPATHLEN1024 1024
295#endif
296#endif /* MAXPATHLEN */
297
298#ifdef UNION_WAIT
299/* Emulate some macros on systems that have a union instead of macros */
300
301#ifndef WIFEXITED
302#define WIFEXITED(u_wait)(((*(int *)&(u_wait)) & 0177) == 0) (!(u_wait).w_termsigw_T.w_Termsig && !(u_wait).w_coredumpw_T.w_Coredump)
303#endif
304
305#ifndef WEXITSTATUS
306#define WEXITSTATUS(u_wait)(((*(int *)&(u_wait)) >> 8) & 0x000000ff) (WIFEXITED(u_wait)(((*(int *)&(u_wait)) & 0177) == 0)?((u_wait).w_retcodew_T.w_Retcode):-1)
307#endif
308
309#ifndef WTERMSIG
310#define WTERMSIG(u_wait)(((*(int *)&(u_wait)) & 0177)) ((u_wait).w_termsigw_T.w_Termsig)
311#endif
312
313#define WAIT_TYPEint union wait
314#define WAIT_STATUS_INT(s)(s) (s.w_status)
315
316#else /* !UNION_WAIT */
317#define WAIT_TYPEint int
318#define WAIT_STATUS_INT(s)(s) (s)
319#endif /* UNION_WAIT */
320
321/* Don't use the "_r" form if we don't need it (also, won't have a
322 prototype for it, at least on Solaris -- maybe others as well?). */
323#if defined(HAVE_CTERMID_R1) && defined(WITH_THREAD1)
324#define USE_CTERMID_R
325#endif
326
327/* choose the appropriate stat and fstat functions and return structs */
328#undef STATstat
329#undef FSTATfstat
330#undef STRUCT_STATstruct stat
331#if defined(MS_WIN64) || defined(MS_WINDOWS)
332# define STATstat win32_stat
333# define FSTATfstat win32_fstat
334# define STRUCT_STATstruct stat struct win32_stat
335#else
336# define STATstat stat
337# define FSTATfstat fstat
338# define STRUCT_STATstruct stat struct stat
339#endif
340
341#if defined(MAJOR_IN_MKDEV)
342#include <sys/mkdev.h>
343#else
344#if defined(MAJOR_IN_SYSMACROS)
345#include <sys/sysmacros.h>
346#endif
347#if defined(HAVE_MKNOD1) && defined(HAVE_SYS_MKDEV_H)
348#include <sys/mkdev.h>
349#endif
350#endif
351
352#if defined _MSC_VER && _MSC_VER >= 1400
353/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
354 * valid and throw an assertion if it isn't.
355 * Normally, an invalid fd is likely to be a C program error and therefore
356 * an assertion can be useful, but it does contradict the POSIX standard
357 * which for write(2) states:
358 * "Otherwise, -1 shall be returned and errno set to indicate the error."
359 * "[EBADF] The fildes argument is not a valid file descriptor open for
360 * writing."
361 * Furthermore, python allows the user to enter any old integer
362 * as a fd and should merely raise a python exception on error.
363 * The Microsoft CRT doesn't provide an official way to check for the
364 * validity of a file descriptor, but we can emulate its internal behaviour
365 * by using the exported __pinfo data member and knowledge of the
366 * internal structures involved.
367 * The structures below must be updated for each version of visual studio
368 * according to the file internal.h in the CRT source, until MS comes
369 * up with a less hacky way to do this.
370 * (all of this is to avoid globally modifying the CRT behaviour using
371 * _set_invalid_parameter_handler() and _CrtSetReportMode())
372 */
373/* The actual size of the structure is determined at runtime.
374 * Only the first items must be present.
375 */
376typedef struct {
377 intptr_t osfhnd;
378 char osfile;
379} my_ioinfo;
380
381extern __declspec(dllimport) char * __pioinfo[];
382#define IOINFO_L2E 5
383#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
384#define IOINFO_ARRAYS 64
385#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
386#define FOPEN 0x01
387#define _NO_CONSOLE_FILENO (intptr_t)-2
388
389/* This function emulates what the windows CRT does to validate file handles */
390int
391_PyVerify_fd(int fd)(1)
392{
393 const int i1 = fd >> IOINFO_L2E;
394 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
395
396 static size_t sizeof_ioinfo = 0;
397
398 /* Determine the actual size of the ioinfo structure,
399 * as used by the CRT loaded in memory
400 */
401 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL((void *)0)) {
402 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
403 }
404 if (sizeof_ioinfo == 0) {
405 /* This should not happen... */
406 goto fail;
407 }
408
409 /* See that it isn't a special CLEAR fileno */
410 if (fd != _NO_CONSOLE_FILENO) {
411 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
412 * we check pointer validity and other info
413 */
414 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL((void *)0)) {
415 /* finally, check that the file is open */
416 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
417 if (info->osfile & FOPEN) {
418 return 1;
419 }
420 }
421 }
422 fail:
423 errno(*__error()) = EBADF9;
424 return 0;
425}
426
427/* the special case of checking dup2. The target fd must be in a sensible range */
428static int
429_PyVerify_fd_dup2(int fd1, int fd2)(1)
430{
431 if (!_PyVerify_fd(fd1)(1))
432 return 0;
433 if (fd2 == _NO_CONSOLE_FILENO)
434 return 0;
435 if ((unsigned)fd2 < _NHANDLE_)
436 return 1;
437 else
438 return 0;
439}
440#else
441/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
442#define _PyVerify_fd_dup2(A, B)(1) (1)
443#endif
444
445#ifdef MS_WINDOWS
446/* The following structure was copied from
447 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
448 include doesn't seem to be present in the Windows SDK (at least as included
449 with Visual Studio Express). */
450typedef struct _REPARSE_DATA_BUFFER {
451 ULONG ReparseTag;
452 USHORT ReparseDataLength;
453 USHORT Reserved;
454 union {
455 struct {
456 USHORT SubstituteNameOffset;
457 USHORT SubstituteNameLength;
458 USHORT PrintNameOffset;
459 USHORT PrintNameLength;
460 ULONG Flags;
461 WCHAR PathBuffer[1];
462 } SymbolicLinkReparseBuffer;
463
464 struct {
465 USHORT SubstituteNameOffset;
466 USHORT SubstituteNameLength;
467 USHORT PrintNameOffset;
468 USHORT PrintNameLength;
469 WCHAR PathBuffer[1];
470 } MountPointReparseBuffer;
471
472 struct {
473 UCHAR DataBuffer[1];
474 } GenericReparseBuffer;
475 };
476} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
477
478#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
479 GenericReparseBuffer)
480#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
481
482static int
483win32_read_link(HANDLE reparse_point_handle, ULONG *reparse_tag, wchar_t **target_path)
484{
485 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
486 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
487 DWORD n_bytes_returned;
488 const wchar_t *ptr;
489 wchar_t *buf;
490 size_t len;
491
492 if (0 == DeviceIoControl(
493 reparse_point_handle,
494 FSCTL_GET_REPARSE_POINT,
495 NULL((void *)0), 0, /* in buffer */
496 target_buffer, sizeof(target_buffer),
497 &n_bytes_returned,
498 NULL((void *)0))) /* we're not using OVERLAPPED_IO */
499 return -1;
500
501 if (reparse_tag)
502 *reparse_tag = rdb->ReparseTag;
503
504 if (target_path) {
505 switch (rdb->ReparseTag) {
506 case IO_REPARSE_TAG_SYMLINK:
507 /* XXX: Maybe should use SubstituteName? */
508 ptr = rdb->SymbolicLinkReparseBuffer.PathBuffer +
509 rdb->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR);
510 len = rdb->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR);
511 break;
512 case IO_REPARSE_TAG_MOUNT_POINT:
513 ptr = rdb->MountPointReparseBuffer.PathBuffer +
514 rdb->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR);
515 len = rdb->MountPointReparseBuffer.SubstituteNameLength/sizeof(WCHAR);
516 break;
517 default:
518 SetLastError(ERROR_REPARSE_TAG_MISMATCH); /* XXX: Proper error code? */
519 return -1;
520 }
521 buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
522 if (!buf) {
523 SetLastError(ERROR_OUTOFMEMORY);
524 return -1;
525 }
526 wcsncpy(buf, ptr, len);
527 buf[len] = L'\0';
528 if (wcsncmp(buf, L"\\??\\", 4) == 0)
529 buf[1] = L'\\';
530 *target_path = buf;
531 }
532
533 return 0;
534}
535#endif /* MS_WINDOWS */
536
537/* Return a dictionary corresponding to the POSIX environment table */
538#ifdef WITH_NEXT_FRAMEWORK
539/* On Darwin/MacOSX a shared library or framework has no access to
540** environ directly, we must obtain it with _NSGetEnviron().
541*/
542#include <crt_externs.h>
543static char **environ;
544#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
545extern char **environ;
546#endif /* !_MSC_VER */
547
548static PyObject *
549convertenviron(void)
550{
551 PyObject *d;
552#ifdef MS_WINDOWS
553 wchar_t **e;
554#else
555 char **e;
556#endif
557#if defined(PYOS_OS2)
558 APIRET rc;
559 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
560#endif
561
562 d = PyDict_New();
563 if (d == NULL((void *)0))
564 return NULL((void *)0);
565#ifdef WITH_NEXT_FRAMEWORK
566 if (environ == NULL((void *)0))
567 environ = *_NSGetEnviron();
568#endif
569#ifdef MS_WINDOWS
570 /* _wenviron must be initialized in this way if the program is started
571 through main() instead of wmain(). */
572 _wgetenv(L"");
573 if (_wenviron == NULL((void *)0))
574 return d;
575 /* This part ignores errors */
576 for (e = _wenviron; *e != NULL((void *)0); e++) {
577 PyObject *k;
578 PyObject *v;
579 wchar_t *p = wcschr(*e, L'=');
580 if (p == NULL((void *)0))
581 continue;
582 k = PyUnicode_FromWideCharPyUnicodeUCS2_FromWideChar(*e, (Py_ssize_t)(p-*e));
583 if (k == NULL((void *)0)) {
584 PyErr_Clear();
585 continue;
586 }
587 v = PyUnicode_FromWideCharPyUnicodeUCS2_FromWideChar(p+1, wcslen(p+1));
588 if (v == NULL((void *)0)) {
589 PyErr_Clear();
590 Py_DECREF(k)do { if (_Py_RefTotal-- , --((PyObject*)(k))->ob_refcnt !=
0) { if (((PyObject*)k)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 590, (PyObject *)(k)); } else _Py_Dealloc
((PyObject *)(k)); } while (0)
;
591 continue;
592 }
593 if (PyDict_GetItem(d, k) == NULL((void *)0)) {
594 if (PyDict_SetItem(d, k, v) != 0)
595 PyErr_Clear();
596 }
597 Py_DECREF(k)do { if (_Py_RefTotal-- , --((PyObject*)(k))->ob_refcnt !=
0) { if (((PyObject*)k)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 597, (PyObject *)(k)); } else _Py_Dealloc
((PyObject *)(k)); } while (0)
;
598 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 598, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
599 }
600#else
601 if (environ == NULL((void *)0))
602 return d;
603 /* This part ignores errors */
604 for (e = environ; *e != NULL((void *)0); e++) {
605 PyObject *k;
606 PyObject *v;
607 char *p = strchr(*e, '=');
608 if (p == NULL((void *)0))
609 continue;
610 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
611 if (k == NULL((void *)0)) {
612 PyErr_Clear();
613 continue;
614 }
615 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
616 if (v == NULL((void *)0)) {
617 PyErr_Clear();
618 Py_DECREF(k)do { if (_Py_RefTotal-- , --((PyObject*)(k))->ob_refcnt !=
0) { if (((PyObject*)k)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 618, (PyObject *)(k)); } else _Py_Dealloc
((PyObject *)(k)); } while (0)
;
619 continue;
620 }
621 if (PyDict_GetItem(d, k) == NULL((void *)0)) {
622 if (PyDict_SetItem(d, k, v) != 0)
623 PyErr_Clear();
624 }
625 Py_DECREF(k)do { if (_Py_RefTotal-- , --((PyObject*)(k))->ob_refcnt !=
0) { if (((PyObject*)k)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 625, (PyObject *)(k)); } else _Py_Dealloc
((PyObject *)(k)); } while (0)
;
626 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 626, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
627 }
628#endif
629#if defined(PYOS_OS2)
630 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
631 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
632 PyObject *v = PyBytes_FromString(buffer);
633 PyDict_SetItemString(d, "BEGINLIBPATH", v);
634 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 634, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
635 }
636 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
637 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
638 PyObject *v = PyBytes_FromString(buffer);
639 PyDict_SetItemString(d, "ENDLIBPATH", v);
640 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 640, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
641 }
642#endif
643 return d;
644}
645
646/* Set a POSIX-specific error from errno, and return NULL */
647
648static PyObject *
649posix_error(void)
650{
651 return PyErr_SetFromErrno(PyExc_OSError);
652}
653static PyObject *
654posix_error_with_filename(char* name)
655{
656 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
657}
658
659
660static PyObject *
661posix_error_with_allocated_filename(PyObject* name)
662{
663 PyObject *name_str, *rc;
664 name_str = PyUnicode_DecodeFSDefaultAndSizePyUnicodeUCS2_DecodeFSDefaultAndSize(PyBytes_AsString(name),
665 PyBytes_GET_SIZE(name)((__builtin_expect(!(((((((PyObject*)(name))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 665, "PyBytes_Check(name)") : (void
)0),(((PyVarObject*)(name))->ob_size))
);
666 Py_DECREF(name)do { if (_Py_RefTotal-- , --((PyObject*)(name))->ob_refcnt
!= 0) { if (((PyObject*)name)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 666, (PyObject *)(name)); } else _Py_Dealloc
((PyObject *)(name)); } while (0)
;
667 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
668 name_str);
669 Py_XDECREF(name_str)do { if ((name_str) == ((void *)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(name_str))->ob_refcnt != 0) { if (((PyObject
*)name_str)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/posixmodule.c"
, 669, (PyObject *)(name_str)); } else _Py_Dealloc((PyObject *
)(name_str)); } while (0); } while (0)
;
670 return rc;
671}
672
673#ifdef MS_WINDOWS
674static PyObject *
675win32_error(char* function, const char* filename)
676{
677 /* XXX We should pass the function name along in the future.
678 (winreg.c also wants to pass the function name.)
679 This would however require an additional param to the
680 Windows error object, which is non-trivial.
681 */
682 errno(*__error()) = GetLastError();
683 if (filename)
684 return PyErr_SetFromWindowsErrWithFilename(errno(*__error()), filename);
685 else
686 return PyErr_SetFromWindowsErr(errno(*__error()));
687}
688
689static PyObject *
690win32_error_unicode(char* function, Py_UNICODE* filename)
691{
692 /* XXX - see win32_error for comments on 'function' */
693 errno(*__error()) = GetLastError();
694 if (filename)
695 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno(*__error()), filename);
696 else
697 return PyErr_SetFromWindowsErr(errno(*__error()));
698}
699
700static int
701convert_to_unicode(PyObject **param)
702{
703 if (PyUnicode_CheckExact(*param)((((PyObject*)(*param))->ob_type) == &PyUnicode_Type))
704 Py_INCREF(*param)( _Py_RefTotal++ , ((PyObject*)(*param))->ob_refcnt++);
705 else if (PyUnicode_Check(*param)((((((PyObject*)(*param))->ob_type))->tp_flags & ((
1L<<28))) != 0)
)
706 /* For a Unicode subtype that's not a Unicode object,
707 return a true Unicode object with the same data. */
708 *param = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(PyUnicode_AS_UNICODE(*param)((__builtin_expect(!(((((((PyObject*)(*param))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 708, "PyUnicode_Check(*param)") :
(void)0),(((PyUnicodeObject *)(*param))->str))
,
709 PyUnicode_GET_SIZE(*param)((__builtin_expect(!(((((((PyObject*)(*param))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 709, "PyUnicode_Check(*param)") :
(void)0),(((PyUnicodeObject *)(*param))->length))
);
710 else
711 *param = PyUnicode_FromEncodedObjectPyUnicodeUCS2_FromEncodedObject(*param,
712 Py_FileSystemDefaultEncoding,
713 "strict");
714 return (*param) != NULL((void *)0);
715}
716
717#endif /* MS_WINDOWS */
718
719#if defined(PYOS_OS2)
720/**********************************************************************
721 * Helper Function to Trim and Format OS/2 Messages
722 **********************************************************************/
723static void
724os2_formatmsg(char *msgbuf, int msglen, char *reason)
725{
726 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
727
728 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
729 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
730
731 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))iswspace(btowc(((unsigned char)((*lastc) & 0xff)))))
732 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
733 }
734
735 /* Add Optional Reason Text */
736 if (reason) {
737 strcat(msgbuf, " : ")((__builtin_object_size (msgbuf, 0) != (size_t) -1) ? __builtin___strcat_chk
(msgbuf, " : ", __builtin_object_size (msgbuf, 2 > 1)) : __inline_strcat_chk
(msgbuf, " : "))
;
738 strcat(msgbuf, reason)((__builtin_object_size (msgbuf, 0) != (size_t) -1) ? __builtin___strcat_chk
(msgbuf, reason, __builtin_object_size (msgbuf, 2 > 1)) :
__inline_strcat_chk (msgbuf, reason))
;
739 }
740}
741
742/**********************************************************************
743 * Decode an OS/2 Operating System Error Code
744 *
745 * A convenience function to lookup an OS/2 error code and return a
746 * text message we can use to raise a Python exception.
747 *
748 * Notes:
749 * The messages for errors returned from the OS/2 kernel reside in
750 * the file OSO001.MSG in the \OS2 directory hierarchy.
751 *
752 **********************************************************************/
753static char *
754os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
755{
756 APIRET rc;
757 ULONG msglen;
758
759 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
760 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
761 rc = DosGetMessage(NULL((void *)0), 0, msgbuf, msgbuflen,
762 errorcode, "oso001.msg", &msglen);
763 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
764
765 if (rc == NO_ERROR)
766 os2_formatmsg(msgbuf, msglen, reason);
767 else
768 PyOS_snprintf(msgbuf, msgbuflen,
769 "unknown OS error #%d", errorcode);
770
771 return msgbuf;
772}
773
774/* Set an OS/2-specific error and return NULL. OS/2 kernel
775 errors are not in a global variable e.g. 'errno' nor are
776 they congruent with posix error numbers. */
777
778static PyObject *
779os2_error(int code)
780{
781 char text[1024];
782 PyObject *v;
783
784 os2_strerror(text, sizeof(text), code, "");
785
786 v = Py_BuildValue_Py_BuildValue_SizeT("(is)", code, text);
787 if (v != NULL((void *)0)) {
788 PyErr_SetObject(PyExc_OSError, v);
789 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 789, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
790 }
791 return NULL((void *)0); /* Signal to Python that an Exception is Pending */
792}
793
794#endif /* OS2 */
795
796/* POSIX generic methods */
797
798static PyObject *
799posix_fildes(PyObject *fdobj, int (*func)(int))
800{
801 int fd;
802 int res;
803 fd = PyObject_AsFileDescriptor(fdobj);
804 if (fd < 0)
805 return NULL((void *)0);
806 if (!_PyVerify_fd(fd)(1))
807 return posix_error();
808 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
809 res = (*func)(fd);
810 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
811 if (res < 0)
812 return posix_error();
813 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
814 return Py_None(&_Py_NoneStruct);
815}
816
817static PyObject *
818posix_1str(PyObject *args, char *format, int (*func)(const char*))
819{
820 PyObject *opath1 = NULL((void *)0);
821 char *path1;
822 int res;
823 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, format,
824 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath1))
825 return NULL((void *)0);
826 path1 = PyBytes_AsString(opath1);
827 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
828 res = (*func)(path1);
829 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
830 if (res < 0)
831 return posix_error_with_allocated_filename(opath1);
832 Py_DECREF(opath1)do { if (_Py_RefTotal-- , --((PyObject*)(opath1))->ob_refcnt
!= 0) { if (((PyObject*)opath1)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 832, (PyObject *)(opath1)); } else
_Py_Dealloc((PyObject *)(opath1)); } while (0)
;
833 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
834 return Py_None(&_Py_NoneStruct);
835}
836
837static PyObject *
838posix_2str(PyObject *args,
839 char *format,
840 int (*func)(const char *, const char *))
841{
842 PyObject *opath1 = NULL((void *)0), *opath2 = NULL((void *)0);
843 char *path1, *path2;
844 int res;
845 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, format,
846 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath1,
847 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath2)) {
848 return NULL((void *)0);
849 }
850 path1 = PyBytes_AsString(opath1);
851 path2 = PyBytes_AsString(opath2);
852 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
853 res = (*func)(path1, path2);
854 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
855 Py_DECREF(opath1)do { if (_Py_RefTotal-- , --((PyObject*)(opath1))->ob_refcnt
!= 0) { if (((PyObject*)opath1)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 855, (PyObject *)(opath1)); } else
_Py_Dealloc((PyObject *)(opath1)); } while (0)
;
856 Py_DECREF(opath2)do { if (_Py_RefTotal-- , --((PyObject*)(opath2))->ob_refcnt
!= 0) { if (((PyObject*)opath2)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 856, (PyObject *)(opath2)); } else
_Py_Dealloc((PyObject *)(opath2)); } while (0)
;
857 if (res != 0)
858 /* XXX how to report both path1 and path2??? */
859 return posix_error();
860 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
861 return Py_None(&_Py_NoneStruct);
862}
863
864#ifdef MS_WINDOWS
865static PyObject*
866win32_1str(PyObject* args, char* func,
867 char* format, BOOL (__stdcall *funcA)(LPCSTR),
868 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
869{
870 PyObject *uni;
871 char *ansi;
872 BOOL result;
873
874 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, wformat, &uni))
875 PyErr_Clear();
876 else {
877 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
878 result = funcW(PyUnicode_AsUnicodePyUnicodeUCS2_AsUnicode(uni));
879 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
880 if (!result)
881 return win32_error_unicode(func, PyUnicode_AsUnicodePyUnicodeUCS2_AsUnicode(uni));
882 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
883 return Py_None(&_Py_NoneStruct);
884 }
885 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, format, &ansi))
886 return NULL((void *)0);
887 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
888 result = funcA(ansi);
889 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
890 if (!result)
891 return win32_error(func, ansi);
892 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
893 return Py_None(&_Py_NoneStruct);
894
895}
896
897/* This is a reimplementation of the C library's chdir function,
898 but one that produces Win32 errors instead of DOS error codes.
899 chdir is essentially a wrapper around SetCurrentDirectory; however,
900 it also needs to set "magic" environment variables indicating
901 the per-drive current directory, which are of the form =<drive>: */
902static BOOL __stdcall
903win32_chdir(LPCSTR path)
904{
905 char new_path[MAX_PATH+1];
906 int result;
907 char env[4] = "=x:";
908
909 if(!SetCurrentDirectoryA(path))
910 return FALSE;
911 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
912 if (!result)
913 return FALSE;
914 /* In the ANSI API, there should not be any paths longer
915 than MAX_PATH. */
916 assert(result <= MAX_PATH+1)(__builtin_expect(!(result <= MAX_PATH+1), 0) ? __assert_rtn
(__func__, "./Modules/posixmodule.c", 916, "result <= MAX_PATH+1"
) : (void)0)
;
917 if (strncmp(new_path, "\\\\", 2) == 0 ||
918 strncmp(new_path, "//", 2) == 0)
919 /* UNC path, nothing to do. */
920 return TRUE;
921 env[1] = new_path[0];
922 return SetEnvironmentVariableA(env, new_path);
923}
924
925/* The Unicode version differs from the ANSI version
926 since the current directory might exceed MAX_PATH characters */
927static BOOL __stdcall
928win32_wchdir(LPCWSTR path)
929{
930 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
931 int result;
932 wchar_t env[4] = L"=x:";
933
934 if(!SetCurrentDirectoryW(path))
935 return FALSE;
936 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
937 if (!result)
938 return FALSE;
939 if (result > MAX_PATH+1) {
940 new_path = malloc(result * sizeof(wchar_t));
941 if (!new_path) {
942 SetLastError(ERROR_OUTOFMEMORY);
943 return FALSE;
944 }
945 result = GetCurrentDirectoryW(result, new_path);
946 if (!result) {
947 free(new_path);
948 return FALSE;
949 }
950 }
951 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
952 wcsncmp(new_path, L"//", 2) == 0)
953 /* UNC path, nothing to do. */
954 return TRUE;
955 env[1] = new_path[0];
956 result = SetEnvironmentVariableW(env, new_path);
957 if (new_path != _new_path)
958 free(new_path);
959 return result;
960}
961#endif
962
963#ifdef MS_WINDOWS
964/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
965 - time stamps are restricted to second resolution
966 - file modification times suffer from forth-and-back conversions between
967 UTC and local time
968 Therefore, we implement our own stat, based on the Win32 API directly.
969*/
970#define HAVE_STAT_NSEC 1
971
972struct win32_stat{
973 int st_dev;
974 __int64 st_ino;
975 unsigned short st_mode;
976 int st_nlink;
977 int st_uid;
978 int st_gid;
979 int st_rdev;
980 __int64 st_size;
981 time_t st_atimest_atimespec.tv_sec;
982 int st_atime_nsec;
983 time_t st_mtimest_mtimespec.tv_sec;
984 int st_mtime_nsec;
985 time_t st_ctimest_ctimespec.tv_sec;
986 int st_ctime_nsec;
987};
988
989static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
990
991static void
992FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
993{
994 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
995 /* Cannot simply cast and dereference in_ptr,
996 since it might not be aligned properly */
997 __int64 in;
998 memcpy(&in, in_ptr, sizeof(in))((__builtin_object_size (&in, 0) != (size_t) -1) ? __builtin___memcpy_chk
(&in, in_ptr, sizeof(in), __builtin_object_size (&in
, 0)) : __inline_memcpy_chk (&in, in_ptr, sizeof(in)))
;
999 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
1000 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t)((__builtin_expect(!((__int64)(time_t)((in / 10000000) - secs_between_epochs
) == ((in / 10000000) - secs_between_epochs)), 0) ? __assert_rtn
(__func__, "./Modules/posixmodule.c", 1000, "(__int64)(time_t)((in / 10000000) - secs_between_epochs) == ((in / 10000000) - secs_between_epochs)"
) : (void)0), (time_t)((in / 10000000) - secs_between_epochs)
)
;
1001}
1002
1003static void
1004time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
1005{
1006 /* XXX endianness */
1007 __int64 out;
1008 out = time_in + secs_between_epochs;
1009 out = out * 10000000 + nsec_in / 100;
1010 memcpy(out_ptr, &out, sizeof(out))((__builtin_object_size (out_ptr, 0) != (size_t) -1) ? __builtin___memcpy_chk
(out_ptr, &out, sizeof(out), __builtin_object_size (out_ptr
, 0)) : __inline_memcpy_chk (out_ptr, &out, sizeof(out)))
;
1011}
1012
1013/* Below, we *know* that ugo+r is 0444 */
1014#if _S_IREAD != 0400
1015#error Unsupported C library
1016#endif
1017static int
1018attributes_to_mode(DWORD attr)
1019{
1020 int m = 0;
1021 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1022 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1023 else
1024 m |= _S_IFREG;
1025 if (attr & FILE_ATTRIBUTE_READONLY)
1026 m |= 0444;
1027 else
1028 m |= 0666;
1029 return m;
1030}
1031
1032static int
1033attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
1034{
1035 memset(result, 0, sizeof(*result))((__builtin_object_size (result, 0) != (size_t) -1) ? __builtin___memset_chk
(result, 0, sizeof(*result), __builtin_object_size (result, 0
)) : __inline_memset_chk (result, 0, sizeof(*result)))
;
1036 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1037 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1038 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctimest_ctimespec.tv_sec, &result->st_ctime_nsec);
1039 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtimest_mtimespec.tv_sec, &result->st_mtime_nsec);
1040 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atimest_atimespec.tv_sec, &result->st_atime_nsec);
1041 result->st_nlink = info->nNumberOfLinks;
1042 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
1043 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1044 /* first clear the S_IFMT bits */
1045 result->st_mode ^= (result->st_mode & 0170000);
1046 /* now set the bits that make this a symlink */
1047 result->st_mode |= 0120000;
1048 }
1049
1050 return 0;
1051}
1052
1053static BOOL
1054attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
1055{
1056 HANDLE hFindFile;
1057 WIN32_FIND_DATAA FileData;
1058 hFindFile = FindFirstFileA(pszFile, &FileData);
1059 if (hFindFile == INVALID_HANDLE_VALUE)
1060 return FALSE;
1061 FindClose(hFindFile);
1062 memset(info, 0, sizeof(*info))((__builtin_object_size (info, 0) != (size_t) -1) ? __builtin___memset_chk
(info, 0, sizeof(*info), __builtin_object_size (info, 0)) : __inline_memset_chk
(info, 0, sizeof(*info)))
;
1063 *reparse_tag = 0;
1064 info->dwFileAttributes = FileData.dwFileAttributes;
1065 info->ftCreationTime = FileData.ftCreationTime;
1066 info->ftLastAccessTime = FileData.ftLastAccessTime;
1067 info->ftLastWriteTime = FileData.ftLastWriteTime;
1068 info->nFileSizeHigh = FileData.nFileSizeHigh;
1069 info->nFileSizeLow = FileData.nFileSizeLow;
1070/* info->nNumberOfLinks = 1; */
1071 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1072 *reparse_tag = FileData.dwReserved0;
1073 return TRUE;
1074}
1075
1076static BOOL
1077attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
1078{
1079 HANDLE hFindFile;
1080 WIN32_FIND_DATAW FileData;
1081 hFindFile = FindFirstFileW(pszFile, &FileData);
1082 if (hFindFile == INVALID_HANDLE_VALUE)
1083 return FALSE;
1084 FindClose(hFindFile);
1085 memset(info, 0, sizeof(*info))((__builtin_object_size (info, 0) != (size_t) -1) ? __builtin___memset_chk
(info, 0, sizeof(*info), __builtin_object_size (info, 0)) : __inline_memset_chk
(info, 0, sizeof(*info)))
;
1086 *reparse_tag = 0;
1087 info->dwFileAttributes = FileData.dwFileAttributes;
1088 info->ftCreationTime = FileData.ftCreationTime;
1089 info->ftLastAccessTime = FileData.ftLastAccessTime;
1090 info->ftLastWriteTime = FileData.ftLastWriteTime;
1091 info->nFileSizeHigh = FileData.nFileSizeHigh;
1092 info->nFileSizeLow = FileData.nFileSizeLow;
1093/* info->nNumberOfLinks = 1; */
1094 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1095 *reparse_tag = FileData.dwReserved0;
1096 return TRUE;
1097}
1098
1099#ifndef SYMLOOP_MAX
1100#define SYMLOOP_MAX ( 88 )
1101#endif
1102
1103static int
1104win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth);
1105
1106static int
1107win32_xstat_impl(const char *path, struct win32_stat *result, BOOL traverse, int depth)
1108{
1109 int code;
1110 HANDLE hFile;
1111 BY_HANDLE_FILE_INFORMATION info;
1112 ULONG reparse_tag = 0;
1113 wchar_t *target_path;
1114 const char *dot;
1115
1116 if (depth > SYMLOOP_MAX) {
1117 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1118 return -1;
1119 }
1120
1121 hFile = CreateFileA(
1122 path,
1123 0, /* desired access */
1124 0, /* share mode */
1125 NULL((void *)0), /* security attributes */
1126 OPEN_EXISTING,
1127 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1128 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1129 NULL((void *)0));
1130
1131 if (hFile == INVALID_HANDLE_VALUE) {
1132 /* Either the target doesn't exist, or we don't have access to
1133 get a handle to it. If the former, we need to return an error.
1134 If the latter, we can use attributes_from_dir. */
1135 if (GetLastError() != ERROR_SHARING_VIOLATION)
1136 return -1;
1137 /* Could not get attributes on open file. Fall back to
1138 reading the directory. */
1139 if (!attributes_from_dir(path, &info, &reparse_tag))
1140 /* Very strange. This should not fail now */
1141 return -1;
1142 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1143 if (traverse) {
1144 /* Should traverse, but could not open reparse point handle */
1145 SetLastError(ERROR_SHARING_VIOLATION);
1146 return -1;
1147 }
1148 }
1149 } else {
1150 if (!GetFileInformationByHandle(hFile, &info)) {
1151 CloseHandle(hFile);
1152 return -1;;
1153 }
1154 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1155 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL((void *)0));
1156 CloseHandle(hFile);
1157 if (code < 0)
1158 return code;
1159 if (traverse) {
1160 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1161 free(target_path);
1162 return code;
1163 }
1164 } else
1165 CloseHandle(hFile);
1166 }
1167 attribute_data_to_stat(&info, reparse_tag, result);
1168
1169 /* Set S_IEXEC if it is an .exe, .bat, ... */
1170 dot = strrchr(path, '.');
1171 if (dot) {
1172 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1173 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1174 result->st_mode |= 0111;
1175 }
1176 return 0;
1177}
1178
1179static int
1180win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth)
1181{
1182 int code;
1183 HANDLE hFile;
1184 BY_HANDLE_FILE_INFORMATION info;
1185 ULONG reparse_tag = 0;
1186 wchar_t *target_path;
1187 const wchar_t *dot;
1188
1189 if (depth > SYMLOOP_MAX) {
1190 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1191 return -1;
1192 }
1193
1194 hFile = CreateFileW(
1195 path,
1196 0, /* desired access */
1197 0, /* share mode */
1198 NULL((void *)0), /* security attributes */
1199 OPEN_EXISTING,
1200 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1201 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1202 NULL((void *)0));
1203
1204 if (hFile == INVALID_HANDLE_VALUE) {
1205 /* Either the target doesn't exist, or we don't have access to
1206 get a handle to it. If the former, we need to return an error.
1207 If the latter, we can use attributes_from_dir. */
1208 if (GetLastError() != ERROR_SHARING_VIOLATION)
1209 return -1;
1210 /* Could not get attributes on open file. Fall back to
1211 reading the directory. */
1212 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1213 /* Very strange. This should not fail now */
1214 return -1;
1215 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1216 if (traverse) {
1217 /* Should traverse, but could not open reparse point handle */
1218 SetLastError(ERROR_SHARING_VIOLATION);
1219 return -1;
1220 }
1221 }
1222 } else {
1223 if (!GetFileInformationByHandle(hFile, &info)) {
1224 CloseHandle(hFile);
1225 return -1;;
1226 }
1227 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1228 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL((void *)0));
1229 CloseHandle(hFile);
1230 if (code < 0)
1231 return code;
1232 if (traverse) {
1233 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1234 free(target_path);
1235 return code;
1236 }
1237 } else
1238 CloseHandle(hFile);
1239 }
1240 attribute_data_to_stat(&info, reparse_tag, result);
1241
1242 /* Set S_IEXEC if it is an .exe, .bat, ... */
1243 dot = wcsrchr(path, '.');
1244 if (dot) {
1245 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1246 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1247 result->st_mode |= 0111;
1248 }
1249 return 0;
1250}
1251
1252static int
1253win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
1254{
1255 /* Protocol violation: we explicitly clear errno, instead of
1256 setting it to a POSIX error. Callers should use GetLastError. */
1257 int code = win32_xstat_impl(path, result, traverse, 0);
1258 errno(*__error()) = 0;
1259 return code;
1260}
1261
1262static int
1263win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1264{
1265 /* Protocol violation: we explicitly clear errno, instead of
1266 setting it to a POSIX error. Callers should use GetLastError. */
1267 int code = win32_xstat_impl_w(path, result, traverse, 0);
1268 errno(*__error()) = 0;
1269 return code;
1270}
1271
1272/* About the following functions: win32_lstat, win32_lstat_w, win32_stat,
1273 win32_stat_w
1274
1275 In Posix, stat automatically traverses symlinks and returns the stat
1276 structure for the target. In Windows, the equivalent GetFileAttributes by
1277 default does not traverse symlinks and instead returns attributes for
1278 the symlink.
1279
1280 Therefore, win32_lstat will get the attributes traditionally, and
1281 win32_stat will first explicitly resolve the symlink target and then will
1282 call win32_lstat on that result.
1283
1284 The _w represent Unicode equivalents of the aformentioned ANSI functions. */
1285
1286static int
1287win32_lstat(const char* path, struct win32_stat *result)
1288{
1289 return win32_xstat(path, result, FALSE);
1290}
1291
1292static int
1293win32_lstat_w(const wchar_t* path, struct win32_stat *result)
1294{
1295 return win32_xstat_w(path, result, FALSE);
1296}
1297
1298static int
1299win32_stat(const char* path, struct win32_stat *result)
1300{
1301 return win32_xstat(path, result, TRUE);
1302}
1303
1304static int
1305win32_stat_w(const wchar_t* path, struct win32_stat *result)
1306{
1307 return win32_xstat_w(path, result, TRUE);
1308}
1309
1310static int
1311win32_fstat(int file_number, struct win32_stat *result)
1312{
1313 BY_HANDLE_FILE_INFORMATION info;
1314 HANDLE h;
1315 int type;
1316
1317 h = (HANDLE)_get_osfhandle(file_number);
1318
1319 /* Protocol violation: we explicitly clear errno, instead of
1320 setting it to a POSIX error. Callers should use GetLastError. */
1321 errno(*__error()) = 0;
1322
1323 if (h == INVALID_HANDLE_VALUE) {
1324 /* This is really a C library error (invalid file handle).
1325 We set the Win32 error to the closes one matching. */
1326 SetLastError(ERROR_INVALID_HANDLE);
1327 return -1;
1328 }
1329 memset(result, 0, sizeof(*result))((__builtin_object_size (result, 0) != (size_t) -1) ? __builtin___memset_chk
(result, 0, sizeof(*result), __builtin_object_size (result, 0
)) : __inline_memset_chk (result, 0, sizeof(*result)))
;
1330
1331 type = GetFileType(h);
1332 if (type == FILE_TYPE_UNKNOWN) {
1333 DWORD error = GetLastError();
1334 if (error != 0) {
1335 return -1;
1336 }
1337 /* else: valid but unknown file */
1338 }
1339
1340 if (type != FILE_TYPE_DISK) {
1341 if (type == FILE_TYPE_CHAR)
1342 result->st_mode = _S_IFCHR;
1343 else if (type == FILE_TYPE_PIPE)
1344 result->st_mode = _S_IFIFO;
1345 return 0;
1346 }
1347
1348 if (!GetFileInformationByHandle(h, &info)) {
1349 return -1;
1350 }
1351
1352 attribute_data_to_stat(&info, 0, result);
1353 /* specific to fstat() */
1354 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1355 return 0;
1356}
1357
1358#endif /* MS_WINDOWS */
1359
1360PyDoc_STRVAR(stat_result__doc__,static char stat_result__doc__[] = "stat_result: Result from stat or lstat.\n\nThis object may be accessed either as a tuple of\n (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\nor via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\nPosix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\nor st_flags, they are available as attributes only.\n\nSee os.stat for more information."
1361"stat_result: Result from stat or lstat.\n\n\static char stat_result__doc__[] = "stat_result: Result from stat or lstat.\n\nThis object may be accessed either as a tuple of\n (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\nor via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\nPosix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\nor st_flags, they are available as attributes only.\n\nSee os.stat for more information."
1362This object may be accessed either as a tuple of\n\static char stat_result__doc__[] = "stat_result: Result from stat or lstat.\n\nThis object may be accessed either as a tuple of\n (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\nor via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\nPosix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\nor st_flags, they are available as attributes only.\n\nSee os.stat for more information."
1363 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\static char stat_result__doc__[] = "stat_result: Result from stat or lstat.\n\nThis object may be accessed either as a tuple of\n (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\nor via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\nPosix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\nor st_flags, they are available as attributes only.\n\nSee os.stat for more information."
1364or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\static char stat_result__doc__[] = "stat_result: Result from stat or lstat.\n\nThis object may be accessed either as a tuple of\n (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\nor via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\nPosix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\nor st_flags, they are available as attributes only.\n\nSee os.stat for more information."
1365\n\static char stat_result__doc__[] = "stat_result: Result from stat or lstat.\n\nThis object may be accessed either as a tuple of\n (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\nor via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\nPosix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\nor st_flags, they are available as attributes only.\n\nSee os.stat for more information."
1366Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\static char stat_result__doc__[] = "stat_result: Result from stat or lstat.\n\nThis object may be accessed either as a tuple of\n (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\nor via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\nPosix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\nor st_flags, they are available as attributes only.\n\nSee os.stat for more information."
1367or st_flags, they are available as attributes only.\n\static char stat_result__doc__[] = "stat_result: Result from stat or lstat.\n\nThis object may be accessed either as a tuple of\n (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\nor via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\nPosix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\nor st_flags, they are available as attributes only.\n\nSee os.stat for more information."
1368\n\static char stat_result__doc__[] = "stat_result: Result from stat or lstat.\n\nThis object may be accessed either as a tuple of\n (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\nor via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\nPosix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\nor st_flags, they are available as attributes only.\n\nSee os.stat for more information."
1369See os.stat for more information.")static char stat_result__doc__[] = "stat_result: Result from stat or lstat.\n\nThis object may be accessed either as a tuple of\n (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\nor via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\nPosix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\nor st_flags, they are available as attributes only.\n\nSee os.stat for more information.";
1370
1371static PyStructSequence_Field stat_result_fields[] = {
1372 {"st_mode", "protection bits"},
1373 {"st_ino", "inode"},
1374 {"st_dev", "device"},
1375 {"st_nlink", "number of hard links"},
1376 {"st_uid", "user ID of owner"},
1377 {"st_gid", "group ID of owner"},
1378 {"st_size", "total size, in bytes"},
1379 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1380 {NULL((void *)0), "integer time of last access"},
1381 {NULL((void *)0), "integer time of last modification"},
1382 {NULL((void *)0), "integer time of last change"},
1383 {"st_atime", "time of last access"},
1384 {"st_mtime", "time of last modification"},
1385 {"st_ctime", "time of last change"},
1386#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE1
1387 {"st_blksize", "blocksize for filesystem I/O"},
1388#endif
1389#ifdef HAVE_STRUCT_STAT_ST_BLOCKS1
1390 {"st_blocks", "number of blocks allocated"},
1391#endif
1392#ifdef HAVE_STRUCT_STAT_ST_RDEV1
1393 {"st_rdev", "device type (if inode device)"},
1394#endif
1395#ifdef HAVE_STRUCT_STAT_ST_FLAGS1
1396 {"st_flags", "user defined flags for file"},
1397#endif
1398#ifdef HAVE_STRUCT_STAT_ST_GEN1
1399 {"st_gen", "generation number"},
1400#endif
1401#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME1
1402 {"st_birthtime", "time of creation"},
1403#endif
1404 {0}
1405};
1406
1407#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE1
1408#define ST_BLKSIZE_IDX13 13
1409#else
1410#define ST_BLKSIZE_IDX13 12
1411#endif
1412
1413#ifdef HAVE_STRUCT_STAT_ST_BLOCKS1
1414#define ST_BLOCKS_IDX(13 +1) (ST_BLKSIZE_IDX13+1)
1415#else
1416#define ST_BLOCKS_IDX(13 +1) ST_BLKSIZE_IDX13
1417#endif
1418
1419#ifdef HAVE_STRUCT_STAT_ST_RDEV1
1420#define ST_RDEV_IDX((13 +1)+1) (ST_BLOCKS_IDX(13 +1)+1)
1421#else
1422#define ST_RDEV_IDX((13 +1)+1) ST_BLOCKS_IDX(13 +1)
1423#endif
1424
1425#ifdef HAVE_STRUCT_STAT_ST_FLAGS1
1426#define ST_FLAGS_IDX(((13 +1)+1)+1) (ST_RDEV_IDX((13 +1)+1)+1)
1427#else
1428#define ST_FLAGS_IDX(((13 +1)+1)+1) ST_RDEV_IDX((13 +1)+1)
1429#endif
1430
1431#ifdef HAVE_STRUCT_STAT_ST_GEN1
1432#define ST_GEN_IDX((((13 +1)+1)+1)+1) (ST_FLAGS_IDX(((13 +1)+1)+1)+1)
1433#else
1434#define ST_GEN_IDX((((13 +1)+1)+1)+1) ST_FLAGS_IDX(((13 +1)+1)+1)
1435#endif
1436
1437#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME1
1438#define ST_BIRTHTIME_IDX(((((13 +1)+1)+1)+1)+1) (ST_GEN_IDX((((13 +1)+1)+1)+1)+1)
1439#else
1440#define ST_BIRTHTIME_IDX(((((13 +1)+1)+1)+1)+1) ST_GEN_IDX((((13 +1)+1)+1)+1)
1441#endif
1442
1443static PyStructSequence_Desc stat_result_desc = {
1444 "stat_result", /* name */
1445 stat_result__doc__, /* doc */
1446 stat_result_fields,
1447 10
1448};
1449
1450PyDoc_STRVAR(statvfs_result__doc__,static char statvfs_result__doc__[] = "statvfs_result: Result from statvfs or fstatvfs.\n\nThis object may be accessed either as a tuple of\n (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\nor via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\nSee os.statvfs for more information."
1451"statvfs_result: Result from statvfs or fstatvfs.\n\n\static char statvfs_result__doc__[] = "statvfs_result: Result from statvfs or fstatvfs.\n\nThis object may be accessed either as a tuple of\n (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\nor via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\nSee os.statvfs for more information."
1452This object may be accessed either as a tuple of\n\static char statvfs_result__doc__[] = "statvfs_result: Result from statvfs or fstatvfs.\n\nThis object may be accessed either as a tuple of\n (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\nor via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\nSee os.statvfs for more information."
1453 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\static char statvfs_result__doc__[] = "statvfs_result: Result from statvfs or fstatvfs.\n\nThis object may be accessed either as a tuple of\n (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\nor via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\nSee os.statvfs for more information."
1454or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\static char statvfs_result__doc__[] = "statvfs_result: Result from statvfs or fstatvfs.\n\nThis object may be accessed either as a tuple of\n (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\nor via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\nSee os.statvfs for more information."
1455\n\static char statvfs_result__doc__[] = "statvfs_result: Result from statvfs or fstatvfs.\n\nThis object may be accessed either as a tuple of\n (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\nor via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\nSee os.statvfs for more information."
1456See os.statvfs for more information.")static char statvfs_result__doc__[] = "statvfs_result: Result from statvfs or fstatvfs.\n\nThis object may be accessed either as a tuple of\n (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\nor via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\nSee os.statvfs for more information.";
1457
1458static PyStructSequence_Field statvfs_result_fields[] = {
1459 {"f_bsize", },
1460 {"f_frsize", },
1461 {"f_blocks", },
1462 {"f_bfree", },
1463 {"f_bavail", },
1464 {"f_files", },
1465 {"f_ffree", },
1466 {"f_favail", },
1467 {"f_flag", },
1468 {"f_namemax",},
1469 {0}
1470};
1471
1472static PyStructSequence_Desc statvfs_result_desc = {
1473 "statvfs_result", /* name */
1474 statvfs_result__doc__, /* doc */
1475 statvfs_result_fields,
1476 10
1477};
1478
1479static int initialized;
1480static PyTypeObject StatResultType;
1481static PyTypeObject StatVFSResultType;
1482static newfunc structseq_new;
1483
1484static PyObject *
1485statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1486{
1487 PyStructSequence *result;
1488 int i;
1489
1490 result = (PyStructSequence*)structseq_new(type, args, kwds);
1491 if (!result)
1492 return NULL((void *)0);
1493 /* If we have been initialized from a tuple,
1494 st_?time might be set to None. Initialize it
1495 from the int slots. */
1496 for (i = 7; i <= 9; i++) {
1497 if (result->ob_item[i+3] == Py_None(&_Py_NoneStruct)) {
1498 Py_DECREF(Py_None)do { if (_Py_RefTotal-- , --((PyObject*)((&_Py_NoneStruct
)))->ob_refcnt != 0) { if (((PyObject*)(&_Py_NoneStruct
))->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/posixmodule.c"
, 1498, (PyObject *)((&_Py_NoneStruct))); } else _Py_Dealloc
((PyObject *)((&_Py_NoneStruct))); } while (0)
;
1499 Py_INCREF(result->ob_item[i])( _Py_RefTotal++ , ((PyObject*)(result->ob_item[i]))->ob_refcnt
++)
;
1500 result->ob_item[i+3] = result->ob_item[i];
1501 }
1502 }
1503 return (PyObject*)result;
1504}
1505
1506
1507
1508/* If true, st_?time is float. */
1509static int _stat_float_times = 1;
1510
1511PyDoc_STRVAR(stat_float_times__doc__,static char stat_float_times__doc__[] = "stat_float_times([newval]) -> oldval\n\nDetermine whether os.[lf]stat represents time stamps as float objects.\nIf newval is True, future calls to stat() return floats, if it is False,\nfuture calls return ints. \nIf newval is omitted, return the current setting.\n"
1512"stat_float_times([newval]) -> oldval\n\n\static char stat_float_times__doc__[] = "stat_float_times([newval]) -> oldval\n\nDetermine whether os.[lf]stat represents time stamps as float objects.\nIf newval is True, future calls to stat() return floats, if it is False,\nfuture calls return ints. \nIf newval is omitted, return the current setting.\n"
1513Determine whether os.[lf]stat represents time stamps as float objects.\n\static char stat_float_times__doc__[] = "stat_float_times([newval]) -> oldval\n\nDetermine whether os.[lf]stat represents time stamps as float objects.\nIf newval is True, future calls to stat() return floats, if it is False,\nfuture calls return ints. \nIf newval is omitted, return the current setting.\n"
1514If newval is True, future calls to stat() return floats, if it is False,\n\static char stat_float_times__doc__[] = "stat_float_times([newval]) -> oldval\n\nDetermine whether os.[lf]stat represents time stamps as float objects.\nIf newval is True, future calls to stat() return floats, if it is False,\nfuture calls return ints. \nIf newval is omitted, return the current setting.\n"
1515future calls return ints. \n\static char stat_float_times__doc__[] = "stat_float_times([newval]) -> oldval\n\nDetermine whether os.[lf]stat represents time stamps as float objects.\nIf newval is True, future calls to stat() return floats, if it is False,\nfuture calls return ints. \nIf newval is omitted, return the current setting.\n"
1516If newval is omitted, return the current setting.\n")static char stat_float_times__doc__[] = "stat_float_times([newval]) -> oldval\n\nDetermine whether os.[lf]stat represents time stamps as float objects.\nIf newval is True, future calls to stat() return floats, if it is False,\nfuture calls return ints. \nIf newval is omitted, return the current setting.\n";
1517
1518static PyObject*
1519stat_float_times(PyObject* self, PyObject *args)
1520{
1521 int newval = -1;
1522 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|i:stat_float_times", &newval))
1523 return NULL((void *)0);
1524 if (newval == -1)
1525 /* Return old value */
1526 return PyBool_FromLong(_stat_float_times);
1527 _stat_float_times = newval;
1528 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
1529 return Py_None(&_Py_NoneStruct);
1530}
1531
1532static void
1533fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1534{
1535 PyObject *fval,*ival;
1536#if SIZEOF_TIME_T8 > SIZEOF_LONG8
1537 ival = PyLong_FromLongLong((PY_LONG_LONGlong long)sec);
1538#else
1539 ival = PyLong_FromLong((long)sec);
1540#endif
1541 if (!ival)
1542 return;
1543 if (_stat_float_times) {
1544 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1545 } else {
1546 fval = ival;
1547 Py_INCREF(fval)( _Py_RefTotal++ , ((PyObject*)(fval))->ob_refcnt++);
1548 }
1549 PyStructSequence_SET_ITEM(v, index, ival)(((PyTupleObject *)(v))->ob_item[index] = ival);
1550 PyStructSequence_SET_ITEM(v, index+3, fval)(((PyTupleObject *)(v))->ob_item[index+3] = fval);
1551}
1552
1553/* pack a system stat C structure into the Python stat tuple
1554 (used by posix_stat() and posix_fstat()) */
1555static PyObject*
1556_pystat_fromstructstat(STRUCT_STATstruct stat *st)
1557{
1558 unsigned long ansec, mnsec, cnsec;
1559 PyObject *v = PyStructSequence_New(&StatResultType);
1560 if (v == NULL((void *)0))
1561 return NULL((void *)0);
1562
1563 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode))(((PyTupleObject *)(v))->ob_item[0] = PyLong_FromLong((long
)st->st_mode))
;
1564#ifdef HAVE_LARGEFILE_SUPPORT
1565 PyStructSequence_SET_ITEM(v, 1,(((PyTupleObject *)(v))->ob_item[1] = PyLong_FromLongLong(
(long long)st->st_ino))
1566 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino))(((PyTupleObject *)(v))->ob_item[1] = PyLong_FromLongLong(
(long long)st->st_ino))
;
1567#else
1568 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino))(((PyTupleObject *)(v))->ob_item[1] = PyLong_FromLong((long
)st->st_ino))
;
1569#endif
1570#if defined(HAVE_LONG_LONG1) && !defined(MS_WINDOWS)
1571 PyStructSequence_SET_ITEM(v, 2,(((PyTupleObject *)(v))->ob_item[2] = PyLong_FromLongLong(
(long long)st->st_dev))
1572 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev))(((PyTupleObject *)(v))->ob_item[2] = PyLong_FromLongLong(
(long long)st->st_dev))
;
1573#else
1574 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev))(((PyTupleObject *)(v))->ob_item[2] = PyLong_FromLong((long
)st->st_dev))
;
1575#endif
1576 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink))(((PyTupleObject *)(v))->ob_item[3] = PyLong_FromLong((long
)st->st_nlink))
;
1577 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid))(((PyTupleObject *)(v))->ob_item[4] = PyLong_FromLong((long
)st->st_uid))
;
1578 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid))(((PyTupleObject *)(v))->ob_item[5] = PyLong_FromLong((long
)st->st_gid))
;
1579#ifdef HAVE_LARGEFILE_SUPPORT
1580 PyStructSequence_SET_ITEM(v, 6,(((PyTupleObject *)(v))->ob_item[6] = PyLong_FromLongLong(
(long long)st->st_size))
1581 PyLong_FromLongLong((PY_LONG_LONG)st->st_size))(((PyTupleObject *)(v))->ob_item[6] = PyLong_FromLongLong(
(long long)st->st_size))
;
1582#else
1583 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size))(((PyTupleObject *)(v))->ob_item[6] = PyLong_FromLong(st->
st_size))
;
1584#endif
1585
1586#if defined(HAVE_STAT_TV_NSEC)
1587 ansec = st->st_atim.tv_nsec;
1588 mnsec = st->st_mtim.tv_nsec;
1589 cnsec = st->st_ctim.tv_nsec;
1590#elif defined(HAVE_STAT_TV_NSEC21)
1591 ansec = st->st_atimespec.tv_nsec;
1592 mnsec = st->st_mtimespec.tv_nsec;
1593 cnsec = st->st_ctimespec.tv_nsec;
1594#elif defined(HAVE_STAT_NSEC)
1595 ansec = st->st_atime_nsec;
1596 mnsec = st->st_mtime_nsec;
1597 cnsec = st->st_ctime_nsec;
1598#else
1599 ansec = mnsec = cnsec = 0;
1600#endif
1601 fill_time(v, 7, st->st_atimest_atimespec.tv_sec, ansec);
1602 fill_time(v, 8, st->st_mtimest_mtimespec.tv_sec, mnsec);
1603 fill_time(v, 9, st->st_ctimest_ctimespec.tv_sec, cnsec);
1604
1605#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE1
1606 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,(((PyTupleObject *)(v))->ob_item[13] = PyLong_FromLong((long
)st->st_blksize))
1607 PyLong_FromLong((long)st->st_blksize))(((PyTupleObject *)(v))->ob_item[13] = PyLong_FromLong((long
)st->st_blksize))
;
1608#endif
1609#ifdef HAVE_STRUCT_STAT_ST_BLOCKS1
1610 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,(((PyTupleObject *)(v))->ob_item[(13 +1)] = PyLong_FromLong
((long)st->st_blocks))
1611 PyLong_FromLong((long)st->st_blocks))(((PyTupleObject *)(v))->ob_item[(13 +1)] = PyLong_FromLong
((long)st->st_blocks))
;
1612#endif
1613#ifdef HAVE_STRUCT_STAT_ST_RDEV1
1614 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,(((PyTupleObject *)(v))->ob_item[((13 +1)+1)] = PyLong_FromLong
((long)st->st_rdev))
1615 PyLong_FromLong((long)st->st_rdev))(((PyTupleObject *)(v))->ob_item[((13 +1)+1)] = PyLong_FromLong
((long)st->st_rdev))
;
1616#endif
1617#ifdef HAVE_STRUCT_STAT_ST_GEN1
1618 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,(((PyTupleObject *)(v))->ob_item[((((13 +1)+1)+1)+1)] = PyLong_FromLong
((long)st->st_gen))
1619 PyLong_FromLong((long)st->st_gen))(((PyTupleObject *)(v))->ob_item[((((13 +1)+1)+1)+1)] = PyLong_FromLong
((long)st->st_gen))
;
1620#endif
1621#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME1
1622 {
1623 PyObject *val;
1624 unsigned long bsec,bnsec;
1625 bsec = (long)st->st_birthtimest_birthtimespec.tv_sec;
1626#ifdef HAVE_STAT_TV_NSEC21
1627 bnsec = st->st_birthtimespec.tv_nsec;
1628#else
1629 bnsec = 0;
1630#endif
1631 if (_stat_float_times) {
1632 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1633 } else {
1634 val = PyLong_FromLong((long)bsec);
1635 }
1636 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,(((PyTupleObject *)(v))->ob_item[(((((13 +1)+1)+1)+1)+1)] =
val)
1637 val)(((PyTupleObject *)(v))->ob_item[(((((13 +1)+1)+1)+1)+1)] =
val)
;
1638 }
1639#endif
1640#ifdef HAVE_STRUCT_STAT_ST_FLAGS1
1641 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,(((PyTupleObject *)(v))->ob_item[(((13 +1)+1)+1)] = PyLong_FromLong
((long)st->st_flags))
1642 PyLong_FromLong((long)st->st_flags))(((PyTupleObject *)(v))->ob_item[(((13 +1)+1)+1)] = PyLong_FromLong
((long)st->st_flags))
;
1643#endif
1644
1645 if (PyErr_Occurred()) {
1646 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 1646, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
1647 return NULL((void *)0);
1648 }
1649
1650 return v;
1651}
1652
1653static PyObject *
1654posix_do_stat(PyObject *self, PyObject *args,
1655 char *format,
1656#ifdef __VMS
1657 int (*statfunc)(const char *, STRUCT_STATstruct stat *, ...),
1658#else
1659 int (*statfunc)(const char *, STRUCT_STATstruct stat *),
1660#endif
1661 char *wformat,
1662 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STATstruct stat *))
1663{
1664 STRUCT_STATstruct stat st;
1665 PyObject *opath;
1666 char *path;
1667 int res;
1668 PyObject *result;
1669
1670#ifdef MS_WINDOWS
1671 PyUnicodeObject *po;
1672 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, wformat, &po)) {
1673 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po)((__builtin_expect(!(((((((PyObject*)(po))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 1673, "PyUnicode_Check(po)") : (
void)0),(((PyUnicodeObject *)(po))->str))
;
1674
1675 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
1676 /* PyUnicode_AS_UNICODE result OK without
1677 thread lock as it is a simple dereference. */
1678 res = wstatfunc(wpath, &st);
1679 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
1680
1681 if (res != 0)
1682 return win32_error_unicode("stat", wpath);
1683 return _pystat_fromstructstat(&st);
1684 }
1685 /* Drop the argument parsing error as narrow strings
1686 are also valid. */
1687 PyErr_Clear();
1688#endif
1689
1690 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, format,
1691 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath))
1692 return NULL((void *)0);
1693 path = PyBytes_AsString(opath);
1694 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
1695 res = (*statfunc)(path, &st);
1696 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
1697
1698 if (res != 0) {
1699#ifdef MS_WINDOWS
1700 result = win32_error("stat", path);
1701#else
1702 result = posix_error_with_filename(path);
1703#endif
1704 }
1705 else
1706 result = _pystat_fromstructstat(&st);
1707
1708 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 1708, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
1709 return result;
1710}
1711
1712/* POSIX methods */
1713
1714PyDoc_STRVAR(posix_access__doc__,static char posix_access__doc__[] = "access(path, mode) -> True if granted, False otherwise\n\nUse the real uid/gid to test for access to a path. Note that most\noperations will use the effective uid/gid, therefore this routine can\nbe used in a suid/sgid environment to test if the invoking user has the\nspecified access to the path. The mode argument can be F_OK to test\nexistence, or the inclusive-OR of R_OK, W_OK, and X_OK."
1715"access(path, mode) -> True if granted, False otherwise\n\n\static char posix_access__doc__[] = "access(path, mode) -> True if granted, False otherwise\n\nUse the real uid/gid to test for access to a path. Note that most\noperations will use the effective uid/gid, therefore this routine can\nbe used in a suid/sgid environment to test if the invoking user has the\nspecified access to the path. The mode argument can be F_OK to test\nexistence, or the inclusive-OR of R_OK, W_OK, and X_OK."
1716Use the real uid/gid to test for access to a path. Note that most\n\static char posix_access__doc__[] = "access(path, mode) -> True if granted, False otherwise\n\nUse the real uid/gid to test for access to a path. Note that most\noperations will use the effective uid/gid, therefore this routine can\nbe used in a suid/sgid environment to test if the invoking user has the\nspecified access to the path. The mode argument can be F_OK to test\nexistence, or the inclusive-OR of R_OK, W_OK, and X_OK."
1717operations will use the effective uid/gid, therefore this routine can\n\static char posix_access__doc__[] = "access(path, mode) -> True if granted, False otherwise\n\nUse the real uid/gid to test for access to a path. Note that most\noperations will use the effective uid/gid, therefore this routine can\nbe used in a suid/sgid environment to test if the invoking user has the\nspecified access to the path. The mode argument can be F_OK to test\nexistence, or the inclusive-OR of R_OK, W_OK, and X_OK."
1718be used in a suid/sgid environment to test if the invoking user has the\n\static char posix_access__doc__[] = "access(path, mode) -> True if granted, False otherwise\n\nUse the real uid/gid to test for access to a path. Note that most\noperations will use the effective uid/gid, therefore this routine can\nbe used in a suid/sgid environment to test if the invoking user has the\nspecified access to the path. The mode argument can be F_OK to test\nexistence, or the inclusive-OR of R_OK, W_OK, and X_OK."
1719specified access to the path. The mode argument can be F_OK to test\n\static char posix_access__doc__[] = "access(path, mode) -> True if granted, False otherwise\n\nUse the real uid/gid to test for access to a path. Note that most\noperations will use the effective uid/gid, therefore this routine can\nbe used in a suid/sgid environment to test if the invoking user has the\nspecified access to the path. The mode argument can be F_OK to test\nexistence, or the inclusive-OR of R_OK, W_OK, and X_OK."
1720existence, or the inclusive-OR of R_OK, W_OK, and X_OK.")static char posix_access__doc__[] = "access(path, mode) -> True if granted, False otherwise\n\nUse the real uid/gid to test for access to a path. Note that most\noperations will use the effective uid/gid, therefore this routine can\nbe used in a suid/sgid environment to test if the invoking user has the\nspecified access to the path. The mode argument can be F_OK to test\nexistence, or the inclusive-OR of R_OK, W_OK, and X_OK.";
1721
1722static PyObject *
1723posix_access(PyObject *self, PyObject *args)
1724{
1725 PyObject *opath;
1726 char *path;
1727 int mode;
1728
1729#ifdef MS_WINDOWS
1730 DWORD attr;
1731 PyUnicodeObject *po;
1732 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "Ui:access", &po, &mode)) {
1733 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
1734 /* PyUnicode_AS_UNICODE OK without thread lock as
1735 it is a simple dereference. */
1736 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)((__builtin_expect(!(((((((PyObject*)(po))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 1736, "PyUnicode_Check(po)") : (
void)0),(((PyUnicodeObject *)(po))->str))
);
1737 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
1738 goto finish;
1739 }
1740 /* Drop the argument parsing error as narrow strings
1741 are also valid. */
1742 PyErr_Clear();
1743 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&i:access",
1744 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath, &mode))
1745 return NULL((void *)0);
1746 path = PyBytes_AsString(opath);
1747 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
1748 attr = GetFileAttributesA(path);
1749 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
1750 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 1750, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
1751finish:
1752 if (attr == 0xFFFFFFFF)
1753 /* File does not exist, or cannot read attributes */
1754 return PyBool_FromLong(0);
1755 /* Access is possible if either write access wasn't requested, or
1756 the file isn't read-only, or if it's a directory, as there are
1757 no read-only directories on Windows. */
1758 return PyBool_FromLong(!(mode & 2)
1759 || !(attr & FILE_ATTRIBUTE_READONLY)
1760 || (attr & FILE_ATTRIBUTE_DIRECTORY));
1761#else
1762 int res;
1763 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&i:access",
1764 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath, &mode))
1765 return NULL((void *)0);
1766 path = PyBytes_AsString(opath);
1767 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
1768 res = access(path, mode);
1769 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
1770 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 1770, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
1771 return PyBool_FromLong(res == 0);
1772#endif
1773}
1774
1775#ifndef F_OK0
1776#define F_OK0 0
1777#endif
1778#ifndef R_OK(1<<2)
1779#define R_OK(1<<2) 4
1780#endif
1781#ifndef W_OK(1<<1)
1782#define W_OK(1<<1) 2
1783#endif
1784#ifndef X_OK(1<<0)
1785#define X_OK(1<<0) 1
1786#endif
1787
1788#ifdef HAVE_TTYNAME1
1789PyDoc_STRVAR(posix_ttyname__doc__,static char posix_ttyname__doc__[] = "ttyname(fd) -> string\n\nReturn the name of the terminal device connected to 'fd'."
1790"ttyname(fd) -> string\n\n\static char posix_ttyname__doc__[] = "ttyname(fd) -> string\n\nReturn the name of the terminal device connected to 'fd'."
1791Return the name of the terminal device connected to 'fd'.")static char posix_ttyname__doc__[] = "ttyname(fd) -> string\n\nReturn the name of the terminal device connected to 'fd'.";
1792
1793static PyObject *
1794posix_ttyname(PyObject *self, PyObject *args)
1795{
1796 int id;
1797 char *ret;
1798
1799 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:ttyname", &id))
1800 return NULL((void *)0);
1801
1802#if defined(__VMS)
1803 /* file descriptor 0 only, the default input device (stdin) */
1804 if (id == 0) {
1805 ret = ttyname();
1806 }
1807 else {
1808 ret = NULL((void *)0);
1809 }
1810#else
1811 ret = ttyname(id);
1812#endif
1813 if (ret == NULL((void *)0))
1814 return posix_error();
1815 return PyUnicode_DecodeFSDefaultPyUnicodeUCS2_DecodeFSDefault(ret);
1816}
1817#endif
1818
1819#ifdef HAVE_CTERMID1
1820PyDoc_STRVAR(posix_ctermid__doc__,static char posix_ctermid__doc__[] = "ctermid() -> string\n\nReturn the name of the controlling terminal for this process."
1821"ctermid() -> string\n\n\static char posix_ctermid__doc__[] = "ctermid() -> string\n\nReturn the name of the controlling terminal for this process."
1822Return the name of the controlling terminal for this process.")static char posix_ctermid__doc__[] = "ctermid() -> string\n\nReturn the name of the controlling terminal for this process.";
1823
1824static PyObject *
1825posix_ctermid(PyObject *self, PyObject *noargs)
1826{
1827 char *ret;
1828 char buffer[L_ctermid1024];
1829
1830#ifdef USE_CTERMID_R
1831 ret = ctermid_r(buffer);
1832#else
1833 ret = ctermid(buffer);
1834#endif
1835 if (ret == NULL((void *)0))
1836 return posix_error();
1837 return PyUnicode_DecodeFSDefaultPyUnicodeUCS2_DecodeFSDefault(buffer);
1838}
1839#endif
1840
1841PyDoc_STRVAR(posix_chdir__doc__,static char posix_chdir__doc__[] = "chdir(path)\n\nChange the current working directory to the specified path."
1842"chdir(path)\n\n\static char posix_chdir__doc__[] = "chdir(path)\n\nChange the current working directory to the specified path."
1843Change the current working directory to the specified path.")static char posix_chdir__doc__[] = "chdir(path)\n\nChange the current working directory to the specified path.";
1844
1845static PyObject *
1846posix_chdir(PyObject *self, PyObject *args)
1847{
1848#ifdef MS_WINDOWS
1849 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
1850#elif defined(PYOS_OS2) && defined(PYCC_GCC)
1851 return posix_1str(args, "O&:chdir", _chdir2);
1852#elif defined(__VMS)
1853 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
1854#else
1855 return posix_1str(args, "O&:chdir", chdir);
1856#endif
1857}
1858
1859#ifdef HAVE_FCHDIR1
1860PyDoc_STRVAR(posix_fchdir__doc__,static char posix_fchdir__doc__[] = "fchdir(fildes)\n\nChange to the directory of the given file descriptor. fildes must be\nopened on a directory, not a file."
1861"fchdir(fildes)\n\n\static char posix_fchdir__doc__[] = "fchdir(fildes)\n\nChange to the directory of the given file descriptor. fildes must be\nopened on a directory, not a file."
1862Change to the directory of the given file descriptor. fildes must be\n\static char posix_fchdir__doc__[] = "fchdir(fildes)\n\nChange to the directory of the given file descriptor. fildes must be\nopened on a directory, not a file."
1863opened on a directory, not a file.")static char posix_fchdir__doc__[] = "fchdir(fildes)\n\nChange to the directory of the given file descriptor. fildes must be\nopened on a directory, not a file.";
1864
1865static PyObject *
1866posix_fchdir(PyObject *self, PyObject *fdobj)
1867{
1868 return posix_fildes(fdobj, fchdir);
1869}
1870#endif /* HAVE_FCHDIR */
1871
1872
1873PyDoc_STRVAR(posix_chmod__doc__,static char posix_chmod__doc__[] = "chmod(path, mode)\n\nChange the access permissions of a file."
1874"chmod(path, mode)\n\n\static char posix_chmod__doc__[] = "chmod(path, mode)\n\nChange the access permissions of a file."
1875Change the access permissions of a file.")static char posix_chmod__doc__[] = "chmod(path, mode)\n\nChange the access permissions of a file.";
1876
1877static PyObject *
1878posix_chmod(PyObject *self, PyObject *args)
1879{
1880 PyObject *opath = NULL((void *)0);
1881 char *path = NULL((void *)0);
1882 int i;
1883 int res;
1884#ifdef MS_WINDOWS
1885 DWORD attr;
1886 PyUnicodeObject *po;
1887 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "Ui|:chmod", &po, &i)) {
1888 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
1889 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)((__builtin_expect(!(((((((PyObject*)(po))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 1889, "PyUnicode_Check(po)") : (
void)0),(((PyUnicodeObject *)(po))->str))
);
1890 if (attr != 0xFFFFFFFF) {
1891 if (i & _S_IWRITE)
1892 attr &= ~FILE_ATTRIBUTE_READONLY;
1893 else
1894 attr |= FILE_ATTRIBUTE_READONLY;
1895 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po)((__builtin_expect(!(((((((PyObject*)(po))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 1895, "PyUnicode_Check(po)") : (
void)0),(((PyUnicodeObject *)(po))->str))
, attr);
1896 }
1897 else
1898 res = 0;
1899 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
1900 if (!res)
1901 return win32_error_unicode("chmod",
1902 PyUnicode_AS_UNICODE(po)((__builtin_expect(!(((((((PyObject*)(po))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 1902, "PyUnicode_Check(po)") : (
void)0),(((PyUnicodeObject *)(po))->str))
);
1903 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
1904 return Py_None(&_Py_NoneStruct);
1905 }
1906 /* Drop the argument parsing error as narrow strings
1907 are also valid. */
1908 PyErr_Clear();
1909
1910 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&i:chmod", PyUnicode_FSConverterPyUnicodeUCS2_FSConverter,
1911 &opath, &i))
1912 return NULL((void *)0);
1913 path = PyBytes_AsString(opath);
1914 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
1915 attr = GetFileAttributesA(path);
1916 if (attr != 0xFFFFFFFF) {
1917 if (i & _S_IWRITE)
1918 attr &= ~FILE_ATTRIBUTE_READONLY;
1919 else
1920 attr |= FILE_ATTRIBUTE_READONLY;
1921 res = SetFileAttributesA(path, attr);
1922 }
1923 else
1924 res = 0;
1925 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
1926 if (!res) {
1927 win32_error("chmod", path);
1928 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 1928, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
1929 return NULL((void *)0);
1930 }
1931 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 1931, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
1932 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
1933 return Py_None(&_Py_NoneStruct);
1934#else /* MS_WINDOWS */
1935 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&i:chmod", PyUnicode_FSConverterPyUnicodeUCS2_FSConverter,
1936 &opath, &i))
1937 return NULL((void *)0);
1938 path = PyBytes_AsString(opath);
1939 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
1940 res = chmod(path, i);
1941 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
1942 if (res < 0)
1943 return posix_error_with_allocated_filename(opath);
1944 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 1944, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
1945 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
1946 return Py_None(&_Py_NoneStruct);
1947#endif
1948}
1949
1950#ifdef HAVE_FCHMOD1
1951PyDoc_STRVAR(posix_fchmod__doc__,static char posix_fchmod__doc__[] = "fchmod(fd, mode)\n\nChange the access permissions of the file given by file\ndescriptor fd."
1952"fchmod(fd, mode)\n\n\static char posix_fchmod__doc__[] = "fchmod(fd, mode)\n\nChange the access permissions of the file given by file\ndescriptor fd."
1953Change the access permissions of the file given by file\n\static char posix_fchmod__doc__[] = "fchmod(fd, mode)\n\nChange the access permissions of the file given by file\ndescriptor fd."
1954descriptor fd.")static char posix_fchmod__doc__[] = "fchmod(fd, mode)\n\nChange the access permissions of the file given by file\ndescriptor fd.";
1955
1956static PyObject *
1957posix_fchmod(PyObject *self, PyObject *args)
1958{
1959 int fd, mode, res;
1960 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ii:fchmod", &fd, &mode))
1961 return NULL((void *)0);
1962 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
1963 res = fchmod(fd, mode);
1964 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
1965 if (res < 0)
1966 return posix_error();
1967 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1968}
1969#endif /* HAVE_FCHMOD */
1970
1971#ifdef HAVE_LCHMOD1
1972PyDoc_STRVAR(posix_lchmod__doc__,static char posix_lchmod__doc__[] = "lchmod(path, mode)\n\nChange the access permissions of a file. If path is a symlink, this\naffects the link itself rather than the target."
1973"lchmod(path, mode)\n\n\static char posix_lchmod__doc__[] = "lchmod(path, mode)\n\nChange the access permissions of a file. If path is a symlink, this\naffects the link itself rather than the target."
1974Change the access permissions of a file. If path is a symlink, this\n\static char posix_lchmod__doc__[] = "lchmod(path, mode)\n\nChange the access permissions of a file. If path is a symlink, this\naffects the link itself rather than the target."
1975affects the link itself rather than the target.")static char posix_lchmod__doc__[] = "lchmod(path, mode)\n\nChange the access permissions of a file. If path is a symlink, this\naffects the link itself rather than the target.";
1976
1977static PyObject *
1978posix_lchmod(PyObject *self, PyObject *args)
1979{
1980 PyObject *opath;
1981 char *path;
1982 int i;
1983 int res;
1984 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&i:lchmod", PyUnicode_FSConverterPyUnicodeUCS2_FSConverter,
1985 &opath, &i))
1986 return NULL((void *)0);
1987 path = PyBytes_AsString(opath);
1988 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
1989 res = lchmod(path, i);
1990 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
1991 if (res < 0)
1992 return posix_error_with_allocated_filename(opath);
1993 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 1993, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
1994 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1995}
1996#endif /* HAVE_LCHMOD */
1997
1998
1999#ifdef HAVE_CHFLAGS
2000PyDoc_STRVAR(posix_chflags__doc__,static char posix_chflags__doc__[] = "chflags(path, flags)\n\nSet file flags."
2001"chflags(path, flags)\n\n\static char posix_chflags__doc__[] = "chflags(path, flags)\n\nSet file flags."
2002Set file flags.")static char posix_chflags__doc__[] = "chflags(path, flags)\n\nSet file flags.";
2003
2004static PyObject *
2005posix_chflags(PyObject *self, PyObject *args)
2006{
2007 PyObject *opath;
2008 char *path;
2009 unsigned long flags;
2010 int res;
2011 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&k:chflags",
2012 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath, &flags))
2013 return NULL((void *)0);
2014 path = PyBytes_AsString(opath);
2015 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2016 res = chflags(path, flags);
2017 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2018 if (res < 0)
2019 return posix_error_with_allocated_filename(opath);
2020 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2020, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
2021 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
2022 return Py_None(&_Py_NoneStruct);
2023}
2024#endif /* HAVE_CHFLAGS */
2025
2026#ifdef HAVE_LCHFLAGS
2027PyDoc_STRVAR(posix_lchflags__doc__,static char posix_lchflags__doc__[] = "lchflags(path, flags)\n\nSet file flags.\nThis function will not follow symbolic links."
2028"lchflags(path, flags)\n\n\static char posix_lchflags__doc__[] = "lchflags(path, flags)\n\nSet file flags.\nThis function will not follow symbolic links."
2029Set file flags.\n\static char posix_lchflags__doc__[] = "lchflags(path, flags)\n\nSet file flags.\nThis function will not follow symbolic links."
2030This function will not follow symbolic links.")static char posix_lchflags__doc__[] = "lchflags(path, flags)\n\nSet file flags.\nThis function will not follow symbolic links.";
2031
2032static PyObject *
2033posix_lchflags(PyObject *self, PyObject *args)
2034{
2035 PyObject *opath;
2036 char *path;
2037 unsigned long flags;
2038 int res;
2039 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&k:lchflags",
2040 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath, &flags))
2041 return NULL((void *)0);
2042 path = PyBytes_AsString(opath);
2043 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2044 res = lchflags(path, flags);
2045 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2046 if (res < 0)
2047 return posix_error_with_allocated_filename(opath);
2048 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2048, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
2049 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
2050 return Py_None(&_Py_NoneStruct);
2051}
2052#endif /* HAVE_LCHFLAGS */
2053
2054#ifdef HAVE_CHROOT1
2055PyDoc_STRVAR(posix_chroot__doc__,static char posix_chroot__doc__[] = "chroot(path)\n\nChange root directory to path."
2056"chroot(path)\n\n\static char posix_chroot__doc__[] = "chroot(path)\n\nChange root directory to path."
2057Change root directory to path.")static char posix_chroot__doc__[] = "chroot(path)\n\nChange root directory to path.";
2058
2059static PyObject *
2060posix_chroot(PyObject *self, PyObject *args)
2061{
2062 return posix_1str(args, "O&:chroot", chroot);
2063}
2064#endif
2065
2066#ifdef HAVE_FSYNC1
2067PyDoc_STRVAR(posix_fsync__doc__,static char posix_fsync__doc__[] = "fsync(fildes)\n\nforce write of file with filedescriptor to disk."
2068"fsync(fildes)\n\n\static char posix_fsync__doc__[] = "fsync(fildes)\n\nforce write of file with filedescriptor to disk."
2069force write of file with filedescriptor to disk.")static char posix_fsync__doc__[] = "fsync(fildes)\n\nforce write of file with filedescriptor to disk.";
2070
2071static PyObject *
2072posix_fsync(PyObject *self, PyObject *fdobj)
2073{
2074 return posix_fildes(fdobj, fsync);
2075}
2076#endif /* HAVE_FSYNC */
2077
2078#ifdef HAVE_FDATASYNC
2079
2080#ifdef __hpux
2081extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2082#endif
2083
2084PyDoc_STRVAR(posix_fdatasync__doc__,static char posix_fdatasync__doc__[] = "fdatasync(fildes)\n\nforce write of file with filedescriptor to disk.\n does not force update of metadata."
2085"fdatasync(fildes)\n\n\static char posix_fdatasync__doc__[] = "fdatasync(fildes)\n\nforce write of file with filedescriptor to disk.\n does not force update of metadata."
2086force write of file with filedescriptor to disk.\n\static char posix_fdatasync__doc__[] = "fdatasync(fildes)\n\nforce write of file with filedescriptor to disk.\n does not force update of metadata."
2087 does not force update of metadata.")static char posix_fdatasync__doc__[] = "fdatasync(fildes)\n\nforce write of file with filedescriptor to disk.\n does not force update of metadata.";
2088
2089static PyObject *
2090posix_fdatasync(PyObject *self, PyObject *fdobj)
2091{
2092 return posix_fildes(fdobj, fdatasync);
2093}
2094#endif /* HAVE_FDATASYNC */
2095
2096
2097#ifdef HAVE_CHOWN1
2098PyDoc_STRVAR(posix_chown__doc__,static char posix_chown__doc__[] = "chown(path, uid, gid)\n\nChange the owner and group id of path to the numeric uid and gid."
2099"chown(path, uid, gid)\n\n\static char posix_chown__doc__[] = "chown(path, uid, gid)\n\nChange the owner and group id of path to the numeric uid and gid."
2100Change the owner and group id of path to the numeric uid and gid.")static char posix_chown__doc__[] = "chown(path, uid, gid)\n\nChange the owner and group id of path to the numeric uid and gid.";
2101
2102static PyObject *
2103posix_chown(PyObject *self, PyObject *args)
2104{
2105 PyObject *opath;
2106 char *path;
2107 long uid, gid;
2108 int res;
2109 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&ll:chown",
2110 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath,
2111 &uid, &gid))
2112 return NULL((void *)0);
2113 path = PyBytes_AsString(opath);
2114 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2115 res = chown(path, (uid_t) uid, (gid_t) gid);
2116 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2117 if (res < 0)
2118 return posix_error_with_allocated_filename(opath);
2119 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2119, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
2120 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
2121 return Py_None(&_Py_NoneStruct);
2122}
2123#endif /* HAVE_CHOWN */
2124
2125#ifdef HAVE_FCHOWN1
2126PyDoc_STRVAR(posix_fchown__doc__,static char posix_fchown__doc__[] = "fchown(fd, uid, gid)\n\nChange the owner and group id of the file given by file descriptor\nfd to the numeric uid and gid."
2127"fchown(fd, uid, gid)\n\n\static char posix_fchown__doc__[] = "fchown(fd, uid, gid)\n\nChange the owner and group id of the file given by file descriptor\nfd to the numeric uid and gid."
2128Change the owner and group id of the file given by file descriptor\n\static char posix_fchown__doc__[] = "fchown(fd, uid, gid)\n\nChange the owner and group id of the file given by file descriptor\nfd to the numeric uid and gid."
2129fd to the numeric uid and gid.")static char posix_fchown__doc__[] = "fchown(fd, uid, gid)\n\nChange the owner and group id of the file given by file descriptor\nfd to the numeric uid and gid.";
2130
2131static PyObject *
2132posix_fchown(PyObject *self, PyObject *args)
2133{
2134 int fd;
2135 long uid, gid;
2136 int res;
2137 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ill:chown", &fd, &uid, &gid))
2138 return NULL((void *)0);
2139 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2140 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2141 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2142 if (res < 0)
2143 return posix_error();
2144 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2145}
2146#endif /* HAVE_FCHOWN */
2147
2148#ifdef HAVE_LCHOWN1
2149PyDoc_STRVAR(posix_lchown__doc__,static char posix_lchown__doc__[] = "lchown(path, uid, gid)\n\nChange the owner and group id of path to the numeric uid and gid.\nThis function will not follow symbolic links."
2150"lchown(path, uid, gid)\n\n\static char posix_lchown__doc__[] = "lchown(path, uid, gid)\n\nChange the owner and group id of path to the numeric uid and gid.\nThis function will not follow symbolic links."
2151Change the owner and group id of path to the numeric uid and gid.\n\static char posix_lchown__doc__[] = "lchown(path, uid, gid)\n\nChange the owner and group id of path to the numeric uid and gid.\nThis function will not follow symbolic links."
2152This function will not follow symbolic links.")static char posix_lchown__doc__[] = "lchown(path, uid, gid)\n\nChange the owner and group id of path to the numeric uid and gid.\nThis function will not follow symbolic links.";
2153
2154static PyObject *
2155posix_lchown(PyObject *self, PyObject *args)
2156{
2157 PyObject *opath;
2158 char *path;
2159 long uid, gid;
2160 int res;
2161 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&ll:lchown",
2162 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath,
2163 &uid, &gid))
2164 return NULL((void *)0);
2165 path = PyBytes_AsString(opath);
2166 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2167 res = lchown(path, (uid_t) uid, (gid_t) gid);
2168 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2169 if (res < 0)
2170 return posix_error_with_allocated_filename(opath);
2171 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2171, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
2172 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
2173 return Py_None(&_Py_NoneStruct);
2174}
2175#endif /* HAVE_LCHOWN */
2176
2177
2178#ifdef HAVE_GETCWD1
2179static PyObject *
2180posix_getcwd(int use_bytes)
2181{
2182 char buf[1026];
2183 char *res;
2184
2185#ifdef MS_WINDOWS
2186 if (!use_bytes) {
2187 wchar_t wbuf[1026];
2188 wchar_t *wbuf2 = wbuf;
2189 PyObject *resobj;
2190 DWORD len;
2191 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2192 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2193 /* If the buffer is large enough, len does not include the
2194 terminating \0. If the buffer is too small, len includes
2195 the space needed for the terminator. */
2196 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2197 wbuf2 = malloc(len * sizeof(wchar_t));
2198 if (wbuf2)
2199 len = GetCurrentDirectoryW(len, wbuf2);
2200 }
2201 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2202 if (!wbuf2) {
2203 PyErr_NoMemory();
2204 return NULL((void *)0);
2205 }
2206 if (!len) {
2207 if (wbuf2 != wbuf) free(wbuf2);
2208 return win32_error("getcwdu", NULL((void *)0));
2209 }
2210 resobj = PyUnicode_FromWideCharPyUnicodeUCS2_FromWideChar(wbuf2, len);
2211 if (wbuf2 != wbuf) free(wbuf2);
2212 return resobj;
2213 }
2214#endif
2215
2216 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2217#if defined(PYOS_OS2) && defined(PYCC_GCC)
2218 res = _getcwd2(buf, sizeof buf);
2219#else
2220 res = getcwd(buf, sizeof buf);
2221#endif
2222 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2223 if (res == NULL((void *)0))
2224 return posix_error();
2225 if (use_bytes)
2226 return PyBytes_FromStringAndSize(buf, strlen(buf));
2227 return PyUnicode_DecodeFSDefaultPyUnicodeUCS2_DecodeFSDefault(buf);
2228}
2229
2230PyDoc_STRVAR(posix_getcwd__doc__,static char posix_getcwd__doc__[] = "getcwd() -> path\n\nReturn a unicode string representing the current working directory."
2231"getcwd() -> path\n\n\static char posix_getcwd__doc__[] = "getcwd() -> path\n\nReturn a unicode string representing the current working directory."
2232Return a unicode string representing the current working directory.")static char posix_getcwd__doc__[] = "getcwd() -> path\n\nReturn a unicode string representing the current working directory.";
2233
2234static PyObject *
2235posix_getcwd_unicode(PyObject *self)
2236{
2237 return posix_getcwd(0);
2238}
2239
2240PyDoc_STRVAR(posix_getcwdb__doc__,static char posix_getcwdb__doc__[] = "getcwdb() -> path\n\nReturn a bytes string representing the current working directory."
2241"getcwdb() -> path\n\n\static char posix_getcwdb__doc__[] = "getcwdb() -> path\n\nReturn a bytes string representing the current working directory."
2242Return a bytes string representing the current working directory.")static char posix_getcwdb__doc__[] = "getcwdb() -> path\n\nReturn a bytes string representing the current working directory.";
2243
2244static PyObject *
2245posix_getcwd_bytes(PyObject *self)
2246{
2247 return posix_getcwd(1);
2248}
2249#endif
2250
2251
2252#ifdef HAVE_LINK1
2253PyDoc_STRVAR(posix_link__doc__,static char posix_link__doc__[] = "link(src, dst)\n\nCreate a hard link to a file."
2254"link(src, dst)\n\n\static char posix_link__doc__[] = "link(src, dst)\n\nCreate a hard link to a file."
2255Create a hard link to a file.")static char posix_link__doc__[] = "link(src, dst)\n\nCreate a hard link to a file.";
2256
2257static PyObject *
2258posix_link(PyObject *self, PyObject *args)
2259{
2260 return posix_2str(args, "O&O&:link", link);
2261}
2262#endif /* HAVE_LINK */
2263
2264#ifdef MS_WINDOWS
2265PyDoc_STRVAR(win32_link__doc__,static char win32_link__doc__[] = "link(src, dst)\n\nCreate a hard link to a file."
2266"link(src, dst)\n\n\static char win32_link__doc__[] = "link(src, dst)\n\nCreate a hard link to a file."
2267Create a hard link to a file.")static char win32_link__doc__[] = "link(src, dst)\n\nCreate a hard link to a file.";
2268
2269static PyObject *
2270win32_link(PyObject *self, PyObject *args)
2271{
2272 PyObject *osrc, *odst;
2273 char *src, *dst;
2274 BOOL rslt;
2275
2276 PyUnicodeObject *usrc, *udst;
2277 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "UU:link", &usrc, &udst)) {
2278 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2279 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst)((__builtin_expect(!(((((((PyObject*)(udst))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 2279, "PyUnicode_Check(udst)") :
(void)0),(((PyUnicodeObject *)(udst))->str))
,
2280 PyUnicode_AS_UNICODE(usrc)((__builtin_expect(!(((((((PyObject*)(usrc))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 2280, "PyUnicode_Check(usrc)") :
(void)0),(((PyUnicodeObject *)(usrc))->str))
, NULL((void *)0));
2281 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2282
2283 if (rslt == 0)
2284 return win32_error("link", NULL((void *)0));
2285
2286 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2287 }
2288
2289 /* Narrow strings also valid. */
2290 PyErr_Clear();
2291
2292 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&O&:link", PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &osrc,
2293 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &odst))
2294 return NULL((void *)0);
2295
2296 src = PyBytes_AsString(osrc);
2297 dst = PyBytes_AsString(odst);
2298
2299 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2300 rslt = CreateHardLinkA(dst, src, NULL((void *)0));
2301 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2302
2303 Py_DECREF(osrc)do { if (_Py_RefTotal-- , --((PyObject*)(osrc))->ob_refcnt
!= 0) { if (((PyObject*)osrc)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2303, (PyObject *)(osrc)); } else
_Py_Dealloc((PyObject *)(osrc)); } while (0)
;
2304 Py_DECREF(odst)do { if (_Py_RefTotal-- , --((PyObject*)(odst))->ob_refcnt
!= 0) { if (((PyObject*)odst)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2304, (PyObject *)(odst)); } else
_Py_Dealloc((PyObject *)(odst)); } while (0)
;
2305 if (rslt == 0)
2306 return win32_error("link", NULL((void *)0));
2307
2308 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2309}
2310#endif /* MS_WINDOWS */
2311
2312
2313PyDoc_STRVAR(posix_listdir__doc__,static char posix_listdir__doc__[] = "listdir([path]) -> list_of_strings\n\nReturn a list containing the names of the entries in the directory.\n\n path: path of directory to list (default: '.')\n\nThe list is in arbitrary order. It does not include the special\nentries '.' and '..' even if they are present in the directory."
2314"listdir([path]) -> list_of_strings\n\n\static char posix_listdir__doc__[] = "listdir([path]) -> list_of_strings\n\nReturn a list containing the names of the entries in the directory.\n\n path: path of directory to list (default: '.')\n\nThe list is in arbitrary order. It does not include the special\nentries '.' and '..' even if they are present in the directory."
2315Return a list containing the names of the entries in the directory.\n\static char posix_listdir__doc__[] = "listdir([path]) -> list_of_strings\n\nReturn a list containing the names of the entries in the directory.\n\n path: path of directory to list (default: '.')\n\nThe list is in arbitrary order. It does not include the special\nentries '.' and '..' even if they are present in the directory."
2316\n\static char posix_listdir__doc__[] = "listdir([path]) -> list_of_strings\n\nReturn a list containing the names of the entries in the directory.\n\n path: path of directory to list (default: '.')\n\nThe list is in arbitrary order. It does not include the special\nentries '.' and '..' even if they are present in the directory."
2317 path: path of directory to list (default: '.')\n\static char posix_listdir__doc__[] = "listdir([path]) -> list_of_strings\n\nReturn a list containing the names of the entries in the directory.\n\n path: path of directory to list (default: '.')\n\nThe list is in arbitrary order. It does not include the special\nentries '.' and '..' even if they are present in the directory."
2318\n\static char posix_listdir__doc__[] = "listdir([path]) -> list_of_strings\n\nReturn a list containing the names of the entries in the directory.\n\n path: path of directory to list (default: '.')\n\nThe list is in arbitrary order. It does not include the special\nentries '.' and '..' even if they are present in the directory."
2319The list is in arbitrary order. It does not include the special\n\static char posix_listdir__doc__[] = "listdir([path]) -> list_of_strings\n\nReturn a list containing the names of the entries in the directory.\n\n path: path of directory to list (default: '.')\n\nThe list is in arbitrary order. It does not include the special\nentries '.' and '..' even if they are present in the directory."
2320entries '.' and '..' even if they are present in the directory.")static char posix_listdir__doc__[] = "listdir([path]) -> list_of_strings\n\nReturn a list containing the names of the entries in the directory.\n\n path: path of directory to list (default: '.')\n\nThe list is in arbitrary order. It does not include the special\nentries '.' and '..' even if they are present in the directory.";
2321
2322static PyObject *
2323posix_listdir(PyObject *self, PyObject *args)
2324{
2325 /* XXX Should redo this putting the (now four) versions of opendir
2326 in separate files instead of having them all here... */
2327#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR1)
2328
2329 PyObject *d, *v;
2330 HANDLE hFindFile;
2331 BOOL result;
2332 WIN32_FIND_DATA FileData;
2333 PyObject *opath;
2334 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2335 char *bufptr = namebuf;
2336 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
2337
2338 PyObject *po = NULL((void *)0);
2339 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|U:listdir", &po)) {
2340 WIN32_FIND_DATAW wFileData;
2341 Py_UNICODE *wnamebuf, *po_wchars;
2342
2343 if (po == NULL((void *)0)) { /* Default arg: "." */
2344 po_wchars = L".";
2345 len = 1;
2346 } else {
2347 po_wchars = PyUnicode_AS_UNICODE(po)((__builtin_expect(!(((((((PyObject*)(po))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 2347, "PyUnicode_Check(po)") : (
void)0),(((PyUnicodeObject *)(po))->str))
;
2348 len = PyUnicode_GET_SIZE(po)((__builtin_expect(!(((((((PyObject*)(po))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 2348, "PyUnicode_Check(po)") : (
void)0),(((PyUnicodeObject *)(po))->length))
;
2349 }
2350 /* Overallocate for \\*.*\0 */
2351 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2352 if (!wnamebuf) {
2353 PyErr_NoMemory();
2354 return NULL((void *)0);
2355 }
2356 wcscpy(wnamebuf, po_wchars);
2357 if (len > 0) {
2358 Py_UNICODE wch = wnamebuf[len-1];
2359 if (wch != L'/' && wch != L'\\' && wch != L':')
2360 wnamebuf[len++] = L'\\';
2361 wcscpy(wnamebuf + len, L"*.*");
2362 }
2363 if ((d = PyList_New(0)) == NULL((void *)0)) {
2364 free(wnamebuf);
2365 return NULL((void *)0);
2366 }
2367 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2368 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2369 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2370 if (hFindFile == INVALID_HANDLE_VALUE) {
2371 int error = GetLastError();
2372 if (error == ERROR_FILE_NOT_FOUND) {
2373 free(wnamebuf);
2374 return d;
2375 }
2376 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2376, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2377 win32_error_unicode("FindFirstFileW", wnamebuf);
2378 free(wnamebuf);
2379 return NULL((void *)0);
2380 }
2381 do {
2382 /* Skip over . and .. */
2383 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2384 wcscmp(wFileData.cFileName, L"..") != 0) {
2385 v = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2386 if (v == NULL((void *)0)) {
2387 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2387, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2388 d = NULL((void *)0);
2389 break;
2390 }
2391 if (PyList_Append(d, v) != 0) {
2392 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2392, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
2393 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2393, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2394 d = NULL((void *)0);
2395 break;
2396 }
2397 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2397, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
2398 }
2399 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2400 result = FindNextFileW(hFindFile, &wFileData);
2401 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2402 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2403 it got to the end of the directory. */
2404 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2405 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2405, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2406 win32_error_unicode("FindNextFileW", wnamebuf);
2407 FindClose(hFindFile);
2408 free(wnamebuf);
2409 return NULL((void *)0);
2410 }
2411 } while (result == TRUE);
2412
2413 if (FindClose(hFindFile) == FALSE) {
2414 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2414, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2415 win32_error_unicode("FindClose", wnamebuf);
2416 free(wnamebuf);
2417 return NULL((void *)0);
2418 }
2419 free(wnamebuf);
2420 return d;
2421 }
2422 /* Drop the argument parsing error as narrow strings
2423 are also valid. */
2424 PyErr_Clear();
2425
2426 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&:listdir",
2427 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath))
2428 return NULL((void *)0);
2429 if (PyBytes_GET_SIZE(opath)((__builtin_expect(!(((((((PyObject*)(opath))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 2429, "PyBytes_Check(opath)") : (
void)0),(((PyVarObject*)(opath))->ob_size))
+1 > MAX_PATH) {
2430 PyErr_SetString(PyExc_ValueError, "path too long");
2431 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2431, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
2432 return NULL((void *)0);
2433 }
2434 strcpy(namebuf, PyBytes_AsString(opath))((__builtin_object_size (namebuf, 0) != (size_t) -1) ? __builtin___strcpy_chk
(namebuf, PyBytes_AsString(opath), __builtin_object_size (namebuf
, 2 > 1)) : __inline_strcpy_chk (namebuf, PyBytes_AsString
(opath)))
;
2435 len = PyObject_Size(opath);
2436 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2436, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
2437 if (len > 0) {
2438 char ch = namebuf[len-1];
2439 if (ch != SEP && ch != ALTSEP && ch != ':')
2440 namebuf[len++] = '/';
2441 strcpy(namebuf + len, "*.*")((__builtin_object_size (namebuf + len, 0) != (size_t) -1) ? __builtin___strcpy_chk
(namebuf + len, "*.*", __builtin_object_size (namebuf + len,
2 > 1)) : __inline_strcpy_chk (namebuf + len, "*.*"))
;
2442 }
2443
2444 if ((d = PyList_New(0)) == NULL((void *)0))
2445 return NULL((void *)0);
2446
2447 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2448 hFindFile = FindFirstFile(namebuf, &FileData);
2449 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2450 if (hFindFile == INVALID_HANDLE_VALUE) {
2451 int error = GetLastError();
2452 if (error == ERROR_FILE_NOT_FOUND)
2453 return d;
2454 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2454, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2455 return win32_error("FindFirstFile", namebuf);
2456 }
2457 do {
2458 /* Skip over . and .. */
2459 if (strcmp(FileData.cFileName, ".") != 0 &&
2460 strcmp(FileData.cFileName, "..") != 0) {
2461 v = PyBytes_FromString(FileData.cFileName);
2462 if (v == NULL((void *)0)) {
2463 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2463, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2464 d = NULL((void *)0);
2465 break;
2466 }
2467 if (PyList_Append(d, v) != 0) {
2468 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2468, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
2469 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2469, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2470 d = NULL((void *)0);
2471 break;
2472 }
2473 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2473, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
2474 }
2475 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2476 result = FindNextFile(hFindFile, &FileData);
2477 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2478 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2479 it got to the end of the directory. */
2480 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2481 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2481, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2482 win32_error("FindNextFile", namebuf);
2483 FindClose(hFindFile);
2484 return NULL((void *)0);
2485 }
2486 } while (result == TRUE);
2487
2488 if (FindClose(hFindFile) == FALSE) {
2489 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2489, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2490 return win32_error("FindClose", namebuf);
2491 }
2492
2493 return d;
2494
2495#elif defined(PYOS_OS2)
2496
2497#ifndef MAX_PATH
2498#define MAX_PATH CCHMAXPATH
2499#endif
2500 PyObject *oname;
2501 char *name, *pt;
2502 Py_ssize_t len;
2503 PyObject *d, *v;
2504 char namebuf[MAX_PATH+5];
2505 HDIR hdir = 1;
2506 ULONG srchcnt = 1;
2507 FILEFINDBUF3 ep;
2508 APIRET rc;
2509
2510 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&:listdir",
2511 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &oname))
2512 return NULL((void *)0);
2513 name = PyBytes_AsString(oname);
2514 len = PyBytes_GET_SIZE(oname)((__builtin_expect(!(((((((PyObject*)(oname))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 2514, "PyBytes_Check(oname)") : (
void)0),(((PyVarObject*)(oname))->ob_size))
;
2515 if (len >= MAX_PATH) {
2516 Py_DECREF(oname)do { if (_Py_RefTotal-- , --((PyObject*)(oname))->ob_refcnt
!= 0) { if (((PyObject*)oname)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2516, (PyObject *)(oname)); } else
_Py_Dealloc((PyObject *)(oname)); } while (0)
;
2517 PyErr_SetString(PyExc_ValueError, "path too long");
2518 return NULL((void *)0);
2519 }
2520 strcpy(namebuf, name)((__builtin_object_size (namebuf, 0) != (size_t) -1) ? __builtin___strcpy_chk
(namebuf, name, __builtin_object_size (namebuf, 2 > 1)) :
__inline_strcpy_chk (namebuf, name))
;
2521 for (pt = namebuf; *pt; pt++)
2522 if (*pt == ALTSEP)
2523 *pt = SEP;
2524 if (namebuf[len-1] != SEP)
2525 namebuf[len++] = SEP;
2526 strcpy(namebuf + len, "*.*")((__builtin_object_size (namebuf + len, 0) != (size_t) -1) ? __builtin___strcpy_chk
(namebuf + len, "*.*", __builtin_object_size (namebuf + len,
2 > 1)) : __inline_strcpy_chk (namebuf + len, "*.*"))
;
2527
2528 if ((d = PyList_New(0)) == NULL((void *)0)) {
2529 Py_DECREF(oname)do { if (_Py_RefTotal-- , --((PyObject*)(oname))->ob_refcnt
!= 0) { if (((PyObject*)oname)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2529, (PyObject *)(oname)); } else
_Py_Dealloc((PyObject *)(oname)); } while (0)
;
2530 return NULL((void *)0);
2531 }
2532
2533 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2534 &hdir, /* Handle to Use While Search Directory */
2535 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
2536 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2537 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2538 FIL_STANDARD); /* Format of Entry (EAs or Not) */
2539
2540 if (rc != NO_ERROR) {
2541 errno(*__error()) = ENOENT2;
2542 return posix_error_with_allocated_filename(oname);
2543 }
2544
2545 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
2546 do {
2547 if (ep.achName[0] == '.'
2548 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
2549 continue; /* Skip Over "." and ".." Names */
2550
2551 strcpy(namebuf, ep.achName)((__builtin_object_size (namebuf, 0) != (size_t) -1) ? __builtin___strcpy_chk
(namebuf, ep.achName, __builtin_object_size (namebuf, 2 >
1)) : __inline_strcpy_chk (namebuf, ep.achName))
;
2552
2553 /* Leave Case of Name Alone -- In Native Form */
2554 /* (Removed Forced Lowercasing Code) */
2555
2556 v = PyBytes_FromString(namebuf);
2557 if (v == NULL((void *)0)) {
2558 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2558, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2559 d = NULL((void *)0);
2560 break;
2561 }
2562 if (PyList_Append(d, v) != 0) {
2563 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2563, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
2564 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2564, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2565 d = NULL((void *)0);
2566 break;
2567 }
2568 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2568, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
2569 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2570 }
2571
2572 Py_DECREF(oname)do { if (_Py_RefTotal-- , --((PyObject*)(oname))->ob_refcnt
!= 0) { if (((PyObject*)oname)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2572, (PyObject *)(oname)); } else
_Py_Dealloc((PyObject *)(oname)); } while (0)
;
2573 return d;
2574#else
2575 PyObject *oname;
2576 char *name;
2577 PyObject *d, *v;
2578 DIR *dirp;
2579 struct dirent *ep;
2580 int arg_is_unicode = 1;
2581
2582 errno(*__error()) = 0;
2583 /* v is never read, so it does not need to be initialized yet. */
2584 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|U:listdir", &v)) {
2585 arg_is_unicode = 0;
2586 PyErr_Clear();
2587 }
2588 oname = NULL((void *)0);
2589 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|O&:listdir", PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &oname))
2590 return NULL((void *)0);
2591 if (oname == NULL((void *)0)) { /* Default arg: "." */
2592 oname = PyBytes_FromString(".");
2593 }
2594 name = PyBytes_AsString(oname);
2595 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2596 dirp = opendir(name);
2597 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2598 if (dirp == NULL((void *)0)) {
2599 return posix_error_with_allocated_filename(oname);
2600 }
2601 if ((d = PyList_New(0)) == NULL((void *)0)) {
2602 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2603 closedir(dirp);
2604 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2605 Py_DECREF(oname)do { if (_Py_RefTotal-- , --((PyObject*)(oname))->ob_refcnt
!= 0) { if (((PyObject*)oname)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2605, (PyObject *)(oname)); } else
_Py_Dealloc((PyObject *)(oname)); } while (0)
;
2606 return NULL((void *)0);
2607 }
2608 for (;;) {
2609 errno(*__error()) = 0;
2610 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2611 ep = readdir(dirp);
2612 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2613 if (ep == NULL((void *)0)) {
2614 if (errno(*__error()) == 0) {
2615 break;
2616 } else {
2617 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2618 closedir(dirp);
2619 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2620 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2620, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2621 return posix_error_with_allocated_filename(oname);
2622 }
2623 }
2624 if (ep->d_name[0] == '.' &&
2625 (NAMLEN(ep)strlen((ep)->d_name) == 1 ||
2626 (ep->d_name[1] == '.' && NAMLEN(ep)strlen((ep)->d_name) == 2)))
2627 continue;
2628 if (arg_is_unicode)
2629 v = PyUnicode_DecodeFSDefaultAndSizePyUnicodeUCS2_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)strlen((ep)->d_name));
2630 else
2631 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)strlen((ep)->d_name));
2632 if (v == NULL((void *)0)) {
2633 Py_CLEAR(d)do { if (d) { PyObject *_py_tmp = (PyObject *)(d); (d) = ((void
*)0); do { if (_Py_RefTotal-- , --((PyObject*)(_py_tmp))->
ob_refcnt != 0) { if (((PyObject*)_py_tmp)->ob_refcnt <
0) _Py_NegativeRefcount("./Modules/posixmodule.c", 2633, (PyObject
*)(_py_tmp)); } else _Py_Dealloc((PyObject *)(_py_tmp)); } while
(0); } } while (0)
;
2634 break;
2635 }
2636 if (PyList_Append(d, v) != 0) {
2637 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2637, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
2638 Py_CLEAR(d)do { if (d) { PyObject *_py_tmp = (PyObject *)(d); (d) = ((void
*)0); do { if (_Py_RefTotal-- , --((PyObject*)(_py_tmp))->
ob_refcnt != 0) { if (((PyObject*)_py_tmp)->ob_refcnt <
0) _Py_NegativeRefcount("./Modules/posixmodule.c", 2638, (PyObject
*)(_py_tmp)); } else _Py_Dealloc((PyObject *)(_py_tmp)); } while
(0); } } while (0)
;
2639 break;
2640 }
2641 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2641, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
2642 }
2643 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2644 closedir(dirp);
2645 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2646 Py_DECREF(oname)do { if (_Py_RefTotal-- , --((PyObject*)(oname))->ob_refcnt
!= 0) { if (((PyObject*)oname)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2646, (PyObject *)(oname)); } else
_Py_Dealloc((PyObject *)(oname)); } while (0)
;
2647
2648 return d;
2649
2650#endif /* which OS */
2651} /* end of posix_listdir */
2652
2653#ifdef MS_WINDOWS
2654/* A helper function for abspath on win32 */
2655static PyObject *
2656posix__getfullpathname(PyObject *self, PyObject *args)
2657{
2658 PyObject *opath;
2659 char *path;
2660 char outbuf[MAX_PATH*2];
2661 char *temp;
2662#ifdef MS_WINDOWS
2663 PyUnicodeObject *po;
2664 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "U|:_getfullpathname", &po)) {
2665 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po)((__builtin_expect(!(((((((PyObject*)(po))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 2665, "PyUnicode_Check(po)") : (
void)0),(((PyUnicodeObject *)(po))->str))
;
2666 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2667 Py_UNICODE *wtemp;
2668 DWORD result;
2669 PyObject *v;
2670 result = GetFullPathNameW(wpath,
2671 sizeof(woutbuf)/sizeof(woutbuf[0]),
2672 woutbuf, &wtemp);
2673 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2674 woutbufp = malloc(result * sizeof(Py_UNICODE));
2675 if (!woutbufp)
2676 return PyErr_NoMemory();
2677 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2678 }
2679 if (result)
2680 v = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(woutbufp, wcslen(woutbufp));
2681 else
2682 v = win32_error_unicode("GetFullPathNameW", wpath);
2683 if (woutbufp != woutbuf)
2684 free(woutbufp);
2685 return v;
2686 }
2687 /* Drop the argument parsing error as narrow strings
2688 are also valid. */
2689 PyErr_Clear();
2690
2691#endif
2692 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT (args, "O&:_getfullpathname",
2693 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath))
2694 return NULL((void *)0);
2695 path = PyBytes_AsString(opath);
2696 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2697 outbuf, &temp)) {
2698 win32_error("GetFullPathName", path);
2699 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2699, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
2700 return NULL((void *)0);
2701 }
2702 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2702, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
2703 if (PyUnicode_Check(PyTuple_GetItem(args, 0))((((((PyObject*)(PyTuple_GetItem(args, 0)))->ob_type))->
tp_flags & ((1L<<28))) != 0)
) {
2704 return PyUnicode_DecodePyUnicodeUCS2_Decode(outbuf, strlen(outbuf),
2705 Py_FileSystemDefaultEncoding, NULL((void *)0));
2706 }
2707 return PyBytes_FromString(outbuf);
2708} /* end of posix__getfullpathname */
2709
2710/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
2711static int has_GetFinalPathNameByHandle = 0;
2712static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
2713 DWORD);
2714static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
2715 DWORD);
2716static int
2717check_GetFinalPathNameByHandle()
2718{
2719 HINSTANCE hKernel32;
2720 /* only recheck */
2721 if (!has_GetFinalPathNameByHandle)
2722 {
2723 hKernel32 = GetModuleHandle("KERNEL32");
2724 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
2725 "GetFinalPathNameByHandleA");
2726 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
2727 "GetFinalPathNameByHandleW");
2728 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
2729 Py_GetFinalPathNameByHandleW;
2730 }
2731 return has_GetFinalPathNameByHandle;
2732}
2733
2734/* A helper function for samepath on windows */
2735static PyObject *
2736posix__getfinalpathname(PyObject *self, PyObject *args)
2737{
2738 HANDLE hFile;
2739 int buf_size;
2740 wchar_t *target_path;
2741 int result_length;
2742 PyObject *result;
2743 wchar_t *path;
2744
2745 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "u|:_getfinalpathname", &path)) {
2746 return NULL((void *)0);
2747 }
2748
2749 if(!check_GetFinalPathNameByHandle()) {
2750 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2751 NotImplementedError. */
2752 return PyErr_Format(PyExc_NotImplementedError,
2753 "GetFinalPathNameByHandle not available on this platform");
2754 }
2755
2756 hFile = CreateFileW(
2757 path,
2758 0, /* desired access */
2759 0, /* share mode */
2760 NULL((void *)0), /* security attributes */
2761 OPEN_EXISTING,
2762 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2763 FILE_FLAG_BACKUP_SEMANTICS,
2764 NULL((void *)0));
2765
2766 if(hFile == INVALID_HANDLE_VALUE) {
2767 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2768 return PyErr_Format(PyExc_RuntimeError,
2769 "Could not get a handle to file.");
2770 }
2771
2772 /* We have a good handle to the target, use it to determine the
2773 target path name. */
2774 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2775
2776 if(!buf_size)
2777 return win32_error_unicode("GetFinalPathNameByHandle", path);
2778
2779 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2780 if(!target_path)
2781 return PyErr_NoMemory();
2782
2783 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2784 buf_size, VOLUME_NAME_DOS);
2785 if(!result_length)
2786 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2787
2788 if(!CloseHandle(hFile))
2789 return win32_error_unicode("GetFinalPathNameByHandle", path);
2790
2791 target_path[result_length] = 0;
2792 result = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(target_path, result_length);
2793 free(target_path);
2794 return result;
2795
2796} /* end of posix__getfinalpathname */
2797
2798static PyObject *
2799posix__getfileinformation(PyObject *self, PyObject *args)
2800{
2801 HANDLE hFile;
2802 BY_HANDLE_FILE_INFORMATION info;
2803 int fd;
2804
2805 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:_getfileinformation", &fd))
2806 return NULL((void *)0);
2807
2808 if (!_PyVerify_fd(fd)(1))
2809 return posix_error();
2810
2811 hFile = (HANDLE)_get_osfhandle(fd);
2812 if (hFile == INVALID_HANDLE_VALUE)
2813 return posix_error();
2814
2815 if (!GetFileInformationByHandle(hFile, &info))
2816 return win32_error("_getfileinformation", NULL((void *)0));
2817
2818 return Py_BuildValue_Py_BuildValue_SizeT("iii", info.dwVolumeSerialNumber,
2819 info.nFileIndexHigh,
2820 info.nFileIndexLow);
2821}
2822#endif /* MS_WINDOWS */
2823
2824PyDoc_STRVAR(posix_mkdir__doc__,static char posix_mkdir__doc__[] = "mkdir(path [, mode=0777])\n\nCreate a directory."
2825"mkdir(path [, mode=0777])\n\n\static char posix_mkdir__doc__[] = "mkdir(path [, mode=0777])\n\nCreate a directory."
2826Create a directory.")static char posix_mkdir__doc__[] = "mkdir(path [, mode=0777])\n\nCreate a directory.";
2827
2828static PyObject *
2829posix_mkdir(PyObject *self, PyObject *args)
2830{
2831 int res;
2832 PyObject *opath;
2833 char *path;
2834 int mode = 0777;
2835
2836#ifdef MS_WINDOWS
2837 PyUnicodeObject *po;
2838 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "U|i:mkdir", &po, &mode)) {
2839 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2840 /* PyUnicode_AS_UNICODE OK without thread lock as
2841 it is a simple dereference. */
2842 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po)((__builtin_expect(!(((((((PyObject*)(po))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 2842, "PyUnicode_Check(po)") : (
void)0),(((PyUnicodeObject *)(po))->str))
, NULL((void *)0));
2843 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2844 if (!res)
2845 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)((__builtin_expect(!(((((((PyObject*)(po))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 2845, "PyUnicode_Check(po)") : (
void)0),(((PyUnicodeObject *)(po))->str))
);
2846 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
2847 return Py_None(&_Py_NoneStruct);
2848 }
2849 /* Drop the argument parsing error as narrow strings
2850 are also valid. */
2851 PyErr_Clear();
2852 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&|i:mkdir",
2853 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath, &mode))
2854 return NULL((void *)0);
2855 path = PyBytes_AsString(opath);
2856 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2857 /* PyUnicode_AS_UNICODE OK without thread lock as
2858 it is a simple dereference. */
2859 res = CreateDirectoryA(path, NULL((void *)0));
2860 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2861 if (!res) {
2862 win32_error("mkdir", path);
2863 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2863, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
2864 return NULL((void *)0);
2865 }
2866 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2866, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
2867 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
2868 return Py_None(&_Py_NoneStruct);
2869#else
2870
2871 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&|i:mkdir",
2872 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath, &mode))
2873 return NULL((void *)0);
2874 path = PyBytes_AsString(opath);
2875 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2876#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
2877 res = mkdir(path);
2878#else
2879 res = mkdir(path, mode);
2880#endif
2881 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2882 if (res < 0)
2883 return posix_error_with_allocated_filename(opath);
2884 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2884, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
2885 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
2886 return Py_None(&_Py_NoneStruct);
2887#endif
2888}
2889
2890
2891/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2892#if defined(HAVE_SYS_RESOURCE_H1)
2893#include <sys/resource.h>
2894#endif
2895
2896
2897#ifdef HAVE_NICE1
2898PyDoc_STRVAR(posix_nice__doc__,static char posix_nice__doc__[] = "nice(inc) -> new_priority\n\nDecrease the priority of process by inc and return the new priority."
2899"nice(inc) -> new_priority\n\n\static char posix_nice__doc__[] = "nice(inc) -> new_priority\n\nDecrease the priority of process by inc and return the new priority."
2900Decrease the priority of process by inc and return the new priority.")static char posix_nice__doc__[] = "nice(inc) -> new_priority\n\nDecrease the priority of process by inc and return the new priority.";
2901
2902static PyObject *
2903posix_nice(PyObject *self, PyObject *args)
2904{
2905 int increment, value;
2906
2907 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:nice", &increment))
2908 return NULL((void *)0);
2909
2910 /* There are two flavours of 'nice': one that returns the new
2911 priority (as required by almost all standards out there) and the
2912 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2913 the use of getpriority() to get the new priority.
2914
2915 If we are of the nice family that returns the new priority, we
2916 need to clear errno before the call, and check if errno is filled
2917 before calling posix_error() on a returnvalue of -1, because the
2918 -1 may be the actual new priority! */
2919
2920 errno(*__error()) = 0;
2921 value = nice(increment);
2922#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY1)
2923 if (value == 0)
2924 value = getpriority(PRIO_PROCESS0, 0);
2925#endif
2926 if (value == -1 && errno(*__error()) != 0)
2927 /* either nice() or getpriority() returned an error */
2928 return posix_error();
2929 return PyLong_FromLong((long) value);
2930}
2931#endif /* HAVE_NICE */
2932
2933PyDoc_STRVAR(posix_rename__doc__,static char posix_rename__doc__[] = "rename(old, new)\n\nRename a file or directory."
2934"rename(old, new)\n\n\static char posix_rename__doc__[] = "rename(old, new)\n\nRename a file or directory."
2935Rename a file or directory.")static char posix_rename__doc__[] = "rename(old, new)\n\nRename a file or directory.";
2936
2937static PyObject *
2938posix_rename(PyObject *self, PyObject *args)
2939{
2940#ifdef MS_WINDOWS
2941 PyObject *o1, *o2;
2942 char *p1, *p2;
2943 BOOL result;
2944 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "OO:rename", &o1, &o2))
2945 goto error;
2946 if (!convert_to_unicode(&o1))
2947 goto error;
2948 if (!convert_to_unicode(&o2)) {
2949 Py_DECREF(o1)do { if (_Py_RefTotal-- , --((PyObject*)(o1))->ob_refcnt !=
0) { if (((PyObject*)o1)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2949, (PyObject *)(o1)); } else _Py_Dealloc
((PyObject *)(o1)); } while (0)
;
2950 goto error;
2951 }
2952 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2953 result = MoveFileW(PyUnicode_AsUnicodePyUnicodeUCS2_AsUnicode(o1),
2954 PyUnicode_AsUnicodePyUnicodeUCS2_AsUnicode(o2));
2955 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2956 Py_DECREF(o1)do { if (_Py_RefTotal-- , --((PyObject*)(o1))->ob_refcnt !=
0) { if (((PyObject*)o1)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2956, (PyObject *)(o1)); } else _Py_Dealloc
((PyObject *)(o1)); } while (0)
;
2957 Py_DECREF(o2)do { if (_Py_RefTotal-- , --((PyObject*)(o2))->ob_refcnt !=
0) { if (((PyObject*)o2)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 2957, (PyObject *)(o2)); } else _Py_Dealloc
((PyObject *)(o2)); } while (0)
;
2958 if (!result)
2959 return win32_error("rename", NULL((void *)0));
2960 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
2961 return Py_None(&_Py_NoneStruct);
2962error:
2963 PyErr_Clear();
2964 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ss:rename", &p1, &p2))
2965 return NULL((void *)0);
2966 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
2967 result = MoveFileA(p1, p2);
2968 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
2969 if (!result)
2970 return win32_error("rename", NULL((void *)0));
2971 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
2972 return Py_None(&_Py_NoneStruct);
2973#else
2974 return posix_2str(args, "O&O&:rename", rename);
2975#endif
2976}
2977
2978
2979PyDoc_STRVAR(posix_rmdir__doc__,static char posix_rmdir__doc__[] = "rmdir(path)\n\nRemove a directory."
2980"rmdir(path)\n\n\static char posix_rmdir__doc__[] = "rmdir(path)\n\nRemove a directory."
2981Remove a directory.")static char posix_rmdir__doc__[] = "rmdir(path)\n\nRemove a directory.";
2982
2983static PyObject *
2984posix_rmdir(PyObject *self, PyObject *args)
2985{
2986#ifdef MS_WINDOWS
2987 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
2988#else
2989 return posix_1str(args, "O&:rmdir", rmdir);
2990#endif
2991}
2992
2993
2994PyDoc_STRVAR(posix_stat__doc__,static char posix_stat__doc__[] = "stat(path) -> stat result\n\nPerform a stat system call on the given path."
2995"stat(path) -> stat result\n\n\static char posix_stat__doc__[] = "stat(path) -> stat result\n\nPerform a stat system call on the given path."
2996Perform a stat system call on the given path.")static char posix_stat__doc__[] = "stat(path) -> stat result\n\nPerform a stat system call on the given path.";
2997
2998static PyObject *
2999posix_stat(PyObject *self, PyObject *args)
3000{
3001#ifdef MS_WINDOWS
3002 return posix_do_stat(self, args, "O&:stat", STATstat, "U:stat", win32_stat_w);
3003#else
3004 return posix_do_stat(self, args, "O&:stat", STATstat, NULL((void *)0), NULL((void *)0));
3005#endif
3006}
3007
3008
3009#ifdef HAVE_SYSTEM1
3010PyDoc_STRVAR(posix_system__doc__,static char posix_system__doc__[] = "system(command) -> exit_status\n\nExecute the command (a string) in a subshell."
3011"system(command) -> exit_status\n\n\static char posix_system__doc__[] = "system(command) -> exit_status\n\nExecute the command (a string) in a subshell."
3012Execute the command (a string) in a subshell.")static char posix_system__doc__[] = "system(command) -> exit_status\n\nExecute the command (a string) in a subshell.";
3013
3014static PyObject *
3015posix_system(PyObject *self, PyObject *args)
3016{
3017 long sts;
3018#ifdef MS_WINDOWS
3019 wchar_t *command;
3020 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "u:system", &command))
3021 return NULL((void *)0);
3022
3023 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
3024 sts = _wsystem(command);
3025 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
3026#else
3027 PyObject *command_obj;
3028 char *command;
3029 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&:system",
3030 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &command_obj))
3031 return NULL((void *)0);
3032
3033 command = PyBytes_AsString(command_obj);
3034 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
3035 sts = system(command);
3036 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
3037 Py_DECREF(command_obj)do { if (_Py_RefTotal-- , --((PyObject*)(command_obj))->ob_refcnt
!= 0) { if (((PyObject*)command_obj)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3037, (PyObject *)(command_obj));
} else _Py_Dealloc((PyObject *)(command_obj)); } while (0)
;
3038#endif
3039 return PyLong_FromLong(sts);
3040}
3041#endif
3042
3043
3044PyDoc_STRVAR(posix_umask__doc__,static char posix_umask__doc__[] = "umask(new_mask) -> old_mask\n\nSet the current numeric umask and return the previous umask."
3045"umask(new_mask) -> old_mask\n\n\static char posix_umask__doc__[] = "umask(new_mask) -> old_mask\n\nSet the current numeric umask and return the previous umask."
3046Set the current numeric umask and return the previous umask.")static char posix_umask__doc__[] = "umask(new_mask) -> old_mask\n\nSet the current numeric umask and return the previous umask.";
3047
3048static PyObject *
3049posix_umask(PyObject *self, PyObject *args)
3050{
3051 int i;
3052 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:umask", &i))
3053 return NULL((void *)0);
3054 i = (int)umask(i);
3055 if (i < 0)
3056 return posix_error();
3057 return PyLong_FromLong((long)i);
3058}
3059
3060#ifdef MS_WINDOWS
3061
3062/* override the default DeleteFileW behavior so that directory
3063symlinks can be removed with this function, the same as with
3064Unix symlinks */
3065BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3066{
3067 WIN32_FILE_ATTRIBUTE_DATA info;
3068 WIN32_FIND_DATAW find_data;
3069 HANDLE find_data_handle;
3070 int is_directory = 0;
3071 int is_link = 0;
3072
3073 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3074 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
3075
3076 /* Get WIN32_FIND_DATA structure for the path to determine if
3077 it is a symlink */
3078 if(is_directory &&
3079 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3080 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3081
3082 if(find_data_handle != INVALID_HANDLE_VALUE) {
3083 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3084 FindClose(find_data_handle);
3085 }
3086 }
3087 }
3088
3089 if (is_directory && is_link)
3090 return RemoveDirectoryW(lpFileName);
3091
3092 return DeleteFileW(lpFileName);
3093}
3094#endif /* MS_WINDOWS */
3095
3096PyDoc_STRVAR(posix_unlink__doc__,static char posix_unlink__doc__[] = "unlink(path)\n\nRemove a file (same as remove(path))."
3097"unlink(path)\n\n\static char posix_unlink__doc__[] = "unlink(path)\n\nRemove a file (same as remove(path))."
3098Remove a file (same as remove(path)).")static char posix_unlink__doc__[] = "unlink(path)\n\nRemove a file (same as remove(path)).";
3099
3100PyDoc_STRVAR(posix_remove__doc__,static char posix_remove__doc__[] = "remove(path)\n\nRemove a file (same as unlink(path))."
3101"remove(path)\n\n\static char posix_remove__doc__[] = "remove(path)\n\nRemove a file (same as unlink(path))."
3102Remove a file (same as unlink(path)).")static char posix_remove__doc__[] = "remove(path)\n\nRemove a file (same as unlink(path)).";
3103
3104static PyObject *
3105posix_unlink(PyObject *self, PyObject *args)
3106{
3107#ifdef MS_WINDOWS
3108 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3109 "U:remove", Py_DeleteFileW);
3110#else
3111 return posix_1str(args, "O&:remove", unlink);
3112#endif
3113}
3114
3115
3116#ifdef HAVE_UNAME1
3117PyDoc_STRVAR(posix_uname__doc__,static char posix_uname__doc__[] = "uname() -> (sysname, nodename, release, version, machine)\n\nReturn a tuple identifying the current operating system."
3118"uname() -> (sysname, nodename, release, version, machine)\n\n\static char posix_uname__doc__[] = "uname() -> (sysname, nodename, release, version, machine)\n\nReturn a tuple identifying the current operating system."
3119Return a tuple identifying the current operating system.")static char posix_uname__doc__[] = "uname() -> (sysname, nodename, release, version, machine)\n\nReturn a tuple identifying the current operating system.";
3120
3121static PyObject *
3122posix_uname(PyObject *self, PyObject *noargs)
3123{
3124 struct utsname u;
3125 int res;
3126
3127 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
3128 res = uname(&u);
3129 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
3130 if (res < 0)
3131 return posix_error();
3132 return Py_BuildValue_Py_BuildValue_SizeT("(sssss)",
3133 u.sysname,
3134 u.nodename,
3135 u.release,
3136 u.version,
3137 u.machine);
3138}
3139#endif /* HAVE_UNAME */
3140
3141static int
3142extract_time(PyObject *t, time_t* sec, long* usec)
3143{
3144 time_t intval;
3145 if (PyFloat_Check(t)((((PyObject*)(t))->ob_type) == (&PyFloat_Type) || PyType_IsSubtype
((((PyObject*)(t))->ob_type), (&PyFloat_Type)))
) {
3146 double tval = PyFloat_AsDouble(t);
3147 PyObject *intobj = PyNumber_Long(t);
3148 if (!intobj)
3149 return -1;
3150#if SIZEOF_TIME_T8 > SIZEOF_LONG8
3151 intval = PyLong_AsUnsignedLongLongMask(intobj);
3152#else
3153 intval = PyLong_AsLong(intobj);
3154#endif
3155 Py_DECREF(intobj)do { if (_Py_RefTotal-- , --((PyObject*)(intobj))->ob_refcnt
!= 0) { if (((PyObject*)intobj)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3155, (PyObject *)(intobj)); } else
_Py_Dealloc((PyObject *)(intobj)); } while (0)
;
3156 if (intval == -1 && PyErr_Occurred())
3157 return -1;
3158 *sec = intval;
3159 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3160 if (*usec < 0)
3161 /* If rounding gave us a negative number,
3162 truncate. */
3163 *usec = 0;
3164 return 0;
3165 }
3166#if SIZEOF_TIME_T8 > SIZEOF_LONG8
3167 intval = PyLong_AsUnsignedLongLongMask(t);
3168#else
3169 intval = PyLong_AsLong(t);
3170#endif
3171 if (intval == -1 && PyErr_Occurred())
3172 return -1;
3173 *sec = intval;
3174 *usec = 0;
3175 return 0;
3176}
3177
3178PyDoc_STRVAR(posix_utime__doc__,static char posix_utime__doc__[] = "utime(path, (atime, mtime))\nutime(path, None)\n\nSet the access and modified time of the file to the given values. If the\nsecond form is used, set the access and modified times to the current time."
3179"utime(path, (atime, mtime))\n\static char posix_utime__doc__[] = "utime(path, (atime, mtime))\nutime(path, None)\n\nSet the access and modified time of the file to the given values. If the\nsecond form is used, set the access and modified times to the current time."
3180utime(path, None)\n\n\static char posix_utime__doc__[] = "utime(path, (atime, mtime))\nutime(path, None)\n\nSet the access and modified time of the file to the given values. If the\nsecond form is used, set the access and modified times to the current time."
3181Set the access and modified time of the file to the given values. If the\n\static char posix_utime__doc__[] = "utime(path, (atime, mtime))\nutime(path, None)\n\nSet the access and modified time of the file to the given values. If the\nsecond form is used, set the access and modified times to the current time."
3182second form is used, set the access and modified times to the current time.")static char posix_utime__doc__[] = "utime(path, (atime, mtime))\nutime(path, None)\n\nSet the access and modified time of the file to the given values. If the\nsecond form is used, set the access and modified times to the current time.";
3183
3184static PyObject *
3185posix_utime(PyObject *self, PyObject *args)
3186{
3187#ifdef MS_WINDOWS
3188 PyObject *arg;
3189 PyUnicodeObject *obwpath;
3190 wchar_t *wpath = NULL((void *)0);
3191 PyObject *oapath;
3192 char *apath;
3193 HANDLE hFile;
3194 time_t atimesec, mtimesec;
3195 long ausec, musec;
3196 FILETIME atime, mtime;
3197 PyObject *result = NULL((void *)0);
3198
3199 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "UO|:utime", &obwpath, &arg)) {
3200 wpath = PyUnicode_AS_UNICODE(obwpath)((__builtin_expect(!(((((((PyObject*)(obwpath))->ob_type))
->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/posixmodule.c", 3200, "PyUnicode_Check(obwpath)"
) : (void)0),(((PyUnicodeObject *)(obwpath))->str))
;
3201 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
3202 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3203 NULL((void *)0), OPEN_EXISTING,
3204 FILE_FLAG_BACKUP_SEMANTICS, NULL((void *)0));
3205 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
3206 if (hFile == INVALID_HANDLE_VALUE)
3207 return win32_error_unicode("utime", wpath);
3208 } else
3209 /* Drop the argument parsing error as narrow strings
3210 are also valid. */
3211 PyErr_Clear();
3212
3213 if (!wpath) {
3214 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&O:utime",
3215 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &oapath, &arg))
3216 return NULL((void *)0);
3217 apath = PyBytes_AsString(oapath);
3218 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
3219 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3220 NULL((void *)0), OPEN_EXISTING,
3221 FILE_FLAG_BACKUP_SEMANTICS, NULL((void *)0));
3222 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
3223 if (hFile == INVALID_HANDLE_VALUE) {
3224 win32_error("utime", apath);
3225 Py_DECREF(oapath)do { if (_Py_RefTotal-- , --((PyObject*)(oapath))->ob_refcnt
!= 0) { if (((PyObject*)oapath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3225, (PyObject *)(oapath)); } else
_Py_Dealloc((PyObject *)(oapath)); } while (0)
;
3226 return NULL((void *)0);
3227 }
3228 Py_DECREF(oapath)do { if (_Py_RefTotal-- , --((PyObject*)(oapath))->ob_refcnt
!= 0) { if (((PyObject*)oapath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3228, (PyObject *)(oapath)); } else
_Py_Dealloc((PyObject *)(oapath)); } while (0)
;
3229 }
3230
3231 if (arg == Py_None(&_Py_NoneStruct)) {
3232 SYSTEMTIME now;
3233 GetSystemTime(&now);
3234 if (!SystemTimeToFileTime(&now, &mtime) ||
3235 !SystemTimeToFileTime(&now, &atime)) {
3236 win32_error("utime", NULL((void *)0));
3237 goto done;
3238 }
3239 }
3240 else if (!PyTuple_Check(arg)((((((PyObject*)(arg))->ob_type))->tp_flags & ((1L<<
26))) != 0)
|| PyTuple_Size(arg) != 2) {
3241 PyErr_SetString(PyExc_TypeError,
3242 "utime() arg 2 must be a tuple (atime, mtime)");
3243 goto done;
3244 }
3245 else {
3246 if (extract_time(PyTuple_GET_ITEM(arg, 0)(((PyTupleObject *)(arg))->ob_item[0]),
3247 &atimesec, &ausec) == -1)
3248 goto done;
3249 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3250 if (extract_time(PyTuple_GET_ITEM(arg, 1)(((PyTupleObject *)(arg))->ob_item[1]),
3251 &mtimesec, &musec) == -1)
3252 goto done;
3253 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3254 }
3255 if (!SetFileTime(hFile, NULL((void *)0), &atime, &mtime)) {
3256 /* Avoid putting the file name into the error here,
3257 as that may confuse the user into believing that
3258 something is wrong with the file, when it also
3259 could be the time stamp that gives a problem. */
3260 win32_error("utime", NULL((void *)0));
3261 goto done;
3262 }
3263 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
3264 result = Py_None(&_Py_NoneStruct);
3265done:
3266 CloseHandle(hFile);
3267 return result;
3268#else /* MS_WINDOWS */
3269
3270 PyObject *opath;
3271 char *path;
3272 time_t atime, mtime;
3273 long ausec, musec;
3274 int res;
3275 PyObject* arg;
3276
3277#if defined(HAVE_UTIMES1)
3278 struct timeval buf[2];
3279#define ATIME buf[0].tv_sec
3280#define MTIME buf[1].tv_sec
3281#elif defined(HAVE_UTIME_H1)
3282/* XXX should define struct utimbuf instead, above */
3283 struct utimbuf buf;
3284#define ATIME buf.actime
3285#define MTIME buf.modtime
3286#define UTIME_ARG &buf
3287#else /* HAVE_UTIMES */
3288 time_t buf[2];
3289#define ATIME buf[0]
3290#define MTIME buf[1]
3291#define UTIME_ARG buf
3292#endif /* HAVE_UTIMES */
3293
3294
3295 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&O:utime",
3296 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath, &arg))
3297 return NULL((void *)0);
3298 path = PyBytes_AsString(opath);
3299 if (arg == Py_None(&_Py_NoneStruct)) {
3300 /* optional time values not given */
3301 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
3302 res = utime(path, NULL((void *)0));
3303 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
3304 }
3305 else if (!PyTuple_Check(arg)((((((PyObject*)(arg))->ob_type))->tp_flags & ((1L<<
26))) != 0)
|| PyTuple_Size(arg) != 2) {
3306 PyErr_SetString(PyExc_TypeError,
3307 "utime() arg 2 must be a tuple (atime, mtime)");
3308 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3308, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3309 return NULL((void *)0);
3310 }
3311 else {
3312 if (extract_time(PyTuple_GET_ITEM(arg, 0)(((PyTupleObject *)(arg))->ob_item[0]),
3313 &atime, &ausec) == -1) {
3314 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3314, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3315 return NULL((void *)0);
3316 }
3317 if (extract_time(PyTuple_GET_ITEM(arg, 1)(((PyTupleObject *)(arg))->ob_item[1]),
3318 &mtime, &musec) == -1) {
3319 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3319, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3320 return NULL((void *)0);
3321 }
3322 ATIME = atime;
3323 MTIME = mtime;
3324#ifdef HAVE_UTIMES1
3325 buf[0].tv_usec = ausec;
3326 buf[1].tv_usec = musec;
3327 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
3328 res = utimes(path, buf);
3329 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
3330#else
3331 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
3332 res = utime(path, UTIME_ARG);
3333 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
3334#endif /* HAVE_UTIMES */
3335 }
3336 if (res < 0) {
3337 return posix_error_with_allocated_filename(opath);
3338 }
3339 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3339, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3340 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
3341 return Py_None(&_Py_NoneStruct);
3342#undef UTIME_ARG
3343#undef ATIME
3344#undef MTIME
3345#endif /* MS_WINDOWS */
3346}
3347
3348
3349/* Process operations */
3350
3351PyDoc_STRVAR(posix__exit__doc__,static char posix__exit__doc__[] = "_exit(status)\n\nExit to the system with specified status, without normal exit processing."
3352"_exit(status)\n\n\static char posix__exit__doc__[] = "_exit(status)\n\nExit to the system with specified status, without normal exit processing."
3353Exit to the system with specified status, without normal exit processing.")static char posix__exit__doc__[] = "_exit(status)\n\nExit to the system with specified status, without normal exit processing.";
3354
3355static PyObject *
3356posix__exit(PyObject *self, PyObject *args)
3357{
3358 int sts;
3359 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:_exit", &sts))
3360 return NULL((void *)0);
3361 _exit(sts);
3362 return NULL((void *)0); /* Make gcc -Wall happy */
3363}
3364
3365#if defined(HAVE_EXECV1) || defined(HAVE_SPAWNV)
3366static void
3367free_string_array(char **array, Py_ssize_t count)
3368{
3369 Py_ssize_t i;
3370 for (i = 0; i < count; i++)
3371 PyMem_Free(array[i]);
3372 PyMem_DEL_PyMem_DebugFree(array);
3373}
3374
3375static
3376int fsconvert_strdup(PyObject *o, char**out)
3377{
3378 PyObject *bytes;
3379 Py_ssize_t size;
3380 if (!PyUnicode_FSConverterPyUnicodeUCS2_FSConverter(o, &bytes))
3381 return 0;
3382 size = PyBytes_GET_SIZE(bytes)((__builtin_expect(!(((((((PyObject*)(bytes))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 3382, "PyBytes_Check(bytes)") : (
void)0),(((PyVarObject*)(bytes))->ob_size))
;
3383 *out = PyMem_Malloc(size+1);
3384 if (!*out)
3385 return 0;
3386 memcpy(*out, PyBytes_AsString(bytes), size+1)((__builtin_object_size (*out, 0) != (size_t) -1) ? __builtin___memcpy_chk
(*out, PyBytes_AsString(bytes), size+1, __builtin_object_size
(*out, 0)) : __inline_memcpy_chk (*out, PyBytes_AsString(bytes
), size+1))
;
3387 Py_DECREF(bytes)do { if (_Py_RefTotal-- , --((PyObject*)(bytes))->ob_refcnt
!= 0) { if (((PyObject*)bytes)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3387, (PyObject *)(bytes)); } else
_Py_Dealloc((PyObject *)(bytes)); } while (0)
;
3388 return 1;
3389}
3390#endif
3391
3392
3393#ifdef HAVE_EXECV1
3394PyDoc_STRVAR(posix_execv__doc__,static char posix_execv__doc__[] = "execv(path, args)\n\nExecute an executable path with arguments, replacing current process.\n\n path: path of executable file\n args: tuple or list of strings"
3395"execv(path, args)\n\n\static char posix_execv__doc__[] = "execv(path, args)\n\nExecute an executable path with arguments, replacing current process.\n\n path: path of executable file\n args: tuple or list of strings"
3396Execute an executable path with arguments, replacing current process.\n\static char posix_execv__doc__[] = "execv(path, args)\n\nExecute an executable path with arguments, replacing current process.\n\n path: path of executable file\n args: tuple or list of strings"
3397\n\static char posix_execv__doc__[] = "execv(path, args)\n\nExecute an executable path with arguments, replacing current process.\n\n path: path of executable file\n args: tuple or list of strings"
3398 path: path of executable file\n\static char posix_execv__doc__[] = "execv(path, args)\n\nExecute an executable path with arguments, replacing current process.\n\n path: path of executable file\n args: tuple or list of strings"
3399 args: tuple or list of strings")static char posix_execv__doc__[] = "execv(path, args)\n\nExecute an executable path with arguments, replacing current process.\n\n path: path of executable file\n args: tuple or list of strings";
3400
3401static PyObject *
3402posix_execv(PyObject *self, PyObject *args)
3403{
3404 PyObject *opath;
3405 char *path;
3406 PyObject *argv;
3407 char **argvlist;
3408 Py_ssize_t i, argc;
3409 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3410
3411 /* execv has two arguments: (path, argv), where
3412 argv is a list or tuple of strings. */
3413
3414 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&O:execv",
3415 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter,
3416 &opath, &argv))
3417 return NULL((void *)0);
3418 path = PyBytes_AsString(opath);
3419 if (PyList_Check(argv)((((((PyObject*)(argv))->ob_type))->tp_flags & ((1L
<<25))) != 0)
) {
3420 argc = PyList_Size(argv);
3421 getitem = PyList_GetItem;
3422 }
3423 else if (PyTuple_Check(argv)((((((PyObject*)(argv))->ob_type))->tp_flags & ((1L
<<26))) != 0)
) {
3424 argc = PyTuple_Size(argv);
3425 getitem = PyTuple_GetItem;
3426 }
3427 else {
3428 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3429 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3429, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3430 return NULL((void *)0);
3431 }
3432 if (argc < 1) {
3433 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3434 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3434, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3435 return NULL((void *)0);
3436 }
3437
3438 argvlist = PyMem_NEW(char *, argc+1)( ((size_t)(argc+1) > ((Py_ssize_t)(((size_t)-1)>>1)
) / sizeof(char *)) ? ((void *)0) : ( (char * *) _PyMem_DebugMalloc
((argc+1) * sizeof(char *)) ) )
;
3439 if (argvlist == NULL((void *)0)) {
3440 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3440, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3441 return PyErr_NoMemory();
3442 }
3443 for (i = 0; i < argc; i++) {
3444 if (!fsconvert_strdup((*getitem)(argv, i),
3445 &argvlist[i])) {
3446 free_string_array(argvlist, i);
3447 PyErr_SetString(PyExc_TypeError,
3448 "execv() arg 2 must contain only strings");
3449 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3449, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3450 return NULL((void *)0);
3451
3452 }
3453 }
3454 argvlist[argc] = NULL((void *)0);
3455
3456 execv(path, argvlist);
3457
3458 /* If we get here it's definitely an error */
3459
3460 free_string_array(argvlist, argc);
3461 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3461, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3462 return posix_error();
3463}
3464
3465static char**
3466parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3467{
3468 char **envlist;
3469 Py_ssize_t i, pos, envc;
3470 PyObject *keys=NULL((void *)0), *vals=NULL((void *)0);
3471 PyObject *key, *val, *key2, *val2;
3472 char *p, *k, *v;
3473 size_t len;
3474
3475 i = PyMapping_Size(env);
3476 if (i < 0)
3477 return NULL((void *)0);
3478 envlist = PyMem_NEW(char *, i + 1)( ((size_t)(i + 1) > ((Py_ssize_t)(((size_t)-1)>>1))
/ sizeof(char *)) ? ((void *)0) : ( (char * *) _PyMem_DebugMalloc
((i + 1) * sizeof(char *)) ) )
;
3479 if (envlist == NULL((void *)0)) {
3480 PyErr_NoMemory();
3481 return NULL((void *)0);
3482 }
3483 envc = 0;
3484 keys = PyMapping_Keys(env);
3485 vals = PyMapping_Values(env);
3486 if (!keys || !vals)
3487 goto error;
3488 if (!PyList_Check(keys)((((((PyObject*)(keys))->ob_type))->tp_flags & ((1L
<<25))) != 0)
|| !PyList_Check(vals)((((((PyObject*)(vals))->ob_type))->tp_flags & ((1L
<<25))) != 0)
) {
3489 PyErr_Format(PyExc_TypeError,
3490 "env.keys() or env.values() is not a list");
3491 goto error;
3492 }
3493
3494 for (pos = 0; pos < i; pos++) {
3495 key = PyList_GetItem(keys, pos);
3496 val = PyList_GetItem(vals, pos);
3497 if (!key || !val)
3498 goto error;
3499
3500 if (PyUnicode_FSConverterPyUnicodeUCS2_FSConverter(key, &key2) == 0)
3501 goto error;
3502 if (PyUnicode_FSConverterPyUnicodeUCS2_FSConverter(val, &val2) == 0) {
3503 Py_DECREF(key2)do { if (_Py_RefTotal-- , --((PyObject*)(key2))->ob_refcnt
!= 0) { if (((PyObject*)key2)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3503, (PyObject *)(key2)); } else
_Py_Dealloc((PyObject *)(key2)); } while (0)
;
3504 goto error;
3505 }
3506
3507#if defined(PYOS_OS2)
3508 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3509 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3510#endif
3511 k = PyBytes_AsString(key2);
3512 v = PyBytes_AsString(val2);
3513 len = PyBytes_GET_SIZE(key2)((__builtin_expect(!(((((((PyObject*)(key2))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 3513, "PyBytes_Check(key2)") : (
void)0),(((PyVarObject*)(key2))->ob_size))
+ PyBytes_GET_SIZE(val2)((__builtin_expect(!(((((((PyObject*)(val2))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 3513, "PyBytes_Check(val2)") : (
void)0),(((PyVarObject*)(val2))->ob_size))
+ 2;
3514
3515 p = PyMem_NEW(char, len)( ((size_t)(len) > ((Py_ssize_t)(((size_t)-1)>>1)) /
sizeof(char)) ? ((void *)0) : ( (char *) _PyMem_DebugMalloc(
(len) * sizeof(char)) ) )
;
3516 if (p == NULL((void *)0)) {
3517 PyErr_NoMemory();
3518 Py_DECREF(key2)do { if (_Py_RefTotal-- , --((PyObject*)(key2))->ob_refcnt
!= 0) { if (((PyObject*)key2)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3518, (PyObject *)(key2)); } else
_Py_Dealloc((PyObject *)(key2)); } while (0)
;
3519 Py_DECREF(val2)do { if (_Py_RefTotal-- , --((PyObject*)(val2))->ob_refcnt
!= 0) { if (((PyObject*)val2)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3519, (PyObject *)(val2)); } else
_Py_Dealloc((PyObject *)(val2)); } while (0)
;
3520 goto error;
3521 }
3522 PyOS_snprintf(p, len, "%s=%s", k, v);
3523 envlist[envc++] = p;
3524 Py_DECREF(key2)do { if (_Py_RefTotal-- , --((PyObject*)(key2))->ob_refcnt
!= 0) { if (((PyObject*)key2)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3524, (PyObject *)(key2)); } else
_Py_Dealloc((PyObject *)(key2)); } while (0)
;
3525 Py_DECREF(val2)do { if (_Py_RefTotal-- , --((PyObject*)(val2))->ob_refcnt
!= 0) { if (((PyObject*)val2)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3525, (PyObject *)(val2)); } else
_Py_Dealloc((PyObject *)(val2)); } while (0)
;
3526#if defined(PYOS_OS2)
3527 }
3528#endif
3529 }
3530 Py_DECREF(vals)do { if (_Py_RefTotal-- , --((PyObject*)(vals))->ob_refcnt
!= 0) { if (((PyObject*)vals)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3530, (PyObject *)(vals)); } else
_Py_Dealloc((PyObject *)(vals)); } while (0)
;
3531 Py_DECREF(keys)do { if (_Py_RefTotal-- , --((PyObject*)(keys))->ob_refcnt
!= 0) { if (((PyObject*)keys)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3531, (PyObject *)(keys)); } else
_Py_Dealloc((PyObject *)(keys)); } while (0)
;
3532
3533 envlist[envc] = 0;
3534 *envc_ptr = envc;
3535 return envlist;
3536
3537error:
3538 Py_XDECREF(keys)do { if ((keys) == ((void *)0)) ; else do { if (_Py_RefTotal--
, --((PyObject*)(keys))->ob_refcnt != 0) { if (((PyObject
*)keys)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/posixmodule.c"
, 3538, (PyObject *)(keys)); } else _Py_Dealloc((PyObject *)(
keys)); } while (0); } while (0)
;
3539 Py_XDECREF(vals)do { if ((vals) == ((void *)0)) ; else do { if (_Py_RefTotal--
, --((PyObject*)(vals))->ob_refcnt != 0) { if (((PyObject
*)vals)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/posixmodule.c"
, 3539, (PyObject *)(vals)); } else _Py_Dealloc((PyObject *)(
vals)); } while (0); } while (0)
;
3540 while (--envc >= 0)
3541 PyMem_DEL_PyMem_DebugFree(envlist[envc]);
3542 PyMem_DEL_PyMem_DebugFree(envlist);
3543 return NULL((void *)0);
3544}
3545
3546PyDoc_STRVAR(posix_execve__doc__,static char posix_execve__doc__[] = "execve(path, args, env)\n\nExecute a path with arguments and environment, replacing current process.\n\n path: path of executable file\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3547"execve(path, args, env)\n\n\static char posix_execve__doc__[] = "execve(path, args, env)\n\nExecute a path with arguments and environment, replacing current process.\n\n path: path of executable file\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3548Execute a path with arguments and environment, replacing current process.\n\static char posix_execve__doc__[] = "execve(path, args, env)\n\nExecute a path with arguments and environment, replacing current process.\n\n path: path of executable file\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3549\n\static char posix_execve__doc__[] = "execve(path, args, env)\n\nExecute a path with arguments and environment, replacing current process.\n\n path: path of executable file\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3550 path: path of executable file\n\static char posix_execve__doc__[] = "execve(path, args, env)\n\nExecute a path with arguments and environment, replacing current process.\n\n path: path of executable file\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3551 args: tuple or list of arguments\n\static char posix_execve__doc__[] = "execve(path, args, env)\n\nExecute a path with arguments and environment, replacing current process.\n\n path: path of executable file\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3552 env: dictionary of strings mapping to strings")static char posix_execve__doc__[] = "execve(path, args, env)\n\nExecute a path with arguments and environment, replacing current process.\n\n path: path of executable file\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings";
3553
3554static PyObject *
3555posix_execve(PyObject *self, PyObject *args)
3556{
3557 PyObject *opath;
3558 char *path;
3559 PyObject *argv, *env;
3560 char **argvlist;
3561 char **envlist;
3562 Py_ssize_t i, argc, envc;
3563 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3564 Py_ssize_t lastarg = 0;
3565
3566 /* execve has three arguments: (path, argv, env), where
3567 argv is a list or tuple of strings and env is a dictionary
3568 like posix.environ. */
3569
3570 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&OO:execve",
3571 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter,
3572 &opath, &argv, &env))
3573 return NULL((void *)0);
3574 path = PyBytes_AsString(opath);
3575 if (PyList_Check(argv)((((((PyObject*)(argv))->ob_type))->tp_flags & ((1L
<<25))) != 0)
) {
3576 argc = PyList_Size(argv);
3577 getitem = PyList_GetItem;
3578 }
3579 else if (PyTuple_Check(argv)((((((PyObject*)(argv))->ob_type))->tp_flags & ((1L
<<26))) != 0)
) {
3580 argc = PyTuple_Size(argv);
3581 getitem = PyTuple_GetItem;
3582 }
3583 else {
3584 PyErr_SetString(PyExc_TypeError,
3585 "execve() arg 2 must be a tuple or list");
3586 goto fail_0;
3587 }
3588 if (!PyMapping_Check(env)) {
3589 PyErr_SetString(PyExc_TypeError,
3590 "execve() arg 3 must be a mapping object");
3591 goto fail_0;
3592 }
3593
3594 argvlist = PyMem_NEW(char *, argc+1)( ((size_t)(argc+1) > ((Py_ssize_t)(((size_t)-1)>>1)
) / sizeof(char *)) ? ((void *)0) : ( (char * *) _PyMem_DebugMalloc
((argc+1) * sizeof(char *)) ) )
;
3595 if (argvlist == NULL((void *)0)) {
3596 PyErr_NoMemory();
3597 goto fail_0;
3598 }
3599 for (i = 0; i < argc; i++) {
3600 if (!fsconvert_strdup((*getitem)(argv, i),
3601 &argvlist[i]))
3602 {
3603 lastarg = i;
3604 goto fail_1;
3605 }
3606 }
3607 lastarg = argc;
3608 argvlist[argc] = NULL((void *)0);
3609
3610 envlist = parse_envlist(env, &envc);
3611 if (envlist == NULL((void *)0))
3612 goto fail_1;
3613
3614 execve(path, argvlist, envlist);
3615
3616 /* If we get here it's definitely an error */
3617
3618 (void) posix_error();
3619
3620 while (--envc >= 0)
3621 PyMem_DEL_PyMem_DebugFree(envlist[envc]);
3622 PyMem_DEL_PyMem_DebugFree(envlist);
3623 fail_1:
3624 free_string_array(argvlist, lastarg);
3625 fail_0:
3626 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3626, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3627 return NULL((void *)0);
3628}
3629#endif /* HAVE_EXECV */
3630
3631
3632#ifdef HAVE_SPAWNV
3633PyDoc_STRVAR(posix_spawnv__doc__,static char posix_spawnv__doc__[] = "spawnv(mode, path, args)\n\nExecute the program 'path' in a new process.\n\n mode: mode of process creation\n path: path of executable file\n args: tuple or list of strings"
3634"spawnv(mode, path, args)\n\n\static char posix_spawnv__doc__[] = "spawnv(mode, path, args)\n\nExecute the program 'path' in a new process.\n\n mode: mode of process creation\n path: path of executable file\n args: tuple or list of strings"
3635Execute the program 'path' in a new process.\n\static char posix_spawnv__doc__[] = "spawnv(mode, path, args)\n\nExecute the program 'path' in a new process.\n\n mode: mode of process creation\n path: path of executable file\n args: tuple or list of strings"
3636\n\static char posix_spawnv__doc__[] = "spawnv(mode, path, args)\n\nExecute the program 'path' in a new process.\n\n mode: mode of process creation\n path: path of executable file\n args: tuple or list of strings"
3637 mode: mode of process creation\n\static char posix_spawnv__doc__[] = "spawnv(mode, path, args)\n\nExecute the program 'path' in a new process.\n\n mode: mode of process creation\n path: path of executable file\n args: tuple or list of strings"
3638 path: path of executable file\n\static char posix_spawnv__doc__[] = "spawnv(mode, path, args)\n\nExecute the program 'path' in a new process.\n\n mode: mode of process creation\n path: path of executable file\n args: tuple or list of strings"
3639 args: tuple or list of strings")static char posix_spawnv__doc__[] = "spawnv(mode, path, args)\n\nExecute the program 'path' in a new process.\n\n mode: mode of process creation\n path: path of executable file\n args: tuple or list of strings";
3640
3641static PyObject *
3642posix_spawnv(PyObject *self, PyObject *args)
3643{
3644 PyObject *opath;
3645 char *path;
3646 PyObject *argv;
3647 char **argvlist;
3648 int mode, i;
3649 Py_ssize_t argc;
3650 Py_intptr_t spawnval;
3651 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3652
3653 /* spawnv has three arguments: (mode, path, argv), where
3654 argv is a list or tuple of strings. */
3655
3656 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "iO&O:spawnv", &mode,
3657 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter,
3658 &opath, &argv))
3659 return NULL((void *)0);
3660 path = PyBytes_AsString(opath);
3661 if (PyList_Check(argv)((((((PyObject*)(argv))->ob_type))->tp_flags & ((1L
<<25))) != 0)
) {
3662 argc = PyList_Size(argv);
3663 getitem = PyList_GetItem;
3664 }
3665 else if (PyTuple_Check(argv)((((((PyObject*)(argv))->ob_type))->tp_flags & ((1L
<<26))) != 0)
) {
3666 argc = PyTuple_Size(argv);
3667 getitem = PyTuple_GetItem;
3668 }
3669 else {
3670 PyErr_SetString(PyExc_TypeError,
3671 "spawnv() arg 2 must be a tuple or list");
3672 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3672, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3673 return NULL((void *)0);
3674 }
3675
3676 argvlist = PyMem_NEW(char *, argc+1)( ((size_t)(argc+1) > ((Py_ssize_t)(((size_t)-1)>>1)
) / sizeof(char *)) ? ((void *)0) : ( (char * *) _PyMem_DebugMalloc
((argc+1) * sizeof(char *)) ) )
;
3677 if (argvlist == NULL((void *)0)) {
3678 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3678, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3679 return PyErr_NoMemory();
3680 }
3681 for (i = 0; i < argc; i++) {
3682 if (!fsconvert_strdup((*getitem)(argv, i),
3683 &argvlist[i])) {
3684 free_string_array(argvlist, i);
3685 PyErr_SetString(
3686 PyExc_TypeError,
3687 "spawnv() arg 2 must contain only strings");
3688 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3688, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3689 return NULL((void *)0);
3690 }
3691 }
3692 argvlist[argc] = NULL((void *)0);
3693
3694#if defined(PYOS_OS2) && defined(PYCC_GCC)
3695 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
3696 spawnval = spawnv(mode, path, argvlist);
3697 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
3698#else
3699 if (mode == _OLD_P_OVERLAY)
3700 mode = _P_OVERLAY;
3701
3702 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
3703 spawnval = _spawnv(mode, path, argvlist);
3704 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
3705#endif
3706
3707 free_string_array(argvlist, argc);
3708 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3708, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3709
3710 if (spawnval == -1)
3711 return posix_error();
3712 else
3713#if SIZEOF_LONG8 == SIZEOF_VOID_P8
3714 return Py_BuildValue_Py_BuildValue_SizeT("l", (long) spawnval);
3715#else
3716 return Py_BuildValue_Py_BuildValue_SizeT("L", (PY_LONG_LONGlong long) spawnval);
3717#endif
3718}
3719
3720
3721PyDoc_STRVAR(posix_spawnve__doc__,static char posix_spawnve__doc__[] = "spawnve(mode, path, args, env)\n\nExecute the program 'path' in a new process.\n\n mode: mode of process creation\n path: path of executable file\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3722"spawnve(mode, path, args, env)\n\n\static char posix_spawnve__doc__[] = "spawnve(mode, path, args, env)\n\nExecute the program 'path' in a new process.\n\n mode: mode of process creation\n path: path of executable file\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3723Execute the program 'path' in a new process.\n\static char posix_spawnve__doc__[] = "spawnve(mode, path, args, env)\n\nExecute the program 'path' in a new process.\n\n mode: mode of process creation\n path: path of executable file\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3724\n\static char posix_spawnve__doc__[] = "spawnve(mode, path, args, env)\n\nExecute the program 'path' in a new process.\n\n mode: mode of process creation\n path: path of executable file\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3725 mode: mode of process creation\n\static char posix_spawnve__doc__[] = "spawnve(mode, path, args, env)\n\nExecute the program 'path' in a new process.\n\n mode: mode of process creation\n path: path of executable file\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3726 path: path of executable file\n\static char posix_spawnve__doc__[] = "spawnve(mode, path, args, env)\n\nExecute the program 'path' in a new process.\n\n mode: mode of process creation\n path: path of executable file\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3727 args: tuple or list of arguments\n\static char posix_spawnve__doc__[] = "spawnve(mode, path, args, env)\n\nExecute the program 'path' in a new process.\n\n mode: mode of process creation\n path: path of executable file\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3728 env: dictionary of strings mapping to strings")static char posix_spawnve__doc__[] = "spawnve(mode, path, args, env)\n\nExecute the program 'path' in a new process.\n\n mode: mode of process creation\n path: path of executable file\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings";
3729
3730static PyObject *
3731posix_spawnve(PyObject *self, PyObject *args)
3732{
3733 PyObject *opath;
3734 char *path;
3735 PyObject *argv, *env;
3736 char **argvlist;
3737 char **envlist;
3738 PyObject *res = NULL((void *)0);
3739 int mode;
3740 Py_ssize_t argc, i, envc;
3741 Py_intptr_t spawnval;
3742 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3743 Py_ssize_t lastarg = 0;
3744
3745 /* spawnve has four arguments: (mode, path, argv, env), where
3746 argv is a list or tuple of strings and env is a dictionary
3747 like posix.environ. */
3748
3749 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "iO&OO:spawnve", &mode,
3750 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter,
3751 &opath, &argv, &env))
3752 return NULL((void *)0);
3753 path = PyBytes_AsString(opath);
3754 if (PyList_Check(argv)((((((PyObject*)(argv))->ob_type))->tp_flags & ((1L
<<25))) != 0)
) {
3755 argc = PyList_Size(argv);
3756 getitem = PyList_GetItem;
3757 }
3758 else if (PyTuple_Check(argv)((((((PyObject*)(argv))->ob_type))->tp_flags & ((1L
<<26))) != 0)
) {
3759 argc = PyTuple_Size(argv);
3760 getitem = PyTuple_GetItem;
3761 }
3762 else {
3763 PyErr_SetString(PyExc_TypeError,
3764 "spawnve() arg 2 must be a tuple or list");
3765 goto fail_0;
3766 }
3767 if (!PyMapping_Check(env)) {
3768 PyErr_SetString(PyExc_TypeError,
3769 "spawnve() arg 3 must be a mapping object");
3770 goto fail_0;
3771 }
3772
3773 argvlist = PyMem_NEW(char *, argc+1)( ((size_t)(argc+1) > ((Py_ssize_t)(((size_t)-1)>>1)
) / sizeof(char *)) ? ((void *)0) : ( (char * *) _PyMem_DebugMalloc
((argc+1) * sizeof(char *)) ) )
;
3774 if (argvlist == NULL((void *)0)) {
3775 PyErr_NoMemory();
3776 goto fail_0;
3777 }
3778 for (i = 0; i < argc; i++) {
3779 if (!fsconvert_strdup((*getitem)(argv, i),
3780 &argvlist[i]))
3781 {
3782 lastarg = i;
3783 goto fail_1;
3784 }
3785 }
3786 lastarg = argc;
3787 argvlist[argc] = NULL((void *)0);
3788
3789 envlist = parse_envlist(env, &envc);
3790 if (envlist == NULL((void *)0))
3791 goto fail_1;
3792
3793#if defined(PYOS_OS2) && defined(PYCC_GCC)
3794 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
3795 spawnval = spawnve(mode, path, argvlist, envlist);
3796 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
3797#else
3798 if (mode == _OLD_P_OVERLAY)
3799 mode = _P_OVERLAY;
3800
3801 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
3802 spawnval = _spawnve(mode, path, argvlist, envlist);
3803 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
3804#endif
3805
3806 if (spawnval == -1)
3807 (void) posix_error();
3808 else
3809#if SIZEOF_LONG8 == SIZEOF_VOID_P8
3810 res = Py_BuildValue_Py_BuildValue_SizeT("l", (long) spawnval);
3811#else
3812 res = Py_BuildValue_Py_BuildValue_SizeT("L", (PY_LONG_LONGlong long) spawnval);
3813#endif
3814
3815 while (--envc >= 0)
3816 PyMem_DEL_PyMem_DebugFree(envlist[envc]);
3817 PyMem_DEL_PyMem_DebugFree(envlist);
3818 fail_1:
3819 free_string_array(argvlist, lastarg);
3820 fail_0:
3821 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3821, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3822 return res;
3823}
3824
3825/* OS/2 supports spawnvp & spawnvpe natively */
3826#if defined(PYOS_OS2)
3827PyDoc_STRVAR(posix_spawnvp__doc__,static char posix_spawnvp__doc__[] = "spawnvp(mode, file, args)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of strings"
3828"spawnvp(mode, file, args)\n\n\static char posix_spawnvp__doc__[] = "spawnvp(mode, file, args)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of strings"
3829Execute the program 'file' in a new process, using the environment\n\static char posix_spawnvp__doc__[] = "spawnvp(mode, file, args)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of strings"
3830search path to find the file.\n\static char posix_spawnvp__doc__[] = "spawnvp(mode, file, args)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of strings"
3831\n\static char posix_spawnvp__doc__[] = "spawnvp(mode, file, args)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of strings"
3832 mode: mode of process creation\n\static char posix_spawnvp__doc__[] = "spawnvp(mode, file, args)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of strings"
3833 file: executable file name\n\static char posix_spawnvp__doc__[] = "spawnvp(mode, file, args)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of strings"
3834 args: tuple or list of strings")static char posix_spawnvp__doc__[] = "spawnvp(mode, file, args)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of strings";
3835
3836static PyObject *
3837posix_spawnvp(PyObject *self, PyObject *args)
3838{
3839 PyObject *opath;
3840 char *path;
3841 PyObject *argv;
3842 char **argvlist;
3843 int mode, i, argc;
3844 Py_intptr_t spawnval;
3845 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3846
3847 /* spawnvp has three arguments: (mode, path, argv), where
3848 argv is a list or tuple of strings. */
3849
3850 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "iO&O:spawnvp", &mode,
3851 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter,
3852 &opath, &argv))
3853 return NULL((void *)0);
3854 path = PyBytes_AsString(opath);
3855 if (PyList_Check(argv)((((((PyObject*)(argv))->ob_type))->tp_flags & ((1L
<<25))) != 0)
) {
3856 argc = PyList_Size(argv);
3857 getitem = PyList_GetItem;
3858 }
3859 else if (PyTuple_Check(argv)((((((PyObject*)(argv))->ob_type))->tp_flags & ((1L
<<26))) != 0)
) {
3860 argc = PyTuple_Size(argv);
3861 getitem = PyTuple_GetItem;
3862 }
3863 else {
3864 PyErr_SetString(PyExc_TypeError,
3865 "spawnvp() arg 2 must be a tuple or list");
3866 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3866, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3867 return NULL((void *)0);
3868 }
3869
3870 argvlist = PyMem_NEW(char *, argc+1)( ((size_t)(argc+1) > ((Py_ssize_t)(((size_t)-1)>>1)
) / sizeof(char *)) ? ((void *)0) : ( (char * *) _PyMem_DebugMalloc
((argc+1) * sizeof(char *)) ) )
;
3871 if (argvlist == NULL((void *)0)) {
3872 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3872, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3873 return PyErr_NoMemory();
3874 }
3875 for (i = 0; i < argc; i++) {
3876 if (!fsconvert_strdup((*getitem)(argv, i),
3877 &argvlist[i])) {
3878 free_string_array(argvlist, i);
3879 PyErr_SetString(
3880 PyExc_TypeError,
3881 "spawnvp() arg 2 must contain only strings");
3882 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3882, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3883 return NULL((void *)0);
3884 }
3885 }
3886 argvlist[argc] = NULL((void *)0);
3887
3888 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
3889#if defined(PYCC_GCC)
3890 spawnval = spawnvp(mode, path, argvlist);
3891#else
3892 spawnval = _spawnvp(mode, path, argvlist);
3893#endif
3894 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
3895
3896 free_string_array(argvlist, argc);
3897 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3897, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3898
3899 if (spawnval == -1)
3900 return posix_error();
3901 else
3902 return Py_BuildValue_Py_BuildValue_SizeT("l", (long) spawnval);
3903}
3904
3905
3906PyDoc_STRVAR(posix_spawnvpe__doc__,static char posix_spawnvpe__doc__[] = "spawnvpe(mode, file, args, env)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3907"spawnvpe(mode, file, args, env)\n\n\static char posix_spawnvpe__doc__[] = "spawnvpe(mode, file, args, env)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3908Execute the program 'file' in a new process, using the environment\n\static char posix_spawnvpe__doc__[] = "spawnvpe(mode, file, args, env)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3909search path to find the file.\n\static char posix_spawnvpe__doc__[] = "spawnvpe(mode, file, args, env)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3910\n\static char posix_spawnvpe__doc__[] = "spawnvpe(mode, file, args, env)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3911 mode: mode of process creation\n\static char posix_spawnvpe__doc__[] = "spawnvpe(mode, file, args, env)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3912 file: executable file name\n\static char posix_spawnvpe__doc__[] = "spawnvpe(mode, file, args, env)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3913 args: tuple or list of arguments\n\static char posix_spawnvpe__doc__[] = "spawnvpe(mode, file, args, env)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings"
3914 env: dictionary of strings mapping to strings")static char posix_spawnvpe__doc__[] = "spawnvpe(mode, file, args, env)\n\nExecute the program 'file' in a new process, using the environment\nsearch path to find the file.\n\n mode: mode of process creation\n file: executable file name\n args: tuple or list of arguments\n env: dictionary of strings mapping to strings";
3915
3916static PyObject *
3917posix_spawnvpe(PyObject *self, PyObject *args)
3918{
3919 PyObject *opath
3920 char *path;
3921 PyObject *argv, *env;
3922 char **argvlist;
3923 char **envlist;
3924 PyObject *res=NULL((void *)0);
3925 int mode;
3926 Py_ssize_t argc, i, envc;
3927 Py_intptr_t spawnval;
3928 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3929 int lastarg = 0;
3930
3931 /* spawnvpe has four arguments: (mode, path, argv, env), where
3932 argv is a list or tuple of strings and env is a dictionary
3933 like posix.environ. */
3934
3935 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ietOO:spawnvpe", &mode,
3936 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter,
3937 &opath, &argv, &env))
3938 return NULL((void *)0);
3939 path = PyBytes_AsString(opath);
3940 if (PyList_Check(argv)((((((PyObject*)(argv))->ob_type))->tp_flags & ((1L
<<25))) != 0)
) {
3941 argc = PyList_Size(argv);
3942 getitem = PyList_GetItem;
3943 }
3944 else if (PyTuple_Check(argv)((((((PyObject*)(argv))->ob_type))->tp_flags & ((1L
<<26))) != 0)
) {
3945 argc = PyTuple_Size(argv);
3946 getitem = PyTuple_GetItem;
3947 }
3948 else {
3949 PyErr_SetString(PyExc_TypeError,
3950 "spawnvpe() arg 2 must be a tuple or list");
3951 goto fail_0;
3952 }
3953 if (!PyMapping_Check(env)) {
3954 PyErr_SetString(PyExc_TypeError,
3955 "spawnvpe() arg 3 must be a mapping object");
3956 goto fail_0;
3957 }
3958
3959 argvlist = PyMem_NEW(char *, argc+1)( ((size_t)(argc+1) > ((Py_ssize_t)(((size_t)-1)>>1)
) / sizeof(char *)) ? ((void *)0) : ( (char * *) _PyMem_DebugMalloc
((argc+1) * sizeof(char *)) ) )
;
3960 if (argvlist == NULL((void *)0)) {
3961 PyErr_NoMemory();
3962 goto fail_0;
3963 }
3964 for (i = 0; i < argc; i++) {
3965 if (!fsconvert_strdup((*getitem)(argv, i),
3966 &argvlist[i]))
3967 {
3968 lastarg = i;
3969 goto fail_1;
3970 }
3971 }
3972 lastarg = argc;
3973 argvlist[argc] = NULL((void *)0);
3974
3975 envlist = parse_envlist(env, &envc);
3976 if (envlist == NULL((void *)0))
3977 goto fail_1;
3978
3979 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
3980#if defined(PYCC_GCC)
3981 spawnval = spawnvpe(mode, path, argvlist, envlist);
3982#else
3983 spawnval = _spawnvpe(mode, path, argvlist, envlist);
3984#endif
3985 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
3986
3987 if (spawnval == -1)
3988 (void) posix_error();
3989 else
3990 res = Py_BuildValue_Py_BuildValue_SizeT("l", (long) spawnval);
3991
3992 while (--envc >= 0)
3993 PyMem_DEL_PyMem_DebugFree(envlist[envc]);
3994 PyMem_DEL_PyMem_DebugFree(envlist);
3995 fail_1:
3996 free_string_array(argvlist, lastarg);
3997 fail_0:
3998 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 3998, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
3999 return res;
4000}
4001#endif /* PYOS_OS2 */
4002#endif /* HAVE_SPAWNV */
4003
4004
4005#ifdef HAVE_FORK1
4006PyDoc_STRVAR(posix_fork1__doc__,static char posix_fork1__doc__[] = "fork1() -> pid\n\nFork a child process with a single multiplexed (i.e., not bound) thread.\n\nReturn 0 to child process and PID of child to parent process."
4007"fork1() -> pid\n\n\static char posix_fork1__doc__[] = "fork1() -> pid\n\nFork a child process with a single multiplexed (i.e., not bound) thread.\n\nReturn 0 to child process and PID of child to parent process."
4008Fork a child process with a single multiplexed (i.e., not bound) thread.\n\static char posix_fork1__doc__[] = "fork1() -> pid\n\nFork a child process with a single multiplexed (i.e., not bound) thread.\n\nReturn 0 to child process and PID of child to parent process."
4009\n\static char posix_fork1__doc__[] = "fork1() -> pid\n\nFork a child process with a single multiplexed (i.e., not bound) thread.\n\nReturn 0 to child process and PID of child to parent process."
4010Return 0 to child process and PID of child to parent process.")static char posix_fork1__doc__[] = "fork1() -> pid\n\nFork a child process with a single multiplexed (i.e., not bound) thread.\n\nReturn 0 to child process and PID of child to parent process.";
4011
4012static PyObject *
4013posix_fork1(PyObject *self, PyObject *noargs)
4014{
4015 pid_t pid;
4016 int result = 0;
4017 _PyImport_AcquireLock();
4018 pid = fork1();
4019 if (pid == 0) {
4020 /* child: this clobbers and resets the import lock. */
4021 PyOS_AfterFork();
4022 } else {
4023 /* parent: release the import lock. */
4024 result = _PyImport_ReleaseLock();
4025 }
4026 if (pid == -1)
4027 return posix_error();
4028 if (result < 0) {
4029 /* Don't clobber the OSError if the fork failed. */
4030 PyErr_SetString(PyExc_RuntimeError,
4031 "not holding the import lock");
4032 return NULL((void *)0);
4033 }
4034 return PyLong_FromPidPyLong_FromLong(pid);
4035}
4036#endif
4037
4038
4039#ifdef HAVE_FORK1
4040PyDoc_STRVAR(posix_fork__doc__,static char posix_fork__doc__[] = "fork() -> pid\n\nFork a child process.\nReturn 0 to child process and PID of child to parent process."
4041"fork() -> pid\n\n\static char posix_fork__doc__[] = "fork() -> pid\n\nFork a child process.\nReturn 0 to child process and PID of child to parent process."
4042Fork a child process.\n\static char posix_fork__doc__[] = "fork() -> pid\n\nFork a child process.\nReturn 0 to child process and PID of child to parent process."
4043Return 0 to child process and PID of child to parent process.")static char posix_fork__doc__[] = "fork() -> pid\n\nFork a child process.\nReturn 0 to child process and PID of child to parent process.";
4044
4045static PyObject *
4046posix_fork(PyObject *self, PyObject *noargs)
4047{
4048 pid_t pid;
4049 int result = 0;
4050 _PyImport_AcquireLock();
4051 pid = fork();
4052 if (pid == 0) {
4053 /* child: this clobbers and resets the import lock. */
4054 PyOS_AfterFork();
4055 } else {
4056 /* parent: release the import lock. */
4057 result = _PyImport_ReleaseLock();
4058 }
4059 if (pid == -1)
4060 return posix_error();
4061 if (result < 0) {
4062 /* Don't clobber the OSError if the fork failed. */
4063 PyErr_SetString(PyExc_RuntimeError,
4064 "not holding the import lock");
4065 return NULL((void *)0);
4066 }
4067 return PyLong_FromPidPyLong_FromLong(pid);
4068}
4069#endif
4070
4071/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
4072/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4073#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX1)
4074#define DEV_PTY_FILE"/dev/ptmx" "/dev/ptc"
4075#define HAVE_DEV_PTMX1
4076#else
4077#define DEV_PTY_FILE"/dev/ptmx" "/dev/ptmx"
4078#endif
4079
4080#if defined(HAVE_OPENPTY1) || defined(HAVE_FORKPTY1) || defined(HAVE_DEV_PTMX1)
4081#ifdef HAVE_PTY_H
4082#include <pty.h>
4083#else
4084#ifdef HAVE_LIBUTIL_H
4085#include <libutil.h>
4086#else
4087#ifdef HAVE_UTIL_H1
4088#include <util.h>
4089#endif /* HAVE_UTIL_H */
4090#endif /* HAVE_LIBUTIL_H */
4091#endif /* HAVE_PTY_H */
4092#ifdef HAVE_STROPTS_H
4093#include <stropts.h>
4094#endif
4095#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
4096
4097#if defined(HAVE_OPENPTY1) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX1)
4098PyDoc_STRVAR(posix_openpty__doc__,static char posix_openpty__doc__[] = "openpty() -> (master_fd, slave_fd)\n\nOpen a pseudo-terminal, returning open fd's for both master and slave end.\n"
4099"openpty() -> (master_fd, slave_fd)\n\n\static char posix_openpty__doc__[] = "openpty() -> (master_fd, slave_fd)\n\nOpen a pseudo-terminal, returning open fd's for both master and slave end.\n"
4100Open a pseudo-terminal, returning open fd's for both master and slave end.\n")static char posix_openpty__doc__[] = "openpty() -> (master_fd, slave_fd)\n\nOpen a pseudo-terminal, returning open fd's for both master and slave end.\n";
4101
4102static PyObject *
4103posix_openpty(PyObject *self, PyObject *noargs)
4104{
4105 int master_fd, slave_fd;
4106#ifndef HAVE_OPENPTY1
4107 char * slave_name;
4108#endif
4109#if defined(HAVE_DEV_PTMX1) && !defined(HAVE_OPENPTY1) && !defined(HAVE__GETPTY)
4110 PyOS_sighandler_t sig_saved;
4111#ifdef sun
4112 extern char *ptsname(int fildes);
4113#endif
4114#endif
4115
4116#ifdef HAVE_OPENPTY1
4117 if (openpty(&master_fd, &slave_fd, NULL((void *)0), NULL((void *)0), NULL((void *)0)) != 0)
4118 return posix_error();
4119#elif defined(HAVE__GETPTY)
4120 slave_name = _getpty(&master_fd, O_RDWR0x0002, 0666, 0);
4121 if (slave_name == NULL((void *)0))
4122 return posix_error();
4123
4124 slave_fd = open(slave_name, O_RDWR0x0002);
4125 if (slave_fd < 0)
4126 return posix_error();
4127#else
4128 master_fd = open(DEV_PTY_FILE"/dev/ptmx", O_RDWR0x0002 | O_NOCTTY0x20000); /* open master */
4129 if (master_fd < 0)
4130 return posix_error();
4131 sig_saved = PyOS_setsig(SIGCHLD20, SIG_DFL(void (*)(int))0);
4132 /* change permission of slave */
4133 if (grantpt(master_fd) < 0) {
4134 PyOS_setsig(SIGCHLD20, sig_saved);
4135 return posix_error();
4136 }
4137 /* unlock slave */
4138 if (unlockpt(master_fd) < 0) {
4139 PyOS_setsig(SIGCHLD20, sig_saved);
4140 return posix_error();
4141 }
4142 PyOS_setsig(SIGCHLD20, sig_saved);
4143 slave_name = ptsname(master_fd); /* get name of slave */
4144 if (slave_name == NULL((void *)0))
4145 return posix_error();
4146 slave_fd = open(slave_name, O_RDWR0x0002 | O_NOCTTY0x20000); /* open slave */
4147 if (slave_fd < 0)
4148 return posix_error();
4149#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
4150 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4151 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
4152#ifndef __hpux
4153 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
4154#endif /* __hpux */
4155#endif /* HAVE_CYGWIN */
4156#endif /* HAVE_OPENPTY */
4157
4158 return Py_BuildValue_Py_BuildValue_SizeT("(ii)", master_fd, slave_fd);
4159
4160}
4161#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
4162
4163#ifdef HAVE_FORKPTY1
4164PyDoc_STRVAR(posix_forkpty__doc__,static char posix_forkpty__doc__[] = "forkpty() -> (pid, master_fd)\n\nFork a new process with a new pseudo-terminal as controlling tty.\n\nLike fork(), return 0 as pid to child process, and PID of child to parent.\nTo both, return fd of newly opened pseudo-terminal.\n"
4165"forkpty() -> (pid, master_fd)\n\n\static char posix_forkpty__doc__[] = "forkpty() -> (pid, master_fd)\n\nFork a new process with a new pseudo-terminal as controlling tty.\n\nLike fork(), return 0 as pid to child process, and PID of child to parent.\nTo both, return fd of newly opened pseudo-terminal.\n"
4166Fork a new process with a new pseudo-terminal as controlling tty.\n\n\static char posix_forkpty__doc__[] = "forkpty() -> (pid, master_fd)\n\nFork a new process with a new pseudo-terminal as controlling tty.\n\nLike fork(), return 0 as pid to child process, and PID of child to parent.\nTo both, return fd of newly opened pseudo-terminal.\n"
4167Like fork(), return 0 as pid to child process, and PID of child to parent.\n\static char posix_forkpty__doc__[] = "forkpty() -> (pid, master_fd)\n\nFork a new process with a new pseudo-terminal as controlling tty.\n\nLike fork(), return 0 as pid to child process, and PID of child to parent.\nTo both, return fd of newly opened pseudo-terminal.\n"
4168To both, return fd of newly opened pseudo-terminal.\n")static char posix_forkpty__doc__[] = "forkpty() -> (pid, master_fd)\n\nFork a new process with a new pseudo-terminal as controlling tty.\n\nLike fork(), return 0 as pid to child process, and PID of child to parent.\nTo both, return fd of newly opened pseudo-terminal.\n";
4169
4170static PyObject *
4171posix_forkpty(PyObject *self, PyObject *noargs)
4172{
4173 int master_fd = -1, result = 0;
4174 pid_t pid;
4175
4176 _PyImport_AcquireLock();
4177 pid = forkpty(&master_fd, NULL((void *)0), NULL((void *)0), NULL((void *)0));
4178 if (pid == 0) {
4179 /* child: this clobbers and resets the import lock. */
4180 PyOS_AfterFork();
4181 } else {
4182 /* parent: release the import lock. */
4183 result = _PyImport_ReleaseLock();
4184 }
4185 if (pid == -1)
4186 return posix_error();
4187 if (result < 0) {
4188 /* Don't clobber the OSError if the fork failed. */
4189 PyErr_SetString(PyExc_RuntimeError,
4190 "not holding the import lock");
4191 return NULL((void *)0);
4192 }
4193 return Py_BuildValue_Py_BuildValue_SizeT("(Ni)", PyLong_FromPidPyLong_FromLong(pid), master_fd);
4194}
4195#endif
4196
4197#ifdef HAVE_GETEGID1
4198PyDoc_STRVAR(posix_getegid__doc__,static char posix_getegid__doc__[] = "getegid() -> egid\n\nReturn the current process's effective group id."
4199"getegid() -> egid\n\n\static char posix_getegid__doc__[] = "getegid() -> egid\n\nReturn the current process's effective group id."
4200Return the current process's effective group id.")static char posix_getegid__doc__[] = "getegid() -> egid\n\nReturn the current process's effective group id.";
4201
4202static PyObject *
4203posix_getegid(PyObject *self, PyObject *noargs)
4204{
4205 return PyLong_FromLong((long)getegid());
4206}
4207#endif
4208
4209
4210#ifdef HAVE_GETEUID1
4211PyDoc_STRVAR(posix_geteuid__doc__,static char posix_geteuid__doc__[] = "geteuid() -> euid\n\nReturn the current process's effective user id."
4212"geteuid() -> euid\n\n\static char posix_geteuid__doc__[] = "geteuid() -> euid\n\nReturn the current process's effective user id."
4213Return the current process's effective user id.")static char posix_geteuid__doc__[] = "geteuid() -> euid\n\nReturn the current process's effective user id.";
4214
4215static PyObject *
4216posix_geteuid(PyObject *self, PyObject *noargs)
4217{
4218 return PyLong_FromLong((long)geteuid());
4219}
4220#endif
4221
4222
4223#ifdef HAVE_GETGID1
4224PyDoc_STRVAR(posix_getgid__doc__,static char posix_getgid__doc__[] = "getgid() -> gid\n\nReturn the current process's group id."
4225"getgid() -> gid\n\n\static char posix_getgid__doc__[] = "getgid() -> gid\n\nReturn the current process's group id."
4226Return the current process's group id.")static char posix_getgid__doc__[] = "getgid() -> gid\n\nReturn the current process's group id.";
4227
4228static PyObject *
4229posix_getgid(PyObject *self, PyObject *noargs)
4230{
4231 return PyLong_FromLong((long)getgid());
4232}
4233#endif
4234
4235
4236PyDoc_STRVAR(posix_getpid__doc__,static char posix_getpid__doc__[] = "getpid() -> pid\n\nReturn the current process id"
4237"getpid() -> pid\n\n\static char posix_getpid__doc__[] = "getpid() -> pid\n\nReturn the current process id"
4238Return the current process id")static char posix_getpid__doc__[] = "getpid() -> pid\n\nReturn the current process id";
4239
4240static PyObject *
4241posix_getpid(PyObject *self, PyObject *noargs)
4242{
4243 return PyLong_FromPidPyLong_FromLong(getpid());
4244}
4245
4246
4247#ifdef HAVE_GETGROUPS1
4248PyDoc_STRVAR(posix_getgroups__doc__,static char posix_getgroups__doc__[] = "getgroups() -> list of group IDs\n\nReturn list of supplemental group IDs for the process."
4249"getgroups() -> list of group IDs\n\n\static char posix_getgroups__doc__[] = "getgroups() -> list of group IDs\n\nReturn list of supplemental group IDs for the process."
4250Return list of supplemental group IDs for the process.")static char posix_getgroups__doc__[] = "getgroups() -> list of group IDs\n\nReturn list of supplemental group IDs for the process.";
4251
4252static PyObject *
4253posix_getgroups(PyObject *self, PyObject *noargs)
4254{
4255 PyObject *result = NULL((void *)0);
4256
4257#ifdef NGROUPS_MAX16
4258#define MAX_GROUPS16 NGROUPS_MAX16
4259#else
4260 /* defined to be 16 on Solaris7, so this should be a small number */
4261#define MAX_GROUPS16 64
4262#endif
4263 gid_t grouplist[MAX_GROUPS16];
4264
4265 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4266 * This is a helper variable to store the intermediate result when
4267 * that happens.
4268 *
4269 * To keep the code readable the OSX behaviour is unconditional,
4270 * according to the POSIX spec this should be safe on all unix-y
4271 * systems.
4272 */
4273 gid_t* alt_grouplist = grouplist;
4274 int n;
4275
4276 n = getgroups(MAX_GROUPS16, grouplist);
4277 if (n < 0) {
4278 if (errno(*__error()) == EINVAL22) {
4279 n = getgroups(0, NULL((void *)0));
4280 if (n == -1) {
4281 return posix_error();
4282 }
4283 if (n == 0) {
4284 /* Avoid malloc(0) */
4285 alt_grouplist = grouplist;
4286 } else {
4287 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4288 if (alt_grouplist == NULL((void *)0)) {
4289 errno(*__error()) = EINVAL22;
4290 return posix_error();
4291 }
4292 n = getgroups(n, alt_grouplist);
4293 if (n == -1) {
4294 PyMem_Free(alt_grouplist);
4295 return posix_error();
4296 }
4297 }
4298 } else {
4299 return posix_error();
4300 }
4301 }
4302 result = PyList_New(n);
4303 if (result != NULL((void *)0)) {
4304 int i;
4305 for (i = 0; i < n; ++i) {
4306 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
4307 if (o == NULL((void *)0)) {
4308 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 4308, (PyObject *)(result)); } else
_Py_Dealloc((PyObject *)(result)); } while (0)
;
4309 result = NULL((void *)0);
4310 break;
4311 }
4312 PyList_SET_ITEM(result, i, o)(((PyListObject *)(result))->ob_item[i] = (o));
4313 }
4314 }
4315
4316 if (alt_grouplist != grouplist) {
4317 PyMem_Free(alt_grouplist);
4318 }
4319
4320 return result;
4321}
4322#endif
4323
4324#ifdef HAVE_INITGROUPS1
4325PyDoc_STRVAR(posix_initgroups__doc__,static char posix_initgroups__doc__[] = "initgroups(username, gid) -> None\n\nCall the system initgroups() to initialize the group access list with all of\nthe groups of which the specified username is a member, plus the specified\ngroup id."
4326"initgroups(username, gid) -> None\n\n\static char posix_initgroups__doc__[] = "initgroups(username, gid) -> None\n\nCall the system initgroups() to initialize the group access list with all of\nthe groups of which the specified username is a member, plus the specified\ngroup id."
4327Call the system initgroups() to initialize the group access list with all of\n\static char posix_initgroups__doc__[] = "initgroups(username, gid) -> None\n\nCall the system initgroups() to initialize the group access list with all of\nthe groups of which the specified username is a member, plus the specified\ngroup id."
4328the groups of which the specified username is a member, plus the specified\n\static char posix_initgroups__doc__[] = "initgroups(username, gid) -> None\n\nCall the system initgroups() to initialize the group access list with all of\nthe groups of which the specified username is a member, plus the specified\ngroup id."
4329group id.")static char posix_initgroups__doc__[] = "initgroups(username, gid) -> None\n\nCall the system initgroups() to initialize the group access list with all of\nthe groups of which the specified username is a member, plus the specified\ngroup id.";
4330
4331static PyObject *
4332posix_initgroups(PyObject *self, PyObject *args)
4333{
4334 PyObject *oname;
4335 char *username;
4336 int res;
4337 long gid;
4338
4339 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&l:initgroups",
4340 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &oname, &gid))
4341 return NULL((void *)0);
4342 username = PyBytes_AS_STRING(oname)((__builtin_expect(!(((((((PyObject*)(oname))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 4342, "PyBytes_Check(oname)") : (
void)0), (((PyBytesObject *)(oname))->ob_sval))
;
4343
4344 res = initgroups(username, (gid_t) gid);
4345 Py_DECREF(oname)do { if (_Py_RefTotal-- , --((PyObject*)(oname))->ob_refcnt
!= 0) { if (((PyObject*)oname)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 4345, (PyObject *)(oname)); } else
_Py_Dealloc((PyObject *)(oname)); } while (0)
;
4346 if (res == -1)
4347 return PyErr_SetFromErrno(PyExc_OSError);
4348
4349 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
4350 return Py_None(&_Py_NoneStruct);
4351}
4352#endif
4353
4354#ifdef HAVE_GETPGID1
4355PyDoc_STRVAR(posix_getpgid__doc__,static char posix_getpgid__doc__[] = "getpgid(pid) -> pgid\n\nCall the system call getpgid()."
4356"getpgid(pid) -> pgid\n\n\static char posix_getpgid__doc__[] = "getpgid(pid) -> pgid\n\nCall the system call getpgid()."
4357Call the system call getpgid().")static char posix_getpgid__doc__[] = "getpgid(pid) -> pgid\n\nCall the system call getpgid().";
4358
4359static PyObject *
4360posix_getpgid(PyObject *self, PyObject *args)
4361{
4362 pid_t pid, pgid;
4363 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, _Py_PARSE_PID"i" ":getpgid", &pid))
4364 return NULL((void *)0);
4365 pgid = getpgid(pid);
4366 if (pgid < 0)
4367 return posix_error();
4368 return PyLong_FromPidPyLong_FromLong(pgid);
4369}
4370#endif /* HAVE_GETPGID */
4371
4372
4373#ifdef HAVE_GETPGRP1
4374PyDoc_STRVAR(posix_getpgrp__doc__,static char posix_getpgrp__doc__[] = "getpgrp() -> pgrp\n\nReturn the current process group id."
4375"getpgrp() -> pgrp\n\n\static char posix_getpgrp__doc__[] = "getpgrp() -> pgrp\n\nReturn the current process group id."
4376Return the current process group id.")static char posix_getpgrp__doc__[] = "getpgrp() -> pgrp\n\nReturn the current process group id.";
4377
4378static PyObject *
4379posix_getpgrp(PyObject *self, PyObject *noargs)
4380{
4381#ifdef GETPGRP_HAVE_ARG
4382 return PyLong_FromPidPyLong_FromLong(getpgrp(0));
4383#else /* GETPGRP_HAVE_ARG */
4384 return PyLong_FromPidPyLong_FromLong(getpgrp());
4385#endif /* GETPGRP_HAVE_ARG */
4386}
4387#endif /* HAVE_GETPGRP */
4388
4389
4390#ifdef HAVE_SETPGRP1
4391PyDoc_STRVAR(posix_setpgrp__doc__,static char posix_setpgrp__doc__[] = "setpgrp()\n\nMake this process the process group leader."
4392"setpgrp()\n\n\static char posix_setpgrp__doc__[] = "setpgrp()\n\nMake this process the process group leader."
4393Make this process the process group leader.")static char posix_setpgrp__doc__[] = "setpgrp()\n\nMake this process the process group leader.";
4394
4395static PyObject *
4396posix_setpgrp(PyObject *self, PyObject *noargs)
4397{
4398#ifdef SETPGRP_HAVE_ARG
4399 if (setpgrp(0, 0) < 0)
4400#else /* SETPGRP_HAVE_ARG */
4401 if (setpgrp() < 0)
4402#endif /* SETPGRP_HAVE_ARG */
4403 return posix_error();
4404 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
4405 return Py_None(&_Py_NoneStruct);
4406}
4407
4408#endif /* HAVE_SETPGRP */
4409
4410#ifdef HAVE_GETPPID1
4411
4412#ifdef MS_WINDOWS
4413#include <tlhelp32.h>
4414
4415static PyObject*
4416win32_getppid()
4417{
4418 HANDLE snapshot;
4419 pid_t mypid;
4420 PyObject* result = NULL((void *)0);
4421 BOOL have_record;
4422 PROCESSENTRY32 pe;
4423
4424 mypid = getpid(); /* This function never fails */
4425
4426 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4427 if (snapshot == INVALID_HANDLE_VALUE)
4428 return PyErr_SetFromWindowsErr(GetLastError());
4429
4430 pe.dwSize = sizeof(pe);
4431 have_record = Process32First(snapshot, &pe);
4432 while (have_record) {
4433 if (mypid == (pid_t)pe.th32ProcessID) {
4434 /* We could cache the ulong value in a static variable. */
4435 result = PyLong_FromPidPyLong_FromLong((pid_t)pe.th32ParentProcessID);
4436 break;
4437 }
4438
4439 have_record = Process32Next(snapshot, &pe);
4440 }
4441
4442 /* If our loop exits and our pid was not found (result will be NULL)
4443 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4444 * error anyway, so let's raise it. */
4445 if (!result)
4446 result = PyErr_SetFromWindowsErr(GetLastError());
4447
4448 CloseHandle(snapshot);
4449
4450 return result;
4451}
4452#endif /*MS_WINDOWS*/
4453
4454PyDoc_STRVAR(posix_getppid__doc__,static char posix_getppid__doc__[] = "getppid() -> ppid\n\nReturn the parent's process id. If the parent process has already exited,\nWindows machines will still return its id; others systems will return the id\nof the 'init' process (1)."
4455"getppid() -> ppid\n\n\static char posix_getppid__doc__[] = "getppid() -> ppid\n\nReturn the parent's process id. If the parent process has already exited,\nWindows machines will still return its id; others systems will return the id\nof the 'init' process (1)."
4456Return the parent's process id. If the parent process has already exited,\n\static char posix_getppid__doc__[] = "getppid() -> ppid\n\nReturn the parent's process id. If the parent process has already exited,\nWindows machines will still return its id; others systems will return the id\nof the 'init' process (1)."
4457Windows machines will still return its id; others systems will return the id\n\static char posix_getppid__doc__[] = "getppid() -> ppid\n\nReturn the parent's process id. If the parent process has already exited,\nWindows machines will still return its id; others systems will return the id\nof the 'init' process (1)."
4458of the 'init' process (1).")static char posix_getppid__doc__[] = "getppid() -> ppid\n\nReturn the parent's process id. If the parent process has already exited,\nWindows machines will still return its id; others systems will return the id\nof the 'init' process (1).";
4459
4460static PyObject *
4461posix_getppid(PyObject *self, PyObject *noargs)
4462{
4463#ifdef MS_WINDOWS
4464 return win32_getppid();
4465#else
4466 return PyLong_FromPidPyLong_FromLong(getppid());
4467#endif
4468}
4469#endif /* HAVE_GETPPID */
4470
4471
4472#ifdef HAVE_GETLOGIN1
4473PyDoc_STRVAR(posix_getlogin__doc__,static char posix_getlogin__doc__[] = "getlogin() -> string\n\nReturn the actual login name."
4474"getlogin() -> string\n\n\static char posix_getlogin__doc__[] = "getlogin() -> string\n\nReturn the actual login name."
4475Return the actual login name.")static char posix_getlogin__doc__[] = "getlogin() -> string\n\nReturn the actual login name.";
4476
4477static PyObject *
4478posix_getlogin(PyObject *self, PyObject *noargs)
4479{
4480 PyObject *result = NULL((void *)0);
4481#ifdef MS_WINDOWS
4482 wchar_t user_name[UNLEN + 1];
4483 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4484
4485 if (GetUserNameW(user_name, &num_chars)) {
4486 /* num_chars is the number of unicode chars plus null terminator */
4487 result = PyUnicode_FromWideCharPyUnicodeUCS2_FromWideChar(user_name, num_chars - 1);
4488 }
4489 else
4490 result = PyErr_SetFromWindowsErr(GetLastError());
4491#else
4492 char *name;
4493 int old_errno = errno(*__error());
4494
4495 errno(*__error()) = 0;
4496 name = getlogin();
4497 if (name == NULL((void *)0)) {
4498 if (errno(*__error()))
4499 posix_error();
4500 else
4501 PyErr_SetString(PyExc_OSError, "unable to determine login name");
4502 }
4503 else
4504 result = PyUnicode_DecodeFSDefaultPyUnicodeUCS2_DecodeFSDefault(name);
4505 errno(*__error()) = old_errno;
4506#endif
4507 return result;
4508}
4509#endif /* HAVE_GETLOGIN */
4510
4511#ifdef HAVE_GETUID1
4512PyDoc_STRVAR(posix_getuid__doc__,static char posix_getuid__doc__[] = "getuid() -> uid\n\nReturn the current process's user id."
4513"getuid() -> uid\n\n\static char posix_getuid__doc__[] = "getuid() -> uid\n\nReturn the current process's user id."
4514Return the current process's user id.")static char posix_getuid__doc__[] = "getuid() -> uid\n\nReturn the current process's user id.";
4515
4516static PyObject *
4517posix_getuid(PyObject *self, PyObject *noargs)
4518{
4519 return PyLong_FromLong((long)getuid());
4520}
4521#endif
4522
4523
4524#ifdef HAVE_KILL1
4525PyDoc_STRVAR(posix_kill__doc__,static char posix_kill__doc__[] = "kill(pid, sig)\n\nKill a process with a signal."
4526"kill(pid, sig)\n\n\static char posix_kill__doc__[] = "kill(pid, sig)\n\nKill a process with a signal."
4527Kill a process with a signal.")static char posix_kill__doc__[] = "kill(pid, sig)\n\nKill a process with a signal.";
4528
4529static PyObject *
4530posix_kill(PyObject *self, PyObject *args)
4531{
4532 pid_t pid;
4533 int sig;
4534 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, _Py_PARSE_PID"i" "i:kill", &pid, &sig))
4535 return NULL((void *)0);
4536#if defined(PYOS_OS2) && !defined(PYCC_GCC)
4537 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4538 APIRET rc;
4539 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
4540 return os2_error(rc);
4541
4542 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4543 APIRET rc;
4544 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
4545 return os2_error(rc);
4546
4547 } else
4548 return NULL((void *)0); /* Unrecognized Signal Requested */
4549#else
4550 if (kill(pid, sig) == -1)
4551 return posix_error();
4552#endif
4553 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
4554 return Py_None(&_Py_NoneStruct);
4555}
4556#endif
4557
4558#ifdef HAVE_KILLPG1
4559PyDoc_STRVAR(posix_killpg__doc__,static char posix_killpg__doc__[] = "killpg(pgid, sig)\n\nKill a process group with a signal."
4560"killpg(pgid, sig)\n\n\static char posix_killpg__doc__[] = "killpg(pgid, sig)\n\nKill a process group with a signal."
4561Kill a process group with a signal.")static char posix_killpg__doc__[] = "killpg(pgid, sig)\n\nKill a process group with a signal.";
4562
4563static PyObject *
4564posix_killpg(PyObject *self, PyObject *args)
4565{
4566 int sig;
4567 pid_t pgid;
4568 /* XXX some man pages make the `pgid` parameter an int, others
4569 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4570 take the same type. Moreover, pid_t is always at least as wide as
4571 int (else compilation of this module fails), which is safe. */
4572 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, _Py_PARSE_PID"i" "i:killpg", &pgid, &sig))
4573 return NULL((void *)0);
4574 if (killpg(pgid, sig) == -1)
4575 return posix_error();
4576 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
4577 return Py_None(&_Py_NoneStruct);
4578}
4579#endif
4580
4581#ifdef MS_WINDOWS
4582PyDoc_STRVAR(win32_kill__doc__,static char win32_kill__doc__[] = "kill(pid, sig)\n\nKill a process with a signal."
4583"kill(pid, sig)\n\n\static char win32_kill__doc__[] = "kill(pid, sig)\n\nKill a process with a signal."
4584Kill a process with a signal.")static char win32_kill__doc__[] = "kill(pid, sig)\n\nKill a process with a signal.";
4585
4586static PyObject *
4587win32_kill(PyObject *self, PyObject *args)
4588{
4589 PyObject *result;
4590 DWORD pid, sig, err;
4591 HANDLE handle;
4592
4593 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "kk:kill", &pid, &sig))
4594 return NULL((void *)0);
4595
4596 /* Console processes which share a common console can be sent CTRL+C or
4597 CTRL+BREAK events, provided they handle said events. */
4598 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4599 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4600 err = GetLastError();
4601 PyErr_SetFromWindowsErr(err);
4602 }
4603 else
4604 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
4605 }
4606
4607 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4608 attempt to open and terminate the process. */
4609 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4610 if (handle == NULL((void *)0)) {
4611 err = GetLastError();
4612 return PyErr_SetFromWindowsErr(err);
4613 }
4614
4615 if (TerminateProcess(handle, sig) == 0) {
4616 err = GetLastError();
4617 result = PyErr_SetFromWindowsErr(err);
4618 } else {
4619 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
4620 result = Py_None(&_Py_NoneStruct);
4621 }
4622
4623 CloseHandle(handle);
4624 return result;
4625}
4626#endif /* MS_WINDOWS */
4627
4628#ifdef HAVE_PLOCK
4629
4630#ifdef HAVE_SYS_LOCK_H1
4631#include <sys/lock.h>
4632#endif
4633
4634PyDoc_STRVAR(posix_plock__doc__,static char posix_plock__doc__[] = "plock(op)\n\nLock program segments into memory."
4635"plock(op)\n\n\static char posix_plock__doc__[] = "plock(op)\n\nLock program segments into memory."
4636Lock program segments into memory.")static char posix_plock__doc__[] = "plock(op)\n\nLock program segments into memory.";
4637
4638static PyObject *
4639posix_plock(PyObject *self, PyObject *args)
4640{
4641 int op;
4642 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:plock", &op))
4643 return NULL((void *)0);
4644 if (plock(op) == -1)
4645 return posix_error();
4646 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
4647 return Py_None(&_Py_NoneStruct);
4648}
4649#endif
4650
4651#ifdef HAVE_SETUID1
4652PyDoc_STRVAR(posix_setuid__doc__,static char posix_setuid__doc__[] = "setuid(uid)\n\nSet the current process's user id."
4653"setuid(uid)\n\n\static char posix_setuid__doc__[] = "setuid(uid)\n\nSet the current process's user id."
4654Set the current process's user id.")static char posix_setuid__doc__[] = "setuid(uid)\n\nSet the current process's user id.";
4655
4656static PyObject *
4657posix_setuid(PyObject *self, PyObject *args)
4658{
4659 long uid_arg;
4660 uid_t uid;
4661 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "l:setuid", &uid_arg))
4662 return NULL((void *)0);
4663 uid = uid_arg;
4664 if (uid != uid_arg) {
4665 PyErr_SetString(PyExc_OverflowError, "user id too big");
4666 return NULL((void *)0);
4667 }
4668 if (setuid(uid) < 0)
4669 return posix_error();
4670 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
4671 return Py_None(&_Py_NoneStruct);
4672}
4673#endif /* HAVE_SETUID */
4674
4675
4676#ifdef HAVE_SETEUID1
4677PyDoc_STRVAR(posix_seteuid__doc__,static char posix_seteuid__doc__[] = "seteuid(uid)\n\nSet the current process's effective user id."
4678"seteuid(uid)\n\n\static char posix_seteuid__doc__[] = "seteuid(uid)\n\nSet the current process's effective user id."
4679Set the current process's effective user id.")static char posix_seteuid__doc__[] = "seteuid(uid)\n\nSet the current process's effective user id.";
4680
4681static PyObject *
4682posix_seteuid (PyObject *self, PyObject *args)
4683{
4684 long euid_arg;
4685 uid_t euid;
4686 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "l", &euid_arg))
4687 return NULL((void *)0);
4688 euid = euid_arg;
4689 if (euid != euid_arg) {
4690 PyErr_SetString(PyExc_OverflowError, "user id too big");
4691 return NULL((void *)0);
4692 }
4693 if (seteuid(euid) < 0) {
4694 return posix_error();
4695 } else {
4696 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
4697 return Py_None(&_Py_NoneStruct);
4698 }
4699}
4700#endif /* HAVE_SETEUID */
4701
4702#ifdef HAVE_SETEGID1
4703PyDoc_STRVAR(posix_setegid__doc__,static char posix_setegid__doc__[] = "setegid(gid)\n\nSet the current process's effective group id."
4704"setegid(gid)\n\n\static char posix_setegid__doc__[] = "setegid(gid)\n\nSet the current process's effective group id."
4705Set the current process's effective group id.")static char posix_setegid__doc__[] = "setegid(gid)\n\nSet the current process's effective group id.";
4706
4707static PyObject *
4708posix_setegid (PyObject *self, PyObject *args)
4709{
4710 long egid_arg;
4711 gid_t egid;
4712 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "l", &egid_arg))
1
Taking false branch
4713 return NULL((void *)0);
4714 egid = egid_arg;
4715 if (egid != egid_arg) {
2
Both operands to '!=' always have the same value
4716 PyErr_SetString(PyExc_OverflowError, "group id too big");
4717 return NULL((void *)0);
4718 }
4719 if (setegid(egid) < 0) {
4720 return posix_error();
4721 } else {
4722 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
4723 return Py_None(&_Py_NoneStruct);
4724 }
4725}
4726#endif /* HAVE_SETEGID */
4727
4728#ifdef HAVE_SETREUID1
4729PyDoc_STRVAR(posix_setreuid__doc__,static char posix_setreuid__doc__[] = "setreuid(ruid, euid)\n\nSet the current process's real and effective user ids."
4730"setreuid(ruid, euid)\n\n\static char posix_setreuid__doc__[] = "setreuid(ruid, euid)\n\nSet the current process's real and effective user ids."
4731Set the current process's real and effective user ids.")static char posix_setreuid__doc__[] = "setreuid(ruid, euid)\n\nSet the current process's real and effective user ids.";
4732
4733static PyObject *
4734posix_setreuid (PyObject *self, PyObject *args)
4735{
4736 long ruid_arg, euid_arg;
4737 uid_t ruid, euid;
4738 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ll", &ruid_arg, &euid_arg))
4739 return NULL((void *)0);
4740 if (ruid_arg == -1)
4741 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4742 else
4743 ruid = ruid_arg; /* otherwise, assign from our long */
4744 if (euid_arg == -1)
4745 euid = (uid_t)-1;
4746 else
4747 euid = euid_arg;
4748 if ((euid_arg != -1 && euid != euid_arg) ||
4749 (ruid_arg != -1 && ruid != ruid_arg)) {
4750 PyErr_SetString(PyExc_OverflowError, "user id too big");
4751 return NULL((void *)0);
4752 }
4753 if (setreuid(ruid, euid) < 0) {
4754 return posix_error();
4755 } else {
4756 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
4757 return Py_None(&_Py_NoneStruct);
4758 }
4759}
4760#endif /* HAVE_SETREUID */
4761
4762#ifdef HAVE_SETREGID1
4763PyDoc_STRVAR(posix_setregid__doc__,static char posix_setregid__doc__[] = "setregid(rgid, egid)\n\nSet the current process's real and effective group ids."
4764"setregid(rgid, egid)\n\n\static char posix_setregid__doc__[] = "setregid(rgid, egid)\n\nSet the current process's real and effective group ids."
4765Set the current process's real and effective group ids.")static char posix_setregid__doc__[] = "setregid(rgid, egid)\n\nSet the current process's real and effective group ids.";
4766
4767static PyObject *
4768posix_setregid (PyObject *self, PyObject *args)
4769{
4770 long rgid_arg, egid_arg;
4771 gid_t rgid, egid;
4772 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ll", &rgid_arg, &egid_arg))
4773 return NULL((void *)0);
4774 if (rgid_arg == -1)
4775 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4776 else
4777 rgid = rgid_arg; /* otherwise, assign from our long */
4778 if (egid_arg == -1)
4779 egid = (gid_t)-1;
4780 else
4781 egid = egid_arg;
4782 if ((egid_arg != -1 && egid != egid_arg) ||
4783 (rgid_arg != -1 && rgid != rgid_arg)) {
4784 PyErr_SetString(PyExc_OverflowError, "group id too big");
4785 return NULL((void *)0);
4786 }
4787 if (setregid(rgid, egid) < 0) {
4788 return posix_error();
4789 } else {
4790 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
4791 return Py_None(&_Py_NoneStruct);
4792 }
4793}
4794#endif /* HAVE_SETREGID */
4795
4796#ifdef HAVE_SETGID1
4797PyDoc_STRVAR(posix_setgid__doc__,static char posix_setgid__doc__[] = "setgid(gid)\n\nSet the current process's group id."
4798"setgid(gid)\n\n\static char posix_setgid__doc__[] = "setgid(gid)\n\nSet the current process's group id."
4799Set the current process's group id.")static char posix_setgid__doc__[] = "setgid(gid)\n\nSet the current process's group id.";
4800
4801static PyObject *
4802posix_setgid(PyObject *self, PyObject *args)
4803{
4804 long gid_arg;
4805 gid_t gid;
4806 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "l:setgid", &gid_arg))
4807 return NULL((void *)0);
4808 gid = gid_arg;
4809 if (gid != gid_arg) {
4810 PyErr_SetString(PyExc_OverflowError, "group id too big");
4811 return NULL((void *)0);
4812 }
4813 if (setgid(gid) < 0)
4814 return posix_error();
4815 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
4816 return Py_None(&_Py_NoneStruct);
4817}
4818#endif /* HAVE_SETGID */
4819
4820#ifdef HAVE_SETGROUPS1
4821PyDoc_STRVAR(posix_setgroups__doc__,static char posix_setgroups__doc__[] = "setgroups(list)\n\nSet the groups of the current process to list."
4822"setgroups(list)\n\n\static char posix_setgroups__doc__[] = "setgroups(list)\n\nSet the groups of the current process to list."
4823Set the groups of the current process to list.")static char posix_setgroups__doc__[] = "setgroups(list)\n\nSet the groups of the current process to list.";
4824
4825static PyObject *
4826posix_setgroups(PyObject *self, PyObject *groups)
4827{
4828 int i, len;
4829 gid_t grouplist[MAX_GROUPS16];
4830
4831 if (!PySequence_Check(groups)) {
4832 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4833 return NULL((void *)0);
4834 }
4835 len = PySequence_Size(groups);
4836 if (len > MAX_GROUPS16) {
4837 PyErr_SetString(PyExc_ValueError, "too many groups");
4838 return NULL((void *)0);
4839 }
4840 for(i = 0; i < len; i++) {
4841 PyObject *elem;
4842 elem = PySequence_GetItem(groups, i);
4843 if (!elem)
4844 return NULL((void *)0);
4845 if (!PyLong_Check(elem)((((((PyObject*)(elem))->ob_type))->tp_flags & ((1L
<<24))) != 0)
) {
4846 PyErr_SetString(PyExc_TypeError,
4847 "groups must be integers");
4848 Py_DECREF(elem)do { if (_Py_RefTotal-- , --((PyObject*)(elem))->ob_refcnt
!= 0) { if (((PyObject*)elem)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 4848, (PyObject *)(elem)); } else
_Py_Dealloc((PyObject *)(elem)); } while (0)
;
4849 return NULL((void *)0);
4850 } else {
4851 unsigned long x = PyLong_AsUnsignedLong(elem);
4852 if (PyErr_Occurred()) {
4853 PyErr_SetString(PyExc_TypeError,
4854 "group id too big");
4855 Py_DECREF(elem)do { if (_Py_RefTotal-- , --((PyObject*)(elem))->ob_refcnt
!= 0) { if (((PyObject*)elem)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 4855, (PyObject *)(elem)); } else
_Py_Dealloc((PyObject *)(elem)); } while (0)
;
4856 return NULL((void *)0);
4857 }
4858 grouplist[i] = x;
4859 /* read back the value to see if it fitted in gid_t */
4860 if (grouplist[i] != x) {
4861 PyErr_SetString(PyExc_TypeError,
4862 "group id too big");
4863 Py_DECREF(elem)do { if (_Py_RefTotal-- , --((PyObject*)(elem))->ob_refcnt
!= 0) { if (((PyObject*)elem)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 4863, (PyObject *)(elem)); } else
_Py_Dealloc((PyObject *)(elem)); } while (0)
;
4864 return NULL((void *)0);
4865 }
4866 }
4867 Py_DECREF(elem)do { if (_Py_RefTotal-- , --((PyObject*)(elem))->ob_refcnt
!= 0) { if (((PyObject*)elem)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 4867, (PyObject *)(elem)); } else
_Py_Dealloc((PyObject *)(elem)); } while (0)
;
4868 }
4869
4870 if (setgroups(len, grouplist) < 0)
4871 return posix_error();
4872 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
4873 return Py_None(&_Py_NoneStruct);
4874}
4875#endif /* HAVE_SETGROUPS */
4876
4877#if defined(HAVE_WAIT31) || defined(HAVE_WAIT41)
4878static PyObject *
4879wait_helper(pid_t pid, int status, struct rusage *ru)
4880{
4881 PyObject *result;
4882 static PyObject *struct_rusage;
4883
4884 if (pid == -1)
4885 return posix_error();
4886
4887 if (struct_rusage == NULL((void *)0)) {
4888 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4889 if (m == NULL((void *)0))
4890 return NULL((void *)0);
4891 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4892 Py_DECREF(m)do { if (_Py_RefTotal-- , --((PyObject*)(m))->ob_refcnt !=
0) { if (((PyObject*)m)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 4892, (PyObject *)(m)); } else _Py_Dealloc
((PyObject *)(m)); } while (0)
;
4893 if (struct_rusage == NULL((void *)0))
4894 return NULL((void *)0);
4895 }
4896
4897 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4898 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4899 if (!result)
4900 return NULL((void *)0);
4901
4902#ifndef doubletime
4903#define doubletime(TV)((double)(TV).tv_sec + (TV).tv_usec * 0.000001) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4904#endif
4905
4906 PyStructSequence_SET_ITEM(result, 0,(((PyTupleObject *)(result))->ob_item[0] = PyFloat_FromDouble
(((double)(ru->ru_utime).tv_sec + (ru->ru_utime).tv_usec
* 0.000001)))
4907 PyFloat_FromDouble(doubletime(ru->ru_utime)))(((PyTupleObject *)(result))->ob_item[0] = PyFloat_FromDouble
(((double)(ru->ru_utime).tv_sec + (ru->ru_utime).tv_usec
* 0.000001)))
;
4908 PyStructSequence_SET_ITEM(result, 1,(((PyTupleObject *)(result))->ob_item[1] = PyFloat_FromDouble
(((double)(ru->ru_stime).tv_sec + (ru->ru_stime).tv_usec
* 0.000001)))
4909 PyFloat_FromDouble(doubletime(ru->ru_stime)))(((PyTupleObject *)(result))->ob_item[1] = PyFloat_FromDouble
(((double)(ru->ru_stime).tv_sec + (ru->ru_stime).tv_usec
* 0.000001)))
;
4910#define SET_INT(result, index, value)\
4911 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))(((PyTupleObject *)(result))->ob_item[index] = PyLong_FromLong
(value))
4912 SET_INT(result, 2, ru->ru_maxrss);
4913 SET_INT(result, 3, ru->ru_ixrss);
4914 SET_INT(result, 4, ru->ru_idrss);
4915 SET_INT(result, 5, ru->ru_isrss);
4916 SET_INT(result, 6, ru->ru_minflt);
4917 SET_INT(result, 7, ru->ru_majflt);
4918 SET_INT(result, 8, ru->ru_nswap);
4919 SET_INT(result, 9, ru->ru_inblock);
4920 SET_INT(result, 10, ru->ru_oublock);
4921 SET_INT(result, 11, ru->ru_msgsnd);
4922 SET_INT(result, 12, ru->ru_msgrcv);
4923 SET_INT(result, 13, ru->ru_nsignals);
4924 SET_INT(result, 14, ru->ru_nvcsw);
4925 SET_INT(result, 15, ru->ru_nivcsw);
4926#undef SET_INT
4927
4928 if (PyErr_Occurred()) {
4929 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 4929, (PyObject *)(result)); } else
_Py_Dealloc((PyObject *)(result)); } while (0)
;
4930 return NULL((void *)0);
4931 }
4932
4933 return Py_BuildValue_Py_BuildValue_SizeT("NiN", PyLong_FromPidPyLong_FromLong(pid), status, result);
4934}
4935#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4936
4937#ifdef HAVE_WAIT31
4938PyDoc_STRVAR(posix_wait3__doc__,static char posix_wait3__doc__[] = "wait3(options) -> (pid, status, rusage)\n\nWait for completion of a child process."
4939"wait3(options) -> (pid, status, rusage)\n\n\static char posix_wait3__doc__[] = "wait3(options) -> (pid, status, rusage)\n\nWait for completion of a child process."
4940Wait for completion of a child process.")static char posix_wait3__doc__[] = "wait3(options) -> (pid, status, rusage)\n\nWait for completion of a child process.";
4941
4942static PyObject *
4943posix_wait3(PyObject *self, PyObject *args)
4944{
4945 pid_t pid;
4946 int options;
4947 struct rusage ru;
4948 WAIT_TYPEint status;
4949 WAIT_STATUS_INT(status)(status) = 0;
4950
4951 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:wait3", &options))
4952 return NULL((void *)0);
4953
4954 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
4955 pid = wait3(&status, options, &ru);
4956 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
4957
4958 return wait_helper(pid, WAIT_STATUS_INT(status)(status), &ru);
4959}
4960#endif /* HAVE_WAIT3 */
4961
4962#ifdef HAVE_WAIT41
4963PyDoc_STRVAR(posix_wait4__doc__,static char posix_wait4__doc__[] = "wait4(pid, options) -> (pid, status, rusage)\n\nWait for completion of a given child process."
4964"wait4(pid, options) -> (pid, status, rusage)\n\n\static char posix_wait4__doc__[] = "wait4(pid, options) -> (pid, status, rusage)\n\nWait for completion of a given child process."
4965Wait for completion of a given child process.")static char posix_wait4__doc__[] = "wait4(pid, options) -> (pid, status, rusage)\n\nWait for completion of a given child process.";
4966
4967static PyObject *
4968posix_wait4(PyObject *self, PyObject *args)
4969{
4970 pid_t pid;
4971 int options;
4972 struct rusage ru;
4973 WAIT_TYPEint status;
4974 WAIT_STATUS_INT(status)(status) = 0;
4975
4976 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, _Py_PARSE_PID"i" "i:wait4", &pid, &options))
4977 return NULL((void *)0);
4978
4979 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
4980 pid = wait4(pid, &status, options, &ru);
4981 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
4982
4983 return wait_helper(pid, WAIT_STATUS_INT(status)(status), &ru);
4984}
4985#endif /* HAVE_WAIT4 */
4986
4987#ifdef HAVE_WAITPID1
4988PyDoc_STRVAR(posix_waitpid__doc__,static char posix_waitpid__doc__[] = "waitpid(pid, options) -> (pid, status)\n\nWait for completion of a given child process."
4989"waitpid(pid, options) -> (pid, status)\n\n\static char posix_waitpid__doc__[] = "waitpid(pid, options) -> (pid, status)\n\nWait for completion of a given child process."
4990Wait for completion of a given child process.")static char posix_waitpid__doc__[] = "waitpid(pid, options) -> (pid, status)\n\nWait for completion of a given child process.";
4991
4992static PyObject *
4993posix_waitpid(PyObject *self, PyObject *args)
4994{
4995 pid_t pid;
4996 int options;
4997 WAIT_TYPEint status;
4998 WAIT_STATUS_INT(status)(status) = 0;
4999
5000 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, _Py_PARSE_PID"i" "i:waitpid", &pid, &options))
5001 return NULL((void *)0);
5002 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5003 pid = waitpid(pid, &status, options);
5004 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5005 if (pid == -1)
5006 return posix_error();
5007
5008 return Py_BuildValue_Py_BuildValue_SizeT("Ni", PyLong_FromPidPyLong_FromLong(pid), WAIT_STATUS_INT(status)(status));
5009}
5010
5011#elif defined(HAVE_CWAIT)
5012
5013/* MS C has a variant of waitpid() that's usable for most purposes. */
5014PyDoc_STRVAR(posix_waitpid__doc__,static char posix_waitpid__doc__[] = "waitpid(pid, options) -> (pid, status << 8)\n\n"
"Wait for completion of a given process. options is ignored on Windows."
5015"waitpid(pid, options) -> (pid, status << 8)\n\n"static char posix_waitpid__doc__[] = "waitpid(pid, options) -> (pid, status << 8)\n\n"
"Wait for completion of a given process. options is ignored on Windows."
5016"Wait for completion of a given process. options is ignored on Windows.")static char posix_waitpid__doc__[] = "waitpid(pid, options) -> (pid, status << 8)\n\n"
"Wait for completion of a given process. options is ignored on Windows."
;
5017
5018static PyObject *
5019posix_waitpid(PyObject *self, PyObject *args)
5020{
5021 Py_intptr_t pid;
5022 int status, options;
5023
5024 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, _Py_PARSE_PID"i" "i:waitpid", &pid, &options))
5025 return NULL((void *)0);
5026 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5027 pid = _cwait(&status, pid, options);
5028 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5029 if (pid == -1)
5030 return posix_error();
5031
5032 /* shift the status left a byte so this is more like the POSIX waitpid */
5033 return Py_BuildValue_Py_BuildValue_SizeT("Ni", PyLong_FromPidPyLong_FromLong(pid), status << 8);
5034}
5035#endif /* HAVE_WAITPID || HAVE_CWAIT */
5036
5037#ifdef HAVE_WAIT1
5038PyDoc_STRVAR(posix_wait__doc__,static char posix_wait__doc__[] = "wait() -> (pid, status)\n\nWait for completion of a child process."
5039"wait() -> (pid, status)\n\n\static char posix_wait__doc__[] = "wait() -> (pid, status)\n\nWait for completion of a child process."
5040Wait for completion of a child process.")static char posix_wait__doc__[] = "wait() -> (pid, status)\n\nWait for completion of a child process.";
5041
5042static PyObject *
5043posix_wait(PyObject *self, PyObject *noargs)
5044{
5045 pid_t pid;
5046 WAIT_TYPEint status;
5047 WAIT_STATUS_INT(status)(status) = 0;
5048
5049 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5050 pid = wait(&status);
5051 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5052 if (pid == -1)
5053 return posix_error();
5054
5055 return Py_BuildValue_Py_BuildValue_SizeT("Ni", PyLong_FromPidPyLong_FromLong(pid), WAIT_STATUS_INT(status)(status));
5056}
5057#endif
5058
5059
5060PyDoc_STRVAR(posix_lstat__doc__,static char posix_lstat__doc__[] = "lstat(path) -> stat result\n\nLike stat(path), but do not follow symbolic links."
5061"lstat(path) -> stat result\n\n\static char posix_lstat__doc__[] = "lstat(path) -> stat result\n\nLike stat(path), but do not follow symbolic links."
5062Like stat(path), but do not follow symbolic links.")static char posix_lstat__doc__[] = "lstat(path) -> stat result\n\nLike stat(path), but do not follow symbolic links.";
5063
5064static PyObject *
5065posix_lstat(PyObject *self, PyObject *args)
5066{
5067#ifdef HAVE_LSTAT1
5068 return posix_do_stat(self, args, "O&:lstat", lstat, NULL((void *)0), NULL((void *)0));
5069#else /* !HAVE_LSTAT */
5070#ifdef MS_WINDOWS
5071 return posix_do_stat(self, args, "O&:lstat", STATstat, "U:lstat",
5072 win32_lstat_w);
5073#else
5074 return posix_do_stat(self, args, "O&:lstat", STATstat, NULL((void *)0), NULL((void *)0));
5075#endif
5076#endif /* !HAVE_LSTAT */
5077}
5078
5079
5080#ifdef HAVE_READLINK1
5081PyDoc_STRVAR(posix_readlink__doc__,static char posix_readlink__doc__[] = "readlink(path) -> path\n\nReturn a string representing the path to which the symbolic link points."
5082"readlink(path) -> path\n\n\static char posix_readlink__doc__[] = "readlink(path) -> path\n\nReturn a string representing the path to which the symbolic link points."
5083Return a string representing the path to which the symbolic link points.")static char posix_readlink__doc__[] = "readlink(path) -> path\n\nReturn a string representing the path to which the symbolic link points.";
5084
5085static PyObject *
5086posix_readlink(PyObject *self, PyObject *args)
5087{
5088 PyObject* v;
5089 char buf[MAXPATHLEN1024];
5090 PyObject *opath;
5091 char *path;
5092 int n;
5093 int arg_is_unicode = 0;
5094
5095 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&:readlink",
5096 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath))
5097 return NULL((void *)0);
5098 path = PyBytes_AsString(opath);
5099 v = PySequence_GetItem(args, 0);
5100 if (v == NULL((void *)0)) {
5101 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 5101, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
5102 return NULL((void *)0);
5103 }
5104
5105 if (PyUnicode_Check(v)((((((PyObject*)(v))->ob_type))->tp_flags & ((1L<<
28))) != 0)
) {
5106 arg_is_unicode = 1;
5107 }
5108 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 5108, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
5109
5110 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5111 n = readlink(path, buf, (int) sizeof buf);
5112 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5113 if (n < 0)
5114 return posix_error_with_allocated_filename(opath);
5115
5116 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 5116, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
5117 if (arg_is_unicode)
5118 return PyUnicode_DecodeFSDefaultAndSizePyUnicodeUCS2_DecodeFSDefaultAndSize(buf, n);
5119 else
5120 return PyBytes_FromStringAndSize(buf, n);
5121}
5122#endif /* HAVE_READLINK */
5123
5124
5125#if defined(HAVE_SYMLINK1) && !defined(MS_WINDOWS)
5126PyDoc_STRVAR(posix_symlink__doc__,static char posix_symlink__doc__[] = "symlink(src, dst)\n\nCreate a symbolic link pointing to src named dst."
5127"symlink(src, dst)\n\n\static char posix_symlink__doc__[] = "symlink(src, dst)\n\nCreate a symbolic link pointing to src named dst."
5128Create a symbolic link pointing to src named dst.")static char posix_symlink__doc__[] = "symlink(src, dst)\n\nCreate a symbolic link pointing to src named dst.";
5129
5130static PyObject *
5131posix_symlink(PyObject *self, PyObject *args)
5132{
5133 return posix_2str(args, "O&O&:symlink", symlink);
5134}
5135#endif /* HAVE_SYMLINK */
5136
5137#if !defined(HAVE_READLINK1) && defined(MS_WINDOWS)
5138
5139PyDoc_STRVAR(win_readlink__doc__,static char win_readlink__doc__[] = "readlink(path) -> path\n\nReturn a string representing the path to which the symbolic link points."
5140"readlink(path) -> path\n\n\static char win_readlink__doc__[] = "readlink(path) -> path\n\nReturn a string representing the path to which the symbolic link points."
5141Return a string representing the path to which the symbolic link points.")static char win_readlink__doc__[] = "readlink(path) -> path\n\nReturn a string representing the path to which the symbolic link points.";
5142
5143/* Windows readlink implementation */
5144static PyObject *
5145win_readlink(PyObject *self, PyObject *args)
5146{
5147 wchar_t *path;
5148 DWORD n_bytes_returned;
5149 DWORD io_result;
5150 PyObject *result;
5151 HANDLE reparse_point_handle;
5152
5153 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5154 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5155 wchar_t *print_name;
5156
5157 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args,
5158 "u:readlink",
5159 &path))
5160 return NULL((void *)0);
5161
5162 /* First get a handle to the reparse point */
5163 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5164 reparse_point_handle = CreateFileW(
5165 path,
5166 0,
5167 0,
5168 0,
5169 OPEN_EXISTING,
5170 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5171 0);
5172 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5173
5174 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5175 {
5176 return win32_error_unicode("readlink", path);
5177 }
5178
5179 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5180 /* New call DeviceIoControl to read the reparse point */
5181 io_result = DeviceIoControl(
5182 reparse_point_handle,
5183 FSCTL_GET_REPARSE_POINT,
5184 0, 0, /* in buffer */
5185 target_buffer, sizeof(target_buffer),
5186 &n_bytes_returned,
5187 0 /* we're not using OVERLAPPED_IO */
5188 );
5189 CloseHandle(reparse_point_handle);
5190 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5191
5192 if (io_result==0)
5193 {
5194 return win32_error_unicode("readlink", path);
5195 }
5196
5197 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5198 {
5199 PyErr_SetString(PyExc_ValueError,
5200 "not a symbolic link");
5201 return NULL((void *)0);
5202 }
5203 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5204 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5205
5206 result = PyUnicode_FromWideCharPyUnicodeUCS2_FromWideChar(print_name,
5207 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
5208 return result;
5209}
5210
5211#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5212
5213#if defined(HAVE_SYMLINK1) && defined(MS_WINDOWS)
5214
5215/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5216static int has_CreateSymbolicLinkW = 0;
5217static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5218static int
5219check_CreateSymbolicLinkW()
5220{
5221 HINSTANCE hKernel32;
5222 /* only recheck */
5223 if (has_CreateSymbolicLinkW)
5224 return has_CreateSymbolicLinkW;
5225 hKernel32 = GetModuleHandle("KERNEL32");
5226 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5227 "CreateSymbolicLinkW");
5228 if (Py_CreateSymbolicLinkW)
5229 has_CreateSymbolicLinkW = 1;
5230 return has_CreateSymbolicLinkW;
5231}
5232
5233PyDoc_STRVAR(win_symlink__doc__,static char win_symlink__doc__[] = "symlink(src, dst, target_is_directory=False)\n\nCreate a symbolic link pointing to src named dst.\ntarget_is_directory is required if the target is to be interpreted as\na directory.\nThis function requires Windows 6.0 or greater, and raises a\nNotImplementedError otherwise."
5234"symlink(src, dst, target_is_directory=False)\n\n\static char win_symlink__doc__[] = "symlink(src, dst, target_is_directory=False)\n\nCreate a symbolic link pointing to src named dst.\ntarget_is_directory is required if the target is to be interpreted as\na directory.\nThis function requires Windows 6.0 or greater, and raises a\nNotImplementedError otherwise."
5235Create a symbolic link pointing to src named dst.\n\static char win_symlink__doc__[] = "symlink(src, dst, target_is_directory=False)\n\nCreate a symbolic link pointing to src named dst.\ntarget_is_directory is required if the target is to be interpreted as\na directory.\nThis function requires Windows 6.0 or greater, and raises a\nNotImplementedError otherwise."
5236target_is_directory is required if the target is to be interpreted as\n\static char win_symlink__doc__[] = "symlink(src, dst, target_is_directory=False)\n\nCreate a symbolic link pointing to src named dst.\ntarget_is_directory is required if the target is to be interpreted as\na directory.\nThis function requires Windows 6.0 or greater, and raises a\nNotImplementedError otherwise."
5237a directory.\n\static char win_symlink__doc__[] = "symlink(src, dst, target_is_directory=False)\n\nCreate a symbolic link pointing to src named dst.\ntarget_is_directory is required if the target is to be interpreted as\na directory.\nThis function requires Windows 6.0 or greater, and raises a\nNotImplementedError otherwise."
5238This function requires Windows 6.0 or greater, and raises a\n\static char win_symlink__doc__[] = "symlink(src, dst, target_is_directory=False)\n\nCreate a symbolic link pointing to src named dst.\ntarget_is_directory is required if the target is to be interpreted as\na directory.\nThis function requires Windows 6.0 or greater, and raises a\nNotImplementedError otherwise."
5239NotImplementedError otherwise.")static char win_symlink__doc__[] = "symlink(src, dst, target_is_directory=False)\n\nCreate a symbolic link pointing to src named dst.\ntarget_is_directory is required if the target is to be interpreted as\na directory.\nThis function requires Windows 6.0 or greater, and raises a\nNotImplementedError otherwise.";
5240
5241static PyObject *
5242win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5243{
5244 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL((void *)0)};
5245 PyObject *src, *dest;
5246 int target_is_directory = 0;
5247 DWORD res;
5248 WIN32_FILE_ATTRIBUTE_DATA src_info;
5249
5250 if (!check_CreateSymbolicLinkW())
5251 {
5252 /* raise NotImplementedError */
5253 return PyErr_Format(PyExc_NotImplementedError,
5254 "CreateSymbolicLinkW not found");
5255 }
5256 if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwargs, "OO|i:symlink",
5257 kwlist, &src, &dest, &target_is_directory))
5258 return NULL((void *)0);
5259
5260 if (win32_can_symlink == 0)
5261 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5262
5263 if (!convert_to_unicode(&src)) { return NULL((void *)0); }
5264 if (!convert_to_unicode(&dest)) {
5265 Py_DECREF(src)do { if (_Py_RefTotal-- , --((PyObject*)(src))->ob_refcnt !=
0) { if (((PyObject*)src)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 5265, (PyObject *)(src)); } else _Py_Dealloc
((PyObject *)(src)); } while (0)
;
5266 return NULL((void *)0);
5267 }
5268
5269 /* if src is a directory, ensure target_is_directory==1 */
5270 if(
5271 GetFileAttributesExW(
5272 PyUnicode_AsUnicodePyUnicodeUCS2_AsUnicode(src), GetFileExInfoStandard, &src_info
5273 ))
5274 {
5275 target_is_directory = target_is_directory ||
5276 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5277 }
5278
5279 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5280 res = Py_CreateSymbolicLinkW(
5281 PyUnicode_AsUnicodePyUnicodeUCS2_AsUnicode(dest),
5282 PyUnicode_AsUnicodePyUnicodeUCS2_AsUnicode(src),
5283 target_is_directory);
5284 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5285 Py_DECREF(src)do { if (_Py_RefTotal-- , --((PyObject*)(src))->ob_refcnt !=
0) { if (((PyObject*)src)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 5285, (PyObject *)(src)); } else _Py_Dealloc
((PyObject *)(src)); } while (0)
;
5286 Py_DECREF(dest)do { if (_Py_RefTotal-- , --((PyObject*)(dest))->ob_refcnt
!= 0) { if (((PyObject*)dest)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 5286, (PyObject *)(dest)); } else
_Py_Dealloc((PyObject *)(dest)); } while (0)
;
5287 if (!res)
5288 {
5289 return win32_error_unicode("symlink", PyUnicode_AsUnicodePyUnicodeUCS2_AsUnicode(src));
5290 }
5291
5292 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
5293 return Py_None(&_Py_NoneStruct);
5294}
5295#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
5296
5297#ifdef HAVE_TIMES1
5298#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5299static long
5300system_uptime(void)
5301{
5302 ULONG value = 0;
5303
5304 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5305 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5306 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5307
5308 return value;
5309}
5310
5311static PyObject *
5312posix_times(PyObject *self, PyObject *noargs)
5313{
5314 /* Currently Only Uptime is Provided -- Others Later */
5315 return Py_BuildValue_Py_BuildValue_SizeT("ddddd",
5316 (double)0 /* t.tms_utime / HZ */,
5317 (double)0 /* t.tms_stime / HZ */,
5318 (double)0 /* t.tms_cutime / HZ */,
5319 (double)0 /* t.tms_cstime / HZ */,
5320 (double)system_uptime() / 1000);
5321}
5322#else /* not OS2 */
5323#define NEED_TICKS_PER_SECOND
5324static long ticks_per_second = -1;
5325static PyObject *
5326posix_times(PyObject *self, PyObject *noargs)
5327{
5328 struct tms t;
5329 clock_t c;
5330 errno(*__error()) = 0;
5331 c = times(&t);
5332 if (c == (clock_t) -1)
5333 return posix_error();
5334 return Py_BuildValue_Py_BuildValue_SizeT("ddddd",
5335 (double)t.tms_utime / ticks_per_second,
5336 (double)t.tms_stime / ticks_per_second,
5337 (double)t.tms_cutime / ticks_per_second,
5338 (double)t.tms_cstime / ticks_per_second,
5339 (double)c / ticks_per_second);
5340}
5341#endif /* not OS2 */
5342#endif /* HAVE_TIMES */
5343
5344
5345#ifdef MS_WINDOWS
5346#define HAVE_TIMES1 /* so the method table will pick it up */
5347static PyObject *
5348posix_times(PyObject *self, PyObject *noargs)
5349{
5350 FILETIME create, exit, kernel, user;
5351 HANDLE hProc;
5352 hProc = GetCurrentProcess();
5353 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5354 /* The fields of a FILETIME structure are the hi and lo part
5355 of a 64-bit value expressed in 100 nanosecond units.
5356 1e7 is one second in such units; 1e-7 the inverse.
5357 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5358 */
5359 return Py_BuildValue_Py_BuildValue_SizeT(
5360 "ddddd",
5361 (double)(user.dwHighDateTime*429.4967296 +
5362 user.dwLowDateTime*1e-7),
5363 (double)(kernel.dwHighDateTime*429.4967296 +
5364 kernel.dwLowDateTime*1e-7),
5365 (double)0,
5366 (double)0,
5367 (double)0);
5368}
5369#endif /* MS_WINDOWS */
5370
5371#ifdef HAVE_TIMES1
5372PyDoc_STRVAR(posix_times__doc__,static char posix_times__doc__[] = "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\nReturn a tuple of floating point numbers indicating process times."
5373"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\static char posix_times__doc__[] = "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\nReturn a tuple of floating point numbers indicating process times."
5374Return a tuple of floating point numbers indicating process times.")static char posix_times__doc__[] = "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\nReturn a tuple of floating point numbers indicating process times.";
5375#endif
5376
5377
5378#ifdef HAVE_GETSID1
5379PyDoc_STRVAR(posix_getsid__doc__,static char posix_getsid__doc__[] = "getsid(pid) -> sid\n\nCall the system call getsid()."
5380"getsid(pid) -> sid\n\n\static char posix_getsid__doc__[] = "getsid(pid) -> sid\n\nCall the system call getsid()."
5381Call the system call getsid().")static char posix_getsid__doc__[] = "getsid(pid) -> sid\n\nCall the system call getsid().";
5382
5383static PyObject *
5384posix_getsid(PyObject *self, PyObject *args)
5385{
5386 pid_t pid;
5387 int sid;
5388 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, _Py_PARSE_PID"i" ":getsid", &pid))
5389 return NULL((void *)0);
5390 sid = getsid(pid);
5391 if (sid < 0)
5392 return posix_error();
5393 return PyLong_FromLong((long)sid);
5394}
5395#endif /* HAVE_GETSID */
5396
5397
5398#ifdef HAVE_SETSID1
5399PyDoc_STRVAR(posix_setsid__doc__,static char posix_setsid__doc__[] = "setsid()\n\nCall the system call setsid()."
5400"setsid()\n\n\static char posix_setsid__doc__[] = "setsid()\n\nCall the system call setsid()."
5401Call the system call setsid().")static char posix_setsid__doc__[] = "setsid()\n\nCall the system call setsid().";
5402
5403static PyObject *
5404posix_setsid(PyObject *self, PyObject *noargs)
5405{
5406 if (setsid() < 0)
5407 return posix_error();
5408 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
5409 return Py_None(&_Py_NoneStruct);
5410}
5411#endif /* HAVE_SETSID */
5412
5413#ifdef HAVE_SETPGID1
5414PyDoc_STRVAR(posix_setpgid__doc__,static char posix_setpgid__doc__[] = "setpgid(pid, pgrp)\n\nCall the system call setpgid()."
5415"setpgid(pid, pgrp)\n\n\static char posix_setpgid__doc__[] = "setpgid(pid, pgrp)\n\nCall the system call setpgid()."
5416Call the system call setpgid().")static char posix_setpgid__doc__[] = "setpgid(pid, pgrp)\n\nCall the system call setpgid().";
5417
5418static PyObject *
5419posix_setpgid(PyObject *self, PyObject *args)
5420{
5421 pid_t pid;
5422 int pgrp;
5423 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, _Py_PARSE_PID"i" "i:setpgid", &pid, &pgrp))
5424 return NULL((void *)0);
5425 if (setpgid(pid, pgrp) < 0)
5426 return posix_error();
5427 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
5428 return Py_None(&_Py_NoneStruct);
5429}
5430#endif /* HAVE_SETPGID */
5431
5432
5433#ifdef HAVE_TCGETPGRP1
5434PyDoc_STRVAR(posix_tcgetpgrp__doc__,static char posix_tcgetpgrp__doc__[] = "tcgetpgrp(fd) -> pgid\n\nReturn the process group associated with the terminal given by a fd."
5435"tcgetpgrp(fd) -> pgid\n\n\static char posix_tcgetpgrp__doc__[] = "tcgetpgrp(fd) -> pgid\n\nReturn the process group associated with the terminal given by a fd."
5436Return the process group associated with the terminal given by a fd.")static char posix_tcgetpgrp__doc__[] = "tcgetpgrp(fd) -> pgid\n\nReturn the process group associated with the terminal given by a fd.";
5437
5438static PyObject *
5439posix_tcgetpgrp(PyObject *self, PyObject *args)
5440{
5441 int fd;
5442 pid_t pgid;
5443 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:tcgetpgrp", &fd))
5444 return NULL((void *)0);
5445 pgid = tcgetpgrp(fd);
5446 if (pgid < 0)
5447 return posix_error();
5448 return PyLong_FromPidPyLong_FromLong(pgid);
5449}
5450#endif /* HAVE_TCGETPGRP */
5451
5452
5453#ifdef HAVE_TCSETPGRP1
5454PyDoc_STRVAR(posix_tcsetpgrp__doc__,static char posix_tcsetpgrp__doc__[] = "tcsetpgrp(fd, pgid)\n\nSet the process group associated with the terminal given by a fd."
5455"tcsetpgrp(fd, pgid)\n\n\static char posix_tcsetpgrp__doc__[] = "tcsetpgrp(fd, pgid)\n\nSet the process group associated with the terminal given by a fd."
5456Set the process group associated with the terminal given by a fd.")static char posix_tcsetpgrp__doc__[] = "tcsetpgrp(fd, pgid)\n\nSet the process group associated with the terminal given by a fd.";
5457
5458static PyObject *
5459posix_tcsetpgrp(PyObject *self, PyObject *args)
5460{
5461 int fd;
5462 pid_t pgid;
5463 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i" _Py_PARSE_PID"i" ":tcsetpgrp", &fd, &pgid))
5464 return NULL((void *)0);
5465 if (tcsetpgrp(fd, pgid) < 0)
5466 return posix_error();
5467 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
5468 return Py_None(&_Py_NoneStruct);
5469}
5470#endif /* HAVE_TCSETPGRP */
5471
5472/* Functions acting on file descriptors */
5473
5474PyDoc_STRVAR(posix_open__doc__,static char posix_open__doc__[] = "open(filename, flag [, mode=0777]) -> fd\n\nOpen a file (for low level IO)."
5475"open(filename, flag [, mode=0777]) -> fd\n\n\static char posix_open__doc__[] = "open(filename, flag [, mode=0777]) -> fd\n\nOpen a file (for low level IO)."
5476Open a file (for low level IO).")static char posix_open__doc__[] = "open(filename, flag [, mode=0777]) -> fd\n\nOpen a file (for low level IO).";
5477
5478static PyObject *
5479posix_open(PyObject *self, PyObject *args)
5480{
5481 PyObject *ofile;
5482 char *file;
5483 int flag;
5484 int mode = 0777;
5485 int fd;
5486
5487#ifdef MS_WINDOWS
5488 PyUnicodeObject *po;
5489 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5490 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5491 /* PyUnicode_AS_UNICODE OK without thread
5492 lock as it is a simple dereference. */
5493 fd = _wopen(PyUnicode_AS_UNICODE(po)((__builtin_expect(!(((((((PyObject*)(po))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 5493, "PyUnicode_Check(po)") : (
void)0),(((PyUnicodeObject *)(po))->str))
, flag, mode);
5494 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5495 if (fd < 0)
5496 return posix_error();
5497 return PyLong_FromLong((long)fd);
5498 }
5499 /* Drop the argument parsing error as narrow strings
5500 are also valid. */
5501 PyErr_Clear();
5502#endif
5503
5504 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&i|i",
5505 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &ofile,
5506 &flag, &mode))
5507 return NULL((void *)0);
5508 file = PyBytes_AsString(ofile);
5509 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5510 fd = open(file, flag, mode);
5511 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5512 if (fd < 0)
5513 return posix_error_with_allocated_filename(ofile);
5514 Py_DECREF(ofile)do { if (_Py_RefTotal-- , --((PyObject*)(ofile))->ob_refcnt
!= 0) { if (((PyObject*)ofile)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 5514, (PyObject *)(ofile)); } else
_Py_Dealloc((PyObject *)(ofile)); } while (0)
;
5515 return PyLong_FromLong((long)fd);
5516}
5517
5518
5519PyDoc_STRVAR(posix_close__doc__,static char posix_close__doc__[] = "close(fd)\n\nClose a file descriptor (for low level IO)."
5520"close(fd)\n\n\static char posix_close__doc__[] = "close(fd)\n\nClose a file descriptor (for low level IO)."
5521Close a file descriptor (for low level IO).")static char posix_close__doc__[] = "close(fd)\n\nClose a file descriptor (for low level IO).";
5522
5523static PyObject *
5524posix_close(PyObject *self, PyObject *args)
5525{
5526 int fd, res;
5527 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:close", &fd))
5528 return NULL((void *)0);
5529 if (!_PyVerify_fd(fd)(1))
5530 return posix_error();
5531 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5532 res = close(fd);
5533 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5534 if (res < 0)
5535 return posix_error();
5536 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
5537 return Py_None(&_Py_NoneStruct);
5538}
5539
5540
5541PyDoc_STRVAR(posix_closerange__doc__,static char posix_closerange__doc__[] = "closerange(fd_low, fd_high)\n\nCloses all file descriptors in [fd_low, fd_high), ignoring errors."
5542"closerange(fd_low, fd_high)\n\n\static char posix_closerange__doc__[] = "closerange(fd_low, fd_high)\n\nCloses all file descriptors in [fd_low, fd_high), ignoring errors."
5543Closes all file descriptors in [fd_low, fd_high), ignoring errors.")static char posix_closerange__doc__[] = "closerange(fd_low, fd_high)\n\nCloses all file descriptors in [fd_low, fd_high), ignoring errors.";
5544
5545static PyObject *
5546posix_closerange(PyObject *self, PyObject *args)
5547{
5548 int fd_from, fd_to, i;
5549 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ii:closerange", &fd_from, &fd_to))
5550 return NULL((void *)0);
5551 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5552 for (i = fd_from; i < fd_to; i++)
5553 if (_PyVerify_fd(i)(1))
5554 close(i);
5555 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5556 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
5557}
5558
5559
5560PyDoc_STRVAR(posix_dup__doc__,static char posix_dup__doc__[] = "dup(fd) -> fd2\n\nReturn a duplicate of a file descriptor."
5561"dup(fd) -> fd2\n\n\static char posix_dup__doc__[] = "dup(fd) -> fd2\n\nReturn a duplicate of a file descriptor."
5562Return a duplicate of a file descriptor.")static char posix_dup__doc__[] = "dup(fd) -> fd2\n\nReturn a duplicate of a file descriptor.";
5563
5564static PyObject *
5565posix_dup(PyObject *self, PyObject *args)
5566{
5567 int fd;
5568 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:dup", &fd))
5569 return NULL((void *)0);
5570 if (!_PyVerify_fd(fd)(1))
5571 return posix_error();
5572 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5573 fd = dup(fd);
5574 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5575 if (fd < 0)
5576 return posix_error();
5577 return PyLong_FromLong((long)fd);
5578}
5579
5580
5581PyDoc_STRVAR(posix_dup2__doc__,static char posix_dup2__doc__[] = "dup2(old_fd, new_fd)\n\nDuplicate file descriptor."
5582"dup2(old_fd, new_fd)\n\n\static char posix_dup2__doc__[] = "dup2(old_fd, new_fd)\n\nDuplicate file descriptor."
5583Duplicate file descriptor.")static char posix_dup2__doc__[] = "dup2(old_fd, new_fd)\n\nDuplicate file descriptor.";
5584
5585static PyObject *
5586posix_dup2(PyObject *self, PyObject *args)
5587{
5588 int fd, fd2, res;
5589 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ii:dup2", &fd, &fd2))
5590 return NULL((void *)0);
5591 if (!_PyVerify_fd_dup2(fd, fd2)(1))
5592 return posix_error();
5593 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5594 res = dup2(fd, fd2);
5595 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5596 if (res < 0)
5597 return posix_error();
5598 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
5599 return Py_None(&_Py_NoneStruct);
5600}
5601
5602
5603PyDoc_STRVAR(posix_lseek__doc__,static char posix_lseek__doc__[] = "lseek(fd, pos, how) -> newpos\n\nSet the current position of a file descriptor."
5604"lseek(fd, pos, how) -> newpos\n\n\static char posix_lseek__doc__[] = "lseek(fd, pos, how) -> newpos\n\nSet the current position of a file descriptor."
5605Set the current position of a file descriptor.")static char posix_lseek__doc__[] = "lseek(fd, pos, how) -> newpos\n\nSet the current position of a file descriptor.";
5606
5607static PyObject *
5608posix_lseek(PyObject *self, PyObject *args)
5609{
5610 int fd, how;
5611#if defined(MS_WIN64) || defined(MS_WINDOWS)
5612 PY_LONG_LONGlong long pos, res;
5613#else
5614 off_t pos, res;
5615#endif
5616 PyObject *posobj;
5617 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "iOi:lseek", &fd, &posobj, &how))
5618 return NULL((void *)0);
5619#ifdef SEEK_SET0
5620 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5621 switch (how) {
5622 case 0: how = SEEK_SET0; break;
5623 case 1: how = SEEK_CUR1; break;
5624 case 2: how = SEEK_END2; break;
5625 }
5626#endif /* SEEK_END */
5627
5628#if !defined(HAVE_LARGEFILE_SUPPORT)
5629 pos = PyLong_AsLong(posobj);
5630#else
5631 pos = PyLong_Check(posobj)((((((PyObject*)(posobj))->ob_type))->tp_flags & ((
1L<<24))) != 0)
?
5632 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
5633#endif
5634 if (PyErr_Occurred())
5635 return NULL((void *)0);
5636
5637 if (!_PyVerify_fd(fd)(1))
5638 return posix_error();
5639 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5640#if defined(MS_WIN64) || defined(MS_WINDOWS)
5641 res = _lseeki64(fd, pos, how);
5642#else
5643 res = lseek(fd, pos, how);
5644#endif
5645 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5646 if (res < 0)
5647 return posix_error();
5648
5649#if !defined(HAVE_LARGEFILE_SUPPORT)
5650 return PyLong_FromLong(res);
5651#else
5652 return PyLong_FromLongLong(res);
5653#endif
5654}
5655
5656
5657PyDoc_STRVAR(posix_read__doc__,static char posix_read__doc__[] = "read(fd, buffersize) -> string\n\nRead a file descriptor."
5658"read(fd, buffersize) -> string\n\n\static char posix_read__doc__[] = "read(fd, buffersize) -> string\n\nRead a file descriptor."
5659Read a file descriptor.")static char posix_read__doc__[] = "read(fd, buffersize) -> string\n\nRead a file descriptor.";
5660
5661static PyObject *
5662posix_read(PyObject *self, PyObject *args)
5663{
5664 int fd, size;
5665 Py_ssize_t n;
5666 PyObject *buffer;
5667 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ii:read", &fd, &size))
5668 return NULL((void *)0);
5669 if (size < 0) {
5670 errno(*__error()) = EINVAL22;
5671 return posix_error();
5672 }
5673 buffer = PyBytes_FromStringAndSize((char *)NULL((void *)0), size);
5674 if (buffer == NULL((void *)0))
5675 return NULL((void *)0);
5676 if (!_PyVerify_fd(fd)(1)) {
5677 Py_DECREF(buffer)do { if (_Py_RefTotal-- , --((PyObject*)(buffer))->ob_refcnt
!= 0) { if (((PyObject*)buffer)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 5677, (PyObject *)(buffer)); } else
_Py_Dealloc((PyObject *)(buffer)); } while (0)
;
5678 return posix_error();
5679 }
5680 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5681 n = read(fd, PyBytes_AS_STRING(buffer)((__builtin_expect(!(((((((PyObject*)(buffer))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 5681, "PyBytes_Check(buffer)") :
(void)0), (((PyBytesObject *)(buffer))->ob_sval))
, size);
5682 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5683 if (n < 0) {
5684 Py_DECREF(buffer)do { if (_Py_RefTotal-- , --((PyObject*)(buffer))->ob_refcnt
!= 0) { if (((PyObject*)buffer)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 5684, (PyObject *)(buffer)); } else
_Py_Dealloc((PyObject *)(buffer)); } while (0)
;
5685 return posix_error();
5686 }
5687 if (n != size)
5688 _PyBytes_Resize(&buffer, n);
5689 return buffer;
5690}
5691
5692
5693PyDoc_STRVAR(posix_write__doc__,static char posix_write__doc__[] = "write(fd, string) -> byteswritten\n\nWrite a string to a file descriptor."
5694"write(fd, string) -> byteswritten\n\n\static char posix_write__doc__[] = "write(fd, string) -> byteswritten\n\nWrite a string to a file descriptor."
5695Write a string to a file descriptor.")static char posix_write__doc__[] = "write(fd, string) -> byteswritten\n\nWrite a string to a file descriptor.";
5696
5697static PyObject *
5698posix_write(PyObject *self, PyObject *args)
5699{
5700 Py_buffer pbuf;
5701 int fd;
5702 Py_ssize_t size, len;
5703
5704 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "iy*:write", &fd, &pbuf))
5705 return NULL((void *)0);
5706 if (!_PyVerify_fd(fd)(1)) {
5707 PyBuffer_Release(&pbuf);
5708 return posix_error();
5709 }
5710 len = pbuf.len;
5711 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5712#if defined(MS_WIN64) || defined(MS_WINDOWS)
5713 if (len > INT_MAX2147483647)
5714 len = INT_MAX2147483647;
5715 size = write(fd, pbuf.buf, (int)len);
5716#else
5717 size = write(fd, pbuf.buf, len);
5718#endif
5719 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5720 PyBuffer_Release(&pbuf);
5721 if (size < 0)
5722 return posix_error();
5723 return PyLong_FromSsize_t(size);
5724}
5725
5726
5727PyDoc_STRVAR(posix_fstat__doc__,static char posix_fstat__doc__[] = "fstat(fd) -> stat result\n\nLike stat(), but for an open file descriptor."
5728"fstat(fd) -> stat result\n\n\static char posix_fstat__doc__[] = "fstat(fd) -> stat result\n\nLike stat(), but for an open file descriptor."
5729Like stat(), but for an open file descriptor.")static char posix_fstat__doc__[] = "fstat(fd) -> stat result\n\nLike stat(), but for an open file descriptor.";
5730
5731static PyObject *
5732posix_fstat(PyObject *self, PyObject *args)
5733{
5734 int fd;
5735 STRUCT_STATstruct stat st;
5736 int res;
5737 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:fstat", &fd))
5738 return NULL((void *)0);
5739#ifdef __VMS
5740 /* on OpenVMS we must ensure that all bytes are written to the file */
5741 fsync(fd);
5742#endif
5743 if (!_PyVerify_fd(fd)(1))
5744 return posix_error();
5745 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5746 res = FSTATfstat(fd, &st);
5747 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5748 if (res != 0) {
5749#ifdef MS_WINDOWS
5750 return win32_error("fstat", NULL((void *)0));
5751#else
5752 return posix_error();
5753#endif
5754 }
5755
5756 return _pystat_fromstructstat(&st);
5757}
5758
5759PyDoc_STRVAR(posix_isatty__doc__,static char posix_isatty__doc__[] = "isatty(fd) -> bool\n\nReturn True if the file descriptor 'fd' is an open file descriptor\nconnected to the slave end of a terminal."
5760"isatty(fd) -> bool\n\n\static char posix_isatty__doc__[] = "isatty(fd) -> bool\n\nReturn True if the file descriptor 'fd' is an open file descriptor\nconnected to the slave end of a terminal."
5761Return True if the file descriptor 'fd' is an open file descriptor\n\static char posix_isatty__doc__[] = "isatty(fd) -> bool\n\nReturn True if the file descriptor 'fd' is an open file descriptor\nconnected to the slave end of a terminal."
5762connected to the slave end of a terminal.")static char posix_isatty__doc__[] = "isatty(fd) -> bool\n\nReturn True if the file descriptor 'fd' is an open file descriptor\nconnected to the slave end of a terminal.";
5763
5764static PyObject *
5765posix_isatty(PyObject *self, PyObject *args)
5766{
5767 int fd;
5768 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:isatty", &fd))
5769 return NULL((void *)0);
5770 if (!_PyVerify_fd(fd)(1))
5771 return PyBool_FromLong(0);
5772 return PyBool_FromLong(isatty(fd));
5773}
5774
5775#ifdef HAVE_PIPE1
5776PyDoc_STRVAR(posix_pipe__doc__,static char posix_pipe__doc__[] = "pipe() -> (read_end, write_end)\n\nCreate a pipe."
5777"pipe() -> (read_end, write_end)\n\n\static char posix_pipe__doc__[] = "pipe() -> (read_end, write_end)\n\nCreate a pipe."
5778Create a pipe.")static char posix_pipe__doc__[] = "pipe() -> (read_end, write_end)\n\nCreate a pipe.";
5779
5780static PyObject *
5781posix_pipe(PyObject *self, PyObject *noargs)
5782{
5783#if defined(PYOS_OS2)
5784 HFILE read, write;
5785 APIRET rc;
5786
5787 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5788 rc = DosCreatePipe( &read, &write, 4096);
5789 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5790 if (rc != NO_ERROR)
5791 return os2_error(rc);
5792
5793 return Py_BuildValue_Py_BuildValue_SizeT("(ii)", read, write);
5794#else
5795#if !defined(MS_WINDOWS)
5796 int fds[2];
5797 int res;
5798 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5799 res = pipe(fds);
5800 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5801 if (res != 0)
5802 return posix_error();
5803 return Py_BuildValue_Py_BuildValue_SizeT("(ii)", fds[0], fds[1]);
5804#else /* MS_WINDOWS */
5805 HANDLE read, write;
5806 int read_fd, write_fd;
5807 BOOL ok;
5808 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5809 ok = CreatePipe(&read, &write, NULL((void *)0), 0);
5810 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5811 if (!ok)
5812 return win32_error("CreatePipe", NULL((void *)0));
5813 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5814 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5815 return Py_BuildValue_Py_BuildValue_SizeT("(ii)", read_fd, write_fd);
5816#endif /* MS_WINDOWS */
5817#endif
5818}
5819#endif /* HAVE_PIPE */
5820
5821
5822#ifdef HAVE_MKFIFO1
5823PyDoc_STRVAR(posix_mkfifo__doc__,static char posix_mkfifo__doc__[] = "mkfifo(filename [, mode=0666])\n\nCreate a FIFO (a POSIX named pipe)."
5824"mkfifo(filename [, mode=0666])\n\n\static char posix_mkfifo__doc__[] = "mkfifo(filename [, mode=0666])\n\nCreate a FIFO (a POSIX named pipe)."
5825Create a FIFO (a POSIX named pipe).")static char posix_mkfifo__doc__[] = "mkfifo(filename [, mode=0666])\n\nCreate a FIFO (a POSIX named pipe).";
5826
5827static PyObject *
5828posix_mkfifo(PyObject *self, PyObject *args)
5829{
5830 PyObject *opath;
5831 char *filename;
5832 int mode = 0666;
5833 int res;
5834 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&|i:mkfifo", PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath,
5835 &mode))
5836 return NULL((void *)0);
5837 filename = PyBytes_AS_STRING(opath)((__builtin_expect(!(((((((PyObject*)(opath))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 5837, "PyBytes_Check(opath)") : (
void)0), (((PyBytesObject *)(opath))->ob_sval))
;
5838 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5839 res = mkfifo(filename, mode);
5840 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5841 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 5841, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
5842 if (res < 0)
5843 return posix_error();
5844 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
5845 return Py_None(&_Py_NoneStruct);
5846}
5847#endif
5848
5849
5850#if defined(HAVE_MKNOD1) && defined(HAVE_MAKEDEV1)
5851PyDoc_STRVAR(posix_mknod__doc__,static char posix_mknod__doc__[] = "mknod(filename [, mode=0600, device])\n\nCreate a filesystem node (file, device special file or named pipe)\nnamed filename. mode specifies both the permissions to use and the\ntype of node to be created, being combined (bitwise OR) with one of\nS_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\ndevice defines the newly created device special file (probably using\nos.makedev()), otherwise it is ignored."
5852"mknod(filename [, mode=0600, device])\n\n\static char posix_mknod__doc__[] = "mknod(filename [, mode=0600, device])\n\nCreate a filesystem node (file, device special file or named pipe)\nnamed filename. mode specifies both the permissions to use and the\ntype of node to be created, being combined (bitwise OR) with one of\nS_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\ndevice defines the newly created device special file (probably using\nos.makedev()), otherwise it is ignored."
5853Create a filesystem node (file, device special file or named pipe)\n\static char posix_mknod__doc__[] = "mknod(filename [, mode=0600, device])\n\nCreate a filesystem node (file, device special file or named pipe)\nnamed filename. mode specifies both the permissions to use and the\ntype of node to be created, being combined (bitwise OR) with one of\nS_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\ndevice defines the newly created device special file (probably using\nos.makedev()), otherwise it is ignored."
5854named filename. mode specifies both the permissions to use and the\n\static char posix_mknod__doc__[] = "mknod(filename [, mode=0600, device])\n\nCreate a filesystem node (file, device special file or named pipe)\nnamed filename. mode specifies both the permissions to use and the\ntype of node to be created, being combined (bitwise OR) with one of\nS_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\ndevice defines the newly created device special file (probably using\nos.makedev()), otherwise it is ignored."
5855type of node to be created, being combined (bitwise OR) with one of\n\static char posix_mknod__doc__[] = "mknod(filename [, mode=0600, device])\n\nCreate a filesystem node (file, device special file or named pipe)\nnamed filename. mode specifies both the permissions to use and the\ntype of node to be created, being combined (bitwise OR) with one of\nS_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\ndevice defines the newly created device special file (probably using\nos.makedev()), otherwise it is ignored."
5856S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\static char posix_mknod__doc__[] = "mknod(filename [, mode=0600, device])\n\nCreate a filesystem node (file, device special file or named pipe)\nnamed filename. mode specifies both the permissions to use and the\ntype of node to be created, being combined (bitwise OR) with one of\nS_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\ndevice defines the newly created device special file (probably using\nos.makedev()), otherwise it is ignored."
5857device defines the newly created device special file (probably using\n\static char posix_mknod__doc__[] = "mknod(filename [, mode=0600, device])\n\nCreate a filesystem node (file, device special file or named pipe)\nnamed filename. mode specifies both the permissions to use and the\ntype of node to be created, being combined (bitwise OR) with one of\nS_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\ndevice defines the newly created device special file (probably using\nos.makedev()), otherwise it is ignored."
5858os.makedev()), otherwise it is ignored.")static char posix_mknod__doc__[] = "mknod(filename [, mode=0600, device])\n\nCreate a filesystem node (file, device special file or named pipe)\nnamed filename. mode specifies both the permissions to use and the\ntype of node to be created, being combined (bitwise OR) with one of\nS_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\ndevice defines the newly created device special file (probably using\nos.makedev()), otherwise it is ignored.";
5859
5860
5861static PyObject *
5862posix_mknod(PyObject *self, PyObject *args)
5863{
5864 PyObject *opath;
5865 char *filename;
5866 int mode = 0600;
5867 int device = 0;
5868 int res;
5869 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&|ii:mknod", PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &opath,
5870 &mode, &device))
5871 return NULL((void *)0);
5872 filename = PyBytes_AS_STRING(opath)((__builtin_expect(!(((((((PyObject*)(opath))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 5872, "PyBytes_Check(opath)") : (
void)0), (((PyBytesObject *)(opath))->ob_sval))
;
5873 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5874 res = mknod(filename, mode, device);
5875 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5876 Py_DECREF(opath)do { if (_Py_RefTotal-- , --((PyObject*)(opath))->ob_refcnt
!= 0) { if (((PyObject*)opath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 5876, (PyObject *)(opath)); } else
_Py_Dealloc((PyObject *)(opath)); } while (0)
;
5877 if (res < 0)
5878 return posix_error();
5879 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
5880 return Py_None(&_Py_NoneStruct);
5881}
5882#endif
5883
5884#ifdef HAVE_DEVICE_MACROS1
5885PyDoc_STRVAR(posix_major__doc__,static char posix_major__doc__[] = "major(device) -> major number\nExtracts a device major number from a raw device number."
5886"major(device) -> major number\n\static char posix_major__doc__[] = "major(device) -> major number\nExtracts a device major number from a raw device number."
5887Extracts a device major number from a raw device number.")static char posix_major__doc__[] = "major(device) -> major number\nExtracts a device major number from a raw device number.";
5888
5889static PyObject *
5890posix_major(PyObject *self, PyObject *args)
5891{
5892 int device;
5893 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:major", &device))
5894 return NULL((void *)0);
5895 return PyLong_FromLong((long)major(device)((int32_t)(((u_int32_t)(device) >> 24) & 0xff)));
5896}
5897
5898PyDoc_STRVAR(posix_minor__doc__,static char posix_minor__doc__[] = "minor(device) -> minor number\nExtracts a device minor number from a raw device number."
5899"minor(device) -> minor number\n\static char posix_minor__doc__[] = "minor(device) -> minor number\nExtracts a device minor number from a raw device number."
5900Extracts a device minor number from a raw device number.")static char posix_minor__doc__[] = "minor(device) -> minor number\nExtracts a device minor number from a raw device number.";
5901
5902static PyObject *
5903posix_minor(PyObject *self, PyObject *args)
5904{
5905 int device;
5906 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:minor", &device))
5907 return NULL((void *)0);
5908 return PyLong_FromLong((long)minor(device)((int32_t)((device) & 0xffffff)));
5909}
5910
5911PyDoc_STRVAR(posix_makedev__doc__,static char posix_makedev__doc__[] = "makedev(major, minor) -> device number\nComposes a raw device number from the major and minor device numbers."
5912"makedev(major, minor) -> device number\n\static char posix_makedev__doc__[] = "makedev(major, minor) -> device number\nComposes a raw device number from the major and minor device numbers."
5913Composes a raw device number from the major and minor device numbers.")static char posix_makedev__doc__[] = "makedev(major, minor) -> device number\nComposes a raw device number from the major and minor device numbers.";
5914
5915static PyObject *
5916posix_makedev(PyObject *self, PyObject *args)
5917{
5918 int major, minor;
5919 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ii:makedev", &major, &minor))
5920 return NULL((void *)0);
5921 return PyLong_FromLong((long)makedev(major, minor)((dev_t)(((major) << 24) | (minor))));
5922}
5923#endif /* device macros */
5924
5925
5926#ifdef HAVE_FTRUNCATE1
5927PyDoc_STRVAR(posix_ftruncate__doc__,static char posix_ftruncate__doc__[] = "ftruncate(fd, length)\n\nTruncate a file to a specified length."
5928"ftruncate(fd, length)\n\n\static char posix_ftruncate__doc__[] = "ftruncate(fd, length)\n\nTruncate a file to a specified length."
5929Truncate a file to a specified length.")static char posix_ftruncate__doc__[] = "ftruncate(fd, length)\n\nTruncate a file to a specified length.";
5930
5931static PyObject *
5932posix_ftruncate(PyObject *self, PyObject *args)
5933{
5934 int fd;
5935 off_t length;
5936 int res;
5937 PyObject *lenobj;
5938
5939 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "iO:ftruncate", &fd, &lenobj))
5940 return NULL((void *)0);
5941
5942#if !defined(HAVE_LARGEFILE_SUPPORT)
5943 length = PyLong_AsLong(lenobj);
5944#else
5945 length = PyLong_Check(lenobj)((((((PyObject*)(lenobj))->ob_type))->tp_flags & ((
1L<<24))) != 0)
?
5946 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
5947#endif
5948 if (PyErr_Occurred())
5949 return NULL((void *)0);
5950
5951 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
5952 res = ftruncate(fd, length);
5953 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
5954 if (res < 0)
5955 return posix_error();
5956 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
5957 return Py_None(&_Py_NoneStruct);
5958}
5959#endif
5960
5961#ifdef HAVE_PUTENV1
5962PyDoc_STRVAR(posix_putenv__doc__,static char posix_putenv__doc__[] = "putenv(key, value)\n\nChange or add an environment variable."
5963"putenv(key, value)\n\n\static char posix_putenv__doc__[] = "putenv(key, value)\n\nChange or add an environment variable."
5964Change or add an environment variable.")static char posix_putenv__doc__[] = "putenv(key, value)\n\nChange or add an environment variable.";
5965
5966/* Save putenv() parameters as values here, so we can collect them when they
5967 * get re-set with another call for the same key. */
5968static PyObject *posix_putenv_garbage;
5969
5970static PyObject *
5971posix_putenv(PyObject *self, PyObject *args)
5972{
5973#ifdef MS_WINDOWS
5974 wchar_t *s1, *s2;
5975 wchar_t *newenv;
5976#else
5977 PyObject *os1, *os2;
5978 char *s1, *s2;
5979 char *newenv;
5980#endif
5981 PyObject *newstr = NULL((void *)0);
5982 size_t len;
5983
5984#ifdef MS_WINDOWS
5985 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args,
5986 "uu:putenv",
5987 &s1, &s2))
5988 return NULL((void *)0);
5989#else
5990 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args,
5991 "O&O&:putenv",
5992 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &os1,
5993 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &os2))
5994 return NULL((void *)0);
5995 s1 = PyBytes_AsString(os1);
5996 s2 = PyBytes_AsString(os2);
5997#endif
5998
5999#if defined(PYOS_OS2)
6000 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6001 APIRET rc;
6002
6003 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6004 if (rc != NO_ERROR) {
6005 os2_error(rc);
6006 goto error;
6007 }
6008
6009 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6010 APIRET rc;
6011
6012 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6013 if (rc != NO_ERROR) {
6014 os2_error(rc);
6015 goto error;
6016 }
6017 } else {
6018#endif
6019 /* XXX This can leak memory -- not easy to fix :-( */
6020 /* len includes space for a trailing \0; the size arg to
6021 PyBytes_FromStringAndSize does not count that */
6022#ifdef MS_WINDOWS
6023 len = wcslen(s1) + wcslen(s2) + 2;
6024 newstr = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(NULL((void *)0), (int)len - 1);
6025#else
6026 len = PyBytes_GET_SIZE(os1)((__builtin_expect(!(((((((PyObject*)(os1))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 6026, "PyBytes_Check(os1)") : (void
)0),(((PyVarObject*)(os1))->ob_size))
+ PyBytes_GET_SIZE(os2)((__builtin_expect(!(((((((PyObject*)(os2))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 6026, "PyBytes_Check(os2)") : (void
)0),(((PyVarObject*)(os2))->ob_size))
+ 2;
6027 newstr = PyBytes_FromStringAndSize(NULL((void *)0), (int)len - 1);
6028#endif
6029 if (newstr == NULL((void *)0)) {
6030 PyErr_NoMemory();
6031 goto error;
6032 }
6033#ifdef MS_WINDOWS
6034 newenv = PyUnicode_AsUnicodePyUnicodeUCS2_AsUnicode(newstr);
6035 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6036 if (_wputenv(newenv)) {
6037 posix_error();
6038 goto error;
6039 }
6040#else
6041 newenv = PyBytes_AS_STRING(newstr)((__builtin_expect(!(((((((PyObject*)(newstr))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 6041, "PyBytes_Check(newstr)") :
(void)0), (((PyBytesObject *)(newstr))->ob_sval))
;
6042 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6043 if (putenv(newenv)) {
6044 posix_error();
6045 goto error;
6046 }
6047#endif
6048
6049 /* Install the first arg and newstr in posix_putenv_garbage;
6050 * this will cause previous value to be collected. This has to
6051 * happen after the real putenv() call because the old value
6052 * was still accessible until then. */
6053 if (PyDict_SetItem(posix_putenv_garbage,
6054#ifdef MS_WINDOWS
6055 PyTuple_GET_ITEM(args, 0)(((PyTupleObject *)(args))->ob_item[0]),
6056#else
6057 os1,
6058#endif
6059 newstr)) {
6060 /* really not much we can do; just leak */
6061 PyErr_Clear();
6062 }
6063 else {
6064 Py_DECREF(newstr)do { if (_Py_RefTotal-- , --((PyObject*)(newstr))->ob_refcnt
!= 0) { if (((PyObject*)newstr)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 6064, (PyObject *)(newstr)); } else
_Py_Dealloc((PyObject *)(newstr)); } while (0)
;
6065 }
6066
6067#if defined(PYOS_OS2)
6068 }
6069#endif
6070
6071#ifndef MS_WINDOWS
6072 Py_DECREF(os1)do { if (_Py_RefTotal-- , --((PyObject*)(os1))->ob_refcnt !=
0) { if (((PyObject*)os1)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 6072, (PyObject *)(os1)); } else _Py_Dealloc
((PyObject *)(os1)); } while (0)
;
6073 Py_DECREF(os2)do { if (_Py_RefTotal-- , --((PyObject*)(os2))->ob_refcnt !=
0) { if (((PyObject*)os2)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 6073, (PyObject *)(os2)); } else _Py_Dealloc
((PyObject *)(os2)); } while (0)
;
6074#endif
6075 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
6076
6077error:
6078#ifndef MS_WINDOWS
6079 Py_DECREF(os1)do { if (_Py_RefTotal-- , --((PyObject*)(os1))->ob_refcnt !=
0) { if (((PyObject*)os1)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 6079, (PyObject *)(os1)); } else _Py_Dealloc
((PyObject *)(os1)); } while (0)
;
6080 Py_DECREF(os2)do { if (_Py_RefTotal-- , --((PyObject*)(os2))->ob_refcnt !=
0) { if (((PyObject*)os2)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 6080, (PyObject *)(os2)); } else _Py_Dealloc
((PyObject *)(os2)); } while (0)
;
6081#endif
6082 Py_XDECREF(newstr)do { if ((newstr) == ((void *)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(newstr))->ob_refcnt != 0) { if (((PyObject
*)newstr)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/posixmodule.c"
, 6082, (PyObject *)(newstr)); } else _Py_Dealloc((PyObject *
)(newstr)); } while (0); } while (0)
;
6083 return NULL((void *)0);
6084}
6085#endif /* putenv */
6086
6087#ifdef HAVE_UNSETENV1
6088PyDoc_STRVAR(posix_unsetenv__doc__,static char posix_unsetenv__doc__[] = "unsetenv(key)\n\nDelete an environment variable."
6089"unsetenv(key)\n\n\static char posix_unsetenv__doc__[] = "unsetenv(key)\n\nDelete an environment variable."
6090Delete an environment variable.")static char posix_unsetenv__doc__[] = "unsetenv(key)\n\nDelete an environment variable.";
6091
6092static PyObject *
6093posix_unsetenv(PyObject *self, PyObject *args)
6094{
6095#ifdef MS_WINDOWS
6096 char *s1;
6097
6098 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "s:unsetenv", &s1))
6099 return NULL((void *)0);
6100#else
6101 PyObject *os1;
6102 char *s1;
6103
6104 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&:unsetenv",
6105 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &os1))
6106 return NULL((void *)0);
6107 s1 = PyBytes_AsString(os1);
6108#endif
6109
6110 unsetenv(s1);
6111
6112 /* Remove the key from posix_putenv_garbage;
6113 * this will cause it to be collected. This has to
6114 * happen after the real unsetenv() call because the
6115 * old value was still accessible until then.
6116 */
6117 if (PyDict_DelItem(posix_putenv_garbage,
6118#ifdef MS_WINDOWS
6119 PyTuple_GET_ITEM(args, 0)(((PyTupleObject *)(args))->ob_item[0])
6120#else
6121 os1
6122#endif
6123 )) {
6124 /* really not much we can do; just leak */
6125 PyErr_Clear();
6126 }
6127
6128#ifndef MS_WINDOWS
6129 Py_DECREF(os1)do { if (_Py_RefTotal-- , --((PyObject*)(os1))->ob_refcnt !=
0) { if (((PyObject*)os1)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 6129, (PyObject *)(os1)); } else _Py_Dealloc
((PyObject *)(os1)); } while (0)
;
6130#endif
6131 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
6132}
6133#endif /* unsetenv */
6134
6135PyDoc_STRVAR(posix_strerror__doc__,static char posix_strerror__doc__[] = "strerror(code) -> string\n\nTranslate an error code to a message string."
6136"strerror(code) -> string\n\n\static char posix_strerror__doc__[] = "strerror(code) -> string\n\nTranslate an error code to a message string."
6137Translate an error code to a message string.")static char posix_strerror__doc__[] = "strerror(code) -> string\n\nTranslate an error code to a message string.";
6138
6139static PyObject *
6140posix_strerror(PyObject *self, PyObject *args)
6141{
6142 int code;
6143 char *message;
6144 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:strerror", &code))
6145 return NULL((void *)0);
6146 message = strerror(code);
6147 if (message == NULL((void *)0)) {
6148 PyErr_SetString(PyExc_ValueError,
6149 "strerror() argument out of range");
6150 return NULL((void *)0);
6151 }
6152 return PyUnicode_FromStringPyUnicodeUCS2_FromString(message);
6153}
6154
6155
6156#ifdef HAVE_SYS_WAIT_H1
6157
6158#ifdef WCOREDUMP
6159PyDoc_STRVAR(posix_WCOREDUMP__doc__,static char posix_WCOREDUMP__doc__[] = "WCOREDUMP(status) -> bool\n\nReturn True if the process returning 'status' was dumped to a core file."
6160"WCOREDUMP(status) -> bool\n\n\static char posix_WCOREDUMP__doc__[] = "WCOREDUMP(status) -> bool\n\nReturn True if the process returning 'status' was dumped to a core file."
6161Return True if the process returning 'status' was dumped to a core file.")static char posix_WCOREDUMP__doc__[] = "WCOREDUMP(status) -> bool\n\nReturn True if the process returning 'status' was dumped to a core file.";
6162
6163static PyObject *
6164posix_WCOREDUMP(PyObject *self, PyObject *args)
6165{
6166 WAIT_TYPEint status;
6167 WAIT_STATUS_INT(status)(status) = 0;
6168
6169 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)(status)))
6170 return NULL((void *)0);
6171
6172 return PyBool_FromLong(WCOREDUMP(status)((*(int *)&(status)) & 0200));
6173}
6174#endif /* WCOREDUMP */
6175
6176#ifdef WIFCONTINUED
6177PyDoc_STRVAR(posix_WIFCONTINUED__doc__,static char posix_WIFCONTINUED__doc__[] = "WIFCONTINUED(status) -> bool\n\nReturn True if the process returning 'status' was continued from a\njob control stop."
6178"WIFCONTINUED(status) -> bool\n\n\static char posix_WIFCONTINUED__doc__[] = "WIFCONTINUED(status) -> bool\n\nReturn True if the process returning 'status' was continued from a\njob control stop."
6179Return True if the process returning 'status' was continued from a\n\static char posix_WIFCONTINUED__doc__[] = "WIFCONTINUED(status) -> bool\n\nReturn True if the process returning 'status' was continued from a\njob control stop."
6180job control stop.")static char posix_WIFCONTINUED__doc__[] = "WIFCONTINUED(status) -> bool\n\nReturn True if the process returning 'status' was continued from a\njob control stop.";
6181
6182static PyObject *
6183posix_WIFCONTINUED(PyObject *self, PyObject *args)
6184{
6185 WAIT_TYPEint status;
6186 WAIT_STATUS_INT(status)(status) = 0;
6187
6188 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)(status)))
6189 return NULL((void *)0);
6190
6191 return PyBool_FromLong(WIFCONTINUED(status)(((*(int *)&(status)) & 0177) == 0177 && ((*(
int *)&(status)) >> 8) == 0x13)
);
6192}
6193#endif /* WIFCONTINUED */
6194
6195#ifdef WIFSTOPPED
6196PyDoc_STRVAR(posix_WIFSTOPPED__doc__,static char posix_WIFSTOPPED__doc__[] = "WIFSTOPPED(status) -> bool\n\nReturn True if the process returning 'status' was stopped."
6197"WIFSTOPPED(status) -> bool\n\n\static char posix_WIFSTOPPED__doc__[] = "WIFSTOPPED(status) -> bool\n\nReturn True if the process returning 'status' was stopped."
6198Return True if the process returning 'status' was stopped.")static char posix_WIFSTOPPED__doc__[] = "WIFSTOPPED(status) -> bool\n\nReturn True if the process returning 'status' was stopped.";
6199
6200static PyObject *
6201posix_WIFSTOPPED(PyObject *self, PyObject *args)
6202{
6203 WAIT_TYPEint status;
6204 WAIT_STATUS_INT(status)(status) = 0;
6205
6206 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)(status)))
6207 return NULL((void *)0);
6208
6209 return PyBool_FromLong(WIFSTOPPED(status)(((*(int *)&(status)) & 0177) == 0177 && ((*(
int *)&(status)) >> 8) != 0x13)
);
6210}
6211#endif /* WIFSTOPPED */
6212
6213#ifdef WIFSIGNALED
6214PyDoc_STRVAR(posix_WIFSIGNALED__doc__,static char posix_WIFSIGNALED__doc__[] = "WIFSIGNALED(status) -> bool\n\nReturn True if the process returning 'status' was terminated by a signal."
6215"WIFSIGNALED(status) -> bool\n\n\static char posix_WIFSIGNALED__doc__[] = "WIFSIGNALED(status) -> bool\n\nReturn True if the process returning 'status' was terminated by a signal."
6216Return True if the process returning 'status' was terminated by a signal.")static char posix_WIFSIGNALED__doc__[] = "WIFSIGNALED(status) -> bool\n\nReturn True if the process returning 'status' was terminated by a signal.";
6217
6218static PyObject *
6219posix_WIFSIGNALED(PyObject *self, PyObject *args)
6220{
6221 WAIT_TYPEint status;
6222 WAIT_STATUS_INT(status)(status) = 0;
6223
6224 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)(status)))
6225 return NULL((void *)0);
6226
6227 return PyBool_FromLong(WIFSIGNALED(status)(((*(int *)&(status)) & 0177) != 0177 && ((*(
int *)&(status)) & 0177) != 0)
);
6228}
6229#endif /* WIFSIGNALED */
6230
6231#ifdef WIFEXITED
6232PyDoc_STRVAR(posix_WIFEXITED__doc__,static char posix_WIFEXITED__doc__[] = "WIFEXITED(status) -> bool\n\nReturn true if the process returning 'status' exited using the exit()\nsystem call."
6233"WIFEXITED(status) -> bool\n\n\static char posix_WIFEXITED__doc__[] = "WIFEXITED(status) -> bool\n\nReturn true if the process returning 'status' exited using the exit()\nsystem call."
6234Return true if the process returning 'status' exited using the exit()\n\static char posix_WIFEXITED__doc__[] = "WIFEXITED(status) -> bool\n\nReturn true if the process returning 'status' exited using the exit()\nsystem call."
6235system call.")static char posix_WIFEXITED__doc__[] = "WIFEXITED(status) -> bool\n\nReturn true if the process returning 'status' exited using the exit()\nsystem call.";
6236
6237static PyObject *
6238posix_WIFEXITED(PyObject *self, PyObject *args)
6239{
6240 WAIT_TYPEint status;
6241 WAIT_STATUS_INT(status)(status) = 0;
6242
6243 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)(status)))
6244 return NULL((void *)0);
6245
6246 return PyBool_FromLong(WIFEXITED(status)(((*(int *)&(status)) & 0177) == 0));
6247}
6248#endif /* WIFEXITED */
6249
6250#ifdef WEXITSTATUS
6251PyDoc_STRVAR(posix_WEXITSTATUS__doc__,static char posix_WEXITSTATUS__doc__[] = "WEXITSTATUS(status) -> integer\n\nReturn the process return code from 'status'."
6252"WEXITSTATUS(status) -> integer\n\n\static char posix_WEXITSTATUS__doc__[] = "WEXITSTATUS(status) -> integer\n\nReturn the process return code from 'status'."
6253Return the process return code from 'status'.")static char posix_WEXITSTATUS__doc__[] = "WEXITSTATUS(status) -> integer\n\nReturn the process return code from 'status'.";
6254
6255static PyObject *
6256posix_WEXITSTATUS(PyObject *self, PyObject *args)
6257{
6258 WAIT_TYPEint status;
6259 WAIT_STATUS_INT(status)(status) = 0;
6260
6261 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)(status)))
6262 return NULL((void *)0);
6263
6264 return Py_BuildValue_Py_BuildValue_SizeT("i", WEXITSTATUS(status)(((*(int *)&(status)) >> 8) & 0x000000ff));
6265}
6266#endif /* WEXITSTATUS */
6267
6268#ifdef WTERMSIG
6269PyDoc_STRVAR(posix_WTERMSIG__doc__,static char posix_WTERMSIG__doc__[] = "WTERMSIG(status) -> integer\n\nReturn the signal that terminated the process that provided the 'status'\nvalue."
6270"WTERMSIG(status) -> integer\n\n\static char posix_WTERMSIG__doc__[] = "WTERMSIG(status) -> integer\n\nReturn the signal that terminated the process that provided the 'status'\nvalue."
6271Return the signal that terminated the process that provided the 'status'\n\static char posix_WTERMSIG__doc__[] = "WTERMSIG(status) -> integer\n\nReturn the signal that terminated the process that provided the 'status'\nvalue."
6272value.")static char posix_WTERMSIG__doc__[] = "WTERMSIG(status) -> integer\n\nReturn the signal that terminated the process that provided the 'status'\nvalue.";
6273
6274static PyObject *
6275posix_WTERMSIG(PyObject *self, PyObject *args)
6276{
6277 WAIT_TYPEint status;
6278 WAIT_STATUS_INT(status)(status) = 0;
6279
6280 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)(status)))
6281 return NULL((void *)0);
6282
6283 return Py_BuildValue_Py_BuildValue_SizeT("i", WTERMSIG(status)(((*(int *)&(status)) & 0177)));
6284}
6285#endif /* WTERMSIG */
6286
6287#ifdef WSTOPSIG
6288PyDoc_STRVAR(posix_WSTOPSIG__doc__,static char posix_WSTOPSIG__doc__[] = "WSTOPSIG(status) -> integer\n\nReturn the signal that stopped the process that provided\nthe 'status' value."
6289"WSTOPSIG(status) -> integer\n\n\static char posix_WSTOPSIG__doc__[] = "WSTOPSIG(status) -> integer\n\nReturn the signal that stopped the process that provided\nthe 'status' value."
6290Return the signal that stopped the process that provided\n\static char posix_WSTOPSIG__doc__[] = "WSTOPSIG(status) -> integer\n\nReturn the signal that stopped the process that provided\nthe 'status' value."
6291the 'status' value.")static char posix_WSTOPSIG__doc__[] = "WSTOPSIG(status) -> integer\n\nReturn the signal that stopped the process that provided\nthe 'status' value.";
6292
6293static PyObject *
6294posix_WSTOPSIG(PyObject *self, PyObject *args)
6295{
6296 WAIT_TYPEint status;
6297 WAIT_STATUS_INT(status)(status) = 0;
6298
6299 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)(status)))
6300 return NULL((void *)0);
6301
6302 return Py_BuildValue_Py_BuildValue_SizeT("i", WSTOPSIG(status)((*(int *)&(status)) >> 8));
6303}
6304#endif /* WSTOPSIG */
6305
6306#endif /* HAVE_SYS_WAIT_H */
6307
6308
6309#if defined(HAVE_FSTATVFS1) && defined(HAVE_SYS_STATVFS_H1)
6310#ifdef _SCO_DS
6311/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6312 needed definitions in sys/statvfs.h */
6313#define _SVID3
6314#endif
6315#include <sys/statvfs.h>
6316
6317static PyObject*
6318_pystatvfs_fromstructstatvfs(struct statvfs st) {
6319 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6320 if (v == NULL((void *)0))
6321 return NULL((void *)0);
6322
6323#if !defined(HAVE_LARGEFILE_SUPPORT)
6324 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize))(((PyTupleObject *)(v))->ob_item[0] = PyLong_FromLong((long
) st.f_bsize))
;
6325 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize))(((PyTupleObject *)(v))->ob_item[1] = PyLong_FromLong((long
) st.f_frsize))
;
6326 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks))(((PyTupleObject *)(v))->ob_item[2] = PyLong_FromLong((long
) st.f_blocks))
;
6327 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree))(((PyTupleObject *)(v))->ob_item[3] = PyLong_FromLong((long
) st.f_bfree))
;
6328 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail))(((PyTupleObject *)(v))->ob_item[4] = PyLong_FromLong((long
) st.f_bavail))
;
6329 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files))(((PyTupleObject *)(v))->ob_item[5] = PyLong_FromLong((long
) st.f_files))
;
6330 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree))(((PyTupleObject *)(v))->ob_item[6] = PyLong_FromLong((long
) st.f_ffree))
;
6331 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail))(((PyTupleObject *)(v))->ob_item[7] = PyLong_FromLong((long
) st.f_favail))
;
6332 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag))(((PyTupleObject *)(v))->ob_item[8] = PyLong_FromLong((long
) st.f_flag))
;
6333 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax))(((PyTupleObject *)(v))->ob_item[9] = PyLong_FromLong((long
) st.f_namemax))
;
6334#else
6335 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize))(((PyTupleObject *)(v))->ob_item[0] = PyLong_FromLong((long
) st.f_bsize))
;
6336 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize))(((PyTupleObject *)(v))->ob_item[1] = PyLong_FromLong((long
) st.f_frsize))
;
6337 PyStructSequence_SET_ITEM(v, 2,(((PyTupleObject *)(v))->ob_item[2] = PyLong_FromLongLong(
(long long) st.f_blocks))
6338 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks))(((PyTupleObject *)(v))->ob_item[2] = PyLong_FromLongLong(
(long long) st.f_blocks))
;
6339 PyStructSequence_SET_ITEM(v, 3,(((PyTupleObject *)(v))->ob_item[3] = PyLong_FromLongLong(
(long long) st.f_bfree))
6340 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree))(((PyTupleObject *)(v))->ob_item[3] = PyLong_FromLongLong(
(long long) st.f_bfree))
;
6341 PyStructSequence_SET_ITEM(v, 4,(((PyTupleObject *)(v))->ob_item[4] = PyLong_FromLongLong(
(long long) st.f_bavail))
6342 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail))(((PyTupleObject *)(v))->ob_item[4] = PyLong_FromLongLong(
(long long) st.f_bavail))
;
6343 PyStructSequence_SET_ITEM(v, 5,(((PyTupleObject *)(v))->ob_item[5] = PyLong_FromLongLong(
(long long) st.f_files))
6344 PyLong_FromLongLong((PY_LONG_LONG) st.f_files))(((PyTupleObject *)(v))->ob_item[5] = PyLong_FromLongLong(
(long long) st.f_files))
;
6345 PyStructSequence_SET_ITEM(v, 6,(((PyTupleObject *)(v))->ob_item[6] = PyLong_FromLongLong(
(long long) st.f_ffree))
6346 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree))(((PyTupleObject *)(v))->ob_item[6] = PyLong_FromLongLong(
(long long) st.f_ffree))
;
6347 PyStructSequence_SET_ITEM(v, 7,(((PyTupleObject *)(v))->ob_item[7] = PyLong_FromLongLong(
(long long) st.f_favail))
6348 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail))(((PyTupleObject *)(v))->ob_item[7] = PyLong_FromLongLong(
(long long) st.f_favail))
;
6349 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag))(((PyTupleObject *)(v))->ob_item[8] = PyLong_FromLong((long
) st.f_flag))
;
6350 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax))(((PyTupleObject *)(v))->ob_item[9] = PyLong_FromLong((long
) st.f_namemax))
;
6351#endif
6352
6353 return v;
6354}
6355
6356PyDoc_STRVAR(posix_fstatvfs__doc__,static char posix_fstatvfs__doc__[] = "fstatvfs(fd) -> statvfs result\n\nPerform an fstatvfs system call on the given fd."
6357"fstatvfs(fd) -> statvfs result\n\n\static char posix_fstatvfs__doc__[] = "fstatvfs(fd) -> statvfs result\n\nPerform an fstatvfs system call on the given fd."
6358Perform an fstatvfs system call on the given fd.")static char posix_fstatvfs__doc__[] = "fstatvfs(fd) -> statvfs result\n\nPerform an fstatvfs system call on the given fd.";
6359
6360static PyObject *
6361posix_fstatvfs(PyObject *self, PyObject *args)
6362{
6363 int fd, res;
6364 struct statvfs st;
6365
6366 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:fstatvfs", &fd))
6367 return NULL((void *)0);
6368 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
6369 res = fstatvfs(fd, &st);
6370 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
6371 if (res != 0)
6372 return posix_error();
6373
6374 return _pystatvfs_fromstructstatvfs(st);
6375}
6376#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
6377
6378
6379#if defined(HAVE_STATVFS1) && defined(HAVE_SYS_STATVFS_H1)
6380#include <sys/statvfs.h>
6381
6382PyDoc_STRVAR(posix_statvfs__doc__,static char posix_statvfs__doc__[] = "statvfs(path) -> statvfs result\n\nPerform a statvfs system call on the given path."
6383"statvfs(path) -> statvfs result\n\n\static char posix_statvfs__doc__[] = "statvfs(path) -> statvfs result\n\nPerform a statvfs system call on the given path."
6384Perform a statvfs system call on the given path.")static char posix_statvfs__doc__[] = "statvfs(path) -> statvfs result\n\nPerform a statvfs system call on the given path.";
6385
6386static PyObject *
6387posix_statvfs(PyObject *self, PyObject *args)
6388{
6389 char *path;
6390 int res;
6391 struct statvfs st;
6392 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "s:statvfs", &path))
6393 return NULL((void *)0);
6394 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
6395 res = statvfs(path, &st);
6396 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
6397 if (res != 0)
6398 return posix_error_with_filename(path);
6399
6400 return _pystatvfs_fromstructstatvfs(st);
6401}
6402#endif /* HAVE_STATVFS */
6403
6404/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6405 * It maps strings representing configuration variable names to
6406 * integer values, allowing those functions to be called with the
6407 * magic names instead of polluting the module's namespace with tons of
6408 * rarely-used constants. There are three separate tables that use
6409 * these definitions.
6410 *
6411 * This code is always included, even if none of the interfaces that
6412 * need it are included. The #if hackery needed to avoid it would be
6413 * sufficiently pervasive that it's not worth the loss of readability.
6414 */
6415struct constdef {
6416 char *name;
6417 long value;
6418};
6419
6420static int
6421conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6422 size_t tablesize)
6423{
6424 if (PyLong_Check(arg)((((((PyObject*)(arg))->ob_type))->tp_flags & ((1L<<
24))) != 0)
) {
6425 *valuep = PyLong_AS_LONG(arg)PyLong_AsLong(arg);
6426 return 1;
6427 }
6428 else {
6429 /* look up the value in the table using a binary search */
6430 size_t lo = 0;
6431 size_t mid;
6432 size_t hi = tablesize;
6433 int cmp;
6434 const char *confname;
6435 if (!PyUnicode_Check(arg)((((((PyObject*)(arg))->ob_type))->tp_flags & ((1L<<
28))) != 0)
) {
6436 PyErr_SetString(PyExc_TypeError,
6437 "configuration names must be strings or integers");
6438 return 0;
6439 }
6440 confname = _PyUnicode_AsString(arg);
6441 if (confname == NULL((void *)0))
6442 return 0;
6443 while (lo < hi) {
6444 mid = (lo + hi) / 2;
6445 cmp = strcmp(confname, table[mid].name);
6446 if (cmp < 0)
6447 hi = mid;
6448 else if (cmp > 0)
6449 lo = mid + 1;
6450 else {
6451 *valuep = table[mid].value;
6452 return 1;
6453 }
6454 }
6455 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6456 return 0;
6457 }
6458}
6459
6460
6461#if defined(HAVE_FPATHCONF1) || defined(HAVE_PATHCONF1)
6462static struct constdef posix_constants_pathconf[] = {
6463#ifdef _PC_ABI_AIO_XFER_MAX
6464 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
6465#endif
6466#ifdef _PC_ABI_ASYNC_IO
6467 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
6468#endif
6469#ifdef _PC_ASYNC_IO17
6470 {"PC_ASYNC_IO", _PC_ASYNC_IO17},
6471#endif
6472#ifdef _PC_CHOWN_RESTRICTED7
6473 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED7},
6474#endif
6475#ifdef _PC_FILESIZEBITS18
6476 {"PC_FILESIZEBITS", _PC_FILESIZEBITS18},
6477#endif
6478#ifdef _PC_LAST
6479 {"PC_LAST", _PC_LAST},
6480#endif
6481#ifdef _PC_LINK_MAX1
6482 {"PC_LINK_MAX", _PC_LINK_MAX1},
6483#endif
6484#ifdef _PC_MAX_CANON2
6485 {"PC_MAX_CANON", _PC_MAX_CANON2},
6486#endif
6487#ifdef _PC_MAX_INPUT3
6488 {"PC_MAX_INPUT", _PC_MAX_INPUT3},
6489#endif
6490#ifdef _PC_NAME_MAX4
6491 {"PC_NAME_MAX", _PC_NAME_MAX4},
6492#endif
6493#ifdef _PC_NO_TRUNC8
6494 {"PC_NO_TRUNC", _PC_NO_TRUNC8},
6495#endif
6496#ifdef _PC_PATH_MAX5
6497 {"PC_PATH_MAX", _PC_PATH_MAX5},
6498#endif
6499#ifdef _PC_PIPE_BUF6
6500 {"PC_PIPE_BUF", _PC_PIPE_BUF6},
6501#endif
6502#ifdef _PC_PRIO_IO19
6503 {"PC_PRIO_IO", _PC_PRIO_IO19},
6504#endif
6505#ifdef _PC_SOCK_MAXBUF
6506 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
6507#endif
6508#ifdef _PC_SYNC_IO25
6509 {"PC_SYNC_IO", _PC_SYNC_IO25},
6510#endif
6511#ifdef _PC_VDISABLE9
6512 {"PC_VDISABLE", _PC_VDISABLE9},
6513#endif
6514#ifdef _PC_ACL_ENABLED
6515 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
6516#endif
6517#ifdef _PC_MIN_HOLE_SIZE
6518 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
6519#endif
6520#ifdef _PC_ALLOC_SIZE_MIN16
6521 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN16},
6522#endif
6523#ifdef _PC_REC_INCR_XFER_SIZE20
6524 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE20},
6525#endif
6526#ifdef _PC_REC_MAX_XFER_SIZE21
6527 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE21},
6528#endif
6529#ifdef _PC_REC_MIN_XFER_SIZE22
6530 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE22},
6531#endif
6532#ifdef _PC_REC_XFER_ALIGN23
6533 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN23},
6534#endif
6535#ifdef _PC_SYMLINK_MAX24
6536 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX24},
6537#endif
6538#ifdef _PC_XATTR_ENABLED
6539 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
6540#endif
6541#ifdef _PC_XATTR_EXISTS
6542 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
6543#endif
6544#ifdef _PC_TIMESTAMP_RESOLUTION
6545 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
6546#endif
6547};
6548
6549static int
6550conv_path_confname(PyObject *arg, int *valuep)
6551{
6552 return conv_confname(arg, valuep, posix_constants_pathconf,
6553 sizeof(posix_constants_pathconf)
6554 / sizeof(struct constdef));
6555}
6556#endif
6557
6558#ifdef HAVE_FPATHCONF1
6559PyDoc_STRVAR(posix_fpathconf__doc__,static char posix_fpathconf__doc__[] = "fpathconf(fd, name) -> integer\n\nReturn the configuration limit name for the file descriptor fd.\nIf there is no limit, return -1."
6560"fpathconf(fd, name) -> integer\n\n\static char posix_fpathconf__doc__[] = "fpathconf(fd, name) -> integer\n\nReturn the configuration limit name for the file descriptor fd.\nIf there is no limit, return -1."
6561Return the configuration limit name for the file descriptor fd.\n\static char posix_fpathconf__doc__[] = "fpathconf(fd, name) -> integer\n\nReturn the configuration limit name for the file descriptor fd.\nIf there is no limit, return -1."
6562If there is no limit, return -1.")static char posix_fpathconf__doc__[] = "fpathconf(fd, name) -> integer\n\nReturn the configuration limit name for the file descriptor fd.\nIf there is no limit, return -1.";
6563
6564static PyObject *
6565posix_fpathconf(PyObject *self, PyObject *args)
6566{
6567 PyObject *result = NULL((void *)0);
6568 int name, fd;
6569
6570 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "iO&:fpathconf", &fd,
6571 conv_path_confname, &name)) {
6572 long limit;
6573
6574 errno(*__error()) = 0;
6575 limit = fpathconf(fd, name);
6576 if (limit == -1 && errno(*__error()) != 0)
6577 posix_error();
6578 else
6579 result = PyLong_FromLong(limit);
6580 }
6581 return result;
6582}
6583#endif
6584
6585
6586#ifdef HAVE_PATHCONF1
6587PyDoc_STRVAR(posix_pathconf__doc__,static char posix_pathconf__doc__[] = "pathconf(path, name) -> integer\n\nReturn the configuration limit name for the file or directory path.\nIf there is no limit, return -1."
6588"pathconf(path, name) -> integer\n\n\static char posix_pathconf__doc__[] = "pathconf(path, name) -> integer\n\nReturn the configuration limit name for the file or directory path.\nIf there is no limit, return -1."
6589Return the configuration limit name for the file or directory path.\n\static char posix_pathconf__doc__[] = "pathconf(path, name) -> integer\n\nReturn the configuration limit name for the file or directory path.\nIf there is no limit, return -1."
6590If there is no limit, return -1.")static char posix_pathconf__doc__[] = "pathconf(path, name) -> integer\n\nReturn the configuration limit name for the file or directory path.\nIf there is no limit, return -1.";
6591
6592static PyObject *
6593posix_pathconf(PyObject *self, PyObject *args)
6594{
6595 PyObject *result = NULL((void *)0);
6596 int name;
6597 char *path;
6598
6599 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "sO&:pathconf", &path,
6600 conv_path_confname, &name)) {
6601 long limit;
6602
6603 errno(*__error()) = 0;
6604 limit = pathconf(path, name);
6605 if (limit == -1 && errno(*__error()) != 0) {
6606 if (errno(*__error()) == EINVAL22)
6607 /* could be a path or name problem */
6608 posix_error();
6609 else
6610 posix_error_with_filename(path);
6611 }
6612 else
6613 result = PyLong_FromLong(limit);
6614 }
6615 return result;
6616}
6617#endif
6618
6619#ifdef HAVE_CONFSTR1
6620static struct constdef posix_constants_confstr[] = {
6621#ifdef _CS_ARCHITECTURE
6622 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
6623#endif
6624#ifdef _CS_GNU_LIBC_VERSION
6625 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
6626#endif
6627#ifdef _CS_GNU_LIBPTHREAD_VERSION
6628 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
6629#endif
6630#ifdef _CS_HOSTNAME
6631 {"CS_HOSTNAME", _CS_HOSTNAME},
6632#endif
6633#ifdef _CS_HW_PROVIDER
6634 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6635#endif
6636#ifdef _CS_HW_SERIAL
6637 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6638#endif
6639#ifdef _CS_INITTAB_NAME
6640 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6641#endif
6642#ifdef _CS_LFS64_CFLAGS
6643 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6644#endif
6645#ifdef _CS_LFS64_LDFLAGS
6646 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6647#endif
6648#ifdef _CS_LFS64_LIBS
6649 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6650#endif
6651#ifdef _CS_LFS64_LINTFLAGS
6652 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6653#endif
6654#ifdef _CS_LFS_CFLAGS
6655 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6656#endif
6657#ifdef _CS_LFS_LDFLAGS
6658 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6659#endif
6660#ifdef _CS_LFS_LIBS
6661 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6662#endif
6663#ifdef _CS_LFS_LINTFLAGS
6664 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6665#endif
6666#ifdef _CS_MACHINE
6667 {"CS_MACHINE", _CS_MACHINE},
6668#endif
6669#ifdef _CS_PATH1
6670 {"CS_PATH", _CS_PATH1},
6671#endif
6672#ifdef _CS_RELEASE
6673 {"CS_RELEASE", _CS_RELEASE},
6674#endif
6675#ifdef _CS_SRPC_DOMAIN
6676 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6677#endif
6678#ifdef _CS_SYSNAME
6679 {"CS_SYSNAME", _CS_SYSNAME},
6680#endif
6681#ifdef _CS_VERSION
6682 {"CS_VERSION", _CS_VERSION},
6683#endif
6684#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS20
6685 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS20},
6686#endif
6687#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS21
6688 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS21},
6689#endif
6690#ifdef _CS_XBS5_ILP32_OFF32_LIBS22
6691 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS22},
6692#endif
6693#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS23
6694 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS23},
6695#endif
6696#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS24
6697 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS24},
6698#endif
6699#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS25
6700 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS25},
6701#endif
6702#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS26
6703 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS26},
6704#endif
6705#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS27
6706 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS27},
6707#endif
6708#ifdef _CS_XBS5_LP64_OFF64_CFLAGS28
6709 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS28},
6710#endif
6711#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS29
6712 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS29},
6713#endif
6714#ifdef _CS_XBS5_LP64_OFF64_LIBS30
6715 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS30},
6716#endif
6717#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS31
6718 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS31},
6719#endif
6720#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS32
6721 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS32},
6722#endif
6723#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS33
6724 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS33},
6725#endif
6726#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS34
6727 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS34},
6728#endif
6729#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS35
6730 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS35},
6731#endif
6732#ifdef _MIPS_CS_AVAIL_PROCESSORS
6733 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6734#endif
6735#ifdef _MIPS_CS_BASE
6736 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6737#endif
6738#ifdef _MIPS_CS_HOSTID
6739 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6740#endif
6741#ifdef _MIPS_CS_HW_NAME
6742 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6743#endif
6744#ifdef _MIPS_CS_NUM_PROCESSORS
6745 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6746#endif
6747#ifdef _MIPS_CS_OSREL_MAJ
6748 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6749#endif
6750#ifdef _MIPS_CS_OSREL_MIN
6751 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6752#endif
6753#ifdef _MIPS_CS_OSREL_PATCH
6754 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6755#endif
6756#ifdef _MIPS_CS_OS_NAME
6757 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6758#endif
6759#ifdef _MIPS_CS_OS_PROVIDER
6760 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6761#endif
6762#ifdef _MIPS_CS_PROCESSORS
6763 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6764#endif
6765#ifdef _MIPS_CS_SERIAL
6766 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6767#endif
6768#ifdef _MIPS_CS_VENDOR
6769 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6770#endif
6771};
6772
6773static int
6774conv_confstr_confname(PyObject *arg, int *valuep)
6775{
6776 return conv_confname(arg, valuep, posix_constants_confstr,
6777 sizeof(posix_constants_confstr)
6778 / sizeof(struct constdef));
6779}
6780
6781PyDoc_STRVAR(posix_confstr__doc__,static char posix_confstr__doc__[] = "confstr(name) -> string\n\nReturn a string-valued system configuration variable."
6782"confstr(name) -> string\n\n\static char posix_confstr__doc__[] = "confstr(name) -> string\n\nReturn a string-valued system configuration variable."
6783Return a string-valued system configuration variable.")static char posix_confstr__doc__[] = "confstr(name) -> string\n\nReturn a string-valued system configuration variable.";
6784
6785static PyObject *
6786posix_confstr(PyObject *self, PyObject *args)
6787{
6788 PyObject *result = NULL((void *)0);
6789 int name;
6790 char buffer[255];
6791 int len;
6792
6793 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&:confstr", conv_confstr_confname, &name))
6794 return NULL((void *)0);
6795
6796 errno(*__error()) = 0;
6797 len = confstr(name, buffer, sizeof(buffer));
6798 if (len == 0) {
6799 if (errno(*__error())) {
6800 posix_error();
6801 return NULL((void *)0);
6802 }
6803 else {
6804 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
6805 }
6806 }
6807
6808 if ((unsigned int)len >= sizeof(buffer)) {
6809 char *buf = PyMem_Malloc(len);
6810 if (buf == NULL((void *)0))
6811 return PyErr_NoMemory();
6812 confstr(name, buf, len);
6813 result = PyUnicode_DecodeFSDefaultAndSizePyUnicodeUCS2_DecodeFSDefaultAndSize(buf, len-1);
6814 PyMem_Free(buf);
6815 }
6816 else
6817 result = PyUnicode_DecodeFSDefaultAndSizePyUnicodeUCS2_DecodeFSDefaultAndSize(buffer, len-1);
6818 return result;
6819}
6820#endif
6821
6822
6823#ifdef HAVE_SYSCONF1
6824static struct constdef posix_constants_sysconf[] = {
6825#ifdef _SC_2_CHAR_TERM20
6826 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM20},
6827#endif
6828#ifdef _SC_2_C_BIND18
6829 {"SC_2_C_BIND", _SC_2_C_BIND18},
6830#endif
6831#ifdef _SC_2_C_DEV19
6832 {"SC_2_C_DEV", _SC_2_C_DEV19},
6833#endif
6834#ifdef _SC_2_C_VERSION
6835 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6836#endif
6837#ifdef _SC_2_FORT_DEV21
6838 {"SC_2_FORT_DEV", _SC_2_FORT_DEV21},
6839#endif
6840#ifdef _SC_2_FORT_RUN22
6841 {"SC_2_FORT_RUN", _SC_2_FORT_RUN22},
6842#endif
6843#ifdef _SC_2_LOCALEDEF23
6844 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF23},
6845#endif
6846#ifdef _SC_2_SW_DEV24
6847 {"SC_2_SW_DEV", _SC_2_SW_DEV24},
6848#endif
6849#ifdef _SC_2_UPE25
6850 {"SC_2_UPE", _SC_2_UPE25},
6851#endif
6852#ifdef _SC_2_VERSION17
6853 {"SC_2_VERSION", _SC_2_VERSION17},
6854#endif
6855#ifdef _SC_ABI_ASYNCHRONOUS_IO
6856 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6857#endif
6858#ifdef _SC_ACL
6859 {"SC_ACL", _SC_ACL},
6860#endif
6861#ifdef _SC_AIO_LISTIO_MAX42
6862 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX42},
6863#endif
6864#ifdef _SC_AIO_MAX43
6865 {"SC_AIO_MAX", _SC_AIO_MAX43},
6866#endif
6867#ifdef _SC_AIO_PRIO_DELTA_MAX44
6868 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX44},
6869#endif
6870#ifdef _SC_ARG_MAX1
6871 {"SC_ARG_MAX", _SC_ARG_MAX1},
6872#endif
6873#ifdef _SC_ASYNCHRONOUS_IO28
6874 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO28},
6875#endif
6876#ifdef _SC_ATEXIT_MAX107
6877 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX107},
6878#endif
6879#ifdef _SC_AUDIT
6880 {"SC_AUDIT", _SC_AUDIT},
6881#endif
6882#ifdef _SC_AVPHYS_PAGES
6883 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6884#endif
6885#ifdef _SC_BC_BASE_MAX9
6886 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX9},
6887#endif
6888#ifdef _SC_BC_DIM_MAX10
6889 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX10},
6890#endif
6891#ifdef _SC_BC_SCALE_MAX11
6892 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX11},
6893#endif
6894#ifdef _SC_BC_STRING_MAX12
6895 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX12},
6896#endif
6897#ifdef _SC_CAP
6898 {"SC_CAP", _SC_CAP},
6899#endif
6900#ifdef _SC_CHARCLASS_NAME_MAX
6901 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6902#endif
6903#ifdef _SC_CHAR_BIT
6904 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6905#endif
6906#ifdef _SC_CHAR_MAX
6907 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6908#endif
6909#ifdef _SC_CHAR_MIN
6910 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6911#endif
6912#ifdef _SC_CHILD_MAX2
6913 {"SC_CHILD_MAX", _SC_CHILD_MAX2},
6914#endif
6915#ifdef _SC_CLK_TCK3
6916 {"SC_CLK_TCK", _SC_CLK_TCK3},
6917#endif
6918#ifdef _SC_COHER_BLKSZ
6919 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6920#endif
6921#ifdef _SC_COLL_WEIGHTS_MAX13
6922 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX13},
6923#endif
6924#ifdef _SC_DCACHE_ASSOC
6925 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6926#endif
6927#ifdef _SC_DCACHE_BLKSZ
6928 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6929#endif
6930#ifdef _SC_DCACHE_LINESZ
6931 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6932#endif
6933#ifdef _SC_DCACHE_SZ
6934 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6935#endif
6936#ifdef _SC_DCACHE_TBLKSZ
6937 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6938#endif
6939#ifdef _SC_DELAYTIMER_MAX45
6940 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX45},
6941#endif
6942#ifdef _SC_EQUIV_CLASS_MAX
6943 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6944#endif
6945#ifdef _SC_EXPR_NEST_MAX14
6946 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX14},
6947#endif
6948#ifdef _SC_FSYNC38
6949 {"SC_FSYNC", _SC_FSYNC38},
6950#endif
6951#ifdef _SC_GETGR_R_SIZE_MAX70
6952 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX70},
6953#endif
6954#ifdef _SC_GETPW_R_SIZE_MAX71
6955 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX71},
6956#endif
6957#ifdef _SC_ICACHE_ASSOC
6958 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6959#endif
6960#ifdef _SC_ICACHE_BLKSZ
6961 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6962#endif
6963#ifdef _SC_ICACHE_LINESZ
6964 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6965#endif
6966#ifdef _SC_ICACHE_SZ
6967 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6968#endif
6969#ifdef _SC_INF
6970 {"SC_INF", _SC_INF},
6971#endif
6972#ifdef _SC_INT_MAX
6973 {"SC_INT_MAX", _SC_INT_MAX},
6974#endif
6975#ifdef _SC_INT_MIN
6976 {"SC_INT_MIN", _SC_INT_MIN},
6977#endif
6978#ifdef _SC_IOV_MAX56
6979 {"SC_IOV_MAX", _SC_IOV_MAX56},
6980#endif
6981#ifdef _SC_IP_SECOPTS
6982 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6983#endif
6984#ifdef _SC_JOB_CONTROL6
6985 {"SC_JOB_CONTROL", _SC_JOB_CONTROL6},
6986#endif
6987#ifdef _SC_KERN_POINTERS
6988 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6989#endif
6990#ifdef _SC_KERN_SIM
6991 {"SC_KERN_SIM", _SC_KERN_SIM},
6992#endif
6993#ifdef _SC_LINE_MAX15
6994 {"SC_LINE_MAX", _SC_LINE_MAX15},
6995#endif
6996#ifdef _SC_LOGIN_NAME_MAX73
6997 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX73},
6998#endif
6999#ifdef _SC_LOGNAME_MAX
7000 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7001#endif
7002#ifdef _SC_LONG_BIT
7003 {"SC_LONG_BIT", _SC_LONG_BIT},
7004#endif
7005#ifdef _SC_MAC
7006 {"SC_MAC", _SC_MAC},
7007#endif
7008#ifdef _SC_MAPPED_FILES47
7009 {"SC_MAPPED_FILES", _SC_MAPPED_FILES47},
7010#endif
7011#ifdef _SC_MAXPID
7012 {"SC_MAXPID", _SC_MAXPID},
7013#endif
7014#ifdef _SC_MB_LEN_MAX
7015 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7016#endif
7017#ifdef _SC_MEMLOCK30
7018 {"SC_MEMLOCK", _SC_MEMLOCK30},
7019#endif
7020#ifdef _SC_MEMLOCK_RANGE31
7021 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE31},
7022#endif
7023#ifdef _SC_MEMORY_PROTECTION32
7024 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION32},
7025#endif
7026#ifdef _SC_MESSAGE_PASSING33
7027 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING33},
7028#endif
7029#ifdef _SC_MMAP_FIXED_ALIGNMENT
7030 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7031#endif
7032#ifdef _SC_MQ_OPEN_MAX46
7033 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX46},
7034#endif
7035#ifdef _SC_MQ_PRIO_MAX75
7036 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX75},
7037#endif
7038#ifdef _SC_NACLS_MAX
7039 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7040#endif
7041#ifdef _SC_NGROUPS_MAX4
7042 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX4},
7043#endif
7044#ifdef _SC_NL_ARGMAX
7045 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7046#endif
7047#ifdef _SC_NL_LANGMAX
7048 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7049#endif
7050#ifdef _SC_NL_MSGMAX
7051 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7052#endif
7053#ifdef _SC_NL_NMAX
7054 {"SC_NL_NMAX", _SC_NL_NMAX},
7055#endif
7056#ifdef _SC_NL_SETMAX
7057 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7058#endif
7059#ifdef _SC_NL_TEXTMAX
7060 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7061#endif
7062#ifdef _SC_NPROCESSORS_CONF57
7063 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF57},
7064#endif
7065#ifdef _SC_NPROCESSORS_ONLN58
7066 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN58},
7067#endif
7068#ifdef _SC_NPROC_CONF
7069 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7070#endif
7071#ifdef _SC_NPROC_ONLN
7072 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7073#endif
7074#ifdef _SC_NZERO
7075 {"SC_NZERO", _SC_NZERO},
7076#endif
7077#ifdef _SC_OPEN_MAX5
7078 {"SC_OPEN_MAX", _SC_OPEN_MAX5},
7079#endif
7080#ifdef _SC_PAGESIZE29
7081 {"SC_PAGESIZE", _SC_PAGESIZE29},
7082#endif
7083#ifdef _SC_PAGE_SIZE29
7084 {"SC_PAGE_SIZE", _SC_PAGE_SIZE29},
7085#endif
7086#ifdef _SC_PASS_MAX131
7087 {"SC_PASS_MAX", _SC_PASS_MAX131},
7088#endif
7089#ifdef _SC_PHYS_PAGES
7090 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7091#endif
7092#ifdef _SC_PII
7093 {"SC_PII", _SC_PII},
7094#endif
7095#ifdef _SC_PII_INTERNET
7096 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7097#endif
7098#ifdef _SC_PII_INTERNET_DGRAM
7099 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7100#endif
7101#ifdef _SC_PII_INTERNET_STREAM
7102 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7103#endif
7104#ifdef _SC_PII_OSI
7105 {"SC_PII_OSI", _SC_PII_OSI},
7106#endif
7107#ifdef _SC_PII_OSI_CLTS
7108 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7109#endif
7110#ifdef _SC_PII_OSI_COTS
7111 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7112#endif
7113#ifdef _SC_PII_OSI_M
7114 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7115#endif
7116#ifdef _SC_PII_SOCKET
7117 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7118#endif
7119#ifdef _SC_PII_XTI
7120 {"SC_PII_XTI", _SC_PII_XTI},
7121#endif
7122#ifdef _SC_POLL
7123 {"SC_POLL", _SC_POLL},
7124#endif
7125#ifdef _SC_PRIORITIZED_IO34
7126 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO34},
7127#endif
7128#ifdef _SC_PRIORITY_SCHEDULING35
7129 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING35},
7130#endif
7131#ifdef _SC_REALTIME_SIGNALS36
7132 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS36},
7133#endif
7134#ifdef _SC_RE_DUP_MAX16
7135 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX16},
7136#endif
7137#ifdef _SC_RTSIG_MAX48
7138 {"SC_RTSIG_MAX", _SC_RTSIG_MAX48},
7139#endif
7140#ifdef _SC_SAVED_IDS7
7141 {"SC_SAVED_IDS", _SC_SAVED_IDS7},
7142#endif
7143#ifdef _SC_SCHAR_MAX
7144 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7145#endif
7146#ifdef _SC_SCHAR_MIN
7147 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7148#endif
7149#ifdef _SC_SELECT
7150 {"SC_SELECT", _SC_SELECT},
7151#endif
7152#ifdef _SC_SEMAPHORES37
7153 {"SC_SEMAPHORES", _SC_SEMAPHORES37},
7154#endif
7155#ifdef _SC_SEM_NSEMS_MAX49
7156 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX49},
7157#endif
7158#ifdef _SC_SEM_VALUE_MAX50
7159 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX50},
7160#endif
7161#ifdef _SC_SHARED_MEMORY_OBJECTS39
7162 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS39},
7163#endif
7164#ifdef _SC_SHRT_MAX
7165 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7166#endif
7167#ifdef _SC_SHRT_MIN
7168 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7169#endif
7170#ifdef _SC_SIGQUEUE_MAX51
7171 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX51},
7172#endif
7173#ifdef _SC_SIGRT_MAX
7174 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7175#endif
7176#ifdef _SC_SIGRT_MIN
7177 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7178#endif
7179#ifdef _SC_SOFTPOWER
7180 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7181#endif
7182#ifdef _SC_SPLIT_CACHE
7183 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7184#endif
7185#ifdef _SC_SSIZE_MAX
7186 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7187#endif
7188#ifdef _SC_STACK_PROT
7189 {"SC_STACK_PROT", _SC_STACK_PROT},
7190#endif
7191#ifdef _SC_STREAM_MAX26
7192 {"SC_STREAM_MAX", _SC_STREAM_MAX26},
7193#endif
7194#ifdef _SC_SYNCHRONIZED_IO40
7195 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO40},
7196#endif
7197#ifdef _SC_THREADS96
7198 {"SC_THREADS", _SC_THREADS96},
7199#endif
7200#ifdef _SC_THREAD_ATTR_STACKADDR82
7201 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR82},
7202#endif
7203#ifdef _SC_THREAD_ATTR_STACKSIZE83
7204 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE83},
7205#endif
7206#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS85
7207 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS85},
7208#endif
7209#ifdef _SC_THREAD_KEYS_MAX86
7210 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX86},
7211#endif
7212#ifdef _SC_THREAD_PRIORITY_SCHEDULING89
7213 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING89},
7214#endif
7215#ifdef _SC_THREAD_PRIO_INHERIT87
7216 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT87},
7217#endif
7218#ifdef _SC_THREAD_PRIO_PROTECT88
7219 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT88},
7220#endif
7221#ifdef _SC_THREAD_PROCESS_SHARED90
7222 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED90},
7223#endif
7224#ifdef _SC_THREAD_SAFE_FUNCTIONS91
7225 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS91},
7226#endif
7227#ifdef _SC_THREAD_STACK_MIN93
7228 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN93},
7229#endif
7230#ifdef _SC_THREAD_THREADS_MAX94
7231 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX94},
7232#endif
7233#ifdef _SC_TIMERS41
7234 {"SC_TIMERS", _SC_TIMERS41},
7235#endif
7236#ifdef _SC_TIMER_MAX52
7237 {"SC_TIMER_MAX", _SC_TIMER_MAX52},
7238#endif
7239#ifdef _SC_TTY_NAME_MAX101
7240 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX101},
7241#endif
7242#ifdef _SC_TZNAME_MAX27
7243 {"SC_TZNAME_MAX", _SC_TZNAME_MAX27},
7244#endif
7245#ifdef _SC_T_IOV_MAX
7246 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7247#endif
7248#ifdef _SC_UCHAR_MAX
7249 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7250#endif
7251#ifdef _SC_UINT_MAX
7252 {"SC_UINT_MAX", _SC_UINT_MAX},
7253#endif
7254#ifdef _SC_UIO_MAXIOV
7255 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7256#endif
7257#ifdef _SC_ULONG_MAX
7258 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7259#endif
7260#ifdef _SC_USHRT_MAX
7261 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7262#endif
7263#ifdef _SC_VERSION8
7264 {"SC_VERSION", _SC_VERSION8},
7265#endif
7266#ifdef _SC_WORD_BIT
7267 {"SC_WORD_BIT", _SC_WORD_BIT},
7268#endif
7269#ifdef _SC_XBS5_ILP32_OFF32122
7270 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32122},
7271#endif
7272#ifdef _SC_XBS5_ILP32_OFFBIG123
7273 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG123},
7274#endif
7275#ifdef _SC_XBS5_LP64_OFF64124
7276 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64124},
7277#endif
7278#ifdef _SC_XBS5_LPBIG_OFFBIG125
7279 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG125},
7280#endif
7281#ifdef _SC_XOPEN_CRYPT108
7282 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT108},
7283#endif
7284#ifdef _SC_XOPEN_ENH_I18N109
7285 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N109},
7286#endif
7287#ifdef _SC_XOPEN_LEGACY110
7288 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY110},
7289#endif
7290#ifdef _SC_XOPEN_REALTIME111
7291 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME111},
7292#endif
7293#ifdef _SC_XOPEN_REALTIME_THREADS112
7294 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS112},
7295#endif
7296#ifdef _SC_XOPEN_SHM113
7297 {"SC_XOPEN_SHM", _SC_XOPEN_SHM113},
7298#endif
7299#ifdef _SC_XOPEN_UNIX115
7300 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX115},
7301#endif
7302#ifdef _SC_XOPEN_VERSION116
7303 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION116},
7304#endif
7305#ifdef _SC_XOPEN_XCU_VERSION121
7306 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION121},
7307#endif
7308#ifdef _SC_XOPEN_XPG2
7309 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7310#endif
7311#ifdef _SC_XOPEN_XPG3
7312 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7313#endif
7314#ifdef _SC_XOPEN_XPG4
7315 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7316#endif
7317};
7318
7319static int
7320conv_sysconf_confname(PyObject *arg, int *valuep)
7321{
7322 return conv_confname(arg, valuep, posix_constants_sysconf,
7323 sizeof(posix_constants_sysconf)
7324 / sizeof(struct constdef));
7325}
7326
7327PyDoc_STRVAR(posix_sysconf__doc__,static char posix_sysconf__doc__[] = "sysconf(name) -> integer\n\nReturn an integer-valued system configuration variable."
7328"sysconf(name) -> integer\n\n\static char posix_sysconf__doc__[] = "sysconf(name) -> integer\n\nReturn an integer-valued system configuration variable."
7329Return an integer-valued system configuration variable.")static char posix_sysconf__doc__[] = "sysconf(name) -> integer\n\nReturn an integer-valued system configuration variable.";
7330
7331static PyObject *
7332posix_sysconf(PyObject *self, PyObject *args)
7333{
7334 PyObject *result = NULL((void *)0);
7335 int name;
7336
7337 if (PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7338 int value;
7339
7340 errno(*__error()) = 0;
7341 value = sysconf(name);
7342 if (value == -1 && errno(*__error()) != 0)
7343 posix_error();
7344 else
7345 result = PyLong_FromLong(value);
7346 }
7347 return result;
7348}
7349#endif
7350
7351
7352/* This code is used to ensure that the tables of configuration value names
7353 * are in sorted order as required by conv_confname(), and also to build the
7354 * the exported dictionaries that are used to publish information about the
7355 * names available on the host platform.
7356 *
7357 * Sorting the table at runtime ensures that the table is properly ordered
7358 * when used, even for platforms we're not able to test on. It also makes
7359 * it easier to add additional entries to the tables.
7360 */
7361
7362static int
7363cmp_constdefs(const void *v1, const void *v2)
7364{
7365 const struct constdef *c1 =
7366 (const struct constdef *) v1;
7367 const struct constdef *c2 =
7368 (const struct constdef *) v2;
7369
7370 return strcmp(c1->name, c2->name);
7371}
7372
7373static int
7374setup_confname_table(struct constdef *table, size_t tablesize,
7375 char *tablename, PyObject *module)
7376{
7377 PyObject *d = NULL((void *)0);
7378 size_t i;
7379
7380 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7381 d = PyDict_New();
7382 if (d == NULL((void *)0))
7383 return -1;
7384
7385 for (i=0; i < tablesize; ++i) {
7386 PyObject *o = PyLong_FromLong(table[i].value);
7387 if (o == NULL((void *)0) || PyDict_SetItemString(d, table[i].name, o) == -1) {
7388 Py_XDECREF(o)do { if ((o) == ((void *)0)) ; else do { if (_Py_RefTotal-- ,
--((PyObject*)(o))->ob_refcnt != 0) { if (((PyObject*)o)->
ob_refcnt < 0) _Py_NegativeRefcount("./Modules/posixmodule.c"
, 7388, (PyObject *)(o)); } else _Py_Dealloc((PyObject *)(o))
; } while (0); } while (0)
;
7389 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 7389, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
7390 return -1;
7391 }
7392 Py_DECREF(o)do { if (_Py_RefTotal-- , --((PyObject*)(o))->ob_refcnt !=
0) { if (((PyObject*)o)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 7392, (PyObject *)(o)); } else _Py_Dealloc
((PyObject *)(o)); } while (0)
;
7393 }
7394 return PyModule_AddObject(module, tablename, d);
7395}
7396
7397/* Return -1 on failure, 0 on success. */
7398static int
7399setup_confname_tables(PyObject *module)
7400{
7401#if defined(HAVE_FPATHCONF1) || defined(HAVE_PATHCONF1)
7402 if (setup_confname_table(posix_constants_pathconf,
7403 sizeof(posix_constants_pathconf)
7404 / sizeof(struct constdef),
7405 "pathconf_names", module))
7406 return -1;
7407#endif
7408#ifdef HAVE_CONFSTR1
7409 if (setup_confname_table(posix_constants_confstr,
7410 sizeof(posix_constants_confstr)
7411 / sizeof(struct constdef),
7412 "confstr_names", module))
7413 return -1;
7414#endif
7415#ifdef HAVE_SYSCONF1
7416 if (setup_confname_table(posix_constants_sysconf,
7417 sizeof(posix_constants_sysconf)
7418 / sizeof(struct constdef),
7419 "sysconf_names", module))
7420 return -1;
7421#endif
7422 return 0;
7423}
7424
7425
7426PyDoc_STRVAR(posix_abort__doc__,static char posix_abort__doc__[] = "abort() -> does not return!\n\nAbort the interpreter immediately. This 'dumps core' or otherwise fails\nin the hardest way possible on the hosting operating system."
7427"abort() -> does not return!\n\n\static char posix_abort__doc__[] = "abort() -> does not return!\n\nAbort the interpreter immediately. This 'dumps core' or otherwise fails\nin the hardest way possible on the hosting operating system."
7428Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\static char posix_abort__doc__[] = "abort() -> does not return!\n\nAbort the interpreter immediately. This 'dumps core' or otherwise fails\nin the hardest way possible on the hosting operating system."
7429in the hardest way possible on the hosting operating system.")static char posix_abort__doc__[] = "abort() -> does not return!\n\nAbort the interpreter immediately. This 'dumps core' or otherwise fails\nin the hardest way possible on the hosting operating system.";
7430
7431static PyObject *
7432posix_abort(PyObject *self, PyObject *noargs)
7433{
7434 abort();
7435 /*NOTREACHED*/
7436 Py_FatalError("abort() called from Python code didn't abort!");
7437 return NULL((void *)0);
7438}
7439
7440#ifdef MS_WINDOWS
7441PyDoc_STRVAR(win32_startfile__doc__,static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7442"startfile(filepath [, operation]) - Start a file with its associated\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7443application.\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7444\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7445When \"operation\" is not specified or \"open\", this acts like\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7446double-clicking the file in Explorer, or giving the file name as an\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7447argument to the DOS \"start\" command: the file is opened with whatever\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7448application (if any) its extension is associated.\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7449When another \"operation\" is given, it specifies what should be done with\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7450the file. A typical operation is \"print\".\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7451\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7452startfile returns as soon as the associated application is launched.\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7453There is no option to wait for the application to close, and no way\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7454to retrieve the application's exit status.\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7455\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7456The filepath is relative to the current directory. If you want to use\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7457an absolute path, make sure the first character is not a slash (\"/\");\n\static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is."
7458the underlying Win32 ShellExecute function doesn't work if it is.")static char win32_startfile__doc__[] = "startfile(filepath [, operation]) - Start a file with its associated\napplication.\n\nWhen \"operation\" is not specified or \"open\", this acts like\ndouble-clicking the file in Explorer, or giving the file name as an\nargument to the DOS \"start\" command: the file is opened with whatever\napplication (if any) its extension is associated.\nWhen another \"operation\" is given, it specifies what should be done with\nthe file. A typical operation is \"print\".\n\nstartfile returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way\nto retrieve the application's exit status.\n\nThe filepath is relative to the current directory. If you want to use\nan absolute path, make sure the first character is not a slash (\"/\");\nthe underlying Win32 ShellExecute function doesn't work if it is.";
7459
7460static PyObject *
7461win32_startfile(PyObject *self, PyObject *args)
7462{
7463 PyObject *ofilepath;
7464 char *filepath;
7465 char *operation = NULL((void *)0);
7466 HINSTANCE rc;
7467
7468 PyObject *unipath, *woperation = NULL((void *)0);
7469 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "U|s:startfile",
7470 &unipath, &operation)) {
7471 PyErr_Clear();
7472 goto normal;
7473 }
7474
7475 if (operation) {
7476 woperation = PyUnicode_DecodeASCIIPyUnicodeUCS2_DecodeASCII(operation,
7477 strlen(operation), NULL((void *)0));
7478 if (!woperation) {
7479 PyErr_Clear();
7480 operation = NULL((void *)0);
7481 goto normal;
7482 }
7483 }
7484
7485 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
7486 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation)((__builtin_expect(!(((((((PyObject*)(woperation))->ob_type
))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/posixmodule.c", 7486, "PyUnicode_Check(woperation)"
) : (void)0),(((PyUnicodeObject *)(woperation))->str))
: 0,
7487 PyUnicode_AS_UNICODE(unipath)((__builtin_expect(!(((((((PyObject*)(unipath))->ob_type))
->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/posixmodule.c", 7487, "PyUnicode_Check(unipath)"
) : (void)0),(((PyUnicodeObject *)(unipath))->str))
,
7488 NULL((void *)0), NULL((void *)0), SW_SHOWNORMAL);
7489 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
7490
7491 Py_XDECREF(woperation)do { if ((woperation) == ((void *)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(woperation))->ob_refcnt != 0) { if (((
PyObject*)woperation)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 7491, (PyObject *)(woperation)); }
else _Py_Dealloc((PyObject *)(woperation)); } while (0); } while
(0)
;
7492 if (rc <= (HINSTANCE)32) {
7493 PyObject *errval = win32_error_unicode("startfile",
7494 PyUnicode_AS_UNICODE(unipath)((__builtin_expect(!(((((((PyObject*)(unipath))->ob_type))
->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/posixmodule.c", 7494, "PyUnicode_Check(unipath)"
) : (void)0),(((PyUnicodeObject *)(unipath))->str))
);
7495 return errval;
7496 }
7497 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
7498 return Py_None(&_Py_NoneStruct);
7499
7500normal:
7501 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&|s:startfile",
7502 PyUnicode_FSConverterPyUnicodeUCS2_FSConverter, &ofilepath,
7503 &operation))
7504 return NULL((void *)0);
7505 filepath = PyBytes_AsString(ofilepath);
7506 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
7507 rc = ShellExecute((HWND)0, operation, filepath,
7508 NULL((void *)0), NULL((void *)0), SW_SHOWNORMAL);
7509 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
7510 if (rc <= (HINSTANCE)32) {
7511 PyObject *errval = win32_error("startfile", filepath);
7512 Py_DECREF(ofilepath)do { if (_Py_RefTotal-- , --((PyObject*)(ofilepath))->ob_refcnt
!= 0) { if (((PyObject*)ofilepath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 7512, (PyObject *)(ofilepath)); }
else _Py_Dealloc((PyObject *)(ofilepath)); } while (0)
;
7513 return errval;
7514 }
7515 Py_DECREF(ofilepath)do { if (_Py_RefTotal-- , --((PyObject*)(ofilepath))->ob_refcnt
!= 0) { if (((PyObject*)ofilepath)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 7515, (PyObject *)(ofilepath)); }
else _Py_Dealloc((PyObject *)(ofilepath)); } while (0)
;
7516 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
7517 return Py_None(&_Py_NoneStruct);
7518}
7519#endif
7520
7521#ifdef HAVE_GETLOADAVG1
7522PyDoc_STRVAR(posix_getloadavg__doc__,static char posix_getloadavg__doc__[] = "getloadavg() -> (float, float, float)\n\nReturn the number of processes in the system run queue averaged over\nthe last 1, 5, and 15 minutes or raises OSError if the load average\nwas unobtainable"
7523"getloadavg() -> (float, float, float)\n\n\static char posix_getloadavg__doc__[] = "getloadavg() -> (float, float, float)\n\nReturn the number of processes in the system run queue averaged over\nthe last 1, 5, and 15 minutes or raises OSError if the load average\nwas unobtainable"
7524Return the number of processes in the system run queue averaged over\n\static char posix_getloadavg__doc__[] = "getloadavg() -> (float, float, float)\n\nReturn the number of processes in the system run queue averaged over\nthe last 1, 5, and 15 minutes or raises OSError if the load average\nwas unobtainable"
7525the last 1, 5, and 15 minutes or raises OSError if the load average\n\static char posix_getloadavg__doc__[] = "getloadavg() -> (float, float, float)\n\nReturn the number of processes in the system run queue averaged over\nthe last 1, 5, and 15 minutes or raises OSError if the load average\nwas unobtainable"
7526was unobtainable")static char posix_getloadavg__doc__[] = "getloadavg() -> (float, float, float)\n\nReturn the number of processes in the system run queue averaged over\nthe last 1, 5, and 15 minutes or raises OSError if the load average\nwas unobtainable";
7527
7528static PyObject *
7529posix_getloadavg(PyObject *self, PyObject *noargs)
7530{
7531 double loadavg[3];
7532 if (getloadavg(loadavg, 3)!=3) {
7533 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7534 return NULL((void *)0);
7535 } else
7536 return Py_BuildValue_Py_BuildValue_SizeT("ddd", loadavg[0], loadavg[1], loadavg[2]);
7537}
7538#endif
7539
7540#ifdef MS_WINDOWS
7541
7542PyDoc_STRVAR(win32_urandom__doc__,static char win32_urandom__doc__[] = "urandom(n) -> str\n\nReturn n random bytes suitable for cryptographic use."
7543"urandom(n) -> str\n\n\static char win32_urandom__doc__[] = "urandom(n) -> str\n\nReturn n random bytes suitable for cryptographic use."
7544Return n random bytes suitable for cryptographic use.")static char win32_urandom__doc__[] = "urandom(n) -> str\n\nReturn n random bytes suitable for cryptographic use.";
7545
7546typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7547 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7548 DWORD dwFlags );
7549typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7550 BYTE *pbBuffer );
7551
7552static CRYPTGENRANDOM pCryptGenRandom = NULL((void *)0);
7553/* This handle is never explicitly released. Instead, the operating
7554 system will release it when the process terminates. */
7555static HCRYPTPROV hCryptProv = 0;
7556
7557static PyObject*
7558win32_urandom(PyObject *self, PyObject *args)
7559{
7560 int howMany;
7561 PyObject* result;
7562
7563 /* Read arguments */
7564 if (! PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:urandom", &howMany))
7565 return NULL((void *)0);
7566 if (howMany < 0)
7567 return PyErr_Format(PyExc_ValueError,
7568 "negative argument not allowed");
7569
7570 if (hCryptProv == 0) {
7571 HINSTANCE hAdvAPI32 = NULL((void *)0);
7572 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL((void *)0);
7573
7574 /* Obtain handle to the DLL containing CryptoAPI
7575 This should not fail */
7576 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7577 if(hAdvAPI32 == NULL((void *)0))
7578 return win32_error("GetModuleHandle", NULL((void *)0));
7579
7580 /* Obtain pointers to the CryptoAPI functions
7581 This will fail on some early versions of Win95 */
7582 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7583 hAdvAPI32,
7584 "CryptAcquireContextA");
7585 if (pCryptAcquireContext == NULL((void *)0))
7586 return PyErr_Format(PyExc_NotImplementedError,
7587 "CryptAcquireContextA not found");
7588
7589 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7590 hAdvAPI32, "CryptGenRandom");
7591 if (pCryptGenRandom == NULL((void *)0))
7592 return PyErr_Format(PyExc_NotImplementedError,
7593 "CryptGenRandom not found");
7594
7595 /* Acquire context */
7596 if (! pCryptAcquireContext(&hCryptProv, NULL((void *)0), NULL((void *)0),
7597 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7598 return win32_error("CryptAcquireContext", NULL((void *)0));
7599 }
7600
7601 /* Allocate bytes */
7602 result = PyBytes_FromStringAndSize(NULL((void *)0), howMany);
7603 if (result != NULL((void *)0)) {
7604 /* Get random data */
7605 memset(PyBytes_AS_STRING(result), 0, howMany)((__builtin_object_size (((__builtin_expect(!(((((((PyObject*
)(result))->ob_type))->tp_flags & ((1L<<27)))
!= 0)), 0) ? __assert_rtn(__func__, "./Modules/posixmodule.c"
, 7605, "PyBytes_Check(result)") : (void)0), (((PyBytesObject
*)(result))->ob_sval)), 0) != (size_t) -1) ? __builtin___memset_chk
(((__builtin_expect(!(((((((PyObject*)(result))->ob_type)
)->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/posixmodule.c", 7605, "PyBytes_Check(result)"
) : (void)0), (((PyBytesObject *)(result))->ob_sval)), 0, howMany
, __builtin_object_size (((__builtin_expect(!(((((((PyObject*
)(result))->ob_type))->tp_flags & ((1L<<27)))
!= 0)), 0) ? __assert_rtn(__func__, "./Modules/posixmodule.c"
, 7605, "PyBytes_Check(result)") : (void)0), (((PyBytesObject
*)(result))->ob_sval)), 0)) : __inline_memset_chk (((__builtin_expect
(!(((((((PyObject*)(result))->ob_type))->tp_flags &
((1L<<27))) != 0)), 0) ? __assert_rtn(__func__, "./Modules/posixmodule.c"
, 7605, "PyBytes_Check(result)") : (void)0), (((PyBytesObject
*)(result))->ob_sval)), 0, howMany))
; /* zero seed */
7606 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7607 PyBytes_AS_STRING(result)((__builtin_expect(!(((((((PyObject*)(result))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 7607, "PyBytes_Check(result)") :
(void)0), (((PyBytesObject *)(result))->ob_sval))
)) {
7608 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 7608, (PyObject *)(result)); } else
_Py_Dealloc((PyObject *)(result)); } while (0)
;
7609 return win32_error("CryptGenRandom", NULL((void *)0));
7610 }
7611 }
7612 return result;
7613}
7614#endif
7615
7616PyDoc_STRVAR(device_encoding__doc__,static char device_encoding__doc__[] = "device_encoding(fd) -> str\n\nReturn a string describing the encoding of the device\nif the output is a terminal; else return None."
7617"device_encoding(fd) -> str\n\n\static char device_encoding__doc__[] = "device_encoding(fd) -> str\n\nReturn a string describing the encoding of the device\nif the output is a terminal; else return None."
7618Return a string describing the encoding of the device\n\static char device_encoding__doc__[] = "device_encoding(fd) -> str\n\nReturn a string describing the encoding of the device\nif the output is a terminal; else return None."
7619if the output is a terminal; else return None.")static char device_encoding__doc__[] = "device_encoding(fd) -> str\n\nReturn a string describing the encoding of the device\nif the output is a terminal; else return None.";
7620
7621static PyObject *
7622device_encoding(PyObject *self, PyObject *args)
7623{
7624 int fd;
7625 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:device_encoding", &fd))
7626 return NULL((void *)0);
7627 if (!_PyVerify_fd(fd)(1) || !isatty(fd)) {
7628 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
7629 return Py_None(&_Py_NoneStruct);
7630 }
7631#if defined(MS_WINDOWS) || defined(MS_WIN64)
7632 if (fd == 0) {
7633 char buf[100];
7634 sprintf(buf, "cp%d", GetConsoleCP())__builtin___sprintf_chk (buf, 0, __builtin_object_size (buf, 2
> 1), "cp%d", GetConsoleCP())
;
7635 return PyUnicode_FromStringPyUnicodeUCS2_FromString(buf);
7636 }
7637 if (fd == 1 || fd == 2) {
7638 char buf[100];
7639 sprintf(buf, "cp%d", GetConsoleOutputCP())__builtin___sprintf_chk (buf, 0, __builtin_object_size (buf, 2
> 1), "cp%d", GetConsoleOutputCP())
;
7640 return PyUnicode_FromStringPyUnicodeUCS2_FromString(buf);
7641 }
7642#elif defined(CODESET0)
7643 {
7644 char *codeset = nl_langinfo(CODESET0);
7645 if (codeset != NULL((void *)0) && codeset[0] != 0)
7646 return PyUnicode_FromStringPyUnicodeUCS2_FromString(codeset);
7647 }
7648#endif
7649 Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt
++)
;
7650 return Py_None(&_Py_NoneStruct);
7651}
7652
7653#ifdef __VMS
7654/* Use openssl random routine */
7655#include <openssl/rand.h>
7656PyDoc_STRVAR(vms_urandom__doc__,static char vms_urandom__doc__[] = "urandom(n) -> str\n\nReturn n random bytes suitable for cryptographic use."
7657"urandom(n) -> str\n\n\static char vms_urandom__doc__[] = "urandom(n) -> str\n\nReturn n random bytes suitable for cryptographic use."
7658Return n random bytes suitable for cryptographic use.")static char vms_urandom__doc__[] = "urandom(n) -> str\n\nReturn n random bytes suitable for cryptographic use.";
7659
7660static PyObject*
7661vms_urandom(PyObject *self, PyObject *args)
7662{
7663 int howMany;
7664 PyObject* result;
7665
7666 /* Read arguments */
7667 if (! PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i:urandom", &howMany))
7668 return NULL((void *)0);
7669 if (howMany < 0)
7670 return PyErr_Format(PyExc_ValueError,
7671 "negative argument not allowed");
7672
7673 /* Allocate bytes */
7674 result = PyBytes_FromStringAndSize(NULL((void *)0), howMany);
7675 if (result != NULL((void *)0)) {
7676 /* Get random data */
7677 if (RAND_pseudo_bytes((unsigned char*)
7678 PyBytes_AS_STRING(result)((__builtin_expect(!(((((((PyObject*)(result))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/posixmodule.c", 7678, "PyBytes_Check(result)") :
(void)0), (((PyBytesObject *)(result))->ob_sval))
,
7679 howMany) < 0) {
7680 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 7680, (PyObject *)(result)); } else
_Py_Dealloc((PyObject *)(result)); } while (0)
;
7681 return PyErr_Format(PyExc_ValueError,
7682 "RAND_pseudo_bytes");
7683 }
7684 }
7685 return result;
7686}
7687#endif
7688
7689#ifdef HAVE_SETRESUID
7690PyDoc_STRVAR(posix_setresuid__doc__,static char posix_setresuid__doc__[] = "setresuid(ruid, euid, suid)\n\nSet the current process's real, effective, and saved user ids."
7691"setresuid(ruid, euid, suid)\n\n\static char posix_setresuid__doc__[] = "setresuid(ruid, euid, suid)\n\nSet the current process's real, effective, and saved user ids."
7692Set the current process's real, effective, and saved user ids.")static char posix_setresuid__doc__[] = "setresuid(ruid, euid, suid)\n\nSet the current process's real, effective, and saved user ids.";
7693
7694static PyObject*
7695posix_setresuid (PyObject *self, PyObject *args)
7696{
7697 /* We assume uid_t is no larger than a long. */
7698 long ruid, euid, suid;
7699 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "lll", &ruid, &euid, &suid))
7700 return NULL((void *)0);
7701 if (setresuid(ruid, euid, suid) < 0)
7702 return posix_error();
7703 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
7704}
7705#endif
7706
7707#ifdef HAVE_SETRESGID
7708PyDoc_STRVAR(posix_setresgid__doc__,static char posix_setresgid__doc__[] = "setresgid(rgid, egid, sgid)\n\nSet the current process's real, effective, and saved group ids."
7709"setresgid(rgid, egid, sgid)\n\n\static char posix_setresgid__doc__[] = "setresgid(rgid, egid, sgid)\n\nSet the current process's real, effective, and saved group ids."
7710Set the current process's real, effective, and saved group ids.")static char posix_setresgid__doc__[] = "setresgid(rgid, egid, sgid)\n\nSet the current process's real, effective, and saved group ids.";
7711
7712static PyObject*
7713posix_setresgid (PyObject *self, PyObject *args)
7714{
7715 /* We assume uid_t is no larger than a long. */
7716 long rgid, egid, sgid;
7717 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "lll", &rgid, &egid, &sgid))
7718 return NULL((void *)0);
7719 if (setresgid(rgid, egid, sgid) < 0)
7720 return posix_error();
7721 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
7722}
7723#endif
7724
7725#ifdef HAVE_GETRESUID
7726PyDoc_STRVAR(posix_getresuid__doc__,static char posix_getresuid__doc__[] = "getresuid() -> (ruid, euid, suid)\n\nGet tuple of the current process's real, effective, and saved user ids."
7727"getresuid() -> (ruid, euid, suid)\n\n\static char posix_getresuid__doc__[] = "getresuid() -> (ruid, euid, suid)\n\nGet tuple of the current process's real, effective, and saved user ids."
7728Get tuple of the current process's real, effective, and saved user ids.")static char posix_getresuid__doc__[] = "getresuid() -> (ruid, euid, suid)\n\nGet tuple of the current process's real, effective, and saved user ids.";
7729
7730static PyObject*
7731posix_getresuid (PyObject *self, PyObject *noargs)
7732{
7733 uid_t ruid, euid, suid;
7734 long l_ruid, l_euid, l_suid;
7735 if (getresuid(&ruid, &euid, &suid) < 0)
7736 return posix_error();
7737 /* Force the values into long's as we don't know the size of uid_t. */
7738 l_ruid = ruid;
7739 l_euid = euid;
7740 l_suid = suid;
7741 return Py_BuildValue_Py_BuildValue_SizeT("(lll)", l_ruid, l_euid, l_suid);
7742}
7743#endif
7744
7745#ifdef HAVE_GETRESGID
7746PyDoc_STRVAR(posix_getresgid__doc__,static char posix_getresgid__doc__[] = "getresgid() -> (rgid, egid, sgid)\n\nGet tuple of the current process's real, effective, and saved group ids."
7747"getresgid() -> (rgid, egid, sgid)\n\n\static char posix_getresgid__doc__[] = "getresgid() -> (rgid, egid, sgid)\n\nGet tuple of the current process's real, effective, and saved group ids."
7748Get tuple of the current process's real, effective, and saved group ids.")static char posix_getresgid__doc__[] = "getresgid() -> (rgid, egid, sgid)\n\nGet tuple of the current process's real, effective, and saved group ids.";
7749
7750static PyObject*
7751posix_getresgid (PyObject *self, PyObject *noargs)
7752{
7753 uid_t rgid, egid, sgid;
7754 long l_rgid, l_egid, l_sgid;
7755 if (getresgid(&rgid, &egid, &sgid) < 0)
7756 return posix_error();
7757 /* Force the values into long's as we don't know the size of uid_t. */
7758 l_rgid = rgid;
7759 l_egid = egid;
7760 l_sgid = sgid;
7761 return Py_BuildValue_Py_BuildValue_SizeT("(lll)", l_rgid, l_egid, l_sgid);
7762}
7763#endif
7764
7765static PyMethodDef posix_methods[] = {
7766 {"access", posix_access, METH_VARARGS0x0001, posix_access__doc__},
7767#ifdef HAVE_TTYNAME1
7768 {"ttyname", posix_ttyname, METH_VARARGS0x0001, posix_ttyname__doc__},
7769#endif
7770 {"chdir", posix_chdir, METH_VARARGS0x0001, posix_chdir__doc__},
7771#ifdef HAVE_CHFLAGS
7772 {"chflags", posix_chflags, METH_VARARGS0x0001, posix_chflags__doc__},
7773#endif /* HAVE_CHFLAGS */
7774 {"chmod", posix_chmod, METH_VARARGS0x0001, posix_chmod__doc__},
7775#ifdef HAVE_FCHMOD1
7776 {"fchmod", posix_fchmod, METH_VARARGS0x0001, posix_fchmod__doc__},
7777#endif /* HAVE_FCHMOD */
7778#ifdef HAVE_CHOWN1
7779 {"chown", posix_chown, METH_VARARGS0x0001, posix_chown__doc__},
7780#endif /* HAVE_CHOWN */
7781#ifdef HAVE_LCHMOD1
7782 {"lchmod", posix_lchmod, METH_VARARGS0x0001, posix_lchmod__doc__},
7783#endif /* HAVE_LCHMOD */
7784#ifdef HAVE_FCHOWN1
7785 {"fchown", posix_fchown, METH_VARARGS0x0001, posix_fchown__doc__},
7786#endif /* HAVE_FCHOWN */
7787#ifdef HAVE_LCHFLAGS
7788 {"lchflags", posix_lchflags, METH_VARARGS0x0001, posix_lchflags__doc__},
7789#endif /* HAVE_LCHFLAGS */
7790#ifdef HAVE_LCHOWN1
7791 {"lchown", posix_lchown, METH_VARARGS0x0001, posix_lchown__doc__},
7792#endif /* HAVE_LCHOWN */
7793#ifdef HAVE_CHROOT1
7794 {"chroot", posix_chroot, METH_VARARGS0x0001, posix_chroot__doc__},
7795#endif
7796#ifdef HAVE_CTERMID1
7797 {"ctermid", posix_ctermid, METH_NOARGS0x0004, posix_ctermid__doc__},
7798#endif
7799#ifdef HAVE_GETCWD1
7800 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7801 METH_NOARGS0x0004, posix_getcwd__doc__},
7802 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7803 METH_NOARGS0x0004, posix_getcwdb__doc__},
7804#endif
7805#ifdef HAVE_LINK1
7806 {"link", posix_link, METH_VARARGS0x0001, posix_link__doc__},
7807#endif /* HAVE_LINK */
7808 {"listdir", posix_listdir, METH_VARARGS0x0001, posix_listdir__doc__},
7809 {"lstat", posix_lstat, METH_VARARGS0x0001, posix_lstat__doc__},
7810 {"mkdir", posix_mkdir, METH_VARARGS0x0001, posix_mkdir__doc__},
7811#ifdef HAVE_NICE1
7812 {"nice", posix_nice, METH_VARARGS0x0001, posix_nice__doc__},
7813#endif /* HAVE_NICE */
7814#ifdef HAVE_READLINK1
7815 {"readlink", posix_readlink, METH_VARARGS0x0001, posix_readlink__doc__},
7816#endif /* HAVE_READLINK */
7817#if !defined(HAVE_READLINK1) && defined(MS_WINDOWS)
7818 {"readlink", win_readlink, METH_VARARGS0x0001, win_readlink__doc__},
7819#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
7820 {"rename", posix_rename, METH_VARARGS0x0001, posix_rename__doc__},
7821 {"rmdir", posix_rmdir, METH_VARARGS0x0001, posix_rmdir__doc__},
7822 {"stat", posix_stat, METH_VARARGS0x0001, posix_stat__doc__},
7823 {"stat_float_times", stat_float_times, METH_VARARGS0x0001, stat_float_times__doc__},
7824#if defined(HAVE_SYMLINK1) && !defined(MS_WINDOWS)
7825 {"symlink", posix_symlink, METH_VARARGS0x0001, posix_symlink__doc__},
7826#endif /* HAVE_SYMLINK */
7827#if defined(HAVE_SYMLINK1) && defined(MS_WINDOWS)
7828 {"symlink", (PyCFunction)win_symlink, METH_VARARGS0x0001 | METH_KEYWORDS0x0002,
7829 win_symlink__doc__},
7830#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
7831#ifdef HAVE_SYSTEM1
7832 {"system", posix_system, METH_VARARGS0x0001, posix_system__doc__},
7833#endif
7834 {"umask", posix_umask, METH_VARARGS0x0001, posix_umask__doc__},
7835#ifdef HAVE_UNAME1
7836 {"uname", posix_uname, METH_NOARGS0x0004, posix_uname__doc__},
7837#endif /* HAVE_UNAME */
7838 {"unlink", posix_unlink, METH_VARARGS0x0001, posix_unlink__doc__},
7839 {"remove", posix_unlink, METH_VARARGS0x0001, posix_remove__doc__},
7840 {"utime", posix_utime, METH_VARARGS0x0001, posix_utime__doc__},
7841#ifdef HAVE_TIMES1
7842 {"times", posix_times, METH_NOARGS0x0004, posix_times__doc__},
7843#endif /* HAVE_TIMES */
7844 {"_exit", posix__exit, METH_VARARGS0x0001, posix__exit__doc__},
7845#ifdef HAVE_EXECV1
7846 {"execv", posix_execv, METH_VARARGS0x0001, posix_execv__doc__},
7847 {"execve", posix_execve, METH_VARARGS0x0001, posix_execve__doc__},
7848#endif /* HAVE_EXECV */
7849#ifdef HAVE_SPAWNV
7850 {"spawnv", posix_spawnv, METH_VARARGS0x0001, posix_spawnv__doc__},
7851 {"spawnve", posix_spawnve, METH_VARARGS0x0001, posix_spawnve__doc__},
7852#if defined(PYOS_OS2)
7853 {"spawnvp", posix_spawnvp, METH_VARARGS0x0001, posix_spawnvp__doc__},
7854 {"spawnvpe", posix_spawnvpe, METH_VARARGS0x0001, posix_spawnvpe__doc__},
7855#endif /* PYOS_OS2 */
7856#endif /* HAVE_SPAWNV */
7857#ifdef HAVE_FORK1
7858 {"fork1", posix_fork1, METH_NOARGS0x0004, posix_fork1__doc__},
7859#endif /* HAVE_FORK1 */
7860#ifdef HAVE_FORK1
7861 {"fork", posix_fork, METH_NOARGS0x0004, posix_fork__doc__},
7862#endif /* HAVE_FORK */
7863#if defined(HAVE_OPENPTY1) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX1)
7864 {"openpty", posix_openpty, METH_NOARGS0x0004, posix_openpty__doc__},
7865#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
7866#ifdef HAVE_FORKPTY1
7867 {"forkpty", posix_forkpty, METH_NOARGS0x0004, posix_forkpty__doc__},
7868#endif /* HAVE_FORKPTY */
7869#ifdef HAVE_GETEGID1
7870 {"getegid", posix_getegid, METH_NOARGS0x0004, posix_getegid__doc__},
7871#endif /* HAVE_GETEGID */
7872#ifdef HAVE_GETEUID1
7873 {"geteuid", posix_geteuid, METH_NOARGS0x0004, posix_geteuid__doc__},
7874#endif /* HAVE_GETEUID */
7875#ifdef HAVE_GETGID1
7876 {"getgid", posix_getgid, METH_NOARGS0x0004, posix_getgid__doc__},
7877#endif /* HAVE_GETGID */
7878#ifdef HAVE_GETGROUPS1
7879 {"getgroups", posix_getgroups, METH_NOARGS0x0004, posix_getgroups__doc__},
7880#endif
7881 {"getpid", posix_getpid, METH_NOARGS0x0004, posix_getpid__doc__},
7882#ifdef HAVE_GETPGRP1
7883 {"getpgrp", posix_getpgrp, METH_NOARGS0x0004, posix_getpgrp__doc__},
7884#endif /* HAVE_GETPGRP */
7885#ifdef HAVE_GETPPID1
7886 {"getppid", posix_getppid, METH_NOARGS0x0004, posix_getppid__doc__},
7887#endif /* HAVE_GETPPID */
7888#ifdef HAVE_GETUID1
7889 {"getuid", posix_getuid, METH_NOARGS0x0004, posix_getuid__doc__},
7890#endif /* HAVE_GETUID */
7891#ifdef HAVE_GETLOGIN1
7892 {"getlogin", posix_getlogin, METH_NOARGS0x0004, posix_getlogin__doc__},
7893#endif
7894#ifdef HAVE_KILL1
7895 {"kill", posix_kill, METH_VARARGS0x0001, posix_kill__doc__},
7896#endif /* HAVE_KILL */
7897#ifdef HAVE_KILLPG1
7898 {"killpg", posix_killpg, METH_VARARGS0x0001, posix_killpg__doc__},
7899#endif /* HAVE_KILLPG */
7900#ifdef HAVE_PLOCK
7901 {"plock", posix_plock, METH_VARARGS0x0001, posix_plock__doc__},
7902#endif /* HAVE_PLOCK */
7903#ifdef MS_WINDOWS
7904 {"startfile", win32_startfile, METH_VARARGS0x0001, win32_startfile__doc__},
7905 {"kill", win32_kill, METH_VARARGS0x0001, win32_kill__doc__},
7906 {"link", win32_link, METH_VARARGS0x0001, win32_link__doc__},
7907#endif
7908#ifdef HAVE_SETUID1
7909 {"setuid", posix_setuid, METH_VARARGS0x0001, posix_setuid__doc__},
7910#endif /* HAVE_SETUID */
7911#ifdef HAVE_SETEUID1
7912 {"seteuid", posix_seteuid, METH_VARARGS0x0001, posix_seteuid__doc__},
7913#endif /* HAVE_SETEUID */
7914#ifdef HAVE_SETEGID1
7915 {"setegid", posix_setegid, METH_VARARGS0x0001, posix_setegid__doc__},
7916#endif /* HAVE_SETEGID */
7917#ifdef HAVE_SETREUID1
7918 {"setreuid", posix_setreuid, METH_VARARGS0x0001, posix_setreuid__doc__},
7919#endif /* HAVE_SETREUID */
7920#ifdef HAVE_SETREGID1
7921 {"setregid", posix_setregid, METH_VARARGS0x0001, posix_setregid__doc__},
7922#endif /* HAVE_SETREGID */
7923#ifdef HAVE_SETGID1
7924 {"setgid", posix_setgid, METH_VARARGS0x0001, posix_setgid__doc__},
7925#endif /* HAVE_SETGID */
7926#ifdef HAVE_SETGROUPS1
7927 {"setgroups", posix_setgroups, METH_O0x0008, posix_setgroups__doc__},
7928#endif /* HAVE_SETGROUPS */
7929#ifdef HAVE_INITGROUPS1
7930 {"initgroups", posix_initgroups, METH_VARARGS0x0001, posix_initgroups__doc__},
7931#endif /* HAVE_INITGROUPS */
7932#ifdef HAVE_GETPGID1
7933 {"getpgid", posix_getpgid, METH_VARARGS0x0001, posix_getpgid__doc__},
7934#endif /* HAVE_GETPGID */
7935#ifdef HAVE_SETPGRP1
7936 {"setpgrp", posix_setpgrp, METH_NOARGS0x0004, posix_setpgrp__doc__},
7937#endif /* HAVE_SETPGRP */
7938#ifdef HAVE_WAIT1
7939 {"wait", posix_wait, METH_NOARGS0x0004, posix_wait__doc__},
7940#endif /* HAVE_WAIT */
7941#ifdef HAVE_WAIT31
7942 {"wait3", posix_wait3, METH_VARARGS0x0001, posix_wait3__doc__},
7943#endif /* HAVE_WAIT3 */
7944#ifdef HAVE_WAIT41
7945 {"wait4", posix_wait4, METH_VARARGS0x0001, posix_wait4__doc__},
7946#endif /* HAVE_WAIT4 */
7947#if defined(HAVE_WAITPID1) || defined(HAVE_CWAIT)
7948 {"waitpid", posix_waitpid, METH_VARARGS0x0001, posix_waitpid__doc__},
7949#endif /* HAVE_WAITPID */
7950#ifdef HAVE_GETSID1
7951 {"getsid", posix_getsid, METH_VARARGS0x0001, posix_getsid__doc__},
7952#endif /* HAVE_GETSID */
7953#ifdef HAVE_SETSID1
7954 {"setsid", posix_setsid, METH_NOARGS0x0004, posix_setsid__doc__},
7955#endif /* HAVE_SETSID */
7956#ifdef HAVE_SETPGID1
7957 {"setpgid", posix_setpgid, METH_VARARGS0x0001, posix_setpgid__doc__},
7958#endif /* HAVE_SETPGID */
7959#ifdef HAVE_TCGETPGRP1
7960 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS0x0001, posix_tcgetpgrp__doc__},
7961#endif /* HAVE_TCGETPGRP */
7962#ifdef HAVE_TCSETPGRP1
7963 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS0x0001, posix_tcsetpgrp__doc__},
7964#endif /* HAVE_TCSETPGRP */
7965 {"open", posix_open, METH_VARARGS0x0001, posix_open__doc__},
7966 {"close", posix_close, METH_VARARGS0x0001, posix_close__doc__},
7967 {"closerange", posix_closerange, METH_VARARGS0x0001, posix_closerange__doc__},
7968 {"device_encoding", device_encoding, METH_VARARGS0x0001, device_encoding__doc__},
7969 {"dup", posix_dup, METH_VARARGS0x0001, posix_dup__doc__},
7970 {"dup2", posix_dup2, METH_VARARGS0x0001, posix_dup2__doc__},
7971 {"lseek", posix_lseek, METH_VARARGS0x0001, posix_lseek__doc__},
7972 {"read", posix_read, METH_VARARGS0x0001, posix_read__doc__},
7973 {"write", posix_write, METH_VARARGS0x0001, posix_write__doc__},
7974 {"fstat", posix_fstat, METH_VARARGS0x0001, posix_fstat__doc__},
7975 {"isatty", posix_isatty, METH_VARARGS0x0001, posix_isatty__doc__},
7976#ifdef HAVE_PIPE1
7977 {"pipe", posix_pipe, METH_NOARGS0x0004, posix_pipe__doc__},
7978#endif
7979#ifdef HAVE_MKFIFO1
7980 {"mkfifo", posix_mkfifo, METH_VARARGS0x0001, posix_mkfifo__doc__},
7981#endif
7982#if defined(HAVE_MKNOD1) && defined(HAVE_MAKEDEV1)
7983 {"mknod", posix_mknod, METH_VARARGS0x0001, posix_mknod__doc__},
7984#endif
7985#ifdef HAVE_DEVICE_MACROS1
7986 {"major", posix_major, METH_VARARGS0x0001, posix_major__doc__},
7987 {"minor", posix_minor, METH_VARARGS0x0001, posix_minor__doc__},
7988 {"makedev", posix_makedev, METH_VARARGS0x0001, posix_makedev__doc__},
7989#endif
7990#ifdef HAVE_FTRUNCATE1
7991 {"ftruncate", posix_ftruncate, METH_VARARGS0x0001, posix_ftruncate__doc__},
7992#endif
7993#ifdef HAVE_PUTENV1
7994 {"putenv", posix_putenv, METH_VARARGS0x0001, posix_putenv__doc__},
7995#endif
7996#ifdef HAVE_UNSETENV1
7997 {"unsetenv", posix_unsetenv, METH_VARARGS0x0001, posix_unsetenv__doc__},
7998#endif
7999 {"strerror", posix_strerror, METH_VARARGS0x0001, posix_strerror__doc__},
8000#ifdef HAVE_FCHDIR1
8001 {"fchdir", posix_fchdir, METH_O0x0008, posix_fchdir__doc__},
8002#endif
8003#ifdef HAVE_FSYNC1
8004 {"fsync", posix_fsync, METH_O0x0008, posix_fsync__doc__},
8005#endif
8006#ifdef HAVE_FDATASYNC
8007 {"fdatasync", posix_fdatasync, METH_O0x0008, posix_fdatasync__doc__},
8008#endif
8009#ifdef HAVE_SYS_WAIT_H1
8010#ifdef WCOREDUMP
8011 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS0x0001, posix_WCOREDUMP__doc__},
8012#endif /* WCOREDUMP */
8013#ifdef WIFCONTINUED
8014 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS0x0001, posix_WIFCONTINUED__doc__},
8015#endif /* WIFCONTINUED */
8016#ifdef WIFSTOPPED
8017 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS0x0001, posix_WIFSTOPPED__doc__},
8018#endif /* WIFSTOPPED */
8019#ifdef WIFSIGNALED
8020 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS0x0001, posix_WIFSIGNALED__doc__},
8021#endif /* WIFSIGNALED */
8022#ifdef WIFEXITED
8023 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS0x0001, posix_WIFEXITED__doc__},
8024#endif /* WIFEXITED */
8025#ifdef WEXITSTATUS
8026 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS0x0001, posix_WEXITSTATUS__doc__},
8027#endif /* WEXITSTATUS */
8028#ifdef WTERMSIG
8029 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS0x0001, posix_WTERMSIG__doc__},
8030#endif /* WTERMSIG */
8031#ifdef WSTOPSIG
8032 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS0x0001, posix_WSTOPSIG__doc__},
8033#endif /* WSTOPSIG */
8034#endif /* HAVE_SYS_WAIT_H */
8035#if defined(HAVE_FSTATVFS1) && defined(HAVE_SYS_STATVFS_H1)
8036 {"fstatvfs", posix_fstatvfs, METH_VARARGS0x0001, posix_fstatvfs__doc__},
8037#endif
8038#if defined(HAVE_STATVFS1) && defined(HAVE_SYS_STATVFS_H1)
8039 {"statvfs", posix_statvfs, METH_VARARGS0x0001, posix_statvfs__doc__},
8040#endif
8041#ifdef HAVE_CONFSTR1
8042 {"confstr", posix_confstr, METH_VARARGS0x0001, posix_confstr__doc__},
8043#endif
8044#ifdef HAVE_SYSCONF1
8045 {"sysconf", posix_sysconf, METH_VARARGS0x0001, posix_sysconf__doc__},
8046#endif
8047#ifdef HAVE_FPATHCONF1
8048 {"fpathconf", posix_fpathconf, METH_VARARGS0x0001, posix_fpathconf__doc__},
8049#endif
8050#ifdef HAVE_PATHCONF1
8051 {"pathconf", posix_pathconf, METH_VARARGS0x0001, posix_pathconf__doc__},
8052#endif
8053 {"abort", posix_abort, METH_NOARGS0x0004, posix_abort__doc__},
8054#ifdef MS_WINDOWS
8055 {"_getfullpathname", posix__getfullpathname, METH_VARARGS0x0001, NULL((void *)0)},
8056 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS0x0001, NULL((void *)0)},
8057 {"_getfileinformation", posix__getfileinformation, METH_VARARGS0x0001, NULL((void *)0)},
8058#endif
8059#ifdef HAVE_GETLOADAVG1
8060 {"getloadavg", posix_getloadavg, METH_NOARGS0x0004, posix_getloadavg__doc__},
8061#endif
8062 #ifdef MS_WINDOWS
8063 {"urandom", win32_urandom, METH_VARARGS0x0001, win32_urandom__doc__},
8064 #endif
8065 #ifdef __VMS
8066 {"urandom", vms_urandom, METH_VARARGS0x0001, vms_urandom__doc__},
8067 #endif
8068#ifdef HAVE_SETRESUID
8069 {"setresuid", posix_setresuid, METH_VARARGS0x0001, posix_setresuid__doc__},
8070#endif
8071#ifdef HAVE_SETRESGID
8072 {"setresgid", posix_setresgid, METH_VARARGS0x0001, posix_setresgid__doc__},
8073#endif
8074#ifdef HAVE_GETRESUID
8075 {"getresuid", posix_getresuid, METH_NOARGS0x0004, posix_getresuid__doc__},
8076#endif
8077#ifdef HAVE_GETRESGID
8078 {"getresgid", posix_getresgid, METH_NOARGS0x0004, posix_getresgid__doc__},
8079#endif
8080
8081 {NULL((void *)0), NULL((void *)0)} /* Sentinel */
8082};
8083
8084
8085static int
8086ins(PyObject *module, char *symbol, long value)
8087{
8088 return PyModule_AddIntConstant(module, symbol, value);
8089}
8090
8091#if defined(PYOS_OS2)
8092/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
8093static int insertvalues(PyObject *module)
8094{
8095 APIRET rc;
8096 ULONG values[QSV_MAX+1];
8097 PyObject *v;
8098 char *ver, tmp[50];
8099
8100 Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread();
8101 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
8102 Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); }
8103
8104 if (rc != NO_ERROR) {
8105 os2_error(rc);
8106 return -1;
8107 }
8108
8109 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8110 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8111 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8112 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8113 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8114 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8115 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
8116
8117 switch (values[QSV_VERSION_MINOR]) {
8118 case 0: ver = "2.00"; break;
8119 case 10: ver = "2.10"; break;
8120 case 11: ver = "2.11"; break;
8121 case 30: ver = "3.00"; break;
8122 case 40: ver = "4.00"; break;
8123 case 50: ver = "5.00"; break;
8124 default:
8125 PyOS_snprintf(tmp, sizeof(tmp),
8126 "%d-%d", values[QSV_VERSION_MAJOR],
8127 values[QSV_VERSION_MINOR]);
8128 ver = &tmp[0];
8129 }
8130
8131 /* Add Indicator of the Version of the Operating System */
8132 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
8133 return -1;
8134
8135 /* Add Indicator of Which Drive was Used to Boot the System */
8136 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8137 tmp[1] = ':';
8138 tmp[2] = '\0';
8139
8140 return PyModule_AddStringConstant(module, "bootdrive", tmp);
8141}
8142#endif
8143
8144#if defined(HAVE_SYMLINK1) && defined(MS_WINDOWS)
8145static int
8146enable_symlink()
8147{
8148 HANDLE tok;
8149 TOKEN_PRIVILEGES tok_priv;
8150 LUID luid;
8151 int meth_idx = 0;
8152
8153 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
8154 return 0;
8155
8156 if (!LookupPrivilegeValue(NULL((void *)0), SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
8157 return 0;
8158
8159 tok_priv.PrivilegeCount = 1;
8160 tok_priv.Privileges[0].Luid = luid;
8161 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
8162
8163 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
8164 sizeof(TOKEN_PRIVILEGES),
8165 (PTOKEN_PRIVILEGES) NULL((void *)0), (PDWORD) NULL((void *)0)))
8166 return 0;
8167
8168 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
8169 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
8170}
8171#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
8172
8173static int
8174all_ins(PyObject *d)
8175{
8176#ifdef F_OK0
8177 if (ins(d, "F_OK", (long)F_OK0)) return -1;
8178#endif
8179#ifdef R_OK(1<<2)
8180 if (ins(d, "R_OK", (long)R_OK(1<<2))) return -1;
8181#endif
8182#ifdef W_OK(1<<1)
8183 if (ins(d, "W_OK", (long)W_OK(1<<1))) return -1;
8184#endif
8185#ifdef X_OK(1<<0)
8186 if (ins(d, "X_OK", (long)X_OK(1<<0))) return -1;
8187#endif
8188#ifdef NGROUPS_MAX16
8189 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX16)) return -1;
8190#endif
8191#ifdef TMP_MAX308915776
8192 if (ins(d, "TMP_MAX", (long)TMP_MAX308915776)) return -1;
8193#endif
8194#ifdef WCONTINUED0x00000010
8195 if (ins(d, "WCONTINUED", (long)WCONTINUED0x00000010)) return -1;
8196#endif
8197#ifdef WNOHANG0x00000001
8198 if (ins(d, "WNOHANG", (long)WNOHANG0x00000001)) return -1;
8199#endif
8200#ifdef WUNTRACED0x00000002
8201 if (ins(d, "WUNTRACED", (long)WUNTRACED0x00000002)) return -1;
8202#endif
8203#ifdef O_RDONLY0x0000
8204 if (ins(d, "O_RDONLY", (long)O_RDONLY0x0000)) return -1;
8205#endif
8206#ifdef O_WRONLY0x0001
8207 if (ins(d, "O_WRONLY", (long)O_WRONLY0x0001)) return -1;
8208#endif
8209#ifdef O_RDWR0x0002
8210 if (ins(d, "O_RDWR", (long)O_RDWR0x0002)) return -1;
8211#endif
8212#ifdef O_NDELAY0x0004
8213 if (ins(d, "O_NDELAY", (long)O_NDELAY0x0004)) return -1;
8214#endif
8215#ifdef O_NONBLOCK0x0004
8216 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK0x0004)) return -1;
8217#endif
8218#ifdef O_APPEND0x0008
8219 if (ins(d, "O_APPEND", (long)O_APPEND0x0008)) return -1;
8220#endif
8221#ifdef O_DSYNC0x400000
8222 if (ins(d, "O_DSYNC", (long)O_DSYNC0x400000)) return -1;
8223#endif
8224#ifdef O_RSYNC
8225 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8226#endif
8227#ifdef O_SYNC0x0080
8228 if (ins(d, "O_SYNC", (long)O_SYNC0x0080)) return -1;
8229#endif
8230#ifdef O_NOCTTY0x20000
8231 if (ins(d, "O_NOCTTY", (long)O_NOCTTY0x20000)) return -1;
8232#endif
8233#ifdef O_CREAT0x0200
8234 if (ins(d, "O_CREAT", (long)O_CREAT0x0200)) return -1;
8235#endif
8236#ifdef O_EXCL0x0800
8237 if (ins(d, "O_EXCL", (long)O_EXCL0x0800)) return -1;
8238#endif
8239#ifdef O_TRUNC0x0400
8240 if (ins(d, "O_TRUNC", (long)O_TRUNC0x0400)) return -1;
8241#endif
8242#ifdef O_BINARY
8243 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8244#endif
8245#ifdef O_TEXT
8246 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8247#endif
8248#ifdef O_LARGEFILE
8249 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8250#endif
8251#ifdef O_SHLOCK0x0010
8252 if (ins(d, "O_SHLOCK", (long)O_SHLOCK0x0010)) return -1;
8253#endif
8254#ifdef O_EXLOCK0x0020
8255 if (ins(d, "O_EXLOCK", (long)O_EXLOCK0x0020)) return -1;
8256#endif
8257
8258/* MS Windows */
8259#ifdef O_NOINHERIT
8260 /* Don't inherit in child processes. */
8261 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8262#endif
8263#ifdef _O_SHORT_LIVED
8264 /* Optimize for short life (keep in memory). */
8265 /* MS forgot to define this one with a non-underscore form too. */
8266 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8267#endif
8268#ifdef O_TEMPORARY
8269 /* Automatically delete when last handle is closed. */
8270 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8271#endif
8272#ifdef O_RANDOM
8273 /* Optimize for random access. */
8274 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8275#endif
8276#ifdef O_SEQUENTIAL
8277 /* Optimize for sequential access. */
8278 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8279#endif
8280
8281/* GNU extensions. */
8282#ifdef O_ASYNC0x0040
8283 /* Send a SIGIO signal whenever input or output
8284 becomes available on file descriptor */
8285 if (ins(d, "O_ASYNC", (long)O_ASYNC0x0040)) return -1;
8286#endif
8287#ifdef O_DIRECT
8288 /* Direct disk access. */
8289 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8290#endif
8291#ifdef O_DIRECTORY0x100000
8292 /* Must be a directory. */
8293 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY0x100000)) return -1;
8294#endif
8295#ifdef O_NOFOLLOW0x0100
8296 /* Do not follow links. */
8297 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW0x0100)) return -1;
8298#endif
8299#ifdef O_NOATIME
8300 /* Do not update the access time. */
8301 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
8302#endif
8303
8304 /* These come from sysexits.h */
8305#ifdef EX_OK0
8306 if (ins(d, "EX_OK", (long)EX_OK0)) return -1;
8307#endif /* EX_OK */
8308#ifdef EX_USAGE64
8309 if (ins(d, "EX_USAGE", (long)EX_USAGE64)) return -1;
8310#endif /* EX_USAGE */
8311#ifdef EX_DATAERR65
8312 if (ins(d, "EX_DATAERR", (long)EX_DATAERR65)) return -1;
8313#endif /* EX_DATAERR */
8314#ifdef EX_NOINPUT66
8315 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT66)) return -1;
8316#endif /* EX_NOINPUT */
8317#ifdef EX_NOUSER67
8318 if (ins(d, "EX_NOUSER", (long)EX_NOUSER67)) return -1;
8319#endif /* EX_NOUSER */
8320#ifdef EX_NOHOST68
8321 if (ins(d, "EX_NOHOST", (long)EX_NOHOST68)) return -1;
8322#endif /* EX_NOHOST */
8323#ifdef EX_UNAVAILABLE69
8324 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE69)) return -1;
8325#endif /* EX_UNAVAILABLE */
8326#ifdef EX_SOFTWARE70
8327 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE70)) return -1;
8328#endif /* EX_SOFTWARE */
8329#ifdef EX_OSERR71
8330 if (ins(d, "EX_OSERR", (long)EX_OSERR71)) return -1;
8331#endif /* EX_OSERR */
8332#ifdef EX_OSFILE72
8333 if (ins(d, "EX_OSFILE", (long)EX_OSFILE72)) return -1;
8334#endif /* EX_OSFILE */
8335#ifdef EX_CANTCREAT73
8336 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT73)) return -1;
8337#endif /* EX_CANTCREAT */
8338#ifdef EX_IOERR74
8339 if (ins(d, "EX_IOERR", (long)EX_IOERR74)) return -1;
8340#endif /* EX_IOERR */
8341#ifdef EX_TEMPFAIL75
8342 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL75)) return -1;
8343#endif /* EX_TEMPFAIL */
8344#ifdef EX_PROTOCOL76
8345 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL76)) return -1;
8346#endif /* EX_PROTOCOL */
8347#ifdef EX_NOPERM77
8348 if (ins(d, "EX_NOPERM", (long)EX_NOPERM77)) return -1;
8349#endif /* EX_NOPERM */
8350#ifdef EX_CONFIG78
8351 if (ins(d, "EX_CONFIG", (long)EX_CONFIG78)) return -1;
8352#endif /* EX_CONFIG */
8353#ifdef EX_NOTFOUND
8354 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
8355#endif /* EX_NOTFOUND */
8356
8357 /* statvfs */
8358#ifdef ST_RDONLY0x00000001
8359 if (ins(d, "ST_RDONLY", (long)ST_RDONLY0x00000001)) return -1;
8360#endif /* ST_RDONLY */
8361#ifdef ST_NOSUID0x00000002
8362 if (ins(d, "ST_NOSUID", (long)ST_NOSUID0x00000002)) return -1;
8363#endif /* ST_NOSUID */
8364
8365#ifdef HAVE_SPAWNV
8366#if defined(PYOS_OS2) && defined(PYCC_GCC)
8367 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8368 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8369 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8370 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8371 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8372 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8373 if (ins(d, "P_PM", (long)P_PM)) return -1;
8374 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8375 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8376 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8377 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8378 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8379 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8380 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8381 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8382 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8383 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8384 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8385 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8386 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8387#else
8388 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8389 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8390 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8391 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8392 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
8393#endif
8394#endif
8395
8396#if defined(PYOS_OS2)
8397 if (insertvalues(d)) return -1;
8398#endif
8399 return 0;
8400}
8401
8402
8403#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
8404#define INITFUNCPyInit_posix PyInit_nt
8405#define MODNAME"posix" "nt"
8406
8407#elif defined(PYOS_OS2)
8408#define INITFUNCPyInit_posix PyInit_os2
8409#define MODNAME"posix" "os2"
8410
8411#else
8412#define INITFUNCPyInit_posix PyInit_posix
8413#define MODNAME"posix" "posix"
8414#endif
8415
8416static struct PyModuleDef posixmodule = {
8417 PyModuleDef_HEAD_INIT{ { 0, 0, 1, ((void *)0) }, ((void *)0), 0, ((void *)0), },
8418 MODNAME"posix",
8419 posix__doc__,
8420 -1,
8421 posix_methods,
8422 NULL((void *)0),
8423 NULL((void *)0),
8424 NULL((void *)0),
8425 NULL((void *)0)
8426};
8427
8428
8429PyMODINIT_FUNCPyObject*
8430INITFUNCPyInit_posix(void)
8431{
8432 PyObject *m, *v;
8433
8434#if defined(HAVE_SYMLINK1) && defined(MS_WINDOWS)
8435 win32_can_symlink = enable_symlink();
8436#endif
8437
8438 m = PyModule_Create(&posixmodule)PyModule_Create2TraceRefs(&posixmodule, 1013);
8439 if (m == NULL((void *)0))
8440 return NULL((void *)0);
8441
8442 /* Initialize environ dictionary */
8443 v = convertenviron();
8444 Py_XINCREF(v)do { if ((v) == ((void *)0)) ; else ( _Py_RefTotal++ , ((PyObject
*)(v))->ob_refcnt++); } while (0)
;
8445 if (v == NULL((void *)0) || PyModule_AddObject(m, "environ", v) != 0)
8446 return NULL((void *)0);
8447 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/posixmodule.c", 8447, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
8448
8449 if (all_ins(m))
8450 return NULL((void *)0);
8451
8452 if (setup_confname_tables(m))
8453 return NULL((void *)0);
8454
8455 Py_INCREF(PyExc_OSError)( _Py_RefTotal++ , ((PyObject*)(PyExc_OSError))->ob_refcnt
++)
;
8456 PyModule_AddObject(m, "error", PyExc_OSError);
8457
8458#ifdef HAVE_PUTENV1
8459 if (posix_putenv_garbage == NULL((void *)0))
8460 posix_putenv_garbage = PyDict_New();
8461#endif
8462
8463 if (!initialized) {
8464 stat_result_desc.name = MODNAME"posix" ".stat_result";
8465 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8466 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8467 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8468 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8469 structseq_new = StatResultType.tp_new;
8470 StatResultType.tp_new = statresult_new;
8471
8472 statvfs_result_desc.name = MODNAME"posix" ".statvfs_result";
8473 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
8474#ifdef NEED_TICKS_PER_SECOND
8475# if defined(HAVE_SYSCONF1) && defined(_SC_CLK_TCK3)
8476 ticks_per_second = sysconf(_SC_CLK_TCK3);
8477# elif defined(HZ)
8478 ticks_per_second = HZ;
8479# else
8480 ticks_per_second = 60; /* magic fallback value; may be bogus */
8481# endif
8482#endif
8483 }
8484 Py_INCREF((PyObject*) &StatResultType)( _Py_RefTotal++ , ((PyObject*)((PyObject*) &StatResultType
))->ob_refcnt++)
;
8485 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8486 Py_INCREF((PyObject*) &StatVFSResultType)( _Py_RefTotal++ , ((PyObject*)((PyObject*) &StatVFSResultType
))->ob_refcnt++)
;
8487 PyModule_AddObject(m, "statvfs_result",
8488 (PyObject*) &StatVFSResultType);
8489 initialized = 1;
8490
8491#ifdef __APPLE__1
8492 /*
8493 * Step 2 of weak-linking support on Mac OS X.
8494 *
8495 * The code below removes functions that are not available on the
8496 * currently active platform.
8497 *
8498 * This block allow one to use a python binary that was build on
8499 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8500 * OSX 10.4.
8501 */
8502#ifdef HAVE_FSTATVFS1
8503 if (fstatvfs == NULL((void *)0)) {
8504 if (PyObject_DelAttrString(m, "fstatvfs")PyObject_SetAttrString((m),("fstatvfs"),((void *)0)) == -1) {
8505 return NULL((void *)0);
8506 }
8507 }
8508#endif /* HAVE_FSTATVFS */
8509
8510#ifdef HAVE_STATVFS1
8511 if (statvfs == NULL((void *)0)) {
8512 if (PyObject_DelAttrString(m, "statvfs")PyObject_SetAttrString((m),("statvfs"),((void *)0)) == -1) {
8513 return NULL((void *)0);
8514 }
8515 }
8516#endif /* HAVE_STATVFS */
8517
8518# ifdef HAVE_LCHOWN1
8519 if (lchown == NULL((void *)0)) {
8520 if (PyObject_DelAttrString(m, "lchown")PyObject_SetAttrString((m),("lchown"),((void *)0)) == -1) {
8521 return NULL((void *)0);
8522 }
8523 }
8524#endif /* HAVE_LCHOWN */
8525
8526
8527#endif /* __APPLE__ */
8528 return m;
8529
8530}
8531
8532#ifdef __cplusplus
8533}
8534#endif