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 | |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 14 | #ifdef __APPLE__ |
| 15 | /* |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 16 | * Step 1 of support for weak-linking a number of symbols existing on |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 17 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 18 | * at the end of this file for more information. |
| 19 | */ |
| 20 | # pragma weak lchown |
| 21 | # pragma weak statvfs |
| 22 | # pragma weak fstatvfs |
| 23 | |
| 24 | #endif /* __APPLE__ */ |
| 25 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 26 | #define PY_SSIZE_T_CLEAN |
| 27 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 28 | #include "Python.h" |
| 29 | #include "structseq.h" |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 30 | #ifndef MS_WINDOWS |
| 31 | #include "posixmodule.h" |
| 32 | #endif |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 33 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 34 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 35 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 36 | #endif /* defined(__VMS) */ |
| 37 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 38 | #ifdef __cplusplus |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 42 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 43 | "This module provides access to operating system functionality that is\n\ |
| 44 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 45 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 46 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 48 | #ifndef Py_USING_UNICODE |
| 49 | /* This is used in signatures of functions. */ |
| 50 | #define Py_UNICODE void |
| 51 | #endif |
| 52 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 53 | #if defined(PYOS_OS2) |
| 54 | #define INCL_DOS |
| 55 | #define INCL_DOSERRORS |
| 56 | #define INCL_DOSPROCESS |
| 57 | #define INCL_NOPMAPI |
| 58 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 59 | #if defined(PYCC_GCC) |
| 60 | #include <ctype.h> |
| 61 | #include <io.h> |
| 62 | #include <stdio.h> |
| 63 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 64 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 65 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 66 | #endif |
| 67 | |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 68 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 69 | #include <sys/types.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 70 | #endif /* HAVE_SYS_TYPES_H */ |
| 71 | |
| 72 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 73 | #include <sys/stat.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 74 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 76 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 77 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 78 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 79 | |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 80 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 81 | #include <signal.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 82 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 84 | #ifdef HAVE_FCNTL_H |
| 85 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 86 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 88 | #ifdef HAVE_GRP_H |
| 89 | #include <grp.h> |
| 90 | #endif |
| 91 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 92 | #ifdef HAVE_SYSEXITS_H |
| 93 | #include <sysexits.h> |
| 94 | #endif /* HAVE_SYSEXITS_H */ |
| 95 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 96 | #ifdef HAVE_SYS_LOADAVG_H |
| 97 | #include <sys/loadavg.h> |
| 98 | #endif |
| 99 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 100 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 101 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 102 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 103 | #include <process.h> |
| 104 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 105 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 106 | #define HAVE_GETCWD 1 |
| 107 | #define HAVE_OPENDIR 1 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 108 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 109 | #if defined(__OS2__) |
| 110 | #define HAVE_EXECV 1 |
| 111 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 112 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 113 | #include <process.h> |
| 114 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 115 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 116 | #define HAVE_EXECV 1 |
| 117 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 118 | #define HAVE_OPENDIR 1 |
| 119 | #define HAVE_PIPE 1 |
| 120 | #define HAVE_POPEN 1 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 121 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 122 | #define HAVE_WAIT 1 |
| 123 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 124 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 125 | #define HAVE_GETCWD 1 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 126 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 127 | #define HAVE_EXECV 1 |
| 128 | #define HAVE_PIPE 1 |
| 129 | #define HAVE_POPEN 1 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 130 | #define HAVE_SYSTEM 1 |
| 131 | #define HAVE_CWAIT 1 |
| 132 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 133 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 134 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 135 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 136 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 137 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 138 | /* Unix functions that the configure script doesn't check for */ |
| 139 | #define HAVE_EXECV 1 |
| 140 | #define HAVE_FORK 1 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 141 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 142 | #define HAVE_FORK1 1 |
| 143 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 144 | #define HAVE_GETCWD 1 |
| 145 | #define HAVE_GETEGID 1 |
| 146 | #define HAVE_GETEUID 1 |
| 147 | #define HAVE_GETGID 1 |
| 148 | #define HAVE_GETPPID 1 |
| 149 | #define HAVE_GETUID 1 |
| 150 | #define HAVE_KILL 1 |
| 151 | #define HAVE_OPENDIR 1 |
| 152 | #define HAVE_PIPE 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 153 | #ifndef __rtems__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 154 | #define HAVE_POPEN 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 155 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 156 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 157 | #define HAVE_WAIT 1 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 158 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 159 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 160 | #endif /* _MSC_VER */ |
| 161 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 162 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 163 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 165 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 166 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 167 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 168 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 169 | (default) */ |
| 170 | extern char *ctermid_r(char *); |
| 171 | #endif |
| 172 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 173 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 174 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 175 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 176 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 177 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 178 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 179 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 180 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 181 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 182 | #endif |
| 183 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 184 | extern int chdir(char *); |
| 185 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 186 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 187 | extern int chdir(const char *); |
| 188 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 189 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 190 | #ifdef __BORLANDC__ |
| 191 | extern int chmod(const char *, int); |
| 192 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 193 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 194 | #endif |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 195 | /*#ifdef HAVE_FCHMOD |
| 196 | extern int fchmod(int, mode_t); |
| 197 | #endif*/ |
| 198 | /*#ifdef HAVE_LCHMOD |
| 199 | extern int lchmod(const char *, mode_t); |
| 200 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 201 | extern int chown(const char *, uid_t, gid_t); |
| 202 | extern char *getcwd(char *, int); |
| 203 | extern char *strerror(int); |
| 204 | extern int link(const char *, const char *); |
| 205 | extern int rename(const char *, const char *); |
| 206 | extern int stat(const char *, struct stat *); |
| 207 | extern int unlink(const char *); |
| 208 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 210 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 211 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 212 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 213 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 214 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 216 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 217 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 218 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 219 | #ifdef HAVE_UTIME_H |
| 220 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 221 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 223 | #ifdef HAVE_SYS_UTIME_H |
| 224 | #include <sys/utime.h> |
| 225 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 226 | #endif /* HAVE_SYS_UTIME_H */ |
| 227 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | #ifdef HAVE_SYS_TIMES_H |
| 229 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 230 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 231 | |
| 232 | #ifdef HAVE_SYS_PARAM_H |
| 233 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 234 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | |
| 236 | #ifdef HAVE_SYS_UTSNAME_H |
| 237 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 238 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 240 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 242 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 243 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 244 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 245 | #include <direct.h> |
| 246 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 247 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 248 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 249 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 250 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 251 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 252 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 253 | #endif |
| 254 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 255 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 256 | #endif |
| 257 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 258 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 259 | #endif |
| 260 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 262 | #ifdef _MSC_VER |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 263 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 264 | #include <direct.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 265 | #endif |
| 266 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 267 | #include <io.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 268 | #endif |
| 269 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 270 | #include <process.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 271 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 272 | #include "osdefs.h" |
Kristján Valur Jónsson | feab334 | 2009-04-01 16:08:34 +0000 | [diff] [blame] | 273 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 274 | #include <windows.h> |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 275 | #include <shellapi.h> /* for ShellExecute() */ |
| 276 | #define popen _popen |
| 277 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 278 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 279 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 280 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 281 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 282 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 283 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 284 | #ifndef MAXPATHLEN |
Thomas Wouters | 1ddba60 | 2006-04-25 15:29:46 +0000 | [diff] [blame] | 285 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 286 | #define MAXPATHLEN PATH_MAX |
| 287 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 288 | #define MAXPATHLEN 1024 |
Thomas Wouters | 1ddba60 | 2006-04-25 15:29:46 +0000 | [diff] [blame] | 289 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 290 | #endif /* MAXPATHLEN */ |
| 291 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 292 | #ifdef UNION_WAIT |
| 293 | /* Emulate some macros on systems that have a union instead of macros */ |
| 294 | |
| 295 | #ifndef WIFEXITED |
| 296 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 297 | #endif |
| 298 | |
| 299 | #ifndef WEXITSTATUS |
| 300 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 301 | #endif |
| 302 | |
| 303 | #ifndef WTERMSIG |
| 304 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 305 | #endif |
| 306 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 307 | #define WAIT_TYPE union wait |
| 308 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 309 | |
| 310 | #else /* !UNION_WAIT */ |
| 311 | #define WAIT_TYPE int |
| 312 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 313 | #endif /* UNION_WAIT */ |
| 314 | |
Antoine Pitrou | 5e858fe | 2009-05-23 15:37:45 +0000 | [diff] [blame] | 315 | /* Issue #1983: pid_t can be longer than a C long on some systems */ |
Antoine Pitrou | 4fe3858 | 2009-05-24 12:15:04 +0000 | [diff] [blame] | 316 | #if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT |
Antoine Pitrou | 5e858fe | 2009-05-23 15:37:45 +0000 | [diff] [blame] | 317 | #define PARSE_PID "i" |
| 318 | #define PyLong_FromPid PyInt_FromLong |
| 319 | #define PyLong_AsPid PyInt_AsLong |
| 320 | #elif SIZEOF_PID_T == SIZEOF_LONG |
| 321 | #define PARSE_PID "l" |
| 322 | #define PyLong_FromPid PyInt_FromLong |
| 323 | #define PyLong_AsPid PyInt_AsLong |
| 324 | #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG |
| 325 | #define PARSE_PID "L" |
| 326 | #define PyLong_FromPid PyLong_FromLongLong |
| 327 | #define PyLong_AsPid PyInt_AsLongLong |
| 328 | #else |
| 329 | #error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)" |
Antoine Pitrou | 5e858fe | 2009-05-23 15:37:45 +0000 | [diff] [blame] | 330 | #endif /* SIZEOF_PID_T */ |
| 331 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 332 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 333 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 334 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 335 | #define USE_CTERMID_R |
| 336 | #endif |
| 337 | |
| 338 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 339 | #define USE_TMPNAM_R |
| 340 | #endif |
| 341 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 342 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 343 | #include <sys/mkdev.h> |
| 344 | #else |
| 345 | #if defined(MAJOR_IN_SYSMACROS) |
| 346 | #include <sys/sysmacros.h> |
| 347 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 348 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 349 | #include <sys/mkdev.h> |
| 350 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 351 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 352 | |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 353 | |
| 354 | #ifndef MS_WINDOWS |
| 355 | PyObject * |
| 356 | _PyInt_FromUid(uid_t uid) |
| 357 | { |
| 358 | if (uid <= LONG_MAX) |
| 359 | return PyInt_FromLong(uid); |
Benjamin Peterson | 4d7fc3c | 2013-03-23 15:35:24 -0500 | [diff] [blame^] | 360 | return PyLong_FromUnsignedLong(uid); |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | PyObject * |
| 364 | _PyInt_FromGid(gid_t gid) |
| 365 | { |
| 366 | if (gid <= LONG_MAX) |
| 367 | return PyInt_FromLong(gid); |
Benjamin Peterson | 4d7fc3c | 2013-03-23 15:35:24 -0500 | [diff] [blame^] | 368 | return PyLong_FromUnsignedLong(gid); |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | int |
| 372 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 373 | { |
| 374 | int overflow; |
| 375 | long result; |
| 376 | if (PyFloat_Check(obj)) { |
| 377 | PyErr_SetString(PyExc_TypeError, |
| 378 | "integer argument expected, got float"); |
| 379 | return 0; |
| 380 | } |
| 381 | result = PyLong_AsLongAndOverflow(obj, &overflow); |
| 382 | if (overflow < 0) |
| 383 | goto OverflowDown; |
| 384 | if (!overflow && result == -1) { |
| 385 | /* error or -1 */ |
| 386 | if (PyErr_Occurred()) |
| 387 | return 0; |
| 388 | *(uid_t *)p = (uid_t)-1; |
| 389 | } |
| 390 | else { |
| 391 | /* unsigned uid_t */ |
| 392 | unsigned long uresult; |
| 393 | if (overflow > 0) { |
| 394 | uresult = PyLong_AsUnsignedLong(obj); |
| 395 | if (PyErr_Occurred()) { |
| 396 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 397 | goto OverflowUp; |
| 398 | return 0; |
| 399 | } |
| 400 | if ((uid_t)uresult == (uid_t)-1) |
| 401 | goto OverflowUp; |
| 402 | } else { |
| 403 | if (result < 0) |
| 404 | goto OverflowDown; |
| 405 | uresult = result; |
| 406 | } |
| 407 | if (sizeof(uid_t) < sizeof(long) && |
| 408 | (unsigned long)(uid_t)uresult != uresult) |
| 409 | goto OverflowUp; |
| 410 | *(uid_t *)p = (uid_t)uresult; |
| 411 | } |
| 412 | return 1; |
| 413 | |
| 414 | OverflowDown: |
| 415 | PyErr_SetString(PyExc_OverflowError, |
| 416 | "user id is less than minimum"); |
| 417 | return 0; |
| 418 | |
| 419 | OverflowUp: |
| 420 | PyErr_SetString(PyExc_OverflowError, |
| 421 | "user id is greater than maximum"); |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | int |
| 426 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 427 | { |
| 428 | int overflow; |
| 429 | long result; |
| 430 | if (PyFloat_Check(obj)) { |
| 431 | PyErr_SetString(PyExc_TypeError, |
| 432 | "integer argument expected, got float"); |
| 433 | return 0; |
| 434 | } |
| 435 | result = PyLong_AsLongAndOverflow(obj, &overflow); |
| 436 | if (overflow < 0) |
| 437 | goto OverflowDown; |
| 438 | if (!overflow && result == -1) { |
| 439 | /* error or -1 */ |
| 440 | if (PyErr_Occurred()) |
| 441 | return 0; |
| 442 | *(gid_t *)p = (gid_t)-1; |
| 443 | } |
| 444 | else { |
| 445 | /* unsigned gid_t */ |
| 446 | unsigned long uresult; |
| 447 | if (overflow > 0) { |
| 448 | uresult = PyLong_AsUnsignedLong(obj); |
| 449 | if (PyErr_Occurred()) { |
| 450 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 451 | goto OverflowUp; |
| 452 | return 0; |
| 453 | } |
| 454 | if ((gid_t)uresult == (gid_t)-1) |
| 455 | goto OverflowUp; |
| 456 | } else { |
| 457 | if (result < 0) |
| 458 | goto OverflowDown; |
| 459 | uresult = result; |
| 460 | } |
| 461 | if (sizeof(gid_t) < sizeof(long) && |
| 462 | (unsigned long)(gid_t)uresult != uresult) |
| 463 | goto OverflowUp; |
| 464 | *(gid_t *)p = (gid_t)uresult; |
| 465 | } |
| 466 | return 1; |
| 467 | |
| 468 | OverflowDown: |
| 469 | PyErr_SetString(PyExc_OverflowError, |
| 470 | "group id is less than minimum"); |
| 471 | return 0; |
| 472 | |
| 473 | OverflowUp: |
| 474 | PyErr_SetString(PyExc_OverflowError, |
| 475 | "group id is greater than maximum"); |
| 476 | return 0; |
| 477 | } |
| 478 | #endif /* MS_WINDOWS */ |
| 479 | |
| 480 | |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 481 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 482 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
Andrew Svetlov | 4bb142b | 2012-12-18 21:27:37 +0200 | [diff] [blame] | 483 | * valid and raise an assertion if it isn't. |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 484 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 485 | * an assertion can be useful, but it does contradict the POSIX standard |
| 486 | * which for write(2) states: |
| 487 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 488 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 489 | * writing." |
| 490 | * Furthermore, python allows the user to enter any old integer |
| 491 | * as a fd and should merely raise a python exception on error. |
| 492 | * The Microsoft CRT doesn't provide an official way to check for the |
| 493 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 494 | * by using the exported __pinfo data member and knowledge of the |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 495 | * internal structures involved. |
| 496 | * The structures below must be updated for each version of visual studio |
| 497 | * according to the file internal.h in the CRT source, until MS comes |
| 498 | * up with a less hacky way to do this. |
| 499 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 500 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 501 | */ |
Kristján Valur Jónsson | feab334 | 2009-04-01 16:08:34 +0000 | [diff] [blame] | 502 | /* The actual size of the structure is determined at runtime. |
| 503 | * Only the first items must be present. |
| 504 | */ |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 505 | typedef struct { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 506 | intptr_t osfhnd; |
| 507 | char osfile; |
Kristján Valur Jónsson | feab334 | 2009-04-01 16:08:34 +0000 | [diff] [blame] | 508 | } my_ioinfo; |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 509 | |
Kristján Valur Jónsson | feab334 | 2009-04-01 16:08:34 +0000 | [diff] [blame] | 510 | extern __declspec(dllimport) char * __pioinfo[]; |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 511 | #define IOINFO_L2E 5 |
| 512 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 513 | #define IOINFO_ARRAYS 64 |
| 514 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 515 | #define FOPEN 0x01 |
| 516 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 517 | |
| 518 | /* This function emulates what the windows CRT does to validate file handles */ |
| 519 | int |
| 520 | _PyVerify_fd(int fd) |
| 521 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 522 | const int i1 = fd >> IOINFO_L2E; |
| 523 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | feab334 | 2009-04-01 16:08:34 +0000 | [diff] [blame] | 524 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 525 | static int sizeof_ioinfo = 0; |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 526 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 527 | /* Determine the actual size of the ioinfo structure, |
| 528 | * as used by the CRT loaded in memory |
| 529 | */ |
| 530 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 531 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 532 | } |
| 533 | if (sizeof_ioinfo == 0) { |
| 534 | /* This should not happen... */ |
| 535 | goto fail; |
| 536 | } |
| 537 | |
| 538 | /* See that it isn't a special CLEAR fileno */ |
| 539 | if (fd != _NO_CONSOLE_FILENO) { |
| 540 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 541 | * we check pointer validity and other info |
| 542 | */ |
| 543 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 544 | /* finally, check that the file is open */ |
| 545 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 546 | if (info->osfile & FOPEN) { |
| 547 | return 1; |
| 548 | } |
| 549 | } |
| 550 | } |
Kristján Valur Jónsson | feab334 | 2009-04-01 16:08:34 +0000 | [diff] [blame] | 551 | fail: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 552 | errno = EBADF; |
| 553 | return 0; |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 557 | static int |
| 558 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 559 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 560 | if (!_PyVerify_fd(fd1)) |
| 561 | return 0; |
| 562 | if (fd2 == _NO_CONSOLE_FILENO) |
| 563 | return 0; |
| 564 | if ((unsigned)fd2 < _NHANDLE_) |
| 565 | return 1; |
| 566 | else |
| 567 | return 0; |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 568 | } |
| 569 | #else |
| 570 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 571 | #define _PyVerify_fd_dup2(A, B) (1) |
| 572 | #endif |
| 573 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 574 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 1c60c7a | 2013-01-25 17:55:39 +0100 | [diff] [blame] | 575 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 576 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 1c60c7a | 2013-01-25 17:55:39 +0100 | [diff] [blame] | 577 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 578 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 579 | */ |
| 580 | #include <crt_externs.h> |
| 581 | static char **environ; |
| 582 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 583 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 584 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 585 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 586 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 587 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 588 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 589 | PyObject *d; |
| 590 | char **e; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 591 | #if defined(PYOS_OS2) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 592 | APIRET rc; |
| 593 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 594 | #endif |
| 595 | d = PyDict_New(); |
| 596 | if (d == NULL) |
| 597 | return NULL; |
Ronald Oussoren | 1c60c7a | 2013-01-25 17:55:39 +0100 | [diff] [blame] | 598 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 599 | if (environ == NULL) |
| 600 | environ = *_NSGetEnviron(); |
| 601 | #endif |
| 602 | if (environ == NULL) |
| 603 | return d; |
| 604 | /* This part ignores errors */ |
| 605 | for (e = environ; *e != NULL; e++) { |
| 606 | PyObject *k; |
| 607 | PyObject *v; |
| 608 | char *p = strchr(*e, '='); |
| 609 | if (p == NULL) |
| 610 | continue; |
| 611 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 612 | if (k == NULL) { |
| 613 | PyErr_Clear(); |
| 614 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 615 | } |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 616 | v = PyString_FromString(p+1); |
| 617 | if (v == NULL) { |
| 618 | PyErr_Clear(); |
| 619 | Py_DECREF(k); |
| 620 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 621 | } |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 622 | if (PyDict_GetItem(d, k) == NULL) { |
| 623 | if (PyDict_SetItem(d, k, v) != 0) |
| 624 | PyErr_Clear(); |
| 625 | } |
| 626 | Py_DECREF(k); |
| 627 | Py_DECREF(v); |
| 628 | } |
| 629 | #if defined(PYOS_OS2) |
| 630 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 631 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 632 | PyObject *v = PyString_FromString(buffer); |
| 633 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 634 | Py_DECREF(v); |
| 635 | } |
| 636 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 637 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 638 | PyObject *v = PyString_FromString(buffer); |
| 639 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 640 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 641 | } |
| 642 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 643 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 647 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 648 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 649 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 650 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 651 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 652 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 653 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 654 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 655 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 656 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 657 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 660 | #ifdef MS_WINDOWS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 661 | static PyObject * |
| 662 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 663 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 664 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 665 | } |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 666 | #endif /* MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 667 | |
| 668 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 669 | static PyObject * |
| 670 | posix_error_with_allocated_filename(char* name) |
| 671 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 672 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 673 | PyMem_Free(name); |
| 674 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 677 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 678 | static PyObject * |
| 679 | win32_error(char* function, char* filename) |
| 680 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 681 | /* XXX We should pass the function name along in the future. |
| 682 | (_winreg.c also wants to pass the function name.) |
| 683 | This would however require an additional param to the |
| 684 | Windows error object, which is non-trivial. |
| 685 | */ |
| 686 | errno = GetLastError(); |
| 687 | if (filename) |
| 688 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 689 | else |
| 690 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 691 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 692 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 693 | static PyObject * |
| 694 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 695 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 696 | /* XXX - see win32_error for comments on 'function' */ |
| 697 | errno = GetLastError(); |
| 698 | if (filename) |
| 699 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 700 | else |
| 701 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 704 | static int |
Hirokazu Yamamoto | a0fdd72 | 2008-08-17 09:19:52 +0000 | [diff] [blame] | 705 | convert_to_unicode(PyObject **param) |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 706 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 707 | if (PyUnicode_CheckExact(*param)) |
| 708 | Py_INCREF(*param); |
| 709 | else if (PyUnicode_Check(*param)) |
| 710 | /* For a Unicode subtype that's not a Unicode object, |
| 711 | return a true Unicode object with the same data. */ |
| 712 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), |
| 713 | PyUnicode_GET_SIZE(*param)); |
| 714 | else |
| 715 | *param = PyUnicode_FromEncodedObject(*param, |
| 716 | Py_FileSystemDefaultEncoding, |
| 717 | "strict"); |
| 718 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 721 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 722 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 723 | #if defined(PYOS_OS2) |
| 724 | /********************************************************************** |
| 725 | * Helper Function to Trim and Format OS/2 Messages |
| 726 | **********************************************************************/ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 727 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 728 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 729 | { |
| 730 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 731 | |
| 732 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
Victor Stinner | 862490a | 2010-05-06 00:03:44 +0000 | [diff] [blame] | 733 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 734 | |
Victor Stinner | 862490a | 2010-05-06 00:03:44 +0000 | [diff] [blame] | 735 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
| 736 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /* Add Optional Reason Text */ |
| 740 | if (reason) { |
| 741 | strcat(msgbuf, " : "); |
| 742 | strcat(msgbuf, reason); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | /********************************************************************** |
| 747 | * Decode an OS/2 Operating System Error Code |
| 748 | * |
| 749 | * A convenience function to lookup an OS/2 error code and return a |
| 750 | * text message we can use to raise a Python exception. |
| 751 | * |
| 752 | * Notes: |
| 753 | * The messages for errors returned from the OS/2 kernel reside in |
| 754 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 755 | * |
| 756 | **********************************************************************/ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 757 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 758 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 759 | { |
| 760 | APIRET rc; |
| 761 | ULONG msglen; |
| 762 | |
| 763 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 764 | Py_BEGIN_ALLOW_THREADS |
| 765 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 766 | errorcode, "oso001.msg", &msglen); |
| 767 | Py_END_ALLOW_THREADS |
| 768 | |
| 769 | if (rc == NO_ERROR) |
| 770 | os2_formatmsg(msgbuf, msglen, reason); |
| 771 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 772 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 773 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 774 | |
| 775 | return msgbuf; |
| 776 | } |
| 777 | |
| 778 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 779 | errors are not in a global variable e.g. 'errno' nor are |
| 780 | they congruent with posix error numbers. */ |
| 781 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 782 | static PyObject * |
| 783 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 784 | { |
| 785 | char text[1024]; |
| 786 | PyObject *v; |
| 787 | |
| 788 | os2_strerror(text, sizeof(text), code, ""); |
| 789 | |
| 790 | v = Py_BuildValue("(is)", code, text); |
| 791 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 792 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 793 | Py_DECREF(v); |
| 794 | } |
| 795 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 796 | } |
| 797 | |
| 798 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 799 | |
| 800 | /* POSIX generic methods */ |
| 801 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 802 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 803 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 804 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 805 | int fd; |
| 806 | int res; |
| 807 | fd = PyObject_AsFileDescriptor(fdobj); |
| 808 | if (fd < 0) |
| 809 | return NULL; |
| 810 | if (!_PyVerify_fd(fd)) |
| 811 | return posix_error(); |
| 812 | Py_BEGIN_ALLOW_THREADS |
| 813 | res = (*func)(fd); |
| 814 | Py_END_ALLOW_THREADS |
| 815 | if (res < 0) |
| 816 | return posix_error(); |
| 817 | Py_INCREF(Py_None); |
| 818 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 819 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 820 | |
| 821 | static PyObject * |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 822 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 823 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 824 | char *path1 = NULL; |
| 825 | int res; |
| 826 | if (!PyArg_ParseTuple(args, format, |
| 827 | Py_FileSystemDefaultEncoding, &path1)) |
| 828 | return NULL; |
| 829 | Py_BEGIN_ALLOW_THREADS |
| 830 | res = (*func)(path1); |
| 831 | Py_END_ALLOW_THREADS |
| 832 | if (res < 0) |
| 833 | return posix_error_with_allocated_filename(path1); |
| 834 | PyMem_Free(path1); |
| 835 | Py_INCREF(Py_None); |
| 836 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 839 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 840 | posix_2str(PyObject *args, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 841 | char *format, |
| 842 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 843 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 844 | char *path1 = NULL, *path2 = NULL; |
| 845 | int res; |
| 846 | if (!PyArg_ParseTuple(args, format, |
| 847 | Py_FileSystemDefaultEncoding, &path1, |
| 848 | Py_FileSystemDefaultEncoding, &path2)) |
| 849 | return NULL; |
| 850 | Py_BEGIN_ALLOW_THREADS |
| 851 | res = (*func)(path1, path2); |
| 852 | Py_END_ALLOW_THREADS |
| 853 | PyMem_Free(path1); |
| 854 | PyMem_Free(path2); |
| 855 | if (res != 0) |
| 856 | /* XXX how to report both path1 and path2??? */ |
| 857 | return posix_error(); |
| 858 | Py_INCREF(Py_None); |
| 859 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 862 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 863 | static PyObject* |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 864 | win32_1str(PyObject* args, char* func, |
| 865 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 866 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 867 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 868 | PyObject *uni; |
| 869 | char *ansi; |
| 870 | BOOL result; |
Hirokazu Yamamoto | a3c5609 | 2009-06-28 10:23:00 +0000 | [diff] [blame] | 871 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 872 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 873 | PyErr_Clear(); |
| 874 | else { |
| 875 | Py_BEGIN_ALLOW_THREADS |
| 876 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 877 | Py_END_ALLOW_THREADS |
| 878 | if (!result) |
| 879 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 880 | Py_INCREF(Py_None); |
| 881 | return Py_None; |
| 882 | } |
| 883 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 884 | return NULL; |
| 885 | Py_BEGIN_ALLOW_THREADS |
| 886 | result = funcA(ansi); |
| 887 | Py_END_ALLOW_THREADS |
| 888 | if (!result) |
| 889 | return win32_error(func, ansi); |
| 890 | Py_INCREF(Py_None); |
| 891 | return Py_None; |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 892 | |
| 893 | } |
| 894 | |
| 895 | /* This is a reimplementation of the C library's chdir function, |
| 896 | but one that produces Win32 errors instead of DOS error codes. |
| 897 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 898 | it also needs to set "magic" environment variables indicating |
| 899 | the per-drive current directory, which are of the form =<drive>: */ |
Hirokazu Yamamoto | 6137640 | 2008-10-16 06:25:25 +0000 | [diff] [blame] | 900 | static BOOL __stdcall |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 901 | win32_chdir(LPCSTR path) |
| 902 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 903 | char new_path[MAX_PATH+1]; |
| 904 | int result; |
| 905 | char env[4] = "=x:"; |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 906 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 907 | if(!SetCurrentDirectoryA(path)) |
| 908 | return FALSE; |
| 909 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 910 | if (!result) |
| 911 | return FALSE; |
| 912 | /* In the ANSI API, there should not be any paths longer |
| 913 | than MAX_PATH. */ |
| 914 | assert(result <= MAX_PATH+1); |
| 915 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 916 | strncmp(new_path, "//", 2) == 0) |
| 917 | /* UNC path, nothing to do. */ |
| 918 | return TRUE; |
| 919 | env[1] = new_path[0]; |
| 920 | return SetEnvironmentVariableA(env, new_path); |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /* The Unicode version differs from the ANSI version |
| 924 | since the current directory might exceed MAX_PATH characters */ |
Hirokazu Yamamoto | 6137640 | 2008-10-16 06:25:25 +0000 | [diff] [blame] | 925 | static BOOL __stdcall |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 926 | win32_wchdir(LPCWSTR path) |
| 927 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 928 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 929 | int result; |
| 930 | wchar_t env[4] = L"=x:"; |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 931 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 932 | if(!SetCurrentDirectoryW(path)) |
| 933 | return FALSE; |
| 934 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 935 | if (!result) |
| 936 | return FALSE; |
| 937 | if (result > MAX_PATH+1) { |
| 938 | new_path = malloc(result * sizeof(wchar_t)); |
| 939 | if (!new_path) { |
| 940 | SetLastError(ERROR_OUTOFMEMORY); |
| 941 | return FALSE; |
| 942 | } |
| 943 | result = GetCurrentDirectoryW(result, new_path); |
| 944 | if (!result) { |
| 945 | free(new_path); |
| 946 | return FALSE; |
| 947 | } |
| 948 | } |
| 949 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 950 | wcsncmp(new_path, L"//", 2) == 0) |
| 951 | /* UNC path, nothing to do. */ |
| 952 | return TRUE; |
| 953 | env[1] = new_path[0]; |
| 954 | result = SetEnvironmentVariableW(env, new_path); |
| 955 | if (new_path != _new_path) |
| 956 | free(new_path); |
| 957 | return result; |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 958 | } |
| 959 | #endif |
| 960 | |
Antoine Pitrou | ff48c0a | 2011-07-01 22:56:03 +0200 | [diff] [blame] | 961 | /* choose the appropriate stat and fstat functions and return structs */ |
| 962 | #undef STAT |
| 963 | #undef FSTAT |
| 964 | #undef STRUCT_STAT |
| 965 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 966 | # define STAT win32_stat |
| 967 | # define FSTAT win32_fstat |
| 968 | # define STRUCT_STAT struct win32_stat |
| 969 | #else |
| 970 | # define STAT stat |
| 971 | # define FSTAT fstat |
| 972 | # define STRUCT_STAT struct stat |
| 973 | #endif |
| 974 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 975 | #ifdef MS_WINDOWS |
| 976 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 977 | - time stamps are restricted to second resolution |
| 978 | - file modification times suffer from forth-and-back conversions between |
| 979 | UTC and local time |
| 980 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 981 | */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 982 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 983 | |
| 984 | struct win32_stat{ |
| 985 | int st_dev; |
| 986 | __int64 st_ino; |
| 987 | unsigned short st_mode; |
| 988 | int st_nlink; |
| 989 | int st_uid; |
| 990 | int st_gid; |
| 991 | int st_rdev; |
| 992 | __int64 st_size; |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 993 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 994 | int st_atime_nsec; |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 995 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 996 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 997 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 998 | int st_ctime_nsec; |
| 999 | }; |
| 1000 | |
| 1001 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1002 | |
| 1003 | static void |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 1004 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1005 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1006 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1007 | /* Cannot simply cast and dereference in_ptr, |
| 1008 | since it might not be aligned properly */ |
| 1009 | __int64 in; |
| 1010 | memcpy(&in, in_ptr, sizeof(in)); |
| 1011 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 1012 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1015 | static void |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 1016 | time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr) |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1017 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1018 | /* XXX endianness */ |
| 1019 | __int64 out; |
| 1020 | out = time_in + secs_between_epochs; |
| 1021 | out = out * 10000000 + nsec_in / 100; |
| 1022 | memcpy(out_ptr, &out, sizeof(out)); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1025 | /* Below, we *know* that ugo+r is 0444 */ |
| 1026 | #if _S_IREAD != 0400 |
| 1027 | #error Unsupported C library |
| 1028 | #endif |
| 1029 | static int |
| 1030 | attributes_to_mode(DWORD attr) |
| 1031 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1032 | int m = 0; |
| 1033 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1034 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1035 | else |
| 1036 | m |= _S_IFREG; |
| 1037 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1038 | m |= 0444; |
| 1039 | else |
| 1040 | m |= 0666; |
| 1041 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | static int |
| 1045 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 1046 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1047 | memset(result, 0, sizeof(*result)); |
| 1048 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1049 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1050 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1051 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1052 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1053 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1054 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Martin v. Löwis | 3bf573f | 2007-04-04 18:30:36 +0000 | [diff] [blame] | 1057 | static BOOL |
| 1058 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 1059 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1060 | HANDLE hFindFile; |
| 1061 | WIN32_FIND_DATAA FileData; |
| 1062 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1063 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1064 | return FALSE; |
| 1065 | FindClose(hFindFile); |
| 1066 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 1067 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 1068 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1069 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1070 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1071 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 1072 | return TRUE; |
Martin v. Löwis | 3bf573f | 2007-04-04 18:30:36 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | static BOOL |
| 1076 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 1077 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1078 | HANDLE hFindFile; |
| 1079 | WIN32_FIND_DATAW FileData; |
| 1080 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1081 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1082 | return FALSE; |
| 1083 | FindClose(hFindFile); |
| 1084 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 1085 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 1086 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1087 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1088 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1089 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 1090 | return TRUE; |
Martin v. Löwis | 3bf573f | 2007-04-04 18:30:36 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1093 | static int |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1094 | win32_stat(const char* path, struct win32_stat *result) |
| 1095 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1096 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1097 | int code; |
| 1098 | char *dot; |
| 1099 | if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
| 1100 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1101 | /* Protocol violation: we explicitly clear errno, instead of |
| 1102 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1103 | errno = 0; |
| 1104 | return -1; |
| 1105 | } else { |
| 1106 | /* Could not get attributes on open file. Fall back to |
| 1107 | reading the directory. */ |
| 1108 | if (!attributes_from_dir(path, &info)) { |
| 1109 | /* Very strange. This should not fail now */ |
| 1110 | errno = 0; |
| 1111 | return -1; |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | code = attribute_data_to_stat(&info, result); |
| 1116 | if (code != 0) |
| 1117 | return code; |
| 1118 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 1119 | dot = strrchr(path, '.'); |
| 1120 | if (dot) { |
| 1121 | if (stricmp(dot, ".bat") == 0 || |
| 1122 | stricmp(dot, ".cmd") == 0 || |
| 1123 | stricmp(dot, ".exe") == 0 || |
| 1124 | stricmp(dot, ".com") == 0) |
| 1125 | result->st_mode |= 0111; |
| 1126 | } |
| 1127 | return code; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1130 | static int |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1131 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 1132 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1133 | int code; |
| 1134 | const wchar_t *dot; |
| 1135 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1136 | if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
| 1137 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1138 | /* Protocol violation: we explicitly clear errno, instead of |
| 1139 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1140 | errno = 0; |
| 1141 | return -1; |
| 1142 | } else { |
| 1143 | /* Could not get attributes on open file. Fall back to |
| 1144 | reading the directory. */ |
| 1145 | if (!attributes_from_dir_w(path, &info)) { |
| 1146 | /* Very strange. This should not fail now */ |
| 1147 | errno = 0; |
| 1148 | return -1; |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | code = attribute_data_to_stat(&info, result); |
| 1153 | if (code < 0) |
| 1154 | return code; |
| 1155 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 1156 | dot = wcsrchr(path, '.'); |
| 1157 | if (dot) { |
| 1158 | if (_wcsicmp(dot, L".bat") == 0 || |
| 1159 | _wcsicmp(dot, L".cmd") == 0 || |
| 1160 | _wcsicmp(dot, L".exe") == 0 || |
| 1161 | _wcsicmp(dot, L".com") == 0) |
| 1162 | result->st_mode |= 0111; |
| 1163 | } |
| 1164 | return code; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | static int |
| 1168 | win32_fstat(int file_number, struct win32_stat *result) |
| 1169 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1170 | BY_HANDLE_FILE_INFORMATION info; |
| 1171 | HANDLE h; |
| 1172 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1173 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1174 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1175 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1176 | /* Protocol violation: we explicitly clear errno, instead of |
| 1177 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1178 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1179 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1180 | if (h == INVALID_HANDLE_VALUE) { |
| 1181 | /* This is really a C library error (invalid file handle). |
| 1182 | We set the Win32 error to the closes one matching. */ |
| 1183 | SetLastError(ERROR_INVALID_HANDLE); |
| 1184 | return -1; |
| 1185 | } |
| 1186 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1187 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1188 | type = GetFileType(h); |
| 1189 | if (type == FILE_TYPE_UNKNOWN) { |
| 1190 | DWORD error = GetLastError(); |
| 1191 | if (error != 0) { |
| 1192 | return -1; |
| 1193 | } |
| 1194 | /* else: valid but unknown file */ |
| 1195 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1196 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1197 | if (type != FILE_TYPE_DISK) { |
| 1198 | if (type == FILE_TYPE_CHAR) |
| 1199 | result->st_mode = _S_IFCHR; |
| 1200 | else if (type == FILE_TYPE_PIPE) |
| 1201 | result->st_mode = _S_IFIFO; |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
| 1205 | if (!GetFileInformationByHandle(h, &info)) { |
| 1206 | return -1; |
| 1207 | } |
| 1208 | |
| 1209 | /* similar to stat() */ |
| 1210 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1211 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1212 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1213 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1214 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1215 | /* specific to fstat() */ |
| 1216 | result->st_nlink = info.nNumberOfLinks; |
| 1217 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1218 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | #endif /* MS_WINDOWS */ |
| 1222 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1223 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1224 | "stat_result: Result from stat or lstat.\n\n\ |
| 1225 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1226 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1227 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1228 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1229 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1230 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1231 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1232 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1233 | |
| 1234 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1235 | {"st_mode", "protection bits"}, |
| 1236 | {"st_ino", "inode"}, |
| 1237 | {"st_dev", "device"}, |
| 1238 | {"st_nlink", "number of hard links"}, |
| 1239 | {"st_uid", "user ID of owner"}, |
| 1240 | {"st_gid", "group ID of owner"}, |
| 1241 | {"st_size", "total size, in bytes"}, |
| 1242 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1243 | {NULL, "integer time of last access"}, |
| 1244 | {NULL, "integer time of last modification"}, |
| 1245 | {NULL, "integer time of last change"}, |
| 1246 | {"st_atime", "time of last access"}, |
| 1247 | {"st_mtime", "time of last modification"}, |
| 1248 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1249 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1250 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1251 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1252 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1253 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1254 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1255 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1256 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1257 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1258 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1259 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1260 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1261 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1262 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1263 | #endif |
| 1264 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1265 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1266 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1267 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1268 | }; |
| 1269 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1270 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1271 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1272 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1273 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1274 | #endif |
| 1275 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1276 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1277 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1278 | #else |
| 1279 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1280 | #endif |
| 1281 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1282 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1283 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1284 | #else |
| 1285 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1286 | #endif |
| 1287 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1288 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1289 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1290 | #else |
| 1291 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1292 | #endif |
| 1293 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1294 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1295 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1296 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1297 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1298 | #endif |
| 1299 | |
| 1300 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1301 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1302 | #else |
| 1303 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1304 | #endif |
| 1305 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1306 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1307 | "stat_result", /* name */ |
| 1308 | stat_result__doc__, /* doc */ |
| 1309 | stat_result_fields, |
| 1310 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1311 | }; |
| 1312 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1313 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1314 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1315 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1316 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1317 | 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] | 1318 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1319 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1320 | |
| 1321 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1322 | {"f_bsize", }, |
| 1323 | {"f_frsize", }, |
| 1324 | {"f_blocks", }, |
| 1325 | {"f_bfree", }, |
| 1326 | {"f_bavail", }, |
| 1327 | {"f_files", }, |
| 1328 | {"f_ffree", }, |
| 1329 | {"f_favail", }, |
| 1330 | {"f_flag", }, |
| 1331 | {"f_namemax",}, |
| 1332 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1333 | }; |
| 1334 | |
| 1335 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1336 | "statvfs_result", /* name */ |
| 1337 | statvfs_result__doc__, /* doc */ |
| 1338 | statvfs_result_fields, |
| 1339 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1340 | }; |
| 1341 | |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 1342 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1343 | static PyTypeObject StatResultType; |
| 1344 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1345 | static newfunc structseq_new; |
| 1346 | |
| 1347 | static PyObject * |
| 1348 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1349 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1350 | PyStructSequence *result; |
| 1351 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1352 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1353 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1354 | if (!result) |
| 1355 | return NULL; |
| 1356 | /* If we have been initialized from a tuple, |
| 1357 | st_?time might be set to None. Initialize it |
| 1358 | from the int slots. */ |
| 1359 | for (i = 7; i <= 9; i++) { |
| 1360 | if (result->ob_item[i+3] == Py_None) { |
| 1361 | Py_DECREF(Py_None); |
| 1362 | Py_INCREF(result->ob_item[i]); |
| 1363 | result->ob_item[i+3] = result->ob_item[i]; |
| 1364 | } |
| 1365 | } |
| 1366 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | |
| 1370 | |
| 1371 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1372 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1373 | |
| 1374 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1375 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1376 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1377 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1378 | future calls return ints. \n\ |
| 1379 | If newval is omitted, return the current setting.\n"); |
| 1380 | |
| 1381 | static PyObject* |
| 1382 | stat_float_times(PyObject* self, PyObject *args) |
| 1383 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1384 | int newval = -1; |
| 1385 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1386 | return NULL; |
| 1387 | if (newval == -1) |
| 1388 | /* Return old value */ |
| 1389 | return PyBool_FromLong(_stat_float_times); |
| 1390 | _stat_float_times = newval; |
| 1391 | Py_INCREF(Py_None); |
| 1392 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1393 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1394 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1395 | static void |
| 1396 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1397 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1398 | PyObject *fval,*ival; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1399 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1400 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1401 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1402 | ival = PyInt_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1403 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1404 | if (!ival) |
| 1405 | return; |
| 1406 | if (_stat_float_times) { |
| 1407 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1408 | } else { |
| 1409 | fval = ival; |
| 1410 | Py_INCREF(fval); |
| 1411 | } |
| 1412 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1413 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1416 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1417 | (used by posix_stat() and posix_fstat()) */ |
| 1418 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1419 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1420 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1421 | unsigned long ansec, mnsec, cnsec; |
| 1422 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1423 | if (v == NULL) |
| 1424 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1425 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1426 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1427 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1428 | PyStructSequence_SET_ITEM(v, 1, |
| 1429 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1430 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1431 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1432 | #endif |
| 1433 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1434 | PyStructSequence_SET_ITEM(v, 2, |
| 1435 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1436 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1437 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1438 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1439 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 1440 | #if defined(MS_WINDOWS) |
| 1441 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong(0)); |
| 1442 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong(0)); |
| 1443 | #else |
| 1444 | PyStructSequence_SET_ITEM(v, 4, _PyInt_FromUid(st->st_uid)); |
| 1445 | PyStructSequence_SET_ITEM(v, 5, _PyInt_FromGid(st->st_gid)); |
| 1446 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1447 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1448 | PyStructSequence_SET_ITEM(v, 6, |
| 1449 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1450 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1451 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1452 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1453 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1454 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1455 | ansec = st->st_atim.tv_nsec; |
| 1456 | mnsec = st->st_mtim.tv_nsec; |
| 1457 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1458 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1459 | ansec = st->st_atimespec.tv_nsec; |
| 1460 | mnsec = st->st_mtimespec.tv_nsec; |
| 1461 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1462 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1463 | ansec = st->st_atime_nsec; |
| 1464 | mnsec = st->st_mtime_nsec; |
| 1465 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1466 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1467 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1468 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1469 | fill_time(v, 7, st->st_atime, ansec); |
| 1470 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1471 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1472 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1473 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1474 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1475 | PyInt_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1476 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1477 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1478 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1479 | PyInt_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1480 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1481 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1482 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1483 | PyInt_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1484 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1485 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1486 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 1487 | PyInt_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1488 | #endif |
| 1489 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1490 | { |
| 1491 | PyObject *val; |
| 1492 | unsigned long bsec,bnsec; |
| 1493 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1494 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1495 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1496 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1497 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1498 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1499 | if (_stat_float_times) { |
| 1500 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1501 | } else { |
| 1502 | val = PyInt_FromLong((long)bsec); |
| 1503 | } |
| 1504 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1505 | val); |
| 1506 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1507 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1508 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1509 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 1510 | PyInt_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1511 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1512 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1513 | if (PyErr_Occurred()) { |
| 1514 | Py_DECREF(v); |
| 1515 | return NULL; |
| 1516 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1517 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1518 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1521 | #ifdef MS_WINDOWS |
| 1522 | |
| 1523 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1524 | where / can be used in place of \ and the trailing slash is optional. |
| 1525 | Both SERVER and SHARE must have at least one character. |
| 1526 | */ |
| 1527 | |
| 1528 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1529 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Kristján Valur Jónsson | dbeaa69 | 2006-06-09 16:28:01 +0000 | [diff] [blame] | 1530 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1531 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Kristján Valur Jónsson | dbeaa69 | 2006-06-09 16:28:01 +0000 | [diff] [blame] | 1532 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1533 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1534 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1535 | IsUNCRootA(char *path, int pathlen) |
| 1536 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1537 | #define ISSLASH ISSLASHA |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1538 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1539 | int i, share; |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1540 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1541 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1542 | /* minimum UNCRoot is \\x\y */ |
| 1543 | return FALSE; |
| 1544 | for (i = 2; i < pathlen ; i++) |
| 1545 | if (ISSLASH(path[i])) break; |
| 1546 | if (i == 2 || i == pathlen) |
| 1547 | /* do not allow \\\SHARE or \\SERVER */ |
| 1548 | return FALSE; |
| 1549 | share = i+1; |
| 1550 | for (i = share; i < pathlen; i++) |
| 1551 | if (ISSLASH(path[i])) break; |
| 1552 | return (i != share && (i == pathlen || i == pathlen-1)); |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1553 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1554 | #undef ISSLASH |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1557 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1558 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1559 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1560 | #define ISSLASH ISSLASHW |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1561 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1562 | int i, share; |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1563 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1564 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1565 | /* minimum UNCRoot is \\x\y */ |
| 1566 | return FALSE; |
| 1567 | for (i = 2; i < pathlen ; i++) |
| 1568 | if (ISSLASH(path[i])) break; |
| 1569 | if (i == 2 || i == pathlen) |
| 1570 | /* do not allow \\\SHARE or \\SERVER */ |
| 1571 | return FALSE; |
| 1572 | share = i+1; |
| 1573 | for (i = share; i < pathlen; i++) |
| 1574 | if (ISSLASH(path[i])) break; |
| 1575 | return (i != share && (i == pathlen || i == pathlen-1)); |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1576 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1577 | #undef ISSLASH |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1578 | } |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1579 | #endif /* MS_WINDOWS */ |
| 1580 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1581 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1582 | posix_do_stat(PyObject *self, PyObject *args, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1583 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1584 | #ifdef __VMS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1585 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1586 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1587 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1588 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1589 | char *wformat, |
| 1590 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1591 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1592 | STRUCT_STAT st; |
| 1593 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1594 | char *pathfree = NULL; /* this memory must be free'd */ |
| 1595 | int res; |
| 1596 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1597 | |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 1598 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1599 | PyUnicodeObject *po; |
| 1600 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 1601 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1602 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1603 | Py_BEGIN_ALLOW_THREADS |
| 1604 | /* PyUnicode_AS_UNICODE result OK without |
| 1605 | thread lock as it is a simple dereference. */ |
| 1606 | res = wstatfunc(wpath, &st); |
| 1607 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1608 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1609 | if (res != 0) |
| 1610 | return win32_error_unicode("stat", wpath); |
| 1611 | return _pystat_fromstructstat(&st); |
| 1612 | } |
| 1613 | /* Drop the argument parsing error as narrow strings |
| 1614 | are also valid. */ |
| 1615 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1616 | #endif |
| 1617 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1618 | if (!PyArg_ParseTuple(args, format, |
| 1619 | Py_FileSystemDefaultEncoding, &path)) |
| 1620 | return NULL; |
| 1621 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1622 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1623 | Py_BEGIN_ALLOW_THREADS |
| 1624 | res = (*statfunc)(path, &st); |
| 1625 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1626 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1627 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1628 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1629 | result = win32_error("stat", pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1630 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1631 | result = posix_error_with_filename(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1632 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1633 | } |
| 1634 | else |
| 1635 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1636 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1637 | PyMem_Free(pathfree); |
| 1638 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1641 | /* POSIX methods */ |
| 1642 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1643 | PyDoc_STRVAR(posix_access__doc__, |
Georg Brandl | 7a28447 | 2007-01-27 19:38:50 +0000 | [diff] [blame] | 1644 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1645 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1646 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1647 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1648 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1649 | 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] | 1650 | |
| 1651 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1652 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1653 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1654 | char *path; |
| 1655 | int mode; |
| 1656 | |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 1657 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1658 | DWORD attr; |
| 1659 | PyUnicodeObject *po; |
| 1660 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1661 | Py_BEGIN_ALLOW_THREADS |
| 1662 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1663 | it is a simple dereference. */ |
| 1664 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1665 | Py_END_ALLOW_THREADS |
| 1666 | goto finish; |
| 1667 | } |
| 1668 | /* Drop the argument parsing error as narrow strings |
| 1669 | are also valid. */ |
| 1670 | PyErr_Clear(); |
| 1671 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1672 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 1673 | return NULL; |
| 1674 | Py_BEGIN_ALLOW_THREADS |
| 1675 | attr = GetFileAttributesA(path); |
| 1676 | Py_END_ALLOW_THREADS |
| 1677 | PyMem_Free(path); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1678 | finish: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1679 | if (attr == 0xFFFFFFFF) |
| 1680 | /* File does not exist, or cannot read attributes */ |
| 1681 | return PyBool_FromLong(0); |
| 1682 | /* Access is possible if either write access wasn't requested, or |
| 1683 | the file isn't read-only, or if it's a directory, as there are |
| 1684 | no read-only directories on Windows. */ |
| 1685 | return PyBool_FromLong(!(mode & 2) |
| 1686 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1687 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 1688 | #else /* MS_WINDOWS */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1689 | int res; |
| 1690 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1691 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 1692 | return NULL; |
| 1693 | Py_BEGIN_ALLOW_THREADS |
| 1694 | res = access(path, mode); |
| 1695 | Py_END_ALLOW_THREADS |
| 1696 | PyMem_Free(path); |
| 1697 | return PyBool_FromLong(res == 0); |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 1698 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1701 | #ifndef F_OK |
| 1702 | #define F_OK 0 |
| 1703 | #endif |
| 1704 | #ifndef R_OK |
| 1705 | #define R_OK 4 |
| 1706 | #endif |
| 1707 | #ifndef W_OK |
| 1708 | #define W_OK 2 |
| 1709 | #endif |
| 1710 | #ifndef X_OK |
| 1711 | #define X_OK 1 |
| 1712 | #endif |
| 1713 | |
| 1714 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1715 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1716 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1717 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1718 | |
| 1719 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1720 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1721 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1722 | int id; |
| 1723 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1724 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1725 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 1726 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1727 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1728 | #if defined(__VMS) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1729 | /* file descriptor 0 only, the default input device (stdin) */ |
| 1730 | if (id == 0) { |
| 1731 | ret = ttyname(); |
| 1732 | } |
| 1733 | else { |
| 1734 | ret = NULL; |
| 1735 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1736 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1737 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1738 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1739 | if (ret == NULL) |
| 1740 | return posix_error(); |
| 1741 | return PyString_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1742 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1743 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1744 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1745 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1746 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1747 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1748 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1749 | |
| 1750 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1751 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1752 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1753 | char *ret; |
| 1754 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1755 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1756 | #ifdef USE_CTERMID_R |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1757 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1758 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1759 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1760 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1761 | if (ret == NULL) |
| 1762 | return posix_error(); |
| 1763 | return PyString_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1764 | } |
| 1765 | #endif |
| 1766 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1767 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1768 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1769 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1770 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1771 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1772 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1773 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1774 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1775 | return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1776 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1777 | return posix_1str(args, "et:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1778 | #elif defined(__VMS) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1779 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1780 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1781 | return posix_1str(args, "et:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1782 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1785 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1786 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1787 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1788 | 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] | 1789 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1790 | |
| 1791 | static PyObject * |
| 1792 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1793 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1794 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1795 | } |
| 1796 | #endif /* HAVE_FCHDIR */ |
| 1797 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1798 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1799 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1800 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1801 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1802 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1803 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1804 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1805 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1806 | char *path = NULL; |
| 1807 | int i; |
| 1808 | int res; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 1809 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1810 | DWORD attr; |
| 1811 | PyUnicodeObject *po; |
| 1812 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1813 | Py_BEGIN_ALLOW_THREADS |
| 1814 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1815 | if (attr != 0xFFFFFFFF) { |
| 1816 | if (i & _S_IWRITE) |
| 1817 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1818 | else |
| 1819 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1820 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1821 | } |
| 1822 | else |
| 1823 | res = 0; |
| 1824 | Py_END_ALLOW_THREADS |
| 1825 | if (!res) |
| 1826 | return win32_error_unicode("chmod", |
| 1827 | PyUnicode_AS_UNICODE(po)); |
| 1828 | Py_INCREF(Py_None); |
| 1829 | return Py_None; |
| 1830 | } |
| 1831 | /* Drop the argument parsing error as narrow strings |
| 1832 | are also valid. */ |
| 1833 | PyErr_Clear(); |
Hirokazu Yamamoto | a3c5609 | 2009-06-28 10:23:00 +0000 | [diff] [blame] | 1834 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1835 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
| 1836 | &path, &i)) |
| 1837 | return NULL; |
| 1838 | Py_BEGIN_ALLOW_THREADS |
| 1839 | attr = GetFileAttributesA(path); |
| 1840 | if (attr != 0xFFFFFFFF) { |
| 1841 | if (i & _S_IWRITE) |
| 1842 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1843 | else |
| 1844 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1845 | res = SetFileAttributesA(path, attr); |
| 1846 | } |
| 1847 | else |
| 1848 | res = 0; |
| 1849 | Py_END_ALLOW_THREADS |
| 1850 | if (!res) { |
| 1851 | win32_error("chmod", path); |
| 1852 | PyMem_Free(path); |
| 1853 | return NULL; |
| 1854 | } |
| 1855 | PyMem_Free(path); |
| 1856 | Py_INCREF(Py_None); |
| 1857 | return Py_None; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 1858 | #else /* MS_WINDOWS */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1859 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
| 1860 | &path, &i)) |
| 1861 | return NULL; |
| 1862 | Py_BEGIN_ALLOW_THREADS |
| 1863 | res = chmod(path, i); |
| 1864 | Py_END_ALLOW_THREADS |
| 1865 | if (res < 0) |
| 1866 | return posix_error_with_allocated_filename(path); |
| 1867 | PyMem_Free(path); |
| 1868 | Py_INCREF(Py_None); |
| 1869 | return Py_None; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1870 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 1873 | #ifdef HAVE_FCHMOD |
| 1874 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 1875 | "fchmod(fd, mode)\n\n\ |
| 1876 | Change the access permissions of the file given by file\n\ |
| 1877 | descriptor fd."); |
| 1878 | |
| 1879 | static PyObject * |
| 1880 | posix_fchmod(PyObject *self, PyObject *args) |
| 1881 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1882 | int fd, mode, res; |
| 1883 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 1884 | return NULL; |
| 1885 | Py_BEGIN_ALLOW_THREADS |
| 1886 | res = fchmod(fd, mode); |
| 1887 | Py_END_ALLOW_THREADS |
| 1888 | if (res < 0) |
| 1889 | return posix_error(); |
| 1890 | Py_RETURN_NONE; |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 1891 | } |
| 1892 | #endif /* HAVE_FCHMOD */ |
| 1893 | |
| 1894 | #ifdef HAVE_LCHMOD |
| 1895 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 1896 | "lchmod(path, mode)\n\n\ |
| 1897 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 1898 | affects the link itself rather than the target."); |
| 1899 | |
| 1900 | static PyObject * |
| 1901 | posix_lchmod(PyObject *self, PyObject *args) |
| 1902 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1903 | char *path = NULL; |
| 1904 | int i; |
| 1905 | int res; |
| 1906 | if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding, |
| 1907 | &path, &i)) |
| 1908 | return NULL; |
| 1909 | Py_BEGIN_ALLOW_THREADS |
| 1910 | res = lchmod(path, i); |
| 1911 | Py_END_ALLOW_THREADS |
| 1912 | if (res < 0) |
| 1913 | return posix_error_with_allocated_filename(path); |
| 1914 | PyMem_Free(path); |
| 1915 | Py_RETURN_NONE; |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 1916 | } |
| 1917 | #endif /* HAVE_LCHMOD */ |
| 1918 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1919 | |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 1920 | #ifdef HAVE_CHFLAGS |
| 1921 | PyDoc_STRVAR(posix_chflags__doc__, |
| 1922 | "chflags(path, flags)\n\n\ |
| 1923 | Set file flags."); |
| 1924 | |
| 1925 | static PyObject * |
| 1926 | posix_chflags(PyObject *self, PyObject *args) |
| 1927 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1928 | char *path; |
| 1929 | unsigned long flags; |
| 1930 | int res; |
| 1931 | if (!PyArg_ParseTuple(args, "etk:chflags", |
| 1932 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1933 | return NULL; |
| 1934 | Py_BEGIN_ALLOW_THREADS |
| 1935 | res = chflags(path, flags); |
| 1936 | Py_END_ALLOW_THREADS |
| 1937 | if (res < 0) |
| 1938 | return posix_error_with_allocated_filename(path); |
| 1939 | PyMem_Free(path); |
| 1940 | Py_INCREF(Py_None); |
| 1941 | return Py_None; |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 1942 | } |
| 1943 | #endif /* HAVE_CHFLAGS */ |
| 1944 | |
| 1945 | #ifdef HAVE_LCHFLAGS |
| 1946 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 1947 | "lchflags(path, flags)\n\n\ |
| 1948 | Set file flags.\n\ |
| 1949 | This function will not follow symbolic links."); |
| 1950 | |
| 1951 | static PyObject * |
| 1952 | posix_lchflags(PyObject *self, PyObject *args) |
| 1953 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1954 | char *path; |
| 1955 | unsigned long flags; |
| 1956 | int res; |
| 1957 | if (!PyArg_ParseTuple(args, "etk:lchflags", |
| 1958 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1959 | return NULL; |
| 1960 | Py_BEGIN_ALLOW_THREADS |
| 1961 | res = lchflags(path, flags); |
| 1962 | Py_END_ALLOW_THREADS |
| 1963 | if (res < 0) |
| 1964 | return posix_error_with_allocated_filename(path); |
| 1965 | PyMem_Free(path); |
| 1966 | Py_INCREF(Py_None); |
| 1967 | return Py_None; |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 1968 | } |
| 1969 | #endif /* HAVE_LCHFLAGS */ |
| 1970 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1971 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1972 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1973 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1974 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1975 | |
| 1976 | static PyObject * |
| 1977 | posix_chroot(PyObject *self, PyObject *args) |
| 1978 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1979 | return posix_1str(args, "et:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1980 | } |
| 1981 | #endif |
| 1982 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1983 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1984 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1985 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1986 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1987 | |
| 1988 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1989 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1990 | { |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 1991 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1992 | } |
| 1993 | #endif /* HAVE_FSYNC */ |
| 1994 | |
| 1995 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1996 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1997 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1998 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1999 | #endif |
| 2000 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2001 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2002 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2003 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2004 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2005 | |
| 2006 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2007 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2008 | { |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 2009 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2010 | } |
| 2011 | #endif /* HAVE_FDATASYNC */ |
| 2012 | |
| 2013 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2014 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2015 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2016 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2017 | 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] | 2018 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2019 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2020 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2021 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2022 | char *path = NULL; |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 2023 | uid_t uid; |
| 2024 | gid_t gid; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2025 | int res; |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 2026 | if (!PyArg_ParseTuple(args, "etO&O&:chown", |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2027 | Py_FileSystemDefaultEncoding, &path, |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 2028 | _Py_Uid_Converter, &uid, |
| 2029 | _Py_Gid_Converter, &gid)) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2030 | return NULL; |
| 2031 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 2032 | res = chown(path, uid, gid); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2033 | Py_END_ALLOW_THREADS |
| 2034 | if (res < 0) |
| 2035 | return posix_error_with_allocated_filename(path); |
| 2036 | PyMem_Free(path); |
| 2037 | Py_INCREF(Py_None); |
| 2038 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2039 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2040 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2041 | |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 2042 | #ifdef HAVE_FCHOWN |
| 2043 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2044 | "fchown(fd, uid, gid)\n\n\ |
| 2045 | Change the owner and group id of the file given by file descriptor\n\ |
| 2046 | fd to the numeric uid and gid."); |
| 2047 | |
| 2048 | static PyObject * |
| 2049 | posix_fchown(PyObject *self, PyObject *args) |
| 2050 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2051 | int fd; |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 2052 | uid_t uid; |
| 2053 | gid_t gid; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2054 | int res; |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 2055 | if (!PyArg_ParseTuple(args, "iO&O&:fchown", &fd, |
| 2056 | _Py_Uid_Converter, &uid, |
| 2057 | _Py_Gid_Converter, &gid)) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2058 | return NULL; |
| 2059 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 2060 | res = fchown(fd, uid, gid); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2061 | Py_END_ALLOW_THREADS |
| 2062 | if (res < 0) |
| 2063 | return posix_error(); |
| 2064 | Py_RETURN_NONE; |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 2065 | } |
| 2066 | #endif /* HAVE_FCHOWN */ |
| 2067 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2068 | #ifdef HAVE_LCHOWN |
| 2069 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2070 | "lchown(path, uid, gid)\n\n\ |
| 2071 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2072 | This function will not follow symbolic links."); |
| 2073 | |
| 2074 | static PyObject * |
| 2075 | posix_lchown(PyObject *self, PyObject *args) |
| 2076 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2077 | char *path = NULL; |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 2078 | uid_t uid; |
| 2079 | gid_t gid; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2080 | int res; |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 2081 | if (!PyArg_ParseTuple(args, "etO&O&:lchown", |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2082 | Py_FileSystemDefaultEncoding, &path, |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 2083 | _Py_Uid_Converter, &uid, |
| 2084 | _Py_Gid_Converter, &gid)) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2085 | return NULL; |
| 2086 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 2087 | res = lchown(path, uid, gid); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2088 | Py_END_ALLOW_THREADS |
| 2089 | if (res < 0) |
| 2090 | return posix_error_with_allocated_filename(path); |
| 2091 | PyMem_Free(path); |
| 2092 | Py_INCREF(Py_None); |
| 2093 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2094 | } |
| 2095 | #endif /* HAVE_LCHOWN */ |
| 2096 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2097 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2098 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2099 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2100 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2101 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2102 | |
Trent Nelson | da4277a | 2012-08-29 09:20:41 -0400 | [diff] [blame] | 2103 | #if (defined(__sun) && defined(__SVR4)) || \ |
| 2104 | defined(__OpenBSD__) || \ |
| 2105 | defined(__NetBSD__) |
Stefan Krah | 182ae64 | 2010-07-13 19:17:08 +0000 | [diff] [blame] | 2106 | /* Issue 9185: getcwd() returns NULL/ERANGE indefinitely. */ |
| 2107 | static PyObject * |
| 2108 | posix_getcwd(PyObject *self, PyObject *noargs) |
| 2109 | { |
| 2110 | char buf[PATH_MAX+2]; |
| 2111 | char *res; |
| 2112 | |
| 2113 | Py_BEGIN_ALLOW_THREADS |
Stefan Krah | 8cb9f03 | 2010-07-13 19:40:00 +0000 | [diff] [blame] | 2114 | res = getcwd(buf, sizeof buf); |
Stefan Krah | 182ae64 | 2010-07-13 19:17:08 +0000 | [diff] [blame] | 2115 | Py_END_ALLOW_THREADS |
| 2116 | |
| 2117 | if (res == NULL) |
| 2118 | return posix_error(); |
| 2119 | |
| 2120 | return PyString_FromString(buf); |
| 2121 | } |
| 2122 | #else |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2123 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2124 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2125 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2126 | int bufsize_incr = 1024; |
| 2127 | int bufsize = 0; |
| 2128 | char *tmpbuf = NULL; |
| 2129 | char *res = NULL; |
| 2130 | PyObject *dynamic_return; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2131 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2132 | Py_BEGIN_ALLOW_THREADS |
| 2133 | do { |
| 2134 | bufsize = bufsize + bufsize_incr; |
| 2135 | tmpbuf = malloc(bufsize); |
| 2136 | if (tmpbuf == NULL) { |
| 2137 | break; |
| 2138 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2139 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2140 | res = _getcwd2(tmpbuf, bufsize); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2141 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2142 | res = getcwd(tmpbuf, bufsize); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2143 | #endif |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 2144 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2145 | if (res == NULL) { |
| 2146 | free(tmpbuf); |
| 2147 | } |
| 2148 | } while ((res == NULL) && (errno == ERANGE)); |
| 2149 | Py_END_ALLOW_THREADS |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 2150 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2151 | if (res == NULL) |
| 2152 | return posix_error(); |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 2153 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2154 | dynamic_return = PyString_FromString(tmpbuf); |
| 2155 | free(tmpbuf); |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 2156 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2157 | return dynamic_return; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2158 | } |
Stefan Krah | 182ae64 | 2010-07-13 19:17:08 +0000 | [diff] [blame] | 2159 | #endif /* getcwd() NULL/ERANGE workaround. */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2160 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 2161 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2162 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 2163 | "getcwdu() -> path\n\n\ |
| 2164 | Return a unicode string representing the current working directory."); |
| 2165 | |
| 2166 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2167 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2168 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2169 | char buf[1026]; |
| 2170 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2171 | |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2172 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2173 | DWORD len; |
| 2174 | wchar_t wbuf[1026]; |
| 2175 | wchar_t *wbuf2 = wbuf; |
| 2176 | PyObject *resobj; |
| 2177 | Py_BEGIN_ALLOW_THREADS |
| 2178 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2179 | /* If the buffer is large enough, len does not include the |
| 2180 | terminating \0. If the buffer is too small, len includes |
| 2181 | the space needed for the terminator. */ |
| 2182 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2183 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2184 | if (wbuf2) |
| 2185 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2186 | } |
| 2187 | Py_END_ALLOW_THREADS |
| 2188 | if (!wbuf2) { |
| 2189 | PyErr_NoMemory(); |
| 2190 | return NULL; |
| 2191 | } |
| 2192 | if (!len) { |
| 2193 | if (wbuf2 != wbuf) free(wbuf2); |
| 2194 | return win32_error("getcwdu", NULL); |
| 2195 | } |
| 2196 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2197 | if (wbuf2 != wbuf) free(wbuf2); |
| 2198 | return resobj; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2199 | #endif /* MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2200 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2201 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2202 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2203 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2204 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2205 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2206 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2207 | Py_END_ALLOW_THREADS |
| 2208 | if (res == NULL) |
| 2209 | return posix_error(); |
| 2210 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2211 | } |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2212 | #endif /* Py_USING_UNICODE */ |
| 2213 | #endif /* HAVE_GETCWD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2214 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2215 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2216 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2217 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2218 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2219 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2220 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2221 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2222 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2223 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2224 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2225 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2226 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2227 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2228 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2229 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2230 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2231 | Return a list containing the names of the entries in the directory.\n\ |
| 2232 | \n\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2233 | path: path of directory to list\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2234 | \n\ |
| 2235 | 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] | 2236 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2237 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2238 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2239 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2240 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2241 | /* XXX Should redo this putting the (now four) versions of opendir |
| 2242 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2243 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2244 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2245 | PyObject *d, *v; |
| 2246 | HANDLE hFindFile; |
| 2247 | BOOL result; |
| 2248 | WIN32_FIND_DATA FileData; |
| 2249 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 2250 | char *bufptr = namebuf; |
| 2251 | 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] | 2252 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2253 | PyObject *po; |
| 2254 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 2255 | WIN32_FIND_DATAW wFileData; |
| 2256 | Py_UNICODE *wnamebuf; |
| 2257 | /* Overallocate for \\*.*\0 */ |
| 2258 | len = PyUnicode_GET_SIZE(po); |
| 2259 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2260 | if (!wnamebuf) { |
| 2261 | PyErr_NoMemory(); |
| 2262 | return NULL; |
| 2263 | } |
| 2264 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
| 2265 | if (len > 0) { |
| 2266 | Py_UNICODE wch = wnamebuf[len-1]; |
| 2267 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2268 | wnamebuf[len++] = L'\\'; |
| 2269 | wcscpy(wnamebuf + len, L"*.*"); |
| 2270 | } |
| 2271 | if ((d = PyList_New(0)) == NULL) { |
| 2272 | free(wnamebuf); |
| 2273 | return NULL; |
| 2274 | } |
Antoine Pitrou | 8cdede4 | 2010-08-10 00:04:13 +0000 | [diff] [blame] | 2275 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2276 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | 8cdede4 | 2010-08-10 00:04:13 +0000 | [diff] [blame] | 2277 | Py_END_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2278 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2279 | int error = GetLastError(); |
| 2280 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2281 | free(wnamebuf); |
| 2282 | return d; |
| 2283 | } |
| 2284 | Py_DECREF(d); |
| 2285 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2286 | free(wnamebuf); |
| 2287 | return NULL; |
| 2288 | } |
| 2289 | do { |
| 2290 | /* Skip over . and .. */ |
| 2291 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2292 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2293 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2294 | if (v == NULL) { |
| 2295 | Py_DECREF(d); |
| 2296 | d = NULL; |
| 2297 | break; |
| 2298 | } |
| 2299 | if (PyList_Append(d, v) != 0) { |
| 2300 | Py_DECREF(v); |
| 2301 | Py_DECREF(d); |
| 2302 | d = NULL; |
| 2303 | break; |
| 2304 | } |
| 2305 | Py_DECREF(v); |
| 2306 | } |
| 2307 | Py_BEGIN_ALLOW_THREADS |
| 2308 | result = FindNextFileW(hFindFile, &wFileData); |
| 2309 | Py_END_ALLOW_THREADS |
| 2310 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2311 | it got to the end of the directory. */ |
| 2312 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2313 | Py_DECREF(d); |
| 2314 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2315 | FindClose(hFindFile); |
| 2316 | free(wnamebuf); |
| 2317 | return NULL; |
| 2318 | } |
| 2319 | } while (result == TRUE); |
Hirokazu Yamamoto | a3c5609 | 2009-06-28 10:23:00 +0000 | [diff] [blame] | 2320 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2321 | if (FindClose(hFindFile) == FALSE) { |
| 2322 | Py_DECREF(d); |
| 2323 | win32_error_unicode("FindClose", wnamebuf); |
| 2324 | free(wnamebuf); |
| 2325 | return NULL; |
| 2326 | } |
| 2327 | free(wnamebuf); |
| 2328 | return d; |
| 2329 | } |
| 2330 | /* Drop the argument parsing error as narrow strings |
| 2331 | are also valid. */ |
| 2332 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2333 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2334 | if (!PyArg_ParseTuple(args, "et#:listdir", |
| 2335 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
| 2336 | return NULL; |
| 2337 | if (len > 0) { |
| 2338 | char ch = namebuf[len-1]; |
| 2339 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2340 | namebuf[len++] = '/'; |
| 2341 | strcpy(namebuf + len, "*.*"); |
| 2342 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2343 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2344 | if ((d = PyList_New(0)) == NULL) |
| 2345 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2346 | |
Antoine Pitrou | 8cdede4 | 2010-08-10 00:04:13 +0000 | [diff] [blame] | 2347 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2348 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | 8cdede4 | 2010-08-10 00:04:13 +0000 | [diff] [blame] | 2349 | Py_END_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2350 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2351 | int error = GetLastError(); |
| 2352 | if (error == ERROR_FILE_NOT_FOUND) |
| 2353 | return d; |
| 2354 | Py_DECREF(d); |
| 2355 | return win32_error("FindFirstFile", namebuf); |
| 2356 | } |
| 2357 | do { |
| 2358 | /* Skip over . and .. */ |
| 2359 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2360 | strcmp(FileData.cFileName, "..") != 0) { |
| 2361 | v = PyString_FromString(FileData.cFileName); |
| 2362 | if (v == NULL) { |
| 2363 | Py_DECREF(d); |
| 2364 | d = NULL; |
| 2365 | break; |
| 2366 | } |
| 2367 | if (PyList_Append(d, v) != 0) { |
| 2368 | Py_DECREF(v); |
| 2369 | Py_DECREF(d); |
| 2370 | d = NULL; |
| 2371 | break; |
| 2372 | } |
| 2373 | Py_DECREF(v); |
| 2374 | } |
| 2375 | Py_BEGIN_ALLOW_THREADS |
| 2376 | result = FindNextFile(hFindFile, &FileData); |
| 2377 | Py_END_ALLOW_THREADS |
| 2378 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2379 | it got to the end of the directory. */ |
| 2380 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2381 | Py_DECREF(d); |
| 2382 | win32_error("FindNextFile", namebuf); |
| 2383 | FindClose(hFindFile); |
| 2384 | return NULL; |
| 2385 | } |
| 2386 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2387 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2388 | if (FindClose(hFindFile) == FALSE) { |
| 2389 | Py_DECREF(d); |
| 2390 | return win32_error("FindClose", namebuf); |
| 2391 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2392 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2393 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2394 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2395 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2396 | |
| 2397 | #ifndef MAX_PATH |
| 2398 | #define MAX_PATH CCHMAXPATH |
| 2399 | #endif |
| 2400 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2401 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2402 | PyObject *d, *v; |
| 2403 | char namebuf[MAX_PATH+5]; |
| 2404 | HDIR hdir = 1; |
| 2405 | ULONG srchcnt = 1; |
| 2406 | FILEFINDBUF3 ep; |
| 2407 | APIRET rc; |
| 2408 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2409 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2410 | return NULL; |
| 2411 | if (len >= MAX_PATH) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2412 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2413 | return NULL; |
| 2414 | } |
| 2415 | strcpy(namebuf, name); |
| 2416 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2417 | if (*pt == ALTSEP) |
| 2418 | *pt = SEP; |
| 2419 | if (namebuf[len-1] != SEP) |
| 2420 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2421 | strcpy(namebuf + len, "*.*"); |
| 2422 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2423 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2424 | return NULL; |
| 2425 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2426 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2427 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2428 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2429 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2430 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2431 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2432 | |
| 2433 | if (rc != NO_ERROR) { |
| 2434 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 2435 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2438 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2439 | do { |
| 2440 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2441 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2442 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2443 | |
| 2444 | strcpy(namebuf, ep.achName); |
| 2445 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2446 | /* Leave Case of Name Alone -- In Native Form */ |
| 2447 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2448 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2449 | v = PyString_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2450 | if (v == NULL) { |
| 2451 | Py_DECREF(d); |
| 2452 | d = NULL; |
| 2453 | break; |
| 2454 | } |
| 2455 | if (PyList_Append(d, v) != 0) { |
| 2456 | Py_DECREF(v); |
| 2457 | Py_DECREF(d); |
| 2458 | d = NULL; |
| 2459 | break; |
| 2460 | } |
| 2461 | Py_DECREF(v); |
| 2462 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2463 | } |
| 2464 | |
| 2465 | return d; |
| 2466 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2467 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2468 | char *name = NULL; |
| 2469 | PyObject *d, *v; |
| 2470 | DIR *dirp; |
| 2471 | struct dirent *ep; |
| 2472 | int arg_is_unicode = 1; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2473 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2474 | errno = 0; |
| 2475 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2476 | arg_is_unicode = 0; |
| 2477 | PyErr_Clear(); |
| 2478 | } |
| 2479 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
| 2480 | return NULL; |
Antoine Pitrou | 247e2fd | 2010-09-04 17:27:10 +0000 | [diff] [blame] | 2481 | Py_BEGIN_ALLOW_THREADS |
| 2482 | dirp = opendir(name); |
| 2483 | Py_END_ALLOW_THREADS |
| 2484 | if (dirp == NULL) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2485 | return posix_error_with_allocated_filename(name); |
| 2486 | } |
| 2487 | if ((d = PyList_New(0)) == NULL) { |
Antoine Pitrou | 247e2fd | 2010-09-04 17:27:10 +0000 | [diff] [blame] | 2488 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2489 | closedir(dirp); |
Antoine Pitrou | 247e2fd | 2010-09-04 17:27:10 +0000 | [diff] [blame] | 2490 | Py_END_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2491 | PyMem_Free(name); |
| 2492 | return NULL; |
| 2493 | } |
| 2494 | for (;;) { |
| 2495 | errno = 0; |
| 2496 | Py_BEGIN_ALLOW_THREADS |
| 2497 | ep = readdir(dirp); |
| 2498 | Py_END_ALLOW_THREADS |
| 2499 | if (ep == NULL) { |
| 2500 | if (errno == 0) { |
| 2501 | break; |
| 2502 | } else { |
Antoine Pitrou | 247e2fd | 2010-09-04 17:27:10 +0000 | [diff] [blame] | 2503 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2504 | closedir(dirp); |
Antoine Pitrou | 247e2fd | 2010-09-04 17:27:10 +0000 | [diff] [blame] | 2505 | Py_END_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2506 | Py_DECREF(d); |
| 2507 | return posix_error_with_allocated_filename(name); |
| 2508 | } |
| 2509 | } |
| 2510 | if (ep->d_name[0] == '.' && |
| 2511 | (NAMLEN(ep) == 1 || |
| 2512 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2513 | continue; |
| 2514 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
| 2515 | if (v == NULL) { |
| 2516 | Py_DECREF(d); |
| 2517 | d = NULL; |
| 2518 | break; |
| 2519 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2520 | #ifdef Py_USING_UNICODE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2521 | if (arg_is_unicode) { |
| 2522 | PyObject *w; |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2523 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2524 | w = PyUnicode_FromEncodedObject(v, |
| 2525 | Py_FileSystemDefaultEncoding, |
| 2526 | "strict"); |
| 2527 | if (w != NULL) { |
| 2528 | Py_DECREF(v); |
| 2529 | v = w; |
| 2530 | } |
| 2531 | else { |
| 2532 | /* fall back to the original byte string, as |
| 2533 | discussed in patch #683592 */ |
| 2534 | PyErr_Clear(); |
| 2535 | } |
| 2536 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2537 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2538 | if (PyList_Append(d, v) != 0) { |
| 2539 | Py_DECREF(v); |
| 2540 | Py_DECREF(d); |
| 2541 | d = NULL; |
| 2542 | break; |
| 2543 | } |
| 2544 | Py_DECREF(v); |
| 2545 | } |
Antoine Pitrou | 247e2fd | 2010-09-04 17:27:10 +0000 | [diff] [blame] | 2546 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2547 | closedir(dirp); |
Antoine Pitrou | 247e2fd | 2010-09-04 17:27:10 +0000 | [diff] [blame] | 2548 | Py_END_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2549 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2550 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2551 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2552 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2553 | #endif /* which OS */ |
| 2554 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2555 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2556 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2557 | /* A helper function for abspath on win32 */ |
| 2558 | static PyObject * |
| 2559 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2560 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2561 | /* assume encoded strings won't more than double no of chars */ |
| 2562 | char inbuf[MAX_PATH*2]; |
| 2563 | char *inbufp = inbuf; |
| 2564 | Py_ssize_t insize = sizeof(inbuf); |
| 2565 | char outbuf[MAX_PATH*2]; |
| 2566 | char *temp; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2567 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2568 | PyUnicodeObject *po; |
| 2569 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2570 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2571 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 2572 | Py_UNICODE *wtemp; |
| 2573 | DWORD result; |
| 2574 | PyObject *v; |
| 2575 | result = GetFullPathNameW(wpath, |
| 2576 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2577 | woutbuf, &wtemp); |
| 2578 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { |
| 2579 | woutbufp = malloc(result * sizeof(Py_UNICODE)); |
| 2580 | if (!woutbufp) |
| 2581 | return PyErr_NoMemory(); |
| 2582 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2583 | } |
| 2584 | if (result) |
| 2585 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2586 | else |
| 2587 | v = win32_error_unicode("GetFullPathNameW", wpath); |
| 2588 | if (woutbufp != woutbuf) |
| 2589 | free(woutbufp); |
| 2590 | return v; |
| 2591 | } |
| 2592 | /* Drop the argument parsing error as narrow strings |
| 2593 | are also valid. */ |
| 2594 | PyErr_Clear(); |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2595 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2596 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 2597 | Py_FileSystemDefaultEncoding, &inbufp, |
| 2598 | &insize)) |
| 2599 | return NULL; |
| 2600 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2601 | outbuf, &temp)) |
| 2602 | return win32_error("GetFullPathName", inbuf); |
| 2603 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2604 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2605 | Py_FileSystemDefaultEncoding, NULL); |
| 2606 | } |
| 2607 | return PyString_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2608 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2609 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2610 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2611 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2612 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2613 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2614 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2615 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2616 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2617 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2618 | int res; |
| 2619 | char *path = NULL; |
| 2620 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2621 | |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2622 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2623 | PyUnicodeObject *po; |
| 2624 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
| 2625 | Py_BEGIN_ALLOW_THREADS |
| 2626 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2627 | it is a simple dereference. */ |
| 2628 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
| 2629 | Py_END_ALLOW_THREADS |
| 2630 | if (!res) |
| 2631 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
| 2632 | Py_INCREF(Py_None); |
| 2633 | return Py_None; |
| 2634 | } |
| 2635 | /* Drop the argument parsing error as narrow strings |
| 2636 | are also valid. */ |
| 2637 | PyErr_Clear(); |
| 2638 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 2639 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 2640 | return NULL; |
| 2641 | Py_BEGIN_ALLOW_THREADS |
| 2642 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2643 | it is a simple dereference. */ |
| 2644 | res = CreateDirectoryA(path, NULL); |
| 2645 | Py_END_ALLOW_THREADS |
| 2646 | if (!res) { |
| 2647 | win32_error("mkdir", path); |
| 2648 | PyMem_Free(path); |
| 2649 | return NULL; |
| 2650 | } |
| 2651 | PyMem_Free(path); |
| 2652 | Py_INCREF(Py_None); |
| 2653 | return Py_None; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2654 | #else /* MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2655 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2656 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 2657 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 2658 | return NULL; |
| 2659 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2660 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2661 | res = mkdir(path); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2662 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2663 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2664 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2665 | Py_END_ALLOW_THREADS |
| 2666 | if (res < 0) |
| 2667 | return posix_error_with_allocated_filename(path); |
| 2668 | PyMem_Free(path); |
| 2669 | Py_INCREF(Py_None); |
| 2670 | return Py_None; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2671 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2674 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2675 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2676 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2677 | #include <sys/resource.h> |
| 2678 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2679 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2680 | |
| 2681 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2682 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2683 | "nice(inc) -> new_priority\n\n\ |
| 2684 | 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] | 2685 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2686 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2687 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2688 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2689 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2690 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2691 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 2692 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2693 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2694 | /* There are two flavours of 'nice': one that returns the new |
| 2695 | priority (as required by almost all standards out there) and the |
| 2696 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2697 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2698 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2699 | If we are of the nice family that returns the new priority, we |
| 2700 | need to clear errno before the call, and check if errno is filled |
| 2701 | before calling posix_error() on a returnvalue of -1, because the |
| 2702 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2703 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2704 | errno = 0; |
| 2705 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2706 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2707 | if (value == 0) |
| 2708 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2709 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2710 | if (value == -1 && errno != 0) |
| 2711 | /* either nice() or getpriority() returned an error */ |
| 2712 | return posix_error(); |
| 2713 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2714 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2715 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2716 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2717 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2718 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2719 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2720 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2721 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2722 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2723 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2724 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2725 | PyObject *o1, *o2; |
| 2726 | char *p1, *p2; |
| 2727 | BOOL result; |
| 2728 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
| 2729 | goto error; |
| 2730 | if (!convert_to_unicode(&o1)) |
| 2731 | goto error; |
| 2732 | if (!convert_to_unicode(&o2)) { |
| 2733 | Py_DECREF(o1); |
| 2734 | goto error; |
| 2735 | } |
| 2736 | Py_BEGIN_ALLOW_THREADS |
| 2737 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2738 | PyUnicode_AsUnicode(o2)); |
| 2739 | Py_END_ALLOW_THREADS |
| 2740 | Py_DECREF(o1); |
| 2741 | Py_DECREF(o2); |
| 2742 | if (!result) |
| 2743 | return win32_error("rename", NULL); |
| 2744 | Py_INCREF(Py_None); |
| 2745 | return Py_None; |
Hirokazu Yamamoto | a3c5609 | 2009-06-28 10:23:00 +0000 | [diff] [blame] | 2746 | error: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2747 | PyErr_Clear(); |
| 2748 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2749 | return NULL; |
| 2750 | Py_BEGIN_ALLOW_THREADS |
| 2751 | result = MoveFileA(p1, p2); |
| 2752 | Py_END_ALLOW_THREADS |
| 2753 | if (!result) |
| 2754 | return win32_error("rename", NULL); |
| 2755 | Py_INCREF(Py_None); |
| 2756 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2757 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2758 | return posix_2str(args, "etet:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2759 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2762 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2763 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2764 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2765 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2766 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2767 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2768 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2769 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2770 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2771 | return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2772 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2773 | return posix_1str(args, "et:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2774 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2777 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2778 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2779 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2780 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2781 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2782 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2783 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2784 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2785 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2786 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2787 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2788 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2789 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2792 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2793 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2794 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2795 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2796 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2797 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2798 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2799 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2800 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2801 | char *command; |
| 2802 | long sts; |
| 2803 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
| 2804 | return NULL; |
| 2805 | Py_BEGIN_ALLOW_THREADS |
| 2806 | sts = system(command); |
| 2807 | Py_END_ALLOW_THREADS |
| 2808 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2809 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2810 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2811 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2812 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2813 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2814 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2815 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2816 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2817 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2818 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2819 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2820 | int i; |
| 2821 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 2822 | return NULL; |
| 2823 | i = (int)umask(i); |
| 2824 | if (i < 0) |
| 2825 | return posix_error(); |
| 2826 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2829 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2830 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2831 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2832 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2833 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2834 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2835 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2836 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2837 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2838 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2839 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2840 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2841 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2842 | return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2843 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2844 | return posix_1str(args, "et:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2845 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2848 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2849 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2850 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2851 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2852 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2853 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2854 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2855 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2856 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2857 | struct utsname u; |
| 2858 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2859 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2860 | Py_BEGIN_ALLOW_THREADS |
| 2861 | res = uname(&u); |
| 2862 | Py_END_ALLOW_THREADS |
| 2863 | if (res < 0) |
| 2864 | return posix_error(); |
| 2865 | return Py_BuildValue("(sssss)", |
| 2866 | u.sysname, |
| 2867 | u.nodename, |
| 2868 | u.release, |
| 2869 | u.version, |
| 2870 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2871 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2872 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2873 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2874 | static int |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 2875 | extract_time(PyObject *t, time_t* sec, long* usec) |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2876 | { |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 2877 | time_t intval; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2878 | if (PyFloat_Check(t)) { |
| 2879 | double tval = PyFloat_AsDouble(t); |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 2880 | PyObject *intobj = PyNumber_Long(t); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2881 | if (!intobj) |
| 2882 | return -1; |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 2883 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 2884 | intval = PyInt_AsUnsignedLongLongMask(intobj); |
| 2885 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2886 | intval = PyInt_AsLong(intobj); |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 2887 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2888 | Py_DECREF(intobj); |
| 2889 | if (intval == -1 && PyErr_Occurred()) |
| 2890 | return -1; |
| 2891 | *sec = intval; |
| 2892 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
| 2893 | if (*usec < 0) |
| 2894 | /* If rounding gave us a negative number, |
| 2895 | truncate. */ |
| 2896 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2897 | return 0; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2898 | } |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 2899 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 2900 | intval = PyInt_AsUnsignedLongLongMask(t); |
| 2901 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2902 | intval = PyInt_AsLong(t); |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 2903 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2904 | if (intval == -1 && PyErr_Occurred()) |
| 2905 | return -1; |
| 2906 | *sec = intval; |
| 2907 | *usec = 0; |
| 2908 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2909 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2910 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2911 | PyDoc_STRVAR(posix_utime__doc__, |
Georg Brandl | 9e5b5e4 | 2006-05-17 14:18:20 +0000 | [diff] [blame] | 2912 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2913 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2914 | 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] | 2915 | 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] | 2916 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2917 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2918 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2919 | { |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2920 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2921 | PyObject *arg; |
| 2922 | PyUnicodeObject *obwpath; |
| 2923 | wchar_t *wpath = NULL; |
| 2924 | char *apath = NULL; |
| 2925 | HANDLE hFile; |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 2926 | time_t atimesec, mtimesec; |
| 2927 | long ausec, musec; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2928 | FILETIME atime, mtime; |
| 2929 | PyObject *result = NULL; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2930 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2931 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2932 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2933 | Py_BEGIN_ALLOW_THREADS |
| 2934 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 2935 | NULL, OPEN_EXISTING, |
| 2936 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 2937 | Py_END_ALLOW_THREADS |
| 2938 | if (hFile == INVALID_HANDLE_VALUE) |
| 2939 | return win32_error_unicode("utime", wpath); |
| 2940 | } else |
| 2941 | /* Drop the argument parsing error as narrow strings |
| 2942 | are also valid. */ |
| 2943 | PyErr_Clear(); |
Hirokazu Yamamoto | a3c5609 | 2009-06-28 10:23:00 +0000 | [diff] [blame] | 2944 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2945 | if (!wpath) { |
| 2946 | if (!PyArg_ParseTuple(args, "etO:utime", |
| 2947 | Py_FileSystemDefaultEncoding, &apath, &arg)) |
| 2948 | return NULL; |
| 2949 | Py_BEGIN_ALLOW_THREADS |
| 2950 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 2951 | NULL, OPEN_EXISTING, |
| 2952 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 2953 | Py_END_ALLOW_THREADS |
| 2954 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2955 | win32_error("utime", apath); |
| 2956 | PyMem_Free(apath); |
| 2957 | return NULL; |
| 2958 | } |
| 2959 | PyMem_Free(apath); |
| 2960 | } |
| 2961 | |
| 2962 | if (arg == Py_None) { |
| 2963 | SYSTEMTIME now; |
| 2964 | GetSystemTime(&now); |
| 2965 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2966 | !SystemTimeToFileTime(&now, &atime)) { |
| 2967 | win32_error("utime", NULL); |
| 2968 | goto done; |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 2969 | } |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2970 | } |
| 2971 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2972 | PyErr_SetString(PyExc_TypeError, |
| 2973 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2974 | goto done; |
| 2975 | } |
| 2976 | else { |
| 2977 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2978 | &atimesec, &ausec) == -1) |
| 2979 | goto done; |
| 2980 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
| 2981 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2982 | &mtimesec, &musec) == -1) |
| 2983 | goto done; |
| 2984 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
| 2985 | } |
| 2986 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2987 | /* Avoid putting the file name into the error here, |
| 2988 | as that may confuse the user into believing that |
| 2989 | something is wrong with the file, when it also |
| 2990 | could be the time stamp that gives a problem. */ |
| 2991 | win32_error("utime", NULL); |
Antoine Pitrou | 1f1613f | 2011-01-06 18:30:26 +0000 | [diff] [blame] | 2992 | goto done; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2993 | } |
| 2994 | Py_INCREF(Py_None); |
| 2995 | result = Py_None; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2996 | done: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2997 | CloseHandle(hFile); |
| 2998 | return result; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2999 | #else /* MS_WINDOWS */ |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 3000 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3001 | char *path = NULL; |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 3002 | time_t atime, mtime; |
| 3003 | long ausec, musec; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3004 | int res; |
| 3005 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3006 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3007 | #if defined(HAVE_UTIMES) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3008 | struct timeval buf[2]; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3009 | #define ATIME buf[0].tv_sec |
| 3010 | #define MTIME buf[1].tv_sec |
| 3011 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 3012 | /* XXX should define struct utimbuf instead, above */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3013 | struct utimbuf buf; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3014 | #define ATIME buf.actime |
| 3015 | #define MTIME buf.modtime |
| 3016 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3017 | #else /* HAVE_UTIMES */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3018 | time_t buf[2]; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3019 | #define ATIME buf[0] |
| 3020 | #define MTIME buf[1] |
| 3021 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3022 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3023 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3024 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3025 | if (!PyArg_ParseTuple(args, "etO:utime", |
| 3026 | Py_FileSystemDefaultEncoding, &path, &arg)) |
| 3027 | return NULL; |
| 3028 | if (arg == Py_None) { |
| 3029 | /* optional time values not given */ |
| 3030 | Py_BEGIN_ALLOW_THREADS |
| 3031 | res = utime(path, NULL); |
| 3032 | Py_END_ALLOW_THREADS |
| 3033 | } |
| 3034 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3035 | PyErr_SetString(PyExc_TypeError, |
| 3036 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3037 | PyMem_Free(path); |
| 3038 | return NULL; |
| 3039 | } |
| 3040 | else { |
| 3041 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3042 | &atime, &ausec) == -1) { |
| 3043 | PyMem_Free(path); |
| 3044 | return NULL; |
| 3045 | } |
| 3046 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3047 | &mtime, &musec) == -1) { |
| 3048 | PyMem_Free(path); |
| 3049 | return NULL; |
| 3050 | } |
| 3051 | ATIME = atime; |
| 3052 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3053 | #ifdef HAVE_UTIMES |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3054 | buf[0].tv_usec = ausec; |
| 3055 | buf[1].tv_usec = musec; |
| 3056 | Py_BEGIN_ALLOW_THREADS |
| 3057 | res = utimes(path, buf); |
| 3058 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3059 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3060 | Py_BEGIN_ALLOW_THREADS |
| 3061 | res = utime(path, UTIME_ARG); |
| 3062 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3063 | #endif /* HAVE_UTIMES */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3064 | } |
| 3065 | if (res < 0) { |
| 3066 | return posix_error_with_allocated_filename(path); |
| 3067 | } |
| 3068 | PyMem_Free(path); |
| 3069 | Py_INCREF(Py_None); |
| 3070 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3071 | #undef UTIME_ARG |
| 3072 | #undef ATIME |
| 3073 | #undef MTIME |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 3074 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3077 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3078 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3079 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3080 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3081 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3082 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3083 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3084 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3085 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3086 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3087 | int sts; |
| 3088 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 3089 | return NULL; |
| 3090 | _exit(sts); |
| 3091 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3094 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3095 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3096 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3097 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3098 | Py_ssize_t i; |
| 3099 | for (i = 0; i < count; i++) |
| 3100 | PyMem_Free(array[i]); |
| 3101 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3102 | } |
| 3103 | #endif |
| 3104 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3105 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3106 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3107 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3108 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3109 | Execute an executable path with arguments, replacing current process.\n\ |
| 3110 | \n\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3111 | path: path of executable file\n\ |
| 3112 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3113 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3114 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3115 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3116 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3117 | char *path; |
| 3118 | PyObject *argv; |
| 3119 | char **argvlist; |
| 3120 | Py_ssize_t i, argc; |
| 3121 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3122 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3123 | /* execv has two arguments: (path, argv), where |
| 3124 | argv is a list or tuple of strings. */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3125 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3126 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 3127 | Py_FileSystemDefaultEncoding, |
| 3128 | &path, &argv)) |
| 3129 | return NULL; |
| 3130 | if (PyList_Check(argv)) { |
| 3131 | argc = PyList_Size(argv); |
| 3132 | getitem = PyList_GetItem; |
| 3133 | } |
| 3134 | else if (PyTuple_Check(argv)) { |
| 3135 | argc = PyTuple_Size(argv); |
| 3136 | getitem = PyTuple_GetItem; |
| 3137 | } |
| 3138 | else { |
| 3139 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); |
| 3140 | PyMem_Free(path); |
| 3141 | return NULL; |
| 3142 | } |
| 3143 | if (argc < 1) { |
| 3144 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 3145 | PyMem_Free(path); |
| 3146 | return NULL; |
| 3147 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3148 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3149 | argvlist = PyMem_NEW(char *, argc+1); |
| 3150 | if (argvlist == NULL) { |
| 3151 | PyMem_Free(path); |
| 3152 | return PyErr_NoMemory(); |
| 3153 | } |
| 3154 | for (i = 0; i < argc; i++) { |
| 3155 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3156 | Py_FileSystemDefaultEncoding, |
| 3157 | &argvlist[i])) { |
| 3158 | free_string_array(argvlist, i); |
| 3159 | PyErr_SetString(PyExc_TypeError, |
| 3160 | "execv() arg 2 must contain only strings"); |
| 3161 | PyMem_Free(path); |
| 3162 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3163 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3164 | } |
| 3165 | } |
| 3166 | argvlist[argc] = NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3167 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3168 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3169 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3170 | /* If we get here it's definitely an error */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3171 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3172 | free_string_array(argvlist, argc); |
| 3173 | PyMem_Free(path); |
| 3174 | return posix_error(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3175 | } |
| 3176 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3177 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3178 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3179 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3180 | Execute a path with arguments and environment, replacing current process.\n\ |
| 3181 | \n\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3182 | path: path of executable file\n\ |
| 3183 | args: tuple or list of arguments\n\ |
| 3184 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3185 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3186 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3187 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3188 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3189 | char *path; |
| 3190 | PyObject *argv, *env; |
| 3191 | char **argvlist; |
| 3192 | char **envlist; |
| 3193 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
| 3194 | Py_ssize_t i, pos, argc, envc; |
| 3195 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 3196 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3197 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3198 | /* execve has three arguments: (path, argv, env), where |
| 3199 | argv is a list or tuple of strings and env is a dictionary |
| 3200 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3201 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3202 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 3203 | Py_FileSystemDefaultEncoding, |
| 3204 | &path, &argv, &env)) |
| 3205 | return NULL; |
| 3206 | if (PyList_Check(argv)) { |
| 3207 | argc = PyList_Size(argv); |
| 3208 | getitem = PyList_GetItem; |
| 3209 | } |
| 3210 | else if (PyTuple_Check(argv)) { |
| 3211 | argc = PyTuple_Size(argv); |
| 3212 | getitem = PyTuple_GetItem; |
| 3213 | } |
| 3214 | else { |
| 3215 | PyErr_SetString(PyExc_TypeError, |
| 3216 | "execve() arg 2 must be a tuple or list"); |
| 3217 | goto fail_0; |
| 3218 | } |
| 3219 | if (!PyMapping_Check(env)) { |
| 3220 | PyErr_SetString(PyExc_TypeError, |
| 3221 | "execve() arg 3 must be a mapping object"); |
| 3222 | goto fail_0; |
| 3223 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3224 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3225 | argvlist = PyMem_NEW(char *, argc+1); |
| 3226 | if (argvlist == NULL) { |
| 3227 | PyErr_NoMemory(); |
| 3228 | goto fail_0; |
| 3229 | } |
| 3230 | for (i = 0; i < argc; i++) { |
| 3231 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3232 | "et;execve() arg 2 must contain only strings", |
| 3233 | Py_FileSystemDefaultEncoding, |
| 3234 | &argvlist[i])) |
| 3235 | { |
| 3236 | lastarg = i; |
| 3237 | goto fail_1; |
| 3238 | } |
| 3239 | } |
| 3240 | lastarg = argc; |
| 3241 | argvlist[argc] = NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3242 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3243 | i = PyMapping_Size(env); |
| 3244 | if (i < 0) |
| 3245 | goto fail_1; |
| 3246 | envlist = PyMem_NEW(char *, i + 1); |
| 3247 | if (envlist == NULL) { |
| 3248 | PyErr_NoMemory(); |
| 3249 | goto fail_1; |
| 3250 | } |
| 3251 | envc = 0; |
| 3252 | keys = PyMapping_Keys(env); |
| 3253 | vals = PyMapping_Values(env); |
| 3254 | if (!keys || !vals) |
| 3255 | goto fail_2; |
| 3256 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3257 | PyErr_SetString(PyExc_TypeError, |
| 3258 | "execve(): env.keys() or env.values() is not a list"); |
| 3259 | goto fail_2; |
| 3260 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3261 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3262 | for (pos = 0; pos < i; pos++) { |
| 3263 | char *p, *k, *v; |
| 3264 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3265 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3266 | key = PyList_GetItem(keys, pos); |
| 3267 | val = PyList_GetItem(vals, pos); |
| 3268 | if (!key || !val) |
| 3269 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3270 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3271 | if (!PyArg_Parse( |
| 3272 | key, |
| 3273 | "s;execve() arg 3 contains a non-string key", |
| 3274 | &k) || |
| 3275 | !PyArg_Parse( |
| 3276 | val, |
| 3277 | "s;execve() arg 3 contains a non-string value", |
| 3278 | &v)) |
| 3279 | { |
| 3280 | goto fail_2; |
| 3281 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3282 | |
| 3283 | #if defined(PYOS_OS2) |
| 3284 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3285 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 3286 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3287 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3288 | p = PyMem_NEW(char, len); |
| 3289 | if (p == NULL) { |
| 3290 | PyErr_NoMemory(); |
| 3291 | goto fail_2; |
| 3292 | } |
| 3293 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3294 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3295 | #if defined(PYOS_OS2) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3296 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3297 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3298 | } |
| 3299 | envlist[envc] = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3300 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3301 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3302 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3303 | /* If we get here it's definitely an error */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3304 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3305 | (void) posix_error(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3306 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3307 | fail_2: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3308 | while (--envc >= 0) |
| 3309 | PyMem_DEL(envlist[envc]); |
| 3310 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3311 | fail_1: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3312 | free_string_array(argvlist, lastarg); |
| 3313 | Py_XDECREF(vals); |
| 3314 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3315 | fail_0: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3316 | PyMem_Free(path); |
| 3317 | return NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3318 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3319 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3320 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3321 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3322 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3323 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3324 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3325 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3326 | \n\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3327 | mode: mode of process creation\n\ |
| 3328 | path: path of executable file\n\ |
| 3329 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3330 | |
| 3331 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3332 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3333 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3334 | char *path; |
| 3335 | PyObject *argv; |
| 3336 | char **argvlist; |
| 3337 | int mode, i; |
| 3338 | Py_ssize_t argc; |
| 3339 | Py_intptr_t spawnval; |
| 3340 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3341 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3342 | /* spawnv has three arguments: (mode, path, argv), where |
| 3343 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3344 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3345 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 3346 | Py_FileSystemDefaultEncoding, |
| 3347 | &path, &argv)) |
| 3348 | return NULL; |
| 3349 | if (PyList_Check(argv)) { |
| 3350 | argc = PyList_Size(argv); |
| 3351 | getitem = PyList_GetItem; |
| 3352 | } |
| 3353 | else if (PyTuple_Check(argv)) { |
| 3354 | argc = PyTuple_Size(argv); |
| 3355 | getitem = PyTuple_GetItem; |
| 3356 | } |
| 3357 | else { |
| 3358 | PyErr_SetString(PyExc_TypeError, |
| 3359 | "spawnv() arg 2 must be a tuple or list"); |
| 3360 | PyMem_Free(path); |
| 3361 | return NULL; |
| 3362 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3363 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3364 | argvlist = PyMem_NEW(char *, argc+1); |
| 3365 | if (argvlist == NULL) { |
| 3366 | PyMem_Free(path); |
| 3367 | return PyErr_NoMemory(); |
| 3368 | } |
| 3369 | for (i = 0; i < argc; i++) { |
| 3370 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3371 | Py_FileSystemDefaultEncoding, |
| 3372 | &argvlist[i])) { |
| 3373 | free_string_array(argvlist, i); |
| 3374 | PyErr_SetString( |
| 3375 | PyExc_TypeError, |
| 3376 | "spawnv() arg 2 must contain only strings"); |
| 3377 | PyMem_Free(path); |
| 3378 | return NULL; |
| 3379 | } |
| 3380 | } |
| 3381 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3382 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3383 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3384 | Py_BEGIN_ALLOW_THREADS |
| 3385 | spawnval = spawnv(mode, path, argvlist); |
| 3386 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3387 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3388 | if (mode == _OLD_P_OVERLAY) |
| 3389 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3390 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3391 | Py_BEGIN_ALLOW_THREADS |
| 3392 | spawnval = _spawnv(mode, path, argvlist); |
| 3393 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3394 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3395 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3396 | free_string_array(argvlist, argc); |
| 3397 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3398 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3399 | if (spawnval == -1) |
| 3400 | return posix_error(); |
| 3401 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3402 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3403 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3404 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3405 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3406 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3410 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3411 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3412 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3413 | \n\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3414 | mode: mode of process creation\n\ |
| 3415 | path: path of executable file\n\ |
| 3416 | args: tuple or list of arguments\n\ |
| 3417 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3418 | |
| 3419 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3420 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3421 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3422 | char *path; |
| 3423 | PyObject *argv, *env; |
| 3424 | char **argvlist; |
| 3425 | char **envlist; |
| 3426 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3427 | int mode, pos, envc; |
| 3428 | Py_ssize_t argc, i; |
| 3429 | Py_intptr_t spawnval; |
| 3430 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 3431 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3432 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3433 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3434 | argv is a list or tuple of strings and env is a dictionary |
| 3435 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3436 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3437 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 3438 | Py_FileSystemDefaultEncoding, |
| 3439 | &path, &argv, &env)) |
| 3440 | return NULL; |
| 3441 | if (PyList_Check(argv)) { |
| 3442 | argc = PyList_Size(argv); |
| 3443 | getitem = PyList_GetItem; |
| 3444 | } |
| 3445 | else if (PyTuple_Check(argv)) { |
| 3446 | argc = PyTuple_Size(argv); |
| 3447 | getitem = PyTuple_GetItem; |
| 3448 | } |
| 3449 | else { |
| 3450 | PyErr_SetString(PyExc_TypeError, |
| 3451 | "spawnve() arg 2 must be a tuple or list"); |
| 3452 | goto fail_0; |
| 3453 | } |
| 3454 | if (!PyMapping_Check(env)) { |
| 3455 | PyErr_SetString(PyExc_TypeError, |
| 3456 | "spawnve() arg 3 must be a mapping object"); |
| 3457 | goto fail_0; |
| 3458 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3459 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3460 | argvlist = PyMem_NEW(char *, argc+1); |
| 3461 | if (argvlist == NULL) { |
| 3462 | PyErr_NoMemory(); |
| 3463 | goto fail_0; |
| 3464 | } |
| 3465 | for (i = 0; i < argc; i++) { |
| 3466 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3467 | "et;spawnve() arg 2 must contain only strings", |
| 3468 | Py_FileSystemDefaultEncoding, |
| 3469 | &argvlist[i])) |
| 3470 | { |
| 3471 | lastarg = i; |
| 3472 | goto fail_1; |
| 3473 | } |
| 3474 | } |
| 3475 | lastarg = argc; |
| 3476 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3477 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3478 | i = PyMapping_Size(env); |
| 3479 | if (i < 0) |
| 3480 | goto fail_1; |
| 3481 | envlist = PyMem_NEW(char *, i + 1); |
| 3482 | if (envlist == NULL) { |
| 3483 | PyErr_NoMemory(); |
| 3484 | goto fail_1; |
| 3485 | } |
| 3486 | envc = 0; |
| 3487 | keys = PyMapping_Keys(env); |
| 3488 | vals = PyMapping_Values(env); |
| 3489 | if (!keys || !vals) |
| 3490 | goto fail_2; |
| 3491 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3492 | PyErr_SetString(PyExc_TypeError, |
| 3493 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3494 | goto fail_2; |
| 3495 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3496 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3497 | for (pos = 0; pos < i; pos++) { |
| 3498 | char *p, *k, *v; |
| 3499 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3500 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3501 | key = PyList_GetItem(keys, pos); |
| 3502 | val = PyList_GetItem(vals, pos); |
| 3503 | if (!key || !val) |
| 3504 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3505 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3506 | if (!PyArg_Parse( |
| 3507 | key, |
| 3508 | "s;spawnve() arg 3 contains a non-string key", |
| 3509 | &k) || |
| 3510 | !PyArg_Parse( |
| 3511 | val, |
| 3512 | "s;spawnve() arg 3 contains a non-string value", |
| 3513 | &v)) |
| 3514 | { |
| 3515 | goto fail_2; |
| 3516 | } |
| 3517 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3518 | p = PyMem_NEW(char, len); |
| 3519 | if (p == NULL) { |
| 3520 | PyErr_NoMemory(); |
| 3521 | goto fail_2; |
| 3522 | } |
| 3523 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3524 | envlist[envc++] = p; |
| 3525 | } |
| 3526 | envlist[envc] = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3527 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3528 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3529 | Py_BEGIN_ALLOW_THREADS |
| 3530 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3531 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3532 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3533 | if (mode == _OLD_P_OVERLAY) |
| 3534 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3535 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3536 | Py_BEGIN_ALLOW_THREADS |
| 3537 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3538 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3539 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3540 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3541 | if (spawnval == -1) |
| 3542 | (void) posix_error(); |
| 3543 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3544 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3545 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3546 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3547 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3548 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3549 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3550 | fail_2: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3551 | while (--envc >= 0) |
| 3552 | PyMem_DEL(envlist[envc]); |
| 3553 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3554 | fail_1: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3555 | free_string_array(argvlist, lastarg); |
| 3556 | Py_XDECREF(vals); |
| 3557 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3558 | fail_0: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3559 | PyMem_Free(path); |
| 3560 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3561 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3562 | |
| 3563 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3564 | #if defined(PYOS_OS2) |
| 3565 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3566 | "spawnvp(mode, file, args)\n\n\ |
| 3567 | Execute the program 'file' in a new process, using the environment\n\ |
| 3568 | search path to find the file.\n\ |
| 3569 | \n\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3570 | mode: mode of process creation\n\ |
| 3571 | file: executable file name\n\ |
| 3572 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3573 | |
| 3574 | static PyObject * |
| 3575 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3576 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3577 | char *path; |
| 3578 | PyObject *argv; |
| 3579 | char **argvlist; |
| 3580 | int mode, i, argc; |
| 3581 | Py_intptr_t spawnval; |
| 3582 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3583 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3584 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3585 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3586 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3587 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 3588 | Py_FileSystemDefaultEncoding, |
| 3589 | &path, &argv)) |
| 3590 | return NULL; |
| 3591 | if (PyList_Check(argv)) { |
| 3592 | argc = PyList_Size(argv); |
| 3593 | getitem = PyList_GetItem; |
| 3594 | } |
| 3595 | else if (PyTuple_Check(argv)) { |
| 3596 | argc = PyTuple_Size(argv); |
| 3597 | getitem = PyTuple_GetItem; |
| 3598 | } |
| 3599 | else { |
| 3600 | PyErr_SetString(PyExc_TypeError, |
| 3601 | "spawnvp() arg 2 must be a tuple or list"); |
| 3602 | PyMem_Free(path); |
| 3603 | return NULL; |
| 3604 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3605 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3606 | argvlist = PyMem_NEW(char *, argc+1); |
| 3607 | if (argvlist == NULL) { |
| 3608 | PyMem_Free(path); |
| 3609 | return PyErr_NoMemory(); |
| 3610 | } |
| 3611 | for (i = 0; i < argc; i++) { |
| 3612 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3613 | Py_FileSystemDefaultEncoding, |
| 3614 | &argvlist[i])) { |
| 3615 | free_string_array(argvlist, i); |
| 3616 | PyErr_SetString( |
| 3617 | PyExc_TypeError, |
| 3618 | "spawnvp() arg 2 must contain only strings"); |
| 3619 | PyMem_Free(path); |
| 3620 | return NULL; |
| 3621 | } |
| 3622 | } |
| 3623 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3624 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3625 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3626 | #if defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3627 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3628 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3629 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3630 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3631 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3632 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3633 | free_string_array(argvlist, argc); |
| 3634 | PyMem_Free(path); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3635 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3636 | if (spawnval == -1) |
| 3637 | return posix_error(); |
| 3638 | else |
| 3639 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
| 3642 | |
| 3643 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3644 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3645 | Execute the program 'file' in a new process, using the environment\n\ |
| 3646 | search path to find the file.\n\ |
| 3647 | \n\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3648 | mode: mode of process creation\n\ |
| 3649 | file: executable file name\n\ |
| 3650 | args: tuple or list of arguments\n\ |
| 3651 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3652 | |
| 3653 | static PyObject * |
| 3654 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3655 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3656 | char *path; |
| 3657 | PyObject *argv, *env; |
| 3658 | char **argvlist; |
| 3659 | char **envlist; |
| 3660 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3661 | int mode, i, pos, argc, envc; |
| 3662 | Py_intptr_t spawnval; |
| 3663 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 3664 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3665 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3666 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3667 | argv is a list or tuple of strings and env is a dictionary |
| 3668 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3669 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3670 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 3671 | Py_FileSystemDefaultEncoding, |
| 3672 | &path, &argv, &env)) |
| 3673 | return NULL; |
| 3674 | if (PyList_Check(argv)) { |
| 3675 | argc = PyList_Size(argv); |
| 3676 | getitem = PyList_GetItem; |
| 3677 | } |
| 3678 | else if (PyTuple_Check(argv)) { |
| 3679 | argc = PyTuple_Size(argv); |
| 3680 | getitem = PyTuple_GetItem; |
| 3681 | } |
| 3682 | else { |
| 3683 | PyErr_SetString(PyExc_TypeError, |
| 3684 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3685 | goto fail_0; |
| 3686 | } |
| 3687 | if (!PyMapping_Check(env)) { |
| 3688 | PyErr_SetString(PyExc_TypeError, |
| 3689 | "spawnvpe() arg 3 must be a mapping object"); |
| 3690 | goto fail_0; |
| 3691 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3692 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3693 | argvlist = PyMem_NEW(char *, argc+1); |
| 3694 | if (argvlist == NULL) { |
| 3695 | PyErr_NoMemory(); |
| 3696 | goto fail_0; |
| 3697 | } |
| 3698 | for (i = 0; i < argc; i++) { |
| 3699 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3700 | "et;spawnvpe() arg 2 must contain only strings", |
| 3701 | Py_FileSystemDefaultEncoding, |
| 3702 | &argvlist[i])) |
| 3703 | { |
| 3704 | lastarg = i; |
| 3705 | goto fail_1; |
| 3706 | } |
| 3707 | } |
| 3708 | lastarg = argc; |
| 3709 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3710 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3711 | i = PyMapping_Size(env); |
| 3712 | if (i < 0) |
| 3713 | goto fail_1; |
| 3714 | envlist = PyMem_NEW(char *, i + 1); |
| 3715 | if (envlist == NULL) { |
| 3716 | PyErr_NoMemory(); |
| 3717 | goto fail_1; |
| 3718 | } |
| 3719 | envc = 0; |
| 3720 | keys = PyMapping_Keys(env); |
| 3721 | vals = PyMapping_Values(env); |
| 3722 | if (!keys || !vals) |
| 3723 | goto fail_2; |
| 3724 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3725 | PyErr_SetString(PyExc_TypeError, |
| 3726 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3727 | goto fail_2; |
| 3728 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3729 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3730 | for (pos = 0; pos < i; pos++) { |
| 3731 | char *p, *k, *v; |
| 3732 | size_t len; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3733 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3734 | key = PyList_GetItem(keys, pos); |
| 3735 | val = PyList_GetItem(vals, pos); |
| 3736 | if (!key || !val) |
| 3737 | goto fail_2; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3738 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3739 | if (!PyArg_Parse( |
| 3740 | key, |
| 3741 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3742 | &k) || |
| 3743 | !PyArg_Parse( |
| 3744 | val, |
| 3745 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3746 | &v)) |
| 3747 | { |
| 3748 | goto fail_2; |
| 3749 | } |
| 3750 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3751 | p = PyMem_NEW(char, len); |
| 3752 | if (p == NULL) { |
| 3753 | PyErr_NoMemory(); |
| 3754 | goto fail_2; |
| 3755 | } |
| 3756 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3757 | envlist[envc++] = p; |
| 3758 | } |
| 3759 | envlist[envc] = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3760 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3761 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3762 | #if defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3763 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3764 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3765 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3766 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3767 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3768 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3769 | if (spawnval == -1) |
| 3770 | (void) posix_error(); |
| 3771 | else |
| 3772 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3773 | |
| 3774 | fail_2: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3775 | while (--envc >= 0) |
| 3776 | PyMem_DEL(envlist[envc]); |
| 3777 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3778 | fail_1: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3779 | free_string_array(argvlist, lastarg); |
| 3780 | Py_XDECREF(vals); |
| 3781 | Py_XDECREF(keys); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3782 | fail_0: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3783 | PyMem_Free(path); |
| 3784 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3785 | } |
| 3786 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3787 | #endif /* HAVE_SPAWNV */ |
| 3788 | |
| 3789 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3790 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3791 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3792 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3793 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3794 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3795 | 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] | 3796 | |
| 3797 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3798 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3799 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3800 | pid_t pid; |
| 3801 | int result = 0; |
| 3802 | _PyImport_AcquireLock(); |
| 3803 | pid = fork1(); |
| 3804 | if (pid == 0) { |
| 3805 | /* child: this clobbers and resets the import lock. */ |
| 3806 | PyOS_AfterFork(); |
| 3807 | } else { |
| 3808 | /* parent: release the import lock. */ |
| 3809 | result = _PyImport_ReleaseLock(); |
| 3810 | } |
| 3811 | if (pid == -1) |
| 3812 | return posix_error(); |
| 3813 | if (result < 0) { |
| 3814 | /* Don't clobber the OSError if the fork failed. */ |
| 3815 | PyErr_SetString(PyExc_RuntimeError, |
| 3816 | "not holding the import lock"); |
| 3817 | return NULL; |
| 3818 | } |
| 3819 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3820 | } |
| 3821 | #endif |
| 3822 | |
| 3823 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3824 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3825 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3826 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3827 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3828 | 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] | 3829 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3830 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3831 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3832 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3833 | pid_t pid; |
| 3834 | int result = 0; |
| 3835 | _PyImport_AcquireLock(); |
| 3836 | pid = fork(); |
| 3837 | if (pid == 0) { |
| 3838 | /* child: this clobbers and resets the import lock. */ |
| 3839 | PyOS_AfterFork(); |
| 3840 | } else { |
| 3841 | /* parent: release the import lock. */ |
| 3842 | result = _PyImport_ReleaseLock(); |
| 3843 | } |
| 3844 | if (pid == -1) |
| 3845 | return posix_error(); |
| 3846 | if (result < 0) { |
| 3847 | /* Don't clobber the OSError if the fork failed. */ |
| 3848 | PyErr_SetString(PyExc_RuntimeError, |
| 3849 | "not holding the import lock"); |
| 3850 | return NULL; |
| 3851 | } |
| 3852 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3853 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3854 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3855 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3856 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3857 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3858 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3859 | #define DEV_PTY_FILE "/dev/ptc" |
| 3860 | #define HAVE_DEV_PTMX |
| 3861 | #else |
| 3862 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3863 | #endif |
| 3864 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3865 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3866 | #ifdef HAVE_PTY_H |
| 3867 | #include <pty.h> |
| 3868 | #else |
| 3869 | #ifdef HAVE_LIBUTIL_H |
| 3870 | #include <libutil.h> |
Ronald Oussoren | a55af9a | 2010-01-17 16:25:57 +0000 | [diff] [blame] | 3871 | #else |
| 3872 | #ifdef HAVE_UTIL_H |
| 3873 | #include <util.h> |
| 3874 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3875 | #endif /* HAVE_LIBUTIL_H */ |
| 3876 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3877 | #ifdef HAVE_STROPTS_H |
| 3878 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3879 | #endif |
| 3880 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3881 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3882 | #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] | 3883 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3884 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3885 | 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] | 3886 | |
| 3887 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3888 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3889 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3890 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3891 | #ifndef HAVE_OPENPTY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3892 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3893 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3894 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3895 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3896 | #ifdef sun |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3897 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3898 | #endif |
| 3899 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3900 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3901 | #ifdef HAVE_OPENPTY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3902 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3903 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3904 | #elif defined(HAVE__GETPTY) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3905 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3906 | if (slave_name == NULL) |
| 3907 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3908 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3909 | slave_fd = open(slave_name, O_RDWR); |
| 3910 | if (slave_fd < 0) |
| 3911 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3912 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3913 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 3914 | if (master_fd < 0) |
| 3915 | return posix_error(); |
| 3916 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 3917 | /* change permission of slave */ |
| 3918 | if (grantpt(master_fd) < 0) { |
| 3919 | PyOS_setsig(SIGCHLD, sig_saved); |
| 3920 | return posix_error(); |
| 3921 | } |
| 3922 | /* unlock slave */ |
| 3923 | if (unlockpt(master_fd) < 0) { |
| 3924 | PyOS_setsig(SIGCHLD, sig_saved); |
| 3925 | return posix_error(); |
| 3926 | } |
| 3927 | PyOS_setsig(SIGCHLD, sig_saved); |
| 3928 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3929 | if (slave_name == NULL) |
| 3930 | return posix_error(); |
| 3931 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3932 | if (slave_fd < 0) |
| 3933 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3934 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3935 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3936 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3937 | #ifndef __hpux |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3938 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3939 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3940 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3941 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3942 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3943 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3944 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3945 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3946 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3947 | |
| 3948 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3949 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3950 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3951 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3952 | 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] | 3953 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3954 | |
| 3955 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3956 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3957 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3958 | int master_fd = -1, result = 0; |
| 3959 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3960 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3961 | _PyImport_AcquireLock(); |
| 3962 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3963 | if (pid == 0) { |
| 3964 | /* child: this clobbers and resets the import lock. */ |
| 3965 | PyOS_AfterFork(); |
| 3966 | } else { |
| 3967 | /* parent: release the import lock. */ |
| 3968 | result = _PyImport_ReleaseLock(); |
| 3969 | } |
| 3970 | if (pid == -1) |
| 3971 | return posix_error(); |
| 3972 | if (result < 0) { |
| 3973 | /* Don't clobber the OSError if the fork failed. */ |
| 3974 | PyErr_SetString(PyExc_RuntimeError, |
| 3975 | "not holding the import lock"); |
| 3976 | return NULL; |
| 3977 | } |
| 3978 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3979 | } |
| 3980 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3981 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3982 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3983 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3984 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3985 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3986 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3987 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3988 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3989 | { |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 3990 | return _PyInt_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3991 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3992 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3993 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3994 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3995 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3996 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3997 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3998 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3999 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4000 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4001 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4002 | { |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 4003 | return _PyInt_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4004 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4005 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4006 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4007 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4008 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4009 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4010 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4011 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4012 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4013 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4014 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4015 | { |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 4016 | return _PyInt_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4017 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4018 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4019 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4020 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4021 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4022 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4023 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4024 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4025 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4026 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4027 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4028 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4031 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4032 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4033 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4034 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4035 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4036 | |
| 4037 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4038 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4039 | { |
| 4040 | PyObject *result = NULL; |
| 4041 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4042 | #ifdef NGROUPS_MAX |
| 4043 | #define MAX_GROUPS NGROUPS_MAX |
| 4044 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4045 | /* defined to be 16 on Solaris7, so this should be a small number */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4046 | #define MAX_GROUPS 64 |
| 4047 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4048 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 4049 | |
Victor Stinner | 59729ff | 2011-07-05 11:28:19 +0200 | [diff] [blame] | 4050 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 4051 | * This is a helper variable to store the intermediate result when |
| 4052 | * that happens. |
| 4053 | * |
| 4054 | * To keep the code readable the OSX behaviour is unconditional, |
| 4055 | * according to the POSIX spec this should be safe on all unix-y |
| 4056 | * systems. |
| 4057 | */ |
| 4058 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4059 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4060 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4061 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 4062 | if (n < 0) { |
| 4063 | if (errno == EINVAL) { |
| 4064 | n = getgroups(0, NULL); |
| 4065 | if (n == -1) { |
| 4066 | return posix_error(); |
| 4067 | } |
| 4068 | if (n == 0) { |
| 4069 | /* Avoid malloc(0) */ |
| 4070 | alt_grouplist = grouplist; |
| 4071 | } else { |
| 4072 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 4073 | if (alt_grouplist == NULL) { |
| 4074 | errno = EINVAL; |
| 4075 | return posix_error(); |
| 4076 | } |
| 4077 | n = getgroups(n, alt_grouplist); |
| 4078 | if (n == -1) { |
| 4079 | PyMem_Free(alt_grouplist); |
| 4080 | return posix_error(); |
| 4081 | } |
| 4082 | } |
| 4083 | } else { |
| 4084 | return posix_error(); |
| 4085 | } |
| 4086 | } |
| 4087 | result = PyList_New(n); |
| 4088 | if (result != NULL) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4089 | int i; |
| 4090 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 4091 | PyObject *o = _PyInt_FromGid(alt_grouplist[i]); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4092 | if (o == NULL) { |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 4093 | Py_DECREF(result); |
| 4094 | result = NULL; |
| 4095 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4096 | } |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4097 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4098 | } |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | if (alt_grouplist != grouplist) { |
| 4102 | PyMem_Free(alt_grouplist); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4103 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4104 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4105 | return result; |
| 4106 | } |
| 4107 | #endif |
| 4108 | |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 4109 | #ifdef HAVE_INITGROUPS |
| 4110 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 4111 | "initgroups(username, gid) -> None\n\n\ |
| 4112 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 4113 | the groups of which the specified username is a member, plus the specified\n\ |
| 4114 | group id."); |
| 4115 | |
| 4116 | static PyObject * |
| 4117 | posix_initgroups(PyObject *self, PyObject *args) |
| 4118 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4119 | char *username; |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 4120 | #ifdef __APPLE__ |
| 4121 | int gid; |
| 4122 | #else |
| 4123 | gid_t gid; |
| 4124 | #endif |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 4125 | |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 4126 | #ifdef __APPLE__ |
| 4127 | if (!PyArg_ParseTuple(args, "si:initgroups", &username, |
| 4128 | &gid)) |
| 4129 | #else |
| 4130 | if (!PyArg_ParseTuple(args, "sO&:initgroups", &username, |
| 4131 | _Py_Gid_Converter, &gid)) |
| 4132 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4133 | return NULL; |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 4134 | |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 4135 | if (initgroups(username, gid) == -1) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4136 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 4137 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4138 | Py_INCREF(Py_None); |
| 4139 | return Py_None; |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 4140 | } |
| 4141 | #endif |
| 4142 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4143 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4144 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4145 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4146 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4147 | |
| 4148 | static PyObject * |
| 4149 | posix_getpgid(PyObject *self, PyObject *args) |
| 4150 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4151 | pid_t pid, pgid; |
| 4152 | if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid)) |
| 4153 | return NULL; |
| 4154 | pgid = getpgid(pid); |
| 4155 | if (pgid < 0) |
| 4156 | return posix_error(); |
| 4157 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4158 | } |
| 4159 | #endif /* HAVE_GETPGID */ |
| 4160 | |
| 4161 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4162 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4163 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4164 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4165 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4166 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4167 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4168 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4169 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4170 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4171 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4172 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4173 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4174 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4175 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4176 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4177 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4178 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4179 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4180 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4181 | "setpgrp()\n\n\ |
Senthil Kumaran | 0d6908b | 2010-06-17 16:38:34 +0000 | [diff] [blame] | 4182 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4183 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4184 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4185 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4186 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4187 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4188 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4189 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4190 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4191 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4192 | return posix_error(); |
| 4193 | Py_INCREF(Py_None); |
| 4194 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4197 | #endif /* HAVE_SETPGRP */ |
| 4198 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4199 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4200 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4201 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4202 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4203 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4204 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4205 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4206 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4207 | return PyLong_FromPid(getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4208 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4209 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4210 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4211 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4212 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4213 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4214 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4215 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4216 | |
| 4217 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4218 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4219 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4220 | PyObject *result = NULL; |
| 4221 | char *name; |
| 4222 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4223 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4224 | errno = 0; |
| 4225 | name = getlogin(); |
| 4226 | if (name == NULL) { |
| 4227 | if (errno) |
| 4228 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4229 | else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4230 | PyErr_SetString(PyExc_OSError, |
| 4231 | "unable to determine login name"); |
| 4232 | } |
| 4233 | else |
| 4234 | result = PyString_FromString(name); |
| 4235 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4236 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4237 | return result; |
| 4238 | } |
| 4239 | #endif |
| 4240 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4241 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4242 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4243 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4244 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4245 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4246 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4247 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4248 | { |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 4249 | return _PyInt_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4250 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4251 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4252 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4253 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4254 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4255 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4256 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4257 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4258 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4259 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4260 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4261 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4262 | pid_t pid; |
| 4263 | int sig; |
| 4264 | if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig)) |
| 4265 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4266 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4267 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 4268 | APIRET rc; |
| 4269 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4270 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4271 | |
| 4272 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 4273 | APIRET rc; |
| 4274 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4275 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4276 | |
| 4277 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 4278 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4279 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4280 | if (kill(pid, sig) == -1) |
| 4281 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4282 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4283 | Py_INCREF(Py_None); |
| 4284 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4285 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4286 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4287 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4288 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4289 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4290 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4291 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4292 | |
| 4293 | static PyObject * |
| 4294 | posix_killpg(PyObject *self, PyObject *args) |
| 4295 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4296 | int sig; |
| 4297 | pid_t pgid; |
| 4298 | /* XXX some man pages make the `pgid` parameter an int, others |
| 4299 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 4300 | take the same type. Moreover, pid_t is always at least as wide as |
| 4301 | int (else compilation of this module fails), which is safe. */ |
| 4302 | if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig)) |
| 4303 | return NULL; |
| 4304 | if (killpg(pgid, sig) == -1) |
| 4305 | return posix_error(); |
| 4306 | Py_INCREF(Py_None); |
| 4307 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4308 | } |
| 4309 | #endif |
| 4310 | |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 4311 | #ifdef MS_WINDOWS |
| 4312 | PyDoc_STRVAR(win32_kill__doc__, |
| 4313 | "kill(pid, sig)\n\n\ |
| 4314 | Kill a process with a signal."); |
| 4315 | |
| 4316 | static PyObject * |
| 4317 | win32_kill(PyObject *self, PyObject *args) |
| 4318 | { |
Amaury Forgeot d'Arc | 03acec2 | 2010-05-15 21:45:30 +0000 | [diff] [blame] | 4319 | PyObject *result; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4320 | DWORD pid, sig, err; |
| 4321 | HANDLE handle; |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 4322 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4323 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 4324 | return NULL; |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 4325 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4326 | /* Console processes which share a common console can be sent CTRL+C or |
| 4327 | CTRL+BREAK events, provided they handle said events. */ |
| 4328 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 4329 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 4330 | err = GetLastError(); |
| 4331 | return PyErr_SetFromWindowsErr(err); |
| 4332 | } |
| 4333 | else |
| 4334 | Py_RETURN_NONE; |
| 4335 | } |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 4336 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4337 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 4338 | attempt to open and terminate the process. */ |
| 4339 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 4340 | if (handle == NULL) { |
| 4341 | err = GetLastError(); |
| 4342 | return PyErr_SetFromWindowsErr(err); |
| 4343 | } |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 4344 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4345 | if (TerminateProcess(handle, sig) == 0) { |
| 4346 | err = GetLastError(); |
| 4347 | result = PyErr_SetFromWindowsErr(err); |
| 4348 | } else { |
| 4349 | Py_INCREF(Py_None); |
| 4350 | result = Py_None; |
| 4351 | } |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 4352 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4353 | CloseHandle(handle); |
| 4354 | return result; |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 4355 | } |
Brian Curtin | caea7e8 | 2011-06-08 19:29:53 -0500 | [diff] [blame] | 4356 | |
Brian Curtin | 5446f08 | 2011-06-09 10:00:42 -0500 | [diff] [blame] | 4357 | PyDoc_STRVAR(posix__isdir__doc__, |
| 4358 | "Return true if the pathname refers to an existing directory."); |
| 4359 | |
Brian Curtin | caea7e8 | 2011-06-08 19:29:53 -0500 | [diff] [blame] | 4360 | static PyObject * |
| 4361 | posix__isdir(PyObject *self, PyObject *args) |
| 4362 | { |
| 4363 | PyObject *opath; |
| 4364 | char *path; |
| 4365 | PyUnicodeObject *po; |
| 4366 | DWORD attributes; |
| 4367 | |
| 4368 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
| 4369 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 4370 | |
| 4371 | attributes = GetFileAttributesW(wpath); |
| 4372 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 4373 | Py_RETURN_FALSE; |
| 4374 | goto check; |
| 4375 | } |
| 4376 | /* Drop the argument parsing error as narrow strings |
| 4377 | are also valid. */ |
| 4378 | PyErr_Clear(); |
| 4379 | |
| 4380 | if (!PyArg_ParseTuple(args, "et:_isdir", |
| 4381 | Py_FileSystemDefaultEncoding, &path)) |
| 4382 | return NULL; |
| 4383 | |
| 4384 | attributes = GetFileAttributesA(path); |
Serhiy Storchaka | 46f5b35 | 2013-01-28 20:19:50 +0200 | [diff] [blame] | 4385 | PyMem_Free(path); |
Brian Curtin | caea7e8 | 2011-06-08 19:29:53 -0500 | [diff] [blame] | 4386 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 4387 | Py_RETURN_FALSE; |
| 4388 | |
| 4389 | check: |
| 4390 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 4391 | Py_RETURN_TRUE; |
| 4392 | else |
| 4393 | Py_RETURN_FALSE; |
| 4394 | } |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 4395 | #endif /* MS_WINDOWS */ |
| 4396 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4397 | #ifdef HAVE_PLOCK |
| 4398 | |
| 4399 | #ifdef HAVE_SYS_LOCK_H |
| 4400 | #include <sys/lock.h> |
| 4401 | #endif |
| 4402 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4403 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4404 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4405 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4406 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4407 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4408 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4409 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4410 | int op; |
| 4411 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 4412 | return NULL; |
| 4413 | if (plock(op) == -1) |
| 4414 | return posix_error(); |
| 4415 | Py_INCREF(Py_None); |
| 4416 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4417 | } |
| 4418 | #endif |
| 4419 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4420 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4421 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4422 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4423 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4424 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4425 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4426 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4427 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4428 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4429 | async_system(const char *command) |
| 4430 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4431 | char errormsg[256], args[1024]; |
| 4432 | RESULTCODES rcodes; |
| 4433 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4434 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4435 | char *shell = getenv("COMSPEC"); |
| 4436 | if (!shell) |
| 4437 | shell = "cmd"; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4438 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4439 | /* avoid overflowing the argument buffer */ |
| 4440 | if (strlen(shell) + 3 + strlen(command) >= 1024) |
| 4441 | return ERROR_NOT_ENOUGH_MEMORY |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 4442 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4443 | args[0] = '\0'; |
| 4444 | strcat(args, shell); |
| 4445 | strcat(args, "/c "); |
| 4446 | strcat(args, command); |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 4447 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4448 | /* execute asynchronously, inheriting the environment */ |
| 4449 | rc = DosExecPgm(errormsg, |
| 4450 | sizeof(errormsg), |
| 4451 | EXEC_ASYNC, |
| 4452 | args, |
| 4453 | NULL, |
| 4454 | &rcodes, |
| 4455 | shell); |
| 4456 | return rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4457 | } |
| 4458 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4459 | static FILE * |
| 4460 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4461 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4462 | int oldfd, tgtfd; |
| 4463 | HFILE pipeh[2]; |
| 4464 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4465 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4466 | /* mode determines which of stdin or stdout is reconnected to |
| 4467 | * the pipe to the child |
| 4468 | */ |
| 4469 | if (strchr(mode, 'r') != NULL) { |
| 4470 | tgt_fd = 1; /* stdout */ |
| 4471 | } else if (strchr(mode, 'w')) { |
| 4472 | tgt_fd = 0; /* stdin */ |
| 4473 | } else { |
| 4474 | *err = ERROR_INVALID_ACCESS; |
| 4475 | return NULL; |
| 4476 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4477 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4478 | /* setup the pipe */ |
| 4479 | if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) { |
| 4480 | *err = rc; |
| 4481 | return NULL; |
| 4482 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4483 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4484 | /* prevent other threads accessing stdio */ |
| 4485 | DosEnterCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4486 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4487 | /* reconnect stdio and execute child */ |
| 4488 | oldfd = dup(tgtfd); |
| 4489 | close(tgtfd); |
| 4490 | if (dup2(pipeh[tgtfd], tgtfd) == 0) { |
| 4491 | DosClose(pipeh[tgtfd]); |
| 4492 | rc = async_system(command); |
| 4493 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4494 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4495 | /* restore stdio */ |
| 4496 | dup2(oldfd, tgtfd); |
| 4497 | close(oldfd); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4498 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4499 | /* allow other threads access to stdio */ |
| 4500 | DosExitCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4501 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4502 | /* if execution of child was successful return file stream */ |
| 4503 | if (rc == NO_ERROR) |
| 4504 | return fdopen(pipeh[1 - tgtfd], mode); |
| 4505 | else { |
| 4506 | DosClose(pipeh[1 - tgtfd]); |
| 4507 | *err = rc; |
| 4508 | return NULL; |
| 4509 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4510 | } |
| 4511 | |
| 4512 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4513 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4514 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4515 | char *name; |
| 4516 | char *mode = "r"; |
| 4517 | int err, bufsize = -1; |
| 4518 | FILE *fp; |
| 4519 | PyObject *f; |
| 4520 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 4521 | return NULL; |
| 4522 | Py_BEGIN_ALLOW_THREADS |
| 4523 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
| 4524 | Py_END_ALLOW_THREADS |
| 4525 | if (fp == NULL) |
| 4526 | return os2_error(err); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4527 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4528 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 4529 | if (f != NULL) |
| 4530 | PyFile_SetBufSize(f, bufsize); |
| 4531 | return f; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4532 | } |
| 4533 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4534 | #elif defined(PYCC_GCC) |
| 4535 | |
| 4536 | /* standard posix version of popen() support */ |
| 4537 | static PyObject * |
| 4538 | posix_popen(PyObject *self, PyObject *args) |
| 4539 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4540 | char *name; |
| 4541 | char *mode = "r"; |
| 4542 | int bufsize = -1; |
| 4543 | FILE *fp; |
| 4544 | PyObject *f; |
| 4545 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 4546 | return NULL; |
| 4547 | Py_BEGIN_ALLOW_THREADS |
| 4548 | fp = popen(name, mode); |
| 4549 | Py_END_ALLOW_THREADS |
| 4550 | if (fp == NULL) |
| 4551 | return posix_error(); |
| 4552 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 4553 | if (f != NULL) |
| 4554 | PyFile_SetBufSize(f, bufsize); |
| 4555 | return f; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | /* fork() under OS/2 has lots'o'warts |
| 4559 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 4560 | * most of this code is a ripoff of the win32 code, but using the |
| 4561 | * capabilities of EMX's C library routines |
| 4562 | */ |
| 4563 | |
| 4564 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 4565 | #define POPEN_1 1 |
| 4566 | #define POPEN_2 2 |
| 4567 | #define POPEN_3 3 |
| 4568 | #define POPEN_4 4 |
| 4569 | |
| 4570 | static PyObject *_PyPopen(char *, int, int, int); |
| 4571 | static int _PyPclose(FILE *file); |
| 4572 | |
| 4573 | /* |
| 4574 | * Internal dictionary mapping popen* file pointers to process handles, |
| 4575 | * for use when retrieving the process exit code. See _PyPclose() below |
| 4576 | * for more information on this dictionary's use. |
| 4577 | */ |
| 4578 | static PyObject *_PyPopenProcs = NULL; |
| 4579 | |
| 4580 | /* os2emx version of popen2() |
| 4581 | * |
| 4582 | * The result of this function is a pipe (file) connected to the |
| 4583 | * process's stdin, and a pipe connected to the process's stdout. |
| 4584 | */ |
| 4585 | |
| 4586 | static PyObject * |
| 4587 | os2emx_popen2(PyObject *self, PyObject *args) |
| 4588 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4589 | PyObject *f; |
| 4590 | int tm=0; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4591 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4592 | char *cmdstring; |
| 4593 | char *mode = "t"; |
| 4594 | int bufsize = -1; |
| 4595 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 4596 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4597 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4598 | if (*mode == 't') |
| 4599 | tm = O_TEXT; |
| 4600 | else if (*mode != 'b') { |
| 4601 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 4602 | return NULL; |
| 4603 | } else |
| 4604 | tm = O_BINARY; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4605 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4606 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4607 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4608 | return f; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
| 4611 | /* |
| 4612 | * Variation on os2emx.popen2 |
| 4613 | * |
| 4614 | * The result of this function is 3 pipes - the process's stdin, |
| 4615 | * stdout and stderr |
| 4616 | */ |
| 4617 | |
| 4618 | static PyObject * |
| 4619 | os2emx_popen3(PyObject *self, PyObject *args) |
| 4620 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4621 | PyObject *f; |
| 4622 | int tm = 0; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4623 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4624 | char *cmdstring; |
| 4625 | char *mode = "t"; |
| 4626 | int bufsize = -1; |
| 4627 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 4628 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4629 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4630 | if (*mode == 't') |
| 4631 | tm = O_TEXT; |
| 4632 | else if (*mode != 'b') { |
| 4633 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 4634 | return NULL; |
| 4635 | } else |
| 4636 | tm = O_BINARY; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4637 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4638 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4639 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4640 | return f; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4641 | } |
| 4642 | |
| 4643 | /* |
| 4644 | * Variation on os2emx.popen2 |
| 4645 | * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4646 | * The result of this function is 2 pipes - the processes stdin, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4647 | * and stdout+stderr combined as a single pipe. |
| 4648 | */ |
| 4649 | |
| 4650 | static PyObject * |
| 4651 | os2emx_popen4(PyObject *self, PyObject *args) |
| 4652 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4653 | PyObject *f; |
| 4654 | int tm = 0; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4655 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4656 | char *cmdstring; |
| 4657 | char *mode = "t"; |
| 4658 | int bufsize = -1; |
| 4659 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 4660 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4661 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4662 | if (*mode == 't') |
| 4663 | tm = O_TEXT; |
| 4664 | else if (*mode != 'b') { |
| 4665 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 4666 | return NULL; |
| 4667 | } else |
| 4668 | tm = O_BINARY; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4669 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4670 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4671 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4672 | return f; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4673 | } |
| 4674 | |
| 4675 | /* a couple of structures for convenient handling of multiple |
| 4676 | * file handles and pipes |
| 4677 | */ |
| 4678 | struct file_ref |
| 4679 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4680 | int handle; |
| 4681 | int flags; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4682 | }; |
| 4683 | |
| 4684 | struct pipe_ref |
| 4685 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4686 | int rd; |
| 4687 | int wr; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4688 | }; |
| 4689 | |
| 4690 | /* The following code is derived from the win32 code */ |
| 4691 | |
| 4692 | static PyObject * |
| 4693 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 4694 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4695 | struct file_ref stdio[3]; |
| 4696 | struct pipe_ref p_fd[3]; |
| 4697 | FILE *p_s[3]; |
| 4698 | int file_count, i, pipe_err; |
| 4699 | pid_t pipe_pid; |
| 4700 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 4701 | PyObject *f, *p_f[3]; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4702 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4703 | /* file modes for subsequent fdopen's on pipe handles */ |
| 4704 | if (mode == O_TEXT) |
| 4705 | { |
| 4706 | rd_mode = "rt"; |
| 4707 | wr_mode = "wt"; |
| 4708 | } |
| 4709 | else |
| 4710 | { |
| 4711 | rd_mode = "rb"; |
| 4712 | wr_mode = "wb"; |
| 4713 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4714 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4715 | /* prepare shell references */ |
| 4716 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 4717 | if ((shell = getenv("COMSPEC")) == NULL) |
| 4718 | { |
| 4719 | errno = ENOENT; |
| 4720 | return posix_error(); |
| 4721 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4722 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4723 | sh_name = _getname(shell); |
| 4724 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 4725 | opt = "/c"; |
| 4726 | else |
| 4727 | opt = "-c"; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4728 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4729 | /* save current stdio fds + their flags, and set not inheritable */ |
| 4730 | i = pipe_err = 0; |
| 4731 | while (pipe_err >= 0 && i < 3) |
| 4732 | { |
| 4733 | pipe_err = stdio[i].handle = dup(i); |
| 4734 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 4735 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 4736 | i++; |
| 4737 | } |
| 4738 | if (pipe_err < 0) |
| 4739 | { |
| 4740 | /* didn't get them all saved - clean up and bail out */ |
| 4741 | int saved_err = errno; |
| 4742 | while (i-- > 0) |
| 4743 | { |
| 4744 | close(stdio[i].handle); |
| 4745 | } |
| 4746 | errno = saved_err; |
| 4747 | return posix_error(); |
| 4748 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4749 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4750 | /* create pipe ends */ |
| 4751 | file_count = 2; |
| 4752 | if (n == POPEN_3) |
| 4753 | file_count = 3; |
| 4754 | i = pipe_err = 0; |
| 4755 | while ((pipe_err == 0) && (i < file_count)) |
| 4756 | pipe_err = pipe((int *)&p_fd[i++]); |
| 4757 | if (pipe_err < 0) |
| 4758 | { |
| 4759 | /* didn't get them all made - clean up and bail out */ |
| 4760 | while (i-- > 0) |
| 4761 | { |
| 4762 | close(p_fd[i].wr); |
| 4763 | close(p_fd[i].rd); |
| 4764 | } |
| 4765 | errno = EPIPE; |
| 4766 | return posix_error(); |
| 4767 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4768 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4769 | /* change the actual standard IO streams over temporarily, |
| 4770 | * making the retained pipe ends non-inheritable |
| 4771 | */ |
| 4772 | pipe_err = 0; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4773 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4774 | /* - stdin */ |
| 4775 | if (dup2(p_fd[0].rd, 0) == 0) |
| 4776 | { |
| 4777 | close(p_fd[0].rd); |
| 4778 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 4779 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 4780 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 4781 | { |
| 4782 | close(p_fd[0].wr); |
| 4783 | pipe_err = -1; |
| 4784 | } |
| 4785 | } |
| 4786 | else |
| 4787 | { |
| 4788 | pipe_err = -1; |
| 4789 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4790 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4791 | /* - stdout */ |
| 4792 | if (pipe_err == 0) |
| 4793 | { |
| 4794 | if (dup2(p_fd[1].wr, 1) == 1) |
| 4795 | { |
| 4796 | close(p_fd[1].wr); |
| 4797 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 4798 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 4799 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 4800 | { |
| 4801 | close(p_fd[1].rd); |
| 4802 | pipe_err = -1; |
| 4803 | } |
| 4804 | } |
| 4805 | else |
| 4806 | { |
| 4807 | pipe_err = -1; |
| 4808 | } |
| 4809 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4810 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4811 | /* - stderr, as required */ |
| 4812 | if (pipe_err == 0) |
| 4813 | switch (n) |
| 4814 | { |
| 4815 | case POPEN_3: |
| 4816 | { |
| 4817 | if (dup2(p_fd[2].wr, 2) == 2) |
| 4818 | { |
| 4819 | close(p_fd[2].wr); |
| 4820 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 4821 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 4822 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 4823 | { |
| 4824 | close(p_fd[2].rd); |
| 4825 | pipe_err = -1; |
| 4826 | } |
| 4827 | } |
| 4828 | else |
| 4829 | { |
| 4830 | pipe_err = -1; |
| 4831 | } |
| 4832 | break; |
| 4833 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4834 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4835 | case POPEN_4: |
| 4836 | { |
| 4837 | if (dup2(1, 2) != 2) |
| 4838 | { |
| 4839 | pipe_err = -1; |
| 4840 | } |
| 4841 | break; |
| 4842 | } |
| 4843 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4844 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4845 | /* spawn the child process */ |
| 4846 | if (pipe_err == 0) |
| 4847 | { |
| 4848 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 4849 | if (pipe_pid == -1) |
| 4850 | { |
| 4851 | pipe_err = -1; |
| 4852 | } |
| 4853 | else |
| 4854 | { |
| 4855 | /* save the PID into the FILE structure |
| 4856 | * NOTE: this implementation doesn't actually |
| 4857 | * take advantage of this, but do it for |
| 4858 | * completeness - AIM Apr01 |
| 4859 | */ |
| 4860 | for (i = 0; i < file_count; i++) |
| 4861 | p_s[i]->_pid = pipe_pid; |
| 4862 | } |
| 4863 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4864 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4865 | /* reset standard IO to normal */ |
| 4866 | for (i = 0; i < 3; i++) |
| 4867 | { |
| 4868 | dup2(stdio[i].handle, i); |
| 4869 | fcntl(i, F_SETFD, stdio[i].flags); |
| 4870 | close(stdio[i].handle); |
| 4871 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4872 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4873 | /* if any remnant problems, clean up and bail out */ |
| 4874 | if (pipe_err < 0) |
| 4875 | { |
| 4876 | for (i = 0; i < 3; i++) |
| 4877 | { |
| 4878 | close(p_fd[i].rd); |
| 4879 | close(p_fd[i].wr); |
| 4880 | } |
| 4881 | errno = EPIPE; |
| 4882 | return posix_error_with_filename(cmdstring); |
| 4883 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4884 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4885 | /* build tuple of file objects to return */ |
| 4886 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 4887 | PyFile_SetBufSize(p_f[0], bufsize); |
| 4888 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 4889 | PyFile_SetBufSize(p_f[1], bufsize); |
| 4890 | if (n == POPEN_3) |
| 4891 | { |
| 4892 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 4893 | PyFile_SetBufSize(p_f[0], bufsize); |
| 4894 | f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]); |
| 4895 | } |
| 4896 | else |
| 4897 | f = PyTuple_Pack(2, p_f[0], p_f[1]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4898 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4899 | /* |
| 4900 | * Insert the files we've created into the process dictionary |
| 4901 | * all referencing the list with the process handle and the |
| 4902 | * initial number of files (see description below in _PyPclose). |
| 4903 | * Since if _PyPclose later tried to wait on a process when all |
| 4904 | * handles weren't closed, it could create a deadlock with the |
| 4905 | * child, we spend some energy here to try to ensure that we |
| 4906 | * either insert all file handles into the dictionary or none |
| 4907 | * at all. It's a little clumsy with the various popen modes |
| 4908 | * and variable number of files involved. |
| 4909 | */ |
| 4910 | if (!_PyPopenProcs) |
| 4911 | { |
| 4912 | _PyPopenProcs = PyDict_New(); |
| 4913 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4914 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4915 | if (_PyPopenProcs) |
| 4916 | { |
| 4917 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 4918 | int ins_rc[3]; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4919 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4920 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4921 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4922 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4923 | procObj = PyList_New(2); |
| 4924 | pidObj = PyLong_FromPid(pipe_pid); |
| 4925 | intObj = PyInt_FromLong((long) file_count); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4926 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4927 | if (procObj && pidObj && intObj) |
| 4928 | { |
| 4929 | PyList_SetItem(procObj, 0, pidObj); |
| 4930 | PyList_SetItem(procObj, 1, intObj); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4931 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4932 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 4933 | if (fileObj[0]) |
| 4934 | { |
| 4935 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4936 | fileObj[0], |
| 4937 | procObj); |
| 4938 | } |
| 4939 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 4940 | if (fileObj[1]) |
| 4941 | { |
| 4942 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4943 | fileObj[1], |
| 4944 | procObj); |
| 4945 | } |
| 4946 | if (file_count >= 3) |
| 4947 | { |
| 4948 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 4949 | if (fileObj[2]) |
| 4950 | { |
| 4951 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4952 | fileObj[2], |
| 4953 | procObj); |
| 4954 | } |
| 4955 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4956 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4957 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4958 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4959 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 4960 | { |
| 4961 | /* Something failed - remove any dictionary |
| 4962 | * entries that did make it. |
| 4963 | */ |
| 4964 | if (!ins_rc[0] && fileObj[0]) |
| 4965 | { |
| 4966 | PyDict_DelItem(_PyPopenProcs, |
| 4967 | fileObj[0]); |
| 4968 | } |
| 4969 | if (!ins_rc[1] && fileObj[1]) |
| 4970 | { |
| 4971 | PyDict_DelItem(_PyPopenProcs, |
| 4972 | fileObj[1]); |
| 4973 | } |
| 4974 | if (!ins_rc[2] && fileObj[2]) |
| 4975 | { |
| 4976 | PyDict_DelItem(_PyPopenProcs, |
| 4977 | fileObj[2]); |
| 4978 | } |
| 4979 | } |
| 4980 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4981 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4982 | /* |
| 4983 | * Clean up our localized references for the dictionary keys |
| 4984 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4985 | * that got placed in the dictionary. |
| 4986 | */ |
| 4987 | Py_XDECREF(procObj); |
| 4988 | Py_XDECREF(fileObj[0]); |
| 4989 | Py_XDECREF(fileObj[1]); |
| 4990 | Py_XDECREF(fileObj[2]); |
| 4991 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4992 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4993 | /* Child is launched. */ |
| 4994 | return f; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4995 | } |
| 4996 | |
| 4997 | /* |
| 4998 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4999 | * exit code for the child process and return as a result of the close. |
| 5000 | * |
| 5001 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 5002 | * input file pointer to information about the process that was |
| 5003 | * originally created by the popen* call that created the file pointer. |
| 5004 | * The dictionary uses the file pointer as a key (with one entry |
| 5005 | * inserted for each file returned by the original popen* call) and a |
| 5006 | * single list object as the value for all files from a single call. |
| 5007 | * The list object contains the Win32 process handle at [0], and a file |
| 5008 | * count at [1], which is initialized to the total number of file |
| 5009 | * handles using that list. |
| 5010 | * |
| 5011 | * This function closes whichever handle it is passed, and decrements |
| 5012 | * the file count in the dictionary for the process handle pointed to |
| 5013 | * by this file. On the last close (when the file count reaches zero), |
| 5014 | * this function will wait for the child process and then return its |
| 5015 | * exit code as the result of the close() operation. This permits the |
| 5016 | * files to be closed in any order - it is always the close() of the |
| 5017 | * final handle that will return the exit code. |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 5018 | * |
| 5019 | * NOTE: This function is currently called with the GIL released. |
| 5020 | * hence we use the GILState API to manage our state. |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5021 | */ |
| 5022 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5023 | static int _PyPclose(FILE *file) |
| 5024 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5025 | int result; |
| 5026 | int exit_code; |
| 5027 | pid_t pipe_pid; |
| 5028 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 5029 | int file_count; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5030 | #ifdef WITH_THREAD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5031 | PyGILState_STATE state; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5032 | #endif |
| 5033 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5034 | /* Close the file handle first, to ensure it can't block the |
| 5035 | * child from exiting if it's the last handle. |
| 5036 | */ |
| 5037 | result = fclose(file); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5038 | |
| 5039 | #ifdef WITH_THREAD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5040 | state = PyGILState_Ensure(); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5041 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5042 | if (_PyPopenProcs) |
| 5043 | { |
| 5044 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 5045 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 5046 | fileObj)) != NULL && |
| 5047 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 5048 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 5049 | { |
| 5050 | pipe_pid = (pid_t) PyLong_AsPid(pidObj); |
| 5051 | file_count = (int) PyInt_AsLong(intObj); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5052 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5053 | if (file_count > 1) |
| 5054 | { |
| 5055 | /* Still other files referencing process */ |
| 5056 | file_count--; |
| 5057 | PyList_SetItem(procObj,1, |
| 5058 | PyInt_FromLong((long) file_count)); |
| 5059 | } |
| 5060 | else |
| 5061 | { |
| 5062 | /* Last file for this process */ |
| 5063 | if (result != EOF && |
| 5064 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 5065 | { |
| 5066 | /* extract exit status */ |
| 5067 | if (WIFEXITED(exit_code)) |
| 5068 | { |
| 5069 | result = WEXITSTATUS(exit_code); |
| 5070 | } |
| 5071 | else |
| 5072 | { |
| 5073 | errno = EPIPE; |
| 5074 | result = -1; |
| 5075 | } |
| 5076 | } |
| 5077 | else |
| 5078 | { |
| 5079 | /* Indicate failure - this will cause the file object |
| 5080 | * to raise an I/O error and translate the last |
| 5081 | * error code from errno. We do have a problem with |
| 5082 | * last errors that overlap the normal errno table, |
| 5083 | * but that's a consistent problem with the file object. |
| 5084 | */ |
| 5085 | result = -1; |
| 5086 | } |
| 5087 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5088 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5089 | /* Remove this file pointer from dictionary */ |
| 5090 | PyDict_DelItem(_PyPopenProcs, fileObj); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5091 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5092 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 5093 | { |
| 5094 | Py_DECREF(_PyPopenProcs); |
| 5095 | _PyPopenProcs = NULL; |
| 5096 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5097 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5098 | } /* if object retrieval ok */ |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5099 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5100 | Py_XDECREF(fileObj); |
| 5101 | } /* if _PyPopenProcs */ |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5102 | |
| 5103 | #ifdef WITH_THREAD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5104 | PyGILState_Release(state); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5105 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5106 | return result; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5107 | } |
| 5108 | |
| 5109 | #endif /* PYCC_??? */ |
| 5110 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5111 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5112 | |
| 5113 | /* |
| 5114 | * Portable 'popen' replacement for Win32. |
| 5115 | * |
| 5116 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 5117 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5118 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5119 | */ |
| 5120 | |
| 5121 | #include <malloc.h> |
| 5122 | #include <io.h> |
| 5123 | #include <fcntl.h> |
| 5124 | |
| 5125 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 5126 | #define POPEN_1 1 |
| 5127 | #define POPEN_2 2 |
| 5128 | #define POPEN_3 3 |
| 5129 | #define POPEN_4 4 |
| 5130 | |
| 5131 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5132 | static int _PyPclose(FILE *file); |
| 5133 | |
| 5134 | /* |
| 5135 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5136 | * for use when retrieving the process exit code. See _PyPclose() below |
| 5137 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5138 | */ |
| 5139 | static PyObject *_PyPopenProcs = NULL; |
| 5140 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5141 | |
| 5142 | /* popen that works from a GUI. |
| 5143 | * |
| 5144 | * The result of this function is a pipe (file) connected to the |
| 5145 | * processes stdin or stdout, depending on the requested mode. |
| 5146 | */ |
| 5147 | |
| 5148 | static PyObject * |
| 5149 | posix_popen(PyObject *self, PyObject *args) |
| 5150 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5151 | PyObject *f; |
| 5152 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5153 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5154 | char *cmdstring; |
| 5155 | char *mode = "r"; |
| 5156 | int bufsize = -1; |
| 5157 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
| 5158 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5159 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5160 | if (*mode == 'r') |
| 5161 | tm = _O_RDONLY; |
| 5162 | else if (*mode != 'w') { |
| 5163 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
| 5164 | return NULL; |
| 5165 | } else |
| 5166 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5167 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5168 | if (bufsize != -1) { |
| 5169 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
| 5170 | return NULL; |
| 5171 | } |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 5172 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5173 | if (*(mode+1) == 't') |
| 5174 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 5175 | else if (*(mode+1) == 'b') |
| 5176 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
| 5177 | else |
| 5178 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5179 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5180 | return f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5181 | } |
| 5182 | |
| 5183 | /* Variation on win32pipe.popen |
| 5184 | * |
| 5185 | * The result of this function is a pipe (file) connected to the |
| 5186 | * process's stdin, and a pipe connected to the process's stdout. |
| 5187 | */ |
| 5188 | |
| 5189 | static PyObject * |
| 5190 | win32_popen2(PyObject *self, PyObject *args) |
| 5191 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5192 | PyObject *f; |
| 5193 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5194 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5195 | char *cmdstring; |
| 5196 | char *mode = "t"; |
| 5197 | int bufsize = -1; |
| 5198 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 5199 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5200 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5201 | if (*mode == 't') |
| 5202 | tm = _O_TEXT; |
| 5203 | else if (*mode != 'b') { |
| 5204 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
| 5205 | return NULL; |
| 5206 | } else |
| 5207 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5208 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5209 | if (bufsize != -1) { |
| 5210 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
| 5211 | return NULL; |
| 5212 | } |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 5213 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5214 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5215 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5216 | return f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | /* |
| 5220 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 5221 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5222 | * The result of this function is 3 pipes - the process's stdin, |
| 5223 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5224 | */ |
| 5225 | |
| 5226 | static PyObject * |
| 5227 | win32_popen3(PyObject *self, PyObject *args) |
| 5228 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5229 | PyObject *f; |
| 5230 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5231 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5232 | char *cmdstring; |
| 5233 | char *mode = "t"; |
| 5234 | int bufsize = -1; |
| 5235 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 5236 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5237 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5238 | if (*mode == 't') |
| 5239 | tm = _O_TEXT; |
| 5240 | else if (*mode != 'b') { |
| 5241 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
| 5242 | return NULL; |
| 5243 | } else |
| 5244 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5245 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5246 | if (bufsize != -1) { |
| 5247 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
| 5248 | return NULL; |
| 5249 | } |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 5250 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5251 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5252 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5253 | return f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5254 | } |
| 5255 | |
| 5256 | /* |
| 5257 | * Variation on win32pipe.popen |
| 5258 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5259 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5260 | * and stdout+stderr combined as a single pipe. |
| 5261 | */ |
| 5262 | |
| 5263 | static PyObject * |
| 5264 | win32_popen4(PyObject *self, PyObject *args) |
| 5265 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5266 | PyObject *f; |
| 5267 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5268 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5269 | char *cmdstring; |
| 5270 | char *mode = "t"; |
| 5271 | int bufsize = -1; |
| 5272 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 5273 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5274 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5275 | if (*mode == 't') |
| 5276 | tm = _O_TEXT; |
| 5277 | else if (*mode != 'b') { |
| 5278 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
| 5279 | return NULL; |
| 5280 | } else |
| 5281 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 5282 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5283 | if (bufsize != -1) { |
| 5284 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
| 5285 | return NULL; |
| 5286 | } |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 5287 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5288 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 5289 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5290 | return f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5291 | } |
| 5292 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 5293 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5294 | _PyPopenCreateProcess(char *cmdstring, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5295 | HANDLE hStdin, |
| 5296 | HANDLE hStdout, |
| 5297 | HANDLE hStderr, |
| 5298 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5299 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5300 | PROCESS_INFORMATION piProcInfo; |
| 5301 | STARTUPINFO siStartInfo; |
| 5302 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
| 5303 | char *s1,*s2, *s3 = " /c "; |
| 5304 | const char *szConsoleSpawn = "w9xpopen.exe"; |
| 5305 | int i; |
| 5306 | Py_ssize_t x; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5307 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5308 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
| 5309 | char *comshell; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 5310 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5311 | s1 = (char *)alloca(i); |
| 5312 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 5313 | /* x < i, so x fits into an integer */ |
| 5314 | return (int)x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 5315 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5316 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 5317 | * then use the w9xpopen hack. |
| 5318 | */ |
| 5319 | comshell = s1 + x; |
| 5320 | while (comshell >= s1 && *comshell != '\\') |
| 5321 | --comshell; |
| 5322 | ++comshell; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 5323 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5324 | if (GetVersion() < 0x80000000 && |
| 5325 | _stricmp(comshell, "command.com") != 0) { |
| 5326 | /* NT/2000 and not using command.com. */ |
| 5327 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
| 5328 | s2 = (char *)alloca(x); |
| 5329 | ZeroMemory(s2, x); |
| 5330 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
| 5331 | } |
| 5332 | else { |
| 5333 | /* |
| 5334 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 5335 | * the workaround listed in KB: Q150956 |
| 5336 | */ |
| 5337 | char modulepath[_MAX_PATH]; |
| 5338 | struct stat statinfo; |
| 5339 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 5340 | for (x = i = 0; modulepath[i]; i++) |
| 5341 | if (modulepath[i] == SEP) |
| 5342 | x = i+1; |
| 5343 | modulepath[x] = '\0'; |
| 5344 | /* Create the full-name to w9xpopen, so we can test it exists */ |
| 5345 | strncat(modulepath, |
| 5346 | szConsoleSpawn, |
| 5347 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 5348 | -strlen(modulepath)); |
| 5349 | if (stat(modulepath, &statinfo) != 0) { |
| 5350 | size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]); |
| 5351 | /* Eeek - file-not-found - possibly an embedding |
| 5352 | situation - see if we can locate it in sys.prefix |
| 5353 | */ |
| 5354 | strncpy(modulepath, |
| 5355 | Py_GetExecPrefix(), |
| 5356 | mplen); |
| 5357 | modulepath[mplen-1] = '\0'; |
| 5358 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 5359 | strcat(modulepath, "\\"); |
| 5360 | strncat(modulepath, |
| 5361 | szConsoleSpawn, |
| 5362 | mplen-strlen(modulepath)); |
| 5363 | /* No where else to look - raise an easily identifiable |
| 5364 | error, rather than leaving Windows to report |
| 5365 | "file not found" - as the user is probably blissfully |
| 5366 | unaware this shim EXE is used, and it will confuse them. |
| 5367 | (well, it confused me for a while ;-) |
| 5368 | */ |
| 5369 | if (stat(modulepath, &statinfo) != 0) { |
| 5370 | PyErr_Format(PyExc_RuntimeError, |
| 5371 | "Can not locate '%s' which is needed " |
| 5372 | "for popen to work with your shell " |
| 5373 | "or platform.", |
| 5374 | szConsoleSpawn); |
| 5375 | return FALSE; |
| 5376 | } |
| 5377 | } |
| 5378 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
| 5379 | strlen(modulepath) + |
| 5380 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 5381 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5382 | s2 = (char *)alloca(x); |
| 5383 | ZeroMemory(s2, x); |
| 5384 | /* To maintain correct argument passing semantics, |
| 5385 | we pass the command-line as it stands, and allow |
| 5386 | quoting to be applied. w9xpopen.exe will then |
| 5387 | use its argv vector, and re-quote the necessary |
| 5388 | args for the ultimate child process. |
| 5389 | */ |
| 5390 | PyOS_snprintf( |
| 5391 | s2, x, |
| 5392 | "\"%s\" %s%s%s", |
| 5393 | modulepath, |
| 5394 | s1, |
| 5395 | s3, |
| 5396 | cmdstring); |
| 5397 | /* Not passing CREATE_NEW_CONSOLE has been known to |
| 5398 | cause random failures on win9x. Specifically a |
| 5399 | dialog: |
| 5400 | "Your program accessed mem currently in use at xxx" |
| 5401 | and a hopeful warning about the stability of your |
| 5402 | system. |
| 5403 | Cost is Ctrl+C won't kill children, but anyone |
| 5404 | who cares can have a go! |
| 5405 | */ |
| 5406 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
| 5407 | } |
| 5408 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5409 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5410 | /* Could be an else here to try cmd.exe / command.com in the path |
| 5411 | Now we'll just error out.. */ |
| 5412 | else { |
| 5413 | PyErr_SetString(PyExc_RuntimeError, |
| 5414 | "Cannot locate a COMSPEC environment variable to " |
| 5415 | "use as the shell"); |
| 5416 | return FALSE; |
| 5417 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5418 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5419 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 5420 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 5421 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 5422 | siStartInfo.hStdInput = hStdin; |
| 5423 | siStartInfo.hStdOutput = hStdout; |
| 5424 | siStartInfo.hStdError = hStderr; |
| 5425 | siStartInfo.wShowWindow = SW_HIDE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5426 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5427 | if (CreateProcess(NULL, |
| 5428 | s2, |
| 5429 | NULL, |
| 5430 | NULL, |
| 5431 | TRUE, |
| 5432 | dwProcessFlags, |
| 5433 | NULL, |
| 5434 | NULL, |
| 5435 | &siStartInfo, |
| 5436 | &piProcInfo) ) { |
| 5437 | /* Close the handles now so anyone waiting is woken. */ |
| 5438 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5439 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5440 | /* Return process handle */ |
| 5441 | *hProcess = piProcInfo.hProcess; |
| 5442 | return TRUE; |
| 5443 | } |
| 5444 | win32_error("CreateProcess", s2); |
| 5445 | return FALSE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | /* The following code is based off of KB: Q190351 */ |
| 5449 | |
| 5450 | static PyObject * |
| 5451 | _PyPopen(char *cmdstring, int mode, int n) |
| 5452 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5453 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 5454 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
| 5455 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5456 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5457 | SECURITY_ATTRIBUTES saAttr; |
| 5458 | BOOL fSuccess; |
| 5459 | int fd1, fd2, fd3; |
| 5460 | FILE *f1, *f2, *f3; |
| 5461 | long file_count; |
| 5462 | PyObject *f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5463 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5464 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 5465 | saAttr.bInheritHandle = TRUE; |
| 5466 | saAttr.lpSecurityDescriptor = NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5467 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5468 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 5469 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5470 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5471 | /* Create new output read handle and the input write handle. Set |
| 5472 | * the inheritance properties to FALSE. Otherwise, the child inherits |
| 5473 | * these handles; resulting in non-closeable handles to the pipes |
| 5474 | * being created. */ |
| 5475 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
| 5476 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 5477 | FALSE, |
| 5478 | DUPLICATE_SAME_ACCESS); |
| 5479 | if (!fSuccess) |
| 5480 | return win32_error("DuplicateHandle", NULL); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5481 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5482 | /* Close the inheritable version of ChildStdin |
| 5483 | that we're using. */ |
| 5484 | CloseHandle(hChildStdinWr); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5485 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5486 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 5487 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5488 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5489 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
| 5490 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 5491 | FALSE, DUPLICATE_SAME_ACCESS); |
| 5492 | if (!fSuccess) |
| 5493 | return win32_error("DuplicateHandle", NULL); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5494 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5495 | /* Close the inheritable version of ChildStdout |
| 5496 | that we're using. */ |
| 5497 | CloseHandle(hChildStdoutRd); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5498 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5499 | if (n != POPEN_4) { |
| 5500 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 5501 | return win32_error("CreatePipe", NULL); |
| 5502 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 5503 | hChildStderrRd, |
| 5504 | GetCurrentProcess(), |
| 5505 | &hChildStderrRdDup, 0, |
| 5506 | FALSE, DUPLICATE_SAME_ACCESS); |
| 5507 | if (!fSuccess) |
| 5508 | return win32_error("DuplicateHandle", NULL); |
| 5509 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 5510 | CloseHandle(hChildStderrRd); |
| 5511 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5512 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5513 | switch (n) { |
| 5514 | case POPEN_1: |
| 5515 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 5516 | case _O_WRONLY | _O_TEXT: |
| 5517 | /* Case for writing to child Stdin in text mode. */ |
| 5518 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
| 5519 | f1 = _fdopen(fd1, "w"); |
| 5520 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
| 5521 | PyFile_SetBufSize(f, 0); |
| 5522 | /* We don't care about these pipes anymore, so close them. */ |
| 5523 | CloseHandle(hChildStdoutRdDup); |
| 5524 | CloseHandle(hChildStderrRdDup); |
| 5525 | break; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5526 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5527 | case _O_RDONLY | _O_TEXT: |
| 5528 | /* Case for reading from child Stdout in text mode. */ |
| 5529 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
| 5530 | f1 = _fdopen(fd1, "r"); |
| 5531 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
| 5532 | PyFile_SetBufSize(f, 0); |
| 5533 | /* We don't care about these pipes anymore, so close them. */ |
| 5534 | CloseHandle(hChildStdinWrDup); |
| 5535 | CloseHandle(hChildStderrRdDup); |
| 5536 | break; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5537 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5538 | case _O_RDONLY | _O_BINARY: |
| 5539 | /* Case for readinig from child Stdout in binary mode. */ |
| 5540 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
| 5541 | f1 = _fdopen(fd1, "rb"); |
| 5542 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
| 5543 | PyFile_SetBufSize(f, 0); |
| 5544 | /* We don't care about these pipes anymore, so close them. */ |
| 5545 | CloseHandle(hChildStdinWrDup); |
| 5546 | CloseHandle(hChildStderrRdDup); |
| 5547 | break; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5548 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5549 | case _O_WRONLY | _O_BINARY: |
| 5550 | /* Case for writing to child Stdin in binary mode. */ |
| 5551 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
| 5552 | f1 = _fdopen(fd1, "wb"); |
| 5553 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
| 5554 | PyFile_SetBufSize(f, 0); |
| 5555 | /* We don't care about these pipes anymore, so close them. */ |
| 5556 | CloseHandle(hChildStdoutRdDup); |
| 5557 | CloseHandle(hChildStderrRdDup); |
| 5558 | break; |
| 5559 | } |
| 5560 | file_count = 1; |
| 5561 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5562 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5563 | case POPEN_2: |
| 5564 | case POPEN_4: |
| 5565 | { |
| 5566 | char *m1, *m2; |
| 5567 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5568 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5569 | if (mode & _O_TEXT) { |
| 5570 | m1 = "r"; |
| 5571 | m2 = "w"; |
| 5572 | } else { |
| 5573 | m1 = "rb"; |
| 5574 | m2 = "wb"; |
| 5575 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5576 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5577 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
| 5578 | f1 = _fdopen(fd1, m2); |
| 5579 | fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
| 5580 | f2 = _fdopen(fd2, m1); |
| 5581 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
| 5582 | PyFile_SetBufSize(p1, 0); |
| 5583 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 5584 | PyFile_SetBufSize(p2, 0); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5585 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5586 | if (n != 4) |
| 5587 | CloseHandle(hChildStderrRdDup); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5588 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5589 | f = PyTuple_Pack(2,p1,p2); |
| 5590 | Py_XDECREF(p1); |
| 5591 | Py_XDECREF(p2); |
| 5592 | file_count = 2; |
| 5593 | break; |
| 5594 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5595 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5596 | case POPEN_3: |
| 5597 | { |
| 5598 | char *m1, *m2; |
| 5599 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5600 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5601 | if (mode & _O_TEXT) { |
| 5602 | m1 = "r"; |
| 5603 | m2 = "w"; |
| 5604 | } else { |
| 5605 | m1 = "rb"; |
| 5606 | m2 = "wb"; |
| 5607 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5608 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5609 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
| 5610 | f1 = _fdopen(fd1, m2); |
| 5611 | fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
| 5612 | f2 = _fdopen(fd2, m1); |
| 5613 | fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode); |
| 5614 | f3 = _fdopen(fd3, m1); |
| 5615 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
| 5616 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 5617 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
| 5618 | PyFile_SetBufSize(p1, 0); |
| 5619 | PyFile_SetBufSize(p2, 0); |
| 5620 | PyFile_SetBufSize(p3, 0); |
| 5621 | f = PyTuple_Pack(3,p1,p2,p3); |
| 5622 | Py_XDECREF(p1); |
| 5623 | Py_XDECREF(p2); |
| 5624 | Py_XDECREF(p3); |
| 5625 | file_count = 3; |
| 5626 | break; |
| 5627 | } |
| 5628 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5629 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5630 | if (n == POPEN_4) { |
| 5631 | if (!_PyPopenCreateProcess(cmdstring, |
| 5632 | hChildStdinRd, |
| 5633 | hChildStdoutWr, |
| 5634 | hChildStdoutWr, |
| 5635 | &hProcess)) |
| 5636 | return NULL; |
| 5637 | } |
| 5638 | else { |
| 5639 | if (!_PyPopenCreateProcess(cmdstring, |
| 5640 | hChildStdinRd, |
| 5641 | hChildStdoutWr, |
| 5642 | hChildStderrWr, |
| 5643 | &hProcess)) |
| 5644 | return NULL; |
| 5645 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5646 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5647 | /* |
| 5648 | * Insert the files we've created into the process dictionary |
| 5649 | * all referencing the list with the process handle and the |
| 5650 | * initial number of files (see description below in _PyPclose). |
| 5651 | * Since if _PyPclose later tried to wait on a process when all |
| 5652 | * handles weren't closed, it could create a deadlock with the |
| 5653 | * child, we spend some energy here to try to ensure that we |
| 5654 | * either insert all file handles into the dictionary or none |
| 5655 | * at all. It's a little clumsy with the various popen modes |
| 5656 | * and variable number of files involved. |
| 5657 | */ |
| 5658 | if (!_PyPopenProcs) { |
| 5659 | _PyPopenProcs = PyDict_New(); |
| 5660 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5661 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5662 | if (_PyPopenProcs) { |
| 5663 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 5664 | int ins_rc[3]; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5665 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5666 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 5667 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5668 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5669 | procObj = PyList_New(2); |
| 5670 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 5671 | intObj = PyInt_FromLong(file_count); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5672 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5673 | if (procObj && hProcessObj && intObj) { |
| 5674 | PyList_SetItem(procObj,0,hProcessObj); |
| 5675 | PyList_SetItem(procObj,1,intObj); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5676 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5677 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 5678 | if (fileObj[0]) { |
| 5679 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 5680 | fileObj[0], |
| 5681 | procObj); |
| 5682 | } |
| 5683 | if (file_count >= 2) { |
| 5684 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 5685 | if (fileObj[1]) { |
| 5686 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 5687 | fileObj[1], |
| 5688 | procObj); |
| 5689 | } |
| 5690 | } |
| 5691 | if (file_count >= 3) { |
| 5692 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 5693 | if (fileObj[2]) { |
| 5694 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 5695 | fileObj[2], |
| 5696 | procObj); |
| 5697 | } |
| 5698 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5699 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5700 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 5701 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 5702 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 5703 | /* Something failed - remove any dictionary |
| 5704 | * entries that did make it. |
| 5705 | */ |
| 5706 | if (!ins_rc[0] && fileObj[0]) { |
| 5707 | PyDict_DelItem(_PyPopenProcs, |
| 5708 | fileObj[0]); |
| 5709 | } |
| 5710 | if (!ins_rc[1] && fileObj[1]) { |
| 5711 | PyDict_DelItem(_PyPopenProcs, |
| 5712 | fileObj[1]); |
| 5713 | } |
| 5714 | if (!ins_rc[2] && fileObj[2]) { |
| 5715 | PyDict_DelItem(_PyPopenProcs, |
| 5716 | fileObj[2]); |
| 5717 | } |
| 5718 | } |
| 5719 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5720 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5721 | /* |
| 5722 | * Clean up our localized references for the dictionary keys |
| 5723 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 5724 | * that got placed in the dictionary. |
| 5725 | */ |
| 5726 | Py_XDECREF(procObj); |
| 5727 | Py_XDECREF(fileObj[0]); |
| 5728 | Py_XDECREF(fileObj[1]); |
| 5729 | Py_XDECREF(fileObj[2]); |
| 5730 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5731 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5732 | /* Child is launched. Close the parents copy of those pipe |
| 5733 | * handles that only the child should have open. You need to |
| 5734 | * make sure that no handles to the write end of the output pipe |
| 5735 | * are maintained in this process or else the pipe will not close |
| 5736 | * when the child process exits and the ReadFile will hang. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5737 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5738 | if (!CloseHandle(hChildStdinRd)) |
| 5739 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5740 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5741 | if (!CloseHandle(hChildStdoutWr)) |
| 5742 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5743 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5744 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 5745 | return win32_error("CloseHandle", NULL); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5746 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5747 | return f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5748 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5749 | |
| 5750 | /* |
| 5751 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 5752 | * exit code for the child process and return as a result of the close. |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5753 | * |
| 5754 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 5755 | * input file pointer to information about the process that was |
| 5756 | * originally created by the popen* call that created the file pointer. |
| 5757 | * The dictionary uses the file pointer as a key (with one entry |
| 5758 | * inserted for each file returned by the original popen* call) and a |
| 5759 | * single list object as the value for all files from a single call. |
| 5760 | * The list object contains the Win32 process handle at [0], and a file |
| 5761 | * count at [1], which is initialized to the total number of file |
| 5762 | * handles using that list. |
| 5763 | * |
| 5764 | * This function closes whichever handle it is passed, and decrements |
| 5765 | * the file count in the dictionary for the process handle pointed to |
| 5766 | * by this file. On the last close (when the file count reaches zero), |
| 5767 | * this function will wait for the child process and then return its |
| 5768 | * exit code as the result of the close() operation. This permits the |
| 5769 | * files to be closed in any order - it is always the close() of the |
| 5770 | * final handle that will return the exit code. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 5771 | * |
| 5772 | * NOTE: This function is currently called with the GIL released. |
| 5773 | * hence we use the GILState API to manage our state. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5774 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5775 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5776 | static int _PyPclose(FILE *file) |
| 5777 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5778 | int result; |
| 5779 | DWORD exit_code; |
| 5780 | HANDLE hProcess; |
| 5781 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 5782 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5783 | #ifdef WITH_THREAD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5784 | PyGILState_STATE state; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5785 | #endif |
| 5786 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5787 | /* Close the file handle first, to ensure it can't block the |
| 5788 | * child from exiting if it's the last handle. |
| 5789 | */ |
| 5790 | result = fclose(file); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5791 | #ifdef WITH_THREAD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5792 | state = PyGILState_Ensure(); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5793 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5794 | if (_PyPopenProcs) { |
| 5795 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 5796 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 5797 | fileObj)) != NULL && |
| 5798 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 5799 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5800 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5801 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 5802 | file_count = PyInt_AsLong(intObj); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5803 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5804 | if (file_count > 1) { |
| 5805 | /* Still other files referencing process */ |
| 5806 | file_count--; |
| 5807 | PyList_SetItem(procObj,1, |
| 5808 | PyInt_FromLong(file_count)); |
| 5809 | } else { |
| 5810 | /* Last file for this process */ |
| 5811 | if (result != EOF && |
| 5812 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 5813 | GetExitCodeProcess(hProcess, &exit_code)) { |
| 5814 | /* Possible truncation here in 16-bit environments, but |
| 5815 | * real exit codes are just the lower byte in any event. |
| 5816 | */ |
| 5817 | result = exit_code; |
| 5818 | } else { |
| 5819 | /* Indicate failure - this will cause the file object |
| 5820 | * to raise an I/O error and translate the last Win32 |
| 5821 | * error code from errno. We do have a problem with |
| 5822 | * last errors that overlap the normal errno table, |
| 5823 | * but that's a consistent problem with the file object. |
| 5824 | */ |
| 5825 | if (result != EOF) { |
| 5826 | /* If the error wasn't from the fclose(), then |
| 5827 | * set errno for the file object error handling. |
| 5828 | */ |
| 5829 | errno = GetLastError(); |
| 5830 | } |
| 5831 | result = -1; |
| 5832 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5833 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5834 | /* Free up the native handle at this point */ |
| 5835 | CloseHandle(hProcess); |
| 5836 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5837 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5838 | /* Remove this file pointer from dictionary */ |
| 5839 | PyDict_DelItem(_PyPopenProcs, fileObj); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5840 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5841 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 5842 | Py_DECREF(_PyPopenProcs); |
| 5843 | _PyPopenProcs = NULL; |
| 5844 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5845 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5846 | } /* if object retrieval ok */ |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5847 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5848 | Py_XDECREF(fileObj); |
| 5849 | } /* if _PyPopenProcs */ |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5850 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5851 | #ifdef WITH_THREAD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5852 | PyGILState_Release(state); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5853 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5854 | return result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5855 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 5856 | |
| 5857 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5858 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5859 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5860 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5861 | char *name; |
| 5862 | char *mode = "r"; |
| 5863 | int bufsize = -1; |
| 5864 | FILE *fp; |
| 5865 | PyObject *f; |
| 5866 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 5867 | return NULL; |
| 5868 | /* Strip mode of binary or text modifiers */ |
| 5869 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) |
| 5870 | mode = "r"; |
| 5871 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) |
| 5872 | mode = "w"; |
| 5873 | Py_BEGIN_ALLOW_THREADS |
| 5874 | fp = popen(name, mode); |
| 5875 | Py_END_ALLOW_THREADS |
| 5876 | if (fp == NULL) |
| 5877 | return posix_error(); |
| 5878 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 5879 | if (f != NULL) |
| 5880 | PyFile_SetBufSize(f, bufsize); |
| 5881 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5882 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5883 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5884 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5885 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5886 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5887 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5888 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5889 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5890 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5891 | Set the current process's user id."); |
| 5892 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5893 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5894 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5895 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5896 | uid_t uid; |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 5897 | if (!PyArg_ParseTuple(args, "O&:setuid", _Py_Uid_Converter, &uid)) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5898 | return NULL; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5899 | if (setuid(uid) < 0) |
| 5900 | return posix_error(); |
| 5901 | Py_INCREF(Py_None); |
| 5902 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5903 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5904 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5905 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5906 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5907 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5908 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5909 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5910 | Set the current process's effective user id."); |
| 5911 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5912 | static PyObject * |
| 5913 | posix_seteuid (PyObject *self, PyObject *args) |
| 5914 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5915 | uid_t euid; |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 5916 | if (!PyArg_ParseTuple(args, "O&:seteuid", _Py_Uid_Converter, &euid)) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5917 | return NULL; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5918 | if (seteuid(euid) < 0) { |
| 5919 | return posix_error(); |
| 5920 | } else { |
| 5921 | Py_INCREF(Py_None); |
| 5922 | return Py_None; |
| 5923 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5924 | } |
| 5925 | #endif /* HAVE_SETEUID */ |
| 5926 | |
| 5927 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5928 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5929 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5930 | Set the current process's effective group id."); |
| 5931 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5932 | static PyObject * |
| 5933 | posix_setegid (PyObject *self, PyObject *args) |
| 5934 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5935 | gid_t egid; |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 5936 | if (!PyArg_ParseTuple(args, "O&:setegid", _Py_Gid_Converter, &egid)) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5937 | return NULL; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5938 | if (setegid(egid) < 0) { |
| 5939 | return posix_error(); |
| 5940 | } else { |
| 5941 | Py_INCREF(Py_None); |
| 5942 | return Py_None; |
| 5943 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5944 | } |
| 5945 | #endif /* HAVE_SETEGID */ |
| 5946 | |
| 5947 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5948 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5949 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5950 | Set the current process's real and effective user ids."); |
| 5951 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5952 | static PyObject * |
| 5953 | posix_setreuid (PyObject *self, PyObject *args) |
| 5954 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5955 | uid_t ruid, euid; |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 5956 | if (!PyArg_ParseTuple(args, "O&O&:setreuid", |
| 5957 | _Py_Uid_Converter, &ruid, |
| 5958 | _Py_Uid_Converter, &euid)) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5959 | return NULL; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5960 | if (setreuid(ruid, euid) < 0) { |
| 5961 | return posix_error(); |
| 5962 | } else { |
| 5963 | Py_INCREF(Py_None); |
| 5964 | return Py_None; |
| 5965 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5966 | } |
| 5967 | #endif /* HAVE_SETREUID */ |
| 5968 | |
| 5969 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5970 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5971 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5972 | Set the current process's real and effective group ids."); |
| 5973 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5974 | static PyObject * |
| 5975 | posix_setregid (PyObject *self, PyObject *args) |
| 5976 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5977 | gid_t rgid, egid; |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 5978 | if (!PyArg_ParseTuple(args, "O&O&:setregid", |
| 5979 | _Py_Gid_Converter, &rgid, |
| 5980 | _Py_Gid_Converter, &egid)) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5981 | return NULL; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5982 | if (setregid(rgid, egid) < 0) { |
| 5983 | return posix_error(); |
| 5984 | } else { |
| 5985 | Py_INCREF(Py_None); |
| 5986 | return Py_None; |
| 5987 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5988 | } |
| 5989 | #endif /* HAVE_SETREGID */ |
| 5990 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5991 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5992 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5993 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5994 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5995 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5996 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5997 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5998 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5999 | gid_t gid; |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 6000 | if (!PyArg_ParseTuple(args, "O&:setgid", _Py_Gid_Converter, &gid)) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6001 | return NULL; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6002 | if (setgid(gid) < 0) |
| 6003 | return posix_error(); |
| 6004 | Py_INCREF(Py_None); |
| 6005 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6006 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6007 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6008 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6009 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6010 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6011 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6012 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6013 | |
| 6014 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 6015 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6016 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6017 | int i, len; |
| 6018 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6019 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6020 | if (!PySequence_Check(groups)) { |
| 6021 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6022 | return NULL; |
| 6023 | } |
| 6024 | len = PySequence_Size(groups); |
| 6025 | if (len > MAX_GROUPS) { |
| 6026 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6027 | return NULL; |
| 6028 | } |
| 6029 | for(i = 0; i < len; i++) { |
| 6030 | PyObject *elem; |
| 6031 | elem = PySequence_GetItem(groups, i); |
| 6032 | if (!elem) |
| 6033 | return NULL; |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 6034 | if (!PyInt_Check(elem) && !PyLong_Check(elem)) { |
| 6035 | PyErr_SetString(PyExc_TypeError, |
| 6036 | "groups must be integers"); |
| 6037 | Py_DECREF(elem); |
| 6038 | return NULL; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6039 | } else { |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 6040 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6041 | Py_DECREF(elem); |
| 6042 | return NULL; |
| 6043 | } |
| 6044 | } |
| 6045 | Py_DECREF(elem); |
| 6046 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6047 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6048 | if (setgroups(len, grouplist) < 0) |
| 6049 | return posix_error(); |
| 6050 | Py_INCREF(Py_None); |
| 6051 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6052 | } |
| 6053 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6054 | |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 6055 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6056 | static PyObject * |
Christian Heimes | d491d71 | 2008-02-01 18:49:26 +0000 | [diff] [blame] | 6057 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6058 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6059 | PyObject *result; |
| 6060 | static PyObject *struct_rusage; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6061 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6062 | if (pid == -1) |
| 6063 | return posix_error(); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6064 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6065 | if (struct_rusage == NULL) { |
| 6066 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6067 | if (m == NULL) |
| 6068 | return NULL; |
| 6069 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 6070 | Py_DECREF(m); |
| 6071 | if (struct_rusage == NULL) |
| 6072 | return NULL; |
| 6073 | } |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6074 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6075 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6076 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6077 | if (!result) |
| 6078 | return NULL; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6079 | |
| 6080 | #ifndef doubletime |
| 6081 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6082 | #endif |
| 6083 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6084 | PyStructSequence_SET_ITEM(result, 0, |
| 6085 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 6086 | PyStructSequence_SET_ITEM(result, 1, |
| 6087 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6088 | #define SET_INT(result, index, value)\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6089 | PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value)) |
| 6090 | SET_INT(result, 2, ru->ru_maxrss); |
| 6091 | SET_INT(result, 3, ru->ru_ixrss); |
| 6092 | SET_INT(result, 4, ru->ru_idrss); |
| 6093 | SET_INT(result, 5, ru->ru_isrss); |
| 6094 | SET_INT(result, 6, ru->ru_minflt); |
| 6095 | SET_INT(result, 7, ru->ru_majflt); |
| 6096 | SET_INT(result, 8, ru->ru_nswap); |
| 6097 | SET_INT(result, 9, ru->ru_inblock); |
| 6098 | SET_INT(result, 10, ru->ru_oublock); |
| 6099 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6100 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6101 | SET_INT(result, 13, ru->ru_nsignals); |
| 6102 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6103 | SET_INT(result, 15, ru->ru_nivcsw); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6104 | #undef SET_INT |
| 6105 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6106 | if (PyErr_Occurred()) { |
| 6107 | Py_DECREF(result); |
| 6108 | return NULL; |
| 6109 | } |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6110 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6111 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6112 | } |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 6113 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6114 | |
| 6115 | #ifdef HAVE_WAIT3 |
| 6116 | PyDoc_STRVAR(posix_wait3__doc__, |
| 6117 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 6118 | Wait for completion of a child process."); |
| 6119 | |
| 6120 | static PyObject * |
| 6121 | posix_wait3(PyObject *self, PyObject *args) |
| 6122 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6123 | pid_t pid; |
| 6124 | int options; |
| 6125 | struct rusage ru; |
| 6126 | WAIT_TYPE status; |
| 6127 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6128 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6129 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 6130 | return NULL; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6131 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6132 | Py_BEGIN_ALLOW_THREADS |
| 6133 | pid = wait3(&status, options, &ru); |
| 6134 | Py_END_ALLOW_THREADS |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6135 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6136 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6137 | } |
| 6138 | #endif /* HAVE_WAIT3 */ |
| 6139 | |
| 6140 | #ifdef HAVE_WAIT4 |
| 6141 | PyDoc_STRVAR(posix_wait4__doc__, |
| 6142 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 6143 | Wait for completion of a given child process."); |
| 6144 | |
| 6145 | static PyObject * |
| 6146 | posix_wait4(PyObject *self, PyObject *args) |
| 6147 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6148 | pid_t pid; |
| 6149 | int options; |
| 6150 | struct rusage ru; |
| 6151 | WAIT_TYPE status; |
| 6152 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6153 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6154 | if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options)) |
| 6155 | return NULL; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6156 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6157 | Py_BEGIN_ALLOW_THREADS |
| 6158 | pid = wait4(pid, &status, options, &ru); |
| 6159 | Py_END_ALLOW_THREADS |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6160 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6161 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6162 | } |
| 6163 | #endif /* HAVE_WAIT4 */ |
| 6164 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6165 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6166 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6167 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6168 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6169 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6170 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6171 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6172 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6173 | pid_t pid; |
| 6174 | int options; |
| 6175 | WAIT_TYPE status; |
| 6176 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6177 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6178 | if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) |
| 6179 | return NULL; |
| 6180 | Py_BEGIN_ALLOW_THREADS |
| 6181 | pid = waitpid(pid, &status, options); |
| 6182 | Py_END_ALLOW_THREADS |
| 6183 | if (pid == -1) |
| 6184 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6185 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6186 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6187 | } |
| 6188 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6189 | #elif defined(HAVE_CWAIT) |
| 6190 | |
| 6191 | /* 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] | 6192 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6193 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6194 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6195 | |
| 6196 | static PyObject * |
| 6197 | posix_waitpid(PyObject *self, PyObject *args) |
| 6198 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6199 | Py_intptr_t pid; |
| 6200 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6201 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6202 | if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) |
| 6203 | return NULL; |
| 6204 | Py_BEGIN_ALLOW_THREADS |
| 6205 | pid = _cwait(&status, pid, options); |
| 6206 | Py_END_ALLOW_THREADS |
| 6207 | if (pid == -1) |
| 6208 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6209 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6210 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6211 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6212 | } |
| 6213 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6214 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6215 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6216 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6217 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6218 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6219 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6220 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6221 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6222 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6223 | pid_t pid; |
| 6224 | WAIT_TYPE status; |
| 6225 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6226 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6227 | Py_BEGIN_ALLOW_THREADS |
| 6228 | pid = wait(&status); |
| 6229 | Py_END_ALLOW_THREADS |
| 6230 | if (pid == -1) |
| 6231 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6232 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6233 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6234 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6235 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6236 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6237 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6238 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6239 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6240 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6241 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6242 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6243 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6244 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6245 | #ifdef HAVE_LSTAT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6246 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6247 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6248 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6249 | return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6250 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6251 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6252 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6253 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6254 | } |
| 6255 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6256 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6257 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6258 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6259 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6260 | 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] | 6261 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6262 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6263 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6264 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6265 | PyObject* v; |
| 6266 | char buf[MAXPATHLEN]; |
| 6267 | char *path; |
| 6268 | int n; |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6269 | #ifdef Py_USING_UNICODE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6270 | int arg_is_unicode = 0; |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6271 | #endif |
| 6272 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6273 | if (!PyArg_ParseTuple(args, "et:readlink", |
| 6274 | Py_FileSystemDefaultEncoding, &path)) |
| 6275 | return NULL; |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6276 | #ifdef Py_USING_UNICODE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6277 | v = PySequence_GetItem(args, 0); |
| 6278 | if (v == NULL) { |
| 6279 | PyMem_Free(path); |
| 6280 | return NULL; |
| 6281 | } |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6282 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6283 | if (PyUnicode_Check(v)) { |
| 6284 | arg_is_unicode = 1; |
| 6285 | } |
| 6286 | Py_DECREF(v); |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6287 | #endif |
| 6288 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6289 | Py_BEGIN_ALLOW_THREADS |
| 6290 | n = readlink(path, buf, (int) sizeof buf); |
| 6291 | Py_END_ALLOW_THREADS |
| 6292 | if (n < 0) |
| 6293 | return posix_error_with_allocated_filename(path); |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6294 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6295 | PyMem_Free(path); |
| 6296 | v = PyString_FromStringAndSize(buf, n); |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6297 | #ifdef Py_USING_UNICODE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6298 | if (arg_is_unicode) { |
| 6299 | PyObject *w; |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6300 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6301 | w = PyUnicode_FromEncodedObject(v, |
| 6302 | Py_FileSystemDefaultEncoding, |
| 6303 | "strict"); |
| 6304 | if (w != NULL) { |
| 6305 | Py_DECREF(v); |
| 6306 | v = w; |
| 6307 | } |
| 6308 | else { |
| 6309 | /* fall back to the original byte string, as |
| 6310 | discussed in patch #683592 */ |
| 6311 | PyErr_Clear(); |
| 6312 | } |
| 6313 | } |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6314 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6315 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6316 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6317 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6318 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6319 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6320 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6321 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6322 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 6323 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6324 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6325 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6326 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6327 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6328 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6329 | } |
| 6330 | #endif /* HAVE_SYMLINK */ |
| 6331 | |
| 6332 | |
| 6333 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6334 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 6335 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6336 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6337 | { |
| 6338 | ULONG value = 0; |
| 6339 | |
| 6340 | Py_BEGIN_ALLOW_THREADS |
| 6341 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 6342 | Py_END_ALLOW_THREADS |
| 6343 | |
| 6344 | return value; |
| 6345 | } |
| 6346 | |
| 6347 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6348 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6349 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6350 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6351 | return Py_BuildValue("ddddd", |
| 6352 | (double)0 /* t.tms_utime / HZ */, |
| 6353 | (double)0 /* t.tms_stime / HZ */, |
| 6354 | (double)0 /* t.tms_cutime / HZ */, |
| 6355 | (double)0 /* t.tms_cstime / HZ */, |
| 6356 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6357 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6358 | #else /* not OS2 */ |
Martin v. Löwis | 03824e4 | 2008-12-29 18:17:34 +0000 | [diff] [blame] | 6359 | #define NEED_TICKS_PER_SECOND |
| 6360 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6361 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6362 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6363 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6364 | struct tms t; |
| 6365 | clock_t c; |
| 6366 | errno = 0; |
| 6367 | c = times(&t); |
| 6368 | if (c == (clock_t) -1) |
| 6369 | return posix_error(); |
| 6370 | return Py_BuildValue("ddddd", |
| 6371 | (double)t.tms_utime / ticks_per_second, |
| 6372 | (double)t.tms_stime / ticks_per_second, |
| 6373 | (double)t.tms_cutime / ticks_per_second, |
| 6374 | (double)t.tms_cstime / ticks_per_second, |
| 6375 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6376 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6377 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6378 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6379 | |
| 6380 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6381 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6382 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6383 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6384 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6385 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6386 | FILETIME create, exit, kernel, user; |
| 6387 | HANDLE hProc; |
| 6388 | hProc = GetCurrentProcess(); |
| 6389 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 6390 | /* The fields of a FILETIME structure are the hi and lo part |
| 6391 | of a 64-bit value expressed in 100 nanosecond units. |
| 6392 | 1e7 is one second in such units; 1e-7 the inverse. |
| 6393 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 6394 | */ |
| 6395 | return Py_BuildValue( |
| 6396 | "ddddd", |
| 6397 | (double)(user.dwHighDateTime*429.4967296 + |
| 6398 | user.dwLowDateTime*1e-7), |
| 6399 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 6400 | kernel.dwLowDateTime*1e-7), |
| 6401 | (double)0, |
| 6402 | (double)0, |
| 6403 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6404 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6405 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6406 | |
| 6407 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6408 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6409 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6410 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6411 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6412 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6413 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6414 | #ifdef HAVE_GETSID |
| 6415 | PyDoc_STRVAR(posix_getsid__doc__, |
| 6416 | "getsid(pid) -> sid\n\n\ |
| 6417 | Call the system call getsid()."); |
| 6418 | |
| 6419 | static PyObject * |
| 6420 | posix_getsid(PyObject *self, PyObject *args) |
| 6421 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6422 | pid_t pid; |
| 6423 | int sid; |
| 6424 | if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid)) |
| 6425 | return NULL; |
| 6426 | sid = getsid(pid); |
| 6427 | if (sid < 0) |
| 6428 | return posix_error(); |
| 6429 | return PyInt_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6430 | } |
| 6431 | #endif /* HAVE_GETSID */ |
| 6432 | |
| 6433 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6434 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6435 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6436 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6437 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6438 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6439 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6440 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6441 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6442 | if (setsid() < 0) |
| 6443 | return posix_error(); |
| 6444 | Py_INCREF(Py_None); |
| 6445 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6446 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6447 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6448 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6449 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6450 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6451 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6452 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6453 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6454 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6455 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6456 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6457 | pid_t pid; |
| 6458 | int pgrp; |
| 6459 | if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 6460 | return NULL; |
| 6461 | if (setpgid(pid, pgrp) < 0) |
| 6462 | return posix_error(); |
| 6463 | Py_INCREF(Py_None); |
| 6464 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6465 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6466 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6467 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6468 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6469 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6470 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6471 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6472 | 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] | 6473 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6474 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6475 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6476 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6477 | int fd; |
| 6478 | pid_t pgid; |
| 6479 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 6480 | return NULL; |
| 6481 | pgid = tcgetpgrp(fd); |
| 6482 | if (pgid < 0) |
| 6483 | return posix_error(); |
| 6484 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6485 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6486 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6487 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6488 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6489 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6490 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6491 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6492 | 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] | 6493 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6494 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6495 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6496 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6497 | int fd; |
| 6498 | pid_t pgid; |
| 6499 | if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 6500 | return NULL; |
| 6501 | if (tcsetpgrp(fd, pgid) < 0) |
| 6502 | return posix_error(); |
| 6503 | Py_INCREF(Py_None); |
| 6504 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6505 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6506 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6507 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6508 | /* Functions acting on file descriptors */ |
| 6509 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6510 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6511 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6512 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6513 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6514 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6515 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6516 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6517 | char *file = NULL; |
| 6518 | int flag; |
| 6519 | int mode = 0777; |
| 6520 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6521 | |
| 6522 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6523 | PyUnicodeObject *po; |
| 6524 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 6525 | Py_BEGIN_ALLOW_THREADS |
| 6526 | /* PyUnicode_AS_UNICODE OK without thread |
| 6527 | lock as it is a simple dereference. */ |
| 6528 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 6529 | Py_END_ALLOW_THREADS |
| 6530 | if (fd < 0) |
| 6531 | return posix_error(); |
| 6532 | return PyInt_FromLong((long)fd); |
| 6533 | } |
| 6534 | /* Drop the argument parsing error as narrow strings |
| 6535 | are also valid. */ |
| 6536 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6537 | #endif |
| 6538 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6539 | if (!PyArg_ParseTuple(args, "eti|i", |
| 6540 | Py_FileSystemDefaultEncoding, &file, |
| 6541 | &flag, &mode)) |
| 6542 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 6543 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6544 | Py_BEGIN_ALLOW_THREADS |
| 6545 | fd = open(file, flag, mode); |
| 6546 | Py_END_ALLOW_THREADS |
| 6547 | if (fd < 0) |
| 6548 | return posix_error_with_allocated_filename(file); |
| 6549 | PyMem_Free(file); |
| 6550 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6551 | } |
| 6552 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6553 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6554 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6555 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6556 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6557 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6558 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6559 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6560 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6561 | int fd, res; |
| 6562 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 6563 | return NULL; |
| 6564 | if (!_PyVerify_fd(fd)) |
| 6565 | return posix_error(); |
| 6566 | Py_BEGIN_ALLOW_THREADS |
| 6567 | res = close(fd); |
| 6568 | Py_END_ALLOW_THREADS |
| 6569 | if (res < 0) |
| 6570 | return posix_error(); |
| 6571 | Py_INCREF(Py_None); |
| 6572 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6573 | } |
| 6574 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6575 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6576 | PyDoc_STRVAR(posix_closerange__doc__, |
Georg Brandl | 309501a | 2008-01-19 20:22:13 +0000 | [diff] [blame] | 6577 | "closerange(fd_low, fd_high)\n\n\ |
| 6578 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 6579 | |
| 6580 | static PyObject * |
| 6581 | posix_closerange(PyObject *self, PyObject *args) |
| 6582 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6583 | int fd_from, fd_to, i; |
| 6584 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 6585 | return NULL; |
| 6586 | Py_BEGIN_ALLOW_THREADS |
| 6587 | for (i = fd_from; i < fd_to; i++) |
| 6588 | if (_PyVerify_fd(i)) |
| 6589 | close(i); |
| 6590 | Py_END_ALLOW_THREADS |
| 6591 | Py_RETURN_NONE; |
Georg Brandl | 309501a | 2008-01-19 20:22:13 +0000 | [diff] [blame] | 6592 | } |
| 6593 | |
| 6594 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6595 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6596 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6597 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6598 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6599 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6600 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6601 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6602 | int fd; |
| 6603 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 6604 | return NULL; |
| 6605 | if (!_PyVerify_fd(fd)) |
| 6606 | return posix_error(); |
| 6607 | Py_BEGIN_ALLOW_THREADS |
| 6608 | fd = dup(fd); |
| 6609 | Py_END_ALLOW_THREADS |
| 6610 | if (fd < 0) |
| 6611 | return posix_error(); |
| 6612 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6613 | } |
| 6614 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6615 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6616 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 6617 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6618 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6619 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6620 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6621 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6622 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6623 | int fd, fd2, res; |
| 6624 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 6625 | return NULL; |
| 6626 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 6627 | return posix_error(); |
| 6628 | Py_BEGIN_ALLOW_THREADS |
| 6629 | res = dup2(fd, fd2); |
| 6630 | Py_END_ALLOW_THREADS |
| 6631 | if (res < 0) |
| 6632 | return posix_error(); |
| 6633 | Py_INCREF(Py_None); |
| 6634 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6635 | } |
| 6636 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6637 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6638 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6639 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6640 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6641 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6642 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6643 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6644 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6645 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6646 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6647 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6648 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6649 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6650 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6651 | PyObject *posobj; |
| 6652 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
| 6653 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6654 | #ifdef SEEK_SET |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6655 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 6656 | switch (how) { |
| 6657 | case 0: how = SEEK_SET; break; |
| 6658 | case 1: how = SEEK_CUR; break; |
| 6659 | case 2: how = SEEK_END; break; |
| 6660 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6661 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6662 | |
| 6663 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6664 | pos = PyInt_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6665 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6666 | pos = PyLong_Check(posobj) ? |
| 6667 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6668 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6669 | if (PyErr_Occurred()) |
| 6670 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6671 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6672 | if (!_PyVerify_fd(fd)) |
| 6673 | return posix_error(); |
| 6674 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6675 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6676 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6677 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6678 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6679 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6680 | Py_END_ALLOW_THREADS |
| 6681 | if (res < 0) |
| 6682 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6683 | |
| 6684 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6685 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6686 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6687 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6688 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6689 | } |
| 6690 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6691 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6692 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6693 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6694 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6695 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6696 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6697 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6698 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6699 | int fd, size, n; |
| 6700 | PyObject *buffer; |
| 6701 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 6702 | return NULL; |
| 6703 | if (size < 0) { |
| 6704 | errno = EINVAL; |
| 6705 | return posix_error(); |
| 6706 | } |
| 6707 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
| 6708 | if (buffer == NULL) |
| 6709 | return NULL; |
Stefan Krah | acaab2a | 2010-11-26 15:06:27 +0000 | [diff] [blame] | 6710 | if (!_PyVerify_fd(fd)) { |
| 6711 | Py_DECREF(buffer); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6712 | return posix_error(); |
Stefan Krah | acaab2a | 2010-11-26 15:06:27 +0000 | [diff] [blame] | 6713 | } |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6714 | Py_BEGIN_ALLOW_THREADS |
| 6715 | n = read(fd, PyString_AsString(buffer), size); |
| 6716 | Py_END_ALLOW_THREADS |
| 6717 | if (n < 0) { |
| 6718 | Py_DECREF(buffer); |
| 6719 | return posix_error(); |
| 6720 | } |
| 6721 | if (n != size) |
| 6722 | _PyString_Resize(&buffer, n); |
| 6723 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6724 | } |
| 6725 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6726 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6727 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6728 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6729 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6730 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6731 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6732 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6733 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6734 | Py_buffer pbuf; |
| 6735 | int fd; |
Victor Stinner | 59729ff | 2011-07-05 11:28:19 +0200 | [diff] [blame] | 6736 | Py_ssize_t size, len; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 6737 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6738 | if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf)) |
| 6739 | return NULL; |
Stefan Krah | acaab2a | 2010-11-26 15:06:27 +0000 | [diff] [blame] | 6740 | if (!_PyVerify_fd(fd)) { |
| 6741 | PyBuffer_Release(&pbuf); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6742 | return posix_error(); |
Stefan Krah | acaab2a | 2010-11-26 15:06:27 +0000 | [diff] [blame] | 6743 | } |
Victor Stinner | 59729ff | 2011-07-05 11:28:19 +0200 | [diff] [blame] | 6744 | len = pbuf.len; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6745 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 59729ff | 2011-07-05 11:28:19 +0200 | [diff] [blame] | 6746 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 6747 | if (len > INT_MAX) |
| 6748 | len = INT_MAX; |
| 6749 | size = write(fd, pbuf.buf, (int)len); |
| 6750 | #else |
| 6751 | size = write(fd, pbuf.buf, len); |
| 6752 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6753 | Py_END_ALLOW_THREADS |
Stefan Krah | acaab2a | 2010-11-26 15:06:27 +0000 | [diff] [blame] | 6754 | PyBuffer_Release(&pbuf); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6755 | if (size < 0) |
| 6756 | return posix_error(); |
| 6757 | return PyInt_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6758 | } |
| 6759 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6760 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6761 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6762 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6763 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6764 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6765 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6766 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6767 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6768 | int fd; |
| 6769 | STRUCT_STAT st; |
| 6770 | int res; |
| 6771 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
| 6772 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 6773 | #ifdef __VMS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6774 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 6775 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 6776 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6777 | if (!_PyVerify_fd(fd)) |
| 6778 | return posix_error(); |
| 6779 | Py_BEGIN_ALLOW_THREADS |
| 6780 | res = FSTAT(fd, &st); |
| 6781 | Py_END_ALLOW_THREADS |
| 6782 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6783 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6784 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6785 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6786 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6787 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6788 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6789 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6790 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6791 | } |
| 6792 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6793 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6794 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6795 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6796 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6797 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6798 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6799 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6800 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6801 | int fd; |
| 6802 | char *orgmode = "r"; |
| 6803 | int bufsize = -1; |
| 6804 | FILE *fp; |
| 6805 | PyObject *f; |
| 6806 | char *mode; |
| 6807 | if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize)) |
| 6808 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 6809 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6810 | /* Sanitize mode. See fileobject.c */ |
| 6811 | mode = PyMem_MALLOC(strlen(orgmode)+3); |
| 6812 | if (!mode) { |
| 6813 | PyErr_NoMemory(); |
| 6814 | return NULL; |
| 6815 | } |
| 6816 | strcpy(mode, orgmode); |
| 6817 | if (_PyFile_SanitizeMode(mode)) { |
| 6818 | PyMem_FREE(mode); |
| 6819 | return NULL; |
| 6820 | } |
| 6821 | if (!_PyVerify_fd(fd)) |
| 6822 | return posix_error(); |
| 6823 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 6824 | #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6825 | if (mode[0] == 'a') { |
| 6826 | /* try to make sure the O_APPEND flag is set */ |
| 6827 | int flags; |
| 6828 | flags = fcntl(fd, F_GETFL); |
| 6829 | if (flags != -1) |
| 6830 | fcntl(fd, F_SETFL, flags | O_APPEND); |
| 6831 | fp = fdopen(fd, mode); |
| 6832 | if (fp == NULL && flags != -1) |
| 6833 | /* restore old mode if fdopen failed */ |
| 6834 | fcntl(fd, F_SETFL, flags); |
| 6835 | } else { |
| 6836 | fp = fdopen(fd, mode); |
| 6837 | } |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 6838 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6839 | fp = fdopen(fd, mode); |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 6840 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6841 | Py_END_ALLOW_THREADS |
| 6842 | PyMem_FREE(mode); |
| 6843 | if (fp == NULL) |
| 6844 | return posix_error(); |
Nadeem Vawda | d7664de | 2012-01-19 00:40:46 +0200 | [diff] [blame] | 6845 | /* The dummy filename used here must be kept in sync with the value |
| 6846 | tested against in gzip.GzipFile.__init__() - see issue #13781. */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6847 | f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose); |
| 6848 | if (f != NULL) |
| 6849 | PyFile_SetBufSize(f, bufsize); |
| 6850 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6851 | } |
| 6852 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6853 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6854 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6855 | 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] | 6856 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6857 | |
| 6858 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 6859 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6860 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6861 | int fd; |
| 6862 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 6863 | return NULL; |
| 6864 | if (!_PyVerify_fd(fd)) |
| 6865 | return PyBool_FromLong(0); |
| 6866 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6867 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6868 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6869 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6870 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6871 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6872 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6873 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6874 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6875 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6876 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6877 | #if defined(PYOS_OS2) |
| 6878 | HFILE read, write; |
| 6879 | APIRET rc; |
| 6880 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6881 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6882 | rc = DosCreatePipe( &read, &write, 4096); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6883 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6884 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6885 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6886 | |
| 6887 | return Py_BuildValue("(ii)", read, write); |
| 6888 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6889 | #if !defined(MS_WINDOWS) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6890 | int fds[2]; |
| 6891 | int res; |
| 6892 | Py_BEGIN_ALLOW_THREADS |
| 6893 | res = pipe(fds); |
| 6894 | Py_END_ALLOW_THREADS |
| 6895 | if (res != 0) |
| 6896 | return posix_error(); |
| 6897 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6898 | #else /* MS_WINDOWS */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6899 | HANDLE read, write; |
| 6900 | int read_fd, write_fd; |
| 6901 | BOOL ok; |
| 6902 | Py_BEGIN_ALLOW_THREADS |
| 6903 | ok = CreatePipe(&read, &write, NULL, 0); |
| 6904 | Py_END_ALLOW_THREADS |
| 6905 | if (!ok) |
| 6906 | return win32_error("CreatePipe", NULL); |
| 6907 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 6908 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 6909 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6910 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6911 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6912 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6913 | #endif /* HAVE_PIPE */ |
| 6914 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6915 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6916 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6917 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6918 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6919 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6920 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6921 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6922 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6923 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6924 | char *filename; |
| 6925 | int mode = 0666; |
| 6926 | int res; |
| 6927 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
| 6928 | return NULL; |
| 6929 | Py_BEGIN_ALLOW_THREADS |
| 6930 | res = mkfifo(filename, mode); |
| 6931 | Py_END_ALLOW_THREADS |
| 6932 | if (res < 0) |
| 6933 | return posix_error(); |
| 6934 | Py_INCREF(Py_None); |
| 6935 | return Py_None; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6936 | } |
| 6937 | #endif |
| 6938 | |
| 6939 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6940 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6941 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6942 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6943 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 6944 | named filename. mode specifies both the permissions to use and the\n\ |
| 6945 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 6946 | 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] | 6947 | device defines the newly created device special file (probably using\n\ |
| 6948 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6949 | |
| 6950 | |
| 6951 | static PyObject * |
| 6952 | posix_mknod(PyObject *self, PyObject *args) |
| 6953 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6954 | char *filename; |
| 6955 | int mode = 0600; |
| 6956 | int device = 0; |
| 6957 | int res; |
| 6958 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
| 6959 | return NULL; |
| 6960 | Py_BEGIN_ALLOW_THREADS |
| 6961 | res = mknod(filename, mode, device); |
| 6962 | Py_END_ALLOW_THREADS |
| 6963 | if (res < 0) |
| 6964 | return posix_error(); |
| 6965 | Py_INCREF(Py_None); |
| 6966 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6967 | } |
| 6968 | #endif |
| 6969 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6970 | #ifdef HAVE_DEVICE_MACROS |
| 6971 | PyDoc_STRVAR(posix_major__doc__, |
| 6972 | "major(device) -> major number\n\ |
| 6973 | Extracts a device major number from a raw device number."); |
| 6974 | |
| 6975 | static PyObject * |
| 6976 | posix_major(PyObject *self, PyObject *args) |
| 6977 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6978 | int device; |
| 6979 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 6980 | return NULL; |
| 6981 | return PyInt_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6982 | } |
| 6983 | |
| 6984 | PyDoc_STRVAR(posix_minor__doc__, |
| 6985 | "minor(device) -> minor number\n\ |
| 6986 | Extracts a device minor number from a raw device number."); |
| 6987 | |
| 6988 | static PyObject * |
| 6989 | posix_minor(PyObject *self, PyObject *args) |
| 6990 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6991 | int device; |
| 6992 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 6993 | return NULL; |
| 6994 | return PyInt_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6995 | } |
| 6996 | |
| 6997 | PyDoc_STRVAR(posix_makedev__doc__, |
| 6998 | "makedev(major, minor) -> device number\n\ |
| 6999 | Composes a raw device number from the major and minor device numbers."); |
| 7000 | |
| 7001 | static PyObject * |
| 7002 | posix_makedev(PyObject *self, PyObject *args) |
| 7003 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7004 | int major, minor; |
| 7005 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 7006 | return NULL; |
| 7007 | return PyInt_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7008 | } |
| 7009 | #endif /* device macros */ |
| 7010 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7011 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7012 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7013 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7014 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7015 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7016 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7017 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7018 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7019 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7020 | int fd; |
| 7021 | off_t length; |
| 7022 | int res; |
| 7023 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7024 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7025 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
| 7026 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7027 | |
| 7028 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7029 | length = PyInt_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7030 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7031 | length = PyLong_Check(lenobj) ? |
| 7032 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7033 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7034 | if (PyErr_Occurred()) |
| 7035 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7036 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7037 | Py_BEGIN_ALLOW_THREADS |
| 7038 | res = ftruncate(fd, length); |
| 7039 | Py_END_ALLOW_THREADS |
| 7040 | if (res < 0) |
| 7041 | return posix_error(); |
| 7042 | Py_INCREF(Py_None); |
| 7043 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7044 | } |
| 7045 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7046 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7047 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7048 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7049 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7050 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7051 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7052 | /* Save putenv() parameters as values here, so we can collect them when they |
| 7053 | * get re-set with another call for the same key. */ |
| 7054 | static PyObject *posix_putenv_garbage; |
| 7055 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7056 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7057 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7058 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7059 | char *s1, *s2; |
| 7060 | char *newenv; |
| 7061 | PyObject *newstr; |
| 7062 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7063 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7064 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
| 7065 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7066 | |
| 7067 | #if defined(PYOS_OS2) |
| 7068 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 7069 | APIRET rc; |
| 7070 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7071 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 7072 | if (rc != NO_ERROR) |
| 7073 | return os2_error(rc); |
| 7074 | |
| 7075 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 7076 | APIRET rc; |
| 7077 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7078 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 7079 | if (rc != NO_ERROR) |
| 7080 | return os2_error(rc); |
| 7081 | } else { |
| 7082 | #endif |
| 7083 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7084 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 7085 | len = strlen(s1) + strlen(s2) + 2; |
Victor Stinner | 53853c3 | 2011-11-22 22:20:13 +0100 | [diff] [blame] | 7086 | #ifdef MS_WINDOWS |
| 7087 | if (_MAX_ENV < (len - 1)) { |
| 7088 | PyErr_Format(PyExc_ValueError, |
| 7089 | "the environment variable is longer than %u bytes", |
| 7090 | _MAX_ENV); |
| 7091 | return NULL; |
| 7092 | } |
| 7093 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7094 | /* len includes space for a trailing \0; the size arg to |
| 7095 | PyString_FromStringAndSize does not count that */ |
| 7096 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
| 7097 | if (newstr == NULL) |
| 7098 | return PyErr_NoMemory(); |
| 7099 | newenv = PyString_AS_STRING(newstr); |
| 7100 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 7101 | if (putenv(newenv)) { |
| 7102 | Py_DECREF(newstr); |
| 7103 | posix_error(); |
| 7104 | return NULL; |
| 7105 | } |
| 7106 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 7107 | * this will cause previous value to be collected. This has to |
| 7108 | * happen after the real putenv() call because the old value |
| 7109 | * was still accessible until then. */ |
| 7110 | if (PyDict_SetItem(posix_putenv_garbage, |
| 7111 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 7112 | /* really not much we can do; just leak */ |
| 7113 | PyErr_Clear(); |
| 7114 | } |
| 7115 | else { |
| 7116 | Py_DECREF(newstr); |
| 7117 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7118 | |
| 7119 | #if defined(PYOS_OS2) |
| 7120 | } |
| 7121 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7122 | Py_INCREF(Py_None); |
| 7123 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7124 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7125 | #endif /* putenv */ |
| 7126 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7127 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7128 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7129 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7130 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7131 | |
| 7132 | static PyObject * |
| 7133 | posix_unsetenv(PyObject *self, PyObject *args) |
| 7134 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7135 | char *s1; |
Charles-François Natali | 93a1175 | 2011-11-27 13:01:35 +0100 | [diff] [blame] | 7136 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 53853c3 | 2011-11-22 22:20:13 +0100 | [diff] [blame] | 7137 | int err; |
Charles-François Natali | 93a1175 | 2011-11-27 13:01:35 +0100 | [diff] [blame] | 7138 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7139 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7140 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 7141 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7142 | |
Charles-François Natali | 93a1175 | 2011-11-27 13:01:35 +0100 | [diff] [blame] | 7143 | #ifdef HAVE_BROKEN_UNSETENV |
| 7144 | unsetenv(s1); |
| 7145 | #else |
Victor Stinner | 53853c3 | 2011-11-22 22:20:13 +0100 | [diff] [blame] | 7146 | err = unsetenv(s1); |
Benjamin Peterson | 42d96dc | 2011-11-22 23:56:06 -0600 | [diff] [blame] | 7147 | if (err) |
Victor Stinner | 53853c3 | 2011-11-22 22:20:13 +0100 | [diff] [blame] | 7148 | return posix_error(); |
Charles-François Natali | 93a1175 | 2011-11-27 13:01:35 +0100 | [diff] [blame] | 7149 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7150 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7151 | /* Remove the key from posix_putenv_garbage; |
| 7152 | * this will cause it to be collected. This has to |
| 7153 | * happen after the real unsetenv() call because the |
| 7154 | * old value was still accessible until then. |
| 7155 | */ |
| 7156 | if (PyDict_DelItem(posix_putenv_garbage, |
| 7157 | PyTuple_GET_ITEM(args, 0))) { |
| 7158 | /* really not much we can do; just leak */ |
| 7159 | PyErr_Clear(); |
| 7160 | } |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7161 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7162 | Py_INCREF(Py_None); |
| 7163 | return Py_None; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7164 | } |
| 7165 | #endif /* unsetenv */ |
| 7166 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7167 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7168 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7169 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7170 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 7171 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7172 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7173 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7174 | int code; |
| 7175 | char *message; |
| 7176 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 7177 | return NULL; |
| 7178 | message = strerror(code); |
| 7179 | if (message == NULL) { |
| 7180 | PyErr_SetString(PyExc_ValueError, |
| 7181 | "strerror() argument out of range"); |
| 7182 | return NULL; |
| 7183 | } |
| 7184 | return PyString_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7185 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7186 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7187 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7188 | #ifdef HAVE_SYS_WAIT_H |
| 7189 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7190 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7191 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7192 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7193 | 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] | 7194 | |
| 7195 | static PyObject * |
| 7196 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 7197 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7198 | WAIT_TYPE status; |
| 7199 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7200 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7201 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 7202 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7203 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7204 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7205 | } |
| 7206 | #endif /* WCOREDUMP */ |
| 7207 | |
| 7208 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7209 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7210 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7211 | 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] | 7212 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7213 | |
| 7214 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7215 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7216 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7217 | WAIT_TYPE status; |
| 7218 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7219 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7220 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 7221 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7222 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7223 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7224 | } |
| 7225 | #endif /* WIFCONTINUED */ |
| 7226 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7227 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7228 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7229 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7230 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7231 | |
| 7232 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7233 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7234 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7235 | WAIT_TYPE status; |
| 7236 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7237 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7238 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 7239 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7240 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7241 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7242 | } |
| 7243 | #endif /* WIFSTOPPED */ |
| 7244 | |
| 7245 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7246 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7247 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7248 | 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] | 7249 | |
| 7250 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7251 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7252 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7253 | WAIT_TYPE status; |
| 7254 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7255 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7256 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 7257 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7258 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7259 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7260 | } |
| 7261 | #endif /* WIFSIGNALED */ |
| 7262 | |
| 7263 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7264 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7265 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7266 | 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] | 7267 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7268 | |
| 7269 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7270 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7271 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7272 | WAIT_TYPE status; |
| 7273 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7274 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7275 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 7276 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7277 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7278 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7279 | } |
| 7280 | #endif /* WIFEXITED */ |
| 7281 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7282 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7283 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7284 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7285 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7286 | |
| 7287 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7288 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7289 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7290 | WAIT_TYPE status; |
| 7291 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7292 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7293 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 7294 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7295 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7296 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7297 | } |
| 7298 | #endif /* WEXITSTATUS */ |
| 7299 | |
| 7300 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7301 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7302 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7303 | 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] | 7304 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7305 | |
| 7306 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7307 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7308 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7309 | WAIT_TYPE status; |
| 7310 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7311 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7312 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 7313 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7314 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7315 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7316 | } |
| 7317 | #endif /* WTERMSIG */ |
| 7318 | |
| 7319 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7320 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7321 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7322 | Return the signal that stopped the process that provided\n\ |
| 7323 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7324 | |
| 7325 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7326 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7327 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7328 | WAIT_TYPE status; |
| 7329 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7330 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7331 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 7332 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7333 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7334 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7335 | } |
| 7336 | #endif /* WSTOPSIG */ |
| 7337 | |
| 7338 | #endif /* HAVE_SYS_WAIT_H */ |
| 7339 | |
| 7340 | |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 7341 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 7342 | #ifdef _SCO_DS |
| 7343 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 7344 | needed definitions in sys/statvfs.h */ |
| 7345 | #define _SVID3 |
| 7346 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7347 | #include <sys/statvfs.h> |
| 7348 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7349 | static PyObject* |
| 7350 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7351 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 7352 | if (v == NULL) |
| 7353 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7354 | |
| 7355 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7356 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 7357 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 7358 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 7359 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 7360 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 7361 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 7362 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 7363 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 7364 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 7365 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7366 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7367 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 7368 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 7369 | PyStructSequence_SET_ITEM(v, 2, |
| 7370 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 7371 | PyStructSequence_SET_ITEM(v, 3, |
| 7372 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 7373 | PyStructSequence_SET_ITEM(v, 4, |
| 7374 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 7375 | PyStructSequence_SET_ITEM(v, 5, |
| 7376 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 7377 | PyStructSequence_SET_ITEM(v, 6, |
| 7378 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 7379 | PyStructSequence_SET_ITEM(v, 7, |
| 7380 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 7381 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 7382 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7383 | #endif |
| 7384 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7385 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7386 | } |
| 7387 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7388 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7389 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7390 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7391 | |
| 7392 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7393 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7394 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7395 | int fd, res; |
| 7396 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7397 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7398 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 7399 | return NULL; |
| 7400 | Py_BEGIN_ALLOW_THREADS |
| 7401 | res = fstatvfs(fd, &st); |
| 7402 | Py_END_ALLOW_THREADS |
| 7403 | if (res != 0) |
| 7404 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7405 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7406 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7407 | } |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 7408 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7409 | |
| 7410 | |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 7411 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7412 | #include <sys/statvfs.h> |
| 7413 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7414 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7415 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7416 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7417 | |
| 7418 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7419 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7420 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7421 | char *path; |
| 7422 | int res; |
| 7423 | struct statvfs st; |
| 7424 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
| 7425 | return NULL; |
| 7426 | Py_BEGIN_ALLOW_THREADS |
| 7427 | res = statvfs(path, &st); |
| 7428 | Py_END_ALLOW_THREADS |
| 7429 | if (res != 0) |
| 7430 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7431 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7432 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7433 | } |
| 7434 | #endif /* HAVE_STATVFS */ |
| 7435 | |
| 7436 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7437 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7438 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7439 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7440 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 7441 | The directory and a prefix may be specified as strings; they may be omitted\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7442 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7443 | |
| 7444 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7445 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7446 | { |
| 7447 | PyObject *result = NULL; |
| 7448 | char *dir = NULL; |
| 7449 | char *pfx = NULL; |
| 7450 | char *name; |
| 7451 | |
| 7452 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7453 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 7454 | |
| 7455 | if (PyErr_Warn(PyExc_RuntimeWarning, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7456 | "tempnam is a potential security risk to your program") < 0) |
| 7457 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 7458 | |
Antoine Pitrou | b061461 | 2011-01-02 20:04:52 +0000 | [diff] [blame] | 7459 | if (PyErr_WarnPy3k("tempnam has been removed in 3.x; " |
| 7460 | "use the tempfile module", 1) < 0) |
| 7461 | return NULL; |
| 7462 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7463 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 7464 | name = _tempnam(dir, pfx); |
| 7465 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7466 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 7467 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7468 | if (name == NULL) |
Antoine Pitrou | da4a5f0 | 2011-01-02 20:06:12 +0000 | [diff] [blame] | 7469 | return PyErr_NoMemory(); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 7470 | result = PyString_FromString(name); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7471 | free(name); |
| 7472 | return result; |
| 7473 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 7474 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7475 | |
| 7476 | |
| 7477 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7478 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7479 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7480 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7481 | |
| 7482 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7483 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7484 | { |
| 7485 | FILE *fp; |
| 7486 | |
Antoine Pitrou | b061461 | 2011-01-02 20:04:52 +0000 | [diff] [blame] | 7487 | if (PyErr_WarnPy3k("tmpfile has been removed in 3.x; " |
| 7488 | "use the tempfile module", 1) < 0) |
| 7489 | return NULL; |
| 7490 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7491 | fp = tmpfile(); |
| 7492 | if (fp == NULL) |
Antoine Pitrou | da4a5f0 | 2011-01-02 20:06:12 +0000 | [diff] [blame] | 7493 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 7494 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7495 | } |
| 7496 | #endif |
| 7497 | |
| 7498 | |
| 7499 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7500 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7501 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7502 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7503 | |
| 7504 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7505 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7506 | { |
| 7507 | char buffer[L_tmpnam]; |
| 7508 | char *name; |
| 7509 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 7510 | if (PyErr_Warn(PyExc_RuntimeWarning, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7511 | "tmpnam is a potential security risk to your program") < 0) |
| 7512 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 7513 | |
Antoine Pitrou | b061461 | 2011-01-02 20:04:52 +0000 | [diff] [blame] | 7514 | if (PyErr_WarnPy3k("tmpnam has been removed in 3.x; " |
| 7515 | "use the tempfile module", 1) < 0) |
| 7516 | return NULL; |
| 7517 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 7518 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7519 | name = tmpnam_r(buffer); |
| 7520 | #else |
| 7521 | name = tmpnam(buffer); |
| 7522 | #endif |
| 7523 | if (name == NULL) { |
Antoine Pitrou | da4a5f0 | 2011-01-02 20:06:12 +0000 | [diff] [blame] | 7524 | PyObject *err = Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 7525 | #ifdef USE_TMPNAM_R |
Antoine Pitrou | da4a5f0 | 2011-01-02 20:06:12 +0000 | [diff] [blame] | 7526 | "unexpected NULL from tmpnam_r" |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7527 | #else |
Antoine Pitrou | da4a5f0 | 2011-01-02 20:06:12 +0000 | [diff] [blame] | 7528 | "unexpected NULL from tmpnam" |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7529 | #endif |
Antoine Pitrou | da4a5f0 | 2011-01-02 20:06:12 +0000 | [diff] [blame] | 7530 | ); |
| 7531 | PyErr_SetObject(PyExc_OSError, err); |
| 7532 | Py_XDECREF(err); |
| 7533 | return NULL; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7534 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 7535 | return PyString_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7536 | } |
| 7537 | #endif |
| 7538 | |
| 7539 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7540 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 7541 | * It maps strings representing configuration variable names to |
| 7542 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7543 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7544 | * rarely-used constants. There are three separate tables that use |
| 7545 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7546 | * |
| 7547 | * This code is always included, even if none of the interfaces that |
| 7548 | * need it are included. The #if hackery needed to avoid it would be |
| 7549 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7550 | */ |
| 7551 | struct constdef { |
| 7552 | char *name; |
| 7553 | long value; |
| 7554 | }; |
| 7555 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7556 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7557 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7558 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7559 | { |
| 7560 | if (PyInt_Check(arg)) { |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 7561 | *valuep = PyInt_AS_LONG(arg); |
| 7562 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7563 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 7564 | if (PyString_Check(arg)) { |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 7565 | /* look up the value in the table using a binary search */ |
| 7566 | size_t lo = 0; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7567 | size_t mid; |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 7568 | size_t hi = tablesize; |
| 7569 | int cmp; |
| 7570 | char *confname = PyString_AS_STRING(arg); |
| 7571 | while (lo < hi) { |
| 7572 | mid = (lo + hi) / 2; |
| 7573 | cmp = strcmp(confname, table[mid].name); |
| 7574 | if (cmp < 0) |
| 7575 | hi = mid; |
| 7576 | else if (cmp > 0) |
| 7577 | lo = mid + 1; |
| 7578 | else { |
| 7579 | *valuep = table[mid].value; |
| 7580 | return 1; |
| 7581 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7582 | } |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 7583 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7584 | } |
| 7585 | else |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 7586 | PyErr_SetString(PyExc_TypeError, |
| 7587 | "configuration names must be strings or integers"); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7588 | return 0; |
| 7589 | } |
| 7590 | |
| 7591 | |
| 7592 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 7593 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7594 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7595 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7596 | #endif |
| 7597 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7598 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7599 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7600 | #ifdef _PC_ASYNC_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7601 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7602 | #endif |
| 7603 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7604 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7605 | #endif |
| 7606 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7607 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7608 | #endif |
| 7609 | #ifdef _PC_LAST |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7610 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7611 | #endif |
| 7612 | #ifdef _PC_LINK_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7613 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7614 | #endif |
| 7615 | #ifdef _PC_MAX_CANON |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7616 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7617 | #endif |
| 7618 | #ifdef _PC_MAX_INPUT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7619 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7620 | #endif |
| 7621 | #ifdef _PC_NAME_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7622 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7623 | #endif |
| 7624 | #ifdef _PC_NO_TRUNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7625 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7626 | #endif |
| 7627 | #ifdef _PC_PATH_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7628 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7629 | #endif |
| 7630 | #ifdef _PC_PIPE_BUF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7631 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7632 | #endif |
| 7633 | #ifdef _PC_PRIO_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7634 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7635 | #endif |
| 7636 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7637 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7638 | #endif |
| 7639 | #ifdef _PC_SYNC_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7640 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7641 | #endif |
| 7642 | #ifdef _PC_VDISABLE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7643 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7644 | #endif |
| 7645 | }; |
| 7646 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7647 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7648 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7649 | { |
| 7650 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 7651 | sizeof(posix_constants_pathconf) |
| 7652 | / sizeof(struct constdef)); |
| 7653 | } |
| 7654 | #endif |
| 7655 | |
| 7656 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7657 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7658 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7659 | 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] | 7660 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7661 | |
| 7662 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7663 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7664 | { |
| 7665 | PyObject *result = NULL; |
| 7666 | int name, fd; |
| 7667 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7668 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 7669 | conv_path_confname, &name)) { |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 7670 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7671 | |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 7672 | errno = 0; |
| 7673 | limit = fpathconf(fd, name); |
| 7674 | if (limit == -1 && errno != 0) |
| 7675 | posix_error(); |
| 7676 | else |
| 7677 | result = PyInt_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7678 | } |
| 7679 | return result; |
| 7680 | } |
| 7681 | #endif |
| 7682 | |
| 7683 | |
| 7684 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7685 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7686 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7687 | 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] | 7688 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7689 | |
| 7690 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7691 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7692 | { |
| 7693 | PyObject *result = NULL; |
| 7694 | int name; |
| 7695 | char *path; |
| 7696 | |
| 7697 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 7698 | conv_path_confname, &name)) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7699 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7700 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7701 | errno = 0; |
| 7702 | limit = pathconf(path, name); |
| 7703 | if (limit == -1 && errno != 0) { |
| 7704 | if (errno == EINVAL) |
Stefan Krah | acaab2a | 2010-11-26 15:06:27 +0000 | [diff] [blame] | 7705 | /* could be a path or name problem */ |
| 7706 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7707 | else |
Stefan Krah | acaab2a | 2010-11-26 15:06:27 +0000 | [diff] [blame] | 7708 | posix_error_with_filename(path); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7709 | } |
| 7710 | else |
| 7711 | result = PyInt_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7712 | } |
| 7713 | return result; |
| 7714 | } |
| 7715 | #endif |
| 7716 | |
| 7717 | #ifdef HAVE_CONFSTR |
| 7718 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7719 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7720 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7721 | #endif |
| 7722 | #ifdef _CS_HOSTNAME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7723 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7724 | #endif |
| 7725 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7726 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7727 | #endif |
| 7728 | #ifdef _CS_HW_SERIAL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7729 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7730 | #endif |
| 7731 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7732 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7733 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7734 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7735 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7736 | #endif |
| 7737 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7738 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7739 | #endif |
| 7740 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7741 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7742 | #endif |
| 7743 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7744 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7745 | #endif |
| 7746 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7747 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7748 | #endif |
| 7749 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7750 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7751 | #endif |
| 7752 | #ifdef _CS_LFS_LIBS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7753 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7754 | #endif |
| 7755 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7756 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7757 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7758 | #ifdef _CS_MACHINE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7759 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7760 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7761 | #ifdef _CS_PATH |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7762 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7763 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7764 | #ifdef _CS_RELEASE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7765 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7766 | #endif |
| 7767 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7768 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7769 | #endif |
| 7770 | #ifdef _CS_SYSNAME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7771 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7772 | #endif |
| 7773 | #ifdef _CS_VERSION |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7774 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7775 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7776 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7777 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7778 | #endif |
| 7779 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7780 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7781 | #endif |
| 7782 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7783 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7784 | #endif |
| 7785 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7786 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7787 | #endif |
| 7788 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7789 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7790 | #endif |
| 7791 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7792 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7793 | #endif |
| 7794 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7795 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7796 | #endif |
| 7797 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7798 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7799 | #endif |
| 7800 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7801 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7802 | #endif |
| 7803 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7804 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7805 | #endif |
| 7806 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7807 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7808 | #endif |
| 7809 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7810 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7811 | #endif |
| 7812 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7813 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7814 | #endif |
| 7815 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7816 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7817 | #endif |
| 7818 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7819 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7820 | #endif |
| 7821 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7822 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7823 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7824 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7825 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7826 | #endif |
| 7827 | #ifdef _MIPS_CS_BASE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7828 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7829 | #endif |
| 7830 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7831 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7832 | #endif |
| 7833 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7834 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7835 | #endif |
| 7836 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7837 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7838 | #endif |
| 7839 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7840 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7841 | #endif |
| 7842 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7843 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7844 | #endif |
| 7845 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7846 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7847 | #endif |
| 7848 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7849 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7850 | #endif |
| 7851 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7852 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7853 | #endif |
| 7854 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7855 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7856 | #endif |
| 7857 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7858 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7859 | #endif |
| 7860 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7861 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7862 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7863 | }; |
| 7864 | |
| 7865 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7866 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7867 | { |
| 7868 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 7869 | sizeof(posix_constants_confstr) |
| 7870 | / sizeof(struct constdef)); |
| 7871 | } |
| 7872 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7873 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7874 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7875 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7876 | |
| 7877 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7878 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7879 | { |
| 7880 | PyObject *result = NULL; |
| 7881 | int name; |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 7882 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7883 | |
| 7884 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7885 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7886 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7887 | errno = 0; |
| 7888 | len = confstr(name, buffer, sizeof(buffer)); |
| 7889 | if (len == 0) { |
| 7890 | if (errno) { |
| 7891 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7892 | } |
| 7893 | else { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7894 | result = Py_None; |
| 7895 | Py_INCREF(Py_None); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7896 | } |
| 7897 | } |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7898 | else { |
| 7899 | if ((unsigned int)len >= sizeof(buffer)) { |
| 7900 | result = PyString_FromStringAndSize(NULL, len-1); |
| 7901 | if (result != NULL) |
| 7902 | confstr(name, PyString_AS_STRING(result), len); |
| 7903 | } |
| 7904 | else |
| 7905 | result = PyString_FromStringAndSize(buffer, len-1); |
| 7906 | } |
| 7907 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7908 | return result; |
| 7909 | } |
| 7910 | #endif |
| 7911 | |
| 7912 | |
| 7913 | #ifdef HAVE_SYSCONF |
| 7914 | static struct constdef posix_constants_sysconf[] = { |
| 7915 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7916 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7917 | #endif |
| 7918 | #ifdef _SC_2_C_BIND |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7919 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7920 | #endif |
| 7921 | #ifdef _SC_2_C_DEV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7922 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7923 | #endif |
| 7924 | #ifdef _SC_2_C_VERSION |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7925 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7926 | #endif |
| 7927 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7928 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7929 | #endif |
| 7930 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7931 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7932 | #endif |
| 7933 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7934 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7935 | #endif |
| 7936 | #ifdef _SC_2_SW_DEV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7937 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7938 | #endif |
| 7939 | #ifdef _SC_2_UPE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7940 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7941 | #endif |
| 7942 | #ifdef _SC_2_VERSION |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7943 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7944 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7945 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7946 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7947 | #endif |
| 7948 | #ifdef _SC_ACL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7949 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7950 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7951 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7952 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7953 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7954 | #ifdef _SC_AIO_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7955 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7956 | #endif |
| 7957 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7958 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7959 | #endif |
| 7960 | #ifdef _SC_ARG_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7961 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7962 | #endif |
| 7963 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7964 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7965 | #endif |
| 7966 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7967 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7968 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7969 | #ifdef _SC_AUDIT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7970 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7971 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7972 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7973 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7974 | #endif |
| 7975 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7976 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7977 | #endif |
| 7978 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7979 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7980 | #endif |
| 7981 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7982 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7983 | #endif |
| 7984 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7985 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7986 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7987 | #ifdef _SC_CAP |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7988 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7989 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7990 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7991 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7992 | #endif |
| 7993 | #ifdef _SC_CHAR_BIT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7994 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7995 | #endif |
| 7996 | #ifdef _SC_CHAR_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7997 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7998 | #endif |
| 7999 | #ifdef _SC_CHAR_MIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8000 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8001 | #endif |
| 8002 | #ifdef _SC_CHILD_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8003 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8004 | #endif |
| 8005 | #ifdef _SC_CLK_TCK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8006 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8007 | #endif |
| 8008 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8009 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8010 | #endif |
| 8011 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8012 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8013 | #endif |
| 8014 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8015 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8016 | #endif |
| 8017 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8018 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8019 | #endif |
| 8020 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8021 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8022 | #endif |
| 8023 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8024 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8025 | #endif |
| 8026 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8027 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8028 | #endif |
| 8029 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8030 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8031 | #endif |
| 8032 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8033 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8034 | #endif |
| 8035 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8036 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8037 | #endif |
| 8038 | #ifdef _SC_FSYNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8039 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8040 | #endif |
| 8041 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8042 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8043 | #endif |
| 8044 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8045 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8046 | #endif |
| 8047 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8048 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8049 | #endif |
| 8050 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8051 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8052 | #endif |
| 8053 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8054 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8055 | #endif |
| 8056 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8057 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8058 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8059 | #ifdef _SC_INF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8060 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8061 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8062 | #ifdef _SC_INT_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8063 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8064 | #endif |
| 8065 | #ifdef _SC_INT_MIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8066 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8067 | #endif |
| 8068 | #ifdef _SC_IOV_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8069 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8070 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8071 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8072 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8073 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8074 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8075 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8076 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8077 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8078 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8079 | #endif |
| 8080 | #ifdef _SC_KERN_SIM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8081 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8082 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8083 | #ifdef _SC_LINE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8084 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8085 | #endif |
| 8086 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8087 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8088 | #endif |
| 8089 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8090 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8091 | #endif |
| 8092 | #ifdef _SC_LONG_BIT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8093 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8094 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8095 | #ifdef _SC_MAC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8096 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8097 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8098 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8099 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8100 | #endif |
| 8101 | #ifdef _SC_MAXPID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8102 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8103 | #endif |
| 8104 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8105 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8106 | #endif |
| 8107 | #ifdef _SC_MEMLOCK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8108 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8109 | #endif |
| 8110 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8111 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8112 | #endif |
| 8113 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8114 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8115 | #endif |
| 8116 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8117 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8118 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8119 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8120 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8121 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8122 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8123 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8124 | #endif |
| 8125 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8126 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8127 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8128 | #ifdef _SC_NACLS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8129 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8130 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8131 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8132 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8133 | #endif |
| 8134 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8135 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8136 | #endif |
| 8137 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8138 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8139 | #endif |
| 8140 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8141 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8142 | #endif |
| 8143 | #ifdef _SC_NL_NMAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8144 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8145 | #endif |
| 8146 | #ifdef _SC_NL_SETMAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8147 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8148 | #endif |
| 8149 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8150 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8151 | #endif |
| 8152 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8153 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8154 | #endif |
| 8155 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8156 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8157 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8158 | #ifdef _SC_NPROC_CONF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8159 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8160 | #endif |
| 8161 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8162 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8163 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8164 | #ifdef _SC_NZERO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8165 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8166 | #endif |
| 8167 | #ifdef _SC_OPEN_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8168 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8169 | #endif |
| 8170 | #ifdef _SC_PAGESIZE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8171 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8172 | #endif |
| 8173 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8174 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8175 | #endif |
| 8176 | #ifdef _SC_PASS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8177 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8178 | #endif |
| 8179 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8180 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8181 | #endif |
| 8182 | #ifdef _SC_PII |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8183 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8184 | #endif |
| 8185 | #ifdef _SC_PII_INTERNET |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8186 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8187 | #endif |
| 8188 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8189 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8190 | #endif |
| 8191 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8192 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8193 | #endif |
| 8194 | #ifdef _SC_PII_OSI |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8195 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8196 | #endif |
| 8197 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8198 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8199 | #endif |
| 8200 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8201 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8202 | #endif |
| 8203 | #ifdef _SC_PII_OSI_M |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8204 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8205 | #endif |
| 8206 | #ifdef _SC_PII_SOCKET |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8207 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8208 | #endif |
| 8209 | #ifdef _SC_PII_XTI |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8210 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8211 | #endif |
| 8212 | #ifdef _SC_POLL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8213 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8214 | #endif |
| 8215 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8216 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8217 | #endif |
| 8218 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8219 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8220 | #endif |
| 8221 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8222 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8223 | #endif |
| 8224 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8225 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8226 | #endif |
| 8227 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8228 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8229 | #endif |
| 8230 | #ifdef _SC_SAVED_IDS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8231 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8232 | #endif |
| 8233 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8234 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8235 | #endif |
| 8236 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8237 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8238 | #endif |
| 8239 | #ifdef _SC_SELECT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8240 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8241 | #endif |
| 8242 | #ifdef _SC_SEMAPHORES |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8243 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8244 | #endif |
| 8245 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8246 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8247 | #endif |
| 8248 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8249 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8250 | #endif |
| 8251 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8252 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8253 | #endif |
| 8254 | #ifdef _SC_SHRT_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8255 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8256 | #endif |
| 8257 | #ifdef _SC_SHRT_MIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8258 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8259 | #endif |
| 8260 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8261 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8262 | #endif |
| 8263 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8264 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8265 | #endif |
| 8266 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8267 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8268 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8269 | #ifdef _SC_SOFTPOWER |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8270 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8271 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8272 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8273 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8274 | #endif |
| 8275 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8276 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8277 | #endif |
| 8278 | #ifdef _SC_STACK_PROT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8279 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8280 | #endif |
| 8281 | #ifdef _SC_STREAM_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8282 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8283 | #endif |
| 8284 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8285 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8286 | #endif |
| 8287 | #ifdef _SC_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8288 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8289 | #endif |
| 8290 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8291 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8292 | #endif |
| 8293 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8294 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8295 | #endif |
| 8296 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8297 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8298 | #endif |
| 8299 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8300 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8301 | #endif |
| 8302 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8303 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8304 | #endif |
| 8305 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8306 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8307 | #endif |
| 8308 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8309 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8310 | #endif |
| 8311 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8312 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8313 | #endif |
| 8314 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8315 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8316 | #endif |
| 8317 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8318 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8319 | #endif |
| 8320 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8321 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8322 | #endif |
| 8323 | #ifdef _SC_TIMERS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8324 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8325 | #endif |
| 8326 | #ifdef _SC_TIMER_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8327 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8328 | #endif |
| 8329 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8330 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8331 | #endif |
| 8332 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8333 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8334 | #endif |
| 8335 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8336 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8337 | #endif |
| 8338 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8339 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8340 | #endif |
| 8341 | #ifdef _SC_UINT_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8342 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8343 | #endif |
| 8344 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8345 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8346 | #endif |
| 8347 | #ifdef _SC_ULONG_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8348 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8349 | #endif |
| 8350 | #ifdef _SC_USHRT_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8351 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8352 | #endif |
| 8353 | #ifdef _SC_VERSION |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8354 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8355 | #endif |
| 8356 | #ifdef _SC_WORD_BIT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8357 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8358 | #endif |
| 8359 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8360 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8361 | #endif |
| 8362 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8363 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8364 | #endif |
| 8365 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8366 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8367 | #endif |
| 8368 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8369 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8370 | #endif |
| 8371 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8372 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8373 | #endif |
| 8374 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8375 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8376 | #endif |
| 8377 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8378 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8379 | #endif |
| 8380 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8381 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8382 | #endif |
| 8383 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8384 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8385 | #endif |
| 8386 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8387 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8388 | #endif |
| 8389 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8390 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8391 | #endif |
| 8392 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8393 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8394 | #endif |
| 8395 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8396 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8397 | #endif |
| 8398 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8399 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8400 | #endif |
| 8401 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8402 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8403 | #endif |
| 8404 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8405 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8406 | #endif |
| 8407 | }; |
| 8408 | |
| 8409 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8410 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8411 | { |
| 8412 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 8413 | sizeof(posix_constants_sysconf) |
| 8414 | / sizeof(struct constdef)); |
| 8415 | } |
| 8416 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8417 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8418 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8419 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8420 | |
| 8421 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8422 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8423 | { |
| 8424 | PyObject *result = NULL; |
| 8425 | int name; |
| 8426 | |
| 8427 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
Victor Stinner | 862490a | 2010-05-06 00:03:44 +0000 | [diff] [blame] | 8428 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8429 | |
Victor Stinner | 862490a | 2010-05-06 00:03:44 +0000 | [diff] [blame] | 8430 | errno = 0; |
| 8431 | value = sysconf(name); |
| 8432 | if (value == -1 && errno != 0) |
| 8433 | posix_error(); |
| 8434 | else |
| 8435 | result = PyInt_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8436 | } |
| 8437 | return result; |
| 8438 | } |
| 8439 | #endif |
| 8440 | |
| 8441 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8442 | /* This code is used to ensure that the tables of configuration value names |
| 8443 | * are in sorted order as required by conv_confname(), and also to build the |
| 8444 | * the exported dictionaries that are used to publish information about the |
| 8445 | * names available on the host platform. |
| 8446 | * |
| 8447 | * Sorting the table at runtime ensures that the table is properly ordered |
| 8448 | * when used, even for platforms we're not able to test on. It also makes |
| 8449 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8450 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8451 | |
| 8452 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8453 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8454 | { |
| 8455 | const struct constdef *c1 = |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8456 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8457 | const struct constdef *c2 = |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8458 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8459 | |
| 8460 | return strcmp(c1->name, c2->name); |
| 8461 | } |
| 8462 | |
| 8463 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8464 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8465 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8466 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8467 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 8468 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8469 | |
| 8470 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 8471 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 8472 | if (d == NULL) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8473 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8474 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 8475 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8476 | PyObject *o = PyInt_FromLong(table[i].value); |
| 8477 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 8478 | Py_XDECREF(o); |
| 8479 | Py_DECREF(d); |
| 8480 | return -1; |
| 8481 | } |
| 8482 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8483 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8484 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8485 | } |
| 8486 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8487 | /* Return -1 on failure, 0 on success. */ |
| 8488 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8489 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8490 | { |
| 8491 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8492 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8493 | sizeof(posix_constants_pathconf) |
| 8494 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8495 | "pathconf_names", module)) |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 8496 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8497 | #endif |
| 8498 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8499 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8500 | sizeof(posix_constants_confstr) |
| 8501 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8502 | "confstr_names", module)) |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 8503 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8504 | #endif |
| 8505 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8506 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8507 | sizeof(posix_constants_sysconf) |
| 8508 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8509 | "sysconf_names", module)) |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 8510 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8511 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8512 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8513 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8514 | |
| 8515 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8516 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8517 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8518 | 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] | 8519 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8520 | |
| 8521 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8522 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8523 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8524 | abort(); |
| 8525 | /*NOTREACHED*/ |
| 8526 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 8527 | return NULL; |
| 8528 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8529 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8530 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8531 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 8532 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 8533 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 8534 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 8535 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 8536 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 8537 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 8538 | application (if any) its extension is associated.\n\ |
| 8539 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 8540 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 8541 | \n\ |
| 8542 | startfile returns as soon as the associated application is launched.\n\ |
| 8543 | There is no option to wait for the application to close, and no way\n\ |
| 8544 | to retrieve the application's exit status.\n\ |
| 8545 | \n\ |
| 8546 | The filepath is relative to the current directory. If you want to use\n\ |
| 8547 | 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] | 8548 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 8549 | |
| 8550 | static PyObject * |
| 8551 | win32_startfile(PyObject *self, PyObject *args) |
| 8552 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8553 | char *filepath; |
| 8554 | char *operation = NULL; |
| 8555 | HINSTANCE rc; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 8556 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8557 | PyObject *unipath, *woperation = NULL; |
| 8558 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 8559 | &unipath, &operation)) { |
| 8560 | PyErr_Clear(); |
| 8561 | goto normal; |
| 8562 | } |
Hirokazu Yamamoto | a3c5609 | 2009-06-28 10:23:00 +0000 | [diff] [blame] | 8563 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8564 | if (operation) { |
| 8565 | woperation = PyUnicode_DecodeASCII(operation, |
| 8566 | strlen(operation), NULL); |
| 8567 | if (!woperation) { |
| 8568 | PyErr_Clear(); |
| 8569 | operation = NULL; |
| 8570 | goto normal; |
| 8571 | } |
| 8572 | } |
Hirokazu Yamamoto | a3c5609 | 2009-06-28 10:23:00 +0000 | [diff] [blame] | 8573 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8574 | Py_BEGIN_ALLOW_THREADS |
| 8575 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 8576 | PyUnicode_AS_UNICODE(unipath), |
| 8577 | NULL, NULL, SW_SHOWNORMAL); |
| 8578 | Py_END_ALLOW_THREADS |
| 8579 | |
| 8580 | Py_XDECREF(woperation); |
| 8581 | if (rc <= (HINSTANCE)32) { |
| 8582 | PyObject *errval = win32_error_unicode("startfile", |
| 8583 | PyUnicode_AS_UNICODE(unipath)); |
| 8584 | return errval; |
| 8585 | } |
| 8586 | Py_INCREF(Py_None); |
| 8587 | return Py_None; |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 8588 | |
| 8589 | normal: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8590 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 8591 | Py_FileSystemDefaultEncoding, &filepath, |
| 8592 | &operation)) |
| 8593 | return NULL; |
| 8594 | Py_BEGIN_ALLOW_THREADS |
| 8595 | rc = ShellExecute((HWND)0, operation, filepath, |
| 8596 | NULL, NULL, SW_SHOWNORMAL); |
| 8597 | Py_END_ALLOW_THREADS |
| 8598 | if (rc <= (HINSTANCE)32) { |
| 8599 | PyObject *errval = win32_error("startfile", filepath); |
| 8600 | PyMem_Free(filepath); |
| 8601 | return errval; |
| 8602 | } |
| 8603 | PyMem_Free(filepath); |
| 8604 | Py_INCREF(Py_None); |
| 8605 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 8606 | } |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 8607 | #endif /* MS_WINDOWS */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8608 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8609 | #ifdef HAVE_GETLOADAVG |
| 8610 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 8611 | "getloadavg() -> (float, float, float)\n\n\ |
| 8612 | Return the number of processes in the system run queue averaged over\n\ |
| 8613 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 8614 | was unobtainable"); |
| 8615 | |
| 8616 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8617 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8618 | { |
| 8619 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8620 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 8621 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 8622 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8623 | } else |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 8624 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8625 | } |
| 8626 | #endif |
| 8627 | |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 8628 | PyDoc_STRVAR(posix_urandom__doc__, |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8629 | "urandom(n) -> str\n\n\ |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 8630 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8631 | |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 8632 | static PyObject * |
| 8633 | posix_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8634 | { |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 8635 | Py_ssize_t size; |
| 8636 | PyObject *result; |
| 8637 | int ret; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8638 | |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 8639 | /* Read arguments */ |
| 8640 | if (!PyArg_ParseTuple(args, "n:urandom", &size)) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8641 | return NULL; |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 8642 | if (size < 0) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8643 | return PyErr_Format(PyExc_ValueError, |
| 8644 | "negative argument not allowed"); |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 8645 | result = PyBytes_FromStringAndSize(NULL, size); |
| 8646 | if (result == NULL) |
| 8647 | return NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8648 | |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 8649 | ret = _PyOS_URandom(PyBytes_AS_STRING(result), |
| 8650 | PyBytes_GET_SIZE(result)); |
| 8651 | if (ret == -1) { |
| 8652 | Py_DECREF(result); |
| 8653 | return NULL; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8654 | } |
| 8655 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8656 | } |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 8657 | |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8658 | #ifdef HAVE_SETRESUID |
| 8659 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 8660 | "setresuid(ruid, euid, suid)\n\n\ |
| 8661 | Set the current process's real, effective, and saved user ids."); |
| 8662 | |
| 8663 | static PyObject* |
| 8664 | posix_setresuid (PyObject *self, PyObject *args) |
| 8665 | { |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 8666 | uid_t ruid, euid, suid; |
| 8667 | if (!PyArg_ParseTuple(args, "O&O&O&:setresuid", |
| 8668 | _Py_Uid_Converter, &ruid, |
| 8669 | _Py_Uid_Converter, &euid, |
| 8670 | _Py_Uid_Converter, &suid)) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8671 | return NULL; |
| 8672 | if (setresuid(ruid, euid, suid) < 0) |
| 8673 | return posix_error(); |
| 8674 | Py_RETURN_NONE; |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8675 | } |
| 8676 | #endif |
| 8677 | |
| 8678 | #ifdef HAVE_SETRESGID |
| 8679 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 8680 | "setresgid(rgid, egid, sgid)\n\n\ |
| 8681 | Set the current process's real, effective, and saved group ids."); |
| 8682 | |
| 8683 | static PyObject* |
| 8684 | posix_setresgid (PyObject *self, PyObject *args) |
| 8685 | { |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 8686 | gid_t rgid, egid, sgid; |
| 8687 | if (!PyArg_ParseTuple(args, "O&O&O&:setresgid", |
| 8688 | _Py_Gid_Converter, &rgid, |
| 8689 | _Py_Gid_Converter, &egid, |
| 8690 | _Py_Gid_Converter, &sgid)) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8691 | return NULL; |
| 8692 | if (setresgid(rgid, egid, sgid) < 0) |
| 8693 | return posix_error(); |
| 8694 | Py_RETURN_NONE; |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8695 | } |
| 8696 | #endif |
| 8697 | |
| 8698 | #ifdef HAVE_GETRESUID |
| 8699 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 8700 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 8701 | Get tuple of the current process's real, effective, and saved user ids."); |
| 8702 | |
| 8703 | static PyObject* |
| 8704 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 8705 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8706 | uid_t ruid, euid, suid; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8707 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 8708 | return posix_error(); |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 8709 | return Py_BuildValue("(NNN)", _PyInt_FromUid(ruid), |
| 8710 | _PyInt_FromUid(euid), |
| 8711 | _PyInt_FromUid(suid)); |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8712 | } |
| 8713 | #endif |
| 8714 | |
| 8715 | #ifdef HAVE_GETRESGID |
| 8716 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 8717 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | 21946af | 2010-10-06 09:28:45 +0000 | [diff] [blame] | 8718 | Get tuple of the current process's real, effective, and saved group ids."); |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8719 | |
| 8720 | static PyObject* |
| 8721 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 8722 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8723 | uid_t rgid, egid, sgid; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8724 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 8725 | return posix_error(); |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 8726 | return Py_BuildValue("(NNN)", _PyInt_FromGid(rgid), |
| 8727 | _PyInt_FromGid(egid), |
| 8728 | _PyInt_FromGid(sgid)); |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8729 | } |
| 8730 | #endif |
| 8731 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8732 | static PyMethodDef posix_methods[] = { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8733 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8734 | #ifdef HAVE_TTYNAME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8735 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8736 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8737 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 8738 | #ifdef HAVE_CHFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8739 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 8740 | #endif /* HAVE_CHFLAGS */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8741 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 8742 | #ifdef HAVE_FCHMOD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8743 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 8744 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8745 | #ifdef HAVE_CHOWN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8746 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8747 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 8748 | #ifdef HAVE_LCHMOD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8749 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 8750 | #endif /* HAVE_LCHMOD */ |
| 8751 | #ifdef HAVE_FCHOWN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8752 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 8753 | #endif /* HAVE_FCHOWN */ |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 8754 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8755 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 8756 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 8757 | #ifdef HAVE_LCHOWN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8758 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 8759 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 8760 | #ifdef HAVE_CHROOT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8761 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 8762 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8763 | #ifdef HAVE_CTERMID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8764 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8765 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 8766 | #ifdef HAVE_GETCWD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8767 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 8768 | #ifdef Py_USING_UNICODE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8769 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 8770 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 8771 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8772 | #ifdef HAVE_LINK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8773 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8774 | #endif /* HAVE_LINK */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8775 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 8776 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 8777 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8778 | #ifdef HAVE_NICE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8779 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8780 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8781 | #ifdef HAVE_READLINK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8782 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8783 | #endif /* HAVE_READLINK */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8784 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 8785 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 8786 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
| 8787 | {"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] | 8788 | #ifdef HAVE_SYMLINK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8789 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8790 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8791 | #ifdef HAVE_SYSTEM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8792 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8793 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8794 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8795 | #ifdef HAVE_UNAME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8796 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8797 | #endif /* HAVE_UNAME */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8798 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 8799 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 8800 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8801 | #ifdef HAVE_TIMES |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8802 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8803 | #endif /* HAVE_TIMES */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8804 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8805 | #ifdef HAVE_EXECV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8806 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 8807 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8808 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 8809 | #ifdef HAVE_SPAWNV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8810 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 8811 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 8812 | #if defined(PYOS_OS2) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8813 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 8814 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 8815 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 8816 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 8817 | #ifdef HAVE_FORK1 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8818 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 8819 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8820 | #ifdef HAVE_FORK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8821 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8822 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 8823 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8824 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 8825 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 8826 | #ifdef HAVE_FORKPTY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8827 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 8828 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8829 | #ifdef HAVE_GETEGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8830 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8831 | #endif /* HAVE_GETEGID */ |
| 8832 | #ifdef HAVE_GETEUID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8833 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8834 | #endif /* HAVE_GETEUID */ |
| 8835 | #ifdef HAVE_GETGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8836 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8837 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8838 | #ifdef HAVE_GETGROUPS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8839 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8840 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8841 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8842 | #ifdef HAVE_GETPGRP |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8843 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8844 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8845 | #ifdef HAVE_GETPPID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8846 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8847 | #endif /* HAVE_GETPPID */ |
| 8848 | #ifdef HAVE_GETUID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8849 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8850 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8851 | #ifdef HAVE_GETLOGIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8852 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8853 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8854 | #ifdef HAVE_KILL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8855 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8856 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 8857 | #ifdef HAVE_KILLPG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8858 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 8859 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8860 | #ifdef HAVE_PLOCK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8861 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8862 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8863 | #ifdef HAVE_POPEN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8864 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8865 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8866 | {"popen2", win32_popen2, METH_VARARGS}, |
| 8867 | {"popen3", win32_popen3, METH_VARARGS}, |
| 8868 | {"popen4", win32_popen4, METH_VARARGS}, |
| 8869 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 8870 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8871 | #else |
| 8872 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8873 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 8874 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 8875 | {"popen4", os2emx_popen4, METH_VARARGS}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8876 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 8877 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8878 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8879 | #ifdef HAVE_SETUID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8880 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8881 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8882 | #ifdef HAVE_SETEUID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8883 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8884 | #endif /* HAVE_SETEUID */ |
| 8885 | #ifdef HAVE_SETEGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8886 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8887 | #endif /* HAVE_SETEGID */ |
| 8888 | #ifdef HAVE_SETREUID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8889 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8890 | #endif /* HAVE_SETREUID */ |
| 8891 | #ifdef HAVE_SETREGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8892 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8893 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8894 | #ifdef HAVE_SETGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8895 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8896 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8897 | #ifdef HAVE_SETGROUPS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8898 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8899 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 8900 | #ifdef HAVE_INITGROUPS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8901 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 8902 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 8903 | #ifdef HAVE_GETPGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8904 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 8905 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8906 | #ifdef HAVE_SETPGRP |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8907 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8908 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8909 | #ifdef HAVE_WAIT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8910 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8911 | #endif /* HAVE_WAIT */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 8912 | #ifdef HAVE_WAIT3 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8913 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 8914 | #endif /* HAVE_WAIT3 */ |
| 8915 | #ifdef HAVE_WAIT4 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8916 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 8917 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 8918 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8919 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8920 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8921 | #ifdef HAVE_GETSID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8922 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8923 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8924 | #ifdef HAVE_SETSID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8925 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8926 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8927 | #ifdef HAVE_SETPGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8928 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8929 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8930 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8931 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8932 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8933 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8934 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8935 | #endif /* HAVE_TCSETPGRP */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8936 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 8937 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 8938 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 8939 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 8940 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 8941 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 8942 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 8943 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 8944 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 8945 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
| 8946 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8947 | #ifdef HAVE_PIPE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8948 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8949 | #endif |
| 8950 | #ifdef HAVE_MKFIFO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8951 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8952 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 8953 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8954 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8955 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8956 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8957 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 8958 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 8959 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8960 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8961 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8962 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8963 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8964 | #ifdef HAVE_PUTENV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8965 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8966 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8967 | #ifdef HAVE_UNSETENV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8968 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8969 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8970 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8971 | #ifdef HAVE_FCHDIR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8972 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8973 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8974 | #ifdef HAVE_FSYNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8975 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8976 | #endif |
| 8977 | #ifdef HAVE_FDATASYNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8978 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8979 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8980 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8981 | #ifdef WCOREDUMP |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8982 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8983 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8984 | #ifdef WIFCONTINUED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8985 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8986 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8987 | #ifdef WIFSTOPPED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8988 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8989 | #endif /* WIFSTOPPED */ |
| 8990 | #ifdef WIFSIGNALED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8991 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8992 | #endif /* WIFSIGNALED */ |
| 8993 | #ifdef WIFEXITED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8994 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8995 | #endif /* WIFEXITED */ |
| 8996 | #ifdef WEXITSTATUS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8997 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8998 | #endif /* WEXITSTATUS */ |
| 8999 | #ifdef WTERMSIG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9000 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9001 | #endif /* WTERMSIG */ |
| 9002 | #ifdef WSTOPSIG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9003 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9004 | #endif /* WSTOPSIG */ |
| 9005 | #endif /* HAVE_SYS_WAIT_H */ |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 9006 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9007 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9008 | #endif |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 9009 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9010 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9011 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 9012 | #ifdef HAVE_TMPFILE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9013 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9014 | #endif |
| 9015 | #ifdef HAVE_TEMPNAM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9016 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9017 | #endif |
| 9018 | #ifdef HAVE_TMPNAM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9019 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9020 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9021 | #ifdef HAVE_CONFSTR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9022 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9023 | #endif |
| 9024 | #ifdef HAVE_SYSCONF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9025 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9026 | #endif |
| 9027 | #ifdef HAVE_FPATHCONF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9028 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9029 | #endif |
| 9030 | #ifdef HAVE_PATHCONF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9031 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9032 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9033 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9034 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9035 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | 5446f08 | 2011-06-09 10:00:42 -0500 | [diff] [blame] | 9036 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 9037 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9038 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9039 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9040 | #endif |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 9041 | #ifdef HAVE_SETRESUID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9042 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 9043 | #endif |
| 9044 | #ifdef HAVE_SETRESGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9045 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 9046 | #endif |
| 9047 | #ifdef HAVE_GETRESUID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9048 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 9049 | #endif |
| 9050 | #ifdef HAVE_GETRESGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9051 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 9052 | #endif |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 9053 | {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__}, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9054 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9055 | }; |
| 9056 | |
| 9057 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9058 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9059 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9060 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9061 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9062 | } |
| 9063 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9064 | #if defined(PYOS_OS2) |
| 9065 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9066 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9067 | { |
| 9068 | APIRET rc; |
| 9069 | ULONG values[QSV_MAX+1]; |
| 9070 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 9071 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9072 | |
| 9073 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 9074 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9075 | Py_END_ALLOW_THREADS |
| 9076 | |
| 9077 | if (rc != NO_ERROR) { |
| 9078 | os2_error(rc); |
| 9079 | return -1; |
| 9080 | } |
| 9081 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9082 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 9083 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 9084 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 9085 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 9086 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 9087 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 9088 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9089 | |
| 9090 | switch (values[QSV_VERSION_MINOR]) { |
| 9091 | case 0: ver = "2.00"; break; |
| 9092 | case 10: ver = "2.10"; break; |
| 9093 | case 11: ver = "2.11"; break; |
| 9094 | case 30: ver = "3.00"; break; |
| 9095 | case 40: ver = "4.00"; break; |
| 9096 | case 50: ver = "5.00"; break; |
| 9097 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 9098 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9099 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 9100 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9101 | ver = &tmp[0]; |
| 9102 | } |
| 9103 | |
| 9104 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9105 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9106 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9107 | |
| 9108 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 9109 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 9110 | tmp[1] = ':'; |
| 9111 | tmp[2] = '\0'; |
| 9112 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9113 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9114 | } |
| 9115 | #endif |
| 9116 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9117 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 9118 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9119 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9120 | #ifdef F_OK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9121 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9122 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9123 | #ifdef R_OK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9124 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9125 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9126 | #ifdef W_OK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9127 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9128 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9129 | #ifdef X_OK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9130 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9131 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9132 | #ifdef NGROUPS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9133 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9134 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9135 | #ifdef TMP_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9136 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9137 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9138 | #ifdef WCONTINUED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9139 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9140 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9141 | #ifdef WNOHANG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9142 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9143 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9144 | #ifdef WUNTRACED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9145 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9146 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9147 | #ifdef O_RDONLY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9148 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9149 | #endif |
| 9150 | #ifdef O_WRONLY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9151 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9152 | #endif |
| 9153 | #ifdef O_RDWR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9154 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9155 | #endif |
| 9156 | #ifdef O_NDELAY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9157 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9158 | #endif |
| 9159 | #ifdef O_NONBLOCK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9160 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9161 | #endif |
| 9162 | #ifdef O_APPEND |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9163 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9164 | #endif |
| 9165 | #ifdef O_DSYNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9166 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9167 | #endif |
| 9168 | #ifdef O_RSYNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9169 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9170 | #endif |
| 9171 | #ifdef O_SYNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9172 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9173 | #endif |
| 9174 | #ifdef O_NOCTTY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9175 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9176 | #endif |
| 9177 | #ifdef O_CREAT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9178 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9179 | #endif |
| 9180 | #ifdef O_EXCL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9181 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9182 | #endif |
| 9183 | #ifdef O_TRUNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9184 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9185 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 9186 | #ifdef O_BINARY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9187 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 9188 | #endif |
| 9189 | #ifdef O_TEXT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9190 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 9191 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9192 | #ifdef O_LARGEFILE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9193 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9194 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 9195 | #ifdef O_SHLOCK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9196 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 9197 | #endif |
| 9198 | #ifdef O_EXLOCK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9199 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 9200 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9201 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9202 | /* MS Windows */ |
| 9203 | #ifdef O_NOINHERIT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9204 | /* Don't inherit in child processes. */ |
| 9205 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9206 | #endif |
| 9207 | #ifdef _O_SHORT_LIVED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9208 | /* Optimize for short life (keep in memory). */ |
| 9209 | /* MS forgot to define this one with a non-underscore form too. */ |
| 9210 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9211 | #endif |
| 9212 | #ifdef O_TEMPORARY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9213 | /* Automatically delete when last handle is closed. */ |
| 9214 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9215 | #endif |
| 9216 | #ifdef O_RANDOM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9217 | /* Optimize for random access. */ |
| 9218 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9219 | #endif |
| 9220 | #ifdef O_SEQUENTIAL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9221 | /* Optimize for sequential access. */ |
| 9222 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9223 | #endif |
| 9224 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9225 | /* GNU extensions. */ |
Georg Brandl | 5049a85 | 2008-05-16 13:10:15 +0000 | [diff] [blame] | 9226 | #ifdef O_ASYNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9227 | /* Send a SIGIO signal whenever input or output |
| 9228 | becomes available on file descriptor */ |
| 9229 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Georg Brandl | 5049a85 | 2008-05-16 13:10:15 +0000 | [diff] [blame] | 9230 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9231 | #ifdef O_DIRECT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9232 | /* Direct disk access. */ |
| 9233 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9234 | #endif |
| 9235 | #ifdef O_DIRECTORY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9236 | /* Must be a directory. */ |
| 9237 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9238 | #endif |
| 9239 | #ifdef O_NOFOLLOW |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9240 | /* Do not follow links. */ |
| 9241 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9242 | #endif |
Georg Brandl | b67da6e | 2007-11-24 13:56:09 +0000 | [diff] [blame] | 9243 | #ifdef O_NOATIME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9244 | /* Do not update the access time. */ |
| 9245 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Georg Brandl | b67da6e | 2007-11-24 13:56:09 +0000 | [diff] [blame] | 9246 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9247 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9248 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9249 | #ifdef EX_OK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9250 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9251 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9252 | #ifdef EX_USAGE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9253 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9254 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9255 | #ifdef EX_DATAERR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9256 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9257 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9258 | #ifdef EX_NOINPUT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9259 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9260 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9261 | #ifdef EX_NOUSER |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9262 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9263 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9264 | #ifdef EX_NOHOST |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9265 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9266 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9267 | #ifdef EX_UNAVAILABLE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9268 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9269 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9270 | #ifdef EX_SOFTWARE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9271 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9272 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9273 | #ifdef EX_OSERR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9274 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9275 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9276 | #ifdef EX_OSFILE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9277 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9278 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9279 | #ifdef EX_CANTCREAT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9280 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9281 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9282 | #ifdef EX_IOERR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9283 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9284 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9285 | #ifdef EX_TEMPFAIL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9286 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9287 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9288 | #ifdef EX_PROTOCOL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9289 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9290 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9291 | #ifdef EX_NOPERM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9292 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9293 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9294 | #ifdef EX_CONFIG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9295 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9296 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9297 | #ifdef EX_NOTFOUND |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9298 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9299 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9300 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 9301 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9302 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9303 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 9304 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 9305 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 9306 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 9307 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 9308 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 9309 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 9310 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 9311 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 9312 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 9313 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 9314 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 9315 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 9316 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 9317 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 9318 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 9319 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 9320 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 9321 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 9322 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9323 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9324 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 9325 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 9326 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 9327 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 9328 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 9329 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9330 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 9331 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9332 | #if defined(PYOS_OS2) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9333 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9334 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9335 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9336 | } |
| 9337 | |
| 9338 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9339 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 9340 | #define INITFUNC initnt |
| 9341 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 9342 | |
| 9343 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 9344 | #define INITFUNC initos2 |
| 9345 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 9346 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 9347 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 9348 | #define INITFUNC initposix |
| 9349 | #define MODNAME "posix" |
| 9350 | #endif |
| 9351 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 9352 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 9353 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9354 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9355 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9356 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9357 | m = Py_InitModule3(MODNAME, |
| 9358 | posix_methods, |
| 9359 | posix__doc__); |
| 9360 | if (m == NULL) |
| 9361 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9362 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9363 | /* Initialize environ dictionary */ |
| 9364 | v = convertenviron(); |
| 9365 | Py_XINCREF(v); |
| 9366 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 9367 | return; |
| 9368 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9369 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9370 | if (all_ins(m)) |
| 9371 | return; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9372 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9373 | if (setup_confname_tables(m)) |
| 9374 | return; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9375 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9376 | Py_INCREF(PyExc_OSError); |
| 9377 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 9378 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 9379 | #ifdef HAVE_PUTENV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9380 | if (posix_putenv_garbage == NULL) |
| 9381 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 9382 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9383 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9384 | if (!initialized) { |
| 9385 | stat_result_desc.name = MODNAME ".stat_result"; |
| 9386 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 9387 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 9388 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 9389 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 9390 | structseq_new = StatResultType.tp_new; |
| 9391 | StatResultType.tp_new = statresult_new; |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 9392 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9393 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 9394 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 03824e4 | 2008-12-29 18:17:34 +0000 | [diff] [blame] | 9395 | #ifdef NEED_TICKS_PER_SECOND |
| 9396 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9397 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 03824e4 | 2008-12-29 18:17:34 +0000 | [diff] [blame] | 9398 | # elif defined(HZ) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9399 | ticks_per_second = HZ; |
Martin v. Löwis | 03824e4 | 2008-12-29 18:17:34 +0000 | [diff] [blame] | 9400 | # else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9401 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 03824e4 | 2008-12-29 18:17:34 +0000 | [diff] [blame] | 9402 | # endif |
| 9403 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9404 | } |
| 9405 | Py_INCREF((PyObject*) &StatResultType); |
| 9406 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 9407 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 9408 | PyModule_AddObject(m, "statvfs_result", |
| 9409 | (PyObject*) &StatVFSResultType); |
| 9410 | initialized = 1; |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 9411 | |
| 9412 | #ifdef __APPLE__ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9413 | /* |
| 9414 | * Step 2 of weak-linking support on Mac OS X. |
| 9415 | * |
| 9416 | * The code below removes functions that are not available on the |
| 9417 | * currently active platform. |
| 9418 | * |
| 9419 | * This block allow one to use a python binary that was build on |
| 9420 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 9421 | * OSX 10.4. |
| 9422 | */ |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 9423 | #ifdef HAVE_FSTATVFS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9424 | if (fstatvfs == NULL) { |
| 9425 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 9426 | return; |
| 9427 | } |
| 9428 | } |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 9429 | #endif /* HAVE_FSTATVFS */ |
| 9430 | |
| 9431 | #ifdef HAVE_STATVFS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9432 | if (statvfs == NULL) { |
| 9433 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 9434 | return; |
| 9435 | } |
| 9436 | } |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 9437 | #endif /* HAVE_STATVFS */ |
| 9438 | |
| 9439 | # ifdef HAVE_LCHOWN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9440 | if (lchown == NULL) { |
| 9441 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 9442 | return; |
| 9443 | } |
| 9444 | } |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 9445 | #endif /* HAVE_LCHOWN */ |
| 9446 | |
| 9447 | |
| 9448 | #endif /* __APPLE__ */ |
| 9449 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9450 | } |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 9451 | |
| 9452 | #ifdef __cplusplus |
| 9453 | } |
| 9454 | #endif |
| 9455 | |
Martin v. Löwis | 18aaa56 | 2006-10-15 08:43:33 +0000 | [diff] [blame] | 9456 | |