Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 14 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 15 | |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 16 | #ifdef __APPLE__ |
| 17 | /* |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 18 | * 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] | 19 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 20 | * at the end of this file for more information. |
| 21 | */ |
| 22 | # pragma weak lchown |
| 23 | # pragma weak statvfs |
| 24 | # pragma weak fstatvfs |
| 25 | |
| 26 | #endif /* __APPLE__ */ |
| 27 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 28 | #define PY_SSIZE_T_CLEAN |
| 29 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 30 | #include "Python.h" |
| 31 | #include "structseq.h" |
| 32 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 33 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 34 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 35 | #endif /* defined(__VMS) */ |
| 36 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 41 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 42 | "This module provides access to operating system functionality that is\n\ |
| 43 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 44 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 45 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 47 | #ifndef Py_USING_UNICODE |
| 48 | /* This is used in signatures of functions. */ |
| 49 | #define Py_UNICODE void |
| 50 | #endif |
| 51 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 52 | #if defined(PYOS_OS2) |
| 53 | #define INCL_DOS |
| 54 | #define INCL_DOSERRORS |
| 55 | #define INCL_DOSPROCESS |
| 56 | #define INCL_NOPMAPI |
| 57 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 58 | #if defined(PYCC_GCC) |
| 59 | #include <ctype.h> |
| 60 | #include <io.h> |
| 61 | #include <stdio.h> |
| 62 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 63 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 64 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 65 | #endif |
| 66 | |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 67 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 68 | #include <sys/types.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 69 | #endif /* HAVE_SYS_TYPES_H */ |
| 70 | |
| 71 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 72 | #include <sys/stat.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 73 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 75 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 76 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 77 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 78 | |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 79 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 80 | #include <signal.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 81 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 83 | #ifdef HAVE_FCNTL_H |
| 84 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 85 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 87 | #ifdef HAVE_GRP_H |
| 88 | #include <grp.h> |
| 89 | #endif |
| 90 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 91 | #ifdef HAVE_SYSEXITS_H |
| 92 | #include <sysexits.h> |
| 93 | #endif /* HAVE_SYSEXITS_H */ |
| 94 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 95 | #ifdef HAVE_SYS_LOADAVG_H |
| 96 | #include <sys/loadavg.h> |
| 97 | #endif |
| 98 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 99 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 100 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 101 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 102 | #include <process.h> |
| 103 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 104 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 105 | #define HAVE_GETCWD 1 |
| 106 | #define HAVE_OPENDIR 1 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 107 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 108 | #if defined(__OS2__) |
| 109 | #define HAVE_EXECV 1 |
| 110 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 111 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 112 | #include <process.h> |
| 113 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 114 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 115 | #define HAVE_EXECV 1 |
| 116 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 117 | #define HAVE_OPENDIR 1 |
| 118 | #define HAVE_PIPE 1 |
| 119 | #define HAVE_POPEN 1 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 120 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 121 | #define HAVE_WAIT 1 |
| 122 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 123 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 124 | #define HAVE_GETCWD 1 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 125 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 126 | #define HAVE_EXECV 1 |
| 127 | #define HAVE_PIPE 1 |
| 128 | #define HAVE_POPEN 1 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 129 | #define HAVE_SYSTEM 1 |
| 130 | #define HAVE_CWAIT 1 |
| 131 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 132 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 133 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 134 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 135 | /* 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] | 136 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 137 | /* Unix functions that the configure script doesn't check for */ |
| 138 | #define HAVE_EXECV 1 |
| 139 | #define HAVE_FORK 1 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 140 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 141 | #define HAVE_FORK1 1 |
| 142 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 143 | #define HAVE_GETCWD 1 |
| 144 | #define HAVE_GETEGID 1 |
| 145 | #define HAVE_GETEUID 1 |
| 146 | #define HAVE_GETGID 1 |
| 147 | #define HAVE_GETPPID 1 |
| 148 | #define HAVE_GETUID 1 |
| 149 | #define HAVE_KILL 1 |
| 150 | #define HAVE_OPENDIR 1 |
| 151 | #define HAVE_PIPE 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 152 | #ifndef __rtems__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 153 | #define HAVE_POPEN 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 154 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 155 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 156 | #define HAVE_WAIT 1 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 157 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 158 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 159 | #endif /* _MSC_VER */ |
| 160 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 161 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 162 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 164 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 165 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 166 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 167 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 168 | (default) */ |
| 169 | extern char *ctermid_r(char *); |
| 170 | #endif |
| 171 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 172 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 173 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 174 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 175 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 176 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 177 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 178 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 179 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 180 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 181 | #endif |
| 182 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 183 | extern int chdir(char *); |
| 184 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 185 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 186 | extern int chdir(const char *); |
| 187 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 188 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 189 | #ifdef __BORLANDC__ |
| 190 | extern int chmod(const char *, int); |
| 191 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 192 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 193 | #endif |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 194 | /*#ifdef HAVE_FCHMOD |
| 195 | extern int fchmod(int, mode_t); |
| 196 | #endif*/ |
| 197 | /*#ifdef HAVE_LCHMOD |
| 198 | extern int lchmod(const char *, mode_t); |
| 199 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 200 | extern int chown(const char *, uid_t, gid_t); |
| 201 | extern char *getcwd(char *, int); |
| 202 | extern char *strerror(int); |
| 203 | extern int link(const char *, const char *); |
| 204 | extern int rename(const char *, const char *); |
| 205 | extern int stat(const char *, struct stat *); |
| 206 | extern int unlink(const char *); |
| 207 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 208 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 209 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 210 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 211 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 212 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 213 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 214 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 216 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 217 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 218 | #ifdef HAVE_UTIME_H |
| 219 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 220 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 222 | #ifdef HAVE_SYS_UTIME_H |
| 223 | #include <sys/utime.h> |
| 224 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 225 | #endif /* HAVE_SYS_UTIME_H */ |
| 226 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 227 | #ifdef HAVE_SYS_TIMES_H |
| 228 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 229 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 230 | |
| 231 | #ifdef HAVE_SYS_PARAM_H |
| 232 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 233 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | |
| 235 | #ifdef HAVE_SYS_UTSNAME_H |
| 236 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 237 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 239 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 240 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 241 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 242 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 243 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 244 | #include <direct.h> |
| 245 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 246 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 247 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 248 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 249 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 250 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 252 | #endif |
| 253 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 255 | #endif |
| 256 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 258 | #endif |
| 259 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 261 | #ifdef _MSC_VER |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 262 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | #include <direct.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 264 | #endif |
| 265 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 266 | #include <io.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 267 | #endif |
| 268 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 269 | #include <process.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 270 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 271 | #include "osdefs.h" |
Kristján Valur Jónsson | feab334 | 2009-04-01 16:08:34 +0000 | [diff] [blame] | 272 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 273 | #include <windows.h> |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 274 | #include <shellapi.h> /* for ShellExecute() */ |
| 275 | #define popen _popen |
| 276 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 277 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 278 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 279 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 280 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 281 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 282 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 283 | #ifndef MAXPATHLEN |
Thomas Wouters | 1ddba60 | 2006-04-25 15:29:46 +0000 | [diff] [blame] | 284 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 285 | #define MAXPATHLEN PATH_MAX |
| 286 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 287 | #define MAXPATHLEN 1024 |
Thomas Wouters | 1ddba60 | 2006-04-25 15:29:46 +0000 | [diff] [blame] | 288 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 289 | #endif /* MAXPATHLEN */ |
| 290 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 291 | #ifdef UNION_WAIT |
| 292 | /* Emulate some macros on systems that have a union instead of macros */ |
| 293 | |
| 294 | #ifndef WIFEXITED |
| 295 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 296 | #endif |
| 297 | |
| 298 | #ifndef WEXITSTATUS |
| 299 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 300 | #endif |
| 301 | |
| 302 | #ifndef WTERMSIG |
| 303 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 304 | #endif |
| 305 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 306 | #define WAIT_TYPE union wait |
| 307 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 308 | |
| 309 | #else /* !UNION_WAIT */ |
| 310 | #define WAIT_TYPE int |
| 311 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 312 | #endif /* UNION_WAIT */ |
| 313 | |
Antoine Pitrou | 5e858fe | 2009-05-23 15:37:45 +0000 | [diff] [blame] | 314 | /* 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] | 315 | #if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT |
Antoine Pitrou | 5e858fe | 2009-05-23 15:37:45 +0000 | [diff] [blame] | 316 | #define PARSE_PID "i" |
| 317 | #define PyLong_FromPid PyInt_FromLong |
| 318 | #define PyLong_AsPid PyInt_AsLong |
| 319 | #elif SIZEOF_PID_T == SIZEOF_LONG |
| 320 | #define PARSE_PID "l" |
| 321 | #define PyLong_FromPid PyInt_FromLong |
| 322 | #define PyLong_AsPid PyInt_AsLong |
| 323 | #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG |
| 324 | #define PARSE_PID "L" |
| 325 | #define PyLong_FromPid PyLong_FromLongLong |
| 326 | #define PyLong_AsPid PyInt_AsLongLong |
| 327 | #else |
| 328 | #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] | 329 | #endif /* SIZEOF_PID_T */ |
| 330 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 331 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 332 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 333 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 334 | #define USE_CTERMID_R |
| 335 | #endif |
| 336 | |
| 337 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 338 | #define USE_TMPNAM_R |
| 339 | #endif |
| 340 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 341 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 342 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 343 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 344 | # define STAT win32_stat |
| 345 | # define FSTAT win32_fstat |
| 346 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 347 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 348 | # define STAT stat |
| 349 | # define FSTAT fstat |
| 350 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 351 | #endif |
| 352 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 353 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 354 | #include <sys/mkdev.h> |
| 355 | #else |
| 356 | #if defined(MAJOR_IN_SYSMACROS) |
| 357 | #include <sys/sysmacros.h> |
| 358 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 359 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 360 | #include <sys/mkdev.h> |
| 361 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 362 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 363 | |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 364 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 365 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 366 | * valid and throw an assertion if it isn't. |
| 367 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 368 | * an assertion can be useful, but it does contradict the POSIX standard |
| 369 | * which for write(2) states: |
| 370 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 371 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 372 | * writing." |
| 373 | * Furthermore, python allows the user to enter any old integer |
| 374 | * as a fd and should merely raise a python exception on error. |
| 375 | * The Microsoft CRT doesn't provide an official way to check for the |
| 376 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 377 | * 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] | 378 | * internal structures involved. |
| 379 | * The structures below must be updated for each version of visual studio |
| 380 | * according to the file internal.h in the CRT source, until MS comes |
| 381 | * up with a less hacky way to do this. |
| 382 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 383 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 384 | */ |
Kristján Valur Jónsson | feab334 | 2009-04-01 16:08:34 +0000 | [diff] [blame] | 385 | /* The actual size of the structure is determined at runtime. |
| 386 | * Only the first items must be present. |
| 387 | */ |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 388 | typedef struct { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 389 | intptr_t osfhnd; |
| 390 | char osfile; |
Kristján Valur Jónsson | feab334 | 2009-04-01 16:08:34 +0000 | [diff] [blame] | 391 | } my_ioinfo; |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 392 | |
Kristján Valur Jónsson | feab334 | 2009-04-01 16:08:34 +0000 | [diff] [blame] | 393 | extern __declspec(dllimport) char * __pioinfo[]; |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 394 | #define IOINFO_L2E 5 |
| 395 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 396 | #define IOINFO_ARRAYS 64 |
| 397 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 398 | #define FOPEN 0x01 |
| 399 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 400 | |
| 401 | /* This function emulates what the windows CRT does to validate file handles */ |
| 402 | int |
| 403 | _PyVerify_fd(int fd) |
| 404 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 405 | const int i1 = fd >> IOINFO_L2E; |
| 406 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | feab334 | 2009-04-01 16:08:34 +0000 | [diff] [blame] | 407 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 408 | static int sizeof_ioinfo = 0; |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 409 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 410 | /* Determine the actual size of the ioinfo structure, |
| 411 | * as used by the CRT loaded in memory |
| 412 | */ |
| 413 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 414 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 415 | } |
| 416 | if (sizeof_ioinfo == 0) { |
| 417 | /* This should not happen... */ |
| 418 | goto fail; |
| 419 | } |
| 420 | |
| 421 | /* See that it isn't a special CLEAR fileno */ |
| 422 | if (fd != _NO_CONSOLE_FILENO) { |
| 423 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 424 | * we check pointer validity and other info |
| 425 | */ |
| 426 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 427 | /* finally, check that the file is open */ |
| 428 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 429 | if (info->osfile & FOPEN) { |
| 430 | return 1; |
| 431 | } |
| 432 | } |
| 433 | } |
Kristján Valur Jónsson | feab334 | 2009-04-01 16:08:34 +0000 | [diff] [blame] | 434 | fail: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 435 | errno = EBADF; |
| 436 | return 0; |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 440 | static int |
| 441 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 442 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 443 | if (!_PyVerify_fd(fd1)) |
| 444 | return 0; |
| 445 | if (fd2 == _NO_CONSOLE_FILENO) |
| 446 | return 0; |
| 447 | if ((unsigned)fd2 < _NHANDLE_) |
| 448 | return 1; |
| 449 | else |
| 450 | return 0; |
Kristján Valur Jónsson | 6a743d3 | 2009-02-10 13:32:24 +0000 | [diff] [blame] | 451 | } |
| 452 | #else |
| 453 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 454 | #define _PyVerify_fd_dup2(A, B) (1) |
| 455 | #endif |
| 456 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 457 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 458 | #ifdef WITH_NEXT_FRAMEWORK |
| 459 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 460 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 461 | */ |
| 462 | #include <crt_externs.h> |
| 463 | static char **environ; |
| 464 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 465 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 466 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 467 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 468 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 469 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 470 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 471 | PyObject *d; |
| 472 | char **e; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 473 | #if defined(PYOS_OS2) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 474 | APIRET rc; |
| 475 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 476 | #endif |
| 477 | d = PyDict_New(); |
| 478 | if (d == NULL) |
| 479 | return NULL; |
| 480 | #ifdef WITH_NEXT_FRAMEWORK |
| 481 | if (environ == NULL) |
| 482 | environ = *_NSGetEnviron(); |
| 483 | #endif |
| 484 | if (environ == NULL) |
| 485 | return d; |
| 486 | /* This part ignores errors */ |
| 487 | for (e = environ; *e != NULL; e++) { |
| 488 | PyObject *k; |
| 489 | PyObject *v; |
| 490 | char *p = strchr(*e, '='); |
| 491 | if (p == NULL) |
| 492 | continue; |
| 493 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 494 | if (k == NULL) { |
| 495 | PyErr_Clear(); |
| 496 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 497 | } |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 498 | v = PyString_FromString(p+1); |
| 499 | if (v == NULL) { |
| 500 | PyErr_Clear(); |
| 501 | Py_DECREF(k); |
| 502 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 503 | } |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 504 | if (PyDict_GetItem(d, k) == NULL) { |
| 505 | if (PyDict_SetItem(d, k, v) != 0) |
| 506 | PyErr_Clear(); |
| 507 | } |
| 508 | Py_DECREF(k); |
| 509 | Py_DECREF(v); |
| 510 | } |
| 511 | #if defined(PYOS_OS2) |
| 512 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 513 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 514 | PyObject *v = PyString_FromString(buffer); |
| 515 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 516 | Py_DECREF(v); |
| 517 | } |
| 518 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 519 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 520 | PyObject *v = PyString_FromString(buffer); |
| 521 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 522 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 523 | } |
| 524 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 525 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 529 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 530 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 531 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 532 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 533 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 534 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 535 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 536 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 537 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 538 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 539 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 542 | #ifdef MS_WINDOWS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 543 | static PyObject * |
| 544 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 545 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 546 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 547 | } |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 548 | #endif /* MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 549 | |
| 550 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 551 | static PyObject * |
| 552 | posix_error_with_allocated_filename(char* name) |
| 553 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 554 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 555 | PyMem_Free(name); |
| 556 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 559 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 560 | static PyObject * |
| 561 | win32_error(char* function, char* filename) |
| 562 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 563 | /* XXX We should pass the function name along in the future. |
| 564 | (_winreg.c also wants to pass the function name.) |
| 565 | This would however require an additional param to the |
| 566 | Windows error object, which is non-trivial. |
| 567 | */ |
| 568 | errno = GetLastError(); |
| 569 | if (filename) |
| 570 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 571 | else |
| 572 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 573 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 574 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 575 | static PyObject * |
| 576 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 577 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 578 | /* XXX - see win32_error for comments on 'function' */ |
| 579 | errno = GetLastError(); |
| 580 | if (filename) |
| 581 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 582 | else |
| 583 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 586 | static int |
Hirokazu Yamamoto | a0fdd72 | 2008-08-17 09:19:52 +0000 | [diff] [blame] | 587 | convert_to_unicode(PyObject **param) |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 588 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 589 | if (PyUnicode_CheckExact(*param)) |
| 590 | Py_INCREF(*param); |
| 591 | else if (PyUnicode_Check(*param)) |
| 592 | /* For a Unicode subtype that's not a Unicode object, |
| 593 | return a true Unicode object with the same data. */ |
| 594 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), |
| 595 | PyUnicode_GET_SIZE(*param)); |
| 596 | else |
| 597 | *param = PyUnicode_FromEncodedObject(*param, |
| 598 | Py_FileSystemDefaultEncoding, |
| 599 | "strict"); |
| 600 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 603 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 604 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 605 | #if defined(PYOS_OS2) |
| 606 | /********************************************************************** |
| 607 | * Helper Function to Trim and Format OS/2 Messages |
| 608 | **********************************************************************/ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 609 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 610 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 611 | { |
| 612 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 613 | |
| 614 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
Victor Stinner | 862490a | 2010-05-06 00:03:44 +0000 | [diff] [blame] | 615 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 616 | |
Victor Stinner | 862490a | 2010-05-06 00:03:44 +0000 | [diff] [blame] | 617 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
| 618 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /* Add Optional Reason Text */ |
| 622 | if (reason) { |
| 623 | strcat(msgbuf, " : "); |
| 624 | strcat(msgbuf, reason); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /********************************************************************** |
| 629 | * Decode an OS/2 Operating System Error Code |
| 630 | * |
| 631 | * A convenience function to lookup an OS/2 error code and return a |
| 632 | * text message we can use to raise a Python exception. |
| 633 | * |
| 634 | * Notes: |
| 635 | * The messages for errors returned from the OS/2 kernel reside in |
| 636 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 637 | * |
| 638 | **********************************************************************/ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 639 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 640 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 641 | { |
| 642 | APIRET rc; |
| 643 | ULONG msglen; |
| 644 | |
| 645 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 646 | Py_BEGIN_ALLOW_THREADS |
| 647 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 648 | errorcode, "oso001.msg", &msglen); |
| 649 | Py_END_ALLOW_THREADS |
| 650 | |
| 651 | if (rc == NO_ERROR) |
| 652 | os2_formatmsg(msgbuf, msglen, reason); |
| 653 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 654 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 655 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 656 | |
| 657 | return msgbuf; |
| 658 | } |
| 659 | |
| 660 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 661 | errors are not in a global variable e.g. 'errno' nor are |
| 662 | they congruent with posix error numbers. */ |
| 663 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 664 | static PyObject * |
| 665 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 666 | { |
| 667 | char text[1024]; |
| 668 | PyObject *v; |
| 669 | |
| 670 | os2_strerror(text, sizeof(text), code, ""); |
| 671 | |
| 672 | v = Py_BuildValue("(is)", code, text); |
| 673 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 674 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 675 | Py_DECREF(v); |
| 676 | } |
| 677 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 678 | } |
| 679 | |
| 680 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 681 | |
| 682 | /* POSIX generic methods */ |
| 683 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 684 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 685 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 686 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 687 | int fd; |
| 688 | int res; |
| 689 | fd = PyObject_AsFileDescriptor(fdobj); |
| 690 | if (fd < 0) |
| 691 | return NULL; |
| 692 | if (!_PyVerify_fd(fd)) |
| 693 | return posix_error(); |
| 694 | Py_BEGIN_ALLOW_THREADS |
| 695 | res = (*func)(fd); |
| 696 | Py_END_ALLOW_THREADS |
| 697 | if (res < 0) |
| 698 | return posix_error(); |
| 699 | Py_INCREF(Py_None); |
| 700 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 701 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 702 | |
| 703 | static PyObject * |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 704 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 705 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 706 | char *path1 = NULL; |
| 707 | int res; |
| 708 | if (!PyArg_ParseTuple(args, format, |
| 709 | Py_FileSystemDefaultEncoding, &path1)) |
| 710 | return NULL; |
| 711 | Py_BEGIN_ALLOW_THREADS |
| 712 | res = (*func)(path1); |
| 713 | Py_END_ALLOW_THREADS |
| 714 | if (res < 0) |
| 715 | return posix_error_with_allocated_filename(path1); |
| 716 | PyMem_Free(path1); |
| 717 | Py_INCREF(Py_None); |
| 718 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 721 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 722 | posix_2str(PyObject *args, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 723 | char *format, |
| 724 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 725 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 726 | char *path1 = NULL, *path2 = NULL; |
| 727 | int res; |
| 728 | if (!PyArg_ParseTuple(args, format, |
| 729 | Py_FileSystemDefaultEncoding, &path1, |
| 730 | Py_FileSystemDefaultEncoding, &path2)) |
| 731 | return NULL; |
| 732 | Py_BEGIN_ALLOW_THREADS |
| 733 | res = (*func)(path1, path2); |
| 734 | Py_END_ALLOW_THREADS |
| 735 | PyMem_Free(path1); |
| 736 | PyMem_Free(path2); |
| 737 | if (res != 0) |
| 738 | /* XXX how to report both path1 and path2??? */ |
| 739 | return posix_error(); |
| 740 | Py_INCREF(Py_None); |
| 741 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 744 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 745 | static PyObject* |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 746 | win32_1str(PyObject* args, char* func, |
| 747 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 748 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 749 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 750 | PyObject *uni; |
| 751 | char *ansi; |
| 752 | BOOL result; |
Hirokazu Yamamoto | a3c5609 | 2009-06-28 10:23:00 +0000 | [diff] [blame] | 753 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 754 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 755 | PyErr_Clear(); |
| 756 | else { |
| 757 | Py_BEGIN_ALLOW_THREADS |
| 758 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 759 | Py_END_ALLOW_THREADS |
| 760 | if (!result) |
| 761 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 762 | Py_INCREF(Py_None); |
| 763 | return Py_None; |
| 764 | } |
| 765 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 766 | return NULL; |
| 767 | Py_BEGIN_ALLOW_THREADS |
| 768 | result = funcA(ansi); |
| 769 | Py_END_ALLOW_THREADS |
| 770 | if (!result) |
| 771 | return win32_error(func, ansi); |
| 772 | Py_INCREF(Py_None); |
| 773 | return Py_None; |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 774 | |
| 775 | } |
| 776 | |
| 777 | /* This is a reimplementation of the C library's chdir function, |
| 778 | but one that produces Win32 errors instead of DOS error codes. |
| 779 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 780 | it also needs to set "magic" environment variables indicating |
| 781 | the per-drive current directory, which are of the form =<drive>: */ |
Hirokazu Yamamoto | 6137640 | 2008-10-16 06:25:25 +0000 | [diff] [blame] | 782 | static BOOL __stdcall |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 783 | win32_chdir(LPCSTR path) |
| 784 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 785 | char new_path[MAX_PATH+1]; |
| 786 | int result; |
| 787 | char env[4] = "=x:"; |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 788 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 789 | if(!SetCurrentDirectoryA(path)) |
| 790 | return FALSE; |
| 791 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 792 | if (!result) |
| 793 | return FALSE; |
| 794 | /* In the ANSI API, there should not be any paths longer |
| 795 | than MAX_PATH. */ |
| 796 | assert(result <= MAX_PATH+1); |
| 797 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 798 | strncmp(new_path, "//", 2) == 0) |
| 799 | /* UNC path, nothing to do. */ |
| 800 | return TRUE; |
| 801 | env[1] = new_path[0]; |
| 802 | return SetEnvironmentVariableA(env, new_path); |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* The Unicode version differs from the ANSI version |
| 806 | since the current directory might exceed MAX_PATH characters */ |
Hirokazu Yamamoto | 6137640 | 2008-10-16 06:25:25 +0000 | [diff] [blame] | 807 | static BOOL __stdcall |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 808 | win32_wchdir(LPCWSTR path) |
| 809 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 810 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 811 | int result; |
| 812 | wchar_t env[4] = L"=x:"; |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 813 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 814 | if(!SetCurrentDirectoryW(path)) |
| 815 | return FALSE; |
| 816 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 817 | if (!result) |
| 818 | return FALSE; |
| 819 | if (result > MAX_PATH+1) { |
| 820 | new_path = malloc(result * sizeof(wchar_t)); |
| 821 | if (!new_path) { |
| 822 | SetLastError(ERROR_OUTOFMEMORY); |
| 823 | return FALSE; |
| 824 | } |
| 825 | result = GetCurrentDirectoryW(result, new_path); |
| 826 | if (!result) { |
| 827 | free(new_path); |
| 828 | return FALSE; |
| 829 | } |
| 830 | } |
| 831 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 832 | wcsncmp(new_path, L"//", 2) == 0) |
| 833 | /* UNC path, nothing to do. */ |
| 834 | return TRUE; |
| 835 | env[1] = new_path[0]; |
| 836 | result = SetEnvironmentVariableW(env, new_path); |
| 837 | if (new_path != _new_path) |
| 838 | free(new_path); |
| 839 | return result; |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 840 | } |
| 841 | #endif |
| 842 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 843 | #ifdef MS_WINDOWS |
| 844 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 845 | - time stamps are restricted to second resolution |
| 846 | - file modification times suffer from forth-and-back conversions between |
| 847 | UTC and local time |
| 848 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 849 | */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 850 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 851 | |
| 852 | struct win32_stat{ |
| 853 | int st_dev; |
| 854 | __int64 st_ino; |
| 855 | unsigned short st_mode; |
| 856 | int st_nlink; |
| 857 | int st_uid; |
| 858 | int st_gid; |
| 859 | int st_rdev; |
| 860 | __int64 st_size; |
| 861 | int st_atime; |
| 862 | int st_atime_nsec; |
| 863 | int st_mtime; |
| 864 | int st_mtime_nsec; |
| 865 | int st_ctime; |
| 866 | int st_ctime_nsec; |
| 867 | }; |
| 868 | |
| 869 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 870 | |
| 871 | static void |
| 872 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 873 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 874 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 875 | /* Cannot simply cast and dereference in_ptr, |
| 876 | since it might not be aligned properly */ |
| 877 | __int64 in; |
| 878 | memcpy(&in, in_ptr, sizeof(in)); |
| 879 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 880 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 881 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 884 | static void |
| 885 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) |
| 886 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 887 | /* XXX endianness */ |
| 888 | __int64 out; |
| 889 | out = time_in + secs_between_epochs; |
| 890 | out = out * 10000000 + nsec_in / 100; |
| 891 | memcpy(out_ptr, &out, sizeof(out)); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 894 | /* Below, we *know* that ugo+r is 0444 */ |
| 895 | #if _S_IREAD != 0400 |
| 896 | #error Unsupported C library |
| 897 | #endif |
| 898 | static int |
| 899 | attributes_to_mode(DWORD attr) |
| 900 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 901 | int m = 0; |
| 902 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 903 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 904 | else |
| 905 | m |= _S_IFREG; |
| 906 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 907 | m |= 0444; |
| 908 | else |
| 909 | m |= 0666; |
| 910 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | static int |
| 914 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 915 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 916 | memset(result, 0, sizeof(*result)); |
| 917 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 918 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 919 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 920 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 921 | 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] | 922 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 923 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Martin v. Löwis | 3bf573f | 2007-04-04 18:30:36 +0000 | [diff] [blame] | 926 | static BOOL |
| 927 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 928 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 929 | HANDLE hFindFile; |
| 930 | WIN32_FIND_DATAA FileData; |
| 931 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 932 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 933 | return FALSE; |
| 934 | FindClose(hFindFile); |
| 935 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 936 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 937 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 938 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 939 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 940 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 941 | return TRUE; |
Martin v. Löwis | 3bf573f | 2007-04-04 18:30:36 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | static BOOL |
| 945 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 946 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 947 | HANDLE hFindFile; |
| 948 | WIN32_FIND_DATAW FileData; |
| 949 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 950 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 951 | return FALSE; |
| 952 | FindClose(hFindFile); |
| 953 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 954 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 955 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 956 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 957 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 958 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 959 | return TRUE; |
Martin v. Löwis | 3bf573f | 2007-04-04 18:30:36 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 962 | static int |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 963 | win32_stat(const char* path, struct win32_stat *result) |
| 964 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 965 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 966 | int code; |
| 967 | char *dot; |
| 968 | if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
| 969 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 970 | /* Protocol violation: we explicitly clear errno, instead of |
| 971 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 972 | errno = 0; |
| 973 | return -1; |
| 974 | } else { |
| 975 | /* Could not get attributes on open file. Fall back to |
| 976 | reading the directory. */ |
| 977 | if (!attributes_from_dir(path, &info)) { |
| 978 | /* Very strange. This should not fail now */ |
| 979 | errno = 0; |
| 980 | return -1; |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | code = attribute_data_to_stat(&info, result); |
| 985 | if (code != 0) |
| 986 | return code; |
| 987 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 988 | dot = strrchr(path, '.'); |
| 989 | if (dot) { |
| 990 | if (stricmp(dot, ".bat") == 0 || |
| 991 | stricmp(dot, ".cmd") == 0 || |
| 992 | stricmp(dot, ".exe") == 0 || |
| 993 | stricmp(dot, ".com") == 0) |
| 994 | result->st_mode |= 0111; |
| 995 | } |
| 996 | return code; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 999 | static int |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1000 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 1001 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1002 | int code; |
| 1003 | const wchar_t *dot; |
| 1004 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1005 | if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
| 1006 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1007 | /* Protocol violation: we explicitly clear errno, instead of |
| 1008 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1009 | errno = 0; |
| 1010 | return -1; |
| 1011 | } else { |
| 1012 | /* Could not get attributes on open file. Fall back to |
| 1013 | reading the directory. */ |
| 1014 | if (!attributes_from_dir_w(path, &info)) { |
| 1015 | /* Very strange. This should not fail now */ |
| 1016 | errno = 0; |
| 1017 | return -1; |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | code = attribute_data_to_stat(&info, result); |
| 1022 | if (code < 0) |
| 1023 | return code; |
| 1024 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 1025 | dot = wcsrchr(path, '.'); |
| 1026 | if (dot) { |
| 1027 | if (_wcsicmp(dot, L".bat") == 0 || |
| 1028 | _wcsicmp(dot, L".cmd") == 0 || |
| 1029 | _wcsicmp(dot, L".exe") == 0 || |
| 1030 | _wcsicmp(dot, L".com") == 0) |
| 1031 | result->st_mode |= 0111; |
| 1032 | } |
| 1033 | return code; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | static int |
| 1037 | win32_fstat(int file_number, struct win32_stat *result) |
| 1038 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1039 | BY_HANDLE_FILE_INFORMATION info; |
| 1040 | HANDLE h; |
| 1041 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1042 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1043 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1044 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1045 | /* Protocol violation: we explicitly clear errno, instead of |
| 1046 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1047 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1048 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1049 | if (h == INVALID_HANDLE_VALUE) { |
| 1050 | /* This is really a C library error (invalid file handle). |
| 1051 | We set the Win32 error to the closes one matching. */ |
| 1052 | SetLastError(ERROR_INVALID_HANDLE); |
| 1053 | return -1; |
| 1054 | } |
| 1055 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1056 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1057 | type = GetFileType(h); |
| 1058 | if (type == FILE_TYPE_UNKNOWN) { |
| 1059 | DWORD error = GetLastError(); |
| 1060 | if (error != 0) { |
| 1061 | return -1; |
| 1062 | } |
| 1063 | /* else: valid but unknown file */ |
| 1064 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1065 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1066 | if (type != FILE_TYPE_DISK) { |
| 1067 | if (type == FILE_TYPE_CHAR) |
| 1068 | result->st_mode = _S_IFCHR; |
| 1069 | else if (type == FILE_TYPE_PIPE) |
| 1070 | result->st_mode = _S_IFIFO; |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | if (!GetFileInformationByHandle(h, &info)) { |
| 1075 | return -1; |
| 1076 | } |
| 1077 | |
| 1078 | /* similar to stat() */ |
| 1079 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1080 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1081 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1082 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1083 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1084 | /* specific to fstat() */ |
| 1085 | result->st_nlink = info.nNumberOfLinks; |
| 1086 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1087 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | #endif /* MS_WINDOWS */ |
| 1091 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1092 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1093 | "stat_result: Result from stat or lstat.\n\n\ |
| 1094 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1095 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1096 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1097 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1098 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1099 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1100 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1101 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1102 | |
| 1103 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1104 | {"st_mode", "protection bits"}, |
| 1105 | {"st_ino", "inode"}, |
| 1106 | {"st_dev", "device"}, |
| 1107 | {"st_nlink", "number of hard links"}, |
| 1108 | {"st_uid", "user ID of owner"}, |
| 1109 | {"st_gid", "group ID of owner"}, |
| 1110 | {"st_size", "total size, in bytes"}, |
| 1111 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1112 | {NULL, "integer time of last access"}, |
| 1113 | {NULL, "integer time of last modification"}, |
| 1114 | {NULL, "integer time of last change"}, |
| 1115 | {"st_atime", "time of last access"}, |
| 1116 | {"st_mtime", "time of last modification"}, |
| 1117 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1118 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1119 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1120 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1121 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1122 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1123 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1124 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1125 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1126 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1127 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1128 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1129 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1130 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1131 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1132 | #endif |
| 1133 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1134 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1135 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1136 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1137 | }; |
| 1138 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1139 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1140 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1141 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1142 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1143 | #endif |
| 1144 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1145 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1146 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1147 | #else |
| 1148 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1149 | #endif |
| 1150 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1151 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1152 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1153 | #else |
| 1154 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1155 | #endif |
| 1156 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1157 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1158 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1159 | #else |
| 1160 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1161 | #endif |
| 1162 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1163 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1164 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1165 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1166 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1167 | #endif |
| 1168 | |
| 1169 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1170 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1171 | #else |
| 1172 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1173 | #endif |
| 1174 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1175 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1176 | "stat_result", /* name */ |
| 1177 | stat_result__doc__, /* doc */ |
| 1178 | stat_result_fields, |
| 1179 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1180 | }; |
| 1181 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1182 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1183 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1184 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1185 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1186 | 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] | 1187 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1188 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1189 | |
| 1190 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1191 | {"f_bsize", }, |
| 1192 | {"f_frsize", }, |
| 1193 | {"f_blocks", }, |
| 1194 | {"f_bfree", }, |
| 1195 | {"f_bavail", }, |
| 1196 | {"f_files", }, |
| 1197 | {"f_ffree", }, |
| 1198 | {"f_favail", }, |
| 1199 | {"f_flag", }, |
| 1200 | {"f_namemax",}, |
| 1201 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1202 | }; |
| 1203 | |
| 1204 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1205 | "statvfs_result", /* name */ |
| 1206 | statvfs_result__doc__, /* doc */ |
| 1207 | statvfs_result_fields, |
| 1208 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1209 | }; |
| 1210 | |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 1211 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1212 | static PyTypeObject StatResultType; |
| 1213 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1214 | static newfunc structseq_new; |
| 1215 | |
| 1216 | static PyObject * |
| 1217 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1218 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1219 | PyStructSequence *result; |
| 1220 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1221 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1222 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1223 | if (!result) |
| 1224 | return NULL; |
| 1225 | /* If we have been initialized from a tuple, |
| 1226 | st_?time might be set to None. Initialize it |
| 1227 | from the int slots. */ |
| 1228 | for (i = 7; i <= 9; i++) { |
| 1229 | if (result->ob_item[i+3] == Py_None) { |
| 1230 | Py_DECREF(Py_None); |
| 1231 | Py_INCREF(result->ob_item[i]); |
| 1232 | result->ob_item[i+3] = result->ob_item[i]; |
| 1233 | } |
| 1234 | } |
| 1235 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1241 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1242 | |
| 1243 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1244 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1245 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1246 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1247 | future calls return ints. \n\ |
| 1248 | If newval is omitted, return the current setting.\n"); |
| 1249 | |
| 1250 | static PyObject* |
| 1251 | stat_float_times(PyObject* self, PyObject *args) |
| 1252 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1253 | int newval = -1; |
| 1254 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1255 | return NULL; |
| 1256 | if (newval == -1) |
| 1257 | /* Return old value */ |
| 1258 | return PyBool_FromLong(_stat_float_times); |
| 1259 | _stat_float_times = newval; |
| 1260 | Py_INCREF(Py_None); |
| 1261 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1262 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1263 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1264 | static void |
| 1265 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1266 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1267 | PyObject *fval,*ival; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1268 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1269 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1270 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1271 | ival = PyInt_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1272 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1273 | if (!ival) |
| 1274 | return; |
| 1275 | if (_stat_float_times) { |
| 1276 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1277 | } else { |
| 1278 | fval = ival; |
| 1279 | Py_INCREF(fval); |
| 1280 | } |
| 1281 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1282 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1285 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1286 | (used by posix_stat() and posix_fstat()) */ |
| 1287 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1288 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1289 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1290 | unsigned long ansec, mnsec, cnsec; |
| 1291 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1292 | if (v == NULL) |
| 1293 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1294 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1295 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1296 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1297 | PyStructSequence_SET_ITEM(v, 1, |
| 1298 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1299 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1300 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1301 | #endif |
| 1302 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1303 | PyStructSequence_SET_ITEM(v, 2, |
| 1304 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1305 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1306 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1307 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1308 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); |
| 1309 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); |
| 1310 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1311 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1312 | PyStructSequence_SET_ITEM(v, 6, |
| 1313 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1314 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1315 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1316 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1317 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1318 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1319 | ansec = st->st_atim.tv_nsec; |
| 1320 | mnsec = st->st_mtim.tv_nsec; |
| 1321 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1322 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1323 | ansec = st->st_atimespec.tv_nsec; |
| 1324 | mnsec = st->st_mtimespec.tv_nsec; |
| 1325 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1326 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1327 | ansec = st->st_atime_nsec; |
| 1328 | mnsec = st->st_mtime_nsec; |
| 1329 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1330 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1331 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1332 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1333 | fill_time(v, 7, st->st_atime, ansec); |
| 1334 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1335 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1336 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1337 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1338 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1339 | PyInt_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1340 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1341 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1342 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1343 | PyInt_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1344 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1345 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1346 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1347 | PyInt_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1348 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1349 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1350 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 1351 | PyInt_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1352 | #endif |
| 1353 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1354 | { |
| 1355 | PyObject *val; |
| 1356 | unsigned long bsec,bnsec; |
| 1357 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1358 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1359 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1360 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1361 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1362 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1363 | if (_stat_float_times) { |
| 1364 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1365 | } else { |
| 1366 | val = PyInt_FromLong((long)bsec); |
| 1367 | } |
| 1368 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1369 | val); |
| 1370 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1371 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1372 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1373 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 1374 | PyInt_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1375 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1376 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1377 | if (PyErr_Occurred()) { |
| 1378 | Py_DECREF(v); |
| 1379 | return NULL; |
| 1380 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1381 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1382 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1385 | #ifdef MS_WINDOWS |
| 1386 | |
| 1387 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1388 | where / can be used in place of \ and the trailing slash is optional. |
| 1389 | Both SERVER and SHARE must have at least one character. |
| 1390 | */ |
| 1391 | |
| 1392 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1393 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Kristján Valur Jónsson | dbeaa69 | 2006-06-09 16:28:01 +0000 | [diff] [blame] | 1394 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1395 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Kristján Valur Jónsson | dbeaa69 | 2006-06-09 16:28:01 +0000 | [diff] [blame] | 1396 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1397 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1398 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1399 | IsUNCRootA(char *path, int pathlen) |
| 1400 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1401 | #define ISSLASH ISSLASHA |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1402 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1403 | int i, share; |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1404 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1405 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1406 | /* minimum UNCRoot is \\x\y */ |
| 1407 | return FALSE; |
| 1408 | for (i = 2; i < pathlen ; i++) |
| 1409 | if (ISSLASH(path[i])) break; |
| 1410 | if (i == 2 || i == pathlen) |
| 1411 | /* do not allow \\\SHARE or \\SERVER */ |
| 1412 | return FALSE; |
| 1413 | share = i+1; |
| 1414 | for (i = share; i < pathlen; i++) |
| 1415 | if (ISSLASH(path[i])) break; |
| 1416 | return (i != share && (i == pathlen || i == pathlen-1)); |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1417 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1418 | #undef ISSLASH |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1421 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1422 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1423 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1424 | #define ISSLASH ISSLASHW |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1425 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1426 | int i, share; |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1427 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1428 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1429 | /* minimum UNCRoot is \\x\y */ |
| 1430 | return FALSE; |
| 1431 | for (i = 2; i < pathlen ; i++) |
| 1432 | if (ISSLASH(path[i])) break; |
| 1433 | if (i == 2 || i == pathlen) |
| 1434 | /* do not allow \\\SHARE or \\SERVER */ |
| 1435 | return FALSE; |
| 1436 | share = i+1; |
| 1437 | for (i = share; i < pathlen; i++) |
| 1438 | if (ISSLASH(path[i])) break; |
| 1439 | return (i != share && (i == pathlen || i == pathlen-1)); |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1440 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1441 | #undef ISSLASH |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1442 | } |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1443 | #endif /* MS_WINDOWS */ |
| 1444 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1445 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1446 | posix_do_stat(PyObject *self, PyObject *args, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1447 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1448 | #ifdef __VMS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1449 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1450 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1451 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1452 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1453 | char *wformat, |
| 1454 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1455 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1456 | STRUCT_STAT st; |
| 1457 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1458 | char *pathfree = NULL; /* this memory must be free'd */ |
| 1459 | int res; |
| 1460 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1461 | |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 1462 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1463 | PyUnicodeObject *po; |
| 1464 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 1465 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1466 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1467 | Py_BEGIN_ALLOW_THREADS |
| 1468 | /* PyUnicode_AS_UNICODE result OK without |
| 1469 | thread lock as it is a simple dereference. */ |
| 1470 | res = wstatfunc(wpath, &st); |
| 1471 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1472 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1473 | if (res != 0) |
| 1474 | return win32_error_unicode("stat", wpath); |
| 1475 | return _pystat_fromstructstat(&st); |
| 1476 | } |
| 1477 | /* Drop the argument parsing error as narrow strings |
| 1478 | are also valid. */ |
| 1479 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1480 | #endif |
| 1481 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1482 | if (!PyArg_ParseTuple(args, format, |
| 1483 | Py_FileSystemDefaultEncoding, &path)) |
| 1484 | return NULL; |
| 1485 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1486 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1487 | Py_BEGIN_ALLOW_THREADS |
| 1488 | res = (*statfunc)(path, &st); |
| 1489 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1490 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1491 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1492 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1493 | result = win32_error("stat", pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1494 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1495 | result = posix_error_with_filename(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1496 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1497 | } |
| 1498 | else |
| 1499 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1500 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1501 | PyMem_Free(pathfree); |
| 1502 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1505 | /* POSIX methods */ |
| 1506 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1507 | PyDoc_STRVAR(posix_access__doc__, |
Georg Brandl | 7a28447 | 2007-01-27 19:38:50 +0000 | [diff] [blame] | 1508 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1509 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1510 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1511 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1512 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1513 | 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] | 1514 | |
| 1515 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1516 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1517 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1518 | char *path; |
| 1519 | int mode; |
| 1520 | |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 1521 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1522 | DWORD attr; |
| 1523 | PyUnicodeObject *po; |
| 1524 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1525 | Py_BEGIN_ALLOW_THREADS |
| 1526 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1527 | it is a simple dereference. */ |
| 1528 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1529 | Py_END_ALLOW_THREADS |
| 1530 | goto finish; |
| 1531 | } |
| 1532 | /* Drop the argument parsing error as narrow strings |
| 1533 | are also valid. */ |
| 1534 | PyErr_Clear(); |
| 1535 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1536 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 1537 | return NULL; |
| 1538 | Py_BEGIN_ALLOW_THREADS |
| 1539 | attr = GetFileAttributesA(path); |
| 1540 | Py_END_ALLOW_THREADS |
| 1541 | PyMem_Free(path); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1542 | finish: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1543 | if (attr == 0xFFFFFFFF) |
| 1544 | /* File does not exist, or cannot read attributes */ |
| 1545 | return PyBool_FromLong(0); |
| 1546 | /* Access is possible if either write access wasn't requested, or |
| 1547 | the file isn't read-only, or if it's a directory, as there are |
| 1548 | no read-only directories on Windows. */ |
| 1549 | return PyBool_FromLong(!(mode & 2) |
| 1550 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1551 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 1552 | #else /* MS_WINDOWS */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1553 | int res; |
| 1554 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1555 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 1556 | return NULL; |
| 1557 | Py_BEGIN_ALLOW_THREADS |
| 1558 | res = access(path, mode); |
| 1559 | Py_END_ALLOW_THREADS |
| 1560 | PyMem_Free(path); |
| 1561 | return PyBool_FromLong(res == 0); |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 1562 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1565 | #ifndef F_OK |
| 1566 | #define F_OK 0 |
| 1567 | #endif |
| 1568 | #ifndef R_OK |
| 1569 | #define R_OK 4 |
| 1570 | #endif |
| 1571 | #ifndef W_OK |
| 1572 | #define W_OK 2 |
| 1573 | #endif |
| 1574 | #ifndef X_OK |
| 1575 | #define X_OK 1 |
| 1576 | #endif |
| 1577 | |
| 1578 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1579 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1580 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1581 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1582 | |
| 1583 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1584 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1585 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1586 | int id; |
| 1587 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1588 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1589 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 1590 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1591 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1592 | #if defined(__VMS) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1593 | /* file descriptor 0 only, the default input device (stdin) */ |
| 1594 | if (id == 0) { |
| 1595 | ret = ttyname(); |
| 1596 | } |
| 1597 | else { |
| 1598 | ret = NULL; |
| 1599 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1600 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1601 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1602 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1603 | if (ret == NULL) |
| 1604 | return posix_error(); |
| 1605 | return PyString_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1606 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1607 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1608 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1609 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1610 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1611 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1612 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1613 | |
| 1614 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1615 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1616 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1617 | char *ret; |
| 1618 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1619 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1620 | #ifdef USE_CTERMID_R |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1621 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1622 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1623 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1624 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1625 | if (ret == NULL) |
| 1626 | return posix_error(); |
| 1627 | return PyString_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1628 | } |
| 1629 | #endif |
| 1630 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1631 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1632 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1633 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1634 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1635 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1636 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1637 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1638 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1639 | 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] | 1640 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1641 | return posix_1str(args, "et:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1642 | #elif defined(__VMS) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1643 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1644 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1645 | return posix_1str(args, "et:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1646 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1649 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1650 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1651 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1652 | 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] | 1653 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1654 | |
| 1655 | static PyObject * |
| 1656 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1657 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1658 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1659 | } |
| 1660 | #endif /* HAVE_FCHDIR */ |
| 1661 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1662 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1663 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1664 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1665 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1666 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1667 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1668 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1669 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1670 | char *path = NULL; |
| 1671 | int i; |
| 1672 | int res; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 1673 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1674 | DWORD attr; |
| 1675 | PyUnicodeObject *po; |
| 1676 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1677 | Py_BEGIN_ALLOW_THREADS |
| 1678 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1679 | if (attr != 0xFFFFFFFF) { |
| 1680 | if (i & _S_IWRITE) |
| 1681 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1682 | else |
| 1683 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1684 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1685 | } |
| 1686 | else |
| 1687 | res = 0; |
| 1688 | Py_END_ALLOW_THREADS |
| 1689 | if (!res) |
| 1690 | return win32_error_unicode("chmod", |
| 1691 | PyUnicode_AS_UNICODE(po)); |
| 1692 | Py_INCREF(Py_None); |
| 1693 | return Py_None; |
| 1694 | } |
| 1695 | /* Drop the argument parsing error as narrow strings |
| 1696 | are also valid. */ |
| 1697 | PyErr_Clear(); |
Hirokazu Yamamoto | a3c5609 | 2009-06-28 10:23:00 +0000 | [diff] [blame] | 1698 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1699 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
| 1700 | &path, &i)) |
| 1701 | return NULL; |
| 1702 | Py_BEGIN_ALLOW_THREADS |
| 1703 | attr = GetFileAttributesA(path); |
| 1704 | if (attr != 0xFFFFFFFF) { |
| 1705 | if (i & _S_IWRITE) |
| 1706 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1707 | else |
| 1708 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1709 | res = SetFileAttributesA(path, attr); |
| 1710 | } |
| 1711 | else |
| 1712 | res = 0; |
| 1713 | Py_END_ALLOW_THREADS |
| 1714 | if (!res) { |
| 1715 | win32_error("chmod", path); |
| 1716 | PyMem_Free(path); |
| 1717 | return NULL; |
| 1718 | } |
| 1719 | PyMem_Free(path); |
| 1720 | Py_INCREF(Py_None); |
| 1721 | return Py_None; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 1722 | #else /* MS_WINDOWS */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1723 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
| 1724 | &path, &i)) |
| 1725 | return NULL; |
| 1726 | Py_BEGIN_ALLOW_THREADS |
| 1727 | res = chmod(path, i); |
| 1728 | Py_END_ALLOW_THREADS |
| 1729 | if (res < 0) |
| 1730 | return posix_error_with_allocated_filename(path); |
| 1731 | PyMem_Free(path); |
| 1732 | Py_INCREF(Py_None); |
| 1733 | return Py_None; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1734 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 1737 | #ifdef HAVE_FCHMOD |
| 1738 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 1739 | "fchmod(fd, mode)\n\n\ |
| 1740 | Change the access permissions of the file given by file\n\ |
| 1741 | descriptor fd."); |
| 1742 | |
| 1743 | static PyObject * |
| 1744 | posix_fchmod(PyObject *self, PyObject *args) |
| 1745 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1746 | int fd, mode, res; |
| 1747 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 1748 | return NULL; |
| 1749 | Py_BEGIN_ALLOW_THREADS |
| 1750 | res = fchmod(fd, mode); |
| 1751 | Py_END_ALLOW_THREADS |
| 1752 | if (res < 0) |
| 1753 | return posix_error(); |
| 1754 | Py_RETURN_NONE; |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 1755 | } |
| 1756 | #endif /* HAVE_FCHMOD */ |
| 1757 | |
| 1758 | #ifdef HAVE_LCHMOD |
| 1759 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 1760 | "lchmod(path, mode)\n\n\ |
| 1761 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 1762 | affects the link itself rather than the target."); |
| 1763 | |
| 1764 | static PyObject * |
| 1765 | posix_lchmod(PyObject *self, PyObject *args) |
| 1766 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1767 | char *path = NULL; |
| 1768 | int i; |
| 1769 | int res; |
| 1770 | if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding, |
| 1771 | &path, &i)) |
| 1772 | return NULL; |
| 1773 | Py_BEGIN_ALLOW_THREADS |
| 1774 | res = lchmod(path, i); |
| 1775 | Py_END_ALLOW_THREADS |
| 1776 | if (res < 0) |
| 1777 | return posix_error_with_allocated_filename(path); |
| 1778 | PyMem_Free(path); |
| 1779 | Py_RETURN_NONE; |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 1780 | } |
| 1781 | #endif /* HAVE_LCHMOD */ |
| 1782 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1783 | |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 1784 | #ifdef HAVE_CHFLAGS |
| 1785 | PyDoc_STRVAR(posix_chflags__doc__, |
| 1786 | "chflags(path, flags)\n\n\ |
| 1787 | Set file flags."); |
| 1788 | |
| 1789 | static PyObject * |
| 1790 | posix_chflags(PyObject *self, PyObject *args) |
| 1791 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1792 | char *path; |
| 1793 | unsigned long flags; |
| 1794 | int res; |
| 1795 | if (!PyArg_ParseTuple(args, "etk:chflags", |
| 1796 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1797 | return NULL; |
| 1798 | Py_BEGIN_ALLOW_THREADS |
| 1799 | res = chflags(path, flags); |
| 1800 | Py_END_ALLOW_THREADS |
| 1801 | if (res < 0) |
| 1802 | return posix_error_with_allocated_filename(path); |
| 1803 | PyMem_Free(path); |
| 1804 | Py_INCREF(Py_None); |
| 1805 | return Py_None; |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 1806 | } |
| 1807 | #endif /* HAVE_CHFLAGS */ |
| 1808 | |
| 1809 | #ifdef HAVE_LCHFLAGS |
| 1810 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 1811 | "lchflags(path, flags)\n\n\ |
| 1812 | Set file flags.\n\ |
| 1813 | This function will not follow symbolic links."); |
| 1814 | |
| 1815 | static PyObject * |
| 1816 | posix_lchflags(PyObject *self, PyObject *args) |
| 1817 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1818 | char *path; |
| 1819 | unsigned long flags; |
| 1820 | int res; |
| 1821 | if (!PyArg_ParseTuple(args, "etk:lchflags", |
| 1822 | Py_FileSystemDefaultEncoding, &path, &flags)) |
| 1823 | return NULL; |
| 1824 | Py_BEGIN_ALLOW_THREADS |
| 1825 | res = lchflags(path, flags); |
| 1826 | Py_END_ALLOW_THREADS |
| 1827 | if (res < 0) |
| 1828 | return posix_error_with_allocated_filename(path); |
| 1829 | PyMem_Free(path); |
| 1830 | Py_INCREF(Py_None); |
| 1831 | return Py_None; |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 1832 | } |
| 1833 | #endif /* HAVE_LCHFLAGS */ |
| 1834 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1835 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1836 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1837 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1838 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1839 | |
| 1840 | static PyObject * |
| 1841 | posix_chroot(PyObject *self, PyObject *args) |
| 1842 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1843 | return posix_1str(args, "et:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1844 | } |
| 1845 | #endif |
| 1846 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1847 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1848 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1849 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1850 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1851 | |
| 1852 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1853 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1854 | { |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 1855 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1856 | } |
| 1857 | #endif /* HAVE_FSYNC */ |
| 1858 | |
| 1859 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1860 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1861 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1862 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1863 | #endif |
| 1864 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1865 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1866 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1867 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1868 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1869 | |
| 1870 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1871 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1872 | { |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 1873 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1874 | } |
| 1875 | #endif /* HAVE_FDATASYNC */ |
| 1876 | |
| 1877 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1878 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1879 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1880 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1881 | 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] | 1882 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1883 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1884 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1885 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1886 | char *path = NULL; |
| 1887 | long uid, gid; |
| 1888 | int res; |
| 1889 | if (!PyArg_ParseTuple(args, "etll:chown", |
| 1890 | Py_FileSystemDefaultEncoding, &path, |
| 1891 | &uid, &gid)) |
| 1892 | return NULL; |
| 1893 | Py_BEGIN_ALLOW_THREADS |
| 1894 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1895 | Py_END_ALLOW_THREADS |
| 1896 | if (res < 0) |
| 1897 | return posix_error_with_allocated_filename(path); |
| 1898 | PyMem_Free(path); |
| 1899 | Py_INCREF(Py_None); |
| 1900 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1901 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1902 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1903 | |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 1904 | #ifdef HAVE_FCHOWN |
| 1905 | PyDoc_STRVAR(posix_fchown__doc__, |
| 1906 | "fchown(fd, uid, gid)\n\n\ |
| 1907 | Change the owner and group id of the file given by file descriptor\n\ |
| 1908 | fd to the numeric uid and gid."); |
| 1909 | |
| 1910 | static PyObject * |
| 1911 | posix_fchown(PyObject *self, PyObject *args) |
| 1912 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1913 | int fd; |
| 1914 | long uid, gid; |
| 1915 | int res; |
| 1916 | if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid)) |
| 1917 | return NULL; |
| 1918 | Py_BEGIN_ALLOW_THREADS |
| 1919 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 1920 | Py_END_ALLOW_THREADS |
| 1921 | if (res < 0) |
| 1922 | return posix_error(); |
| 1923 | Py_RETURN_NONE; |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 1924 | } |
| 1925 | #endif /* HAVE_FCHOWN */ |
| 1926 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1927 | #ifdef HAVE_LCHOWN |
| 1928 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1929 | "lchown(path, uid, gid)\n\n\ |
| 1930 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1931 | This function will not follow symbolic links."); |
| 1932 | |
| 1933 | static PyObject * |
| 1934 | posix_lchown(PyObject *self, PyObject *args) |
| 1935 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1936 | char *path = NULL; |
| 1937 | long uid, gid; |
| 1938 | int res; |
| 1939 | if (!PyArg_ParseTuple(args, "etll:lchown", |
| 1940 | Py_FileSystemDefaultEncoding, &path, |
| 1941 | &uid, &gid)) |
| 1942 | return NULL; |
| 1943 | Py_BEGIN_ALLOW_THREADS |
| 1944 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1945 | Py_END_ALLOW_THREADS |
| 1946 | if (res < 0) |
| 1947 | return posix_error_with_allocated_filename(path); |
| 1948 | PyMem_Free(path); |
| 1949 | Py_INCREF(Py_None); |
| 1950 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1951 | } |
| 1952 | #endif /* HAVE_LCHOWN */ |
| 1953 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1954 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1955 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1956 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1957 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1958 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1959 | |
Stefan Krah | 182ae64 | 2010-07-13 19:17:08 +0000 | [diff] [blame] | 1960 | #if (defined(__sun) && defined(__SVR4)) || defined(__OpenBSD__) |
| 1961 | /* Issue 9185: getcwd() returns NULL/ERANGE indefinitely. */ |
| 1962 | static PyObject * |
| 1963 | posix_getcwd(PyObject *self, PyObject *noargs) |
| 1964 | { |
| 1965 | char buf[PATH_MAX+2]; |
| 1966 | char *res; |
| 1967 | |
| 1968 | Py_BEGIN_ALLOW_THREADS |
Stefan Krah | 8cb9f03 | 2010-07-13 19:40:00 +0000 | [diff] [blame] | 1969 | res = getcwd(buf, sizeof buf); |
Stefan Krah | 182ae64 | 2010-07-13 19:17:08 +0000 | [diff] [blame] | 1970 | Py_END_ALLOW_THREADS |
| 1971 | |
| 1972 | if (res == NULL) |
| 1973 | return posix_error(); |
| 1974 | |
| 1975 | return PyString_FromString(buf); |
| 1976 | } |
| 1977 | #else |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1978 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1979 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1980 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1981 | int bufsize_incr = 1024; |
| 1982 | int bufsize = 0; |
| 1983 | char *tmpbuf = NULL; |
| 1984 | char *res = NULL; |
| 1985 | PyObject *dynamic_return; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1986 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1987 | Py_BEGIN_ALLOW_THREADS |
| 1988 | do { |
| 1989 | bufsize = bufsize + bufsize_incr; |
| 1990 | tmpbuf = malloc(bufsize); |
| 1991 | if (tmpbuf == NULL) { |
| 1992 | break; |
| 1993 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1994 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1995 | res = _getcwd2(tmpbuf, bufsize); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1996 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 1997 | res = getcwd(tmpbuf, bufsize); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1998 | #endif |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 1999 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2000 | if (res == NULL) { |
| 2001 | free(tmpbuf); |
| 2002 | } |
| 2003 | } while ((res == NULL) && (errno == ERANGE)); |
| 2004 | Py_END_ALLOW_THREADS |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 2005 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2006 | if (res == NULL) |
| 2007 | return posix_error(); |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 2008 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2009 | dynamic_return = PyString_FromString(tmpbuf); |
| 2010 | free(tmpbuf); |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 2011 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2012 | return dynamic_return; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2013 | } |
Stefan Krah | 182ae64 | 2010-07-13 19:17:08 +0000 | [diff] [blame] | 2014 | #endif /* getcwd() NULL/ERANGE workaround. */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2015 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 2016 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2017 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 2018 | "getcwdu() -> path\n\n\ |
| 2019 | Return a unicode string representing the current working directory."); |
| 2020 | |
| 2021 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2022 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2023 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2024 | char buf[1026]; |
| 2025 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2026 | |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2027 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2028 | DWORD len; |
| 2029 | wchar_t wbuf[1026]; |
| 2030 | wchar_t *wbuf2 = wbuf; |
| 2031 | PyObject *resobj; |
| 2032 | Py_BEGIN_ALLOW_THREADS |
| 2033 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2034 | /* If the buffer is large enough, len does not include the |
| 2035 | terminating \0. If the buffer is too small, len includes |
| 2036 | the space needed for the terminator. */ |
| 2037 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2038 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2039 | if (wbuf2) |
| 2040 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2041 | } |
| 2042 | Py_END_ALLOW_THREADS |
| 2043 | if (!wbuf2) { |
| 2044 | PyErr_NoMemory(); |
| 2045 | return NULL; |
| 2046 | } |
| 2047 | if (!len) { |
| 2048 | if (wbuf2 != wbuf) free(wbuf2); |
| 2049 | return win32_error("getcwdu", NULL); |
| 2050 | } |
| 2051 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2052 | if (wbuf2 != wbuf) free(wbuf2); |
| 2053 | return resobj; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2054 | #endif /* MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2055 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2056 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2057 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2058 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2059 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2060 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2061 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2062 | Py_END_ALLOW_THREADS |
| 2063 | if (res == NULL) |
| 2064 | return posix_error(); |
| 2065 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2066 | } |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2067 | #endif /* Py_USING_UNICODE */ |
| 2068 | #endif /* HAVE_GETCWD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2069 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2070 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2071 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2072 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2073 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2074 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2075 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2076 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2077 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2078 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2079 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2080 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2081 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2082 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2083 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2084 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2085 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2086 | Return a list containing the names of the entries in the directory.\n\ |
| 2087 | \n\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2088 | path: path of directory to list\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2089 | \n\ |
| 2090 | 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] | 2091 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2092 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2093 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2094 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2095 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2096 | /* XXX Should redo this putting the (now four) versions of opendir |
| 2097 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2098 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2099 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2100 | PyObject *d, *v; |
| 2101 | HANDLE hFindFile; |
| 2102 | BOOL result; |
| 2103 | WIN32_FIND_DATA FileData; |
| 2104 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 2105 | char *bufptr = namebuf; |
| 2106 | 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] | 2107 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2108 | PyObject *po; |
| 2109 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 2110 | WIN32_FIND_DATAW wFileData; |
| 2111 | Py_UNICODE *wnamebuf; |
| 2112 | /* Overallocate for \\*.*\0 */ |
| 2113 | len = PyUnicode_GET_SIZE(po); |
| 2114 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2115 | if (!wnamebuf) { |
| 2116 | PyErr_NoMemory(); |
| 2117 | return NULL; |
| 2118 | } |
| 2119 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
| 2120 | if (len > 0) { |
| 2121 | Py_UNICODE wch = wnamebuf[len-1]; |
| 2122 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2123 | wnamebuf[len++] = L'\\'; |
| 2124 | wcscpy(wnamebuf + len, L"*.*"); |
| 2125 | } |
| 2126 | if ((d = PyList_New(0)) == NULL) { |
| 2127 | free(wnamebuf); |
| 2128 | return NULL; |
| 2129 | } |
Antoine Pitrou | 8cdede4 | 2010-08-10 00:04:13 +0000 | [diff] [blame] | 2130 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2131 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | 8cdede4 | 2010-08-10 00:04:13 +0000 | [diff] [blame] | 2132 | Py_END_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2133 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2134 | int error = GetLastError(); |
| 2135 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2136 | free(wnamebuf); |
| 2137 | return d; |
| 2138 | } |
| 2139 | Py_DECREF(d); |
| 2140 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2141 | free(wnamebuf); |
| 2142 | return NULL; |
| 2143 | } |
| 2144 | do { |
| 2145 | /* Skip over . and .. */ |
| 2146 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2147 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2148 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2149 | if (v == NULL) { |
| 2150 | Py_DECREF(d); |
| 2151 | d = NULL; |
| 2152 | break; |
| 2153 | } |
| 2154 | if (PyList_Append(d, v) != 0) { |
| 2155 | Py_DECREF(v); |
| 2156 | Py_DECREF(d); |
| 2157 | d = NULL; |
| 2158 | break; |
| 2159 | } |
| 2160 | Py_DECREF(v); |
| 2161 | } |
| 2162 | Py_BEGIN_ALLOW_THREADS |
| 2163 | result = FindNextFileW(hFindFile, &wFileData); |
| 2164 | Py_END_ALLOW_THREADS |
| 2165 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2166 | it got to the end of the directory. */ |
| 2167 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2168 | Py_DECREF(d); |
| 2169 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2170 | FindClose(hFindFile); |
| 2171 | free(wnamebuf); |
| 2172 | return NULL; |
| 2173 | } |
| 2174 | } while (result == TRUE); |
Hirokazu Yamamoto | a3c5609 | 2009-06-28 10:23:00 +0000 | [diff] [blame] | 2175 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2176 | if (FindClose(hFindFile) == FALSE) { |
| 2177 | Py_DECREF(d); |
| 2178 | win32_error_unicode("FindClose", wnamebuf); |
| 2179 | free(wnamebuf); |
| 2180 | return NULL; |
| 2181 | } |
| 2182 | free(wnamebuf); |
| 2183 | return d; |
| 2184 | } |
| 2185 | /* Drop the argument parsing error as narrow strings |
| 2186 | are also valid. */ |
| 2187 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2188 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2189 | if (!PyArg_ParseTuple(args, "et#:listdir", |
| 2190 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
| 2191 | return NULL; |
| 2192 | if (len > 0) { |
| 2193 | char ch = namebuf[len-1]; |
| 2194 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2195 | namebuf[len++] = '/'; |
| 2196 | strcpy(namebuf + len, "*.*"); |
| 2197 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2198 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2199 | if ((d = PyList_New(0)) == NULL) |
| 2200 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2201 | |
Antoine Pitrou | 8cdede4 | 2010-08-10 00:04:13 +0000 | [diff] [blame] | 2202 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2203 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | 8cdede4 | 2010-08-10 00:04:13 +0000 | [diff] [blame] | 2204 | Py_END_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2205 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2206 | int error = GetLastError(); |
| 2207 | if (error == ERROR_FILE_NOT_FOUND) |
| 2208 | return d; |
| 2209 | Py_DECREF(d); |
| 2210 | return win32_error("FindFirstFile", namebuf); |
| 2211 | } |
| 2212 | do { |
| 2213 | /* Skip over . and .. */ |
| 2214 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2215 | strcmp(FileData.cFileName, "..") != 0) { |
| 2216 | v = PyString_FromString(FileData.cFileName); |
| 2217 | if (v == NULL) { |
| 2218 | Py_DECREF(d); |
| 2219 | d = NULL; |
| 2220 | break; |
| 2221 | } |
| 2222 | if (PyList_Append(d, v) != 0) { |
| 2223 | Py_DECREF(v); |
| 2224 | Py_DECREF(d); |
| 2225 | d = NULL; |
| 2226 | break; |
| 2227 | } |
| 2228 | Py_DECREF(v); |
| 2229 | } |
| 2230 | Py_BEGIN_ALLOW_THREADS |
| 2231 | result = FindNextFile(hFindFile, &FileData); |
| 2232 | Py_END_ALLOW_THREADS |
| 2233 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2234 | it got to the end of the directory. */ |
| 2235 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2236 | Py_DECREF(d); |
| 2237 | win32_error("FindNextFile", namebuf); |
| 2238 | FindClose(hFindFile); |
| 2239 | return NULL; |
| 2240 | } |
| 2241 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2242 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2243 | if (FindClose(hFindFile) == FALSE) { |
| 2244 | Py_DECREF(d); |
| 2245 | return win32_error("FindClose", namebuf); |
| 2246 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2247 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2248 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2249 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2250 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2251 | |
| 2252 | #ifndef MAX_PATH |
| 2253 | #define MAX_PATH CCHMAXPATH |
| 2254 | #endif |
| 2255 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2256 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2257 | PyObject *d, *v; |
| 2258 | char namebuf[MAX_PATH+5]; |
| 2259 | HDIR hdir = 1; |
| 2260 | ULONG srchcnt = 1; |
| 2261 | FILEFINDBUF3 ep; |
| 2262 | APIRET rc; |
| 2263 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2264 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2265 | return NULL; |
| 2266 | if (len >= MAX_PATH) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2267 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2268 | return NULL; |
| 2269 | } |
| 2270 | strcpy(namebuf, name); |
| 2271 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2272 | if (*pt == ALTSEP) |
| 2273 | *pt = SEP; |
| 2274 | if (namebuf[len-1] != SEP) |
| 2275 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2276 | strcpy(namebuf + len, "*.*"); |
| 2277 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2278 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2279 | return NULL; |
| 2280 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2281 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2282 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2283 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2284 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2285 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2286 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2287 | |
| 2288 | if (rc != NO_ERROR) { |
| 2289 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 2290 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2293 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2294 | do { |
| 2295 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2296 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2297 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2298 | |
| 2299 | strcpy(namebuf, ep.achName); |
| 2300 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2301 | /* Leave Case of Name Alone -- In Native Form */ |
| 2302 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2303 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2304 | v = PyString_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2305 | if (v == NULL) { |
| 2306 | Py_DECREF(d); |
| 2307 | d = NULL; |
| 2308 | break; |
| 2309 | } |
| 2310 | if (PyList_Append(d, v) != 0) { |
| 2311 | Py_DECREF(v); |
| 2312 | Py_DECREF(d); |
| 2313 | d = NULL; |
| 2314 | break; |
| 2315 | } |
| 2316 | Py_DECREF(v); |
| 2317 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2318 | } |
| 2319 | |
| 2320 | return d; |
| 2321 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2322 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2323 | char *name = NULL; |
| 2324 | PyObject *d, *v; |
| 2325 | DIR *dirp; |
| 2326 | struct dirent *ep; |
| 2327 | int arg_is_unicode = 1; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2328 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2329 | errno = 0; |
| 2330 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2331 | arg_is_unicode = 0; |
| 2332 | PyErr_Clear(); |
| 2333 | } |
| 2334 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
| 2335 | return NULL; |
Antoine Pitrou | 247e2fd | 2010-09-04 17:27:10 +0000 | [diff] [blame] | 2336 | Py_BEGIN_ALLOW_THREADS |
| 2337 | dirp = opendir(name); |
| 2338 | Py_END_ALLOW_THREADS |
| 2339 | if (dirp == NULL) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2340 | return posix_error_with_allocated_filename(name); |
| 2341 | } |
| 2342 | if ((d = PyList_New(0)) == NULL) { |
Antoine Pitrou | 247e2fd | 2010-09-04 17:27:10 +0000 | [diff] [blame] | 2343 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2344 | closedir(dirp); |
Antoine Pitrou | 247e2fd | 2010-09-04 17:27:10 +0000 | [diff] [blame] | 2345 | Py_END_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2346 | PyMem_Free(name); |
| 2347 | return NULL; |
| 2348 | } |
| 2349 | for (;;) { |
| 2350 | errno = 0; |
| 2351 | Py_BEGIN_ALLOW_THREADS |
| 2352 | ep = readdir(dirp); |
| 2353 | Py_END_ALLOW_THREADS |
| 2354 | if (ep == NULL) { |
| 2355 | if (errno == 0) { |
| 2356 | break; |
| 2357 | } else { |
Antoine Pitrou | 247e2fd | 2010-09-04 17:27:10 +0000 | [diff] [blame] | 2358 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2359 | closedir(dirp); |
Antoine Pitrou | 247e2fd | 2010-09-04 17:27:10 +0000 | [diff] [blame] | 2360 | Py_END_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2361 | Py_DECREF(d); |
| 2362 | return posix_error_with_allocated_filename(name); |
| 2363 | } |
| 2364 | } |
| 2365 | if (ep->d_name[0] == '.' && |
| 2366 | (NAMLEN(ep) == 1 || |
| 2367 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2368 | continue; |
| 2369 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
| 2370 | if (v == NULL) { |
| 2371 | Py_DECREF(d); |
| 2372 | d = NULL; |
| 2373 | break; |
| 2374 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2375 | #ifdef Py_USING_UNICODE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2376 | if (arg_is_unicode) { |
| 2377 | PyObject *w; |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2378 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2379 | w = PyUnicode_FromEncodedObject(v, |
| 2380 | Py_FileSystemDefaultEncoding, |
| 2381 | "strict"); |
| 2382 | if (w != NULL) { |
| 2383 | Py_DECREF(v); |
| 2384 | v = w; |
| 2385 | } |
| 2386 | else { |
| 2387 | /* fall back to the original byte string, as |
| 2388 | discussed in patch #683592 */ |
| 2389 | PyErr_Clear(); |
| 2390 | } |
| 2391 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2392 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2393 | if (PyList_Append(d, v) != 0) { |
| 2394 | Py_DECREF(v); |
| 2395 | Py_DECREF(d); |
| 2396 | d = NULL; |
| 2397 | break; |
| 2398 | } |
| 2399 | Py_DECREF(v); |
| 2400 | } |
Antoine Pitrou | 247e2fd | 2010-09-04 17:27:10 +0000 | [diff] [blame] | 2401 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2402 | closedir(dirp); |
Antoine Pitrou | 247e2fd | 2010-09-04 17:27:10 +0000 | [diff] [blame] | 2403 | Py_END_ALLOW_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2404 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2405 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2406 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2407 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2408 | #endif /* which OS */ |
| 2409 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2410 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2411 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2412 | /* A helper function for abspath on win32 */ |
| 2413 | static PyObject * |
| 2414 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2415 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2416 | /* assume encoded strings won't more than double no of chars */ |
| 2417 | char inbuf[MAX_PATH*2]; |
| 2418 | char *inbufp = inbuf; |
| 2419 | Py_ssize_t insize = sizeof(inbuf); |
| 2420 | char outbuf[MAX_PATH*2]; |
| 2421 | char *temp; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2422 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2423 | PyUnicodeObject *po; |
| 2424 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2425 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2426 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 2427 | Py_UNICODE *wtemp; |
| 2428 | DWORD result; |
| 2429 | PyObject *v; |
| 2430 | result = GetFullPathNameW(wpath, |
| 2431 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2432 | woutbuf, &wtemp); |
| 2433 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { |
| 2434 | woutbufp = malloc(result * sizeof(Py_UNICODE)); |
| 2435 | if (!woutbufp) |
| 2436 | return PyErr_NoMemory(); |
| 2437 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2438 | } |
| 2439 | if (result) |
| 2440 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2441 | else |
| 2442 | v = win32_error_unicode("GetFullPathNameW", wpath); |
| 2443 | if (woutbufp != woutbuf) |
| 2444 | free(woutbufp); |
| 2445 | return v; |
| 2446 | } |
| 2447 | /* Drop the argument parsing error as narrow strings |
| 2448 | are also valid. */ |
| 2449 | PyErr_Clear(); |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2450 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2451 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 2452 | Py_FileSystemDefaultEncoding, &inbufp, |
| 2453 | &insize)) |
| 2454 | return NULL; |
| 2455 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2456 | outbuf, &temp)) |
| 2457 | return win32_error("GetFullPathName", inbuf); |
| 2458 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2459 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2460 | Py_FileSystemDefaultEncoding, NULL); |
| 2461 | } |
| 2462 | return PyString_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2463 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2464 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2465 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2466 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2467 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2468 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2469 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2470 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2471 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2472 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2473 | int res; |
| 2474 | char *path = NULL; |
| 2475 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2476 | |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2477 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2478 | PyUnicodeObject *po; |
| 2479 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
| 2480 | Py_BEGIN_ALLOW_THREADS |
| 2481 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2482 | it is a simple dereference. */ |
| 2483 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
| 2484 | Py_END_ALLOW_THREADS |
| 2485 | if (!res) |
| 2486 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
| 2487 | Py_INCREF(Py_None); |
| 2488 | return Py_None; |
| 2489 | } |
| 2490 | /* Drop the argument parsing error as narrow strings |
| 2491 | are also valid. */ |
| 2492 | PyErr_Clear(); |
| 2493 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 2494 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 2495 | return NULL; |
| 2496 | Py_BEGIN_ALLOW_THREADS |
| 2497 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2498 | it is a simple dereference. */ |
| 2499 | res = CreateDirectoryA(path, NULL); |
| 2500 | Py_END_ALLOW_THREADS |
| 2501 | if (!res) { |
| 2502 | win32_error("mkdir", path); |
| 2503 | PyMem_Free(path); |
| 2504 | return NULL; |
| 2505 | } |
| 2506 | PyMem_Free(path); |
| 2507 | Py_INCREF(Py_None); |
| 2508 | return Py_None; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2509 | #else /* MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2510 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2511 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 2512 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 2513 | return NULL; |
| 2514 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2515 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2516 | res = mkdir(path); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2517 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2518 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2519 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2520 | Py_END_ALLOW_THREADS |
| 2521 | if (res < 0) |
| 2522 | return posix_error_with_allocated_filename(path); |
| 2523 | PyMem_Free(path); |
| 2524 | Py_INCREF(Py_None); |
| 2525 | return Py_None; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2526 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2529 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2530 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2531 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2532 | #include <sys/resource.h> |
| 2533 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2534 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2535 | |
| 2536 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2537 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2538 | "nice(inc) -> new_priority\n\n\ |
| 2539 | 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] | 2540 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2541 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2542 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2543 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2544 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2545 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2546 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 2547 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2548 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2549 | /* There are two flavours of 'nice': one that returns the new |
| 2550 | priority (as required by almost all standards out there) and the |
| 2551 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2552 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2553 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2554 | If we are of the nice family that returns the new priority, we |
| 2555 | need to clear errno before the call, and check if errno is filled |
| 2556 | before calling posix_error() on a returnvalue of -1, because the |
| 2557 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2558 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2559 | errno = 0; |
| 2560 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2561 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2562 | if (value == 0) |
| 2563 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2564 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2565 | if (value == -1 && errno != 0) |
| 2566 | /* either nice() or getpriority() returned an error */ |
| 2567 | return posix_error(); |
| 2568 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2569 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2570 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2571 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2572 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2573 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2574 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2575 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2576 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2577 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2578 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2579 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2580 | PyObject *o1, *o2; |
| 2581 | char *p1, *p2; |
| 2582 | BOOL result; |
| 2583 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
| 2584 | goto error; |
| 2585 | if (!convert_to_unicode(&o1)) |
| 2586 | goto error; |
| 2587 | if (!convert_to_unicode(&o2)) { |
| 2588 | Py_DECREF(o1); |
| 2589 | goto error; |
| 2590 | } |
| 2591 | Py_BEGIN_ALLOW_THREADS |
| 2592 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2593 | PyUnicode_AsUnicode(o2)); |
| 2594 | Py_END_ALLOW_THREADS |
| 2595 | Py_DECREF(o1); |
| 2596 | Py_DECREF(o2); |
| 2597 | if (!result) |
| 2598 | return win32_error("rename", NULL); |
| 2599 | Py_INCREF(Py_None); |
| 2600 | return Py_None; |
Hirokazu Yamamoto | a3c5609 | 2009-06-28 10:23:00 +0000 | [diff] [blame] | 2601 | error: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2602 | PyErr_Clear(); |
| 2603 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2604 | return NULL; |
| 2605 | Py_BEGIN_ALLOW_THREADS |
| 2606 | result = MoveFileA(p1, p2); |
| 2607 | Py_END_ALLOW_THREADS |
| 2608 | if (!result) |
| 2609 | return win32_error("rename", NULL); |
| 2610 | Py_INCREF(Py_None); |
| 2611 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2612 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2613 | return posix_2str(args, "etet:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2614 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2617 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2618 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2619 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2620 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2621 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2622 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2623 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2624 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2625 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2626 | return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2627 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2628 | return posix_1str(args, "et:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2629 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2632 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2633 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2634 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2635 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2636 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2637 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2638 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2639 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2640 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2641 | 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] | 2642 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2643 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2644 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2647 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2648 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2649 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2650 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2651 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2652 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2653 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2654 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2655 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2656 | char *command; |
| 2657 | long sts; |
| 2658 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
| 2659 | return NULL; |
| 2660 | Py_BEGIN_ALLOW_THREADS |
| 2661 | sts = system(command); |
| 2662 | Py_END_ALLOW_THREADS |
| 2663 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2664 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2665 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2666 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2667 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2668 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2669 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2670 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2671 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2672 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2673 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2674 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2675 | int i; |
| 2676 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 2677 | return NULL; |
| 2678 | i = (int)umask(i); |
| 2679 | if (i < 0) |
| 2680 | return posix_error(); |
| 2681 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2684 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2685 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2686 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2687 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2688 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2689 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2690 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2691 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2692 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2693 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2694 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2695 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2696 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2697 | return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2698 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2699 | return posix_1str(args, "et:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2700 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2703 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2704 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2705 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2706 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2707 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2708 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2709 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2710 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2711 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2712 | struct utsname u; |
| 2713 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2714 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2715 | Py_BEGIN_ALLOW_THREADS |
| 2716 | res = uname(&u); |
| 2717 | Py_END_ALLOW_THREADS |
| 2718 | if (res < 0) |
| 2719 | return posix_error(); |
| 2720 | return Py_BuildValue("(sssss)", |
| 2721 | u.sysname, |
| 2722 | u.nodename, |
| 2723 | u.release, |
| 2724 | u.version, |
| 2725 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2726 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2727 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2728 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2729 | static int |
| 2730 | extract_time(PyObject *t, long* sec, long* usec) |
| 2731 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2732 | long intval; |
| 2733 | if (PyFloat_Check(t)) { |
| 2734 | double tval = PyFloat_AsDouble(t); |
| 2735 | PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t); |
| 2736 | if (!intobj) |
| 2737 | return -1; |
| 2738 | intval = PyInt_AsLong(intobj); |
| 2739 | Py_DECREF(intobj); |
| 2740 | if (intval == -1 && PyErr_Occurred()) |
| 2741 | return -1; |
| 2742 | *sec = intval; |
| 2743 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
| 2744 | if (*usec < 0) |
| 2745 | /* If rounding gave us a negative number, |
| 2746 | truncate. */ |
| 2747 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2748 | return 0; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2749 | } |
| 2750 | intval = PyInt_AsLong(t); |
| 2751 | if (intval == -1 && PyErr_Occurred()) |
| 2752 | return -1; |
| 2753 | *sec = intval; |
| 2754 | *usec = 0; |
| 2755 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2756 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2757 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2758 | PyDoc_STRVAR(posix_utime__doc__, |
Georg Brandl | 9e5b5e4 | 2006-05-17 14:18:20 +0000 | [diff] [blame] | 2759 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2760 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2761 | 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] | 2762 | 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] | 2763 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2764 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2765 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2766 | { |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2767 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2768 | PyObject *arg; |
| 2769 | PyUnicodeObject *obwpath; |
| 2770 | wchar_t *wpath = NULL; |
| 2771 | char *apath = NULL; |
| 2772 | HANDLE hFile; |
| 2773 | long atimesec, mtimesec, ausec, musec; |
| 2774 | FILETIME atime, mtime; |
| 2775 | PyObject *result = NULL; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2776 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2777 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2778 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2779 | Py_BEGIN_ALLOW_THREADS |
| 2780 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 2781 | NULL, OPEN_EXISTING, |
| 2782 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 2783 | Py_END_ALLOW_THREADS |
| 2784 | if (hFile == INVALID_HANDLE_VALUE) |
| 2785 | return win32_error_unicode("utime", wpath); |
| 2786 | } else |
| 2787 | /* Drop the argument parsing error as narrow strings |
| 2788 | are also valid. */ |
| 2789 | PyErr_Clear(); |
Hirokazu Yamamoto | a3c5609 | 2009-06-28 10:23:00 +0000 | [diff] [blame] | 2790 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2791 | if (!wpath) { |
| 2792 | if (!PyArg_ParseTuple(args, "etO:utime", |
| 2793 | Py_FileSystemDefaultEncoding, &apath, &arg)) |
| 2794 | return NULL; |
| 2795 | Py_BEGIN_ALLOW_THREADS |
| 2796 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 2797 | NULL, OPEN_EXISTING, |
| 2798 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 2799 | Py_END_ALLOW_THREADS |
| 2800 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2801 | win32_error("utime", apath); |
| 2802 | PyMem_Free(apath); |
| 2803 | return NULL; |
| 2804 | } |
| 2805 | PyMem_Free(apath); |
| 2806 | } |
| 2807 | |
| 2808 | if (arg == Py_None) { |
| 2809 | SYSTEMTIME now; |
| 2810 | GetSystemTime(&now); |
| 2811 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2812 | !SystemTimeToFileTime(&now, &atime)) { |
| 2813 | win32_error("utime", NULL); |
| 2814 | goto done; |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 2815 | } |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2816 | } |
| 2817 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2818 | PyErr_SetString(PyExc_TypeError, |
| 2819 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2820 | goto done; |
| 2821 | } |
| 2822 | else { |
| 2823 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2824 | &atimesec, &ausec) == -1) |
| 2825 | goto done; |
| 2826 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
| 2827 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2828 | &mtimesec, &musec) == -1) |
| 2829 | goto done; |
| 2830 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
| 2831 | } |
| 2832 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2833 | /* Avoid putting the file name into the error here, |
| 2834 | as that may confuse the user into believing that |
| 2835 | something is wrong with the file, when it also |
| 2836 | could be the time stamp that gives a problem. */ |
| 2837 | win32_error("utime", NULL); |
| 2838 | } |
| 2839 | Py_INCREF(Py_None); |
| 2840 | result = Py_None; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2841 | done: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2842 | CloseHandle(hFile); |
| 2843 | return result; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2844 | #else /* MS_WINDOWS */ |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2845 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2846 | char *path = NULL; |
| 2847 | long atime, mtime, ausec, musec; |
| 2848 | int res; |
| 2849 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2850 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2851 | #if defined(HAVE_UTIMES) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2852 | struct timeval buf[2]; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2853 | #define ATIME buf[0].tv_sec |
| 2854 | #define MTIME buf[1].tv_sec |
| 2855 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2856 | /* XXX should define struct utimbuf instead, above */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2857 | struct utimbuf buf; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2858 | #define ATIME buf.actime |
| 2859 | #define MTIME buf.modtime |
| 2860 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2861 | #else /* HAVE_UTIMES */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2862 | time_t buf[2]; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2863 | #define ATIME buf[0] |
| 2864 | #define MTIME buf[1] |
| 2865 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2866 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2867 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2868 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2869 | if (!PyArg_ParseTuple(args, "etO:utime", |
| 2870 | Py_FileSystemDefaultEncoding, &path, &arg)) |
| 2871 | return NULL; |
| 2872 | if (arg == Py_None) { |
| 2873 | /* optional time values not given */ |
| 2874 | Py_BEGIN_ALLOW_THREADS |
| 2875 | res = utime(path, NULL); |
| 2876 | Py_END_ALLOW_THREADS |
| 2877 | } |
| 2878 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2879 | PyErr_SetString(PyExc_TypeError, |
| 2880 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2881 | PyMem_Free(path); |
| 2882 | return NULL; |
| 2883 | } |
| 2884 | else { |
| 2885 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2886 | &atime, &ausec) == -1) { |
| 2887 | PyMem_Free(path); |
| 2888 | return NULL; |
| 2889 | } |
| 2890 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2891 | &mtime, &musec) == -1) { |
| 2892 | PyMem_Free(path); |
| 2893 | return NULL; |
| 2894 | } |
| 2895 | ATIME = atime; |
| 2896 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2897 | #ifdef HAVE_UTIMES |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2898 | buf[0].tv_usec = ausec; |
| 2899 | buf[1].tv_usec = musec; |
| 2900 | Py_BEGIN_ALLOW_THREADS |
| 2901 | res = utimes(path, buf); |
| 2902 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2903 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2904 | Py_BEGIN_ALLOW_THREADS |
| 2905 | res = utime(path, UTIME_ARG); |
| 2906 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2907 | #endif /* HAVE_UTIMES */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2908 | } |
| 2909 | if (res < 0) { |
| 2910 | return posix_error_with_allocated_filename(path); |
| 2911 | } |
| 2912 | PyMem_Free(path); |
| 2913 | Py_INCREF(Py_None); |
| 2914 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2915 | #undef UTIME_ARG |
| 2916 | #undef ATIME |
| 2917 | #undef MTIME |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2918 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2921 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2922 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2923 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2924 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2925 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2926 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2927 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2928 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2929 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2930 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2931 | int sts; |
| 2932 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 2933 | return NULL; |
| 2934 | _exit(sts); |
| 2935 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2938 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2939 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2940 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2941 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2942 | Py_ssize_t i; |
| 2943 | for (i = 0; i < count; i++) |
| 2944 | PyMem_Free(array[i]); |
| 2945 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2946 | } |
| 2947 | #endif |
| 2948 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2949 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2950 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2951 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2952 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2953 | Execute an executable path with arguments, replacing current process.\n\ |
| 2954 | \n\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2955 | path: path of executable file\n\ |
| 2956 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2957 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2958 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2959 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2960 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2961 | char *path; |
| 2962 | PyObject *argv; |
| 2963 | char **argvlist; |
| 2964 | Py_ssize_t i, argc; |
| 2965 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2966 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2967 | /* execv has two arguments: (path, argv), where |
| 2968 | argv is a list or tuple of strings. */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2969 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2970 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2971 | Py_FileSystemDefaultEncoding, |
| 2972 | &path, &argv)) |
| 2973 | return NULL; |
| 2974 | if (PyList_Check(argv)) { |
| 2975 | argc = PyList_Size(argv); |
| 2976 | getitem = PyList_GetItem; |
| 2977 | } |
| 2978 | else if (PyTuple_Check(argv)) { |
| 2979 | argc = PyTuple_Size(argv); |
| 2980 | getitem = PyTuple_GetItem; |
| 2981 | } |
| 2982 | else { |
| 2983 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); |
| 2984 | PyMem_Free(path); |
| 2985 | return NULL; |
| 2986 | } |
| 2987 | if (argc < 1) { |
| 2988 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 2989 | PyMem_Free(path); |
| 2990 | return NULL; |
| 2991 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2992 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 2993 | argvlist = PyMem_NEW(char *, argc+1); |
| 2994 | if (argvlist == NULL) { |
| 2995 | PyMem_Free(path); |
| 2996 | return PyErr_NoMemory(); |
| 2997 | } |
| 2998 | for (i = 0; i < argc; i++) { |
| 2999 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3000 | Py_FileSystemDefaultEncoding, |
| 3001 | &argvlist[i])) { |
| 3002 | free_string_array(argvlist, i); |
| 3003 | PyErr_SetString(PyExc_TypeError, |
| 3004 | "execv() arg 2 must contain only strings"); |
| 3005 | PyMem_Free(path); |
| 3006 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3007 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3008 | } |
| 3009 | } |
| 3010 | argvlist[argc] = NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3011 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3012 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3013 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3014 | /* If we get here it's definitely an error */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3015 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3016 | free_string_array(argvlist, argc); |
| 3017 | PyMem_Free(path); |
| 3018 | return posix_error(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3021 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3022 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3023 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3024 | Execute a path with arguments and environment, replacing current process.\n\ |
| 3025 | \n\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3026 | path: path of executable file\n\ |
| 3027 | args: tuple or list of arguments\n\ |
| 3028 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3029 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3030 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3031 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3032 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3033 | char *path; |
| 3034 | PyObject *argv, *env; |
| 3035 | char **argvlist; |
| 3036 | char **envlist; |
| 3037 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
| 3038 | Py_ssize_t i, pos, argc, envc; |
| 3039 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 3040 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3041 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3042 | /* execve has three arguments: (path, argv, env), where |
| 3043 | argv is a list or tuple of strings and env is a dictionary |
| 3044 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3045 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3046 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 3047 | Py_FileSystemDefaultEncoding, |
| 3048 | &path, &argv, &env)) |
| 3049 | return NULL; |
| 3050 | if (PyList_Check(argv)) { |
| 3051 | argc = PyList_Size(argv); |
| 3052 | getitem = PyList_GetItem; |
| 3053 | } |
| 3054 | else if (PyTuple_Check(argv)) { |
| 3055 | argc = PyTuple_Size(argv); |
| 3056 | getitem = PyTuple_GetItem; |
| 3057 | } |
| 3058 | else { |
| 3059 | PyErr_SetString(PyExc_TypeError, |
| 3060 | "execve() arg 2 must be a tuple or list"); |
| 3061 | goto fail_0; |
| 3062 | } |
| 3063 | if (!PyMapping_Check(env)) { |
| 3064 | PyErr_SetString(PyExc_TypeError, |
| 3065 | "execve() arg 3 must be a mapping object"); |
| 3066 | goto fail_0; |
| 3067 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3068 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3069 | argvlist = PyMem_NEW(char *, argc+1); |
| 3070 | if (argvlist == NULL) { |
| 3071 | PyErr_NoMemory(); |
| 3072 | goto fail_0; |
| 3073 | } |
| 3074 | for (i = 0; i < argc; i++) { |
| 3075 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3076 | "et;execve() arg 2 must contain only strings", |
| 3077 | Py_FileSystemDefaultEncoding, |
| 3078 | &argvlist[i])) |
| 3079 | { |
| 3080 | lastarg = i; |
| 3081 | goto fail_1; |
| 3082 | } |
| 3083 | } |
| 3084 | lastarg = argc; |
| 3085 | argvlist[argc] = NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3086 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3087 | i = PyMapping_Size(env); |
| 3088 | if (i < 0) |
| 3089 | goto fail_1; |
| 3090 | envlist = PyMem_NEW(char *, i + 1); |
| 3091 | if (envlist == NULL) { |
| 3092 | PyErr_NoMemory(); |
| 3093 | goto fail_1; |
| 3094 | } |
| 3095 | envc = 0; |
| 3096 | keys = PyMapping_Keys(env); |
| 3097 | vals = PyMapping_Values(env); |
| 3098 | if (!keys || !vals) |
| 3099 | goto fail_2; |
| 3100 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3101 | PyErr_SetString(PyExc_TypeError, |
| 3102 | "execve(): env.keys() or env.values() is not a list"); |
| 3103 | goto fail_2; |
| 3104 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3105 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3106 | for (pos = 0; pos < i; pos++) { |
| 3107 | char *p, *k, *v; |
| 3108 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3109 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3110 | key = PyList_GetItem(keys, pos); |
| 3111 | val = PyList_GetItem(vals, pos); |
| 3112 | if (!key || !val) |
| 3113 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3114 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3115 | if (!PyArg_Parse( |
| 3116 | key, |
| 3117 | "s;execve() arg 3 contains a non-string key", |
| 3118 | &k) || |
| 3119 | !PyArg_Parse( |
| 3120 | val, |
| 3121 | "s;execve() arg 3 contains a non-string value", |
| 3122 | &v)) |
| 3123 | { |
| 3124 | goto fail_2; |
| 3125 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3126 | |
| 3127 | #if defined(PYOS_OS2) |
| 3128 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3129 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 3130 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3131 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3132 | p = PyMem_NEW(char, len); |
| 3133 | if (p == NULL) { |
| 3134 | PyErr_NoMemory(); |
| 3135 | goto fail_2; |
| 3136 | } |
| 3137 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3138 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3139 | #if defined(PYOS_OS2) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3140 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3141 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3142 | } |
| 3143 | envlist[envc] = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3144 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3145 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3146 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3147 | /* If we get here it's definitely an error */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3148 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3149 | (void) posix_error(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3150 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3151 | fail_2: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3152 | while (--envc >= 0) |
| 3153 | PyMem_DEL(envlist[envc]); |
| 3154 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3155 | fail_1: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3156 | free_string_array(argvlist, lastarg); |
| 3157 | Py_XDECREF(vals); |
| 3158 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3159 | fail_0: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3160 | PyMem_Free(path); |
| 3161 | return NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3162 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3163 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3164 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3165 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3166 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3167 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3168 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3169 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3170 | \n\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3171 | mode: mode of process creation\n\ |
| 3172 | path: path of executable file\n\ |
| 3173 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3174 | |
| 3175 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3176 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3177 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3178 | char *path; |
| 3179 | PyObject *argv; |
| 3180 | char **argvlist; |
| 3181 | int mode, i; |
| 3182 | Py_ssize_t argc; |
| 3183 | Py_intptr_t spawnval; |
| 3184 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3185 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3186 | /* spawnv has three arguments: (mode, path, argv), where |
| 3187 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3188 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3189 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 3190 | Py_FileSystemDefaultEncoding, |
| 3191 | &path, &argv)) |
| 3192 | return NULL; |
| 3193 | if (PyList_Check(argv)) { |
| 3194 | argc = PyList_Size(argv); |
| 3195 | getitem = PyList_GetItem; |
| 3196 | } |
| 3197 | else if (PyTuple_Check(argv)) { |
| 3198 | argc = PyTuple_Size(argv); |
| 3199 | getitem = PyTuple_GetItem; |
| 3200 | } |
| 3201 | else { |
| 3202 | PyErr_SetString(PyExc_TypeError, |
| 3203 | "spawnv() arg 2 must be a tuple or list"); |
| 3204 | PyMem_Free(path); |
| 3205 | return NULL; |
| 3206 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3207 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3208 | argvlist = PyMem_NEW(char *, argc+1); |
| 3209 | if (argvlist == NULL) { |
| 3210 | PyMem_Free(path); |
| 3211 | return PyErr_NoMemory(); |
| 3212 | } |
| 3213 | for (i = 0; i < argc; i++) { |
| 3214 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3215 | Py_FileSystemDefaultEncoding, |
| 3216 | &argvlist[i])) { |
| 3217 | free_string_array(argvlist, i); |
| 3218 | PyErr_SetString( |
| 3219 | PyExc_TypeError, |
| 3220 | "spawnv() arg 2 must contain only strings"); |
| 3221 | PyMem_Free(path); |
| 3222 | return NULL; |
| 3223 | } |
| 3224 | } |
| 3225 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3226 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3227 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3228 | Py_BEGIN_ALLOW_THREADS |
| 3229 | spawnval = spawnv(mode, path, argvlist); |
| 3230 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3231 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3232 | if (mode == _OLD_P_OVERLAY) |
| 3233 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3234 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3235 | Py_BEGIN_ALLOW_THREADS |
| 3236 | spawnval = _spawnv(mode, path, argvlist); |
| 3237 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3238 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3239 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3240 | free_string_array(argvlist, argc); |
| 3241 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3242 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3243 | if (spawnval == -1) |
| 3244 | return posix_error(); |
| 3245 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3246 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3247 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3248 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3249 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3250 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3254 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3255 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3256 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3257 | \n\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3258 | mode: mode of process creation\n\ |
| 3259 | path: path of executable file\n\ |
| 3260 | args: tuple or list of arguments\n\ |
| 3261 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3262 | |
| 3263 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3264 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3265 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3266 | char *path; |
| 3267 | PyObject *argv, *env; |
| 3268 | char **argvlist; |
| 3269 | char **envlist; |
| 3270 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3271 | int mode, pos, envc; |
| 3272 | Py_ssize_t argc, i; |
| 3273 | Py_intptr_t spawnval; |
| 3274 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 3275 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3276 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3277 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3278 | argv is a list or tuple of strings and env is a dictionary |
| 3279 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3280 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3281 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 3282 | Py_FileSystemDefaultEncoding, |
| 3283 | &path, &argv, &env)) |
| 3284 | return NULL; |
| 3285 | if (PyList_Check(argv)) { |
| 3286 | argc = PyList_Size(argv); |
| 3287 | getitem = PyList_GetItem; |
| 3288 | } |
| 3289 | else if (PyTuple_Check(argv)) { |
| 3290 | argc = PyTuple_Size(argv); |
| 3291 | getitem = PyTuple_GetItem; |
| 3292 | } |
| 3293 | else { |
| 3294 | PyErr_SetString(PyExc_TypeError, |
| 3295 | "spawnve() arg 2 must be a tuple or list"); |
| 3296 | goto fail_0; |
| 3297 | } |
| 3298 | if (!PyMapping_Check(env)) { |
| 3299 | PyErr_SetString(PyExc_TypeError, |
| 3300 | "spawnve() arg 3 must be a mapping object"); |
| 3301 | goto fail_0; |
| 3302 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3303 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3304 | argvlist = PyMem_NEW(char *, argc+1); |
| 3305 | if (argvlist == NULL) { |
| 3306 | PyErr_NoMemory(); |
| 3307 | goto fail_0; |
| 3308 | } |
| 3309 | for (i = 0; i < argc; i++) { |
| 3310 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3311 | "et;spawnve() arg 2 must contain only strings", |
| 3312 | Py_FileSystemDefaultEncoding, |
| 3313 | &argvlist[i])) |
| 3314 | { |
| 3315 | lastarg = i; |
| 3316 | goto fail_1; |
| 3317 | } |
| 3318 | } |
| 3319 | lastarg = argc; |
| 3320 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3321 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3322 | i = PyMapping_Size(env); |
| 3323 | if (i < 0) |
| 3324 | goto fail_1; |
| 3325 | envlist = PyMem_NEW(char *, i + 1); |
| 3326 | if (envlist == NULL) { |
| 3327 | PyErr_NoMemory(); |
| 3328 | goto fail_1; |
| 3329 | } |
| 3330 | envc = 0; |
| 3331 | keys = PyMapping_Keys(env); |
| 3332 | vals = PyMapping_Values(env); |
| 3333 | if (!keys || !vals) |
| 3334 | goto fail_2; |
| 3335 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3336 | PyErr_SetString(PyExc_TypeError, |
| 3337 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3338 | goto fail_2; |
| 3339 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3340 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3341 | for (pos = 0; pos < i; pos++) { |
| 3342 | char *p, *k, *v; |
| 3343 | size_t len; |
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 | key = PyList_GetItem(keys, pos); |
| 3346 | val = PyList_GetItem(vals, pos); |
| 3347 | if (!key || !val) |
| 3348 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3349 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3350 | if (!PyArg_Parse( |
| 3351 | key, |
| 3352 | "s;spawnve() arg 3 contains a non-string key", |
| 3353 | &k) || |
| 3354 | !PyArg_Parse( |
| 3355 | val, |
| 3356 | "s;spawnve() arg 3 contains a non-string value", |
| 3357 | &v)) |
| 3358 | { |
| 3359 | goto fail_2; |
| 3360 | } |
| 3361 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3362 | p = PyMem_NEW(char, len); |
| 3363 | if (p == NULL) { |
| 3364 | PyErr_NoMemory(); |
| 3365 | goto fail_2; |
| 3366 | } |
| 3367 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3368 | envlist[envc++] = p; |
| 3369 | } |
| 3370 | envlist[envc] = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3371 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3372 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3373 | Py_BEGIN_ALLOW_THREADS |
| 3374 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3375 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3376 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3377 | if (mode == _OLD_P_OVERLAY) |
| 3378 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3379 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3380 | Py_BEGIN_ALLOW_THREADS |
| 3381 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3382 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3383 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3384 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3385 | if (spawnval == -1) |
| 3386 | (void) posix_error(); |
| 3387 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3388 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3389 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3390 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3391 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3392 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3393 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3394 | fail_2: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3395 | while (--envc >= 0) |
| 3396 | PyMem_DEL(envlist[envc]); |
| 3397 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3398 | fail_1: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3399 | free_string_array(argvlist, lastarg); |
| 3400 | Py_XDECREF(vals); |
| 3401 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3402 | fail_0: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3403 | PyMem_Free(path); |
| 3404 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3405 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3406 | |
| 3407 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3408 | #if defined(PYOS_OS2) |
| 3409 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3410 | "spawnvp(mode, file, args)\n\n\ |
| 3411 | Execute the program 'file' in a new process, using the environment\n\ |
| 3412 | search path to find the file.\n\ |
| 3413 | \n\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3414 | mode: mode of process creation\n\ |
| 3415 | file: executable file name\n\ |
| 3416 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3417 | |
| 3418 | static PyObject * |
| 3419 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3420 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3421 | char *path; |
| 3422 | PyObject *argv; |
| 3423 | char **argvlist; |
| 3424 | int mode, i, argc; |
| 3425 | Py_intptr_t spawnval; |
| 3426 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3427 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3428 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3429 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3430 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3431 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 3432 | Py_FileSystemDefaultEncoding, |
| 3433 | &path, &argv)) |
| 3434 | return NULL; |
| 3435 | if (PyList_Check(argv)) { |
| 3436 | argc = PyList_Size(argv); |
| 3437 | getitem = PyList_GetItem; |
| 3438 | } |
| 3439 | else if (PyTuple_Check(argv)) { |
| 3440 | argc = PyTuple_Size(argv); |
| 3441 | getitem = PyTuple_GetItem; |
| 3442 | } |
| 3443 | else { |
| 3444 | PyErr_SetString(PyExc_TypeError, |
| 3445 | "spawnvp() arg 2 must be a tuple or list"); |
| 3446 | PyMem_Free(path); |
| 3447 | return NULL; |
| 3448 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3449 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3450 | argvlist = PyMem_NEW(char *, argc+1); |
| 3451 | if (argvlist == NULL) { |
| 3452 | PyMem_Free(path); |
| 3453 | return PyErr_NoMemory(); |
| 3454 | } |
| 3455 | for (i = 0; i < argc; i++) { |
| 3456 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3457 | Py_FileSystemDefaultEncoding, |
| 3458 | &argvlist[i])) { |
| 3459 | free_string_array(argvlist, i); |
| 3460 | PyErr_SetString( |
| 3461 | PyExc_TypeError, |
| 3462 | "spawnvp() arg 2 must contain only strings"); |
| 3463 | PyMem_Free(path); |
| 3464 | return NULL; |
| 3465 | } |
| 3466 | } |
| 3467 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3468 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3469 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3470 | #if defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3471 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3472 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3473 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3474 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3475 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3476 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3477 | free_string_array(argvlist, argc); |
| 3478 | PyMem_Free(path); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3479 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3480 | if (spawnval == -1) |
| 3481 | return posix_error(); |
| 3482 | else |
| 3483 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
| 3486 | |
| 3487 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3488 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3489 | Execute the program 'file' in a new process, using the environment\n\ |
| 3490 | search path to find the file.\n\ |
| 3491 | \n\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3492 | mode: mode of process creation\n\ |
| 3493 | file: executable file name\n\ |
| 3494 | args: tuple or list of arguments\n\ |
| 3495 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3496 | |
| 3497 | static PyObject * |
| 3498 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3499 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3500 | char *path; |
| 3501 | PyObject *argv, *env; |
| 3502 | char **argvlist; |
| 3503 | char **envlist; |
| 3504 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3505 | int mode, i, pos, argc, envc; |
| 3506 | Py_intptr_t spawnval; |
| 3507 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 3508 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3509 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3510 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3511 | argv is a list or tuple of strings and env is a dictionary |
| 3512 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3513 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3514 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 3515 | Py_FileSystemDefaultEncoding, |
| 3516 | &path, &argv, &env)) |
| 3517 | return NULL; |
| 3518 | if (PyList_Check(argv)) { |
| 3519 | argc = PyList_Size(argv); |
| 3520 | getitem = PyList_GetItem; |
| 3521 | } |
| 3522 | else if (PyTuple_Check(argv)) { |
| 3523 | argc = PyTuple_Size(argv); |
| 3524 | getitem = PyTuple_GetItem; |
| 3525 | } |
| 3526 | else { |
| 3527 | PyErr_SetString(PyExc_TypeError, |
| 3528 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3529 | goto fail_0; |
| 3530 | } |
| 3531 | if (!PyMapping_Check(env)) { |
| 3532 | PyErr_SetString(PyExc_TypeError, |
| 3533 | "spawnvpe() arg 3 must be a mapping object"); |
| 3534 | goto fail_0; |
| 3535 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3536 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3537 | argvlist = PyMem_NEW(char *, argc+1); |
| 3538 | if (argvlist == NULL) { |
| 3539 | PyErr_NoMemory(); |
| 3540 | goto fail_0; |
| 3541 | } |
| 3542 | for (i = 0; i < argc; i++) { |
| 3543 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3544 | "et;spawnvpe() arg 2 must contain only strings", |
| 3545 | Py_FileSystemDefaultEncoding, |
| 3546 | &argvlist[i])) |
| 3547 | { |
| 3548 | lastarg = i; |
| 3549 | goto fail_1; |
| 3550 | } |
| 3551 | } |
| 3552 | lastarg = argc; |
| 3553 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3554 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3555 | i = PyMapping_Size(env); |
| 3556 | if (i < 0) |
| 3557 | goto fail_1; |
| 3558 | envlist = PyMem_NEW(char *, i + 1); |
| 3559 | if (envlist == NULL) { |
| 3560 | PyErr_NoMemory(); |
| 3561 | goto fail_1; |
| 3562 | } |
| 3563 | envc = 0; |
| 3564 | keys = PyMapping_Keys(env); |
| 3565 | vals = PyMapping_Values(env); |
| 3566 | if (!keys || !vals) |
| 3567 | goto fail_2; |
| 3568 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3569 | PyErr_SetString(PyExc_TypeError, |
| 3570 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3571 | goto fail_2; |
| 3572 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3573 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3574 | for (pos = 0; pos < i; pos++) { |
| 3575 | char *p, *k, *v; |
| 3576 | size_t len; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3577 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3578 | key = PyList_GetItem(keys, pos); |
| 3579 | val = PyList_GetItem(vals, pos); |
| 3580 | if (!key || !val) |
| 3581 | goto fail_2; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3582 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3583 | if (!PyArg_Parse( |
| 3584 | key, |
| 3585 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3586 | &k) || |
| 3587 | !PyArg_Parse( |
| 3588 | val, |
| 3589 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3590 | &v)) |
| 3591 | { |
| 3592 | goto fail_2; |
| 3593 | } |
| 3594 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3595 | p = PyMem_NEW(char, len); |
| 3596 | if (p == NULL) { |
| 3597 | PyErr_NoMemory(); |
| 3598 | goto fail_2; |
| 3599 | } |
| 3600 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3601 | envlist[envc++] = p; |
| 3602 | } |
| 3603 | envlist[envc] = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3604 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3605 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3606 | #if defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3607 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3608 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3609 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3610 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3611 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3612 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3613 | if (spawnval == -1) |
| 3614 | (void) posix_error(); |
| 3615 | else |
| 3616 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3617 | |
| 3618 | fail_2: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3619 | while (--envc >= 0) |
| 3620 | PyMem_DEL(envlist[envc]); |
| 3621 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3622 | fail_1: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3623 | free_string_array(argvlist, lastarg); |
| 3624 | Py_XDECREF(vals); |
| 3625 | Py_XDECREF(keys); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3626 | fail_0: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3627 | PyMem_Free(path); |
| 3628 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3629 | } |
| 3630 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3631 | #endif /* HAVE_SPAWNV */ |
| 3632 | |
| 3633 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3634 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3635 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3636 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3637 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3638 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3639 | 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] | 3640 | |
| 3641 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3642 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3643 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3644 | pid_t pid; |
| 3645 | int result = 0; |
| 3646 | _PyImport_AcquireLock(); |
| 3647 | pid = fork1(); |
| 3648 | if (pid == 0) { |
| 3649 | /* child: this clobbers and resets the import lock. */ |
| 3650 | PyOS_AfterFork(); |
| 3651 | } else { |
| 3652 | /* parent: release the import lock. */ |
| 3653 | result = _PyImport_ReleaseLock(); |
| 3654 | } |
| 3655 | if (pid == -1) |
| 3656 | return posix_error(); |
| 3657 | if (result < 0) { |
| 3658 | /* Don't clobber the OSError if the fork failed. */ |
| 3659 | PyErr_SetString(PyExc_RuntimeError, |
| 3660 | "not holding the import lock"); |
| 3661 | return NULL; |
| 3662 | } |
| 3663 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3664 | } |
| 3665 | #endif |
| 3666 | |
| 3667 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3668 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3669 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3670 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3671 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3672 | 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] | 3673 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3674 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3675 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3676 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3677 | pid_t pid; |
| 3678 | int result = 0; |
| 3679 | _PyImport_AcquireLock(); |
| 3680 | pid = fork(); |
| 3681 | if (pid == 0) { |
| 3682 | /* child: this clobbers and resets the import lock. */ |
| 3683 | PyOS_AfterFork(); |
| 3684 | } else { |
| 3685 | /* parent: release the import lock. */ |
| 3686 | result = _PyImport_ReleaseLock(); |
| 3687 | } |
| 3688 | if (pid == -1) |
| 3689 | return posix_error(); |
| 3690 | if (result < 0) { |
| 3691 | /* Don't clobber the OSError if the fork failed. */ |
| 3692 | PyErr_SetString(PyExc_RuntimeError, |
| 3693 | "not holding the import lock"); |
| 3694 | return NULL; |
| 3695 | } |
| 3696 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3697 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3698 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3699 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3700 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3701 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3702 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3703 | #define DEV_PTY_FILE "/dev/ptc" |
| 3704 | #define HAVE_DEV_PTMX |
| 3705 | #else |
| 3706 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3707 | #endif |
| 3708 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3709 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3710 | #ifdef HAVE_PTY_H |
| 3711 | #include <pty.h> |
| 3712 | #else |
| 3713 | #ifdef HAVE_LIBUTIL_H |
| 3714 | #include <libutil.h> |
Ronald Oussoren | a55af9a | 2010-01-17 16:25:57 +0000 | [diff] [blame] | 3715 | #else |
| 3716 | #ifdef HAVE_UTIL_H |
| 3717 | #include <util.h> |
| 3718 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3719 | #endif /* HAVE_LIBUTIL_H */ |
| 3720 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3721 | #ifdef HAVE_STROPTS_H |
| 3722 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3723 | #endif |
| 3724 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3725 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3726 | #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] | 3727 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3728 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3729 | 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] | 3730 | |
| 3731 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3732 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3733 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3734 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3735 | #ifndef HAVE_OPENPTY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3736 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3737 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3738 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3739 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3740 | #ifdef sun |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3741 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3742 | #endif |
| 3743 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3744 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3745 | #ifdef HAVE_OPENPTY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3746 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3747 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3748 | #elif defined(HAVE__GETPTY) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3749 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3750 | if (slave_name == NULL) |
| 3751 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3752 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3753 | slave_fd = open(slave_name, O_RDWR); |
| 3754 | if (slave_fd < 0) |
| 3755 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3756 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3757 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 3758 | if (master_fd < 0) |
| 3759 | return posix_error(); |
| 3760 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 3761 | /* change permission of slave */ |
| 3762 | if (grantpt(master_fd) < 0) { |
| 3763 | PyOS_setsig(SIGCHLD, sig_saved); |
| 3764 | return posix_error(); |
| 3765 | } |
| 3766 | /* unlock slave */ |
| 3767 | if (unlockpt(master_fd) < 0) { |
| 3768 | PyOS_setsig(SIGCHLD, sig_saved); |
| 3769 | return posix_error(); |
| 3770 | } |
| 3771 | PyOS_setsig(SIGCHLD, sig_saved); |
| 3772 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3773 | if (slave_name == NULL) |
| 3774 | return posix_error(); |
| 3775 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3776 | if (slave_fd < 0) |
| 3777 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3778 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3779 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3780 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3781 | #ifndef __hpux |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3782 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3783 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3784 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3785 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3786 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3787 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3788 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3789 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3790 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3791 | |
| 3792 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3793 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3794 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3795 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3796 | 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] | 3797 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3798 | |
| 3799 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3800 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3801 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3802 | int master_fd = -1, result = 0; |
| 3803 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3804 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3805 | _PyImport_AcquireLock(); |
| 3806 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3807 | if (pid == 0) { |
| 3808 | /* child: this clobbers and resets the import lock. */ |
| 3809 | PyOS_AfterFork(); |
| 3810 | } else { |
| 3811 | /* parent: release the import lock. */ |
| 3812 | result = _PyImport_ReleaseLock(); |
| 3813 | } |
| 3814 | if (pid == -1) |
| 3815 | return posix_error(); |
| 3816 | if (result < 0) { |
| 3817 | /* Don't clobber the OSError if the fork failed. */ |
| 3818 | PyErr_SetString(PyExc_RuntimeError, |
| 3819 | "not holding the import lock"); |
| 3820 | return NULL; |
| 3821 | } |
| 3822 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3823 | } |
| 3824 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3825 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3826 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3827 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3828 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3829 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3830 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3831 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3832 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3833 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3834 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3835 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3836 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3837 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3838 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3839 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3840 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3841 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3842 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3843 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3844 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3845 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3846 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3847 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3848 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3849 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3850 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3851 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3852 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3853 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3854 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3855 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3856 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3857 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3858 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3859 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3860 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3861 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3862 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3863 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3864 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3865 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3866 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3867 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3868 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3869 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3870 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3871 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3872 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3875 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3876 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3877 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3878 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3879 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3880 | |
| 3881 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3882 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3883 | { |
| 3884 | PyObject *result = NULL; |
| 3885 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3886 | #ifdef NGROUPS_MAX |
| 3887 | #define MAX_GROUPS NGROUPS_MAX |
| 3888 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3889 | /* 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] | 3890 | #define MAX_GROUPS 64 |
| 3891 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3892 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 3893 | |
| 3894 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
| 3895 | * This is a helper variable to store the intermediate result when |
| 3896 | * that happens. |
| 3897 | * |
| 3898 | * To keep the code readable the OSX behaviour is unconditional, |
| 3899 | * according to the POSIX spec this should be safe on all unix-y |
| 3900 | * systems. |
| 3901 | */ |
| 3902 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3903 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3904 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3905 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 3906 | if (n < 0) { |
| 3907 | if (errno == EINVAL) { |
| 3908 | n = getgroups(0, NULL); |
| 3909 | if (n == -1) { |
| 3910 | return posix_error(); |
| 3911 | } |
| 3912 | if (n == 0) { |
| 3913 | /* Avoid malloc(0) */ |
| 3914 | alt_grouplist = grouplist; |
| 3915 | } else { |
| 3916 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 3917 | if (alt_grouplist == NULL) { |
| 3918 | errno = EINVAL; |
| 3919 | return posix_error(); |
| 3920 | } |
| 3921 | n = getgroups(n, alt_grouplist); |
| 3922 | if (n == -1) { |
| 3923 | PyMem_Free(alt_grouplist); |
| 3924 | return posix_error(); |
| 3925 | } |
| 3926 | } |
| 3927 | } else { |
| 3928 | return posix_error(); |
| 3929 | } |
| 3930 | } |
| 3931 | result = PyList_New(n); |
| 3932 | if (result != NULL) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3933 | int i; |
| 3934 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 3935 | PyObject *o = PyInt_FromLong((long)alt_grouplist[i]); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3936 | if (o == NULL) { |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 3937 | Py_DECREF(result); |
| 3938 | result = NULL; |
| 3939 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3940 | } |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3941 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3942 | } |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | if (alt_grouplist != grouplist) { |
| 3946 | PyMem_Free(alt_grouplist); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3947 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3948 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3949 | return result; |
| 3950 | } |
| 3951 | #endif |
| 3952 | |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 3953 | #ifdef HAVE_INITGROUPS |
| 3954 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 3955 | "initgroups(username, gid) -> None\n\n\ |
| 3956 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 3957 | the groups of which the specified username is a member, plus the specified\n\ |
| 3958 | group id."); |
| 3959 | |
| 3960 | static PyObject * |
| 3961 | posix_initgroups(PyObject *self, PyObject *args) |
| 3962 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3963 | char *username; |
| 3964 | long gid; |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 3965 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3966 | if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid)) |
| 3967 | return NULL; |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 3968 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3969 | if (initgroups(username, (gid_t) gid) == -1) |
| 3970 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 3971 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3972 | Py_INCREF(Py_None); |
| 3973 | return Py_None; |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 3974 | } |
| 3975 | #endif |
| 3976 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3977 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3978 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3979 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3980 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3981 | |
| 3982 | static PyObject * |
| 3983 | posix_getpgid(PyObject *self, PyObject *args) |
| 3984 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 3985 | pid_t pid, pgid; |
| 3986 | if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid)) |
| 3987 | return NULL; |
| 3988 | pgid = getpgid(pid); |
| 3989 | if (pgid < 0) |
| 3990 | return posix_error(); |
| 3991 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3992 | } |
| 3993 | #endif /* HAVE_GETPGID */ |
| 3994 | |
| 3995 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3996 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3997 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3998 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3999 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4000 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4001 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4002 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4003 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4004 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4005 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4006 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4007 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4008 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4009 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4010 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4011 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4012 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4013 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4014 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4015 | "setpgrp()\n\n\ |
Senthil Kumaran | 0d6908b | 2010-06-17 16:38:34 +0000 | [diff] [blame] | 4016 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4017 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4018 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4019 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4020 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4021 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4022 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4023 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4024 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4025 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4026 | return posix_error(); |
| 4027 | Py_INCREF(Py_None); |
| 4028 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4031 | #endif /* HAVE_SETPGRP */ |
| 4032 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4033 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4034 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4035 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4036 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4037 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4038 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4039 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4040 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4041 | return PyLong_FromPid(getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4042 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4043 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4044 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4045 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4046 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4047 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4048 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4049 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4050 | |
| 4051 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4052 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4053 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4054 | PyObject *result = NULL; |
| 4055 | char *name; |
| 4056 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4057 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4058 | errno = 0; |
| 4059 | name = getlogin(); |
| 4060 | if (name == NULL) { |
| 4061 | if (errno) |
| 4062 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4063 | else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4064 | PyErr_SetString(PyExc_OSError, |
| 4065 | "unable to determine login name"); |
| 4066 | } |
| 4067 | else |
| 4068 | result = PyString_FromString(name); |
| 4069 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4070 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4071 | return result; |
| 4072 | } |
| 4073 | #endif |
| 4074 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4075 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4076 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4077 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4078 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4079 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4080 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4081 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4082 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4083 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4084 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4085 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4086 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4087 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4088 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4089 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4090 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4091 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4092 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4093 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4094 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4095 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4096 | pid_t pid; |
| 4097 | int sig; |
| 4098 | if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig)) |
| 4099 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4100 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4101 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 4102 | APIRET rc; |
| 4103 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4104 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4105 | |
| 4106 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 4107 | APIRET rc; |
| 4108 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4109 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4110 | |
| 4111 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 4112 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4113 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4114 | if (kill(pid, sig) == -1) |
| 4115 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4116 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4117 | Py_INCREF(Py_None); |
| 4118 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4119 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4120 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4121 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4122 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4123 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4124 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4125 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4126 | |
| 4127 | static PyObject * |
| 4128 | posix_killpg(PyObject *self, PyObject *args) |
| 4129 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4130 | int sig; |
| 4131 | pid_t pgid; |
| 4132 | /* XXX some man pages make the `pgid` parameter an int, others |
| 4133 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 4134 | take the same type. Moreover, pid_t is always at least as wide as |
| 4135 | int (else compilation of this module fails), which is safe. */ |
| 4136 | if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig)) |
| 4137 | return NULL; |
| 4138 | if (killpg(pgid, sig) == -1) |
| 4139 | return posix_error(); |
| 4140 | Py_INCREF(Py_None); |
| 4141 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4142 | } |
| 4143 | #endif |
| 4144 | |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 4145 | #ifdef MS_WINDOWS |
| 4146 | PyDoc_STRVAR(win32_kill__doc__, |
| 4147 | "kill(pid, sig)\n\n\ |
| 4148 | Kill a process with a signal."); |
| 4149 | |
| 4150 | static PyObject * |
| 4151 | win32_kill(PyObject *self, PyObject *args) |
| 4152 | { |
Amaury Forgeot d'Arc | 03acec2 | 2010-05-15 21:45:30 +0000 | [diff] [blame] | 4153 | PyObject *result; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4154 | DWORD pid, sig, err; |
| 4155 | HANDLE handle; |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 4156 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4157 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 4158 | return NULL; |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 4159 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4160 | /* Console processes which share a common console can be sent CTRL+C or |
| 4161 | CTRL+BREAK events, provided they handle said events. */ |
| 4162 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 4163 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 4164 | err = GetLastError(); |
| 4165 | return PyErr_SetFromWindowsErr(err); |
| 4166 | } |
| 4167 | else |
| 4168 | Py_RETURN_NONE; |
| 4169 | } |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 4170 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4171 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 4172 | attempt to open and terminate the process. */ |
| 4173 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 4174 | if (handle == NULL) { |
| 4175 | err = GetLastError(); |
| 4176 | return PyErr_SetFromWindowsErr(err); |
| 4177 | } |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 4178 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4179 | if (TerminateProcess(handle, sig) == 0) { |
| 4180 | err = GetLastError(); |
| 4181 | result = PyErr_SetFromWindowsErr(err); |
| 4182 | } else { |
| 4183 | Py_INCREF(Py_None); |
| 4184 | result = Py_None; |
| 4185 | } |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 4186 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4187 | CloseHandle(handle); |
| 4188 | return result; |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 4189 | } |
| 4190 | #endif /* MS_WINDOWS */ |
| 4191 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4192 | #ifdef HAVE_PLOCK |
| 4193 | |
| 4194 | #ifdef HAVE_SYS_LOCK_H |
| 4195 | #include <sys/lock.h> |
| 4196 | #endif |
| 4197 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4198 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4199 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4200 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4201 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4202 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4203 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4204 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4205 | int op; |
| 4206 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 4207 | return NULL; |
| 4208 | if (plock(op) == -1) |
| 4209 | return posix_error(); |
| 4210 | Py_INCREF(Py_None); |
| 4211 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4212 | } |
| 4213 | #endif |
| 4214 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4215 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4216 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4217 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4218 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4219 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4220 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4221 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4222 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4223 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4224 | async_system(const char *command) |
| 4225 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4226 | char errormsg[256], args[1024]; |
| 4227 | RESULTCODES rcodes; |
| 4228 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4229 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4230 | char *shell = getenv("COMSPEC"); |
| 4231 | if (!shell) |
| 4232 | shell = "cmd"; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4233 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4234 | /* avoid overflowing the argument buffer */ |
| 4235 | if (strlen(shell) + 3 + strlen(command) >= 1024) |
| 4236 | return ERROR_NOT_ENOUGH_MEMORY |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 4237 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4238 | args[0] = '\0'; |
| 4239 | strcat(args, shell); |
| 4240 | strcat(args, "/c "); |
| 4241 | strcat(args, command); |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 4242 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4243 | /* execute asynchronously, inheriting the environment */ |
| 4244 | rc = DosExecPgm(errormsg, |
| 4245 | sizeof(errormsg), |
| 4246 | EXEC_ASYNC, |
| 4247 | args, |
| 4248 | NULL, |
| 4249 | &rcodes, |
| 4250 | shell); |
| 4251 | return rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4254 | static FILE * |
| 4255 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4256 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4257 | int oldfd, tgtfd; |
| 4258 | HFILE pipeh[2]; |
| 4259 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4260 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4261 | /* mode determines which of stdin or stdout is reconnected to |
| 4262 | * the pipe to the child |
| 4263 | */ |
| 4264 | if (strchr(mode, 'r') != NULL) { |
| 4265 | tgt_fd = 1; /* stdout */ |
| 4266 | } else if (strchr(mode, 'w')) { |
| 4267 | tgt_fd = 0; /* stdin */ |
| 4268 | } else { |
| 4269 | *err = ERROR_INVALID_ACCESS; |
| 4270 | return NULL; |
| 4271 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4272 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4273 | /* setup the pipe */ |
| 4274 | if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) { |
| 4275 | *err = rc; |
| 4276 | return NULL; |
| 4277 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4278 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4279 | /* prevent other threads accessing stdio */ |
| 4280 | DosEnterCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4281 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4282 | /* reconnect stdio and execute child */ |
| 4283 | oldfd = dup(tgtfd); |
| 4284 | close(tgtfd); |
| 4285 | if (dup2(pipeh[tgtfd], tgtfd) == 0) { |
| 4286 | DosClose(pipeh[tgtfd]); |
| 4287 | rc = async_system(command); |
| 4288 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4289 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4290 | /* restore stdio */ |
| 4291 | dup2(oldfd, tgtfd); |
| 4292 | close(oldfd); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4293 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4294 | /* allow other threads access to stdio */ |
| 4295 | DosExitCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4296 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4297 | /* if execution of child was successful return file stream */ |
| 4298 | if (rc == NO_ERROR) |
| 4299 | return fdopen(pipeh[1 - tgtfd], mode); |
| 4300 | else { |
| 4301 | DosClose(pipeh[1 - tgtfd]); |
| 4302 | *err = rc; |
| 4303 | return NULL; |
| 4304 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4305 | } |
| 4306 | |
| 4307 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4308 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4309 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4310 | char *name; |
| 4311 | char *mode = "r"; |
| 4312 | int err, bufsize = -1; |
| 4313 | FILE *fp; |
| 4314 | PyObject *f; |
| 4315 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 4316 | return NULL; |
| 4317 | Py_BEGIN_ALLOW_THREADS |
| 4318 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
| 4319 | Py_END_ALLOW_THREADS |
| 4320 | if (fp == NULL) |
| 4321 | return os2_error(err); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4322 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4323 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 4324 | if (f != NULL) |
| 4325 | PyFile_SetBufSize(f, bufsize); |
| 4326 | return f; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4329 | #elif defined(PYCC_GCC) |
| 4330 | |
| 4331 | /* standard posix version of popen() support */ |
| 4332 | static PyObject * |
| 4333 | posix_popen(PyObject *self, PyObject *args) |
| 4334 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4335 | char *name; |
| 4336 | char *mode = "r"; |
| 4337 | int bufsize = -1; |
| 4338 | FILE *fp; |
| 4339 | PyObject *f; |
| 4340 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 4341 | return NULL; |
| 4342 | Py_BEGIN_ALLOW_THREADS |
| 4343 | fp = popen(name, mode); |
| 4344 | Py_END_ALLOW_THREADS |
| 4345 | if (fp == NULL) |
| 4346 | return posix_error(); |
| 4347 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 4348 | if (f != NULL) |
| 4349 | PyFile_SetBufSize(f, bufsize); |
| 4350 | return f; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4351 | } |
| 4352 | |
| 4353 | /* fork() under OS/2 has lots'o'warts |
| 4354 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 4355 | * most of this code is a ripoff of the win32 code, but using the |
| 4356 | * capabilities of EMX's C library routines |
| 4357 | */ |
| 4358 | |
| 4359 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 4360 | #define POPEN_1 1 |
| 4361 | #define POPEN_2 2 |
| 4362 | #define POPEN_3 3 |
| 4363 | #define POPEN_4 4 |
| 4364 | |
| 4365 | static PyObject *_PyPopen(char *, int, int, int); |
| 4366 | static int _PyPclose(FILE *file); |
| 4367 | |
| 4368 | /* |
| 4369 | * Internal dictionary mapping popen* file pointers to process handles, |
| 4370 | * for use when retrieving the process exit code. See _PyPclose() below |
| 4371 | * for more information on this dictionary's use. |
| 4372 | */ |
| 4373 | static PyObject *_PyPopenProcs = NULL; |
| 4374 | |
| 4375 | /* os2emx version of popen2() |
| 4376 | * |
| 4377 | * The result of this function is a pipe (file) connected to the |
| 4378 | * process's stdin, and a pipe connected to the process's stdout. |
| 4379 | */ |
| 4380 | |
| 4381 | static PyObject * |
| 4382 | os2emx_popen2(PyObject *self, PyObject *args) |
| 4383 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4384 | PyObject *f; |
| 4385 | int tm=0; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4386 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4387 | char *cmdstring; |
| 4388 | char *mode = "t"; |
| 4389 | int bufsize = -1; |
| 4390 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 4391 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4392 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4393 | if (*mode == 't') |
| 4394 | tm = O_TEXT; |
| 4395 | else if (*mode != 'b') { |
| 4396 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 4397 | return NULL; |
| 4398 | } else |
| 4399 | tm = O_BINARY; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4400 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4401 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4402 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4403 | return f; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | /* |
| 4407 | * Variation on os2emx.popen2 |
| 4408 | * |
| 4409 | * The result of this function is 3 pipes - the process's stdin, |
| 4410 | * stdout and stderr |
| 4411 | */ |
| 4412 | |
| 4413 | static PyObject * |
| 4414 | os2emx_popen3(PyObject *self, PyObject *args) |
| 4415 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4416 | PyObject *f; |
| 4417 | int tm = 0; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4418 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4419 | char *cmdstring; |
| 4420 | char *mode = "t"; |
| 4421 | int bufsize = -1; |
| 4422 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 4423 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4424 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4425 | if (*mode == 't') |
| 4426 | tm = O_TEXT; |
| 4427 | else if (*mode != 'b') { |
| 4428 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 4429 | return NULL; |
| 4430 | } else |
| 4431 | tm = O_BINARY; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4432 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4433 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4434 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4435 | return f; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4436 | } |
| 4437 | |
| 4438 | /* |
| 4439 | * Variation on os2emx.popen2 |
| 4440 | * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4441 | * The result of this function is 2 pipes - the processes stdin, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4442 | * and stdout+stderr combined as a single pipe. |
| 4443 | */ |
| 4444 | |
| 4445 | static PyObject * |
| 4446 | os2emx_popen4(PyObject *self, PyObject *args) |
| 4447 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4448 | PyObject *f; |
| 4449 | int tm = 0; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4450 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4451 | char *cmdstring; |
| 4452 | char *mode = "t"; |
| 4453 | int bufsize = -1; |
| 4454 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 4455 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4456 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4457 | if (*mode == 't') |
| 4458 | tm = O_TEXT; |
| 4459 | else if (*mode != 'b') { |
| 4460 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 4461 | return NULL; |
| 4462 | } else |
| 4463 | tm = O_BINARY; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4464 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4465 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4466 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4467 | return f; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4468 | } |
| 4469 | |
| 4470 | /* a couple of structures for convenient handling of multiple |
| 4471 | * file handles and pipes |
| 4472 | */ |
| 4473 | struct file_ref |
| 4474 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4475 | int handle; |
| 4476 | int flags; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4477 | }; |
| 4478 | |
| 4479 | struct pipe_ref |
| 4480 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4481 | int rd; |
| 4482 | int wr; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4483 | }; |
| 4484 | |
| 4485 | /* The following code is derived from the win32 code */ |
| 4486 | |
| 4487 | static PyObject * |
| 4488 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 4489 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4490 | struct file_ref stdio[3]; |
| 4491 | struct pipe_ref p_fd[3]; |
| 4492 | FILE *p_s[3]; |
| 4493 | int file_count, i, pipe_err; |
| 4494 | pid_t pipe_pid; |
| 4495 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 4496 | PyObject *f, *p_f[3]; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4497 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4498 | /* file modes for subsequent fdopen's on pipe handles */ |
| 4499 | if (mode == O_TEXT) |
| 4500 | { |
| 4501 | rd_mode = "rt"; |
| 4502 | wr_mode = "wt"; |
| 4503 | } |
| 4504 | else |
| 4505 | { |
| 4506 | rd_mode = "rb"; |
| 4507 | wr_mode = "wb"; |
| 4508 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4509 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4510 | /* prepare shell references */ |
| 4511 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 4512 | if ((shell = getenv("COMSPEC")) == NULL) |
| 4513 | { |
| 4514 | errno = ENOENT; |
| 4515 | return posix_error(); |
| 4516 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4517 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4518 | sh_name = _getname(shell); |
| 4519 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 4520 | opt = "/c"; |
| 4521 | else |
| 4522 | opt = "-c"; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4523 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4524 | /* save current stdio fds + their flags, and set not inheritable */ |
| 4525 | i = pipe_err = 0; |
| 4526 | while (pipe_err >= 0 && i < 3) |
| 4527 | { |
| 4528 | pipe_err = stdio[i].handle = dup(i); |
| 4529 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 4530 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 4531 | i++; |
| 4532 | } |
| 4533 | if (pipe_err < 0) |
| 4534 | { |
| 4535 | /* didn't get them all saved - clean up and bail out */ |
| 4536 | int saved_err = errno; |
| 4537 | while (i-- > 0) |
| 4538 | { |
| 4539 | close(stdio[i].handle); |
| 4540 | } |
| 4541 | errno = saved_err; |
| 4542 | return posix_error(); |
| 4543 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4544 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4545 | /* create pipe ends */ |
| 4546 | file_count = 2; |
| 4547 | if (n == POPEN_3) |
| 4548 | file_count = 3; |
| 4549 | i = pipe_err = 0; |
| 4550 | while ((pipe_err == 0) && (i < file_count)) |
| 4551 | pipe_err = pipe((int *)&p_fd[i++]); |
| 4552 | if (pipe_err < 0) |
| 4553 | { |
| 4554 | /* didn't get them all made - clean up and bail out */ |
| 4555 | while (i-- > 0) |
| 4556 | { |
| 4557 | close(p_fd[i].wr); |
| 4558 | close(p_fd[i].rd); |
| 4559 | } |
| 4560 | errno = EPIPE; |
| 4561 | return posix_error(); |
| 4562 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4563 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4564 | /* change the actual standard IO streams over temporarily, |
| 4565 | * making the retained pipe ends non-inheritable |
| 4566 | */ |
| 4567 | pipe_err = 0; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4568 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4569 | /* - stdin */ |
| 4570 | if (dup2(p_fd[0].rd, 0) == 0) |
| 4571 | { |
| 4572 | close(p_fd[0].rd); |
| 4573 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 4574 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 4575 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 4576 | { |
| 4577 | close(p_fd[0].wr); |
| 4578 | pipe_err = -1; |
| 4579 | } |
| 4580 | } |
| 4581 | else |
| 4582 | { |
| 4583 | pipe_err = -1; |
| 4584 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4585 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4586 | /* - stdout */ |
| 4587 | if (pipe_err == 0) |
| 4588 | { |
| 4589 | if (dup2(p_fd[1].wr, 1) == 1) |
| 4590 | { |
| 4591 | close(p_fd[1].wr); |
| 4592 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 4593 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 4594 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 4595 | { |
| 4596 | close(p_fd[1].rd); |
| 4597 | pipe_err = -1; |
| 4598 | } |
| 4599 | } |
| 4600 | else |
| 4601 | { |
| 4602 | pipe_err = -1; |
| 4603 | } |
| 4604 | } |
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 | /* - stderr, as required */ |
| 4607 | if (pipe_err == 0) |
| 4608 | switch (n) |
| 4609 | { |
| 4610 | case POPEN_3: |
| 4611 | { |
| 4612 | if (dup2(p_fd[2].wr, 2) == 2) |
| 4613 | { |
| 4614 | close(p_fd[2].wr); |
| 4615 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 4616 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 4617 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 4618 | { |
| 4619 | close(p_fd[2].rd); |
| 4620 | pipe_err = -1; |
| 4621 | } |
| 4622 | } |
| 4623 | else |
| 4624 | { |
| 4625 | pipe_err = -1; |
| 4626 | } |
| 4627 | break; |
| 4628 | } |
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 | case POPEN_4: |
| 4631 | { |
| 4632 | if (dup2(1, 2) != 2) |
| 4633 | { |
| 4634 | pipe_err = -1; |
| 4635 | } |
| 4636 | break; |
| 4637 | } |
| 4638 | } |
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 | /* spawn the child process */ |
| 4641 | if (pipe_err == 0) |
| 4642 | { |
| 4643 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 4644 | if (pipe_pid == -1) |
| 4645 | { |
| 4646 | pipe_err = -1; |
| 4647 | } |
| 4648 | else |
| 4649 | { |
| 4650 | /* save the PID into the FILE structure |
| 4651 | * NOTE: this implementation doesn't actually |
| 4652 | * take advantage of this, but do it for |
| 4653 | * completeness - AIM Apr01 |
| 4654 | */ |
| 4655 | for (i = 0; i < file_count; i++) |
| 4656 | p_s[i]->_pid = pipe_pid; |
| 4657 | } |
| 4658 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4659 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4660 | /* reset standard IO to normal */ |
| 4661 | for (i = 0; i < 3; i++) |
| 4662 | { |
| 4663 | dup2(stdio[i].handle, i); |
| 4664 | fcntl(i, F_SETFD, stdio[i].flags); |
| 4665 | close(stdio[i].handle); |
| 4666 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4667 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4668 | /* if any remnant problems, clean up and bail out */ |
| 4669 | if (pipe_err < 0) |
| 4670 | { |
| 4671 | for (i = 0; i < 3; i++) |
| 4672 | { |
| 4673 | close(p_fd[i].rd); |
| 4674 | close(p_fd[i].wr); |
| 4675 | } |
| 4676 | errno = EPIPE; |
| 4677 | return posix_error_with_filename(cmdstring); |
| 4678 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4679 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4680 | /* build tuple of file objects to return */ |
| 4681 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 4682 | PyFile_SetBufSize(p_f[0], bufsize); |
| 4683 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 4684 | PyFile_SetBufSize(p_f[1], bufsize); |
| 4685 | if (n == POPEN_3) |
| 4686 | { |
| 4687 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 4688 | PyFile_SetBufSize(p_f[0], bufsize); |
| 4689 | f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]); |
| 4690 | } |
| 4691 | else |
| 4692 | f = PyTuple_Pack(2, p_f[0], p_f[1]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4693 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4694 | /* |
| 4695 | * Insert the files we've created into the process dictionary |
| 4696 | * all referencing the list with the process handle and the |
| 4697 | * initial number of files (see description below in _PyPclose). |
| 4698 | * Since if _PyPclose later tried to wait on a process when all |
| 4699 | * handles weren't closed, it could create a deadlock with the |
| 4700 | * child, we spend some energy here to try to ensure that we |
| 4701 | * either insert all file handles into the dictionary or none |
| 4702 | * at all. It's a little clumsy with the various popen modes |
| 4703 | * and variable number of files involved. |
| 4704 | */ |
| 4705 | if (!_PyPopenProcs) |
| 4706 | { |
| 4707 | _PyPopenProcs = PyDict_New(); |
| 4708 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4709 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4710 | if (_PyPopenProcs) |
| 4711 | { |
| 4712 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 4713 | int ins_rc[3]; |
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 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4716 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4717 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4718 | procObj = PyList_New(2); |
| 4719 | pidObj = PyLong_FromPid(pipe_pid); |
| 4720 | intObj = PyInt_FromLong((long) file_count); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4721 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4722 | if (procObj && pidObj && intObj) |
| 4723 | { |
| 4724 | PyList_SetItem(procObj, 0, pidObj); |
| 4725 | PyList_SetItem(procObj, 1, intObj); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4726 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4727 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 4728 | if (fileObj[0]) |
| 4729 | { |
| 4730 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4731 | fileObj[0], |
| 4732 | procObj); |
| 4733 | } |
| 4734 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 4735 | if (fileObj[1]) |
| 4736 | { |
| 4737 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4738 | fileObj[1], |
| 4739 | procObj); |
| 4740 | } |
| 4741 | if (file_count >= 3) |
| 4742 | { |
| 4743 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 4744 | if (fileObj[2]) |
| 4745 | { |
| 4746 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4747 | fileObj[2], |
| 4748 | procObj); |
| 4749 | } |
| 4750 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4751 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4752 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4753 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4754 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 4755 | { |
| 4756 | /* Something failed - remove any dictionary |
| 4757 | * entries that did make it. |
| 4758 | */ |
| 4759 | if (!ins_rc[0] && fileObj[0]) |
| 4760 | { |
| 4761 | PyDict_DelItem(_PyPopenProcs, |
| 4762 | fileObj[0]); |
| 4763 | } |
| 4764 | if (!ins_rc[1] && fileObj[1]) |
| 4765 | { |
| 4766 | PyDict_DelItem(_PyPopenProcs, |
| 4767 | fileObj[1]); |
| 4768 | } |
| 4769 | if (!ins_rc[2] && fileObj[2]) |
| 4770 | { |
| 4771 | PyDict_DelItem(_PyPopenProcs, |
| 4772 | fileObj[2]); |
| 4773 | } |
| 4774 | } |
| 4775 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4776 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4777 | /* |
| 4778 | * Clean up our localized references for the dictionary keys |
| 4779 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4780 | * that got placed in the dictionary. |
| 4781 | */ |
| 4782 | Py_XDECREF(procObj); |
| 4783 | Py_XDECREF(fileObj[0]); |
| 4784 | Py_XDECREF(fileObj[1]); |
| 4785 | Py_XDECREF(fileObj[2]); |
| 4786 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4787 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4788 | /* Child is launched. */ |
| 4789 | return f; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | /* |
| 4793 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4794 | * exit code for the child process and return as a result of the close. |
| 4795 | * |
| 4796 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4797 | * input file pointer to information about the process that was |
| 4798 | * originally created by the popen* call that created the file pointer. |
| 4799 | * The dictionary uses the file pointer as a key (with one entry |
| 4800 | * inserted for each file returned by the original popen* call) and a |
| 4801 | * single list object as the value for all files from a single call. |
| 4802 | * The list object contains the Win32 process handle at [0], and a file |
| 4803 | * count at [1], which is initialized to the total number of file |
| 4804 | * handles using that list. |
| 4805 | * |
| 4806 | * This function closes whichever handle it is passed, and decrements |
| 4807 | * the file count in the dictionary for the process handle pointed to |
| 4808 | * by this file. On the last close (when the file count reaches zero), |
| 4809 | * this function will wait for the child process and then return its |
| 4810 | * exit code as the result of the close() operation. This permits the |
| 4811 | * files to be closed in any order - it is always the close() of the |
| 4812 | * final handle that will return the exit code. |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4813 | * |
| 4814 | * NOTE: This function is currently called with the GIL released. |
| 4815 | * hence we use the GILState API to manage our state. |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4816 | */ |
| 4817 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4818 | static int _PyPclose(FILE *file) |
| 4819 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4820 | int result; |
| 4821 | int exit_code; |
| 4822 | pid_t pipe_pid; |
| 4823 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 4824 | int file_count; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4825 | #ifdef WITH_THREAD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4826 | PyGILState_STATE state; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4827 | #endif |
| 4828 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4829 | /* Close the file handle first, to ensure it can't block the |
| 4830 | * child from exiting if it's the last handle. |
| 4831 | */ |
| 4832 | result = fclose(file); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4833 | |
| 4834 | #ifdef WITH_THREAD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4835 | state = PyGILState_Ensure(); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4836 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4837 | if (_PyPopenProcs) |
| 4838 | { |
| 4839 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4840 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4841 | fileObj)) != NULL && |
| 4842 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 4843 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 4844 | { |
| 4845 | pipe_pid = (pid_t) PyLong_AsPid(pidObj); |
| 4846 | file_count = (int) PyInt_AsLong(intObj); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4847 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4848 | if (file_count > 1) |
| 4849 | { |
| 4850 | /* Still other files referencing process */ |
| 4851 | file_count--; |
| 4852 | PyList_SetItem(procObj,1, |
| 4853 | PyInt_FromLong((long) file_count)); |
| 4854 | } |
| 4855 | else |
| 4856 | { |
| 4857 | /* Last file for this process */ |
| 4858 | if (result != EOF && |
| 4859 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 4860 | { |
| 4861 | /* extract exit status */ |
| 4862 | if (WIFEXITED(exit_code)) |
| 4863 | { |
| 4864 | result = WEXITSTATUS(exit_code); |
| 4865 | } |
| 4866 | else |
| 4867 | { |
| 4868 | errno = EPIPE; |
| 4869 | result = -1; |
| 4870 | } |
| 4871 | } |
| 4872 | else |
| 4873 | { |
| 4874 | /* Indicate failure - this will cause the file object |
| 4875 | * to raise an I/O error and translate the last |
| 4876 | * error code from errno. We do have a problem with |
| 4877 | * last errors that overlap the normal errno table, |
| 4878 | * but that's a consistent problem with the file object. |
| 4879 | */ |
| 4880 | result = -1; |
| 4881 | } |
| 4882 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4883 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4884 | /* Remove this file pointer from dictionary */ |
| 4885 | PyDict_DelItem(_PyPopenProcs, fileObj); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4886 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4887 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 4888 | { |
| 4889 | Py_DECREF(_PyPopenProcs); |
| 4890 | _PyPopenProcs = NULL; |
| 4891 | } |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4892 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4893 | } /* if object retrieval ok */ |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4894 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4895 | Py_XDECREF(fileObj); |
| 4896 | } /* if _PyPopenProcs */ |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4897 | |
| 4898 | #ifdef WITH_THREAD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4899 | PyGILState_Release(state); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4900 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4901 | return result; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4902 | } |
| 4903 | |
| 4904 | #endif /* PYCC_??? */ |
| 4905 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4906 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4907 | |
| 4908 | /* |
| 4909 | * Portable 'popen' replacement for Win32. |
| 4910 | * |
| 4911 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 4912 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4913 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4914 | */ |
| 4915 | |
| 4916 | #include <malloc.h> |
| 4917 | #include <io.h> |
| 4918 | #include <fcntl.h> |
| 4919 | |
| 4920 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 4921 | #define POPEN_1 1 |
| 4922 | #define POPEN_2 2 |
| 4923 | #define POPEN_3 3 |
| 4924 | #define POPEN_4 4 |
| 4925 | |
| 4926 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4927 | static int _PyPclose(FILE *file); |
| 4928 | |
| 4929 | /* |
| 4930 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4931 | * for use when retrieving the process exit code. See _PyPclose() below |
| 4932 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4933 | */ |
| 4934 | static PyObject *_PyPopenProcs = NULL; |
| 4935 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4936 | |
| 4937 | /* popen that works from a GUI. |
| 4938 | * |
| 4939 | * The result of this function is a pipe (file) connected to the |
| 4940 | * processes stdin or stdout, depending on the requested mode. |
| 4941 | */ |
| 4942 | |
| 4943 | static PyObject * |
| 4944 | posix_popen(PyObject *self, PyObject *args) |
| 4945 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4946 | PyObject *f; |
| 4947 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4948 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4949 | char *cmdstring; |
| 4950 | char *mode = "r"; |
| 4951 | int bufsize = -1; |
| 4952 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
| 4953 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4954 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4955 | if (*mode == 'r') |
| 4956 | tm = _O_RDONLY; |
| 4957 | else if (*mode != 'w') { |
| 4958 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
| 4959 | return NULL; |
| 4960 | } else |
| 4961 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4962 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4963 | if (bufsize != -1) { |
| 4964 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
| 4965 | return NULL; |
| 4966 | } |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4967 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4968 | if (*(mode+1) == 't') |
| 4969 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 4970 | else if (*(mode+1) == 'b') |
| 4971 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
| 4972 | else |
| 4973 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4974 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4975 | return f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4976 | } |
| 4977 | |
| 4978 | /* Variation on win32pipe.popen |
| 4979 | * |
| 4980 | * The result of this function is a pipe (file) connected to the |
| 4981 | * process's stdin, and a pipe connected to the process's stdout. |
| 4982 | */ |
| 4983 | |
| 4984 | static PyObject * |
| 4985 | win32_popen2(PyObject *self, PyObject *args) |
| 4986 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4987 | PyObject *f; |
| 4988 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4989 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4990 | char *cmdstring; |
| 4991 | char *mode = "t"; |
| 4992 | int bufsize = -1; |
| 4993 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 4994 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4995 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 4996 | if (*mode == 't') |
| 4997 | tm = _O_TEXT; |
| 4998 | else if (*mode != 'b') { |
| 4999 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
| 5000 | return NULL; |
| 5001 | } else |
| 5002 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5003 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5004 | if (bufsize != -1) { |
| 5005 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
| 5006 | return NULL; |
| 5007 | } |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 5008 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5009 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5010 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5011 | return f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5012 | } |
| 5013 | |
| 5014 | /* |
| 5015 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 5016 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5017 | * The result of this function is 3 pipes - the process's stdin, |
| 5018 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5019 | */ |
| 5020 | |
| 5021 | static PyObject * |
| 5022 | win32_popen3(PyObject *self, PyObject *args) |
| 5023 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5024 | PyObject *f; |
| 5025 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5026 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5027 | char *cmdstring; |
| 5028 | char *mode = "t"; |
| 5029 | int bufsize = -1; |
| 5030 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 5031 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5032 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5033 | if (*mode == 't') |
| 5034 | tm = _O_TEXT; |
| 5035 | else if (*mode != 'b') { |
| 5036 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
| 5037 | return NULL; |
| 5038 | } else |
| 5039 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5040 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5041 | if (bufsize != -1) { |
| 5042 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
| 5043 | return NULL; |
| 5044 | } |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 5045 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5046 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5047 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5048 | return f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5049 | } |
| 5050 | |
| 5051 | /* |
| 5052 | * Variation on win32pipe.popen |
| 5053 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5054 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5055 | * and stdout+stderr combined as a single pipe. |
| 5056 | */ |
| 5057 | |
| 5058 | static PyObject * |
| 5059 | win32_popen4(PyObject *self, PyObject *args) |
| 5060 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5061 | PyObject *f; |
| 5062 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5063 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5064 | char *cmdstring; |
| 5065 | char *mode = "t"; |
| 5066 | int bufsize = -1; |
| 5067 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 5068 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5069 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5070 | if (*mode == 't') |
| 5071 | tm = _O_TEXT; |
| 5072 | else if (*mode != 'b') { |
| 5073 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
| 5074 | return NULL; |
| 5075 | } else |
| 5076 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 5077 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5078 | if (bufsize != -1) { |
| 5079 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
| 5080 | return NULL; |
| 5081 | } |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 5082 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5083 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 5084 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5085 | return f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 5088 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5089 | _PyPopenCreateProcess(char *cmdstring, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5090 | HANDLE hStdin, |
| 5091 | HANDLE hStdout, |
| 5092 | HANDLE hStderr, |
| 5093 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5094 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5095 | PROCESS_INFORMATION piProcInfo; |
| 5096 | STARTUPINFO siStartInfo; |
| 5097 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
| 5098 | char *s1,*s2, *s3 = " /c "; |
| 5099 | const char *szConsoleSpawn = "w9xpopen.exe"; |
| 5100 | int i; |
| 5101 | Py_ssize_t x; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5102 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5103 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
| 5104 | char *comshell; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 5105 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5106 | s1 = (char *)alloca(i); |
| 5107 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 5108 | /* x < i, so x fits into an integer */ |
| 5109 | return (int)x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 5110 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5111 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 5112 | * then use the w9xpopen hack. |
| 5113 | */ |
| 5114 | comshell = s1 + x; |
| 5115 | while (comshell >= s1 && *comshell != '\\') |
| 5116 | --comshell; |
| 5117 | ++comshell; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 5118 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5119 | if (GetVersion() < 0x80000000 && |
| 5120 | _stricmp(comshell, "command.com") != 0) { |
| 5121 | /* NT/2000 and not using command.com. */ |
| 5122 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
| 5123 | s2 = (char *)alloca(x); |
| 5124 | ZeroMemory(s2, x); |
| 5125 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
| 5126 | } |
| 5127 | else { |
| 5128 | /* |
| 5129 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 5130 | * the workaround listed in KB: Q150956 |
| 5131 | */ |
| 5132 | char modulepath[_MAX_PATH]; |
| 5133 | struct stat statinfo; |
| 5134 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 5135 | for (x = i = 0; modulepath[i]; i++) |
| 5136 | if (modulepath[i] == SEP) |
| 5137 | x = i+1; |
| 5138 | modulepath[x] = '\0'; |
| 5139 | /* Create the full-name to w9xpopen, so we can test it exists */ |
| 5140 | strncat(modulepath, |
| 5141 | szConsoleSpawn, |
| 5142 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 5143 | -strlen(modulepath)); |
| 5144 | if (stat(modulepath, &statinfo) != 0) { |
| 5145 | size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]); |
| 5146 | /* Eeek - file-not-found - possibly an embedding |
| 5147 | situation - see if we can locate it in sys.prefix |
| 5148 | */ |
| 5149 | strncpy(modulepath, |
| 5150 | Py_GetExecPrefix(), |
| 5151 | mplen); |
| 5152 | modulepath[mplen-1] = '\0'; |
| 5153 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 5154 | strcat(modulepath, "\\"); |
| 5155 | strncat(modulepath, |
| 5156 | szConsoleSpawn, |
| 5157 | mplen-strlen(modulepath)); |
| 5158 | /* No where else to look - raise an easily identifiable |
| 5159 | error, rather than leaving Windows to report |
| 5160 | "file not found" - as the user is probably blissfully |
| 5161 | unaware this shim EXE is used, and it will confuse them. |
| 5162 | (well, it confused me for a while ;-) |
| 5163 | */ |
| 5164 | if (stat(modulepath, &statinfo) != 0) { |
| 5165 | PyErr_Format(PyExc_RuntimeError, |
| 5166 | "Can not locate '%s' which is needed " |
| 5167 | "for popen to work with your shell " |
| 5168 | "or platform.", |
| 5169 | szConsoleSpawn); |
| 5170 | return FALSE; |
| 5171 | } |
| 5172 | } |
| 5173 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
| 5174 | strlen(modulepath) + |
| 5175 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 5176 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5177 | s2 = (char *)alloca(x); |
| 5178 | ZeroMemory(s2, x); |
| 5179 | /* To maintain correct argument passing semantics, |
| 5180 | we pass the command-line as it stands, and allow |
| 5181 | quoting to be applied. w9xpopen.exe will then |
| 5182 | use its argv vector, and re-quote the necessary |
| 5183 | args for the ultimate child process. |
| 5184 | */ |
| 5185 | PyOS_snprintf( |
| 5186 | s2, x, |
| 5187 | "\"%s\" %s%s%s", |
| 5188 | modulepath, |
| 5189 | s1, |
| 5190 | s3, |
| 5191 | cmdstring); |
| 5192 | /* Not passing CREATE_NEW_CONSOLE has been known to |
| 5193 | cause random failures on win9x. Specifically a |
| 5194 | dialog: |
| 5195 | "Your program accessed mem currently in use at xxx" |
| 5196 | and a hopeful warning about the stability of your |
| 5197 | system. |
| 5198 | Cost is Ctrl+C won't kill children, but anyone |
| 5199 | who cares can have a go! |
| 5200 | */ |
| 5201 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
| 5202 | } |
| 5203 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5204 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5205 | /* Could be an else here to try cmd.exe / command.com in the path |
| 5206 | Now we'll just error out.. */ |
| 5207 | else { |
| 5208 | PyErr_SetString(PyExc_RuntimeError, |
| 5209 | "Cannot locate a COMSPEC environment variable to " |
| 5210 | "use as the shell"); |
| 5211 | return FALSE; |
| 5212 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5213 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5214 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 5215 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 5216 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 5217 | siStartInfo.hStdInput = hStdin; |
| 5218 | siStartInfo.hStdOutput = hStdout; |
| 5219 | siStartInfo.hStdError = hStderr; |
| 5220 | siStartInfo.wShowWindow = SW_HIDE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5221 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5222 | if (CreateProcess(NULL, |
| 5223 | s2, |
| 5224 | NULL, |
| 5225 | NULL, |
| 5226 | TRUE, |
| 5227 | dwProcessFlags, |
| 5228 | NULL, |
| 5229 | NULL, |
| 5230 | &siStartInfo, |
| 5231 | &piProcInfo) ) { |
| 5232 | /* Close the handles now so anyone waiting is woken. */ |
| 5233 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5234 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5235 | /* Return process handle */ |
| 5236 | *hProcess = piProcInfo.hProcess; |
| 5237 | return TRUE; |
| 5238 | } |
| 5239 | win32_error("CreateProcess", s2); |
| 5240 | return FALSE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5241 | } |
| 5242 | |
| 5243 | /* The following code is based off of KB: Q190351 */ |
| 5244 | |
| 5245 | static PyObject * |
| 5246 | _PyPopen(char *cmdstring, int mode, int n) |
| 5247 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5248 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 5249 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
| 5250 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5251 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5252 | SECURITY_ATTRIBUTES saAttr; |
| 5253 | BOOL fSuccess; |
| 5254 | int fd1, fd2, fd3; |
| 5255 | FILE *f1, *f2, *f3; |
| 5256 | long file_count; |
| 5257 | PyObject *f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5258 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5259 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 5260 | saAttr.bInheritHandle = TRUE; |
| 5261 | saAttr.lpSecurityDescriptor = NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5262 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5263 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 5264 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5265 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5266 | /* Create new output read handle and the input write handle. Set |
| 5267 | * the inheritance properties to FALSE. Otherwise, the child inherits |
| 5268 | * these handles; resulting in non-closeable handles to the pipes |
| 5269 | * being created. */ |
| 5270 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
| 5271 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 5272 | FALSE, |
| 5273 | DUPLICATE_SAME_ACCESS); |
| 5274 | if (!fSuccess) |
| 5275 | return win32_error("DuplicateHandle", NULL); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5276 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5277 | /* Close the inheritable version of ChildStdin |
| 5278 | that we're using. */ |
| 5279 | CloseHandle(hChildStdinWr); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5280 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5281 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 5282 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5283 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5284 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
| 5285 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 5286 | FALSE, DUPLICATE_SAME_ACCESS); |
| 5287 | if (!fSuccess) |
| 5288 | return win32_error("DuplicateHandle", NULL); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5289 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5290 | /* Close the inheritable version of ChildStdout |
| 5291 | that we're using. */ |
| 5292 | CloseHandle(hChildStdoutRd); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5293 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5294 | if (n != POPEN_4) { |
| 5295 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 5296 | return win32_error("CreatePipe", NULL); |
| 5297 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 5298 | hChildStderrRd, |
| 5299 | GetCurrentProcess(), |
| 5300 | &hChildStderrRdDup, 0, |
| 5301 | FALSE, DUPLICATE_SAME_ACCESS); |
| 5302 | if (!fSuccess) |
| 5303 | return win32_error("DuplicateHandle", NULL); |
| 5304 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 5305 | CloseHandle(hChildStderrRd); |
| 5306 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5307 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5308 | switch (n) { |
| 5309 | case POPEN_1: |
| 5310 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 5311 | case _O_WRONLY | _O_TEXT: |
| 5312 | /* Case for writing to child Stdin in text mode. */ |
| 5313 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
| 5314 | f1 = _fdopen(fd1, "w"); |
| 5315 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
| 5316 | PyFile_SetBufSize(f, 0); |
| 5317 | /* We don't care about these pipes anymore, so close them. */ |
| 5318 | CloseHandle(hChildStdoutRdDup); |
| 5319 | CloseHandle(hChildStderrRdDup); |
| 5320 | break; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5321 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5322 | case _O_RDONLY | _O_TEXT: |
| 5323 | /* Case for reading from child Stdout in text mode. */ |
| 5324 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
| 5325 | f1 = _fdopen(fd1, "r"); |
| 5326 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
| 5327 | PyFile_SetBufSize(f, 0); |
| 5328 | /* We don't care about these pipes anymore, so close them. */ |
| 5329 | CloseHandle(hChildStdinWrDup); |
| 5330 | CloseHandle(hChildStderrRdDup); |
| 5331 | break; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5332 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5333 | case _O_RDONLY | _O_BINARY: |
| 5334 | /* Case for readinig from child Stdout in binary mode. */ |
| 5335 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
| 5336 | f1 = _fdopen(fd1, "rb"); |
| 5337 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
| 5338 | PyFile_SetBufSize(f, 0); |
| 5339 | /* We don't care about these pipes anymore, so close them. */ |
| 5340 | CloseHandle(hChildStdinWrDup); |
| 5341 | CloseHandle(hChildStderrRdDup); |
| 5342 | break; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5343 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5344 | case _O_WRONLY | _O_BINARY: |
| 5345 | /* Case for writing to child Stdin in binary mode. */ |
| 5346 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
| 5347 | f1 = _fdopen(fd1, "wb"); |
| 5348 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
| 5349 | PyFile_SetBufSize(f, 0); |
| 5350 | /* We don't care about these pipes anymore, so close them. */ |
| 5351 | CloseHandle(hChildStdoutRdDup); |
| 5352 | CloseHandle(hChildStderrRdDup); |
| 5353 | break; |
| 5354 | } |
| 5355 | file_count = 1; |
| 5356 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5357 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5358 | case POPEN_2: |
| 5359 | case POPEN_4: |
| 5360 | { |
| 5361 | char *m1, *m2; |
| 5362 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5363 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5364 | if (mode & _O_TEXT) { |
| 5365 | m1 = "r"; |
| 5366 | m2 = "w"; |
| 5367 | } else { |
| 5368 | m1 = "rb"; |
| 5369 | m2 = "wb"; |
| 5370 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5371 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5372 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
| 5373 | f1 = _fdopen(fd1, m2); |
| 5374 | fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
| 5375 | f2 = _fdopen(fd2, m1); |
| 5376 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
| 5377 | PyFile_SetBufSize(p1, 0); |
| 5378 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 5379 | PyFile_SetBufSize(p2, 0); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5380 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5381 | if (n != 4) |
| 5382 | CloseHandle(hChildStderrRdDup); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5383 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5384 | f = PyTuple_Pack(2,p1,p2); |
| 5385 | Py_XDECREF(p1); |
| 5386 | Py_XDECREF(p2); |
| 5387 | file_count = 2; |
| 5388 | break; |
| 5389 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5390 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5391 | case POPEN_3: |
| 5392 | { |
| 5393 | char *m1, *m2; |
| 5394 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5395 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5396 | if (mode & _O_TEXT) { |
| 5397 | m1 = "r"; |
| 5398 | m2 = "w"; |
| 5399 | } else { |
| 5400 | m1 = "rb"; |
| 5401 | m2 = "wb"; |
| 5402 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5403 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5404 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
| 5405 | f1 = _fdopen(fd1, m2); |
| 5406 | fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
| 5407 | f2 = _fdopen(fd2, m1); |
| 5408 | fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode); |
| 5409 | f3 = _fdopen(fd3, m1); |
| 5410 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
| 5411 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 5412 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
| 5413 | PyFile_SetBufSize(p1, 0); |
| 5414 | PyFile_SetBufSize(p2, 0); |
| 5415 | PyFile_SetBufSize(p3, 0); |
| 5416 | f = PyTuple_Pack(3,p1,p2,p3); |
| 5417 | Py_XDECREF(p1); |
| 5418 | Py_XDECREF(p2); |
| 5419 | Py_XDECREF(p3); |
| 5420 | file_count = 3; |
| 5421 | break; |
| 5422 | } |
| 5423 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5424 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5425 | if (n == POPEN_4) { |
| 5426 | if (!_PyPopenCreateProcess(cmdstring, |
| 5427 | hChildStdinRd, |
| 5428 | hChildStdoutWr, |
| 5429 | hChildStdoutWr, |
| 5430 | &hProcess)) |
| 5431 | return NULL; |
| 5432 | } |
| 5433 | else { |
| 5434 | if (!_PyPopenCreateProcess(cmdstring, |
| 5435 | hChildStdinRd, |
| 5436 | hChildStdoutWr, |
| 5437 | hChildStderrWr, |
| 5438 | &hProcess)) |
| 5439 | return NULL; |
| 5440 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5441 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5442 | /* |
| 5443 | * Insert the files we've created into the process dictionary |
| 5444 | * all referencing the list with the process handle and the |
| 5445 | * initial number of files (see description below in _PyPclose). |
| 5446 | * Since if _PyPclose later tried to wait on a process when all |
| 5447 | * handles weren't closed, it could create a deadlock with the |
| 5448 | * child, we spend some energy here to try to ensure that we |
| 5449 | * either insert all file handles into the dictionary or none |
| 5450 | * at all. It's a little clumsy with the various popen modes |
| 5451 | * and variable number of files involved. |
| 5452 | */ |
| 5453 | if (!_PyPopenProcs) { |
| 5454 | _PyPopenProcs = PyDict_New(); |
| 5455 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5456 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5457 | if (_PyPopenProcs) { |
| 5458 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 5459 | int ins_rc[3]; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5460 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5461 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 5462 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5463 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5464 | procObj = PyList_New(2); |
| 5465 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 5466 | intObj = PyInt_FromLong(file_count); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5467 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5468 | if (procObj && hProcessObj && intObj) { |
| 5469 | PyList_SetItem(procObj,0,hProcessObj); |
| 5470 | PyList_SetItem(procObj,1,intObj); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5471 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5472 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 5473 | if (fileObj[0]) { |
| 5474 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 5475 | fileObj[0], |
| 5476 | procObj); |
| 5477 | } |
| 5478 | if (file_count >= 2) { |
| 5479 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 5480 | if (fileObj[1]) { |
| 5481 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 5482 | fileObj[1], |
| 5483 | procObj); |
| 5484 | } |
| 5485 | } |
| 5486 | if (file_count >= 3) { |
| 5487 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 5488 | if (fileObj[2]) { |
| 5489 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 5490 | fileObj[2], |
| 5491 | procObj); |
| 5492 | } |
| 5493 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5494 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5495 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 5496 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 5497 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 5498 | /* Something failed - remove any dictionary |
| 5499 | * entries that did make it. |
| 5500 | */ |
| 5501 | if (!ins_rc[0] && fileObj[0]) { |
| 5502 | PyDict_DelItem(_PyPopenProcs, |
| 5503 | fileObj[0]); |
| 5504 | } |
| 5505 | if (!ins_rc[1] && fileObj[1]) { |
| 5506 | PyDict_DelItem(_PyPopenProcs, |
| 5507 | fileObj[1]); |
| 5508 | } |
| 5509 | if (!ins_rc[2] && fileObj[2]) { |
| 5510 | PyDict_DelItem(_PyPopenProcs, |
| 5511 | fileObj[2]); |
| 5512 | } |
| 5513 | } |
| 5514 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5515 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5516 | /* |
| 5517 | * Clean up our localized references for the dictionary keys |
| 5518 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 5519 | * that got placed in the dictionary. |
| 5520 | */ |
| 5521 | Py_XDECREF(procObj); |
| 5522 | Py_XDECREF(fileObj[0]); |
| 5523 | Py_XDECREF(fileObj[1]); |
| 5524 | Py_XDECREF(fileObj[2]); |
| 5525 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5526 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5527 | /* Child is launched. Close the parents copy of those pipe |
| 5528 | * handles that only the child should have open. You need to |
| 5529 | * make sure that no handles to the write end of the output pipe |
| 5530 | * are maintained in this process or else the pipe will not close |
| 5531 | * when the child process exits and the ReadFile will hang. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5532 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5533 | if (!CloseHandle(hChildStdinRd)) |
| 5534 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5535 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5536 | if (!CloseHandle(hChildStdoutWr)) |
| 5537 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5538 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5539 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 5540 | return win32_error("CloseHandle", NULL); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5541 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5542 | return f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5543 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5544 | |
| 5545 | /* |
| 5546 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 5547 | * 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] | 5548 | * |
| 5549 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 5550 | * input file pointer to information about the process that was |
| 5551 | * originally created by the popen* call that created the file pointer. |
| 5552 | * The dictionary uses the file pointer as a key (with one entry |
| 5553 | * inserted for each file returned by the original popen* call) and a |
| 5554 | * single list object as the value for all files from a single call. |
| 5555 | * The list object contains the Win32 process handle at [0], and a file |
| 5556 | * count at [1], which is initialized to the total number of file |
| 5557 | * handles using that list. |
| 5558 | * |
| 5559 | * This function closes whichever handle it is passed, and decrements |
| 5560 | * the file count in the dictionary for the process handle pointed to |
| 5561 | * by this file. On the last close (when the file count reaches zero), |
| 5562 | * this function will wait for the child process and then return its |
| 5563 | * exit code as the result of the close() operation. This permits the |
| 5564 | * files to be closed in any order - it is always the close() of the |
| 5565 | * final handle that will return the exit code. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 5566 | * |
| 5567 | * NOTE: This function is currently called with the GIL released. |
| 5568 | * hence we use the GILState API to manage our state. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5569 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5570 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5571 | static int _PyPclose(FILE *file) |
| 5572 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5573 | int result; |
| 5574 | DWORD exit_code; |
| 5575 | HANDLE hProcess; |
| 5576 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 5577 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5578 | #ifdef WITH_THREAD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5579 | PyGILState_STATE state; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5580 | #endif |
| 5581 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5582 | /* Close the file handle first, to ensure it can't block the |
| 5583 | * child from exiting if it's the last handle. |
| 5584 | */ |
| 5585 | result = fclose(file); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5586 | #ifdef WITH_THREAD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5587 | state = PyGILState_Ensure(); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5588 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5589 | if (_PyPopenProcs) { |
| 5590 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 5591 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 5592 | fileObj)) != NULL && |
| 5593 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 5594 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5595 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5596 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 5597 | file_count = PyInt_AsLong(intObj); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5598 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5599 | if (file_count > 1) { |
| 5600 | /* Still other files referencing process */ |
| 5601 | file_count--; |
| 5602 | PyList_SetItem(procObj,1, |
| 5603 | PyInt_FromLong(file_count)); |
| 5604 | } else { |
| 5605 | /* Last file for this process */ |
| 5606 | if (result != EOF && |
| 5607 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 5608 | GetExitCodeProcess(hProcess, &exit_code)) { |
| 5609 | /* Possible truncation here in 16-bit environments, but |
| 5610 | * real exit codes are just the lower byte in any event. |
| 5611 | */ |
| 5612 | result = exit_code; |
| 5613 | } else { |
| 5614 | /* Indicate failure - this will cause the file object |
| 5615 | * to raise an I/O error and translate the last Win32 |
| 5616 | * error code from errno. We do have a problem with |
| 5617 | * last errors that overlap the normal errno table, |
| 5618 | * but that's a consistent problem with the file object. |
| 5619 | */ |
| 5620 | if (result != EOF) { |
| 5621 | /* If the error wasn't from the fclose(), then |
| 5622 | * set errno for the file object error handling. |
| 5623 | */ |
| 5624 | errno = GetLastError(); |
| 5625 | } |
| 5626 | result = -1; |
| 5627 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5628 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5629 | /* Free up the native handle at this point */ |
| 5630 | CloseHandle(hProcess); |
| 5631 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5632 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5633 | /* Remove this file pointer from dictionary */ |
| 5634 | PyDict_DelItem(_PyPopenProcs, fileObj); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5635 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5636 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 5637 | Py_DECREF(_PyPopenProcs); |
| 5638 | _PyPopenProcs = NULL; |
| 5639 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5640 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5641 | } /* if object retrieval ok */ |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5642 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5643 | Py_XDECREF(fileObj); |
| 5644 | } /* if _PyPopenProcs */ |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5645 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5646 | #ifdef WITH_THREAD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5647 | PyGILState_Release(state); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5648 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5649 | return result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5650 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 5651 | |
| 5652 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5653 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5654 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5655 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5656 | char *name; |
| 5657 | char *mode = "r"; |
| 5658 | int bufsize = -1; |
| 5659 | FILE *fp; |
| 5660 | PyObject *f; |
| 5661 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 5662 | return NULL; |
| 5663 | /* Strip mode of binary or text modifiers */ |
| 5664 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) |
| 5665 | mode = "r"; |
| 5666 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) |
| 5667 | mode = "w"; |
| 5668 | Py_BEGIN_ALLOW_THREADS |
| 5669 | fp = popen(name, mode); |
| 5670 | Py_END_ALLOW_THREADS |
| 5671 | if (fp == NULL) |
| 5672 | return posix_error(); |
| 5673 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 5674 | if (f != NULL) |
| 5675 | PyFile_SetBufSize(f, bufsize); |
| 5676 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5677 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5678 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5679 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5680 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5681 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5682 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5683 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5684 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5685 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5686 | Set the current process's user id."); |
| 5687 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5688 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5689 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5690 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5691 | long uid_arg; |
| 5692 | uid_t uid; |
| 5693 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 5694 | return NULL; |
| 5695 | uid = uid_arg; |
| 5696 | if (uid != uid_arg) { |
| 5697 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5698 | return NULL; |
| 5699 | } |
| 5700 | if (setuid(uid) < 0) |
| 5701 | return posix_error(); |
| 5702 | Py_INCREF(Py_None); |
| 5703 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5704 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5705 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5706 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5707 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5708 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5709 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5710 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5711 | Set the current process's effective user id."); |
| 5712 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5713 | static PyObject * |
| 5714 | posix_seteuid (PyObject *self, PyObject *args) |
| 5715 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5716 | long euid_arg; |
| 5717 | uid_t euid; |
| 5718 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 5719 | return NULL; |
| 5720 | euid = euid_arg; |
| 5721 | if (euid != euid_arg) { |
| 5722 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5723 | return NULL; |
| 5724 | } |
| 5725 | if (seteuid(euid) < 0) { |
| 5726 | return posix_error(); |
| 5727 | } else { |
| 5728 | Py_INCREF(Py_None); |
| 5729 | return Py_None; |
| 5730 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5731 | } |
| 5732 | #endif /* HAVE_SETEUID */ |
| 5733 | |
| 5734 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5735 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5736 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5737 | Set the current process's effective group id."); |
| 5738 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5739 | static PyObject * |
| 5740 | posix_setegid (PyObject *self, PyObject *args) |
| 5741 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5742 | long egid_arg; |
| 5743 | gid_t egid; |
| 5744 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 5745 | return NULL; |
| 5746 | egid = egid_arg; |
| 5747 | if (egid != egid_arg) { |
| 5748 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5749 | return NULL; |
| 5750 | } |
| 5751 | if (setegid(egid) < 0) { |
| 5752 | return posix_error(); |
| 5753 | } else { |
| 5754 | Py_INCREF(Py_None); |
| 5755 | return Py_None; |
| 5756 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5757 | } |
| 5758 | #endif /* HAVE_SETEGID */ |
| 5759 | |
| 5760 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5761 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5762 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5763 | Set the current process's real and effective user ids."); |
| 5764 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5765 | static PyObject * |
| 5766 | posix_setreuid (PyObject *self, PyObject *args) |
| 5767 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5768 | long ruid_arg, euid_arg; |
| 5769 | uid_t ruid, euid; |
| 5770 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 5771 | return NULL; |
| 5772 | if (ruid_arg == -1) |
| 5773 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 5774 | else |
| 5775 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 5776 | if (euid_arg == -1) |
| 5777 | euid = (uid_t)-1; |
| 5778 | else |
| 5779 | euid = euid_arg; |
| 5780 | if ((euid_arg != -1 && euid != euid_arg) || |
| 5781 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 5782 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5783 | return NULL; |
| 5784 | } |
| 5785 | if (setreuid(ruid, euid) < 0) { |
| 5786 | return posix_error(); |
| 5787 | } else { |
| 5788 | Py_INCREF(Py_None); |
| 5789 | return Py_None; |
| 5790 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5791 | } |
| 5792 | #endif /* HAVE_SETREUID */ |
| 5793 | |
| 5794 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5795 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5796 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5797 | Set the current process's real and effective group ids."); |
| 5798 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5799 | static PyObject * |
| 5800 | posix_setregid (PyObject *self, PyObject *args) |
| 5801 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5802 | long rgid_arg, egid_arg; |
| 5803 | gid_t rgid, egid; |
| 5804 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 5805 | return NULL; |
| 5806 | if (rgid_arg == -1) |
| 5807 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 5808 | else |
| 5809 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 5810 | if (egid_arg == -1) |
| 5811 | egid = (gid_t)-1; |
| 5812 | else |
| 5813 | egid = egid_arg; |
| 5814 | if ((egid_arg != -1 && egid != egid_arg) || |
| 5815 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 5816 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5817 | return NULL; |
| 5818 | } |
| 5819 | if (setregid(rgid, egid) < 0) { |
| 5820 | return posix_error(); |
| 5821 | } else { |
| 5822 | Py_INCREF(Py_None); |
| 5823 | return Py_None; |
| 5824 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5825 | } |
| 5826 | #endif /* HAVE_SETREGID */ |
| 5827 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5828 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5829 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5830 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5831 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5832 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5833 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5834 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5835 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5836 | long gid_arg; |
| 5837 | gid_t gid; |
| 5838 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 5839 | return NULL; |
| 5840 | gid = gid_arg; |
| 5841 | if (gid != gid_arg) { |
| 5842 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5843 | return NULL; |
| 5844 | } |
| 5845 | if (setgid(gid) < 0) |
| 5846 | return posix_error(); |
| 5847 | Py_INCREF(Py_None); |
| 5848 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5849 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5850 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5851 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5852 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5853 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5854 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5855 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5856 | |
| 5857 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5858 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5859 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5860 | int i, len; |
| 5861 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5862 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5863 | if (!PySequence_Check(groups)) { |
| 5864 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 5865 | return NULL; |
| 5866 | } |
| 5867 | len = PySequence_Size(groups); |
| 5868 | if (len > MAX_GROUPS) { |
| 5869 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 5870 | return NULL; |
| 5871 | } |
| 5872 | for(i = 0; i < len; i++) { |
| 5873 | PyObject *elem; |
| 5874 | elem = PySequence_GetItem(groups, i); |
| 5875 | if (!elem) |
| 5876 | return NULL; |
| 5877 | if (!PyInt_Check(elem)) { |
| 5878 | if (!PyLong_Check(elem)) { |
| 5879 | PyErr_SetString(PyExc_TypeError, |
| 5880 | "groups must be integers"); |
| 5881 | Py_DECREF(elem); |
| 5882 | return NULL; |
| 5883 | } else { |
| 5884 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 5885 | if (PyErr_Occurred()) { |
| 5886 | PyErr_SetString(PyExc_TypeError, |
| 5887 | "group id too big"); |
| 5888 | Py_DECREF(elem); |
| 5889 | return NULL; |
| 5890 | } |
| 5891 | grouplist[i] = x; |
| 5892 | /* read back to see if it fits in gid_t */ |
| 5893 | if (grouplist[i] != x) { |
| 5894 | PyErr_SetString(PyExc_TypeError, |
| 5895 | "group id too big"); |
| 5896 | Py_DECREF(elem); |
| 5897 | return NULL; |
| 5898 | } |
| 5899 | } |
| 5900 | } else { |
| 5901 | long x = PyInt_AsLong(elem); |
| 5902 | grouplist[i] = x; |
| 5903 | if (grouplist[i] != x) { |
| 5904 | PyErr_SetString(PyExc_TypeError, |
| 5905 | "group id too big"); |
| 5906 | Py_DECREF(elem); |
| 5907 | return NULL; |
| 5908 | } |
| 5909 | } |
| 5910 | Py_DECREF(elem); |
| 5911 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5912 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5913 | if (setgroups(len, grouplist) < 0) |
| 5914 | return posix_error(); |
| 5915 | Py_INCREF(Py_None); |
| 5916 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5917 | } |
| 5918 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5919 | |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 5920 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5921 | static PyObject * |
Christian Heimes | d491d71 | 2008-02-01 18:49:26 +0000 | [diff] [blame] | 5922 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5923 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5924 | PyObject *result; |
| 5925 | static PyObject *struct_rusage; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5926 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5927 | if (pid == -1) |
| 5928 | return posix_error(); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5929 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5930 | if (struct_rusage == NULL) { |
| 5931 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 5932 | if (m == NULL) |
| 5933 | return NULL; |
| 5934 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 5935 | Py_DECREF(m); |
| 5936 | if (struct_rusage == NULL) |
| 5937 | return NULL; |
| 5938 | } |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5939 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5940 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 5941 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 5942 | if (!result) |
| 5943 | return NULL; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5944 | |
| 5945 | #ifndef doubletime |
| 5946 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 5947 | #endif |
| 5948 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5949 | PyStructSequence_SET_ITEM(result, 0, |
| 5950 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 5951 | PyStructSequence_SET_ITEM(result, 1, |
| 5952 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5953 | #define SET_INT(result, index, value)\ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5954 | PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value)) |
| 5955 | SET_INT(result, 2, ru->ru_maxrss); |
| 5956 | SET_INT(result, 3, ru->ru_ixrss); |
| 5957 | SET_INT(result, 4, ru->ru_idrss); |
| 5958 | SET_INT(result, 5, ru->ru_isrss); |
| 5959 | SET_INT(result, 6, ru->ru_minflt); |
| 5960 | SET_INT(result, 7, ru->ru_majflt); |
| 5961 | SET_INT(result, 8, ru->ru_nswap); |
| 5962 | SET_INT(result, 9, ru->ru_inblock); |
| 5963 | SET_INT(result, 10, ru->ru_oublock); |
| 5964 | SET_INT(result, 11, ru->ru_msgsnd); |
| 5965 | SET_INT(result, 12, ru->ru_msgrcv); |
| 5966 | SET_INT(result, 13, ru->ru_nsignals); |
| 5967 | SET_INT(result, 14, ru->ru_nvcsw); |
| 5968 | SET_INT(result, 15, ru->ru_nivcsw); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5969 | #undef SET_INT |
| 5970 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5971 | if (PyErr_Occurred()) { |
| 5972 | Py_DECREF(result); |
| 5973 | return NULL; |
| 5974 | } |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5975 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5976 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5977 | } |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 5978 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5979 | |
| 5980 | #ifdef HAVE_WAIT3 |
| 5981 | PyDoc_STRVAR(posix_wait3__doc__, |
| 5982 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 5983 | Wait for completion of a child process."); |
| 5984 | |
| 5985 | static PyObject * |
| 5986 | posix_wait3(PyObject *self, PyObject *args) |
| 5987 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5988 | pid_t pid; |
| 5989 | int options; |
| 5990 | struct rusage ru; |
| 5991 | WAIT_TYPE status; |
| 5992 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5993 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5994 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 5995 | return NULL; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5996 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 5997 | Py_BEGIN_ALLOW_THREADS |
| 5998 | pid = wait3(&status, options, &ru); |
| 5999 | Py_END_ALLOW_THREADS |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6000 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6001 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6002 | } |
| 6003 | #endif /* HAVE_WAIT3 */ |
| 6004 | |
| 6005 | #ifdef HAVE_WAIT4 |
| 6006 | PyDoc_STRVAR(posix_wait4__doc__, |
| 6007 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 6008 | Wait for completion of a given child process."); |
| 6009 | |
| 6010 | static PyObject * |
| 6011 | posix_wait4(PyObject *self, PyObject *args) |
| 6012 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6013 | pid_t pid; |
| 6014 | int options; |
| 6015 | struct rusage ru; |
| 6016 | WAIT_TYPE status; |
| 6017 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6018 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6019 | if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options)) |
| 6020 | return NULL; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6021 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6022 | Py_BEGIN_ALLOW_THREADS |
| 6023 | pid = wait4(pid, &status, options, &ru); |
| 6024 | Py_END_ALLOW_THREADS |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6025 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6026 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6027 | } |
| 6028 | #endif /* HAVE_WAIT4 */ |
| 6029 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6030 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6031 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6032 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6033 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6034 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6035 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6036 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6037 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6038 | pid_t pid; |
| 6039 | int options; |
| 6040 | WAIT_TYPE status; |
| 6041 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6042 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6043 | if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) |
| 6044 | return NULL; |
| 6045 | Py_BEGIN_ALLOW_THREADS |
| 6046 | pid = waitpid(pid, &status, options); |
| 6047 | Py_END_ALLOW_THREADS |
| 6048 | if (pid == -1) |
| 6049 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6050 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6051 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6052 | } |
| 6053 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6054 | #elif defined(HAVE_CWAIT) |
| 6055 | |
| 6056 | /* 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] | 6057 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6058 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6059 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6060 | |
| 6061 | static PyObject * |
| 6062 | posix_waitpid(PyObject *self, PyObject *args) |
| 6063 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6064 | Py_intptr_t pid; |
| 6065 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6066 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6067 | if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) |
| 6068 | return NULL; |
| 6069 | Py_BEGIN_ALLOW_THREADS |
| 6070 | pid = _cwait(&status, pid, options); |
| 6071 | Py_END_ALLOW_THREADS |
| 6072 | if (pid == -1) |
| 6073 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6074 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6075 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6076 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6077 | } |
| 6078 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6079 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6080 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6081 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6082 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6083 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6084 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6085 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6086 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6087 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6088 | pid_t pid; |
| 6089 | WAIT_TYPE status; |
| 6090 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6091 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6092 | Py_BEGIN_ALLOW_THREADS |
| 6093 | pid = wait(&status); |
| 6094 | Py_END_ALLOW_THREADS |
| 6095 | if (pid == -1) |
| 6096 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6097 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6098 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6099 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6100 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6101 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6102 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6103 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6104 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6105 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6106 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6107 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6108 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6109 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6110 | #ifdef HAVE_LSTAT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6111 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6112 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6113 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6114 | 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] | 6115 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6116 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6117 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6118 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6119 | } |
| 6120 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6121 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6122 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6123 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6124 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6125 | 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] | 6126 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6127 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6128 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6129 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6130 | PyObject* v; |
| 6131 | char buf[MAXPATHLEN]; |
| 6132 | char *path; |
| 6133 | int n; |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6134 | #ifdef Py_USING_UNICODE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6135 | int arg_is_unicode = 0; |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6136 | #endif |
| 6137 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6138 | if (!PyArg_ParseTuple(args, "et:readlink", |
| 6139 | Py_FileSystemDefaultEncoding, &path)) |
| 6140 | return NULL; |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6141 | #ifdef Py_USING_UNICODE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6142 | v = PySequence_GetItem(args, 0); |
| 6143 | if (v == NULL) { |
| 6144 | PyMem_Free(path); |
| 6145 | return NULL; |
| 6146 | } |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6147 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6148 | if (PyUnicode_Check(v)) { |
| 6149 | arg_is_unicode = 1; |
| 6150 | } |
| 6151 | Py_DECREF(v); |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6152 | #endif |
| 6153 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6154 | Py_BEGIN_ALLOW_THREADS |
| 6155 | n = readlink(path, buf, (int) sizeof buf); |
| 6156 | Py_END_ALLOW_THREADS |
| 6157 | if (n < 0) |
| 6158 | return posix_error_with_allocated_filename(path); |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6159 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6160 | PyMem_Free(path); |
| 6161 | v = PyString_FromStringAndSize(buf, n); |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6162 | #ifdef Py_USING_UNICODE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6163 | if (arg_is_unicode) { |
| 6164 | PyObject *w; |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6165 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6166 | w = PyUnicode_FromEncodedObject(v, |
| 6167 | Py_FileSystemDefaultEncoding, |
| 6168 | "strict"); |
| 6169 | if (w != NULL) { |
| 6170 | Py_DECREF(v); |
| 6171 | v = w; |
| 6172 | } |
| 6173 | else { |
| 6174 | /* fall back to the original byte string, as |
| 6175 | discussed in patch #683592 */ |
| 6176 | PyErr_Clear(); |
| 6177 | } |
| 6178 | } |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 6179 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6180 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6181 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6182 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6183 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6184 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6185 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6186 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6187 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 6188 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6189 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6190 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6191 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6192 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6193 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6194 | } |
| 6195 | #endif /* HAVE_SYMLINK */ |
| 6196 | |
| 6197 | |
| 6198 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6199 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 6200 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6201 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6202 | { |
| 6203 | ULONG value = 0; |
| 6204 | |
| 6205 | Py_BEGIN_ALLOW_THREADS |
| 6206 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 6207 | Py_END_ALLOW_THREADS |
| 6208 | |
| 6209 | return value; |
| 6210 | } |
| 6211 | |
| 6212 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6213 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6214 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6215 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6216 | return Py_BuildValue("ddddd", |
| 6217 | (double)0 /* t.tms_utime / HZ */, |
| 6218 | (double)0 /* t.tms_stime / HZ */, |
| 6219 | (double)0 /* t.tms_cutime / HZ */, |
| 6220 | (double)0 /* t.tms_cstime / HZ */, |
| 6221 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6222 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6223 | #else /* not OS2 */ |
Martin v. Löwis | 03824e4 | 2008-12-29 18:17:34 +0000 | [diff] [blame] | 6224 | #define NEED_TICKS_PER_SECOND |
| 6225 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6226 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6227 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6228 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6229 | struct tms t; |
| 6230 | clock_t c; |
| 6231 | errno = 0; |
| 6232 | c = times(&t); |
| 6233 | if (c == (clock_t) -1) |
| 6234 | return posix_error(); |
| 6235 | return Py_BuildValue("ddddd", |
| 6236 | (double)t.tms_utime / ticks_per_second, |
| 6237 | (double)t.tms_stime / ticks_per_second, |
| 6238 | (double)t.tms_cutime / ticks_per_second, |
| 6239 | (double)t.tms_cstime / ticks_per_second, |
| 6240 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6241 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6242 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6243 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6244 | |
| 6245 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6246 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6247 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6248 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6249 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6250 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6251 | FILETIME create, exit, kernel, user; |
| 6252 | HANDLE hProc; |
| 6253 | hProc = GetCurrentProcess(); |
| 6254 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 6255 | /* The fields of a FILETIME structure are the hi and lo part |
| 6256 | of a 64-bit value expressed in 100 nanosecond units. |
| 6257 | 1e7 is one second in such units; 1e-7 the inverse. |
| 6258 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 6259 | */ |
| 6260 | return Py_BuildValue( |
| 6261 | "ddddd", |
| 6262 | (double)(user.dwHighDateTime*429.4967296 + |
| 6263 | user.dwLowDateTime*1e-7), |
| 6264 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 6265 | kernel.dwLowDateTime*1e-7), |
| 6266 | (double)0, |
| 6267 | (double)0, |
| 6268 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6269 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6270 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6271 | |
| 6272 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6273 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6274 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6275 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6276 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6277 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6278 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6279 | #ifdef HAVE_GETSID |
| 6280 | PyDoc_STRVAR(posix_getsid__doc__, |
| 6281 | "getsid(pid) -> sid\n\n\ |
| 6282 | Call the system call getsid()."); |
| 6283 | |
| 6284 | static PyObject * |
| 6285 | posix_getsid(PyObject *self, PyObject *args) |
| 6286 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6287 | pid_t pid; |
| 6288 | int sid; |
| 6289 | if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid)) |
| 6290 | return NULL; |
| 6291 | sid = getsid(pid); |
| 6292 | if (sid < 0) |
| 6293 | return posix_error(); |
| 6294 | return PyInt_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6295 | } |
| 6296 | #endif /* HAVE_GETSID */ |
| 6297 | |
| 6298 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6299 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6300 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6301 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6302 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6303 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6304 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6305 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6306 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6307 | if (setsid() < 0) |
| 6308 | return posix_error(); |
| 6309 | Py_INCREF(Py_None); |
| 6310 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6311 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6312 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6313 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6314 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6315 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6316 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6317 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6318 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6319 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6320 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6321 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6322 | pid_t pid; |
| 6323 | int pgrp; |
| 6324 | if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 6325 | return NULL; |
| 6326 | if (setpgid(pid, pgrp) < 0) |
| 6327 | return posix_error(); |
| 6328 | Py_INCREF(Py_None); |
| 6329 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6330 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6331 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6332 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6333 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6334 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6335 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6336 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6337 | 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] | 6338 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6339 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6340 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6341 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6342 | int fd; |
| 6343 | pid_t pgid; |
| 6344 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 6345 | return NULL; |
| 6346 | pgid = tcgetpgrp(fd); |
| 6347 | if (pgid < 0) |
| 6348 | return posix_error(); |
| 6349 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6350 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6351 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6352 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6353 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6354 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6355 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6356 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6357 | 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] | 6358 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6359 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6360 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6361 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6362 | int fd; |
| 6363 | pid_t pgid; |
| 6364 | if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 6365 | return NULL; |
| 6366 | if (tcsetpgrp(fd, pgid) < 0) |
| 6367 | return posix_error(); |
| 6368 | Py_INCREF(Py_None); |
| 6369 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6370 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6371 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6372 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6373 | /* Functions acting on file descriptors */ |
| 6374 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6375 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6376 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6377 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6378 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6379 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6380 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6381 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6382 | char *file = NULL; |
| 6383 | int flag; |
| 6384 | int mode = 0777; |
| 6385 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6386 | |
| 6387 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6388 | PyUnicodeObject *po; |
| 6389 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 6390 | Py_BEGIN_ALLOW_THREADS |
| 6391 | /* PyUnicode_AS_UNICODE OK without thread |
| 6392 | lock as it is a simple dereference. */ |
| 6393 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 6394 | Py_END_ALLOW_THREADS |
| 6395 | if (fd < 0) |
| 6396 | return posix_error(); |
| 6397 | return PyInt_FromLong((long)fd); |
| 6398 | } |
| 6399 | /* Drop the argument parsing error as narrow strings |
| 6400 | are also valid. */ |
| 6401 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6402 | #endif |
| 6403 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6404 | if (!PyArg_ParseTuple(args, "eti|i", |
| 6405 | Py_FileSystemDefaultEncoding, &file, |
| 6406 | &flag, &mode)) |
| 6407 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 6408 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6409 | Py_BEGIN_ALLOW_THREADS |
| 6410 | fd = open(file, flag, mode); |
| 6411 | Py_END_ALLOW_THREADS |
| 6412 | if (fd < 0) |
| 6413 | return posix_error_with_allocated_filename(file); |
| 6414 | PyMem_Free(file); |
| 6415 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6418 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6419 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6420 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6421 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6422 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6423 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6424 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6425 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6426 | int fd, res; |
| 6427 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 6428 | return NULL; |
| 6429 | if (!_PyVerify_fd(fd)) |
| 6430 | return posix_error(); |
| 6431 | Py_BEGIN_ALLOW_THREADS |
| 6432 | res = close(fd); |
| 6433 | Py_END_ALLOW_THREADS |
| 6434 | if (res < 0) |
| 6435 | return posix_error(); |
| 6436 | Py_INCREF(Py_None); |
| 6437 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6438 | } |
| 6439 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6440 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6441 | PyDoc_STRVAR(posix_closerange__doc__, |
Georg Brandl | 309501a | 2008-01-19 20:22:13 +0000 | [diff] [blame] | 6442 | "closerange(fd_low, fd_high)\n\n\ |
| 6443 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 6444 | |
| 6445 | static PyObject * |
| 6446 | posix_closerange(PyObject *self, PyObject *args) |
| 6447 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6448 | int fd_from, fd_to, i; |
| 6449 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 6450 | return NULL; |
| 6451 | Py_BEGIN_ALLOW_THREADS |
| 6452 | for (i = fd_from; i < fd_to; i++) |
| 6453 | if (_PyVerify_fd(i)) |
| 6454 | close(i); |
| 6455 | Py_END_ALLOW_THREADS |
| 6456 | Py_RETURN_NONE; |
Georg Brandl | 309501a | 2008-01-19 20:22:13 +0000 | [diff] [blame] | 6457 | } |
| 6458 | |
| 6459 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6460 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6461 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6462 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6463 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6464 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6465 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6466 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6467 | int fd; |
| 6468 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 6469 | return NULL; |
| 6470 | if (!_PyVerify_fd(fd)) |
| 6471 | return posix_error(); |
| 6472 | Py_BEGIN_ALLOW_THREADS |
| 6473 | fd = dup(fd); |
| 6474 | Py_END_ALLOW_THREADS |
| 6475 | if (fd < 0) |
| 6476 | return posix_error(); |
| 6477 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6478 | } |
| 6479 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6480 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6481 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 6482 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6483 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6484 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6485 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6486 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6487 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6488 | int fd, fd2, res; |
| 6489 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 6490 | return NULL; |
| 6491 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 6492 | return posix_error(); |
| 6493 | Py_BEGIN_ALLOW_THREADS |
| 6494 | res = dup2(fd, fd2); |
| 6495 | Py_END_ALLOW_THREADS |
| 6496 | if (res < 0) |
| 6497 | return posix_error(); |
| 6498 | Py_INCREF(Py_None); |
| 6499 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6500 | } |
| 6501 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6502 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6503 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6504 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6505 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6506 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6507 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6508 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6509 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6510 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6511 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6512 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6513 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6514 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6515 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6516 | PyObject *posobj; |
| 6517 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
| 6518 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6519 | #ifdef SEEK_SET |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6520 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 6521 | switch (how) { |
| 6522 | case 0: how = SEEK_SET; break; |
| 6523 | case 1: how = SEEK_CUR; break; |
| 6524 | case 2: how = SEEK_END; break; |
| 6525 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6526 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6527 | |
| 6528 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6529 | pos = PyInt_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6530 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6531 | pos = PyLong_Check(posobj) ? |
| 6532 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6533 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6534 | if (PyErr_Occurred()) |
| 6535 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6536 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6537 | if (!_PyVerify_fd(fd)) |
| 6538 | return posix_error(); |
| 6539 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6540 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6541 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6542 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6543 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6544 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6545 | Py_END_ALLOW_THREADS |
| 6546 | if (res < 0) |
| 6547 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6548 | |
| 6549 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6550 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6551 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6552 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6553 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6554 | } |
| 6555 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6556 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6557 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6558 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6559 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6560 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6561 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6562 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6563 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6564 | int fd, size, n; |
| 6565 | PyObject *buffer; |
| 6566 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 6567 | return NULL; |
| 6568 | if (size < 0) { |
| 6569 | errno = EINVAL; |
| 6570 | return posix_error(); |
| 6571 | } |
| 6572 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
| 6573 | if (buffer == NULL) |
| 6574 | return NULL; |
Stefan Krah | acaab2a | 2010-11-26 15:06:27 +0000 | [diff] [blame] | 6575 | if (!_PyVerify_fd(fd)) { |
| 6576 | Py_DECREF(buffer); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6577 | return posix_error(); |
Stefan Krah | acaab2a | 2010-11-26 15:06:27 +0000 | [diff] [blame] | 6578 | } |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6579 | Py_BEGIN_ALLOW_THREADS |
| 6580 | n = read(fd, PyString_AsString(buffer), size); |
| 6581 | Py_END_ALLOW_THREADS |
| 6582 | if (n < 0) { |
| 6583 | Py_DECREF(buffer); |
| 6584 | return posix_error(); |
| 6585 | } |
| 6586 | if (n != size) |
| 6587 | _PyString_Resize(&buffer, n); |
| 6588 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6589 | } |
| 6590 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6591 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6592 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6593 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6594 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6595 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6596 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6597 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6598 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6599 | Py_buffer pbuf; |
| 6600 | int fd; |
| 6601 | Py_ssize_t size; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 6602 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6603 | if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf)) |
| 6604 | return NULL; |
Stefan Krah | acaab2a | 2010-11-26 15:06:27 +0000 | [diff] [blame] | 6605 | if (!_PyVerify_fd(fd)) { |
| 6606 | PyBuffer_Release(&pbuf); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6607 | return posix_error(); |
Stefan Krah | acaab2a | 2010-11-26 15:06:27 +0000 | [diff] [blame] | 6608 | } |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6609 | Py_BEGIN_ALLOW_THREADS |
| 6610 | size = write(fd, pbuf.buf, (size_t)pbuf.len); |
| 6611 | Py_END_ALLOW_THREADS |
Stefan Krah | acaab2a | 2010-11-26 15:06:27 +0000 | [diff] [blame] | 6612 | PyBuffer_Release(&pbuf); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6613 | if (size < 0) |
| 6614 | return posix_error(); |
| 6615 | return PyInt_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6616 | } |
| 6617 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6618 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6619 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6620 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6621 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6622 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6623 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6624 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6625 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6626 | int fd; |
| 6627 | STRUCT_STAT st; |
| 6628 | int res; |
| 6629 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
| 6630 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 6631 | #ifdef __VMS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6632 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 6633 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 6634 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6635 | if (!_PyVerify_fd(fd)) |
| 6636 | return posix_error(); |
| 6637 | Py_BEGIN_ALLOW_THREADS |
| 6638 | res = FSTAT(fd, &st); |
| 6639 | Py_END_ALLOW_THREADS |
| 6640 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6641 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6642 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6643 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6644 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6645 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6646 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6647 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6648 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6649 | } |
| 6650 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6651 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6652 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6653 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6654 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6655 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6656 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6657 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6658 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6659 | int fd; |
| 6660 | char *orgmode = "r"; |
| 6661 | int bufsize = -1; |
| 6662 | FILE *fp; |
| 6663 | PyObject *f; |
| 6664 | char *mode; |
| 6665 | if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize)) |
| 6666 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 6667 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6668 | /* Sanitize mode. See fileobject.c */ |
| 6669 | mode = PyMem_MALLOC(strlen(orgmode)+3); |
| 6670 | if (!mode) { |
| 6671 | PyErr_NoMemory(); |
| 6672 | return NULL; |
| 6673 | } |
| 6674 | strcpy(mode, orgmode); |
| 6675 | if (_PyFile_SanitizeMode(mode)) { |
| 6676 | PyMem_FREE(mode); |
| 6677 | return NULL; |
| 6678 | } |
| 6679 | if (!_PyVerify_fd(fd)) |
| 6680 | return posix_error(); |
| 6681 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 6682 | #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6683 | if (mode[0] == 'a') { |
| 6684 | /* try to make sure the O_APPEND flag is set */ |
| 6685 | int flags; |
| 6686 | flags = fcntl(fd, F_GETFL); |
| 6687 | if (flags != -1) |
| 6688 | fcntl(fd, F_SETFL, flags | O_APPEND); |
| 6689 | fp = fdopen(fd, mode); |
| 6690 | if (fp == NULL && flags != -1) |
| 6691 | /* restore old mode if fdopen failed */ |
| 6692 | fcntl(fd, F_SETFL, flags); |
| 6693 | } else { |
| 6694 | fp = fdopen(fd, mode); |
| 6695 | } |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 6696 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6697 | fp = fdopen(fd, mode); |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 6698 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6699 | Py_END_ALLOW_THREADS |
| 6700 | PyMem_FREE(mode); |
| 6701 | if (fp == NULL) |
| 6702 | return posix_error(); |
| 6703 | f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose); |
| 6704 | if (f != NULL) |
| 6705 | PyFile_SetBufSize(f, bufsize); |
| 6706 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6707 | } |
| 6708 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6709 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6710 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6711 | 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] | 6712 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6713 | |
| 6714 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 6715 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6716 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6717 | int fd; |
| 6718 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 6719 | return NULL; |
| 6720 | if (!_PyVerify_fd(fd)) |
| 6721 | return PyBool_FromLong(0); |
| 6722 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6723 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6724 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6725 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6726 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6727 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6728 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6729 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6730 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6731 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6732 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6733 | #if defined(PYOS_OS2) |
| 6734 | HFILE read, write; |
| 6735 | APIRET rc; |
| 6736 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6737 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6738 | rc = DosCreatePipe( &read, &write, 4096); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6739 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6740 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6741 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6742 | |
| 6743 | return Py_BuildValue("(ii)", read, write); |
| 6744 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6745 | #if !defined(MS_WINDOWS) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6746 | int fds[2]; |
| 6747 | int res; |
| 6748 | Py_BEGIN_ALLOW_THREADS |
| 6749 | res = pipe(fds); |
| 6750 | Py_END_ALLOW_THREADS |
| 6751 | if (res != 0) |
| 6752 | return posix_error(); |
| 6753 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6754 | #else /* MS_WINDOWS */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6755 | HANDLE read, write; |
| 6756 | int read_fd, write_fd; |
| 6757 | BOOL ok; |
| 6758 | Py_BEGIN_ALLOW_THREADS |
| 6759 | ok = CreatePipe(&read, &write, NULL, 0); |
| 6760 | Py_END_ALLOW_THREADS |
| 6761 | if (!ok) |
| 6762 | return win32_error("CreatePipe", NULL); |
| 6763 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 6764 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 6765 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6766 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6767 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6768 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6769 | #endif /* HAVE_PIPE */ |
| 6770 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6771 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6772 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6773 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6774 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6775 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6776 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6777 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6778 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6779 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6780 | char *filename; |
| 6781 | int mode = 0666; |
| 6782 | int res; |
| 6783 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
| 6784 | return NULL; |
| 6785 | Py_BEGIN_ALLOW_THREADS |
| 6786 | res = mkfifo(filename, mode); |
| 6787 | Py_END_ALLOW_THREADS |
| 6788 | if (res < 0) |
| 6789 | return posix_error(); |
| 6790 | Py_INCREF(Py_None); |
| 6791 | return Py_None; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6792 | } |
| 6793 | #endif |
| 6794 | |
| 6795 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6796 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6797 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6798 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6799 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 6800 | named filename. mode specifies both the permissions to use and the\n\ |
| 6801 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 6802 | 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] | 6803 | device defines the newly created device special file (probably using\n\ |
| 6804 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6805 | |
| 6806 | |
| 6807 | static PyObject * |
| 6808 | posix_mknod(PyObject *self, PyObject *args) |
| 6809 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6810 | char *filename; |
| 6811 | int mode = 0600; |
| 6812 | int device = 0; |
| 6813 | int res; |
| 6814 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
| 6815 | return NULL; |
| 6816 | Py_BEGIN_ALLOW_THREADS |
| 6817 | res = mknod(filename, mode, device); |
| 6818 | Py_END_ALLOW_THREADS |
| 6819 | if (res < 0) |
| 6820 | return posix_error(); |
| 6821 | Py_INCREF(Py_None); |
| 6822 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6823 | } |
| 6824 | #endif |
| 6825 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6826 | #ifdef HAVE_DEVICE_MACROS |
| 6827 | PyDoc_STRVAR(posix_major__doc__, |
| 6828 | "major(device) -> major number\n\ |
| 6829 | Extracts a device major number from a raw device number."); |
| 6830 | |
| 6831 | static PyObject * |
| 6832 | posix_major(PyObject *self, PyObject *args) |
| 6833 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6834 | int device; |
| 6835 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 6836 | return NULL; |
| 6837 | return PyInt_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6838 | } |
| 6839 | |
| 6840 | PyDoc_STRVAR(posix_minor__doc__, |
| 6841 | "minor(device) -> minor number\n\ |
| 6842 | Extracts a device minor number from a raw device number."); |
| 6843 | |
| 6844 | static PyObject * |
| 6845 | posix_minor(PyObject *self, PyObject *args) |
| 6846 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6847 | int device; |
| 6848 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 6849 | return NULL; |
| 6850 | return PyInt_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6851 | } |
| 6852 | |
| 6853 | PyDoc_STRVAR(posix_makedev__doc__, |
| 6854 | "makedev(major, minor) -> device number\n\ |
| 6855 | Composes a raw device number from the major and minor device numbers."); |
| 6856 | |
| 6857 | static PyObject * |
| 6858 | posix_makedev(PyObject *self, PyObject *args) |
| 6859 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6860 | int major, minor; |
| 6861 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 6862 | return NULL; |
| 6863 | return PyInt_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6864 | } |
| 6865 | #endif /* device macros */ |
| 6866 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6867 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6868 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6869 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6870 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6871 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6872 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6873 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6874 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6875 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6876 | int fd; |
| 6877 | off_t length; |
| 6878 | int res; |
| 6879 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6880 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6881 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
| 6882 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6883 | |
| 6884 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6885 | length = PyInt_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6886 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6887 | length = PyLong_Check(lenobj) ? |
| 6888 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6889 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6890 | if (PyErr_Occurred()) |
| 6891 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6892 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6893 | Py_BEGIN_ALLOW_THREADS |
| 6894 | res = ftruncate(fd, length); |
| 6895 | Py_END_ALLOW_THREADS |
| 6896 | if (res < 0) |
| 6897 | return posix_error(); |
| 6898 | Py_INCREF(Py_None); |
| 6899 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6900 | } |
| 6901 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6902 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6903 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6904 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6905 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6906 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6907 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6908 | /* Save putenv() parameters as values here, so we can collect them when they |
| 6909 | * get re-set with another call for the same key. */ |
| 6910 | static PyObject *posix_putenv_garbage; |
| 6911 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6912 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6913 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6914 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6915 | char *s1, *s2; |
| 6916 | char *newenv; |
| 6917 | PyObject *newstr; |
| 6918 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6919 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6920 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
| 6921 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6922 | |
| 6923 | #if defined(PYOS_OS2) |
| 6924 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 6925 | APIRET rc; |
| 6926 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6927 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 6928 | if (rc != NO_ERROR) |
| 6929 | return os2_error(rc); |
| 6930 | |
| 6931 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 6932 | APIRET rc; |
| 6933 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6934 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 6935 | if (rc != NO_ERROR) |
| 6936 | return os2_error(rc); |
| 6937 | } else { |
| 6938 | #endif |
| 6939 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6940 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 6941 | len = strlen(s1) + strlen(s2) + 2; |
| 6942 | /* len includes space for a trailing \0; the size arg to |
| 6943 | PyString_FromStringAndSize does not count that */ |
| 6944 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
| 6945 | if (newstr == NULL) |
| 6946 | return PyErr_NoMemory(); |
| 6947 | newenv = PyString_AS_STRING(newstr); |
| 6948 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 6949 | if (putenv(newenv)) { |
| 6950 | Py_DECREF(newstr); |
| 6951 | posix_error(); |
| 6952 | return NULL; |
| 6953 | } |
| 6954 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 6955 | * this will cause previous value to be collected. This has to |
| 6956 | * happen after the real putenv() call because the old value |
| 6957 | * was still accessible until then. */ |
| 6958 | if (PyDict_SetItem(posix_putenv_garbage, |
| 6959 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 6960 | /* really not much we can do; just leak */ |
| 6961 | PyErr_Clear(); |
| 6962 | } |
| 6963 | else { |
| 6964 | Py_DECREF(newstr); |
| 6965 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6966 | |
| 6967 | #if defined(PYOS_OS2) |
| 6968 | } |
| 6969 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6970 | Py_INCREF(Py_None); |
| 6971 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6972 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6973 | #endif /* putenv */ |
| 6974 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6975 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6976 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6977 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6978 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6979 | |
| 6980 | static PyObject * |
| 6981 | posix_unsetenv(PyObject *self, PyObject *args) |
| 6982 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6983 | char *s1; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6984 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6985 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 6986 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6987 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6988 | unsetenv(s1); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6989 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 6990 | /* Remove the key from posix_putenv_garbage; |
| 6991 | * this will cause it to be collected. This has to |
| 6992 | * happen after the real unsetenv() call because the |
| 6993 | * old value was still accessible until then. |
| 6994 | */ |
| 6995 | if (PyDict_DelItem(posix_putenv_garbage, |
| 6996 | PyTuple_GET_ITEM(args, 0))) { |
| 6997 | /* really not much we can do; just leak */ |
| 6998 | PyErr_Clear(); |
| 6999 | } |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7000 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7001 | Py_INCREF(Py_None); |
| 7002 | return Py_None; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7003 | } |
| 7004 | #endif /* unsetenv */ |
| 7005 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7006 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7007 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7008 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7009 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 7010 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7011 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7012 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7013 | int code; |
| 7014 | char *message; |
| 7015 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 7016 | return NULL; |
| 7017 | message = strerror(code); |
| 7018 | if (message == NULL) { |
| 7019 | PyErr_SetString(PyExc_ValueError, |
| 7020 | "strerror() argument out of range"); |
| 7021 | return NULL; |
| 7022 | } |
| 7023 | return PyString_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7024 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7025 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7026 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7027 | #ifdef HAVE_SYS_WAIT_H |
| 7028 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7029 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7030 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7031 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7032 | 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] | 7033 | |
| 7034 | static PyObject * |
| 7035 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 7036 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7037 | WAIT_TYPE status; |
| 7038 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7039 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7040 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 7041 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7042 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7043 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7044 | } |
| 7045 | #endif /* WCOREDUMP */ |
| 7046 | |
| 7047 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7048 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7049 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7050 | 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] | 7051 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7052 | |
| 7053 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7054 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7055 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7056 | WAIT_TYPE status; |
| 7057 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7058 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7059 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 7060 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7061 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7062 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7063 | } |
| 7064 | #endif /* WIFCONTINUED */ |
| 7065 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7066 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7067 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7068 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7069 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7070 | |
| 7071 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7072 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7073 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7074 | WAIT_TYPE status; |
| 7075 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7076 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7077 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 7078 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7079 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7080 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7081 | } |
| 7082 | #endif /* WIFSTOPPED */ |
| 7083 | |
| 7084 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7085 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7086 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7087 | 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] | 7088 | |
| 7089 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7090 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7091 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7092 | WAIT_TYPE status; |
| 7093 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7094 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7095 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 7096 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7097 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7098 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7099 | } |
| 7100 | #endif /* WIFSIGNALED */ |
| 7101 | |
| 7102 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7103 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7104 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7105 | 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] | 7106 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7107 | |
| 7108 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7109 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7110 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7111 | WAIT_TYPE status; |
| 7112 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7113 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7114 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 7115 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7116 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7117 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7118 | } |
| 7119 | #endif /* WIFEXITED */ |
| 7120 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7121 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7122 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7123 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7124 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7125 | |
| 7126 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7127 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7128 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7129 | WAIT_TYPE status; |
| 7130 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7131 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7132 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 7133 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7134 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7135 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7136 | } |
| 7137 | #endif /* WEXITSTATUS */ |
| 7138 | |
| 7139 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7140 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7141 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7142 | 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] | 7143 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7144 | |
| 7145 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7146 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7147 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7148 | WAIT_TYPE status; |
| 7149 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7150 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7151 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 7152 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7153 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7154 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7155 | } |
| 7156 | #endif /* WTERMSIG */ |
| 7157 | |
| 7158 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7159 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7160 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7161 | Return the signal that stopped the process that provided\n\ |
| 7162 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7163 | |
| 7164 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7165 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7166 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7167 | WAIT_TYPE status; |
| 7168 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7169 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7170 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 7171 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7172 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7173 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7174 | } |
| 7175 | #endif /* WSTOPSIG */ |
| 7176 | |
| 7177 | #endif /* HAVE_SYS_WAIT_H */ |
| 7178 | |
| 7179 | |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 7180 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 7181 | #ifdef _SCO_DS |
| 7182 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 7183 | needed definitions in sys/statvfs.h */ |
| 7184 | #define _SVID3 |
| 7185 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7186 | #include <sys/statvfs.h> |
| 7187 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7188 | static PyObject* |
| 7189 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7190 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 7191 | if (v == NULL) |
| 7192 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7193 | |
| 7194 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7195 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 7196 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 7197 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 7198 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 7199 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 7200 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 7201 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 7202 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 7203 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 7204 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7205 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7206 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 7207 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 7208 | PyStructSequence_SET_ITEM(v, 2, |
| 7209 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 7210 | PyStructSequence_SET_ITEM(v, 3, |
| 7211 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 7212 | PyStructSequence_SET_ITEM(v, 4, |
| 7213 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 7214 | PyStructSequence_SET_ITEM(v, 5, |
| 7215 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 7216 | PyStructSequence_SET_ITEM(v, 6, |
| 7217 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 7218 | PyStructSequence_SET_ITEM(v, 7, |
| 7219 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 7220 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 7221 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7222 | #endif |
| 7223 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7224 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7225 | } |
| 7226 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7227 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7228 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7229 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7230 | |
| 7231 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7232 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7233 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7234 | int fd, res; |
| 7235 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7236 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7237 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 7238 | return NULL; |
| 7239 | Py_BEGIN_ALLOW_THREADS |
| 7240 | res = fstatvfs(fd, &st); |
| 7241 | Py_END_ALLOW_THREADS |
| 7242 | if (res != 0) |
| 7243 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7244 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7245 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7246 | } |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 7247 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7248 | |
| 7249 | |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 7250 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7251 | #include <sys/statvfs.h> |
| 7252 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7253 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7254 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7255 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7256 | |
| 7257 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7258 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7259 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7260 | char *path; |
| 7261 | int res; |
| 7262 | struct statvfs st; |
| 7263 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
| 7264 | return NULL; |
| 7265 | Py_BEGIN_ALLOW_THREADS |
| 7266 | res = statvfs(path, &st); |
| 7267 | Py_END_ALLOW_THREADS |
| 7268 | if (res != 0) |
| 7269 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7270 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7271 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7272 | } |
| 7273 | #endif /* HAVE_STATVFS */ |
| 7274 | |
| 7275 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7276 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7277 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7278 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7279 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 7280 | 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] | 7281 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7282 | |
| 7283 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7284 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7285 | { |
| 7286 | PyObject *result = NULL; |
| 7287 | char *dir = NULL; |
| 7288 | char *pfx = NULL; |
| 7289 | char *name; |
| 7290 | |
| 7291 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7292 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 7293 | |
| 7294 | if (PyErr_Warn(PyExc_RuntimeWarning, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7295 | "tempnam is a potential security risk to your program") < 0) |
| 7296 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 7297 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7298 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 7299 | name = _tempnam(dir, pfx); |
| 7300 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7301 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 7302 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7303 | if (name == NULL) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7304 | return PyErr_NoMemory(); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 7305 | result = PyString_FromString(name); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7306 | free(name); |
| 7307 | return result; |
| 7308 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 7309 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7310 | |
| 7311 | |
| 7312 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7313 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7314 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7315 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7316 | |
| 7317 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7318 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7319 | { |
| 7320 | FILE *fp; |
| 7321 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7322 | fp = tmpfile(); |
| 7323 | if (fp == NULL) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7324 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 7325 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7326 | } |
| 7327 | #endif |
| 7328 | |
| 7329 | |
| 7330 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7331 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7332 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7333 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7334 | |
| 7335 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7336 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7337 | { |
| 7338 | char buffer[L_tmpnam]; |
| 7339 | char *name; |
| 7340 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 7341 | if (PyErr_Warn(PyExc_RuntimeWarning, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7342 | "tmpnam is a potential security risk to your program") < 0) |
| 7343 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 7344 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 7345 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7346 | name = tmpnam_r(buffer); |
| 7347 | #else |
| 7348 | name = tmpnam(buffer); |
| 7349 | #endif |
| 7350 | if (name == NULL) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7351 | PyObject *err = Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 7352 | #ifdef USE_TMPNAM_R |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7353 | "unexpected NULL from tmpnam_r" |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7354 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7355 | "unexpected NULL from tmpnam" |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7356 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7357 | ); |
| 7358 | PyErr_SetObject(PyExc_OSError, err); |
| 7359 | Py_XDECREF(err); |
| 7360 | return NULL; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7361 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 7362 | return PyString_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7363 | } |
| 7364 | #endif |
| 7365 | |
| 7366 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7367 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 7368 | * It maps strings representing configuration variable names to |
| 7369 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7370 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7371 | * rarely-used constants. There are three separate tables that use |
| 7372 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7373 | * |
| 7374 | * This code is always included, even if none of the interfaces that |
| 7375 | * need it are included. The #if hackery needed to avoid it would be |
| 7376 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7377 | */ |
| 7378 | struct constdef { |
| 7379 | char *name; |
| 7380 | long value; |
| 7381 | }; |
| 7382 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7383 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7384 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7385 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7386 | { |
| 7387 | if (PyInt_Check(arg)) { |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 7388 | *valuep = PyInt_AS_LONG(arg); |
| 7389 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7390 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 7391 | if (PyString_Check(arg)) { |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 7392 | /* look up the value in the table using a binary search */ |
| 7393 | size_t lo = 0; |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7394 | size_t mid; |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 7395 | size_t hi = tablesize; |
| 7396 | int cmp; |
| 7397 | char *confname = PyString_AS_STRING(arg); |
| 7398 | while (lo < hi) { |
| 7399 | mid = (lo + hi) / 2; |
| 7400 | cmp = strcmp(confname, table[mid].name); |
| 7401 | if (cmp < 0) |
| 7402 | hi = mid; |
| 7403 | else if (cmp > 0) |
| 7404 | lo = mid + 1; |
| 7405 | else { |
| 7406 | *valuep = table[mid].value; |
| 7407 | return 1; |
| 7408 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7409 | } |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 7410 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7411 | } |
| 7412 | else |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 7413 | PyErr_SetString(PyExc_TypeError, |
| 7414 | "configuration names must be strings or integers"); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7415 | return 0; |
| 7416 | } |
| 7417 | |
| 7418 | |
| 7419 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 7420 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7421 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7422 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7423 | #endif |
| 7424 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7425 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7426 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7427 | #ifdef _PC_ASYNC_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7428 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7429 | #endif |
| 7430 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7431 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7432 | #endif |
| 7433 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7434 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7435 | #endif |
| 7436 | #ifdef _PC_LAST |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7437 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7438 | #endif |
| 7439 | #ifdef _PC_LINK_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7440 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7441 | #endif |
| 7442 | #ifdef _PC_MAX_CANON |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7443 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7444 | #endif |
| 7445 | #ifdef _PC_MAX_INPUT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7446 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7447 | #endif |
| 7448 | #ifdef _PC_NAME_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7449 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7450 | #endif |
| 7451 | #ifdef _PC_NO_TRUNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7452 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7453 | #endif |
| 7454 | #ifdef _PC_PATH_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7455 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7456 | #endif |
| 7457 | #ifdef _PC_PIPE_BUF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7458 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7459 | #endif |
| 7460 | #ifdef _PC_PRIO_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7461 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7462 | #endif |
| 7463 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7464 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7465 | #endif |
| 7466 | #ifdef _PC_SYNC_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7467 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7468 | #endif |
| 7469 | #ifdef _PC_VDISABLE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7470 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7471 | #endif |
| 7472 | }; |
| 7473 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7474 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7475 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7476 | { |
| 7477 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 7478 | sizeof(posix_constants_pathconf) |
| 7479 | / sizeof(struct constdef)); |
| 7480 | } |
| 7481 | #endif |
| 7482 | |
| 7483 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7484 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7485 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7486 | 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] | 7487 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7488 | |
| 7489 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7490 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7491 | { |
| 7492 | PyObject *result = NULL; |
| 7493 | int name, fd; |
| 7494 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7495 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 7496 | conv_path_confname, &name)) { |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 7497 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7498 | |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 7499 | errno = 0; |
| 7500 | limit = fpathconf(fd, name); |
| 7501 | if (limit == -1 && errno != 0) |
| 7502 | posix_error(); |
| 7503 | else |
| 7504 | result = PyInt_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7505 | } |
| 7506 | return result; |
| 7507 | } |
| 7508 | #endif |
| 7509 | |
| 7510 | |
| 7511 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7512 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7513 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7514 | 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] | 7515 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7516 | |
| 7517 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7518 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7519 | { |
| 7520 | PyObject *result = NULL; |
| 7521 | int name; |
| 7522 | char *path; |
| 7523 | |
| 7524 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 7525 | conv_path_confname, &name)) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7526 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7527 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7528 | errno = 0; |
| 7529 | limit = pathconf(path, name); |
| 7530 | if (limit == -1 && errno != 0) { |
| 7531 | if (errno == EINVAL) |
Stefan Krah | acaab2a | 2010-11-26 15:06:27 +0000 | [diff] [blame] | 7532 | /* could be a path or name problem */ |
| 7533 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7534 | else |
Stefan Krah | acaab2a | 2010-11-26 15:06:27 +0000 | [diff] [blame] | 7535 | posix_error_with_filename(path); |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7536 | } |
| 7537 | else |
| 7538 | result = PyInt_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7539 | } |
| 7540 | return result; |
| 7541 | } |
| 7542 | #endif |
| 7543 | |
| 7544 | #ifdef HAVE_CONFSTR |
| 7545 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7546 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7547 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7548 | #endif |
| 7549 | #ifdef _CS_HOSTNAME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7550 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7551 | #endif |
| 7552 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7553 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7554 | #endif |
| 7555 | #ifdef _CS_HW_SERIAL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7556 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7557 | #endif |
| 7558 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7559 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7560 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7561 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7562 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7563 | #endif |
| 7564 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7565 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7566 | #endif |
| 7567 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7568 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7569 | #endif |
| 7570 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7571 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7572 | #endif |
| 7573 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7574 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7575 | #endif |
| 7576 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7577 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7578 | #endif |
| 7579 | #ifdef _CS_LFS_LIBS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7580 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7581 | #endif |
| 7582 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7583 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7584 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7585 | #ifdef _CS_MACHINE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7586 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7587 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7588 | #ifdef _CS_PATH |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7589 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7590 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7591 | #ifdef _CS_RELEASE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7592 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7593 | #endif |
| 7594 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7595 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7596 | #endif |
| 7597 | #ifdef _CS_SYSNAME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7598 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7599 | #endif |
| 7600 | #ifdef _CS_VERSION |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7601 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7602 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7603 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7604 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7605 | #endif |
| 7606 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7607 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7608 | #endif |
| 7609 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7610 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7611 | #endif |
| 7612 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7613 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7614 | #endif |
| 7615 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7616 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7617 | #endif |
| 7618 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7619 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7620 | #endif |
| 7621 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7622 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7623 | #endif |
| 7624 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7625 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7626 | #endif |
| 7627 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7628 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7629 | #endif |
| 7630 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7631 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7632 | #endif |
| 7633 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7634 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7635 | #endif |
| 7636 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7637 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7638 | #endif |
| 7639 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7640 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7641 | #endif |
| 7642 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7643 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7644 | #endif |
| 7645 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7646 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7647 | #endif |
| 7648 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7649 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7650 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7651 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7652 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7653 | #endif |
| 7654 | #ifdef _MIPS_CS_BASE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7655 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7656 | #endif |
| 7657 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7658 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7659 | #endif |
| 7660 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7661 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7662 | #endif |
| 7663 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7664 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7665 | #endif |
| 7666 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7667 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7668 | #endif |
| 7669 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7670 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7671 | #endif |
| 7672 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7673 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7674 | #endif |
| 7675 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7676 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7677 | #endif |
| 7678 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7679 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7680 | #endif |
| 7681 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7682 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7683 | #endif |
| 7684 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7685 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7686 | #endif |
| 7687 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7688 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7689 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7690 | }; |
| 7691 | |
| 7692 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7693 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7694 | { |
| 7695 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 7696 | sizeof(posix_constants_confstr) |
| 7697 | / sizeof(struct constdef)); |
| 7698 | } |
| 7699 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7700 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7701 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7702 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7703 | |
| 7704 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7705 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7706 | { |
| 7707 | PyObject *result = NULL; |
| 7708 | int name; |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 7709 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7710 | |
| 7711 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7712 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7713 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7714 | errno = 0; |
| 7715 | len = confstr(name, buffer, sizeof(buffer)); |
| 7716 | if (len == 0) { |
| 7717 | if (errno) { |
| 7718 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7719 | } |
| 7720 | else { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7721 | result = Py_None; |
| 7722 | Py_INCREF(Py_None); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7723 | } |
| 7724 | } |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7725 | else { |
| 7726 | if ((unsigned int)len >= sizeof(buffer)) { |
| 7727 | result = PyString_FromStringAndSize(NULL, len-1); |
| 7728 | if (result != NULL) |
| 7729 | confstr(name, PyString_AS_STRING(result), len); |
| 7730 | } |
| 7731 | else |
| 7732 | result = PyString_FromStringAndSize(buffer, len-1); |
| 7733 | } |
| 7734 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7735 | return result; |
| 7736 | } |
| 7737 | #endif |
| 7738 | |
| 7739 | |
| 7740 | #ifdef HAVE_SYSCONF |
| 7741 | static struct constdef posix_constants_sysconf[] = { |
| 7742 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7743 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7744 | #endif |
| 7745 | #ifdef _SC_2_C_BIND |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7746 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7747 | #endif |
| 7748 | #ifdef _SC_2_C_DEV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7749 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7750 | #endif |
| 7751 | #ifdef _SC_2_C_VERSION |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7752 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7753 | #endif |
| 7754 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7755 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7756 | #endif |
| 7757 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7758 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7759 | #endif |
| 7760 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7761 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7762 | #endif |
| 7763 | #ifdef _SC_2_SW_DEV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7764 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7765 | #endif |
| 7766 | #ifdef _SC_2_UPE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7767 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7768 | #endif |
| 7769 | #ifdef _SC_2_VERSION |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7770 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7771 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7772 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7773 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7774 | #endif |
| 7775 | #ifdef _SC_ACL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7776 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7777 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7778 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7779 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7780 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7781 | #ifdef _SC_AIO_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7782 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7783 | #endif |
| 7784 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7785 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7786 | #endif |
| 7787 | #ifdef _SC_ARG_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7788 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7789 | #endif |
| 7790 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7791 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7792 | #endif |
| 7793 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7794 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7795 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7796 | #ifdef _SC_AUDIT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7797 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7798 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7799 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7800 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7801 | #endif |
| 7802 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7803 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7804 | #endif |
| 7805 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7806 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7807 | #endif |
| 7808 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7809 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7810 | #endif |
| 7811 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7812 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7813 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7814 | #ifdef _SC_CAP |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7815 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7816 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7817 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7818 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7819 | #endif |
| 7820 | #ifdef _SC_CHAR_BIT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7821 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7822 | #endif |
| 7823 | #ifdef _SC_CHAR_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7824 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7825 | #endif |
| 7826 | #ifdef _SC_CHAR_MIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7827 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7828 | #endif |
| 7829 | #ifdef _SC_CHILD_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7830 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7831 | #endif |
| 7832 | #ifdef _SC_CLK_TCK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7833 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7834 | #endif |
| 7835 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7836 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7837 | #endif |
| 7838 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7839 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7840 | #endif |
| 7841 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7842 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7843 | #endif |
| 7844 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7845 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7846 | #endif |
| 7847 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7848 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7849 | #endif |
| 7850 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7851 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7852 | #endif |
| 7853 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7854 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7855 | #endif |
| 7856 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7857 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7858 | #endif |
| 7859 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7860 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7861 | #endif |
| 7862 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7863 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7864 | #endif |
| 7865 | #ifdef _SC_FSYNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7866 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7867 | #endif |
| 7868 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7869 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7870 | #endif |
| 7871 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7872 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7873 | #endif |
| 7874 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7875 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7876 | #endif |
| 7877 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7878 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7879 | #endif |
| 7880 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7881 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7882 | #endif |
| 7883 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7884 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7885 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7886 | #ifdef _SC_INF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7887 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7888 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7889 | #ifdef _SC_INT_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7890 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7891 | #endif |
| 7892 | #ifdef _SC_INT_MIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7893 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7894 | #endif |
| 7895 | #ifdef _SC_IOV_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7896 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7897 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7898 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7899 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7900 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7901 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7902 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7903 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7904 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7905 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7906 | #endif |
| 7907 | #ifdef _SC_KERN_SIM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7908 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7909 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7910 | #ifdef _SC_LINE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7911 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7912 | #endif |
| 7913 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7914 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7915 | #endif |
| 7916 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7917 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7918 | #endif |
| 7919 | #ifdef _SC_LONG_BIT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7920 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7921 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7922 | #ifdef _SC_MAC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7923 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7924 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7925 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7926 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7927 | #endif |
| 7928 | #ifdef _SC_MAXPID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7929 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7930 | #endif |
| 7931 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7932 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7933 | #endif |
| 7934 | #ifdef _SC_MEMLOCK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7935 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7936 | #endif |
| 7937 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7938 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7939 | #endif |
| 7940 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7941 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7942 | #endif |
| 7943 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7944 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7945 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7946 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7947 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7948 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7949 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7950 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7951 | #endif |
| 7952 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7953 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7954 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7955 | #ifdef _SC_NACLS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7956 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7957 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7958 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7959 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7960 | #endif |
| 7961 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7962 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7963 | #endif |
| 7964 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7965 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7966 | #endif |
| 7967 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7968 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7969 | #endif |
| 7970 | #ifdef _SC_NL_NMAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7971 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7972 | #endif |
| 7973 | #ifdef _SC_NL_SETMAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7974 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7975 | #endif |
| 7976 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7977 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7978 | #endif |
| 7979 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7980 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7981 | #endif |
| 7982 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7983 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7984 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7985 | #ifdef _SC_NPROC_CONF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7986 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7987 | #endif |
| 7988 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7989 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7990 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7991 | #ifdef _SC_NZERO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7992 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7993 | #endif |
| 7994 | #ifdef _SC_OPEN_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7995 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7996 | #endif |
| 7997 | #ifdef _SC_PAGESIZE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 7998 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7999 | #endif |
| 8000 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8001 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8002 | #endif |
| 8003 | #ifdef _SC_PASS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8004 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8005 | #endif |
| 8006 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8007 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8008 | #endif |
| 8009 | #ifdef _SC_PII |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8010 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8011 | #endif |
| 8012 | #ifdef _SC_PII_INTERNET |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8013 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8014 | #endif |
| 8015 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8016 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8017 | #endif |
| 8018 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8019 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8020 | #endif |
| 8021 | #ifdef _SC_PII_OSI |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8022 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8023 | #endif |
| 8024 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8025 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8026 | #endif |
| 8027 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8028 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8029 | #endif |
| 8030 | #ifdef _SC_PII_OSI_M |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8031 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8032 | #endif |
| 8033 | #ifdef _SC_PII_SOCKET |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8034 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8035 | #endif |
| 8036 | #ifdef _SC_PII_XTI |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8037 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8038 | #endif |
| 8039 | #ifdef _SC_POLL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8040 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8041 | #endif |
| 8042 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8043 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8044 | #endif |
| 8045 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8046 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8047 | #endif |
| 8048 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8049 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8050 | #endif |
| 8051 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8052 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8053 | #endif |
| 8054 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8055 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8056 | #endif |
| 8057 | #ifdef _SC_SAVED_IDS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8058 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8059 | #endif |
| 8060 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8061 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8062 | #endif |
| 8063 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8064 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8065 | #endif |
| 8066 | #ifdef _SC_SELECT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8067 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8068 | #endif |
| 8069 | #ifdef _SC_SEMAPHORES |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8070 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8071 | #endif |
| 8072 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8073 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8074 | #endif |
| 8075 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8076 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8077 | #endif |
| 8078 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8079 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8080 | #endif |
| 8081 | #ifdef _SC_SHRT_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8082 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8083 | #endif |
| 8084 | #ifdef _SC_SHRT_MIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8085 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8086 | #endif |
| 8087 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8088 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8089 | #endif |
| 8090 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8091 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8092 | #endif |
| 8093 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8094 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8095 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8096 | #ifdef _SC_SOFTPOWER |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8097 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8098 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8099 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8100 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8101 | #endif |
| 8102 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8103 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8104 | #endif |
| 8105 | #ifdef _SC_STACK_PROT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8106 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8107 | #endif |
| 8108 | #ifdef _SC_STREAM_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8109 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8110 | #endif |
| 8111 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8112 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8113 | #endif |
| 8114 | #ifdef _SC_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8115 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8116 | #endif |
| 8117 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8118 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8119 | #endif |
| 8120 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8121 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8122 | #endif |
| 8123 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8124 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8125 | #endif |
| 8126 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8127 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8128 | #endif |
| 8129 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8130 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8131 | #endif |
| 8132 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8133 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8134 | #endif |
| 8135 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8136 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8137 | #endif |
| 8138 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8139 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8140 | #endif |
| 8141 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8142 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8143 | #endif |
| 8144 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8145 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8146 | #endif |
| 8147 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8148 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8149 | #endif |
| 8150 | #ifdef _SC_TIMERS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8151 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8152 | #endif |
| 8153 | #ifdef _SC_TIMER_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8154 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8155 | #endif |
| 8156 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8157 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8158 | #endif |
| 8159 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8160 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8161 | #endif |
| 8162 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8163 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8164 | #endif |
| 8165 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8166 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8167 | #endif |
| 8168 | #ifdef _SC_UINT_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8169 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8170 | #endif |
| 8171 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8172 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8173 | #endif |
| 8174 | #ifdef _SC_ULONG_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8175 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8176 | #endif |
| 8177 | #ifdef _SC_USHRT_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8178 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8179 | #endif |
| 8180 | #ifdef _SC_VERSION |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8181 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8182 | #endif |
| 8183 | #ifdef _SC_WORD_BIT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8184 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8185 | #endif |
| 8186 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8187 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8188 | #endif |
| 8189 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8190 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8191 | #endif |
| 8192 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8193 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8194 | #endif |
| 8195 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8196 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8197 | #endif |
| 8198 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8199 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8200 | #endif |
| 8201 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8202 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8203 | #endif |
| 8204 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8205 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8206 | #endif |
| 8207 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8208 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8209 | #endif |
| 8210 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8211 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8212 | #endif |
| 8213 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8214 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8215 | #endif |
| 8216 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8217 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8218 | #endif |
| 8219 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8220 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8221 | #endif |
| 8222 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8223 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8224 | #endif |
| 8225 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8226 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8227 | #endif |
| 8228 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8229 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8230 | #endif |
| 8231 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8232 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8233 | #endif |
| 8234 | }; |
| 8235 | |
| 8236 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8237 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8238 | { |
| 8239 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 8240 | sizeof(posix_constants_sysconf) |
| 8241 | / sizeof(struct constdef)); |
| 8242 | } |
| 8243 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8244 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8245 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8246 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8247 | |
| 8248 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8249 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8250 | { |
| 8251 | PyObject *result = NULL; |
| 8252 | int name; |
| 8253 | |
| 8254 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
Victor Stinner | 862490a | 2010-05-06 00:03:44 +0000 | [diff] [blame] | 8255 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8256 | |
Victor Stinner | 862490a | 2010-05-06 00:03:44 +0000 | [diff] [blame] | 8257 | errno = 0; |
| 8258 | value = sysconf(name); |
| 8259 | if (value == -1 && errno != 0) |
| 8260 | posix_error(); |
| 8261 | else |
| 8262 | result = PyInt_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8263 | } |
| 8264 | return result; |
| 8265 | } |
| 8266 | #endif |
| 8267 | |
| 8268 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8269 | /* This code is used to ensure that the tables of configuration value names |
| 8270 | * are in sorted order as required by conv_confname(), and also to build the |
| 8271 | * the exported dictionaries that are used to publish information about the |
| 8272 | * names available on the host platform. |
| 8273 | * |
| 8274 | * Sorting the table at runtime ensures that the table is properly ordered |
| 8275 | * when used, even for platforms we're not able to test on. It also makes |
| 8276 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8277 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8278 | |
| 8279 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8280 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8281 | { |
| 8282 | const struct constdef *c1 = |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8283 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8284 | const struct constdef *c2 = |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8285 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8286 | |
| 8287 | return strcmp(c1->name, c2->name); |
| 8288 | } |
| 8289 | |
| 8290 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8291 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8292 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8293 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8294 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 8295 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8296 | |
| 8297 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 8298 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 8299 | if (d == NULL) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8300 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8301 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 8302 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8303 | PyObject *o = PyInt_FromLong(table[i].value); |
| 8304 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 8305 | Py_XDECREF(o); |
| 8306 | Py_DECREF(d); |
| 8307 | return -1; |
| 8308 | } |
| 8309 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8310 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8311 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8312 | } |
| 8313 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8314 | /* Return -1 on failure, 0 on success. */ |
| 8315 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8316 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8317 | { |
| 8318 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8319 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8320 | sizeof(posix_constants_pathconf) |
| 8321 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8322 | "pathconf_names", module)) |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 8323 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8324 | #endif |
| 8325 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8326 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8327 | sizeof(posix_constants_confstr) |
| 8328 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8329 | "confstr_names", module)) |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 8330 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8331 | #endif |
| 8332 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8333 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8334 | sizeof(posix_constants_sysconf) |
| 8335 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8336 | "sysconf_names", module)) |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 8337 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8338 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8339 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8340 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8341 | |
| 8342 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8343 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8344 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8345 | 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] | 8346 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8347 | |
| 8348 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8349 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8350 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8351 | abort(); |
| 8352 | /*NOTREACHED*/ |
| 8353 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 8354 | return NULL; |
| 8355 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8356 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8357 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8358 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 8359 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 8360 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 8361 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 8362 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 8363 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 8364 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 8365 | application (if any) its extension is associated.\n\ |
| 8366 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 8367 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 8368 | \n\ |
| 8369 | startfile returns as soon as the associated application is launched.\n\ |
| 8370 | There is no option to wait for the application to close, and no way\n\ |
| 8371 | to retrieve the application's exit status.\n\ |
| 8372 | \n\ |
| 8373 | The filepath is relative to the current directory. If you want to use\n\ |
| 8374 | 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] | 8375 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 8376 | |
| 8377 | static PyObject * |
| 8378 | win32_startfile(PyObject *self, PyObject *args) |
| 8379 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8380 | char *filepath; |
| 8381 | char *operation = NULL; |
| 8382 | HINSTANCE rc; |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 8383 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8384 | PyObject *unipath, *woperation = NULL; |
| 8385 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 8386 | &unipath, &operation)) { |
| 8387 | PyErr_Clear(); |
| 8388 | goto normal; |
| 8389 | } |
Hirokazu Yamamoto | a3c5609 | 2009-06-28 10:23:00 +0000 | [diff] [blame] | 8390 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8391 | if (operation) { |
| 8392 | woperation = PyUnicode_DecodeASCII(operation, |
| 8393 | strlen(operation), NULL); |
| 8394 | if (!woperation) { |
| 8395 | PyErr_Clear(); |
| 8396 | operation = NULL; |
| 8397 | goto normal; |
| 8398 | } |
| 8399 | } |
Hirokazu Yamamoto | a3c5609 | 2009-06-28 10:23:00 +0000 | [diff] [blame] | 8400 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8401 | Py_BEGIN_ALLOW_THREADS |
| 8402 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 8403 | PyUnicode_AS_UNICODE(unipath), |
| 8404 | NULL, NULL, SW_SHOWNORMAL); |
| 8405 | Py_END_ALLOW_THREADS |
| 8406 | |
| 8407 | Py_XDECREF(woperation); |
| 8408 | if (rc <= (HINSTANCE)32) { |
| 8409 | PyObject *errval = win32_error_unicode("startfile", |
| 8410 | PyUnicode_AS_UNICODE(unipath)); |
| 8411 | return errval; |
| 8412 | } |
| 8413 | Py_INCREF(Py_None); |
| 8414 | return Py_None; |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 8415 | |
| 8416 | normal: |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8417 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 8418 | Py_FileSystemDefaultEncoding, &filepath, |
| 8419 | &operation)) |
| 8420 | return NULL; |
| 8421 | Py_BEGIN_ALLOW_THREADS |
| 8422 | rc = ShellExecute((HWND)0, operation, filepath, |
| 8423 | NULL, NULL, SW_SHOWNORMAL); |
| 8424 | Py_END_ALLOW_THREADS |
| 8425 | if (rc <= (HINSTANCE)32) { |
| 8426 | PyObject *errval = win32_error("startfile", filepath); |
| 8427 | PyMem_Free(filepath); |
| 8428 | return errval; |
| 8429 | } |
| 8430 | PyMem_Free(filepath); |
| 8431 | Py_INCREF(Py_None); |
| 8432 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 8433 | } |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 8434 | #endif /* MS_WINDOWS */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8435 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8436 | #ifdef HAVE_GETLOADAVG |
| 8437 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 8438 | "getloadavg() -> (float, float, float)\n\n\ |
| 8439 | Return the number of processes in the system run queue averaged over\n\ |
| 8440 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 8441 | was unobtainable"); |
| 8442 | |
| 8443 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8444 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8445 | { |
| 8446 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8447 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 8448 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 8449 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8450 | } else |
Stefan Krah | 93f7a32 | 2010-11-26 17:35:50 +0000 | [diff] [blame] | 8451 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8452 | } |
| 8453 | #endif |
| 8454 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8455 | #ifdef MS_WINDOWS |
| 8456 | |
| 8457 | PyDoc_STRVAR(win32_urandom__doc__, |
| 8458 | "urandom(n) -> str\n\n\ |
| 8459 | Return a string of n random bytes suitable for cryptographic use."); |
| 8460 | |
| 8461 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 8462 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 8463 | DWORD dwFlags ); |
| 8464 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 8465 | BYTE *pbBuffer ); |
| 8466 | |
| 8467 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Martin v. Löwis | af699dd | 2007-09-04 09:51:57 +0000 | [diff] [blame] | 8468 | /* This handle is never explicitly released. Instead, the operating |
| 8469 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8470 | static HCRYPTPROV hCryptProv = 0; |
| 8471 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 8472 | static PyObject* |
| 8473 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8474 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8475 | int howMany; |
| 8476 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8477 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8478 | /* Read arguments */ |
| 8479 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 8480 | return NULL; |
| 8481 | if (howMany < 0) |
| 8482 | return PyErr_Format(PyExc_ValueError, |
| 8483 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8484 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8485 | if (hCryptProv == 0) { |
| 8486 | HINSTANCE hAdvAPI32 = NULL; |
| 8487 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8488 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8489 | /* Obtain handle to the DLL containing CryptoAPI |
| 8490 | This should not fail */ |
| 8491 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 8492 | if(hAdvAPI32 == NULL) |
| 8493 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8494 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8495 | /* Obtain pointers to the CryptoAPI functions |
| 8496 | This will fail on some early versions of Win95 */ |
| 8497 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 8498 | hAdvAPI32, |
| 8499 | "CryptAcquireContextA"); |
| 8500 | if (pCryptAcquireContext == NULL) |
| 8501 | return PyErr_Format(PyExc_NotImplementedError, |
| 8502 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8503 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8504 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 8505 | hAdvAPI32, "CryptGenRandom"); |
| 8506 | if (pCryptGenRandom == NULL) |
| 8507 | return PyErr_Format(PyExc_NotImplementedError, |
| 8508 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8509 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8510 | /* Acquire context */ |
| 8511 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 8512 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 8513 | return win32_error("CryptAcquireContext", NULL); |
| 8514 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8515 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8516 | /* Allocate bytes */ |
| 8517 | result = PyString_FromStringAndSize(NULL, howMany); |
| 8518 | if (result != NULL) { |
| 8519 | /* Get random data */ |
| 8520 | memset(PyString_AS_STRING(result), 0, howMany); /* zero seed */ |
| 8521 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 8522 | PyString_AS_STRING(result))) { |
| 8523 | Py_DECREF(result); |
| 8524 | return win32_error("CryptGenRandom", NULL); |
| 8525 | } |
| 8526 | } |
| 8527 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8528 | } |
| 8529 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8530 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 8531 | #ifdef __VMS |
| 8532 | /* Use openssl random routine */ |
| 8533 | #include <openssl/rand.h> |
| 8534 | PyDoc_STRVAR(vms_urandom__doc__, |
| 8535 | "urandom(n) -> str\n\n\ |
| 8536 | Return a string of n random bytes suitable for cryptographic use."); |
| 8537 | |
| 8538 | static PyObject* |
| 8539 | vms_urandom(PyObject *self, PyObject *args) |
| 8540 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8541 | int howMany; |
| 8542 | PyObject* result; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 8543 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8544 | /* Read arguments */ |
| 8545 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 8546 | return NULL; |
| 8547 | if (howMany < 0) |
| 8548 | return PyErr_Format(PyExc_ValueError, |
| 8549 | "negative argument not allowed"); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 8550 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8551 | /* Allocate bytes */ |
| 8552 | result = PyString_FromStringAndSize(NULL, howMany); |
| 8553 | if (result != NULL) { |
| 8554 | /* Get random data */ |
| 8555 | if (RAND_pseudo_bytes((unsigned char*) |
| 8556 | PyString_AS_STRING(result), |
| 8557 | howMany) < 0) { |
| 8558 | Py_DECREF(result); |
| 8559 | return PyErr_Format(PyExc_ValueError, |
| 8560 | "RAND_pseudo_bytes"); |
| 8561 | } |
| 8562 | } |
| 8563 | return result; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 8564 | } |
| 8565 | #endif |
| 8566 | |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8567 | #ifdef HAVE_SETRESUID |
| 8568 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 8569 | "setresuid(ruid, euid, suid)\n\n\ |
| 8570 | Set the current process's real, effective, and saved user ids."); |
| 8571 | |
| 8572 | static PyObject* |
| 8573 | posix_setresuid (PyObject *self, PyObject *args) |
| 8574 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8575 | /* We assume uid_t is no larger than a long. */ |
| 8576 | long ruid, euid, suid; |
| 8577 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 8578 | return NULL; |
| 8579 | if (setresuid(ruid, euid, suid) < 0) |
| 8580 | return posix_error(); |
| 8581 | Py_RETURN_NONE; |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8582 | } |
| 8583 | #endif |
| 8584 | |
| 8585 | #ifdef HAVE_SETRESGID |
| 8586 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 8587 | "setresgid(rgid, egid, sgid)\n\n\ |
| 8588 | Set the current process's real, effective, and saved group ids."); |
| 8589 | |
| 8590 | static PyObject* |
| 8591 | posix_setresgid (PyObject *self, PyObject *args) |
| 8592 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8593 | /* We assume uid_t is no larger than a long. */ |
| 8594 | long rgid, egid, sgid; |
| 8595 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 8596 | return NULL; |
| 8597 | if (setresgid(rgid, egid, sgid) < 0) |
| 8598 | return posix_error(); |
| 8599 | Py_RETURN_NONE; |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8600 | } |
| 8601 | #endif |
| 8602 | |
| 8603 | #ifdef HAVE_GETRESUID |
| 8604 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 8605 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 8606 | Get tuple of the current process's real, effective, and saved user ids."); |
| 8607 | |
| 8608 | static PyObject* |
| 8609 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 8610 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8611 | uid_t ruid, euid, suid; |
| 8612 | long l_ruid, l_euid, l_suid; |
| 8613 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 8614 | return posix_error(); |
| 8615 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 8616 | l_ruid = ruid; |
| 8617 | l_euid = euid; |
| 8618 | l_suid = suid; |
| 8619 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8620 | } |
| 8621 | #endif |
| 8622 | |
| 8623 | #ifdef HAVE_GETRESGID |
| 8624 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 8625 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | 21946af | 2010-10-06 09:28:45 +0000 | [diff] [blame] | 8626 | 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] | 8627 | |
| 8628 | static PyObject* |
| 8629 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 8630 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8631 | uid_t rgid, egid, sgid; |
| 8632 | long l_rgid, l_egid, l_sgid; |
| 8633 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 8634 | return posix_error(); |
| 8635 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 8636 | l_rgid = rgid; |
| 8637 | l_egid = egid; |
| 8638 | l_sgid = sgid; |
| 8639 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8640 | } |
| 8641 | #endif |
| 8642 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8643 | static PyMethodDef posix_methods[] = { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8644 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8645 | #ifdef HAVE_TTYNAME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8646 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8647 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8648 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 8649 | #ifdef HAVE_CHFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8650 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 8651 | #endif /* HAVE_CHFLAGS */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8652 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 8653 | #ifdef HAVE_FCHMOD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8654 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 8655 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8656 | #ifdef HAVE_CHOWN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8657 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8658 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 8659 | #ifdef HAVE_LCHMOD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8660 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 8661 | #endif /* HAVE_LCHMOD */ |
| 8662 | #ifdef HAVE_FCHOWN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8663 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 3628187 | 2007-11-30 21:11:28 +0000 | [diff] [blame] | 8664 | #endif /* HAVE_FCHOWN */ |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 8665 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8666 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 8667 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 8668 | #ifdef HAVE_LCHOWN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8669 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 8670 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 8671 | #ifdef HAVE_CHROOT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8672 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 8673 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8674 | #ifdef HAVE_CTERMID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8675 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8676 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 8677 | #ifdef HAVE_GETCWD |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8678 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 8679 | #ifdef Py_USING_UNICODE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8680 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 8681 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 8682 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8683 | #ifdef HAVE_LINK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8684 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8685 | #endif /* HAVE_LINK */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8686 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 8687 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 8688 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8689 | #ifdef HAVE_NICE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8690 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8691 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8692 | #ifdef HAVE_READLINK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8693 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8694 | #endif /* HAVE_READLINK */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8695 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 8696 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 8697 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
| 8698 | {"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] | 8699 | #ifdef HAVE_SYMLINK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8700 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8701 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8702 | #ifdef HAVE_SYSTEM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8703 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8704 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8705 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8706 | #ifdef HAVE_UNAME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8707 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8708 | #endif /* HAVE_UNAME */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8709 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 8710 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 8711 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8712 | #ifdef HAVE_TIMES |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8713 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8714 | #endif /* HAVE_TIMES */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8715 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8716 | #ifdef HAVE_EXECV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8717 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 8718 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8719 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 8720 | #ifdef HAVE_SPAWNV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8721 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 8722 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 8723 | #if defined(PYOS_OS2) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8724 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 8725 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 8726 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 8727 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 8728 | #ifdef HAVE_FORK1 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8729 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 8730 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8731 | #ifdef HAVE_FORK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8732 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8733 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 8734 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8735 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 8736 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 8737 | #ifdef HAVE_FORKPTY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8738 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 8739 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8740 | #ifdef HAVE_GETEGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8741 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8742 | #endif /* HAVE_GETEGID */ |
| 8743 | #ifdef HAVE_GETEUID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8744 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8745 | #endif /* HAVE_GETEUID */ |
| 8746 | #ifdef HAVE_GETGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8747 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8748 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8749 | #ifdef HAVE_GETGROUPS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8750 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8751 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8752 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8753 | #ifdef HAVE_GETPGRP |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8754 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8755 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8756 | #ifdef HAVE_GETPPID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8757 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8758 | #endif /* HAVE_GETPPID */ |
| 8759 | #ifdef HAVE_GETUID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8760 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8761 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8762 | #ifdef HAVE_GETLOGIN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8763 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8764 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8765 | #ifdef HAVE_KILL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8766 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8767 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 8768 | #ifdef HAVE_KILLPG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8769 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 8770 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8771 | #ifdef HAVE_PLOCK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8772 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8773 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8774 | #ifdef HAVE_POPEN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8775 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8776 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8777 | {"popen2", win32_popen2, METH_VARARGS}, |
| 8778 | {"popen3", win32_popen3, METH_VARARGS}, |
| 8779 | {"popen4", win32_popen4, METH_VARARGS}, |
| 8780 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 8781 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8782 | #else |
| 8783 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8784 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 8785 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 8786 | {"popen4", os2emx_popen4, METH_VARARGS}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8787 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 8788 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8789 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8790 | #ifdef HAVE_SETUID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8791 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8792 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8793 | #ifdef HAVE_SETEUID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8794 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8795 | #endif /* HAVE_SETEUID */ |
| 8796 | #ifdef HAVE_SETEGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8797 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8798 | #endif /* HAVE_SETEGID */ |
| 8799 | #ifdef HAVE_SETREUID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8800 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8801 | #endif /* HAVE_SETREUID */ |
| 8802 | #ifdef HAVE_SETREGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8803 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8804 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8805 | #ifdef HAVE_SETGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8806 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8807 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8808 | #ifdef HAVE_SETGROUPS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8809 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8810 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 8811 | #ifdef HAVE_INITGROUPS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8812 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 8813 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 8814 | #ifdef HAVE_GETPGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8815 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 8816 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8817 | #ifdef HAVE_SETPGRP |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8818 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8819 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8820 | #ifdef HAVE_WAIT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8821 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8822 | #endif /* HAVE_WAIT */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 8823 | #ifdef HAVE_WAIT3 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8824 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 8825 | #endif /* HAVE_WAIT3 */ |
| 8826 | #ifdef HAVE_WAIT4 |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8827 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 8828 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 8829 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8830 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8831 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8832 | #ifdef HAVE_GETSID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8833 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8834 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8835 | #ifdef HAVE_SETSID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8836 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8837 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8838 | #ifdef HAVE_SETPGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8839 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8840 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8841 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8842 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8843 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8844 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8845 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8846 | #endif /* HAVE_TCSETPGRP */ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8847 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 8848 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 8849 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 8850 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 8851 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 8852 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 8853 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 8854 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 8855 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 8856 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
| 8857 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8858 | #ifdef HAVE_PIPE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8859 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8860 | #endif |
| 8861 | #ifdef HAVE_MKFIFO |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8862 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8863 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 8864 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8865 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8866 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8867 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8868 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 8869 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 8870 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8871 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8872 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8873 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8874 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8875 | #ifdef HAVE_PUTENV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8876 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8877 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8878 | #ifdef HAVE_UNSETENV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8879 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8880 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8881 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8882 | #ifdef HAVE_FCHDIR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8883 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8884 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8885 | #ifdef HAVE_FSYNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8886 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8887 | #endif |
| 8888 | #ifdef HAVE_FDATASYNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8889 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8890 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8891 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8892 | #ifdef WCOREDUMP |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8893 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8894 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8895 | #ifdef WIFCONTINUED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8896 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8897 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8898 | #ifdef WIFSTOPPED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8899 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8900 | #endif /* WIFSTOPPED */ |
| 8901 | #ifdef WIFSIGNALED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8902 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8903 | #endif /* WIFSIGNALED */ |
| 8904 | #ifdef WIFEXITED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8905 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8906 | #endif /* WIFEXITED */ |
| 8907 | #ifdef WEXITSTATUS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8908 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8909 | #endif /* WEXITSTATUS */ |
| 8910 | #ifdef WTERMSIG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8911 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8912 | #endif /* WTERMSIG */ |
| 8913 | #ifdef WSTOPSIG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8914 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8915 | #endif /* WSTOPSIG */ |
| 8916 | #endif /* HAVE_SYS_WAIT_H */ |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 8917 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8918 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8919 | #endif |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 8920 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8921 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8922 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 8923 | #ifdef HAVE_TMPFILE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8924 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8925 | #endif |
| 8926 | #ifdef HAVE_TEMPNAM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8927 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8928 | #endif |
| 8929 | #ifdef HAVE_TMPNAM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8930 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8931 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8932 | #ifdef HAVE_CONFSTR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8933 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8934 | #endif |
| 8935 | #ifdef HAVE_SYSCONF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8936 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8937 | #endif |
| 8938 | #ifdef HAVE_FPATHCONF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8939 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8940 | #endif |
| 8941 | #ifdef HAVE_PATHCONF |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8942 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8943 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8944 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8945 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8946 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 8947 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8948 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8949 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8950 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8951 | #ifdef MS_WINDOWS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8952 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8953 | #endif |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 8954 | #ifdef __VMS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8955 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 8956 | #endif |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8957 | #ifdef HAVE_SETRESUID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8958 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8959 | #endif |
| 8960 | #ifdef HAVE_SETRESGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8961 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8962 | #endif |
| 8963 | #ifdef HAVE_GETRESUID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8964 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8965 | #endif |
| 8966 | #ifdef HAVE_GETRESGID |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8967 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 8968 | #endif |
| 8969 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8970 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8971 | }; |
| 8972 | |
| 8973 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8974 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8975 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8976 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 8977 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8978 | } |
| 8979 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8980 | #if defined(PYOS_OS2) |
| 8981 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8982 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8983 | { |
| 8984 | APIRET rc; |
| 8985 | ULONG values[QSV_MAX+1]; |
| 8986 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 8987 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8988 | |
| 8989 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 8990 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8991 | Py_END_ALLOW_THREADS |
| 8992 | |
| 8993 | if (rc != NO_ERROR) { |
| 8994 | os2_error(rc); |
| 8995 | return -1; |
| 8996 | } |
| 8997 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8998 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 8999 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 9000 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 9001 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 9002 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 9003 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 9004 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9005 | |
| 9006 | switch (values[QSV_VERSION_MINOR]) { |
| 9007 | case 0: ver = "2.00"; break; |
| 9008 | case 10: ver = "2.10"; break; |
| 9009 | case 11: ver = "2.11"; break; |
| 9010 | case 30: ver = "3.00"; break; |
| 9011 | case 40: ver = "4.00"; break; |
| 9012 | case 50: ver = "5.00"; break; |
| 9013 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 9014 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9015 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 9016 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9017 | ver = &tmp[0]; |
| 9018 | } |
| 9019 | |
| 9020 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9021 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9022 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9023 | |
| 9024 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 9025 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 9026 | tmp[1] = ':'; |
| 9027 | tmp[2] = '\0'; |
| 9028 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9029 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9030 | } |
| 9031 | #endif |
| 9032 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9033 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 9034 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9035 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9036 | #ifdef F_OK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9037 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9038 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9039 | #ifdef R_OK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9040 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9041 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9042 | #ifdef W_OK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9043 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9044 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9045 | #ifdef X_OK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9046 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9047 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9048 | #ifdef NGROUPS_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9049 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9050 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9051 | #ifdef TMP_MAX |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9052 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9053 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9054 | #ifdef WCONTINUED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9055 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9056 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9057 | #ifdef WNOHANG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9058 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9059 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9060 | #ifdef WUNTRACED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9061 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9062 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9063 | #ifdef O_RDONLY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9064 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9065 | #endif |
| 9066 | #ifdef O_WRONLY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9067 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9068 | #endif |
| 9069 | #ifdef O_RDWR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9070 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9071 | #endif |
| 9072 | #ifdef O_NDELAY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9073 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9074 | #endif |
| 9075 | #ifdef O_NONBLOCK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9076 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9077 | #endif |
| 9078 | #ifdef O_APPEND |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9079 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9080 | #endif |
| 9081 | #ifdef O_DSYNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9082 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9083 | #endif |
| 9084 | #ifdef O_RSYNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9085 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9086 | #endif |
| 9087 | #ifdef O_SYNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9088 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9089 | #endif |
| 9090 | #ifdef O_NOCTTY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9091 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9092 | #endif |
| 9093 | #ifdef O_CREAT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9094 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9095 | #endif |
| 9096 | #ifdef O_EXCL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9097 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9098 | #endif |
| 9099 | #ifdef O_TRUNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9100 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9101 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 9102 | #ifdef O_BINARY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9103 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 9104 | #endif |
| 9105 | #ifdef O_TEXT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9106 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 9107 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9108 | #ifdef O_LARGEFILE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9109 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9110 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 9111 | #ifdef O_SHLOCK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9112 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 9113 | #endif |
| 9114 | #ifdef O_EXLOCK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9115 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 9116 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9117 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9118 | /* MS Windows */ |
| 9119 | #ifdef O_NOINHERIT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9120 | /* Don't inherit in child processes. */ |
| 9121 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9122 | #endif |
| 9123 | #ifdef _O_SHORT_LIVED |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9124 | /* Optimize for short life (keep in memory). */ |
| 9125 | /* MS forgot to define this one with a non-underscore form too. */ |
| 9126 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9127 | #endif |
| 9128 | #ifdef O_TEMPORARY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9129 | /* Automatically delete when last handle is closed. */ |
| 9130 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9131 | #endif |
| 9132 | #ifdef O_RANDOM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9133 | /* Optimize for random access. */ |
| 9134 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9135 | #endif |
| 9136 | #ifdef O_SEQUENTIAL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9137 | /* Optimize for sequential access. */ |
| 9138 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9139 | #endif |
| 9140 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9141 | /* GNU extensions. */ |
Georg Brandl | 5049a85 | 2008-05-16 13:10:15 +0000 | [diff] [blame] | 9142 | #ifdef O_ASYNC |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9143 | /* Send a SIGIO signal whenever input or output |
| 9144 | becomes available on file descriptor */ |
| 9145 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Georg Brandl | 5049a85 | 2008-05-16 13:10:15 +0000 | [diff] [blame] | 9146 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9147 | #ifdef O_DIRECT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9148 | /* Direct disk access. */ |
| 9149 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9150 | #endif |
| 9151 | #ifdef O_DIRECTORY |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9152 | /* Must be a directory. */ |
| 9153 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9154 | #endif |
| 9155 | #ifdef O_NOFOLLOW |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9156 | /* Do not follow links. */ |
| 9157 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 9158 | #endif |
Georg Brandl | b67da6e | 2007-11-24 13:56:09 +0000 | [diff] [blame] | 9159 | #ifdef O_NOATIME |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9160 | /* Do not update the access time. */ |
| 9161 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Georg Brandl | b67da6e | 2007-11-24 13:56:09 +0000 | [diff] [blame] | 9162 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9163 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9164 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9165 | #ifdef EX_OK |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9166 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9167 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9168 | #ifdef EX_USAGE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9169 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9170 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9171 | #ifdef EX_DATAERR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9172 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9173 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9174 | #ifdef EX_NOINPUT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9175 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9176 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9177 | #ifdef EX_NOUSER |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9178 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9179 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9180 | #ifdef EX_NOHOST |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9181 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9182 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9183 | #ifdef EX_UNAVAILABLE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9184 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9185 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9186 | #ifdef EX_SOFTWARE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9187 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9188 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9189 | #ifdef EX_OSERR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9190 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9191 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9192 | #ifdef EX_OSFILE |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9193 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9194 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9195 | #ifdef EX_CANTCREAT |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9196 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9197 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9198 | #ifdef EX_IOERR |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9199 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9200 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9201 | #ifdef EX_TEMPFAIL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9202 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9203 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9204 | #ifdef EX_PROTOCOL |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9205 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9206 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9207 | #ifdef EX_NOPERM |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9208 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9209 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9210 | #ifdef EX_CONFIG |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9211 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9212 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9213 | #ifdef EX_NOTFOUND |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9214 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 9215 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 9216 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 9217 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9218 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9219 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 9220 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 9221 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 9222 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 9223 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 9224 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 9225 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 9226 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 9227 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 9228 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 9229 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 9230 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 9231 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 9232 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 9233 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 9234 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 9235 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 9236 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 9237 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 9238 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9239 | #else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9240 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 9241 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 9242 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 9243 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 9244 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 9245 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9246 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 9247 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9248 | #if defined(PYOS_OS2) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9249 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 9250 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9251 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9252 | } |
| 9253 | |
| 9254 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9255 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 9256 | #define INITFUNC initnt |
| 9257 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 9258 | |
| 9259 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 9260 | #define INITFUNC initos2 |
| 9261 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 9262 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 9263 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 9264 | #define INITFUNC initposix |
| 9265 | #define MODNAME "posix" |
| 9266 | #endif |
| 9267 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 9268 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 9269 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9270 | { |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9271 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9272 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9273 | m = Py_InitModule3(MODNAME, |
| 9274 | posix_methods, |
| 9275 | posix__doc__); |
| 9276 | if (m == NULL) |
| 9277 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9278 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9279 | /* Initialize environ dictionary */ |
| 9280 | v = convertenviron(); |
| 9281 | Py_XINCREF(v); |
| 9282 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 9283 | return; |
| 9284 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9285 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9286 | if (all_ins(m)) |
| 9287 | return; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 9288 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9289 | if (setup_confname_tables(m)) |
| 9290 | return; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9291 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9292 | Py_INCREF(PyExc_OSError); |
| 9293 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 9294 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 9295 | #ifdef HAVE_PUTENV |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9296 | if (posix_putenv_garbage == NULL) |
| 9297 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 9298 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9299 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9300 | if (!initialized) { |
| 9301 | stat_result_desc.name = MODNAME ".stat_result"; |
| 9302 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 9303 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 9304 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 9305 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 9306 | structseq_new = StatResultType.tp_new; |
| 9307 | StatResultType.tp_new = statresult_new; |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 9308 | |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9309 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 9310 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 03824e4 | 2008-12-29 18:17:34 +0000 | [diff] [blame] | 9311 | #ifdef NEED_TICKS_PER_SECOND |
| 9312 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9313 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 03824e4 | 2008-12-29 18:17:34 +0000 | [diff] [blame] | 9314 | # elif defined(HZ) |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9315 | ticks_per_second = HZ; |
Martin v. Löwis | 03824e4 | 2008-12-29 18:17:34 +0000 | [diff] [blame] | 9316 | # else |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9317 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 03824e4 | 2008-12-29 18:17:34 +0000 | [diff] [blame] | 9318 | # endif |
| 9319 | #endif |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9320 | } |
| 9321 | Py_INCREF((PyObject*) &StatResultType); |
| 9322 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 9323 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 9324 | PyModule_AddObject(m, "statvfs_result", |
| 9325 | (PyObject*) &StatVFSResultType); |
| 9326 | initialized = 1; |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 9327 | |
| 9328 | #ifdef __APPLE__ |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9329 | /* |
| 9330 | * Step 2 of weak-linking support on Mac OS X. |
| 9331 | * |
| 9332 | * The code below removes functions that are not available on the |
| 9333 | * currently active platform. |
| 9334 | * |
| 9335 | * This block allow one to use a python binary that was build on |
| 9336 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 9337 | * OSX 10.4. |
| 9338 | */ |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 9339 | #ifdef HAVE_FSTATVFS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9340 | if (fstatvfs == NULL) { |
| 9341 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 9342 | return; |
| 9343 | } |
| 9344 | } |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 9345 | #endif /* HAVE_FSTATVFS */ |
| 9346 | |
| 9347 | #ifdef HAVE_STATVFS |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9348 | if (statvfs == NULL) { |
| 9349 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 9350 | return; |
| 9351 | } |
| 9352 | } |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 9353 | #endif /* HAVE_STATVFS */ |
| 9354 | |
| 9355 | # ifdef HAVE_LCHOWN |
Victor Stinner | d6f8542 | 2010-05-05 23:33:33 +0000 | [diff] [blame] | 9356 | if (lchown == NULL) { |
| 9357 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 9358 | return; |
| 9359 | } |
| 9360 | } |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 9361 | #endif /* HAVE_LCHOWN */ |
| 9362 | |
| 9363 | |
| 9364 | #endif /* __APPLE__ */ |
| 9365 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9366 | } |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 9367 | |
| 9368 | #ifdef __cplusplus |
| 9369 | } |
| 9370 | #endif |
| 9371 | |
Martin v. Löwis | 18aaa56 | 2006-10-15 08:43:33 +0000 | [diff] [blame] | 9372 | |