2 /* POSIX module implementation */
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. */
14 /* See also ../Dos/dosmodule.c */
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.
24 # pragma weak fstatvfs
26 #endif /* __APPLE__ */
28 #define PY_SSIZE_T_CLEAN
31 #include "structseq.h"
35 #endif /* defined(__VMS) */
41 PyDoc_STRVAR(posix__doc__
,
42 "This module provides access to operating system functionality that is\n\
43 standardized by the C Standard and the POSIX standard (a thinly\n\
44 disguised Unix interface). Refer to the library manual and\n\
45 corresponding Unix manual entries for more information on calls.");
50 #define INCL_DOSERRORS
51 #define INCL_DOSPROCESS
63 #ifdef HAVE_SYS_TYPES_H
64 #include <sys/types.h>
65 #endif /* HAVE_SYS_TYPES_H */
67 #ifdef HAVE_SYS_STAT_H
69 #endif /* HAVE_SYS_STAT_H */
71 #ifdef HAVE_SYS_WAIT_H
72 #include <sys/wait.h> /* For WNOHANG */
81 #endif /* HAVE_FCNTL_H */
87 #ifdef HAVE_SYSEXITS_H
89 #endif /* HAVE_SYSEXITS_H */
91 #ifdef HAVE_SYS_LOADAVG_H
92 #include <sys/loadavg.h>
95 #ifdef HAVE_LANGINFO_H
99 /* Various compilers have only certain posix functions */
100 /* XXX Gosh I wish these were all moved into pyconfig.h */
101 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
104 #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
105 #define HAVE_GETCWD 1
106 #define HAVE_OPENDIR 1
107 #define HAVE_SYSTEM 1
114 #ifdef __BORLANDC__ /* Borland compiler */
116 #define HAVE_GETCWD 1
117 #define HAVE_OPENDIR 1
119 #define HAVE_SYSTEM 1
122 #ifdef _MSC_VER /* Microsoft compiler */
123 #define HAVE_GETCWD 1
124 #define HAVE_SPAWNV 1
127 #define HAVE_SYSTEM 1
130 #define fsync _commit
132 #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
133 /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
134 #else /* all other compilers */
135 /* Unix functions that the configure script doesn't check for */
138 #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
141 #define HAVE_GETCWD 1
142 #define HAVE_GETEGID 1
143 #define HAVE_GETEUID 1
144 #define HAVE_GETGID 1
145 #define HAVE_GETPPID 1
146 #define HAVE_GETUID 1
148 #define HAVE_OPENDIR 1
150 #define HAVE_SYSTEM 1
152 #define HAVE_TTYNAME 1
153 #endif /* PYOS_OS2 && PYCC_GCC && __VMS */
154 #endif /* _MSC_VER */
155 #endif /* __BORLANDC__ */
156 #endif /* ! __WATCOMC__ || __QNX__ */
157 #endif /* ! __IBMC__ */
161 #if defined(__sgi)&&_COMPILER_VERSION>=700
162 /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
164 extern char *ctermid_r(char *);
167 #ifndef HAVE_UNISTD_H
168 #if defined(PYCC_VACPP)
169 extern int mkdir(char *);
171 #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
172 extern int mkdir(const char *);
174 extern int mkdir(const char *, mode_t
);
177 #if defined(__IBMC__) || defined(__IBMCPP__)
178 extern int chdir(char *);
179 extern int rmdir(char *);
181 extern int chdir(const char *);
182 extern int rmdir(const char *);
185 extern int chmod(const char *, int);
187 extern int chmod(const char *, mode_t
);
190 extern int fchmod(int, mode_t);
193 extern int lchmod(const char *, mode_t);
195 extern int chown(const char *, uid_t
, gid_t
);
196 extern char *getcwd(char *, int);
197 extern char *strerror(int);
198 extern int link(const char *, const char *);
199 extern int rename(const char *, const char *);
200 extern int stat(const char *, struct stat
*);
201 extern int unlink(const char *);
203 extern int symlink(const char *, const char *);
204 #endif /* HAVE_SYMLINK */
206 extern int lstat(const char *, struct stat
*);
207 #endif /* HAVE_LSTAT */
208 #endif /* !HAVE_UNISTD_H */
210 #endif /* !_MSC_VER */
214 #endif /* HAVE_UTIME_H */
216 #ifdef HAVE_SYS_UTIME_H
217 #include <sys/utime.h>
218 #define HAVE_UTIME_H /* pretend we do for the rest of this file */
219 #endif /* HAVE_SYS_UTIME_H */
221 #ifdef HAVE_SYS_TIMES_H
222 #include <sys/times.h>
223 #endif /* HAVE_SYS_TIMES_H */
225 #ifdef HAVE_SYS_PARAM_H
226 #include <sys/param.h>
227 #endif /* HAVE_SYS_PARAM_H */
229 #ifdef HAVE_SYS_UTSNAME_H
230 #include <sys/utsname.h>
231 #endif /* HAVE_SYS_UTSNAME_H */
235 #define NAMLEN(dirent) strlen((dirent)->d_name)
237 #if defined(__WATCOMC__) && !defined(__QNX__)
239 #define NAMLEN(dirent) strlen((dirent)->d_name)
241 #define dirent direct
242 #define NAMLEN(dirent) (dirent)->d_namlen
244 #ifdef HAVE_SYS_NDIR_H
245 #include <sys/ndir.h>
247 #ifdef HAVE_SYS_DIR_H
262 #ifdef HAVE_PROCESS_H
268 #include <shellapi.h> /* for ShellExecute() */
269 #endif /* _MSC_VER */
271 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
276 #if defined(PATH_MAX) && PATH_MAX > 1024
277 #define MAXPATHLEN PATH_MAX
279 #define MAXPATHLEN 1024
281 #endif /* MAXPATHLEN */
284 /* Emulate some macros on systems that have a union instead of macros */
287 #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
291 #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
295 #define WTERMSIG(u_wait) ((u_wait).w_termsig)
298 #define WAIT_TYPE union wait
299 #define WAIT_STATUS_INT(s) (s.w_status)
301 #else /* !UNION_WAIT */
302 #define WAIT_TYPE int
303 #define WAIT_STATUS_INT(s) (s)
304 #endif /* UNION_WAIT */
306 /* Issue #1983: pid_t can be longer than a C long on some systems */
307 #if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
308 #define PARSE_PID "i"
309 #define PyLong_FromPid PyLong_FromLong
310 #define PyLong_AsPid PyLong_AsLong
311 #elif SIZEOF_PID_T == SIZEOF_LONG
312 #define PARSE_PID "l"
313 #define PyLong_FromPid PyLong_FromLong
314 #define PyLong_AsPid PyLong_AsLong
315 #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
316 #define PARSE_PID "L"
317 #define PyLong_FromPid PyLong_FromLongLong
318 #define PyLong_AsPid PyLong_AsLongLong
320 #error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
321 #endif /* SIZEOF_PID_T */
323 /* Don't use the "_r" form if we don't need it (also, won't have a
324 prototype for it, at least on Solaris -- maybe others as well?). */
325 #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
326 #define USE_CTERMID_R
329 /* choose the appropriate stat and fstat functions and return structs */
331 #if defined(MS_WIN64) || defined(MS_WINDOWS)
332 # define STAT win32_stat
333 # define FSTAT win32_fstat
334 # define STRUCT_STAT struct win32_stat
338 # define STRUCT_STAT struct stat
341 #if defined(MAJOR_IN_MKDEV)
342 #include <sys/mkdev.h>
344 #if defined(MAJOR_IN_SYSMACROS)
345 #include <sys/sysmacros.h>
347 #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
348 #include <sys/mkdev.h>
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
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())
373 /* The actual size of the structure is determined at runtime.
374 * Only the first items must be present.
381 extern __declspec(dllimport
) char * __pioinfo
[];
383 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
384 #define IOINFO_ARRAYS 64
385 #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
387 #define _NO_CONSOLE_FILENO (intptr_t)-2
389 /* This function emulates what the windows CRT does to validate file handles */
393 const int i1
= fd
>> IOINFO_L2E
;
394 const int i2
= fd
& ((1 << IOINFO_L2E
) - 1);
396 static int sizeof_ioinfo
= 0;
398 /* Determine the actual size of the ioinfo structure,
399 * as used by the CRT loaded in memory
401 if (sizeof_ioinfo
== 0 && __pioinfo
[0] != NULL
) {
402 sizeof_ioinfo
= _msize(__pioinfo
[0]) / IOINFO_ARRAY_ELTS
;
404 if (sizeof_ioinfo
== 0) {
405 /* This should not happen... */
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
414 if (0 <= i1
&& i1
< IOINFO_ARRAYS
&& __pioinfo
[i1
] != NULL
) {
415 /* finally, check that the file is open */
416 my_ioinfo
* info
= (my_ioinfo
*)(__pioinfo
[i1
] + i2
* sizeof_ioinfo
);
417 if (info
->osfile
& FOPEN
) {
427 /* the special case of checking dup2. The target fd must be in a sensible range */
429 _PyVerify_fd_dup2(int fd1
, int fd2
)
431 if (!_PyVerify_fd(fd1
))
433 if (fd2
== _NO_CONSOLE_FILENO
)
435 if ((unsigned)fd2
< _NHANDLE_
)
441 /* dummy version. _PyVerify_fd() is already defined in fileobject.h */
442 #define _PyVerify_fd_dup2(A, B) (1)
445 /* Return a dictionary corresponding to the POSIX environment table */
446 #ifdef WITH_NEXT_FRAMEWORK
447 /* On Darwin/MacOSX a shared library or framework has no access to
448 ** environ directly, we must obtain it with _NSGetEnviron().
450 #include <crt_externs.h>
451 static char **environ
;
452 #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
453 extern char **environ
;
454 #endif /* !_MSC_VER */
468 #ifdef WITH_NEXT_FRAMEWORK
470 environ
= *_NSGetEnviron();
473 /* _wenviron must be initialized in this way if the program is started
474 through main() instead of wmain(). */
476 if (_wenviron
== NULL
)
478 /* This part ignores errors */
479 for (e
= _wenviron
; *e
!= NULL
; e
++) {
482 wchar_t *p
= wcschr(*e
, L
'=');
485 k
= PyUnicode_FromWideChar(*e
, (Py_ssize_t
)(p
-*e
));
490 v
= PyUnicode_FromWideChar(p
+1, wcslen(p
+1));
496 if (PyDict_GetItem(d
, k
) == NULL
) {
497 if (PyDict_SetItem(d
, k
, v
) != 0)
506 /* This part ignores errors */
507 for (e
= environ
; *e
!= NULL
; e
++) {
510 char *p
= strchr(*e
, '=');
513 k
= PyUnicode_Decode(*e
, (int)(p
-*e
),
514 Py_FileSystemDefaultEncoding
, "surrogateescape");
519 v
= PyUnicode_Decode(p
+1, strlen(p
+1),
520 Py_FileSystemDefaultEncoding
, "surrogateescape");
526 if (PyDict_GetItem(d
, k
) == NULL
) {
527 if (PyDict_SetItem(d
, k
, v
) != 0)
534 #if defined(PYOS_OS2)
537 char buffer
[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
539 rc
= DosQueryExtLIBPATH(buffer
, BEGIN_LIBPATH
);
540 if (rc
== NO_ERROR
) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
541 PyObject
*v
= PyBytes_FromString(buffer
);
542 PyDict_SetItemString(d
, "BEGINLIBPATH", v
);
545 rc
= DosQueryExtLIBPATH(buffer
, END_LIBPATH
);
546 if (rc
== NO_ERROR
) { /* (not a typo, envname is NOT 'END_LIBPATH') */
547 PyObject
*v
= PyBytes_FromString(buffer
);
548 PyDict_SetItemString(d
, "ENDLIBPATH", v
);
556 /* Convert a bytes object to a char*. Optionally lock the buffer if it is a
560 bytes2str(PyObject
* o
, int lock
)
563 return PyBytes_AsString(o
);
564 else if(PyByteArray_Check(o
)) {
565 if (lock
&& PyObject_GetBuffer(o
, NULL
, 0) < 0)
566 /* On a bytearray, this should not fail. */
567 PyErr_BadInternalCall();
568 return PyByteArray_AsString(o
);
570 /* The FS converter should have verified that this
571 is either bytes or bytearray. */
572 Py_FatalError("bad object passed to bytes2str");
578 /* Release the lock, decref the object. */
580 release_bytes(PyObject
* o
)
582 if (PyByteArray_Check(o
))
583 o
->ob_type
->tp_as_buffer
->bf_releasebuffer(o
, 0);
588 /* Set a POSIX-specific error from errno, and return NULL */
593 return PyErr_SetFromErrno(PyExc_OSError
);
596 posix_error_with_filename(char* name
)
598 return PyErr_SetFromErrnoWithFilename(PyExc_OSError
, name
);
603 posix_error_with_unicode_filename(Py_UNICODE
* name
)
605 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError
, name
);
607 #endif /* MS_WINDOWS */
611 posix_error_with_allocated_filename(PyObject
* name
)
613 PyObject
*rc
= PyErr_SetFromErrnoWithFilename(PyExc_OSError
,
621 win32_error(char* function
, char* filename
)
623 /* XXX We should pass the function name along in the future.
624 (winreg.c also wants to pass the function name.)
625 This would however require an additional param to the
626 Windows error object, which is non-trivial.
628 errno
= GetLastError();
630 return PyErr_SetFromWindowsErrWithFilename(errno
, filename
);
632 return PyErr_SetFromWindowsErr(errno
);
636 win32_error_unicode(char* function
, Py_UNICODE
* filename
)
638 /* XXX - see win32_error for comments on 'function' */
639 errno
= GetLastError();
641 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno
, filename
);
643 return PyErr_SetFromWindowsErr(errno
);
647 convert_to_unicode(PyObject
**param
)
649 if (PyUnicode_CheckExact(*param
))
651 else if (PyUnicode_Check(*param
))
652 /* For a Unicode subtype that's not a Unicode object,
653 return a true Unicode object with the same data. */
654 *param
= PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param
),
655 PyUnicode_GET_SIZE(*param
));
657 *param
= PyUnicode_FromEncodedObject(*param
,
658 Py_FileSystemDefaultEncoding
,
660 return (*param
) != NULL
;
663 #endif /* MS_WINDOWS */
665 #if defined(PYOS_OS2)
666 /**********************************************************************
667 * Helper Function to Trim and Format OS/2 Messages
668 **********************************************************************/
670 os2_formatmsg(char *msgbuf
, int msglen
, char *reason
)
672 msgbuf
[msglen
] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
674 if (strlen(msgbuf
) > 0) { /* If Non-Empty Msg, Trim CRLF */
675 char *lastc
= &msgbuf
[ strlen(msgbuf
)-1 ];
677 while (lastc
> msgbuf
&& isspace(Py_CHARMASK(*lastc
)))
678 *lastc
-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
681 /* Add Optional Reason Text */
683 strcat(msgbuf
, " : ");
684 strcat(msgbuf
, reason
);
688 /**********************************************************************
689 * Decode an OS/2 Operating System Error Code
691 * A convenience function to lookup an OS/2 error code and return a
692 * text message we can use to raise a Python exception.
695 * The messages for errors returned from the OS/2 kernel reside in
696 * the file OSO001.MSG in the \OS2 directory hierarchy.
698 **********************************************************************/
700 os2_strerror(char *msgbuf
, int msgbuflen
, int errorcode
, char *reason
)
705 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
706 Py_BEGIN_ALLOW_THREADS
707 rc
= DosGetMessage(NULL
, 0, msgbuf
, msgbuflen
,
708 errorcode
, "oso001.msg", &msglen
);
712 os2_formatmsg(msgbuf
, msglen
, reason
);
714 PyOS_snprintf(msgbuf
, msgbuflen
,
715 "unknown OS error #%d", errorcode
);
720 /* Set an OS/2-specific error and return NULL. OS/2 kernel
721 errors are not in a global variable e.g. 'errno' nor are
722 they congruent with posix error numbers. */
724 static PyObject
* os2_error(int code
)
729 os2_strerror(text
, sizeof(text
), code
, "");
731 v
= Py_BuildValue("(is)", code
, text
);
733 PyErr_SetObject(PyExc_OSError
, v
);
736 return NULL
; /* Signal to Python that an Exception is Pending */
741 /* POSIX generic methods */
744 posix_fildes(PyObject
*fdobj
, int (*func
)(int))
748 fd
= PyObject_AsFileDescriptor(fdobj
);
751 if (!_PyVerify_fd(fd
))
752 return posix_error();
753 Py_BEGIN_ALLOW_THREADS
757 return posix_error();
764 unicode_file_names(void)
766 static int canusewide
= -1;
767 if (canusewide
== -1) {
768 /* As per doc for ::GetVersion(), this is the correct test for
769 the Windows NT family. */
770 canusewide
= (GetVersion() < 0x80000000) ? 1 : 0;
777 posix_1str(PyObject
*args
, char *format
, int (*func
)(const char*))
779 PyObject
*opath1
= NULL
;
782 if (!PyArg_ParseTuple(args
, format
,
783 PyUnicode_FSConverter
, &opath1
))
785 path1
= bytes2str(opath1
, 1);
786 Py_BEGIN_ALLOW_THREADS
787 res
= (*func
)(path1
);
790 return posix_error_with_allocated_filename(opath1
);
791 release_bytes(opath1
);
797 posix_2str(PyObject
*args
,
799 int (*func
)(const char *, const char *))
801 PyObject
*opath1
= NULL
, *opath2
= NULL
;
804 if (!PyArg_ParseTuple(args
, format
,
805 PyUnicode_FSConverter
, &opath1
,
806 PyUnicode_FSConverter
, &opath2
)) {
809 path1
= bytes2str(opath1
, 1);
810 path2
= bytes2str(opath2
, 1);
811 Py_BEGIN_ALLOW_THREADS
812 res
= (*func
)(path1
, path2
);
814 release_bytes(opath1
);
815 release_bytes(opath2
);
817 /* XXX how to report both path1 and path2??? */
818 return posix_error();
825 win32_1str(PyObject
* args
, char* func
,
826 char* format
, BOOL (__stdcall
*funcA
)(LPCSTR
),
827 char* wformat
, BOOL (__stdcall
*funcW
)(LPWSTR
))
832 if (unicode_file_names()) {
833 if (!PyArg_ParseTuple(args
, wformat
, &uni
))
836 Py_BEGIN_ALLOW_THREADS
837 result
= funcW(PyUnicode_AsUnicode(uni
));
840 return win32_error_unicode(func
, PyUnicode_AsUnicode(uni
));
845 if (!PyArg_ParseTuple(args
, format
, &ansi
))
847 Py_BEGIN_ALLOW_THREADS
848 result
= funcA(ansi
);
851 return win32_error(func
, ansi
);
857 /* This is a reimplementation of the C library's chdir function,
858 but one that produces Win32 errors instead of DOS error codes.
859 chdir is essentially a wrapper around SetCurrentDirectory; however,
860 it also needs to set "magic" environment variables indicating
861 the per-drive current directory, which are of the form =<drive>: */
862 static BOOL __stdcall
863 win32_chdir(LPCSTR path
)
865 char new_path
[MAX_PATH
+1];
869 if(!SetCurrentDirectoryA(path
))
871 result
= GetCurrentDirectoryA(MAX_PATH
+1, new_path
);
874 /* In the ANSI API, there should not be any paths longer
876 assert(result
<= MAX_PATH
+1);
877 if (strncmp(new_path
, "\\\\", 2) == 0 ||
878 strncmp(new_path
, "//", 2) == 0)
879 /* UNC path, nothing to do. */
881 env
[1] = new_path
[0];
882 return SetEnvironmentVariableA(env
, new_path
);
885 /* The Unicode version differs from the ANSI version
886 since the current directory might exceed MAX_PATH characters */
887 static BOOL __stdcall
888 win32_wchdir(LPCWSTR path
)
890 wchar_t _new_path
[MAX_PATH
+1], *new_path
= _new_path
;
892 wchar_t env
[4] = L
"=x:";
894 if(!SetCurrentDirectoryW(path
))
896 result
= GetCurrentDirectoryW(MAX_PATH
+1, new_path
);
899 if (result
> MAX_PATH
+1) {
900 new_path
= malloc(result
* sizeof(wchar_t));
902 SetLastError(ERROR_OUTOFMEMORY
);
905 result
= GetCurrentDirectoryW(result
, new_path
);
911 if (wcsncmp(new_path
, L
"\\\\", 2) == 0 ||
912 wcsncmp(new_path
, L
"//", 2) == 0)
913 /* UNC path, nothing to do. */
915 env
[1] = new_path
[0];
916 result
= SetEnvironmentVariableW(env
, new_path
);
917 if (new_path
!= _new_path
)
924 /* The CRT of Windows has a number of flaws wrt. its stat() implementation:
925 - time stamps are restricted to second resolution
926 - file modification times suffer from forth-and-back conversions between
928 Therefore, we implement our own stat, based on the Win32 API directly.
930 #define HAVE_STAT_NSEC 1
935 unsigned short st_mode
;
949 static __int64 secs_between_epochs
= 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
952 FILE_TIME_to_time_t_nsec(FILETIME
*in_ptr
, int *time_out
, int* nsec_out
)
954 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
955 /* Cannot simply cast and dereference in_ptr,
956 since it might not be aligned properly */
958 memcpy(&in
, in_ptr
, sizeof(in
));
959 *nsec_out
= (int)(in
% 10000000) * 100; /* FILETIME is in units of 100 nsec. */
960 /* XXX Win32 supports time stamps past 2038; we currently don't */
961 *time_out
= Py_SAFE_DOWNCAST((in
/ 10000000) - secs_between_epochs
, __int64
, int);
965 time_t_to_FILE_TIME(int time_in
, int nsec_in
, FILETIME
*out_ptr
)
969 out
= time_in
+ secs_between_epochs
;
970 out
= out
* 10000000 + nsec_in
/ 100;
971 memcpy(out_ptr
, &out
, sizeof(out
));
974 /* Below, we *know* that ugo+r is 0444 */
976 #error Unsupported C library
979 attributes_to_mode(DWORD attr
)
982 if (attr
& FILE_ATTRIBUTE_DIRECTORY
)
983 m
|= _S_IFDIR
| 0111; /* IFEXEC for user,group,other */
986 if (attr
& FILE_ATTRIBUTE_READONLY
)
994 attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA
*info
, struct win32_stat
*result
)
996 memset(result
, 0, sizeof(*result
));
997 result
->st_mode
= attributes_to_mode(info
->dwFileAttributes
);
998 result
->st_size
= (((__int64
)info
->nFileSizeHigh
)<<32) + info
->nFileSizeLow
;
999 FILE_TIME_to_time_t_nsec(&info
->ftCreationTime
, &result
->st_ctime
, &result
->st_ctime_nsec
);
1000 FILE_TIME_to_time_t_nsec(&info
->ftLastWriteTime
, &result
->st_mtime
, &result
->st_mtime_nsec
);
1001 FILE_TIME_to_time_t_nsec(&info
->ftLastAccessTime
, &result
->st_atime
, &result
->st_atime_nsec
);
1006 /* Emulate GetFileAttributesEx[AW] on Windows 95 */
1007 static int checked
= 0;
1008 static BOOL (CALLBACK
*gfaxa
)(LPCSTR
, GET_FILEEX_INFO_LEVELS
, LPVOID
);
1009 static BOOL (CALLBACK
*gfaxw
)(LPCWSTR
, GET_FILEEX_INFO_LEVELS
, LPVOID
);
1013 HINSTANCE hKernel32
;
1017 hKernel32
= GetModuleHandle("KERNEL32");
1018 *(FARPROC
*)&gfaxa
= GetProcAddress(hKernel32
, "GetFileAttributesExA");
1019 *(FARPROC
*)&gfaxw
= GetProcAddress(hKernel32
, "GetFileAttributesExW");
1023 attributes_from_dir(LPCSTR pszFile
, LPWIN32_FILE_ATTRIBUTE_DATA pfad
)
1026 WIN32_FIND_DATAA FileData
;
1027 hFindFile
= FindFirstFileA(pszFile
, &FileData
);
1028 if (hFindFile
== INVALID_HANDLE_VALUE
)
1030 FindClose(hFindFile
);
1031 pfad
->dwFileAttributes
= FileData
.dwFileAttributes
;
1032 pfad
->ftCreationTime
= FileData
.ftCreationTime
;
1033 pfad
->ftLastAccessTime
= FileData
.ftLastAccessTime
;
1034 pfad
->ftLastWriteTime
= FileData
.ftLastWriteTime
;
1035 pfad
->nFileSizeHigh
= FileData
.nFileSizeHigh
;
1036 pfad
->nFileSizeLow
= FileData
.nFileSizeLow
;
1041 attributes_from_dir_w(LPCWSTR pszFile
, LPWIN32_FILE_ATTRIBUTE_DATA pfad
)
1044 WIN32_FIND_DATAW FileData
;
1045 hFindFile
= FindFirstFileW(pszFile
, &FileData
);
1046 if (hFindFile
== INVALID_HANDLE_VALUE
)
1048 FindClose(hFindFile
);
1049 pfad
->dwFileAttributes
= FileData
.dwFileAttributes
;
1050 pfad
->ftCreationTime
= FileData
.ftCreationTime
;
1051 pfad
->ftLastAccessTime
= FileData
.ftLastAccessTime
;
1052 pfad
->ftLastWriteTime
= FileData
.ftLastWriteTime
;
1053 pfad
->nFileSizeHigh
= FileData
.nFileSizeHigh
;
1054 pfad
->nFileSizeLow
= FileData
.nFileSizeLow
;
1059 Py_GetFileAttributesExA(LPCSTR pszFile
,
1060 GET_FILEEX_INFO_LEVELS level
,
1064 LPWIN32_FILE_ATTRIBUTE_DATA pfad
= pv
;
1065 /* First try to use the system's implementation, if that is
1066 available and either succeeds to gives an error other than
1067 that it isn't implemented. */
1070 result
= gfaxa(pszFile
, level
, pv
);
1071 if (result
|| GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
1074 /* It's either not present, or not implemented.
1075 Emulate using FindFirstFile. */
1076 if (level
!= GetFileExInfoStandard
) {
1077 SetLastError(ERROR_INVALID_PARAMETER
);
1080 /* Use GetFileAttributes to validate that the file name
1081 does not contain wildcards (which FindFirstFile would
1083 if (GetFileAttributesA(pszFile
) == 0xFFFFFFFF)
1085 return attributes_from_dir(pszFile
, pfad
);
1089 Py_GetFileAttributesExW(LPCWSTR pszFile
,
1090 GET_FILEEX_INFO_LEVELS level
,
1094 LPWIN32_FILE_ATTRIBUTE_DATA pfad
= pv
;
1095 /* First try to use the system's implementation, if that is
1096 available and either succeeds to gives an error other than
1097 that it isn't implemented. */
1100 result
= gfaxw(pszFile
, level
, pv
);
1101 if (result
|| GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
1104 /* It's either not present, or not implemented.
1105 Emulate using FindFirstFile. */
1106 if (level
!= GetFileExInfoStandard
) {
1107 SetLastError(ERROR_INVALID_PARAMETER
);
1110 /* Use GetFileAttributes to validate that the file name
1111 does not contain wildcards (which FindFirstFile would
1113 if (GetFileAttributesW(pszFile
) == 0xFFFFFFFF)
1115 return attributes_from_dir_w(pszFile
, pfad
);
1119 win32_stat(const char* path
, struct win32_stat
*result
)
1121 WIN32_FILE_ATTRIBUTE_DATA info
;
1124 /* XXX not supported on Win95 and NT 3.x */
1125 if (!Py_GetFileAttributesExA(path
, GetFileExInfoStandard
, &info
)) {
1126 if (GetLastError() != ERROR_SHARING_VIOLATION
) {
1127 /* Protocol violation: we explicitly clear errno, instead of
1128 setting it to a POSIX error. Callers should use GetLastError. */
1132 /* Could not get attributes on open file. Fall back to
1133 reading the directory. */
1134 if (!attributes_from_dir(path
, &info
)) {
1135 /* Very strange. This should not fail now */
1141 code
= attribute_data_to_stat(&info
, result
);
1144 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1145 dot
= strrchr(path
, '.');
1147 if (stricmp(dot
, ".bat") == 0 ||
1148 stricmp(dot
, ".cmd") == 0 ||
1149 stricmp(dot
, ".exe") == 0 ||
1150 stricmp(dot
, ".com") == 0)
1151 result
->st_mode
|= 0111;
1157 win32_wstat(const wchar_t* path
, struct win32_stat
*result
)
1161 WIN32_FILE_ATTRIBUTE_DATA info
;
1162 /* XXX not supported on Win95 and NT 3.x */
1163 if (!Py_GetFileAttributesExW(path
, GetFileExInfoStandard
, &info
)) {
1164 if (GetLastError() != ERROR_SHARING_VIOLATION
) {
1165 /* Protocol violation: we explicitly clear errno, instead of
1166 setting it to a POSIX error. Callers should use GetLastError. */
1170 /* Could not get attributes on open file. Fall back to
1171 reading the directory. */
1172 if (!attributes_from_dir_w(path
, &info
)) {
1173 /* Very strange. This should not fail now */
1179 code
= attribute_data_to_stat(&info
, result
);
1182 /* Set IFEXEC if it is an .exe, .bat, ... */
1183 dot
= wcsrchr(path
, '.');
1185 if (_wcsicmp(dot
, L
".bat") == 0 ||
1186 _wcsicmp(dot
, L
".cmd") == 0 ||
1187 _wcsicmp(dot
, L
".exe") == 0 ||
1188 _wcsicmp(dot
, L
".com") == 0)
1189 result
->st_mode
|= 0111;
1195 win32_fstat(int file_number
, struct win32_stat
*result
)
1197 BY_HANDLE_FILE_INFORMATION info
;
1201 h
= (HANDLE
)_get_osfhandle(file_number
);
1203 /* Protocol violation: we explicitly clear errno, instead of
1204 setting it to a POSIX error. Callers should use GetLastError. */
1207 if (h
== INVALID_HANDLE_VALUE
) {
1208 /* This is really a C library error (invalid file handle).
1209 We set the Win32 error to the closes one matching. */
1210 SetLastError(ERROR_INVALID_HANDLE
);
1213 memset(result
, 0, sizeof(*result
));
1215 type
= GetFileType(h
);
1216 if (type
== FILE_TYPE_UNKNOWN
) {
1217 DWORD error
= GetLastError();
1221 /* else: valid but unknown file */
1224 if (type
!= FILE_TYPE_DISK
) {
1225 if (type
== FILE_TYPE_CHAR
)
1226 result
->st_mode
= _S_IFCHR
;
1227 else if (type
== FILE_TYPE_PIPE
)
1228 result
->st_mode
= _S_IFIFO
;
1232 if (!GetFileInformationByHandle(h
, &info
)) {
1236 /* similar to stat() */
1237 result
->st_mode
= attributes_to_mode(info
.dwFileAttributes
);
1238 result
->st_size
= (((__int64
)info
.nFileSizeHigh
)<<32) + info
.nFileSizeLow
;
1239 FILE_TIME_to_time_t_nsec(&info
.ftCreationTime
, &result
->st_ctime
, &result
->st_ctime_nsec
);
1240 FILE_TIME_to_time_t_nsec(&info
.ftLastWriteTime
, &result
->st_mtime
, &result
->st_mtime_nsec
);
1241 FILE_TIME_to_time_t_nsec(&info
.ftLastAccessTime
, &result
->st_atime
, &result
->st_atime_nsec
);
1242 /* specific to fstat() */
1243 result
->st_nlink
= info
.nNumberOfLinks
;
1244 result
->st_ino
= (((__int64
)info
.nFileIndexHigh
)<<32) + info
.nFileIndexLow
;
1248 #endif /* MS_WINDOWS */
1250 PyDoc_STRVAR(stat_result__doc__
,
1251 "stat_result: Result from stat or lstat.\n\n\
1252 This object may be accessed either as a tuple of\n\
1253 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
1254 or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1256 Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1257 or st_flags, they are available as attributes only.\n\
1259 See os.stat for more information.");
1261 static PyStructSequence_Field stat_result_fields
[] = {
1262 {"st_mode", "protection bits"},
1263 {"st_ino", "inode"},
1264 {"st_dev", "device"},
1265 {"st_nlink", "number of hard links"},
1266 {"st_uid", "user ID of owner"},
1267 {"st_gid", "group ID of owner"},
1268 {"st_size", "total size, in bytes"},
1269 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1270 {NULL
, "integer time of last access"},
1271 {NULL
, "integer time of last modification"},
1272 {NULL
, "integer time of last change"},
1273 {"st_atime", "time of last access"},
1274 {"st_mtime", "time of last modification"},
1275 {"st_ctime", "time of last change"},
1276 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1277 {"st_blksize", "blocksize for filesystem I/O"},
1279 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1280 {"st_blocks", "number of blocks allocated"},
1282 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1283 {"st_rdev", "device type (if inode device)"},
1285 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
1286 {"st_flags", "user defined flags for file"},
1288 #ifdef HAVE_STRUCT_STAT_ST_GEN
1289 {"st_gen", "generation number"},
1291 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1292 {"st_birthtime", "time of creation"},
1297 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1298 #define ST_BLKSIZE_IDX 13
1300 #define ST_BLKSIZE_IDX 12
1303 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1304 #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1306 #define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1309 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1310 #define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1312 #define ST_RDEV_IDX ST_BLOCKS_IDX
1315 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
1316 #define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1318 #define ST_FLAGS_IDX ST_RDEV_IDX
1321 #ifdef HAVE_STRUCT_STAT_ST_GEN
1322 #define ST_GEN_IDX (ST_FLAGS_IDX+1)
1324 #define ST_GEN_IDX ST_FLAGS_IDX
1327 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1328 #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1330 #define ST_BIRTHTIME_IDX ST_GEN_IDX
1333 static PyStructSequence_Desc stat_result_desc
= {
1334 "stat_result", /* name */
1335 stat_result__doc__
, /* doc */
1340 PyDoc_STRVAR(statvfs_result__doc__
,
1341 "statvfs_result: Result from statvfs or fstatvfs.\n\n\
1342 This object may be accessed either as a tuple of\n\
1343 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
1344 or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
1346 See os.statvfs for more information.");
1348 static PyStructSequence_Field statvfs_result_fields
[] = {
1362 static PyStructSequence_Desc statvfs_result_desc
= {
1363 "statvfs_result", /* name */
1364 statvfs_result__doc__
, /* doc */
1365 statvfs_result_fields
,
1369 static int initialized
;
1370 static PyTypeObject StatResultType
;
1371 static PyTypeObject StatVFSResultType
;
1372 static newfunc structseq_new
;
1375 statresult_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1377 PyStructSequence
*result
;
1380 result
= (PyStructSequence
*)structseq_new(type
, args
, kwds
);
1383 /* If we have been initialized from a tuple,
1384 st_?time might be set to None. Initialize it
1385 from the int slots. */
1386 for (i
= 7; i
<= 9; i
++) {
1387 if (result
->ob_item
[i
+3] == Py_None
) {
1389 Py_INCREF(result
->ob_item
[i
]);
1390 result
->ob_item
[i
+3] = result
->ob_item
[i
];
1393 return (PyObject
*)result
;
1398 /* If true, st_?time is float. */
1399 static int _stat_float_times
= 1;
1401 PyDoc_STRVAR(stat_float_times__doc__
,
1402 "stat_float_times([newval]) -> oldval\n\n\
1403 Determine whether os.[lf]stat represents time stamps as float objects.\n\
1404 If newval is True, future calls to stat() return floats, if it is False,\n\
1405 future calls return ints. \n\
1406 If newval is omitted, return the current setting.\n");
1409 stat_float_times(PyObject
* self
, PyObject
*args
)
1412 if (!PyArg_ParseTuple(args
, "|i:stat_float_times", &newval
))
1415 /* Return old value */
1416 return PyBool_FromLong(_stat_float_times
);
1417 _stat_float_times
= newval
;
1423 fill_time(PyObject
*v
, int index
, time_t sec
, unsigned long nsec
)
1425 PyObject
*fval
,*ival
;
1426 #if SIZEOF_TIME_T > SIZEOF_LONG
1427 ival
= PyLong_FromLongLong((PY_LONG_LONG
)sec
);
1429 ival
= PyLong_FromLong((long)sec
);
1433 if (_stat_float_times
) {
1434 fval
= PyFloat_FromDouble(sec
+ 1e-9*nsec
);
1439 PyStructSequence_SET_ITEM(v
, index
, ival
);
1440 PyStructSequence_SET_ITEM(v
, index
+3, fval
);
1443 /* pack a system stat C structure into the Python stat tuple
1444 (used by posix_stat() and posix_fstat()) */
1446 _pystat_fromstructstat(STRUCT_STAT
*st
)
1448 unsigned long ansec
, mnsec
, cnsec
;
1449 PyObject
*v
= PyStructSequence_New(&StatResultType
);
1453 PyStructSequence_SET_ITEM(v
, 0, PyLong_FromLong((long)st
->st_mode
));
1454 #ifdef HAVE_LARGEFILE_SUPPORT
1455 PyStructSequence_SET_ITEM(v
, 1,
1456 PyLong_FromLongLong((PY_LONG_LONG
)st
->st_ino
));
1458 PyStructSequence_SET_ITEM(v
, 1, PyLong_FromLong((long)st
->st_ino
));
1460 #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
1461 PyStructSequence_SET_ITEM(v
, 2,
1462 PyLong_FromLongLong((PY_LONG_LONG
)st
->st_dev
));
1464 PyStructSequence_SET_ITEM(v
, 2, PyLong_FromLong((long)st
->st_dev
));
1466 PyStructSequence_SET_ITEM(v
, 3, PyLong_FromLong((long)st
->st_nlink
));
1467 PyStructSequence_SET_ITEM(v
, 4, PyLong_FromLong((long)st
->st_uid
));
1468 PyStructSequence_SET_ITEM(v
, 5, PyLong_FromLong((long)st
->st_gid
));
1469 #ifdef HAVE_LARGEFILE_SUPPORT
1470 PyStructSequence_SET_ITEM(v
, 6,
1471 PyLong_FromLongLong((PY_LONG_LONG
)st
->st_size
));
1473 PyStructSequence_SET_ITEM(v
, 6, PyLong_FromLong(st
->st_size
));
1476 #if defined(HAVE_STAT_TV_NSEC)
1477 ansec
= st
->st_atim
.tv_nsec
;
1478 mnsec
= st
->st_mtim
.tv_nsec
;
1479 cnsec
= st
->st_ctim
.tv_nsec
;
1480 #elif defined(HAVE_STAT_TV_NSEC2)
1481 ansec
= st
->st_atimespec
.tv_nsec
;
1482 mnsec
= st
->st_mtimespec
.tv_nsec
;
1483 cnsec
= st
->st_ctimespec
.tv_nsec
;
1484 #elif defined(HAVE_STAT_NSEC)
1485 ansec
= st
->st_atime_nsec
;
1486 mnsec
= st
->st_mtime_nsec
;
1487 cnsec
= st
->st_ctime_nsec
;
1489 ansec
= mnsec
= cnsec
= 0;
1491 fill_time(v
, 7, st
->st_atime
, ansec
);
1492 fill_time(v
, 8, st
->st_mtime
, mnsec
);
1493 fill_time(v
, 9, st
->st_ctime
, cnsec
);
1495 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1496 PyStructSequence_SET_ITEM(v
, ST_BLKSIZE_IDX
,
1497 PyLong_FromLong((long)st
->st_blksize
));
1499 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1500 PyStructSequence_SET_ITEM(v
, ST_BLOCKS_IDX
,
1501 PyLong_FromLong((long)st
->st_blocks
));
1503 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1504 PyStructSequence_SET_ITEM(v
, ST_RDEV_IDX
,
1505 PyLong_FromLong((long)st
->st_rdev
));
1507 #ifdef HAVE_STRUCT_STAT_ST_GEN
1508 PyStructSequence_SET_ITEM(v
, ST_GEN_IDX
,
1509 PyLong_FromLong((long)st
->st_gen
));
1511 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1514 unsigned long bsec
,bnsec
;
1515 bsec
= (long)st
->st_birthtime
;
1516 #ifdef HAVE_STAT_TV_NSEC2
1517 bnsec
= st
->st_birthtimespec
.tv_nsec
;
1521 if (_stat_float_times
) {
1522 val
= PyFloat_FromDouble(bsec
+ 1e-9*bnsec
);
1524 val
= PyLong_FromLong((long)bsec
);
1526 PyStructSequence_SET_ITEM(v
, ST_BIRTHTIME_IDX
,
1530 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
1531 PyStructSequence_SET_ITEM(v
, ST_FLAGS_IDX
,
1532 PyLong_FromLong((long)st
->st_flags
));
1535 if (PyErr_Occurred()) {
1545 /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1546 where / can be used in place of \ and the trailing slash is optional.
1547 Both SERVER and SHARE must have at least one character.
1550 #define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1551 #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
1553 #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
1557 IsUNCRootA(char *path
, int pathlen
)
1559 #define ISSLASH ISSLASHA
1563 if (pathlen
< 5 || !ISSLASH(path
[0]) || !ISSLASH(path
[1]))
1564 /* minimum UNCRoot is \\x\y */
1566 for (i
= 2; i
< pathlen
; i
++)
1567 if (ISSLASH(path
[i
])) break;
1568 if (i
== 2 || i
== pathlen
)
1569 /* do not allow \\\SHARE or \\SERVER */
1572 for (i
= share
; i
< pathlen
; i
++)
1573 if (ISSLASH(path
[i
])) break;
1574 return (i
!= share
&& (i
== pathlen
|| i
== pathlen
-1));
1580 IsUNCRootW(Py_UNICODE
*path
, int pathlen
)
1582 #define ISSLASH ISSLASHW
1586 if (pathlen
< 5 || !ISSLASH(path
[0]) || !ISSLASH(path
[1]))
1587 /* minimum UNCRoot is \\x\y */
1589 for (i
= 2; i
< pathlen
; i
++)
1590 if (ISSLASH(path
[i
])) break;
1591 if (i
== 2 || i
== pathlen
)
1592 /* do not allow \\\SHARE or \\SERVER */
1595 for (i
= share
; i
< pathlen
; i
++)
1596 if (ISSLASH(path
[i
])) break;
1597 return (i
!= share
&& (i
== pathlen
|| i
== pathlen
-1));
1601 #endif /* MS_WINDOWS */
1604 posix_do_stat(PyObject
*self
, PyObject
*args
,
1607 int (*statfunc
)(const char *, STRUCT_STAT
*, ...),
1609 int (*statfunc
)(const char *, STRUCT_STAT
*),
1612 int (*wstatfunc
)(const Py_UNICODE
*, STRUCT_STAT
*))
1621 /* If on wide-character-capable OS see if argument
1622 is Unicode and if so use wide API. */
1623 if (unicode_file_names()) {
1624 PyUnicodeObject
*po
;
1625 if (PyArg_ParseTuple(args
, wformat
, &po
)) {
1626 Py_UNICODE
*wpath
= PyUnicode_AS_UNICODE(po
);
1628 Py_BEGIN_ALLOW_THREADS
1629 /* PyUnicode_AS_UNICODE result OK without
1630 thread lock as it is a simple dereference. */
1631 res
= wstatfunc(wpath
, &st
);
1632 Py_END_ALLOW_THREADS
1635 return win32_error_unicode("stat", wpath
);
1636 return _pystat_fromstructstat(&st
);
1638 /* Drop the argument parsing error as narrow strings
1644 if (!PyArg_ParseTuple(args
, format
,
1645 PyUnicode_FSConverter
, &opath
))
1647 path
= bytes2str(opath
, 1);
1648 Py_BEGIN_ALLOW_THREADS
1649 res
= (*statfunc
)(path
, &st
);
1650 Py_END_ALLOW_THREADS
1654 result
= win32_error("stat", path
);
1656 result
= posix_error_with_filename(path
);
1660 result
= _pystat_fromstructstat(&st
);
1662 release_bytes(opath
);
1668 PyDoc_STRVAR(posix_access__doc__
,
1669 "access(path, mode) -> True if granted, False otherwise\n\n\
1670 Use the real uid/gid to test for access to a path. Note that most\n\
1671 operations will use the effective uid/gid, therefore this routine can\n\
1672 be used in a suid/sgid environment to test if the invoking user has the\n\
1673 specified access to the path. The mode argument can be F_OK to test\n\
1674 existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
1677 posix_access(PyObject
*self
, PyObject
*args
)
1685 if (unicode_file_names()) {
1686 PyUnicodeObject
*po
;
1687 if (PyArg_ParseTuple(args
, "Ui:access", &po
, &mode
)) {
1688 Py_BEGIN_ALLOW_THREADS
1689 /* PyUnicode_AS_UNICODE OK without thread lock as
1690 it is a simple dereference. */
1691 attr
= GetFileAttributesW(PyUnicode_AS_UNICODE(po
));
1692 Py_END_ALLOW_THREADS
1695 /* Drop the argument parsing error as narrow strings
1699 if (!PyArg_ParseTuple(args
, "O&i:access",
1700 PyUnicode_FSConverter
, &opath
, &mode
))
1702 path
= bytes2str(opath
, 1);
1703 Py_BEGIN_ALLOW_THREADS
1704 attr
= GetFileAttributesA(path
);
1705 Py_END_ALLOW_THREADS
1706 release_bytes(opath
);
1708 if (attr
== 0xFFFFFFFF)
1709 /* File does not exist, or cannot read attributes */
1710 return PyBool_FromLong(0);
1711 /* Access is possible if either write access wasn't requested, or
1712 the file isn't read-only, or if it's a directory, as there are
1713 no read-only directories on Windows. */
1714 return PyBool_FromLong(!(mode
& 2)
1715 || !(attr
& FILE_ATTRIBUTE_READONLY
)
1716 || (attr
& FILE_ATTRIBUTE_DIRECTORY
));
1719 if (!PyArg_ParseTuple(args
, "O&i:access",
1720 PyUnicode_FSConverter
, &opath
, &mode
))
1722 path
= bytes2str(opath
, 1);
1723 Py_BEGIN_ALLOW_THREADS
1724 res
= access(path
, mode
);
1725 Py_END_ALLOW_THREADS
1726 release_bytes(opath
);
1727 return PyBool_FromLong(res
== 0);
1745 PyDoc_STRVAR(posix_ttyname__doc__
,
1746 "ttyname(fd) -> string\n\n\
1747 Return the name of the terminal device connected to 'fd'.");
1750 posix_ttyname(PyObject
*self
, PyObject
*args
)
1755 if (!PyArg_ParseTuple(args
, "i:ttyname", &id
))
1759 /* file descriptor 0 only, the default input device (stdin) */
1770 return posix_error();
1771 return PyUnicode_FromString(ret
);
1776 PyDoc_STRVAR(posix_ctermid__doc__
,
1777 "ctermid() -> string\n\n\
1778 Return the name of the controlling terminal for this process.");
1781 posix_ctermid(PyObject
*self
, PyObject
*noargs
)
1784 char buffer
[L_ctermid
];
1786 #ifdef USE_CTERMID_R
1787 ret
= ctermid_r(buffer
);
1789 ret
= ctermid(buffer
);
1792 return posix_error();
1793 return PyUnicode_FromString(buffer
);
1797 PyDoc_STRVAR(posix_chdir__doc__
,
1799 Change the current working directory to the specified path.");
1802 posix_chdir(PyObject
*self
, PyObject
*args
)
1805 return win32_1str(args
, "chdir", "y:chdir", win32_chdir
, "U:chdir", win32_wchdir
);
1806 #elif defined(PYOS_OS2) && defined(PYCC_GCC)
1807 return posix_1str(args
, "O&:chdir", _chdir2
);
1808 #elif defined(__VMS)
1809 return posix_1str(args
, "O&:chdir", (int (*)(const char *))chdir
);
1811 return posix_1str(args
, "O&:chdir", chdir
);
1816 PyDoc_STRVAR(posix_fchdir__doc__
,
1817 "fchdir(fildes)\n\n\
1818 Change to the directory of the given file descriptor. fildes must be\n\
1819 opened on a directory, not a file.");
1822 posix_fchdir(PyObject
*self
, PyObject
*fdobj
)
1824 return posix_fildes(fdobj
, fchdir
);
1826 #endif /* HAVE_FCHDIR */
1829 PyDoc_STRVAR(posix_chmod__doc__
,
1830 "chmod(path, mode)\n\n\
1831 Change the access permissions of a file.");
1834 posix_chmod(PyObject
*self
, PyObject
*args
)
1836 PyObject
*opath
= NULL
;
1842 if (unicode_file_names()) {
1843 PyUnicodeObject
*po
;
1844 if (PyArg_ParseTuple(args
, "Ui|:chmod", &po
, &i
)) {
1845 Py_BEGIN_ALLOW_THREADS
1846 attr
= GetFileAttributesW(PyUnicode_AS_UNICODE(po
));
1847 if (attr
!= 0xFFFFFFFF) {
1849 attr
&= ~FILE_ATTRIBUTE_READONLY
;
1851 attr
|= FILE_ATTRIBUTE_READONLY
;
1852 res
= SetFileAttributesW(PyUnicode_AS_UNICODE(po
), attr
);
1856 Py_END_ALLOW_THREADS
1858 return win32_error_unicode("chmod",
1859 PyUnicode_AS_UNICODE(po
));
1863 /* Drop the argument parsing error as narrow strings
1867 if (!PyArg_ParseTuple(args
, "O&i:chmod", PyUnicode_FSConverter
,
1870 path
= bytes2str(opath
, 1);
1871 Py_BEGIN_ALLOW_THREADS
1872 attr
= GetFileAttributesA(path
);
1873 if (attr
!= 0xFFFFFFFF) {
1875 attr
&= ~FILE_ATTRIBUTE_READONLY
;
1877 attr
|= FILE_ATTRIBUTE_READONLY
;
1878 res
= SetFileAttributesA(path
, attr
);
1882 Py_END_ALLOW_THREADS
1884 win32_error("chmod", path
);
1885 release_bytes(opath
);
1888 release_bytes(opath
);
1891 #else /* MS_WINDOWS */
1892 if (!PyArg_ParseTuple(args
, "O&i:chmod", PyUnicode_FSConverter
,
1895 path
= bytes2str(opath
, 1);
1896 Py_BEGIN_ALLOW_THREADS
1897 res
= chmod(path
, i
);
1898 Py_END_ALLOW_THREADS
1900 return posix_error_with_allocated_filename(opath
);
1901 release_bytes(opath
);
1908 PyDoc_STRVAR(posix_fchmod__doc__
,
1909 "fchmod(fd, mode)\n\n\
1910 Change the access permissions of the file given by file\n\
1914 posix_fchmod(PyObject
*self
, PyObject
*args
)
1917 if (!PyArg_ParseTuple(args
, "ii:fchmod", &fd
, &mode
))
1919 Py_BEGIN_ALLOW_THREADS
1920 res
= fchmod(fd
, mode
);
1921 Py_END_ALLOW_THREADS
1923 return posix_error();
1926 #endif /* HAVE_FCHMOD */
1929 PyDoc_STRVAR(posix_lchmod__doc__
,
1930 "lchmod(path, mode)\n\n\
1931 Change the access permissions of a file. If path is a symlink, this\n\
1932 affects the link itself rather than the target.");
1935 posix_lchmod(PyObject
*self
, PyObject
*args
)
1941 if (!PyArg_ParseTuple(args
, "O&i:lchmod", PyUnicode_FSConverter
,
1944 path
= bytes2str(opath
, 1);
1945 Py_BEGIN_ALLOW_THREADS
1946 res
= lchmod(path
, i
);
1947 Py_END_ALLOW_THREADS
1949 return posix_error_with_allocated_filename(opath
);
1950 release_bytes(opath
);
1953 #endif /* HAVE_LCHMOD */
1957 PyDoc_STRVAR(posix_chflags__doc__
,
1958 "chflags(path, flags)\n\n\
1962 posix_chflags(PyObject
*self
, PyObject
*args
)
1966 unsigned long flags
;
1968 if (!PyArg_ParseTuple(args
, "O&k:chflags",
1969 PyUnicode_FSConverter
, &opath
, &flags
))
1971 path
= bytes2str(opath
, 1);
1972 Py_BEGIN_ALLOW_THREADS
1973 res
= chflags(path
, flags
);
1974 Py_END_ALLOW_THREADS
1976 return posix_error_with_allocated_filename(opath
);
1977 release_bytes(opath
);
1981 #endif /* HAVE_CHFLAGS */
1983 #ifdef HAVE_LCHFLAGS
1984 PyDoc_STRVAR(posix_lchflags__doc__
,
1985 "lchflags(path, flags)\n\n\
1987 This function will not follow symbolic links.");
1990 posix_lchflags(PyObject
*self
, PyObject
*args
)
1994 unsigned long flags
;
1996 if (!PyArg_ParseTuple(args
, "O&k:lchflags",
1997 PyUnicode_FSConverter
, &opath
, &flags
))
1999 path
= bytes2str(opath
, 1);
2000 Py_BEGIN_ALLOW_THREADS
2001 res
= lchflags(path
, flags
);
2002 Py_END_ALLOW_THREADS
2004 return posix_error_with_allocated_filename(opath
);
2005 release_bytes(opath
);
2009 #endif /* HAVE_LCHFLAGS */
2012 PyDoc_STRVAR(posix_chroot__doc__
,
2014 Change root directory to path.");
2017 posix_chroot(PyObject
*self
, PyObject
*args
)
2019 return posix_1str(args
, "O&:chroot", chroot
);
2024 PyDoc_STRVAR(posix_fsync__doc__
,
2026 force write of file with filedescriptor to disk.");
2029 posix_fsync(PyObject
*self
, PyObject
*fdobj
)
2031 return posix_fildes(fdobj
, fsync
);
2033 #endif /* HAVE_FSYNC */
2035 #ifdef HAVE_FDATASYNC
2038 extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2041 PyDoc_STRVAR(posix_fdatasync__doc__
,
2042 "fdatasync(fildes)\n\n\
2043 force write of file with filedescriptor to disk.\n\
2044 does not force update of metadata.");
2047 posix_fdatasync(PyObject
*self
, PyObject
*fdobj
)
2049 return posix_fildes(fdobj
, fdatasync
);
2051 #endif /* HAVE_FDATASYNC */
2055 PyDoc_STRVAR(posix_chown__doc__
,
2056 "chown(path, uid, gid)\n\n\
2057 Change the owner and group id of path to the numeric uid and gid.");
2060 posix_chown(PyObject
*self
, PyObject
*args
)
2066 if (!PyArg_ParseTuple(args
, "O&ll:chown",
2067 PyUnicode_FSConverter
, &opath
,
2070 path
= bytes2str(opath
, 1);
2071 Py_BEGIN_ALLOW_THREADS
2072 res
= chown(path
, (uid_t
) uid
, (gid_t
) gid
);
2073 Py_END_ALLOW_THREADS
2075 return posix_error_with_allocated_filename(opath
);
2076 release_bytes(opath
);
2080 #endif /* HAVE_CHOWN */
2083 PyDoc_STRVAR(posix_fchown__doc__
,
2084 "fchown(fd, uid, gid)\n\n\
2085 Change the owner and group id of the file given by file descriptor\n\
2086 fd to the numeric uid and gid.");
2089 posix_fchown(PyObject
*self
, PyObject
*args
)
2094 if (!PyArg_ParseTuple(args
, "ill:chown", &fd
, &uid
, &gid
))
2096 Py_BEGIN_ALLOW_THREADS
2097 res
= fchown(fd
, (uid_t
) uid
, (gid_t
) gid
);
2098 Py_END_ALLOW_THREADS
2100 return posix_error();
2103 #endif /* HAVE_FCHOWN */
2106 PyDoc_STRVAR(posix_lchown__doc__
,
2107 "lchown(path, uid, gid)\n\n\
2108 Change the owner and group id of path to the numeric uid and gid.\n\
2109 This function will not follow symbolic links.");
2112 posix_lchown(PyObject
*self
, PyObject
*args
)
2118 if (!PyArg_ParseTuple(args
, "O&ll:lchown",
2119 PyUnicode_FSConverter
, &opath
,
2122 path
= bytes2str(opath
, 1);
2123 Py_BEGIN_ALLOW_THREADS
2124 res
= lchown(path
, (uid_t
) uid
, (gid_t
) gid
);
2125 Py_END_ALLOW_THREADS
2127 return posix_error_with_allocated_filename(opath
);
2128 release_bytes(opath
);
2132 #endif /* HAVE_LCHOWN */
2137 posix_getcwd(int use_bytes
)
2143 if (!use_bytes
&& unicode_file_names()) {
2145 wchar_t *wbuf2
= wbuf
;
2148 Py_BEGIN_ALLOW_THREADS
2149 len
= GetCurrentDirectoryW(sizeof wbuf
/ sizeof wbuf
[0], wbuf
);
2150 /* If the buffer is large enough, len does not include the
2151 terminating \0. If the buffer is too small, len includes
2152 the space needed for the terminator. */
2153 if (len
>= sizeof wbuf
/ sizeof wbuf
[0]) {
2154 wbuf2
= malloc(len
* sizeof(wchar_t));
2156 len
= GetCurrentDirectoryW(len
, wbuf2
);
2158 Py_END_ALLOW_THREADS
2164 if (wbuf2
!= wbuf
) free(wbuf2
);
2165 return win32_error("getcwdu", NULL
);
2167 resobj
= PyUnicode_FromWideChar(wbuf2
, len
);
2168 if (wbuf2
!= wbuf
) free(wbuf2
);
2173 Py_BEGIN_ALLOW_THREADS
2174 #if defined(PYOS_OS2) && defined(PYCC_GCC)
2175 res
= _getcwd2(buf
, sizeof buf
);
2177 res
= getcwd(buf
, sizeof buf
);
2179 Py_END_ALLOW_THREADS
2181 return posix_error();
2183 return PyBytes_FromStringAndSize(buf
, strlen(buf
));
2184 return PyUnicode_Decode(buf
, strlen(buf
), Py_FileSystemDefaultEncoding
,"surrogateescape");
2187 PyDoc_STRVAR(posix_getcwd__doc__
,
2188 "getcwd() -> path\n\n\
2189 Return a unicode string representing the current working directory.");
2192 posix_getcwd_unicode(PyObject
*self
)
2194 return posix_getcwd(0);
2197 PyDoc_STRVAR(posix_getcwdb__doc__
,
2198 "getcwdb() -> path\n\n\
2199 Return a bytes string representing the current working directory.");
2202 posix_getcwd_bytes(PyObject
*self
)
2204 return posix_getcwd(1);
2210 PyDoc_STRVAR(posix_link__doc__
,
2211 "link(src, dst)\n\n\
2212 Create a hard link to a file.");
2215 posix_link(PyObject
*self
, PyObject
*args
)
2217 return posix_2str(args
, "O&O&:link", link
);
2219 #endif /* HAVE_LINK */
2222 PyDoc_STRVAR(posix_listdir__doc__
,
2223 "listdir(path) -> list_of_strings\n\n\
2224 Return a list containing the names of the entries in the directory.\n\
2226 path: path of directory to list\n\
2228 The list is in arbitrary order. It does not include the special\n\
2229 entries '.' and '..' even if they are present in the directory.");
2232 posix_listdir(PyObject
*self
, PyObject
*args
)
2234 /* XXX Should redo this putting the (now four) versions of opendir
2235 in separate files instead of having them all here... */
2236 #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
2241 WIN32_FIND_DATA FileData
;
2243 char namebuf
[MAX_PATH
+5]; /* Overallocate for \\*.*\0 */
2244 char *bufptr
= namebuf
;
2245 Py_ssize_t len
= sizeof(namebuf
)-5; /* only claim to have space for MAX_PATH */
2247 /* If on wide-character-capable OS see if argument
2248 is Unicode and if so use wide API. */
2249 if (unicode_file_names()) {
2251 if (PyArg_ParseTuple(args
, "U:listdir", &po
)) {
2252 WIN32_FIND_DATAW wFileData
;
2253 Py_UNICODE
*wnamebuf
;
2254 /* Overallocate for \\*.*\0 */
2255 len
= PyUnicode_GET_SIZE(po
);
2256 wnamebuf
= malloc((len
+ 5) * sizeof(wchar_t));
2261 wcscpy(wnamebuf
, PyUnicode_AS_UNICODE(po
));
2263 Py_UNICODE wch
= wnamebuf
[len
-1];
2264 if (wch
!= L
'/' && wch
!= L
'\\' && wch
!= L
':')
2265 wnamebuf
[len
++] = L
'\\';
2266 wcscpy(wnamebuf
+ len
, L
"*.*");
2268 if ((d
= PyList_New(0)) == NULL
) {
2272 hFindFile
= FindFirstFileW(wnamebuf
, &wFileData
);
2273 if (hFindFile
== INVALID_HANDLE_VALUE
) {
2274 int error
= GetLastError();
2275 if (error
== ERROR_FILE_NOT_FOUND
) {
2280 win32_error_unicode("FindFirstFileW", wnamebuf
);
2285 /* Skip over . and .. */
2286 if (wcscmp(wFileData
.cFileName
, L
".") != 0 &&
2287 wcscmp(wFileData
.cFileName
, L
"..") != 0) {
2288 v
= PyUnicode_FromUnicode(wFileData
.cFileName
, wcslen(wFileData
.cFileName
));
2294 if (PyList_Append(d
, v
) != 0) {
2302 Py_BEGIN_ALLOW_THREADS
2303 result
= FindNextFileW(hFindFile
, &wFileData
);
2304 Py_END_ALLOW_THREADS
2305 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2306 it got to the end of the directory. */
2307 if (!result
&& GetLastError() != ERROR_NO_MORE_FILES
) {
2309 win32_error_unicode("FindNextFileW", wnamebuf
);
2310 FindClose(hFindFile
);
2314 } while (result
== TRUE
);
2316 if (FindClose(hFindFile
) == FALSE
) {
2318 win32_error_unicode("FindClose", wnamebuf
);
2325 /* Drop the argument parsing error as narrow strings
2330 if (!PyArg_ParseTuple(args
, "O&:listdir",
2331 PyUnicode_FSConverter
, &opath
))
2333 if (PyObject_Size(opath
)+1 > MAX_PATH
) {
2334 PyErr_SetString(PyExc_ValueError
, "path too long");
2338 strcpy(namebuf
, bytes2str(opath
, 0));
2339 len
= PyObject_Size(opath
);
2341 char ch
= namebuf
[len
-1];
2342 if (ch
!= SEP
&& ch
!= ALTSEP
&& ch
!= ':')
2343 namebuf
[len
++] = '/';
2344 strcpy(namebuf
+ len
, "*.*");
2347 if ((d
= PyList_New(0)) == NULL
)
2350 hFindFile
= FindFirstFile(namebuf
, &FileData
);
2351 if (hFindFile
== INVALID_HANDLE_VALUE
) {
2352 int error
= GetLastError();
2353 if (error
== ERROR_FILE_NOT_FOUND
)
2356 return win32_error("FindFirstFile", namebuf
);
2359 /* Skip over . and .. */
2360 if (strcmp(FileData
.cFileName
, ".") != 0 &&
2361 strcmp(FileData
.cFileName
, "..") != 0) {
2362 v
= PyBytes_FromString(FileData
.cFileName
);
2368 if (PyList_Append(d
, v
) != 0) {
2376 Py_BEGIN_ALLOW_THREADS
2377 result
= FindNextFile(hFindFile
, &FileData
);
2378 Py_END_ALLOW_THREADS
2379 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2380 it got to the end of the directory. */
2381 if (!result
&& GetLastError() != ERROR_NO_MORE_FILES
) {
2383 win32_error("FindNextFile", namebuf
);
2384 FindClose(hFindFile
);
2387 } while (result
== TRUE
);
2389 if (FindClose(hFindFile
) == FALSE
) {
2391 return win32_error("FindClose", namebuf
);
2396 #elif defined(PYOS_OS2)
2399 #define MAX_PATH CCHMAXPATH
2405 char namebuf
[MAX_PATH
+5];
2411 if (!PyArg_ParseTuple(args
, "O&:listdir",
2412 PyUnicode_FSConverter
, &oname
))
2414 name
= bytes2str(oname
);
2415 len
= PyObject_Size(oname
);
2416 if (len
>= MAX_PATH
) {
2417 release_bytes(oname
);
2418 PyErr_SetString(PyExc_ValueError
, "path too long");
2421 strcpy(namebuf
, name
);
2422 for (pt
= namebuf
; *pt
; pt
++)
2425 if (namebuf
[len
-1] != SEP
)
2426 namebuf
[len
++] = SEP
;
2427 strcpy(namebuf
+ len
, "*.*");
2429 if ((d
= PyList_New(0)) == NULL
) {
2430 release_bytes(oname
);
2434 rc
= DosFindFirst(namebuf
, /* Wildcard Pattern to Match */
2435 &hdir
, /* Handle to Use While Search Directory */
2436 FILE_READONLY
| FILE_HIDDEN
| FILE_SYSTEM
| FILE_DIRECTORY
,
2437 &ep
, sizeof(ep
), /* Structure to Receive Directory Entry */
2438 &srchcnt
, /* Max and Actual Count of Entries Per Iteration */
2439 FIL_STANDARD
); /* Format of Entry (EAs or Not) */
2441 if (rc
!= NO_ERROR
) {
2443 return posix_error_with_allocated_filename(oname
);
2446 if (srchcnt
> 0) { /* If Directory is NOT Totally Empty, */
2448 if (ep
.achName
[0] == '.'
2449 && (ep
.achName
[1] == '\0' || (ep
.achName
[1] == '.' && ep
.achName
[2] == '\0')))
2450 continue; /* Skip Over "." and ".." Names */
2452 strcpy(namebuf
, ep
.achName
);
2454 /* Leave Case of Name Alone -- In Native Form */
2455 /* (Removed Forced Lowercasing Code) */
2457 v
= PyBytes_FromString(namebuf
);
2463 if (PyList_Append(d
, v
) != 0) {
2470 } while (DosFindNext(hdir
, &ep
, sizeof(ep
), &srchcnt
) == NO_ERROR
&& srchcnt
> 0);
2473 release_bytes(oname
);
2481 int arg_is_unicode
= 1;
2484 if (!PyArg_ParseTuple(args
, "U:listdir", &v
)) {
2488 if (!PyArg_ParseTuple(args
, "O&:listdir", PyUnicode_FSConverter
, &oname
))
2490 name
= bytes2str(oname
, 1);
2491 if ((dirp
= opendir(name
)) == NULL
) {
2492 return posix_error_with_allocated_filename(oname
);
2494 if ((d
= PyList_New(0)) == NULL
) {
2496 release_bytes(oname
);
2501 Py_BEGIN_ALLOW_THREADS
2503 Py_END_ALLOW_THREADS
2510 return posix_error_with_allocated_filename(oname
);
2513 if (ep
->d_name
[0] == '.' &&
2515 (ep
->d_name
[1] == '.' && NAMLEN(ep
) == 2)))
2517 v
= PyBytes_FromStringAndSize(ep
->d_name
, NAMLEN(ep
));
2523 if (arg_is_unicode
) {
2526 w
= PyUnicode_FromEncodedObject(v
,
2527 Py_FileSystemDefaultEncoding
,
2533 /* Encoding failed to decode ASCII bytes.
2540 if (PyList_Append(d
, v
) != 0) {
2549 release_bytes(oname
);
2553 #endif /* which OS */
2554 } /* end of posix_listdir */
2557 /* A helper function for abspath on win32 */
2559 posix__getfullpathname(PyObject
*self
, PyObject
*args
)
2563 char outbuf
[MAX_PATH
*2];
2566 if (unicode_file_names()) {
2567 PyUnicodeObject
*po
;
2568 if (PyArg_ParseTuple(args
, "U|:_getfullpathname", &po
)) {
2569 Py_UNICODE
*wpath
= PyUnicode_AS_UNICODE(po
);
2570 Py_UNICODE woutbuf
[MAX_PATH
*2], *woutbufp
= woutbuf
;
2574 result
= GetFullPathNameW(wpath
,
2575 sizeof(woutbuf
)/sizeof(woutbuf
[0]),
2577 if (result
> sizeof(woutbuf
)/sizeof(woutbuf
[0])) {
2578 woutbufp
= malloc(result
* sizeof(Py_UNICODE
));
2580 return PyErr_NoMemory();
2581 result
= GetFullPathNameW(wpath
, result
, woutbufp
, &wtemp
);
2584 v
= PyUnicode_FromUnicode(woutbufp
, wcslen(woutbufp
));
2586 v
= win32_error_unicode("GetFullPathNameW", wpath
);
2587 if (woutbufp
!= woutbuf
)
2591 /* Drop the argument parsing error as narrow strings
2596 if (!PyArg_ParseTuple (args
, "O&:_getfullpathname",
2597 PyUnicode_FSConverter
, &opath
))
2599 path
= bytes2str(opath
, 1);
2600 if (!GetFullPathName(path
, sizeof(outbuf
)/sizeof(outbuf
[0]),
2602 win32_error("GetFullPathName", path
);
2603 release_bytes(opath
);
2606 release_bytes(opath
);
2607 if (PyUnicode_Check(PyTuple_GetItem(args
, 0))) {
2608 return PyUnicode_Decode(outbuf
, strlen(outbuf
),
2609 Py_FileSystemDefaultEncoding
, NULL
);
2611 return PyBytes_FromString(outbuf
);
2612 } /* end of posix__getfullpathname */
2613 #endif /* MS_WINDOWS */
2615 PyDoc_STRVAR(posix_mkdir__doc__
,
2616 "mkdir(path [, mode=0777])\n\n\
2617 Create a directory.");
2620 posix_mkdir(PyObject
*self
, PyObject
*args
)
2628 if (unicode_file_names()) {
2629 PyUnicodeObject
*po
;
2630 if (PyArg_ParseTuple(args
, "U|i:mkdir", &po
, &mode
)) {
2631 Py_BEGIN_ALLOW_THREADS
2632 /* PyUnicode_AS_UNICODE OK without thread lock as
2633 it is a simple dereference. */
2634 res
= CreateDirectoryW(PyUnicode_AS_UNICODE(po
), NULL
);
2635 Py_END_ALLOW_THREADS
2637 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po
));
2641 /* Drop the argument parsing error as narrow strings
2645 if (!PyArg_ParseTuple(args
, "O&|i:mkdir",
2646 PyUnicode_FSConverter
, &opath
, &mode
))
2648 path
= bytes2str(opath
, 1);
2649 Py_BEGIN_ALLOW_THREADS
2650 /* PyUnicode_AS_UNICODE OK without thread lock as
2651 it is a simple dereference. */
2652 res
= CreateDirectoryA(path
, NULL
);
2653 Py_END_ALLOW_THREADS
2655 win32_error("mkdir", path
);
2656 release_bytes(opath
);
2659 release_bytes(opath
);
2664 if (!PyArg_ParseTuple(args
, "O&|i:mkdir",
2665 PyUnicode_FSConverter
, &opath
, &mode
))
2667 path
= bytes2str(opath
, 1);
2668 Py_BEGIN_ALLOW_THREADS
2669 #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
2672 res
= mkdir(path
, mode
);
2674 Py_END_ALLOW_THREADS
2676 return posix_error_with_allocated_filename(opath
);
2677 release_bytes(opath
);
2684 /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2685 #if defined(HAVE_SYS_RESOURCE_H)
2686 #include <sys/resource.h>
2691 PyDoc_STRVAR(posix_nice__doc__
,
2692 "nice(inc) -> new_priority\n\n\
2693 Decrease the priority of process by inc and return the new priority.");
2696 posix_nice(PyObject
*self
, PyObject
*args
)
2698 int increment
, value
;
2700 if (!PyArg_ParseTuple(args
, "i:nice", &increment
))
2703 /* There are two flavours of 'nice': one that returns the new
2704 priority (as required by almost all standards out there) and the
2705 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2706 the use of getpriority() to get the new priority.
2708 If we are of the nice family that returns the new priority, we
2709 need to clear errno before the call, and check if errno is filled
2710 before calling posix_error() on a returnvalue of -1, because the
2711 -1 may be the actual new priority! */
2714 value
= nice(increment
);
2715 #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
2717 value
= getpriority(PRIO_PROCESS
, 0);
2719 if (value
== -1 && errno
!= 0)
2720 /* either nice() or getpriority() returned an error */
2721 return posix_error();
2722 return PyLong_FromLong((long) value
);
2724 #endif /* HAVE_NICE */
2726 PyDoc_STRVAR(posix_rename__doc__
,
2727 "rename(old, new)\n\n\
2728 Rename a file or directory.");
2731 posix_rename(PyObject
*self
, PyObject
*args
)
2737 if (unicode_file_names()) {
2738 if (!PyArg_ParseTuple(args
, "OO:rename", &o1
, &o2
))
2740 if (!convert_to_unicode(&o1
))
2742 if (!convert_to_unicode(&o2
)) {
2746 Py_BEGIN_ALLOW_THREADS
2747 result
= MoveFileW(PyUnicode_AsUnicode(o1
),
2748 PyUnicode_AsUnicode(o2
));
2749 Py_END_ALLOW_THREADS
2753 return win32_error("rename", NULL
);
2759 if (!PyArg_ParseTuple(args
, "ss:rename", &p1
, &p2
))
2761 Py_BEGIN_ALLOW_THREADS
2762 result
= MoveFileA(p1
, p2
);
2763 Py_END_ALLOW_THREADS
2765 return win32_error("rename", NULL
);
2769 return posix_2str(args
, "O&O&:rename", rename
);
2774 PyDoc_STRVAR(posix_rmdir__doc__
,
2776 Remove a directory.");
2779 posix_rmdir(PyObject
*self
, PyObject
*args
)
2782 return win32_1str(args
, "rmdir", "y:rmdir", RemoveDirectoryA
, "U:rmdir", RemoveDirectoryW
);
2784 return posix_1str(args
, "O&:rmdir", rmdir
);
2789 PyDoc_STRVAR(posix_stat__doc__
,
2790 "stat(path) -> stat result\n\n\
2791 Perform a stat system call on the given path.");
2794 posix_stat(PyObject
*self
, PyObject
*args
)
2797 return posix_do_stat(self
, args
, "O&:stat", STAT
, "U:stat", win32_wstat
);
2799 return posix_do_stat(self
, args
, "O&:stat", STAT
, NULL
, NULL
);
2805 PyDoc_STRVAR(posix_system__doc__
,
2806 "system(command) -> exit_status\n\n\
2807 Execute the command (a string) in a subshell.");
2810 posix_system(PyObject
*self
, PyObject
*args
)
2815 if (!PyArg_ParseTuple(args
, "u:system", &command
))
2818 Py_BEGIN_ALLOW_THREADS
2819 sts
= _wsystem(command
);
2820 Py_END_ALLOW_THREADS
2822 PyObject
*command_obj
;
2824 if (!PyArg_ParseTuple(args
, "O&:system",
2825 PyUnicode_FSConverter
, &command_obj
))
2828 command
= bytes2str(command_obj
, 1);
2829 Py_BEGIN_ALLOW_THREADS
2830 sts
= system(command
);
2831 Py_END_ALLOW_THREADS
2832 release_bytes(command_obj
);
2834 return PyLong_FromLong(sts
);
2839 PyDoc_STRVAR(posix_umask__doc__
,
2840 "umask(new_mask) -> old_mask\n\n\
2841 Set the current numeric umask and return the previous umask.");
2844 posix_umask(PyObject
*self
, PyObject
*args
)
2847 if (!PyArg_ParseTuple(args
, "i:umask", &i
))
2851 return posix_error();
2852 return PyLong_FromLong((long)i
);
2856 PyDoc_STRVAR(posix_unlink__doc__
,
2858 Remove a file (same as remove(path)).");
2860 PyDoc_STRVAR(posix_remove__doc__
,
2862 Remove a file (same as unlink(path)).");
2865 posix_unlink(PyObject
*self
, PyObject
*args
)
2868 return win32_1str(args
, "remove", "y:remove", DeleteFileA
, "U:remove", DeleteFileW
);
2870 return posix_1str(args
, "O&:remove", unlink
);
2876 PyDoc_STRVAR(posix_uname__doc__
,
2877 "uname() -> (sysname, nodename, release, version, machine)\n\n\
2878 Return a tuple identifying the current operating system.");
2881 posix_uname(PyObject
*self
, PyObject
*noargs
)
2886 Py_BEGIN_ALLOW_THREADS
2888 Py_END_ALLOW_THREADS
2890 return posix_error();
2891 return Py_BuildValue("(sssss)",
2898 #endif /* HAVE_UNAME */
2901 extract_time(PyObject
*t
, long* sec
, long* usec
)
2904 if (PyFloat_Check(t
)) {
2905 double tval
= PyFloat_AsDouble(t
);
2906 PyObject
*intobj
= Py_TYPE(t
)->tp_as_number
->nb_int(t
);
2909 intval
= PyLong_AsLong(intobj
);
2911 if (intval
== -1 && PyErr_Occurred())
2914 *usec
= (long)((tval
- intval
) * 1e6
); /* can't exceed 1000000 */
2916 /* If rounding gave us a negative number,
2921 intval
= PyLong_AsLong(t
);
2922 if (intval
== -1 && PyErr_Occurred())
2929 PyDoc_STRVAR(posix_utime__doc__
,
2930 "utime(path, (atime, mtime))\n\
2931 utime(path, None)\n\n\
2932 Set the access and modified time of the file to the given values. If the\n\
2933 second form is used, set the access and modified times to the current time.");
2936 posix_utime(PyObject
*self
, PyObject
*args
)
2940 PyUnicodeObject
*obwpath
;
2941 wchar_t *wpath
= NULL
;
2945 long atimesec
, mtimesec
, ausec
, musec
;
2946 FILETIME atime
, mtime
;
2947 PyObject
*result
= NULL
;
2949 if (unicode_file_names()) {
2950 if (PyArg_ParseTuple(args
, "UO|:utime", &obwpath
, &arg
)) {
2951 wpath
= PyUnicode_AS_UNICODE(obwpath
);
2952 Py_BEGIN_ALLOW_THREADS
2953 hFile
= CreateFileW(wpath
, FILE_WRITE_ATTRIBUTES
, 0,
2954 NULL
, OPEN_EXISTING
,
2955 FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
2956 Py_END_ALLOW_THREADS
2957 if (hFile
== INVALID_HANDLE_VALUE
)
2958 return win32_error_unicode("utime", wpath
);
2960 /* Drop the argument parsing error as narrow strings
2965 if (!PyArg_ParseTuple(args
, "O&O:utime",
2966 PyUnicode_FSConverter
, &oapath
, &arg
))
2968 apath
= bytes2str(oapath
, 1);
2969 Py_BEGIN_ALLOW_THREADS
2970 hFile
= CreateFileA(apath
, FILE_WRITE_ATTRIBUTES
, 0,
2971 NULL
, OPEN_EXISTING
,
2972 FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
2973 Py_END_ALLOW_THREADS
2974 if (hFile
== INVALID_HANDLE_VALUE
) {
2975 win32_error("utime", apath
);
2976 release_bytes(oapath
);
2979 release_bytes(oapath
);
2982 if (arg
== Py_None
) {
2984 GetSystemTime(&now
);
2985 if (!SystemTimeToFileTime(&now
, &mtime
) ||
2986 !SystemTimeToFileTime(&now
, &atime
)) {
2987 win32_error("utime", NULL
);
2991 else if (!PyTuple_Check(arg
) || PyTuple_Size(arg
) != 2) {
2992 PyErr_SetString(PyExc_TypeError
,
2993 "utime() arg 2 must be a tuple (atime, mtime)");
2997 if (extract_time(PyTuple_GET_ITEM(arg
, 0),
2998 &atimesec
, &ausec
) == -1)
3000 time_t_to_FILE_TIME(atimesec
, 1000*ausec
, &atime
);
3001 if (extract_time(PyTuple_GET_ITEM(arg
, 1),
3002 &mtimesec
, &musec
) == -1)
3004 time_t_to_FILE_TIME(mtimesec
, 1000*musec
, &mtime
);
3006 if (!SetFileTime(hFile
, NULL
, &atime
, &mtime
)) {
3007 /* Avoid putting the file name into the error here,
3008 as that may confuse the user into believing that
3009 something is wrong with the file, when it also
3010 could be the time stamp that gives a problem. */
3011 win32_error("utime", NULL
);
3018 #else /* MS_WINDOWS */
3022 long atime
, mtime
, ausec
, musec
;
3026 #if defined(HAVE_UTIMES)
3027 struct timeval buf
[2];
3028 #define ATIME buf[0].tv_sec
3029 #define MTIME buf[1].tv_sec
3030 #elif defined(HAVE_UTIME_H)
3031 /* XXX should define struct utimbuf instead, above */
3033 #define ATIME buf.actime
3034 #define MTIME buf.modtime
3035 #define UTIME_ARG &buf
3036 #else /* HAVE_UTIMES */
3038 #define ATIME buf[0]
3039 #define MTIME buf[1]
3040 #define UTIME_ARG buf
3041 #endif /* HAVE_UTIMES */
3044 if (!PyArg_ParseTuple(args
, "O&O:utime",
3045 PyUnicode_FSConverter
, &opath
, &arg
))
3047 path
= bytes2str(opath
, 1);
3048 if (arg
== Py_None
) {
3049 /* optional time values not given */
3050 Py_BEGIN_ALLOW_THREADS
3051 res
= utime(path
, NULL
);
3052 Py_END_ALLOW_THREADS
3054 else if (!PyTuple_Check(arg
) || PyTuple_Size(arg
) != 2) {
3055 PyErr_SetString(PyExc_TypeError
,
3056 "utime() arg 2 must be a tuple (atime, mtime)");
3057 release_bytes(opath
);
3061 if (extract_time(PyTuple_GET_ITEM(arg
, 0),
3062 &atime
, &ausec
) == -1) {
3063 release_bytes(opath
);
3066 if (extract_time(PyTuple_GET_ITEM(arg
, 1),
3067 &mtime
, &musec
) == -1) {
3068 release_bytes(opath
);
3074 buf
[0].tv_usec
= ausec
;
3075 buf
[1].tv_usec
= musec
;
3076 Py_BEGIN_ALLOW_THREADS
3077 res
= utimes(path
, buf
);
3078 Py_END_ALLOW_THREADS
3080 Py_BEGIN_ALLOW_THREADS
3081 res
= utime(path
, UTIME_ARG
);
3082 Py_END_ALLOW_THREADS
3083 #endif /* HAVE_UTIMES */
3086 return posix_error_with_allocated_filename(opath
);
3088 release_bytes(opath
);
3094 #endif /* MS_WINDOWS */
3098 /* Process operations */
3100 PyDoc_STRVAR(posix__exit__doc__
,
3102 Exit to the system with specified status, without normal exit processing.");
3105 posix__exit(PyObject
*self
, PyObject
*args
)
3108 if (!PyArg_ParseTuple(args
, "i:_exit", &sts
))
3111 return NULL
; /* Make gcc -Wall happy */
3114 #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3116 free_string_array(char **array
, Py_ssize_t count
)
3119 for (i
= 0; i
< count
; i
++)
3120 PyMem_Free(array
[i
]);
3125 int fsconvert_strdup(PyObject
*o
, char**out
)
3129 if (!PyUnicode_FSConverter(o
, &bytes
))
3131 size
= PyObject_Size(bytes
);
3132 *out
= PyMem_Malloc(size
+1);
3135 /* Don't lock bytes, as we hold the GIL */
3136 memcpy(*out
, bytes2str(bytes
, 0), size
+1);
3144 PyDoc_STRVAR(posix_execv__doc__
,
3145 "execv(path, args)\n\n\
3146 Execute an executable path with arguments, replacing current process.\n\
3148 path: path of executable file\n\
3149 args: tuple or list of strings");
3152 posix_execv(PyObject
*self
, PyObject
*args
)
3159 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3161 /* execv has two arguments: (path, argv), where
3162 argv is a list or tuple of strings. */
3164 if (!PyArg_ParseTuple(args
, "O&O:execv",
3165 PyUnicode_FSConverter
,
3168 path
= bytes2str(opath
, 1);
3169 if (PyList_Check(argv
)) {
3170 argc
= PyList_Size(argv
);
3171 getitem
= PyList_GetItem
;
3173 else if (PyTuple_Check(argv
)) {
3174 argc
= PyTuple_Size(argv
);
3175 getitem
= PyTuple_GetItem
;
3178 PyErr_SetString(PyExc_TypeError
, "execv() arg 2 must be a tuple or list");
3179 release_bytes(opath
);
3183 PyErr_SetString(PyExc_ValueError
, "execv() arg 2 must not be empty");
3184 release_bytes(opath
);
3188 argvlist
= PyMem_NEW(char *, argc
+1);
3189 if (argvlist
== NULL
) {
3190 release_bytes(opath
);
3191 return PyErr_NoMemory();
3193 for (i
= 0; i
< argc
; i
++) {
3194 if (!fsconvert_strdup((*getitem
)(argv
, i
),
3196 free_string_array(argvlist
, i
);
3197 PyErr_SetString(PyExc_TypeError
,
3198 "execv() arg 2 must contain only strings");
3199 release_bytes(opath
);
3204 argvlist
[argc
] = NULL
;
3206 execv(path
, argvlist
);
3208 /* If we get here it's definitely an error */
3210 free_string_array(argvlist
, argc
);
3211 release_bytes(opath
);
3212 return posix_error();
3216 PyDoc_STRVAR(posix_execve__doc__
,
3217 "execve(path, args, env)\n\n\
3218 Execute a path with arguments and environment, replacing current process.\n\
3220 path: path of executable file\n\
3221 args: tuple or list of arguments\n\
3222 env: dictionary of strings mapping to strings");
3225 posix_execve(PyObject
*self
, PyObject
*args
)
3229 PyObject
*argv
, *env
;
3232 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
;
3233 Py_ssize_t i
, pos
, argc
, envc
;
3234 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3235 Py_ssize_t lastarg
= 0;
3237 /* execve has three arguments: (path, argv, env), where
3238 argv is a list or tuple of strings and env is a dictionary
3239 like posix.environ. */
3241 if (!PyArg_ParseTuple(args
, "O&OO:execve",
3242 PyUnicode_FSConverter
,
3243 &opath
, &argv
, &env
))
3245 path
= bytes2str(opath
, 1);
3246 if (PyList_Check(argv
)) {
3247 argc
= PyList_Size(argv
);
3248 getitem
= PyList_GetItem
;
3250 else if (PyTuple_Check(argv
)) {
3251 argc
= PyTuple_Size(argv
);
3252 getitem
= PyTuple_GetItem
;
3255 PyErr_SetString(PyExc_TypeError
,
3256 "execve() arg 2 must be a tuple or list");
3259 if (!PyMapping_Check(env
)) {
3260 PyErr_SetString(PyExc_TypeError
,
3261 "execve() arg 3 must be a mapping object");
3265 argvlist
= PyMem_NEW(char *, argc
+1);
3266 if (argvlist
== NULL
) {
3270 for (i
= 0; i
< argc
; i
++) {
3271 if (!fsconvert_strdup((*getitem
)(argv
, i
),
3279 argvlist
[argc
] = NULL
;
3281 i
= PyMapping_Size(env
);
3284 envlist
= PyMem_NEW(char *, i
+ 1);
3285 if (envlist
== NULL
) {
3290 keys
= PyMapping_Keys(env
);
3291 vals
= PyMapping_Values(env
);
3294 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
3295 PyErr_SetString(PyExc_TypeError
,
3296 "execve(): env.keys() or env.values() is not a list");
3300 for (pos
= 0; pos
< i
; pos
++) {
3304 key
= PyList_GetItem(keys
, pos
);
3305 val
= PyList_GetItem(vals
, pos
);
3311 "s;execve() arg 3 contains a non-string key",
3315 "s;execve() arg 3 contains a non-string value",
3321 #if defined(PYOS_OS2)
3322 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3323 if (stricmp(k
, "BEGINLIBPATH") != 0 && stricmp(k
, "ENDLIBPATH") != 0) {
3325 len
= PyUnicode_GetSize(key
) + PyUnicode_GetSize(val
) + 2;
3326 p
= PyMem_NEW(char, len
);
3331 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
3332 envlist
[envc
++] = p
;
3333 #if defined(PYOS_OS2)
3339 execve(path
, argvlist
, envlist
);
3341 /* If we get here it's definitely an error */
3343 (void) posix_error();
3347 PyMem_DEL(envlist
[envc
]);
3350 free_string_array(argvlist
, lastarg
);
3354 release_bytes(opath
);
3357 #endif /* HAVE_EXECV */
3361 PyDoc_STRVAR(posix_spawnv__doc__
,
3362 "spawnv(mode, path, args)\n\n\
3363 Execute the program 'path' in a new process.\n\
3365 mode: mode of process creation\n\
3366 path: path of executable file\n\
3367 args: tuple or list of strings");
3370 posix_spawnv(PyObject
*self
, PyObject
*args
)
3378 Py_intptr_t spawnval
;
3379 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3381 /* spawnv has three arguments: (mode, path, argv), where
3382 argv is a list or tuple of strings. */
3384 if (!PyArg_ParseTuple(args
, "iO&O:spawnv", &mode
,
3385 PyUnicode_FSConverter
,
3388 path
= bytes2str(opath
, 1);
3389 if (PyList_Check(argv
)) {
3390 argc
= PyList_Size(argv
);
3391 getitem
= PyList_GetItem
;
3393 else if (PyTuple_Check(argv
)) {
3394 argc
= PyTuple_Size(argv
);
3395 getitem
= PyTuple_GetItem
;
3398 PyErr_SetString(PyExc_TypeError
,
3399 "spawnv() arg 2 must be a tuple or list");
3400 release_bytes(opath
);
3404 argvlist
= PyMem_NEW(char *, argc
+1);
3405 if (argvlist
== NULL
) {
3406 release_bytes(opath
);
3407 return PyErr_NoMemory();
3409 for (i
= 0; i
< argc
; i
++) {
3410 if (!fsconvert_strdup((*getitem
)(argv
, i
),
3412 free_string_array(argvlist
, i
);
3415 "spawnv() arg 2 must contain only strings");
3416 release_bytes(opath
);
3420 argvlist
[argc
] = NULL
;
3422 #if defined(PYOS_OS2) && defined(PYCC_GCC)
3423 Py_BEGIN_ALLOW_THREADS
3424 spawnval
= spawnv(mode
, path
, argvlist
);
3425 Py_END_ALLOW_THREADS
3427 if (mode
== _OLD_P_OVERLAY
)
3430 Py_BEGIN_ALLOW_THREADS
3431 spawnval
= _spawnv(mode
, path
, argvlist
);
3432 Py_END_ALLOW_THREADS
3435 free_string_array(argvlist
, argc
);
3436 release_bytes(opath
);
3439 return posix_error();
3441 #if SIZEOF_LONG == SIZEOF_VOID_P
3442 return Py_BuildValue("l", (long) spawnval
);
3444 return Py_BuildValue("L", (PY_LONG_LONG
) spawnval
);
3449 PyDoc_STRVAR(posix_spawnve__doc__
,
3450 "spawnve(mode, path, args, env)\n\n\
3451 Execute the program 'path' in a new process.\n\
3453 mode: mode of process creation\n\
3454 path: path of executable file\n\
3455 args: tuple or list of arguments\n\
3456 env: dictionary of strings mapping to strings");
3459 posix_spawnve(PyObject
*self
, PyObject
*args
)
3463 PyObject
*argv
, *env
;
3466 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
, *res
=NULL
;
3467 int mode
, pos
, envc
;
3469 Py_intptr_t spawnval
;
3470 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3471 Py_ssize_t lastarg
= 0;
3473 /* spawnve has four arguments: (mode, path, argv, env), where
3474 argv is a list or tuple of strings and env is a dictionary
3475 like posix.environ. */
3477 if (!PyArg_ParseTuple(args
, "iO&OO:spawnve", &mode
,
3478 PyUnicode_FSConverter
,
3479 &opath
, &argv
, &env
))
3481 path
= bytes2str(opath
, 1);
3482 if (PyList_Check(argv
)) {
3483 argc
= PyList_Size(argv
);
3484 getitem
= PyList_GetItem
;
3486 else if (PyTuple_Check(argv
)) {
3487 argc
= PyTuple_Size(argv
);
3488 getitem
= PyTuple_GetItem
;
3491 PyErr_SetString(PyExc_TypeError
,
3492 "spawnve() arg 2 must be a tuple or list");
3495 if (!PyMapping_Check(env
)) {
3496 PyErr_SetString(PyExc_TypeError
,
3497 "spawnve() arg 3 must be a mapping object");
3501 argvlist
= PyMem_NEW(char *, argc
+1);
3502 if (argvlist
== NULL
) {
3506 for (i
= 0; i
< argc
; i
++) {
3507 if (!fsconvert_strdup((*getitem
)(argv
, i
),
3515 argvlist
[argc
] = NULL
;
3517 i
= PyMapping_Size(env
);
3520 envlist
= PyMem_NEW(char *, i
+ 1);
3521 if (envlist
== NULL
) {
3526 keys
= PyMapping_Keys(env
);
3527 vals
= PyMapping_Values(env
);
3530 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
3531 PyErr_SetString(PyExc_TypeError
,
3532 "spawnve(): env.keys() or env.values() is not a list");
3536 for (pos
= 0; pos
< i
; pos
++) {
3540 key
= PyList_GetItem(keys
, pos
);
3541 val
= PyList_GetItem(vals
, pos
);
3547 "s;spawnve() arg 3 contains a non-string key",
3551 "s;spawnve() arg 3 contains a non-string value",
3556 len
= PyUnicode_GetSize(key
) + PyUnicode_GetSize(val
) + 2;
3557 p
= PyMem_NEW(char, len
);
3562 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
3563 envlist
[envc
++] = p
;
3567 #if defined(PYOS_OS2) && defined(PYCC_GCC)
3568 Py_BEGIN_ALLOW_THREADS
3569 spawnval
= spawnve(mode
, path
, argvlist
, envlist
);
3570 Py_END_ALLOW_THREADS
3572 if (mode
== _OLD_P_OVERLAY
)
3575 Py_BEGIN_ALLOW_THREADS
3576 spawnval
= _spawnve(mode
, path
, argvlist
, envlist
);
3577 Py_END_ALLOW_THREADS
3581 (void) posix_error();
3583 #if SIZEOF_LONG == SIZEOF_VOID_P
3584 res
= Py_BuildValue("l", (long) spawnval
);
3586 res
= Py_BuildValue("L", (PY_LONG_LONG
) spawnval
);
3591 PyMem_DEL(envlist
[envc
]);
3594 free_string_array(argvlist
, lastarg
);
3598 release_bytes(opath
);
3602 /* OS/2 supports spawnvp & spawnvpe natively */
3603 #if defined(PYOS_OS2)
3604 PyDoc_STRVAR(posix_spawnvp__doc__
,
3605 "spawnvp(mode, file, args)\n\n\
3606 Execute the program 'file' in a new process, using the environment\n\
3607 search path to find the file.\n\
3609 mode: mode of process creation\n\
3610 file: executable file name\n\
3611 args: tuple or list of strings");
3614 posix_spawnvp(PyObject
*self
, PyObject
*args
)
3621 Py_intptr_t spawnval
;
3622 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3624 /* spawnvp has three arguments: (mode, path, argv), where
3625 argv is a list or tuple of strings. */
3627 if (!PyArg_ParseTuple(args
, "iO&O:spawnvp", &mode
,
3628 PyUnicode_FSConverter
,
3631 path
= bytes2str(opath
);
3632 if (PyList_Check(argv
)) {
3633 argc
= PyList_Size(argv
);
3634 getitem
= PyList_GetItem
;
3636 else if (PyTuple_Check(argv
)) {
3637 argc
= PyTuple_Size(argv
);
3638 getitem
= PyTuple_GetItem
;
3641 PyErr_SetString(PyExc_TypeError
,
3642 "spawnvp() arg 2 must be a tuple or list");
3643 release_bytes(opath
);
3647 argvlist
= PyMem_NEW(char *, argc
+1);
3648 if (argvlist
== NULL
) {
3649 release_bytes(opath
);
3650 return PyErr_NoMemory();
3652 for (i
= 0; i
< argc
; i
++) {
3653 if (!fsconvert_strdup((*getitem
)(argv
, i
),
3655 free_string_array(argvlist
, i
);
3658 "spawnvp() arg 2 must contain only strings");
3659 release_bytes(opath
);
3663 argvlist
[argc
] = NULL
;
3665 Py_BEGIN_ALLOW_THREADS
3666 #if defined(PYCC_GCC)
3667 spawnval
= spawnvp(mode
, path
, argvlist
);
3669 spawnval
= _spawnvp(mode
, path
, argvlist
);
3671 Py_END_ALLOW_THREADS
3673 free_string_array(argvlist
, argc
);
3674 release_bytes(opath
);
3677 return posix_error();
3679 return Py_BuildValue("l", (long) spawnval
);
3683 PyDoc_STRVAR(posix_spawnvpe__doc__
,
3684 "spawnvpe(mode, file, args, env)\n\n\
3685 Execute the program 'file' in a new process, using the environment\n\
3686 search path to find the file.\n\
3688 mode: mode of process creation\n\
3689 file: executable file name\n\
3690 args: tuple or list of arguments\n\
3691 env: dictionary of strings mapping to strings");
3694 posix_spawnvpe(PyObject
*self
, PyObject
*args
)
3698 PyObject
*argv
, *env
;
3701 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
, *res
=NULL
;
3702 int mode
, i
, pos
, argc
, envc
;
3703 Py_intptr_t spawnval
;
3704 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3707 /* spawnvpe has four arguments: (mode, path, argv, env), where
3708 argv is a list or tuple of strings and env is a dictionary
3709 like posix.environ. */
3711 if (!PyArg_ParseTuple(args
, "ietOO:spawnvpe", &mode
,
3712 PyUnicode_FSConverter
,
3713 &opath
, &argv
, &env
))
3715 path
= bytes2str(opath
);
3716 if (PyList_Check(argv
)) {
3717 argc
= PyList_Size(argv
);
3718 getitem
= PyList_GetItem
;
3720 else if (PyTuple_Check(argv
)) {
3721 argc
= PyTuple_Size(argv
);
3722 getitem
= PyTuple_GetItem
;
3725 PyErr_SetString(PyExc_TypeError
,
3726 "spawnvpe() arg 2 must be a tuple or list");
3729 if (!PyMapping_Check(env
)) {
3730 PyErr_SetString(PyExc_TypeError
,
3731 "spawnvpe() arg 3 must be a mapping object");
3735 argvlist
= PyMem_NEW(char *, argc
+1);
3736 if (argvlist
== NULL
) {
3740 for (i
= 0; i
< argc
; i
++) {
3741 if (!fsconvert_strdup((*getitem
)(argv
, i
),
3749 argvlist
[argc
] = NULL
;
3751 i
= PyMapping_Size(env
);
3754 envlist
= PyMem_NEW(char *, i
+ 1);
3755 if (envlist
== NULL
) {
3760 keys
= PyMapping_Keys(env
);
3761 vals
= PyMapping_Values(env
);
3764 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
3765 PyErr_SetString(PyExc_TypeError
,
3766 "spawnvpe(): env.keys() or env.values() is not a list");
3770 for (pos
= 0; pos
< i
; pos
++) {
3774 key
= PyList_GetItem(keys
, pos
);
3775 val
= PyList_GetItem(vals
, pos
);
3781 "s;spawnvpe() arg 3 contains a non-string key",
3785 "s;spawnvpe() arg 3 contains a non-string value",
3790 len
= PyUnicode_GetSize(key
) + PyUnicode_GetSize(val
) + 2;
3791 p
= PyMem_NEW(char, len
);
3796 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
3797 envlist
[envc
++] = p
;
3801 Py_BEGIN_ALLOW_THREADS
3802 #if defined(PYCC_GCC)
3803 spawnval
= spawnvpe(mode
, path
, argvlist
, envlist
);
3805 spawnval
= _spawnvpe(mode
, path
, argvlist
, envlist
);
3807 Py_END_ALLOW_THREADS
3810 (void) posix_error();
3812 res
= Py_BuildValue("l", (long) spawnval
);
3816 PyMem_DEL(envlist
[envc
]);
3819 free_string_array(argvlist
, lastarg
);
3823 release_bytes(opath
);
3826 #endif /* PYOS_OS2 */
3827 #endif /* HAVE_SPAWNV */
3831 PyDoc_STRVAR(posix_fork1__doc__
,
3832 "fork1() -> pid\n\n\
3833 Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3835 Return 0 to child process and PID of child to parent process.");
3838 posix_fork1(PyObject
*self
, PyObject
*noargs
)
3842 _PyImport_AcquireLock();
3844 result
= _PyImport_ReleaseLock();
3846 return posix_error();
3850 /* Don't clobber the OSError if the fork failed. */
3851 PyErr_SetString(PyExc_RuntimeError
,
3852 "not holding the import lock");
3855 return PyLong_FromPid(pid
);
3861 PyDoc_STRVAR(posix_fork__doc__
,
3863 Fork a child process.\n\
3864 Return 0 to child process and PID of child to parent process.");
3867 posix_fork(PyObject
*self
, PyObject
*noargs
)
3871 _PyImport_AcquireLock();
3873 result
= _PyImport_ReleaseLock();
3875 return posix_error();
3879 /* Don't clobber the OSError if the fork failed. */
3880 PyErr_SetString(PyExc_RuntimeError
,
3881 "not holding the import lock");
3884 return PyLong_FromPid(pid
);
3888 /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
3889 /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3890 #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
3891 #define DEV_PTY_FILE "/dev/ptc"
3892 #define HAVE_DEV_PTMX
3894 #define DEV_PTY_FILE "/dev/ptmx"
3897 #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
3901 #ifdef HAVE_LIBUTIL_H
3902 #include <libutil.h>
3903 #endif /* HAVE_LIBUTIL_H */
3904 #endif /* HAVE_PTY_H */
3905 #ifdef HAVE_STROPTS_H
3906 #include <stropts.h>
3908 #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
3910 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
3911 PyDoc_STRVAR(posix_openpty__doc__
,
3912 "openpty() -> (master_fd, slave_fd)\n\n\
3913 Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
3916 posix_openpty(PyObject
*self
, PyObject
*noargs
)
3918 int master_fd
, slave_fd
;
3919 #ifndef HAVE_OPENPTY
3922 #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
3923 PyOS_sighandler_t sig_saved
;
3925 extern char *ptsname(int fildes
);
3930 if (openpty(&master_fd
, &slave_fd
, NULL
, NULL
, NULL
) != 0)
3931 return posix_error();
3932 #elif defined(HAVE__GETPTY)
3933 slave_name
= _getpty(&master_fd
, O_RDWR
, 0666, 0);
3934 if (slave_name
== NULL
)
3935 return posix_error();
3937 slave_fd
= open(slave_name
, O_RDWR
);
3939 return posix_error();
3941 master_fd
= open(DEV_PTY_FILE
, O_RDWR
| O_NOCTTY
); /* open master */
3943 return posix_error();
3944 sig_saved
= PyOS_setsig(SIGCHLD
, SIG_DFL
);
3945 /* change permission of slave */
3946 if (grantpt(master_fd
) < 0) {
3947 PyOS_setsig(SIGCHLD
, sig_saved
);
3948 return posix_error();
3951 if (unlockpt(master_fd
) < 0) {
3952 PyOS_setsig(SIGCHLD
, sig_saved
);
3953 return posix_error();
3955 PyOS_setsig(SIGCHLD
, sig_saved
);
3956 slave_name
= ptsname(master_fd
); /* get name of slave */
3957 if (slave_name
== NULL
)
3958 return posix_error();
3959 slave_fd
= open(slave_name
, O_RDWR
| O_NOCTTY
); /* open slave */
3961 return posix_error();
3962 #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
3963 ioctl(slave_fd
, I_PUSH
, "ptem"); /* push ptem */
3964 ioctl(slave_fd
, I_PUSH
, "ldterm"); /* push ldterm */
3966 ioctl(slave_fd
, I_PUSH
, "ttcompat"); /* push ttcompat */
3968 #endif /* HAVE_CYGWIN */
3969 #endif /* HAVE_OPENPTY */
3971 return Py_BuildValue("(ii)", master_fd
, slave_fd
);
3974 #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
3977 PyDoc_STRVAR(posix_forkpty__doc__
,
3978 "forkpty() -> (pid, master_fd)\n\n\
3979 Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3980 Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
3981 To both, return fd of newly opened pseudo-terminal.\n");
3984 posix_forkpty(PyObject
*self
, PyObject
*noargs
)
3986 int master_fd
= -1, result
;
3989 _PyImport_AcquireLock();
3990 pid
= forkpty(&master_fd
, NULL
, NULL
, NULL
);
3991 result
= _PyImport_ReleaseLock();
3993 return posix_error();
3997 /* Don't clobber the OSError if the fork failed. */
3998 PyErr_SetString(PyExc_RuntimeError
,
3999 "not holding the import lock");
4002 return Py_BuildValue("(Ni)", PyLong_FromPid(pid
), master_fd
);
4007 PyDoc_STRVAR(posix_getegid__doc__
,
4008 "getegid() -> egid\n\n\
4009 Return the current process's effective group id.");
4012 posix_getegid(PyObject
*self
, PyObject
*noargs
)
4014 return PyLong_FromLong((long)getegid());
4020 PyDoc_STRVAR(posix_geteuid__doc__
,
4021 "geteuid() -> euid\n\n\
4022 Return the current process's effective user id.");
4025 posix_geteuid(PyObject
*self
, PyObject
*noargs
)
4027 return PyLong_FromLong((long)geteuid());
4033 PyDoc_STRVAR(posix_getgid__doc__
,
4034 "getgid() -> gid\n\n\
4035 Return the current process's group id.");
4038 posix_getgid(PyObject
*self
, PyObject
*noargs
)
4040 return PyLong_FromLong((long)getgid());
4045 PyDoc_STRVAR(posix_getpid__doc__
,
4046 "getpid() -> pid\n\n\
4047 Return the current process id");
4050 posix_getpid(PyObject
*self
, PyObject
*noargs
)
4052 return PyLong_FromPid(getpid());
4056 #ifdef HAVE_GETGROUPS
4057 PyDoc_STRVAR(posix_getgroups__doc__
,
4058 "getgroups() -> list of group IDs\n\n\
4059 Return list of supplemental group IDs for the process.");
4062 posix_getgroups(PyObject
*self
, PyObject
*noargs
)
4064 PyObject
*result
= NULL
;
4067 #define MAX_GROUPS NGROUPS_MAX
4069 /* defined to be 16 on Solaris7, so this should be a small number */
4070 #define MAX_GROUPS 64
4072 gid_t grouplist
[MAX_GROUPS
];
4075 n
= getgroups(MAX_GROUPS
, grouplist
);
4079 result
= PyList_New(n
);
4080 if (result
!= NULL
) {
4082 for (i
= 0; i
< n
; ++i
) {
4083 PyObject
*o
= PyLong_FromLong((long)grouplist
[i
]);
4089 PyList_SET_ITEM(result
, i
, o
);
4099 PyDoc_STRVAR(posix_getpgid__doc__
,
4100 "getpgid(pid) -> pgid\n\n\
4101 Call the system call getpgid().");
4104 posix_getpgid(PyObject
*self
, PyObject
*args
)
4107 if (!PyArg_ParseTuple(args
, PARSE_PID
":getpgid", &pid
))
4109 pgid
= getpgid(pid
);
4111 return posix_error();
4112 return PyLong_FromPid(pgid
);
4114 #endif /* HAVE_GETPGID */
4118 PyDoc_STRVAR(posix_getpgrp__doc__
,
4119 "getpgrp() -> pgrp\n\n\
4120 Return the current process group id.");
4123 posix_getpgrp(PyObject
*self
, PyObject
*noargs
)
4125 #ifdef GETPGRP_HAVE_ARG
4126 return PyLong_FromPid(getpgrp(0));
4127 #else /* GETPGRP_HAVE_ARG */
4128 return PyLong_FromPid(getpgrp());
4129 #endif /* GETPGRP_HAVE_ARG */
4131 #endif /* HAVE_GETPGRP */
4135 PyDoc_STRVAR(posix_setpgrp__doc__
,
4137 Make this process a session leader.");
4140 posix_setpgrp(PyObject
*self
, PyObject
*noargs
)
4142 #ifdef SETPGRP_HAVE_ARG
4143 if (setpgrp(0, 0) < 0)
4144 #else /* SETPGRP_HAVE_ARG */
4146 #endif /* SETPGRP_HAVE_ARG */
4147 return posix_error();
4152 #endif /* HAVE_SETPGRP */
4155 PyDoc_STRVAR(posix_getppid__doc__
,
4156 "getppid() -> ppid\n\n\
4157 Return the parent's process id.");
4160 posix_getppid(PyObject
*self
, PyObject
*noargs
)
4162 return PyLong_FromPid(getppid());
4167 #ifdef HAVE_GETLOGIN
4168 PyDoc_STRVAR(posix_getlogin__doc__
,
4169 "getlogin() -> string\n\n\
4170 Return the actual login name.");
4173 posix_getlogin(PyObject
*self
, PyObject
*noargs
)
4175 PyObject
*result
= NULL
;
4177 int old_errno
= errno
;
4185 PyErr_SetString(PyExc_OSError
,
4186 "unable to determine login name");
4189 result
= PyUnicode_FromString(name
);
4197 PyDoc_STRVAR(posix_getuid__doc__
,
4198 "getuid() -> uid\n\n\
4199 Return the current process's user id.");
4202 posix_getuid(PyObject
*self
, PyObject
*noargs
)
4204 return PyLong_FromLong((long)getuid());
4210 PyDoc_STRVAR(posix_kill__doc__
,
4211 "kill(pid, sig)\n\n\
4212 Kill a process with a signal.");
4215 posix_kill(PyObject
*self
, PyObject
*args
)
4219 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:kill", &pid
, &sig
))
4221 #if defined(PYOS_OS2) && !defined(PYCC_GCC)
4222 if (sig
== XCPT_SIGNAL_INTR
|| sig
== XCPT_SIGNAL_BREAK
) {
4224 if ((rc
= DosSendSignalException(pid
, sig
)) != NO_ERROR
)
4225 return os2_error(rc
);
4227 } else if (sig
== XCPT_SIGNAL_KILLPROC
) {
4229 if ((rc
= DosKillProcess(DKP_PROCESS
, pid
)) != NO_ERROR
)
4230 return os2_error(rc
);
4233 return NULL
; /* Unrecognized Signal Requested */
4235 if (kill(pid
, sig
) == -1)
4236 return posix_error();
4244 PyDoc_STRVAR(posix_killpg__doc__
,
4245 "killpg(pgid, sig)\n\n\
4246 Kill a process group with a signal.");
4249 posix_killpg(PyObject
*self
, PyObject
*args
)
4253 /* XXX some man pages make the `pgid` parameter an int, others
4254 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4255 take the same type. Moreover, pid_t is always at least as wide as
4256 int (else compilation of this module fails), which is safe. */
4257 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:killpg", &pgid
, &sig
))
4259 if (killpg(pgid
, sig
) == -1)
4260 return posix_error();
4268 #ifdef HAVE_SYS_LOCK_H
4269 #include <sys/lock.h>
4272 PyDoc_STRVAR(posix_plock__doc__
,
4274 Lock program segments into memory.");
4277 posix_plock(PyObject
*self
, PyObject
*args
)
4280 if (!PyArg_ParseTuple(args
, "i:plock", &op
))
4282 if (plock(op
) == -1)
4283 return posix_error();
4290 PyDoc_STRVAR(posix_setuid__doc__
,
4292 Set the current process's user id.");
4295 posix_setuid(PyObject
*self
, PyObject
*args
)
4299 if (!PyArg_ParseTuple(args
, "l:setuid", &uid_arg
))
4302 if (uid
!= uid_arg
) {
4303 PyErr_SetString(PyExc_OverflowError
, "user id too big");
4306 if (setuid(uid
) < 0)
4307 return posix_error();
4311 #endif /* HAVE_SETUID */
4315 PyDoc_STRVAR(posix_seteuid__doc__
,
4317 Set the current process's effective user id.");
4320 posix_seteuid (PyObject
*self
, PyObject
*args
)
4324 if (!PyArg_ParseTuple(args
, "l", &euid_arg
))
4327 if (euid
!= euid_arg
) {
4328 PyErr_SetString(PyExc_OverflowError
, "user id too big");
4331 if (seteuid(euid
) < 0) {
4332 return posix_error();
4338 #endif /* HAVE_SETEUID */
4341 PyDoc_STRVAR(posix_setegid__doc__
,
4343 Set the current process's effective group id.");
4346 posix_setegid (PyObject
*self
, PyObject
*args
)
4350 if (!PyArg_ParseTuple(args
, "l", &egid_arg
))
4353 if (egid
!= egid_arg
) {
4354 PyErr_SetString(PyExc_OverflowError
, "group id too big");
4357 if (setegid(egid
) < 0) {
4358 return posix_error();
4364 #endif /* HAVE_SETEGID */
4366 #ifdef HAVE_SETREUID
4367 PyDoc_STRVAR(posix_setreuid__doc__
,
4368 "setreuid(ruid, euid)\n\n\
4369 Set the current process's real and effective user ids.");
4372 posix_setreuid (PyObject
*self
, PyObject
*args
)
4374 long ruid_arg
, euid_arg
;
4376 if (!PyArg_ParseTuple(args
, "ll", &ruid_arg
, &euid_arg
))
4379 ruid
= (uid_t
)-1; /* let the compiler choose how -1 fits */
4381 ruid
= ruid_arg
; /* otherwise, assign from our long */
4386 if ((euid_arg
!= -1 && euid
!= euid_arg
) ||
4387 (ruid_arg
!= -1 && ruid
!= ruid_arg
)) {
4388 PyErr_SetString(PyExc_OverflowError
, "user id too big");
4391 if (setreuid(ruid
, euid
) < 0) {
4392 return posix_error();
4398 #endif /* HAVE_SETREUID */
4400 #ifdef HAVE_SETREGID
4401 PyDoc_STRVAR(posix_setregid__doc__
,
4402 "setregid(rgid, egid)\n\n\
4403 Set the current process's real and effective group ids.");
4406 posix_setregid (PyObject
*self
, PyObject
*args
)
4408 long rgid_arg
, egid_arg
;
4410 if (!PyArg_ParseTuple(args
, "ll", &rgid_arg
, &egid_arg
))
4413 rgid
= (gid_t
)-1; /* let the compiler choose how -1 fits */
4415 rgid
= rgid_arg
; /* otherwise, assign from our long */
4420 if ((egid_arg
!= -1 && egid
!= egid_arg
) ||
4421 (rgid_arg
!= -1 && rgid
!= rgid_arg
)) {
4422 PyErr_SetString(PyExc_OverflowError
, "group id too big");
4425 if (setregid(rgid
, egid
) < 0) {
4426 return posix_error();
4432 #endif /* HAVE_SETREGID */
4435 PyDoc_STRVAR(posix_setgid__doc__
,
4437 Set the current process's group id.");
4440 posix_setgid(PyObject
*self
, PyObject
*args
)
4444 if (!PyArg_ParseTuple(args
, "l:setgid", &gid_arg
))
4447 if (gid
!= gid_arg
) {
4448 PyErr_SetString(PyExc_OverflowError
, "group id too big");
4451 if (setgid(gid
) < 0)
4452 return posix_error();
4456 #endif /* HAVE_SETGID */
4458 #ifdef HAVE_SETGROUPS
4459 PyDoc_STRVAR(posix_setgroups__doc__
,
4460 "setgroups(list)\n\n\
4461 Set the groups of the current process to list.");
4464 posix_setgroups(PyObject
*self
, PyObject
*groups
)
4467 gid_t grouplist
[MAX_GROUPS
];
4469 if (!PySequence_Check(groups
)) {
4470 PyErr_SetString(PyExc_TypeError
, "setgroups argument must be a sequence");
4473 len
= PySequence_Size(groups
);
4474 if (len
> MAX_GROUPS
) {
4475 PyErr_SetString(PyExc_ValueError
, "too many groups");
4478 for(i
= 0; i
< len
; i
++) {
4480 elem
= PySequence_GetItem(groups
, i
);
4483 if (!PyLong_Check(elem
)) {
4484 PyErr_SetString(PyExc_TypeError
,
4485 "groups must be integers");
4489 unsigned long x
= PyLong_AsUnsignedLong(elem
);
4490 if (PyErr_Occurred()) {
4491 PyErr_SetString(PyExc_TypeError
,
4492 "group id too big");
4497 /* read back the value to see if it fitted in gid_t */
4498 if (grouplist
[i
] != x
) {
4499 PyErr_SetString(PyExc_TypeError
,
4500 "group id too big");
4508 if (setgroups(len
, grouplist
) < 0)
4509 return posix_error();
4513 #endif /* HAVE_SETGROUPS */
4515 #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4517 wait_helper(pid_t pid
, int status
, struct rusage
*ru
)
4520 static PyObject
*struct_rusage
;
4523 return posix_error();
4525 if (struct_rusage
== NULL
) {
4526 PyObject
*m
= PyImport_ImportModuleNoBlock("resource");
4529 struct_rusage
= PyObject_GetAttrString(m
, "struct_rusage");
4531 if (struct_rusage
== NULL
)
4535 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4536 result
= PyStructSequence_New((PyTypeObject
*) struct_rusage
);
4541 #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4544 PyStructSequence_SET_ITEM(result
, 0,
4545 PyFloat_FromDouble(doubletime(ru
->ru_utime
)));
4546 PyStructSequence_SET_ITEM(result
, 1,
4547 PyFloat_FromDouble(doubletime(ru
->ru_stime
)));
4548 #define SET_INT(result, index, value)\
4549 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4550 SET_INT(result
, 2, ru
->ru_maxrss
);
4551 SET_INT(result
, 3, ru
->ru_ixrss
);
4552 SET_INT(result
, 4, ru
->ru_idrss
);
4553 SET_INT(result
, 5, ru
->ru_isrss
);
4554 SET_INT(result
, 6, ru
->ru_minflt
);
4555 SET_INT(result
, 7, ru
->ru_majflt
);
4556 SET_INT(result
, 8, ru
->ru_nswap
);
4557 SET_INT(result
, 9, ru
->ru_inblock
);
4558 SET_INT(result
, 10, ru
->ru_oublock
);
4559 SET_INT(result
, 11, ru
->ru_msgsnd
);
4560 SET_INT(result
, 12, ru
->ru_msgrcv
);
4561 SET_INT(result
, 13, ru
->ru_nsignals
);
4562 SET_INT(result
, 14, ru
->ru_nvcsw
);
4563 SET_INT(result
, 15, ru
->ru_nivcsw
);
4566 if (PyErr_Occurred()) {
4571 return Py_BuildValue("NiN", PyLong_FromPid(pid
), status
, result
);
4573 #endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4576 PyDoc_STRVAR(posix_wait3__doc__
,
4577 "wait3(options) -> (pid, status, rusage)\n\n\
4578 Wait for completion of a child process.");
4581 posix_wait3(PyObject
*self
, PyObject
*args
)
4587 WAIT_STATUS_INT(status
) = 0;
4589 if (!PyArg_ParseTuple(args
, "i:wait3", &options
))
4592 Py_BEGIN_ALLOW_THREADS
4593 pid
= wait3(&status
, options
, &ru
);
4594 Py_END_ALLOW_THREADS
4596 return wait_helper(pid
, WAIT_STATUS_INT(status
), &ru
);
4598 #endif /* HAVE_WAIT3 */
4601 PyDoc_STRVAR(posix_wait4__doc__
,
4602 "wait4(pid, options) -> (pid, status, rusage)\n\n\
4603 Wait for completion of a given child process.");
4606 posix_wait4(PyObject
*self
, PyObject
*args
)
4612 WAIT_STATUS_INT(status
) = 0;
4614 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:wait4", &pid
, &options
))
4617 Py_BEGIN_ALLOW_THREADS
4618 pid
= wait4(pid
, &status
, options
, &ru
);
4619 Py_END_ALLOW_THREADS
4621 return wait_helper(pid
, WAIT_STATUS_INT(status
), &ru
);
4623 #endif /* HAVE_WAIT4 */
4626 PyDoc_STRVAR(posix_waitpid__doc__
,
4627 "waitpid(pid, options) -> (pid, status)\n\n\
4628 Wait for completion of a given child process.");
4631 posix_waitpid(PyObject
*self
, PyObject
*args
)
4636 WAIT_STATUS_INT(status
) = 0;
4638 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:waitpid", &pid
, &options
))
4640 Py_BEGIN_ALLOW_THREADS
4641 pid
= waitpid(pid
, &status
, options
);
4642 Py_END_ALLOW_THREADS
4644 return posix_error();
4646 return Py_BuildValue("Ni", PyLong_FromPid(pid
), WAIT_STATUS_INT(status
));
4649 #elif defined(HAVE_CWAIT)
4651 /* MS C has a variant of waitpid() that's usable for most purposes. */
4652 PyDoc_STRVAR(posix_waitpid__doc__
,
4653 "waitpid(pid, options) -> (pid, status << 8)\n\n"
4654 "Wait for completion of a given process. options is ignored on Windows.");
4657 posix_waitpid(PyObject
*self
, PyObject
*args
)
4660 int status
, options
;
4662 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:waitpid", &pid
, &options
))
4664 Py_BEGIN_ALLOW_THREADS
4665 pid
= _cwait(&status
, pid
, options
);
4666 Py_END_ALLOW_THREADS
4668 return posix_error();
4670 /* shift the status left a byte so this is more like the POSIX waitpid */
4671 return Py_BuildValue("Ni", PyLong_FromPid(pid
), status
<< 8);
4673 #endif /* HAVE_WAITPID || HAVE_CWAIT */
4676 PyDoc_STRVAR(posix_wait__doc__
,
4677 "wait() -> (pid, status)\n\n\
4678 Wait for completion of a child process.");
4681 posix_wait(PyObject
*self
, PyObject
*noargs
)
4685 WAIT_STATUS_INT(status
) = 0;
4687 Py_BEGIN_ALLOW_THREADS
4688 pid
= wait(&status
);
4689 Py_END_ALLOW_THREADS
4691 return posix_error();
4693 return Py_BuildValue("Ni", PyLong_FromPid(pid
), WAIT_STATUS_INT(status
));
4698 PyDoc_STRVAR(posix_lstat__doc__
,
4699 "lstat(path) -> stat result\n\n\
4700 Like stat(path), but do not follow symbolic links.");
4703 posix_lstat(PyObject
*self
, PyObject
*args
)
4706 return posix_do_stat(self
, args
, "O&:lstat", lstat
, NULL
, NULL
);
4707 #else /* !HAVE_LSTAT */
4709 return posix_do_stat(self
, args
, "O&:lstat", STAT
, "U:lstat", win32_wstat
);
4711 return posix_do_stat(self
, args
, "O&:lstat", STAT
, NULL
, NULL
);
4713 #endif /* !HAVE_LSTAT */
4717 #ifdef HAVE_READLINK
4718 PyDoc_STRVAR(posix_readlink__doc__
,
4719 "readlink(path) -> path\n\n\
4720 Return a string representing the path to which the symbolic link points.");
4723 posix_readlink(PyObject
*self
, PyObject
*args
)
4726 char buf
[MAXPATHLEN
];
4730 int arg_is_unicode
= 0;
4732 if (!PyArg_ParseTuple(args
, "O&:readlink",
4733 PyUnicode_FSConverter
, &opath
))
4735 path
= bytes2str(opath
, 1);
4736 v
= PySequence_GetItem(args
, 0);
4738 release_bytes(opath
);
4742 if (PyUnicode_Check(v
)) {
4747 Py_BEGIN_ALLOW_THREADS
4748 n
= readlink(path
, buf
, (int) sizeof buf
);
4749 Py_END_ALLOW_THREADS
4751 return posix_error_with_allocated_filename(opath
);
4753 release_bytes(opath
);
4754 v
= PyBytes_FromStringAndSize(buf
, n
);
4755 if (arg_is_unicode
) {
4758 w
= PyUnicode_FromEncodedObject(v
,
4759 Py_FileSystemDefaultEncoding
,
4771 #endif /* HAVE_READLINK */
4775 PyDoc_STRVAR(posix_symlink__doc__
,
4776 "symlink(src, dst)\n\n\
4777 Create a symbolic link pointing to src named dst.");
4780 posix_symlink(PyObject
*self
, PyObject
*args
)
4782 return posix_2str(args
, "O&O&:symlink", symlink
);
4784 #endif /* HAVE_SYMLINK */
4788 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
4794 Py_BEGIN_ALLOW_THREADS
4795 DosQuerySysInfo(QSV_MS_COUNT
, QSV_MS_COUNT
, &value
, sizeof(value
));
4796 Py_END_ALLOW_THREADS
4802 posix_times(PyObject
*self
, PyObject
*noargs
)
4804 /* Currently Only Uptime is Provided -- Others Later */
4805 return Py_BuildValue("ddddd",
4806 (double)0 /* t.tms_utime / HZ */,
4807 (double)0 /* t.tms_stime / HZ */,
4808 (double)0 /* t.tms_cutime / HZ */,
4809 (double)0 /* t.tms_cstime / HZ */,
4810 (double)system_uptime() / 1000);
4813 #define NEED_TICKS_PER_SECOND
4814 static long ticks_per_second
= -1;
4816 posix_times(PyObject
*self
, PyObject
*noargs
)
4822 if (c
== (clock_t) -1)
4823 return posix_error();
4824 return Py_BuildValue("ddddd",
4825 (double)t
.tms_utime
/ ticks_per_second
,
4826 (double)t
.tms_stime
/ ticks_per_second
,
4827 (double)t
.tms_cutime
/ ticks_per_second
,
4828 (double)t
.tms_cstime
/ ticks_per_second
,
4829 (double)c
/ ticks_per_second
);
4831 #endif /* not OS2 */
4832 #endif /* HAVE_TIMES */
4836 #define HAVE_TIMES /* so the method table will pick it up */
4838 posix_times(PyObject
*self
, PyObject
*noargs
)
4840 FILETIME create
, exit
, kernel
, user
;
4842 hProc
= GetCurrentProcess();
4843 GetProcessTimes(hProc
, &create
, &exit
, &kernel
, &user
);
4844 /* The fields of a FILETIME structure are the hi and lo part
4845 of a 64-bit value expressed in 100 nanosecond units.
4846 1e7 is one second in such units; 1e-7 the inverse.
4847 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4849 return Py_BuildValue(
4851 (double)(user
.dwHighDateTime
*429.4967296 +
4852 user
.dwLowDateTime
*1e-7),
4853 (double)(kernel
.dwHighDateTime
*429.4967296 +
4854 kernel
.dwLowDateTime
*1e-7),
4859 #endif /* MS_WINDOWS */
4862 PyDoc_STRVAR(posix_times__doc__
,
4863 "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
4864 Return a tuple of floating point numbers indicating process times.");
4869 PyDoc_STRVAR(posix_getsid__doc__
,
4870 "getsid(pid) -> sid\n\n\
4871 Call the system call getsid().");
4874 posix_getsid(PyObject
*self
, PyObject
*args
)
4878 if (!PyArg_ParseTuple(args
, PARSE_PID
":getsid", &pid
))
4882 return posix_error();
4883 return PyLong_FromLong((long)sid
);
4885 #endif /* HAVE_GETSID */
4889 PyDoc_STRVAR(posix_setsid__doc__
,
4891 Call the system call setsid().");
4894 posix_setsid(PyObject
*self
, PyObject
*noargs
)
4897 return posix_error();
4901 #endif /* HAVE_SETSID */
4904 PyDoc_STRVAR(posix_setpgid__doc__
,
4905 "setpgid(pid, pgrp)\n\n\
4906 Call the system call setpgid().");
4909 posix_setpgid(PyObject
*self
, PyObject
*args
)
4913 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:setpgid", &pid
, &pgrp
))
4915 if (setpgid(pid
, pgrp
) < 0)
4916 return posix_error();
4920 #endif /* HAVE_SETPGID */
4923 #ifdef HAVE_TCGETPGRP
4924 PyDoc_STRVAR(posix_tcgetpgrp__doc__
,
4925 "tcgetpgrp(fd) -> pgid\n\n\
4926 Return the process group associated with the terminal given by a fd.");
4929 posix_tcgetpgrp(PyObject
*self
, PyObject
*args
)
4933 if (!PyArg_ParseTuple(args
, "i:tcgetpgrp", &fd
))
4935 pgid
= tcgetpgrp(fd
);
4937 return posix_error();
4938 return PyLong_FromPid(pgid
);
4940 #endif /* HAVE_TCGETPGRP */
4943 #ifdef HAVE_TCSETPGRP
4944 PyDoc_STRVAR(posix_tcsetpgrp__doc__
,
4945 "tcsetpgrp(fd, pgid)\n\n\
4946 Set the process group associated with the terminal given by a fd.");
4949 posix_tcsetpgrp(PyObject
*self
, PyObject
*args
)
4953 if (!PyArg_ParseTuple(args
, "i" PARSE_PID
":tcsetpgrp", &fd
, &pgid
))
4955 if (tcsetpgrp(fd
, pgid
) < 0)
4956 return posix_error();
4960 #endif /* HAVE_TCSETPGRP */
4962 /* Functions acting on file descriptors */
4964 PyDoc_STRVAR(posix_open__doc__
,
4965 "open(filename, flag [, mode=0777]) -> fd\n\n\
4966 Open a file (for low level IO).");
4969 posix_open(PyObject
*self
, PyObject
*args
)
4978 if (unicode_file_names()) {
4979 PyUnicodeObject
*po
;
4980 if (PyArg_ParseTuple(args
, "Ui|i:mkdir", &po
, &flag
, &mode
)) {
4981 Py_BEGIN_ALLOW_THREADS
4982 /* PyUnicode_AS_UNICODE OK without thread
4983 lock as it is a simple dereference. */
4984 fd
= _wopen(PyUnicode_AS_UNICODE(po
), flag
, mode
);
4985 Py_END_ALLOW_THREADS
4987 return posix_error();
4988 return PyLong_FromLong((long)fd
);
4990 /* Drop the argument parsing error as narrow strings
4996 if (!PyArg_ParseTuple(args
, "O&i|i",
4997 PyUnicode_FSConverter
, &ofile
,
5000 file
= bytes2str(ofile
, 1);
5001 Py_BEGIN_ALLOW_THREADS
5002 fd
= open(file
, flag
, mode
);
5003 Py_END_ALLOW_THREADS
5005 return posix_error_with_allocated_filename(ofile
);
5006 release_bytes(ofile
);
5007 return PyLong_FromLong((long)fd
);
5011 PyDoc_STRVAR(posix_close__doc__
,
5013 Close a file descriptor (for low level IO).");
5016 posix_close(PyObject
*self
, PyObject
*args
)
5019 if (!PyArg_ParseTuple(args
, "i:close", &fd
))
5021 if (!_PyVerify_fd(fd
))
5022 return posix_error();
5023 Py_BEGIN_ALLOW_THREADS
5025 Py_END_ALLOW_THREADS
5027 return posix_error();
5033 PyDoc_STRVAR(posix_closerange__doc__
,
5034 "closerange(fd_low, fd_high)\n\n\
5035 Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5038 posix_closerange(PyObject
*self
, PyObject
*args
)
5040 int fd_from
, fd_to
, i
;
5041 if (!PyArg_ParseTuple(args
, "ii:closerange", &fd_from
, &fd_to
))
5043 Py_BEGIN_ALLOW_THREADS
5044 for (i
= fd_from
; i
< fd_to
; i
++)
5045 if (_PyVerify_fd(i
))
5047 Py_END_ALLOW_THREADS
5052 PyDoc_STRVAR(posix_dup__doc__
,
5053 "dup(fd) -> fd2\n\n\
5054 Return a duplicate of a file descriptor.");
5057 posix_dup(PyObject
*self
, PyObject
*args
)
5060 if (!PyArg_ParseTuple(args
, "i:dup", &fd
))
5062 if (!_PyVerify_fd(fd
))
5063 return posix_error();
5064 Py_BEGIN_ALLOW_THREADS
5066 Py_END_ALLOW_THREADS
5068 return posix_error();
5069 return PyLong_FromLong((long)fd
);
5073 PyDoc_STRVAR(posix_dup2__doc__
,
5074 "dup2(old_fd, new_fd)\n\n\
5075 Duplicate file descriptor.");
5078 posix_dup2(PyObject
*self
, PyObject
*args
)
5081 if (!PyArg_ParseTuple(args
, "ii:dup2", &fd
, &fd2
))
5083 if (!_PyVerify_fd_dup2(fd
, fd2
))
5084 return posix_error();
5085 Py_BEGIN_ALLOW_THREADS
5086 res
= dup2(fd
, fd2
);
5087 Py_END_ALLOW_THREADS
5089 return posix_error();
5095 PyDoc_STRVAR(posix_lseek__doc__
,
5096 "lseek(fd, pos, how) -> newpos\n\n\
5097 Set the current position of a file descriptor.");
5100 posix_lseek(PyObject
*self
, PyObject
*args
)
5103 #if defined(MS_WIN64) || defined(MS_WINDOWS)
5104 PY_LONG_LONG pos
, res
;
5109 if (!PyArg_ParseTuple(args
, "iOi:lseek", &fd
, &posobj
, &how
))
5112 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5114 case 0: how
= SEEK_SET
; break;
5115 case 1: how
= SEEK_CUR
; break;
5116 case 2: how
= SEEK_END
; break;
5118 #endif /* SEEK_END */
5120 #if !defined(HAVE_LARGEFILE_SUPPORT)
5121 pos
= PyLong_AsLong(posobj
);
5123 pos
= PyLong_Check(posobj
) ?
5124 PyLong_AsLongLong(posobj
) : PyLong_AsLong(posobj
);
5126 if (PyErr_Occurred())
5129 if (!_PyVerify_fd(fd
))
5130 return posix_error();
5131 Py_BEGIN_ALLOW_THREADS
5132 #if defined(MS_WIN64) || defined(MS_WINDOWS)
5133 res
= _lseeki64(fd
, pos
, how
);
5135 res
= lseek(fd
, pos
, how
);
5137 Py_END_ALLOW_THREADS
5139 return posix_error();
5141 #if !defined(HAVE_LARGEFILE_SUPPORT)
5142 return PyLong_FromLong(res
);
5144 return PyLong_FromLongLong(res
);
5149 PyDoc_STRVAR(posix_read__doc__
,
5150 "read(fd, buffersize) -> string\n\n\
5151 Read a file descriptor.");
5154 posix_read(PyObject
*self
, PyObject
*args
)
5159 if (!PyArg_ParseTuple(args
, "ii:read", &fd
, &size
))
5163 return posix_error();
5165 buffer
= PyBytes_FromStringAndSize((char *)NULL
, size
);
5168 if (!_PyVerify_fd(fd
))
5169 return posix_error();
5170 Py_BEGIN_ALLOW_THREADS
5171 n
= read(fd
, PyBytes_AS_STRING(buffer
), size
);
5172 Py_END_ALLOW_THREADS
5175 return posix_error();
5178 _PyBytes_Resize(&buffer
, n
);
5183 PyDoc_STRVAR(posix_write__doc__
,
5184 "write(fd, string) -> byteswritten\n\n\
5185 Write a string to a file descriptor.");
5188 posix_write(PyObject
*self
, PyObject
*args
)
5194 if (!PyArg_ParseTuple(args
, "iy*:write", &fd
, &pbuf
))
5196 if (!_PyVerify_fd(fd
))
5197 return posix_error();
5198 Py_BEGIN_ALLOW_THREADS
5199 size
= write(fd
, pbuf
.buf
, (size_t)pbuf
.len
);
5200 Py_END_ALLOW_THREADS
5201 PyBuffer_Release(&pbuf
);
5203 return posix_error();
5204 return PyLong_FromSsize_t(size
);
5208 PyDoc_STRVAR(posix_fstat__doc__
,
5209 "fstat(fd) -> stat result\n\n\
5210 Like stat(), but for an open file descriptor.");
5213 posix_fstat(PyObject
*self
, PyObject
*args
)
5218 if (!PyArg_ParseTuple(args
, "i:fstat", &fd
))
5221 /* on OpenVMS we must ensure that all bytes are written to the file */
5224 if (!_PyVerify_fd(fd
))
5225 return posix_error();
5226 Py_BEGIN_ALLOW_THREADS
5227 res
= FSTAT(fd
, &st
);
5228 Py_END_ALLOW_THREADS
5231 return win32_error("fstat", NULL
);
5233 return posix_error();
5237 return _pystat_fromstructstat(&st
);
5240 PyDoc_STRVAR(posix_isatty__doc__
,
5241 "isatty(fd) -> bool\n\n\
5242 Return True if the file descriptor 'fd' is an open file descriptor\n\
5243 connected to the slave end of a terminal.");
5246 posix_isatty(PyObject
*self
, PyObject
*args
)
5249 if (!PyArg_ParseTuple(args
, "i:isatty", &fd
))
5251 if (!_PyVerify_fd(fd
))
5252 return PyBool_FromLong(0);
5253 return PyBool_FromLong(isatty(fd
));
5257 PyDoc_STRVAR(posix_pipe__doc__
,
5258 "pipe() -> (read_end, write_end)\n\n\
5262 posix_pipe(PyObject
*self
, PyObject
*noargs
)
5264 #if defined(PYOS_OS2)
5268 Py_BEGIN_ALLOW_THREADS
5269 rc
= DosCreatePipe( &read
, &write
, 4096);
5270 Py_END_ALLOW_THREADS
5272 return os2_error(rc
);
5274 return Py_BuildValue("(ii)", read
, write
);
5276 #if !defined(MS_WINDOWS)
5279 Py_BEGIN_ALLOW_THREADS
5281 Py_END_ALLOW_THREADS
5283 return posix_error();
5284 return Py_BuildValue("(ii)", fds
[0], fds
[1]);
5285 #else /* MS_WINDOWS */
5287 int read_fd
, write_fd
;
5289 Py_BEGIN_ALLOW_THREADS
5290 ok
= CreatePipe(&read
, &write
, NULL
, 0);
5291 Py_END_ALLOW_THREADS
5293 return win32_error("CreatePipe", NULL
);
5294 read_fd
= _open_osfhandle((Py_intptr_t
)read
, 0);
5295 write_fd
= _open_osfhandle((Py_intptr_t
)write
, 1);
5296 return Py_BuildValue("(ii)", read_fd
, write_fd
);
5297 #endif /* MS_WINDOWS */
5300 #endif /* HAVE_PIPE */
5304 PyDoc_STRVAR(posix_mkfifo__doc__
,
5305 "mkfifo(filename [, mode=0666])\n\n\
5306 Create a FIFO (a POSIX named pipe).");
5309 posix_mkfifo(PyObject
*self
, PyObject
*args
)
5314 if (!PyArg_ParseTuple(args
, "s|i:mkfifo", &filename
, &mode
))
5316 Py_BEGIN_ALLOW_THREADS
5317 res
= mkfifo(filename
, mode
);
5318 Py_END_ALLOW_THREADS
5320 return posix_error();
5327 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
5328 PyDoc_STRVAR(posix_mknod__doc__
,
5329 "mknod(filename [, mode=0600, device])\n\n\
5330 Create a filesystem node (file, device special file or named pipe)\n\
5331 named filename. mode specifies both the permissions to use and the\n\
5332 type of node to be created, being combined (bitwise OR) with one of\n\
5333 S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
5334 device defines the newly created device special file (probably using\n\
5335 os.makedev()), otherwise it is ignored.");
5339 posix_mknod(PyObject
*self
, PyObject
*args
)
5345 if (!PyArg_ParseTuple(args
, "s|ii:mknod", &filename
, &mode
, &device
))
5347 Py_BEGIN_ALLOW_THREADS
5348 res
= mknod(filename
, mode
, device
);
5349 Py_END_ALLOW_THREADS
5351 return posix_error();
5357 #ifdef HAVE_DEVICE_MACROS
5358 PyDoc_STRVAR(posix_major__doc__
,
5359 "major(device) -> major number\n\
5360 Extracts a device major number from a raw device number.");
5363 posix_major(PyObject
*self
, PyObject
*args
)
5366 if (!PyArg_ParseTuple(args
, "i:major", &device
))
5368 return PyLong_FromLong((long)major(device
));
5371 PyDoc_STRVAR(posix_minor__doc__
,
5372 "minor(device) -> minor number\n\
5373 Extracts a device minor number from a raw device number.");
5376 posix_minor(PyObject
*self
, PyObject
*args
)
5379 if (!PyArg_ParseTuple(args
, "i:minor", &device
))
5381 return PyLong_FromLong((long)minor(device
));
5384 PyDoc_STRVAR(posix_makedev__doc__
,
5385 "makedev(major, minor) -> device number\n\
5386 Composes a raw device number from the major and minor device numbers.");
5389 posix_makedev(PyObject
*self
, PyObject
*args
)
5392 if (!PyArg_ParseTuple(args
, "ii:makedev", &major
, &minor
))
5394 return PyLong_FromLong((long)makedev(major
, minor
));
5396 #endif /* device macros */
5399 #ifdef HAVE_FTRUNCATE
5400 PyDoc_STRVAR(posix_ftruncate__doc__
,
5401 "ftruncate(fd, length)\n\n\
5402 Truncate a file to a specified length.");
5405 posix_ftruncate(PyObject
*self
, PyObject
*args
)
5412 if (!PyArg_ParseTuple(args
, "iO:ftruncate", &fd
, &lenobj
))
5415 #if !defined(HAVE_LARGEFILE_SUPPORT)
5416 length
= PyLong_AsLong(lenobj
);
5418 length
= PyLong_Check(lenobj
) ?
5419 PyLong_AsLongLong(lenobj
) : PyLong_AsLong(lenobj
);
5421 if (PyErr_Occurred())
5424 Py_BEGIN_ALLOW_THREADS
5425 res
= ftruncate(fd
, length
);
5426 Py_END_ALLOW_THREADS
5428 return posix_error();
5435 PyDoc_STRVAR(posix_putenv__doc__
,
5436 "putenv(key, value)\n\n\
5437 Change or add an environment variable.");
5439 /* Save putenv() parameters as values here, so we can collect them when they
5440 * get re-set with another call for the same key. */
5441 static PyObject
*posix_putenv_garbage
;
5444 posix_putenv(PyObject
*self
, PyObject
*args
)
5450 PyObject
*os1
, *os2
;
5458 if (!PyArg_ParseTuple(args
,
5463 if (!PyArg_ParseTuple(args
,
5465 PyUnicode_FSConverter
, &os1
,
5466 PyUnicode_FSConverter
, &os2
))
5468 s1
= bytes2str(os1
, 1);
5469 s2
= bytes2str(os2
, 1);
5472 #if defined(PYOS_OS2)
5473 if (stricmp(s1
, "BEGINLIBPATH") == 0) {
5476 rc
= DosSetExtLIBPATH(s2
, BEGIN_LIBPATH
);
5478 return os2_error(rc
);
5480 } else if (stricmp(s1
, "ENDLIBPATH") == 0) {
5483 rc
= DosSetExtLIBPATH(s2
, END_LIBPATH
);
5485 return os2_error(rc
);
5488 /* XXX This can leak memory -- not easy to fix :-( */
5489 /* len includes space for a trailing \0; the size arg to
5490 PyBytes_FromStringAndSize does not count that */
5492 len
= wcslen(s1
) + wcslen(s2
) + 2;
5493 newstr
= PyUnicode_FromUnicode(NULL
, (int)len
- 1);
5495 len
= strlen(s1
) + strlen(s2
) + 2;
5496 newstr
= PyBytes_FromStringAndSize(NULL
, (int)len
- 1);
5499 return PyErr_NoMemory();
5501 newenv
= PyUnicode_AsUnicode(newstr
);
5502 _snwprintf(newenv
, len
, L
"%s=%s", s1
, s2
);
5503 if (_wputenv(newenv
)) {
5509 newenv
= PyBytes_AS_STRING(newstr
);
5510 PyOS_snprintf(newenv
, len
, "%s=%s", s1
, s2
);
5511 if (putenv(newenv
)) {
5519 /* Install the first arg and newstr in posix_putenv_garbage;
5520 * this will cause previous value to be collected. This has to
5521 * happen after the real putenv() call because the old value
5522 * was still accessible until then. */
5523 if (PyDict_SetItem(posix_putenv_garbage
,
5524 PyTuple_GET_ITEM(args
, 0), newstr
)) {
5525 /* really not much we can do; just leak */
5532 #if defined(PYOS_OS2)
5544 #ifdef HAVE_UNSETENV
5545 PyDoc_STRVAR(posix_unsetenv__doc__
,
5547 Delete an environment variable.");
5550 posix_unsetenv(PyObject
*self
, PyObject
*args
)
5554 if (!PyArg_ParseTuple(args
, "s:unsetenv", &s1
))
5559 /* Remove the key from posix_putenv_garbage;
5560 * this will cause it to be collected. This has to
5561 * happen after the real unsetenv() call because the
5562 * old value was still accessible until then.
5564 if (PyDict_DelItem(posix_putenv_garbage
,
5565 PyTuple_GET_ITEM(args
, 0))) {
5566 /* really not much we can do; just leak */
5573 #endif /* unsetenv */
5575 PyDoc_STRVAR(posix_strerror__doc__
,
5576 "strerror(code) -> string\n\n\
5577 Translate an error code to a message string.");
5580 posix_strerror(PyObject
*self
, PyObject
*args
)
5584 if (!PyArg_ParseTuple(args
, "i:strerror", &code
))
5586 message
= strerror(code
);
5587 if (message
== NULL
) {
5588 PyErr_SetString(PyExc_ValueError
,
5589 "strerror() argument out of range");
5592 return PyUnicode_FromString(message
);
5596 #ifdef HAVE_SYS_WAIT_H
5599 PyDoc_STRVAR(posix_WCOREDUMP__doc__
,
5600 "WCOREDUMP(status) -> bool\n\n\
5601 Return True if the process returning 'status' was dumped to a core file.");
5604 posix_WCOREDUMP(PyObject
*self
, PyObject
*args
)
5607 WAIT_STATUS_INT(status
) = 0;
5609 if (!PyArg_ParseTuple(args
, "i:WCOREDUMP", &WAIT_STATUS_INT(status
)))
5612 return PyBool_FromLong(WCOREDUMP(status
));
5614 #endif /* WCOREDUMP */
5617 PyDoc_STRVAR(posix_WIFCONTINUED__doc__
,
5618 "WIFCONTINUED(status) -> bool\n\n\
5619 Return True if the process returning 'status' was continued from a\n\
5620 job control stop.");
5623 posix_WIFCONTINUED(PyObject
*self
, PyObject
*args
)
5626 WAIT_STATUS_INT(status
) = 0;
5628 if (!PyArg_ParseTuple(args
, "i:WCONTINUED", &WAIT_STATUS_INT(status
)))
5631 return PyBool_FromLong(WIFCONTINUED(status
));
5633 #endif /* WIFCONTINUED */
5636 PyDoc_STRVAR(posix_WIFSTOPPED__doc__
,
5637 "WIFSTOPPED(status) -> bool\n\n\
5638 Return True if the process returning 'status' was stopped.");
5641 posix_WIFSTOPPED(PyObject
*self
, PyObject
*args
)
5644 WAIT_STATUS_INT(status
) = 0;
5646 if (!PyArg_ParseTuple(args
, "i:WIFSTOPPED", &WAIT_STATUS_INT(status
)))
5649 return PyBool_FromLong(WIFSTOPPED(status
));
5651 #endif /* WIFSTOPPED */
5654 PyDoc_STRVAR(posix_WIFSIGNALED__doc__
,
5655 "WIFSIGNALED(status) -> bool\n\n\
5656 Return True if the process returning 'status' was terminated by a signal.");
5659 posix_WIFSIGNALED(PyObject
*self
, PyObject
*args
)
5662 WAIT_STATUS_INT(status
) = 0;
5664 if (!PyArg_ParseTuple(args
, "i:WIFSIGNALED", &WAIT_STATUS_INT(status
)))
5667 return PyBool_FromLong(WIFSIGNALED(status
));
5669 #endif /* WIFSIGNALED */
5672 PyDoc_STRVAR(posix_WIFEXITED__doc__
,
5673 "WIFEXITED(status) -> bool\n\n\
5674 Return true if the process returning 'status' exited using the exit()\n\
5678 posix_WIFEXITED(PyObject
*self
, PyObject
*args
)
5681 WAIT_STATUS_INT(status
) = 0;
5683 if (!PyArg_ParseTuple(args
, "i:WIFEXITED", &WAIT_STATUS_INT(status
)))
5686 return PyBool_FromLong(WIFEXITED(status
));
5688 #endif /* WIFEXITED */
5691 PyDoc_STRVAR(posix_WEXITSTATUS__doc__
,
5692 "WEXITSTATUS(status) -> integer\n\n\
5693 Return the process return code from 'status'.");
5696 posix_WEXITSTATUS(PyObject
*self
, PyObject
*args
)
5699 WAIT_STATUS_INT(status
) = 0;
5701 if (!PyArg_ParseTuple(args
, "i:WEXITSTATUS", &WAIT_STATUS_INT(status
)))
5704 return Py_BuildValue("i", WEXITSTATUS(status
));
5706 #endif /* WEXITSTATUS */
5709 PyDoc_STRVAR(posix_WTERMSIG__doc__
,
5710 "WTERMSIG(status) -> integer\n\n\
5711 Return the signal that terminated the process that provided the 'status'\n\
5715 posix_WTERMSIG(PyObject
*self
, PyObject
*args
)
5718 WAIT_STATUS_INT(status
) = 0;
5720 if (!PyArg_ParseTuple(args
, "i:WTERMSIG", &WAIT_STATUS_INT(status
)))
5723 return Py_BuildValue("i", WTERMSIG(status
));
5725 #endif /* WTERMSIG */
5728 PyDoc_STRVAR(posix_WSTOPSIG__doc__
,
5729 "WSTOPSIG(status) -> integer\n\n\
5730 Return the signal that stopped the process that provided\n\
5731 the 'status' value.");
5734 posix_WSTOPSIG(PyObject
*self
, PyObject
*args
)
5737 WAIT_STATUS_INT(status
) = 0;
5739 if (!PyArg_ParseTuple(args
, "i:WSTOPSIG", &WAIT_STATUS_INT(status
)))
5742 return Py_BuildValue("i", WSTOPSIG(status
));
5744 #endif /* WSTOPSIG */
5746 #endif /* HAVE_SYS_WAIT_H */
5749 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
5751 /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5752 needed definitions in sys/statvfs.h */
5755 #include <sys/statvfs.h>
5758 _pystatvfs_fromstructstatvfs(struct statvfs st
) {
5759 PyObject
*v
= PyStructSequence_New(&StatVFSResultType
);
5763 #if !defined(HAVE_LARGEFILE_SUPPORT)
5764 PyStructSequence_SET_ITEM(v
, 0, PyLong_FromLong((long) st
.f_bsize
));
5765 PyStructSequence_SET_ITEM(v
, 1, PyLong_FromLong((long) st
.f_frsize
));
5766 PyStructSequence_SET_ITEM(v
, 2, PyLong_FromLong((long) st
.f_blocks
));
5767 PyStructSequence_SET_ITEM(v
, 3, PyLong_FromLong((long) st
.f_bfree
));
5768 PyStructSequence_SET_ITEM(v
, 4, PyLong_FromLong((long) st
.f_bavail
));
5769 PyStructSequence_SET_ITEM(v
, 5, PyLong_FromLong((long) st
.f_files
));
5770 PyStructSequence_SET_ITEM(v
, 6, PyLong_FromLong((long) st
.f_ffree
));
5771 PyStructSequence_SET_ITEM(v
, 7, PyLong_FromLong((long) st
.f_favail
));
5772 PyStructSequence_SET_ITEM(v
, 8, PyLong_FromLong((long) st
.f_flag
));
5773 PyStructSequence_SET_ITEM(v
, 9, PyLong_FromLong((long) st
.f_namemax
));
5775 PyStructSequence_SET_ITEM(v
, 0, PyLong_FromLong((long) st
.f_bsize
));
5776 PyStructSequence_SET_ITEM(v
, 1, PyLong_FromLong((long) st
.f_frsize
));
5777 PyStructSequence_SET_ITEM(v
, 2,
5778 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_blocks
));
5779 PyStructSequence_SET_ITEM(v
, 3,
5780 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_bfree
));
5781 PyStructSequence_SET_ITEM(v
, 4,
5782 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_bavail
));
5783 PyStructSequence_SET_ITEM(v
, 5,
5784 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_files
));
5785 PyStructSequence_SET_ITEM(v
, 6,
5786 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_ffree
));
5787 PyStructSequence_SET_ITEM(v
, 7,
5788 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_favail
));
5789 PyStructSequence_SET_ITEM(v
, 8, PyLong_FromLong((long) st
.f_flag
));
5790 PyStructSequence_SET_ITEM(v
, 9, PyLong_FromLong((long) st
.f_namemax
));
5796 PyDoc_STRVAR(posix_fstatvfs__doc__
,
5797 "fstatvfs(fd) -> statvfs result\n\n\
5798 Perform an fstatvfs system call on the given fd.");
5801 posix_fstatvfs(PyObject
*self
, PyObject
*args
)
5806 if (!PyArg_ParseTuple(args
, "i:fstatvfs", &fd
))
5808 Py_BEGIN_ALLOW_THREADS
5809 res
= fstatvfs(fd
, &st
);
5810 Py_END_ALLOW_THREADS
5812 return posix_error();
5814 return _pystatvfs_fromstructstatvfs(st
);
5816 #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
5819 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
5820 #include <sys/statvfs.h>
5822 PyDoc_STRVAR(posix_statvfs__doc__
,
5823 "statvfs(path) -> statvfs result\n\n\
5824 Perform a statvfs system call on the given path.");
5827 posix_statvfs(PyObject
*self
, PyObject
*args
)
5832 if (!PyArg_ParseTuple(args
, "s:statvfs", &path
))
5834 Py_BEGIN_ALLOW_THREADS
5835 res
= statvfs(path
, &st
);
5836 Py_END_ALLOW_THREADS
5838 return posix_error_with_filename(path
);
5840 return _pystatvfs_fromstructstatvfs(st
);
5842 #endif /* HAVE_STATVFS */
5844 /* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5845 * It maps strings representing configuration variable names to
5846 * integer values, allowing those functions to be called with the
5847 * magic names instead of polluting the module's namespace with tons of
5848 * rarely-used constants. There are three separate tables that use
5849 * these definitions.
5851 * This code is always included, even if none of the interfaces that
5852 * need it are included. The #if hackery needed to avoid it would be
5853 * sufficiently pervasive that it's not worth the loss of readability.
5861 conv_confname(PyObject
*arg
, int *valuep
, struct constdef
*table
,
5864 if (PyLong_Check(arg
)) {
5865 *valuep
= PyLong_AS_LONG(arg
);
5869 /* look up the value in the table using a binary search */
5872 size_t hi
= tablesize
;
5874 const char *confname
;
5875 if (!PyUnicode_Check(arg
)) {
5876 PyErr_SetString(PyExc_TypeError
,
5877 "configuration names must be strings or integers");
5880 confname
= _PyUnicode_AsString(arg
);
5881 if (confname
== NULL
)
5884 mid
= (lo
+ hi
) / 2;
5885 cmp
= strcmp(confname
, table
[mid
].name
);
5891 *valuep
= table
[mid
].value
;
5895 PyErr_SetString(PyExc_ValueError
, "unrecognized configuration name");
5901 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5902 static struct constdef posix_constants_pathconf
[] = {
5903 #ifdef _PC_ABI_AIO_XFER_MAX
5904 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX
},
5906 #ifdef _PC_ABI_ASYNC_IO
5907 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO
},
5910 {"PC_ASYNC_IO", _PC_ASYNC_IO
},
5912 #ifdef _PC_CHOWN_RESTRICTED
5913 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED
},
5915 #ifdef _PC_FILESIZEBITS
5916 {"PC_FILESIZEBITS", _PC_FILESIZEBITS
},
5919 {"PC_LAST", _PC_LAST
},
5922 {"PC_LINK_MAX", _PC_LINK_MAX
},
5924 #ifdef _PC_MAX_CANON
5925 {"PC_MAX_CANON", _PC_MAX_CANON
},
5927 #ifdef _PC_MAX_INPUT
5928 {"PC_MAX_INPUT", _PC_MAX_INPUT
},
5931 {"PC_NAME_MAX", _PC_NAME_MAX
},
5934 {"PC_NO_TRUNC", _PC_NO_TRUNC
},
5937 {"PC_PATH_MAX", _PC_PATH_MAX
},
5940 {"PC_PIPE_BUF", _PC_PIPE_BUF
},
5943 {"PC_PRIO_IO", _PC_PRIO_IO
},
5945 #ifdef _PC_SOCK_MAXBUF
5946 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF
},
5949 {"PC_SYNC_IO", _PC_SYNC_IO
},
5952 {"PC_VDISABLE", _PC_VDISABLE
},
5957 conv_path_confname(PyObject
*arg
, int *valuep
)
5959 return conv_confname(arg
, valuep
, posix_constants_pathconf
,
5960 sizeof(posix_constants_pathconf
)
5961 / sizeof(struct constdef
));
5965 #ifdef HAVE_FPATHCONF
5966 PyDoc_STRVAR(posix_fpathconf__doc__
,
5967 "fpathconf(fd, name) -> integer\n\n\
5968 Return the configuration limit name for the file descriptor fd.\n\
5969 If there is no limit, return -1.");
5972 posix_fpathconf(PyObject
*self
, PyObject
*args
)
5974 PyObject
*result
= NULL
;
5977 if (PyArg_ParseTuple(args
, "iO&:fpathconf", &fd
,
5978 conv_path_confname
, &name
)) {
5982 limit
= fpathconf(fd
, name
);
5983 if (limit
== -1 && errno
!= 0)
5986 result
= PyLong_FromLong(limit
);
5993 #ifdef HAVE_PATHCONF
5994 PyDoc_STRVAR(posix_pathconf__doc__
,
5995 "pathconf(path, name) -> integer\n\n\
5996 Return the configuration limit name for the file or directory path.\n\
5997 If there is no limit, return -1.");
6000 posix_pathconf(PyObject
*self
, PyObject
*args
)
6002 PyObject
*result
= NULL
;
6006 if (PyArg_ParseTuple(args
, "sO&:pathconf", &path
,
6007 conv_path_confname
, &name
)) {
6011 limit
= pathconf(path
, name
);
6012 if (limit
== -1 && errno
!= 0) {
6013 if (errno
== EINVAL
)
6014 /* could be a path or name problem */
6017 posix_error_with_filename(path
);
6020 result
= PyLong_FromLong(limit
);
6027 static struct constdef posix_constants_confstr
[] = {
6028 #ifdef _CS_ARCHITECTURE
6029 {"CS_ARCHITECTURE", _CS_ARCHITECTURE
},
6032 {"CS_HOSTNAME", _CS_HOSTNAME
},
6034 #ifdef _CS_HW_PROVIDER
6035 {"CS_HW_PROVIDER", _CS_HW_PROVIDER
},
6037 #ifdef _CS_HW_SERIAL
6038 {"CS_HW_SERIAL", _CS_HW_SERIAL
},
6040 #ifdef _CS_INITTAB_NAME
6041 {"CS_INITTAB_NAME", _CS_INITTAB_NAME
},
6043 #ifdef _CS_LFS64_CFLAGS
6044 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS
},
6046 #ifdef _CS_LFS64_LDFLAGS
6047 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS
},
6049 #ifdef _CS_LFS64_LIBS
6050 {"CS_LFS64_LIBS", _CS_LFS64_LIBS
},
6052 #ifdef _CS_LFS64_LINTFLAGS
6053 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS
},
6055 #ifdef _CS_LFS_CFLAGS
6056 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS
},
6058 #ifdef _CS_LFS_LDFLAGS
6059 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS
},
6062 {"CS_LFS_LIBS", _CS_LFS_LIBS
},
6064 #ifdef _CS_LFS_LINTFLAGS
6065 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS
},
6068 {"CS_MACHINE", _CS_MACHINE
},
6071 {"CS_PATH", _CS_PATH
},
6074 {"CS_RELEASE", _CS_RELEASE
},
6076 #ifdef _CS_SRPC_DOMAIN
6077 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN
},
6080 {"CS_SYSNAME", _CS_SYSNAME
},
6083 {"CS_VERSION", _CS_VERSION
},
6085 #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6086 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS
},
6088 #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6089 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS
},
6091 #ifdef _CS_XBS5_ILP32_OFF32_LIBS
6092 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS
},
6094 #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6095 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS
},
6097 #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6098 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS
},
6100 #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6101 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS
},
6103 #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6104 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS
},
6106 #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6107 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
},
6109 #ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6110 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS
},
6112 #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6113 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS
},
6115 #ifdef _CS_XBS5_LP64_OFF64_LIBS
6116 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS
},
6118 #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6119 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS
},
6121 #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6122 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS
},
6124 #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6125 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
},
6127 #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6128 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS
},
6130 #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6131 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
},
6133 #ifdef _MIPS_CS_AVAIL_PROCESSORS
6134 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS
},
6136 #ifdef _MIPS_CS_BASE
6137 {"MIPS_CS_BASE", _MIPS_CS_BASE
},
6139 #ifdef _MIPS_CS_HOSTID
6140 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID
},
6142 #ifdef _MIPS_CS_HW_NAME
6143 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME
},
6145 #ifdef _MIPS_CS_NUM_PROCESSORS
6146 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS
},
6148 #ifdef _MIPS_CS_OSREL_MAJ
6149 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ
},
6151 #ifdef _MIPS_CS_OSREL_MIN
6152 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN
},
6154 #ifdef _MIPS_CS_OSREL_PATCH
6155 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH
},
6157 #ifdef _MIPS_CS_OS_NAME
6158 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME
},
6160 #ifdef _MIPS_CS_OS_PROVIDER
6161 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER
},
6163 #ifdef _MIPS_CS_PROCESSORS
6164 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS
},
6166 #ifdef _MIPS_CS_SERIAL
6167 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL
},
6169 #ifdef _MIPS_CS_VENDOR
6170 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR
},
6175 conv_confstr_confname(PyObject
*arg
, int *valuep
)
6177 return conv_confname(arg
, valuep
, posix_constants_confstr
,
6178 sizeof(posix_constants_confstr
)
6179 / sizeof(struct constdef
));
6182 PyDoc_STRVAR(posix_confstr__doc__
,
6183 "confstr(name) -> string\n\n\
6184 Return a string-valued system configuration variable.");
6187 posix_confstr(PyObject
*self
, PyObject
*args
)
6189 PyObject
*result
= NULL
;
6193 if (PyArg_ParseTuple(args
, "O&:confstr", conv_confstr_confname
, &name
)) {
6197 len
= confstr(name
, buffer
, sizeof(buffer
));
6208 if ((unsigned int)len
>= sizeof(buffer
)) {
6209 result
= PyUnicode_FromStringAndSize(NULL
, len
-1);
6211 confstr(name
, _PyUnicode_AsString(result
), len
);
6214 result
= PyUnicode_FromStringAndSize(buffer
, len
-1);
6223 static struct constdef posix_constants_sysconf
[] = {
6224 #ifdef _SC_2_CHAR_TERM
6225 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM
},
6228 {"SC_2_C_BIND", _SC_2_C_BIND
},
6231 {"SC_2_C_DEV", _SC_2_C_DEV
},
6233 #ifdef _SC_2_C_VERSION
6234 {"SC_2_C_VERSION", _SC_2_C_VERSION
},
6236 #ifdef _SC_2_FORT_DEV
6237 {"SC_2_FORT_DEV", _SC_2_FORT_DEV
},
6239 #ifdef _SC_2_FORT_RUN
6240 {"SC_2_FORT_RUN", _SC_2_FORT_RUN
},
6242 #ifdef _SC_2_LOCALEDEF
6243 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF
},
6246 {"SC_2_SW_DEV", _SC_2_SW_DEV
},
6249 {"SC_2_UPE", _SC_2_UPE
},
6251 #ifdef _SC_2_VERSION
6252 {"SC_2_VERSION", _SC_2_VERSION
},
6254 #ifdef _SC_ABI_ASYNCHRONOUS_IO
6255 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO
},
6258 {"SC_ACL", _SC_ACL
},
6260 #ifdef _SC_AIO_LISTIO_MAX
6261 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX
},
6264 {"SC_AIO_MAX", _SC_AIO_MAX
},
6266 #ifdef _SC_AIO_PRIO_DELTA_MAX
6267 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX
},
6270 {"SC_ARG_MAX", _SC_ARG_MAX
},
6272 #ifdef _SC_ASYNCHRONOUS_IO
6273 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO
},
6275 #ifdef _SC_ATEXIT_MAX
6276 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX
},
6279 {"SC_AUDIT", _SC_AUDIT
},
6281 #ifdef _SC_AVPHYS_PAGES
6282 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES
},
6284 #ifdef _SC_BC_BASE_MAX
6285 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX
},
6287 #ifdef _SC_BC_DIM_MAX
6288 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX
},
6290 #ifdef _SC_BC_SCALE_MAX
6291 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX
},
6293 #ifdef _SC_BC_STRING_MAX
6294 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX
},
6297 {"SC_CAP", _SC_CAP
},
6299 #ifdef _SC_CHARCLASS_NAME_MAX
6300 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX
},
6303 {"SC_CHAR_BIT", _SC_CHAR_BIT
},
6306 {"SC_CHAR_MAX", _SC_CHAR_MAX
},
6309 {"SC_CHAR_MIN", _SC_CHAR_MIN
},
6311 #ifdef _SC_CHILD_MAX
6312 {"SC_CHILD_MAX", _SC_CHILD_MAX
},
6315 {"SC_CLK_TCK", _SC_CLK_TCK
},
6317 #ifdef _SC_COHER_BLKSZ
6318 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ
},
6320 #ifdef _SC_COLL_WEIGHTS_MAX
6321 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX
},
6323 #ifdef _SC_DCACHE_ASSOC
6324 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC
},
6326 #ifdef _SC_DCACHE_BLKSZ
6327 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ
},
6329 #ifdef _SC_DCACHE_LINESZ
6330 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ
},
6332 #ifdef _SC_DCACHE_SZ
6333 {"SC_DCACHE_SZ", _SC_DCACHE_SZ
},
6335 #ifdef _SC_DCACHE_TBLKSZ
6336 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ
},
6338 #ifdef _SC_DELAYTIMER_MAX
6339 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX
},
6341 #ifdef _SC_EQUIV_CLASS_MAX
6342 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX
},
6344 #ifdef _SC_EXPR_NEST_MAX
6345 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX
},
6348 {"SC_FSYNC", _SC_FSYNC
},
6350 #ifdef _SC_GETGR_R_SIZE_MAX
6351 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX
},
6353 #ifdef _SC_GETPW_R_SIZE_MAX
6354 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX
},
6356 #ifdef _SC_ICACHE_ASSOC
6357 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC
},
6359 #ifdef _SC_ICACHE_BLKSZ
6360 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ
},
6362 #ifdef _SC_ICACHE_LINESZ
6363 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ
},
6365 #ifdef _SC_ICACHE_SZ
6366 {"SC_ICACHE_SZ", _SC_ICACHE_SZ
},
6369 {"SC_INF", _SC_INF
},
6372 {"SC_INT_MAX", _SC_INT_MAX
},
6375 {"SC_INT_MIN", _SC_INT_MIN
},
6378 {"SC_IOV_MAX", _SC_IOV_MAX
},
6380 #ifdef _SC_IP_SECOPTS
6381 {"SC_IP_SECOPTS", _SC_IP_SECOPTS
},
6383 #ifdef _SC_JOB_CONTROL
6384 {"SC_JOB_CONTROL", _SC_JOB_CONTROL
},
6386 #ifdef _SC_KERN_POINTERS
6387 {"SC_KERN_POINTERS", _SC_KERN_POINTERS
},
6390 {"SC_KERN_SIM", _SC_KERN_SIM
},
6393 {"SC_LINE_MAX", _SC_LINE_MAX
},
6395 #ifdef _SC_LOGIN_NAME_MAX
6396 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX
},
6398 #ifdef _SC_LOGNAME_MAX
6399 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX
},
6402 {"SC_LONG_BIT", _SC_LONG_BIT
},
6405 {"SC_MAC", _SC_MAC
},
6407 #ifdef _SC_MAPPED_FILES
6408 {"SC_MAPPED_FILES", _SC_MAPPED_FILES
},
6411 {"SC_MAXPID", _SC_MAXPID
},
6413 #ifdef _SC_MB_LEN_MAX
6414 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX
},
6417 {"SC_MEMLOCK", _SC_MEMLOCK
},
6419 #ifdef _SC_MEMLOCK_RANGE
6420 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE
},
6422 #ifdef _SC_MEMORY_PROTECTION
6423 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION
},
6425 #ifdef _SC_MESSAGE_PASSING
6426 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING
},
6428 #ifdef _SC_MMAP_FIXED_ALIGNMENT
6429 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT
},
6431 #ifdef _SC_MQ_OPEN_MAX
6432 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX
},
6434 #ifdef _SC_MQ_PRIO_MAX
6435 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX
},
6437 #ifdef _SC_NACLS_MAX
6438 {"SC_NACLS_MAX", _SC_NACLS_MAX
},
6440 #ifdef _SC_NGROUPS_MAX
6441 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX
},
6443 #ifdef _SC_NL_ARGMAX
6444 {"SC_NL_ARGMAX", _SC_NL_ARGMAX
},
6446 #ifdef _SC_NL_LANGMAX
6447 {"SC_NL_LANGMAX", _SC_NL_LANGMAX
},
6449 #ifdef _SC_NL_MSGMAX
6450 {"SC_NL_MSGMAX", _SC_NL_MSGMAX
},
6453 {"SC_NL_NMAX", _SC_NL_NMAX
},
6455 #ifdef _SC_NL_SETMAX
6456 {"SC_NL_SETMAX", _SC_NL_SETMAX
},
6458 #ifdef _SC_NL_TEXTMAX
6459 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX
},
6461 #ifdef _SC_NPROCESSORS_CONF
6462 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF
},
6464 #ifdef _SC_NPROCESSORS_ONLN
6465 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN
},
6467 #ifdef _SC_NPROC_CONF
6468 {"SC_NPROC_CONF", _SC_NPROC_CONF
},
6470 #ifdef _SC_NPROC_ONLN
6471 {"SC_NPROC_ONLN", _SC_NPROC_ONLN
},
6474 {"SC_NZERO", _SC_NZERO
},
6477 {"SC_OPEN_MAX", _SC_OPEN_MAX
},
6480 {"SC_PAGESIZE", _SC_PAGESIZE
},
6482 #ifdef _SC_PAGE_SIZE
6483 {"SC_PAGE_SIZE", _SC_PAGE_SIZE
},
6486 {"SC_PASS_MAX", _SC_PASS_MAX
},
6488 #ifdef _SC_PHYS_PAGES
6489 {"SC_PHYS_PAGES", _SC_PHYS_PAGES
},
6492 {"SC_PII", _SC_PII
},
6494 #ifdef _SC_PII_INTERNET
6495 {"SC_PII_INTERNET", _SC_PII_INTERNET
},
6497 #ifdef _SC_PII_INTERNET_DGRAM
6498 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM
},
6500 #ifdef _SC_PII_INTERNET_STREAM
6501 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM
},
6504 {"SC_PII_OSI", _SC_PII_OSI
},
6506 #ifdef _SC_PII_OSI_CLTS
6507 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS
},
6509 #ifdef _SC_PII_OSI_COTS
6510 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS
},
6512 #ifdef _SC_PII_OSI_M
6513 {"SC_PII_OSI_M", _SC_PII_OSI_M
},
6515 #ifdef _SC_PII_SOCKET
6516 {"SC_PII_SOCKET", _SC_PII_SOCKET
},
6519 {"SC_PII_XTI", _SC_PII_XTI
},
6522 {"SC_POLL", _SC_POLL
},
6524 #ifdef _SC_PRIORITIZED_IO
6525 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO
},
6527 #ifdef _SC_PRIORITY_SCHEDULING
6528 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING
},
6530 #ifdef _SC_REALTIME_SIGNALS
6531 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS
},
6533 #ifdef _SC_RE_DUP_MAX
6534 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX
},
6536 #ifdef _SC_RTSIG_MAX
6537 {"SC_RTSIG_MAX", _SC_RTSIG_MAX
},
6539 #ifdef _SC_SAVED_IDS
6540 {"SC_SAVED_IDS", _SC_SAVED_IDS
},
6542 #ifdef _SC_SCHAR_MAX
6543 {"SC_SCHAR_MAX", _SC_SCHAR_MAX
},
6545 #ifdef _SC_SCHAR_MIN
6546 {"SC_SCHAR_MIN", _SC_SCHAR_MIN
},
6549 {"SC_SELECT", _SC_SELECT
},
6551 #ifdef _SC_SEMAPHORES
6552 {"SC_SEMAPHORES", _SC_SEMAPHORES
},
6554 #ifdef _SC_SEM_NSEMS_MAX
6555 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX
},
6557 #ifdef _SC_SEM_VALUE_MAX
6558 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX
},
6560 #ifdef _SC_SHARED_MEMORY_OBJECTS
6561 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS
},
6564 {"SC_SHRT_MAX", _SC_SHRT_MAX
},
6567 {"SC_SHRT_MIN", _SC_SHRT_MIN
},
6569 #ifdef _SC_SIGQUEUE_MAX
6570 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX
},
6572 #ifdef _SC_SIGRT_MAX
6573 {"SC_SIGRT_MAX", _SC_SIGRT_MAX
},
6575 #ifdef _SC_SIGRT_MIN
6576 {"SC_SIGRT_MIN", _SC_SIGRT_MIN
},
6578 #ifdef _SC_SOFTPOWER
6579 {"SC_SOFTPOWER", _SC_SOFTPOWER
},
6581 #ifdef _SC_SPLIT_CACHE
6582 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE
},
6584 #ifdef _SC_SSIZE_MAX
6585 {"SC_SSIZE_MAX", _SC_SSIZE_MAX
},
6587 #ifdef _SC_STACK_PROT
6588 {"SC_STACK_PROT", _SC_STACK_PROT
},
6590 #ifdef _SC_STREAM_MAX
6591 {"SC_STREAM_MAX", _SC_STREAM_MAX
},
6593 #ifdef _SC_SYNCHRONIZED_IO
6594 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO
},
6597 {"SC_THREADS", _SC_THREADS
},
6599 #ifdef _SC_THREAD_ATTR_STACKADDR
6600 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR
},
6602 #ifdef _SC_THREAD_ATTR_STACKSIZE
6603 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE
},
6605 #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6606 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS
},
6608 #ifdef _SC_THREAD_KEYS_MAX
6609 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX
},
6611 #ifdef _SC_THREAD_PRIORITY_SCHEDULING
6612 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING
},
6614 #ifdef _SC_THREAD_PRIO_INHERIT
6615 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT
},
6617 #ifdef _SC_THREAD_PRIO_PROTECT
6618 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT
},
6620 #ifdef _SC_THREAD_PROCESS_SHARED
6621 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED
},
6623 #ifdef _SC_THREAD_SAFE_FUNCTIONS
6624 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS
},
6626 #ifdef _SC_THREAD_STACK_MIN
6627 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN
},
6629 #ifdef _SC_THREAD_THREADS_MAX
6630 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX
},
6633 {"SC_TIMERS", _SC_TIMERS
},
6635 #ifdef _SC_TIMER_MAX
6636 {"SC_TIMER_MAX", _SC_TIMER_MAX
},
6638 #ifdef _SC_TTY_NAME_MAX
6639 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX
},
6641 #ifdef _SC_TZNAME_MAX
6642 {"SC_TZNAME_MAX", _SC_TZNAME_MAX
},
6644 #ifdef _SC_T_IOV_MAX
6645 {"SC_T_IOV_MAX", _SC_T_IOV_MAX
},
6647 #ifdef _SC_UCHAR_MAX
6648 {"SC_UCHAR_MAX", _SC_UCHAR_MAX
},
6651 {"SC_UINT_MAX", _SC_UINT_MAX
},
6653 #ifdef _SC_UIO_MAXIOV
6654 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV
},
6656 #ifdef _SC_ULONG_MAX
6657 {"SC_ULONG_MAX", _SC_ULONG_MAX
},
6659 #ifdef _SC_USHRT_MAX
6660 {"SC_USHRT_MAX", _SC_USHRT_MAX
},
6663 {"SC_VERSION", _SC_VERSION
},
6666 {"SC_WORD_BIT", _SC_WORD_BIT
},
6668 #ifdef _SC_XBS5_ILP32_OFF32
6669 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32
},
6671 #ifdef _SC_XBS5_ILP32_OFFBIG
6672 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG
},
6674 #ifdef _SC_XBS5_LP64_OFF64
6675 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64
},
6677 #ifdef _SC_XBS5_LPBIG_OFFBIG
6678 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG
},
6680 #ifdef _SC_XOPEN_CRYPT
6681 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT
},
6683 #ifdef _SC_XOPEN_ENH_I18N
6684 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N
},
6686 #ifdef _SC_XOPEN_LEGACY
6687 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY
},
6689 #ifdef _SC_XOPEN_REALTIME
6690 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME
},
6692 #ifdef _SC_XOPEN_REALTIME_THREADS
6693 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS
},
6695 #ifdef _SC_XOPEN_SHM
6696 {"SC_XOPEN_SHM", _SC_XOPEN_SHM
},
6698 #ifdef _SC_XOPEN_UNIX
6699 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX
},
6701 #ifdef _SC_XOPEN_VERSION
6702 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION
},
6704 #ifdef _SC_XOPEN_XCU_VERSION
6705 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION
},
6707 #ifdef _SC_XOPEN_XPG2
6708 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2
},
6710 #ifdef _SC_XOPEN_XPG3
6711 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3
},
6713 #ifdef _SC_XOPEN_XPG4
6714 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4
},
6719 conv_sysconf_confname(PyObject
*arg
, int *valuep
)
6721 return conv_confname(arg
, valuep
, posix_constants_sysconf
,
6722 sizeof(posix_constants_sysconf
)
6723 / sizeof(struct constdef
));
6726 PyDoc_STRVAR(posix_sysconf__doc__
,
6727 "sysconf(name) -> integer\n\n\
6728 Return an integer-valued system configuration variable.");
6731 posix_sysconf(PyObject
*self
, PyObject
*args
)
6733 PyObject
*result
= NULL
;
6736 if (PyArg_ParseTuple(args
, "O&:sysconf", conv_sysconf_confname
, &name
)) {
6740 value
= sysconf(name
);
6741 if (value
== -1 && errno
!= 0)
6744 result
= PyLong_FromLong(value
);
6751 /* This code is used to ensure that the tables of configuration value names
6752 * are in sorted order as required by conv_confname(), and also to build the
6753 * the exported dictionaries that are used to publish information about the
6754 * names available on the host platform.
6756 * Sorting the table at runtime ensures that the table is properly ordered
6757 * when used, even for platforms we're not able to test on. It also makes
6758 * it easier to add additional entries to the tables.
6762 cmp_constdefs(const void *v1
, const void *v2
)
6764 const struct constdef
*c1
=
6765 (const struct constdef
*) v1
;
6766 const struct constdef
*c2
=
6767 (const struct constdef
*) v2
;
6769 return strcmp(c1
->name
, c2
->name
);
6773 setup_confname_table(struct constdef
*table
, size_t tablesize
,
6774 char *tablename
, PyObject
*module
)
6779 qsort(table
, tablesize
, sizeof(struct constdef
), cmp_constdefs
);
6784 for (i
=0; i
< tablesize
; ++i
) {
6785 PyObject
*o
= PyLong_FromLong(table
[i
].value
);
6786 if (o
== NULL
|| PyDict_SetItemString(d
, table
[i
].name
, o
) == -1) {
6793 return PyModule_AddObject(module
, tablename
, d
);
6796 /* Return -1 on failure, 0 on success. */
6798 setup_confname_tables(PyObject
*module
)
6800 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6801 if (setup_confname_table(posix_constants_pathconf
,
6802 sizeof(posix_constants_pathconf
)
6803 / sizeof(struct constdef
),
6804 "pathconf_names", module
))
6808 if (setup_confname_table(posix_constants_confstr
,
6809 sizeof(posix_constants_confstr
)
6810 / sizeof(struct constdef
),
6811 "confstr_names", module
))
6815 if (setup_confname_table(posix_constants_sysconf
,
6816 sizeof(posix_constants_sysconf
)
6817 / sizeof(struct constdef
),
6818 "sysconf_names", module
))
6825 PyDoc_STRVAR(posix_abort__doc__
,
6826 "abort() -> does not return!\n\n\
6827 Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
6828 in the hardest way possible on the hosting operating system.");
6831 posix_abort(PyObject
*self
, PyObject
*noargs
)
6835 Py_FatalError("abort() called from Python code didn't abort!");
6840 PyDoc_STRVAR(win32_startfile__doc__
,
6841 "startfile(filepath [, operation]) - Start a file with its associated\n\
6844 When \"operation\" is not specified or \"open\", this acts like\n\
6845 double-clicking the file in Explorer, or giving the file name as an\n\
6846 argument to the DOS \"start\" command: the file is opened with whatever\n\
6847 application (if any) its extension is associated.\n\
6848 When another \"operation\" is given, it specifies what should be done with\n\
6849 the file. A typical operation is \"print\".\n\
6851 startfile returns as soon as the associated application is launched.\n\
6852 There is no option to wait for the application to close, and no way\n\
6853 to retrieve the application's exit status.\n\
6855 The filepath is relative to the current directory. If you want to use\n\
6856 an absolute path, make sure the first character is not a slash (\"/\");\n\
6857 the underlying Win32 ShellExecute function doesn't work if it is.");
6860 win32_startfile(PyObject
*self
, PyObject
*args
)
6862 PyObject
*ofilepath
;
6864 char *operation
= NULL
;
6867 if (unicode_file_names()) {
6868 PyObject
*unipath
, *woperation
= NULL
;
6869 if (!PyArg_ParseTuple(args
, "U|s:startfile",
6870 &unipath
, &operation
)) {
6877 woperation
= PyUnicode_DecodeASCII(operation
,
6878 strlen(operation
), NULL
);
6886 Py_BEGIN_ALLOW_THREADS
6887 rc
= ShellExecuteW((HWND
)0, woperation
? PyUnicode_AS_UNICODE(woperation
) : 0,
6888 PyUnicode_AS_UNICODE(unipath
),
6889 NULL
, NULL
, SW_SHOWNORMAL
);
6890 Py_END_ALLOW_THREADS
6892 Py_XDECREF(woperation
);
6893 if (rc
<= (HINSTANCE
)32) {
6894 PyObject
*errval
= win32_error_unicode("startfile",
6895 PyUnicode_AS_UNICODE(unipath
));
6903 if (!PyArg_ParseTuple(args
, "O&|s:startfile",
6904 PyUnicode_FSConverter
, &ofilepath
,
6907 filepath
= bytes2str(ofilepath
, 1);
6908 Py_BEGIN_ALLOW_THREADS
6909 rc
= ShellExecute((HWND
)0, operation
, filepath
,
6910 NULL
, NULL
, SW_SHOWNORMAL
);
6911 Py_END_ALLOW_THREADS
6912 if (rc
<= (HINSTANCE
)32) {
6913 PyObject
*errval
= win32_error("startfile", filepath
);
6914 release_bytes(ofilepath
);
6917 release_bytes(ofilepath
);
6923 #ifdef HAVE_GETLOADAVG
6924 PyDoc_STRVAR(posix_getloadavg__doc__
,
6925 "getloadavg() -> (float, float, float)\n\n\
6926 Return the number of processes in the system run queue averaged over\n\
6927 the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6931 posix_getloadavg(PyObject
*self
, PyObject
*noargs
)
6934 if (getloadavg(loadavg
, 3)!=3) {
6935 PyErr_SetString(PyExc_OSError
, "Load averages are unobtainable");
6938 return Py_BuildValue("ddd", loadavg
[0], loadavg
[1], loadavg
[2]);
6944 PyDoc_STRVAR(win32_urandom__doc__
,
6945 "urandom(n) -> str\n\n\
6946 Return n random bytes suitable for cryptographic use.");
6948 typedef BOOL (WINAPI
*CRYPTACQUIRECONTEXTA
)(HCRYPTPROV
*phProv
,\
6949 LPCSTR pszContainer
, LPCSTR pszProvider
, DWORD dwProvType
,\
6951 typedef BOOL (WINAPI
*CRYPTGENRANDOM
)(HCRYPTPROV hProv
, DWORD dwLen
,\
6954 static CRYPTGENRANDOM pCryptGenRandom
= NULL
;
6955 /* This handle is never explicitly released. Instead, the operating
6956 system will release it when the process terminates. */
6957 static HCRYPTPROV hCryptProv
= 0;
6960 win32_urandom(PyObject
*self
, PyObject
*args
)
6965 /* Read arguments */
6966 if (! PyArg_ParseTuple(args
, "i:urandom", &howMany
))
6969 return PyErr_Format(PyExc_ValueError
,
6970 "negative argument not allowed");
6972 if (hCryptProv
== 0) {
6973 HINSTANCE hAdvAPI32
= NULL
;
6974 CRYPTACQUIRECONTEXTA pCryptAcquireContext
= NULL
;
6976 /* Obtain handle to the DLL containing CryptoAPI
6977 This should not fail */
6978 hAdvAPI32
= GetModuleHandle("advapi32.dll");
6979 if(hAdvAPI32
== NULL
)
6980 return win32_error("GetModuleHandle", NULL
);
6982 /* Obtain pointers to the CryptoAPI functions
6983 This will fail on some early versions of Win95 */
6984 pCryptAcquireContext
= (CRYPTACQUIRECONTEXTA
)GetProcAddress(
6986 "CryptAcquireContextA");
6987 if (pCryptAcquireContext
== NULL
)
6988 return PyErr_Format(PyExc_NotImplementedError
,
6989 "CryptAcquireContextA not found");
6991 pCryptGenRandom
= (CRYPTGENRANDOM
)GetProcAddress(
6992 hAdvAPI32
, "CryptGenRandom");
6993 if (pCryptGenRandom
== NULL
)
6994 return PyErr_Format(PyExc_NotImplementedError
,
6995 "CryptGenRandom not found");
6997 /* Acquire context */
6998 if (! pCryptAcquireContext(&hCryptProv
, NULL
, NULL
,
6999 PROV_RSA_FULL
, CRYPT_VERIFYCONTEXT
))
7000 return win32_error("CryptAcquireContext", NULL
);
7003 /* Allocate bytes */
7004 result
= PyBytes_FromStringAndSize(NULL
, howMany
);
7005 if (result
!= NULL
) {
7006 /* Get random data */
7007 memset(PyBytes_AS_STRING(result
), 0, howMany
); /* zero seed */
7008 if (! pCryptGenRandom(hCryptProv
, howMany
, (unsigned char*)
7009 PyBytes_AS_STRING(result
))) {
7011 return win32_error("CryptGenRandom", NULL
);
7018 PyDoc_STRVAR(device_encoding__doc__
,
7019 "device_encoding(fd) -> str\n\n\
7020 Return a string describing the encoding of the device\n\
7021 if the output is a terminal; else return None.");
7024 device_encoding(PyObject
*self
, PyObject
*args
)
7027 if (!PyArg_ParseTuple(args
, "i:device_encoding", &fd
))
7029 if (!_PyVerify_fd(fd
) || !isatty(fd
)) {
7033 #if defined(MS_WINDOWS) || defined(MS_WIN64)
7036 sprintf(buf
, "cp%d", GetConsoleCP());
7037 return PyUnicode_FromString(buf
);
7039 if (fd
== 1 || fd
== 2) {
7041 sprintf(buf
, "cp%d", GetConsoleOutputCP());
7042 return PyUnicode_FromString(buf
);
7044 #elif defined(CODESET)
7046 char *codeset
= nl_langinfo(CODESET
);
7047 if (codeset
!= NULL
&& codeset
[0] != 0)
7048 return PyUnicode_FromString(codeset
);
7056 /* Use openssl random routine */
7057 #include <openssl/rand.h>
7058 PyDoc_STRVAR(vms_urandom__doc__
,
7059 "urandom(n) -> str\n\n\
7060 Return n random bytes suitable for cryptographic use.");
7063 vms_urandom(PyObject
*self
, PyObject
*args
)
7068 /* Read arguments */
7069 if (! PyArg_ParseTuple(args
, "i:urandom", &howMany
))
7072 return PyErr_Format(PyExc_ValueError
,
7073 "negative argument not allowed");
7075 /* Allocate bytes */
7076 result
= PyBytes_FromStringAndSize(NULL
, howMany
);
7077 if (result
!= NULL
) {
7078 /* Get random data */
7079 if (RAND_pseudo_bytes((unsigned char*)
7080 PyBytes_AS_STRING(result
),
7083 return PyErr_Format(PyExc_ValueError
,
7084 "RAND_pseudo_bytes");
7091 static PyMethodDef posix_methods
[] = {
7092 {"access", posix_access
, METH_VARARGS
, posix_access__doc__
},
7094 {"ttyname", posix_ttyname
, METH_VARARGS
, posix_ttyname__doc__
},
7096 {"chdir", posix_chdir
, METH_VARARGS
, posix_chdir__doc__
},
7098 {"chflags", posix_chflags
, METH_VARARGS
, posix_chflags__doc__
},
7099 #endif /* HAVE_CHFLAGS */
7100 {"chmod", posix_chmod
, METH_VARARGS
, posix_chmod__doc__
},
7102 {"fchmod", posix_fchmod
, METH_VARARGS
, posix_fchmod__doc__
},
7103 #endif /* HAVE_FCHMOD */
7105 {"chown", posix_chown
, METH_VARARGS
, posix_chown__doc__
},
7106 #endif /* HAVE_CHOWN */
7108 {"lchmod", posix_lchmod
, METH_VARARGS
, posix_lchmod__doc__
},
7109 #endif /* HAVE_LCHMOD */
7111 {"fchown", posix_fchown
, METH_VARARGS
, posix_fchown__doc__
},
7112 #endif /* HAVE_FCHOWN */
7113 #ifdef HAVE_LCHFLAGS
7114 {"lchflags", posix_lchflags
, METH_VARARGS
, posix_lchflags__doc__
},
7115 #endif /* HAVE_LCHFLAGS */
7117 {"lchown", posix_lchown
, METH_VARARGS
, posix_lchown__doc__
},
7118 #endif /* HAVE_LCHOWN */
7120 {"chroot", posix_chroot
, METH_VARARGS
, posix_chroot__doc__
},
7123 {"ctermid", posix_ctermid
, METH_NOARGS
, posix_ctermid__doc__
},
7126 {"getcwd", (PyCFunction
)posix_getcwd_unicode
,
7127 METH_NOARGS
, posix_getcwd__doc__
},
7128 {"getcwdb", (PyCFunction
)posix_getcwd_bytes
,
7129 METH_NOARGS
, posix_getcwdb__doc__
},
7132 {"link", posix_link
, METH_VARARGS
, posix_link__doc__
},
7133 #endif /* HAVE_LINK */
7134 {"listdir", posix_listdir
, METH_VARARGS
, posix_listdir__doc__
},
7135 {"lstat", posix_lstat
, METH_VARARGS
, posix_lstat__doc__
},
7136 {"mkdir", posix_mkdir
, METH_VARARGS
, posix_mkdir__doc__
},
7138 {"nice", posix_nice
, METH_VARARGS
, posix_nice__doc__
},
7139 #endif /* HAVE_NICE */
7140 #ifdef HAVE_READLINK
7141 {"readlink", posix_readlink
, METH_VARARGS
, posix_readlink__doc__
},
7142 #endif /* HAVE_READLINK */
7143 {"rename", posix_rename
, METH_VARARGS
, posix_rename__doc__
},
7144 {"rmdir", posix_rmdir
, METH_VARARGS
, posix_rmdir__doc__
},
7145 {"stat", posix_stat
, METH_VARARGS
, posix_stat__doc__
},
7146 {"stat_float_times", stat_float_times
, METH_VARARGS
, stat_float_times__doc__
},
7148 {"symlink", posix_symlink
, METH_VARARGS
, posix_symlink__doc__
},
7149 #endif /* HAVE_SYMLINK */
7151 {"system", posix_system
, METH_VARARGS
, posix_system__doc__
},
7153 {"umask", posix_umask
, METH_VARARGS
, posix_umask__doc__
},
7155 {"uname", posix_uname
, METH_NOARGS
, posix_uname__doc__
},
7156 #endif /* HAVE_UNAME */
7157 {"unlink", posix_unlink
, METH_VARARGS
, posix_unlink__doc__
},
7158 {"remove", posix_unlink
, METH_VARARGS
, posix_remove__doc__
},
7159 {"utime", posix_utime
, METH_VARARGS
, posix_utime__doc__
},
7161 {"times", posix_times
, METH_NOARGS
, posix_times__doc__
},
7162 #endif /* HAVE_TIMES */
7163 {"_exit", posix__exit
, METH_VARARGS
, posix__exit__doc__
},
7165 {"execv", posix_execv
, METH_VARARGS
, posix_execv__doc__
},
7166 {"execve", posix_execve
, METH_VARARGS
, posix_execve__doc__
},
7167 #endif /* HAVE_EXECV */
7169 {"spawnv", posix_spawnv
, METH_VARARGS
, posix_spawnv__doc__
},
7170 {"spawnve", posix_spawnve
, METH_VARARGS
, posix_spawnve__doc__
},
7171 #if defined(PYOS_OS2)
7172 {"spawnvp", posix_spawnvp
, METH_VARARGS
, posix_spawnvp__doc__
},
7173 {"spawnvpe", posix_spawnvpe
, METH_VARARGS
, posix_spawnvpe__doc__
},
7174 #endif /* PYOS_OS2 */
7175 #endif /* HAVE_SPAWNV */
7177 {"fork1", posix_fork1
, METH_NOARGS
, posix_fork1__doc__
},
7178 #endif /* HAVE_FORK1 */
7180 {"fork", posix_fork
, METH_NOARGS
, posix_fork__doc__
},
7181 #endif /* HAVE_FORK */
7182 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
7183 {"openpty", posix_openpty
, METH_NOARGS
, posix_openpty__doc__
},
7184 #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
7186 {"forkpty", posix_forkpty
, METH_NOARGS
, posix_forkpty__doc__
},
7187 #endif /* HAVE_FORKPTY */
7189 {"getegid", posix_getegid
, METH_NOARGS
, posix_getegid__doc__
},
7190 #endif /* HAVE_GETEGID */
7192 {"geteuid", posix_geteuid
, METH_NOARGS
, posix_geteuid__doc__
},
7193 #endif /* HAVE_GETEUID */
7195 {"getgid", posix_getgid
, METH_NOARGS
, posix_getgid__doc__
},
7196 #endif /* HAVE_GETGID */
7197 #ifdef HAVE_GETGROUPS
7198 {"getgroups", posix_getgroups
, METH_NOARGS
, posix_getgroups__doc__
},
7200 {"getpid", posix_getpid
, METH_NOARGS
, posix_getpid__doc__
},
7202 {"getpgrp", posix_getpgrp
, METH_NOARGS
, posix_getpgrp__doc__
},
7203 #endif /* HAVE_GETPGRP */
7205 {"getppid", posix_getppid
, METH_NOARGS
, posix_getppid__doc__
},
7206 #endif /* HAVE_GETPPID */
7208 {"getuid", posix_getuid
, METH_NOARGS
, posix_getuid__doc__
},
7209 #endif /* HAVE_GETUID */
7210 #ifdef HAVE_GETLOGIN
7211 {"getlogin", posix_getlogin
, METH_NOARGS
, posix_getlogin__doc__
},
7214 {"kill", posix_kill
, METH_VARARGS
, posix_kill__doc__
},
7215 #endif /* HAVE_KILL */
7217 {"killpg", posix_killpg
, METH_VARARGS
, posix_killpg__doc__
},
7218 #endif /* HAVE_KILLPG */
7220 {"plock", posix_plock
, METH_VARARGS
, posix_plock__doc__
},
7221 #endif /* HAVE_PLOCK */
7223 {"startfile", win32_startfile
, METH_VARARGS
, win32_startfile__doc__
},
7226 {"setuid", posix_setuid
, METH_VARARGS
, posix_setuid__doc__
},
7227 #endif /* HAVE_SETUID */
7229 {"seteuid", posix_seteuid
, METH_VARARGS
, posix_seteuid__doc__
},
7230 #endif /* HAVE_SETEUID */
7232 {"setegid", posix_setegid
, METH_VARARGS
, posix_setegid__doc__
},
7233 #endif /* HAVE_SETEGID */
7234 #ifdef HAVE_SETREUID
7235 {"setreuid", posix_setreuid
, METH_VARARGS
, posix_setreuid__doc__
},
7236 #endif /* HAVE_SETREUID */
7237 #ifdef HAVE_SETREGID
7238 {"setregid", posix_setregid
, METH_VARARGS
, posix_setregid__doc__
},
7239 #endif /* HAVE_SETREGID */
7241 {"setgid", posix_setgid
, METH_VARARGS
, posix_setgid__doc__
},
7242 #endif /* HAVE_SETGID */
7243 #ifdef HAVE_SETGROUPS
7244 {"setgroups", posix_setgroups
, METH_O
, posix_setgroups__doc__
},
7245 #endif /* HAVE_SETGROUPS */
7247 {"getpgid", posix_getpgid
, METH_VARARGS
, posix_getpgid__doc__
},
7248 #endif /* HAVE_GETPGID */
7250 {"setpgrp", posix_setpgrp
, METH_NOARGS
, posix_setpgrp__doc__
},
7251 #endif /* HAVE_SETPGRP */
7253 {"wait", posix_wait
, METH_NOARGS
, posix_wait__doc__
},
7254 #endif /* HAVE_WAIT */
7256 {"wait3", posix_wait3
, METH_VARARGS
, posix_wait3__doc__
},
7257 #endif /* HAVE_WAIT3 */
7259 {"wait4", posix_wait4
, METH_VARARGS
, posix_wait4__doc__
},
7260 #endif /* HAVE_WAIT4 */
7261 #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
7262 {"waitpid", posix_waitpid
, METH_VARARGS
, posix_waitpid__doc__
},
7263 #endif /* HAVE_WAITPID */
7265 {"getsid", posix_getsid
, METH_VARARGS
, posix_getsid__doc__
},
7266 #endif /* HAVE_GETSID */
7268 {"setsid", posix_setsid
, METH_NOARGS
, posix_setsid__doc__
},
7269 #endif /* HAVE_SETSID */
7271 {"setpgid", posix_setpgid
, METH_VARARGS
, posix_setpgid__doc__
},
7272 #endif /* HAVE_SETPGID */
7273 #ifdef HAVE_TCGETPGRP
7274 {"tcgetpgrp", posix_tcgetpgrp
, METH_VARARGS
, posix_tcgetpgrp__doc__
},
7275 #endif /* HAVE_TCGETPGRP */
7276 #ifdef HAVE_TCSETPGRP
7277 {"tcsetpgrp", posix_tcsetpgrp
, METH_VARARGS
, posix_tcsetpgrp__doc__
},
7278 #endif /* HAVE_TCSETPGRP */
7279 {"open", posix_open
, METH_VARARGS
, posix_open__doc__
},
7280 {"close", posix_close
, METH_VARARGS
, posix_close__doc__
},
7281 {"closerange", posix_closerange
, METH_VARARGS
, posix_closerange__doc__
},
7282 {"device_encoding", device_encoding
, METH_VARARGS
, device_encoding__doc__
},
7283 {"dup", posix_dup
, METH_VARARGS
, posix_dup__doc__
},
7284 {"dup2", posix_dup2
, METH_VARARGS
, posix_dup2__doc__
},
7285 {"lseek", posix_lseek
, METH_VARARGS
, posix_lseek__doc__
},
7286 {"read", posix_read
, METH_VARARGS
, posix_read__doc__
},
7287 {"write", posix_write
, METH_VARARGS
, posix_write__doc__
},
7288 {"fstat", posix_fstat
, METH_VARARGS
, posix_fstat__doc__
},
7289 {"isatty", posix_isatty
, METH_VARARGS
, posix_isatty__doc__
},
7291 {"pipe", posix_pipe
, METH_NOARGS
, posix_pipe__doc__
},
7294 {"mkfifo", posix_mkfifo
, METH_VARARGS
, posix_mkfifo__doc__
},
7296 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
7297 {"mknod", posix_mknod
, METH_VARARGS
, posix_mknod__doc__
},
7299 #ifdef HAVE_DEVICE_MACROS
7300 {"major", posix_major
, METH_VARARGS
, posix_major__doc__
},
7301 {"minor", posix_minor
, METH_VARARGS
, posix_minor__doc__
},
7302 {"makedev", posix_makedev
, METH_VARARGS
, posix_makedev__doc__
},
7304 #ifdef HAVE_FTRUNCATE
7305 {"ftruncate", posix_ftruncate
, METH_VARARGS
, posix_ftruncate__doc__
},
7308 {"putenv", posix_putenv
, METH_VARARGS
, posix_putenv__doc__
},
7310 #ifdef HAVE_UNSETENV
7311 {"unsetenv", posix_unsetenv
, METH_VARARGS
, posix_unsetenv__doc__
},
7313 {"strerror", posix_strerror
, METH_VARARGS
, posix_strerror__doc__
},
7315 {"fchdir", posix_fchdir
, METH_O
, posix_fchdir__doc__
},
7318 {"fsync", posix_fsync
, METH_O
, posix_fsync__doc__
},
7320 #ifdef HAVE_FDATASYNC
7321 {"fdatasync", posix_fdatasync
, METH_O
, posix_fdatasync__doc__
},
7323 #ifdef HAVE_SYS_WAIT_H
7325 {"WCOREDUMP", posix_WCOREDUMP
, METH_VARARGS
, posix_WCOREDUMP__doc__
},
7326 #endif /* WCOREDUMP */
7328 {"WIFCONTINUED",posix_WIFCONTINUED
, METH_VARARGS
, posix_WIFCONTINUED__doc__
},
7329 #endif /* WIFCONTINUED */
7331 {"WIFSTOPPED", posix_WIFSTOPPED
, METH_VARARGS
, posix_WIFSTOPPED__doc__
},
7332 #endif /* WIFSTOPPED */
7334 {"WIFSIGNALED", posix_WIFSIGNALED
, METH_VARARGS
, posix_WIFSIGNALED__doc__
},
7335 #endif /* WIFSIGNALED */
7337 {"WIFEXITED", posix_WIFEXITED
, METH_VARARGS
, posix_WIFEXITED__doc__
},
7338 #endif /* WIFEXITED */
7340 {"WEXITSTATUS", posix_WEXITSTATUS
, METH_VARARGS
, posix_WEXITSTATUS__doc__
},
7341 #endif /* WEXITSTATUS */
7343 {"WTERMSIG", posix_WTERMSIG
, METH_VARARGS
, posix_WTERMSIG__doc__
},
7344 #endif /* WTERMSIG */
7346 {"WSTOPSIG", posix_WSTOPSIG
, METH_VARARGS
, posix_WSTOPSIG__doc__
},
7347 #endif /* WSTOPSIG */
7348 #endif /* HAVE_SYS_WAIT_H */
7349 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
7350 {"fstatvfs", posix_fstatvfs
, METH_VARARGS
, posix_fstatvfs__doc__
},
7352 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
7353 {"statvfs", posix_statvfs
, METH_VARARGS
, posix_statvfs__doc__
},
7356 {"confstr", posix_confstr
, METH_VARARGS
, posix_confstr__doc__
},
7359 {"sysconf", posix_sysconf
, METH_VARARGS
, posix_sysconf__doc__
},
7361 #ifdef HAVE_FPATHCONF
7362 {"fpathconf", posix_fpathconf
, METH_VARARGS
, posix_fpathconf__doc__
},
7364 #ifdef HAVE_PATHCONF
7365 {"pathconf", posix_pathconf
, METH_VARARGS
, posix_pathconf__doc__
},
7367 {"abort", posix_abort
, METH_NOARGS
, posix_abort__doc__
},
7369 {"_getfullpathname", posix__getfullpathname
, METH_VARARGS
, NULL
},
7371 #ifdef HAVE_GETLOADAVG
7372 {"getloadavg", posix_getloadavg
, METH_NOARGS
, posix_getloadavg__doc__
},
7375 {"urandom", win32_urandom
, METH_VARARGS
, win32_urandom__doc__
},
7378 {"urandom", vms_urandom
, METH_VARARGS
, vms_urandom__doc__
},
7380 {NULL
, NULL
} /* Sentinel */
7385 ins(PyObject
*module
, char *symbol
, long value
)
7387 return PyModule_AddIntConstant(module
, symbol
, value
);
7390 #if defined(PYOS_OS2)
7391 /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
7392 static int insertvalues(PyObject
*module
)
7395 ULONG values
[QSV_MAX
+1];
7399 Py_BEGIN_ALLOW_THREADS
7400 rc
= DosQuerySysInfo(1L, QSV_MAX
, &values
[1], sizeof(ULONG
) * QSV_MAX
);
7401 Py_END_ALLOW_THREADS
7403 if (rc
!= NO_ERROR
) {
7408 if (ins(module
, "meminstalled", values
[QSV_TOTPHYSMEM
])) return -1;
7409 if (ins(module
, "memkernel", values
[QSV_TOTRESMEM
])) return -1;
7410 if (ins(module
, "memvirtual", values
[QSV_TOTAVAILMEM
])) return -1;
7411 if (ins(module
, "maxpathlen", values
[QSV_MAX_PATH_LENGTH
])) return -1;
7412 if (ins(module
, "maxnamelen", values
[QSV_MAX_COMP_LENGTH
])) return -1;
7413 if (ins(module
, "revision", values
[QSV_VERSION_REVISION
])) return -1;
7414 if (ins(module
, "timeslice", values
[QSV_MIN_SLICE
])) return -1;
7416 switch (values
[QSV_VERSION_MINOR
]) {
7417 case 0: ver
= "2.00"; break;
7418 case 10: ver
= "2.10"; break;
7419 case 11: ver
= "2.11"; break;
7420 case 30: ver
= "3.00"; break;
7421 case 40: ver
= "4.00"; break;
7422 case 50: ver
= "5.00"; break;
7424 PyOS_snprintf(tmp
, sizeof(tmp
),
7425 "%d-%d", values
[QSV_VERSION_MAJOR
],
7426 values
[QSV_VERSION_MINOR
]);
7430 /* Add Indicator of the Version of the Operating System */
7431 if (PyModule_AddStringConstant(module
, "version", tmp
) < 0)
7434 /* Add Indicator of Which Drive was Used to Boot the System */
7435 tmp
[0] = 'A' + values
[QSV_BOOT_DRIVE
] - 1;
7439 return PyModule_AddStringConstant(module
, "bootdrive", tmp
);
7444 all_ins(PyObject
*d
)
7447 if (ins(d
, "F_OK", (long)F_OK
)) return -1;
7450 if (ins(d
, "R_OK", (long)R_OK
)) return -1;
7453 if (ins(d
, "W_OK", (long)W_OK
)) return -1;
7456 if (ins(d
, "X_OK", (long)X_OK
)) return -1;
7459 if (ins(d
, "NGROUPS_MAX", (long)NGROUPS_MAX
)) return -1;
7462 if (ins(d
, "TMP_MAX", (long)TMP_MAX
)) return -1;
7465 if (ins(d
, "WCONTINUED", (long)WCONTINUED
)) return -1;
7468 if (ins(d
, "WNOHANG", (long)WNOHANG
)) return -1;
7471 if (ins(d
, "WUNTRACED", (long)WUNTRACED
)) return -1;
7474 if (ins(d
, "O_RDONLY", (long)O_RDONLY
)) return -1;
7477 if (ins(d
, "O_WRONLY", (long)O_WRONLY
)) return -1;
7480 if (ins(d
, "O_RDWR", (long)O_RDWR
)) return -1;
7483 if (ins(d
, "O_NDELAY", (long)O_NDELAY
)) return -1;
7486 if (ins(d
, "O_NONBLOCK", (long)O_NONBLOCK
)) return -1;
7489 if (ins(d
, "O_APPEND", (long)O_APPEND
)) return -1;
7492 if (ins(d
, "O_DSYNC", (long)O_DSYNC
)) return -1;
7495 if (ins(d
, "O_RSYNC", (long)O_RSYNC
)) return -1;
7498 if (ins(d
, "O_SYNC", (long)O_SYNC
)) return -1;
7501 if (ins(d
, "O_NOCTTY", (long)O_NOCTTY
)) return -1;
7504 if (ins(d
, "O_CREAT", (long)O_CREAT
)) return -1;
7507 if (ins(d
, "O_EXCL", (long)O_EXCL
)) return -1;
7510 if (ins(d
, "O_TRUNC", (long)O_TRUNC
)) return -1;
7513 if (ins(d
, "O_BINARY", (long)O_BINARY
)) return -1;
7516 if (ins(d
, "O_TEXT", (long)O_TEXT
)) return -1;
7519 if (ins(d
, "O_LARGEFILE", (long)O_LARGEFILE
)) return -1;
7522 if (ins(d
, "O_SHLOCK", (long)O_SHLOCK
)) return -1;
7525 if (ins(d
, "O_EXLOCK", (long)O_EXLOCK
)) return -1;
7530 /* Don't inherit in child processes. */
7531 if (ins(d
, "O_NOINHERIT", (long)O_NOINHERIT
)) return -1;
7533 #ifdef _O_SHORT_LIVED
7534 /* Optimize for short life (keep in memory). */
7535 /* MS forgot to define this one with a non-underscore form too. */
7536 if (ins(d
, "O_SHORT_LIVED", (long)_O_SHORT_LIVED
)) return -1;
7539 /* Automatically delete when last handle is closed. */
7540 if (ins(d
, "O_TEMPORARY", (long)O_TEMPORARY
)) return -1;
7543 /* Optimize for random access. */
7544 if (ins(d
, "O_RANDOM", (long)O_RANDOM
)) return -1;
7547 /* Optimize for sequential access. */
7548 if (ins(d
, "O_SEQUENTIAL", (long)O_SEQUENTIAL
)) return -1;
7551 /* GNU extensions. */
7553 /* Send a SIGIO signal whenever input or output
7554 becomes available on file descriptor */
7555 if (ins(d
, "O_ASYNC", (long)O_ASYNC
)) return -1;
7558 /* Direct disk access. */
7559 if (ins(d
, "O_DIRECT", (long)O_DIRECT
)) return -1;
7562 /* Must be a directory. */
7563 if (ins(d
, "O_DIRECTORY", (long)O_DIRECTORY
)) return -1;
7566 /* Do not follow links. */
7567 if (ins(d
, "O_NOFOLLOW", (long)O_NOFOLLOW
)) return -1;
7570 /* Do not update the access time. */
7571 if (ins(d
, "O_NOATIME", (long)O_NOATIME
)) return -1;
7574 /* These come from sysexits.h */
7576 if (ins(d
, "EX_OK", (long)EX_OK
)) return -1;
7579 if (ins(d
, "EX_USAGE", (long)EX_USAGE
)) return -1;
7580 #endif /* EX_USAGE */
7582 if (ins(d
, "EX_DATAERR", (long)EX_DATAERR
)) return -1;
7583 #endif /* EX_DATAERR */
7585 if (ins(d
, "EX_NOINPUT", (long)EX_NOINPUT
)) return -1;
7586 #endif /* EX_NOINPUT */
7588 if (ins(d
, "EX_NOUSER", (long)EX_NOUSER
)) return -1;
7589 #endif /* EX_NOUSER */
7591 if (ins(d
, "EX_NOHOST", (long)EX_NOHOST
)) return -1;
7592 #endif /* EX_NOHOST */
7593 #ifdef EX_UNAVAILABLE
7594 if (ins(d
, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE
)) return -1;
7595 #endif /* EX_UNAVAILABLE */
7597 if (ins(d
, "EX_SOFTWARE", (long)EX_SOFTWARE
)) return -1;
7598 #endif /* EX_SOFTWARE */
7600 if (ins(d
, "EX_OSERR", (long)EX_OSERR
)) return -1;
7601 #endif /* EX_OSERR */
7603 if (ins(d
, "EX_OSFILE", (long)EX_OSFILE
)) return -1;
7604 #endif /* EX_OSFILE */
7606 if (ins(d
, "EX_CANTCREAT", (long)EX_CANTCREAT
)) return -1;
7607 #endif /* EX_CANTCREAT */
7609 if (ins(d
, "EX_IOERR", (long)EX_IOERR
)) return -1;
7610 #endif /* EX_IOERR */
7612 if (ins(d
, "EX_TEMPFAIL", (long)EX_TEMPFAIL
)) return -1;
7613 #endif /* EX_TEMPFAIL */
7615 if (ins(d
, "EX_PROTOCOL", (long)EX_PROTOCOL
)) return -1;
7616 #endif /* EX_PROTOCOL */
7618 if (ins(d
, "EX_NOPERM", (long)EX_NOPERM
)) return -1;
7619 #endif /* EX_NOPERM */
7621 if (ins(d
, "EX_CONFIG", (long)EX_CONFIG
)) return -1;
7622 #endif /* EX_CONFIG */
7624 if (ins(d
, "EX_NOTFOUND", (long)EX_NOTFOUND
)) return -1;
7625 #endif /* EX_NOTFOUND */
7628 #if defined(PYOS_OS2) && defined(PYCC_GCC)
7629 if (ins(d
, "P_WAIT", (long)P_WAIT
)) return -1;
7630 if (ins(d
, "P_NOWAIT", (long)P_NOWAIT
)) return -1;
7631 if (ins(d
, "P_OVERLAY", (long)P_OVERLAY
)) return -1;
7632 if (ins(d
, "P_DEBUG", (long)P_DEBUG
)) return -1;
7633 if (ins(d
, "P_SESSION", (long)P_SESSION
)) return -1;
7634 if (ins(d
, "P_DETACH", (long)P_DETACH
)) return -1;
7635 if (ins(d
, "P_PM", (long)P_PM
)) return -1;
7636 if (ins(d
, "P_DEFAULT", (long)P_DEFAULT
)) return -1;
7637 if (ins(d
, "P_MINIMIZE", (long)P_MINIMIZE
)) return -1;
7638 if (ins(d
, "P_MAXIMIZE", (long)P_MAXIMIZE
)) return -1;
7639 if (ins(d
, "P_FULLSCREEN", (long)P_FULLSCREEN
)) return -1;
7640 if (ins(d
, "P_WINDOWED", (long)P_WINDOWED
)) return -1;
7641 if (ins(d
, "P_FOREGROUND", (long)P_FOREGROUND
)) return -1;
7642 if (ins(d
, "P_BACKGROUND", (long)P_BACKGROUND
)) return -1;
7643 if (ins(d
, "P_NOCLOSE", (long)P_NOCLOSE
)) return -1;
7644 if (ins(d
, "P_NOSESSION", (long)P_NOSESSION
)) return -1;
7645 if (ins(d
, "P_QUOTE", (long)P_QUOTE
)) return -1;
7646 if (ins(d
, "P_TILDE", (long)P_TILDE
)) return -1;
7647 if (ins(d
, "P_UNRELATED", (long)P_UNRELATED
)) return -1;
7648 if (ins(d
, "P_DEBUGDESC", (long)P_DEBUGDESC
)) return -1;
7650 if (ins(d
, "P_WAIT", (long)_P_WAIT
)) return -1;
7651 if (ins(d
, "P_NOWAIT", (long)_P_NOWAIT
)) return -1;
7652 if (ins(d
, "P_OVERLAY", (long)_OLD_P_OVERLAY
)) return -1;
7653 if (ins(d
, "P_NOWAITO", (long)_P_NOWAITO
)) return -1;
7654 if (ins(d
, "P_DETACH", (long)_P_DETACH
)) return -1;
7658 #if defined(PYOS_OS2)
7659 if (insertvalues(d
)) return -1;
7665 #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
7666 #define INITFUNC PyInit_nt
7667 #define MODNAME "nt"
7669 #elif defined(PYOS_OS2)
7670 #define INITFUNC PyInit_os2
7671 #define MODNAME "os2"
7674 #define INITFUNC PyInit_posix
7675 #define MODNAME "posix"
7678 static struct PyModuleDef posixmodule
= {
7679 PyModuleDef_HEAD_INIT
,
7696 m
= PyModule_Create(&posixmodule
);
7700 /* Initialize environ dictionary */
7701 v
= convertenviron();
7703 if (v
== NULL
|| PyModule_AddObject(m
, "environ", v
) != 0)
7710 if (setup_confname_tables(m
))
7713 Py_INCREF(PyExc_OSError
);
7714 PyModule_AddObject(m
, "error", PyExc_OSError
);
7717 if (posix_putenv_garbage
== NULL
)
7718 posix_putenv_garbage
= PyDict_New();
7722 stat_result_desc
.name
= MODNAME
".stat_result";
7723 stat_result_desc
.fields
[7].name
= PyStructSequence_UnnamedField
;
7724 stat_result_desc
.fields
[8].name
= PyStructSequence_UnnamedField
;
7725 stat_result_desc
.fields
[9].name
= PyStructSequence_UnnamedField
;
7726 PyStructSequence_InitType(&StatResultType
, &stat_result_desc
);
7727 structseq_new
= StatResultType
.tp_new
;
7728 StatResultType
.tp_new
= statresult_new
;
7730 statvfs_result_desc
.name
= MODNAME
".statvfs_result";
7731 PyStructSequence_InitType(&StatVFSResultType
, &statvfs_result_desc
);
7732 #ifdef NEED_TICKS_PER_SECOND
7733 # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7734 ticks_per_second
= sysconf(_SC_CLK_TCK
);
7736 ticks_per_second
= HZ
;
7738 ticks_per_second
= 60; /* magic fallback value; may be bogus */
7742 Py_INCREF((PyObject
*) &StatResultType
);
7743 PyModule_AddObject(m
, "stat_result", (PyObject
*) &StatResultType
);
7744 Py_INCREF((PyObject
*) &StatVFSResultType
);
7745 PyModule_AddObject(m
, "statvfs_result",
7746 (PyObject
*) &StatVFSResultType
);
7751 * Step 2 of weak-linking support on Mac OS X.
7753 * The code below removes functions that are not available on the
7754 * currently active platform.
7756 * This block allow one to use a python binary that was build on
7757 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7760 #ifdef HAVE_FSTATVFS
7761 if (fstatvfs
== NULL
) {
7762 if (PyObject_DelAttrString(m
, "fstatvfs") == -1) {
7766 #endif /* HAVE_FSTATVFS */
7769 if (statvfs
== NULL
) {
7770 if (PyObject_DelAttrString(m
, "statvfs") == -1) {
7774 #endif /* HAVE_STATVFS */
7777 if (lchown
== NULL
) {
7778 if (PyObject_DelAttrString(m
, "lchown") == -1) {
7782 #endif /* HAVE_LCHOWN */
7785 #endif /* __APPLE__ */