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 | /* |
| 18 | * Step 1 of support for weak-linking a number of symbols existing on |
| 19 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 20 | * at the end of this file for more information. |
| 21 | */ |
| 22 | # pragma weak lchown |
| 23 | # pragma weak statvfs |
| 24 | # pragma weak fstatvfs |
| 25 | |
| 26 | #endif /* __APPLE__ */ |
| 27 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 28 | #define PY_SSIZE_T_CLEAN |
| 29 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 30 | #include "Python.h" |
| 31 | #include "structseq.h" |
| 32 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 33 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 34 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 35 | #endif /* defined(__VMS) */ |
| 36 | |
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 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 67 | #include <sys/types.h> |
| 68 | #include <sys/stat.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 70 | #ifdef HAVE_SYS_WAIT_H |
| 71 | #include <sys/wait.h> /* For WNOHANG */ |
| 72 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 74 | #include <signal.h> |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 76 | #ifdef HAVE_FCNTL_H |
| 77 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 78 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 80 | #ifdef HAVE_GRP_H |
| 81 | #include <grp.h> |
| 82 | #endif |
| 83 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 84 | #ifdef HAVE_SYSEXITS_H |
| 85 | #include <sysexits.h> |
| 86 | #endif /* HAVE_SYSEXITS_H */ |
| 87 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 88 | #ifdef HAVE_SYS_LOADAVG_H |
| 89 | #include <sys/loadavg.h> |
| 90 | #endif |
| 91 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 92 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 93 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 94 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 95 | #include <process.h> |
| 96 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 97 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 98 | #define HAVE_GETCWD 1 |
| 99 | #define HAVE_OPENDIR 1 |
| 100 | #define HAVE_SYSTEM 1 |
| 101 | #if defined(__OS2__) |
| 102 | #define HAVE_EXECV 1 |
| 103 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 104 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 105 | #include <process.h> |
| 106 | #else |
| 107 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 108 | #define HAVE_EXECV 1 |
| 109 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 110 | #define HAVE_OPENDIR 1 |
| 111 | #define HAVE_PIPE 1 |
| 112 | #define HAVE_POPEN 1 |
| 113 | #define HAVE_SYSTEM 1 |
| 114 | #define HAVE_WAIT 1 |
| 115 | #else |
| 116 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 117 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 118 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 119 | #define HAVE_EXECV 1 |
| 120 | #define HAVE_PIPE 1 |
| 121 | #define HAVE_POPEN 1 |
| 122 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 123 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 124 | #define HAVE_FSYNC 1 |
| 125 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 126 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 127 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 128 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 129 | #else /* all other compilers */ |
| 130 | /* Unix functions that the configure script doesn't check for */ |
| 131 | #define HAVE_EXECV 1 |
| 132 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 133 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 134 | #define HAVE_FORK1 1 |
| 135 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 136 | #define HAVE_GETCWD 1 |
| 137 | #define HAVE_GETEGID 1 |
| 138 | #define HAVE_GETEUID 1 |
| 139 | #define HAVE_GETGID 1 |
| 140 | #define HAVE_GETPPID 1 |
| 141 | #define HAVE_GETUID 1 |
| 142 | #define HAVE_KILL 1 |
| 143 | #define HAVE_OPENDIR 1 |
| 144 | #define HAVE_PIPE 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 145 | #ifndef __rtems__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 146 | #define HAVE_POPEN 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 147 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 148 | #define HAVE_SYSTEM 1 |
| 149 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 150 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 151 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 152 | #endif /* _MSC_VER */ |
| 153 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 154 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 155 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 157 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 158 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 159 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 160 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 161 | (default) */ |
| 162 | extern char *ctermid_r(char *); |
| 163 | #endif |
| 164 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 165 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 166 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 167 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 168 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 169 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 170 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 171 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 172 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 173 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 174 | #endif |
| 175 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 176 | extern int chdir(char *); |
| 177 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 178 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 179 | extern int chdir(const char *); |
| 180 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 181 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 182 | #ifdef __BORLANDC__ |
| 183 | extern int chmod(const char *, int); |
| 184 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 185 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 186 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 187 | extern int chown(const char *, uid_t, gid_t); |
| 188 | extern char *getcwd(char *, int); |
| 189 | extern char *strerror(int); |
| 190 | extern int link(const char *, const char *); |
| 191 | extern int rename(const char *, const char *); |
| 192 | extern int stat(const char *, struct stat *); |
| 193 | extern int unlink(const char *); |
| 194 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 195 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 196 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 197 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 198 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 199 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 200 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 201 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 202 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 203 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 204 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | #ifdef HAVE_UTIME_H |
| 206 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 207 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 208 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 209 | #ifdef HAVE_SYS_UTIME_H |
| 210 | #include <sys/utime.h> |
| 211 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 212 | #endif /* HAVE_SYS_UTIME_H */ |
| 213 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 214 | #ifdef HAVE_SYS_TIMES_H |
| 215 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 216 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 217 | |
| 218 | #ifdef HAVE_SYS_PARAM_H |
| 219 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 220 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 221 | |
| 222 | #ifdef HAVE_SYS_UTSNAME_H |
| 223 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 224 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 225 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 226 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 227 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 228 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 229 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 230 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 231 | #include <direct.h> |
| 232 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 233 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 235 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 236 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 237 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 238 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 239 | #endif |
| 240 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 242 | #endif |
| 243 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 244 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 245 | #endif |
| 246 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 248 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 249 | #include <direct.h> |
| 250 | #include <io.h> |
| 251 | #include <process.h> |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 252 | #include "osdefs.h" |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 253 | #define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 255 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 256 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 257 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 258 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 259 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 260 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 261 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 262 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 263 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 264 | #ifndef MAXPATHLEN |
Thomas Wouters | 1ddba60 | 2006-04-25 15:29:46 +0000 | [diff] [blame] | 265 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 266 | #define MAXPATHLEN PATH_MAX |
| 267 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 268 | #define MAXPATHLEN 1024 |
Thomas Wouters | 1ddba60 | 2006-04-25 15:29:46 +0000 | [diff] [blame] | 269 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 270 | #endif /* MAXPATHLEN */ |
| 271 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 272 | #ifdef UNION_WAIT |
| 273 | /* Emulate some macros on systems that have a union instead of macros */ |
| 274 | |
| 275 | #ifndef WIFEXITED |
| 276 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 277 | #endif |
| 278 | |
| 279 | #ifndef WEXITSTATUS |
| 280 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 281 | #endif |
| 282 | |
| 283 | #ifndef WTERMSIG |
| 284 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 285 | #endif |
| 286 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 287 | #define WAIT_TYPE union wait |
| 288 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 289 | |
| 290 | #else /* !UNION_WAIT */ |
| 291 | #define WAIT_TYPE int |
| 292 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 293 | #endif /* UNION_WAIT */ |
| 294 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 295 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 296 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 297 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 298 | #define USE_CTERMID_R |
| 299 | #endif |
| 300 | |
| 301 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 302 | #define USE_TMPNAM_R |
| 303 | #endif |
| 304 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 305 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 306 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 307 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 308 | # define STAT win32_stat |
| 309 | # define FSTAT win32_fstat |
| 310 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 311 | #else |
| 312 | # define STAT stat |
| 313 | # define FSTAT fstat |
| 314 | # define STRUCT_STAT struct stat |
| 315 | #endif |
| 316 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 317 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 318 | #include <sys/mkdev.h> |
| 319 | #else |
| 320 | #if defined(MAJOR_IN_SYSMACROS) |
| 321 | #include <sys/sysmacros.h> |
| 322 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 323 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 324 | #include <sys/mkdev.h> |
| 325 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 326 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 327 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 328 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 329 | #ifdef WITH_NEXT_FRAMEWORK |
| 330 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 331 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 332 | */ |
| 333 | #include <crt_externs.h> |
| 334 | static char **environ; |
| 335 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 336 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 337 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 338 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 339 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 340 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 341 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 342 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 343 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 344 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 345 | if (d == NULL) |
| 346 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 347 | #ifdef WITH_NEXT_FRAMEWORK |
| 348 | if (environ == NULL) |
| 349 | environ = *_NSGetEnviron(); |
| 350 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 351 | if (environ == NULL) |
| 352 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 353 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 354 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 355 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 356 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 357 | char *p = strchr(*e, '='); |
| 358 | if (p == NULL) |
| 359 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 360 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 361 | if (k == NULL) { |
| 362 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 363 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 364 | } |
| 365 | v = PyString_FromString(p+1); |
| 366 | if (v == NULL) { |
| 367 | PyErr_Clear(); |
| 368 | Py_DECREF(k); |
| 369 | continue; |
| 370 | } |
| 371 | if (PyDict_GetItem(d, k) == NULL) { |
| 372 | if (PyDict_SetItem(d, k, v) != 0) |
| 373 | PyErr_Clear(); |
| 374 | } |
| 375 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 376 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 377 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 378 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 379 | { |
| 380 | APIRET rc; |
| 381 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 382 | |
| 383 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 384 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 385 | PyObject *v = PyString_FromString(buffer); |
| 386 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 387 | Py_DECREF(v); |
| 388 | } |
| 389 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 390 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 391 | PyObject *v = PyString_FromString(buffer); |
| 392 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 393 | Py_DECREF(v); |
| 394 | } |
| 395 | } |
| 396 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 397 | return d; |
| 398 | } |
| 399 | |
| 400 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 401 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 402 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 403 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 404 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 405 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 406 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 407 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 408 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 409 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 410 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 411 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 414 | #ifdef Py_WIN_WIDE_FILENAMES |
| 415 | static PyObject * |
| 416 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 417 | { |
| 418 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 419 | } |
| 420 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 421 | |
| 422 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 423 | static PyObject * |
| 424 | posix_error_with_allocated_filename(char* name) |
| 425 | { |
| 426 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 427 | PyMem_Free(name); |
| 428 | return rc; |
| 429 | } |
| 430 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 431 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 432 | static PyObject * |
| 433 | win32_error(char* function, char* filename) |
| 434 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 435 | /* XXX We should pass the function name along in the future. |
| 436 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 437 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 438 | Windows error object, which is non-trivial. |
| 439 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 440 | errno = GetLastError(); |
| 441 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 442 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 443 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 444 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 445 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 446 | |
| 447 | #ifdef Py_WIN_WIDE_FILENAMES |
| 448 | static PyObject * |
| 449 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 450 | { |
| 451 | /* XXX - see win32_error for comments on 'function' */ |
| 452 | errno = GetLastError(); |
| 453 | if (filename) |
| 454 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 455 | else |
| 456 | return PyErr_SetFromWindowsErr(errno); |
| 457 | } |
| 458 | |
| 459 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 460 | { |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* Function suitable for O& conversion */ |
| 464 | static int |
| 465 | convert_to_unicode(PyObject *arg, void* _param) |
| 466 | { |
| 467 | PyObject **param = (PyObject**)_param; |
| 468 | if (PyUnicode_CheckExact(arg)) { |
| 469 | Py_INCREF(arg); |
| 470 | *param = arg; |
| 471 | } |
| 472 | else if (PyUnicode_Check(arg)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 473 | /* For a Unicode subtype that's not a Unicode object, |
| 474 | return a true Unicode object with the same data. */ |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 475 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg), |
| 476 | PyUnicode_GET_SIZE(arg)); |
| 477 | return *param != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 478 | } |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 479 | else |
| 480 | *param = PyUnicode_FromEncodedObject(arg, |
| 481 | Py_FileSystemDefaultEncoding, |
| 482 | "strict"); |
| 483 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 487 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 488 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 489 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 490 | #if defined(PYOS_OS2) |
| 491 | /********************************************************************** |
| 492 | * Helper Function to Trim and Format OS/2 Messages |
| 493 | **********************************************************************/ |
| 494 | static void |
| 495 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 496 | { |
| 497 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 498 | |
| 499 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 500 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 501 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 502 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 503 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 504 | } |
| 505 | |
| 506 | /* Add Optional Reason Text */ |
| 507 | if (reason) { |
| 508 | strcat(msgbuf, " : "); |
| 509 | strcat(msgbuf, reason); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /********************************************************************** |
| 514 | * Decode an OS/2 Operating System Error Code |
| 515 | * |
| 516 | * A convenience function to lookup an OS/2 error code and return a |
| 517 | * text message we can use to raise a Python exception. |
| 518 | * |
| 519 | * Notes: |
| 520 | * The messages for errors returned from the OS/2 kernel reside in |
| 521 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 522 | * |
| 523 | **********************************************************************/ |
| 524 | static char * |
| 525 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 526 | { |
| 527 | APIRET rc; |
| 528 | ULONG msglen; |
| 529 | |
| 530 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 531 | Py_BEGIN_ALLOW_THREADS |
| 532 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 533 | errorcode, "oso001.msg", &msglen); |
| 534 | Py_END_ALLOW_THREADS |
| 535 | |
| 536 | if (rc == NO_ERROR) |
| 537 | os2_formatmsg(msgbuf, msglen, reason); |
| 538 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 539 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 540 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 541 | |
| 542 | return msgbuf; |
| 543 | } |
| 544 | |
| 545 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 546 | errors are not in a global variable e.g. 'errno' nor are |
| 547 | they congruent with posix error numbers. */ |
| 548 | |
| 549 | static PyObject * os2_error(int code) |
| 550 | { |
| 551 | char text[1024]; |
| 552 | PyObject *v; |
| 553 | |
| 554 | os2_strerror(text, sizeof(text), code, ""); |
| 555 | |
| 556 | v = Py_BuildValue("(is)", code, text); |
| 557 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 558 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 559 | Py_DECREF(v); |
| 560 | } |
| 561 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 562 | } |
| 563 | |
| 564 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 565 | |
| 566 | /* POSIX generic methods */ |
| 567 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 568 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 569 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 570 | { |
| 571 | int fd; |
| 572 | int res; |
| 573 | fd = PyObject_AsFileDescriptor(fdobj); |
| 574 | if (fd < 0) |
| 575 | return NULL; |
| 576 | Py_BEGIN_ALLOW_THREADS |
| 577 | res = (*func)(fd); |
| 578 | Py_END_ALLOW_THREADS |
| 579 | if (res < 0) |
| 580 | return posix_error(); |
| 581 | Py_INCREF(Py_None); |
| 582 | return Py_None; |
| 583 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 584 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 585 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 586 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 587 | unicode_file_names(void) |
| 588 | { |
| 589 | static int canusewide = -1; |
| 590 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 591 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 592 | the Windows NT family. */ |
| 593 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 594 | } |
| 595 | return canusewide; |
| 596 | } |
| 597 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 598 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 599 | static PyObject * |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 600 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 601 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 602 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 603 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 604 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 605 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 606 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 607 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 608 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 609 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 610 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 611 | return posix_error_with_allocated_filename(path1); |
| 612 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 613 | Py_INCREF(Py_None); |
| 614 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 617 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 618 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 619 | char *format, |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 620 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 621 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 622 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 623 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 624 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 625 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 626 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 627 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 628 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 629 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 630 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 631 | PyMem_Free(path1); |
| 632 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 633 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 634 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 635 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 636 | Py_INCREF(Py_None); |
| 637 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 640 | #ifdef Py_WIN_WIDE_FILENAMES |
| 641 | static PyObject* |
| 642 | win32_1str(PyObject* args, char* func, |
| 643 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 644 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
| 645 | { |
| 646 | PyObject *uni; |
| 647 | char *ansi; |
| 648 | BOOL result; |
| 649 | if (unicode_file_names()) { |
| 650 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 651 | PyErr_Clear(); |
| 652 | else { |
| 653 | Py_BEGIN_ALLOW_THREADS |
| 654 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 655 | Py_END_ALLOW_THREADS |
| 656 | if (!result) |
| 657 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 658 | Py_INCREF(Py_None); |
| 659 | return Py_None; |
| 660 | } |
| 661 | } |
| 662 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 663 | return NULL; |
| 664 | Py_BEGIN_ALLOW_THREADS |
| 665 | result = funcA(ansi); |
| 666 | Py_END_ALLOW_THREADS |
| 667 | if (!result) |
| 668 | return win32_error(func, ansi); |
| 669 | Py_INCREF(Py_None); |
| 670 | return Py_None; |
| 671 | |
| 672 | } |
| 673 | |
| 674 | /* This is a reimplementation of the C library's chdir function, |
| 675 | but one that produces Win32 errors instead of DOS error codes. |
| 676 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 677 | it also needs to set "magic" environment variables indicating |
| 678 | the per-drive current directory, which are of the form =<drive>: */ |
| 679 | BOOL __stdcall |
| 680 | win32_chdir(LPCSTR path) |
| 681 | { |
| 682 | char new_path[MAX_PATH+1]; |
| 683 | int result; |
| 684 | char env[4] = "=x:"; |
| 685 | |
| 686 | if(!SetCurrentDirectoryA(path)) |
| 687 | return FALSE; |
| 688 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 689 | if (!result) |
| 690 | return FALSE; |
| 691 | /* In the ANSI API, there should not be any paths longer |
| 692 | than MAX_PATH. */ |
| 693 | assert(result <= MAX_PATH+1); |
| 694 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 695 | strncmp(new_path, "//", 2) == 0) |
| 696 | /* UNC path, nothing to do. */ |
| 697 | return TRUE; |
| 698 | env[1] = new_path[0]; |
| 699 | return SetEnvironmentVariableA(env, new_path); |
| 700 | } |
| 701 | |
| 702 | /* The Unicode version differs from the ANSI version |
| 703 | since the current directory might exceed MAX_PATH characters */ |
| 704 | BOOL __stdcall |
| 705 | win32_wchdir(LPCWSTR path) |
| 706 | { |
| 707 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 708 | int result; |
| 709 | wchar_t env[4] = L"=x:"; |
| 710 | |
| 711 | if(!SetCurrentDirectoryW(path)) |
| 712 | return FALSE; |
| 713 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 714 | if (!result) |
| 715 | return FALSE; |
| 716 | if (result > MAX_PATH+1) { |
| 717 | new_path = malloc(result); |
| 718 | if (!new_path) { |
| 719 | SetLastError(ERROR_OUTOFMEMORY); |
| 720 | return FALSE; |
| 721 | } |
| 722 | } |
| 723 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 724 | wcsncmp(new_path, L"//", 2) == 0) |
| 725 | /* UNC path, nothing to do. */ |
| 726 | return TRUE; |
| 727 | env[1] = new_path[0]; |
| 728 | result = SetEnvironmentVariableW(env, new_path); |
| 729 | if (new_path != _new_path) |
| 730 | free(new_path); |
| 731 | return result; |
| 732 | } |
| 733 | #endif |
| 734 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 735 | #ifdef MS_WINDOWS |
| 736 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 737 | - time stamps are restricted to second resolution |
| 738 | - file modification times suffer from forth-and-back conversions between |
| 739 | UTC and local time |
| 740 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 741 | */ |
| 742 | #define HAVE_STAT_NSEC 1 |
| 743 | |
| 744 | struct win32_stat{ |
| 745 | int st_dev; |
| 746 | __int64 st_ino; |
| 747 | unsigned short st_mode; |
| 748 | int st_nlink; |
| 749 | int st_uid; |
| 750 | int st_gid; |
| 751 | int st_rdev; |
| 752 | __int64 st_size; |
| 753 | int st_atime; |
| 754 | int st_atime_nsec; |
| 755 | int st_mtime; |
| 756 | int st_mtime_nsec; |
| 757 | int st_ctime; |
| 758 | int st_ctime_nsec; |
| 759 | }; |
| 760 | |
| 761 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 762 | |
| 763 | static void |
| 764 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 765 | { |
| 766 | /* XXX endianness */ |
| 767 | __int64 in = *(__int64*)in_ptr; |
| 768 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 769 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 770 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 771 | } |
| 772 | |
| 773 | /* Below, we *know* that ugo+r is 0444 */ |
| 774 | #if _S_IREAD != 0400 |
| 775 | #error Unsupported C library |
| 776 | #endif |
| 777 | static int |
| 778 | attributes_to_mode(DWORD attr) |
| 779 | { |
| 780 | int m = 0; |
| 781 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 782 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 783 | else |
| 784 | m |= _S_IFREG; |
| 785 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 786 | m |= 0444; |
| 787 | else |
| 788 | m |= 0666; |
| 789 | return m; |
| 790 | } |
| 791 | |
| 792 | static int |
| 793 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 794 | { |
| 795 | memset(result, 0, sizeof(*result)); |
| 796 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 797 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 798 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 799 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 800 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | static int |
| 806 | win32_stat(const char* path, struct win32_stat *result) |
| 807 | { |
| 808 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 809 | int code; |
| 810 | char *dot; |
| 811 | /* XXX not supported on Win95 and NT 3.x */ |
| 812 | if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
| 813 | /* Protocol violation: we explicitly clear errno, instead of |
| 814 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 815 | errno = 0; |
| 816 | return -1; |
| 817 | } |
| 818 | code = attribute_data_to_stat(&info, result); |
| 819 | if (code != 0) |
| 820 | return code; |
| 821 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 822 | dot = strrchr(path, '.'); |
| 823 | if (dot) { |
| 824 | if (stricmp(dot, ".bat") == 0 || |
| 825 | stricmp(dot, ".cmd") == 0 || |
| 826 | stricmp(dot, ".exe") == 0 || |
| 827 | stricmp(dot, ".com") == 0) |
| 828 | result->st_mode |= 0111; |
| 829 | } |
| 830 | return code; |
| 831 | } |
| 832 | |
| 833 | static int |
| 834 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 835 | { |
| 836 | int code; |
| 837 | const wchar_t *dot; |
| 838 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 839 | /* XXX not supported on Win95 and NT 3.x */ |
| 840 | if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
| 841 | /* Protocol violation: we explicitly clear errno, instead of |
| 842 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 843 | errno = 0; |
| 844 | return -1; |
| 845 | } |
| 846 | code = attribute_data_to_stat(&info, result); |
| 847 | if (code < 0) |
| 848 | return code; |
| 849 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 850 | dot = wcsrchr(path, '.'); |
| 851 | if (dot) { |
| 852 | if (_wcsicmp(dot, L".bat") == 0 || |
| 853 | _wcsicmp(dot, L".cmd") == 0 || |
| 854 | _wcsicmp(dot, L".exe") == 0 || |
| 855 | _wcsicmp(dot, L".com") == 0) |
| 856 | result->st_mode |= 0111; |
| 857 | } |
| 858 | return code; |
| 859 | } |
| 860 | |
| 861 | static int |
| 862 | win32_fstat(int file_number, struct win32_stat *result) |
| 863 | { |
| 864 | BY_HANDLE_FILE_INFORMATION info; |
| 865 | HANDLE h; |
| 866 | int type; |
| 867 | |
| 868 | h = (HANDLE)_get_osfhandle(file_number); |
| 869 | |
| 870 | /* Protocol violation: we explicitly clear errno, instead of |
| 871 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 872 | errno = 0; |
| 873 | |
| 874 | if (h == INVALID_HANDLE_VALUE) { |
| 875 | /* This is really a C library error (invalid file handle). |
| 876 | We set the Win32 error to the closes one matching. */ |
| 877 | SetLastError(ERROR_INVALID_HANDLE); |
| 878 | return -1; |
| 879 | } |
| 880 | memset(result, 0, sizeof(*result)); |
| 881 | |
| 882 | type = GetFileType(h); |
| 883 | if (type == FILE_TYPE_UNKNOWN) { |
| 884 | DWORD error = GetLastError(); |
| 885 | if (error != 0) { |
| 886 | return -1; |
| 887 | } |
| 888 | /* else: valid but unknown file */ |
| 889 | } |
| 890 | |
| 891 | if (type != FILE_TYPE_DISK) { |
| 892 | if (type == FILE_TYPE_CHAR) |
| 893 | result->st_mode = _S_IFCHR; |
| 894 | else if (type == FILE_TYPE_PIPE) |
| 895 | result->st_mode = _S_IFIFO; |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | if (!GetFileInformationByHandle(h, &info)) { |
| 900 | return -1; |
| 901 | } |
| 902 | |
| 903 | /* similar to stat() */ |
| 904 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 905 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 906 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 907 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 908 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 909 | /* specific to fstat() */ |
| 910 | result->st_nlink = info.nNumberOfLinks; |
| 911 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 912 | return 0; |
| 913 | } |
| 914 | |
| 915 | #endif /* MS_WINDOWS */ |
| 916 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 917 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 918 | "stat_result: Result from stat or lstat.\n\n\ |
| 919 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 920 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 921 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 922 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 923 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 924 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 925 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 926 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 927 | |
| 928 | static PyStructSequence_Field stat_result_fields[] = { |
| 929 | {"st_mode", "protection bits"}, |
| 930 | {"st_ino", "inode"}, |
| 931 | {"st_dev", "device"}, |
| 932 | {"st_nlink", "number of hard links"}, |
| 933 | {"st_uid", "user ID of owner"}, |
| 934 | {"st_gid", "group ID of owner"}, |
| 935 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 936 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 937 | {NULL, "integer time of last access"}, |
| 938 | {NULL, "integer time of last modification"}, |
| 939 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 940 | {"st_atime", "time of last access"}, |
| 941 | {"st_mtime", "time of last modification"}, |
| 942 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 943 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 944 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 945 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 946 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 947 | {"st_blocks", "number of blocks allocated"}, |
| 948 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 949 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 950 | {"st_rdev", "device type (if inode device)"}, |
| 951 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 952 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 953 | {"st_flags", "user defined flags for file"}, |
| 954 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 955 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 956 | {"st_gen", "generation number"}, |
| 957 | #endif |
| 958 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 959 | {"st_birthtime", "time of creation"}, |
| 960 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 961 | {0} |
| 962 | }; |
| 963 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 964 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 965 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 966 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 967 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 968 | #endif |
| 969 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 970 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 971 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 972 | #else |
| 973 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 974 | #endif |
| 975 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 976 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 977 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 978 | #else |
| 979 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 980 | #endif |
| 981 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 982 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 983 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 984 | #else |
| 985 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 986 | #endif |
| 987 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 988 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 989 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 990 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 991 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 992 | #endif |
| 993 | |
| 994 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 995 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 996 | #else |
| 997 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 998 | #endif |
| 999 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1000 | static PyStructSequence_Desc stat_result_desc = { |
| 1001 | "stat_result", /* name */ |
| 1002 | stat_result__doc__, /* doc */ |
| 1003 | stat_result_fields, |
| 1004 | 10 |
| 1005 | }; |
| 1006 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1007 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1008 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1009 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1010 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1011 | 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] | 1012 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1013 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1014 | |
| 1015 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 1016 | {"f_bsize", }, |
| 1017 | {"f_frsize", }, |
| 1018 | {"f_blocks", }, |
| 1019 | {"f_bfree", }, |
| 1020 | {"f_bavail", }, |
| 1021 | {"f_files", }, |
| 1022 | {"f_ffree", }, |
| 1023 | {"f_favail", }, |
| 1024 | {"f_flag", }, |
| 1025 | {"f_namemax",}, |
| 1026 | {0} |
| 1027 | }; |
| 1028 | |
| 1029 | static PyStructSequence_Desc statvfs_result_desc = { |
| 1030 | "statvfs_result", /* name */ |
| 1031 | statvfs_result__doc__, /* doc */ |
| 1032 | statvfs_result_fields, |
| 1033 | 10 |
| 1034 | }; |
| 1035 | |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 1036 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1037 | static PyTypeObject StatResultType; |
| 1038 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1039 | static newfunc structseq_new; |
| 1040 | |
| 1041 | static PyObject * |
| 1042 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1043 | { |
| 1044 | PyStructSequence *result; |
| 1045 | int i; |
| 1046 | |
| 1047 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1048 | if (!result) |
| 1049 | return NULL; |
| 1050 | /* If we have been initialized from a tuple, |
| 1051 | st_?time might be set to None. Initialize it |
| 1052 | from the int slots. */ |
| 1053 | for (i = 7; i <= 9; i++) { |
| 1054 | if (result->ob_item[i+3] == Py_None) { |
| 1055 | Py_DECREF(Py_None); |
| 1056 | Py_INCREF(result->ob_item[i]); |
| 1057 | result->ob_item[i+3] = result->ob_item[i]; |
| 1058 | } |
| 1059 | } |
| 1060 | return (PyObject*)result; |
| 1061 | } |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1066 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1067 | |
| 1068 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1069 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1070 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1071 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1072 | future calls return ints. \n\ |
| 1073 | If newval is omitted, return the current setting.\n"); |
| 1074 | |
| 1075 | static PyObject* |
| 1076 | stat_float_times(PyObject* self, PyObject *args) |
| 1077 | { |
| 1078 | int newval = -1; |
| 1079 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1080 | return NULL; |
| 1081 | if (newval == -1) |
| 1082 | /* Return old value */ |
| 1083 | return PyBool_FromLong(_stat_float_times); |
| 1084 | _stat_float_times = newval; |
| 1085 | Py_INCREF(Py_None); |
| 1086 | return Py_None; |
| 1087 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1088 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1089 | static void |
| 1090 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1091 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1092 | PyObject *fval,*ival; |
| 1093 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1094 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1095 | #else |
| 1096 | ival = PyInt_FromLong((long)sec); |
| 1097 | #endif |
| 1098 | if (_stat_float_times) { |
| 1099 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1100 | } else { |
| 1101 | fval = ival; |
| 1102 | Py_INCREF(fval); |
| 1103 | } |
| 1104 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1105 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1108 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1109 | (used by posix_stat() and posix_fstat()) */ |
| 1110 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1111 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1112 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1113 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1114 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1115 | if (v == NULL) |
| 1116 | return NULL; |
| 1117 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1118 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1119 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1120 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1121 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1122 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1123 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1124 | #endif |
| 1125 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1126 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1127 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1128 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1129 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1130 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1131 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); |
| 1132 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); |
| 1133 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1134 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1135 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1136 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1137 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1138 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1139 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1140 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1141 | #if defined(HAVE_STAT_TV_NSEC) |
| 1142 | ansec = st->st_atim.tv_nsec; |
| 1143 | mnsec = st->st_mtim.tv_nsec; |
| 1144 | cnsec = st->st_ctim.tv_nsec; |
| 1145 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1146 | ansec = st->st_atimespec.tv_nsec; |
| 1147 | mnsec = st->st_mtimespec.tv_nsec; |
| 1148 | cnsec = st->st_ctimespec.tv_nsec; |
| 1149 | #elif defined(HAVE_STAT_NSEC) |
| 1150 | ansec = st->st_atime_nsec; |
| 1151 | mnsec = st->st_mtime_nsec; |
| 1152 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1153 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1154 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1155 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1156 | fill_time(v, 7, st->st_atime, ansec); |
| 1157 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1158 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1159 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1160 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1161 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1162 | PyInt_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1163 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1164 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1165 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1166 | PyInt_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1167 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1168 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1169 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1170 | PyInt_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1171 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1172 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1173 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1174 | PyInt_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1175 | #endif |
| 1176 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1177 | { |
| 1178 | PyObject *val; |
| 1179 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1180 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1181 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1182 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1183 | #else |
| 1184 | bnsec = 0; |
| 1185 | #endif |
| 1186 | if (_stat_float_times) { |
| 1187 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1188 | } else { |
| 1189 | val = PyInt_FromLong((long)bsec); |
| 1190 | } |
| 1191 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1192 | val); |
| 1193 | } |
| 1194 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1195 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1196 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1197 | PyInt_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1198 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1199 | |
| 1200 | if (PyErr_Occurred()) { |
| 1201 | Py_DECREF(v); |
| 1202 | return NULL; |
| 1203 | } |
| 1204 | |
| 1205 | return v; |
| 1206 | } |
| 1207 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1208 | #ifdef MS_WINDOWS |
| 1209 | |
| 1210 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1211 | where / can be used in place of \ and the trailing slash is optional. |
| 1212 | Both SERVER and SHARE must have at least one character. |
| 1213 | */ |
| 1214 | |
| 1215 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1216 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
| 1217 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
| 1218 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1219 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1220 | IsUNCRootA(char *path, int pathlen) |
| 1221 | { |
| 1222 | #define ISSLASH ISSLASHA |
| 1223 | |
| 1224 | int i, share; |
| 1225 | |
| 1226 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1227 | /* minimum UNCRoot is \\x\y */ |
| 1228 | return FALSE; |
| 1229 | for (i = 2; i < pathlen ; i++) |
| 1230 | if (ISSLASH(path[i])) break; |
| 1231 | if (i == 2 || i == pathlen) |
| 1232 | /* do not allow \\\SHARE or \\SERVER */ |
| 1233 | return FALSE; |
| 1234 | share = i+1; |
| 1235 | for (i = share; i < pathlen; i++) |
| 1236 | if (ISSLASH(path[i])) break; |
| 1237 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1238 | |
| 1239 | #undef ISSLASH |
| 1240 | } |
| 1241 | |
| 1242 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1243 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1244 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1245 | { |
| 1246 | #define ISSLASH ISSLASHW |
| 1247 | |
| 1248 | int i, share; |
| 1249 | |
| 1250 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1251 | /* minimum UNCRoot is \\x\y */ |
| 1252 | return FALSE; |
| 1253 | for (i = 2; i < pathlen ; i++) |
| 1254 | if (ISSLASH(path[i])) break; |
| 1255 | if (i == 2 || i == pathlen) |
| 1256 | /* do not allow \\\SHARE or \\SERVER */ |
| 1257 | return FALSE; |
| 1258 | share = i+1; |
| 1259 | for (i = share; i < pathlen; i++) |
| 1260 | if (ISSLASH(path[i])) break; |
| 1261 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1262 | |
| 1263 | #undef ISSLASH |
| 1264 | } |
| 1265 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1266 | #endif /* MS_WINDOWS */ |
| 1267 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1268 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1269 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1270 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1271 | #ifdef __VMS |
| 1272 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1273 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1274 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1275 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1276 | char *wformat, |
| 1277 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1278 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1279 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1280 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1281 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1282 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1283 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1284 | |
| 1285 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1286 | /* If on wide-character-capable OS see if argument |
| 1287 | is Unicode and if so use wide API. */ |
| 1288 | if (unicode_file_names()) { |
| 1289 | PyUnicodeObject *po; |
| 1290 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1291 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1292 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1293 | Py_BEGIN_ALLOW_THREADS |
| 1294 | /* PyUnicode_AS_UNICODE result OK without |
| 1295 | thread lock as it is a simple dereference. */ |
| 1296 | res = wstatfunc(wpath, &st); |
| 1297 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1298 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1299 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1300 | return win32_error_unicode("stat", wpath); |
| 1301 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1302 | } |
| 1303 | /* Drop the argument parsing error as narrow strings |
| 1304 | are also valid. */ |
| 1305 | PyErr_Clear(); |
| 1306 | } |
| 1307 | #endif |
| 1308 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1309 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1310 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1311 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1312 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1313 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1314 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1315 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1316 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1317 | |
| 1318 | if (res != 0) { |
| 1319 | #ifdef MS_WINDOWS |
| 1320 | result = win32_error("stat", pathfree); |
| 1321 | #else |
| 1322 | result = posix_error_with_filename(pathfree); |
| 1323 | #endif |
| 1324 | } |
| 1325 | else |
| 1326 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1327 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1328 | PyMem_Free(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1329 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1332 | /* POSIX methods */ |
| 1333 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1334 | PyDoc_STRVAR(posix_access__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1335 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1336 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1337 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1338 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1339 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1340 | 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] | 1341 | |
| 1342 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1343 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1344 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1345 | char *path; |
| 1346 | int mode; |
| 1347 | int res; |
| 1348 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1349 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1350 | if (unicode_file_names()) { |
| 1351 | PyUnicodeObject *po; |
| 1352 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1353 | Py_BEGIN_ALLOW_THREADS |
| 1354 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1355 | it is a simple dereference. */ |
| 1356 | res = _waccess(PyUnicode_AS_UNICODE(po), mode); |
| 1357 | Py_END_ALLOW_THREADS |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1358 | return PyBool_FromLong(res == 0); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1359 | } |
| 1360 | /* Drop the argument parsing error as narrow strings |
| 1361 | are also valid. */ |
| 1362 | PyErr_Clear(); |
| 1363 | } |
| 1364 | #endif |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame] | 1365 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1366 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1367 | return NULL; |
| 1368 | Py_BEGIN_ALLOW_THREADS |
| 1369 | res = access(path, mode); |
| 1370 | Py_END_ALLOW_THREADS |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 1371 | PyMem_Free(path); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1372 | return PyBool_FromLong(res == 0); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1375 | #ifndef F_OK |
| 1376 | #define F_OK 0 |
| 1377 | #endif |
| 1378 | #ifndef R_OK |
| 1379 | #define R_OK 4 |
| 1380 | #endif |
| 1381 | #ifndef W_OK |
| 1382 | #define W_OK 2 |
| 1383 | #endif |
| 1384 | #ifndef X_OK |
| 1385 | #define X_OK 1 |
| 1386 | #endif |
| 1387 | |
| 1388 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1389 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1390 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1391 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1392 | |
| 1393 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1394 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1395 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1396 | int id; |
| 1397 | char *ret; |
| 1398 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1399 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1400 | return NULL; |
| 1401 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1402 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1403 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1404 | if (id == 0) { |
| 1405 | ret = ttyname(); |
| 1406 | } |
| 1407 | else { |
| 1408 | ret = NULL; |
| 1409 | } |
| 1410 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1411 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1412 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1413 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1414 | return posix_error(); |
| 1415 | return PyString_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1416 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1417 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1418 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1419 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1420 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1421 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1422 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1423 | |
| 1424 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1425 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1426 | { |
| 1427 | char *ret; |
| 1428 | char buffer[L_ctermid]; |
| 1429 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1430 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1431 | ret = ctermid_r(buffer); |
| 1432 | #else |
| 1433 | ret = ctermid(buffer); |
| 1434 | #endif |
| 1435 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1436 | return posix_error(); |
| 1437 | return PyString_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1438 | } |
| 1439 | #endif |
| 1440 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1441 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1442 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1443 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1444 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1445 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1446 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1447 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1448 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1449 | 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] | 1450 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1451 | return posix_1str(args, "et:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1452 | #elif defined(__VMS) |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1453 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1454 | #else |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1455 | return posix_1str(args, "et:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1456 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1459 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1460 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1461 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1462 | 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] | 1463 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1464 | |
| 1465 | static PyObject * |
| 1466 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1467 | { |
| 1468 | return posix_fildes(fdobj, fchdir); |
| 1469 | } |
| 1470 | #endif /* HAVE_FCHDIR */ |
| 1471 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1472 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1473 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1474 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1475 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1476 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1477 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1478 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1479 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1480 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1481 | int i; |
| 1482 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1483 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1484 | if (unicode_file_names()) { |
| 1485 | PyUnicodeObject *po; |
| 1486 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1487 | Py_BEGIN_ALLOW_THREADS |
| 1488 | res = _wchmod(PyUnicode_AS_UNICODE(po), i); |
| 1489 | Py_END_ALLOW_THREADS |
| 1490 | if (res < 0) |
| 1491 | return posix_error_with_unicode_filename( |
| 1492 | PyUnicode_AS_UNICODE(po)); |
| 1493 | Py_INCREF(Py_None); |
| 1494 | return Py_None; |
| 1495 | } |
| 1496 | /* Drop the argument parsing error as narrow strings |
| 1497 | are also valid. */ |
| 1498 | PyErr_Clear(); |
| 1499 | } |
| 1500 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1501 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1502 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1503 | return NULL; |
| 1504 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1505 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1506 | Py_END_ALLOW_THREADS |
| 1507 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1508 | return posix_error_with_allocated_filename(path); |
| 1509 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1510 | Py_INCREF(Py_None); |
| 1511 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1514 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1515 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1516 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1517 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1518 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1519 | |
| 1520 | static PyObject * |
| 1521 | posix_chroot(PyObject *self, PyObject *args) |
| 1522 | { |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1523 | return posix_1str(args, "et:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1524 | } |
| 1525 | #endif |
| 1526 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1527 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1528 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1529 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1530 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1531 | |
| 1532 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1533 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1534 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1535 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1536 | } |
| 1537 | #endif /* HAVE_FSYNC */ |
| 1538 | |
| 1539 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1540 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1541 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1542 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1543 | #endif |
| 1544 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1545 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1546 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1547 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1548 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1549 | |
| 1550 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1551 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1552 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1553 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1554 | } |
| 1555 | #endif /* HAVE_FDATASYNC */ |
| 1556 | |
| 1557 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1558 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1559 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1560 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1561 | 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] | 1562 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1563 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1564 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1565 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1566 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1567 | int uid, gid; |
| 1568 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1569 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1570 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1571 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1572 | return NULL; |
| 1573 | Py_BEGIN_ALLOW_THREADS |
| 1574 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1575 | Py_END_ALLOW_THREADS |
| 1576 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1577 | return posix_error_with_allocated_filename(path); |
| 1578 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1579 | Py_INCREF(Py_None); |
| 1580 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1581 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1582 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1583 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1584 | #ifdef HAVE_LCHOWN |
| 1585 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1586 | "lchown(path, uid, gid)\n\n\ |
| 1587 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1588 | This function will not follow symbolic links."); |
| 1589 | |
| 1590 | static PyObject * |
| 1591 | posix_lchown(PyObject *self, PyObject *args) |
| 1592 | { |
| 1593 | char *path = NULL; |
| 1594 | int uid, gid; |
| 1595 | int res; |
| 1596 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1597 | Py_FileSystemDefaultEncoding, &path, |
| 1598 | &uid, &gid)) |
| 1599 | return NULL; |
| 1600 | Py_BEGIN_ALLOW_THREADS |
| 1601 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1602 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1603 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1604 | return posix_error_with_allocated_filename(path); |
| 1605 | PyMem_Free(path); |
| 1606 | Py_INCREF(Py_None); |
| 1607 | return Py_None; |
| 1608 | } |
| 1609 | #endif /* HAVE_LCHOWN */ |
| 1610 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1611 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1612 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1613 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1614 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1615 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1616 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1617 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1618 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1619 | { |
| 1620 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1621 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1622 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1623 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1624 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1625 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1626 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1627 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1628 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1629 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1630 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1631 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1632 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1633 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1634 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1635 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1636 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1637 | "getcwdu() -> path\n\n\ |
| 1638 | Return a unicode string representing the current working directory."); |
| 1639 | |
| 1640 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1641 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1642 | { |
| 1643 | char buf[1026]; |
| 1644 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1645 | |
| 1646 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1647 | if (unicode_file_names()) { |
| 1648 | wchar_t *wres; |
| 1649 | wchar_t wbuf[1026]; |
| 1650 | Py_BEGIN_ALLOW_THREADS |
| 1651 | wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]); |
| 1652 | Py_END_ALLOW_THREADS |
| 1653 | if (wres == NULL) |
| 1654 | return posix_error(); |
| 1655 | return PyUnicode_FromWideChar(wbuf, wcslen(wbuf)); |
| 1656 | } |
| 1657 | #endif |
| 1658 | |
| 1659 | Py_BEGIN_ALLOW_THREADS |
| 1660 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1661 | res = _getcwd2(buf, sizeof buf); |
| 1662 | #else |
| 1663 | res = getcwd(buf, sizeof buf); |
| 1664 | #endif |
| 1665 | Py_END_ALLOW_THREADS |
| 1666 | if (res == NULL) |
| 1667 | return posix_error(); |
| 1668 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1669 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1670 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1671 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1672 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1673 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1674 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1675 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1676 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1677 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1678 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1679 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1680 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1681 | { |
Martin v. Löwis | 4fc2bda | 2006-05-04 12:04:27 +0000 | [diff] [blame^] | 1682 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1683 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1684 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1685 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1686 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1687 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1688 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1689 | Return a list containing the names of the entries in the directory.\n\ |
| 1690 | \n\ |
| 1691 | path: path of directory to list\n\ |
| 1692 | \n\ |
| 1693 | 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] | 1694 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1695 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1696 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1697 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1698 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1699 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1700 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1701 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1702 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1703 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1704 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1705 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1706 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1707 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 1708 | char namebuf[MAX_PATH*2+5]; |
| 1709 | char *bufptr = namebuf; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1710 | Py_ssize_t len = sizeof(namebuf)/sizeof(namebuf[0]); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1711 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1712 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1713 | /* If on wide-character-capable OS see if argument |
| 1714 | is Unicode and if so use wide API. */ |
| 1715 | if (unicode_file_names()) { |
| 1716 | PyUnicodeObject *po; |
| 1717 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1718 | WIN32_FIND_DATAW wFileData; |
| 1719 | Py_UNICODE wnamebuf[MAX_PATH*2+5]; |
| 1720 | Py_UNICODE wch; |
| 1721 | wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH); |
| 1722 | wnamebuf[MAX_PATH] = L'\0'; |
| 1723 | len = wcslen(wnamebuf); |
| 1724 | wch = (len > 0) ? wnamebuf[len-1] : L'\0'; |
| 1725 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1726 | wnamebuf[len++] = L'/'; |
| 1727 | wcscpy(wnamebuf + len, L"*.*"); |
| 1728 | if ((d = PyList_New(0)) == NULL) |
| 1729 | return NULL; |
| 1730 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1731 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1732 | errno = GetLastError(); |
| 1733 | if (errno == ERROR_FILE_NOT_FOUND) { |
| 1734 | return d; |
| 1735 | } |
| 1736 | Py_DECREF(d); |
| 1737 | return win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1738 | } |
| 1739 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1740 | /* Skip over . and .. */ |
| 1741 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 1742 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 1743 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1744 | if (v == NULL) { |
| 1745 | Py_DECREF(d); |
| 1746 | d = NULL; |
| 1747 | break; |
| 1748 | } |
| 1749 | if (PyList_Append(d, v) != 0) { |
| 1750 | Py_DECREF(v); |
| 1751 | Py_DECREF(d); |
| 1752 | d = NULL; |
| 1753 | break; |
| 1754 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1755 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1756 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1757 | Py_BEGIN_ALLOW_THREADS |
| 1758 | result = FindNextFileW(hFindFile, &wFileData); |
| 1759 | Py_END_ALLOW_THREADS |
| 1760 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1761 | |
| 1762 | if (FindClose(hFindFile) == FALSE) { |
| 1763 | Py_DECREF(d); |
| 1764 | return win32_error_unicode("FindClose", wnamebuf); |
| 1765 | } |
| 1766 | return d; |
| 1767 | } |
| 1768 | /* Drop the argument parsing error as narrow strings |
| 1769 | are also valid. */ |
| 1770 | PyErr_Clear(); |
| 1771 | } |
| 1772 | #endif |
| 1773 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1774 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1775 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1776 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1777 | if (len > 0) { |
| 1778 | char ch = namebuf[len-1]; |
| 1779 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 1780 | namebuf[len++] = '/'; |
| 1781 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1782 | strcpy(namebuf + len, "*.*"); |
| 1783 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1784 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1785 | return NULL; |
| 1786 | |
| 1787 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 1788 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1789 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 1790 | if (errno == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1791 | return d; |
| 1792 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1793 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1794 | } |
| 1795 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1796 | /* Skip over . and .. */ |
| 1797 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 1798 | strcmp(FileData.cFileName, "..") != 0) { |
| 1799 | v = PyString_FromString(FileData.cFileName); |
| 1800 | if (v == NULL) { |
| 1801 | Py_DECREF(d); |
| 1802 | d = NULL; |
| 1803 | break; |
| 1804 | } |
| 1805 | if (PyList_Append(d, v) != 0) { |
| 1806 | Py_DECREF(v); |
| 1807 | Py_DECREF(d); |
| 1808 | d = NULL; |
| 1809 | break; |
| 1810 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1811 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1812 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1813 | Py_BEGIN_ALLOW_THREADS |
| 1814 | result = FindNextFile(hFindFile, &FileData); |
| 1815 | Py_END_ALLOW_THREADS |
| 1816 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1817 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1818 | if (FindClose(hFindFile) == FALSE) { |
| 1819 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1820 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1821 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1822 | |
| 1823 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1824 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1825 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1826 | |
| 1827 | #ifndef MAX_PATH |
| 1828 | #define MAX_PATH CCHMAXPATH |
| 1829 | #endif |
| 1830 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 1831 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1832 | PyObject *d, *v; |
| 1833 | char namebuf[MAX_PATH+5]; |
| 1834 | HDIR hdir = 1; |
| 1835 | ULONG srchcnt = 1; |
| 1836 | FILEFINDBUF3 ep; |
| 1837 | APIRET rc; |
| 1838 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1839 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1840 | return NULL; |
| 1841 | if (len >= MAX_PATH) { |
| 1842 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1843 | return NULL; |
| 1844 | } |
| 1845 | strcpy(namebuf, name); |
| 1846 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1847 | if (*pt == ALTSEP) |
| 1848 | *pt = SEP; |
| 1849 | if (namebuf[len-1] != SEP) |
| 1850 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1851 | strcpy(namebuf + len, "*.*"); |
| 1852 | |
| 1853 | if ((d = PyList_New(0)) == NULL) |
| 1854 | return NULL; |
| 1855 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1856 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1857 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1858 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1859 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1860 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1861 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1862 | |
| 1863 | if (rc != NO_ERROR) { |
| 1864 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1865 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1868 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1869 | do { |
| 1870 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1871 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1872 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1873 | |
| 1874 | strcpy(namebuf, ep.achName); |
| 1875 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1876 | /* Leave Case of Name Alone -- In Native Form */ |
| 1877 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1878 | |
| 1879 | v = PyString_FromString(namebuf); |
| 1880 | if (v == NULL) { |
| 1881 | Py_DECREF(d); |
| 1882 | d = NULL; |
| 1883 | break; |
| 1884 | } |
| 1885 | if (PyList_Append(d, v) != 0) { |
| 1886 | Py_DECREF(v); |
| 1887 | Py_DECREF(d); |
| 1888 | d = NULL; |
| 1889 | break; |
| 1890 | } |
| 1891 | Py_DECREF(v); |
| 1892 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1893 | } |
| 1894 | |
| 1895 | return d; |
| 1896 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1897 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1898 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1899 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1900 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1901 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1902 | int arg_is_unicode = 1; |
| 1903 | |
Georg Brandl | 05e89b8 | 2006-04-11 07:04:06 +0000 | [diff] [blame] | 1904 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1905 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 1906 | arg_is_unicode = 0; |
| 1907 | PyErr_Clear(); |
| 1908 | } |
| 1909 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1910 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1911 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1912 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1913 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1914 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1915 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1916 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1917 | return NULL; |
| 1918 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1919 | for (;;) { |
| 1920 | Py_BEGIN_ALLOW_THREADS |
| 1921 | ep = readdir(dirp); |
| 1922 | Py_END_ALLOW_THREADS |
| 1923 | if (ep == NULL) |
| 1924 | break; |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1925 | if (ep->d_name[0] == '.' && |
| 1926 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1927 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1928 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1929 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1930 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1931 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1932 | d = NULL; |
| 1933 | break; |
| 1934 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1935 | #ifdef Py_USING_UNICODE |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1936 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1937 | PyObject *w; |
| 1938 | |
| 1939 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1940 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1941 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 1942 | if (w != NULL) { |
| 1943 | Py_DECREF(v); |
| 1944 | v = w; |
| 1945 | } |
| 1946 | else { |
| 1947 | /* fall back to the original byte string, as |
| 1948 | discussed in patch #683592 */ |
| 1949 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1950 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1951 | } |
| 1952 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1953 | if (PyList_Append(d, v) != 0) { |
| 1954 | Py_DECREF(v); |
| 1955 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1956 | d = NULL; |
| 1957 | break; |
| 1958 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1959 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1960 | } |
Georg Brandl | bbfe4fa | 2006-04-11 06:47:43 +0000 | [diff] [blame] | 1961 | if (errno != 0 && d != NULL) { |
| 1962 | /* readdir() returned NULL and set errno */ |
| 1963 | closedir(dirp); |
| 1964 | Py_DECREF(d); |
| 1965 | return posix_error_with_allocated_filename(name); |
| 1966 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1967 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1968 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1969 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1970 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1971 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1972 | #endif /* which OS */ |
| 1973 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1974 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1975 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1976 | /* A helper function for abspath on win32 */ |
| 1977 | static PyObject * |
| 1978 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1979 | { |
| 1980 | /* assume encoded strings wont more than double no of chars */ |
| 1981 | char inbuf[MAX_PATH*2]; |
| 1982 | char *inbufp = inbuf; |
Tim Peters | 67d70eb | 2006-03-01 04:35:45 +0000 | [diff] [blame] | 1983 | Py_ssize_t insize = sizeof(inbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1984 | char outbuf[MAX_PATH*2]; |
| 1985 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1986 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1987 | if (unicode_file_names()) { |
| 1988 | PyUnicodeObject *po; |
| 1989 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 1990 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 1991 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1992 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1993 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 1994 | woutbuf, &wtemp)) |
| 1995 | return win32_error("GetFullPathName", ""); |
| 1996 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 1997 | } |
| 1998 | /* Drop the argument parsing error as narrow strings |
| 1999 | are also valid. */ |
| 2000 | PyErr_Clear(); |
| 2001 | } |
| 2002 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2003 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 2004 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2005 | &insize)) |
| 2006 | return NULL; |
| 2007 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2008 | outbuf, &temp)) |
| 2009 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2010 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2011 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2012 | Py_FileSystemDefaultEncoding, NULL); |
| 2013 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2014 | return PyString_FromString(outbuf); |
| 2015 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2016 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2017 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2018 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2019 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2020 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2021 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2022 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2023 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2024 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2025 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2026 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2027 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2028 | |
| 2029 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2030 | if (unicode_file_names()) { |
| 2031 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 2032 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2033 | Py_BEGIN_ALLOW_THREADS |
| 2034 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2035 | it is a simple dereference. */ |
| 2036 | res = _wmkdir(PyUnicode_AS_UNICODE(po)); |
| 2037 | Py_END_ALLOW_THREADS |
| 2038 | if (res < 0) |
| 2039 | return posix_error(); |
| 2040 | Py_INCREF(Py_None); |
| 2041 | return Py_None; |
| 2042 | } |
| 2043 | /* Drop the argument parsing error as narrow strings |
| 2044 | are also valid. */ |
| 2045 | PyErr_Clear(); |
| 2046 | } |
| 2047 | #endif |
| 2048 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2049 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2050 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2051 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2052 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2053 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2054 | res = mkdir(path); |
| 2055 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2056 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2057 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2058 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2059 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2060 | return posix_error_with_allocated_filename(path); |
| 2061 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2062 | Py_INCREF(Py_None); |
| 2063 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2066 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2067 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2068 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2069 | #include <sys/resource.h> |
| 2070 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2071 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2072 | |
| 2073 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2074 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2075 | "nice(inc) -> new_priority\n\n\ |
| 2076 | 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] | 2077 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2078 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2079 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2080 | { |
| 2081 | int increment, value; |
| 2082 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2083 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2084 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2085 | |
| 2086 | /* There are two flavours of 'nice': one that returns the new |
| 2087 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2088 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2089 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2090 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2091 | If we are of the nice family that returns the new priority, we |
| 2092 | need to clear errno before the call, and check if errno is filled |
| 2093 | before calling posix_error() on a returnvalue of -1, because the |
| 2094 | -1 may be the actual new priority! */ |
| 2095 | |
| 2096 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2097 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2098 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2099 | if (value == 0) |
| 2100 | value = getpriority(PRIO_PROCESS, 0); |
| 2101 | #endif |
| 2102 | if (value == -1 && errno != 0) |
| 2103 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2104 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2105 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2106 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2107 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2108 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2109 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2110 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2111 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2112 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2113 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2114 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2115 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2116 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2117 | PyObject *o1, *o2; |
| 2118 | char *p1, *p2; |
| 2119 | BOOL result; |
| 2120 | if (unicode_file_names()) { |
| 2121 | if (!PyArg_ParseTuple(args, "O&O&:rename", |
| 2122 | convert_to_unicode, &o1, |
| 2123 | convert_to_unicode, &o2)) |
| 2124 | PyErr_Clear(); |
| 2125 | else { |
| 2126 | Py_BEGIN_ALLOW_THREADS |
| 2127 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2128 | PyUnicode_AsUnicode(o2)); |
| 2129 | Py_END_ALLOW_THREADS |
| 2130 | Py_DECREF(o1); |
| 2131 | Py_DECREF(o2); |
| 2132 | if (!result) |
| 2133 | return win32_error("rename", NULL); |
| 2134 | Py_INCREF(Py_None); |
| 2135 | return Py_None; |
| 2136 | } |
| 2137 | } |
| 2138 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2139 | return NULL; |
| 2140 | Py_BEGIN_ALLOW_THREADS |
| 2141 | result = MoveFileA(p1, p2); |
| 2142 | Py_END_ALLOW_THREADS |
| 2143 | if (!result) |
| 2144 | return win32_error("rename", NULL); |
| 2145 | Py_INCREF(Py_None); |
| 2146 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2147 | #else |
Martin v. Löwis | 4fc2bda | 2006-05-04 12:04:27 +0000 | [diff] [blame^] | 2148 | return posix_2str(args, "etet:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2149 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2152 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2153 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2154 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2155 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2156 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2157 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2158 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2159 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2160 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2161 | return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2162 | #else |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2163 | return posix_1str(args, "et:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2164 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2167 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2168 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2169 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2170 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2171 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2172 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2173 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2174 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2175 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2176 | 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] | 2177 | #else |
| 2178 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 2179 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2182 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2183 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2184 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2185 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2186 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2187 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2188 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2189 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2190 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2191 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2192 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2193 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2194 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2195 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2196 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2197 | Py_END_ALLOW_THREADS |
| 2198 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2199 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2200 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2201 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2202 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2203 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2204 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2205 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2206 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2207 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2208 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2209 | { |
| 2210 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2211 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2212 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2213 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2214 | if (i < 0) |
| 2215 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2216 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2219 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2220 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2221 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2222 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2223 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2224 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2225 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2226 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2227 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2228 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2229 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2230 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2231 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2232 | return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2233 | #else |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2234 | return posix_1str(args, "et:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2235 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2238 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2239 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2240 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2241 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2242 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2243 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2244 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2245 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2246 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2247 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2248 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2249 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2250 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2251 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2252 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2253 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2254 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2255 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2256 | u.sysname, |
| 2257 | u.nodename, |
| 2258 | u.release, |
| 2259 | u.version, |
| 2260 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2261 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2262 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2263 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2264 | static int |
| 2265 | extract_time(PyObject *t, long* sec, long* usec) |
| 2266 | { |
| 2267 | long intval; |
| 2268 | if (PyFloat_Check(t)) { |
| 2269 | double tval = PyFloat_AsDouble(t); |
| 2270 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 2271 | if (!intobj) |
| 2272 | return -1; |
| 2273 | intval = PyInt_AsLong(intobj); |
| 2274 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2275 | if (intval == -1 && PyErr_Occurred()) |
| 2276 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2277 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2278 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2279 | if (*usec < 0) |
| 2280 | /* If rounding gave us a negative number, |
| 2281 | truncate. */ |
| 2282 | *usec = 0; |
| 2283 | return 0; |
| 2284 | } |
| 2285 | intval = PyInt_AsLong(t); |
| 2286 | if (intval == -1 && PyErr_Occurred()) |
| 2287 | return -1; |
| 2288 | *sec = intval; |
| 2289 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2290 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2291 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2292 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2293 | PyDoc_STRVAR(posix_utime__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2294 | "utime(path, (atime, utime))\n\ |
| 2295 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2296 | 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] | 2297 | 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] | 2298 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2299 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2300 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2301 | { |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2302 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2303 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2304 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2305 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2306 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2307 | #if defined(HAVE_UTIMES) |
| 2308 | struct timeval buf[2]; |
| 2309 | #define ATIME buf[0].tv_sec |
| 2310 | #define MTIME buf[1].tv_sec |
| 2311 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2312 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2313 | struct utimbuf buf; |
| 2314 | #define ATIME buf.actime |
| 2315 | #define MTIME buf.modtime |
| 2316 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2317 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2318 | time_t buf[2]; |
| 2319 | #define ATIME buf[0] |
| 2320 | #define MTIME buf[1] |
| 2321 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2322 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2323 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2324 | int have_unicode_filename = 0; |
| 2325 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2326 | PyUnicodeObject *obwpath; |
| 2327 | wchar_t *wpath; |
| 2328 | if (unicode_file_names()) { |
| 2329 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2330 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2331 | have_unicode_filename = 1; |
| 2332 | } else |
| 2333 | /* Drop the argument parsing error as narrow strings |
| 2334 | are also valid. */ |
| 2335 | PyErr_Clear(); |
| 2336 | } |
| 2337 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 2338 | |
| 2339 | if (!have_unicode_filename && \ |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2340 | !PyArg_ParseTuple(args, "etO:utime", |
| 2341 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2342 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2343 | if (arg == Py_None) { |
| 2344 | /* optional time values not given */ |
| 2345 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2346 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2347 | if (have_unicode_filename) |
| 2348 | res = _wutime(wpath, NULL); |
| 2349 | else |
| 2350 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2351 | res = utime(path, NULL); |
| 2352 | Py_END_ALLOW_THREADS |
| 2353 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2354 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2355 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2356 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2357 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2358 | return NULL; |
| 2359 | } |
| 2360 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2361 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2362 | &atime, &ausec) == -1) { |
| 2363 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2364 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2365 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2366 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2367 | &mtime, &musec) == -1) { |
| 2368 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2369 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2370 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2371 | ATIME = atime; |
| 2372 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2373 | #ifdef HAVE_UTIMES |
| 2374 | buf[0].tv_usec = ausec; |
| 2375 | buf[1].tv_usec = musec; |
| 2376 | Py_BEGIN_ALLOW_THREADS |
| 2377 | res = utimes(path, buf); |
| 2378 | Py_END_ALLOW_THREADS |
| 2379 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2380 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2381 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2382 | if (have_unicode_filename) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 2383 | /* utime is OK with utimbuf, but _wutime insists |
| 2384 | on _utimbuf (the msvc headers assert the |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2385 | underscore version is ansi) */ |
| 2386 | res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG); |
| 2387 | else |
| 2388 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2389 | res = utime(path, UTIME_ARG); |
| 2390 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2391 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2392 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2393 | if (res < 0) { |
| 2394 | #ifdef Py_WIN_WIDE_FILENAMES |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2395 | if (have_unicode_filename) |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2396 | return posix_error_with_unicode_filename(wpath); |
| 2397 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2398 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2399 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2400 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2401 | Py_INCREF(Py_None); |
| 2402 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2403 | #undef UTIME_ARG |
| 2404 | #undef ATIME |
| 2405 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2408 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2409 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2410 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2411 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2412 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2413 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2414 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2415 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2416 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2417 | { |
| 2418 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2419 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2420 | return NULL; |
| 2421 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2422 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2425 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2426 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2427 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2428 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2429 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2430 | for (i = 0; i < count; i++) |
| 2431 | PyMem_Free(array[i]); |
| 2432 | PyMem_DEL(array); |
| 2433 | } |
| 2434 | #endif |
| 2435 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2436 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2437 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2438 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2439 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2440 | Execute an executable path with arguments, replacing current process.\n\ |
| 2441 | \n\ |
| 2442 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2443 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2444 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2445 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2446 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2447 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2448 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2449 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2450 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2451 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2452 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2453 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2454 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2455 | argv is a list or tuple of strings. */ |
| 2456 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2457 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2458 | Py_FileSystemDefaultEncoding, |
| 2459 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2460 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2461 | if (PyList_Check(argv)) { |
| 2462 | argc = PyList_Size(argv); |
| 2463 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2464 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2465 | else if (PyTuple_Check(argv)) { |
| 2466 | argc = PyTuple_Size(argv); |
| 2467 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2468 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2469 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2470 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2471 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2472 | return NULL; |
| 2473 | } |
| 2474 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2475 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2476 | if (argvlist == NULL) { |
| 2477 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2478 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2479 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2480 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2481 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2482 | Py_FileSystemDefaultEncoding, |
| 2483 | &argvlist[i])) { |
| 2484 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2485 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2486 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2487 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2488 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2489 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2490 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2491 | } |
| 2492 | argvlist[argc] = NULL; |
| 2493 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2494 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2495 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2496 | /* If we get here it's definitely an error */ |
| 2497 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2498 | free_string_array(argvlist, argc); |
| 2499 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2500 | return posix_error(); |
| 2501 | } |
| 2502 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2503 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2504 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2505 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2506 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2507 | \n\ |
| 2508 | path: path of executable file\n\ |
| 2509 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2510 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2511 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2512 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2513 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2514 | { |
| 2515 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2516 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2517 | char **argvlist; |
| 2518 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2519 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2520 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2521 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 2522 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2523 | |
| 2524 | /* execve has three arguments: (path, argv, env), where |
| 2525 | argv is a list or tuple of strings and env is a dictionary |
| 2526 | like posix.environ. */ |
| 2527 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2528 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2529 | Py_FileSystemDefaultEncoding, |
| 2530 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2531 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2532 | if (PyList_Check(argv)) { |
| 2533 | argc = PyList_Size(argv); |
| 2534 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2535 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2536 | else if (PyTuple_Check(argv)) { |
| 2537 | argc = PyTuple_Size(argv); |
| 2538 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2539 | } |
| 2540 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2541 | PyErr_SetString(PyExc_TypeError, |
| 2542 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2543 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2544 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2545 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2546 | PyErr_SetString(PyExc_TypeError, |
| 2547 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2548 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2551 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2552 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2553 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2554 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2555 | } |
| 2556 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2557 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2558 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2559 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2560 | &argvlist[i])) |
| 2561 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2562 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2563 | goto fail_1; |
| 2564 | } |
| 2565 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2566 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2567 | argvlist[argc] = NULL; |
| 2568 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2569 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2570 | if (i < 0) |
| 2571 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2572 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2573 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2574 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2575 | goto fail_1; |
| 2576 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2577 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2578 | keys = PyMapping_Keys(env); |
| 2579 | vals = PyMapping_Values(env); |
| 2580 | if (!keys || !vals) |
| 2581 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2582 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2583 | PyErr_SetString(PyExc_TypeError, |
| 2584 | "execve(): env.keys() or env.values() is not a list"); |
| 2585 | goto fail_2; |
| 2586 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2587 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2588 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2589 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2590 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2591 | |
| 2592 | key = PyList_GetItem(keys, pos); |
| 2593 | val = PyList_GetItem(vals, pos); |
| 2594 | if (!key || !val) |
| 2595 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2596 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2597 | if (!PyArg_Parse( |
| 2598 | key, |
| 2599 | "s;execve() arg 3 contains a non-string key", |
| 2600 | &k) || |
| 2601 | !PyArg_Parse( |
| 2602 | val, |
| 2603 | "s;execve() arg 3 contains a non-string value", |
| 2604 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2605 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2606 | goto fail_2; |
| 2607 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2608 | |
| 2609 | #if defined(PYOS_OS2) |
| 2610 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2611 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2612 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2613 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2614 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2615 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2616 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2617 | goto fail_2; |
| 2618 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2619 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2620 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2621 | #if defined(PYOS_OS2) |
| 2622 | } |
| 2623 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2624 | } |
| 2625 | envlist[envc] = 0; |
| 2626 | |
| 2627 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2628 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2629 | /* If we get here it's definitely an error */ |
| 2630 | |
| 2631 | (void) posix_error(); |
| 2632 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2633 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2634 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2635 | PyMem_DEL(envlist[envc]); |
| 2636 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2637 | fail_1: |
| 2638 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2639 | Py_XDECREF(vals); |
| 2640 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2641 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2642 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2643 | return NULL; |
| 2644 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2645 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2646 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2647 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2648 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2649 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2650 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2651 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2652 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2653 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2654 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2655 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2656 | |
| 2657 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2658 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2659 | { |
| 2660 | char *path; |
| 2661 | PyObject *argv; |
| 2662 | char **argvlist; |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 2663 | int mode, i; |
| 2664 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2665 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2666 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2667 | |
| 2668 | /* spawnv has three arguments: (mode, path, argv), where |
| 2669 | argv is a list or tuple of strings. */ |
| 2670 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2671 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2672 | Py_FileSystemDefaultEncoding, |
| 2673 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2674 | return NULL; |
| 2675 | if (PyList_Check(argv)) { |
| 2676 | argc = PyList_Size(argv); |
| 2677 | getitem = PyList_GetItem; |
| 2678 | } |
| 2679 | else if (PyTuple_Check(argv)) { |
| 2680 | argc = PyTuple_Size(argv); |
| 2681 | getitem = PyTuple_GetItem; |
| 2682 | } |
| 2683 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2684 | PyErr_SetString(PyExc_TypeError, |
| 2685 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2686 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2687 | return NULL; |
| 2688 | } |
| 2689 | |
| 2690 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2691 | if (argvlist == NULL) { |
| 2692 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2693 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2694 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2695 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2696 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2697 | Py_FileSystemDefaultEncoding, |
| 2698 | &argvlist[i])) { |
| 2699 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2700 | PyErr_SetString( |
| 2701 | PyExc_TypeError, |
| 2702 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2703 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2704 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2705 | } |
| 2706 | } |
| 2707 | argvlist[argc] = NULL; |
| 2708 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2709 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2710 | Py_BEGIN_ALLOW_THREADS |
| 2711 | spawnval = spawnv(mode, path, argvlist); |
| 2712 | Py_END_ALLOW_THREADS |
| 2713 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2714 | if (mode == _OLD_P_OVERLAY) |
| 2715 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2716 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2717 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2718 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2719 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2720 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2721 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2722 | free_string_array(argvlist, argc); |
| 2723 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2724 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2725 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2726 | return posix_error(); |
| 2727 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2728 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2729 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2730 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2731 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2732 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2736 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2737 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2738 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2739 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2740 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2741 | path: path of executable file\n\ |
| 2742 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2743 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2744 | |
| 2745 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2746 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2747 | { |
| 2748 | char *path; |
| 2749 | PyObject *argv, *env; |
| 2750 | char **argvlist; |
| 2751 | char **envlist; |
| 2752 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 2753 | int mode, pos, envc; |
| 2754 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2755 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2756 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 2757 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2758 | |
| 2759 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 2760 | argv is a list or tuple of strings and env is a dictionary |
| 2761 | like posix.environ. */ |
| 2762 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2763 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 2764 | Py_FileSystemDefaultEncoding, |
| 2765 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2766 | return NULL; |
| 2767 | if (PyList_Check(argv)) { |
| 2768 | argc = PyList_Size(argv); |
| 2769 | getitem = PyList_GetItem; |
| 2770 | } |
| 2771 | else if (PyTuple_Check(argv)) { |
| 2772 | argc = PyTuple_Size(argv); |
| 2773 | getitem = PyTuple_GetItem; |
| 2774 | } |
| 2775 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2776 | PyErr_SetString(PyExc_TypeError, |
| 2777 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2778 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2779 | } |
| 2780 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2781 | PyErr_SetString(PyExc_TypeError, |
| 2782 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2783 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
| 2786 | argvlist = PyMem_NEW(char *, argc+1); |
| 2787 | if (argvlist == NULL) { |
| 2788 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2789 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2790 | } |
| 2791 | for (i = 0; i < argc; i++) { |
| 2792 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2793 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2794 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2795 | &argvlist[i])) |
| 2796 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2797 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2798 | goto fail_1; |
| 2799 | } |
| 2800 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2801 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2802 | argvlist[argc] = NULL; |
| 2803 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2804 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2805 | if (i < 0) |
| 2806 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2807 | envlist = PyMem_NEW(char *, i + 1); |
| 2808 | if (envlist == NULL) { |
| 2809 | PyErr_NoMemory(); |
| 2810 | goto fail_1; |
| 2811 | } |
| 2812 | envc = 0; |
| 2813 | keys = PyMapping_Keys(env); |
| 2814 | vals = PyMapping_Values(env); |
| 2815 | if (!keys || !vals) |
| 2816 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2817 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2818 | PyErr_SetString(PyExc_TypeError, |
| 2819 | "spawnve(): env.keys() or env.values() is not a list"); |
| 2820 | goto fail_2; |
| 2821 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2822 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2823 | for (pos = 0; pos < i; pos++) { |
| 2824 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2825 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2826 | |
| 2827 | key = PyList_GetItem(keys, pos); |
| 2828 | val = PyList_GetItem(vals, pos); |
| 2829 | if (!key || !val) |
| 2830 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2831 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2832 | if (!PyArg_Parse( |
| 2833 | key, |
| 2834 | "s;spawnve() arg 3 contains a non-string key", |
| 2835 | &k) || |
| 2836 | !PyArg_Parse( |
| 2837 | val, |
| 2838 | "s;spawnve() arg 3 contains a non-string value", |
| 2839 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2840 | { |
| 2841 | goto fail_2; |
| 2842 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2843 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2844 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2845 | if (p == NULL) { |
| 2846 | PyErr_NoMemory(); |
| 2847 | goto fail_2; |
| 2848 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2849 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2850 | envlist[envc++] = p; |
| 2851 | } |
| 2852 | envlist[envc] = 0; |
| 2853 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2854 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2855 | Py_BEGIN_ALLOW_THREADS |
| 2856 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2857 | Py_END_ALLOW_THREADS |
| 2858 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2859 | if (mode == _OLD_P_OVERLAY) |
| 2860 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2861 | |
| 2862 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2863 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2864 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2865 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2866 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2867 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2868 | (void) posix_error(); |
| 2869 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2870 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2871 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2872 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2873 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2874 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2875 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2876 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2877 | while (--envc >= 0) |
| 2878 | PyMem_DEL(envlist[envc]); |
| 2879 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2880 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2881 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2882 | Py_XDECREF(vals); |
| 2883 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2884 | fail_0: |
| 2885 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2886 | return res; |
| 2887 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2888 | |
| 2889 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 2890 | #if defined(PYOS_OS2) |
| 2891 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 2892 | "spawnvp(mode, file, args)\n\n\ |
| 2893 | Execute the program 'file' in a new process, using the environment\n\ |
| 2894 | search path to find the file.\n\ |
| 2895 | \n\ |
| 2896 | mode: mode of process creation\n\ |
| 2897 | file: executable file name\n\ |
| 2898 | args: tuple or list of strings"); |
| 2899 | |
| 2900 | static PyObject * |
| 2901 | posix_spawnvp(PyObject *self, PyObject *args) |
| 2902 | { |
| 2903 | char *path; |
| 2904 | PyObject *argv; |
| 2905 | char **argvlist; |
| 2906 | int mode, i, argc; |
| 2907 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2908 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2909 | |
| 2910 | /* spawnvp has three arguments: (mode, path, argv), where |
| 2911 | argv is a list or tuple of strings. */ |
| 2912 | |
| 2913 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 2914 | Py_FileSystemDefaultEncoding, |
| 2915 | &path, &argv)) |
| 2916 | return NULL; |
| 2917 | if (PyList_Check(argv)) { |
| 2918 | argc = PyList_Size(argv); |
| 2919 | getitem = PyList_GetItem; |
| 2920 | } |
| 2921 | else if (PyTuple_Check(argv)) { |
| 2922 | argc = PyTuple_Size(argv); |
| 2923 | getitem = PyTuple_GetItem; |
| 2924 | } |
| 2925 | else { |
| 2926 | PyErr_SetString(PyExc_TypeError, |
| 2927 | "spawnvp() arg 2 must be a tuple or list"); |
| 2928 | PyMem_Free(path); |
| 2929 | return NULL; |
| 2930 | } |
| 2931 | |
| 2932 | argvlist = PyMem_NEW(char *, argc+1); |
| 2933 | if (argvlist == NULL) { |
| 2934 | PyMem_Free(path); |
| 2935 | return PyErr_NoMemory(); |
| 2936 | } |
| 2937 | for (i = 0; i < argc; i++) { |
| 2938 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2939 | Py_FileSystemDefaultEncoding, |
| 2940 | &argvlist[i])) { |
| 2941 | free_string_array(argvlist, i); |
| 2942 | PyErr_SetString( |
| 2943 | PyExc_TypeError, |
| 2944 | "spawnvp() arg 2 must contain only strings"); |
| 2945 | PyMem_Free(path); |
| 2946 | return NULL; |
| 2947 | } |
| 2948 | } |
| 2949 | argvlist[argc] = NULL; |
| 2950 | |
| 2951 | Py_BEGIN_ALLOW_THREADS |
| 2952 | #if defined(PYCC_GCC) |
| 2953 | spawnval = spawnvp(mode, path, argvlist); |
| 2954 | #else |
| 2955 | spawnval = _spawnvp(mode, path, argvlist); |
| 2956 | #endif |
| 2957 | Py_END_ALLOW_THREADS |
| 2958 | |
| 2959 | free_string_array(argvlist, argc); |
| 2960 | PyMem_Free(path); |
| 2961 | |
| 2962 | if (spawnval == -1) |
| 2963 | return posix_error(); |
| 2964 | else |
| 2965 | return Py_BuildValue("l", (long) spawnval); |
| 2966 | } |
| 2967 | |
| 2968 | |
| 2969 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 2970 | "spawnvpe(mode, file, args, env)\n\n\ |
| 2971 | Execute the program 'file' in a new process, using the environment\n\ |
| 2972 | search path to find the file.\n\ |
| 2973 | \n\ |
| 2974 | mode: mode of process creation\n\ |
| 2975 | file: executable file name\n\ |
| 2976 | args: tuple or list of arguments\n\ |
| 2977 | env: dictionary of strings mapping to strings"); |
| 2978 | |
| 2979 | static PyObject * |
| 2980 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 2981 | { |
| 2982 | char *path; |
| 2983 | PyObject *argv, *env; |
| 2984 | char **argvlist; |
| 2985 | char **envlist; |
| 2986 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2987 | int mode, i, pos, argc, envc; |
| 2988 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2989 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2990 | int lastarg = 0; |
| 2991 | |
| 2992 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 2993 | argv is a list or tuple of strings and env is a dictionary |
| 2994 | like posix.environ. */ |
| 2995 | |
| 2996 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 2997 | Py_FileSystemDefaultEncoding, |
| 2998 | &path, &argv, &env)) |
| 2999 | return NULL; |
| 3000 | if (PyList_Check(argv)) { |
| 3001 | argc = PyList_Size(argv); |
| 3002 | getitem = PyList_GetItem; |
| 3003 | } |
| 3004 | else if (PyTuple_Check(argv)) { |
| 3005 | argc = PyTuple_Size(argv); |
| 3006 | getitem = PyTuple_GetItem; |
| 3007 | } |
| 3008 | else { |
| 3009 | PyErr_SetString(PyExc_TypeError, |
| 3010 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3011 | goto fail_0; |
| 3012 | } |
| 3013 | if (!PyMapping_Check(env)) { |
| 3014 | PyErr_SetString(PyExc_TypeError, |
| 3015 | "spawnvpe() arg 3 must be a mapping object"); |
| 3016 | goto fail_0; |
| 3017 | } |
| 3018 | |
| 3019 | argvlist = PyMem_NEW(char *, argc+1); |
| 3020 | if (argvlist == NULL) { |
| 3021 | PyErr_NoMemory(); |
| 3022 | goto fail_0; |
| 3023 | } |
| 3024 | for (i = 0; i < argc; i++) { |
| 3025 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3026 | "et;spawnvpe() arg 2 must contain only strings", |
| 3027 | Py_FileSystemDefaultEncoding, |
| 3028 | &argvlist[i])) |
| 3029 | { |
| 3030 | lastarg = i; |
| 3031 | goto fail_1; |
| 3032 | } |
| 3033 | } |
| 3034 | lastarg = argc; |
| 3035 | argvlist[argc] = NULL; |
| 3036 | |
| 3037 | i = PyMapping_Size(env); |
| 3038 | if (i < 0) |
| 3039 | goto fail_1; |
| 3040 | envlist = PyMem_NEW(char *, i + 1); |
| 3041 | if (envlist == NULL) { |
| 3042 | PyErr_NoMemory(); |
| 3043 | goto fail_1; |
| 3044 | } |
| 3045 | envc = 0; |
| 3046 | keys = PyMapping_Keys(env); |
| 3047 | vals = PyMapping_Values(env); |
| 3048 | if (!keys || !vals) |
| 3049 | goto fail_2; |
| 3050 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3051 | PyErr_SetString(PyExc_TypeError, |
| 3052 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3053 | goto fail_2; |
| 3054 | } |
| 3055 | |
| 3056 | for (pos = 0; pos < i; pos++) { |
| 3057 | char *p, *k, *v; |
| 3058 | size_t len; |
| 3059 | |
| 3060 | key = PyList_GetItem(keys, pos); |
| 3061 | val = PyList_GetItem(vals, pos); |
| 3062 | if (!key || !val) |
| 3063 | goto fail_2; |
| 3064 | |
| 3065 | if (!PyArg_Parse( |
| 3066 | key, |
| 3067 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3068 | &k) || |
| 3069 | !PyArg_Parse( |
| 3070 | val, |
| 3071 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3072 | &v)) |
| 3073 | { |
| 3074 | goto fail_2; |
| 3075 | } |
| 3076 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3077 | p = PyMem_NEW(char, len); |
| 3078 | if (p == NULL) { |
| 3079 | PyErr_NoMemory(); |
| 3080 | goto fail_2; |
| 3081 | } |
| 3082 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3083 | envlist[envc++] = p; |
| 3084 | } |
| 3085 | envlist[envc] = 0; |
| 3086 | |
| 3087 | Py_BEGIN_ALLOW_THREADS |
| 3088 | #if defined(PYCC_GCC) |
| 3089 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3090 | #else |
| 3091 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3092 | #endif |
| 3093 | Py_END_ALLOW_THREADS |
| 3094 | |
| 3095 | if (spawnval == -1) |
| 3096 | (void) posix_error(); |
| 3097 | else |
| 3098 | res = Py_BuildValue("l", (long) spawnval); |
| 3099 | |
| 3100 | fail_2: |
| 3101 | while (--envc >= 0) |
| 3102 | PyMem_DEL(envlist[envc]); |
| 3103 | PyMem_DEL(envlist); |
| 3104 | fail_1: |
| 3105 | free_string_array(argvlist, lastarg); |
| 3106 | Py_XDECREF(vals); |
| 3107 | Py_XDECREF(keys); |
| 3108 | fail_0: |
| 3109 | PyMem_Free(path); |
| 3110 | return res; |
| 3111 | } |
| 3112 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3113 | #endif /* HAVE_SPAWNV */ |
| 3114 | |
| 3115 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3116 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3117 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3118 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3119 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3120 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3121 | 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] | 3122 | |
| 3123 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3124 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3125 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3126 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3127 | if (pid == -1) |
| 3128 | return posix_error(); |
| 3129 | PyOS_AfterFork(); |
| 3130 | return PyInt_FromLong((long)pid); |
| 3131 | } |
| 3132 | #endif |
| 3133 | |
| 3134 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3135 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3136 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3137 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3138 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3139 | 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] | 3140 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3141 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3142 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3143 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3144 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3145 | if (pid == -1) |
| 3146 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3147 | if (pid == 0) |
| 3148 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3149 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3150 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3151 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3152 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3153 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3154 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3155 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3156 | #define DEV_PTY_FILE "/dev/ptc" |
| 3157 | #define HAVE_DEV_PTMX |
| 3158 | #else |
| 3159 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3160 | #endif |
| 3161 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3162 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3163 | #ifdef HAVE_PTY_H |
| 3164 | #include <pty.h> |
| 3165 | #else |
| 3166 | #ifdef HAVE_LIBUTIL_H |
| 3167 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3168 | #endif /* HAVE_LIBUTIL_H */ |
| 3169 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3170 | #ifdef HAVE_STROPTS_H |
| 3171 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3172 | #endif |
| 3173 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3174 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3175 | #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] | 3176 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3177 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3178 | 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] | 3179 | |
| 3180 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3181 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3182 | { |
| 3183 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3184 | #ifndef HAVE_OPENPTY |
| 3185 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3186 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3187 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3188 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3189 | #ifdef sun |
Neal Norwitz | 84a98e0 | 2006-04-10 07:44:23 +0000 | [diff] [blame] | 3190 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3191 | #endif |
| 3192 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3193 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3194 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3195 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3196 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3197 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3198 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3199 | if (slave_name == NULL) |
| 3200 | return posix_error(); |
| 3201 | |
| 3202 | slave_fd = open(slave_name, O_RDWR); |
| 3203 | if (slave_fd < 0) |
| 3204 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3205 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3206 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3207 | if (master_fd < 0) |
| 3208 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3209 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3210 | /* change permission of slave */ |
| 3211 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3212 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3213 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3214 | } |
| 3215 | /* unlock slave */ |
| 3216 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3217 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3218 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3219 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3220 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3221 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3222 | if (slave_name == NULL) |
| 3223 | return posix_error(); |
| 3224 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3225 | if (slave_fd < 0) |
| 3226 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3227 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3228 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3229 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3230 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3231 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3232 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3233 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3234 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3235 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3236 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3237 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3238 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3239 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3240 | |
| 3241 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3242 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3243 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3244 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3245 | 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] | 3246 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3247 | |
| 3248 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3249 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3250 | { |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 3251 | int master_fd = -1, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3252 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3253 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3254 | if (pid == -1) |
| 3255 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3256 | if (pid == 0) |
| 3257 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3258 | return Py_BuildValue("(ii)", pid, master_fd); |
| 3259 | } |
| 3260 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3261 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3262 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3263 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3264 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3265 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3266 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3267 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3268 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3269 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3270 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3271 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3272 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3273 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3274 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3275 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3276 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3277 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3278 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3279 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3280 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3281 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3282 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3283 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3284 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3285 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3286 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3287 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3288 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3289 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3290 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3291 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3292 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3293 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3294 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3295 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3296 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3297 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3298 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3299 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3300 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3301 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3302 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3303 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3304 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3305 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3306 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3307 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3308 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3311 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3312 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3313 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3314 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3315 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3316 | |
| 3317 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3318 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3319 | { |
| 3320 | PyObject *result = NULL; |
| 3321 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3322 | #ifdef NGROUPS_MAX |
| 3323 | #define MAX_GROUPS NGROUPS_MAX |
| 3324 | #else |
| 3325 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3326 | #define MAX_GROUPS 64 |
| 3327 | #endif |
| 3328 | gid_t grouplist[MAX_GROUPS]; |
| 3329 | int n; |
| 3330 | |
| 3331 | n = getgroups(MAX_GROUPS, grouplist); |
| 3332 | if (n < 0) |
| 3333 | posix_error(); |
| 3334 | else { |
| 3335 | result = PyList_New(n); |
| 3336 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3337 | int i; |
| 3338 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3339 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3340 | if (o == NULL) { |
| 3341 | Py_DECREF(result); |
| 3342 | result = NULL; |
| 3343 | break; |
| 3344 | } |
| 3345 | PyList_SET_ITEM(result, i, o); |
| 3346 | } |
| 3347 | } |
| 3348 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3349 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3350 | return result; |
| 3351 | } |
| 3352 | #endif |
| 3353 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3354 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3355 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3356 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3357 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3358 | |
| 3359 | static PyObject * |
| 3360 | posix_getpgid(PyObject *self, PyObject *args) |
| 3361 | { |
| 3362 | int pid, pgid; |
| 3363 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3364 | return NULL; |
| 3365 | pgid = getpgid(pid); |
| 3366 | if (pgid < 0) |
| 3367 | return posix_error(); |
| 3368 | return PyInt_FromLong((long)pgid); |
| 3369 | } |
| 3370 | #endif /* HAVE_GETPGID */ |
| 3371 | |
| 3372 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3373 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3374 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3375 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3376 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3377 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3378 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3379 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3380 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3381 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3382 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3383 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3384 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3385 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3386 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3387 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3388 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3389 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3390 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3391 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3392 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3393 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3394 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3395 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3396 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3397 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3398 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3399 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3400 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3401 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3402 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3403 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3404 | Py_INCREF(Py_None); |
| 3405 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3408 | #endif /* HAVE_SETPGRP */ |
| 3409 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3410 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3411 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3412 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3413 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3414 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3415 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3416 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3417 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3418 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3419 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3420 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3421 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3422 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3423 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3424 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3425 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3426 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3427 | |
| 3428 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3429 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3430 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3431 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3432 | char *name; |
| 3433 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3434 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3435 | errno = 0; |
| 3436 | name = getlogin(); |
| 3437 | if (name == NULL) { |
| 3438 | if (errno) |
| 3439 | posix_error(); |
| 3440 | else |
| 3441 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3442 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3443 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3444 | else |
| 3445 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3446 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3447 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3448 | return result; |
| 3449 | } |
| 3450 | #endif |
| 3451 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3452 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3453 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3454 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3455 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3456 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3457 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3458 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3459 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3460 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3461 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3462 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3463 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3464 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3465 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3466 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3467 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3468 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3469 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3470 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3471 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3472 | { |
| 3473 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3474 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3475 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3476 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3477 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3478 | APIRET rc; |
| 3479 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3480 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3481 | |
| 3482 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3483 | APIRET rc; |
| 3484 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3485 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3486 | |
| 3487 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3488 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3489 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3490 | if (kill(pid, sig) == -1) |
| 3491 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3492 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3493 | Py_INCREF(Py_None); |
| 3494 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3495 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3496 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3497 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3498 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3499 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3500 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3501 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3502 | |
| 3503 | static PyObject * |
| 3504 | posix_killpg(PyObject *self, PyObject *args) |
| 3505 | { |
| 3506 | int pgid, sig; |
| 3507 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3508 | return NULL; |
| 3509 | if (killpg(pgid, sig) == -1) |
| 3510 | return posix_error(); |
| 3511 | Py_INCREF(Py_None); |
| 3512 | return Py_None; |
| 3513 | } |
| 3514 | #endif |
| 3515 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3516 | #ifdef HAVE_PLOCK |
| 3517 | |
| 3518 | #ifdef HAVE_SYS_LOCK_H |
| 3519 | #include <sys/lock.h> |
| 3520 | #endif |
| 3521 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3522 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3523 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3524 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3525 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3526 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3527 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3528 | { |
| 3529 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3530 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3531 | return NULL; |
| 3532 | if (plock(op) == -1) |
| 3533 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3534 | Py_INCREF(Py_None); |
| 3535 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3536 | } |
| 3537 | #endif |
| 3538 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3539 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3540 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3541 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3542 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3543 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3544 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3545 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3546 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3547 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3548 | async_system(const char *command) |
| 3549 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3550 | char errormsg[256], args[1024]; |
| 3551 | RESULTCODES rcodes; |
| 3552 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3553 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3554 | char *shell = getenv("COMSPEC"); |
| 3555 | if (!shell) |
| 3556 | shell = "cmd"; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3557 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3558 | /* avoid overflowing the argument buffer */ |
| 3559 | if (strlen(shell) + 3 + strlen(command) >= 1024) |
| 3560 | return ERROR_NOT_ENOUGH_MEMORY |
| 3561 | |
| 3562 | args[0] = '\0'; |
| 3563 | strcat(args, shell); |
| 3564 | strcat(args, "/c "); |
| 3565 | strcat(args, command); |
| 3566 | |
| 3567 | /* execute asynchronously, inheriting the environment */ |
| 3568 | rc = DosExecPgm(errormsg, |
| 3569 | sizeof(errormsg), |
| 3570 | EXEC_ASYNC, |
| 3571 | args, |
| 3572 | NULL, |
| 3573 | &rcodes, |
| 3574 | shell); |
| 3575 | return rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3578 | static FILE * |
| 3579 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3580 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3581 | int oldfd, tgtfd; |
| 3582 | HFILE pipeh[2]; |
| 3583 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3584 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3585 | /* mode determines which of stdin or stdout is reconnected to |
| 3586 | * the pipe to the child |
| 3587 | */ |
| 3588 | if (strchr(mode, 'r') != NULL) { |
| 3589 | tgt_fd = 1; /* stdout */ |
| 3590 | } else if (strchr(mode, 'w')) { |
| 3591 | tgt_fd = 0; /* stdin */ |
| 3592 | } else { |
| 3593 | *err = ERROR_INVALID_ACCESS; |
| 3594 | return NULL; |
| 3595 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3596 | |
Andrew MacIntyre | a3be258 | 2004-12-18 09:51:05 +0000 | [diff] [blame] | 3597 | /* setup the pipe */ |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3598 | if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) { |
| 3599 | *err = rc; |
| 3600 | return NULL; |
| 3601 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3602 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3603 | /* prevent other threads accessing stdio */ |
| 3604 | DosEnterCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3605 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3606 | /* reconnect stdio and execute child */ |
| 3607 | oldfd = dup(tgtfd); |
| 3608 | close(tgtfd); |
| 3609 | if (dup2(pipeh[tgtfd], tgtfd) == 0) { |
| 3610 | DosClose(pipeh[tgtfd]); |
| 3611 | rc = async_system(command); |
| 3612 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3613 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3614 | /* restore stdio */ |
| 3615 | dup2(oldfd, tgtfd); |
| 3616 | close(oldfd); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3617 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3618 | /* allow other threads access to stdio */ |
| 3619 | DosExitCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3620 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3621 | /* if execution of child was successful return file stream */ |
| 3622 | if (rc == NO_ERROR) |
| 3623 | return fdopen(pipeh[1 - tgtfd], mode); |
| 3624 | else { |
| 3625 | DosClose(pipeh[1 - tgtfd]); |
| 3626 | *err = rc; |
| 3627 | return NULL; |
| 3628 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3632 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3633 | { |
| 3634 | char *name; |
| 3635 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3636 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3637 | FILE *fp; |
| 3638 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3639 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3640 | return NULL; |
| 3641 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3642 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3643 | Py_END_ALLOW_THREADS |
| 3644 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3645 | return os2_error(err); |
| 3646 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3647 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 3648 | if (f != NULL) |
| 3649 | PyFile_SetBufSize(f, bufsize); |
| 3650 | return f; |
| 3651 | } |
| 3652 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3653 | #elif defined(PYCC_GCC) |
| 3654 | |
| 3655 | /* standard posix version of popen() support */ |
| 3656 | static PyObject * |
| 3657 | posix_popen(PyObject *self, PyObject *args) |
| 3658 | { |
| 3659 | char *name; |
| 3660 | char *mode = "r"; |
| 3661 | int bufsize = -1; |
| 3662 | FILE *fp; |
| 3663 | PyObject *f; |
| 3664 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 3665 | return NULL; |
| 3666 | Py_BEGIN_ALLOW_THREADS |
| 3667 | fp = popen(name, mode); |
| 3668 | Py_END_ALLOW_THREADS |
| 3669 | if (fp == NULL) |
| 3670 | return posix_error(); |
| 3671 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 3672 | if (f != NULL) |
| 3673 | PyFile_SetBufSize(f, bufsize); |
| 3674 | return f; |
| 3675 | } |
| 3676 | |
| 3677 | /* fork() under OS/2 has lots'o'warts |
| 3678 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 3679 | * most of this code is a ripoff of the win32 code, but using the |
| 3680 | * capabilities of EMX's C library routines |
| 3681 | */ |
| 3682 | |
| 3683 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 3684 | #define POPEN_1 1 |
| 3685 | #define POPEN_2 2 |
| 3686 | #define POPEN_3 3 |
| 3687 | #define POPEN_4 4 |
| 3688 | |
| 3689 | static PyObject *_PyPopen(char *, int, int, int); |
| 3690 | static int _PyPclose(FILE *file); |
| 3691 | |
| 3692 | /* |
| 3693 | * Internal dictionary mapping popen* file pointers to process handles, |
| 3694 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3695 | * for more information on this dictionary's use. |
| 3696 | */ |
| 3697 | static PyObject *_PyPopenProcs = NULL; |
| 3698 | |
| 3699 | /* os2emx version of popen2() |
| 3700 | * |
| 3701 | * The result of this function is a pipe (file) connected to the |
| 3702 | * process's stdin, and a pipe connected to the process's stdout. |
| 3703 | */ |
| 3704 | |
| 3705 | static PyObject * |
| 3706 | os2emx_popen2(PyObject *self, PyObject *args) |
| 3707 | { |
| 3708 | PyObject *f; |
| 3709 | int tm=0; |
| 3710 | |
| 3711 | char *cmdstring; |
| 3712 | char *mode = "t"; |
| 3713 | int bufsize = -1; |
| 3714 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 3715 | return NULL; |
| 3716 | |
| 3717 | if (*mode == 't') |
| 3718 | tm = O_TEXT; |
| 3719 | else if (*mode != 'b') { |
| 3720 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3721 | return NULL; |
| 3722 | } else |
| 3723 | tm = O_BINARY; |
| 3724 | |
| 3725 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 3726 | |
| 3727 | return f; |
| 3728 | } |
| 3729 | |
| 3730 | /* |
| 3731 | * Variation on os2emx.popen2 |
| 3732 | * |
| 3733 | * The result of this function is 3 pipes - the process's stdin, |
| 3734 | * stdout and stderr |
| 3735 | */ |
| 3736 | |
| 3737 | static PyObject * |
| 3738 | os2emx_popen3(PyObject *self, PyObject *args) |
| 3739 | { |
| 3740 | PyObject *f; |
| 3741 | int tm = 0; |
| 3742 | |
| 3743 | char *cmdstring; |
| 3744 | char *mode = "t"; |
| 3745 | int bufsize = -1; |
| 3746 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 3747 | return NULL; |
| 3748 | |
| 3749 | if (*mode == 't') |
| 3750 | tm = O_TEXT; |
| 3751 | else if (*mode != 'b') { |
| 3752 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3753 | return NULL; |
| 3754 | } else |
| 3755 | tm = O_BINARY; |
| 3756 | |
| 3757 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 3758 | |
| 3759 | return f; |
| 3760 | } |
| 3761 | |
| 3762 | /* |
| 3763 | * Variation on os2emx.popen2 |
| 3764 | * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3765 | * The result of this function is 2 pipes - the processes stdin, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3766 | * and stdout+stderr combined as a single pipe. |
| 3767 | */ |
| 3768 | |
| 3769 | static PyObject * |
| 3770 | os2emx_popen4(PyObject *self, PyObject *args) |
| 3771 | { |
| 3772 | PyObject *f; |
| 3773 | int tm = 0; |
| 3774 | |
| 3775 | char *cmdstring; |
| 3776 | char *mode = "t"; |
| 3777 | int bufsize = -1; |
| 3778 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 3779 | return NULL; |
| 3780 | |
| 3781 | if (*mode == 't') |
| 3782 | tm = O_TEXT; |
| 3783 | else if (*mode != 'b') { |
| 3784 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3785 | return NULL; |
| 3786 | } else |
| 3787 | tm = O_BINARY; |
| 3788 | |
| 3789 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 3790 | |
| 3791 | return f; |
| 3792 | } |
| 3793 | |
| 3794 | /* a couple of structures for convenient handling of multiple |
| 3795 | * file handles and pipes |
| 3796 | */ |
| 3797 | struct file_ref |
| 3798 | { |
| 3799 | int handle; |
| 3800 | int flags; |
| 3801 | }; |
| 3802 | |
| 3803 | struct pipe_ref |
| 3804 | { |
| 3805 | int rd; |
| 3806 | int wr; |
| 3807 | }; |
| 3808 | |
| 3809 | /* The following code is derived from the win32 code */ |
| 3810 | |
| 3811 | static PyObject * |
| 3812 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 3813 | { |
| 3814 | struct file_ref stdio[3]; |
| 3815 | struct pipe_ref p_fd[3]; |
| 3816 | FILE *p_s[3]; |
| 3817 | int file_count, i, pipe_err, pipe_pid; |
| 3818 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 3819 | PyObject *f, *p_f[3]; |
| 3820 | |
| 3821 | /* file modes for subsequent fdopen's on pipe handles */ |
| 3822 | if (mode == O_TEXT) |
| 3823 | { |
| 3824 | rd_mode = "rt"; |
| 3825 | wr_mode = "wt"; |
| 3826 | } |
| 3827 | else |
| 3828 | { |
| 3829 | rd_mode = "rb"; |
| 3830 | wr_mode = "wb"; |
| 3831 | } |
| 3832 | |
| 3833 | /* prepare shell references */ |
| 3834 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 3835 | if ((shell = getenv("COMSPEC")) == NULL) |
| 3836 | { |
| 3837 | errno = ENOENT; |
| 3838 | return posix_error(); |
| 3839 | } |
| 3840 | |
| 3841 | sh_name = _getname(shell); |
| 3842 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 3843 | opt = "/c"; |
| 3844 | else |
| 3845 | opt = "-c"; |
| 3846 | |
| 3847 | /* save current stdio fds + their flags, and set not inheritable */ |
| 3848 | i = pipe_err = 0; |
| 3849 | while (pipe_err >= 0 && i < 3) |
| 3850 | { |
| 3851 | pipe_err = stdio[i].handle = dup(i); |
| 3852 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 3853 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 3854 | i++; |
| 3855 | } |
| 3856 | if (pipe_err < 0) |
| 3857 | { |
| 3858 | /* didn't get them all saved - clean up and bail out */ |
| 3859 | int saved_err = errno; |
| 3860 | while (i-- > 0) |
| 3861 | { |
| 3862 | close(stdio[i].handle); |
| 3863 | } |
| 3864 | errno = saved_err; |
| 3865 | return posix_error(); |
| 3866 | } |
| 3867 | |
| 3868 | /* create pipe ends */ |
| 3869 | file_count = 2; |
| 3870 | if (n == POPEN_3) |
| 3871 | file_count = 3; |
| 3872 | i = pipe_err = 0; |
| 3873 | while ((pipe_err == 0) && (i < file_count)) |
| 3874 | pipe_err = pipe((int *)&p_fd[i++]); |
| 3875 | if (pipe_err < 0) |
| 3876 | { |
| 3877 | /* didn't get them all made - clean up and bail out */ |
| 3878 | while (i-- > 0) |
| 3879 | { |
| 3880 | close(p_fd[i].wr); |
| 3881 | close(p_fd[i].rd); |
| 3882 | } |
| 3883 | errno = EPIPE; |
| 3884 | return posix_error(); |
| 3885 | } |
| 3886 | |
| 3887 | /* change the actual standard IO streams over temporarily, |
| 3888 | * making the retained pipe ends non-inheritable |
| 3889 | */ |
| 3890 | pipe_err = 0; |
| 3891 | |
| 3892 | /* - stdin */ |
| 3893 | if (dup2(p_fd[0].rd, 0) == 0) |
| 3894 | { |
| 3895 | close(p_fd[0].rd); |
| 3896 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 3897 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 3898 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 3899 | { |
| 3900 | close(p_fd[0].wr); |
| 3901 | pipe_err = -1; |
| 3902 | } |
| 3903 | } |
| 3904 | else |
| 3905 | { |
| 3906 | pipe_err = -1; |
| 3907 | } |
| 3908 | |
| 3909 | /* - stdout */ |
| 3910 | if (pipe_err == 0) |
| 3911 | { |
| 3912 | if (dup2(p_fd[1].wr, 1) == 1) |
| 3913 | { |
| 3914 | close(p_fd[1].wr); |
| 3915 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 3916 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 3917 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 3918 | { |
| 3919 | close(p_fd[1].rd); |
| 3920 | pipe_err = -1; |
| 3921 | } |
| 3922 | } |
| 3923 | else |
| 3924 | { |
| 3925 | pipe_err = -1; |
| 3926 | } |
| 3927 | } |
| 3928 | |
| 3929 | /* - stderr, as required */ |
| 3930 | if (pipe_err == 0) |
| 3931 | switch (n) |
| 3932 | { |
| 3933 | case POPEN_3: |
| 3934 | { |
| 3935 | if (dup2(p_fd[2].wr, 2) == 2) |
| 3936 | { |
| 3937 | close(p_fd[2].wr); |
| 3938 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 3939 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 3940 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 3941 | { |
| 3942 | close(p_fd[2].rd); |
| 3943 | pipe_err = -1; |
| 3944 | } |
| 3945 | } |
| 3946 | else |
| 3947 | { |
| 3948 | pipe_err = -1; |
| 3949 | } |
| 3950 | break; |
| 3951 | } |
| 3952 | |
| 3953 | case POPEN_4: |
| 3954 | { |
| 3955 | if (dup2(1, 2) != 2) |
| 3956 | { |
| 3957 | pipe_err = -1; |
| 3958 | } |
| 3959 | break; |
| 3960 | } |
| 3961 | } |
| 3962 | |
| 3963 | /* spawn the child process */ |
| 3964 | if (pipe_err == 0) |
| 3965 | { |
| 3966 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 3967 | if (pipe_pid == -1) |
| 3968 | { |
| 3969 | pipe_err = -1; |
| 3970 | } |
| 3971 | else |
| 3972 | { |
| 3973 | /* save the PID into the FILE structure |
| 3974 | * NOTE: this implementation doesn't actually |
| 3975 | * take advantage of this, but do it for |
| 3976 | * completeness - AIM Apr01 |
| 3977 | */ |
| 3978 | for (i = 0; i < file_count; i++) |
| 3979 | p_s[i]->_pid = pipe_pid; |
| 3980 | } |
| 3981 | } |
| 3982 | |
| 3983 | /* reset standard IO to normal */ |
| 3984 | for (i = 0; i < 3; i++) |
| 3985 | { |
| 3986 | dup2(stdio[i].handle, i); |
| 3987 | fcntl(i, F_SETFD, stdio[i].flags); |
| 3988 | close(stdio[i].handle); |
| 3989 | } |
| 3990 | |
| 3991 | /* if any remnant problems, clean up and bail out */ |
| 3992 | if (pipe_err < 0) |
| 3993 | { |
| 3994 | for (i = 0; i < 3; i++) |
| 3995 | { |
| 3996 | close(p_fd[i].rd); |
| 3997 | close(p_fd[i].wr); |
| 3998 | } |
| 3999 | errno = EPIPE; |
| 4000 | return posix_error_with_filename(cmdstring); |
| 4001 | } |
| 4002 | |
| 4003 | /* build tuple of file objects to return */ |
| 4004 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 4005 | PyFile_SetBufSize(p_f[0], bufsize); |
| 4006 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 4007 | PyFile_SetBufSize(p_f[1], bufsize); |
| 4008 | if (n == POPEN_3) |
| 4009 | { |
| 4010 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 4011 | PyFile_SetBufSize(p_f[0], bufsize); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4012 | f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4013 | } |
| 4014 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4015 | f = PyTuple_Pack(2, p_f[0], p_f[1]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4016 | |
| 4017 | /* |
| 4018 | * Insert the files we've created into the process dictionary |
| 4019 | * all referencing the list with the process handle and the |
| 4020 | * initial number of files (see description below in _PyPclose). |
| 4021 | * Since if _PyPclose later tried to wait on a process when all |
| 4022 | * handles weren't closed, it could create a deadlock with the |
| 4023 | * child, we spend some energy here to try to ensure that we |
| 4024 | * either insert all file handles into the dictionary or none |
| 4025 | * at all. It's a little clumsy with the various popen modes |
| 4026 | * and variable number of files involved. |
| 4027 | */ |
| 4028 | if (!_PyPopenProcs) |
| 4029 | { |
| 4030 | _PyPopenProcs = PyDict_New(); |
| 4031 | } |
| 4032 | |
| 4033 | if (_PyPopenProcs) |
| 4034 | { |
| 4035 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 4036 | int ins_rc[3]; |
| 4037 | |
| 4038 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4039 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4040 | |
| 4041 | procObj = PyList_New(2); |
| 4042 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 4043 | intObj = PyInt_FromLong((long) file_count); |
| 4044 | |
| 4045 | if (procObj && pidObj && intObj) |
| 4046 | { |
| 4047 | PyList_SetItem(procObj, 0, pidObj); |
| 4048 | PyList_SetItem(procObj, 1, intObj); |
| 4049 | |
| 4050 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 4051 | if (fileObj[0]) |
| 4052 | { |
| 4053 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4054 | fileObj[0], |
| 4055 | procObj); |
| 4056 | } |
| 4057 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 4058 | if (fileObj[1]) |
| 4059 | { |
| 4060 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4061 | fileObj[1], |
| 4062 | procObj); |
| 4063 | } |
| 4064 | if (file_count >= 3) |
| 4065 | { |
| 4066 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 4067 | if (fileObj[2]) |
| 4068 | { |
| 4069 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4070 | fileObj[2], |
| 4071 | procObj); |
| 4072 | } |
| 4073 | } |
| 4074 | |
| 4075 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4076 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4077 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 4078 | { |
| 4079 | /* Something failed - remove any dictionary |
| 4080 | * entries that did make it. |
| 4081 | */ |
| 4082 | if (!ins_rc[0] && fileObj[0]) |
| 4083 | { |
| 4084 | PyDict_DelItem(_PyPopenProcs, |
| 4085 | fileObj[0]); |
| 4086 | } |
| 4087 | if (!ins_rc[1] && fileObj[1]) |
| 4088 | { |
| 4089 | PyDict_DelItem(_PyPopenProcs, |
| 4090 | fileObj[1]); |
| 4091 | } |
| 4092 | if (!ins_rc[2] && fileObj[2]) |
| 4093 | { |
| 4094 | PyDict_DelItem(_PyPopenProcs, |
| 4095 | fileObj[2]); |
| 4096 | } |
| 4097 | } |
| 4098 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4099 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4100 | /* |
| 4101 | * Clean up our localized references for the dictionary keys |
| 4102 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4103 | * that got placed in the dictionary. |
| 4104 | */ |
| 4105 | Py_XDECREF(procObj); |
| 4106 | Py_XDECREF(fileObj[0]); |
| 4107 | Py_XDECREF(fileObj[1]); |
| 4108 | Py_XDECREF(fileObj[2]); |
| 4109 | } |
| 4110 | |
| 4111 | /* Child is launched. */ |
| 4112 | return f; |
| 4113 | } |
| 4114 | |
| 4115 | /* |
| 4116 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4117 | * exit code for the child process and return as a result of the close. |
| 4118 | * |
| 4119 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4120 | * input file pointer to information about the process that was |
| 4121 | * originally created by the popen* call that created the file pointer. |
| 4122 | * The dictionary uses the file pointer as a key (with one entry |
| 4123 | * inserted for each file returned by the original popen* call) and a |
| 4124 | * single list object as the value for all files from a single call. |
| 4125 | * The list object contains the Win32 process handle at [0], and a file |
| 4126 | * count at [1], which is initialized to the total number of file |
| 4127 | * handles using that list. |
| 4128 | * |
| 4129 | * This function closes whichever handle it is passed, and decrements |
| 4130 | * the file count in the dictionary for the process handle pointed to |
| 4131 | * by this file. On the last close (when the file count reaches zero), |
| 4132 | * this function will wait for the child process and then return its |
| 4133 | * exit code as the result of the close() operation. This permits the |
| 4134 | * files to be closed in any order - it is always the close() of the |
| 4135 | * final handle that will return the exit code. |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4136 | * |
| 4137 | * NOTE: This function is currently called with the GIL released. |
| 4138 | * hence we use the GILState API to manage our state. |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4139 | */ |
| 4140 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4141 | static int _PyPclose(FILE *file) |
| 4142 | { |
| 4143 | int result; |
| 4144 | int exit_code; |
| 4145 | int pipe_pid; |
| 4146 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 4147 | int file_count; |
| 4148 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4149 | PyGILState_STATE state; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4150 | #endif |
| 4151 | |
| 4152 | /* Close the file handle first, to ensure it can't block the |
| 4153 | * child from exiting if it's the last handle. |
| 4154 | */ |
| 4155 | result = fclose(file); |
| 4156 | |
| 4157 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4158 | state = PyGILState_Ensure(); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4159 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4160 | if (_PyPopenProcs) |
| 4161 | { |
| 4162 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4163 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4164 | fileObj)) != NULL && |
| 4165 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 4166 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 4167 | { |
| 4168 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 4169 | file_count = (int) PyInt_AsLong(intObj); |
| 4170 | |
| 4171 | if (file_count > 1) |
| 4172 | { |
| 4173 | /* Still other files referencing process */ |
| 4174 | file_count--; |
| 4175 | PyList_SetItem(procObj,1, |
| 4176 | PyInt_FromLong((long) file_count)); |
| 4177 | } |
| 4178 | else |
| 4179 | { |
| 4180 | /* Last file for this process */ |
| 4181 | if (result != EOF && |
| 4182 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 4183 | { |
| 4184 | /* extract exit status */ |
| 4185 | if (WIFEXITED(exit_code)) |
| 4186 | { |
| 4187 | result = WEXITSTATUS(exit_code); |
| 4188 | } |
| 4189 | else |
| 4190 | { |
| 4191 | errno = EPIPE; |
| 4192 | result = -1; |
| 4193 | } |
| 4194 | } |
| 4195 | else |
| 4196 | { |
| 4197 | /* Indicate failure - this will cause the file object |
| 4198 | * to raise an I/O error and translate the last |
| 4199 | * error code from errno. We do have a problem with |
| 4200 | * last errors that overlap the normal errno table, |
| 4201 | * but that's a consistent problem with the file object. |
| 4202 | */ |
| 4203 | result = -1; |
| 4204 | } |
| 4205 | } |
| 4206 | |
| 4207 | /* Remove this file pointer from dictionary */ |
| 4208 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4209 | |
| 4210 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 4211 | { |
| 4212 | Py_DECREF(_PyPopenProcs); |
| 4213 | _PyPopenProcs = NULL; |
| 4214 | } |
| 4215 | |
| 4216 | } /* if object retrieval ok */ |
| 4217 | |
| 4218 | Py_XDECREF(fileObj); |
| 4219 | } /* if _PyPopenProcs */ |
| 4220 | |
| 4221 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4222 | PyGILState_Release(state); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4223 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4224 | return result; |
| 4225 | } |
| 4226 | |
| 4227 | #endif /* PYCC_??? */ |
| 4228 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4229 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4230 | |
| 4231 | /* |
| 4232 | * Portable 'popen' replacement for Win32. |
| 4233 | * |
| 4234 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 4235 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4236 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4237 | */ |
| 4238 | |
| 4239 | #include <malloc.h> |
| 4240 | #include <io.h> |
| 4241 | #include <fcntl.h> |
| 4242 | |
| 4243 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 4244 | #define POPEN_1 1 |
| 4245 | #define POPEN_2 2 |
| 4246 | #define POPEN_3 3 |
| 4247 | #define POPEN_4 4 |
| 4248 | |
| 4249 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4250 | static int _PyPclose(FILE *file); |
| 4251 | |
| 4252 | /* |
| 4253 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4254 | * for use when retrieving the process exit code. See _PyPclose() below |
| 4255 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4256 | */ |
| 4257 | static PyObject *_PyPopenProcs = NULL; |
| 4258 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4259 | |
| 4260 | /* popen that works from a GUI. |
| 4261 | * |
| 4262 | * The result of this function is a pipe (file) connected to the |
| 4263 | * processes stdin or stdout, depending on the requested mode. |
| 4264 | */ |
| 4265 | |
| 4266 | static PyObject * |
| 4267 | posix_popen(PyObject *self, PyObject *args) |
| 4268 | { |
Raymond Hettinger | b5cb665 | 2003-09-01 22:25:41 +0000 | [diff] [blame] | 4269 | PyObject *f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4270 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4271 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4272 | char *cmdstring; |
| 4273 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4274 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4275 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4276 | return NULL; |
| 4277 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4278 | if (*mode == 'r') |
| 4279 | tm = _O_RDONLY; |
| 4280 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4281 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4282 | return NULL; |
| 4283 | } else |
| 4284 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4285 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4286 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4287 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4288 | return NULL; |
| 4289 | } |
| 4290 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4291 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4292 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4293 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4294 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4295 | else |
| 4296 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 4297 | |
| 4298 | return f; |
| 4299 | } |
| 4300 | |
| 4301 | /* Variation on win32pipe.popen |
| 4302 | * |
| 4303 | * The result of this function is a pipe (file) connected to the |
| 4304 | * process's stdin, and a pipe connected to the process's stdout. |
| 4305 | */ |
| 4306 | |
| 4307 | static PyObject * |
| 4308 | win32_popen2(PyObject *self, PyObject *args) |
| 4309 | { |
| 4310 | PyObject *f; |
| 4311 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4312 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4313 | char *cmdstring; |
| 4314 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4315 | int bufsize = -1; |
| 4316 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4317 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4318 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4319 | if (*mode == 't') |
| 4320 | tm = _O_TEXT; |
| 4321 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4322 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4323 | return NULL; |
| 4324 | } else |
| 4325 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4326 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4327 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4328 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4329 | return NULL; |
| 4330 | } |
| 4331 | |
| 4332 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4333 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4334 | return f; |
| 4335 | } |
| 4336 | |
| 4337 | /* |
| 4338 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4339 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4340 | * The result of this function is 3 pipes - the process's stdin, |
| 4341 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4342 | */ |
| 4343 | |
| 4344 | static PyObject * |
| 4345 | win32_popen3(PyObject *self, PyObject *args) |
| 4346 | { |
| 4347 | PyObject *f; |
| 4348 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4349 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4350 | char *cmdstring; |
| 4351 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4352 | int bufsize = -1; |
| 4353 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4354 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4355 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4356 | if (*mode == 't') |
| 4357 | tm = _O_TEXT; |
| 4358 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4359 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4360 | return NULL; |
| 4361 | } else |
| 4362 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4363 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4364 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4365 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4366 | return NULL; |
| 4367 | } |
| 4368 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4369 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4370 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4371 | return f; |
| 4372 | } |
| 4373 | |
| 4374 | /* |
| 4375 | * Variation on win32pipe.popen |
| 4376 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4377 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4378 | * and stdout+stderr combined as a single pipe. |
| 4379 | */ |
| 4380 | |
| 4381 | static PyObject * |
| 4382 | win32_popen4(PyObject *self, PyObject *args) |
| 4383 | { |
| 4384 | PyObject *f; |
| 4385 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4386 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4387 | char *cmdstring; |
| 4388 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4389 | int bufsize = -1; |
| 4390 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4391 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4392 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4393 | if (*mode == 't') |
| 4394 | tm = _O_TEXT; |
| 4395 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4396 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4397 | return NULL; |
| 4398 | } else |
| 4399 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4400 | |
| 4401 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4402 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4403 | return NULL; |
| 4404 | } |
| 4405 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4406 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4407 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4408 | return f; |
| 4409 | } |
| 4410 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4411 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4412 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4413 | HANDLE hStdin, |
| 4414 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4415 | HANDLE hStderr, |
| 4416 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4417 | { |
| 4418 | PROCESS_INFORMATION piProcInfo; |
| 4419 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4420 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4421 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4422 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4423 | int i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4424 | Py_ssize_t x; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4425 | |
| 4426 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4427 | char *comshell; |
| 4428 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4429 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4430 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4431 | /* x < i, so x fits into an integer */ |
| 4432 | return (int)x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4433 | |
| 4434 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 4435 | * then use the w9xpopen hack. |
| 4436 | */ |
| 4437 | comshell = s1 + x; |
| 4438 | while (comshell >= s1 && *comshell != '\\') |
| 4439 | --comshell; |
| 4440 | ++comshell; |
| 4441 | |
| 4442 | if (GetVersion() < 0x80000000 && |
| 4443 | _stricmp(comshell, "command.com") != 0) { |
| 4444 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4445 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4446 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4447 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4448 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4449 | } |
| 4450 | else { |
| 4451 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4452 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 4453 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4454 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4455 | char modulepath[_MAX_PATH]; |
| 4456 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4457 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 4458 | for (x = i = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4459 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4460 | x = i+1; |
| 4461 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4462 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4463 | strncat(modulepath, |
| 4464 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4465 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4466 | -strlen(modulepath)); |
| 4467 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4468 | /* Eeek - file-not-found - possibly an embedding |
| 4469 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4470 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4471 | strncpy(modulepath, |
| 4472 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4473 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 4474 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 4475 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4476 | strncat(modulepath, |
| 4477 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4478 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4479 | -strlen(modulepath)); |
| 4480 | /* No where else to look - raise an easily identifiable |
| 4481 | error, rather than leaving Windows to report |
| 4482 | "file not found" - as the user is probably blissfully |
| 4483 | unaware this shim EXE is used, and it will confuse them. |
| 4484 | (well, it confused me for a while ;-) |
| 4485 | */ |
| 4486 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4487 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4488 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4489 | "for popen to work with your shell " |
| 4490 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4491 | szConsoleSpawn); |
| 4492 | return FALSE; |
| 4493 | } |
| 4494 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4495 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4496 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4497 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4498 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4499 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4500 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4501 | /* To maintain correct argument passing semantics, |
| 4502 | we pass the command-line as it stands, and allow |
| 4503 | quoting to be applied. w9xpopen.exe will then |
| 4504 | use its argv vector, and re-quote the necessary |
| 4505 | args for the ultimate child process. |
| 4506 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4507 | PyOS_snprintf( |
| 4508 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4509 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4510 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4511 | s1, |
| 4512 | s3, |
| 4513 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4514 | /* Not passing CREATE_NEW_CONSOLE has been known to |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4515 | cause random failures on win9x. Specifically a |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4516 | dialog: |
| 4517 | "Your program accessed mem currently in use at xxx" |
| 4518 | and a hopeful warning about the stability of your |
| 4519 | system. |
| 4520 | Cost is Ctrl+C wont kill children, but anyone |
| 4521 | who cares can have a go! |
| 4522 | */ |
| 4523 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4524 | } |
| 4525 | } |
| 4526 | |
| 4527 | /* Could be an else here to try cmd.exe / command.com in the path |
| 4528 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4529 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4530 | PyErr_SetString(PyExc_RuntimeError, |
| 4531 | "Cannot locate a COMSPEC environment variable to " |
| 4532 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4533 | return FALSE; |
| 4534 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4535 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4536 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 4537 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 4538 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 4539 | siStartInfo.hStdInput = hStdin; |
| 4540 | siStartInfo.hStdOutput = hStdout; |
| 4541 | siStartInfo.hStdError = hStderr; |
| 4542 | siStartInfo.wShowWindow = SW_HIDE; |
| 4543 | |
| 4544 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4545 | s2, |
| 4546 | NULL, |
| 4547 | NULL, |
| 4548 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4549 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4550 | NULL, |
| 4551 | NULL, |
| 4552 | &siStartInfo, |
| 4553 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4554 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4555 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4556 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4557 | /* Return process handle */ |
| 4558 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4559 | return TRUE; |
| 4560 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4561 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4562 | return FALSE; |
| 4563 | } |
| 4564 | |
| 4565 | /* The following code is based off of KB: Q190351 */ |
| 4566 | |
| 4567 | static PyObject * |
| 4568 | _PyPopen(char *cmdstring, int mode, int n) |
| 4569 | { |
| 4570 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 4571 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4572 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4573 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4574 | SECURITY_ATTRIBUTES saAttr; |
| 4575 | BOOL fSuccess; |
| 4576 | int fd1, fd2, fd3; |
| 4577 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4578 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4579 | PyObject *f; |
| 4580 | |
| 4581 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 4582 | saAttr.bInheritHandle = TRUE; |
| 4583 | saAttr.lpSecurityDescriptor = NULL; |
| 4584 | |
| 4585 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 4586 | return win32_error("CreatePipe", NULL); |
| 4587 | |
| 4588 | /* Create new output read handle and the input write handle. Set |
| 4589 | * the inheritance properties to FALSE. Otherwise, the child inherits |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 4590 | * these handles; resulting in non-closeable handles to the pipes |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4591 | * being created. */ |
| 4592 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4593 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 4594 | FALSE, |
| 4595 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4596 | if (!fSuccess) |
| 4597 | return win32_error("DuplicateHandle", NULL); |
| 4598 | |
| 4599 | /* Close the inheritable version of ChildStdin |
| 4600 | that we're using. */ |
| 4601 | CloseHandle(hChildStdinWr); |
| 4602 | |
| 4603 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 4604 | return win32_error("CreatePipe", NULL); |
| 4605 | |
| 4606 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4607 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 4608 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4609 | if (!fSuccess) |
| 4610 | return win32_error("DuplicateHandle", NULL); |
| 4611 | |
| 4612 | /* Close the inheritable version of ChildStdout |
| 4613 | that we're using. */ |
| 4614 | CloseHandle(hChildStdoutRd); |
| 4615 | |
| 4616 | if (n != POPEN_4) { |
| 4617 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 4618 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4619 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 4620 | hChildStderrRd, |
| 4621 | GetCurrentProcess(), |
| 4622 | &hChildStderrRdDup, 0, |
| 4623 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4624 | if (!fSuccess) |
| 4625 | return win32_error("DuplicateHandle", NULL); |
| 4626 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 4627 | CloseHandle(hChildStderrRd); |
| 4628 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4629 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4630 | switch (n) { |
| 4631 | case POPEN_1: |
| 4632 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 4633 | case _O_WRONLY | _O_TEXT: |
| 4634 | /* Case for writing to child Stdin in text mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4635 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4636 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4637 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4638 | PyFile_SetBufSize(f, 0); |
| 4639 | /* We don't care about these pipes anymore, so close them. */ |
| 4640 | CloseHandle(hChildStdoutRdDup); |
| 4641 | CloseHandle(hChildStderrRdDup); |
| 4642 | break; |
| 4643 | |
| 4644 | case _O_RDONLY | _O_TEXT: |
| 4645 | /* Case for reading from child Stdout in text mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4646 | fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4647 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4648 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4649 | PyFile_SetBufSize(f, 0); |
| 4650 | /* We don't care about these pipes anymore, so close them. */ |
| 4651 | CloseHandle(hChildStdinWrDup); |
| 4652 | CloseHandle(hChildStderrRdDup); |
| 4653 | break; |
| 4654 | |
| 4655 | case _O_RDONLY | _O_BINARY: |
| 4656 | /* Case for readinig from child Stdout in binary mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4657 | fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4658 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4659 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4660 | PyFile_SetBufSize(f, 0); |
| 4661 | /* We don't care about these pipes anymore, so close them. */ |
| 4662 | CloseHandle(hChildStdinWrDup); |
| 4663 | CloseHandle(hChildStderrRdDup); |
| 4664 | break; |
| 4665 | |
| 4666 | case _O_WRONLY | _O_BINARY: |
| 4667 | /* Case for writing to child Stdin in binary mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4668 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4669 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4670 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4671 | PyFile_SetBufSize(f, 0); |
| 4672 | /* We don't care about these pipes anymore, so close them. */ |
| 4673 | CloseHandle(hChildStdoutRdDup); |
| 4674 | CloseHandle(hChildStderrRdDup); |
| 4675 | break; |
| 4676 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4677 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4678 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4679 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4680 | case POPEN_2: |
| 4681 | case POPEN_4: |
| 4682 | { |
| 4683 | char *m1, *m2; |
| 4684 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4685 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4686 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4687 | m1 = "r"; |
| 4688 | m2 = "w"; |
| 4689 | } else { |
| 4690 | m1 = "rb"; |
| 4691 | m2 = "wb"; |
| 4692 | } |
| 4693 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4694 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4695 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4696 | fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4697 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4698 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4699 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4700 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4701 | PyFile_SetBufSize(p2, 0); |
| 4702 | |
| 4703 | if (n != 4) |
| 4704 | CloseHandle(hChildStderrRdDup); |
| 4705 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4706 | f = PyTuple_Pack(2,p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4707 | Py_XDECREF(p1); |
| 4708 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4709 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4710 | break; |
| 4711 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4712 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4713 | case POPEN_3: |
| 4714 | { |
| 4715 | char *m1, *m2; |
| 4716 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4717 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4718 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4719 | m1 = "r"; |
| 4720 | m2 = "w"; |
| 4721 | } else { |
| 4722 | m1 = "rb"; |
| 4723 | m2 = "wb"; |
| 4724 | } |
| 4725 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4726 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4727 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4728 | fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4729 | f2 = _fdopen(fd2, m1); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4730 | fd3 = _open_osfhandle((intptr_t)hChildStderrRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4731 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4732 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4733 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 4734 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4735 | PyFile_SetBufSize(p1, 0); |
| 4736 | PyFile_SetBufSize(p2, 0); |
| 4737 | PyFile_SetBufSize(p3, 0); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4738 | f = PyTuple_Pack(3,p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4739 | Py_XDECREF(p1); |
| 4740 | Py_XDECREF(p2); |
| 4741 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4742 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4743 | break; |
| 4744 | } |
| 4745 | } |
| 4746 | |
| 4747 | if (n == POPEN_4) { |
| 4748 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4749 | hChildStdinRd, |
| 4750 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4751 | hChildStdoutWr, |
| 4752 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4753 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4754 | } |
| 4755 | else { |
| 4756 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4757 | hChildStdinRd, |
| 4758 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4759 | hChildStderrWr, |
| 4760 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4761 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4762 | } |
| 4763 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4764 | /* |
| 4765 | * Insert the files we've created into the process dictionary |
| 4766 | * all referencing the list with the process handle and the |
| 4767 | * initial number of files (see description below in _PyPclose). |
| 4768 | * Since if _PyPclose later tried to wait on a process when all |
| 4769 | * handles weren't closed, it could create a deadlock with the |
| 4770 | * child, we spend some energy here to try to ensure that we |
| 4771 | * either insert all file handles into the dictionary or none |
| 4772 | * at all. It's a little clumsy with the various popen modes |
| 4773 | * and variable number of files involved. |
| 4774 | */ |
| 4775 | if (!_PyPopenProcs) { |
| 4776 | _PyPopenProcs = PyDict_New(); |
| 4777 | } |
| 4778 | |
| 4779 | if (_PyPopenProcs) { |
| 4780 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 4781 | int ins_rc[3]; |
| 4782 | |
| 4783 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4784 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4785 | |
| 4786 | procObj = PyList_New(2); |
| 4787 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 4788 | intObj = PyInt_FromLong(file_count); |
| 4789 | |
| 4790 | if (procObj && hProcessObj && intObj) { |
| 4791 | PyList_SetItem(procObj,0,hProcessObj); |
| 4792 | PyList_SetItem(procObj,1,intObj); |
| 4793 | |
| 4794 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 4795 | if (fileObj[0]) { |
| 4796 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4797 | fileObj[0], |
| 4798 | procObj); |
| 4799 | } |
| 4800 | if (file_count >= 2) { |
| 4801 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 4802 | if (fileObj[1]) { |
| 4803 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4804 | fileObj[1], |
| 4805 | procObj); |
| 4806 | } |
| 4807 | } |
| 4808 | if (file_count >= 3) { |
| 4809 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 4810 | if (fileObj[2]) { |
| 4811 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4812 | fileObj[2], |
| 4813 | procObj); |
| 4814 | } |
| 4815 | } |
| 4816 | |
| 4817 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4818 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4819 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 4820 | /* Something failed - remove any dictionary |
| 4821 | * entries that did make it. |
| 4822 | */ |
| 4823 | if (!ins_rc[0] && fileObj[0]) { |
| 4824 | PyDict_DelItem(_PyPopenProcs, |
| 4825 | fileObj[0]); |
| 4826 | } |
| 4827 | if (!ins_rc[1] && fileObj[1]) { |
| 4828 | PyDict_DelItem(_PyPopenProcs, |
| 4829 | fileObj[1]); |
| 4830 | } |
| 4831 | if (!ins_rc[2] && fileObj[2]) { |
| 4832 | PyDict_DelItem(_PyPopenProcs, |
| 4833 | fileObj[2]); |
| 4834 | } |
| 4835 | } |
| 4836 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4837 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4838 | /* |
| 4839 | * Clean up our localized references for the dictionary keys |
| 4840 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4841 | * that got placed in the dictionary. |
| 4842 | */ |
| 4843 | Py_XDECREF(procObj); |
| 4844 | Py_XDECREF(fileObj[0]); |
| 4845 | Py_XDECREF(fileObj[1]); |
| 4846 | Py_XDECREF(fileObj[2]); |
| 4847 | } |
| 4848 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4849 | /* Child is launched. Close the parents copy of those pipe |
| 4850 | * handles that only the child should have open. You need to |
| 4851 | * make sure that no handles to the write end of the output pipe |
| 4852 | * are maintained in this process or else the pipe will not close |
| 4853 | * when the child process exits and the ReadFile will hang. */ |
| 4854 | |
| 4855 | if (!CloseHandle(hChildStdinRd)) |
| 4856 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4857 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4858 | if (!CloseHandle(hChildStdoutWr)) |
| 4859 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4860 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4861 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 4862 | return win32_error("CloseHandle", NULL); |
| 4863 | |
| 4864 | return f; |
| 4865 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4866 | |
| 4867 | /* |
| 4868 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4869 | * 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] | 4870 | * |
| 4871 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4872 | * input file pointer to information about the process that was |
| 4873 | * originally created by the popen* call that created the file pointer. |
| 4874 | * The dictionary uses the file pointer as a key (with one entry |
| 4875 | * inserted for each file returned by the original popen* call) and a |
| 4876 | * single list object as the value for all files from a single call. |
| 4877 | * The list object contains the Win32 process handle at [0], and a file |
| 4878 | * count at [1], which is initialized to the total number of file |
| 4879 | * handles using that list. |
| 4880 | * |
| 4881 | * This function closes whichever handle it is passed, and decrements |
| 4882 | * the file count in the dictionary for the process handle pointed to |
| 4883 | * by this file. On the last close (when the file count reaches zero), |
| 4884 | * this function will wait for the child process and then return its |
| 4885 | * exit code as the result of the close() operation. This permits the |
| 4886 | * files to be closed in any order - it is always the close() of the |
| 4887 | * final handle that will return the exit code. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4888 | * |
| 4889 | * NOTE: This function is currently called with the GIL released. |
| 4890 | * hence we use the GILState API to manage our state. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4891 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4892 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4893 | static int _PyPclose(FILE *file) |
| 4894 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4895 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4896 | DWORD exit_code; |
| 4897 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4898 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 4899 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4900 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4901 | PyGILState_STATE state; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4902 | #endif |
| 4903 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4904 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4905 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4906 | */ |
| 4907 | result = fclose(file); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4908 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4909 | state = PyGILState_Ensure(); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4910 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4911 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4912 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4913 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4914 | fileObj)) != NULL && |
| 4915 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 4916 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 4917 | |
| 4918 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 4919 | file_count = PyInt_AsLong(intObj); |
| 4920 | |
| 4921 | if (file_count > 1) { |
| 4922 | /* Still other files referencing process */ |
| 4923 | file_count--; |
| 4924 | PyList_SetItem(procObj,1, |
| 4925 | PyInt_FromLong(file_count)); |
| 4926 | } else { |
| 4927 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4928 | if (result != EOF && |
| 4929 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 4930 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4931 | /* Possible truncation here in 16-bit environments, but |
| 4932 | * real exit codes are just the lower byte in any event. |
| 4933 | */ |
| 4934 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4935 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4936 | /* Indicate failure - this will cause the file object |
| 4937 | * to raise an I/O error and translate the last Win32 |
| 4938 | * error code from errno. We do have a problem with |
| 4939 | * last errors that overlap the normal errno table, |
| 4940 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4941 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4942 | if (result != EOF) { |
| 4943 | /* If the error wasn't from the fclose(), then |
| 4944 | * set errno for the file object error handling. |
| 4945 | */ |
| 4946 | errno = GetLastError(); |
| 4947 | } |
| 4948 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4949 | } |
| 4950 | |
| 4951 | /* Free up the native handle at this point */ |
| 4952 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4953 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4954 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4955 | /* Remove this file pointer from dictionary */ |
| 4956 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4957 | |
| 4958 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 4959 | Py_DECREF(_PyPopenProcs); |
| 4960 | _PyPopenProcs = NULL; |
| 4961 | } |
| 4962 | |
| 4963 | } /* if object retrieval ok */ |
| 4964 | |
| 4965 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4966 | } /* if _PyPopenProcs */ |
| 4967 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4968 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4969 | PyGILState_Release(state); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4970 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4971 | return result; |
| 4972 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4973 | |
| 4974 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4975 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4976 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4977 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4978 | char *name; |
| 4979 | char *mode = "r"; |
| 4980 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4981 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4982 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4983 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4984 | return NULL; |
Martin v. Löwis | 9ad853b | 2003-10-31 10:01:53 +0000 | [diff] [blame] | 4985 | /* Strip mode of binary or text modifiers */ |
| 4986 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) |
| 4987 | mode = "r"; |
| 4988 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) |
| 4989 | mode = "w"; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4990 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4991 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4992 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4993 | if (fp == NULL) |
| 4994 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4995 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4996 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4997 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4998 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4999 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5000 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5001 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5002 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5003 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5004 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5005 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5006 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5007 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5008 | Set the current process's user id."); |
| 5009 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5010 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5011 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5012 | { |
| 5013 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5014 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5015 | return NULL; |
| 5016 | if (setuid(uid) < 0) |
| 5017 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5018 | Py_INCREF(Py_None); |
| 5019 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5020 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5021 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5022 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5023 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5024 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5025 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5026 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5027 | Set the current process's effective user id."); |
| 5028 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5029 | static PyObject * |
| 5030 | posix_seteuid (PyObject *self, PyObject *args) |
| 5031 | { |
| 5032 | int euid; |
| 5033 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 5034 | return NULL; |
| 5035 | } else if (seteuid(euid) < 0) { |
| 5036 | return posix_error(); |
| 5037 | } else { |
| 5038 | Py_INCREF(Py_None); |
| 5039 | return Py_None; |
| 5040 | } |
| 5041 | } |
| 5042 | #endif /* HAVE_SETEUID */ |
| 5043 | |
| 5044 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5045 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5046 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5047 | Set the current process's effective group id."); |
| 5048 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5049 | static PyObject * |
| 5050 | posix_setegid (PyObject *self, PyObject *args) |
| 5051 | { |
| 5052 | int egid; |
| 5053 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 5054 | return NULL; |
| 5055 | } else if (setegid(egid) < 0) { |
| 5056 | return posix_error(); |
| 5057 | } else { |
| 5058 | Py_INCREF(Py_None); |
| 5059 | return Py_None; |
| 5060 | } |
| 5061 | } |
| 5062 | #endif /* HAVE_SETEGID */ |
| 5063 | |
| 5064 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5065 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5066 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5067 | Set the current process's real and effective user ids."); |
| 5068 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5069 | static PyObject * |
| 5070 | posix_setreuid (PyObject *self, PyObject *args) |
| 5071 | { |
| 5072 | int ruid, euid; |
| 5073 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 5074 | return NULL; |
| 5075 | } else if (setreuid(ruid, euid) < 0) { |
| 5076 | return posix_error(); |
| 5077 | } else { |
| 5078 | Py_INCREF(Py_None); |
| 5079 | return Py_None; |
| 5080 | } |
| 5081 | } |
| 5082 | #endif /* HAVE_SETREUID */ |
| 5083 | |
| 5084 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5085 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5086 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5087 | Set the current process's real and effective group ids."); |
| 5088 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5089 | static PyObject * |
| 5090 | posix_setregid (PyObject *self, PyObject *args) |
| 5091 | { |
| 5092 | int rgid, egid; |
| 5093 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 5094 | return NULL; |
| 5095 | } else if (setregid(rgid, egid) < 0) { |
| 5096 | return posix_error(); |
| 5097 | } else { |
| 5098 | Py_INCREF(Py_None); |
| 5099 | return Py_None; |
| 5100 | } |
| 5101 | } |
| 5102 | #endif /* HAVE_SETREGID */ |
| 5103 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5104 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5105 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5106 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5107 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5108 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5109 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5110 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5111 | { |
| 5112 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5113 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5114 | return NULL; |
| 5115 | if (setgid(gid) < 0) |
| 5116 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5117 | Py_INCREF(Py_None); |
| 5118 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5119 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5120 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5121 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5122 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5123 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5124 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5125 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5126 | |
| 5127 | static PyObject * |
| 5128 | posix_setgroups(PyObject *self, PyObject *args) |
| 5129 | { |
| 5130 | PyObject *groups; |
| 5131 | int i, len; |
| 5132 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5133 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5134 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 5135 | return NULL; |
| 5136 | if (!PySequence_Check(groups)) { |
| 5137 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 5138 | return NULL; |
| 5139 | } |
| 5140 | len = PySequence_Size(groups); |
| 5141 | if (len > MAX_GROUPS) { |
| 5142 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 5143 | return NULL; |
| 5144 | } |
| 5145 | for(i = 0; i < len; i++) { |
| 5146 | PyObject *elem; |
| 5147 | elem = PySequence_GetItem(groups, i); |
| 5148 | if (!elem) |
| 5149 | return NULL; |
| 5150 | if (!PyInt_Check(elem)) { |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 5151 | if (!PyLong_Check(elem)) { |
| 5152 | PyErr_SetString(PyExc_TypeError, |
| 5153 | "groups must be integers"); |
| 5154 | Py_DECREF(elem); |
| 5155 | return NULL; |
| 5156 | } else { |
| 5157 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 5158 | if (PyErr_Occurred()) { |
| 5159 | PyErr_SetString(PyExc_TypeError, |
| 5160 | "group id too big"); |
| 5161 | Py_DECREF(elem); |
| 5162 | return NULL; |
| 5163 | } |
| 5164 | grouplist[i] = x; |
| 5165 | /* read back the value to see if it fitted in gid_t */ |
| 5166 | if (grouplist[i] != x) { |
| 5167 | PyErr_SetString(PyExc_TypeError, |
| 5168 | "group id too big"); |
| 5169 | Py_DECREF(elem); |
| 5170 | return NULL; |
| 5171 | } |
| 5172 | } |
| 5173 | } else { |
| 5174 | long x = PyInt_AsLong(elem); |
| 5175 | grouplist[i] = x; |
| 5176 | if (grouplist[i] != x) { |
| 5177 | PyErr_SetString(PyExc_TypeError, |
| 5178 | "group id too big"); |
| 5179 | Py_DECREF(elem); |
| 5180 | return NULL; |
| 5181 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5182 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5183 | Py_DECREF(elem); |
| 5184 | } |
| 5185 | |
| 5186 | if (setgroups(len, grouplist) < 0) |
| 5187 | return posix_error(); |
| 5188 | Py_INCREF(Py_None); |
| 5189 | return Py_None; |
| 5190 | } |
| 5191 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5192 | |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 5193 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5194 | static PyObject * |
| 5195 | wait_helper(int pid, int status, struct rusage *ru) |
| 5196 | { |
| 5197 | PyObject *result; |
| 5198 | static PyObject *struct_rusage; |
| 5199 | |
| 5200 | if (pid == -1) |
| 5201 | return posix_error(); |
| 5202 | |
| 5203 | if (struct_rusage == NULL) { |
| 5204 | PyObject *m = PyImport_ImportModule("resource"); |
| 5205 | if (m == NULL) |
| 5206 | return NULL; |
| 5207 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 5208 | Py_DECREF(m); |
| 5209 | if (struct_rusage == NULL) |
| 5210 | return NULL; |
| 5211 | } |
| 5212 | |
| 5213 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 5214 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 5215 | if (!result) |
| 5216 | return NULL; |
| 5217 | |
| 5218 | #ifndef doubletime |
| 5219 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 5220 | #endif |
| 5221 | |
| 5222 | PyStructSequence_SET_ITEM(result, 0, |
| 5223 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 5224 | PyStructSequence_SET_ITEM(result, 1, |
| 5225 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 5226 | #define SET_INT(result, index, value)\ |
| 5227 | PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value)) |
| 5228 | SET_INT(result, 2, ru->ru_maxrss); |
| 5229 | SET_INT(result, 3, ru->ru_ixrss); |
| 5230 | SET_INT(result, 4, ru->ru_idrss); |
| 5231 | SET_INT(result, 5, ru->ru_isrss); |
| 5232 | SET_INT(result, 6, ru->ru_minflt); |
| 5233 | SET_INT(result, 7, ru->ru_majflt); |
| 5234 | SET_INT(result, 8, ru->ru_nswap); |
| 5235 | SET_INT(result, 9, ru->ru_inblock); |
| 5236 | SET_INT(result, 10, ru->ru_oublock); |
| 5237 | SET_INT(result, 11, ru->ru_msgsnd); |
| 5238 | SET_INT(result, 12, ru->ru_msgrcv); |
| 5239 | SET_INT(result, 13, ru->ru_nsignals); |
| 5240 | SET_INT(result, 14, ru->ru_nvcsw); |
| 5241 | SET_INT(result, 15, ru->ru_nivcsw); |
| 5242 | #undef SET_INT |
| 5243 | |
| 5244 | if (PyErr_Occurred()) { |
| 5245 | Py_DECREF(result); |
| 5246 | return NULL; |
| 5247 | } |
| 5248 | |
Neal Norwitz | 9b00a56 | 2006-03-20 08:47:12 +0000 | [diff] [blame] | 5249 | return Py_BuildValue("iiN", pid, status, result); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5250 | } |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 5251 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5252 | |
| 5253 | #ifdef HAVE_WAIT3 |
| 5254 | PyDoc_STRVAR(posix_wait3__doc__, |
| 5255 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 5256 | Wait for completion of a child process."); |
| 5257 | |
| 5258 | static PyObject * |
| 5259 | posix_wait3(PyObject *self, PyObject *args) |
| 5260 | { |
| 5261 | int pid, options; |
| 5262 | struct rusage ru; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5263 | WAIT_TYPE status; |
| 5264 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5265 | |
| 5266 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 5267 | return NULL; |
| 5268 | |
| 5269 | Py_BEGIN_ALLOW_THREADS |
| 5270 | pid = wait3(&status, options, &ru); |
| 5271 | Py_END_ALLOW_THREADS |
| 5272 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5273 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5274 | } |
| 5275 | #endif /* HAVE_WAIT3 */ |
| 5276 | |
| 5277 | #ifdef HAVE_WAIT4 |
| 5278 | PyDoc_STRVAR(posix_wait4__doc__, |
| 5279 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 5280 | Wait for completion of a given child process."); |
| 5281 | |
| 5282 | static PyObject * |
| 5283 | posix_wait4(PyObject *self, PyObject *args) |
| 5284 | { |
| 5285 | int pid, options; |
| 5286 | struct rusage ru; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5287 | WAIT_TYPE status; |
| 5288 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5289 | |
| 5290 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 5291 | return NULL; |
| 5292 | |
| 5293 | Py_BEGIN_ALLOW_THREADS |
| 5294 | pid = wait4(pid, &status, options, &ru); |
| 5295 | Py_END_ALLOW_THREADS |
| 5296 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5297 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5298 | } |
| 5299 | #endif /* HAVE_WAIT4 */ |
| 5300 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5301 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5302 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5303 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5304 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5305 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5306 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5307 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5308 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5309 | int pid, options; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5310 | WAIT_TYPE status; |
| 5311 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5312 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5313 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5314 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5315 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 5316 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5317 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5318 | if (pid == -1) |
| 5319 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5320 | |
| 5321 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5322 | } |
| 5323 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5324 | #elif defined(HAVE_CWAIT) |
| 5325 | |
| 5326 | /* 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] | 5327 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5328 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5329 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5330 | |
| 5331 | static PyObject * |
| 5332 | posix_waitpid(PyObject *self, PyObject *args) |
| 5333 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5334 | intptr_t pid; |
| 5335 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5336 | |
| 5337 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 5338 | return NULL; |
| 5339 | Py_BEGIN_ALLOW_THREADS |
| 5340 | pid = _cwait(&status, pid, options); |
| 5341 | Py_END_ALLOW_THREADS |
| 5342 | if (pid == -1) |
| 5343 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5344 | |
| 5345 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 5346 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5347 | } |
| 5348 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5349 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5350 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5351 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5352 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5353 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5354 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5355 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5356 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5357 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5358 | int pid; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5359 | WAIT_TYPE status; |
| 5360 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5361 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5362 | Py_BEGIN_ALLOW_THREADS |
| 5363 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5364 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5365 | if (pid == -1) |
| 5366 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5367 | |
| 5368 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5369 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5370 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5371 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5372 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5373 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5374 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5375 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5376 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5377 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5378 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5379 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5380 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5381 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5382 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5383 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5384 | 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] | 5385 | #else |
| 5386 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 5387 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5388 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5389 | } |
| 5390 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5391 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5392 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5393 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5394 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5395 | 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] | 5396 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5397 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5398 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5399 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5400 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 5401 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5402 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5403 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5404 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5405 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 5406 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5407 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5408 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 5409 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5410 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5411 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5412 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5413 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5414 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5415 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5416 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5417 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 5418 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5419 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5420 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5421 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5422 | { |
Martin v. Löwis | 4fc2bda | 2006-05-04 12:04:27 +0000 | [diff] [blame^] | 5423 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5424 | } |
| 5425 | #endif /* HAVE_SYMLINK */ |
| 5426 | |
| 5427 | |
| 5428 | #ifdef HAVE_TIMES |
| 5429 | #ifndef HZ |
| 5430 | #define HZ 60 /* Universal constant :-) */ |
| 5431 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5432 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5433 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 5434 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5435 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5436 | { |
| 5437 | ULONG value = 0; |
| 5438 | |
| 5439 | Py_BEGIN_ALLOW_THREADS |
| 5440 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 5441 | Py_END_ALLOW_THREADS |
| 5442 | |
| 5443 | return value; |
| 5444 | } |
| 5445 | |
| 5446 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5447 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5448 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5449 | /* Currently Only Uptime is Provided -- Others Later */ |
| 5450 | return Py_BuildValue("ddddd", |
| 5451 | (double)0 /* t.tms_utime / HZ */, |
| 5452 | (double)0 /* t.tms_stime / HZ */, |
| 5453 | (double)0 /* t.tms_cutime / HZ */, |
| 5454 | (double)0 /* t.tms_cstime / HZ */, |
| 5455 | (double)system_uptime() / 1000); |
| 5456 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5457 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5458 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5459 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5460 | { |
| 5461 | struct tms t; |
| 5462 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5463 | errno = 0; |
| 5464 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5465 | if (c == (clock_t) -1) |
| 5466 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5467 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5468 | (double)t.tms_utime / HZ, |
| 5469 | (double)t.tms_stime / HZ, |
| 5470 | (double)t.tms_cutime / HZ, |
| 5471 | (double)t.tms_cstime / HZ, |
| 5472 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5473 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5474 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5475 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5476 | |
| 5477 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5478 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5479 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5480 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5481 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5482 | { |
| 5483 | FILETIME create, exit, kernel, user; |
| 5484 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5485 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5486 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 5487 | /* The fields of a FILETIME structure are the hi and lo part |
| 5488 | of a 64-bit value expressed in 100 nanosecond units. |
| 5489 | 1e7 is one second in such units; 1e-7 the inverse. |
| 5490 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 5491 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5492 | return Py_BuildValue( |
| 5493 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5494 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 5495 | kernel.dwLowDateTime*1e-7), |
| 5496 | (double)(user.dwHighDateTime*429.4967296 + |
| 5497 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5498 | (double)0, |
| 5499 | (double)0, |
| 5500 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5501 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5502 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5503 | |
| 5504 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5505 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5506 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5507 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5508 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5509 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5510 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5511 | #ifdef HAVE_GETSID |
| 5512 | PyDoc_STRVAR(posix_getsid__doc__, |
| 5513 | "getsid(pid) -> sid\n\n\ |
| 5514 | Call the system call getsid()."); |
| 5515 | |
| 5516 | static PyObject * |
| 5517 | posix_getsid(PyObject *self, PyObject *args) |
| 5518 | { |
| 5519 | int pid, sid; |
| 5520 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 5521 | return NULL; |
| 5522 | sid = getsid(pid); |
| 5523 | if (sid < 0) |
| 5524 | return posix_error(); |
| 5525 | return PyInt_FromLong((long)sid); |
| 5526 | } |
| 5527 | #endif /* HAVE_GETSID */ |
| 5528 | |
| 5529 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5530 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5531 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5532 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5533 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5534 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5535 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5536 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5537 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5538 | if (setsid() < 0) |
| 5539 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5540 | Py_INCREF(Py_None); |
| 5541 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5542 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5543 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5544 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5545 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5546 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5547 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5548 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5549 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5550 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5551 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5552 | { |
| 5553 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5554 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5555 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5556 | if (setpgid(pid, pgrp) < 0) |
| 5557 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5558 | Py_INCREF(Py_None); |
| 5559 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5560 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5561 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5562 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5563 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5564 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5565 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5566 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5567 | 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] | 5568 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5569 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5570 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5571 | { |
| 5572 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5573 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5574 | return NULL; |
| 5575 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5576 | if (pgid < 0) |
| 5577 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5578 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5579 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5580 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5581 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5582 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5583 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5584 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5585 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5586 | 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] | 5587 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5588 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5589 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5590 | { |
| 5591 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5592 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5593 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5594 | if (tcsetpgrp(fd, pgid) < 0) |
| 5595 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5596 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5597 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5598 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5599 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5600 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5601 | /* Functions acting on file descriptors */ |
| 5602 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5603 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5604 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5605 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5606 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5607 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5608 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5609 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5610 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5611 | int flag; |
| 5612 | int mode = 0777; |
| 5613 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5614 | |
| 5615 | #ifdef MS_WINDOWS |
| 5616 | if (unicode_file_names()) { |
| 5617 | PyUnicodeObject *po; |
| 5618 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 5619 | Py_BEGIN_ALLOW_THREADS |
| 5620 | /* PyUnicode_AS_UNICODE OK without thread |
| 5621 | lock as it is a simple dereference. */ |
| 5622 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 5623 | Py_END_ALLOW_THREADS |
| 5624 | if (fd < 0) |
| 5625 | return posix_error(); |
| 5626 | return PyInt_FromLong((long)fd); |
| 5627 | } |
| 5628 | /* Drop the argument parsing error as narrow strings |
| 5629 | are also valid. */ |
| 5630 | PyErr_Clear(); |
| 5631 | } |
| 5632 | #endif |
| 5633 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5634 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5635 | Py_FileSystemDefaultEncoding, &file, |
| 5636 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5637 | return NULL; |
| 5638 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5639 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5640 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5641 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5642 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5643 | return posix_error_with_allocated_filename(file); |
| 5644 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5645 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5646 | } |
| 5647 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5648 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5649 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5650 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5651 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5652 | |
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_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5655 | { |
| 5656 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5657 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5658 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5659 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5660 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5661 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5662 | if (res < 0) |
| 5663 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5664 | Py_INCREF(Py_None); |
| 5665 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5666 | } |
| 5667 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5668 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5669 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5670 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5671 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5672 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5673 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5674 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5675 | { |
| 5676 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5677 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5678 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5679 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5680 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5681 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5682 | if (fd < 0) |
| 5683 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5684 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5685 | } |
| 5686 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5687 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5688 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5689 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5690 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5691 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5692 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5693 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5694 | { |
| 5695 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5696 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5697 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5698 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5699 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5700 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5701 | if (res < 0) |
| 5702 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5703 | Py_INCREF(Py_None); |
| 5704 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5705 | } |
| 5706 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5707 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5708 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5709 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5710 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5711 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5712 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5713 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5714 | { |
| 5715 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5716 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5717 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5718 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5719 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5720 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5721 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5722 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5723 | return NULL; |
| 5724 | #ifdef SEEK_SET |
| 5725 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5726 | switch (how) { |
| 5727 | case 0: how = SEEK_SET; break; |
| 5728 | case 1: how = SEEK_CUR; break; |
| 5729 | case 2: how = SEEK_END; break; |
| 5730 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5731 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5732 | |
| 5733 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5734 | pos = PyInt_AsLong(posobj); |
| 5735 | #else |
| 5736 | pos = PyLong_Check(posobj) ? |
| 5737 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 5738 | #endif |
| 5739 | if (PyErr_Occurred()) |
| 5740 | return NULL; |
| 5741 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5742 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5743 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5744 | res = _lseeki64(fd, pos, how); |
| 5745 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5746 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5747 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5748 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5749 | if (res < 0) |
| 5750 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5751 | |
| 5752 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5753 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5754 | #else |
| 5755 | return PyLong_FromLongLong(res); |
| 5756 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5757 | } |
| 5758 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5759 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5760 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5761 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5762 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5763 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5764 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5765 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5766 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5767 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5768 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5769 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5770 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 5771 | if (size < 0) { |
| 5772 | errno = EINVAL; |
| 5773 | return posix_error(); |
| 5774 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5775 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5776 | if (buffer == NULL) |
| 5777 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5778 | Py_BEGIN_ALLOW_THREADS |
| 5779 | n = read(fd, PyString_AsString(buffer), size); |
| 5780 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5781 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5782 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5783 | return posix_error(); |
| 5784 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5785 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5786 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5787 | return buffer; |
| 5788 | } |
| 5789 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5790 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5791 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5792 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5793 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5794 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5795 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5796 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5797 | { |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5798 | int fd; |
| 5799 | Py_ssize_t size; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5800 | char *buffer; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5801 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5802 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5803 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5804 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5805 | size = write(fd, buffer, (size_t)size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5806 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5807 | if (size < 0) |
| 5808 | return posix_error(); |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5809 | return PyInt_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5810 | } |
| 5811 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5812 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5813 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5814 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5815 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5816 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5817 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5818 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5819 | { |
| 5820 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5821 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5822 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5823 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5824 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5825 | #ifdef __VMS |
| 5826 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5827 | fsync(fd); |
| 5828 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5829 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5830 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5831 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5832 | if (res != 0) { |
| 5833 | #ifdef MS_WINDOWS |
| 5834 | return win32_error("fstat", NULL); |
| 5835 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5836 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5837 | #endif |
| 5838 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5839 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5840 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5841 | } |
| 5842 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5843 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5844 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5845 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5846 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5847 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5848 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5849 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5850 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5851 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5852 | char *mode = "r"; |
| 5853 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5854 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5855 | PyObject *f; |
| 5856 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5857 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5858 | |
Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 5859 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 5860 | PyErr_Format(PyExc_ValueError, |
| 5861 | "invalid file mode '%s'", mode); |
| 5862 | return NULL; |
| 5863 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5864 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 5865 | #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H) |
Georg Brandl | 54a188a | 2006-03-31 20:00:11 +0000 | [diff] [blame] | 5866 | if (mode[0] == 'a') { |
| 5867 | /* try to make sure the O_APPEND flag is set */ |
| 5868 | int flags; |
| 5869 | flags = fcntl(fd, F_GETFL); |
| 5870 | if (flags != -1) |
| 5871 | fcntl(fd, F_SETFL, flags | O_APPEND); |
| 5872 | fp = fdopen(fd, mode); |
Thomas Wouters | 2a9a6b0 | 2006-03-31 22:38:19 +0000 | [diff] [blame] | 5873 | if (fp == NULL && flags != -1) |
Georg Brandl | 54a188a | 2006-03-31 20:00:11 +0000 | [diff] [blame] | 5874 | /* restore old mode if fdopen failed */ |
| 5875 | fcntl(fd, F_SETFL, flags); |
| 5876 | } else { |
| 5877 | fp = fdopen(fd, mode); |
| 5878 | } |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 5879 | #else |
| 5880 | fp = fdopen(fd, mode); |
| 5881 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5882 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5883 | if (fp == NULL) |
| 5884 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 5885 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5886 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5887 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5888 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5889 | } |
| 5890 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5891 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5892 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5893 | 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] | 5894 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5895 | |
| 5896 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5897 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5898 | { |
| 5899 | int fd; |
| 5900 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5901 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5902 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5903 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5904 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5905 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5906 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5907 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5908 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5909 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5910 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5911 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5912 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5913 | #if defined(PYOS_OS2) |
| 5914 | HFILE read, write; |
| 5915 | APIRET rc; |
| 5916 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5917 | Py_BEGIN_ALLOW_THREADS |
| 5918 | rc = DosCreatePipe( &read, &write, 4096); |
| 5919 | Py_END_ALLOW_THREADS |
| 5920 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5921 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5922 | |
| 5923 | return Py_BuildValue("(ii)", read, write); |
| 5924 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5925 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5926 | int fds[2]; |
| 5927 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5928 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5929 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5930 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5931 | if (res != 0) |
| 5932 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5933 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5934 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5935 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5936 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5937 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5938 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5939 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5940 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5941 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5942 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5943 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5944 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5945 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5946 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5947 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5948 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5949 | #endif /* HAVE_PIPE */ |
| 5950 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5951 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5952 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5953 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5954 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5955 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5956 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5957 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5958 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5959 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5960 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5961 | int mode = 0666; |
| 5962 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5963 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5964 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5965 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5966 | res = mkfifo(filename, mode); |
| 5967 | Py_END_ALLOW_THREADS |
| 5968 | if (res < 0) |
| 5969 | return posix_error(); |
| 5970 | Py_INCREF(Py_None); |
| 5971 | return Py_None; |
| 5972 | } |
| 5973 | #endif |
| 5974 | |
| 5975 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5976 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5977 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5978 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5979 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5980 | named filename. mode specifies both the permissions to use and the\n\ |
| 5981 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5982 | 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] | 5983 | device defines the newly created device special file (probably using\n\ |
| 5984 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5985 | |
| 5986 | |
| 5987 | static PyObject * |
| 5988 | posix_mknod(PyObject *self, PyObject *args) |
| 5989 | { |
| 5990 | char *filename; |
| 5991 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5992 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5993 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5994 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5995 | return NULL; |
| 5996 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5997 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5998 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5999 | if (res < 0) |
| 6000 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6001 | Py_INCREF(Py_None); |
| 6002 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6003 | } |
| 6004 | #endif |
| 6005 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6006 | #ifdef HAVE_DEVICE_MACROS |
| 6007 | PyDoc_STRVAR(posix_major__doc__, |
| 6008 | "major(device) -> major number\n\ |
| 6009 | Extracts a device major number from a raw device number."); |
| 6010 | |
| 6011 | static PyObject * |
| 6012 | posix_major(PyObject *self, PyObject *args) |
| 6013 | { |
| 6014 | int device; |
| 6015 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 6016 | return NULL; |
| 6017 | return PyInt_FromLong((long)major(device)); |
| 6018 | } |
| 6019 | |
| 6020 | PyDoc_STRVAR(posix_minor__doc__, |
| 6021 | "minor(device) -> minor number\n\ |
| 6022 | Extracts a device minor number from a raw device number."); |
| 6023 | |
| 6024 | static PyObject * |
| 6025 | posix_minor(PyObject *self, PyObject *args) |
| 6026 | { |
| 6027 | int device; |
| 6028 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 6029 | return NULL; |
| 6030 | return PyInt_FromLong((long)minor(device)); |
| 6031 | } |
| 6032 | |
| 6033 | PyDoc_STRVAR(posix_makedev__doc__, |
| 6034 | "makedev(major, minor) -> device number\n\ |
| 6035 | Composes a raw device number from the major and minor device numbers."); |
| 6036 | |
| 6037 | static PyObject * |
| 6038 | posix_makedev(PyObject *self, PyObject *args) |
| 6039 | { |
| 6040 | int major, minor; |
| 6041 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 6042 | return NULL; |
| 6043 | return PyInt_FromLong((long)makedev(major, minor)); |
| 6044 | } |
| 6045 | #endif /* device macros */ |
| 6046 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6047 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6048 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6049 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6050 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6051 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6052 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6053 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6054 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6055 | { |
| 6056 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6057 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6058 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6059 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6060 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6061 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6062 | return NULL; |
| 6063 | |
| 6064 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6065 | length = PyInt_AsLong(lenobj); |
| 6066 | #else |
| 6067 | length = PyLong_Check(lenobj) ? |
| 6068 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 6069 | #endif |
| 6070 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6071 | return NULL; |
| 6072 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6073 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6074 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6075 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6076 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6077 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6078 | return NULL; |
| 6079 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6080 | Py_INCREF(Py_None); |
| 6081 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6082 | } |
| 6083 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6084 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6085 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6086 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6087 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6088 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6089 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6090 | /* Save putenv() parameters as values here, so we can collect them when they |
| 6091 | * get re-set with another call for the same key. */ |
| 6092 | static PyObject *posix_putenv_garbage; |
| 6093 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6094 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6095 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6096 | { |
| 6097 | char *s1, *s2; |
Anthony Baxter | 64182fe | 2006-04-11 12:14:09 +0000 | [diff] [blame] | 6098 | char *newenv; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6099 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 6100 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6101 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6102 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6103 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6104 | |
| 6105 | #if defined(PYOS_OS2) |
| 6106 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 6107 | APIRET rc; |
| 6108 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6109 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 6110 | if (rc != NO_ERROR) |
| 6111 | return os2_error(rc); |
| 6112 | |
| 6113 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 6114 | APIRET rc; |
| 6115 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6116 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 6117 | if (rc != NO_ERROR) |
| 6118 | return os2_error(rc); |
| 6119 | } else { |
| 6120 | #endif |
| 6121 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6122 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 6123 | len = strlen(s1) + strlen(s2) + 2; |
| 6124 | /* len includes space for a trailing \0; the size arg to |
| 6125 | PyString_FromStringAndSize does not count that */ |
| 6126 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6127 | if (newstr == NULL) |
| 6128 | return PyErr_NoMemory(); |
Anthony Baxter | 64182fe | 2006-04-11 12:14:09 +0000 | [diff] [blame] | 6129 | newenv = PyString_AS_STRING(newstr); |
| 6130 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 6131 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 6132 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6133 | posix_error(); |
| 6134 | return NULL; |
| 6135 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6136 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 6137 | * this will cause previous value to be collected. This has to |
| 6138 | * happen after the real putenv() call because the old value |
| 6139 | * was still accessible until then. */ |
| 6140 | if (PyDict_SetItem(posix_putenv_garbage, |
| 6141 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 6142 | /* really not much we can do; just leak */ |
| 6143 | PyErr_Clear(); |
| 6144 | } |
| 6145 | else { |
| 6146 | Py_DECREF(newstr); |
| 6147 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6148 | |
| 6149 | #if defined(PYOS_OS2) |
| 6150 | } |
| 6151 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6152 | Py_INCREF(Py_None); |
| 6153 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6154 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6155 | #endif /* putenv */ |
| 6156 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6157 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6158 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6159 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6160 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6161 | |
| 6162 | static PyObject * |
| 6163 | posix_unsetenv(PyObject *self, PyObject *args) |
| 6164 | { |
| 6165 | char *s1; |
| 6166 | |
| 6167 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 6168 | return NULL; |
| 6169 | |
| 6170 | unsetenv(s1); |
| 6171 | |
| 6172 | /* Remove the key from posix_putenv_garbage; |
| 6173 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6174 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6175 | * old value was still accessible until then. |
| 6176 | */ |
| 6177 | if (PyDict_DelItem(posix_putenv_garbage, |
| 6178 | PyTuple_GET_ITEM(args, 0))) { |
| 6179 | /* really not much we can do; just leak */ |
| 6180 | PyErr_Clear(); |
| 6181 | } |
| 6182 | |
| 6183 | Py_INCREF(Py_None); |
| 6184 | return Py_None; |
| 6185 | } |
| 6186 | #endif /* unsetenv */ |
| 6187 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6188 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6189 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6190 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6191 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6192 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6193 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6194 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6195 | { |
| 6196 | int code; |
| 6197 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6198 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6199 | return NULL; |
| 6200 | message = strerror(code); |
| 6201 | if (message == NULL) { |
| 6202 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 6203 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6204 | return NULL; |
| 6205 | } |
| 6206 | return PyString_FromString(message); |
| 6207 | } |
| 6208 | #endif /* strerror */ |
| 6209 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6210 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6211 | #ifdef HAVE_SYS_WAIT_H |
| 6212 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6213 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6214 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6215 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6216 | 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] | 6217 | |
| 6218 | static PyObject * |
| 6219 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 6220 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6221 | WAIT_TYPE status; |
| 6222 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6223 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6224 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6225 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6226 | |
| 6227 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6228 | } |
| 6229 | #endif /* WCOREDUMP */ |
| 6230 | |
| 6231 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6232 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6233 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6234 | 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] | 6235 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6236 | |
| 6237 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6238 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6239 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6240 | WAIT_TYPE status; |
| 6241 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6242 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6243 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6244 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6245 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6246 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6247 | } |
| 6248 | #endif /* WIFCONTINUED */ |
| 6249 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6250 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6251 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6252 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6253 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6254 | |
| 6255 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6256 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6257 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6258 | WAIT_TYPE status; |
| 6259 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6260 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6261 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6262 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6263 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6264 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6265 | } |
| 6266 | #endif /* WIFSTOPPED */ |
| 6267 | |
| 6268 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6269 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6270 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6271 | 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] | 6272 | |
| 6273 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6274 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6275 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6276 | WAIT_TYPE status; |
| 6277 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6278 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6279 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6280 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6281 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6282 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6283 | } |
| 6284 | #endif /* WIFSIGNALED */ |
| 6285 | |
| 6286 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6287 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6288 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6289 | 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] | 6290 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6291 | |
| 6292 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6293 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6294 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6295 | WAIT_TYPE status; |
| 6296 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6297 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6298 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6299 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6300 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6301 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6302 | } |
| 6303 | #endif /* WIFEXITED */ |
| 6304 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6305 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6306 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6307 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6308 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6309 | |
| 6310 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6311 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6312 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6313 | WAIT_TYPE status; |
| 6314 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6315 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6316 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6317 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6318 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6319 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 6320 | } |
| 6321 | #endif /* WEXITSTATUS */ |
| 6322 | |
| 6323 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6324 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6325 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6326 | 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] | 6327 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6328 | |
| 6329 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6330 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6331 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6332 | WAIT_TYPE status; |
| 6333 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6334 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6335 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6336 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6337 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6338 | return Py_BuildValue("i", WTERMSIG(status)); |
| 6339 | } |
| 6340 | #endif /* WTERMSIG */ |
| 6341 | |
| 6342 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6343 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6344 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6345 | Return the signal that stopped the process that provided\n\ |
| 6346 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6347 | |
| 6348 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6349 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6350 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6351 | WAIT_TYPE status; |
| 6352 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6353 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6354 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6355 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6356 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6357 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 6358 | } |
| 6359 | #endif /* WSTOPSIG */ |
| 6360 | |
| 6361 | #endif /* HAVE_SYS_WAIT_H */ |
| 6362 | |
| 6363 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6364 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 6365 | #ifdef _SCO_DS |
| 6366 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 6367 | needed definitions in sys/statvfs.h */ |
| 6368 | #define _SVID3 |
| 6369 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6370 | #include <sys/statvfs.h> |
| 6371 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6372 | static PyObject* |
| 6373 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 6374 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 6375 | if (v == NULL) |
| 6376 | return NULL; |
| 6377 | |
| 6378 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6379 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6380 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 6381 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 6382 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 6383 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 6384 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 6385 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 6386 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 6387 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6388 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6389 | #else |
| 6390 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6391 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6392 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6393 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6394 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6395 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6396 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6397 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6398 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6399 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6400 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6401 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6402 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6403 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6404 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6405 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6406 | #endif |
| 6407 | |
| 6408 | return v; |
| 6409 | } |
| 6410 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6411 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6412 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6413 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6414 | |
| 6415 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6416 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6417 | { |
| 6418 | int fd, res; |
| 6419 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6420 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6421 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6422 | return NULL; |
| 6423 | Py_BEGIN_ALLOW_THREADS |
| 6424 | res = fstatvfs(fd, &st); |
| 6425 | Py_END_ALLOW_THREADS |
| 6426 | if (res != 0) |
| 6427 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6428 | |
| 6429 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6430 | } |
| 6431 | #endif /* HAVE_FSTATVFS */ |
| 6432 | |
| 6433 | |
| 6434 | #if defined(HAVE_STATVFS) |
| 6435 | #include <sys/statvfs.h> |
| 6436 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6437 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6438 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6439 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6440 | |
| 6441 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6442 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6443 | { |
| 6444 | char *path; |
| 6445 | int res; |
| 6446 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6447 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6448 | return NULL; |
| 6449 | Py_BEGIN_ALLOW_THREADS |
| 6450 | res = statvfs(path, &st); |
| 6451 | Py_END_ALLOW_THREADS |
| 6452 | if (res != 0) |
| 6453 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6454 | |
| 6455 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6456 | } |
| 6457 | #endif /* HAVE_STATVFS */ |
| 6458 | |
| 6459 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6460 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6461 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6462 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6463 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 6464 | 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] | 6465 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6466 | |
| 6467 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6468 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6469 | { |
| 6470 | PyObject *result = NULL; |
| 6471 | char *dir = NULL; |
| 6472 | char *pfx = NULL; |
| 6473 | char *name; |
| 6474 | |
| 6475 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 6476 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6477 | |
| 6478 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6479 | "tempnam is a potential security risk to your program") < 0) |
| 6480 | return NULL; |
| 6481 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6482 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6483 | name = _tempnam(dir, pfx); |
| 6484 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6485 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6486 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6487 | if (name == NULL) |
| 6488 | return PyErr_NoMemory(); |
| 6489 | result = PyString_FromString(name); |
| 6490 | free(name); |
| 6491 | return result; |
| 6492 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 6493 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6494 | |
| 6495 | |
| 6496 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6497 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6498 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6499 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6500 | |
| 6501 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6502 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6503 | { |
| 6504 | FILE *fp; |
| 6505 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6506 | fp = tmpfile(); |
| 6507 | if (fp == NULL) |
| 6508 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 6509 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6510 | } |
| 6511 | #endif |
| 6512 | |
| 6513 | |
| 6514 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6515 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6516 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6517 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6518 | |
| 6519 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6520 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6521 | { |
| 6522 | char buffer[L_tmpnam]; |
| 6523 | char *name; |
| 6524 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6525 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6526 | "tmpnam is a potential security risk to your program") < 0) |
| 6527 | return NULL; |
| 6528 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6529 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6530 | name = tmpnam_r(buffer); |
| 6531 | #else |
| 6532 | name = tmpnam(buffer); |
| 6533 | #endif |
| 6534 | if (name == NULL) { |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 6535 | PyObject *err = Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6536 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6537 | "unexpected NULL from tmpnam_r" |
| 6538 | #else |
| 6539 | "unexpected NULL from tmpnam" |
| 6540 | #endif |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 6541 | ); |
| 6542 | PyErr_SetObject(PyExc_OSError, err); |
| 6543 | Py_XDECREF(err); |
| 6544 | return NULL; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6545 | } |
| 6546 | return PyString_FromString(buffer); |
| 6547 | } |
| 6548 | #endif |
| 6549 | |
| 6550 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6551 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 6552 | * It maps strings representing configuration variable names to |
| 6553 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6554 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6555 | * rarely-used constants. There are three separate tables that use |
| 6556 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6557 | * |
| 6558 | * This code is always included, even if none of the interfaces that |
| 6559 | * need it are included. The #if hackery needed to avoid it would be |
| 6560 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6561 | */ |
| 6562 | struct constdef { |
| 6563 | char *name; |
| 6564 | long value; |
| 6565 | }; |
| 6566 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6567 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6568 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 6569 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6570 | { |
| 6571 | if (PyInt_Check(arg)) { |
| 6572 | *valuep = PyInt_AS_LONG(arg); |
| 6573 | return 1; |
| 6574 | } |
| 6575 | if (PyString_Check(arg)) { |
| 6576 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6577 | size_t lo = 0; |
| 6578 | size_t mid; |
| 6579 | size_t hi = tablesize; |
| 6580 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6581 | char *confname = PyString_AS_STRING(arg); |
| 6582 | while (lo < hi) { |
| 6583 | mid = (lo + hi) / 2; |
| 6584 | cmp = strcmp(confname, table[mid].name); |
| 6585 | if (cmp < 0) |
| 6586 | hi = mid; |
| 6587 | else if (cmp > 0) |
| 6588 | lo = mid + 1; |
| 6589 | else { |
| 6590 | *valuep = table[mid].value; |
| 6591 | return 1; |
| 6592 | } |
| 6593 | } |
| 6594 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 6595 | } |
| 6596 | else |
| 6597 | PyErr_SetString(PyExc_TypeError, |
| 6598 | "configuration names must be strings or integers"); |
| 6599 | return 0; |
| 6600 | } |
| 6601 | |
| 6602 | |
| 6603 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 6604 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6605 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 6606 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 6607 | #endif |
| 6608 | #ifdef _PC_ABI_ASYNC_IO |
| 6609 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 6610 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6611 | #ifdef _PC_ASYNC_IO |
| 6612 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 6613 | #endif |
| 6614 | #ifdef _PC_CHOWN_RESTRICTED |
| 6615 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 6616 | #endif |
| 6617 | #ifdef _PC_FILESIZEBITS |
| 6618 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 6619 | #endif |
| 6620 | #ifdef _PC_LAST |
| 6621 | {"PC_LAST", _PC_LAST}, |
| 6622 | #endif |
| 6623 | #ifdef _PC_LINK_MAX |
| 6624 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 6625 | #endif |
| 6626 | #ifdef _PC_MAX_CANON |
| 6627 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 6628 | #endif |
| 6629 | #ifdef _PC_MAX_INPUT |
| 6630 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 6631 | #endif |
| 6632 | #ifdef _PC_NAME_MAX |
| 6633 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 6634 | #endif |
| 6635 | #ifdef _PC_NO_TRUNC |
| 6636 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 6637 | #endif |
| 6638 | #ifdef _PC_PATH_MAX |
| 6639 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 6640 | #endif |
| 6641 | #ifdef _PC_PIPE_BUF |
| 6642 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 6643 | #endif |
| 6644 | #ifdef _PC_PRIO_IO |
| 6645 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 6646 | #endif |
| 6647 | #ifdef _PC_SOCK_MAXBUF |
| 6648 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 6649 | #endif |
| 6650 | #ifdef _PC_SYNC_IO |
| 6651 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 6652 | #endif |
| 6653 | #ifdef _PC_VDISABLE |
| 6654 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 6655 | #endif |
| 6656 | }; |
| 6657 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6658 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6659 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6660 | { |
| 6661 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 6662 | sizeof(posix_constants_pathconf) |
| 6663 | / sizeof(struct constdef)); |
| 6664 | } |
| 6665 | #endif |
| 6666 | |
| 6667 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6668 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6669 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6670 | 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] | 6671 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6672 | |
| 6673 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6674 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6675 | { |
| 6676 | PyObject *result = NULL; |
| 6677 | int name, fd; |
| 6678 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6679 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 6680 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6681 | long limit; |
| 6682 | |
| 6683 | errno = 0; |
| 6684 | limit = fpathconf(fd, name); |
| 6685 | if (limit == -1 && errno != 0) |
| 6686 | posix_error(); |
| 6687 | else |
| 6688 | result = PyInt_FromLong(limit); |
| 6689 | } |
| 6690 | return result; |
| 6691 | } |
| 6692 | #endif |
| 6693 | |
| 6694 | |
| 6695 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6696 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6697 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6698 | 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] | 6699 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6700 | |
| 6701 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6702 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6703 | { |
| 6704 | PyObject *result = NULL; |
| 6705 | int name; |
| 6706 | char *path; |
| 6707 | |
| 6708 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 6709 | conv_path_confname, &name)) { |
| 6710 | long limit; |
| 6711 | |
| 6712 | errno = 0; |
| 6713 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6714 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6715 | if (errno == EINVAL) |
| 6716 | /* could be a path or name problem */ |
| 6717 | posix_error(); |
| 6718 | else |
| 6719 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6720 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6721 | else |
| 6722 | result = PyInt_FromLong(limit); |
| 6723 | } |
| 6724 | return result; |
| 6725 | } |
| 6726 | #endif |
| 6727 | |
| 6728 | #ifdef HAVE_CONFSTR |
| 6729 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6730 | #ifdef _CS_ARCHITECTURE |
| 6731 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 6732 | #endif |
| 6733 | #ifdef _CS_HOSTNAME |
| 6734 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 6735 | #endif |
| 6736 | #ifdef _CS_HW_PROVIDER |
| 6737 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 6738 | #endif |
| 6739 | #ifdef _CS_HW_SERIAL |
| 6740 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 6741 | #endif |
| 6742 | #ifdef _CS_INITTAB_NAME |
| 6743 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 6744 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6745 | #ifdef _CS_LFS64_CFLAGS |
| 6746 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 6747 | #endif |
| 6748 | #ifdef _CS_LFS64_LDFLAGS |
| 6749 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 6750 | #endif |
| 6751 | #ifdef _CS_LFS64_LIBS |
| 6752 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 6753 | #endif |
| 6754 | #ifdef _CS_LFS64_LINTFLAGS |
| 6755 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 6756 | #endif |
| 6757 | #ifdef _CS_LFS_CFLAGS |
| 6758 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 6759 | #endif |
| 6760 | #ifdef _CS_LFS_LDFLAGS |
| 6761 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6762 | #endif |
| 6763 | #ifdef _CS_LFS_LIBS |
| 6764 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6765 | #endif |
| 6766 | #ifdef _CS_LFS_LINTFLAGS |
| 6767 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6768 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6769 | #ifdef _CS_MACHINE |
| 6770 | {"CS_MACHINE", _CS_MACHINE}, |
| 6771 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6772 | #ifdef _CS_PATH |
| 6773 | {"CS_PATH", _CS_PATH}, |
| 6774 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6775 | #ifdef _CS_RELEASE |
| 6776 | {"CS_RELEASE", _CS_RELEASE}, |
| 6777 | #endif |
| 6778 | #ifdef _CS_SRPC_DOMAIN |
| 6779 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6780 | #endif |
| 6781 | #ifdef _CS_SYSNAME |
| 6782 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6783 | #endif |
| 6784 | #ifdef _CS_VERSION |
| 6785 | {"CS_VERSION", _CS_VERSION}, |
| 6786 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6787 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6788 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6789 | #endif |
| 6790 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6791 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6792 | #endif |
| 6793 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6794 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6795 | #endif |
| 6796 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6797 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6798 | #endif |
| 6799 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6800 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6801 | #endif |
| 6802 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6803 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6804 | #endif |
| 6805 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6806 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6807 | #endif |
| 6808 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6809 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6810 | #endif |
| 6811 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6812 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6813 | #endif |
| 6814 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6815 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6816 | #endif |
| 6817 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6818 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6819 | #endif |
| 6820 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6821 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6822 | #endif |
| 6823 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6824 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6825 | #endif |
| 6826 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6827 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6828 | #endif |
| 6829 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6830 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6831 | #endif |
| 6832 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6833 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6834 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6835 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6836 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6837 | #endif |
| 6838 | #ifdef _MIPS_CS_BASE |
| 6839 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6840 | #endif |
| 6841 | #ifdef _MIPS_CS_HOSTID |
| 6842 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6843 | #endif |
| 6844 | #ifdef _MIPS_CS_HW_NAME |
| 6845 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6846 | #endif |
| 6847 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6848 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6849 | #endif |
| 6850 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6851 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6852 | #endif |
| 6853 | #ifdef _MIPS_CS_OSREL_MIN |
| 6854 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6855 | #endif |
| 6856 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6857 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6858 | #endif |
| 6859 | #ifdef _MIPS_CS_OS_NAME |
| 6860 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6861 | #endif |
| 6862 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6863 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6864 | #endif |
| 6865 | #ifdef _MIPS_CS_PROCESSORS |
| 6866 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6867 | #endif |
| 6868 | #ifdef _MIPS_CS_SERIAL |
| 6869 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6870 | #endif |
| 6871 | #ifdef _MIPS_CS_VENDOR |
| 6872 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6873 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6874 | }; |
| 6875 | |
| 6876 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6877 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6878 | { |
| 6879 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6880 | sizeof(posix_constants_confstr) |
| 6881 | / sizeof(struct constdef)); |
| 6882 | } |
| 6883 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6884 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6885 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6886 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6887 | |
| 6888 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6889 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6890 | { |
| 6891 | PyObject *result = NULL; |
| 6892 | int name; |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 6893 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6894 | |
| 6895 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Skip Montanaro | dd527fc | 2006-04-18 00:49:49 +0000 | [diff] [blame] | 6896 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6897 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6898 | errno = 0; |
Skip Montanaro | dd527fc | 2006-04-18 00:49:49 +0000 | [diff] [blame] | 6899 | len = confstr(name, buffer, sizeof(buffer)); |
Skip Montanaro | 94785ef | 2006-04-20 01:29:48 +0000 | [diff] [blame] | 6900 | if (len == 0) { |
| 6901 | if (errno) { |
| 6902 | posix_error(); |
| 6903 | } |
| 6904 | else { |
| 6905 | result = Py_None; |
| 6906 | Py_INCREF(Py_None); |
| 6907 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6908 | } |
| 6909 | else { |
Neal Norwitz | 0d21b1e | 2006-04-20 06:44:42 +0000 | [diff] [blame] | 6910 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 6911 | result = PyString_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6912 | if (result != NULL) |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 6913 | confstr(name, PyString_AS_STRING(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6914 | } |
| 6915 | else |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 6916 | result = PyString_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6917 | } |
| 6918 | } |
| 6919 | return result; |
| 6920 | } |
| 6921 | #endif |
| 6922 | |
| 6923 | |
| 6924 | #ifdef HAVE_SYSCONF |
| 6925 | static struct constdef posix_constants_sysconf[] = { |
| 6926 | #ifdef _SC_2_CHAR_TERM |
| 6927 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6928 | #endif |
| 6929 | #ifdef _SC_2_C_BIND |
| 6930 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6931 | #endif |
| 6932 | #ifdef _SC_2_C_DEV |
| 6933 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6934 | #endif |
| 6935 | #ifdef _SC_2_C_VERSION |
| 6936 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6937 | #endif |
| 6938 | #ifdef _SC_2_FORT_DEV |
| 6939 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6940 | #endif |
| 6941 | #ifdef _SC_2_FORT_RUN |
| 6942 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6943 | #endif |
| 6944 | #ifdef _SC_2_LOCALEDEF |
| 6945 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6946 | #endif |
| 6947 | #ifdef _SC_2_SW_DEV |
| 6948 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6949 | #endif |
| 6950 | #ifdef _SC_2_UPE |
| 6951 | {"SC_2_UPE", _SC_2_UPE}, |
| 6952 | #endif |
| 6953 | #ifdef _SC_2_VERSION |
| 6954 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6955 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6956 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6957 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6958 | #endif |
| 6959 | #ifdef _SC_ACL |
| 6960 | {"SC_ACL", _SC_ACL}, |
| 6961 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6962 | #ifdef _SC_AIO_LISTIO_MAX |
| 6963 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6964 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6965 | #ifdef _SC_AIO_MAX |
| 6966 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6967 | #endif |
| 6968 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6969 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6970 | #endif |
| 6971 | #ifdef _SC_ARG_MAX |
| 6972 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6973 | #endif |
| 6974 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6975 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6976 | #endif |
| 6977 | #ifdef _SC_ATEXIT_MAX |
| 6978 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6979 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6980 | #ifdef _SC_AUDIT |
| 6981 | {"SC_AUDIT", _SC_AUDIT}, |
| 6982 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6983 | #ifdef _SC_AVPHYS_PAGES |
| 6984 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6985 | #endif |
| 6986 | #ifdef _SC_BC_BASE_MAX |
| 6987 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6988 | #endif |
| 6989 | #ifdef _SC_BC_DIM_MAX |
| 6990 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6991 | #endif |
| 6992 | #ifdef _SC_BC_SCALE_MAX |
| 6993 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6994 | #endif |
| 6995 | #ifdef _SC_BC_STRING_MAX |
| 6996 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6997 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6998 | #ifdef _SC_CAP |
| 6999 | {"SC_CAP", _SC_CAP}, |
| 7000 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7001 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 7002 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 7003 | #endif |
| 7004 | #ifdef _SC_CHAR_BIT |
| 7005 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 7006 | #endif |
| 7007 | #ifdef _SC_CHAR_MAX |
| 7008 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 7009 | #endif |
| 7010 | #ifdef _SC_CHAR_MIN |
| 7011 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 7012 | #endif |
| 7013 | #ifdef _SC_CHILD_MAX |
| 7014 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 7015 | #endif |
| 7016 | #ifdef _SC_CLK_TCK |
| 7017 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 7018 | #endif |
| 7019 | #ifdef _SC_COHER_BLKSZ |
| 7020 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 7021 | #endif |
| 7022 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 7023 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 7024 | #endif |
| 7025 | #ifdef _SC_DCACHE_ASSOC |
| 7026 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 7027 | #endif |
| 7028 | #ifdef _SC_DCACHE_BLKSZ |
| 7029 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 7030 | #endif |
| 7031 | #ifdef _SC_DCACHE_LINESZ |
| 7032 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 7033 | #endif |
| 7034 | #ifdef _SC_DCACHE_SZ |
| 7035 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 7036 | #endif |
| 7037 | #ifdef _SC_DCACHE_TBLKSZ |
| 7038 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 7039 | #endif |
| 7040 | #ifdef _SC_DELAYTIMER_MAX |
| 7041 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 7042 | #endif |
| 7043 | #ifdef _SC_EQUIV_CLASS_MAX |
| 7044 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 7045 | #endif |
| 7046 | #ifdef _SC_EXPR_NEST_MAX |
| 7047 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 7048 | #endif |
| 7049 | #ifdef _SC_FSYNC |
| 7050 | {"SC_FSYNC", _SC_FSYNC}, |
| 7051 | #endif |
| 7052 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 7053 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 7054 | #endif |
| 7055 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 7056 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 7057 | #endif |
| 7058 | #ifdef _SC_ICACHE_ASSOC |
| 7059 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 7060 | #endif |
| 7061 | #ifdef _SC_ICACHE_BLKSZ |
| 7062 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 7063 | #endif |
| 7064 | #ifdef _SC_ICACHE_LINESZ |
| 7065 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 7066 | #endif |
| 7067 | #ifdef _SC_ICACHE_SZ |
| 7068 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 7069 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7070 | #ifdef _SC_INF |
| 7071 | {"SC_INF", _SC_INF}, |
| 7072 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7073 | #ifdef _SC_INT_MAX |
| 7074 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 7075 | #endif |
| 7076 | #ifdef _SC_INT_MIN |
| 7077 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 7078 | #endif |
| 7079 | #ifdef _SC_IOV_MAX |
| 7080 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 7081 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7082 | #ifdef _SC_IP_SECOPTS |
| 7083 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 7084 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7085 | #ifdef _SC_JOB_CONTROL |
| 7086 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 7087 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7088 | #ifdef _SC_KERN_POINTERS |
| 7089 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 7090 | #endif |
| 7091 | #ifdef _SC_KERN_SIM |
| 7092 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 7093 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7094 | #ifdef _SC_LINE_MAX |
| 7095 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 7096 | #endif |
| 7097 | #ifdef _SC_LOGIN_NAME_MAX |
| 7098 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 7099 | #endif |
| 7100 | #ifdef _SC_LOGNAME_MAX |
| 7101 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 7102 | #endif |
| 7103 | #ifdef _SC_LONG_BIT |
| 7104 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 7105 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7106 | #ifdef _SC_MAC |
| 7107 | {"SC_MAC", _SC_MAC}, |
| 7108 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7109 | #ifdef _SC_MAPPED_FILES |
| 7110 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 7111 | #endif |
| 7112 | #ifdef _SC_MAXPID |
| 7113 | {"SC_MAXPID", _SC_MAXPID}, |
| 7114 | #endif |
| 7115 | #ifdef _SC_MB_LEN_MAX |
| 7116 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 7117 | #endif |
| 7118 | #ifdef _SC_MEMLOCK |
| 7119 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 7120 | #endif |
| 7121 | #ifdef _SC_MEMLOCK_RANGE |
| 7122 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 7123 | #endif |
| 7124 | #ifdef _SC_MEMORY_PROTECTION |
| 7125 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 7126 | #endif |
| 7127 | #ifdef _SC_MESSAGE_PASSING |
| 7128 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 7129 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7130 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 7131 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 7132 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7133 | #ifdef _SC_MQ_OPEN_MAX |
| 7134 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 7135 | #endif |
| 7136 | #ifdef _SC_MQ_PRIO_MAX |
| 7137 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 7138 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7139 | #ifdef _SC_NACLS_MAX |
| 7140 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 7141 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7142 | #ifdef _SC_NGROUPS_MAX |
| 7143 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 7144 | #endif |
| 7145 | #ifdef _SC_NL_ARGMAX |
| 7146 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 7147 | #endif |
| 7148 | #ifdef _SC_NL_LANGMAX |
| 7149 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 7150 | #endif |
| 7151 | #ifdef _SC_NL_MSGMAX |
| 7152 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 7153 | #endif |
| 7154 | #ifdef _SC_NL_NMAX |
| 7155 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 7156 | #endif |
| 7157 | #ifdef _SC_NL_SETMAX |
| 7158 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 7159 | #endif |
| 7160 | #ifdef _SC_NL_TEXTMAX |
| 7161 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 7162 | #endif |
| 7163 | #ifdef _SC_NPROCESSORS_CONF |
| 7164 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 7165 | #endif |
| 7166 | #ifdef _SC_NPROCESSORS_ONLN |
| 7167 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 7168 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7169 | #ifdef _SC_NPROC_CONF |
| 7170 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 7171 | #endif |
| 7172 | #ifdef _SC_NPROC_ONLN |
| 7173 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 7174 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7175 | #ifdef _SC_NZERO |
| 7176 | {"SC_NZERO", _SC_NZERO}, |
| 7177 | #endif |
| 7178 | #ifdef _SC_OPEN_MAX |
| 7179 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 7180 | #endif |
| 7181 | #ifdef _SC_PAGESIZE |
| 7182 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 7183 | #endif |
| 7184 | #ifdef _SC_PAGE_SIZE |
| 7185 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 7186 | #endif |
| 7187 | #ifdef _SC_PASS_MAX |
| 7188 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 7189 | #endif |
| 7190 | #ifdef _SC_PHYS_PAGES |
| 7191 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 7192 | #endif |
| 7193 | #ifdef _SC_PII |
| 7194 | {"SC_PII", _SC_PII}, |
| 7195 | #endif |
| 7196 | #ifdef _SC_PII_INTERNET |
| 7197 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 7198 | #endif |
| 7199 | #ifdef _SC_PII_INTERNET_DGRAM |
| 7200 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 7201 | #endif |
| 7202 | #ifdef _SC_PII_INTERNET_STREAM |
| 7203 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 7204 | #endif |
| 7205 | #ifdef _SC_PII_OSI |
| 7206 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 7207 | #endif |
| 7208 | #ifdef _SC_PII_OSI_CLTS |
| 7209 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 7210 | #endif |
| 7211 | #ifdef _SC_PII_OSI_COTS |
| 7212 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 7213 | #endif |
| 7214 | #ifdef _SC_PII_OSI_M |
| 7215 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 7216 | #endif |
| 7217 | #ifdef _SC_PII_SOCKET |
| 7218 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 7219 | #endif |
| 7220 | #ifdef _SC_PII_XTI |
| 7221 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 7222 | #endif |
| 7223 | #ifdef _SC_POLL |
| 7224 | {"SC_POLL", _SC_POLL}, |
| 7225 | #endif |
| 7226 | #ifdef _SC_PRIORITIZED_IO |
| 7227 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 7228 | #endif |
| 7229 | #ifdef _SC_PRIORITY_SCHEDULING |
| 7230 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 7231 | #endif |
| 7232 | #ifdef _SC_REALTIME_SIGNALS |
| 7233 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 7234 | #endif |
| 7235 | #ifdef _SC_RE_DUP_MAX |
| 7236 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 7237 | #endif |
| 7238 | #ifdef _SC_RTSIG_MAX |
| 7239 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 7240 | #endif |
| 7241 | #ifdef _SC_SAVED_IDS |
| 7242 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 7243 | #endif |
| 7244 | #ifdef _SC_SCHAR_MAX |
| 7245 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 7246 | #endif |
| 7247 | #ifdef _SC_SCHAR_MIN |
| 7248 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 7249 | #endif |
| 7250 | #ifdef _SC_SELECT |
| 7251 | {"SC_SELECT", _SC_SELECT}, |
| 7252 | #endif |
| 7253 | #ifdef _SC_SEMAPHORES |
| 7254 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 7255 | #endif |
| 7256 | #ifdef _SC_SEM_NSEMS_MAX |
| 7257 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 7258 | #endif |
| 7259 | #ifdef _SC_SEM_VALUE_MAX |
| 7260 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 7261 | #endif |
| 7262 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 7263 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 7264 | #endif |
| 7265 | #ifdef _SC_SHRT_MAX |
| 7266 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 7267 | #endif |
| 7268 | #ifdef _SC_SHRT_MIN |
| 7269 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 7270 | #endif |
| 7271 | #ifdef _SC_SIGQUEUE_MAX |
| 7272 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 7273 | #endif |
| 7274 | #ifdef _SC_SIGRT_MAX |
| 7275 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 7276 | #endif |
| 7277 | #ifdef _SC_SIGRT_MIN |
| 7278 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 7279 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7280 | #ifdef _SC_SOFTPOWER |
| 7281 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 7282 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7283 | #ifdef _SC_SPLIT_CACHE |
| 7284 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 7285 | #endif |
| 7286 | #ifdef _SC_SSIZE_MAX |
| 7287 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 7288 | #endif |
| 7289 | #ifdef _SC_STACK_PROT |
| 7290 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 7291 | #endif |
| 7292 | #ifdef _SC_STREAM_MAX |
| 7293 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 7294 | #endif |
| 7295 | #ifdef _SC_SYNCHRONIZED_IO |
| 7296 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 7297 | #endif |
| 7298 | #ifdef _SC_THREADS |
| 7299 | {"SC_THREADS", _SC_THREADS}, |
| 7300 | #endif |
| 7301 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 7302 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 7303 | #endif |
| 7304 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 7305 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 7306 | #endif |
| 7307 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 7308 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 7309 | #endif |
| 7310 | #ifdef _SC_THREAD_KEYS_MAX |
| 7311 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 7312 | #endif |
| 7313 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 7314 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 7315 | #endif |
| 7316 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 7317 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 7318 | #endif |
| 7319 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 7320 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 7321 | #endif |
| 7322 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 7323 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 7324 | #endif |
| 7325 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 7326 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 7327 | #endif |
| 7328 | #ifdef _SC_THREAD_STACK_MIN |
| 7329 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 7330 | #endif |
| 7331 | #ifdef _SC_THREAD_THREADS_MAX |
| 7332 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 7333 | #endif |
| 7334 | #ifdef _SC_TIMERS |
| 7335 | {"SC_TIMERS", _SC_TIMERS}, |
| 7336 | #endif |
| 7337 | #ifdef _SC_TIMER_MAX |
| 7338 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 7339 | #endif |
| 7340 | #ifdef _SC_TTY_NAME_MAX |
| 7341 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 7342 | #endif |
| 7343 | #ifdef _SC_TZNAME_MAX |
| 7344 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 7345 | #endif |
| 7346 | #ifdef _SC_T_IOV_MAX |
| 7347 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 7348 | #endif |
| 7349 | #ifdef _SC_UCHAR_MAX |
| 7350 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 7351 | #endif |
| 7352 | #ifdef _SC_UINT_MAX |
| 7353 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 7354 | #endif |
| 7355 | #ifdef _SC_UIO_MAXIOV |
| 7356 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 7357 | #endif |
| 7358 | #ifdef _SC_ULONG_MAX |
| 7359 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 7360 | #endif |
| 7361 | #ifdef _SC_USHRT_MAX |
| 7362 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 7363 | #endif |
| 7364 | #ifdef _SC_VERSION |
| 7365 | {"SC_VERSION", _SC_VERSION}, |
| 7366 | #endif |
| 7367 | #ifdef _SC_WORD_BIT |
| 7368 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 7369 | #endif |
| 7370 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 7371 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 7372 | #endif |
| 7373 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 7374 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 7375 | #endif |
| 7376 | #ifdef _SC_XBS5_LP64_OFF64 |
| 7377 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 7378 | #endif |
| 7379 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 7380 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 7381 | #endif |
| 7382 | #ifdef _SC_XOPEN_CRYPT |
| 7383 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 7384 | #endif |
| 7385 | #ifdef _SC_XOPEN_ENH_I18N |
| 7386 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 7387 | #endif |
| 7388 | #ifdef _SC_XOPEN_LEGACY |
| 7389 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 7390 | #endif |
| 7391 | #ifdef _SC_XOPEN_REALTIME |
| 7392 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 7393 | #endif |
| 7394 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 7395 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 7396 | #endif |
| 7397 | #ifdef _SC_XOPEN_SHM |
| 7398 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 7399 | #endif |
| 7400 | #ifdef _SC_XOPEN_UNIX |
| 7401 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 7402 | #endif |
| 7403 | #ifdef _SC_XOPEN_VERSION |
| 7404 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 7405 | #endif |
| 7406 | #ifdef _SC_XOPEN_XCU_VERSION |
| 7407 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 7408 | #endif |
| 7409 | #ifdef _SC_XOPEN_XPG2 |
| 7410 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 7411 | #endif |
| 7412 | #ifdef _SC_XOPEN_XPG3 |
| 7413 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 7414 | #endif |
| 7415 | #ifdef _SC_XOPEN_XPG4 |
| 7416 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 7417 | #endif |
| 7418 | }; |
| 7419 | |
| 7420 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7421 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7422 | { |
| 7423 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 7424 | sizeof(posix_constants_sysconf) |
| 7425 | / sizeof(struct constdef)); |
| 7426 | } |
| 7427 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7428 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7429 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7430 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7431 | |
| 7432 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7433 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7434 | { |
| 7435 | PyObject *result = NULL; |
| 7436 | int name; |
| 7437 | |
| 7438 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 7439 | int value; |
| 7440 | |
| 7441 | errno = 0; |
| 7442 | value = sysconf(name); |
| 7443 | if (value == -1 && errno != 0) |
| 7444 | posix_error(); |
| 7445 | else |
| 7446 | result = PyInt_FromLong(value); |
| 7447 | } |
| 7448 | return result; |
| 7449 | } |
| 7450 | #endif |
| 7451 | |
| 7452 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7453 | /* This code is used to ensure that the tables of configuration value names |
| 7454 | * are in sorted order as required by conv_confname(), and also to build the |
| 7455 | * the exported dictionaries that are used to publish information about the |
| 7456 | * names available on the host platform. |
| 7457 | * |
| 7458 | * Sorting the table at runtime ensures that the table is properly ordered |
| 7459 | * when used, even for platforms we're not able to test on. It also makes |
| 7460 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7461 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7462 | |
| 7463 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7464 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7465 | { |
| 7466 | const struct constdef *c1 = |
| 7467 | (const struct constdef *) v1; |
| 7468 | const struct constdef *c2 = |
| 7469 | (const struct constdef *) v2; |
| 7470 | |
| 7471 | return strcmp(c1->name, c2->name); |
| 7472 | } |
| 7473 | |
| 7474 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7475 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7476 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7477 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7478 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7479 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7480 | |
| 7481 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 7482 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7483 | if (d == NULL) |
| 7484 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7485 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7486 | for (i=0; i < tablesize; ++i) { |
| 7487 | PyObject *o = PyInt_FromLong(table[i].value); |
| 7488 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 7489 | Py_XDECREF(o); |
| 7490 | Py_DECREF(d); |
| 7491 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7492 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7493 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7494 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7495 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7496 | } |
| 7497 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7498 | /* Return -1 on failure, 0 on success. */ |
| 7499 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7500 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7501 | { |
| 7502 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7503 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7504 | sizeof(posix_constants_pathconf) |
| 7505 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7506 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7507 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7508 | #endif |
| 7509 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7510 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7511 | sizeof(posix_constants_confstr) |
| 7512 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7513 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7514 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7515 | #endif |
| 7516 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7517 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7518 | sizeof(posix_constants_sysconf) |
| 7519 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7520 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7521 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7522 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7523 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7524 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7525 | |
| 7526 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7527 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7528 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7529 | 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] | 7530 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7531 | |
| 7532 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7533 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7534 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7535 | abort(); |
| 7536 | /*NOTREACHED*/ |
| 7537 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 7538 | return NULL; |
| 7539 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7540 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7541 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7542 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7543 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 7544 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7545 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7546 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 7547 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 7548 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 7549 | application (if any) its extension is associated.\n\ |
| 7550 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 7551 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7552 | \n\ |
| 7553 | startfile returns as soon as the associated application is launched.\n\ |
| 7554 | There is no option to wait for the application to close, and no way\n\ |
| 7555 | to retrieve the application's exit status.\n\ |
| 7556 | \n\ |
| 7557 | The filepath is relative to the current directory. If you want to use\n\ |
| 7558 | 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] | 7559 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7560 | |
| 7561 | static PyObject * |
| 7562 | win32_startfile(PyObject *self, PyObject *args) |
| 7563 | { |
| 7564 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7565 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7566 | HINSTANCE rc; |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7567 | #ifdef Py_WIN_WIDE_FILENAMES |
| 7568 | if (unicode_file_names()) { |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7569 | PyObject *unipath, *woperation = NULL; |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7570 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 7571 | &unipath, &operation)) { |
| 7572 | PyErr_Clear(); |
| 7573 | goto normal; |
| 7574 | } |
| 7575 | |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7576 | |
| 7577 | if (operation) { |
| 7578 | woperation = PyUnicode_DecodeASCII(operation, |
| 7579 | strlen(operation), NULL); |
| 7580 | if (!woperation) { |
| 7581 | PyErr_Clear(); |
| 7582 | operation = NULL; |
| 7583 | goto normal; |
| 7584 | } |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7585 | } |
| 7586 | |
| 7587 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7588 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7589 | PyUnicode_AS_UNICODE(unipath), |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7590 | NULL, NULL, SW_SHOWNORMAL); |
| 7591 | Py_END_ALLOW_THREADS |
| 7592 | |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7593 | Py_XDECREF(woperation); |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7594 | if (rc <= (HINSTANCE)32) { |
| 7595 | PyObject *errval = win32_error_unicode("startfile", |
| 7596 | PyUnicode_AS_UNICODE(unipath)); |
| 7597 | return errval; |
| 7598 | } |
| 7599 | Py_INCREF(Py_None); |
| 7600 | return Py_None; |
| 7601 | } |
| 7602 | #endif |
| 7603 | |
| 7604 | normal: |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7605 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 7606 | Py_FileSystemDefaultEncoding, &filepath, |
| 7607 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7608 | return NULL; |
| 7609 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7610 | rc = ShellExecute((HWND)0, operation, filepath, |
| 7611 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7612 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 7613 | if (rc <= (HINSTANCE)32) { |
| 7614 | PyObject *errval = win32_error("startfile", filepath); |
| 7615 | PyMem_Free(filepath); |
| 7616 | return errval; |
| 7617 | } |
| 7618 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7619 | Py_INCREF(Py_None); |
| 7620 | return Py_None; |
| 7621 | } |
| 7622 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7623 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7624 | #ifdef HAVE_GETLOADAVG |
| 7625 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 7626 | "getloadavg() -> (float, float, float)\n\n\ |
| 7627 | Return the number of processes in the system run queue averaged over\n\ |
| 7628 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 7629 | was unobtainable"); |
| 7630 | |
| 7631 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7632 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7633 | { |
| 7634 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7635 | if (getloadavg(loadavg, 3)!=3) { |
| 7636 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 7637 | return NULL; |
| 7638 | } else |
| 7639 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 7640 | } |
| 7641 | #endif |
| 7642 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7643 | #ifdef MS_WINDOWS |
| 7644 | |
| 7645 | PyDoc_STRVAR(win32_urandom__doc__, |
| 7646 | "urandom(n) -> str\n\n\ |
| 7647 | Return a string of n random bytes suitable for cryptographic use."); |
| 7648 | |
| 7649 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 7650 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 7651 | DWORD dwFlags ); |
| 7652 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 7653 | BYTE *pbBuffer ); |
| 7654 | |
| 7655 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
| 7656 | static HCRYPTPROV hCryptProv = 0; |
| 7657 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7658 | static PyObject* |
| 7659 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7660 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7661 | int howMany; |
| 7662 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7663 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7664 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 7665 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7666 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 7667 | if (howMany < 0) |
| 7668 | return PyErr_Format(PyExc_ValueError, |
| 7669 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7670 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7671 | if (hCryptProv == 0) { |
| 7672 | HINSTANCE hAdvAPI32 = NULL; |
| 7673 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7674 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7675 | /* Obtain handle to the DLL containing CryptoAPI |
| 7676 | This should not fail */ |
| 7677 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 7678 | if(hAdvAPI32 == NULL) |
| 7679 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7680 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7681 | /* Obtain pointers to the CryptoAPI functions |
| 7682 | This will fail on some early versions of Win95 */ |
| 7683 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 7684 | hAdvAPI32, |
| 7685 | "CryptAcquireContextA"); |
| 7686 | if (pCryptAcquireContext == NULL) |
| 7687 | return PyErr_Format(PyExc_NotImplementedError, |
| 7688 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7689 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7690 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 7691 | hAdvAPI32, "CryptGenRandom"); |
| 7692 | if (pCryptAcquireContext == NULL) |
| 7693 | return PyErr_Format(PyExc_NotImplementedError, |
| 7694 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7695 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7696 | /* Acquire context */ |
| 7697 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 7698 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 7699 | return win32_error("CryptAcquireContext", NULL); |
| 7700 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7701 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7702 | /* Allocate bytes */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7703 | result = PyString_FromStringAndSize(NULL, howMany); |
| 7704 | if (result != NULL) { |
| 7705 | /* Get random data */ |
| 7706 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 7707 | PyString_AS_STRING(result))) { |
| 7708 | Py_DECREF(result); |
| 7709 | return win32_error("CryptGenRandom", NULL); |
| 7710 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7711 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7712 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7713 | } |
| 7714 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7715 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7716 | static PyMethodDef posix_methods[] = { |
| 7717 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 7718 | #ifdef HAVE_TTYNAME |
| 7719 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 7720 | #endif |
| 7721 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 7722 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7723 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7724 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7725 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7726 | #ifdef HAVE_LCHOWN |
| 7727 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 7728 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7729 | #ifdef HAVE_CHROOT |
| 7730 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 7731 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7732 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7733 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7734 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7735 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7736 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7737 | #ifdef Py_USING_UNICODE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7738 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7739 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7740 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7741 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7742 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7743 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7744 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7745 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7746 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7747 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7748 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7749 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7750 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7751 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7752 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7753 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7754 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7755 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7756 | {"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] | 7757 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7758 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7759 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7760 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7761 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7762 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7763 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7764 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7765 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7766 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7767 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7768 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7769 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7770 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7771 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7772 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7773 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7774 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7775 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7776 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7777 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7778 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7779 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7780 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7781 | #if defined(PYOS_OS2) |
| 7782 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 7783 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 7784 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7785 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7786 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7787 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7788 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7789 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7790 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7791 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7792 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7793 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7794 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7795 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7796 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7797 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7798 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7799 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7800 | #endif /* HAVE_GETEGID */ |
| 7801 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7802 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7803 | #endif /* HAVE_GETEUID */ |
| 7804 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7805 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7806 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7807 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7808 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7809 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7810 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7811 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7812 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7813 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7814 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7815 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7816 | #endif /* HAVE_GETPPID */ |
| 7817 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7818 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7819 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7820 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7821 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7822 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7823 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7824 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7825 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7826 | #ifdef HAVE_KILLPG |
| 7827 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7828 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7829 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7830 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7831 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7832 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7833 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7834 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7835 | {"popen2", win32_popen2, METH_VARARGS}, |
| 7836 | {"popen3", win32_popen3, METH_VARARGS}, |
| 7837 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7838 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7839 | #else |
| 7840 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7841 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 7842 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 7843 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 7844 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7845 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7846 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7847 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7848 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7849 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7850 | #ifdef HAVE_SETEUID |
| 7851 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7852 | #endif /* HAVE_SETEUID */ |
| 7853 | #ifdef HAVE_SETEGID |
| 7854 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7855 | #endif /* HAVE_SETEGID */ |
| 7856 | #ifdef HAVE_SETREUID |
| 7857 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7858 | #endif /* HAVE_SETREUID */ |
| 7859 | #ifdef HAVE_SETREGID |
| 7860 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7861 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7862 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7863 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7864 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7865 | #ifdef HAVE_SETGROUPS |
| 7866 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 7867 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7868 | #ifdef HAVE_GETPGID |
| 7869 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7870 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7871 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7872 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7873 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7874 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7875 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7876 | #endif /* HAVE_WAIT */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 7877 | #ifdef HAVE_WAIT3 |
| 7878 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 7879 | #endif /* HAVE_WAIT3 */ |
| 7880 | #ifdef HAVE_WAIT4 |
| 7881 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 7882 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7883 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7884 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7885 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7886 | #ifdef HAVE_GETSID |
| 7887 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 7888 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7889 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7890 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7891 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7892 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7893 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7894 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7895 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7896 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7897 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7898 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7899 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7900 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7901 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7902 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 7903 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7904 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7905 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7906 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7907 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7908 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 7909 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7910 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7911 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7912 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7913 | #endif |
| 7914 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7915 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7916 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7917 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7918 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7919 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7920 | #ifdef HAVE_DEVICE_MACROS |
| 7921 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7922 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7923 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7924 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7925 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7926 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7927 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7928 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7929 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7930 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7931 | #ifdef HAVE_UNSETENV |
| 7932 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7933 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7934 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7935 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7936 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7937 | #ifdef HAVE_FCHDIR |
| 7938 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7939 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7940 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7941 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7942 | #endif |
| 7943 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7944 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7945 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7946 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7947 | #ifdef WCOREDUMP |
| 7948 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7949 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7950 | #ifdef WIFCONTINUED |
| 7951 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7952 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7953 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7954 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7955 | #endif /* WIFSTOPPED */ |
| 7956 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7957 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7958 | #endif /* WIFSIGNALED */ |
| 7959 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7960 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7961 | #endif /* WIFEXITED */ |
| 7962 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7963 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7964 | #endif /* WEXITSTATUS */ |
| 7965 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7966 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7967 | #endif /* WTERMSIG */ |
| 7968 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7969 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7970 | #endif /* WSTOPSIG */ |
| 7971 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7972 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7973 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7974 | #endif |
| 7975 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7976 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7977 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 7978 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7979 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7980 | #endif |
| 7981 | #ifdef HAVE_TEMPNAM |
| 7982 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 7983 | #endif |
| 7984 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7985 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7986 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7987 | #ifdef HAVE_CONFSTR |
| 7988 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7989 | #endif |
| 7990 | #ifdef HAVE_SYSCONF |
| 7991 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7992 | #endif |
| 7993 | #ifdef HAVE_FPATHCONF |
| 7994 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7995 | #endif |
| 7996 | #ifdef HAVE_PATHCONF |
| 7997 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7998 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7999 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8000 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 8001 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 8002 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8003 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8004 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8005 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8006 | #ifdef MS_WINDOWS |
| 8007 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 8008 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8009 | {NULL, NULL} /* Sentinel */ |
| 8010 | }; |
| 8011 | |
| 8012 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8013 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8014 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8015 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8016 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8017 | } |
| 8018 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8019 | #if defined(PYOS_OS2) |
| 8020 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8021 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8022 | { |
| 8023 | APIRET rc; |
| 8024 | ULONG values[QSV_MAX+1]; |
| 8025 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 8026 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8027 | |
| 8028 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 8029 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8030 | Py_END_ALLOW_THREADS |
| 8031 | |
| 8032 | if (rc != NO_ERROR) { |
| 8033 | os2_error(rc); |
| 8034 | return -1; |
| 8035 | } |
| 8036 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8037 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 8038 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 8039 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 8040 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 8041 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 8042 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 8043 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8044 | |
| 8045 | switch (values[QSV_VERSION_MINOR]) { |
| 8046 | case 0: ver = "2.00"; break; |
| 8047 | case 10: ver = "2.10"; break; |
| 8048 | case 11: ver = "2.11"; break; |
| 8049 | case 30: ver = "3.00"; break; |
| 8050 | case 40: ver = "4.00"; break; |
| 8051 | case 50: ver = "5.00"; break; |
| 8052 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 8053 | PyOS_snprintf(tmp, sizeof(tmp), |
| 8054 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 8055 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8056 | ver = &tmp[0]; |
| 8057 | } |
| 8058 | |
| 8059 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8060 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8061 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8062 | |
| 8063 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 8064 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 8065 | tmp[1] = ':'; |
| 8066 | tmp[2] = '\0'; |
| 8067 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8068 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8069 | } |
| 8070 | #endif |
| 8071 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8072 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 8073 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8074 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8075 | #ifdef F_OK |
| 8076 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8077 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8078 | #ifdef R_OK |
| 8079 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8080 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8081 | #ifdef W_OK |
| 8082 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8083 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8084 | #ifdef X_OK |
| 8085 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8086 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8087 | #ifdef NGROUPS_MAX |
| 8088 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 8089 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8090 | #ifdef TMP_MAX |
| 8091 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 8092 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8093 | #ifdef WCONTINUED |
| 8094 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 8095 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8096 | #ifdef WNOHANG |
| 8097 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8098 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8099 | #ifdef WUNTRACED |
| 8100 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 8101 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8102 | #ifdef O_RDONLY |
| 8103 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 8104 | #endif |
| 8105 | #ifdef O_WRONLY |
| 8106 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 8107 | #endif |
| 8108 | #ifdef O_RDWR |
| 8109 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 8110 | #endif |
| 8111 | #ifdef O_NDELAY |
| 8112 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 8113 | #endif |
| 8114 | #ifdef O_NONBLOCK |
| 8115 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 8116 | #endif |
| 8117 | #ifdef O_APPEND |
| 8118 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 8119 | #endif |
| 8120 | #ifdef O_DSYNC |
| 8121 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 8122 | #endif |
| 8123 | #ifdef O_RSYNC |
| 8124 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 8125 | #endif |
| 8126 | #ifdef O_SYNC |
| 8127 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 8128 | #endif |
| 8129 | #ifdef O_NOCTTY |
| 8130 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 8131 | #endif |
| 8132 | #ifdef O_CREAT |
| 8133 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 8134 | #endif |
| 8135 | #ifdef O_EXCL |
| 8136 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 8137 | #endif |
| 8138 | #ifdef O_TRUNC |
| 8139 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 8140 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 8141 | #ifdef O_BINARY |
| 8142 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 8143 | #endif |
| 8144 | #ifdef O_TEXT |
| 8145 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 8146 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8147 | #ifdef O_LARGEFILE |
| 8148 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 8149 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 8150 | #ifdef O_SHLOCK |
| 8151 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 8152 | #endif |
| 8153 | #ifdef O_EXLOCK |
| 8154 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 8155 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8156 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8157 | /* MS Windows */ |
| 8158 | #ifdef O_NOINHERIT |
| 8159 | /* Don't inherit in child processes. */ |
| 8160 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 8161 | #endif |
| 8162 | #ifdef _O_SHORT_LIVED |
| 8163 | /* Optimize for short life (keep in memory). */ |
| 8164 | /* MS forgot to define this one with a non-underscore form too. */ |
| 8165 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 8166 | #endif |
| 8167 | #ifdef O_TEMPORARY |
| 8168 | /* Automatically delete when last handle is closed. */ |
| 8169 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 8170 | #endif |
| 8171 | #ifdef O_RANDOM |
| 8172 | /* Optimize for random access. */ |
| 8173 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 8174 | #endif |
| 8175 | #ifdef O_SEQUENTIAL |
| 8176 | /* Optimize for sequential access. */ |
| 8177 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 8178 | #endif |
| 8179 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8180 | /* GNU extensions. */ |
| 8181 | #ifdef O_DIRECT |
| 8182 | /* Direct disk access. */ |
| 8183 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 8184 | #endif |
| 8185 | #ifdef O_DIRECTORY |
| 8186 | /* Must be a directory. */ |
| 8187 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 8188 | #endif |
| 8189 | #ifdef O_NOFOLLOW |
| 8190 | /* Do not follow links. */ |
| 8191 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 8192 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8193 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8194 | /* These come from sysexits.h */ |
| 8195 | #ifdef EX_OK |
| 8196 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8197 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8198 | #ifdef EX_USAGE |
| 8199 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8200 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8201 | #ifdef EX_DATAERR |
| 8202 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8203 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8204 | #ifdef EX_NOINPUT |
| 8205 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8206 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8207 | #ifdef EX_NOUSER |
| 8208 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8209 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8210 | #ifdef EX_NOHOST |
| 8211 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8212 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8213 | #ifdef EX_UNAVAILABLE |
| 8214 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8215 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8216 | #ifdef EX_SOFTWARE |
| 8217 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8218 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8219 | #ifdef EX_OSERR |
| 8220 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8221 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8222 | #ifdef EX_OSFILE |
| 8223 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8224 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8225 | #ifdef EX_CANTCREAT |
| 8226 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8227 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8228 | #ifdef EX_IOERR |
| 8229 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8230 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8231 | #ifdef EX_TEMPFAIL |
| 8232 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8233 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8234 | #ifdef EX_PROTOCOL |
| 8235 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8236 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8237 | #ifdef EX_NOPERM |
| 8238 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8239 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8240 | #ifdef EX_CONFIG |
| 8241 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8242 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8243 | #ifdef EX_NOTFOUND |
| 8244 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8245 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8246 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8247 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8248 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 8249 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 8250 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 8251 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 8252 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 8253 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 8254 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 8255 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 8256 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 8257 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 8258 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 8259 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 8260 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 8261 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 8262 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 8263 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 8264 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 8265 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 8266 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 8267 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 8268 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 8269 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 8270 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 8271 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 8272 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 8273 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 8274 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8275 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8276 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8277 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8278 | #if defined(PYOS_OS2) |
| 8279 | if (insertvalues(d)) return -1; |
| 8280 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8281 | return 0; |
| 8282 | } |
| 8283 | |
| 8284 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8285 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8286 | #define INITFUNC initnt |
| 8287 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8288 | |
| 8289 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8290 | #define INITFUNC initos2 |
| 8291 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8292 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8293 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8294 | #define INITFUNC initposix |
| 8295 | #define MODNAME "posix" |
| 8296 | #endif |
| 8297 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 8298 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 8299 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8300 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8301 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8302 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8303 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8304 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8305 | posix__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 8306 | if (m == NULL) |
| 8307 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8308 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8309 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8310 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8311 | Py_XINCREF(v); |
| 8312 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8313 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8314 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8315 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8316 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8317 | return; |
| 8318 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8319 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8320 | return; |
| 8321 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8322 | Py_INCREF(PyExc_OSError); |
| 8323 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8324 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8325 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 8326 | if (posix_putenv_garbage == NULL) |
| 8327 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8328 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8329 | |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 8330 | if (!initialized) { |
| 8331 | stat_result_desc.name = MODNAME ".stat_result"; |
| 8332 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 8333 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 8334 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 8335 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 8336 | structseq_new = StatResultType.tp_new; |
| 8337 | StatResultType.tp_new = statresult_new; |
| 8338 | |
| 8339 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 8340 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
| 8341 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8342 | Py_INCREF((PyObject*) &StatResultType); |
| 8343 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8344 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 8345 | PyModule_AddObject(m, "statvfs_result", |
| 8346 | (PyObject*) &StatVFSResultType); |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 8347 | initialized = 1; |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 8348 | |
| 8349 | #ifdef __APPLE__ |
| 8350 | /* |
| 8351 | * Step 2 of weak-linking support on Mac OS X. |
| 8352 | * |
| 8353 | * The code below removes functions that are not available on the |
| 8354 | * currently active platform. |
| 8355 | * |
| 8356 | * This block allow one to use a python binary that was build on |
| 8357 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 8358 | * OSX 10.4. |
| 8359 | */ |
| 8360 | #ifdef HAVE_FSTATVFS |
| 8361 | if (fstatvfs == NULL) { |
| 8362 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 8363 | return; |
| 8364 | } |
| 8365 | } |
| 8366 | #endif /* HAVE_FSTATVFS */ |
| 8367 | |
| 8368 | #ifdef HAVE_STATVFS |
| 8369 | if (statvfs == NULL) { |
| 8370 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 8371 | return; |
| 8372 | } |
| 8373 | } |
| 8374 | #endif /* HAVE_STATVFS */ |
| 8375 | |
| 8376 | # ifdef HAVE_LCHOWN |
| 8377 | if (lchown == NULL) { |
| 8378 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 8379 | return; |
| 8380 | } |
| 8381 | } |
| 8382 | #endif /* HAVE_LCHOWN */ |
| 8383 | |
| 8384 | |
| 8385 | #endif /* __APPLE__ */ |
| 8386 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8387 | } |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 8388 | |
| 8389 | #ifdef __cplusplus |
| 8390 | } |
| 8391 | #endif |
| 8392 | |