| Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ | 
|  | 3 |  | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 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 | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used.  Different compilers define their own feature | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 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. */ | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 14 | /* See also ../Dos/dosmodule.c */ | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 15 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 16 | #ifdef __APPLE__ | 
|  | 17 | /* | 
|  | 18 | * Step 1 of support for weak-linking a number of symbols existing on | 
|  | 19 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block | 
|  | 20 | * at the end of this file for more information. | 
|  | 21 | */ | 
|  | 22 | #  pragma weak lchown | 
|  | 23 | #  pragma weak statvfs | 
|  | 24 | #  pragma weak fstatvfs | 
|  | 25 |  | 
|  | 26 | #endif /* __APPLE__ */ | 
|  | 27 |  | 
| Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 28 | #define PY_SSIZE_T_CLEAN | 
|  | 29 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 30 | #include "Python.h" | 
|  | 31 | #include "structseq.h" | 
|  | 32 |  | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 33 | #if defined(__VMS) | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 34 | #    include <unixio.h> | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 35 | #endif /* defined(__VMS) */ | 
|  | 36 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 37 | #ifdef __cplusplus | 
|  | 38 | extern "C" { | 
|  | 39 | #endif | 
|  | 40 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 41 | PyDoc_STRVAR(posix__doc__, | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 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\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 45 | corresponding Unix manual entries for more information on calls."); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 |  | 
| Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 47 |  | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 48 | #if defined(PYOS_OS2) | 
|  | 49 | #define  INCL_DOS | 
|  | 50 | #define  INCL_DOSERRORS | 
|  | 51 | #define  INCL_DOSPROCESS | 
|  | 52 | #define  INCL_NOPMAPI | 
|  | 53 | #include <os2.h> | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 54 | #if defined(PYCC_GCC) | 
|  | 55 | #include <ctype.h> | 
|  | 56 | #include <io.h> | 
|  | 57 | #include <stdio.h> | 
|  | 58 | #include <process.h> | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 59 | #endif | 
| Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 60 | #include "osdefs.h" | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 61 | #endif | 
|  | 62 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 63 | #ifdef HAVE_SYS_TYPES_H | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 64 | #include <sys/types.h> | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 65 | #endif /* HAVE_SYS_TYPES_H */ | 
|  | 66 |  | 
|  | 67 | #ifdef HAVE_SYS_STAT_H | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 68 | #include <sys/stat.h> | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 69 | #endif /* HAVE_SYS_STAT_H */ | 
| Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 70 |  | 
| Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 71 | #ifdef HAVE_SYS_WAIT_H | 
|  | 72 | #include <sys/wait.h>		/* For WNOHANG */ | 
|  | 73 | #endif | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 74 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | #ifdef HAVE_SIGNAL_H | 
| Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 76 | #include <signal.h> | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 77 | #endif | 
| Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 78 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 79 | #ifdef HAVE_FCNTL_H | 
|  | 80 | #include <fcntl.h> | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 81 | #endif /* HAVE_FCNTL_H */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 |  | 
| Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 83 | #ifdef HAVE_GRP_H | 
|  | 84 | #include <grp.h> | 
|  | 85 | #endif | 
|  | 86 |  | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 87 | #ifdef HAVE_SYSEXITS_H | 
|  | 88 | #include <sysexits.h> | 
|  | 89 | #endif /* HAVE_SYSEXITS_H */ | 
|  | 90 |  | 
| Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 91 | #ifdef HAVE_SYS_LOADAVG_H | 
|  | 92 | #include <sys/loadavg.h> | 
|  | 93 | #endif | 
|  | 94 |  | 
| Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 95 | #ifdef HAVE_LANGINFO_H | 
|  | 96 | #include <langinfo.h> | 
|  | 97 | #endif | 
|  | 98 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 99 | /* Various compilers have only certain posix functions */ | 
| Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 100 | /* XXX Gosh I wish these were all moved into pyconfig.h */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 101 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 102 | #include <process.h> | 
|  | 103 | #else | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 104 | #if defined(__WATCOMC__) && !defined(__QNX__)		/* Watcom compiler */ | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 105 | #define HAVE_GETCWD     1 | 
|  | 106 | #define HAVE_OPENDIR    1 | 
|  | 107 | #define HAVE_SYSTEM	1 | 
|  | 108 | #if defined(__OS2__) | 
|  | 109 | #define HAVE_EXECV      1 | 
|  | 110 | #define HAVE_WAIT       1 | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 111 | #endif | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 112 | #include <process.h> | 
|  | 113 | #else | 
|  | 114 | #ifdef __BORLANDC__		/* Borland compiler */ | 
|  | 115 | #define HAVE_EXECV      1 | 
|  | 116 | #define HAVE_GETCWD     1 | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 117 | #define HAVE_OPENDIR    1 | 
|  | 118 | #define HAVE_PIPE       1 | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 119 | #define HAVE_SYSTEM	1 | 
|  | 120 | #define HAVE_WAIT       1 | 
|  | 121 | #else | 
|  | 122 | #ifdef _MSC_VER		/* Microsoft compiler */ | 
| Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 123 | #define HAVE_GETCWD     1 | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 124 | #define HAVE_SPAWNV	1 | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 125 | #define HAVE_EXECV      1 | 
|  | 126 | #define HAVE_PIPE       1 | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 127 | #define HAVE_SYSTEM	1 | 
| Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 128 | #define HAVE_CWAIT	1 | 
| Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 129 | #define HAVE_FSYNC	1 | 
|  | 130 | #define fsync _commit | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 131 | #else | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 132 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) | 
|  | 133 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #else			/* all other compilers */ | 
|  | 135 | /* Unix functions that the configure script doesn't check for */ | 
|  | 136 | #define HAVE_EXECV      1 | 
|  | 137 | #define HAVE_FORK       1 | 
| Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 138 | #if defined(__USLC__) && defined(__SCO_VERSION__)	/* SCO UDK Compiler */ | 
|  | 139 | #define HAVE_FORK1      1 | 
|  | 140 | #endif | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 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 | 
|  | 147 | #define HAVE_KILL       1 | 
|  | 148 | #define HAVE_OPENDIR    1 | 
|  | 149 | #define HAVE_PIPE       1 | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 150 | #define HAVE_SYSTEM	1 | 
|  | 151 | #define HAVE_WAIT       1 | 
| Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 152 | #define HAVE_TTYNAME	1 | 
| Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 153 | #endif  /* PYOS_OS2 && PYCC_GCC && __VMS */ | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 154 | #endif  /* _MSC_VER */ | 
|  | 155 | #endif  /* __BORLANDC__ */ | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 156 | #endif  /* ! __WATCOMC__ || __QNX__ */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 157 | #endif /* ! __IBMC__ */ | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 158 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 159 | #ifndef _MSC_VER | 
| Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 160 |  | 
| Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 161 | #if defined(__sgi)&&_COMPILER_VERSION>=700 | 
|  | 162 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode | 
|  | 163 | (default) */ | 
|  | 164 | extern char        *ctermid_r(char *); | 
|  | 165 | #endif | 
|  | 166 |  | 
| Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 167 | #ifndef HAVE_UNISTD_H | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 168 | #if defined(PYCC_VACPP) | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int mkdir(char *); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 170 | #else | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 171 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 172 | extern int mkdir(const char *); | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 173 | #else | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 174 | extern int mkdir(const char *, mode_t); | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 175 | #endif | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 176 | #endif | 
|  | 177 | #if defined(__IBMC__) || defined(__IBMCPP__) | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 178 | extern int chdir(char *); | 
|  | 179 | extern int rmdir(char *); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 180 | #else | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 181 | extern int chdir(const char *); | 
|  | 182 | extern int rmdir(const char *); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 183 | #endif | 
| Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 184 | #ifdef __BORLANDC__ | 
|  | 185 | extern int chmod(const char *, int); | 
|  | 186 | #else | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 187 | extern int chmod(const char *, mode_t); | 
| Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 188 | #endif | 
| Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 189 | /*#ifdef HAVE_FCHMOD | 
|  | 190 | extern int fchmod(int, mode_t); | 
|  | 191 | #endif*/ | 
|  | 192 | /*#ifdef HAVE_LCHMOD | 
|  | 193 | extern int lchmod(const char *, mode_t); | 
|  | 194 | #endif*/ | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 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 *); | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #ifdef HAVE_SYMLINK | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 203 | extern int symlink(const char *, const char *); | 
| Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 204 | #endif /* HAVE_SYMLINK */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | #ifdef HAVE_LSTAT | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 206 | extern int lstat(const char *, struct stat *); | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 207 | #endif /* HAVE_LSTAT */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 208 | #endif /* !HAVE_UNISTD_H */ | 
| Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 209 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 210 | #endif /* !_MSC_VER */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 211 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 212 | #ifdef HAVE_UTIME_H | 
|  | 213 | #include <utime.h> | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 214 | #endif /* HAVE_UTIME_H */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 |  | 
| Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 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 */ | 
|  | 220 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 221 | #ifdef HAVE_SYS_TIMES_H | 
|  | 222 | #include <sys/times.h> | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 223 | #endif /* HAVE_SYS_TIMES_H */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 |  | 
|  | 225 | #ifdef HAVE_SYS_PARAM_H | 
|  | 226 | #include <sys/param.h> | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 227 | #endif /* HAVE_SYS_PARAM_H */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 |  | 
|  | 229 | #ifdef HAVE_SYS_UTSNAME_H | 
|  | 230 | #include <sys/utsname.h> | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 231 | #endif /* HAVE_SYS_UTSNAME_H */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 |  | 
| Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 233 | #ifdef HAVE_DIRENT_H | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | #include <dirent.h> | 
| Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 235 | #define NAMLEN(dirent) strlen((dirent)->d_name) | 
|  | 236 | #else | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 237 | #if defined(__WATCOMC__) && !defined(__QNX__) | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 238 | #include <direct.h> | 
|  | 239 | #define NAMLEN(dirent) strlen((dirent)->d_name) | 
|  | 240 | #else | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | #define dirent direct | 
| Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 242 | #define NAMLEN(dirent) (dirent)->d_namlen | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 243 | #endif | 
| Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 244 | #ifdef HAVE_SYS_NDIR_H | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | #include <sys/ndir.h> | 
| Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 246 | #endif | 
|  | 247 | #ifdef HAVE_SYS_DIR_H | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 248 | #include <sys/dir.h> | 
| Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 249 | #endif | 
|  | 250 | #ifdef HAVE_NDIR_H | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #include <ndir.h> | 
| Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 252 | #endif | 
|  | 253 | #endif | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 255 | #ifdef _MSC_VER | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 256 | #ifdef HAVE_DIRECT_H | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #include <direct.h> | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 258 | #endif | 
|  | 259 | #ifdef HAVE_IO_H | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | #include <io.h> | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 261 | #endif | 
|  | 262 | #ifdef HAVE_PROCESS_H | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | #include <process.h> | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 264 | #endif | 
| Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 265 | #include "osdefs.h" | 
| Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 266 | #include <malloc.h> | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 267 | #include <windows.h> | 
| Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 268 | #include <shellapi.h>	/* for ShellExecute() */ | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 269 | #endif /* _MSC_VER */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 270 |  | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 271 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 272 | #include <io.h> | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 273 | #endif /* OS2 */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 |  | 
| Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 275 | #ifndef MAXPATHLEN | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 276 | #if defined(PATH_MAX) && PATH_MAX > 1024 | 
|  | 277 | #define MAXPATHLEN PATH_MAX | 
|  | 278 | #else | 
| Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 279 | #define MAXPATHLEN 1024 | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 280 | #endif | 
| Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 281 | #endif /* MAXPATHLEN */ | 
|  | 282 |  | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 283 | #ifdef UNION_WAIT | 
|  | 284 | /* Emulate some macros on systems that have a union instead of macros */ | 
|  | 285 |  | 
|  | 286 | #ifndef WIFEXITED | 
|  | 287 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) | 
|  | 288 | #endif | 
|  | 289 |  | 
|  | 290 | #ifndef WEXITSTATUS | 
|  | 291 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) | 
|  | 292 | #endif | 
|  | 293 |  | 
|  | 294 | #ifndef WTERMSIG | 
|  | 295 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) | 
|  | 296 | #endif | 
|  | 297 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 298 | #define WAIT_TYPE union wait | 
|  | 299 | #define WAIT_STATUS_INT(s) (s.w_status) | 
|  | 300 |  | 
|  | 301 | #else /* !UNION_WAIT */ | 
|  | 302 | #define WAIT_TYPE int | 
|  | 303 | #define WAIT_STATUS_INT(s) (s) | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 304 | #endif /* UNION_WAIT */ | 
|  | 305 |  | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 306 | /* Issue #1983: pid_t can be longer than a C long on some systems */ | 
| Antoine Pitrou | 7852c42 | 2009-05-24 11:58:35 +0000 | [diff] [blame] | 307 | #if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 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 | 
|  | 319 | #else | 
|  | 320 | #error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)" | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 321 | #endif /* SIZEOF_PID_T */ | 
|  | 322 |  | 
| Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 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 | 
|  | 327 | #endif | 
|  | 328 |  | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 329 | /* choose the appropriate stat and fstat functions and return structs */ | 
| Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 330 | #undef STAT | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 331 | #if defined(MS_WIN64) || defined(MS_WINDOWS) | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 332 | #	define STAT win32_stat | 
|  | 333 | #	define FSTAT win32_fstat | 
|  | 334 | #	define STRUCT_STAT struct win32_stat | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 335 | #else | 
|  | 336 | #	define STAT stat | 
|  | 337 | #	define FSTAT fstat | 
|  | 338 | #	define STRUCT_STAT struct stat | 
|  | 339 | #endif | 
|  | 340 |  | 
| Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 341 | #if defined(MAJOR_IN_MKDEV) | 
| Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 342 | #include <sys/mkdev.h> | 
|  | 343 | #else | 
|  | 344 | #if defined(MAJOR_IN_SYSMACROS) | 
|  | 345 | #include <sys/sysmacros.h> | 
|  | 346 | #endif | 
| Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 347 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) | 
|  | 348 | #include <sys/mkdev.h> | 
|  | 349 | #endif | 
| Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 350 | #endif | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 351 |  | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 352 | #if defined _MSC_VER && _MSC_VER >= 1400 | 
|  | 353 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is | 
|  | 354 | * valid and throw an assertion if it isn't. | 
|  | 355 | * Normally, an invalid fd is likely to be a C program error and therefore | 
|  | 356 | * an assertion can be useful, but it does contradict the POSIX standard | 
|  | 357 | * which for write(2) states: | 
|  | 358 | *    "Otherwise, -1 shall be returned and errno set to indicate the error." | 
|  | 359 | *    "[EBADF] The fildes argument is not a valid file descriptor open for | 
|  | 360 | *     writing." | 
|  | 361 | * Furthermore, python allows the user to enter any old integer | 
|  | 362 | * as a fd and should merely raise a python exception on error. | 
|  | 363 | * The Microsoft CRT doesn't provide an official way to check for the | 
|  | 364 | * validity of a file descriptor, but we can emulate its internal behaviour | 
|  | 365 | * by using the exported __pinfo data member and knowledge of the | 
|  | 366 | * internal structures involved. | 
|  | 367 | * The structures below must be updated for each version of visual studio | 
|  | 368 | * according to the file internal.h in the CRT source, until MS comes | 
|  | 369 | * up with a less hacky way to do this. | 
|  | 370 | * (all of this is to avoid globally modifying the CRT behaviour using | 
|  | 371 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) | 
|  | 372 | */ | 
| Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 373 | /* The actual size of the structure is determined at runtime. | 
|  | 374 | * Only the first items must be present. | 
|  | 375 | */ | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 376 | typedef struct { | 
|  | 377 | intptr_t osfhnd; | 
|  | 378 | char osfile; | 
| Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 379 | } my_ioinfo; | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 380 |  | 
| Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 381 | extern __declspec(dllimport) char * __pioinfo[]; | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 382 | #define IOINFO_L2E 5 | 
|  | 383 | #define IOINFO_ARRAY_ELTS   (1 << IOINFO_L2E) | 
|  | 384 | #define IOINFO_ARRAYS 64 | 
|  | 385 | #define _NHANDLE_           (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) | 
|  | 386 | #define FOPEN 0x01 | 
|  | 387 | #define _NO_CONSOLE_FILENO (intptr_t)-2 | 
|  | 388 |  | 
|  | 389 | /* This function emulates what the windows CRT does to validate file handles */ | 
|  | 390 | int | 
|  | 391 | _PyVerify_fd(int fd) | 
|  | 392 | { | 
|  | 393 | const int i1 = fd >> IOINFO_L2E; | 
|  | 394 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); | 
| Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 395 |  | 
|  | 396 | static int sizeof_ioinfo = 0; | 
|  | 397 |  | 
|  | 398 | /* Determine the actual size of the ioinfo structure, | 
|  | 399 | * as used by the CRT loaded in memory | 
|  | 400 | */ | 
|  | 401 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { | 
|  | 402 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; | 
|  | 403 | } | 
|  | 404 | if (sizeof_ioinfo == 0) { | 
|  | 405 | /* This should not happen... */ | 
|  | 406 | goto fail; | 
|  | 407 | } | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 408 |  | 
|  | 409 | /* See that it isn't a special CLEAR fileno */ | 
|  | 410 | if (fd != _NO_CONSOLE_FILENO) { | 
|  | 411 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that.  Instead | 
|  | 412 | * we check pointer validity and other info | 
|  | 413 | */ | 
|  | 414 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { | 
|  | 415 | /* finally, check that the file is open */ | 
| Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 416 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); | 
|  | 417 | if (info->osfile & FOPEN) { | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 418 | return 1; | 
| Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 419 | } | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 420 | } | 
|  | 421 | } | 
| Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 422 | fail: | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 423 | errno = EBADF; | 
|  | 424 | return 0; | 
|  | 425 | } | 
|  | 426 |  | 
|  | 427 | /* the special case of checking dup2.  The target fd must be in a sensible range */ | 
|  | 428 | static int | 
|  | 429 | _PyVerify_fd_dup2(int fd1, int fd2) | 
|  | 430 | { | 
|  | 431 | if (!_PyVerify_fd(fd1)) | 
|  | 432 | return 0; | 
|  | 433 | if (fd2 == _NO_CONSOLE_FILENO) | 
|  | 434 | return 0; | 
|  | 435 | if ((unsigned)fd2 < _NHANDLE_) | 
|  | 436 | return 1; | 
|  | 437 | else | 
|  | 438 | return 0; | 
|  | 439 | } | 
|  | 440 | #else | 
|  | 441 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ | 
|  | 442 | #define _PyVerify_fd_dup2(A, B) (1) | 
|  | 443 | #endif | 
|  | 444 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 445 | /* Return a dictionary corresponding to the POSIX environment table */ | 
| Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 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(). | 
|  | 449 | */ | 
|  | 450 | #include <crt_externs.h> | 
|  | 451 | static char **environ; | 
|  | 452 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 453 | extern char **environ; | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 454 | #endif /* !_MSC_VER */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 455 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 456 | static PyObject * | 
| Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 457 | convertenviron(void) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 458 | { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 459 | PyObject *d; | 
| Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 460 | #ifdef MS_WINDOWS | 
|  | 461 | wchar_t **e; | 
|  | 462 | #else | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 463 | char **e; | 
| Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 464 | #endif | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 465 | d = PyDict_New(); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 466 | if (d == NULL) | 
|  | 467 | return NULL; | 
| Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 468 | #ifdef WITH_NEXT_FRAMEWORK | 
|  | 469 | if (environ == NULL) | 
|  | 470 | environ = *_NSGetEnviron(); | 
|  | 471 | #endif | 
| Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 472 | #ifdef MS_WINDOWS | 
|  | 473 | /* _wenviron must be initialized in this way if the program is started | 
|  | 474 | through main() instead of wmain(). */ | 
|  | 475 | _wgetenv(L""); | 
|  | 476 | if (_wenviron == NULL) | 
|  | 477 | return d; | 
|  | 478 | /* This part ignores errors */ | 
|  | 479 | for (e = _wenviron; *e != NULL; e++) { | 
|  | 480 | PyObject *k; | 
|  | 481 | PyObject *v; | 
|  | 482 | wchar_t *p = wcschr(*e, L'='); | 
|  | 483 | if (p == NULL) | 
|  | 484 | continue; | 
|  | 485 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); | 
|  | 486 | if (k == NULL) { | 
|  | 487 | PyErr_Clear(); | 
|  | 488 | continue; | 
|  | 489 | } | 
|  | 490 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); | 
|  | 491 | if (v == NULL) { | 
|  | 492 | PyErr_Clear(); | 
|  | 493 | Py_DECREF(k); | 
|  | 494 | continue; | 
|  | 495 | } | 
|  | 496 | if (PyDict_GetItem(d, k) == NULL) { | 
|  | 497 | if (PyDict_SetItem(d, k, v) != 0) | 
|  | 498 | PyErr_Clear(); | 
|  | 499 | } | 
|  | 500 | Py_DECREF(k); | 
|  | 501 | Py_DECREF(v); | 
|  | 502 | } | 
|  | 503 | #else | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 504 | if (environ == NULL) | 
|  | 505 | return d; | 
| Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 506 | /* This part ignores errors */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 507 | for (e = environ; *e != NULL; e++) { | 
| Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 508 | PyObject *k; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 509 | PyObject *v; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 510 | char *p = strchr(*e, '='); | 
|  | 511 | if (p == NULL) | 
|  | 512 | continue; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 513 | k = PyUnicode_Decode(*e, (int)(p-*e), | 
| Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 514 | Py_FileSystemDefaultEncoding, "surrogateescape"); | 
| Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 515 | if (k == NULL) { | 
|  | 516 | PyErr_Clear(); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 517 | continue; | 
| Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 518 | } | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 519 | v = PyUnicode_Decode(p+1, strlen(p+1), | 
| Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 520 | Py_FileSystemDefaultEncoding, "surrogateescape"); | 
| Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 521 | if (v == NULL) { | 
|  | 522 | PyErr_Clear(); | 
|  | 523 | Py_DECREF(k); | 
|  | 524 | continue; | 
|  | 525 | } | 
|  | 526 | if (PyDict_GetItem(d, k) == NULL) { | 
|  | 527 | if (PyDict_SetItem(d, k, v) != 0) | 
|  | 528 | PyErr_Clear(); | 
|  | 529 | } | 
|  | 530 | Py_DECREF(k); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 531 | Py_DECREF(v); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 532 | } | 
| Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 533 | #endif | 
| Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 534 | #if defined(PYOS_OS2) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 535 | { | 
|  | 536 | APIRET rc; | 
|  | 537 | char   buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ | 
|  | 538 |  | 
|  | 539 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 540 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 541 | PyObject *v = PyBytes_FromString(buffer); | 
| Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 542 | PyDict_SetItemString(d, "BEGINLIBPATH", v); | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 543 | Py_DECREF(v); | 
|  | 544 | } | 
|  | 545 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); | 
|  | 546 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 547 | PyObject *v = PyBytes_FromString(buffer); | 
| Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 548 | PyDict_SetItemString(d, "ENDLIBPATH", v); | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 549 | Py_DECREF(v); | 
|  | 550 | } | 
|  | 551 | } | 
|  | 552 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 553 | return d; | 
|  | 554 | } | 
|  | 555 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 556 | /* Convert a bytes object to a char*. Optionally lock the buffer if it is a | 
|  | 557 | bytes array. */ | 
|  | 558 |  | 
|  | 559 | static char* | 
|  | 560 | bytes2str(PyObject* o, int lock) | 
|  | 561 | { | 
|  | 562 | if(PyBytes_Check(o)) | 
|  | 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); | 
|  | 569 | } else { | 
|  | 570 | /* The FS converter should have verified that this | 
|  | 571 | is either bytes or bytearray. */ | 
|  | 572 | Py_FatalError("bad object passed to bytes2str"); | 
|  | 573 | /* not reached. */ | 
|  | 574 | return ""; | 
|  | 575 | } | 
|  | 576 | } | 
|  | 577 |  | 
|  | 578 | /* Release the lock, decref the object. */ | 
|  | 579 | static void | 
|  | 580 | release_bytes(PyObject* o) | 
|  | 581 | { | 
|  | 582 | if (PyByteArray_Check(o)) | 
|  | 583 | o->ob_type->tp_as_buffer->bf_releasebuffer(NULL, 0); | 
|  | 584 | Py_DECREF(o); | 
|  | 585 | } | 
|  | 586 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 587 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 588 | /* Set a POSIX-specific error from errno, and return NULL */ | 
|  | 589 |  | 
| Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 590 | static PyObject * | 
| Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 591 | posix_error(void) | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 592 | { | 
| Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 593 | return PyErr_SetFromErrno(PyExc_OSError); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 594 | } | 
| Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 595 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 596 | posix_error_with_filename(char* name) | 
| Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 597 | { | 
| Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 598 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); | 
| Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 599 | } | 
|  | 600 |  | 
| Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 601 | #ifdef MS_WINDOWS | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 602 | static PyObject * | 
|  | 603 | posix_error_with_unicode_filename(Py_UNICODE* name) | 
|  | 604 | { | 
|  | 605 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); | 
|  | 606 | } | 
| Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 607 | #endif /* MS_WINDOWS */ | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 608 |  | 
|  | 609 |  | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 610 | static PyObject * | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 611 | posix_error_with_allocated_filename(PyObject* name) | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 612 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 613 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, | 
|  | 614 | bytes2str(name, 0)); | 
|  | 615 | release_bytes(name); | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 616 | return rc; | 
|  | 617 | } | 
|  | 618 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 619 | #ifdef MS_WINDOWS | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 620 | static PyObject * | 
|  | 621 | win32_error(char* function, char* filename) | 
|  | 622 | { | 
| Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 623 | /* XXX We should pass the function name along in the future. | 
| Georg Brandl | 38feaf0 | 2008-05-25 07:45:51 +0000 | [diff] [blame] | 624 | (winreg.c also wants to pass the function name.) | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 625 | This would however require an additional param to the | 
| Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 626 | Windows error object, which is non-trivial. | 
|  | 627 | */ | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 628 | errno = GetLastError(); | 
|  | 629 | if (filename) | 
| Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 630 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 631 | else | 
| Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 632 | return PyErr_SetFromWindowsErr(errno); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 633 | } | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 634 |  | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 635 | static PyObject * | 
|  | 636 | win32_error_unicode(char* function, Py_UNICODE* filename) | 
|  | 637 | { | 
|  | 638 | /* XXX - see win32_error for comments on 'function' */ | 
|  | 639 | errno = GetLastError(); | 
|  | 640 | if (filename) | 
|  | 641 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); | 
|  | 642 | else | 
|  | 643 | return PyErr_SetFromWindowsErr(errno); | 
|  | 644 | } | 
|  | 645 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 646 | static int | 
| Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 647 | convert_to_unicode(PyObject **param) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 648 | { | 
| Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 649 | if (PyUnicode_CheckExact(*param)) | 
|  | 650 | Py_INCREF(*param); | 
|  | 651 | else if (PyUnicode_Check(*param)) | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 652 | /* For a Unicode subtype that's not a Unicode object, | 
|  | 653 | return a true Unicode object with the same data. */ | 
| Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 654 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), | 
|  | 655 | PyUnicode_GET_SIZE(*param)); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 656 | else | 
| Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 657 | *param = PyUnicode_FromEncodedObject(*param, | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 658 | Py_FileSystemDefaultEncoding, | 
|  | 659 | "strict"); | 
|  | 660 | return (*param) != NULL; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 661 | } | 
|  | 662 |  | 
| Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 663 | #endif /* MS_WINDOWS */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 664 |  | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 665 | #if defined(PYOS_OS2) | 
|  | 666 | /********************************************************************** | 
|  | 667 | *         Helper Function to Trim and Format OS/2 Messages | 
|  | 668 | **********************************************************************/ | 
|  | 669 | static void | 
|  | 670 | os2_formatmsg(char *msgbuf, int msglen, char *reason) | 
|  | 671 | { | 
|  | 672 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ | 
|  | 673 |  | 
|  | 674 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ | 
|  | 675 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; | 
|  | 676 |  | 
| Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 677 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 678 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ | 
|  | 679 | } | 
|  | 680 |  | 
|  | 681 | /* Add Optional Reason Text */ | 
|  | 682 | if (reason) { | 
|  | 683 | strcat(msgbuf, " : "); | 
|  | 684 | strcat(msgbuf, reason); | 
|  | 685 | } | 
|  | 686 | } | 
|  | 687 |  | 
|  | 688 | /********************************************************************** | 
|  | 689 | *             Decode an OS/2 Operating System Error Code | 
|  | 690 | * | 
|  | 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. | 
|  | 693 | * | 
|  | 694 | * Notes: | 
|  | 695 | *   The messages for errors returned from the OS/2 kernel reside in | 
|  | 696 | *   the file OSO001.MSG in the \OS2 directory hierarchy. | 
|  | 697 | * | 
|  | 698 | **********************************************************************/ | 
|  | 699 | static char * | 
|  | 700 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) | 
|  | 701 | { | 
|  | 702 | APIRET rc; | 
|  | 703 | ULONG  msglen; | 
|  | 704 |  | 
|  | 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); | 
|  | 709 | Py_END_ALLOW_THREADS | 
|  | 710 |  | 
|  | 711 | if (rc == NO_ERROR) | 
|  | 712 | os2_formatmsg(msgbuf, msglen, reason); | 
|  | 713 | else | 
| Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 714 | PyOS_snprintf(msgbuf, msgbuflen, | 
| Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 715 | "unknown OS error #%d", errorcode); | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 716 |  | 
|  | 717 | return msgbuf; | 
|  | 718 | } | 
|  | 719 |  | 
|  | 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. */ | 
|  | 723 |  | 
|  | 724 | static PyObject * os2_error(int code) | 
|  | 725 | { | 
|  | 726 | char text[1024]; | 
|  | 727 | PyObject *v; | 
|  | 728 |  | 
|  | 729 | os2_strerror(text, sizeof(text), code, ""); | 
|  | 730 |  | 
|  | 731 | v = Py_BuildValue("(is)", code, text); | 
|  | 732 | if (v != NULL) { | 
| Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 733 | PyErr_SetObject(PyExc_OSError, v); | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 734 | Py_DECREF(v); | 
|  | 735 | } | 
|  | 736 | return NULL; /* Signal to Python that an Exception is Pending */ | 
|  | 737 | } | 
|  | 738 |  | 
|  | 739 | #endif /* OS2 */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 740 |  | 
|  | 741 | /* POSIX generic methods */ | 
|  | 742 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 743 | static PyObject * | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 744 | posix_fildes(PyObject *fdobj, int (*func)(int)) | 
|  | 745 | { | 
|  | 746 | int fd; | 
|  | 747 | int res; | 
|  | 748 | fd = PyObject_AsFileDescriptor(fdobj); | 
|  | 749 | if (fd < 0) | 
|  | 750 | return NULL; | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 751 | if (!_PyVerify_fd(fd)) | 
|  | 752 | return posix_error(); | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 753 | Py_BEGIN_ALLOW_THREADS | 
|  | 754 | res = (*func)(fd); | 
|  | 755 | Py_END_ALLOW_THREADS | 
|  | 756 | if (res < 0) | 
|  | 757 | return posix_error(); | 
|  | 758 | Py_INCREF(Py_None); | 
|  | 759 | return Py_None; | 
|  | 760 | } | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 761 |  | 
|  | 762 | static PyObject * | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 763 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 764 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 765 | PyObject *opath1 = NULL; | 
|  | 766 | char *path1; | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 767 | int res; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 768 | if (!PyArg_ParseTuple(args, format, | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 769 | PyUnicode_FSConverter, &opath1)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 770 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 771 | path1 = bytes2str(opath1, 1); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 772 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 773 | res = (*func)(path1); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 774 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 775 | if (res < 0) | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 776 | return posix_error_with_allocated_filename(opath1); | 
|  | 777 | release_bytes(opath1); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 778 | Py_INCREF(Py_None); | 
|  | 779 | return Py_None; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 780 | } | 
|  | 781 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 782 | static PyObject * | 
| Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 783 | posix_2str(PyObject *args, | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 784 | char *format, | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 785 | int (*func)(const char *, const char *)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 786 | { | 
| Antoine Pitrou | a6bb984 | 2009-05-10 22:27:00 +0000 | [diff] [blame] | 787 | PyObject *opath1 = NULL, *opath2 = NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 788 | char *path1, *path2; | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 789 | int res; | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 790 | if (!PyArg_ParseTuple(args, format, | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 791 | PyUnicode_FSConverter, &opath1, | 
| Antoine Pitrou | a6bb984 | 2009-05-10 22:27:00 +0000 | [diff] [blame] | 792 | PyUnicode_FSConverter, &opath2)) { | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 793 | return NULL; | 
| Antoine Pitrou | a6bb984 | 2009-05-10 22:27:00 +0000 | [diff] [blame] | 794 | } | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 795 | path1 = bytes2str(opath1, 1); | 
|  | 796 | path2 = bytes2str(opath2, 1); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 797 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 798 | res = (*func)(path1, path2); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 799 | Py_END_ALLOW_THREADS | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 800 | release_bytes(opath1); | 
|  | 801 | release_bytes(opath2); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 802 | if (res != 0) | 
| Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 803 | /* XXX how to report both path1 and path2??? */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 804 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 805 | Py_INCREF(Py_None); | 
|  | 806 | return Py_None; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 807 | } | 
|  | 808 |  | 
| Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 809 | #ifdef MS_WINDOWS | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 810 | static PyObject* | 
|  | 811 | win32_1str(PyObject* args, char* func, | 
|  | 812 | char* format, BOOL (__stdcall *funcA)(LPCSTR), | 
|  | 813 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) | 
|  | 814 | { | 
|  | 815 | PyObject *uni; | 
|  | 816 | char *ansi; | 
|  | 817 | BOOL result; | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 818 |  | 
|  | 819 | if (!PyArg_ParseTuple(args, wformat, &uni)) | 
|  | 820 | PyErr_Clear(); | 
|  | 821 | else { | 
|  | 822 | Py_BEGIN_ALLOW_THREADS | 
|  | 823 | result = funcW(PyUnicode_AsUnicode(uni)); | 
|  | 824 | Py_END_ALLOW_THREADS | 
|  | 825 | if (!result) | 
|  | 826 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); | 
|  | 827 | Py_INCREF(Py_None); | 
|  | 828 | return Py_None; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 829 | } | 
|  | 830 | if (!PyArg_ParseTuple(args, format, &ansi)) | 
|  | 831 | return NULL; | 
|  | 832 | Py_BEGIN_ALLOW_THREADS | 
|  | 833 | result = funcA(ansi); | 
|  | 834 | Py_END_ALLOW_THREADS | 
|  | 835 | if (!result) | 
|  | 836 | return win32_error(func, ansi); | 
|  | 837 | Py_INCREF(Py_None); | 
|  | 838 | return Py_None; | 
|  | 839 |  | 
|  | 840 | } | 
|  | 841 |  | 
|  | 842 | /* This is a reimplementation of the C library's chdir function, | 
|  | 843 | but one that produces Win32 errors instead of DOS error codes. | 
|  | 844 | chdir is essentially a wrapper around SetCurrentDirectory; however, | 
|  | 845 | it also needs to set "magic" environment variables indicating | 
|  | 846 | the per-drive current directory, which are of the form =<drive>: */ | 
| Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 847 | static BOOL __stdcall | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 848 | win32_chdir(LPCSTR path) | 
|  | 849 | { | 
|  | 850 | char new_path[MAX_PATH+1]; | 
|  | 851 | int result; | 
|  | 852 | char env[4] = "=x:"; | 
|  | 853 |  | 
|  | 854 | if(!SetCurrentDirectoryA(path)) | 
|  | 855 | return FALSE; | 
|  | 856 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); | 
|  | 857 | if (!result) | 
|  | 858 | return FALSE; | 
|  | 859 | /* In the ANSI API, there should not be any paths longer | 
|  | 860 | than MAX_PATH. */ | 
|  | 861 | assert(result <= MAX_PATH+1); | 
|  | 862 | if (strncmp(new_path, "\\\\", 2) == 0 || | 
|  | 863 | strncmp(new_path, "//", 2) == 0) | 
|  | 864 | /* UNC path, nothing to do. */ | 
|  | 865 | return TRUE; | 
|  | 866 | env[1] = new_path[0]; | 
|  | 867 | return SetEnvironmentVariableA(env, new_path); | 
|  | 868 | } | 
|  | 869 |  | 
|  | 870 | /* The Unicode version differs from the ANSI version | 
|  | 871 | since the current directory might exceed MAX_PATH characters */ | 
| Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 872 | static BOOL __stdcall | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 873 | win32_wchdir(LPCWSTR path) | 
|  | 874 | { | 
|  | 875 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; | 
|  | 876 | int result; | 
|  | 877 | wchar_t env[4] = L"=x:"; | 
|  | 878 |  | 
|  | 879 | if(!SetCurrentDirectoryW(path)) | 
|  | 880 | return FALSE; | 
|  | 881 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); | 
|  | 882 | if (!result) | 
|  | 883 | return FALSE; | 
|  | 884 | if (result > MAX_PATH+1) { | 
| Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 885 | new_path = malloc(result * sizeof(wchar_t)); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 886 | if (!new_path) { | 
|  | 887 | SetLastError(ERROR_OUTOFMEMORY); | 
|  | 888 | return FALSE; | 
|  | 889 | } | 
| Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 890 | result = GetCurrentDirectoryW(result, new_path); | 
|  | 891 | if (!result) { | 
|  | 892 | free(new_path); | 
|  | 893 | return FALSE; | 
|  | 894 | } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 895 | } | 
|  | 896 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || | 
|  | 897 | wcsncmp(new_path, L"//", 2) == 0) | 
|  | 898 | /* UNC path, nothing to do. */ | 
|  | 899 | return TRUE; | 
|  | 900 | env[1] = new_path[0]; | 
|  | 901 | result = SetEnvironmentVariableW(env, new_path); | 
|  | 902 | if (new_path != _new_path) | 
|  | 903 | free(new_path); | 
|  | 904 | return result; | 
|  | 905 | } | 
|  | 906 | #endif | 
|  | 907 |  | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 908 | #ifdef MS_WINDOWS | 
|  | 909 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: | 
|  | 910 | - time stamps are restricted to second resolution | 
|  | 911 | - file modification times suffer from forth-and-back conversions between | 
|  | 912 | UTC and local time | 
|  | 913 | Therefore, we implement our own stat, based on the Win32 API directly. | 
|  | 914 | */ | 
|  | 915 | #define HAVE_STAT_NSEC 1 | 
|  | 916 |  | 
|  | 917 | struct win32_stat{ | 
|  | 918 | int st_dev; | 
|  | 919 | __int64 st_ino; | 
|  | 920 | unsigned short st_mode; | 
|  | 921 | int st_nlink; | 
|  | 922 | int st_uid; | 
|  | 923 | int st_gid; | 
|  | 924 | int st_rdev; | 
|  | 925 | __int64 st_size; | 
|  | 926 | int st_atime; | 
|  | 927 | int st_atime_nsec; | 
|  | 928 | int st_mtime; | 
|  | 929 | int st_mtime_nsec; | 
|  | 930 | int st_ctime; | 
|  | 931 | int st_ctime_nsec; | 
|  | 932 | }; | 
|  | 933 |  | 
|  | 934 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ | 
|  | 935 |  | 
|  | 936 | static void | 
|  | 937 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) | 
|  | 938 | { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 939 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ | 
|  | 940 | /* Cannot simply cast and dereference in_ptr, | 
|  | 941 | since it might not be aligned properly */ | 
|  | 942 | __int64 in; | 
|  | 943 | memcpy(&in, in_ptr, sizeof(in)); | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 944 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ | 
|  | 945 | /* XXX Win32 supports time stamps past 2038; we currently don't */ | 
|  | 946 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); | 
|  | 947 | } | 
|  | 948 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 949 | static void | 
|  | 950 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) | 
|  | 951 | { | 
|  | 952 | /* XXX endianness */ | 
|  | 953 | __int64 out; | 
|  | 954 | out = time_in + secs_between_epochs; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 955 | out = out * 10000000 + nsec_in / 100; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 956 | memcpy(out_ptr, &out, sizeof(out)); | 
|  | 957 | } | 
|  | 958 |  | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 959 | /* Below, we *know* that ugo+r is 0444 */ | 
|  | 960 | #if _S_IREAD != 0400 | 
|  | 961 | #error Unsupported C library | 
|  | 962 | #endif | 
|  | 963 | static int | 
|  | 964 | attributes_to_mode(DWORD attr) | 
|  | 965 | { | 
|  | 966 | int m = 0; | 
|  | 967 | if (attr & FILE_ATTRIBUTE_DIRECTORY) | 
|  | 968 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ | 
|  | 969 | else | 
|  | 970 | m |= _S_IFREG; | 
|  | 971 | if (attr & FILE_ATTRIBUTE_READONLY) | 
|  | 972 | m |= 0444; | 
|  | 973 | else | 
|  | 974 | m |= 0666; | 
|  | 975 | return m; | 
|  | 976 | } | 
|  | 977 |  | 
|  | 978 | static int | 
|  | 979 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) | 
|  | 980 | { | 
|  | 981 | memset(result, 0, sizeof(*result)); | 
|  | 982 | result->st_mode = attributes_to_mode(info->dwFileAttributes); | 
|  | 983 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; | 
|  | 984 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); | 
|  | 985 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); | 
|  | 986 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); | 
|  | 987 |  | 
|  | 988 | return 0; | 
|  | 989 | } | 
|  | 990 |  | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 991 | static BOOL | 
|  | 992 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) | 
|  | 993 | { | 
|  | 994 | HANDLE hFindFile; | 
|  | 995 | WIN32_FIND_DATAA FileData; | 
|  | 996 | hFindFile = FindFirstFileA(pszFile, &FileData); | 
|  | 997 | if (hFindFile == INVALID_HANDLE_VALUE) | 
|  | 998 | return FALSE; | 
|  | 999 | FindClose(hFindFile); | 
|  | 1000 | pfad->dwFileAttributes = FileData.dwFileAttributes; | 
|  | 1001 | pfad->ftCreationTime   = FileData.ftCreationTime; | 
|  | 1002 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; | 
|  | 1003 | pfad->ftLastWriteTime  = FileData.ftLastWriteTime; | 
|  | 1004 | pfad->nFileSizeHigh    = FileData.nFileSizeHigh; | 
|  | 1005 | pfad->nFileSizeLow     = FileData.nFileSizeLow; | 
|  | 1006 | return TRUE; | 
|  | 1007 | } | 
|  | 1008 |  | 
|  | 1009 | static BOOL | 
|  | 1010 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) | 
|  | 1011 | { | 
|  | 1012 | HANDLE hFindFile; | 
|  | 1013 | WIN32_FIND_DATAW FileData; | 
|  | 1014 | hFindFile = FindFirstFileW(pszFile, &FileData); | 
|  | 1015 | if (hFindFile == INVALID_HANDLE_VALUE) | 
|  | 1016 | return FALSE; | 
|  | 1017 | FindClose(hFindFile); | 
|  | 1018 | pfad->dwFileAttributes = FileData.dwFileAttributes; | 
|  | 1019 | pfad->ftCreationTime   = FileData.ftCreationTime; | 
|  | 1020 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; | 
|  | 1021 | pfad->ftLastWriteTime  = FileData.ftLastWriteTime; | 
|  | 1022 | pfad->nFileSizeHigh    = FileData.nFileSizeHigh; | 
|  | 1023 | pfad->nFileSizeLow     = FileData.nFileSizeLow; | 
|  | 1024 | return TRUE; | 
|  | 1025 | } | 
|  | 1026 |  | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1027 | static int | 
|  | 1028 | win32_stat(const char* path, struct win32_stat *result) | 
|  | 1029 | { | 
|  | 1030 | WIN32_FILE_ATTRIBUTE_DATA info; | 
|  | 1031 | int code; | 
|  | 1032 | char *dot; | 
| Hirokazu Yamamoto | 6fbdfda | 2009-06-29 11:37:19 +0000 | [diff] [blame] | 1033 | if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1034 | if (GetLastError() != ERROR_SHARING_VIOLATION) { | 
|  | 1035 | /* Protocol violation: we explicitly clear errno, instead of | 
|  | 1036 | setting it to a POSIX error. Callers should use GetLastError. */ | 
|  | 1037 | errno = 0; | 
|  | 1038 | return -1; | 
|  | 1039 | } else { | 
|  | 1040 | /* Could not get attributes on open file. Fall back to | 
|  | 1041 | reading the directory. */ | 
|  | 1042 | if (!attributes_from_dir(path, &info)) { | 
|  | 1043 | /* Very strange. This should not fail now */ | 
|  | 1044 | errno = 0; | 
|  | 1045 | return -1; | 
|  | 1046 | } | 
|  | 1047 | } | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1048 | } | 
|  | 1049 | code = attribute_data_to_stat(&info, result); | 
|  | 1050 | if (code != 0) | 
|  | 1051 | return code; | 
|  | 1052 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ | 
|  | 1053 | dot = strrchr(path, '.'); | 
|  | 1054 | if (dot) { | 
|  | 1055 | if (stricmp(dot, ".bat") == 0 || | 
|  | 1056 | stricmp(dot, ".cmd") == 0 || | 
|  | 1057 | stricmp(dot, ".exe") == 0 || | 
|  | 1058 | stricmp(dot, ".com") == 0) | 
|  | 1059 | result->st_mode |= 0111; | 
|  | 1060 | } | 
|  | 1061 | return code; | 
|  | 1062 | } | 
|  | 1063 |  | 
|  | 1064 | static int | 
|  | 1065 | win32_wstat(const wchar_t* path, struct win32_stat *result) | 
|  | 1066 | { | 
|  | 1067 | int code; | 
|  | 1068 | const wchar_t *dot; | 
|  | 1069 | WIN32_FILE_ATTRIBUTE_DATA info; | 
| Hirokazu Yamamoto | 6fbdfda | 2009-06-29 11:37:19 +0000 | [diff] [blame] | 1070 | if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1071 | if (GetLastError() != ERROR_SHARING_VIOLATION) { | 
|  | 1072 | /* Protocol violation: we explicitly clear errno, instead of | 
|  | 1073 | setting it to a POSIX error. Callers should use GetLastError. */ | 
|  | 1074 | errno = 0; | 
|  | 1075 | return -1; | 
|  | 1076 | } else { | 
|  | 1077 | /* Could not get attributes on open file. Fall back to | 
|  | 1078 | reading the directory. */ | 
|  | 1079 | if (!attributes_from_dir_w(path, &info)) { | 
|  | 1080 | /* Very strange. This should not fail now */ | 
|  | 1081 | errno = 0; | 
|  | 1082 | return -1; | 
|  | 1083 | } | 
|  | 1084 | } | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1085 | } | 
|  | 1086 | code = attribute_data_to_stat(&info, result); | 
|  | 1087 | if (code < 0) | 
|  | 1088 | return code; | 
|  | 1089 | /* Set IFEXEC if it is an .exe, .bat, ... */ | 
|  | 1090 | dot = wcsrchr(path, '.'); | 
|  | 1091 | if (dot) { | 
|  | 1092 | if (_wcsicmp(dot, L".bat") == 0 || | 
|  | 1093 | _wcsicmp(dot, L".cmd") == 0 || | 
|  | 1094 | _wcsicmp(dot, L".exe") == 0 || | 
|  | 1095 | _wcsicmp(dot, L".com") == 0) | 
|  | 1096 | result->st_mode |= 0111; | 
|  | 1097 | } | 
|  | 1098 | return code; | 
|  | 1099 | } | 
|  | 1100 |  | 
|  | 1101 | static int | 
|  | 1102 | win32_fstat(int file_number, struct win32_stat *result) | 
|  | 1103 | { | 
|  | 1104 | BY_HANDLE_FILE_INFORMATION info; | 
|  | 1105 | HANDLE h; | 
|  | 1106 | int type; | 
|  | 1107 |  | 
|  | 1108 | h = (HANDLE)_get_osfhandle(file_number); | 
|  | 1109 |  | 
|  | 1110 | /* Protocol violation: we explicitly clear errno, instead of | 
|  | 1111 | setting it to a POSIX error. Callers should use GetLastError. */ | 
|  | 1112 | errno = 0; | 
|  | 1113 |  | 
|  | 1114 | if (h == INVALID_HANDLE_VALUE) { | 
|  | 1115 | /* This is really a C library error (invalid file handle). | 
|  | 1116 | We set the Win32 error to the closes one matching. */ | 
|  | 1117 | SetLastError(ERROR_INVALID_HANDLE); | 
|  | 1118 | return -1; | 
|  | 1119 | } | 
|  | 1120 | memset(result, 0, sizeof(*result)); | 
|  | 1121 |  | 
|  | 1122 | type = GetFileType(h); | 
|  | 1123 | if (type == FILE_TYPE_UNKNOWN) { | 
|  | 1124 | DWORD error = GetLastError(); | 
|  | 1125 | if (error != 0) { | 
|  | 1126 | return -1; | 
|  | 1127 | } | 
|  | 1128 | /* else: valid but unknown file */ | 
|  | 1129 | } | 
|  | 1130 |  | 
|  | 1131 | if (type != FILE_TYPE_DISK) { | 
|  | 1132 | if (type == FILE_TYPE_CHAR) | 
|  | 1133 | result->st_mode = _S_IFCHR; | 
|  | 1134 | else if (type == FILE_TYPE_PIPE) | 
|  | 1135 | result->st_mode = _S_IFIFO; | 
|  | 1136 | return 0; | 
|  | 1137 | } | 
|  | 1138 |  | 
|  | 1139 | if (!GetFileInformationByHandle(h, &info)) { | 
|  | 1140 | return -1; | 
|  | 1141 | } | 
|  | 1142 |  | 
|  | 1143 | /* similar to stat() */ | 
|  | 1144 | result->st_mode = attributes_to_mode(info.dwFileAttributes); | 
|  | 1145 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; | 
|  | 1146 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); | 
|  | 1147 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); | 
|  | 1148 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); | 
|  | 1149 | /* specific to fstat() */ | 
|  | 1150 | result->st_nlink = info.nNumberOfLinks; | 
|  | 1151 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; | 
|  | 1152 | return 0; | 
|  | 1153 | } | 
|  | 1154 |  | 
|  | 1155 | #endif /* MS_WINDOWS */ | 
|  | 1156 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1157 | PyDoc_STRVAR(stat_result__doc__, | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1158 | "stat_result: Result from stat or lstat.\n\n\ | 
|  | 1159 | This object may be accessed either as a tuple of\n\ | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1160 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1161 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ | 
|  | 1162 | \n\ | 
| Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1163 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ | 
|  | 1164 | or st_flags, they are available as attributes only.\n\ | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1165 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1166 | See os.stat for more information."); | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1167 |  | 
|  | 1168 | static PyStructSequence_Field stat_result_fields[] = { | 
|  | 1169 | {"st_mode",    "protection bits"}, | 
|  | 1170 | {"st_ino",     "inode"}, | 
|  | 1171 | {"st_dev",     "device"}, | 
|  | 1172 | {"st_nlink",   "number of hard links"}, | 
|  | 1173 | {"st_uid",     "user ID of owner"}, | 
|  | 1174 | {"st_gid",     "group ID of owner"}, | 
|  | 1175 | {"st_size",    "total size, in bytes"}, | 
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1176 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ | 
|  | 1177 | {NULL,   "integer time of last access"}, | 
|  | 1178 | {NULL,   "integer time of last modification"}, | 
|  | 1179 | {NULL,   "integer time of last change"}, | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1180 | {"st_atime",   "time of last access"}, | 
|  | 1181 | {"st_mtime",   "time of last modification"}, | 
|  | 1182 | {"st_ctime",   "time of last change"}, | 
| Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1183 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1184 | {"st_blksize", "blocksize for filesystem I/O"}, | 
|  | 1185 | #endif | 
| Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1186 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1187 | {"st_blocks",  "number of blocks allocated"}, | 
|  | 1188 | #endif | 
| Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1189 | #ifdef HAVE_STRUCT_STAT_ST_RDEV | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1190 | {"st_rdev",    "device type (if inode device)"}, | 
|  | 1191 | #endif | 
| Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1192 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS | 
|  | 1193 | {"st_flags",   "user defined flags for file"}, | 
|  | 1194 | #endif | 
| Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1195 | #ifdef HAVE_STRUCT_STAT_ST_GEN | 
|  | 1196 | {"st_gen",    "generation number"}, | 
|  | 1197 | #endif | 
|  | 1198 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME | 
|  | 1199 | {"st_birthtime",   "time of creation"}, | 
|  | 1200 | #endif | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1201 | {0} | 
|  | 1202 | }; | 
|  | 1203 |  | 
| Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1204 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE | 
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1205 | #define ST_BLKSIZE_IDX 13 | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1206 | #else | 
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1207 | #define ST_BLKSIZE_IDX 12 | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1208 | #endif | 
|  | 1209 |  | 
| Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1210 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1211 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) | 
|  | 1212 | #else | 
|  | 1213 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX | 
|  | 1214 | #endif | 
|  | 1215 |  | 
| Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1216 | #ifdef HAVE_STRUCT_STAT_ST_RDEV | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1217 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) | 
|  | 1218 | #else | 
|  | 1219 | #define ST_RDEV_IDX ST_BLOCKS_IDX | 
|  | 1220 | #endif | 
|  | 1221 |  | 
| Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1222 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS | 
|  | 1223 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) | 
|  | 1224 | #else | 
|  | 1225 | #define ST_FLAGS_IDX ST_RDEV_IDX | 
|  | 1226 | #endif | 
|  | 1227 |  | 
| Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1228 | #ifdef HAVE_STRUCT_STAT_ST_GEN | 
| Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1229 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) | 
| Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1230 | #else | 
| Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1231 | #define ST_GEN_IDX ST_FLAGS_IDX | 
| Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1232 | #endif | 
|  | 1233 |  | 
|  | 1234 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME | 
|  | 1235 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) | 
|  | 1236 | #else | 
|  | 1237 | #define ST_BIRTHTIME_IDX ST_GEN_IDX | 
|  | 1238 | #endif | 
|  | 1239 |  | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1240 | static PyStructSequence_Desc stat_result_desc = { | 
|  | 1241 | "stat_result", /* name */ | 
|  | 1242 | stat_result__doc__, /* doc */ | 
|  | 1243 | stat_result_fields, | 
|  | 1244 | 10 | 
|  | 1245 | }; | 
|  | 1246 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1247 | PyDoc_STRVAR(statvfs_result__doc__, | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1248 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ | 
|  | 1249 | This object may be accessed either as a tuple of\n\ | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1250 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ | 
| Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1251 | or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\ | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1252 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1253 | See os.statvfs for more information."); | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1254 |  | 
|  | 1255 | static PyStructSequence_Field statvfs_result_fields[] = { | 
|  | 1256 | {"f_bsize",  }, | 
|  | 1257 | {"f_frsize", }, | 
|  | 1258 | {"f_blocks", }, | 
|  | 1259 | {"f_bfree",  }, | 
|  | 1260 | {"f_bavail", }, | 
|  | 1261 | {"f_files",  }, | 
|  | 1262 | {"f_ffree",  }, | 
|  | 1263 | {"f_favail", }, | 
|  | 1264 | {"f_flag",   }, | 
|  | 1265 | {"f_namemax",}, | 
|  | 1266 | {0} | 
|  | 1267 | }; | 
|  | 1268 |  | 
|  | 1269 | static PyStructSequence_Desc statvfs_result_desc = { | 
|  | 1270 | "statvfs_result", /* name */ | 
|  | 1271 | statvfs_result__doc__, /* doc */ | 
|  | 1272 | statvfs_result_fields, | 
|  | 1273 | 10 | 
|  | 1274 | }; | 
|  | 1275 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1276 | static int initialized; | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1277 | static PyTypeObject StatResultType; | 
|  | 1278 | static PyTypeObject StatVFSResultType; | 
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1279 | static newfunc structseq_new; | 
|  | 1280 |  | 
|  | 1281 | static PyObject * | 
|  | 1282 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
|  | 1283 | { | 
|  | 1284 | PyStructSequence *result; | 
|  | 1285 | int i; | 
|  | 1286 |  | 
|  | 1287 | result = (PyStructSequence*)structseq_new(type, args, kwds); | 
|  | 1288 | if (!result) | 
|  | 1289 | return NULL; | 
|  | 1290 | /* If we have been initialized from a tuple, | 
|  | 1291 | st_?time might be set to None. Initialize it | 
|  | 1292 | from the int slots.  */ | 
|  | 1293 | for (i = 7; i <= 9; i++) { | 
|  | 1294 | if (result->ob_item[i+3] == Py_None) { | 
|  | 1295 | Py_DECREF(Py_None); | 
|  | 1296 | Py_INCREF(result->ob_item[i]); | 
|  | 1297 | result->ob_item[i+3] = result->ob_item[i]; | 
|  | 1298 | } | 
|  | 1299 | } | 
|  | 1300 | return (PyObject*)result; | 
|  | 1301 | } | 
|  | 1302 |  | 
|  | 1303 |  | 
|  | 1304 |  | 
|  | 1305 | /* If true, st_?time is float. */ | 
| Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1306 | static int _stat_float_times = 1; | 
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1307 |  | 
|  | 1308 | PyDoc_STRVAR(stat_float_times__doc__, | 
|  | 1309 | "stat_float_times([newval]) -> oldval\n\n\ | 
|  | 1310 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ | 
|  | 1311 | If newval is True, future calls to stat() return floats, if it is False,\n\ | 
|  | 1312 | future calls return ints. \n\ | 
|  | 1313 | If newval is omitted, return the current setting.\n"); | 
|  | 1314 |  | 
|  | 1315 | static PyObject* | 
|  | 1316 | stat_float_times(PyObject* self, PyObject *args) | 
|  | 1317 | { | 
|  | 1318 | int newval = -1; | 
|  | 1319 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) | 
|  | 1320 | return NULL; | 
|  | 1321 | if (newval == -1) | 
|  | 1322 | /* Return old value */ | 
|  | 1323 | return PyBool_FromLong(_stat_float_times); | 
|  | 1324 | _stat_float_times = newval; | 
|  | 1325 | Py_INCREF(Py_None); | 
|  | 1326 | return Py_None; | 
|  | 1327 | } | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1328 |  | 
| Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1329 | static void | 
|  | 1330 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) | 
|  | 1331 | { | 
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1332 | PyObject *fval,*ival; | 
|  | 1333 | #if SIZEOF_TIME_T > SIZEOF_LONG | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1334 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); | 
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1335 | #else | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1336 | ival = PyLong_FromLong((long)sec); | 
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1337 | #endif | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1338 | if (!ival) | 
|  | 1339 | return; | 
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1340 | if (_stat_float_times) { | 
|  | 1341 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); | 
|  | 1342 | } else { | 
|  | 1343 | fval = ival; | 
|  | 1344 | Py_INCREF(fval); | 
|  | 1345 | } | 
|  | 1346 | PyStructSequence_SET_ITEM(v, index, ival); | 
|  | 1347 | PyStructSequence_SET_ITEM(v, index+3, fval); | 
| Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1348 | } | 
|  | 1349 |  | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1350 | /* pack a system stat C structure into the Python stat tuple | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1351 | (used by posix_stat() and posix_fstat()) */ | 
|  | 1352 | static PyObject* | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1353 | _pystat_fromstructstat(STRUCT_STAT *st) | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1354 | { | 
| Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1355 | unsigned long ansec, mnsec, cnsec; | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1356 | PyObject *v = PyStructSequence_New(&StatResultType); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1357 | if (v == NULL) | 
|  | 1358 | return NULL; | 
|  | 1359 |  | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1360 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1361 | #ifdef HAVE_LARGEFILE_SUPPORT | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1362 | PyStructSequence_SET_ITEM(v, 1, | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1363 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1364 | #else | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1365 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1366 | #endif | 
|  | 1367 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1368 | PyStructSequence_SET_ITEM(v, 2, | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1369 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1370 | #else | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1371 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1372 | #endif | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1373 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); | 
|  | 1374 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); | 
|  | 1375 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1376 | #ifdef HAVE_LARGEFILE_SUPPORT | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1377 | PyStructSequence_SET_ITEM(v, 6, | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1378 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1379 | #else | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1380 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1381 | #endif | 
| Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1382 |  | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1383 | #if defined(HAVE_STAT_TV_NSEC) | 
|  | 1384 | ansec = st->st_atim.tv_nsec; | 
|  | 1385 | mnsec = st->st_mtim.tv_nsec; | 
|  | 1386 | cnsec = st->st_ctim.tv_nsec; | 
|  | 1387 | #elif defined(HAVE_STAT_TV_NSEC2) | 
|  | 1388 | ansec = st->st_atimespec.tv_nsec; | 
|  | 1389 | mnsec = st->st_mtimespec.tv_nsec; | 
|  | 1390 | cnsec = st->st_ctimespec.tv_nsec; | 
|  | 1391 | #elif defined(HAVE_STAT_NSEC) | 
|  | 1392 | ansec = st->st_atime_nsec; | 
|  | 1393 | mnsec = st->st_mtime_nsec; | 
|  | 1394 | cnsec = st->st_ctime_nsec; | 
| Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1395 | #else | 
| Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1396 | ansec = mnsec = cnsec = 0; | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1397 | #endif | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1398 | fill_time(v, 7, st->st_atime, ansec); | 
|  | 1399 | fill_time(v, 8, st->st_mtime, mnsec); | 
|  | 1400 | fill_time(v, 9, st->st_ctime, cnsec); | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1401 |  | 
| Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1402 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1403 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1404 | PyLong_FromLong((long)st->st_blksize)); | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1405 | #endif | 
| Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1406 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1407 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1408 | PyLong_FromLong((long)st->st_blocks)); | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1409 | #endif | 
| Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1410 | #ifdef HAVE_STRUCT_STAT_ST_RDEV | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1411 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1412 | PyLong_FromLong((long)st->st_rdev)); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1413 | #endif | 
| Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1414 | #ifdef HAVE_STRUCT_STAT_ST_GEN | 
|  | 1415 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1416 | PyLong_FromLong((long)st->st_gen)); | 
| Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1417 | #endif | 
|  | 1418 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME | 
|  | 1419 | { | 
|  | 1420 | PyObject *val; | 
|  | 1421 | unsigned long bsec,bnsec; | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1422 | bsec = (long)st->st_birthtime; | 
| Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1423 | #ifdef HAVE_STAT_TV_NSEC2 | 
| Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1424 | bnsec = st->st_birthtimespec.tv_nsec; | 
| Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1425 | #else | 
|  | 1426 | bnsec = 0; | 
|  | 1427 | #endif | 
|  | 1428 | if (_stat_float_times) { | 
|  | 1429 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); | 
|  | 1430 | } else { | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1431 | val = PyLong_FromLong((long)bsec); | 
| Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1432 | } | 
|  | 1433 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, | 
|  | 1434 | val); | 
|  | 1435 | } | 
|  | 1436 | #endif | 
| Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1437 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS | 
|  | 1438 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1439 | PyLong_FromLong((long)st->st_flags)); | 
| Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1440 | #endif | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1441 |  | 
|  | 1442 | if (PyErr_Occurred()) { | 
|  | 1443 | Py_DECREF(v); | 
|  | 1444 | return NULL; | 
|  | 1445 | } | 
|  | 1446 |  | 
|  | 1447 | return v; | 
|  | 1448 | } | 
|  | 1449 |  | 
| Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1450 | #ifdef MS_WINDOWS | 
|  | 1451 |  | 
|  | 1452 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, | 
|  | 1453 | where / can be used in place of \ and the trailing slash is optional. | 
|  | 1454 | Both SERVER and SHARE must have at least one character. | 
|  | 1455 | */ | 
|  | 1456 |  | 
|  | 1457 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') | 
|  | 1458 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1459 | #ifndef ARRAYSIZE | 
| Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1460 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1461 | #endif | 
| Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1462 |  | 
| Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1463 | static BOOL | 
| Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1464 | IsUNCRootA(char *path, int pathlen) | 
|  | 1465 | { | 
|  | 1466 | #define ISSLASH ISSLASHA | 
|  | 1467 |  | 
|  | 1468 | int i, share; | 
|  | 1469 |  | 
|  | 1470 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) | 
|  | 1471 | /* minimum UNCRoot is \\x\y */ | 
|  | 1472 | return FALSE; | 
|  | 1473 | for (i = 2; i < pathlen ; i++) | 
|  | 1474 | if (ISSLASH(path[i])) break; | 
|  | 1475 | if (i == 2 || i == pathlen) | 
|  | 1476 | /* do not allow \\\SHARE or \\SERVER */ | 
|  | 1477 | return FALSE; | 
|  | 1478 | share = i+1; | 
|  | 1479 | for (i = share; i < pathlen; i++) | 
|  | 1480 | if (ISSLASH(path[i])) break; | 
|  | 1481 | return (i != share && (i == pathlen || i == pathlen-1)); | 
|  | 1482 |  | 
|  | 1483 | #undef ISSLASH | 
|  | 1484 | } | 
|  | 1485 |  | 
| Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1486 | static BOOL | 
| Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1487 | IsUNCRootW(Py_UNICODE *path, int pathlen) | 
|  | 1488 | { | 
|  | 1489 | #define ISSLASH ISSLASHW | 
|  | 1490 |  | 
|  | 1491 | int i, share; | 
|  | 1492 |  | 
|  | 1493 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) | 
|  | 1494 | /* minimum UNCRoot is \\x\y */ | 
|  | 1495 | return FALSE; | 
|  | 1496 | for (i = 2; i < pathlen ; i++) | 
|  | 1497 | if (ISSLASH(path[i])) break; | 
|  | 1498 | if (i == 2 || i == pathlen) | 
|  | 1499 | /* do not allow \\\SHARE or \\SERVER */ | 
|  | 1500 | return FALSE; | 
|  | 1501 | share = i+1; | 
|  | 1502 | for (i = share; i < pathlen; i++) | 
|  | 1503 | if (ISSLASH(path[i])) break; | 
|  | 1504 | return (i != share && (i == pathlen || i == pathlen-1)); | 
|  | 1505 |  | 
|  | 1506 | #undef ISSLASH | 
|  | 1507 | } | 
| Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1508 | #endif /* MS_WINDOWS */ | 
|  | 1509 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1510 | static PyObject * | 
| Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1511 | posix_do_stat(PyObject *self, PyObject *args, | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1512 | char *format, | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1513 | #ifdef __VMS | 
|  | 1514 | int (*statfunc)(const char *, STRUCT_STAT *, ...), | 
|  | 1515 | #else | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1516 | int (*statfunc)(const char *, STRUCT_STAT *), | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1517 | #endif | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1518 | char *wformat, | 
|  | 1519 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1520 | { | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1521 | STRUCT_STAT st; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1522 | PyObject *opath; | 
|  | 1523 | char *path; | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1524 | int res; | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1525 | PyObject *result; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1526 |  | 
| Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1527 | #ifdef MS_WINDOWS | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1528 | PyUnicodeObject *po; | 
|  | 1529 | if (PyArg_ParseTuple(args, wformat, &po)) { | 
|  | 1530 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1531 |  | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1532 | Py_BEGIN_ALLOW_THREADS | 
|  | 1533 | /* PyUnicode_AS_UNICODE result OK without | 
|  | 1534 | thread lock as it is a simple dereference. */ | 
|  | 1535 | res = wstatfunc(wpath, &st); | 
|  | 1536 | Py_END_ALLOW_THREADS | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1537 |  | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1538 | if (res != 0) | 
|  | 1539 | return win32_error_unicode("stat", wpath); | 
|  | 1540 | return _pystat_fromstructstat(&st); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1541 | } | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1542 | /* Drop the argument parsing error as narrow strings | 
|  | 1543 | are also valid. */ | 
|  | 1544 | PyErr_Clear(); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1545 | #endif | 
|  | 1546 |  | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1547 | if (!PyArg_ParseTuple(args, format, | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1548 | PyUnicode_FSConverter, &opath)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1549 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1550 | path = bytes2str(opath, 1); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1551 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1552 | res = (*statfunc)(path, &st); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1553 | Py_END_ALLOW_THREADS | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1554 |  | 
|  | 1555 | if (res != 0) { | 
|  | 1556 | #ifdef MS_WINDOWS | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1557 | result = win32_error("stat", path); | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1558 | #else | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1559 | result = posix_error_with_filename(path); | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1560 | #endif | 
|  | 1561 | } | 
|  | 1562 | else | 
|  | 1563 | result = _pystat_fromstructstat(&st); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1564 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1565 | release_bytes(opath); | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1566 | return result; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1567 | } | 
|  | 1568 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1569 | /* POSIX methods */ | 
|  | 1570 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1571 | PyDoc_STRVAR(posix_access__doc__, | 
| Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1572 | "access(path, mode) -> True if granted, False otherwise\n\n\ | 
| Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1573 | Use the real uid/gid to test for access to a path.  Note that most\n\ | 
|  | 1574 | operations will use the effective uid/gid, therefore this routine can\n\ | 
|  | 1575 | be used in a suid/sgid environment to test if the invoking user has the\n\ | 
|  | 1576 | specified access to the path.  The mode argument can be F_OK to test\n\ | 
|  | 1577 | existence, or the inclusive-OR of R_OK, W_OK, and X_OK."); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1578 |  | 
|  | 1579 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1580 | posix_access(PyObject *self, PyObject *args) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1581 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1582 | PyObject *opath; | 
| Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1583 | char *path; | 
|  | 1584 | int mode; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1585 |  | 
| Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1586 | #ifdef MS_WINDOWS | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1587 | DWORD attr; | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1588 | PyUnicodeObject *po; | 
|  | 1589 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { | 
|  | 1590 | Py_BEGIN_ALLOW_THREADS | 
|  | 1591 | /* PyUnicode_AS_UNICODE OK without thread lock as | 
|  | 1592 | it is a simple dereference. */ | 
|  | 1593 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); | 
|  | 1594 | Py_END_ALLOW_THREADS | 
|  | 1595 | goto finish; | 
| Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1596 | } | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1597 | /* Drop the argument parsing error as narrow strings | 
|  | 1598 | are also valid. */ | 
|  | 1599 | PyErr_Clear(); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1600 | if (!PyArg_ParseTuple(args, "O&i:access", | 
|  | 1601 | PyUnicode_FSConverter, &opath, &mode)) | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1602 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1603 | path = bytes2str(opath, 1); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1604 | Py_BEGIN_ALLOW_THREADS | 
|  | 1605 | attr = GetFileAttributesA(path); | 
|  | 1606 | Py_END_ALLOW_THREADS | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1607 | release_bytes(opath); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1608 | finish: | 
|  | 1609 | if (attr == 0xFFFFFFFF) | 
|  | 1610 | /* File does not exist, or cannot read attributes */ | 
|  | 1611 | return PyBool_FromLong(0); | 
|  | 1612 | /* Access is possible if either write access wasn't requested, or | 
| Guido van Rossum | b00324f | 2007-12-04 01:13:14 +0000 | [diff] [blame] | 1613 | the file isn't read-only, or if it's a directory, as there are | 
|  | 1614 | no read-only directories on Windows. */ | 
|  | 1615 | return PyBool_FromLong(!(mode & 2) | 
|  | 1616 | || !(attr & FILE_ATTRIBUTE_READONLY) | 
|  | 1617 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1618 | #else | 
|  | 1619 | int res; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1620 | if (!PyArg_ParseTuple(args, "O&i:access", | 
|  | 1621 | PyUnicode_FSConverter, &opath, &mode)) | 
| Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1622 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1623 | path = bytes2str(opath, 1); | 
| Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1624 | Py_BEGIN_ALLOW_THREADS | 
|  | 1625 | res = access(path, mode); | 
|  | 1626 | Py_END_ALLOW_THREADS | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1627 | release_bytes(opath); | 
| Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1628 | return PyBool_FromLong(res == 0); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1629 | #endif | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1630 | } | 
|  | 1631 |  | 
| Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1632 | #ifndef F_OK | 
|  | 1633 | #define F_OK 0 | 
|  | 1634 | #endif | 
|  | 1635 | #ifndef R_OK | 
|  | 1636 | #define R_OK 4 | 
|  | 1637 | #endif | 
|  | 1638 | #ifndef W_OK | 
|  | 1639 | #define W_OK 2 | 
|  | 1640 | #endif | 
|  | 1641 | #ifndef X_OK | 
|  | 1642 | #define X_OK 1 | 
|  | 1643 | #endif | 
|  | 1644 |  | 
|  | 1645 | #ifdef HAVE_TTYNAME | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1646 | PyDoc_STRVAR(posix_ttyname__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1647 | "ttyname(fd) -> string\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1648 | Return the name of the terminal device connected to 'fd'."); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1649 |  | 
|  | 1650 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1651 | posix_ttyname(PyObject *self, PyObject *args) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1652 | { | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1653 | int id; | 
|  | 1654 | char *ret; | 
|  | 1655 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1656 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1657 | return NULL; | 
|  | 1658 |  | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1659 | #if defined(__VMS) | 
| Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1660 | /* file descriptor 0 only, the default input device (stdin) */ | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1661 | if (id == 0) { | 
|  | 1662 | ret = ttyname(); | 
|  | 1663 | } | 
|  | 1664 | else { | 
|  | 1665 | ret = NULL; | 
|  | 1666 | } | 
|  | 1667 | #else | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1668 | ret = ttyname(id); | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1669 | #endif | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1670 | if (ret == NULL) | 
| Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1671 | return posix_error(); | 
| Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1672 | return PyUnicode_FromString(ret); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1673 | } | 
| Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1674 | #endif | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1675 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1676 | #ifdef HAVE_CTERMID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1677 | PyDoc_STRVAR(posix_ctermid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1678 | "ctermid() -> string\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1679 | Return the name of the controlling terminal for this process."); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1680 |  | 
|  | 1681 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1682 | posix_ctermid(PyObject *self, PyObject *noargs) | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1683 | { | 
|  | 1684 | char *ret; | 
|  | 1685 | char buffer[L_ctermid]; | 
|  | 1686 |  | 
| Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1687 | #ifdef USE_CTERMID_R | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1688 | ret = ctermid_r(buffer); | 
|  | 1689 | #else | 
|  | 1690 | ret = ctermid(buffer); | 
|  | 1691 | #endif | 
|  | 1692 | if (ret == NULL) | 
| Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1693 | return posix_error(); | 
| Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1694 | return PyUnicode_FromString(buffer); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1695 | } | 
|  | 1696 | #endif | 
|  | 1697 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1698 | PyDoc_STRVAR(posix_chdir__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1699 | "chdir(path)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1700 | Change the current working directory to the specified path."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1701 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1702 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1703 | posix_chdir(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1704 | { | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1705 | #ifdef MS_WINDOWS | 
| Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 1706 | return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1707 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1708 | return posix_1str(args, "O&:chdir", _chdir2); | 
| Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1709 | #elif defined(__VMS) | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1710 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1711 | #else | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1712 | return posix_1str(args, "O&:chdir", chdir); | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1713 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1714 | } | 
|  | 1715 |  | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1716 | #ifdef HAVE_FCHDIR | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1717 | PyDoc_STRVAR(posix_fchdir__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1718 | "fchdir(fildes)\n\n\ | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1719 | Change to the directory of the given file descriptor.  fildes must be\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1720 | opened on a directory, not a file."); | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1721 |  | 
|  | 1722 | static PyObject * | 
|  | 1723 | posix_fchdir(PyObject *self, PyObject *fdobj) | 
|  | 1724 | { | 
|  | 1725 | return posix_fildes(fdobj, fchdir); | 
|  | 1726 | } | 
|  | 1727 | #endif /* HAVE_FCHDIR */ | 
|  | 1728 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1729 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1730 | PyDoc_STRVAR(posix_chmod__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1731 | "chmod(path, mode)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1732 | Change the access permissions of a file."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1733 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1734 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1735 | posix_chmod(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1736 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1737 | PyObject *opath = NULL; | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1738 | char *path = NULL; | 
| Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1739 | int i; | 
|  | 1740 | int res; | 
| Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1741 | #ifdef MS_WINDOWS | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1742 | DWORD attr; | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1743 | PyUnicodeObject *po; | 
|  | 1744 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { | 
|  | 1745 | Py_BEGIN_ALLOW_THREADS | 
|  | 1746 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); | 
|  | 1747 | if (attr != 0xFFFFFFFF) { | 
|  | 1748 | if (i & _S_IWRITE) | 
|  | 1749 | attr &= ~FILE_ATTRIBUTE_READONLY; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1750 | else | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1751 | attr |= FILE_ATTRIBUTE_READONLY; | 
|  | 1752 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); | 
| Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1753 | } | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1754 | else | 
|  | 1755 | res = 0; | 
|  | 1756 | Py_END_ALLOW_THREADS | 
|  | 1757 | if (!res) | 
|  | 1758 | return win32_error_unicode("chmod", | 
|  | 1759 | PyUnicode_AS_UNICODE(po)); | 
|  | 1760 | Py_INCREF(Py_None); | 
|  | 1761 | return Py_None; | 
| Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1762 | } | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1763 | /* Drop the argument parsing error as narrow strings | 
|  | 1764 | are also valid. */ | 
|  | 1765 | PyErr_Clear(); | 
|  | 1766 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1767 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, | 
|  | 1768 | &opath, &i)) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1769 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1770 | path = bytes2str(opath, 1); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1771 | Py_BEGIN_ALLOW_THREADS | 
|  | 1772 | attr = GetFileAttributesA(path); | 
|  | 1773 | if (attr != 0xFFFFFFFF) { | 
|  | 1774 | if (i & _S_IWRITE) | 
|  | 1775 | attr &= ~FILE_ATTRIBUTE_READONLY; | 
|  | 1776 | else | 
|  | 1777 | attr |= FILE_ATTRIBUTE_READONLY; | 
|  | 1778 | res = SetFileAttributesA(path, attr); | 
|  | 1779 | } | 
|  | 1780 | else | 
|  | 1781 | res = 0; | 
|  | 1782 | Py_END_ALLOW_THREADS | 
|  | 1783 | if (!res) { | 
|  | 1784 | win32_error("chmod", path); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1785 | release_bytes(opath); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1786 | return NULL; | 
|  | 1787 | } | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1788 | release_bytes(opath); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1789 | Py_INCREF(Py_None); | 
|  | 1790 | return Py_None; | 
| Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1791 | #else /* MS_WINDOWS */ | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1792 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, | 
|  | 1793 | &opath, &i)) | 
| Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1794 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1795 | path = bytes2str(opath, 1); | 
| Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1796 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1797 | res = chmod(path, i); | 
| Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1798 | Py_END_ALLOW_THREADS | 
|  | 1799 | if (res < 0) | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1800 | return posix_error_with_allocated_filename(opath); | 
|  | 1801 | release_bytes(opath); | 
| Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1802 | Py_INCREF(Py_None); | 
|  | 1803 | return Py_None; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1804 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1805 | } | 
|  | 1806 |  | 
| Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1807 | #ifdef HAVE_FCHMOD | 
|  | 1808 | PyDoc_STRVAR(posix_fchmod__doc__, | 
|  | 1809 | "fchmod(fd, mode)\n\n\ | 
|  | 1810 | Change the access permissions of the file given by file\n\ | 
|  | 1811 | descriptor fd."); | 
|  | 1812 |  | 
|  | 1813 | static PyObject * | 
|  | 1814 | posix_fchmod(PyObject *self, PyObject *args) | 
|  | 1815 | { | 
|  | 1816 | int fd, mode, res; | 
|  | 1817 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) | 
|  | 1818 | return NULL; | 
|  | 1819 | Py_BEGIN_ALLOW_THREADS | 
|  | 1820 | res = fchmod(fd, mode); | 
|  | 1821 | Py_END_ALLOW_THREADS | 
|  | 1822 | if (res < 0) | 
|  | 1823 | return posix_error(); | 
|  | 1824 | Py_RETURN_NONE; | 
|  | 1825 | } | 
|  | 1826 | #endif /* HAVE_FCHMOD */ | 
|  | 1827 |  | 
|  | 1828 | #ifdef HAVE_LCHMOD | 
|  | 1829 | PyDoc_STRVAR(posix_lchmod__doc__, | 
|  | 1830 | "lchmod(path, mode)\n\n\ | 
|  | 1831 | Change the access permissions of a file. If path is a symlink, this\n\ | 
|  | 1832 | affects the link itself rather than the target."); | 
|  | 1833 |  | 
|  | 1834 | static PyObject * | 
|  | 1835 | posix_lchmod(PyObject *self, PyObject *args) | 
|  | 1836 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1837 | PyObject *opath; | 
|  | 1838 | char *path; | 
| Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1839 | int i; | 
|  | 1840 | int res; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1841 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, | 
|  | 1842 | &opath, &i)) | 
| Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1843 | return NULL; | 
| Eric Smith | 86a05ec | 2009-05-05 13:07:30 +0000 | [diff] [blame] | 1844 | path = bytes2str(opath, 1); | 
| Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1845 | Py_BEGIN_ALLOW_THREADS | 
|  | 1846 | res = lchmod(path, i); | 
|  | 1847 | Py_END_ALLOW_THREADS | 
|  | 1848 | if (res < 0) | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1849 | return posix_error_with_allocated_filename(opath); | 
|  | 1850 | release_bytes(opath); | 
| Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1851 | Py_RETURN_NONE; | 
|  | 1852 | } | 
|  | 1853 | #endif /* HAVE_LCHMOD */ | 
|  | 1854 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1855 |  | 
| Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1856 | #ifdef HAVE_CHFLAGS | 
|  | 1857 | PyDoc_STRVAR(posix_chflags__doc__, | 
|  | 1858 | "chflags(path, flags)\n\n\ | 
|  | 1859 | Set file flags."); | 
|  | 1860 |  | 
|  | 1861 | static PyObject * | 
|  | 1862 | posix_chflags(PyObject *self, PyObject *args) | 
|  | 1863 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1864 | PyObject *opath; | 
| Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1865 | char *path; | 
|  | 1866 | unsigned long flags; | 
|  | 1867 | int res; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1868 | if (!PyArg_ParseTuple(args, "O&k:chflags", | 
|  | 1869 | PyUnicode_FSConverter, &opath, &flags)) | 
| Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1870 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1871 | path = bytes2str(opath, 1); | 
| Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1872 | Py_BEGIN_ALLOW_THREADS | 
|  | 1873 | res = chflags(path, flags); | 
|  | 1874 | Py_END_ALLOW_THREADS | 
|  | 1875 | if (res < 0) | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1876 | return posix_error_with_allocated_filename(opath); | 
|  | 1877 | release_bytes(opath); | 
| Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1878 | Py_INCREF(Py_None); | 
|  | 1879 | return Py_None; | 
|  | 1880 | } | 
|  | 1881 | #endif /* HAVE_CHFLAGS */ | 
|  | 1882 |  | 
|  | 1883 | #ifdef HAVE_LCHFLAGS | 
|  | 1884 | PyDoc_STRVAR(posix_lchflags__doc__, | 
|  | 1885 | "lchflags(path, flags)\n\n\ | 
|  | 1886 | Set file flags.\n\ | 
|  | 1887 | This function will not follow symbolic links."); | 
|  | 1888 |  | 
|  | 1889 | static PyObject * | 
|  | 1890 | posix_lchflags(PyObject *self, PyObject *args) | 
|  | 1891 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1892 | PyObject *opath; | 
| Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1893 | char *path; | 
|  | 1894 | unsigned long flags; | 
|  | 1895 | int res; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1896 | if (!PyArg_ParseTuple(args, "O&k:lchflags", | 
| Martin v. Löwis | 4adbc34 | 2009-05-05 17:17:55 +0000 | [diff] [blame] | 1897 | PyUnicode_FSConverter, &opath, &flags)) | 
| Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1898 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1899 | path = bytes2str(opath, 1); | 
| Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1900 | Py_BEGIN_ALLOW_THREADS | 
|  | 1901 | res = lchflags(path, flags); | 
|  | 1902 | Py_END_ALLOW_THREADS | 
|  | 1903 | if (res < 0) | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1904 | return posix_error_with_allocated_filename(opath); | 
|  | 1905 | release_bytes(opath); | 
| Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1906 | Py_INCREF(Py_None); | 
|  | 1907 | return Py_None; | 
|  | 1908 | } | 
|  | 1909 | #endif /* HAVE_LCHFLAGS */ | 
|  | 1910 |  | 
| Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1911 | #ifdef HAVE_CHROOT | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1912 | PyDoc_STRVAR(posix_chroot__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1913 | "chroot(path)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1914 | Change root directory to path."); | 
| Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1915 |  | 
|  | 1916 | static PyObject * | 
|  | 1917 | posix_chroot(PyObject *self, PyObject *args) | 
|  | 1918 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1919 | return posix_1str(args, "O&:chroot", chroot); | 
| Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1920 | } | 
|  | 1921 | #endif | 
|  | 1922 |  | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1923 | #ifdef HAVE_FSYNC | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1924 | PyDoc_STRVAR(posix_fsync__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1925 | "fsync(fildes)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1926 | force write of file with filedescriptor to disk."); | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1927 |  | 
|  | 1928 | static PyObject * | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1929 | posix_fsync(PyObject *self, PyObject *fdobj) | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1930 | { | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1931 | return posix_fildes(fdobj, fsync); | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1932 | } | 
|  | 1933 | #endif /* HAVE_FSYNC */ | 
|  | 1934 |  | 
|  | 1935 | #ifdef HAVE_FDATASYNC | 
| Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1936 |  | 
| Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1937 | #ifdef __hpux | 
| Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1938 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ | 
|  | 1939 | #endif | 
|  | 1940 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1941 | PyDoc_STRVAR(posix_fdatasync__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1942 | "fdatasync(fildes)\n\n\ | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1943 | force write of file with filedescriptor to disk.\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1944 | does not force update of metadata."); | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1945 |  | 
|  | 1946 | static PyObject * | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1947 | posix_fdatasync(PyObject *self, PyObject *fdobj) | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1948 | { | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1949 | return posix_fildes(fdobj, fdatasync); | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1950 | } | 
|  | 1951 | #endif /* HAVE_FDATASYNC */ | 
|  | 1952 |  | 
|  | 1953 |  | 
| Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1954 | #ifdef HAVE_CHOWN | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1955 | PyDoc_STRVAR(posix_chown__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1956 | "chown(path, uid, gid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1957 | Change the owner and group id of path to the numeric uid and gid."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1958 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1959 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1960 | posix_chown(PyObject *self, PyObject *args) | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1961 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1962 | PyObject *opath; | 
|  | 1963 | char *path; | 
| Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 1964 | long uid, gid; | 
| Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1965 | int res; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1966 | if (!PyArg_ParseTuple(args, "O&ll:chown", | 
|  | 1967 | PyUnicode_FSConverter, &opath, | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1968 | &uid, &gid)) | 
| Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1969 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1970 | path = bytes2str(opath, 1); | 
| Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1971 | Py_BEGIN_ALLOW_THREADS | 
|  | 1972 | res = chown(path, (uid_t) uid, (gid_t) gid); | 
|  | 1973 | Py_END_ALLOW_THREADS | 
|  | 1974 | if (res < 0) | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1975 | return posix_error_with_allocated_filename(opath); | 
|  | 1976 | release_bytes(opath); | 
| Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1977 | Py_INCREF(Py_None); | 
|  | 1978 | return Py_None; | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1979 | } | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1980 | #endif /* HAVE_CHOWN */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1981 |  | 
| Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1982 | #ifdef HAVE_FCHOWN | 
|  | 1983 | PyDoc_STRVAR(posix_fchown__doc__, | 
|  | 1984 | "fchown(fd, uid, gid)\n\n\ | 
|  | 1985 | Change the owner and group id of the file given by file descriptor\n\ | 
|  | 1986 | fd to the numeric uid and gid."); | 
|  | 1987 |  | 
|  | 1988 | static PyObject * | 
|  | 1989 | posix_fchown(PyObject *self, PyObject *args) | 
|  | 1990 | { | 
|  | 1991 | int fd, uid, gid; | 
|  | 1992 | int res; | 
|  | 1993 | if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid)) | 
|  | 1994 | return NULL; | 
|  | 1995 | Py_BEGIN_ALLOW_THREADS | 
|  | 1996 | res = fchown(fd, (uid_t) uid, (gid_t) gid); | 
|  | 1997 | Py_END_ALLOW_THREADS | 
|  | 1998 | if (res < 0) | 
|  | 1999 | return posix_error(); | 
|  | 2000 | Py_RETURN_NONE; | 
|  | 2001 | } | 
|  | 2002 | #endif /* HAVE_FCHOWN */ | 
|  | 2003 |  | 
| Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2004 | #ifdef HAVE_LCHOWN | 
|  | 2005 | PyDoc_STRVAR(posix_lchown__doc__, | 
|  | 2006 | "lchown(path, uid, gid)\n\n\ | 
|  | 2007 | Change the owner and group id of path to the numeric uid and gid.\n\ | 
|  | 2008 | This function will not follow symbolic links."); | 
|  | 2009 |  | 
|  | 2010 | static PyObject * | 
|  | 2011 | posix_lchown(PyObject *self, PyObject *args) | 
|  | 2012 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2013 | PyObject *opath; | 
|  | 2014 | char *path; | 
| Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2015 | int uid, gid; | 
|  | 2016 | int res; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2017 | if (!PyArg_ParseTuple(args, "O&ii:lchown", | 
|  | 2018 | PyUnicode_FSConverter, &opath, | 
| Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2019 | &uid, &gid)) | 
|  | 2020 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2021 | path = bytes2str(opath, 1); | 
| Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2022 | Py_BEGIN_ALLOW_THREADS | 
|  | 2023 | res = lchown(path, (uid_t) uid, (gid_t) gid); | 
|  | 2024 | Py_END_ALLOW_THREADS | 
| Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2025 | if (res < 0) | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2026 | return posix_error_with_allocated_filename(opath); | 
|  | 2027 | release_bytes(opath); | 
| Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2028 | Py_INCREF(Py_None); | 
|  | 2029 | return Py_None; | 
|  | 2030 | } | 
|  | 2031 | #endif /* HAVE_LCHOWN */ | 
|  | 2032 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2033 |  | 
| Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2034 | #ifdef HAVE_GETCWD | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2035 | static PyObject * | 
| Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2036 | posix_getcwd(int use_bytes) | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2037 | { | 
|  | 2038 | char buf[1026]; | 
|  | 2039 | char *res; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2040 |  | 
| Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2041 | #ifdef MS_WINDOWS | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2042 | if (!use_bytes) { | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2043 | wchar_t wbuf[1026]; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2044 | wchar_t *wbuf2 = wbuf; | 
|  | 2045 | PyObject *resobj; | 
| Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2046 | DWORD len; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2047 | Py_BEGIN_ALLOW_THREADS | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2048 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); | 
|  | 2049 | /* If the buffer is large enough, len does not include the | 
|  | 2050 | terminating \0. If the buffer is too small, len includes | 
|  | 2051 | the space needed for the terminator. */ | 
|  | 2052 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { | 
|  | 2053 | wbuf2 = malloc(len * sizeof(wchar_t)); | 
|  | 2054 | if (wbuf2) | 
|  | 2055 | len = GetCurrentDirectoryW(len, wbuf2); | 
|  | 2056 | } | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2057 | Py_END_ALLOW_THREADS | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2058 | if (!wbuf2) { | 
|  | 2059 | PyErr_NoMemory(); | 
|  | 2060 | return NULL; | 
|  | 2061 | } | 
|  | 2062 | if (!len) { | 
|  | 2063 | if (wbuf2 != wbuf) free(wbuf2); | 
|  | 2064 | return win32_error("getcwdu", NULL); | 
|  | 2065 | } | 
|  | 2066 | resobj = PyUnicode_FromWideChar(wbuf2, len); | 
|  | 2067 | if (wbuf2 != wbuf) free(wbuf2); | 
|  | 2068 | return resobj; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2069 | } | 
|  | 2070 | #endif | 
|  | 2071 |  | 
|  | 2072 | Py_BEGIN_ALLOW_THREADS | 
|  | 2073 | #if defined(PYOS_OS2) && defined(PYCC_GCC) | 
|  | 2074 | res = _getcwd2(buf, sizeof buf); | 
|  | 2075 | #else | 
|  | 2076 | res = getcwd(buf, sizeof buf); | 
|  | 2077 | #endif | 
|  | 2078 | Py_END_ALLOW_THREADS | 
|  | 2079 | if (res == NULL) | 
|  | 2080 | return posix_error(); | 
| Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2081 | if (use_bytes) | 
|  | 2082 | return PyBytes_FromStringAndSize(buf, strlen(buf)); | 
| Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2083 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape"); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2084 | } | 
| Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2085 |  | 
|  | 2086 | PyDoc_STRVAR(posix_getcwd__doc__, | 
|  | 2087 | "getcwd() -> path\n\n\ | 
|  | 2088 | Return a unicode string representing the current working directory."); | 
|  | 2089 |  | 
|  | 2090 | static PyObject * | 
|  | 2091 | posix_getcwd_unicode(PyObject *self) | 
|  | 2092 | { | 
|  | 2093 | return posix_getcwd(0); | 
|  | 2094 | } | 
|  | 2095 |  | 
|  | 2096 | PyDoc_STRVAR(posix_getcwdb__doc__, | 
|  | 2097 | "getcwdb() -> path\n\n\ | 
|  | 2098 | Return a bytes string representing the current working directory."); | 
|  | 2099 |  | 
|  | 2100 | static PyObject * | 
|  | 2101 | posix_getcwd_bytes(PyObject *self) | 
|  | 2102 | { | 
|  | 2103 | return posix_getcwd(1); | 
|  | 2104 | } | 
| Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2105 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2106 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2107 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2108 | #ifdef HAVE_LINK | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2109 | PyDoc_STRVAR(posix_link__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2110 | "link(src, dst)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2111 | Create a hard link to a file."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2112 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2113 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2114 | posix_link(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2115 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2116 | return posix_2str(args, "O&O&:link", link); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2117 | } | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2118 | #endif /* HAVE_LINK */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2119 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2120 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2121 | PyDoc_STRVAR(posix_listdir__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2122 | "listdir(path) -> list_of_strings\n\n\ | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2123 | Return a list containing the names of the entries in the directory.\n\ | 
|  | 2124 | \n\ | 
|  | 2125 | path: path of directory to list\n\ | 
|  | 2126 | \n\ | 
|  | 2127 | The list is in arbitrary order.  It does not include the special\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2128 | entries '.' and '..' even if they are present in the directory."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2129 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2130 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2131 | posix_listdir(PyObject *self, PyObject *args) | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2132 | { | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2133 | /* XXX Should redo this putting the (now four) versions of opendir | 
| Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2134 | in separate files instead of having them all here... */ | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2135 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2136 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2137 | PyObject *d, *v; | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2138 | HANDLE hFindFile; | 
| Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2139 | BOOL result; | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2140 | WIN32_FIND_DATA FileData; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2141 | PyObject *opath; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2142 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2143 | char *bufptr = namebuf; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2144 | Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2145 |  | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2146 | PyObject *po; | 
|  | 2147 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { | 
|  | 2148 | WIN32_FIND_DATAW wFileData; | 
|  | 2149 | Py_UNICODE *wnamebuf; | 
|  | 2150 | /* Overallocate for \\*.*\0 */ | 
|  | 2151 | len = PyUnicode_GET_SIZE(po); | 
|  | 2152 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); | 
|  | 2153 | if (!wnamebuf) { | 
|  | 2154 | PyErr_NoMemory(); | 
|  | 2155 | return NULL; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2156 | } | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2157 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); | 
|  | 2158 | if (len > 0) { | 
|  | 2159 | Py_UNICODE wch = wnamebuf[len-1]; | 
|  | 2160 | if (wch != L'/' && wch != L'\\' && wch != L':') | 
|  | 2161 | wnamebuf[len++] = L'\\'; | 
|  | 2162 | wcscpy(wnamebuf + len, L"*.*"); | 
|  | 2163 | } | 
|  | 2164 | if ((d = PyList_New(0)) == NULL) { | 
|  | 2165 | free(wnamebuf); | 
|  | 2166 | return NULL; | 
|  | 2167 | } | 
|  | 2168 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); | 
|  | 2169 | if (hFindFile == INVALID_HANDLE_VALUE) { | 
|  | 2170 | int error = GetLastError(); | 
|  | 2171 | if (error == ERROR_FILE_NOT_FOUND) { | 
|  | 2172 | free(wnamebuf); | 
|  | 2173 | return d; | 
|  | 2174 | } | 
|  | 2175 | Py_DECREF(d); | 
|  | 2176 | win32_error_unicode("FindFirstFileW", wnamebuf); | 
|  | 2177 | free(wnamebuf); | 
|  | 2178 | return NULL; | 
|  | 2179 | } | 
|  | 2180 | do { | 
|  | 2181 | /* Skip over . and .. */ | 
|  | 2182 | if (wcscmp(wFileData.cFileName, L".") != 0 && | 
|  | 2183 | wcscmp(wFileData.cFileName, L"..") != 0) { | 
|  | 2184 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); | 
|  | 2185 | if (v == NULL) { | 
|  | 2186 | Py_DECREF(d); | 
|  | 2187 | d = NULL; | 
|  | 2188 | break; | 
|  | 2189 | } | 
|  | 2190 | if (PyList_Append(d, v) != 0) { | 
|  | 2191 | Py_DECREF(v); | 
|  | 2192 | Py_DECREF(d); | 
|  | 2193 | d = NULL; | 
|  | 2194 | break; | 
|  | 2195 | } | 
|  | 2196 | Py_DECREF(v); | 
|  | 2197 | } | 
|  | 2198 | Py_BEGIN_ALLOW_THREADS | 
|  | 2199 | result = FindNextFileW(hFindFile, &wFileData); | 
|  | 2200 | Py_END_ALLOW_THREADS | 
|  | 2201 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if | 
|  | 2202 | it got to the end of the directory. */ | 
|  | 2203 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { | 
|  | 2204 | Py_DECREF(d); | 
|  | 2205 | win32_error_unicode("FindNextFileW", wnamebuf); | 
|  | 2206 | FindClose(hFindFile); | 
|  | 2207 | free(wnamebuf); | 
|  | 2208 | return NULL; | 
|  | 2209 | } | 
|  | 2210 | } while (result == TRUE); | 
|  | 2211 |  | 
|  | 2212 | if (FindClose(hFindFile) == FALSE) { | 
|  | 2213 | Py_DECREF(d); | 
|  | 2214 | win32_error_unicode("FindClose", wnamebuf); | 
|  | 2215 | free(wnamebuf); | 
|  | 2216 | return NULL; | 
|  | 2217 | } | 
|  | 2218 | free(wnamebuf); | 
|  | 2219 | return d; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2220 | } | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2221 | /* Drop the argument parsing error as narrow strings | 
|  | 2222 | are also valid. */ | 
|  | 2223 | PyErr_Clear(); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2224 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2225 | if (!PyArg_ParseTuple(args, "O&:listdir", | 
|  | 2226 | PyUnicode_FSConverter, &opath)) | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2227 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2228 | if (PyObject_Size(opath)+1 > MAX_PATH) { | 
|  | 2229 | PyErr_SetString(PyExc_ValueError, "path too long"); | 
|  | 2230 | Py_DECREF(opath); | 
|  | 2231 | return NULL; | 
|  | 2232 | } | 
|  | 2233 | strcpy(namebuf, bytes2str(opath, 0)); | 
|  | 2234 | len = PyObject_Size(opath); | 
| Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2235 | if (len > 0) { | 
|  | 2236 | char ch = namebuf[len-1]; | 
|  | 2237 | if (ch != SEP && ch != ALTSEP && ch != ':') | 
|  | 2238 | namebuf[len++] = '/'; | 
| Hirokazu Yamamoto | bbb9be7 | 2009-05-04 05:56:46 +0000 | [diff] [blame] | 2239 | strcpy(namebuf + len, "*.*"); | 
| Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2240 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2241 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2242 | if ((d = PyList_New(0)) == NULL) | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2243 | return NULL; | 
|  | 2244 |  | 
|  | 2245 | hFindFile = FindFirstFile(namebuf, &FileData); | 
|  | 2246 | if (hFindFile == INVALID_HANDLE_VALUE) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2247 | int error = GetLastError(); | 
|  | 2248 | if (error == ERROR_FILE_NOT_FOUND) | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2249 | return d; | 
|  | 2250 | Py_DECREF(d); | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2251 | return win32_error("FindFirstFile", namebuf); | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2252 | } | 
|  | 2253 | do { | 
| Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2254 | /* Skip over . and .. */ | 
|  | 2255 | if (strcmp(FileData.cFileName, ".") != 0 && | 
|  | 2256 | strcmp(FileData.cFileName, "..") != 0) { | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2257 | v = PyBytes_FromString(FileData.cFileName); | 
| Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2258 | if (v == NULL) { | 
|  | 2259 | Py_DECREF(d); | 
|  | 2260 | d = NULL; | 
|  | 2261 | break; | 
|  | 2262 | } | 
|  | 2263 | if (PyList_Append(d, v) != 0) { | 
|  | 2264 | Py_DECREF(v); | 
|  | 2265 | Py_DECREF(d); | 
|  | 2266 | d = NULL; | 
|  | 2267 | break; | 
|  | 2268 | } | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2269 | Py_DECREF(v); | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2270 | } | 
| Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2271 | Py_BEGIN_ALLOW_THREADS | 
|  | 2272 | result = FindNextFile(hFindFile, &FileData); | 
|  | 2273 | Py_END_ALLOW_THREADS | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2274 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if | 
|  | 2275 | it got to the end of the directory. */ | 
|  | 2276 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { | 
|  | 2277 | Py_DECREF(d); | 
|  | 2278 | win32_error("FindNextFile", namebuf); | 
|  | 2279 | FindClose(hFindFile); | 
|  | 2280 | return NULL; | 
|  | 2281 | } | 
| Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2282 | } while (result == TRUE); | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2283 |  | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2284 | if (FindClose(hFindFile) == FALSE) { | 
|  | 2285 | Py_DECREF(d); | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2286 | return win32_error("FindClose", namebuf); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2287 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2288 |  | 
|  | 2289 | return d; | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2290 |  | 
| Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2291 | #elif defined(PYOS_OS2) | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2292 |  | 
|  | 2293 | #ifndef MAX_PATH | 
|  | 2294 | #define MAX_PATH    CCHMAXPATH | 
|  | 2295 | #endif | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2296 | PyObject *oname; | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2297 | char *name, *pt; | 
| Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2298 | Py_ssize_t len; | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2299 | PyObject *d, *v; | 
|  | 2300 | char namebuf[MAX_PATH+5]; | 
|  | 2301 | HDIR  hdir = 1; | 
|  | 2302 | ULONG srchcnt = 1; | 
|  | 2303 | FILEFINDBUF3   ep; | 
|  | 2304 | APIRET rc; | 
|  | 2305 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2306 | if (!PyArg_ParseTuple(args, "O&:listdir", | 
|  | 2307 | PyUnicode_FSConverter, &oname)) | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2308 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2309 | name = bytes2str(oname); | 
|  | 2310 | len = PyObject_Size(oname); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2311 | if (len >= MAX_PATH) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2312 | release_bytes(oname); | 
| Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2313 | PyErr_SetString(PyExc_ValueError, "path too long"); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2314 | return NULL; | 
|  | 2315 | } | 
|  | 2316 | strcpy(namebuf, name); | 
|  | 2317 | for (pt = namebuf; *pt; pt++) | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2318 | if (*pt == ALTSEP) | 
|  | 2319 | *pt = SEP; | 
|  | 2320 | if (namebuf[len-1] != SEP) | 
|  | 2321 | namebuf[len++] = SEP; | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2322 | strcpy(namebuf + len, "*.*"); | 
|  | 2323 |  | 
| Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2324 | if ((d = PyList_New(0)) == NULL) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2325 | release_bytes(oname); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2326 | return NULL; | 
| Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2327 | } | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2328 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2329 | rc = DosFindFirst(namebuf,         /* Wildcard Pattern to Match */ | 
|  | 2330 | &hdir,           /* Handle to Use While Search Directory */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2331 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2332 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ | 
|  | 2333 | &srchcnt,        /* Max and Actual Count of Entries Per Iteration */ | 
|  | 2334 | FIL_STANDARD);   /* Format of Entry (EAs or Not) */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2335 |  | 
|  | 2336 | if (rc != NO_ERROR) { | 
|  | 2337 | errno = ENOENT; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2338 | return posix_error_with_allocated_filename(oname); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2339 | } | 
|  | 2340 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2341 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2342 | do { | 
|  | 2343 | if (ep.achName[0] == '.' | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2344 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2345 | continue; /* Skip Over "." and ".." Names */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2346 |  | 
|  | 2347 | strcpy(namebuf, ep.achName); | 
|  | 2348 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2349 | /* Leave Case of Name Alone -- In Native Form */ | 
|  | 2350 | /* (Removed Forced Lowercasing Code) */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2351 |  | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2352 | v = PyBytes_FromString(namebuf); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2353 | if (v == NULL) { | 
|  | 2354 | Py_DECREF(d); | 
|  | 2355 | d = NULL; | 
|  | 2356 | break; | 
|  | 2357 | } | 
|  | 2358 | if (PyList_Append(d, v) != 0) { | 
|  | 2359 | Py_DECREF(v); | 
|  | 2360 | Py_DECREF(d); | 
|  | 2361 | d = NULL; | 
|  | 2362 | break; | 
|  | 2363 | } | 
|  | 2364 | Py_DECREF(v); | 
|  | 2365 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); | 
|  | 2366 | } | 
|  | 2367 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2368 | release_bytes(oname); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2369 | return d; | 
|  | 2370 | #else | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2371 | PyObject *oname; | 
|  | 2372 | char *name; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2373 | PyObject *d, *v; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2374 | DIR *dirp; | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2375 | struct dirent *ep; | 
| Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2376 | int arg_is_unicode = 1; | 
|  | 2377 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2378 | errno = 0; | 
| Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2379 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { | 
|  | 2380 | arg_is_unicode = 0; | 
|  | 2381 | PyErr_Clear(); | 
|  | 2382 | } | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2383 | if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2384 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2385 | name = bytes2str(oname, 1); | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2386 | if ((dirp = opendir(name)) == NULL) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2387 | return posix_error_with_allocated_filename(oname); | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2388 | } | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2389 | if ((d = PyList_New(0)) == NULL) { | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2390 | closedir(dirp); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2391 | release_bytes(oname); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2392 | return NULL; | 
|  | 2393 | } | 
| Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2394 | for (;;) { | 
| Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2395 | errno = 0; | 
| Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2396 | Py_BEGIN_ALLOW_THREADS | 
|  | 2397 | ep = readdir(dirp); | 
|  | 2398 | Py_END_ALLOW_THREADS | 
| Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2399 | if (ep == NULL) { | 
|  | 2400 | if (errno == 0) { | 
|  | 2401 | break; | 
|  | 2402 | } else { | 
|  | 2403 | closedir(dirp); | 
|  | 2404 | Py_DECREF(d); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2405 | return posix_error_with_allocated_filename(oname); | 
| Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2406 | } | 
|  | 2407 | } | 
| Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2408 | if (ep->d_name[0] == '.' && | 
|  | 2409 | (NAMLEN(ep) == 1 || | 
| Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2410 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) | 
| Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2411 | continue; | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2412 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2413 | if (v == NULL) { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2414 | Py_DECREF(d); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2415 | d = NULL; | 
|  | 2416 | break; | 
|  | 2417 | } | 
| Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2418 | if (arg_is_unicode) { | 
| Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2419 | PyObject *w; | 
|  | 2420 |  | 
|  | 2421 | w = PyUnicode_FromEncodedObject(v, | 
| Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2422 | Py_FileSystemDefaultEncoding, | 
| Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2423 | "surrogateescape"); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2424 | Py_DECREF(v); | 
|  | 2425 | if (w != NULL) | 
| Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2426 | v = w; | 
| Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2427 | else { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2428 | /* Encoding failed to decode ASCII bytes. | 
|  | 2429 | Raise exception. */ | 
|  | 2430 | Py_DECREF(d); | 
|  | 2431 | d = NULL; | 
|  | 2432 | break; | 
| Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2433 | } | 
| Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2434 | } | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2435 | if (PyList_Append(d, v) != 0) { | 
|  | 2436 | Py_DECREF(v); | 
|  | 2437 | Py_DECREF(d); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2438 | d = NULL; | 
|  | 2439 | break; | 
|  | 2440 | } | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2441 | Py_DECREF(v); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2442 | } | 
|  | 2443 | closedir(dirp); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2444 | release_bytes(oname); | 
| Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2445 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2446 | return d; | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2447 |  | 
| Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2448 | #endif /* which OS */ | 
|  | 2449 | }  /* end of posix_listdir */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2450 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2451 | #ifdef MS_WINDOWS | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2452 | /* A helper function for abspath on win32 */ | 
|  | 2453 | static PyObject * | 
|  | 2454 | posix__getfullpathname(PyObject *self, PyObject *args) | 
|  | 2455 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2456 | PyObject *opath; | 
|  | 2457 | char *path; | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2458 | char outbuf[MAX_PATH*2]; | 
|  | 2459 | char *temp; | 
| Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2460 | #ifdef MS_WINDOWS | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2461 | PyUnicodeObject *po; | 
|  | 2462 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { | 
|  | 2463 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); | 
|  | 2464 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; | 
|  | 2465 | Py_UNICODE *wtemp; | 
|  | 2466 | DWORD result; | 
|  | 2467 | PyObject *v; | 
|  | 2468 | result = GetFullPathNameW(wpath, | 
|  | 2469 | sizeof(woutbuf)/sizeof(woutbuf[0]), | 
|  | 2470 | woutbuf, &wtemp); | 
|  | 2471 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { | 
|  | 2472 | woutbufp = malloc(result * sizeof(Py_UNICODE)); | 
|  | 2473 | if (!woutbufp) | 
|  | 2474 | return PyErr_NoMemory(); | 
|  | 2475 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2476 | } | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2477 | if (result) | 
|  | 2478 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); | 
|  | 2479 | else | 
|  | 2480 | v = win32_error_unicode("GetFullPathNameW", wpath); | 
|  | 2481 | if (woutbufp != woutbuf) | 
|  | 2482 | free(woutbufp); | 
|  | 2483 | return v; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2484 | } | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2485 | /* Drop the argument parsing error as narrow strings | 
|  | 2486 | are also valid. */ | 
|  | 2487 | PyErr_Clear(); | 
|  | 2488 |  | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2489 | #endif | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2490 | if (!PyArg_ParseTuple (args, "O&:_getfullpathname", | 
|  | 2491 | PyUnicode_FSConverter, &opath)) | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2492 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2493 | path = bytes2str(opath, 1); | 
|  | 2494 | if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]), | 
|  | 2495 | outbuf, &temp)) { | 
|  | 2496 | win32_error("GetFullPathName", path); | 
|  | 2497 | release_bytes(opath); | 
|  | 2498 | return NULL; | 
|  | 2499 | } | 
|  | 2500 | release_bytes(opath); | 
| Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2501 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { | 
|  | 2502 | return PyUnicode_Decode(outbuf, strlen(outbuf), | 
|  | 2503 | Py_FileSystemDefaultEncoding, NULL); | 
|  | 2504 | } | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2505 | return PyBytes_FromString(outbuf); | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2506 | } /* end of posix__getfullpathname */ | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2507 | #endif /* MS_WINDOWS */ | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2508 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2509 | PyDoc_STRVAR(posix_mkdir__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2510 | "mkdir(path [, mode=0777])\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2511 | Create a directory."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2512 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2513 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2514 | posix_mkdir(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2515 | { | 
| Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2516 | int res; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2517 | PyObject *opath; | 
|  | 2518 | char *path; | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2519 | int mode = 0777; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2520 |  | 
| Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2521 | #ifdef MS_WINDOWS | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2522 | PyUnicodeObject *po; | 
|  | 2523 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { | 
|  | 2524 | Py_BEGIN_ALLOW_THREADS | 
|  | 2525 | /* PyUnicode_AS_UNICODE OK without thread lock as | 
|  | 2526 | it is a simple dereference. */ | 
|  | 2527 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); | 
|  | 2528 | Py_END_ALLOW_THREADS | 
|  | 2529 | if (!res) | 
|  | 2530 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); | 
|  | 2531 | Py_INCREF(Py_None); | 
|  | 2532 | return Py_None; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2533 | } | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2534 | /* Drop the argument parsing error as narrow strings | 
|  | 2535 | are also valid. */ | 
|  | 2536 | PyErr_Clear(); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2537 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", | 
|  | 2538 | PyUnicode_FSConverter, &opath, &mode)) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2539 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2540 | path = bytes2str(opath, 1); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2541 | Py_BEGIN_ALLOW_THREADS | 
|  | 2542 | /* PyUnicode_AS_UNICODE OK without thread lock as | 
|  | 2543 | it is a simple dereference. */ | 
|  | 2544 | res = CreateDirectoryA(path, NULL); | 
|  | 2545 | Py_END_ALLOW_THREADS | 
|  | 2546 | if (!res) { | 
|  | 2547 | win32_error("mkdir", path); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2548 | release_bytes(opath); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2549 | return NULL; | 
|  | 2550 | } | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2551 | release_bytes(opath); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2552 | Py_INCREF(Py_None); | 
|  | 2553 | return Py_None; | 
|  | 2554 | #else | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2555 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2556 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", | 
|  | 2557 | PyUnicode_FSConverter, &opath, &mode)) | 
| Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2558 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2559 | path = bytes2str(opath, 1); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2560 | Py_BEGIN_ALLOW_THREADS | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2561 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2562 | res = mkdir(path); | 
|  | 2563 | #else | 
| Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2564 | res = mkdir(path, mode); | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2565 | #endif | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2566 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2567 | if (res < 0) | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2568 | return posix_error_with_allocated_filename(opath); | 
|  | 2569 | release_bytes(opath); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2570 | Py_INCREF(Py_None); | 
|  | 2571 | return Py_None; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2572 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2573 | } | 
|  | 2574 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2575 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2576 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ | 
|  | 2577 | #if defined(HAVE_SYS_RESOURCE_H) | 
| Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2578 | #include <sys/resource.h> | 
|  | 2579 | #endif | 
| Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2580 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2581 |  | 
|  | 2582 | #ifdef HAVE_NICE | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2583 | PyDoc_STRVAR(posix_nice__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2584 | "nice(inc) -> new_priority\n\n\ | 
|  | 2585 | Decrease the priority of process by inc and return the new priority."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2586 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2587 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2588 | posix_nice(PyObject *self, PyObject *args) | 
| Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2589 | { | 
|  | 2590 | int increment, value; | 
|  | 2591 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2592 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) | 
| Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2593 | return NULL; | 
| Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2594 |  | 
|  | 2595 | /* There are two flavours of 'nice': one that returns the new | 
|  | 2596 | priority (as required by almost all standards out there) and the | 
| Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2597 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices | 
|  | 2598 | the use of getpriority() to get the new priority. | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2599 |  | 
| Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2600 | If we are of the nice family that returns the new priority, we | 
|  | 2601 | need to clear errno before the call, and check if errno is filled | 
|  | 2602 | before calling posix_error() on a returnvalue of -1, because the | 
|  | 2603 | -1 may be the actual new priority! */ | 
|  | 2604 |  | 
|  | 2605 | errno = 0; | 
| Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2606 | value = nice(increment); | 
| Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2607 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) | 
| Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2608 | if (value == 0) | 
|  | 2609 | value = getpriority(PRIO_PROCESS, 0); | 
|  | 2610 | #endif | 
|  | 2611 | if (value == -1 && errno != 0) | 
|  | 2612 | /* either nice() or getpriority() returned an error */ | 
| Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2613 | return posix_error(); | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2614 | return PyLong_FromLong((long) value); | 
| Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2615 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2616 | #endif /* HAVE_NICE */ | 
| Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2617 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2618 | PyDoc_STRVAR(posix_rename__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2619 | "rename(old, new)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2620 | Rename a file or directory."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2621 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2622 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2623 | posix_rename(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2624 | { | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2625 | #ifdef MS_WINDOWS | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2626 | PyObject *o1, *o2; | 
|  | 2627 | char *p1, *p2; | 
|  | 2628 | BOOL result; | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2629 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) | 
| Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2630 | goto error; | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2631 | if (!convert_to_unicode(&o1)) | 
| Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2632 | goto error; | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2633 | if (!convert_to_unicode(&o2)) { | 
| Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2634 | Py_DECREF(o1); | 
|  | 2635 | goto error; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2636 | } | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2637 | Py_BEGIN_ALLOW_THREADS | 
|  | 2638 | result = MoveFileW(PyUnicode_AsUnicode(o1), | 
|  | 2639 | PyUnicode_AsUnicode(o2)); | 
|  | 2640 | Py_END_ALLOW_THREADS | 
|  | 2641 | Py_DECREF(o1); | 
|  | 2642 | Py_DECREF(o2); | 
|  | 2643 | if (!result) | 
|  | 2644 | return win32_error("rename", NULL); | 
|  | 2645 | Py_INCREF(Py_None); | 
|  | 2646 | return Py_None; | 
|  | 2647 | error: | 
|  | 2648 | PyErr_Clear(); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2649 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) | 
|  | 2650 | return NULL; | 
|  | 2651 | Py_BEGIN_ALLOW_THREADS | 
|  | 2652 | result = MoveFileA(p1, p2); | 
|  | 2653 | Py_END_ALLOW_THREADS | 
|  | 2654 | if (!result) | 
|  | 2655 | return win32_error("rename", NULL); | 
|  | 2656 | Py_INCREF(Py_None); | 
|  | 2657 | return Py_None; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2658 | #else | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2659 | return posix_2str(args, "O&O&:rename", rename); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2660 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2661 | } | 
|  | 2662 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2663 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2664 | PyDoc_STRVAR(posix_rmdir__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2665 | "rmdir(path)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2666 | Remove a directory."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2667 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2668 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2669 | posix_rmdir(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2670 | { | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2671 | #ifdef MS_WINDOWS | 
| Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 2672 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2673 | #else | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2674 | return posix_1str(args, "O&:rmdir", rmdir); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2675 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2676 | } | 
|  | 2677 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2678 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2679 | PyDoc_STRVAR(posix_stat__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2680 | "stat(path) -> stat result\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2681 | Perform a stat system call on the given path."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2682 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2683 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2684 | posix_stat(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2685 | { | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2686 | #ifdef MS_WINDOWS | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2687 | return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2688 | #else | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2689 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2690 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2691 | } | 
|  | 2692 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2693 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2694 | #ifdef HAVE_SYSTEM | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2695 | PyDoc_STRVAR(posix_system__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2696 | "system(command) -> exit_status\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2697 | Execute the command (a string) in a subshell."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2698 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2699 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2700 | posix_system(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2701 | { | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2702 | long sts; | 
| Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2703 | #ifdef MS_WINDOWS | 
|  | 2704 | wchar_t *command; | 
|  | 2705 | if (!PyArg_ParseTuple(args, "u:system", &command)) | 
|  | 2706 | return NULL; | 
|  | 2707 | #else | 
|  | 2708 | char *command; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2709 | if (!PyArg_ParseTuple(args, "s:system", &command)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2710 | return NULL; | 
| Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2711 | #endif | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2712 | Py_BEGIN_ALLOW_THREADS | 
| Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2713 | #ifdef MS_WINDOWS | 
|  | 2714 | sts = _wsystem(command); | 
|  | 2715 | #else | 
| Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2716 | sts = system(command); | 
| Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2717 | #endif | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2718 | Py_END_ALLOW_THREADS | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2719 | return PyLong_FromLong(sts); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2720 | } | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2721 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2722 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2723 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2724 | PyDoc_STRVAR(posix_umask__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2725 | "umask(new_mask) -> old_mask\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2726 | Set the current numeric umask and return the previous umask."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2727 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2728 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2729 | posix_umask(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2730 | { | 
|  | 2731 | int i; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2732 | if (!PyArg_ParseTuple(args, "i:umask", &i)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2733 | return NULL; | 
| Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2734 | i = (int)umask(i); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2735 | if (i < 0) | 
|  | 2736 | return posix_error(); | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2737 | return PyLong_FromLong((long)i); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2738 | } | 
|  | 2739 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2740 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2741 | PyDoc_STRVAR(posix_unlink__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2742 | "unlink(path)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2743 | Remove a file (same as remove(path))."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2744 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2745 | PyDoc_STRVAR(posix_remove__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2746 | "remove(path)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2747 | Remove a file (same as unlink(path))."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2748 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2749 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2750 | posix_unlink(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2751 | { | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2752 | #ifdef MS_WINDOWS | 
| Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 2753 | return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2754 | #else | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2755 | return posix_1str(args, "O&:remove", unlink); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2756 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2757 | } | 
|  | 2758 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2759 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2760 | #ifdef HAVE_UNAME | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2761 | PyDoc_STRVAR(posix_uname__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2762 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2763 | Return a tuple identifying the current operating system."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2764 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2765 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2766 | posix_uname(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2767 | { | 
| Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2768 | struct utsname u; | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2769 | int res; | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2770 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2771 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2772 | res = uname(&u); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2773 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2774 | if (res < 0) | 
| Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2775 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2776 | return Py_BuildValue("(sssss)", | 
| Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2777 | u.sysname, | 
|  | 2778 | u.nodename, | 
|  | 2779 | u.release, | 
|  | 2780 | u.version, | 
|  | 2781 | u.machine); | 
| Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2782 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2783 | #endif /* HAVE_UNAME */ | 
| Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2784 |  | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2785 | static int | 
|  | 2786 | extract_time(PyObject *t, long* sec, long* usec) | 
|  | 2787 | { | 
|  | 2788 | long intval; | 
|  | 2789 | if (PyFloat_Check(t)) { | 
|  | 2790 | double tval = PyFloat_AsDouble(t); | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2791 | PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t); | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2792 | if (!intobj) | 
|  | 2793 | return -1; | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2794 | intval = PyLong_AsLong(intobj); | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2795 | Py_DECREF(intobj); | 
| Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2796 | if (intval == -1 && PyErr_Occurred()) | 
|  | 2797 | return -1; | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2798 | *sec = intval; | 
| Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2799 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2800 | if (*usec < 0) | 
|  | 2801 | /* If rounding gave us a negative number, | 
|  | 2802 | truncate.  */ | 
|  | 2803 | *usec = 0; | 
|  | 2804 | return 0; | 
|  | 2805 | } | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2806 | intval = PyLong_AsLong(t); | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2807 | if (intval == -1 && PyErr_Occurred()) | 
|  | 2808 | return -1; | 
|  | 2809 | *sec = intval; | 
|  | 2810 | *usec = 0; | 
| Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2811 | return 0; | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2812 | } | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2813 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2814 | PyDoc_STRVAR(posix_utime__doc__, | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2815 | "utime(path, (atime, mtime))\n\ | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2816 | utime(path, None)\n\n\ | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2817 | Set the access and modified time of the file to the given values.  If the\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2818 | second form is used, set the access and modified times to the current time."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2819 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2820 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2821 | posix_utime(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2822 | { | 
| Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2823 | #ifdef MS_WINDOWS | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2824 | PyObject *arg; | 
|  | 2825 | PyUnicodeObject *obwpath; | 
|  | 2826 | wchar_t *wpath = NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2827 | PyObject *oapath; | 
|  | 2828 | char *apath; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2829 | HANDLE hFile; | 
|  | 2830 | long atimesec, mtimesec, ausec, musec; | 
|  | 2831 | FILETIME atime, mtime; | 
|  | 2832 | PyObject *result = NULL; | 
|  | 2833 |  | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2834 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { | 
|  | 2835 | wpath = PyUnicode_AS_UNICODE(obwpath); | 
|  | 2836 | Py_BEGIN_ALLOW_THREADS | 
|  | 2837 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, | 
|  | 2838 | NULL, OPEN_EXISTING, | 
|  | 2839 | FILE_FLAG_BACKUP_SEMANTICS, NULL); | 
|  | 2840 | Py_END_ALLOW_THREADS | 
|  | 2841 | if (hFile == INVALID_HANDLE_VALUE) | 
|  | 2842 | return win32_error_unicode("utime", wpath); | 
|  | 2843 | } else | 
|  | 2844 | /* Drop the argument parsing error as narrow strings | 
|  | 2845 | are also valid. */ | 
|  | 2846 | PyErr_Clear(); | 
|  | 2847 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2848 | if (!wpath) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2849 | if (!PyArg_ParseTuple(args, "O&O:utime", | 
|  | 2850 | PyUnicode_FSConverter, &oapath, &arg)) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2851 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2852 | apath = bytes2str(oapath, 1); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2853 | Py_BEGIN_ALLOW_THREADS | 
|  | 2854 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2855 | NULL, OPEN_EXISTING, | 
|  | 2856 | FILE_FLAG_BACKUP_SEMANTICS, NULL); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2857 | Py_END_ALLOW_THREADS | 
|  | 2858 | if (hFile == INVALID_HANDLE_VALUE) { | 
|  | 2859 | win32_error("utime", apath); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2860 | release_bytes(oapath); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2861 | return NULL; | 
|  | 2862 | } | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2863 | release_bytes(oapath); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2864 | } | 
|  | 2865 |  | 
|  | 2866 | if (arg == Py_None) { | 
|  | 2867 | SYSTEMTIME now; | 
|  | 2868 | GetSystemTime(&now); | 
|  | 2869 | if (!SystemTimeToFileTime(&now, &mtime) || | 
|  | 2870 | !SystemTimeToFileTime(&now, &atime)) { | 
|  | 2871 | win32_error("utime", NULL); | 
|  | 2872 | goto done; | 
|  | 2873 | } | 
|  | 2874 | } | 
|  | 2875 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { | 
|  | 2876 | PyErr_SetString(PyExc_TypeError, | 
|  | 2877 | "utime() arg 2 must be a tuple (atime, mtime)"); | 
|  | 2878 | goto done; | 
|  | 2879 | } | 
|  | 2880 | else { | 
|  | 2881 | if (extract_time(PyTuple_GET_ITEM(arg, 0), | 
|  | 2882 | &atimesec, &ausec) == -1) | 
|  | 2883 | goto done; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2884 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2885 | if (extract_time(PyTuple_GET_ITEM(arg, 1), | 
|  | 2886 | &mtimesec, &musec) == -1) | 
|  | 2887 | goto done; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2888 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2889 | } | 
|  | 2890 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { | 
|  | 2891 | /* Avoid putting the file name into the error here, | 
|  | 2892 | as that may confuse the user into believing that | 
|  | 2893 | something is wrong with the file, when it also | 
|  | 2894 | could be the time stamp that gives a problem. */ | 
|  | 2895 | win32_error("utime", NULL); | 
|  | 2896 | } | 
|  | 2897 | Py_INCREF(Py_None); | 
|  | 2898 | result = Py_None; | 
|  | 2899 | done: | 
|  | 2900 | CloseHandle(hFile); | 
|  | 2901 | return result; | 
| Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2902 | #else /* MS_WINDOWS */ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2903 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2904 | PyObject *opath; | 
|  | 2905 | char *path; | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2906 | long atime, mtime, ausec, musec; | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2907 | int res; | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2908 | PyObject* arg; | 
| Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2909 |  | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2910 | #if defined(HAVE_UTIMES) | 
|  | 2911 | struct timeval buf[2]; | 
|  | 2912 | #define ATIME buf[0].tv_sec | 
|  | 2913 | #define MTIME buf[1].tv_sec | 
|  | 2914 | #elif defined(HAVE_UTIME_H) | 
| Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2915 | /* XXX should define struct utimbuf instead, above */ | 
| Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2916 | struct utimbuf buf; | 
|  | 2917 | #define ATIME buf.actime | 
|  | 2918 | #define MTIME buf.modtime | 
|  | 2919 | #define UTIME_ARG &buf | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2920 | #else /* HAVE_UTIMES */ | 
| Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2921 | time_t buf[2]; | 
|  | 2922 | #define ATIME buf[0] | 
|  | 2923 | #define MTIME buf[1] | 
|  | 2924 | #define UTIME_ARG buf | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2925 | #endif /* HAVE_UTIMES */ | 
| Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2926 |  | 
| Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2927 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2928 | if (!PyArg_ParseTuple(args, "O&O:utime", | 
|  | 2929 | PyUnicode_FSConverter, &opath, &arg)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2930 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2931 | path = bytes2str(opath, 1); | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2932 | if (arg == Py_None) { | 
|  | 2933 | /* optional time values not given */ | 
|  | 2934 | Py_BEGIN_ALLOW_THREADS | 
|  | 2935 | res = utime(path, NULL); | 
|  | 2936 | Py_END_ALLOW_THREADS | 
|  | 2937 | } | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2938 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2939 | PyErr_SetString(PyExc_TypeError, | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2940 | "utime() arg 2 must be a tuple (atime, mtime)"); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2941 | release_bytes(opath); | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2942 | return NULL; | 
|  | 2943 | } | 
|  | 2944 | else { | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2945 | if (extract_time(PyTuple_GET_ITEM(arg, 0), | 
| Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2946 | &atime, &ausec) == -1) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2947 | release_bytes(opath); | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2948 | return NULL; | 
| Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2949 | } | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2950 | if (extract_time(PyTuple_GET_ITEM(arg, 1), | 
| Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2951 | &mtime, &musec) == -1) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2952 | release_bytes(opath); | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2953 | return NULL; | 
| Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2954 | } | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2955 | ATIME = atime; | 
|  | 2956 | MTIME = mtime; | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2957 | #ifdef HAVE_UTIMES | 
|  | 2958 | buf[0].tv_usec = ausec; | 
|  | 2959 | buf[1].tv_usec = musec; | 
|  | 2960 | Py_BEGIN_ALLOW_THREADS | 
|  | 2961 | res = utimes(path, buf); | 
|  | 2962 | Py_END_ALLOW_THREADS | 
|  | 2963 | #else | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2964 | Py_BEGIN_ALLOW_THREADS | 
|  | 2965 | res = utime(path, UTIME_ARG); | 
|  | 2966 | Py_END_ALLOW_THREADS | 
| Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2967 | #endif /* HAVE_UTIMES */ | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2968 | } | 
| Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2969 | if (res < 0) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2970 | return posix_error_with_allocated_filename(opath); | 
| Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2971 | } | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2972 | release_bytes(opath); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2973 | Py_INCREF(Py_None); | 
|  | 2974 | return Py_None; | 
| Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2975 | #undef UTIME_ARG | 
|  | 2976 | #undef ATIME | 
|  | 2977 | #undef MTIME | 
| Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2978 | #endif /* MS_WINDOWS */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2979 | } | 
|  | 2980 |  | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2981 |  | 
| Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2982 | /* Process operations */ | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2983 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2984 | PyDoc_STRVAR(posix__exit__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2985 | "_exit(status)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2986 | Exit to the system with specified status, without normal exit processing."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2987 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2988 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2989 | posix__exit(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2990 | { | 
|  | 2991 | int sts; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2992 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2993 | return NULL; | 
|  | 2994 | _exit(sts); | 
| Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2995 | return NULL; /* Make gcc -Wall happy */ | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2996 | } | 
|  | 2997 |  | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2998 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) | 
|  | 2999 | static void | 
| Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3000 | free_string_array(char **array, Py_ssize_t count) | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3001 | { | 
| Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3002 | Py_ssize_t i; | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3003 | for (i = 0; i < count; i++) | 
|  | 3004 | PyMem_Free(array[i]); | 
|  | 3005 | PyMem_DEL(array); | 
|  | 3006 | } | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3007 |  | 
| Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 3008 | static | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3009 | int fsconvert_strdup(PyObject *o, char**out) | 
|  | 3010 | { | 
|  | 3011 | PyObject *bytes; | 
|  | 3012 | Py_ssize_t size; | 
|  | 3013 | if (!PyUnicode_FSConverter(o, &bytes)) | 
|  | 3014 | return 0; | 
|  | 3015 | size = PyObject_Size(bytes); | 
|  | 3016 | *out = PyMem_Malloc(size+1); | 
|  | 3017 | if (!*out) | 
|  | 3018 | return 0; | 
|  | 3019 | /* Don't lock bytes, as we hold the GIL */ | 
|  | 3020 | memcpy(*out, bytes2str(bytes, 0), size+1); | 
|  | 3021 | Py_DECREF(bytes); | 
|  | 3022 | return 1; | 
|  | 3023 | } | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3024 | #endif | 
|  | 3025 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3026 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3027 | #ifdef HAVE_EXECV | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3028 | PyDoc_STRVAR(posix_execv__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3029 | "execv(path, args)\n\n\ | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3030 | Execute an executable path with arguments, replacing current process.\n\ | 
|  | 3031 | \n\ | 
|  | 3032 | path: path of executable file\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3033 | args: tuple or list of strings"); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3034 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3035 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3036 | posix_execv(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3037 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3038 | PyObject *opath; | 
| Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3039 | char *path; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3040 | PyObject *argv; | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3041 | char **argvlist; | 
| Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3042 | Py_ssize_t i, argc; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3043 | PyObject *(*getitem)(PyObject *, Py_ssize_t); | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3044 |  | 
| Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 3045 | /* execv has two arguments: (path, argv), where | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3046 | argv is a list or tuple of strings. */ | 
|  | 3047 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3048 | if (!PyArg_ParseTuple(args, "O&O:execv", | 
|  | 3049 | PyUnicode_FSConverter, | 
|  | 3050 | &opath, &argv)) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3051 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3052 | path = bytes2str(opath, 1); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3053 | if (PyList_Check(argv)) { | 
|  | 3054 | argc = PyList_Size(argv); | 
|  | 3055 | getitem = PyList_GetItem; | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3056 | } | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3057 | else if (PyTuple_Check(argv)) { | 
|  | 3058 | argc = PyTuple_Size(argv); | 
|  | 3059 | getitem = PyTuple_GetItem; | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3060 | } | 
| Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3061 | else { | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3062 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3063 | release_bytes(opath); | 
| Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3064 | return NULL; | 
|  | 3065 | } | 
| Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 3066 | if (argc < 1) { | 
|  | 3067 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3068 | release_bytes(opath); | 
| Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 3069 | return NULL; | 
|  | 3070 | } | 
| Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3071 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3072 | argvlist = PyMem_NEW(char *, argc+1); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3073 | if (argvlist == NULL) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3074 | release_bytes(opath); | 
| Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3075 | return PyErr_NoMemory(); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3076 | } | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3077 | for (i = 0; i < argc; i++) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3078 | if (!fsconvert_strdup((*getitem)(argv, i), | 
|  | 3079 | &argvlist[i])) { | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3080 | free_string_array(argvlist, i); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3081 | PyErr_SetString(PyExc_TypeError, | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3082 | "execv() arg 2 must contain only strings"); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3083 | release_bytes(opath); | 
| Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3084 | return NULL; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3085 |  | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3086 | } | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3087 | } | 
|  | 3088 | argvlist[argc] = NULL; | 
|  | 3089 |  | 
| Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3090 | execv(path, argvlist); | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3091 |  | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3092 | /* If we get here it's definitely an error */ | 
|  | 3093 |  | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3094 | free_string_array(argvlist, argc); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3095 | release_bytes(opath); | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3096 | return posix_error(); | 
|  | 3097 | } | 
|  | 3098 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3099 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3100 | PyDoc_STRVAR(posix_execve__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3101 | "execve(path, args, env)\n\n\ | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3102 | Execute a path with arguments and environment, replacing current process.\n\ | 
|  | 3103 | \n\ | 
|  | 3104 | path: path of executable file\n\ | 
|  | 3105 | args: tuple or list of arguments\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3106 | env: dictionary of strings mapping to strings"); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3107 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3108 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3109 | posix_execve(PyObject *self, PyObject *args) | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3110 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3111 | PyObject *opath; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3112 | char *path; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3113 | PyObject *argv, *env; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3114 | char **argvlist; | 
|  | 3115 | char **envlist; | 
| Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3116 | PyObject *key, *val, *keys=NULL, *vals=NULL; | 
| Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3117 | Py_ssize_t i, pos, argc, envc; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3118 | PyObject *(*getitem)(PyObject *, Py_ssize_t); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3119 | Py_ssize_t lastarg = 0; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3120 |  | 
|  | 3121 | /* execve has three arguments: (path, argv, env), where | 
|  | 3122 | argv is a list or tuple of strings and env is a dictionary | 
|  | 3123 | like posix.environ. */ | 
|  | 3124 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3125 | if (!PyArg_ParseTuple(args, "O&OO:execve", | 
|  | 3126 | PyUnicode_FSConverter, | 
|  | 3127 | &opath, &argv, &env)) | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3128 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3129 | path = bytes2str(opath, 1); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3130 | if (PyList_Check(argv)) { | 
|  | 3131 | argc = PyList_Size(argv); | 
|  | 3132 | getitem = PyList_GetItem; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3133 | } | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3134 | else if (PyTuple_Check(argv)) { | 
|  | 3135 | argc = PyTuple_Size(argv); | 
|  | 3136 | getitem = PyTuple_GetItem; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3137 | } | 
|  | 3138 | else { | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3139 | PyErr_SetString(PyExc_TypeError, | 
|  | 3140 | "execve() arg 2 must be a tuple or list"); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3141 | goto fail_0; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3142 | } | 
| Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3143 | if (!PyMapping_Check(env)) { | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3144 | PyErr_SetString(PyExc_TypeError, | 
|  | 3145 | "execve() arg 3 must be a mapping object"); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3146 | goto fail_0; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3147 | } | 
|  | 3148 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3149 | argvlist = PyMem_NEW(char *, argc+1); | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3150 | if (argvlist == NULL) { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3151 | PyErr_NoMemory(); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3152 | goto fail_0; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3153 | } | 
|  | 3154 | for (i = 0; i < argc; i++) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3155 | if (!fsconvert_strdup((*getitem)(argv, i), | 
|  | 3156 | &argvlist[i])) | 
| Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3157 | { | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3158 | lastarg = i; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3159 | goto fail_1; | 
|  | 3160 | } | 
|  | 3161 | } | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3162 | lastarg = argc; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3163 | argvlist[argc] = NULL; | 
|  | 3164 |  | 
| Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3165 | i = PyMapping_Size(env); | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3166 | if (i < 0) | 
|  | 3167 | goto fail_1; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3168 | envlist = PyMem_NEW(char *, i + 1); | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3169 | if (envlist == NULL) { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3170 | PyErr_NoMemory(); | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3171 | goto fail_1; | 
|  | 3172 | } | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3173 | envc = 0; | 
| Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3174 | keys = PyMapping_Keys(env); | 
|  | 3175 | vals = PyMapping_Values(env); | 
|  | 3176 | if (!keys || !vals) | 
|  | 3177 | goto fail_2; | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3178 | if (!PyList_Check(keys) || !PyList_Check(vals)) { | 
|  | 3179 | PyErr_SetString(PyExc_TypeError, | 
|  | 3180 | "execve(): env.keys() or env.values() is not a list"); | 
|  | 3181 | goto fail_2; | 
|  | 3182 | } | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3183 |  | 
| Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3184 | for (pos = 0; pos < i; pos++) { | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3185 | char *p, *k, *v; | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3186 | size_t len; | 
| Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3187 |  | 
|  | 3188 | key = PyList_GetItem(keys, pos); | 
|  | 3189 | val = PyList_GetItem(vals, pos); | 
|  | 3190 | if (!key || !val) | 
|  | 3191 | goto fail_2; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3192 |  | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3193 | if (!PyArg_Parse( | 
|  | 3194 | key, | 
|  | 3195 | "s;execve() arg 3 contains a non-string key", | 
|  | 3196 | &k) || | 
|  | 3197 | !PyArg_Parse( | 
|  | 3198 | val, | 
|  | 3199 | "s;execve() arg 3 contains a non-string value", | 
|  | 3200 | &v)) | 
| Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3201 | { | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3202 | goto fail_2; | 
|  | 3203 | } | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3204 |  | 
|  | 3205 | #if defined(PYOS_OS2) | 
|  | 3206 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ | 
|  | 3207 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { | 
|  | 3208 | #endif | 
| Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3209 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3210 | p = PyMem_NEW(char, len); | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3211 | if (p == NULL) { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3212 | PyErr_NoMemory(); | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3213 | goto fail_2; | 
|  | 3214 | } | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3215 | PyOS_snprintf(p, len, "%s=%s", k, v); | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3216 | envlist[envc++] = p; | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3217 | #if defined(PYOS_OS2) | 
|  | 3218 | } | 
|  | 3219 | #endif | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3220 | } | 
|  | 3221 | envlist[envc] = 0; | 
|  | 3222 |  | 
|  | 3223 | execve(path, argvlist, envlist); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3224 |  | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3225 | /* If we get here it's definitely an error */ | 
|  | 3226 |  | 
|  | 3227 | (void) posix_error(); | 
|  | 3228 |  | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3229 | fail_2: | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3230 | while (--envc >= 0) | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3231 | PyMem_DEL(envlist[envc]); | 
|  | 3232 | PyMem_DEL(envlist); | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3233 | fail_1: | 
|  | 3234 | free_string_array(argvlist, lastarg); | 
| Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3235 | Py_XDECREF(vals); | 
|  | 3236 | Py_XDECREF(keys); | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3237 | fail_0: | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3238 | release_bytes(opath); | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3239 | return NULL; | 
|  | 3240 | } | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3241 | #endif /* HAVE_EXECV */ | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3242 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3243 |  | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3244 | #ifdef HAVE_SPAWNV | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3245 | PyDoc_STRVAR(posix_spawnv__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3246 | "spawnv(mode, path, args)\n\n\ | 
| Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3247 | Execute the program 'path' in a new process.\n\ | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3248 | \n\ | 
| Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3249 | mode: mode of process creation\n\ | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3250 | path: path of executable file\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3251 | args: tuple or list of strings"); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3252 |  | 
|  | 3253 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3254 | posix_spawnv(PyObject *self, PyObject *args) | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3255 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3256 | PyObject *opath; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3257 | char *path; | 
|  | 3258 | PyObject *argv; | 
|  | 3259 | char **argvlist; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3260 | int mode, i; | 
|  | 3261 | Py_ssize_t argc; | 
| Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3262 | Py_intptr_t spawnval; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3263 | PyObject *(*getitem)(PyObject *, Py_ssize_t); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3264 |  | 
|  | 3265 | /* spawnv has three arguments: (mode, path, argv), where | 
|  | 3266 | argv is a list or tuple of strings. */ | 
|  | 3267 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3268 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, | 
|  | 3269 | PyUnicode_FSConverter, | 
|  | 3270 | &opath, &argv)) | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3271 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3272 | path = bytes2str(opath, 1); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3273 | if (PyList_Check(argv)) { | 
|  | 3274 | argc = PyList_Size(argv); | 
|  | 3275 | getitem = PyList_GetItem; | 
|  | 3276 | } | 
|  | 3277 | else if (PyTuple_Check(argv)) { | 
|  | 3278 | argc = PyTuple_Size(argv); | 
|  | 3279 | getitem = PyTuple_GetItem; | 
|  | 3280 | } | 
|  | 3281 | else { | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3282 | PyErr_SetString(PyExc_TypeError, | 
|  | 3283 | "spawnv() arg 2 must be a tuple or list"); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3284 | release_bytes(opath); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3285 | return NULL; | 
|  | 3286 | } | 
|  | 3287 |  | 
|  | 3288 | argvlist = PyMem_NEW(char *, argc+1); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3289 | if (argvlist == NULL) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3290 | release_bytes(opath); | 
| Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3291 | return PyErr_NoMemory(); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3292 | } | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3293 | for (i = 0; i < argc; i++) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3294 | if (!fsconvert_strdup((*getitem)(argv, i), | 
|  | 3295 | &argvlist[i])) { | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3296 | free_string_array(argvlist, i); | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3297 | PyErr_SetString( | 
|  | 3298 | PyExc_TypeError, | 
|  | 3299 | "spawnv() arg 2 must contain only strings"); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3300 | release_bytes(opath); | 
| Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 3301 | return NULL; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3302 | } | 
|  | 3303 | } | 
|  | 3304 | argvlist[argc] = NULL; | 
|  | 3305 |  | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3306 | #if defined(PYOS_OS2) && defined(PYCC_GCC) | 
|  | 3307 | Py_BEGIN_ALLOW_THREADS | 
|  | 3308 | spawnval = spawnv(mode, path, argvlist); | 
|  | 3309 | Py_END_ALLOW_THREADS | 
|  | 3310 | #else | 
| Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3311 | if (mode == _OLD_P_OVERLAY) | 
|  | 3312 | mode = _P_OVERLAY; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3313 |  | 
| Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3314 | Py_BEGIN_ALLOW_THREADS | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3315 | spawnval = _spawnv(mode, path, argvlist); | 
| Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3316 | Py_END_ALLOW_THREADS | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3317 | #endif | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3318 |  | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3319 | free_string_array(argvlist, argc); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3320 | release_bytes(opath); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3321 |  | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3322 | if (spawnval == -1) | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3323 | return posix_error(); | 
|  | 3324 | else | 
| Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3325 | #if SIZEOF_LONG == SIZEOF_VOID_P | 
|  | 3326 | return Py_BuildValue("l", (long) spawnval); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3327 | #else | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3328 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3329 | #endif | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3330 | } | 
|  | 3331 |  | 
|  | 3332 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3333 | PyDoc_STRVAR(posix_spawnve__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3334 | "spawnve(mode, path, args, env)\n\n\ | 
| Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3335 | Execute the program 'path' in a new process.\n\ | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3336 | \n\ | 
| Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3337 | mode: mode of process creation\n\ | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3338 | path: path of executable file\n\ | 
|  | 3339 | args: tuple or list of arguments\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3340 | env: dictionary of strings mapping to strings"); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3341 |  | 
|  | 3342 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3343 | posix_spawnve(PyObject *self, PyObject *args) | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3344 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3345 | PyObject *opath; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3346 | char *path; | 
|  | 3347 | PyObject *argv, *env; | 
|  | 3348 | char **argvlist; | 
|  | 3349 | char **envlist; | 
|  | 3350 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3351 | int mode, pos, envc; | 
|  | 3352 | Py_ssize_t argc, i; | 
| Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3353 | Py_intptr_t spawnval; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3354 | PyObject *(*getitem)(PyObject *, Py_ssize_t); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3355 | Py_ssize_t lastarg = 0; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3356 |  | 
|  | 3357 | /* spawnve has four arguments: (mode, path, argv, env), where | 
|  | 3358 | argv is a list or tuple of strings and env is a dictionary | 
|  | 3359 | like posix.environ. */ | 
|  | 3360 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3361 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, | 
|  | 3362 | PyUnicode_FSConverter, | 
|  | 3363 | &opath, &argv, &env)) | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3364 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3365 | path = bytes2str(opath, 1); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3366 | if (PyList_Check(argv)) { | 
|  | 3367 | argc = PyList_Size(argv); | 
|  | 3368 | getitem = PyList_GetItem; | 
|  | 3369 | } | 
|  | 3370 | else if (PyTuple_Check(argv)) { | 
|  | 3371 | argc = PyTuple_Size(argv); | 
|  | 3372 | getitem = PyTuple_GetItem; | 
|  | 3373 | } | 
|  | 3374 | else { | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3375 | PyErr_SetString(PyExc_TypeError, | 
|  | 3376 | "spawnve() arg 2 must be a tuple or list"); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3377 | goto fail_0; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3378 | } | 
|  | 3379 | if (!PyMapping_Check(env)) { | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3380 | PyErr_SetString(PyExc_TypeError, | 
|  | 3381 | "spawnve() arg 3 must be a mapping object"); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3382 | goto fail_0; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3383 | } | 
|  | 3384 |  | 
|  | 3385 | argvlist = PyMem_NEW(char *, argc+1); | 
|  | 3386 | if (argvlist == NULL) { | 
|  | 3387 | PyErr_NoMemory(); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3388 | goto fail_0; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3389 | } | 
|  | 3390 | for (i = 0; i < argc; i++) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3391 | if (!fsconvert_strdup((*getitem)(argv, i), | 
|  | 3392 | &argvlist[i])) | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3393 | { | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3394 | lastarg = i; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3395 | goto fail_1; | 
|  | 3396 | } | 
|  | 3397 | } | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3398 | lastarg = argc; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3399 | argvlist[argc] = NULL; | 
|  | 3400 |  | 
| Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3401 | i = PyMapping_Size(env); | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3402 | if (i < 0) | 
|  | 3403 | goto fail_1; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3404 | envlist = PyMem_NEW(char *, i + 1); | 
|  | 3405 | if (envlist == NULL) { | 
|  | 3406 | PyErr_NoMemory(); | 
|  | 3407 | goto fail_1; | 
|  | 3408 | } | 
|  | 3409 | envc = 0; | 
|  | 3410 | keys = PyMapping_Keys(env); | 
|  | 3411 | vals = PyMapping_Values(env); | 
|  | 3412 | if (!keys || !vals) | 
|  | 3413 | goto fail_2; | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3414 | if (!PyList_Check(keys) || !PyList_Check(vals)) { | 
|  | 3415 | PyErr_SetString(PyExc_TypeError, | 
|  | 3416 | "spawnve(): env.keys() or env.values() is not a list"); | 
|  | 3417 | goto fail_2; | 
|  | 3418 | } | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3419 |  | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3420 | for (pos = 0; pos < i; pos++) { | 
|  | 3421 | char *p, *k, *v; | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3422 | size_t len; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3423 |  | 
|  | 3424 | key = PyList_GetItem(keys, pos); | 
|  | 3425 | val = PyList_GetItem(vals, pos); | 
|  | 3426 | if (!key || !val) | 
|  | 3427 | goto fail_2; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3428 |  | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3429 | if (!PyArg_Parse( | 
|  | 3430 | key, | 
|  | 3431 | "s;spawnve() arg 3 contains a non-string key", | 
|  | 3432 | &k) || | 
|  | 3433 | !PyArg_Parse( | 
|  | 3434 | val, | 
|  | 3435 | "s;spawnve() arg 3 contains a non-string value", | 
|  | 3436 | &v)) | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3437 | { | 
|  | 3438 | goto fail_2; | 
|  | 3439 | } | 
| Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3440 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3441 | p = PyMem_NEW(char, len); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3442 | if (p == NULL) { | 
|  | 3443 | PyErr_NoMemory(); | 
|  | 3444 | goto fail_2; | 
|  | 3445 | } | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3446 | PyOS_snprintf(p, len, "%s=%s", k, v); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3447 | envlist[envc++] = p; | 
|  | 3448 | } | 
|  | 3449 | envlist[envc] = 0; | 
|  | 3450 |  | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3451 | #if defined(PYOS_OS2) && defined(PYCC_GCC) | 
|  | 3452 | Py_BEGIN_ALLOW_THREADS | 
|  | 3453 | spawnval = spawnve(mode, path, argvlist, envlist); | 
|  | 3454 | Py_END_ALLOW_THREADS | 
|  | 3455 | #else | 
| Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3456 | if (mode == _OLD_P_OVERLAY) | 
|  | 3457 | mode = _P_OVERLAY; | 
| Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3458 |  | 
|  | 3459 | Py_BEGIN_ALLOW_THREADS | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3460 | spawnval = _spawnve(mode, path, argvlist, envlist); | 
| Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3461 | Py_END_ALLOW_THREADS | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3462 | #endif | 
| Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3463 |  | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3464 | if (spawnval == -1) | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3465 | (void) posix_error(); | 
|  | 3466 | else | 
| Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3467 | #if SIZEOF_LONG == SIZEOF_VOID_P | 
|  | 3468 | res = Py_BuildValue("l", (long) spawnval); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3469 | #else | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3470 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3471 | #endif | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3472 |  | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3473 | fail_2: | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3474 | while (--envc >= 0) | 
|  | 3475 | PyMem_DEL(envlist[envc]); | 
|  | 3476 | PyMem_DEL(envlist); | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3477 | fail_1: | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3478 | free_string_array(argvlist, lastarg); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3479 | Py_XDECREF(vals); | 
|  | 3480 | Py_XDECREF(keys); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3481 | fail_0: | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3482 | release_bytes(opath); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3483 | return res; | 
|  | 3484 | } | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3485 |  | 
|  | 3486 | /* OS/2 supports spawnvp & spawnvpe natively */ | 
|  | 3487 | #if defined(PYOS_OS2) | 
|  | 3488 | PyDoc_STRVAR(posix_spawnvp__doc__, | 
|  | 3489 | "spawnvp(mode, file, args)\n\n\ | 
|  | 3490 | Execute the program 'file' in a new process, using the environment\n\ | 
|  | 3491 | search path to find the file.\n\ | 
|  | 3492 | \n\ | 
|  | 3493 | mode: mode of process creation\n\ | 
|  | 3494 | file: executable file name\n\ | 
|  | 3495 | args: tuple or list of strings"); | 
|  | 3496 |  | 
|  | 3497 | static PyObject * | 
|  | 3498 | posix_spawnvp(PyObject *self, PyObject *args) | 
|  | 3499 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3500 | PyObject *opath; | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3501 | char *path; | 
|  | 3502 | PyObject *argv; | 
|  | 3503 | char **argvlist; | 
|  | 3504 | int mode, i, argc; | 
|  | 3505 | Py_intptr_t spawnval; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3506 | PyObject *(*getitem)(PyObject *, Py_ssize_t); | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3507 |  | 
|  | 3508 | /* spawnvp has three arguments: (mode, path, argv), where | 
|  | 3509 | argv is a list or tuple of strings. */ | 
|  | 3510 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3511 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, | 
|  | 3512 | PyUnicode_FSConverter, | 
|  | 3513 | &opath, &argv)) | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3514 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3515 | path = bytes2str(opath); | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3516 | if (PyList_Check(argv)) { | 
|  | 3517 | argc = PyList_Size(argv); | 
|  | 3518 | getitem = PyList_GetItem; | 
|  | 3519 | } | 
|  | 3520 | else if (PyTuple_Check(argv)) { | 
|  | 3521 | argc = PyTuple_Size(argv); | 
|  | 3522 | getitem = PyTuple_GetItem; | 
|  | 3523 | } | 
|  | 3524 | else { | 
|  | 3525 | PyErr_SetString(PyExc_TypeError, | 
|  | 3526 | "spawnvp() arg 2 must be a tuple or list"); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3527 | release_bytes(opath); | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3528 | return NULL; | 
|  | 3529 | } | 
|  | 3530 |  | 
|  | 3531 | argvlist = PyMem_NEW(char *, argc+1); | 
|  | 3532 | if (argvlist == NULL) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3533 | release_bytes(opath); | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3534 | return PyErr_NoMemory(); | 
|  | 3535 | } | 
|  | 3536 | for (i = 0; i < argc; i++) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3537 | if (!fsconvert_strdup((*getitem)(argv, i), | 
|  | 3538 | &argvlist[i])) { | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3539 | free_string_array(argvlist, i); | 
|  | 3540 | PyErr_SetString( | 
|  | 3541 | PyExc_TypeError, | 
|  | 3542 | "spawnvp() arg 2 must contain only strings"); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3543 | release_bytes(opath); | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3544 | return NULL; | 
|  | 3545 | } | 
|  | 3546 | } | 
|  | 3547 | argvlist[argc] = NULL; | 
|  | 3548 |  | 
|  | 3549 | Py_BEGIN_ALLOW_THREADS | 
|  | 3550 | #if defined(PYCC_GCC) | 
|  | 3551 | spawnval = spawnvp(mode, path, argvlist); | 
|  | 3552 | #else | 
|  | 3553 | spawnval = _spawnvp(mode, path, argvlist); | 
|  | 3554 | #endif | 
|  | 3555 | Py_END_ALLOW_THREADS | 
|  | 3556 |  | 
|  | 3557 | free_string_array(argvlist, argc); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3558 | release_bytes(opath); | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3559 |  | 
|  | 3560 | if (spawnval == -1) | 
|  | 3561 | return posix_error(); | 
|  | 3562 | else | 
|  | 3563 | return Py_BuildValue("l", (long) spawnval); | 
|  | 3564 | } | 
|  | 3565 |  | 
|  | 3566 |  | 
|  | 3567 | PyDoc_STRVAR(posix_spawnvpe__doc__, | 
|  | 3568 | "spawnvpe(mode, file, args, env)\n\n\ | 
|  | 3569 | Execute the program 'file' in a new process, using the environment\n\ | 
|  | 3570 | search path to find the file.\n\ | 
|  | 3571 | \n\ | 
|  | 3572 | mode: mode of process creation\n\ | 
|  | 3573 | file: executable file name\n\ | 
|  | 3574 | args: tuple or list of arguments\n\ | 
|  | 3575 | env: dictionary of strings mapping to strings"); | 
|  | 3576 |  | 
|  | 3577 | static PyObject * | 
|  | 3578 | posix_spawnvpe(PyObject *self, PyObject *args) | 
|  | 3579 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3580 | PyObject *opath | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3581 | char *path; | 
|  | 3582 | PyObject *argv, *env; | 
|  | 3583 | char **argvlist; | 
|  | 3584 | char **envlist; | 
|  | 3585 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; | 
|  | 3586 | int mode, i, pos, argc, envc; | 
|  | 3587 | Py_intptr_t spawnval; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3588 | PyObject *(*getitem)(PyObject *, Py_ssize_t); | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3589 | int lastarg = 0; | 
|  | 3590 |  | 
|  | 3591 | /* spawnvpe has four arguments: (mode, path, argv, env), where | 
|  | 3592 | argv is a list or tuple of strings and env is a dictionary | 
|  | 3593 | like posix.environ. */ | 
|  | 3594 |  | 
|  | 3595 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3596 | PyUnicode_FSConverter, | 
|  | 3597 | &opath, &argv, &env)) | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3598 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3599 | path = bytes2str(opath); | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3600 | if (PyList_Check(argv)) { | 
|  | 3601 | argc = PyList_Size(argv); | 
|  | 3602 | getitem = PyList_GetItem; | 
|  | 3603 | } | 
|  | 3604 | else if (PyTuple_Check(argv)) { | 
|  | 3605 | argc = PyTuple_Size(argv); | 
|  | 3606 | getitem = PyTuple_GetItem; | 
|  | 3607 | } | 
|  | 3608 | else { | 
|  | 3609 | PyErr_SetString(PyExc_TypeError, | 
|  | 3610 | "spawnvpe() arg 2 must be a tuple or list"); | 
|  | 3611 | goto fail_0; | 
|  | 3612 | } | 
|  | 3613 | if (!PyMapping_Check(env)) { | 
|  | 3614 | PyErr_SetString(PyExc_TypeError, | 
|  | 3615 | "spawnvpe() arg 3 must be a mapping object"); | 
|  | 3616 | goto fail_0; | 
|  | 3617 | } | 
|  | 3618 |  | 
|  | 3619 | argvlist = PyMem_NEW(char *, argc+1); | 
|  | 3620 | if (argvlist == NULL) { | 
|  | 3621 | PyErr_NoMemory(); | 
|  | 3622 | goto fail_0; | 
|  | 3623 | } | 
|  | 3624 | for (i = 0; i < argc; i++) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3625 | if (!fsconvert_strdup((*getitem)(argv, i), | 
|  | 3626 | &argvlist[i])) | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3627 | { | 
|  | 3628 | lastarg = i; | 
|  | 3629 | goto fail_1; | 
|  | 3630 | } | 
|  | 3631 | } | 
|  | 3632 | lastarg = argc; | 
|  | 3633 | argvlist[argc] = NULL; | 
|  | 3634 |  | 
|  | 3635 | i = PyMapping_Size(env); | 
|  | 3636 | if (i < 0) | 
|  | 3637 | goto fail_1; | 
|  | 3638 | envlist = PyMem_NEW(char *, i + 1); | 
|  | 3639 | if (envlist == NULL) { | 
|  | 3640 | PyErr_NoMemory(); | 
|  | 3641 | goto fail_1; | 
|  | 3642 | } | 
|  | 3643 | envc = 0; | 
|  | 3644 | keys = PyMapping_Keys(env); | 
|  | 3645 | vals = PyMapping_Values(env); | 
|  | 3646 | if (!keys || !vals) | 
|  | 3647 | goto fail_2; | 
|  | 3648 | if (!PyList_Check(keys) || !PyList_Check(vals)) { | 
|  | 3649 | PyErr_SetString(PyExc_TypeError, | 
|  | 3650 | "spawnvpe(): env.keys() or env.values() is not a list"); | 
|  | 3651 | goto fail_2; | 
|  | 3652 | } | 
|  | 3653 |  | 
|  | 3654 | for (pos = 0; pos < i; pos++) { | 
|  | 3655 | char *p, *k, *v; | 
|  | 3656 | size_t len; | 
|  | 3657 |  | 
|  | 3658 | key = PyList_GetItem(keys, pos); | 
|  | 3659 | val = PyList_GetItem(vals, pos); | 
|  | 3660 | if (!key || !val) | 
|  | 3661 | goto fail_2; | 
|  | 3662 |  | 
|  | 3663 | if (!PyArg_Parse( | 
|  | 3664 | key, | 
|  | 3665 | "s;spawnvpe() arg 3 contains a non-string key", | 
|  | 3666 | &k) || | 
|  | 3667 | !PyArg_Parse( | 
|  | 3668 | val, | 
|  | 3669 | "s;spawnvpe() arg 3 contains a non-string value", | 
|  | 3670 | &v)) | 
|  | 3671 | { | 
|  | 3672 | goto fail_2; | 
|  | 3673 | } | 
| Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3674 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3675 | p = PyMem_NEW(char, len); | 
|  | 3676 | if (p == NULL) { | 
|  | 3677 | PyErr_NoMemory(); | 
|  | 3678 | goto fail_2; | 
|  | 3679 | } | 
|  | 3680 | PyOS_snprintf(p, len, "%s=%s", k, v); | 
|  | 3681 | envlist[envc++] = p; | 
|  | 3682 | } | 
|  | 3683 | envlist[envc] = 0; | 
|  | 3684 |  | 
|  | 3685 | Py_BEGIN_ALLOW_THREADS | 
|  | 3686 | #if defined(PYCC_GCC) | 
| Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3687 | spawnval = spawnvpe(mode, path, argvlist, envlist); | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3688 | #else | 
| Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3689 | spawnval = _spawnvpe(mode, path, argvlist, envlist); | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3690 | #endif | 
|  | 3691 | Py_END_ALLOW_THREADS | 
|  | 3692 |  | 
|  | 3693 | if (spawnval == -1) | 
|  | 3694 | (void) posix_error(); | 
|  | 3695 | else | 
|  | 3696 | res = Py_BuildValue("l", (long) spawnval); | 
|  | 3697 |  | 
|  | 3698 | fail_2: | 
|  | 3699 | while (--envc >= 0) | 
|  | 3700 | PyMem_DEL(envlist[envc]); | 
|  | 3701 | PyMem_DEL(envlist); | 
|  | 3702 | fail_1: | 
|  | 3703 | free_string_array(argvlist, lastarg); | 
|  | 3704 | Py_XDECREF(vals); | 
|  | 3705 | Py_XDECREF(keys); | 
|  | 3706 | fail_0: | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3707 | release_bytes(opath); | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3708 | return res; | 
|  | 3709 | } | 
|  | 3710 | #endif /* PYOS_OS2 */ | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3711 | #endif /* HAVE_SPAWNV */ | 
|  | 3712 |  | 
|  | 3713 |  | 
| Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3714 | #ifdef HAVE_FORK1 | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3715 | PyDoc_STRVAR(posix_fork1__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3716 | "fork1() -> pid\n\n\ | 
| Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3717 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ | 
|  | 3718 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3719 | Return 0 to child process and PID of child to parent process."); | 
| Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3720 |  | 
|  | 3721 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3722 | posix_fork1(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3723 | { | 
| Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3724 | pid_t pid = fork1(); | 
| Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3725 | if (pid == -1) | 
|  | 3726 | return posix_error(); | 
| Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 3727 | if (pid == 0) | 
|  | 3728 | PyOS_AfterFork(); | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 3729 | return PyLong_FromPid(pid); | 
| Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3730 | } | 
|  | 3731 | #endif | 
|  | 3732 |  | 
|  | 3733 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3734 | #ifdef HAVE_FORK | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3735 | PyDoc_STRVAR(posix_fork__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3736 | "fork() -> pid\n\n\ | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3737 | Fork a child process.\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3738 | Return 0 to child process and PID of child to parent process."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3739 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3740 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3741 | posix_fork(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3742 | { | 
| Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3743 | pid_t pid = fork(); | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3744 | if (pid == -1) | 
|  | 3745 | return posix_error(); | 
| Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3746 | if (pid == 0) | 
|  | 3747 | PyOS_AfterFork(); | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 3748 | return PyLong_FromPid(pid); | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3749 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3750 | #endif | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3751 |  | 
| Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3752 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ | 
| Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3753 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ | 
|  | 3754 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) | 
| Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3755 | #define DEV_PTY_FILE "/dev/ptc" | 
|  | 3756 | #define HAVE_DEV_PTMX | 
|  | 3757 | #else | 
|  | 3758 | #define DEV_PTY_FILE "/dev/ptmx" | 
|  | 3759 | #endif | 
|  | 3760 |  | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3761 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3762 | #ifdef HAVE_PTY_H | 
|  | 3763 | #include <pty.h> | 
|  | 3764 | #else | 
|  | 3765 | #ifdef HAVE_LIBUTIL_H | 
|  | 3766 | #include <libutil.h> | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3767 | #endif /* HAVE_LIBUTIL_H */ | 
|  | 3768 | #endif /* HAVE_PTY_H */ | 
| Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3769 | #ifdef HAVE_STROPTS_H | 
|  | 3770 | #include <stropts.h> | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3771 | #endif | 
|  | 3772 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3773 |  | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3774 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3775 | PyDoc_STRVAR(posix_openpty__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3776 | "openpty() -> (master_fd, slave_fd)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3777 | Open a pseudo-terminal, returning open fd's for both master and slave end.\n"); | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3778 |  | 
|  | 3779 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3780 | posix_openpty(PyObject *self, PyObject *noargs) | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3781 | { | 
|  | 3782 | int master_fd, slave_fd; | 
| Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3783 | #ifndef HAVE_OPENPTY | 
|  | 3784 | char * slave_name; | 
| Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3785 | #endif | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3786 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) | 
| Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3787 | PyOS_sighandler_t sig_saved; | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3788 | #ifdef sun | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3789 | extern char *ptsname(int fildes); | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3790 | #endif | 
|  | 3791 | #endif | 
| Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3792 |  | 
| Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3793 | #ifdef HAVE_OPENPTY | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3794 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) | 
|  | 3795 | return posix_error(); | 
| Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3796 | #elif defined(HAVE__GETPTY) | 
| Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3797 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); | 
|  | 3798 | if (slave_name == NULL) | 
|  | 3799 | return posix_error(); | 
|  | 3800 |  | 
|  | 3801 | slave_fd = open(slave_name, O_RDWR); | 
|  | 3802 | if (slave_fd < 0) | 
|  | 3803 | return posix_error(); | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3804 | #else | 
| Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3805 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3806 | if (master_fd < 0) | 
|  | 3807 | return posix_error(); | 
| Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3808 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); | 
| Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3809 | /* change permission of slave */ | 
|  | 3810 | if (grantpt(master_fd) < 0) { | 
| Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3811 | PyOS_setsig(SIGCHLD, sig_saved); | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3812 | return posix_error(); | 
| Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3813 | } | 
|  | 3814 | /* unlock slave */ | 
|  | 3815 | if (unlockpt(master_fd) < 0) { | 
| Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3816 | PyOS_setsig(SIGCHLD, sig_saved); | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3817 | return posix_error(); | 
| Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3818 | } | 
| Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3819 | PyOS_setsig(SIGCHLD, sig_saved); | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3820 | slave_name = ptsname(master_fd); /* get name of slave */ | 
|  | 3821 | if (slave_name == NULL) | 
|  | 3822 | return posix_error(); | 
|  | 3823 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ | 
|  | 3824 | if (slave_fd < 0) | 
|  | 3825 | return posix_error(); | 
| Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3826 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3827 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ | 
|  | 3828 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ | 
| Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3829 | #ifndef __hpux | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3830 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ | 
| Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3831 | #endif /* __hpux */ | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3832 | #endif /* HAVE_CYGWIN */ | 
| Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3833 | #endif /* HAVE_OPENPTY */ | 
| Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3834 |  | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3835 | return Py_BuildValue("(ii)", master_fd, slave_fd); | 
| Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3836 |  | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3837 | } | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3838 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3839 |  | 
|  | 3840 | #ifdef HAVE_FORKPTY | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3841 | PyDoc_STRVAR(posix_forkpty__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3842 | "forkpty() -> (pid, master_fd)\n\n\ | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3843 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ | 
|  | 3844 | Like fork(), return 0 as pid to child process, and PID of child to parent.\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3845 | To both, return fd of newly opened pseudo-terminal.\n"); | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3846 |  | 
|  | 3847 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3848 | posix_forkpty(PyObject *self, PyObject *noargs) | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3849 | { | 
| Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3850 | int master_fd = -1; | 
|  | 3851 | pid_t pid; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3852 |  | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3853 | pid = forkpty(&master_fd, NULL, NULL, NULL); | 
|  | 3854 | if (pid == -1) | 
|  | 3855 | return posix_error(); | 
| Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3856 | if (pid == 0) | 
|  | 3857 | PyOS_AfterFork(); | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 3858 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3859 | } | 
|  | 3860 | #endif | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3861 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3862 | #ifdef HAVE_GETEGID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3863 | PyDoc_STRVAR(posix_getegid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3864 | "getegid() -> egid\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3865 | Return the current process's effective group id."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3866 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3867 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3868 | posix_getegid(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3869 | { | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3870 | return PyLong_FromLong((long)getegid()); | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3871 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3872 | #endif | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3873 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3874 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3875 | #ifdef HAVE_GETEUID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3876 | PyDoc_STRVAR(posix_geteuid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3877 | "geteuid() -> euid\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3878 | Return the current process's effective user id."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3879 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3880 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3881 | posix_geteuid(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3882 | { | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3883 | return PyLong_FromLong((long)geteuid()); | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3884 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3885 | #endif | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3886 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3887 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3888 | #ifdef HAVE_GETGID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3889 | PyDoc_STRVAR(posix_getgid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3890 | "getgid() -> gid\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3891 | Return the current process's group id."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3892 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3893 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3894 | posix_getgid(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3895 | { | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3896 | return PyLong_FromLong((long)getgid()); | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3897 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3898 | #endif | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3899 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3900 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3901 | PyDoc_STRVAR(posix_getpid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3902 | "getpid() -> pid\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3903 | Return the current process id"); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3904 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3905 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3906 | posix_getpid(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3907 | { | 
| Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 3908 | return PyLong_FromPid(getpid()); | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3909 | } | 
|  | 3910 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3911 |  | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3912 | #ifdef HAVE_GETGROUPS | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3913 | PyDoc_STRVAR(posix_getgroups__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3914 | "getgroups() -> list of group IDs\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3915 | Return list of supplemental group IDs for the process."); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3916 |  | 
|  | 3917 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3918 | posix_getgroups(PyObject *self, PyObject *noargs) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3919 | { | 
|  | 3920 | PyObject *result = NULL; | 
|  | 3921 |  | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3922 | #ifdef NGROUPS_MAX | 
|  | 3923 | #define MAX_GROUPS NGROUPS_MAX | 
|  | 3924 | #else | 
|  | 3925 | /* defined to be 16 on Solaris7, so this should be a small number */ | 
|  | 3926 | #define MAX_GROUPS 64 | 
|  | 3927 | #endif | 
|  | 3928 | gid_t grouplist[MAX_GROUPS]; | 
|  | 3929 | int n; | 
|  | 3930 |  | 
|  | 3931 | n = getgroups(MAX_GROUPS, grouplist); | 
|  | 3932 | if (n < 0) | 
|  | 3933 | posix_error(); | 
|  | 3934 | else { | 
|  | 3935 | result = PyList_New(n); | 
|  | 3936 | if (result != NULL) { | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3937 | int i; | 
|  | 3938 | for (i = 0; i < n; ++i) { | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3939 | PyObject *o = PyLong_FromLong((long)grouplist[i]); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3940 | if (o == NULL) { | 
|  | 3941 | Py_DECREF(result); | 
|  | 3942 | result = NULL; | 
|  | 3943 | break; | 
|  | 3944 | } | 
|  | 3945 | PyList_SET_ITEM(result, i, o); | 
|  | 3946 | } | 
|  | 3947 | } | 
|  | 3948 | } | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3949 |  | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3950 | return result; | 
|  | 3951 | } | 
|  | 3952 | #endif | 
|  | 3953 |  | 
| Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3954 | #ifdef HAVE_GETPGID | 
| Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3955 | PyDoc_STRVAR(posix_getpgid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3956 | "getpgid(pid) -> pgid\n\n\ | 
| Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3957 | Call the system call getpgid()."); | 
| Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3958 |  | 
|  | 3959 | static PyObject * | 
|  | 3960 | posix_getpgid(PyObject *self, PyObject *args) | 
|  | 3961 | { | 
| Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 3962 | pid_t pid, pgid; | 
|  | 3963 | if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid)) | 
| Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3964 | return NULL; | 
|  | 3965 | pgid = getpgid(pid); | 
|  | 3966 | if (pgid < 0) | 
|  | 3967 | return posix_error(); | 
| Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 3968 | return PyLong_FromPid(pgid); | 
| Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3969 | } | 
|  | 3970 | #endif /* HAVE_GETPGID */ | 
|  | 3971 |  | 
|  | 3972 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3973 | #ifdef HAVE_GETPGRP | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3974 | PyDoc_STRVAR(posix_getpgrp__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3975 | "getpgrp() -> pgrp\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3976 | Return the current process group id."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3977 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3978 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3979 | posix_getpgrp(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3980 | { | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3981 | #ifdef GETPGRP_HAVE_ARG | 
| Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 3982 | return PyLong_FromPid(getpgrp(0)); | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3983 | #else /* GETPGRP_HAVE_ARG */ | 
| Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 3984 | return PyLong_FromPid(getpgrp()); | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3985 | #endif /* GETPGRP_HAVE_ARG */ | 
| Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3986 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3987 | #endif /* HAVE_GETPGRP */ | 
| Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3988 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3989 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3990 | #ifdef HAVE_SETPGRP | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3991 | PyDoc_STRVAR(posix_setpgrp__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3992 | "setpgrp()\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3993 | Make this process a session leader."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3994 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3995 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3996 | posix_setpgrp(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3997 | { | 
| Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3998 | #ifdef SETPGRP_HAVE_ARG | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3999 | if (setpgrp(0, 0) < 0) | 
| Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4000 | #else /* SETPGRP_HAVE_ARG */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4001 | if (setpgrp() < 0) | 
| Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4002 | #endif /* SETPGRP_HAVE_ARG */ | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4003 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4004 | Py_INCREF(Py_None); | 
|  | 4005 | return Py_None; | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4006 | } | 
|  | 4007 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4008 | #endif /* HAVE_SETPGRP */ | 
|  | 4009 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4010 | #ifdef HAVE_GETPPID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4011 | PyDoc_STRVAR(posix_getppid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4012 | "getppid() -> ppid\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4013 | Return the parent's process id."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4014 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4015 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4016 | posix_getppid(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4017 | { | 
| Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4018 | return PyLong_FromPid(getppid()); | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4019 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4020 | #endif | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4021 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4022 |  | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4023 | #ifdef HAVE_GETLOGIN | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4024 | PyDoc_STRVAR(posix_getlogin__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4025 | "getlogin() -> string\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4026 | Return the actual login name."); | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4027 |  | 
|  | 4028 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4029 | posix_getlogin(PyObject *self, PyObject *noargs) | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4030 | { | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4031 | PyObject *result = NULL; | 
| Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4032 | char *name; | 
|  | 4033 | int old_errno = errno; | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4034 |  | 
| Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4035 | errno = 0; | 
|  | 4036 | name = getlogin(); | 
|  | 4037 | if (name == NULL) { | 
|  | 4038 | if (errno) | 
|  | 4039 | posix_error(); | 
|  | 4040 | else | 
|  | 4041 | PyErr_SetString(PyExc_OSError, | 
| Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 4042 | "unable to determine login name"); | 
| Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4043 | } | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4044 | else | 
| Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 4045 | result = PyUnicode_FromString(name); | 
| Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4046 | errno = old_errno; | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4047 |  | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4048 | return result; | 
|  | 4049 | } | 
|  | 4050 | #endif | 
|  | 4051 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4052 | #ifdef HAVE_GETUID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4053 | PyDoc_STRVAR(posix_getuid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4054 | "getuid() -> uid\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4055 | Return the current process's user id."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4056 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4057 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4058 | posix_getuid(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4059 | { | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4060 | return PyLong_FromLong((long)getuid()); | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4061 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4062 | #endif | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4063 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4064 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4065 | #ifdef HAVE_KILL | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4066 | PyDoc_STRVAR(posix_kill__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4067 | "kill(pid, sig)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4068 | Kill a process with a signal."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4069 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4070 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4071 | posix_kill(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4072 | { | 
| Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4073 | pid_t pid; | 
|  | 4074 | int sig; | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4075 | if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig)) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4076 | return NULL; | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4077 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4078 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { | 
|  | 4079 | APIRET rc; | 
|  | 4080 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4081 | return os2_error(rc); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4082 |  | 
|  | 4083 | } else if (sig == XCPT_SIGNAL_KILLPROC) { | 
|  | 4084 | APIRET rc; | 
|  | 4085 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4086 | return os2_error(rc); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4087 |  | 
|  | 4088 | } else | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 4089 | return NULL; /* Unrecognized Signal Requested */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4090 | #else | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4091 | if (kill(pid, sig) == -1) | 
|  | 4092 | return posix_error(); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4093 | #endif | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4094 | Py_INCREF(Py_None); | 
|  | 4095 | return Py_None; | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4096 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4097 | #endif | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4098 |  | 
| Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4099 | #ifdef HAVE_KILLPG | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4100 | PyDoc_STRVAR(posix_killpg__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4101 | "killpg(pgid, sig)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4102 | Kill a process group with a signal."); | 
| Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4103 |  | 
|  | 4104 | static PyObject * | 
|  | 4105 | posix_killpg(PyObject *self, PyObject *args) | 
|  | 4106 | { | 
| Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4107 | int sig; | 
|  | 4108 | pid_t pgid; | 
|  | 4109 | /* XXX some man pages make the `pgid` parameter an int, others | 
|  | 4110 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should | 
|  | 4111 | take the same type. Moreover, pid_t is always at least as wide as | 
|  | 4112 | int (else compilation of this module fails), which is safe. */ | 
|  | 4113 | if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig)) | 
| Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4114 | return NULL; | 
|  | 4115 | if (killpg(pgid, sig) == -1) | 
|  | 4116 | return posix_error(); | 
|  | 4117 | Py_INCREF(Py_None); | 
|  | 4118 | return Py_None; | 
|  | 4119 | } | 
|  | 4120 | #endif | 
|  | 4121 |  | 
| Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4122 | #ifdef HAVE_PLOCK | 
|  | 4123 |  | 
|  | 4124 | #ifdef HAVE_SYS_LOCK_H | 
|  | 4125 | #include <sys/lock.h> | 
|  | 4126 | #endif | 
|  | 4127 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4128 | PyDoc_STRVAR(posix_plock__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4129 | "plock(op)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4130 | Lock program segments into memory."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4131 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4132 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4133 | posix_plock(PyObject *self, PyObject *args) | 
| Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4134 | { | 
|  | 4135 | int op; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4136 | if (!PyArg_ParseTuple(args, "i:plock", &op)) | 
| Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4137 | return NULL; | 
|  | 4138 | if (plock(op) == -1) | 
|  | 4139 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4140 | Py_INCREF(Py_None); | 
|  | 4141 | return Py_None; | 
| Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4142 | } | 
|  | 4143 | #endif | 
|  | 4144 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4145 | #ifdef HAVE_SETUID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4146 | PyDoc_STRVAR(posix_setuid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4147 | "setuid(uid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4148 | Set the current process's user id."); | 
|  | 4149 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4150 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4151 | posix_setuid(PyObject *self, PyObject *args) | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4152 | { | 
| Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4153 | long uid_arg; | 
|  | 4154 | uid_t uid; | 
|  | 4155 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4156 | return NULL; | 
| Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4157 | uid = uid_arg; | 
|  | 4158 | if (uid != uid_arg) { | 
|  | 4159 | PyErr_SetString(PyExc_OverflowError, "user id too big"); | 
|  | 4160 | return NULL; | 
|  | 4161 | } | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4162 | if (setuid(uid) < 0) | 
|  | 4163 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4164 | Py_INCREF(Py_None); | 
|  | 4165 | return Py_None; | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4166 | } | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4167 | #endif /* HAVE_SETUID */ | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4168 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4169 |  | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4170 | #ifdef HAVE_SETEUID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4171 | PyDoc_STRVAR(posix_seteuid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4172 | "seteuid(uid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4173 | Set the current process's effective user id."); | 
|  | 4174 |  | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4175 | static PyObject * | 
|  | 4176 | posix_seteuid (PyObject *self, PyObject *args) | 
|  | 4177 | { | 
| Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4178 | long euid_arg; | 
|  | 4179 | uid_t euid; | 
|  | 4180 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4181 | return NULL; | 
| Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4182 | euid = euid_arg; | 
|  | 4183 | if (euid != euid_arg) { | 
|  | 4184 | PyErr_SetString(PyExc_OverflowError, "user id too big"); | 
|  | 4185 | return NULL; | 
|  | 4186 | } | 
|  | 4187 | if (seteuid(euid) < 0) { | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4188 | return posix_error(); | 
|  | 4189 | } else { | 
|  | 4190 | Py_INCREF(Py_None); | 
|  | 4191 | return Py_None; | 
|  | 4192 | } | 
|  | 4193 | } | 
|  | 4194 | #endif /* HAVE_SETEUID */ | 
|  | 4195 |  | 
|  | 4196 | #ifdef HAVE_SETEGID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4197 | PyDoc_STRVAR(posix_setegid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4198 | "setegid(gid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4199 | Set the current process's effective group id."); | 
|  | 4200 |  | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4201 | static PyObject * | 
|  | 4202 | posix_setegid (PyObject *self, PyObject *args) | 
|  | 4203 | { | 
| Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4204 | long egid_arg; | 
|  | 4205 | gid_t egid; | 
|  | 4206 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4207 | return NULL; | 
| Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4208 | egid = egid_arg; | 
|  | 4209 | if (egid != egid_arg) { | 
|  | 4210 | PyErr_SetString(PyExc_OverflowError, "group id too big"); | 
|  | 4211 | return NULL; | 
|  | 4212 | } | 
|  | 4213 | if (setegid(egid) < 0) { | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4214 | return posix_error(); | 
|  | 4215 | } else { | 
|  | 4216 | Py_INCREF(Py_None); | 
|  | 4217 | return Py_None; | 
|  | 4218 | } | 
|  | 4219 | } | 
|  | 4220 | #endif /* HAVE_SETEGID */ | 
|  | 4221 |  | 
|  | 4222 | #ifdef HAVE_SETREUID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4223 | PyDoc_STRVAR(posix_setreuid__doc__, | 
| Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4224 | "setreuid(ruid, euid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4225 | Set the current process's real and effective user ids."); | 
|  | 4226 |  | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4227 | static PyObject * | 
|  | 4228 | posix_setreuid (PyObject *self, PyObject *args) | 
|  | 4229 | { | 
| Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4230 | long ruid_arg, euid_arg; | 
|  | 4231 | uid_t ruid, euid; | 
|  | 4232 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4233 | return NULL; | 
| Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4234 | ruid = ruid_arg; | 
|  | 4235 | euid = euid_arg; | 
|  | 4236 | if (euid != euid_arg || ruid != ruid_arg) { | 
|  | 4237 | PyErr_SetString(PyExc_OverflowError, "user id too big"); | 
|  | 4238 | return NULL; | 
|  | 4239 | } | 
|  | 4240 | if (setreuid(ruid, euid) < 0) { | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4241 | return posix_error(); | 
|  | 4242 | } else { | 
|  | 4243 | Py_INCREF(Py_None); | 
|  | 4244 | return Py_None; | 
|  | 4245 | } | 
|  | 4246 | } | 
|  | 4247 | #endif /* HAVE_SETREUID */ | 
|  | 4248 |  | 
|  | 4249 | #ifdef HAVE_SETREGID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4250 | PyDoc_STRVAR(posix_setregid__doc__, | 
| Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4251 | "setregid(rgid, egid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4252 | Set the current process's real and effective group ids."); | 
|  | 4253 |  | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4254 | static PyObject * | 
|  | 4255 | posix_setregid (PyObject *self, PyObject *args) | 
|  | 4256 | { | 
| Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4257 | long rgid_arg, egid_arg; | 
|  | 4258 | gid_t rgid, egid; | 
|  | 4259 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4260 | return NULL; | 
| Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4261 | rgid = rgid_arg; | 
|  | 4262 | egid = egid_arg; | 
|  | 4263 | if (egid != egid_arg || rgid != rgid_arg) { | 
|  | 4264 | PyErr_SetString(PyExc_OverflowError, "group id too big"); | 
|  | 4265 | return NULL; | 
|  | 4266 | } | 
|  | 4267 | if (setregid(rgid, egid) < 0) { | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4268 | return posix_error(); | 
|  | 4269 | } else { | 
|  | 4270 | Py_INCREF(Py_None); | 
|  | 4271 | return Py_None; | 
|  | 4272 | } | 
|  | 4273 | } | 
|  | 4274 | #endif /* HAVE_SETREGID */ | 
|  | 4275 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4276 | #ifdef HAVE_SETGID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4277 | PyDoc_STRVAR(posix_setgid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4278 | "setgid(gid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4279 | Set the current process's group id."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4280 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4281 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4282 | posix_setgid(PyObject *self, PyObject *args) | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4283 | { | 
| Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4284 | long gid_arg; | 
|  | 4285 | gid_t gid; | 
|  | 4286 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4287 | return NULL; | 
| Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4288 | gid = gid_arg; | 
|  | 4289 | if (gid != gid_arg) { | 
|  | 4290 | PyErr_SetString(PyExc_OverflowError, "group id too big"); | 
|  | 4291 | return NULL; | 
|  | 4292 | } | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4293 | if (setgid(gid) < 0) | 
|  | 4294 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4295 | Py_INCREF(Py_None); | 
|  | 4296 | return Py_None; | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4297 | } | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4298 | #endif /* HAVE_SETGID */ | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4299 |  | 
| Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4300 | #ifdef HAVE_SETGROUPS | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4301 | PyDoc_STRVAR(posix_setgroups__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4302 | "setgroups(list)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4303 | Set the groups of the current process to list."); | 
| Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4304 |  | 
|  | 4305 | static PyObject * | 
| Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4306 | posix_setgroups(PyObject *self, PyObject *groups) | 
| Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4307 | { | 
| Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4308 | int i, len; | 
|  | 4309 | gid_t grouplist[MAX_GROUPS]; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4310 |  | 
| Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4311 | if (!PySequence_Check(groups)) { | 
|  | 4312 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); | 
|  | 4313 | return NULL; | 
|  | 4314 | } | 
|  | 4315 | len = PySequence_Size(groups); | 
|  | 4316 | if (len > MAX_GROUPS) { | 
|  | 4317 | PyErr_SetString(PyExc_ValueError, "too many groups"); | 
|  | 4318 | return NULL; | 
|  | 4319 | } | 
|  | 4320 | for(i = 0; i < len; i++) { | 
|  | 4321 | PyObject *elem; | 
|  | 4322 | elem = PySequence_GetItem(groups, i); | 
|  | 4323 | if (!elem) | 
|  | 4324 | return NULL; | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4325 | if (!PyLong_Check(elem)) { | 
|  | 4326 | PyErr_SetString(PyExc_TypeError, | 
|  | 4327 | "groups must be integers"); | 
|  | 4328 | Py_DECREF(elem); | 
|  | 4329 | return NULL; | 
|  | 4330 | } else { | 
|  | 4331 | unsigned long x = PyLong_AsUnsignedLong(elem); | 
|  | 4332 | if (PyErr_Occurred()) { | 
|  | 4333 | PyErr_SetString(PyExc_TypeError, | 
|  | 4334 | "group id too big"); | 
| Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4335 | Py_DECREF(elem); | 
|  | 4336 | return NULL; | 
| Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4337 | } | 
| Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4338 | grouplist[i] = x; | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4339 | /* read back the value to see if it fitted in gid_t */ | 
| Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4340 | if (grouplist[i] != x) { | 
|  | 4341 | PyErr_SetString(PyExc_TypeError, | 
|  | 4342 | "group id too big"); | 
|  | 4343 | Py_DECREF(elem); | 
|  | 4344 | return NULL; | 
|  | 4345 | } | 
| Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4346 | } | 
| Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4347 | Py_DECREF(elem); | 
|  | 4348 | } | 
|  | 4349 |  | 
|  | 4350 | if (setgroups(len, grouplist) < 0) | 
|  | 4351 | return posix_error(); | 
|  | 4352 | Py_INCREF(Py_None); | 
|  | 4353 | return Py_None; | 
|  | 4354 | } | 
|  | 4355 | #endif /* HAVE_SETGROUPS */ | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4356 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4357 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) | 
|  | 4358 | static PyObject * | 
| Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4359 | wait_helper(pid_t pid, int status, struct rusage *ru) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4360 | { | 
|  | 4361 | PyObject *result; | 
|  | 4362 | static PyObject *struct_rusage; | 
|  | 4363 |  | 
|  | 4364 | if (pid == -1) | 
|  | 4365 | return posix_error(); | 
|  | 4366 |  | 
|  | 4367 | if (struct_rusage == NULL) { | 
| Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 4368 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4369 | if (m == NULL) | 
|  | 4370 | return NULL; | 
|  | 4371 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); | 
|  | 4372 | Py_DECREF(m); | 
|  | 4373 | if (struct_rusage == NULL) | 
|  | 4374 | return NULL; | 
|  | 4375 | } | 
|  | 4376 |  | 
|  | 4377 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ | 
|  | 4378 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); | 
|  | 4379 | if (!result) | 
|  | 4380 | return NULL; | 
|  | 4381 |  | 
|  | 4382 | #ifndef doubletime | 
|  | 4383 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) | 
|  | 4384 | #endif | 
|  | 4385 |  | 
|  | 4386 | PyStructSequence_SET_ITEM(result, 0, | 
|  | 4387 | PyFloat_FromDouble(doubletime(ru->ru_utime))); | 
|  | 4388 | PyStructSequence_SET_ITEM(result, 1, | 
|  | 4389 | PyFloat_FromDouble(doubletime(ru->ru_stime))); | 
|  | 4390 | #define SET_INT(result, index, value)\ | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4391 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4392 | SET_INT(result, 2, ru->ru_maxrss); | 
|  | 4393 | SET_INT(result, 3, ru->ru_ixrss); | 
|  | 4394 | SET_INT(result, 4, ru->ru_idrss); | 
|  | 4395 | SET_INT(result, 5, ru->ru_isrss); | 
|  | 4396 | SET_INT(result, 6, ru->ru_minflt); | 
|  | 4397 | SET_INT(result, 7, ru->ru_majflt); | 
|  | 4398 | SET_INT(result, 8, ru->ru_nswap); | 
|  | 4399 | SET_INT(result, 9, ru->ru_inblock); | 
|  | 4400 | SET_INT(result, 10, ru->ru_oublock); | 
|  | 4401 | SET_INT(result, 11, ru->ru_msgsnd); | 
|  | 4402 | SET_INT(result, 12, ru->ru_msgrcv); | 
|  | 4403 | SET_INT(result, 13, ru->ru_nsignals); | 
|  | 4404 | SET_INT(result, 14, ru->ru_nvcsw); | 
|  | 4405 | SET_INT(result, 15, ru->ru_nivcsw); | 
|  | 4406 | #undef SET_INT | 
|  | 4407 |  | 
|  | 4408 | if (PyErr_Occurred()) { | 
|  | 4409 | Py_DECREF(result); | 
|  | 4410 | return NULL; | 
|  | 4411 | } | 
|  | 4412 |  | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4413 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4414 | } | 
|  | 4415 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ | 
|  | 4416 |  | 
|  | 4417 | #ifdef HAVE_WAIT3 | 
|  | 4418 | PyDoc_STRVAR(posix_wait3__doc__, | 
|  | 4419 | "wait3(options) -> (pid, status, rusage)\n\n\ | 
|  | 4420 | Wait for completion of a child process."); | 
|  | 4421 |  | 
|  | 4422 | static PyObject * | 
|  | 4423 | posix_wait3(PyObject *self, PyObject *args) | 
|  | 4424 | { | 
| Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4425 | pid_t pid; | 
|  | 4426 | int options; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4427 | struct rusage ru; | 
|  | 4428 | WAIT_TYPE status; | 
|  | 4429 | WAIT_STATUS_INT(status) = 0; | 
|  | 4430 |  | 
|  | 4431 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) | 
|  | 4432 | return NULL; | 
|  | 4433 |  | 
|  | 4434 | Py_BEGIN_ALLOW_THREADS | 
|  | 4435 | pid = wait3(&status, options, &ru); | 
|  | 4436 | Py_END_ALLOW_THREADS | 
|  | 4437 |  | 
|  | 4438 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); | 
|  | 4439 | } | 
|  | 4440 | #endif /* HAVE_WAIT3 */ | 
|  | 4441 |  | 
|  | 4442 | #ifdef HAVE_WAIT4 | 
|  | 4443 | PyDoc_STRVAR(posix_wait4__doc__, | 
|  | 4444 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ | 
|  | 4445 | Wait for completion of a given child process."); | 
|  | 4446 |  | 
|  | 4447 | static PyObject * | 
|  | 4448 | posix_wait4(PyObject *self, PyObject *args) | 
|  | 4449 | { | 
| Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4450 | pid_t pid; | 
|  | 4451 | int options; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4452 | struct rusage ru; | 
|  | 4453 | WAIT_TYPE status; | 
|  | 4454 | WAIT_STATUS_INT(status) = 0; | 
|  | 4455 |  | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4456 | if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options)) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4457 | return NULL; | 
|  | 4458 |  | 
|  | 4459 | Py_BEGIN_ALLOW_THREADS | 
|  | 4460 | pid = wait4(pid, &status, options, &ru); | 
|  | 4461 | Py_END_ALLOW_THREADS | 
|  | 4462 |  | 
|  | 4463 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); | 
|  | 4464 | } | 
|  | 4465 | #endif /* HAVE_WAIT4 */ | 
|  | 4466 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4467 | #ifdef HAVE_WAITPID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4468 | PyDoc_STRVAR(posix_waitpid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4469 | "waitpid(pid, options) -> (pid, status)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4470 | Wait for completion of a given child process."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4471 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4472 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4473 | posix_waitpid(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4474 | { | 
| Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4475 | pid_t pid; | 
|  | 4476 | int options; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4477 | WAIT_TYPE status; | 
|  | 4478 | WAIT_STATUS_INT(status) = 0; | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4479 |  | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4480 | if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) | 
| Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4481 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4482 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4483 | pid = waitpid(pid, &status, options); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4484 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4485 | if (pid == -1) | 
|  | 4486 | return posix_error(); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4487 |  | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4488 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); | 
| Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4489 | } | 
|  | 4490 |  | 
| Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4491 | #elif defined(HAVE_CWAIT) | 
|  | 4492 |  | 
|  | 4493 | /* MS C has a variant of waitpid() that's usable for most purposes. */ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4494 | PyDoc_STRVAR(posix_waitpid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4495 | "waitpid(pid, options) -> (pid, status << 8)\n\n" | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4496 | "Wait for completion of a given process.  options is ignored on Windows."); | 
| Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4497 |  | 
|  | 4498 | static PyObject * | 
|  | 4499 | posix_waitpid(PyObject *self, PyObject *args) | 
|  | 4500 | { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4501 | Py_intptr_t pid; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4502 | int status, options; | 
| Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4503 |  | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4504 | if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) | 
| Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4505 | return NULL; | 
|  | 4506 | Py_BEGIN_ALLOW_THREADS | 
|  | 4507 | pid = _cwait(&status, pid, options); | 
|  | 4508 | Py_END_ALLOW_THREADS | 
|  | 4509 | if (pid == -1) | 
|  | 4510 | return posix_error(); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4511 |  | 
|  | 4512 | /* shift the status left a byte so this is more like the POSIX waitpid */ | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4513 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); | 
| Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4514 | } | 
|  | 4515 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4516 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4517 | #ifdef HAVE_WAIT | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4518 | PyDoc_STRVAR(posix_wait__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4519 | "wait() -> (pid, status)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4520 | Wait for completion of a child process."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4521 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4522 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4523 | posix_wait(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4524 | { | 
| Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4525 | pid_t pid; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4526 | WAIT_TYPE status; | 
|  | 4527 | WAIT_STATUS_INT(status) = 0; | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4528 |  | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4529 | Py_BEGIN_ALLOW_THREADS | 
|  | 4530 | pid = wait(&status); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4531 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4532 | if (pid == -1) | 
|  | 4533 | return posix_error(); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4534 |  | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4535 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4536 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4537 | #endif | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4538 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4539 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4540 | PyDoc_STRVAR(posix_lstat__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4541 | "lstat(path) -> stat result\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4542 | Like stat(path), but do not follow symbolic links."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4543 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4544 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4545 | posix_lstat(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4546 | { | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4547 | #ifdef HAVE_LSTAT | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4548 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4549 | #else /* !HAVE_LSTAT */ | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4550 | #ifdef MS_WINDOWS | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4551 | return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4552 | #else | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4553 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4554 | #endif | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4555 | #endif /* !HAVE_LSTAT */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4556 | } | 
|  | 4557 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4558 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4559 | #ifdef HAVE_READLINK | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4560 | PyDoc_STRVAR(posix_readlink__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4561 | "readlink(path) -> path\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4562 | Return a string representing the path to which the symbolic link points."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4563 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4564 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4565 | posix_readlink(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4566 | { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4567 | PyObject* v; | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4568 | char buf[MAXPATHLEN]; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4569 | PyObject *opath; | 
| Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4570 | char *path; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4571 | int n; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4572 | int arg_is_unicode = 0; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4573 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4574 | if (!PyArg_ParseTuple(args, "O&:readlink", | 
|  | 4575 | PyUnicode_FSConverter, &opath)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4576 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4577 | path = bytes2str(opath, 1); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4578 | v = PySequence_GetItem(args, 0); | 
| Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4579 | if (v == NULL) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4580 | release_bytes(opath); | 
| Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4581 | return NULL; | 
|  | 4582 | } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4583 |  | 
|  | 4584 | if (PyUnicode_Check(v)) { | 
|  | 4585 | arg_is_unicode = 1; | 
|  | 4586 | } | 
|  | 4587 | Py_DECREF(v); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4588 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4589 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4590 | n = readlink(path, buf, (int) sizeof buf); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4591 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4592 | if (n < 0) | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4593 | return posix_error_with_allocated_filename(opath); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4594 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4595 | release_bytes(opath); | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4596 | v = PyBytes_FromStringAndSize(buf, n); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4597 | if (arg_is_unicode) { | 
|  | 4598 | PyObject *w; | 
|  | 4599 |  | 
|  | 4600 | w = PyUnicode_FromEncodedObject(v, | 
|  | 4601 | Py_FileSystemDefaultEncoding, | 
| Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 4602 | "surrogateescape"); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4603 | if (w != NULL) { | 
|  | 4604 | Py_DECREF(v); | 
|  | 4605 | v = w; | 
|  | 4606 | } | 
|  | 4607 | else { | 
| Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 4608 | v = NULL; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4609 | } | 
|  | 4610 | } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4611 | return v; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4612 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4613 | #endif /* HAVE_READLINK */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4614 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4615 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4616 | #ifdef HAVE_SYMLINK | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4617 | PyDoc_STRVAR(posix_symlink__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4618 | "symlink(src, dst)\n\n\ | 
| Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4619 | Create a symbolic link pointing to src named dst."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4620 |  | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4621 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4622 | posix_symlink(PyObject *self, PyObject *args) | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4623 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4624 | return posix_2str(args, "O&O&:symlink", symlink); | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4625 | } | 
|  | 4626 | #endif /* HAVE_SYMLINK */ | 
|  | 4627 |  | 
|  | 4628 |  | 
|  | 4629 | #ifdef HAVE_TIMES | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4630 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) | 
|  | 4631 | static long | 
| Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4632 | system_uptime(void) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4633 | { | 
|  | 4634 | ULONG     value = 0; | 
|  | 4635 |  | 
|  | 4636 | Py_BEGIN_ALLOW_THREADS | 
|  | 4637 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); | 
|  | 4638 | Py_END_ALLOW_THREADS | 
|  | 4639 |  | 
|  | 4640 | return value; | 
|  | 4641 | } | 
|  | 4642 |  | 
|  | 4643 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4644 | posix_times(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4645 | { | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4646 | /* Currently Only Uptime is Provided -- Others Later */ | 
|  | 4647 | return Py_BuildValue("ddddd", | 
|  | 4648 | (double)0 /* t.tms_utime / HZ */, | 
|  | 4649 | (double)0 /* t.tms_stime / HZ */, | 
|  | 4650 | (double)0 /* t.tms_cutime / HZ */, | 
|  | 4651 | (double)0 /* t.tms_cstime / HZ */, | 
|  | 4652 | (double)system_uptime() / 1000); | 
|  | 4653 | } | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4654 | #else /* not OS2 */ | 
| Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 4655 | #define NEED_TICKS_PER_SECOND | 
|  | 4656 | static long ticks_per_second = -1; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4657 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4658 | posix_times(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4659 | { | 
|  | 4660 | struct tms t; | 
|  | 4661 | clock_t c; | 
| Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4662 | errno = 0; | 
|  | 4663 | c = times(&t); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4664 | if (c == (clock_t) -1) | 
|  | 4665 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4666 | return Py_BuildValue("ddddd", | 
| Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 4667 | (double)t.tms_utime / ticks_per_second, | 
|  | 4668 | (double)t.tms_stime / ticks_per_second, | 
|  | 4669 | (double)t.tms_cutime / ticks_per_second, | 
|  | 4670 | (double)t.tms_cstime / ticks_per_second, | 
|  | 4671 | (double)c / ticks_per_second); | 
| Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4672 | } | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4673 | #endif /* not OS2 */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4674 | #endif /* HAVE_TIMES */ | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4675 |  | 
|  | 4676 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4677 | #ifdef MS_WINDOWS | 
| Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4678 | #define HAVE_TIMES	/* so the method table will pick it up */ | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4679 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4680 | posix_times(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4681 | { | 
|  | 4682 | FILETIME create, exit, kernel, user; | 
|  | 4683 | HANDLE hProc; | 
| Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4684 | hProc = GetCurrentProcess(); | 
| Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4685 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); | 
|  | 4686 | /* The fields of a FILETIME structure are the hi and lo part | 
|  | 4687 | of a 64-bit value expressed in 100 nanosecond units. | 
|  | 4688 | 1e7 is one second in such units; 1e-7 the inverse. | 
|  | 4689 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. | 
|  | 4690 | */ | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4691 | return Py_BuildValue( | 
|  | 4692 | "ddddd", | 
| Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4693 | (double)(user.dwHighDateTime*429.4967296 + | 
|  | 4694 | user.dwLowDateTime*1e-7), | 
| Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 4695 | (double)(kernel.dwHighDateTime*429.4967296 + | 
|  | 4696 | kernel.dwLowDateTime*1e-7), | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4697 | (double)0, | 
|  | 4698 | (double)0, | 
|  | 4699 | (double)0); | 
| Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4700 | } | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4701 | #endif /* MS_WINDOWS */ | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4702 |  | 
|  | 4703 | #ifdef HAVE_TIMES | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4704 | PyDoc_STRVAR(posix_times__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4705 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4706 | Return a tuple of floating point numbers indicating process times."); | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4707 | #endif | 
| Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4708 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4709 |  | 
| Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4710 | #ifdef HAVE_GETSID | 
|  | 4711 | PyDoc_STRVAR(posix_getsid__doc__, | 
|  | 4712 | "getsid(pid) -> sid\n\n\ | 
|  | 4713 | Call the system call getsid()."); | 
|  | 4714 |  | 
|  | 4715 | static PyObject * | 
|  | 4716 | posix_getsid(PyObject *self, PyObject *args) | 
|  | 4717 | { | 
| Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4718 | pid_t pid; | 
|  | 4719 | int sid; | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4720 | if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid)) | 
| Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4721 | return NULL; | 
|  | 4722 | sid = getsid(pid); | 
|  | 4723 | if (sid < 0) | 
|  | 4724 | return posix_error(); | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4725 | return PyLong_FromLong((long)sid); | 
| Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4726 | } | 
|  | 4727 | #endif /* HAVE_GETSID */ | 
|  | 4728 |  | 
|  | 4729 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4730 | #ifdef HAVE_SETSID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4731 | PyDoc_STRVAR(posix_setsid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4732 | "setsid()\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4733 | Call the system call setsid()."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4734 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4735 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4736 | posix_setsid(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4737 | { | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4738 | if (setsid() < 0) | 
|  | 4739 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4740 | Py_INCREF(Py_None); | 
|  | 4741 | return Py_None; | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4742 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4743 | #endif /* HAVE_SETSID */ | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4744 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4745 | #ifdef HAVE_SETPGID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4746 | PyDoc_STRVAR(posix_setpgid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4747 | "setpgid(pid, pgrp)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4748 | Call the system call setpgid()."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4749 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4750 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4751 | posix_setpgid(PyObject *self, PyObject *args) | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4752 | { | 
| Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4753 | pid_t pid; | 
|  | 4754 | int pgrp; | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4755 | if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp)) | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4756 | return NULL; | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4757 | if (setpgid(pid, pgrp) < 0) | 
|  | 4758 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4759 | Py_INCREF(Py_None); | 
|  | 4760 | return Py_None; | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4761 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4762 | #endif /* HAVE_SETPGID */ | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4763 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4764 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4765 | #ifdef HAVE_TCGETPGRP | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4766 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4767 | "tcgetpgrp(fd) -> pgid\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4768 | Return the process group associated with the terminal given by a fd."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4769 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4770 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4771 | posix_tcgetpgrp(PyObject *self, PyObject *args) | 
| Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4772 | { | 
| Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 4773 | int fd; | 
|  | 4774 | pid_t pgid; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4775 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) | 
| Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4776 | return NULL; | 
|  | 4777 | pgid = tcgetpgrp(fd); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4778 | if (pgid < 0) | 
|  | 4779 | return posix_error(); | 
| Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4780 | return PyLong_FromPid(pgid); | 
| Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4781 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4782 | #endif /* HAVE_TCGETPGRP */ | 
| Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4783 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4784 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4785 | #ifdef HAVE_TCSETPGRP | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4786 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4787 | "tcsetpgrp(fd, pgid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4788 | Set the process group associated with the terminal given by a fd."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4789 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4790 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4791 | posix_tcsetpgrp(PyObject *self, PyObject *args) | 
| Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4792 | { | 
| Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4793 | int fd; | 
|  | 4794 | pid_t pgid; | 
|  | 4795 | if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid)) | 
| Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4796 | return NULL; | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4797 | if (tcsetpgrp(fd, pgid) < 0) | 
|  | 4798 | return posix_error(); | 
| Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4799 | Py_INCREF(Py_None); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4800 | return Py_None; | 
| Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4801 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4802 | #endif /* HAVE_TCSETPGRP */ | 
| Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4803 |  | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4804 | /* Functions acting on file descriptors */ | 
|  | 4805 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4806 | PyDoc_STRVAR(posix_open__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4807 | "open(filename, flag [, mode=0777]) -> fd\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4808 | Open a file (for low level IO)."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4809 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4810 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4811 | posix_open(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4812 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4813 | PyObject *ofile; | 
|  | 4814 | char *file; | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4815 | int flag; | 
|  | 4816 | int mode = 0777; | 
|  | 4817 | int fd; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4818 |  | 
|  | 4819 | #ifdef MS_WINDOWS | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 4820 | PyUnicodeObject *po; | 
|  | 4821 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { | 
|  | 4822 | Py_BEGIN_ALLOW_THREADS | 
|  | 4823 | /* PyUnicode_AS_UNICODE OK without thread | 
|  | 4824 | lock as it is a simple dereference. */ | 
|  | 4825 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); | 
|  | 4826 | Py_END_ALLOW_THREADS | 
|  | 4827 | if (fd < 0) | 
|  | 4828 | return posix_error(); | 
|  | 4829 | return PyLong_FromLong((long)fd); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4830 | } | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 4831 | /* Drop the argument parsing error as narrow strings | 
|  | 4832 | are also valid. */ | 
|  | 4833 | PyErr_Clear(); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4834 | #endif | 
|  | 4835 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4836 | if (!PyArg_ParseTuple(args, "O&i|i", | 
|  | 4837 | PyUnicode_FSConverter, &ofile, | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4838 | &flag, &mode)) | 
| Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4839 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4840 | file = bytes2str(ofile, 1); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4841 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4842 | fd = open(file, flag, mode); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4843 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4844 | if (fd < 0) | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4845 | return posix_error_with_allocated_filename(ofile); | 
|  | 4846 | release_bytes(ofile); | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4847 | return PyLong_FromLong((long)fd); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4848 | } | 
|  | 4849 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4850 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4851 | PyDoc_STRVAR(posix_close__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4852 | "close(fd)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4853 | Close a file descriptor (for low level IO)."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4854 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4855 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4856 | posix_close(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4857 | { | 
|  | 4858 | int fd, res; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4859 | if (!PyArg_ParseTuple(args, "i:close", &fd)) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4860 | return NULL; | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4861 | if (!_PyVerify_fd(fd)) | 
|  | 4862 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4863 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4864 | res = close(fd); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4865 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4866 | if (res < 0) | 
|  | 4867 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4868 | Py_INCREF(Py_None); | 
|  | 4869 | return Py_None; | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4870 | } | 
|  | 4871 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4872 |  | 
| Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 4873 | PyDoc_STRVAR(posix_closerange__doc__, | 
|  | 4874 | "closerange(fd_low, fd_high)\n\n\ | 
|  | 4875 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); | 
|  | 4876 |  | 
|  | 4877 | static PyObject * | 
|  | 4878 | posix_closerange(PyObject *self, PyObject *args) | 
|  | 4879 | { | 
|  | 4880 | int fd_from, fd_to, i; | 
|  | 4881 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) | 
|  | 4882 | return NULL; | 
|  | 4883 | Py_BEGIN_ALLOW_THREADS | 
|  | 4884 | for (i = fd_from; i < fd_to; i++) | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4885 | if (_PyVerify_fd(i)) | 
|  | 4886 | close(i); | 
| Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 4887 | Py_END_ALLOW_THREADS | 
|  | 4888 | Py_RETURN_NONE; | 
|  | 4889 | } | 
|  | 4890 |  | 
|  | 4891 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4892 | PyDoc_STRVAR(posix_dup__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4893 | "dup(fd) -> fd2\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4894 | Return a duplicate of a file descriptor."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4895 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4896 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4897 | posix_dup(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4898 | { | 
|  | 4899 | int fd; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4900 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4901 | return NULL; | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4902 | if (!_PyVerify_fd(fd)) | 
|  | 4903 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4904 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4905 | fd = dup(fd); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4906 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4907 | if (fd < 0) | 
|  | 4908 | return posix_error(); | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4909 | return PyLong_FromLong((long)fd); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4910 | } | 
|  | 4911 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4912 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4913 | PyDoc_STRVAR(posix_dup2__doc__, | 
| Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 4914 | "dup2(old_fd, new_fd)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4915 | Duplicate file descriptor."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4916 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4917 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4918 | posix_dup2(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4919 | { | 
|  | 4920 | int fd, fd2, res; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4921 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4922 | return NULL; | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4923 | if (!_PyVerify_fd_dup2(fd, fd2)) | 
|  | 4924 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4925 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4926 | res = dup2(fd, fd2); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4927 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4928 | if (res < 0) | 
|  | 4929 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4930 | Py_INCREF(Py_None); | 
|  | 4931 | return Py_None; | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4932 | } | 
|  | 4933 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4934 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4935 | PyDoc_STRVAR(posix_lseek__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4936 | "lseek(fd, pos, how) -> newpos\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4937 | Set the current position of a file descriptor."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4938 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4939 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4940 | posix_lseek(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4941 | { | 
|  | 4942 | int fd, how; | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4943 | #if defined(MS_WIN64) || defined(MS_WINDOWS) | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 4944 | PY_LONG_LONG pos, res; | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4945 | #else | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4946 | off_t pos, res; | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4947 | #endif | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4948 | PyObject *posobj; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4949 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4950 | return NULL; | 
|  | 4951 | #ifdef SEEK_SET | 
|  | 4952 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ | 
|  | 4953 | switch (how) { | 
|  | 4954 | case 0: how = SEEK_SET; break; | 
|  | 4955 | case 1: how = SEEK_CUR; break; | 
|  | 4956 | case 2: how = SEEK_END; break; | 
|  | 4957 | } | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4958 | #endif /* SEEK_END */ | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4959 |  | 
|  | 4960 | #if !defined(HAVE_LARGEFILE_SUPPORT) | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4961 | pos = PyLong_AsLong(posobj); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4962 | #else | 
|  | 4963 | pos = PyLong_Check(posobj) ? | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4964 | PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4965 | #endif | 
|  | 4966 | if (PyErr_Occurred()) | 
|  | 4967 | return NULL; | 
|  | 4968 |  | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4969 | if (!_PyVerify_fd(fd)) | 
|  | 4970 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4971 | Py_BEGIN_ALLOW_THREADS | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4972 | #if defined(MS_WIN64) || defined(MS_WINDOWS) | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4973 | res = _lseeki64(fd, pos, how); | 
|  | 4974 | #else | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4975 | res = lseek(fd, pos, how); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4976 | #endif | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4977 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4978 | if (res < 0) | 
|  | 4979 | return posix_error(); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4980 |  | 
|  | 4981 | #if !defined(HAVE_LARGEFILE_SUPPORT) | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4982 | return PyLong_FromLong(res); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4983 | #else | 
|  | 4984 | return PyLong_FromLongLong(res); | 
|  | 4985 | #endif | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4986 | } | 
|  | 4987 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4988 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4989 | PyDoc_STRVAR(posix_read__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4990 | "read(fd, buffersize) -> string\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4991 | Read a file descriptor."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4992 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4993 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4994 | posix_read(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4995 | { | 
| Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 4996 | int fd, size; | 
|  | 4997 | Py_ssize_t n; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4998 | PyObject *buffer; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4999 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5000 | return NULL; | 
| Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 5001 | if (size < 0) { | 
|  | 5002 | errno = EINVAL; | 
|  | 5003 | return posix_error(); | 
|  | 5004 | } | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5005 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5006 | if (buffer == NULL) | 
|  | 5007 | return NULL; | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5008 | if (!_PyVerify_fd(fd)) | 
|  | 5009 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5010 | Py_BEGIN_ALLOW_THREADS | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5011 | n = read(fd, PyBytes_AS_STRING(buffer), size); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5012 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5013 | if (n < 0) { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5014 | Py_DECREF(buffer); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5015 | return posix_error(); | 
|  | 5016 | } | 
| Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5017 | if (n != size) | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5018 | _PyBytes_Resize(&buffer, n); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5019 | return buffer; | 
|  | 5020 | } | 
|  | 5021 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5022 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5023 | PyDoc_STRVAR(posix_write__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5024 | "write(fd, string) -> byteswritten\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5025 | Write a string to a file descriptor."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5026 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5027 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5028 | posix_write(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5029 | { | 
| Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5030 | Py_buffer pbuf; | 
| Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5031 | int fd; | 
|  | 5032 | Py_ssize_t size; | 
| Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5033 |  | 
| Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 5034 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5035 | return NULL; | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5036 | if (!_PyVerify_fd(fd)) | 
|  | 5037 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5038 | Py_BEGIN_ALLOW_THREADS | 
| Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5039 | size = write(fd, pbuf.buf, (size_t)pbuf.len); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5040 | Py_END_ALLOW_THREADS | 
| Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5041 | PyBuffer_Release(&pbuf); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5042 | if (size < 0) | 
|  | 5043 | return posix_error(); | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5044 | return PyLong_FromSsize_t(size); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5045 | } | 
|  | 5046 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5047 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5048 | PyDoc_STRVAR(posix_fstat__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5049 | "fstat(fd) -> stat result\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5050 | Like stat(), but for an open file descriptor."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5051 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5052 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5053 | posix_fstat(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5054 | { | 
|  | 5055 | int fd; | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5056 | STRUCT_STAT st; | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5057 | int res; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5058 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5059 | return NULL; | 
| Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5060 | #ifdef __VMS | 
|  | 5061 | /* on OpenVMS we must ensure that all bytes are written to the file */ | 
|  | 5062 | fsync(fd); | 
|  | 5063 | #endif | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5064 | if (!_PyVerify_fd(fd)) | 
|  | 5065 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5066 | Py_BEGIN_ALLOW_THREADS | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5067 | res = FSTAT(fd, &st); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5068 | Py_END_ALLOW_THREADS | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5069 | if (res != 0) { | 
|  | 5070 | #ifdef MS_WINDOWS | 
|  | 5071 | return win32_error("fstat", NULL); | 
|  | 5072 | #else | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5073 | return posix_error(); | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5074 | #endif | 
|  | 5075 | } | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5076 |  | 
| Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5077 | return _pystat_fromstructstat(&st); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5078 | } | 
|  | 5079 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5080 | PyDoc_STRVAR(posix_isatty__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5081 | "isatty(fd) -> bool\n\n\ | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5082 | Return True if the file descriptor 'fd' is an open file descriptor\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5083 | connected to the slave end of a terminal."); | 
| Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5084 |  | 
|  | 5085 | static PyObject * | 
| Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5086 | posix_isatty(PyObject *self, PyObject *args) | 
| Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5087 | { | 
|  | 5088 | int fd; | 
|  | 5089 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) | 
|  | 5090 | return NULL; | 
| Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5091 | if (!_PyVerify_fd(fd)) | 
|  | 5092 | return PyBool_FromLong(0); | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5093 | return PyBool_FromLong(isatty(fd)); | 
| Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5094 | } | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5095 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5096 | #ifdef HAVE_PIPE | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5097 | PyDoc_STRVAR(posix_pipe__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5098 | "pipe() -> (read_end, write_end)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5099 | Create a pipe."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5100 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5101 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5102 | posix_pipe(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5103 | { | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5104 | #if defined(PYOS_OS2) | 
|  | 5105 | HFILE read, write; | 
|  | 5106 | APIRET rc; | 
|  | 5107 |  | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5108 | Py_BEGIN_ALLOW_THREADS | 
|  | 5109 | rc = DosCreatePipe( &read, &write, 4096); | 
|  | 5110 | Py_END_ALLOW_THREADS | 
|  | 5111 | if (rc != NO_ERROR) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5112 | return os2_error(rc); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5113 |  | 
|  | 5114 | return Py_BuildValue("(ii)", read, write); | 
|  | 5115 | #else | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5116 | #if !defined(MS_WINDOWS) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5117 | int fds[2]; | 
|  | 5118 | int res; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5119 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5120 | res = pipe(fds); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5121 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5122 | if (res != 0) | 
|  | 5123 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5124 | return Py_BuildValue("(ii)", fds[0], fds[1]); | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5125 | #else /* MS_WINDOWS */ | 
| Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5126 | HANDLE read, write; | 
| Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5127 | int read_fd, write_fd; | 
| Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5128 | BOOL ok; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5129 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5130 | ok = CreatePipe(&read, &write, NULL, 0); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5131 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5132 | if (!ok) | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5133 | return win32_error("CreatePipe", NULL); | 
| Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5134 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); | 
|  | 5135 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); | 
| Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5136 | return Py_BuildValue("(ii)", read_fd, write_fd); | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5137 | #endif /* MS_WINDOWS */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5138 | #endif | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5139 | } | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5140 | #endif  /* HAVE_PIPE */ | 
|  | 5141 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5142 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5143 | #ifdef HAVE_MKFIFO | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5144 | PyDoc_STRVAR(posix_mkfifo__doc__, | 
| Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5145 | "mkfifo(filename [, mode=0666])\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5146 | Create a FIFO (a POSIX named pipe)."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5147 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5148 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5149 | posix_mkfifo(PyObject *self, PyObject *args) | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5150 | { | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5151 | char *filename; | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5152 | int mode = 0666; | 
|  | 5153 | int res; | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5154 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5155 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5156 | Py_BEGIN_ALLOW_THREADS | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5157 | res = mkfifo(filename, mode); | 
|  | 5158 | Py_END_ALLOW_THREADS | 
|  | 5159 | if (res < 0) | 
|  | 5160 | return posix_error(); | 
|  | 5161 | Py_INCREF(Py_None); | 
|  | 5162 | return Py_None; | 
|  | 5163 | } | 
|  | 5164 | #endif | 
|  | 5165 |  | 
|  | 5166 |  | 
| Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5167 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5168 | PyDoc_STRVAR(posix_mknod__doc__, | 
| Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5169 | "mknod(filename [, mode=0600, device])\n\n\ | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5170 | Create a filesystem node (file, device special file or named pipe)\n\ | 
|  | 5171 | named filename. mode specifies both the permissions to use and the\n\ | 
|  | 5172 | type of node to be created, being combined (bitwise OR) with one of\n\ | 
|  | 5173 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\ | 
| Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5174 | device defines the newly created device special file (probably using\n\ | 
|  | 5175 | os.makedev()), otherwise it is ignored."); | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5176 |  | 
|  | 5177 |  | 
|  | 5178 | static PyObject * | 
|  | 5179 | posix_mknod(PyObject *self, PyObject *args) | 
|  | 5180 | { | 
|  | 5181 | char *filename; | 
|  | 5182 | int mode = 0600; | 
| Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5183 | int device = 0; | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5184 | int res; | 
| Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5185 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5186 | return NULL; | 
|  | 5187 | Py_BEGIN_ALLOW_THREADS | 
| Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5188 | res = mknod(filename, mode, device); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5189 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5190 | if (res < 0) | 
|  | 5191 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5192 | Py_INCREF(Py_None); | 
|  | 5193 | return Py_None; | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5194 | } | 
|  | 5195 | #endif | 
|  | 5196 |  | 
| Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5197 | #ifdef HAVE_DEVICE_MACROS | 
|  | 5198 | PyDoc_STRVAR(posix_major__doc__, | 
|  | 5199 | "major(device) -> major number\n\ | 
|  | 5200 | Extracts a device major number from a raw device number."); | 
|  | 5201 |  | 
|  | 5202 | static PyObject * | 
|  | 5203 | posix_major(PyObject *self, PyObject *args) | 
|  | 5204 | { | 
|  | 5205 | int device; | 
|  | 5206 | if (!PyArg_ParseTuple(args, "i:major", &device)) | 
|  | 5207 | return NULL; | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5208 | return PyLong_FromLong((long)major(device)); | 
| Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5209 | } | 
|  | 5210 |  | 
|  | 5211 | PyDoc_STRVAR(posix_minor__doc__, | 
|  | 5212 | "minor(device) -> minor number\n\ | 
|  | 5213 | Extracts a device minor number from a raw device number."); | 
|  | 5214 |  | 
|  | 5215 | static PyObject * | 
|  | 5216 | posix_minor(PyObject *self, PyObject *args) | 
|  | 5217 | { | 
|  | 5218 | int device; | 
|  | 5219 | if (!PyArg_ParseTuple(args, "i:minor", &device)) | 
|  | 5220 | return NULL; | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5221 | return PyLong_FromLong((long)minor(device)); | 
| Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5222 | } | 
|  | 5223 |  | 
|  | 5224 | PyDoc_STRVAR(posix_makedev__doc__, | 
|  | 5225 | "makedev(major, minor) -> device number\n\ | 
|  | 5226 | Composes a raw device number from the major and minor device numbers."); | 
|  | 5227 |  | 
|  | 5228 | static PyObject * | 
|  | 5229 | posix_makedev(PyObject *self, PyObject *args) | 
|  | 5230 | { | 
|  | 5231 | int major, minor; | 
|  | 5232 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) | 
|  | 5233 | return NULL; | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5234 | return PyLong_FromLong((long)makedev(major, minor)); | 
| Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5235 | } | 
|  | 5236 | #endif /* device macros */ | 
|  | 5237 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5238 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5239 | #ifdef HAVE_FTRUNCATE | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5240 | PyDoc_STRVAR(posix_ftruncate__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5241 | "ftruncate(fd, length)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5242 | Truncate a file to a specified length."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5243 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5244 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5245 | posix_ftruncate(PyObject *self, PyObject *args) | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5246 | { | 
|  | 5247 | int fd; | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5248 | off_t length; | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5249 | int res; | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5250 | PyObject *lenobj; | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5251 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5252 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5253 | return NULL; | 
|  | 5254 |  | 
|  | 5255 | #if !defined(HAVE_LARGEFILE_SUPPORT) | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5256 | length = PyLong_AsLong(lenobj); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5257 | #else | 
|  | 5258 | length = PyLong_Check(lenobj) ? | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5259 | PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5260 | #endif | 
|  | 5261 | if (PyErr_Occurred()) | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5262 | return NULL; | 
|  | 5263 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5264 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5265 | res = ftruncate(fd, length); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5266 | Py_END_ALLOW_THREADS | 
| Benjamin Peterson | 9053d75 | 2009-01-19 17:53:36 +0000 | [diff] [blame] | 5267 | if (res < 0) | 
|  | 5268 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5269 | Py_INCREF(Py_None); | 
|  | 5270 | return Py_None; | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5271 | } | 
|  | 5272 | #endif | 
| Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5273 |  | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5274 | #ifdef HAVE_PUTENV | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5275 | PyDoc_STRVAR(posix_putenv__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5276 | "putenv(key, value)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5277 | Change or add an environment variable."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5278 |  | 
| Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5279 | /* Save putenv() parameters as values here, so we can collect them when they | 
|  | 5280 | * get re-set with another call for the same key. */ | 
|  | 5281 | static PyObject *posix_putenv_garbage; | 
|  | 5282 |  | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5283 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5284 | posix_putenv(PyObject *self, PyObject *args) | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5285 | { | 
| Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5286 | #ifdef MS_WINDOWS | 
|  | 5287 | wchar_t *s1, *s2; | 
|  | 5288 | wchar_t *newenv; | 
|  | 5289 | #else | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5290 | PyObject *os1, *os2; | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5291 | char *s1, *s2; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5292 | char *newenv; | 
| Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5293 | #endif | 
| Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5294 | PyObject *newstr; | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5295 | size_t len; | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5296 |  | 
| Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5297 | #ifdef MS_WINDOWS | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5298 | if (!PyArg_ParseTuple(args, | 
| Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5299 | "uu:putenv", | 
| Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5300 | &s1, &s2)) | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5301 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5302 | #else | 
|  | 5303 | if (!PyArg_ParseTuple(args, | 
|  | 5304 | "O&O&:putenv", | 
|  | 5305 | PyUnicode_FSConverter, &os1, | 
|  | 5306 | PyUnicode_FSConverter, &os2)) | 
|  | 5307 | return NULL; | 
|  | 5308 | s1 = bytes2str(os1, 1); | 
|  | 5309 | s2 = bytes2str(os2, 1); | 
|  | 5310 | #endif | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5311 |  | 
|  | 5312 | #if defined(PYOS_OS2) | 
|  | 5313 | if (stricmp(s1, "BEGINLIBPATH") == 0) { | 
|  | 5314 | APIRET rc; | 
|  | 5315 |  | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5316 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); | 
|  | 5317 | if (rc != NO_ERROR) | 
|  | 5318 | return os2_error(rc); | 
|  | 5319 |  | 
|  | 5320 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { | 
|  | 5321 | APIRET rc; | 
|  | 5322 |  | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5323 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); | 
|  | 5324 | if (rc != NO_ERROR) | 
|  | 5325 | return os2_error(rc); | 
|  | 5326 | } else { | 
|  | 5327 | #endif | 
| Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5328 | /* XXX This can leak memory -- not easy to fix :-( */ | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5329 | /* len includes space for a trailing \0; the size arg to | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5330 | PyBytes_FromStringAndSize does not count that */ | 
| Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5331 | #ifdef MS_WINDOWS | 
|  | 5332 | len = wcslen(s1) + wcslen(s2) + 2; | 
|  | 5333 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); | 
|  | 5334 | #else | 
|  | 5335 | len = strlen(s1) + strlen(s2) + 2; | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5336 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); | 
| Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5337 | #endif | 
| Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5338 | if (newstr == NULL) | 
|  | 5339 | return PyErr_NoMemory(); | 
| Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5340 | #ifdef MS_WINDOWS | 
|  | 5341 | newenv = PyUnicode_AsUnicode(newstr); | 
|  | 5342 | _snwprintf(newenv, len, L"%s=%s", s1, s2); | 
|  | 5343 | if (_wputenv(newenv)) { | 
|  | 5344 | Py_DECREF(newstr); | 
|  | 5345 | posix_error(); | 
|  | 5346 | return NULL; | 
|  | 5347 | } | 
|  | 5348 | #else | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5349 | newenv = PyBytes_AS_STRING(newstr); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5350 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); | 
|  | 5351 | if (putenv(newenv)) { | 
| Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5352 | Py_DECREF(newstr); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5353 | release_bytes(os1); | 
|  | 5354 | release_bytes(os2); | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5355 | posix_error(); | 
|  | 5356 | return NULL; | 
|  | 5357 | } | 
| Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5358 | #endif | 
| Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5359 | /* Install the first arg and newstr in posix_putenv_garbage; | 
|  | 5360 | * this will cause previous value to be collected.  This has to | 
|  | 5361 | * happen after the real putenv() call because the old value | 
|  | 5362 | * was still accessible until then. */ | 
|  | 5363 | if (PyDict_SetItem(posix_putenv_garbage, | 
|  | 5364 | PyTuple_GET_ITEM(args, 0), newstr)) { | 
|  | 5365 | /* really not much we can do; just leak */ | 
|  | 5366 | PyErr_Clear(); | 
|  | 5367 | } | 
|  | 5368 | else { | 
|  | 5369 | Py_DECREF(newstr); | 
|  | 5370 | } | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5371 |  | 
|  | 5372 | #if defined(PYOS_OS2) | 
|  | 5373 | } | 
|  | 5374 | #endif | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5375 | #ifndef MS_WINDOWS | 
|  | 5376 | release_bytes(os1); | 
|  | 5377 | release_bytes(os2); | 
|  | 5378 | #endif | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5379 | Py_INCREF(Py_None); | 
|  | 5380 | return Py_None; | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5381 | } | 
| Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5382 | #endif /* putenv */ | 
|  | 5383 |  | 
| Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5384 | #ifdef HAVE_UNSETENV | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5385 | PyDoc_STRVAR(posix_unsetenv__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5386 | "unsetenv(key)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5387 | Delete an environment variable."); | 
| Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5388 |  | 
|  | 5389 | static PyObject * | 
|  | 5390 | posix_unsetenv(PyObject *self, PyObject *args) | 
|  | 5391 | { | 
|  | 5392 | char *s1; | 
|  | 5393 |  | 
|  | 5394 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) | 
|  | 5395 | return NULL; | 
|  | 5396 |  | 
|  | 5397 | unsetenv(s1); | 
|  | 5398 |  | 
|  | 5399 | /* Remove the key from posix_putenv_garbage; | 
|  | 5400 | * this will cause it to be collected.  This has to | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5401 | * happen after the real unsetenv() call because the | 
| Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5402 | * old value was still accessible until then. | 
|  | 5403 | */ | 
|  | 5404 | if (PyDict_DelItem(posix_putenv_garbage, | 
|  | 5405 | PyTuple_GET_ITEM(args, 0))) { | 
|  | 5406 | /* really not much we can do; just leak */ | 
|  | 5407 | PyErr_Clear(); | 
|  | 5408 | } | 
|  | 5409 |  | 
|  | 5410 | Py_INCREF(Py_None); | 
|  | 5411 | return Py_None; | 
|  | 5412 | } | 
|  | 5413 | #endif /* unsetenv */ | 
|  | 5414 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5415 | PyDoc_STRVAR(posix_strerror__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5416 | "strerror(code) -> string\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5417 | Translate an error code to a message string."); | 
| Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5418 |  | 
| Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5419 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5420 | posix_strerror(PyObject *self, PyObject *args) | 
| Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5421 | { | 
|  | 5422 | int code; | 
|  | 5423 | char *message; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5424 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) | 
| Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5425 | return NULL; | 
|  | 5426 | message = strerror(code); | 
|  | 5427 | if (message == NULL) { | 
|  | 5428 | PyErr_SetString(PyExc_ValueError, | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5429 | "strerror() argument out of range"); | 
| Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5430 | return NULL; | 
|  | 5431 | } | 
| Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5432 | return PyUnicode_FromString(message); | 
| Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5433 | } | 
| Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5434 |  | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5435 |  | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5436 | #ifdef HAVE_SYS_WAIT_H | 
|  | 5437 |  | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5438 | #ifdef WCOREDUMP | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5439 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5440 | "WCOREDUMP(status) -> bool\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5441 | Return True if the process returning 'status' was dumped to a core file."); | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5442 |  | 
|  | 5443 | static PyObject * | 
|  | 5444 | posix_WCOREDUMP(PyObject *self, PyObject *args) | 
|  | 5445 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5446 | WAIT_TYPE status; | 
|  | 5447 | WAIT_STATUS_INT(status) = 0; | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5448 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5449 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5450 | return NULL; | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5451 |  | 
|  | 5452 | return PyBool_FromLong(WCOREDUMP(status)); | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5453 | } | 
|  | 5454 | #endif /* WCOREDUMP */ | 
|  | 5455 |  | 
|  | 5456 | #ifdef WIFCONTINUED | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5457 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5458 | "WIFCONTINUED(status) -> bool\n\n\ | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5459 | Return True if the process returning 'status' was continued from a\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5460 | job control stop."); | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5461 |  | 
|  | 5462 | static PyObject * | 
| Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5463 | posix_WIFCONTINUED(PyObject *self, PyObject *args) | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5464 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5465 | WAIT_TYPE status; | 
|  | 5466 | WAIT_STATUS_INT(status) = 0; | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5467 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5468 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5469 | return NULL; | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5470 |  | 
| Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5471 | return PyBool_FromLong(WIFCONTINUED(status)); | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5472 | } | 
|  | 5473 | #endif /* WIFCONTINUED */ | 
|  | 5474 |  | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5475 | #ifdef WIFSTOPPED | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5476 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5477 | "WIFSTOPPED(status) -> bool\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5478 | Return True if the process returning 'status' was stopped."); | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5479 |  | 
|  | 5480 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5481 | posix_WIFSTOPPED(PyObject *self, PyObject *args) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5482 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5483 | WAIT_TYPE status; | 
|  | 5484 | WAIT_STATUS_INT(status) = 0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5485 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5486 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5487 | return NULL; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5488 |  | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5489 | return PyBool_FromLong(WIFSTOPPED(status)); | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5490 | } | 
|  | 5491 | #endif /* WIFSTOPPED */ | 
|  | 5492 |  | 
|  | 5493 | #ifdef WIFSIGNALED | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5494 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5495 | "WIFSIGNALED(status) -> bool\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5496 | Return True if the process returning 'status' was terminated by a signal."); | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5497 |  | 
|  | 5498 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5499 | posix_WIFSIGNALED(PyObject *self, PyObject *args) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5500 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5501 | WAIT_TYPE status; | 
|  | 5502 | WAIT_STATUS_INT(status) = 0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5503 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5504 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5505 | return NULL; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5506 |  | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5507 | return PyBool_FromLong(WIFSIGNALED(status)); | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5508 | } | 
|  | 5509 | #endif /* WIFSIGNALED */ | 
|  | 5510 |  | 
|  | 5511 | #ifdef WIFEXITED | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5512 | PyDoc_STRVAR(posix_WIFEXITED__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5513 | "WIFEXITED(status) -> bool\n\n\ | 
| Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5514 | Return true if the process returning 'status' exited using the exit()\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5515 | system call."); | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5516 |  | 
|  | 5517 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5518 | posix_WIFEXITED(PyObject *self, PyObject *args) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5519 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5520 | WAIT_TYPE status; | 
|  | 5521 | WAIT_STATUS_INT(status) = 0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5522 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5523 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5524 | return NULL; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5525 |  | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5526 | return PyBool_FromLong(WIFEXITED(status)); | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5527 | } | 
|  | 5528 | #endif /* WIFEXITED */ | 
|  | 5529 |  | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5530 | #ifdef WEXITSTATUS | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5531 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5532 | "WEXITSTATUS(status) -> integer\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5533 | Return the process return code from 'status'."); | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5534 |  | 
|  | 5535 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5536 | posix_WEXITSTATUS(PyObject *self, PyObject *args) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5537 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5538 | WAIT_TYPE status; | 
|  | 5539 | WAIT_STATUS_INT(status) = 0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5540 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5541 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5542 | return NULL; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5543 |  | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5544 | return Py_BuildValue("i", WEXITSTATUS(status)); | 
|  | 5545 | } | 
|  | 5546 | #endif /* WEXITSTATUS */ | 
|  | 5547 |  | 
|  | 5548 | #ifdef WTERMSIG | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5549 | PyDoc_STRVAR(posix_WTERMSIG__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5550 | "WTERMSIG(status) -> integer\n\n\ | 
| Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5551 | Return the signal that terminated the process that provided the 'status'\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5552 | value."); | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5553 |  | 
|  | 5554 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5555 | posix_WTERMSIG(PyObject *self, PyObject *args) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5556 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5557 | WAIT_TYPE status; | 
|  | 5558 | WAIT_STATUS_INT(status) = 0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5559 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5560 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5561 | return NULL; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5562 |  | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5563 | return Py_BuildValue("i", WTERMSIG(status)); | 
|  | 5564 | } | 
|  | 5565 | #endif /* WTERMSIG */ | 
|  | 5566 |  | 
|  | 5567 | #ifdef WSTOPSIG | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5568 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5569 | "WSTOPSIG(status) -> integer\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5570 | Return the signal that stopped the process that provided\n\ | 
|  | 5571 | the 'status' value."); | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5572 |  | 
|  | 5573 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5574 | posix_WSTOPSIG(PyObject *self, PyObject *args) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5575 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5576 | WAIT_TYPE status; | 
|  | 5577 | WAIT_STATUS_INT(status) = 0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5578 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5579 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5580 | return NULL; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5581 |  | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5582 | return Py_BuildValue("i", WSTOPSIG(status)); | 
|  | 5583 | } | 
|  | 5584 | #endif /* WSTOPSIG */ | 
|  | 5585 |  | 
|  | 5586 | #endif /* HAVE_SYS_WAIT_H */ | 
|  | 5587 |  | 
|  | 5588 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5589 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) | 
| Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5590 | #ifdef _SCO_DS | 
|  | 5591 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the | 
|  | 5592 | needed definitions in sys/statvfs.h */ | 
|  | 5593 | #define _SVID3 | 
|  | 5594 | #endif | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5595 | #include <sys/statvfs.h> | 
|  | 5596 |  | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5597 | static PyObject* | 
|  | 5598 | _pystatvfs_fromstructstatvfs(struct statvfs st) { | 
|  | 5599 | PyObject *v = PyStructSequence_New(&StatVFSResultType); | 
|  | 5600 | if (v == NULL) | 
|  | 5601 | return NULL; | 
|  | 5602 |  | 
|  | 5603 | #if !defined(HAVE_LARGEFILE_SUPPORT) | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5604 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); | 
|  | 5605 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); | 
|  | 5606 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); | 
|  | 5607 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); | 
|  | 5608 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); | 
|  | 5609 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); | 
|  | 5610 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); | 
|  | 5611 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); | 
|  | 5612 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); | 
|  | 5613 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5614 | #else | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5615 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); | 
|  | 5616 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5617 | PyStructSequence_SET_ITEM(v, 2, | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5618 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5619 | PyStructSequence_SET_ITEM(v, 3, | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5620 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5621 | PyStructSequence_SET_ITEM(v, 4, | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5622 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5623 | PyStructSequence_SET_ITEM(v, 5, | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5624 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5625 | PyStructSequence_SET_ITEM(v, 6, | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5626 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5627 | PyStructSequence_SET_ITEM(v, 7, | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5628 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5629 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); | 
|  | 5630 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5631 | #endif | 
|  | 5632 |  | 
|  | 5633 | return v; | 
|  | 5634 | } | 
|  | 5635 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5636 | PyDoc_STRVAR(posix_fstatvfs__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5637 | "fstatvfs(fd) -> statvfs result\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5638 | Perform an fstatvfs system call on the given fd."); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5639 |  | 
|  | 5640 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5641 | posix_fstatvfs(PyObject *self, PyObject *args) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5642 | { | 
|  | 5643 | int fd, res; | 
|  | 5644 | struct statvfs st; | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5645 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5646 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5647 | return NULL; | 
|  | 5648 | Py_BEGIN_ALLOW_THREADS | 
|  | 5649 | res = fstatvfs(fd, &st); | 
|  | 5650 | Py_END_ALLOW_THREADS | 
|  | 5651 | if (res != 0) | 
|  | 5652 | return posix_error(); | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5653 |  | 
|  | 5654 | return _pystatvfs_fromstructstatvfs(st); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5655 | } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5656 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5657 |  | 
|  | 5658 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5659 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5660 | #include <sys/statvfs.h> | 
|  | 5661 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5662 | PyDoc_STRVAR(posix_statvfs__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5663 | "statvfs(path) -> statvfs result\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5664 | Perform a statvfs system call on the given path."); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5665 |  | 
|  | 5666 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5667 | posix_statvfs(PyObject *self, PyObject *args) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5668 | { | 
|  | 5669 | char *path; | 
|  | 5670 | int res; | 
|  | 5671 | struct statvfs st; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5672 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5673 | return NULL; | 
|  | 5674 | Py_BEGIN_ALLOW_THREADS | 
|  | 5675 | res = statvfs(path, &st); | 
|  | 5676 | Py_END_ALLOW_THREADS | 
|  | 5677 | if (res != 0) | 
|  | 5678 | return posix_error_with_filename(path); | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5679 |  | 
|  | 5680 | return _pystatvfs_fromstructstatvfs(st); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5681 | } | 
|  | 5682 | #endif /* HAVE_STATVFS */ | 
|  | 5683 |  | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5684 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). | 
|  | 5685 | * It maps strings representing configuration variable names to | 
|  | 5686 | * integer values, allowing those functions to be called with the | 
| Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5687 | * magic names instead of polluting the module's namespace with tons of | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5688 | * rarely-used constants.  There are three separate tables that use | 
|  | 5689 | * these definitions. | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5690 | * | 
|  | 5691 | * This code is always included, even if none of the interfaces that | 
|  | 5692 | * need it are included.  The #if hackery needed to avoid it would be | 
|  | 5693 | * sufficiently pervasive that it's not worth the loss of readability. | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5694 | */ | 
|  | 5695 | struct constdef { | 
|  | 5696 | char *name; | 
|  | 5697 | long value; | 
|  | 5698 | }; | 
|  | 5699 |  | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5700 | static int | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5701 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, | 
| Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5702 | size_t tablesize) | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5703 | { | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5704 | if (PyLong_Check(arg)) { | 
|  | 5705 | *valuep = PyLong_AS_LONG(arg); | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5706 | return 1; | 
|  | 5707 | } | 
| Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5708 | else { | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5709 | /* look up the value in the table using a binary search */ | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5710 | size_t lo = 0; | 
| Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5711 | size_t mid; | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5712 | size_t hi = tablesize; | 
|  | 5713 | int cmp; | 
| Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5714 | const char *confname; | 
| Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5715 | if (!PyUnicode_Check(arg)) { | 
| Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5716 | PyErr_SetString(PyExc_TypeError, | 
|  | 5717 | "configuration names must be strings or integers"); | 
|  | 5718 | return 0; | 
|  | 5719 | } | 
| Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 5720 | confname = _PyUnicode_AsString(arg); | 
| Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5721 | if (confname == NULL) | 
|  | 5722 | return 0; | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5723 | while (lo < hi) { | 
|  | 5724 | mid = (lo + hi) / 2; | 
|  | 5725 | cmp = strcmp(confname, table[mid].name); | 
|  | 5726 | if (cmp < 0) | 
|  | 5727 | hi = mid; | 
|  | 5728 | else if (cmp > 0) | 
|  | 5729 | lo = mid + 1; | 
|  | 5730 | else { | 
|  | 5731 | *valuep = table[mid].value; | 
|  | 5732 | return 1; | 
|  | 5733 | } | 
|  | 5734 | } | 
|  | 5735 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); | 
| Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5736 | return 0; | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5737 | } | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5738 | } | 
|  | 5739 |  | 
|  | 5740 |  | 
|  | 5741 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) | 
|  | 5742 | static struct constdef  posix_constants_pathconf[] = { | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5743 | #ifdef _PC_ABI_AIO_XFER_MAX | 
|  | 5744 | {"PC_ABI_AIO_XFER_MAX",	_PC_ABI_AIO_XFER_MAX}, | 
|  | 5745 | #endif | 
|  | 5746 | #ifdef _PC_ABI_ASYNC_IO | 
|  | 5747 | {"PC_ABI_ASYNC_IO",	_PC_ABI_ASYNC_IO}, | 
|  | 5748 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5749 | #ifdef _PC_ASYNC_IO | 
|  | 5750 | {"PC_ASYNC_IO",	_PC_ASYNC_IO}, | 
|  | 5751 | #endif | 
|  | 5752 | #ifdef _PC_CHOWN_RESTRICTED | 
|  | 5753 | {"PC_CHOWN_RESTRICTED",	_PC_CHOWN_RESTRICTED}, | 
|  | 5754 | #endif | 
|  | 5755 | #ifdef _PC_FILESIZEBITS | 
|  | 5756 | {"PC_FILESIZEBITS",	_PC_FILESIZEBITS}, | 
|  | 5757 | #endif | 
|  | 5758 | #ifdef _PC_LAST | 
|  | 5759 | {"PC_LAST",	_PC_LAST}, | 
|  | 5760 | #endif | 
|  | 5761 | #ifdef _PC_LINK_MAX | 
|  | 5762 | {"PC_LINK_MAX",	_PC_LINK_MAX}, | 
|  | 5763 | #endif | 
|  | 5764 | #ifdef _PC_MAX_CANON | 
|  | 5765 | {"PC_MAX_CANON",	_PC_MAX_CANON}, | 
|  | 5766 | #endif | 
|  | 5767 | #ifdef _PC_MAX_INPUT | 
|  | 5768 | {"PC_MAX_INPUT",	_PC_MAX_INPUT}, | 
|  | 5769 | #endif | 
|  | 5770 | #ifdef _PC_NAME_MAX | 
|  | 5771 | {"PC_NAME_MAX",	_PC_NAME_MAX}, | 
|  | 5772 | #endif | 
|  | 5773 | #ifdef _PC_NO_TRUNC | 
|  | 5774 | {"PC_NO_TRUNC",	_PC_NO_TRUNC}, | 
|  | 5775 | #endif | 
|  | 5776 | #ifdef _PC_PATH_MAX | 
|  | 5777 | {"PC_PATH_MAX",	_PC_PATH_MAX}, | 
|  | 5778 | #endif | 
|  | 5779 | #ifdef _PC_PIPE_BUF | 
|  | 5780 | {"PC_PIPE_BUF",	_PC_PIPE_BUF}, | 
|  | 5781 | #endif | 
|  | 5782 | #ifdef _PC_PRIO_IO | 
|  | 5783 | {"PC_PRIO_IO",	_PC_PRIO_IO}, | 
|  | 5784 | #endif | 
|  | 5785 | #ifdef _PC_SOCK_MAXBUF | 
|  | 5786 | {"PC_SOCK_MAXBUF",	_PC_SOCK_MAXBUF}, | 
|  | 5787 | #endif | 
|  | 5788 | #ifdef _PC_SYNC_IO | 
|  | 5789 | {"PC_SYNC_IO",	_PC_SYNC_IO}, | 
|  | 5790 | #endif | 
|  | 5791 | #ifdef _PC_VDISABLE | 
|  | 5792 | {"PC_VDISABLE",	_PC_VDISABLE}, | 
|  | 5793 | #endif | 
|  | 5794 | }; | 
|  | 5795 |  | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5796 | static int | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5797 | conv_path_confname(PyObject *arg, int *valuep) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5798 | { | 
|  | 5799 | return conv_confname(arg, valuep, posix_constants_pathconf, | 
|  | 5800 | sizeof(posix_constants_pathconf) | 
|  | 5801 | / sizeof(struct constdef)); | 
|  | 5802 | } | 
|  | 5803 | #endif | 
|  | 5804 |  | 
|  | 5805 | #ifdef HAVE_FPATHCONF | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5806 | PyDoc_STRVAR(posix_fpathconf__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5807 | "fpathconf(fd, name) -> integer\n\n\ | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5808 | Return the configuration limit name for the file descriptor fd.\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5809 | If there is no limit, return -1."); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5810 |  | 
|  | 5811 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5812 | posix_fpathconf(PyObject *self, PyObject *args) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5813 | { | 
|  | 5814 | PyObject *result = NULL; | 
|  | 5815 | int name, fd; | 
|  | 5816 |  | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5817 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, | 
|  | 5818 | conv_path_confname, &name)) { | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5819 | long limit; | 
|  | 5820 |  | 
|  | 5821 | errno = 0; | 
|  | 5822 | limit = fpathconf(fd, name); | 
|  | 5823 | if (limit == -1 && errno != 0) | 
|  | 5824 | posix_error(); | 
|  | 5825 | else | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5826 | result = PyLong_FromLong(limit); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5827 | } | 
|  | 5828 | return result; | 
|  | 5829 | } | 
|  | 5830 | #endif | 
|  | 5831 |  | 
|  | 5832 |  | 
|  | 5833 | #ifdef HAVE_PATHCONF | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5834 | PyDoc_STRVAR(posix_pathconf__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5835 | "pathconf(path, name) -> integer\n\n\ | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5836 | Return the configuration limit name for the file or directory path.\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5837 | If there is no limit, return -1."); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5838 |  | 
|  | 5839 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5840 | posix_pathconf(PyObject *self, PyObject *args) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5841 | { | 
|  | 5842 | PyObject *result = NULL; | 
|  | 5843 | int name; | 
|  | 5844 | char *path; | 
|  | 5845 |  | 
|  | 5846 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, | 
|  | 5847 | conv_path_confname, &name)) { | 
|  | 5848 | long limit; | 
|  | 5849 |  | 
|  | 5850 | errno = 0; | 
|  | 5851 | limit = pathconf(path, name); | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5852 | if (limit == -1 && errno != 0) { | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5853 | if (errno == EINVAL) | 
|  | 5854 | /* could be a path or name problem */ | 
|  | 5855 | posix_error(); | 
|  | 5856 | else | 
|  | 5857 | posix_error_with_filename(path); | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5858 | } | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5859 | else | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5860 | result = PyLong_FromLong(limit); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5861 | } | 
|  | 5862 | return result; | 
|  | 5863 | } | 
|  | 5864 | #endif | 
|  | 5865 |  | 
|  | 5866 | #ifdef HAVE_CONFSTR | 
|  | 5867 | static struct constdef posix_constants_confstr[] = { | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5868 | #ifdef _CS_ARCHITECTURE | 
|  | 5869 | {"CS_ARCHITECTURE",	_CS_ARCHITECTURE}, | 
|  | 5870 | #endif | 
|  | 5871 | #ifdef _CS_HOSTNAME | 
|  | 5872 | {"CS_HOSTNAME",	_CS_HOSTNAME}, | 
|  | 5873 | #endif | 
|  | 5874 | #ifdef _CS_HW_PROVIDER | 
|  | 5875 | {"CS_HW_PROVIDER",	_CS_HW_PROVIDER}, | 
|  | 5876 | #endif | 
|  | 5877 | #ifdef _CS_HW_SERIAL | 
|  | 5878 | {"CS_HW_SERIAL",	_CS_HW_SERIAL}, | 
|  | 5879 | #endif | 
|  | 5880 | #ifdef _CS_INITTAB_NAME | 
|  | 5881 | {"CS_INITTAB_NAME",	_CS_INITTAB_NAME}, | 
|  | 5882 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5883 | #ifdef _CS_LFS64_CFLAGS | 
|  | 5884 | {"CS_LFS64_CFLAGS",	_CS_LFS64_CFLAGS}, | 
|  | 5885 | #endif | 
|  | 5886 | #ifdef _CS_LFS64_LDFLAGS | 
|  | 5887 | {"CS_LFS64_LDFLAGS",	_CS_LFS64_LDFLAGS}, | 
|  | 5888 | #endif | 
|  | 5889 | #ifdef _CS_LFS64_LIBS | 
|  | 5890 | {"CS_LFS64_LIBS",	_CS_LFS64_LIBS}, | 
|  | 5891 | #endif | 
|  | 5892 | #ifdef _CS_LFS64_LINTFLAGS | 
|  | 5893 | {"CS_LFS64_LINTFLAGS",	_CS_LFS64_LINTFLAGS}, | 
|  | 5894 | #endif | 
|  | 5895 | #ifdef _CS_LFS_CFLAGS | 
|  | 5896 | {"CS_LFS_CFLAGS",	_CS_LFS_CFLAGS}, | 
|  | 5897 | #endif | 
|  | 5898 | #ifdef _CS_LFS_LDFLAGS | 
|  | 5899 | {"CS_LFS_LDFLAGS",	_CS_LFS_LDFLAGS}, | 
|  | 5900 | #endif | 
|  | 5901 | #ifdef _CS_LFS_LIBS | 
|  | 5902 | {"CS_LFS_LIBS",	_CS_LFS_LIBS}, | 
|  | 5903 | #endif | 
|  | 5904 | #ifdef _CS_LFS_LINTFLAGS | 
|  | 5905 | {"CS_LFS_LINTFLAGS",	_CS_LFS_LINTFLAGS}, | 
|  | 5906 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5907 | #ifdef _CS_MACHINE | 
|  | 5908 | {"CS_MACHINE",	_CS_MACHINE}, | 
|  | 5909 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5910 | #ifdef _CS_PATH | 
|  | 5911 | {"CS_PATH",	_CS_PATH}, | 
|  | 5912 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5913 | #ifdef _CS_RELEASE | 
|  | 5914 | {"CS_RELEASE",	_CS_RELEASE}, | 
|  | 5915 | #endif | 
|  | 5916 | #ifdef _CS_SRPC_DOMAIN | 
|  | 5917 | {"CS_SRPC_DOMAIN",	_CS_SRPC_DOMAIN}, | 
|  | 5918 | #endif | 
|  | 5919 | #ifdef _CS_SYSNAME | 
|  | 5920 | {"CS_SYSNAME",	_CS_SYSNAME}, | 
|  | 5921 | #endif | 
|  | 5922 | #ifdef _CS_VERSION | 
|  | 5923 | {"CS_VERSION",	_CS_VERSION}, | 
|  | 5924 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5925 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS | 
|  | 5926 | {"CS_XBS5_ILP32_OFF32_CFLAGS",	_CS_XBS5_ILP32_OFF32_CFLAGS}, | 
|  | 5927 | #endif | 
|  | 5928 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS | 
|  | 5929 | {"CS_XBS5_ILP32_OFF32_LDFLAGS",	_CS_XBS5_ILP32_OFF32_LDFLAGS}, | 
|  | 5930 | #endif | 
|  | 5931 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS | 
|  | 5932 | {"CS_XBS5_ILP32_OFF32_LIBS",	_CS_XBS5_ILP32_OFF32_LIBS}, | 
|  | 5933 | #endif | 
|  | 5934 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS | 
|  | 5935 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS",	_CS_XBS5_ILP32_OFF32_LINTFLAGS}, | 
|  | 5936 | #endif | 
|  | 5937 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS | 
|  | 5938 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS",	_CS_XBS5_ILP32_OFFBIG_CFLAGS}, | 
|  | 5939 | #endif | 
|  | 5940 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS | 
|  | 5941 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS",	_CS_XBS5_ILP32_OFFBIG_LDFLAGS}, | 
|  | 5942 | #endif | 
|  | 5943 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS | 
|  | 5944 | {"CS_XBS5_ILP32_OFFBIG_LIBS",	_CS_XBS5_ILP32_OFFBIG_LIBS}, | 
|  | 5945 | #endif | 
|  | 5946 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS | 
|  | 5947 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS",	_CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, | 
|  | 5948 | #endif | 
|  | 5949 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS | 
|  | 5950 | {"CS_XBS5_LP64_OFF64_CFLAGS",	_CS_XBS5_LP64_OFF64_CFLAGS}, | 
|  | 5951 | #endif | 
|  | 5952 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS | 
|  | 5953 | {"CS_XBS5_LP64_OFF64_LDFLAGS",	_CS_XBS5_LP64_OFF64_LDFLAGS}, | 
|  | 5954 | #endif | 
|  | 5955 | #ifdef _CS_XBS5_LP64_OFF64_LIBS | 
|  | 5956 | {"CS_XBS5_LP64_OFF64_LIBS",	_CS_XBS5_LP64_OFF64_LIBS}, | 
|  | 5957 | #endif | 
|  | 5958 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS | 
|  | 5959 | {"CS_XBS5_LP64_OFF64_LINTFLAGS",	_CS_XBS5_LP64_OFF64_LINTFLAGS}, | 
|  | 5960 | #endif | 
|  | 5961 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS | 
|  | 5962 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS",	_CS_XBS5_LPBIG_OFFBIG_CFLAGS}, | 
|  | 5963 | #endif | 
|  | 5964 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS | 
|  | 5965 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS",	_CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, | 
|  | 5966 | #endif | 
|  | 5967 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS | 
|  | 5968 | {"CS_XBS5_LPBIG_OFFBIG_LIBS",	_CS_XBS5_LPBIG_OFFBIG_LIBS}, | 
|  | 5969 | #endif | 
|  | 5970 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS | 
|  | 5971 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS",	_CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, | 
|  | 5972 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5973 | #ifdef _MIPS_CS_AVAIL_PROCESSORS | 
|  | 5974 | {"MIPS_CS_AVAIL_PROCESSORS",	_MIPS_CS_AVAIL_PROCESSORS}, | 
|  | 5975 | #endif | 
|  | 5976 | #ifdef _MIPS_CS_BASE | 
|  | 5977 | {"MIPS_CS_BASE",	_MIPS_CS_BASE}, | 
|  | 5978 | #endif | 
|  | 5979 | #ifdef _MIPS_CS_HOSTID | 
|  | 5980 | {"MIPS_CS_HOSTID",	_MIPS_CS_HOSTID}, | 
|  | 5981 | #endif | 
|  | 5982 | #ifdef _MIPS_CS_HW_NAME | 
|  | 5983 | {"MIPS_CS_HW_NAME",	_MIPS_CS_HW_NAME}, | 
|  | 5984 | #endif | 
|  | 5985 | #ifdef _MIPS_CS_NUM_PROCESSORS | 
|  | 5986 | {"MIPS_CS_NUM_PROCESSORS",	_MIPS_CS_NUM_PROCESSORS}, | 
|  | 5987 | #endif | 
|  | 5988 | #ifdef _MIPS_CS_OSREL_MAJ | 
|  | 5989 | {"MIPS_CS_OSREL_MAJ",	_MIPS_CS_OSREL_MAJ}, | 
|  | 5990 | #endif | 
|  | 5991 | #ifdef _MIPS_CS_OSREL_MIN | 
|  | 5992 | {"MIPS_CS_OSREL_MIN",	_MIPS_CS_OSREL_MIN}, | 
|  | 5993 | #endif | 
|  | 5994 | #ifdef _MIPS_CS_OSREL_PATCH | 
|  | 5995 | {"MIPS_CS_OSREL_PATCH",	_MIPS_CS_OSREL_PATCH}, | 
|  | 5996 | #endif | 
|  | 5997 | #ifdef _MIPS_CS_OS_NAME | 
|  | 5998 | {"MIPS_CS_OS_NAME",	_MIPS_CS_OS_NAME}, | 
|  | 5999 | #endif | 
|  | 6000 | #ifdef _MIPS_CS_OS_PROVIDER | 
|  | 6001 | {"MIPS_CS_OS_PROVIDER",	_MIPS_CS_OS_PROVIDER}, | 
|  | 6002 | #endif | 
|  | 6003 | #ifdef _MIPS_CS_PROCESSORS | 
|  | 6004 | {"MIPS_CS_PROCESSORS",	_MIPS_CS_PROCESSORS}, | 
|  | 6005 | #endif | 
|  | 6006 | #ifdef _MIPS_CS_SERIAL | 
|  | 6007 | {"MIPS_CS_SERIAL",	_MIPS_CS_SERIAL}, | 
|  | 6008 | #endif | 
|  | 6009 | #ifdef _MIPS_CS_VENDOR | 
|  | 6010 | {"MIPS_CS_VENDOR",	_MIPS_CS_VENDOR}, | 
|  | 6011 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6012 | }; | 
|  | 6013 |  | 
|  | 6014 | static int | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6015 | conv_confstr_confname(PyObject *arg, int *valuep) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6016 | { | 
|  | 6017 | return conv_confname(arg, valuep, posix_constants_confstr, | 
|  | 6018 | sizeof(posix_constants_confstr) | 
|  | 6019 | / sizeof(struct constdef)); | 
|  | 6020 | } | 
|  | 6021 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6022 | PyDoc_STRVAR(posix_confstr__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6023 | "confstr(name) -> string\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6024 | Return a string-valued system configuration variable."); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6025 |  | 
|  | 6026 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6027 | posix_confstr(PyObject *self, PyObject *args) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6028 | { | 
|  | 6029 | PyObject *result = NULL; | 
|  | 6030 | int name; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6031 | char buffer[256]; | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6032 |  | 
|  | 6033 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6034 | int len; | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6035 |  | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6036 | errno = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6037 | len = confstr(name, buffer, sizeof(buffer)); | 
|  | 6038 | if (len == 0) { | 
|  | 6039 | if (errno) { | 
|  | 6040 | posix_error(); | 
|  | 6041 | } | 
|  | 6042 | else { | 
|  | 6043 | result = Py_None; | 
|  | 6044 | Py_INCREF(Py_None); | 
|  | 6045 | } | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6046 | } | 
|  | 6047 | else { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6048 | if ((unsigned int)len >= sizeof(buffer)) { | 
| Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6049 | result = PyUnicode_FromStringAndSize(NULL, len-1); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6050 | if (result != NULL) | 
| Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 6051 | confstr(name, _PyUnicode_AsString(result), len); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6052 | } | 
|  | 6053 | else | 
| Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6054 | result = PyUnicode_FromStringAndSize(buffer, len-1); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6055 | } | 
|  | 6056 | } | 
|  | 6057 | return result; | 
|  | 6058 | } | 
|  | 6059 | #endif | 
|  | 6060 |  | 
|  | 6061 |  | 
|  | 6062 | #ifdef HAVE_SYSCONF | 
|  | 6063 | static struct constdef posix_constants_sysconf[] = { | 
|  | 6064 | #ifdef _SC_2_CHAR_TERM | 
|  | 6065 | {"SC_2_CHAR_TERM",	_SC_2_CHAR_TERM}, | 
|  | 6066 | #endif | 
|  | 6067 | #ifdef _SC_2_C_BIND | 
|  | 6068 | {"SC_2_C_BIND",	_SC_2_C_BIND}, | 
|  | 6069 | #endif | 
|  | 6070 | #ifdef _SC_2_C_DEV | 
|  | 6071 | {"SC_2_C_DEV",	_SC_2_C_DEV}, | 
|  | 6072 | #endif | 
|  | 6073 | #ifdef _SC_2_C_VERSION | 
|  | 6074 | {"SC_2_C_VERSION",	_SC_2_C_VERSION}, | 
|  | 6075 | #endif | 
|  | 6076 | #ifdef _SC_2_FORT_DEV | 
|  | 6077 | {"SC_2_FORT_DEV",	_SC_2_FORT_DEV}, | 
|  | 6078 | #endif | 
|  | 6079 | #ifdef _SC_2_FORT_RUN | 
|  | 6080 | {"SC_2_FORT_RUN",	_SC_2_FORT_RUN}, | 
|  | 6081 | #endif | 
|  | 6082 | #ifdef _SC_2_LOCALEDEF | 
|  | 6083 | {"SC_2_LOCALEDEF",	_SC_2_LOCALEDEF}, | 
|  | 6084 | #endif | 
|  | 6085 | #ifdef _SC_2_SW_DEV | 
|  | 6086 | {"SC_2_SW_DEV",	_SC_2_SW_DEV}, | 
|  | 6087 | #endif | 
|  | 6088 | #ifdef _SC_2_UPE | 
|  | 6089 | {"SC_2_UPE",	_SC_2_UPE}, | 
|  | 6090 | #endif | 
|  | 6091 | #ifdef _SC_2_VERSION | 
|  | 6092 | {"SC_2_VERSION",	_SC_2_VERSION}, | 
|  | 6093 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6094 | #ifdef _SC_ABI_ASYNCHRONOUS_IO | 
|  | 6095 | {"SC_ABI_ASYNCHRONOUS_IO",	_SC_ABI_ASYNCHRONOUS_IO}, | 
|  | 6096 | #endif | 
|  | 6097 | #ifdef _SC_ACL | 
|  | 6098 | {"SC_ACL",	_SC_ACL}, | 
|  | 6099 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6100 | #ifdef _SC_AIO_LISTIO_MAX | 
|  | 6101 | {"SC_AIO_LISTIO_MAX",	_SC_AIO_LISTIO_MAX}, | 
|  | 6102 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6103 | #ifdef _SC_AIO_MAX | 
|  | 6104 | {"SC_AIO_MAX",	_SC_AIO_MAX}, | 
|  | 6105 | #endif | 
|  | 6106 | #ifdef _SC_AIO_PRIO_DELTA_MAX | 
|  | 6107 | {"SC_AIO_PRIO_DELTA_MAX",	_SC_AIO_PRIO_DELTA_MAX}, | 
|  | 6108 | #endif | 
|  | 6109 | #ifdef _SC_ARG_MAX | 
|  | 6110 | {"SC_ARG_MAX",	_SC_ARG_MAX}, | 
|  | 6111 | #endif | 
|  | 6112 | #ifdef _SC_ASYNCHRONOUS_IO | 
|  | 6113 | {"SC_ASYNCHRONOUS_IO",	_SC_ASYNCHRONOUS_IO}, | 
|  | 6114 | #endif | 
|  | 6115 | #ifdef _SC_ATEXIT_MAX | 
|  | 6116 | {"SC_ATEXIT_MAX",	_SC_ATEXIT_MAX}, | 
|  | 6117 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6118 | #ifdef _SC_AUDIT | 
|  | 6119 | {"SC_AUDIT",	_SC_AUDIT}, | 
|  | 6120 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6121 | #ifdef _SC_AVPHYS_PAGES | 
|  | 6122 | {"SC_AVPHYS_PAGES",	_SC_AVPHYS_PAGES}, | 
|  | 6123 | #endif | 
|  | 6124 | #ifdef _SC_BC_BASE_MAX | 
|  | 6125 | {"SC_BC_BASE_MAX",	_SC_BC_BASE_MAX}, | 
|  | 6126 | #endif | 
|  | 6127 | #ifdef _SC_BC_DIM_MAX | 
|  | 6128 | {"SC_BC_DIM_MAX",	_SC_BC_DIM_MAX}, | 
|  | 6129 | #endif | 
|  | 6130 | #ifdef _SC_BC_SCALE_MAX | 
|  | 6131 | {"SC_BC_SCALE_MAX",	_SC_BC_SCALE_MAX}, | 
|  | 6132 | #endif | 
|  | 6133 | #ifdef _SC_BC_STRING_MAX | 
|  | 6134 | {"SC_BC_STRING_MAX",	_SC_BC_STRING_MAX}, | 
|  | 6135 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6136 | #ifdef _SC_CAP | 
|  | 6137 | {"SC_CAP",	_SC_CAP}, | 
|  | 6138 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6139 | #ifdef _SC_CHARCLASS_NAME_MAX | 
|  | 6140 | {"SC_CHARCLASS_NAME_MAX",	_SC_CHARCLASS_NAME_MAX}, | 
|  | 6141 | #endif | 
|  | 6142 | #ifdef _SC_CHAR_BIT | 
|  | 6143 | {"SC_CHAR_BIT",	_SC_CHAR_BIT}, | 
|  | 6144 | #endif | 
|  | 6145 | #ifdef _SC_CHAR_MAX | 
|  | 6146 | {"SC_CHAR_MAX",	_SC_CHAR_MAX}, | 
|  | 6147 | #endif | 
|  | 6148 | #ifdef _SC_CHAR_MIN | 
|  | 6149 | {"SC_CHAR_MIN",	_SC_CHAR_MIN}, | 
|  | 6150 | #endif | 
|  | 6151 | #ifdef _SC_CHILD_MAX | 
|  | 6152 | {"SC_CHILD_MAX",	_SC_CHILD_MAX}, | 
|  | 6153 | #endif | 
|  | 6154 | #ifdef _SC_CLK_TCK | 
|  | 6155 | {"SC_CLK_TCK",	_SC_CLK_TCK}, | 
|  | 6156 | #endif | 
|  | 6157 | #ifdef _SC_COHER_BLKSZ | 
|  | 6158 | {"SC_COHER_BLKSZ",	_SC_COHER_BLKSZ}, | 
|  | 6159 | #endif | 
|  | 6160 | #ifdef _SC_COLL_WEIGHTS_MAX | 
|  | 6161 | {"SC_COLL_WEIGHTS_MAX",	_SC_COLL_WEIGHTS_MAX}, | 
|  | 6162 | #endif | 
|  | 6163 | #ifdef _SC_DCACHE_ASSOC | 
|  | 6164 | {"SC_DCACHE_ASSOC",	_SC_DCACHE_ASSOC}, | 
|  | 6165 | #endif | 
|  | 6166 | #ifdef _SC_DCACHE_BLKSZ | 
|  | 6167 | {"SC_DCACHE_BLKSZ",	_SC_DCACHE_BLKSZ}, | 
|  | 6168 | #endif | 
|  | 6169 | #ifdef _SC_DCACHE_LINESZ | 
|  | 6170 | {"SC_DCACHE_LINESZ",	_SC_DCACHE_LINESZ}, | 
|  | 6171 | #endif | 
|  | 6172 | #ifdef _SC_DCACHE_SZ | 
|  | 6173 | {"SC_DCACHE_SZ",	_SC_DCACHE_SZ}, | 
|  | 6174 | #endif | 
|  | 6175 | #ifdef _SC_DCACHE_TBLKSZ | 
|  | 6176 | {"SC_DCACHE_TBLKSZ",	_SC_DCACHE_TBLKSZ}, | 
|  | 6177 | #endif | 
|  | 6178 | #ifdef _SC_DELAYTIMER_MAX | 
|  | 6179 | {"SC_DELAYTIMER_MAX",	_SC_DELAYTIMER_MAX}, | 
|  | 6180 | #endif | 
|  | 6181 | #ifdef _SC_EQUIV_CLASS_MAX | 
|  | 6182 | {"SC_EQUIV_CLASS_MAX",	_SC_EQUIV_CLASS_MAX}, | 
|  | 6183 | #endif | 
|  | 6184 | #ifdef _SC_EXPR_NEST_MAX | 
|  | 6185 | {"SC_EXPR_NEST_MAX",	_SC_EXPR_NEST_MAX}, | 
|  | 6186 | #endif | 
|  | 6187 | #ifdef _SC_FSYNC | 
|  | 6188 | {"SC_FSYNC",	_SC_FSYNC}, | 
|  | 6189 | #endif | 
|  | 6190 | #ifdef _SC_GETGR_R_SIZE_MAX | 
|  | 6191 | {"SC_GETGR_R_SIZE_MAX",	_SC_GETGR_R_SIZE_MAX}, | 
|  | 6192 | #endif | 
|  | 6193 | #ifdef _SC_GETPW_R_SIZE_MAX | 
|  | 6194 | {"SC_GETPW_R_SIZE_MAX",	_SC_GETPW_R_SIZE_MAX}, | 
|  | 6195 | #endif | 
|  | 6196 | #ifdef _SC_ICACHE_ASSOC | 
|  | 6197 | {"SC_ICACHE_ASSOC",	_SC_ICACHE_ASSOC}, | 
|  | 6198 | #endif | 
|  | 6199 | #ifdef _SC_ICACHE_BLKSZ | 
|  | 6200 | {"SC_ICACHE_BLKSZ",	_SC_ICACHE_BLKSZ}, | 
|  | 6201 | #endif | 
|  | 6202 | #ifdef _SC_ICACHE_LINESZ | 
|  | 6203 | {"SC_ICACHE_LINESZ",	_SC_ICACHE_LINESZ}, | 
|  | 6204 | #endif | 
|  | 6205 | #ifdef _SC_ICACHE_SZ | 
|  | 6206 | {"SC_ICACHE_SZ",	_SC_ICACHE_SZ}, | 
|  | 6207 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6208 | #ifdef _SC_INF | 
|  | 6209 | {"SC_INF",	_SC_INF}, | 
|  | 6210 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6211 | #ifdef _SC_INT_MAX | 
|  | 6212 | {"SC_INT_MAX",	_SC_INT_MAX}, | 
|  | 6213 | #endif | 
|  | 6214 | #ifdef _SC_INT_MIN | 
|  | 6215 | {"SC_INT_MIN",	_SC_INT_MIN}, | 
|  | 6216 | #endif | 
|  | 6217 | #ifdef _SC_IOV_MAX | 
|  | 6218 | {"SC_IOV_MAX",	_SC_IOV_MAX}, | 
|  | 6219 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6220 | #ifdef _SC_IP_SECOPTS | 
|  | 6221 | {"SC_IP_SECOPTS",	_SC_IP_SECOPTS}, | 
|  | 6222 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6223 | #ifdef _SC_JOB_CONTROL | 
|  | 6224 | {"SC_JOB_CONTROL",	_SC_JOB_CONTROL}, | 
|  | 6225 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6226 | #ifdef _SC_KERN_POINTERS | 
|  | 6227 | {"SC_KERN_POINTERS",	_SC_KERN_POINTERS}, | 
|  | 6228 | #endif | 
|  | 6229 | #ifdef _SC_KERN_SIM | 
|  | 6230 | {"SC_KERN_SIM",	_SC_KERN_SIM}, | 
|  | 6231 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6232 | #ifdef _SC_LINE_MAX | 
|  | 6233 | {"SC_LINE_MAX",	_SC_LINE_MAX}, | 
|  | 6234 | #endif | 
|  | 6235 | #ifdef _SC_LOGIN_NAME_MAX | 
|  | 6236 | {"SC_LOGIN_NAME_MAX",	_SC_LOGIN_NAME_MAX}, | 
|  | 6237 | #endif | 
|  | 6238 | #ifdef _SC_LOGNAME_MAX | 
|  | 6239 | {"SC_LOGNAME_MAX",	_SC_LOGNAME_MAX}, | 
|  | 6240 | #endif | 
|  | 6241 | #ifdef _SC_LONG_BIT | 
|  | 6242 | {"SC_LONG_BIT",	_SC_LONG_BIT}, | 
|  | 6243 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6244 | #ifdef _SC_MAC | 
|  | 6245 | {"SC_MAC",	_SC_MAC}, | 
|  | 6246 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6247 | #ifdef _SC_MAPPED_FILES | 
|  | 6248 | {"SC_MAPPED_FILES",	_SC_MAPPED_FILES}, | 
|  | 6249 | #endif | 
|  | 6250 | #ifdef _SC_MAXPID | 
|  | 6251 | {"SC_MAXPID",	_SC_MAXPID}, | 
|  | 6252 | #endif | 
|  | 6253 | #ifdef _SC_MB_LEN_MAX | 
|  | 6254 | {"SC_MB_LEN_MAX",	_SC_MB_LEN_MAX}, | 
|  | 6255 | #endif | 
|  | 6256 | #ifdef _SC_MEMLOCK | 
|  | 6257 | {"SC_MEMLOCK",	_SC_MEMLOCK}, | 
|  | 6258 | #endif | 
|  | 6259 | #ifdef _SC_MEMLOCK_RANGE | 
|  | 6260 | {"SC_MEMLOCK_RANGE",	_SC_MEMLOCK_RANGE}, | 
|  | 6261 | #endif | 
|  | 6262 | #ifdef _SC_MEMORY_PROTECTION | 
|  | 6263 | {"SC_MEMORY_PROTECTION",	_SC_MEMORY_PROTECTION}, | 
|  | 6264 | #endif | 
|  | 6265 | #ifdef _SC_MESSAGE_PASSING | 
|  | 6266 | {"SC_MESSAGE_PASSING",	_SC_MESSAGE_PASSING}, | 
|  | 6267 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6268 | #ifdef _SC_MMAP_FIXED_ALIGNMENT | 
|  | 6269 | {"SC_MMAP_FIXED_ALIGNMENT",	_SC_MMAP_FIXED_ALIGNMENT}, | 
|  | 6270 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6271 | #ifdef _SC_MQ_OPEN_MAX | 
|  | 6272 | {"SC_MQ_OPEN_MAX",	_SC_MQ_OPEN_MAX}, | 
|  | 6273 | #endif | 
|  | 6274 | #ifdef _SC_MQ_PRIO_MAX | 
|  | 6275 | {"SC_MQ_PRIO_MAX",	_SC_MQ_PRIO_MAX}, | 
|  | 6276 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6277 | #ifdef _SC_NACLS_MAX | 
|  | 6278 | {"SC_NACLS_MAX",	_SC_NACLS_MAX}, | 
|  | 6279 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6280 | #ifdef _SC_NGROUPS_MAX | 
|  | 6281 | {"SC_NGROUPS_MAX",	_SC_NGROUPS_MAX}, | 
|  | 6282 | #endif | 
|  | 6283 | #ifdef _SC_NL_ARGMAX | 
|  | 6284 | {"SC_NL_ARGMAX",	_SC_NL_ARGMAX}, | 
|  | 6285 | #endif | 
|  | 6286 | #ifdef _SC_NL_LANGMAX | 
|  | 6287 | {"SC_NL_LANGMAX",	_SC_NL_LANGMAX}, | 
|  | 6288 | #endif | 
|  | 6289 | #ifdef _SC_NL_MSGMAX | 
|  | 6290 | {"SC_NL_MSGMAX",	_SC_NL_MSGMAX}, | 
|  | 6291 | #endif | 
|  | 6292 | #ifdef _SC_NL_NMAX | 
|  | 6293 | {"SC_NL_NMAX",	_SC_NL_NMAX}, | 
|  | 6294 | #endif | 
|  | 6295 | #ifdef _SC_NL_SETMAX | 
|  | 6296 | {"SC_NL_SETMAX",	_SC_NL_SETMAX}, | 
|  | 6297 | #endif | 
|  | 6298 | #ifdef _SC_NL_TEXTMAX | 
|  | 6299 | {"SC_NL_TEXTMAX",	_SC_NL_TEXTMAX}, | 
|  | 6300 | #endif | 
|  | 6301 | #ifdef _SC_NPROCESSORS_CONF | 
|  | 6302 | {"SC_NPROCESSORS_CONF",	_SC_NPROCESSORS_CONF}, | 
|  | 6303 | #endif | 
|  | 6304 | #ifdef _SC_NPROCESSORS_ONLN | 
|  | 6305 | {"SC_NPROCESSORS_ONLN",	_SC_NPROCESSORS_ONLN}, | 
|  | 6306 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6307 | #ifdef _SC_NPROC_CONF | 
|  | 6308 | {"SC_NPROC_CONF",	_SC_NPROC_CONF}, | 
|  | 6309 | #endif | 
|  | 6310 | #ifdef _SC_NPROC_ONLN | 
|  | 6311 | {"SC_NPROC_ONLN",	_SC_NPROC_ONLN}, | 
|  | 6312 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6313 | #ifdef _SC_NZERO | 
|  | 6314 | {"SC_NZERO",	_SC_NZERO}, | 
|  | 6315 | #endif | 
|  | 6316 | #ifdef _SC_OPEN_MAX | 
|  | 6317 | {"SC_OPEN_MAX",	_SC_OPEN_MAX}, | 
|  | 6318 | #endif | 
|  | 6319 | #ifdef _SC_PAGESIZE | 
|  | 6320 | {"SC_PAGESIZE",	_SC_PAGESIZE}, | 
|  | 6321 | #endif | 
|  | 6322 | #ifdef _SC_PAGE_SIZE | 
|  | 6323 | {"SC_PAGE_SIZE",	_SC_PAGE_SIZE}, | 
|  | 6324 | #endif | 
|  | 6325 | #ifdef _SC_PASS_MAX | 
|  | 6326 | {"SC_PASS_MAX",	_SC_PASS_MAX}, | 
|  | 6327 | #endif | 
|  | 6328 | #ifdef _SC_PHYS_PAGES | 
|  | 6329 | {"SC_PHYS_PAGES",	_SC_PHYS_PAGES}, | 
|  | 6330 | #endif | 
|  | 6331 | #ifdef _SC_PII | 
|  | 6332 | {"SC_PII",	_SC_PII}, | 
|  | 6333 | #endif | 
|  | 6334 | #ifdef _SC_PII_INTERNET | 
|  | 6335 | {"SC_PII_INTERNET",	_SC_PII_INTERNET}, | 
|  | 6336 | #endif | 
|  | 6337 | #ifdef _SC_PII_INTERNET_DGRAM | 
|  | 6338 | {"SC_PII_INTERNET_DGRAM",	_SC_PII_INTERNET_DGRAM}, | 
|  | 6339 | #endif | 
|  | 6340 | #ifdef _SC_PII_INTERNET_STREAM | 
|  | 6341 | {"SC_PII_INTERNET_STREAM",	_SC_PII_INTERNET_STREAM}, | 
|  | 6342 | #endif | 
|  | 6343 | #ifdef _SC_PII_OSI | 
|  | 6344 | {"SC_PII_OSI",	_SC_PII_OSI}, | 
|  | 6345 | #endif | 
|  | 6346 | #ifdef _SC_PII_OSI_CLTS | 
|  | 6347 | {"SC_PII_OSI_CLTS",	_SC_PII_OSI_CLTS}, | 
|  | 6348 | #endif | 
|  | 6349 | #ifdef _SC_PII_OSI_COTS | 
|  | 6350 | {"SC_PII_OSI_COTS",	_SC_PII_OSI_COTS}, | 
|  | 6351 | #endif | 
|  | 6352 | #ifdef _SC_PII_OSI_M | 
|  | 6353 | {"SC_PII_OSI_M",	_SC_PII_OSI_M}, | 
|  | 6354 | #endif | 
|  | 6355 | #ifdef _SC_PII_SOCKET | 
|  | 6356 | {"SC_PII_SOCKET",	_SC_PII_SOCKET}, | 
|  | 6357 | #endif | 
|  | 6358 | #ifdef _SC_PII_XTI | 
|  | 6359 | {"SC_PII_XTI",	_SC_PII_XTI}, | 
|  | 6360 | #endif | 
|  | 6361 | #ifdef _SC_POLL | 
|  | 6362 | {"SC_POLL",	_SC_POLL}, | 
|  | 6363 | #endif | 
|  | 6364 | #ifdef _SC_PRIORITIZED_IO | 
|  | 6365 | {"SC_PRIORITIZED_IO",	_SC_PRIORITIZED_IO}, | 
|  | 6366 | #endif | 
|  | 6367 | #ifdef _SC_PRIORITY_SCHEDULING | 
|  | 6368 | {"SC_PRIORITY_SCHEDULING",	_SC_PRIORITY_SCHEDULING}, | 
|  | 6369 | #endif | 
|  | 6370 | #ifdef _SC_REALTIME_SIGNALS | 
|  | 6371 | {"SC_REALTIME_SIGNALS",	_SC_REALTIME_SIGNALS}, | 
|  | 6372 | #endif | 
|  | 6373 | #ifdef _SC_RE_DUP_MAX | 
|  | 6374 | {"SC_RE_DUP_MAX",	_SC_RE_DUP_MAX}, | 
|  | 6375 | #endif | 
|  | 6376 | #ifdef _SC_RTSIG_MAX | 
|  | 6377 | {"SC_RTSIG_MAX",	_SC_RTSIG_MAX}, | 
|  | 6378 | #endif | 
|  | 6379 | #ifdef _SC_SAVED_IDS | 
|  | 6380 | {"SC_SAVED_IDS",	_SC_SAVED_IDS}, | 
|  | 6381 | #endif | 
|  | 6382 | #ifdef _SC_SCHAR_MAX | 
|  | 6383 | {"SC_SCHAR_MAX",	_SC_SCHAR_MAX}, | 
|  | 6384 | #endif | 
|  | 6385 | #ifdef _SC_SCHAR_MIN | 
|  | 6386 | {"SC_SCHAR_MIN",	_SC_SCHAR_MIN}, | 
|  | 6387 | #endif | 
|  | 6388 | #ifdef _SC_SELECT | 
|  | 6389 | {"SC_SELECT",	_SC_SELECT}, | 
|  | 6390 | #endif | 
|  | 6391 | #ifdef _SC_SEMAPHORES | 
|  | 6392 | {"SC_SEMAPHORES",	_SC_SEMAPHORES}, | 
|  | 6393 | #endif | 
|  | 6394 | #ifdef _SC_SEM_NSEMS_MAX | 
|  | 6395 | {"SC_SEM_NSEMS_MAX",	_SC_SEM_NSEMS_MAX}, | 
|  | 6396 | #endif | 
|  | 6397 | #ifdef _SC_SEM_VALUE_MAX | 
|  | 6398 | {"SC_SEM_VALUE_MAX",	_SC_SEM_VALUE_MAX}, | 
|  | 6399 | #endif | 
|  | 6400 | #ifdef _SC_SHARED_MEMORY_OBJECTS | 
|  | 6401 | {"SC_SHARED_MEMORY_OBJECTS",	_SC_SHARED_MEMORY_OBJECTS}, | 
|  | 6402 | #endif | 
|  | 6403 | #ifdef _SC_SHRT_MAX | 
|  | 6404 | {"SC_SHRT_MAX",	_SC_SHRT_MAX}, | 
|  | 6405 | #endif | 
|  | 6406 | #ifdef _SC_SHRT_MIN | 
|  | 6407 | {"SC_SHRT_MIN",	_SC_SHRT_MIN}, | 
|  | 6408 | #endif | 
|  | 6409 | #ifdef _SC_SIGQUEUE_MAX | 
|  | 6410 | {"SC_SIGQUEUE_MAX",	_SC_SIGQUEUE_MAX}, | 
|  | 6411 | #endif | 
|  | 6412 | #ifdef _SC_SIGRT_MAX | 
|  | 6413 | {"SC_SIGRT_MAX",	_SC_SIGRT_MAX}, | 
|  | 6414 | #endif | 
|  | 6415 | #ifdef _SC_SIGRT_MIN | 
|  | 6416 | {"SC_SIGRT_MIN",	_SC_SIGRT_MIN}, | 
|  | 6417 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6418 | #ifdef _SC_SOFTPOWER | 
|  | 6419 | {"SC_SOFTPOWER",	_SC_SOFTPOWER}, | 
|  | 6420 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6421 | #ifdef _SC_SPLIT_CACHE | 
|  | 6422 | {"SC_SPLIT_CACHE",	_SC_SPLIT_CACHE}, | 
|  | 6423 | #endif | 
|  | 6424 | #ifdef _SC_SSIZE_MAX | 
|  | 6425 | {"SC_SSIZE_MAX",	_SC_SSIZE_MAX}, | 
|  | 6426 | #endif | 
|  | 6427 | #ifdef _SC_STACK_PROT | 
|  | 6428 | {"SC_STACK_PROT",	_SC_STACK_PROT}, | 
|  | 6429 | #endif | 
|  | 6430 | #ifdef _SC_STREAM_MAX | 
|  | 6431 | {"SC_STREAM_MAX",	_SC_STREAM_MAX}, | 
|  | 6432 | #endif | 
|  | 6433 | #ifdef _SC_SYNCHRONIZED_IO | 
|  | 6434 | {"SC_SYNCHRONIZED_IO",	_SC_SYNCHRONIZED_IO}, | 
|  | 6435 | #endif | 
|  | 6436 | #ifdef _SC_THREADS | 
|  | 6437 | {"SC_THREADS",	_SC_THREADS}, | 
|  | 6438 | #endif | 
|  | 6439 | #ifdef _SC_THREAD_ATTR_STACKADDR | 
|  | 6440 | {"SC_THREAD_ATTR_STACKADDR",	_SC_THREAD_ATTR_STACKADDR}, | 
|  | 6441 | #endif | 
|  | 6442 | #ifdef _SC_THREAD_ATTR_STACKSIZE | 
|  | 6443 | {"SC_THREAD_ATTR_STACKSIZE",	_SC_THREAD_ATTR_STACKSIZE}, | 
|  | 6444 | #endif | 
|  | 6445 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS | 
|  | 6446 | {"SC_THREAD_DESTRUCTOR_ITERATIONS",	_SC_THREAD_DESTRUCTOR_ITERATIONS}, | 
|  | 6447 | #endif | 
|  | 6448 | #ifdef _SC_THREAD_KEYS_MAX | 
|  | 6449 | {"SC_THREAD_KEYS_MAX",	_SC_THREAD_KEYS_MAX}, | 
|  | 6450 | #endif | 
|  | 6451 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING | 
|  | 6452 | {"SC_THREAD_PRIORITY_SCHEDULING",	_SC_THREAD_PRIORITY_SCHEDULING}, | 
|  | 6453 | #endif | 
|  | 6454 | #ifdef _SC_THREAD_PRIO_INHERIT | 
|  | 6455 | {"SC_THREAD_PRIO_INHERIT",	_SC_THREAD_PRIO_INHERIT}, | 
|  | 6456 | #endif | 
|  | 6457 | #ifdef _SC_THREAD_PRIO_PROTECT | 
|  | 6458 | {"SC_THREAD_PRIO_PROTECT",	_SC_THREAD_PRIO_PROTECT}, | 
|  | 6459 | #endif | 
|  | 6460 | #ifdef _SC_THREAD_PROCESS_SHARED | 
|  | 6461 | {"SC_THREAD_PROCESS_SHARED",	_SC_THREAD_PROCESS_SHARED}, | 
|  | 6462 | #endif | 
|  | 6463 | #ifdef _SC_THREAD_SAFE_FUNCTIONS | 
|  | 6464 | {"SC_THREAD_SAFE_FUNCTIONS",	_SC_THREAD_SAFE_FUNCTIONS}, | 
|  | 6465 | #endif | 
|  | 6466 | #ifdef _SC_THREAD_STACK_MIN | 
|  | 6467 | {"SC_THREAD_STACK_MIN",	_SC_THREAD_STACK_MIN}, | 
|  | 6468 | #endif | 
|  | 6469 | #ifdef _SC_THREAD_THREADS_MAX | 
|  | 6470 | {"SC_THREAD_THREADS_MAX",	_SC_THREAD_THREADS_MAX}, | 
|  | 6471 | #endif | 
|  | 6472 | #ifdef _SC_TIMERS | 
|  | 6473 | {"SC_TIMERS",	_SC_TIMERS}, | 
|  | 6474 | #endif | 
|  | 6475 | #ifdef _SC_TIMER_MAX | 
|  | 6476 | {"SC_TIMER_MAX",	_SC_TIMER_MAX}, | 
|  | 6477 | #endif | 
|  | 6478 | #ifdef _SC_TTY_NAME_MAX | 
|  | 6479 | {"SC_TTY_NAME_MAX",	_SC_TTY_NAME_MAX}, | 
|  | 6480 | #endif | 
|  | 6481 | #ifdef _SC_TZNAME_MAX | 
|  | 6482 | {"SC_TZNAME_MAX",	_SC_TZNAME_MAX}, | 
|  | 6483 | #endif | 
|  | 6484 | #ifdef _SC_T_IOV_MAX | 
|  | 6485 | {"SC_T_IOV_MAX",	_SC_T_IOV_MAX}, | 
|  | 6486 | #endif | 
|  | 6487 | #ifdef _SC_UCHAR_MAX | 
|  | 6488 | {"SC_UCHAR_MAX",	_SC_UCHAR_MAX}, | 
|  | 6489 | #endif | 
|  | 6490 | #ifdef _SC_UINT_MAX | 
|  | 6491 | {"SC_UINT_MAX",	_SC_UINT_MAX}, | 
|  | 6492 | #endif | 
|  | 6493 | #ifdef _SC_UIO_MAXIOV | 
|  | 6494 | {"SC_UIO_MAXIOV",	_SC_UIO_MAXIOV}, | 
|  | 6495 | #endif | 
|  | 6496 | #ifdef _SC_ULONG_MAX | 
|  | 6497 | {"SC_ULONG_MAX",	_SC_ULONG_MAX}, | 
|  | 6498 | #endif | 
|  | 6499 | #ifdef _SC_USHRT_MAX | 
|  | 6500 | {"SC_USHRT_MAX",	_SC_USHRT_MAX}, | 
|  | 6501 | #endif | 
|  | 6502 | #ifdef _SC_VERSION | 
|  | 6503 | {"SC_VERSION",	_SC_VERSION}, | 
|  | 6504 | #endif | 
|  | 6505 | #ifdef _SC_WORD_BIT | 
|  | 6506 | {"SC_WORD_BIT",	_SC_WORD_BIT}, | 
|  | 6507 | #endif | 
|  | 6508 | #ifdef _SC_XBS5_ILP32_OFF32 | 
|  | 6509 | {"SC_XBS5_ILP32_OFF32",	_SC_XBS5_ILP32_OFF32}, | 
|  | 6510 | #endif | 
|  | 6511 | #ifdef _SC_XBS5_ILP32_OFFBIG | 
|  | 6512 | {"SC_XBS5_ILP32_OFFBIG",	_SC_XBS5_ILP32_OFFBIG}, | 
|  | 6513 | #endif | 
|  | 6514 | #ifdef _SC_XBS5_LP64_OFF64 | 
|  | 6515 | {"SC_XBS5_LP64_OFF64",	_SC_XBS5_LP64_OFF64}, | 
|  | 6516 | #endif | 
|  | 6517 | #ifdef _SC_XBS5_LPBIG_OFFBIG | 
|  | 6518 | {"SC_XBS5_LPBIG_OFFBIG",	_SC_XBS5_LPBIG_OFFBIG}, | 
|  | 6519 | #endif | 
|  | 6520 | #ifdef _SC_XOPEN_CRYPT | 
|  | 6521 | {"SC_XOPEN_CRYPT",	_SC_XOPEN_CRYPT}, | 
|  | 6522 | #endif | 
|  | 6523 | #ifdef _SC_XOPEN_ENH_I18N | 
|  | 6524 | {"SC_XOPEN_ENH_I18N",	_SC_XOPEN_ENH_I18N}, | 
|  | 6525 | #endif | 
|  | 6526 | #ifdef _SC_XOPEN_LEGACY | 
|  | 6527 | {"SC_XOPEN_LEGACY",	_SC_XOPEN_LEGACY}, | 
|  | 6528 | #endif | 
|  | 6529 | #ifdef _SC_XOPEN_REALTIME | 
|  | 6530 | {"SC_XOPEN_REALTIME",	_SC_XOPEN_REALTIME}, | 
|  | 6531 | #endif | 
|  | 6532 | #ifdef _SC_XOPEN_REALTIME_THREADS | 
|  | 6533 | {"SC_XOPEN_REALTIME_THREADS",	_SC_XOPEN_REALTIME_THREADS}, | 
|  | 6534 | #endif | 
|  | 6535 | #ifdef _SC_XOPEN_SHM | 
|  | 6536 | {"SC_XOPEN_SHM",	_SC_XOPEN_SHM}, | 
|  | 6537 | #endif | 
|  | 6538 | #ifdef _SC_XOPEN_UNIX | 
|  | 6539 | {"SC_XOPEN_UNIX",	_SC_XOPEN_UNIX}, | 
|  | 6540 | #endif | 
|  | 6541 | #ifdef _SC_XOPEN_VERSION | 
|  | 6542 | {"SC_XOPEN_VERSION",	_SC_XOPEN_VERSION}, | 
|  | 6543 | #endif | 
|  | 6544 | #ifdef _SC_XOPEN_XCU_VERSION | 
|  | 6545 | {"SC_XOPEN_XCU_VERSION",	_SC_XOPEN_XCU_VERSION}, | 
|  | 6546 | #endif | 
|  | 6547 | #ifdef _SC_XOPEN_XPG2 | 
|  | 6548 | {"SC_XOPEN_XPG2",	_SC_XOPEN_XPG2}, | 
|  | 6549 | #endif | 
|  | 6550 | #ifdef _SC_XOPEN_XPG3 | 
|  | 6551 | {"SC_XOPEN_XPG3",	_SC_XOPEN_XPG3}, | 
|  | 6552 | #endif | 
|  | 6553 | #ifdef _SC_XOPEN_XPG4 | 
|  | 6554 | {"SC_XOPEN_XPG4",	_SC_XOPEN_XPG4}, | 
|  | 6555 | #endif | 
|  | 6556 | }; | 
|  | 6557 |  | 
|  | 6558 | static int | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6559 | conv_sysconf_confname(PyObject *arg, int *valuep) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6560 | { | 
|  | 6561 | return conv_confname(arg, valuep, posix_constants_sysconf, | 
|  | 6562 | sizeof(posix_constants_sysconf) | 
|  | 6563 | / sizeof(struct constdef)); | 
|  | 6564 | } | 
|  | 6565 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6566 | PyDoc_STRVAR(posix_sysconf__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6567 | "sysconf(name) -> integer\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6568 | Return an integer-valued system configuration variable."); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6569 |  | 
|  | 6570 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6571 | posix_sysconf(PyObject *self, PyObject *args) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6572 | { | 
|  | 6573 | PyObject *result = NULL; | 
|  | 6574 | int name; | 
|  | 6575 |  | 
|  | 6576 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { | 
|  | 6577 | int value; | 
|  | 6578 |  | 
|  | 6579 | errno = 0; | 
|  | 6580 | value = sysconf(name); | 
|  | 6581 | if (value == -1 && errno != 0) | 
|  | 6582 | posix_error(); | 
|  | 6583 | else | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6584 | result = PyLong_FromLong(value); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6585 | } | 
|  | 6586 | return result; | 
|  | 6587 | } | 
|  | 6588 | #endif | 
|  | 6589 |  | 
|  | 6590 |  | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6591 | /* This code is used to ensure that the tables of configuration value names | 
|  | 6592 | * are in sorted order as required by conv_confname(), and also to build the | 
|  | 6593 | * the exported dictionaries that are used to publish information about the | 
|  | 6594 | * names available on the host platform. | 
|  | 6595 | * | 
|  | 6596 | * Sorting the table at runtime ensures that the table is properly ordered | 
|  | 6597 | * when used, even for platforms we're not able to test on.  It also makes | 
|  | 6598 | * it easier to add additional entries to the tables. | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6599 | */ | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6600 |  | 
|  | 6601 | static int | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6602 | cmp_constdefs(const void *v1,  const void *v2) | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6603 | { | 
|  | 6604 | const struct constdef *c1 = | 
|  | 6605 | (const struct constdef *) v1; | 
|  | 6606 | const struct constdef *c2 = | 
|  | 6607 | (const struct constdef *) v2; | 
|  | 6608 |  | 
|  | 6609 | return strcmp(c1->name, c2->name); | 
|  | 6610 | } | 
|  | 6611 |  | 
|  | 6612 | static int | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6613 | setup_confname_table(struct constdef *table, size_t tablesize, | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6614 | char *tablename, PyObject *module) | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6615 | { | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6616 | PyObject *d = NULL; | 
| Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6617 | size_t i; | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6618 |  | 
|  | 6619 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); | 
|  | 6620 | d = PyDict_New(); | 
| Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6621 | if (d == NULL) | 
|  | 6622 | return -1; | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6623 |  | 
| Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6624 | for (i=0; i < tablesize; ++i) { | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6625 | PyObject *o = PyLong_FromLong(table[i].value); | 
| Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6626 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { | 
|  | 6627 | Py_XDECREF(o); | 
|  | 6628 | Py_DECREF(d); | 
|  | 6629 | return -1; | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6630 | } | 
| Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6631 | Py_DECREF(o); | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6632 | } | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6633 | return PyModule_AddObject(module, tablename, d); | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6634 | } | 
|  | 6635 |  | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6636 | /* Return -1 on failure, 0 on success. */ | 
|  | 6637 | static int | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6638 | setup_confname_tables(PyObject *module) | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6639 | { | 
|  | 6640 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6641 | if (setup_confname_table(posix_constants_pathconf, | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6642 | sizeof(posix_constants_pathconf) | 
|  | 6643 | / sizeof(struct constdef), | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6644 | "pathconf_names", module)) | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6645 | return -1; | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6646 | #endif | 
|  | 6647 | #ifdef HAVE_CONFSTR | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6648 | if (setup_confname_table(posix_constants_confstr, | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6649 | sizeof(posix_constants_confstr) | 
|  | 6650 | / sizeof(struct constdef), | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6651 | "confstr_names", module)) | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6652 | return -1; | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6653 | #endif | 
|  | 6654 | #ifdef HAVE_SYSCONF | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6655 | if (setup_confname_table(posix_constants_sysconf, | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6656 | sizeof(posix_constants_sysconf) | 
|  | 6657 | / sizeof(struct constdef), | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6658 | "sysconf_names", module)) | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6659 | return -1; | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6660 | #endif | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6661 | return 0; | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6662 | } | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6663 |  | 
|  | 6664 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6665 | PyDoc_STRVAR(posix_abort__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6666 | "abort() -> does not return!\n\n\ | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6667 | Abort the interpreter immediately.  This 'dumps core' or otherwise fails\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6668 | in the hardest way possible on the hosting operating system."); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6669 |  | 
|  | 6670 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6671 | posix_abort(PyObject *self, PyObject *noargs) | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6672 | { | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6673 | abort(); | 
|  | 6674 | /*NOTREACHED*/ | 
|  | 6675 | Py_FatalError("abort() called from Python code didn't abort!"); | 
|  | 6676 | return NULL; | 
|  | 6677 | } | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6678 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6679 | #ifdef MS_WINDOWS | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6680 | PyDoc_STRVAR(win32_startfile__doc__, | 
| Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6681 | "startfile(filepath [, operation]) - Start a file with its associated\n\ | 
|  | 6682 | application.\n\ | 
| Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6683 | \n\ | 
| Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6684 | When \"operation\" is not specified or \"open\", this acts like\n\ | 
|  | 6685 | double-clicking the file in Explorer, or giving the file name as an\n\ | 
|  | 6686 | argument to the DOS \"start\" command: the file is opened with whatever\n\ | 
|  | 6687 | application (if any) its extension is associated.\n\ | 
|  | 6688 | When another \"operation\" is given, it specifies what should be done with\n\ | 
|  | 6689 | the file.  A typical operation is \"print\".\n\ | 
| Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6690 | \n\ | 
|  | 6691 | startfile returns as soon as the associated application is launched.\n\ | 
|  | 6692 | There is no option to wait for the application to close, and no way\n\ | 
|  | 6693 | to retrieve the application's exit status.\n\ | 
|  | 6694 | \n\ | 
|  | 6695 | The filepath is relative to the current directory.  If you want to use\n\ | 
|  | 6696 | an absolute path, make sure the first character is not a slash (\"/\");\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6697 | the underlying Win32 ShellExecute function doesn't work if it is."); | 
| Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6698 |  | 
|  | 6699 | static PyObject * | 
|  | 6700 | win32_startfile(PyObject *self, PyObject *args) | 
|  | 6701 | { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6702 | PyObject *ofilepath; | 
| Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6703 | char *filepath; | 
| Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6704 | char *operation = NULL; | 
| Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6705 | HINSTANCE rc; | 
| Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 6706 |  | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 6707 | PyObject *unipath, *woperation = NULL; | 
|  | 6708 | if (!PyArg_ParseTuple(args, "U|s:startfile", | 
|  | 6709 | &unipath, &operation)) { | 
|  | 6710 | PyErr_Clear(); | 
|  | 6711 | goto normal; | 
|  | 6712 | } | 
|  | 6713 |  | 
|  | 6714 | if (operation) { | 
|  | 6715 | woperation = PyUnicode_DecodeASCII(operation, | 
|  | 6716 | strlen(operation), NULL); | 
|  | 6717 | if (!woperation) { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6718 | PyErr_Clear(); | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 6719 | operation = NULL; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6720 | goto normal; | 
|  | 6721 | } | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6722 | } | 
| Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 6723 |  | 
|  | 6724 | Py_BEGIN_ALLOW_THREADS | 
|  | 6725 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, | 
|  | 6726 | PyUnicode_AS_UNICODE(unipath), | 
|  | 6727 | NULL, NULL, SW_SHOWNORMAL); | 
|  | 6728 | Py_END_ALLOW_THREADS | 
|  | 6729 |  | 
|  | 6730 | Py_XDECREF(woperation); | 
|  | 6731 | if (rc <= (HINSTANCE)32) { | 
|  | 6732 | PyObject *errval = win32_error_unicode("startfile", | 
|  | 6733 | PyUnicode_AS_UNICODE(unipath)); | 
|  | 6734 | return errval; | 
|  | 6735 | } | 
|  | 6736 | Py_INCREF(Py_None); | 
|  | 6737 | return Py_None; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6738 |  | 
|  | 6739 | normal: | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6740 | if (!PyArg_ParseTuple(args, "O&|s:startfile", | 
|  | 6741 | PyUnicode_FSConverter, &ofilepath, | 
| Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6742 | &operation)) | 
| Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6743 | return NULL; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6744 | filepath = bytes2str(ofilepath, 1); | 
| Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6745 | Py_BEGIN_ALLOW_THREADS | 
| Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6746 | rc = ShellExecute((HWND)0, operation, filepath, | 
|  | 6747 | NULL, NULL, SW_SHOWNORMAL); | 
| Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6748 | Py_END_ALLOW_THREADS | 
| Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6749 | if (rc <= (HINSTANCE)32) { | 
|  | 6750 | PyObject *errval = win32_error("startfile", filepath); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6751 | release_bytes(ofilepath); | 
| Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6752 | return errval; | 
|  | 6753 | } | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6754 | release_bytes(ofilepath); | 
| Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6755 | Py_INCREF(Py_None); | 
|  | 6756 | return Py_None; | 
|  | 6757 | } | 
|  | 6758 | #endif | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6759 |  | 
| Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6760 | #ifdef HAVE_GETLOADAVG | 
|  | 6761 | PyDoc_STRVAR(posix_getloadavg__doc__, | 
|  | 6762 | "getloadavg() -> (float, float, float)\n\n\ | 
|  | 6763 | Return the number of processes in the system run queue averaged over\n\ | 
|  | 6764 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ | 
|  | 6765 | was unobtainable"); | 
|  | 6766 |  | 
|  | 6767 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6768 | posix_getloadavg(PyObject *self, PyObject *noargs) | 
| Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6769 | { | 
|  | 6770 | double loadavg[3]; | 
| Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6771 | if (getloadavg(loadavg, 3)!=3) { | 
|  | 6772 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); | 
|  | 6773 | return NULL; | 
|  | 6774 | } else | 
|  | 6775 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); | 
|  | 6776 | } | 
|  | 6777 | #endif | 
|  | 6778 |  | 
| Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6779 | #ifdef MS_WINDOWS | 
|  | 6780 |  | 
|  | 6781 | PyDoc_STRVAR(win32_urandom__doc__, | 
|  | 6782 | "urandom(n) -> str\n\n\ | 
| Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6783 | Return n random bytes suitable for cryptographic use."); | 
| Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6784 |  | 
|  | 6785 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ | 
|  | 6786 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ | 
|  | 6787 | DWORD dwFlags ); | 
|  | 6788 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ | 
|  | 6789 | BYTE *pbBuffer ); | 
|  | 6790 |  | 
|  | 6791 | static CRYPTGENRANDOM pCryptGenRandom = NULL; | 
| Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 6792 | /* This handle is never explicitly released. Instead, the operating | 
|  | 6793 | system will release it when the process terminates. */ | 
| Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6794 | static HCRYPTPROV hCryptProv = 0; | 
|  | 6795 |  | 
| Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6796 | static PyObject* | 
|  | 6797 | win32_urandom(PyObject *self, PyObject *args) | 
| Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6798 | { | 
| Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6799 | int howMany; | 
|  | 6800 | PyObject* result; | 
| Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6801 |  | 
| Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6802 | /* Read arguments */ | 
| Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 6803 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) | 
| Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6804 | return NULL; | 
| Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 6805 | if (howMany < 0) | 
|  | 6806 | return PyErr_Format(PyExc_ValueError, | 
|  | 6807 | "negative argument not allowed"); | 
| Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6808 |  | 
| Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6809 | if (hCryptProv == 0) { | 
|  | 6810 | HINSTANCE hAdvAPI32 = NULL; | 
|  | 6811 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; | 
| Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6812 |  | 
| Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6813 | /* Obtain handle to the DLL containing CryptoAPI | 
|  | 6814 | This should not fail	*/ | 
|  | 6815 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); | 
|  | 6816 | if(hAdvAPI32 == NULL) | 
|  | 6817 | return win32_error("GetModuleHandle", NULL); | 
| Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6818 |  | 
| Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6819 | /* Obtain pointers to the CryptoAPI functions | 
|  | 6820 | This will fail on some early versions of Win95 */ | 
|  | 6821 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( | 
|  | 6822 | hAdvAPI32, | 
|  | 6823 | "CryptAcquireContextA"); | 
|  | 6824 | if (pCryptAcquireContext == NULL) | 
|  | 6825 | return PyErr_Format(PyExc_NotImplementedError, | 
|  | 6826 | "CryptAcquireContextA not found"); | 
| Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6827 |  | 
| Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6828 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( | 
|  | 6829 | hAdvAPI32, "CryptGenRandom"); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6830 | if (pCryptGenRandom == NULL) | 
| Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6831 | return PyErr_Format(PyExc_NotImplementedError, | 
|  | 6832 | "CryptGenRandom not found"); | 
| Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6833 |  | 
| Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6834 | /* Acquire context */ | 
|  | 6835 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, | 
|  | 6836 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) | 
|  | 6837 | return win32_error("CryptAcquireContext", NULL); | 
|  | 6838 | } | 
| Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6839 |  | 
| Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6840 | /* Allocate bytes */ | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6841 | result = PyBytes_FromStringAndSize(NULL, howMany); | 
| Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6842 | if (result != NULL) { | 
|  | 6843 | /* Get random data */ | 
| Amaury Forgeot d'Arc | a05ada3 | 2008-07-21 21:13:14 +0000 | [diff] [blame] | 6844 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ | 
| Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6845 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6846 | PyBytes_AS_STRING(result))) { | 
| Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6847 | Py_DECREF(result); | 
|  | 6848 | return win32_error("CryptGenRandom", NULL); | 
|  | 6849 | } | 
| Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6850 | } | 
| Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6851 | return result; | 
| Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6852 | } | 
|  | 6853 | #endif | 
| Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6854 |  | 
| Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6855 | PyDoc_STRVAR(device_encoding__doc__, | 
|  | 6856 | "device_encoding(fd) -> str\n\n\ | 
|  | 6857 | Return a string describing the encoding of the device\n\ | 
|  | 6858 | if the output is a terminal; else return None."); | 
|  | 6859 |  | 
|  | 6860 | static PyObject * | 
|  | 6861 | device_encoding(PyObject *self, PyObject *args) | 
|  | 6862 | { | 
|  | 6863 | int fd; | 
|  | 6864 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) | 
|  | 6865 | return NULL; | 
| Kristján Valur Jónsson | 649170b | 2009-03-24 14:15:49 +0000 | [diff] [blame] | 6866 | if (!_PyVerify_fd(fd) || !isatty(fd)) { | 
| Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6867 | Py_INCREF(Py_None); | 
|  | 6868 | return Py_None; | 
|  | 6869 | } | 
|  | 6870 | #if defined(MS_WINDOWS) || defined(MS_WIN64) | 
|  | 6871 | if (fd == 0) { | 
|  | 6872 | char buf[100]; | 
|  | 6873 | sprintf(buf, "cp%d", GetConsoleCP()); | 
|  | 6874 | return PyUnicode_FromString(buf); | 
|  | 6875 | } | 
|  | 6876 | if (fd == 1 || fd == 2) { | 
|  | 6877 | char buf[100]; | 
|  | 6878 | sprintf(buf, "cp%d", GetConsoleOutputCP()); | 
|  | 6879 | return PyUnicode_FromString(buf); | 
|  | 6880 | } | 
|  | 6881 | #elif defined(CODESET) | 
|  | 6882 | { | 
|  | 6883 | char *codeset = nl_langinfo(CODESET); | 
| Mark Dickinson | da2706b | 2008-12-11 18:03:03 +0000 | [diff] [blame] | 6884 | if (codeset != NULL && codeset[0] != 0) | 
| Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6885 | return PyUnicode_FromString(codeset); | 
|  | 6886 | } | 
|  | 6887 | #endif | 
|  | 6888 | Py_INCREF(Py_None); | 
|  | 6889 | return Py_None; | 
|  | 6890 | } | 
|  | 6891 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6892 | #ifdef __VMS | 
|  | 6893 | /* Use openssl random routine */ | 
|  | 6894 | #include <openssl/rand.h> | 
|  | 6895 | PyDoc_STRVAR(vms_urandom__doc__, | 
|  | 6896 | "urandom(n) -> str\n\n\ | 
| Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6897 | Return n random bytes suitable for cryptographic use."); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6898 |  | 
|  | 6899 | static PyObject* | 
|  | 6900 | vms_urandom(PyObject *self, PyObject *args) | 
|  | 6901 | { | 
|  | 6902 | int howMany; | 
|  | 6903 | PyObject* result; | 
|  | 6904 |  | 
|  | 6905 | /* Read arguments */ | 
|  | 6906 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) | 
|  | 6907 | return NULL; | 
|  | 6908 | if (howMany < 0) | 
|  | 6909 | return PyErr_Format(PyExc_ValueError, | 
|  | 6910 | "negative argument not allowed"); | 
|  | 6911 |  | 
|  | 6912 | /* Allocate bytes */ | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6913 | result = PyBytes_FromStringAndSize(NULL, howMany); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6914 | if (result != NULL) { | 
|  | 6915 | /* Get random data */ | 
|  | 6916 | if (RAND_pseudo_bytes((unsigned char*) | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6917 | PyBytes_AS_STRING(result), | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6918 | howMany) < 0) { | 
|  | 6919 | Py_DECREF(result); | 
|  | 6920 | return PyErr_Format(PyExc_ValueError, | 
|  | 6921 | "RAND_pseudo_bytes"); | 
|  | 6922 | } | 
|  | 6923 | } | 
|  | 6924 | return result; | 
|  | 6925 | } | 
|  | 6926 | #endif | 
|  | 6927 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6928 | static PyMethodDef posix_methods[] = { | 
|  | 6929 | {"access",	posix_access, METH_VARARGS, posix_access__doc__}, | 
|  | 6930 | #ifdef HAVE_TTYNAME | 
|  | 6931 | {"ttyname",	posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, | 
|  | 6932 | #endif | 
|  | 6933 | {"chdir",	posix_chdir, METH_VARARGS, posix_chdir__doc__}, | 
| Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6934 | #ifdef HAVE_CHFLAGS | 
|  | 6935 | {"chflags",	posix_chflags, METH_VARARGS, posix_chflags__doc__}, | 
|  | 6936 | #endif /* HAVE_CHFLAGS */ | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6937 | {"chmod",	posix_chmod, METH_VARARGS, posix_chmod__doc__}, | 
| Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 6938 | #ifdef HAVE_FCHMOD | 
|  | 6939 | {"fchmod",	posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, | 
|  | 6940 | #endif /* HAVE_FCHMOD */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6941 | #ifdef HAVE_CHOWN | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6942 | {"chown",	posix_chown, METH_VARARGS, posix_chown__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6943 | #endif /* HAVE_CHOWN */ | 
| Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 6944 | #ifdef HAVE_LCHMOD | 
|  | 6945 | {"lchmod",	posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, | 
|  | 6946 | #endif /* HAVE_LCHMOD */ | 
|  | 6947 | #ifdef HAVE_FCHOWN | 
|  | 6948 | {"fchown",	posix_fchown, METH_VARARGS, posix_fchown__doc__}, | 
|  | 6949 | #endif /* HAVE_FCHOWN */ | 
| Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 6950 | #ifdef HAVE_LCHFLAGS | 
|  | 6951 | {"lchflags",	posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, | 
|  | 6952 | #endif /* HAVE_LCHFLAGS */ | 
| Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 6953 | #ifdef HAVE_LCHOWN | 
|  | 6954 | {"lchown",	posix_lchown, METH_VARARGS, posix_lchown__doc__}, | 
|  | 6955 | #endif /* HAVE_LCHOWN */ | 
| Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 6956 | #ifdef HAVE_CHROOT | 
|  | 6957 | {"chroot",	posix_chroot, METH_VARARGS, posix_chroot__doc__}, | 
|  | 6958 | #endif | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6959 | #ifdef HAVE_CTERMID | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6960 | {"ctermid",	posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6961 | #endif | 
| Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6962 | #ifdef HAVE_GETCWD | 
| Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 6963 | {"getcwd",	(PyCFunction)posix_getcwd_unicode, | 
|  | 6964 | METH_NOARGS, posix_getcwd__doc__}, | 
|  | 6965 | {"getcwdb",	(PyCFunction)posix_getcwd_bytes, | 
|  | 6966 | METH_NOARGS, posix_getcwdb__doc__}, | 
| Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6967 | #endif | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6968 | #ifdef HAVE_LINK | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6969 | {"link",	posix_link, METH_VARARGS, posix_link__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6970 | #endif /* HAVE_LINK */ | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6971 | {"listdir",	posix_listdir, METH_VARARGS, posix_listdir__doc__}, | 
|  | 6972 | {"lstat",	posix_lstat, METH_VARARGS, posix_lstat__doc__}, | 
|  | 6973 | {"mkdir",	posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6974 | #ifdef HAVE_NICE | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6975 | {"nice",	posix_nice, METH_VARARGS, posix_nice__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6976 | #endif /* HAVE_NICE */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6977 | #ifdef HAVE_READLINK | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6978 | {"readlink",	posix_readlink, METH_VARARGS, posix_readlink__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6979 | #endif /* HAVE_READLINK */ | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6980 | {"rename",	posix_rename, METH_VARARGS, posix_rename__doc__}, | 
|  | 6981 | {"rmdir",	posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, | 
|  | 6982 | {"stat",	posix_stat, METH_VARARGS, posix_stat__doc__}, | 
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 6983 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6984 | #ifdef HAVE_SYMLINK | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6985 | {"symlink",	posix_symlink, METH_VARARGS, posix_symlink__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6986 | #endif /* HAVE_SYMLINK */ | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6987 | #ifdef HAVE_SYSTEM | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6988 | {"system",	posix_system, METH_VARARGS, posix_system__doc__}, | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6989 | #endif | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6990 | {"umask",	posix_umask, METH_VARARGS, posix_umask__doc__}, | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6991 | #ifdef HAVE_UNAME | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6992 | {"uname",	posix_uname, METH_NOARGS, posix_uname__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6993 | #endif /* HAVE_UNAME */ | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6994 | {"unlink",	posix_unlink, METH_VARARGS, posix_unlink__doc__}, | 
|  | 6995 | {"remove",	posix_unlink, METH_VARARGS, posix_remove__doc__}, | 
|  | 6996 | {"utime",	posix_utime, METH_VARARGS, posix_utime__doc__}, | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6997 | #ifdef HAVE_TIMES | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6998 | {"times",	posix_times, METH_NOARGS, posix_times__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6999 | #endif /* HAVE_TIMES */ | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7000 | {"_exit",	posix__exit, METH_VARARGS, posix__exit__doc__}, | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7001 | #ifdef HAVE_EXECV | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7002 | {"execv",	posix_execv, METH_VARARGS, posix_execv__doc__}, | 
|  | 7003 | {"execve",	posix_execve, METH_VARARGS, posix_execve__doc__}, | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7004 | #endif /* HAVE_EXECV */ | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7005 | #ifdef HAVE_SPAWNV | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7006 | {"spawnv",	posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, | 
|  | 7007 | {"spawnve",	posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7008 | #if defined(PYOS_OS2) | 
|  | 7009 | {"spawnvp",	posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, | 
|  | 7010 | {"spawnvpe",	posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, | 
|  | 7011 | #endif /* PYOS_OS2 */ | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7012 | #endif /* HAVE_SPAWNV */ | 
| Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7013 | #ifdef HAVE_FORK1 | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7014 | {"fork1",       posix_fork1, METH_NOARGS, posix_fork1__doc__}, | 
| Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7015 | #endif /* HAVE_FORK1 */ | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7016 | #ifdef HAVE_FORK | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7017 | {"fork",	posix_fork, METH_NOARGS, posix_fork__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7018 | #endif /* HAVE_FORK */ | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7019 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7020 | {"openpty",	posix_openpty, METH_NOARGS, posix_openpty__doc__}, | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7021 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7022 | #ifdef HAVE_FORKPTY | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7023 | {"forkpty",	posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7024 | #endif /* HAVE_FORKPTY */ | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7025 | #ifdef HAVE_GETEGID | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7026 | {"getegid",	posix_getegid, METH_NOARGS, posix_getegid__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7027 | #endif /* HAVE_GETEGID */ | 
|  | 7028 | #ifdef HAVE_GETEUID | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7029 | {"geteuid",	posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7030 | #endif /* HAVE_GETEUID */ | 
|  | 7031 | #ifdef HAVE_GETGID | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7032 | {"getgid",	posix_getgid, METH_NOARGS, posix_getgid__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7033 | #endif /* HAVE_GETGID */ | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7034 | #ifdef HAVE_GETGROUPS | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7035 | {"getgroups",	posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7036 | #endif | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7037 | {"getpid",	posix_getpid, METH_NOARGS, posix_getpid__doc__}, | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7038 | #ifdef HAVE_GETPGRP | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7039 | {"getpgrp",	posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7040 | #endif /* HAVE_GETPGRP */ | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7041 | #ifdef HAVE_GETPPID | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7042 | {"getppid",	posix_getppid, METH_NOARGS, posix_getppid__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7043 | #endif /* HAVE_GETPPID */ | 
|  | 7044 | #ifdef HAVE_GETUID | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7045 | {"getuid",	posix_getuid, METH_NOARGS, posix_getuid__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7046 | #endif /* HAVE_GETUID */ | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7047 | #ifdef HAVE_GETLOGIN | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7048 | {"getlogin",	posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7049 | #endif | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7050 | #ifdef HAVE_KILL | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7051 | {"kill",	posix_kill, METH_VARARGS, posix_kill__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7052 | #endif /* HAVE_KILL */ | 
| Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7053 | #ifdef HAVE_KILLPG | 
|  | 7054 | {"killpg",	posix_killpg, METH_VARARGS, posix_killpg__doc__}, | 
|  | 7055 | #endif /* HAVE_KILLPG */ | 
| Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7056 | #ifdef HAVE_PLOCK | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7057 | {"plock",	posix_plock, METH_VARARGS, posix_plock__doc__}, | 
| Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7058 | #endif /* HAVE_PLOCK */ | 
| Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 7059 | #ifdef MS_WINDOWS | 
|  | 7060 | {"startfile",	win32_startfile, METH_VARARGS, win32_startfile__doc__}, | 
|  | 7061 | #endif | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7062 | #ifdef HAVE_SETUID | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7063 | {"setuid",	posix_setuid, METH_VARARGS, posix_setuid__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7064 | #endif /* HAVE_SETUID */ | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7065 | #ifdef HAVE_SETEUID | 
|  | 7066 | {"seteuid",	posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, | 
|  | 7067 | #endif /* HAVE_SETEUID */ | 
|  | 7068 | #ifdef HAVE_SETEGID | 
|  | 7069 | {"setegid",	posix_setegid, METH_VARARGS, posix_setegid__doc__}, | 
|  | 7070 | #endif /* HAVE_SETEGID */ | 
|  | 7071 | #ifdef HAVE_SETREUID | 
|  | 7072 | {"setreuid",	posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, | 
|  | 7073 | #endif /* HAVE_SETREUID */ | 
|  | 7074 | #ifdef HAVE_SETREGID | 
|  | 7075 | {"setregid",	posix_setregid,	METH_VARARGS, posix_setregid__doc__}, | 
|  | 7076 | #endif /* HAVE_SETREGID */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7077 | #ifdef HAVE_SETGID | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7078 | {"setgid",	posix_setgid, METH_VARARGS, posix_setgid__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7079 | #endif /* HAVE_SETGID */ | 
| Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7080 | #ifdef HAVE_SETGROUPS | 
| Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 7081 | {"setgroups",	posix_setgroups, METH_O, posix_setgroups__doc__}, | 
| Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7082 | #endif /* HAVE_SETGROUPS */ | 
| Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7083 | #ifdef HAVE_GETPGID | 
|  | 7084 | {"getpgid",	posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, | 
|  | 7085 | #endif /* HAVE_GETPGID */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7086 | #ifdef HAVE_SETPGRP | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7087 | {"setpgrp",	posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7088 | #endif /* HAVE_SETPGRP */ | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7089 | #ifdef HAVE_WAIT | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7090 | {"wait",	posix_wait, METH_NOARGS, posix_wait__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7091 | #endif /* HAVE_WAIT */ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7092 | #ifdef HAVE_WAIT3 | 
|  | 7093 | {"wait3",	posix_wait3, METH_VARARGS, posix_wait3__doc__}, | 
|  | 7094 | #endif /* HAVE_WAIT3 */ | 
|  | 7095 | #ifdef HAVE_WAIT4 | 
|  | 7096 | {"wait4",	posix_wait4, METH_VARARGS, posix_wait4__doc__}, | 
|  | 7097 | #endif /* HAVE_WAIT4 */ | 
| Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7098 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7099 | {"waitpid",	posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7100 | #endif /* HAVE_WAITPID */ | 
| Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7101 | #ifdef HAVE_GETSID | 
|  | 7102 | {"getsid",	posix_getsid, METH_VARARGS, posix_getsid__doc__}, | 
|  | 7103 | #endif /* HAVE_GETSID */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7104 | #ifdef HAVE_SETSID | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7105 | {"setsid",	posix_setsid, METH_NOARGS, posix_setsid__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7106 | #endif /* HAVE_SETSID */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7107 | #ifdef HAVE_SETPGID | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7108 | {"setpgid",	posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7109 | #endif /* HAVE_SETPGID */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7110 | #ifdef HAVE_TCGETPGRP | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7111 | {"tcgetpgrp",	posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7112 | #endif /* HAVE_TCGETPGRP */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7113 | #ifdef HAVE_TCSETPGRP | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7114 | {"tcsetpgrp",	posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7115 | #endif /* HAVE_TCSETPGRP */ | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7116 | {"open",	posix_open, METH_VARARGS, posix_open__doc__}, | 
|  | 7117 | {"close",	posix_close, METH_VARARGS, posix_close__doc__}, | 
| Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7118 | {"closerange",	posix_closerange, METH_VARARGS, posix_closerange__doc__}, | 
| Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7119 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7120 | {"dup",		posix_dup, METH_VARARGS, posix_dup__doc__}, | 
|  | 7121 | {"dup2",	posix_dup2, METH_VARARGS, posix_dup2__doc__}, | 
|  | 7122 | {"lseek",	posix_lseek, METH_VARARGS, posix_lseek__doc__}, | 
|  | 7123 | {"read",	posix_read, METH_VARARGS, posix_read__doc__}, | 
|  | 7124 | {"write",	posix_write, METH_VARARGS, posix_write__doc__}, | 
|  | 7125 | {"fstat",	posix_fstat, METH_VARARGS, posix_fstat__doc__}, | 
| Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7126 | {"isatty",	posix_isatty, METH_VARARGS, posix_isatty__doc__}, | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7127 | #ifdef HAVE_PIPE | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7128 | {"pipe",	posix_pipe, METH_NOARGS, posix_pipe__doc__}, | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7129 | #endif | 
|  | 7130 | #ifdef HAVE_MKFIFO | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7131 | {"mkfifo",	posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7132 | #endif | 
| Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7133 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7134 | {"mknod",	posix_mknod, METH_VARARGS, posix_mknod__doc__}, | 
|  | 7135 | #endif | 
| Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7136 | #ifdef HAVE_DEVICE_MACROS | 
|  | 7137 | {"major",	posix_major, METH_VARARGS, posix_major__doc__}, | 
|  | 7138 | {"minor",	posix_minor, METH_VARARGS, posix_minor__doc__}, | 
|  | 7139 | {"makedev",	posix_makedev, METH_VARARGS, posix_makedev__doc__}, | 
|  | 7140 | #endif | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7141 | #ifdef HAVE_FTRUNCATE | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7142 | {"ftruncate",	posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7143 | #endif | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7144 | #ifdef HAVE_PUTENV | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7145 | {"putenv",	posix_putenv, METH_VARARGS, posix_putenv__doc__}, | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7146 | #endif | 
| Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7147 | #ifdef HAVE_UNSETENV | 
|  | 7148 | {"unsetenv",	posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, | 
|  | 7149 | #endif | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7150 | {"strerror",	posix_strerror, METH_VARARGS, posix_strerror__doc__}, | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7151 | #ifdef HAVE_FCHDIR | 
|  | 7152 | {"fchdir",	posix_fchdir, METH_O, posix_fchdir__doc__}, | 
|  | 7153 | #endif | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7154 | #ifdef HAVE_FSYNC | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7155 | {"fsync",       posix_fsync, METH_O, posix_fsync__doc__}, | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7156 | #endif | 
|  | 7157 | #ifdef HAVE_FDATASYNC | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7158 | {"fdatasync",   posix_fdatasync,  METH_O, posix_fdatasync__doc__}, | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7159 | #endif | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7160 | #ifdef HAVE_SYS_WAIT_H | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7161 | #ifdef WCOREDUMP | 
|  | 7162 | {"WCOREDUMP",	posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, | 
|  | 7163 | #endif /* WCOREDUMP */ | 
| Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7164 | #ifdef WIFCONTINUED | 
|  | 7165 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, | 
|  | 7166 | #endif /* WIFCONTINUED */ | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7167 | #ifdef WIFSTOPPED | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7168 | {"WIFSTOPPED",	posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7169 | #endif /* WIFSTOPPED */ | 
|  | 7170 | #ifdef WIFSIGNALED | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7171 | {"WIFSIGNALED",	posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7172 | #endif /* WIFSIGNALED */ | 
|  | 7173 | #ifdef WIFEXITED | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7174 | {"WIFEXITED",	posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7175 | #endif /* WIFEXITED */ | 
|  | 7176 | #ifdef WEXITSTATUS | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7177 | {"WEXITSTATUS",	posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7178 | #endif /* WEXITSTATUS */ | 
|  | 7179 | #ifdef WTERMSIG | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7180 | {"WTERMSIG",	posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7181 | #endif /* WTERMSIG */ | 
|  | 7182 | #ifdef WSTOPSIG | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7183 | {"WSTOPSIG",	posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7184 | #endif /* WSTOPSIG */ | 
|  | 7185 | #endif /* HAVE_SYS_WAIT_H */ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7186 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7187 | {"fstatvfs",	posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7188 | #endif | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7189 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7190 | {"statvfs",	posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7191 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7192 | #ifdef HAVE_CONFSTR | 
|  | 7193 | {"confstr",	posix_confstr, METH_VARARGS, posix_confstr__doc__}, | 
|  | 7194 | #endif | 
|  | 7195 | #ifdef HAVE_SYSCONF | 
|  | 7196 | {"sysconf",	posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, | 
|  | 7197 | #endif | 
|  | 7198 | #ifdef HAVE_FPATHCONF | 
|  | 7199 | {"fpathconf",	posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, | 
|  | 7200 | #endif | 
|  | 7201 | #ifdef HAVE_PATHCONF | 
|  | 7202 | {"pathconf",	posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, | 
|  | 7203 | #endif | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7204 | {"abort",	posix_abort, METH_NOARGS, posix_abort__doc__}, | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7205 | #ifdef MS_WINDOWS | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7206 | {"_getfullpathname",	posix__getfullpathname, METH_VARARGS, NULL}, | 
|  | 7207 | #endif | 
| Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7208 | #ifdef HAVE_GETLOADAVG | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7209 | {"getloadavg",	posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, | 
| Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7210 | #endif | 
| Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7211 | #ifdef MS_WINDOWS | 
|  | 7212 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, | 
|  | 7213 | #endif | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7214 | #ifdef __VMS | 
|  | 7215 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, | 
|  | 7216 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7217 | {NULL,		NULL}		 /* Sentinel */ | 
|  | 7218 | }; | 
|  | 7219 |  | 
|  | 7220 |  | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7221 | static int | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7222 | ins(PyObject *module, char *symbol, long value) | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7223 | { | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7224 | return PyModule_AddIntConstant(module, symbol, value); | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7225 | } | 
|  | 7226 |  | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7227 | #if defined(PYOS_OS2) | 
|  | 7228 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7229 | static int insertvalues(PyObject *module) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7230 | { | 
|  | 7231 | APIRET    rc; | 
|  | 7232 | ULONG     values[QSV_MAX+1]; | 
|  | 7233 | PyObject *v; | 
| Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7234 | char     *ver, tmp[50]; | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7235 |  | 
|  | 7236 | Py_BEGIN_ALLOW_THREADS | 
| Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7237 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7238 | Py_END_ALLOW_THREADS | 
|  | 7239 |  | 
|  | 7240 | if (rc != NO_ERROR) { | 
|  | 7241 | os2_error(rc); | 
|  | 7242 | return -1; | 
|  | 7243 | } | 
|  | 7244 |  | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7245 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; | 
|  | 7246 | if (ins(module, "memkernel",    values[QSV_TOTRESMEM])) return -1; | 
|  | 7247 | if (ins(module, "memvirtual",   values[QSV_TOTAVAILMEM])) return -1; | 
|  | 7248 | if (ins(module, "maxpathlen",   values[QSV_MAX_PATH_LENGTH])) return -1; | 
|  | 7249 | if (ins(module, "maxnamelen",   values[QSV_MAX_COMP_LENGTH])) return -1; | 
|  | 7250 | if (ins(module, "revision",     values[QSV_VERSION_REVISION])) return -1; | 
|  | 7251 | if (ins(module, "timeslice",    values[QSV_MIN_SLICE])) return -1; | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7252 |  | 
|  | 7253 | switch (values[QSV_VERSION_MINOR]) { | 
|  | 7254 | case 0:  ver = "2.00"; break; | 
|  | 7255 | case 10: ver = "2.10"; break; | 
|  | 7256 | case 11: ver = "2.11"; break; | 
|  | 7257 | case 30: ver = "3.00"; break; | 
|  | 7258 | case 40: ver = "4.00"; break; | 
|  | 7259 | case 50: ver = "5.00"; break; | 
|  | 7260 | default: | 
| Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7261 | PyOS_snprintf(tmp, sizeof(tmp), | 
|  | 7262 | "%d-%d", values[QSV_VERSION_MAJOR], | 
|  | 7263 | values[QSV_VERSION_MINOR]); | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7264 | ver = &tmp[0]; | 
|  | 7265 | } | 
|  | 7266 |  | 
|  | 7267 | /* Add Indicator of the Version of the Operating System */ | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7268 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7269 | return -1; | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7270 |  | 
|  | 7271 | /* Add Indicator of Which Drive was Used to Boot the System */ | 
|  | 7272 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; | 
|  | 7273 | tmp[1] = ':'; | 
|  | 7274 | tmp[2] = '\0'; | 
|  | 7275 |  | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7276 | return PyModule_AddStringConstant(module, "bootdrive", tmp); | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7277 | } | 
|  | 7278 | #endif | 
|  | 7279 |  | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7280 | static int | 
| Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7281 | all_ins(PyObject *d) | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7282 | { | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7283 | #ifdef F_OK | 
|  | 7284 | if (ins(d, "F_OK", (long)F_OK)) return -1; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7285 | #endif | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7286 | #ifdef R_OK | 
|  | 7287 | if (ins(d, "R_OK", (long)R_OK)) return -1; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7288 | #endif | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7289 | #ifdef W_OK | 
|  | 7290 | if (ins(d, "W_OK", (long)W_OK)) return -1; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7291 | #endif | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7292 | #ifdef X_OK | 
|  | 7293 | if (ins(d, "X_OK", (long)X_OK)) return -1; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7294 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7295 | #ifdef NGROUPS_MAX | 
|  | 7296 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; | 
|  | 7297 | #endif | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7298 | #ifdef TMP_MAX | 
|  | 7299 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; | 
|  | 7300 | #endif | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7301 | #ifdef WCONTINUED | 
|  | 7302 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; | 
|  | 7303 | #endif | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7304 | #ifdef WNOHANG | 
|  | 7305 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7306 | #endif | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7307 | #ifdef WUNTRACED | 
|  | 7308 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; | 
|  | 7309 | #endif | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7310 | #ifdef O_RDONLY | 
|  | 7311 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; | 
|  | 7312 | #endif | 
|  | 7313 | #ifdef O_WRONLY | 
|  | 7314 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; | 
|  | 7315 | #endif | 
|  | 7316 | #ifdef O_RDWR | 
|  | 7317 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; | 
|  | 7318 | #endif | 
|  | 7319 | #ifdef O_NDELAY | 
|  | 7320 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; | 
|  | 7321 | #endif | 
|  | 7322 | #ifdef O_NONBLOCK | 
|  | 7323 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; | 
|  | 7324 | #endif | 
|  | 7325 | #ifdef O_APPEND | 
|  | 7326 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; | 
|  | 7327 | #endif | 
|  | 7328 | #ifdef O_DSYNC | 
|  | 7329 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; | 
|  | 7330 | #endif | 
|  | 7331 | #ifdef O_RSYNC | 
|  | 7332 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; | 
|  | 7333 | #endif | 
|  | 7334 | #ifdef O_SYNC | 
|  | 7335 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; | 
|  | 7336 | #endif | 
|  | 7337 | #ifdef O_NOCTTY | 
|  | 7338 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; | 
|  | 7339 | #endif | 
|  | 7340 | #ifdef O_CREAT | 
|  | 7341 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; | 
|  | 7342 | #endif | 
|  | 7343 | #ifdef O_EXCL | 
|  | 7344 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; | 
|  | 7345 | #endif | 
|  | 7346 | #ifdef O_TRUNC | 
|  | 7347 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; | 
|  | 7348 | #endif | 
| Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7349 | #ifdef O_BINARY | 
|  | 7350 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; | 
|  | 7351 | #endif | 
|  | 7352 | #ifdef O_TEXT | 
|  | 7353 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; | 
|  | 7354 | #endif | 
| Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7355 | #ifdef O_LARGEFILE | 
|  | 7356 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; | 
|  | 7357 | #endif | 
| Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7358 | #ifdef O_SHLOCK | 
|  | 7359 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; | 
|  | 7360 | #endif | 
|  | 7361 | #ifdef O_EXLOCK | 
|  | 7362 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; | 
|  | 7363 | #endif | 
| Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7364 |  | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7365 | /* MS Windows */ | 
|  | 7366 | #ifdef O_NOINHERIT | 
|  | 7367 | /* Don't inherit in child processes. */ | 
|  | 7368 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; | 
|  | 7369 | #endif | 
|  | 7370 | #ifdef _O_SHORT_LIVED | 
|  | 7371 | /* Optimize for short life (keep in memory). */ | 
|  | 7372 | /* MS forgot to define this one with a non-underscore form too. */ | 
|  | 7373 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; | 
|  | 7374 | #endif | 
|  | 7375 | #ifdef O_TEMPORARY | 
|  | 7376 | /* Automatically delete when last handle is closed. */ | 
|  | 7377 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; | 
|  | 7378 | #endif | 
|  | 7379 | #ifdef O_RANDOM | 
|  | 7380 | /* Optimize for random access. */ | 
|  | 7381 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; | 
|  | 7382 | #endif | 
|  | 7383 | #ifdef O_SEQUENTIAL | 
|  | 7384 | /* Optimize for sequential access. */ | 
|  | 7385 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; | 
|  | 7386 | #endif | 
|  | 7387 |  | 
| Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7388 | /* GNU extensions. */ | 
| Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 7389 | #ifdef O_ASYNC | 
|  | 7390 | /* Send a SIGIO signal whenever input or output | 
|  | 7391 | becomes available on file descriptor */ | 
|  | 7392 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; | 
|  | 7393 | #endif | 
| Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7394 | #ifdef O_DIRECT | 
|  | 7395 | /* Direct disk access. */ | 
|  | 7396 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; | 
|  | 7397 | #endif | 
|  | 7398 | #ifdef O_DIRECTORY | 
|  | 7399 | /* Must be a directory.	 */ | 
|  | 7400 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; | 
|  | 7401 | #endif | 
|  | 7402 | #ifdef O_NOFOLLOW | 
|  | 7403 | /* Do not follow links.	 */ | 
|  | 7404 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; | 
|  | 7405 | #endif | 
| Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 7406 | #ifdef O_NOATIME | 
|  | 7407 | /* Do not update the access time. */ | 
|  | 7408 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; | 
|  | 7409 | #endif | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7410 |  | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7411 | /* These come from sysexits.h */ | 
|  | 7412 | #ifdef EX_OK | 
|  | 7413 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7414 | #endif /* EX_OK */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7415 | #ifdef EX_USAGE | 
|  | 7416 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7417 | #endif /* EX_USAGE */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7418 | #ifdef EX_DATAERR | 
|  | 7419 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7420 | #endif /* EX_DATAERR */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7421 | #ifdef EX_NOINPUT | 
|  | 7422 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7423 | #endif /* EX_NOINPUT */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7424 | #ifdef EX_NOUSER | 
|  | 7425 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7426 | #endif /* EX_NOUSER */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7427 | #ifdef EX_NOHOST | 
|  | 7428 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7429 | #endif /* EX_NOHOST */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7430 | #ifdef EX_UNAVAILABLE | 
|  | 7431 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7432 | #endif /* EX_UNAVAILABLE */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7433 | #ifdef EX_SOFTWARE | 
|  | 7434 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7435 | #endif /* EX_SOFTWARE */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7436 | #ifdef EX_OSERR | 
|  | 7437 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7438 | #endif /* EX_OSERR */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7439 | #ifdef EX_OSFILE | 
|  | 7440 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7441 | #endif /* EX_OSFILE */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7442 | #ifdef EX_CANTCREAT | 
|  | 7443 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7444 | #endif /* EX_CANTCREAT */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7445 | #ifdef EX_IOERR | 
|  | 7446 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7447 | #endif /* EX_IOERR */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7448 | #ifdef EX_TEMPFAIL | 
|  | 7449 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7450 | #endif /* EX_TEMPFAIL */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7451 | #ifdef EX_PROTOCOL | 
|  | 7452 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7453 | #endif /* EX_PROTOCOL */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7454 | #ifdef EX_NOPERM | 
|  | 7455 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7456 | #endif /* EX_NOPERM */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7457 | #ifdef EX_CONFIG | 
|  | 7458 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7459 | #endif /* EX_CONFIG */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7460 | #ifdef EX_NOTFOUND | 
|  | 7461 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7462 | #endif /* EX_NOTFOUND */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7463 |  | 
| Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7464 | #ifdef HAVE_SPAWNV | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7465 | #if defined(PYOS_OS2) && defined(PYCC_GCC) | 
|  | 7466 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; | 
|  | 7467 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; | 
|  | 7468 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; | 
|  | 7469 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; | 
|  | 7470 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; | 
|  | 7471 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; | 
|  | 7472 | if (ins(d, "P_PM", (long)P_PM)) return -1; | 
|  | 7473 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; | 
|  | 7474 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; | 
|  | 7475 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; | 
|  | 7476 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; | 
|  | 7477 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; | 
|  | 7478 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; | 
|  | 7479 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; | 
|  | 7480 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; | 
|  | 7481 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; | 
|  | 7482 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; | 
|  | 7483 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; | 
|  | 7484 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; | 
|  | 7485 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; | 
|  | 7486 | #else | 
| Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7487 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; | 
|  | 7488 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; | 
|  | 7489 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; | 
|  | 7490 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; | 
|  | 7491 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; | 
| Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7492 | #endif | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7493 | #endif | 
| Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7494 |  | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7495 | #if defined(PYOS_OS2) | 
|  | 7496 | if (insertvalues(d)) return -1; | 
|  | 7497 | #endif | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7498 | return 0; | 
|  | 7499 | } | 
|  | 7500 |  | 
|  | 7501 |  | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7502 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7503 | #define INITFUNC PyInit_nt | 
| Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7504 | #define MODNAME "nt" | 
| Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7505 |  | 
|  | 7506 | #elif defined(PYOS_OS2) | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7507 | #define INITFUNC PyInit_os2 | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7508 | #define MODNAME "os2" | 
| Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7509 |  | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7510 | #else | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7511 | #define INITFUNC PyInit_posix | 
| Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7512 | #define MODNAME "posix" | 
|  | 7513 | #endif | 
|  | 7514 |  | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7515 | static struct PyModuleDef posixmodule = { | 
|  | 7516 | PyModuleDef_HEAD_INIT, | 
|  | 7517 | MODNAME, | 
|  | 7518 | posix__doc__, | 
|  | 7519 | -1, | 
|  | 7520 | posix_methods, | 
|  | 7521 | NULL, | 
|  | 7522 | NULL, | 
|  | 7523 | NULL, | 
|  | 7524 | NULL | 
|  | 7525 | }; | 
|  | 7526 |  | 
|  | 7527 |  | 
| Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7528 | PyMODINIT_FUNC | 
| Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7529 | INITFUNC(void) | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7530 | { | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7531 | PyObject *m, *v; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7532 |  | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7533 | m = PyModule_Create(&posixmodule); | 
| Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 7534 | if (m == NULL) | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7535 | return NULL; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7536 |  | 
| Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7537 | /* Initialize environ dictionary */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7538 | v = convertenviron(); | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7539 | Py_XINCREF(v); | 
|  | 7540 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7541 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7542 | Py_DECREF(v); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7543 |  | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7544 | if (all_ins(m)) | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7545 | return NULL; | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7546 |  | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7547 | if (setup_confname_tables(m)) | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7548 | return NULL; | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7549 |  | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7550 | Py_INCREF(PyExc_OSError); | 
|  | 7551 | PyModule_AddObject(m, "error", PyExc_OSError); | 
| Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7552 |  | 
| Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7553 | #ifdef HAVE_PUTENV | 
| Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7554 | if (posix_putenv_garbage == NULL) | 
|  | 7555 | posix_putenv_garbage = PyDict_New(); | 
| Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7556 | #endif | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7557 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7558 | if (!initialized) { | 
|  | 7559 | stat_result_desc.name = MODNAME ".stat_result"; | 
|  | 7560 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; | 
|  | 7561 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; | 
|  | 7562 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; | 
|  | 7563 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); | 
|  | 7564 | structseq_new = StatResultType.tp_new; | 
|  | 7565 | StatResultType.tp_new = statresult_new; | 
|  | 7566 |  | 
|  | 7567 | statvfs_result_desc.name = MODNAME ".statvfs_result"; | 
|  | 7568 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); | 
| Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 7569 | #ifdef NEED_TICKS_PER_SECOND | 
|  | 7570 | #  if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) | 
|  | 7571 | ticks_per_second = sysconf(_SC_CLK_TCK); | 
|  | 7572 | #  elif defined(HZ) | 
|  | 7573 | ticks_per_second = HZ; | 
|  | 7574 | #  else | 
|  | 7575 | ticks_per_second = 60; /* magic fallback value; may be bogus */ | 
|  | 7576 | #  endif | 
|  | 7577 | #endif | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7578 | } | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7579 | Py_INCREF((PyObject*) &StatResultType); | 
|  | 7580 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7581 | Py_INCREF((PyObject*) &StatVFSResultType); | 
|  | 7582 | PyModule_AddObject(m, "statvfs_result", | 
|  | 7583 | (PyObject*) &StatVFSResultType); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7584 | initialized = 1; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7585 |  | 
|  | 7586 | #ifdef __APPLE__ | 
|  | 7587 | /* | 
|  | 7588 | * Step 2 of weak-linking support on Mac OS X. | 
|  | 7589 | * | 
|  | 7590 | * The code below removes functions that are not available on the | 
|  | 7591 | * currently active platform. | 
|  | 7592 | * | 
|  | 7593 | * This block allow one to use a python binary that was build on | 
|  | 7594 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on | 
|  | 7595 | * OSX 10.4. | 
|  | 7596 | */ | 
|  | 7597 | #ifdef HAVE_FSTATVFS | 
|  | 7598 | if (fstatvfs == NULL) { | 
|  | 7599 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7600 | return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7601 | } | 
|  | 7602 | } | 
|  | 7603 | #endif /* HAVE_FSTATVFS */ | 
|  | 7604 |  | 
|  | 7605 | #ifdef HAVE_STATVFS | 
|  | 7606 | if (statvfs == NULL) { | 
|  | 7607 | if (PyObject_DelAttrString(m, "statvfs") == -1) { | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7608 | return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7609 | } | 
|  | 7610 | } | 
|  | 7611 | #endif /* HAVE_STATVFS */ | 
|  | 7612 |  | 
|  | 7613 | # ifdef HAVE_LCHOWN | 
|  | 7614 | if (lchown == NULL) { | 
|  | 7615 | if (PyObject_DelAttrString(m, "lchown") == -1) { | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7616 | return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7617 | } | 
|  | 7618 | } | 
|  | 7619 | #endif /* HAVE_LCHOWN */ | 
|  | 7620 |  | 
|  | 7621 |  | 
|  | 7622 | #endif /* __APPLE__ */ | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7623 | return m; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7624 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7625 | } | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7626 |  | 
|  | 7627 | #ifdef __cplusplus | 
|  | 7628 | } | 
|  | 7629 | #endif |