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 | |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 67 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 68 | #include <sys/types.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 69 | #endif /* HAVE_SYS_TYPES_H */ |
| 70 | |
| 71 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 72 | #include <sys/stat.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 73 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 75 | #ifdef HAVE_SYS_WAIT_H |
| 76 | #include <sys/wait.h> /* For WNOHANG */ |
| 77 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 78 | |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 79 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 80 | #include <signal.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 81 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 83 | #ifdef HAVE_FCNTL_H |
| 84 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 85 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 87 | #ifdef HAVE_GRP_H |
| 88 | #include <grp.h> |
| 89 | #endif |
| 90 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 91 | #ifdef HAVE_SYSEXITS_H |
| 92 | #include <sysexits.h> |
| 93 | #endif /* HAVE_SYSEXITS_H */ |
| 94 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 95 | #ifdef HAVE_SYS_LOADAVG_H |
| 96 | #include <sys/loadavg.h> |
| 97 | #endif |
| 98 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 99 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 100 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 101 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 102 | #include <process.h> |
| 103 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 104 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 105 | #define HAVE_GETCWD 1 |
| 106 | #define HAVE_OPENDIR 1 |
| 107 | #define HAVE_SYSTEM 1 |
| 108 | #if defined(__OS2__) |
| 109 | #define HAVE_EXECV 1 |
| 110 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 111 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 112 | #include <process.h> |
| 113 | #else |
| 114 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 115 | #define HAVE_EXECV 1 |
| 116 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 117 | #define HAVE_OPENDIR 1 |
| 118 | #define HAVE_PIPE 1 |
| 119 | #define HAVE_POPEN 1 |
| 120 | #define HAVE_SYSTEM 1 |
| 121 | #define HAVE_WAIT 1 |
| 122 | #else |
| 123 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 124 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 125 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 126 | #define HAVE_EXECV 1 |
| 127 | #define HAVE_PIPE 1 |
| 128 | #define HAVE_POPEN 1 |
| 129 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 130 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 131 | #define HAVE_FSYNC 1 |
| 132 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 133 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 134 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 135 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 136 | #else /* all other compilers */ |
| 137 | /* Unix functions that the configure script doesn't check for */ |
| 138 | #define HAVE_EXECV 1 |
| 139 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 140 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 141 | #define HAVE_FORK1 1 |
| 142 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 143 | #define HAVE_GETCWD 1 |
| 144 | #define HAVE_GETEGID 1 |
| 145 | #define HAVE_GETEUID 1 |
| 146 | #define HAVE_GETGID 1 |
| 147 | #define HAVE_GETPPID 1 |
| 148 | #define HAVE_GETUID 1 |
| 149 | #define HAVE_KILL 1 |
| 150 | #define HAVE_OPENDIR 1 |
| 151 | #define HAVE_PIPE 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 152 | #ifndef __rtems__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 153 | #define HAVE_POPEN 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 154 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 155 | #define HAVE_SYSTEM 1 |
| 156 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 157 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 158 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 159 | #endif /* _MSC_VER */ |
| 160 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 161 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 162 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 164 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 165 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 166 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 167 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 168 | (default) */ |
| 169 | extern char *ctermid_r(char *); |
| 170 | #endif |
| 171 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 172 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 173 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 174 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 175 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 176 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 177 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 178 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 179 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 180 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 181 | #endif |
| 182 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 183 | extern int chdir(char *); |
| 184 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 185 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 186 | extern int chdir(const char *); |
| 187 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 188 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 189 | #ifdef __BORLANDC__ |
| 190 | extern int chmod(const char *, int); |
| 191 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 192 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 193 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 194 | extern int chown(const char *, uid_t, gid_t); |
| 195 | extern char *getcwd(char *, int); |
| 196 | extern char *strerror(int); |
| 197 | extern int link(const char *, const char *); |
| 198 | extern int rename(const char *, const char *); |
| 199 | extern int stat(const char *, struct stat *); |
| 200 | extern int unlink(const char *); |
| 201 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 203 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 204 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 206 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 207 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 208 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 210 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 212 | #ifdef HAVE_UTIME_H |
| 213 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 214 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 216 | #ifdef HAVE_SYS_UTIME_H |
| 217 | #include <sys/utime.h> |
| 218 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 219 | #endif /* HAVE_SYS_UTIME_H */ |
| 220 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 221 | #ifdef HAVE_SYS_TIMES_H |
| 222 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 223 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | |
| 225 | #ifdef HAVE_SYS_PARAM_H |
| 226 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 227 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | |
| 229 | #ifdef HAVE_SYS_UTSNAME_H |
| 230 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 231 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 233 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 235 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 236 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 237 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 238 | #include <direct.h> |
| 239 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 240 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 242 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 243 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 244 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 246 | #endif |
| 247 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 248 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 249 | #endif |
| 250 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 252 | #endif |
| 253 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 255 | #ifdef _MSC_VER |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 256 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #include <direct.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 258 | #endif |
| 259 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | #include <io.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 261 | #endif |
| 262 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | #include <process.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 264 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 265 | #include "osdefs.h" |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 266 | #define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 267 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 268 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 269 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 270 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 271 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 272 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 273 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 274 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 275 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 276 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 277 | #ifndef MAXPATHLEN |
Thomas Wouters | 1ddba60 | 2006-04-25 15:29:46 +0000 | [diff] [blame] | 278 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 279 | #define MAXPATHLEN PATH_MAX |
| 280 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 281 | #define MAXPATHLEN 1024 |
Thomas Wouters | 1ddba60 | 2006-04-25 15:29:46 +0000 | [diff] [blame] | 282 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 283 | #endif /* MAXPATHLEN */ |
| 284 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 285 | #ifdef UNION_WAIT |
| 286 | /* Emulate some macros on systems that have a union instead of macros */ |
| 287 | |
| 288 | #ifndef WIFEXITED |
| 289 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 290 | #endif |
| 291 | |
| 292 | #ifndef WEXITSTATUS |
| 293 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 294 | #endif |
| 295 | |
| 296 | #ifndef WTERMSIG |
| 297 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 298 | #endif |
| 299 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 300 | #define WAIT_TYPE union wait |
| 301 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 302 | |
| 303 | #else /* !UNION_WAIT */ |
| 304 | #define WAIT_TYPE int |
| 305 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 306 | #endif /* UNION_WAIT */ |
| 307 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 308 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 309 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 310 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 311 | #define USE_CTERMID_R |
| 312 | #endif |
| 313 | |
| 314 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 315 | #define USE_TMPNAM_R |
| 316 | #endif |
| 317 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 318 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 319 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 320 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 321 | # define STAT win32_stat |
| 322 | # define FSTAT win32_fstat |
| 323 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 324 | #else |
| 325 | # define STAT stat |
| 326 | # define FSTAT fstat |
| 327 | # define STRUCT_STAT struct stat |
| 328 | #endif |
| 329 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 330 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 331 | #include <sys/mkdev.h> |
| 332 | #else |
| 333 | #if defined(MAJOR_IN_SYSMACROS) |
| 334 | #include <sys/sysmacros.h> |
| 335 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 336 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 337 | #include <sys/mkdev.h> |
| 338 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 339 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 340 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 341 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 342 | #ifdef WITH_NEXT_FRAMEWORK |
| 343 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 344 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 345 | */ |
| 346 | #include <crt_externs.h> |
| 347 | static char **environ; |
| 348 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 349 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 350 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 351 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 352 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 353 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 354 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 355 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 356 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 357 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 358 | if (d == NULL) |
| 359 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 360 | #ifdef WITH_NEXT_FRAMEWORK |
| 361 | if (environ == NULL) |
| 362 | environ = *_NSGetEnviron(); |
| 363 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 364 | if (environ == NULL) |
| 365 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 366 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 367 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 368 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 369 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 370 | char *p = strchr(*e, '='); |
| 371 | if (p == NULL) |
| 372 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 373 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 374 | if (k == NULL) { |
| 375 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 376 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 377 | } |
| 378 | v = PyString_FromString(p+1); |
| 379 | if (v == NULL) { |
| 380 | PyErr_Clear(); |
| 381 | Py_DECREF(k); |
| 382 | continue; |
| 383 | } |
| 384 | if (PyDict_GetItem(d, k) == NULL) { |
| 385 | if (PyDict_SetItem(d, k, v) != 0) |
| 386 | PyErr_Clear(); |
| 387 | } |
| 388 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 389 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 390 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 391 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 392 | { |
| 393 | APIRET rc; |
| 394 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 395 | |
| 396 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 397 | 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] | 398 | PyObject *v = PyString_FromString(buffer); |
| 399 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 400 | Py_DECREF(v); |
| 401 | } |
| 402 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 403 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 404 | PyObject *v = PyString_FromString(buffer); |
| 405 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 406 | Py_DECREF(v); |
| 407 | } |
| 408 | } |
| 409 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 410 | return d; |
| 411 | } |
| 412 | |
| 413 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 414 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 415 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 416 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 417 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 418 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 419 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 420 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 421 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 422 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 423 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 424 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 427 | #ifdef Py_WIN_WIDE_FILENAMES |
| 428 | static PyObject * |
| 429 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 430 | { |
| 431 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 432 | } |
| 433 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 434 | |
| 435 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 436 | static PyObject * |
| 437 | posix_error_with_allocated_filename(char* name) |
| 438 | { |
| 439 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 440 | PyMem_Free(name); |
| 441 | return rc; |
| 442 | } |
| 443 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 444 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 445 | static PyObject * |
| 446 | win32_error(char* function, char* filename) |
| 447 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 448 | /* XXX We should pass the function name along in the future. |
| 449 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 450 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 451 | Windows error object, which is non-trivial. |
| 452 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 453 | errno = GetLastError(); |
| 454 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 455 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 456 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 457 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 458 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 459 | |
| 460 | #ifdef Py_WIN_WIDE_FILENAMES |
| 461 | static PyObject * |
| 462 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 463 | { |
| 464 | /* XXX - see win32_error for comments on 'function' */ |
| 465 | errno = GetLastError(); |
| 466 | if (filename) |
| 467 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 468 | else |
| 469 | return PyErr_SetFromWindowsErr(errno); |
| 470 | } |
| 471 | |
| 472 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 473 | { |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* Function suitable for O& conversion */ |
| 477 | static int |
| 478 | convert_to_unicode(PyObject *arg, void* _param) |
| 479 | { |
| 480 | PyObject **param = (PyObject**)_param; |
| 481 | if (PyUnicode_CheckExact(arg)) { |
| 482 | Py_INCREF(arg); |
| 483 | *param = arg; |
| 484 | } |
| 485 | else if (PyUnicode_Check(arg)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 486 | /* For a Unicode subtype that's not a Unicode object, |
| 487 | return a true Unicode object with the same data. */ |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 488 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg), |
| 489 | PyUnicode_GET_SIZE(arg)); |
| 490 | return *param != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 491 | } |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 492 | else |
| 493 | *param = PyUnicode_FromEncodedObject(arg, |
| 494 | Py_FileSystemDefaultEncoding, |
| 495 | "strict"); |
| 496 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 500 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 501 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 502 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 503 | #if defined(PYOS_OS2) |
| 504 | /********************************************************************** |
| 505 | * Helper Function to Trim and Format OS/2 Messages |
| 506 | **********************************************************************/ |
| 507 | static void |
| 508 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 509 | { |
| 510 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 511 | |
| 512 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 513 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 514 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 515 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 516 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 517 | } |
| 518 | |
| 519 | /* Add Optional Reason Text */ |
| 520 | if (reason) { |
| 521 | strcat(msgbuf, " : "); |
| 522 | strcat(msgbuf, reason); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /********************************************************************** |
| 527 | * Decode an OS/2 Operating System Error Code |
| 528 | * |
| 529 | * A convenience function to lookup an OS/2 error code and return a |
| 530 | * text message we can use to raise a Python exception. |
| 531 | * |
| 532 | * Notes: |
| 533 | * The messages for errors returned from the OS/2 kernel reside in |
| 534 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 535 | * |
| 536 | **********************************************************************/ |
| 537 | static char * |
| 538 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 539 | { |
| 540 | APIRET rc; |
| 541 | ULONG msglen; |
| 542 | |
| 543 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 544 | Py_BEGIN_ALLOW_THREADS |
| 545 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 546 | errorcode, "oso001.msg", &msglen); |
| 547 | Py_END_ALLOW_THREADS |
| 548 | |
| 549 | if (rc == NO_ERROR) |
| 550 | os2_formatmsg(msgbuf, msglen, reason); |
| 551 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 552 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 553 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 554 | |
| 555 | return msgbuf; |
| 556 | } |
| 557 | |
| 558 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 559 | errors are not in a global variable e.g. 'errno' nor are |
| 560 | they congruent with posix error numbers. */ |
| 561 | |
| 562 | static PyObject * os2_error(int code) |
| 563 | { |
| 564 | char text[1024]; |
| 565 | PyObject *v; |
| 566 | |
| 567 | os2_strerror(text, sizeof(text), code, ""); |
| 568 | |
| 569 | v = Py_BuildValue("(is)", code, text); |
| 570 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 571 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 572 | Py_DECREF(v); |
| 573 | } |
| 574 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 575 | } |
| 576 | |
| 577 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 578 | |
| 579 | /* POSIX generic methods */ |
| 580 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 581 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 582 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 583 | { |
| 584 | int fd; |
| 585 | int res; |
| 586 | fd = PyObject_AsFileDescriptor(fdobj); |
| 587 | if (fd < 0) |
| 588 | return NULL; |
| 589 | Py_BEGIN_ALLOW_THREADS |
| 590 | res = (*func)(fd); |
| 591 | Py_END_ALLOW_THREADS |
| 592 | if (res < 0) |
| 593 | return posix_error(); |
| 594 | Py_INCREF(Py_None); |
| 595 | return Py_None; |
| 596 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 597 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 598 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 599 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 600 | unicode_file_names(void) |
| 601 | { |
| 602 | static int canusewide = -1; |
| 603 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 604 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 605 | the Windows NT family. */ |
| 606 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 607 | } |
| 608 | return canusewide; |
| 609 | } |
| 610 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 611 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 612 | static PyObject * |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 613 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 614 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 615 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 616 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 617 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 618 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 619 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 620 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 621 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 622 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 623 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 624 | return posix_error_with_allocated_filename(path1); |
| 625 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 626 | Py_INCREF(Py_None); |
| 627 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 630 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 631 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 632 | char *format, |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 633 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 634 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 635 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 636 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 637 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 638 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 639 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 640 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 641 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 642 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 643 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 644 | PyMem_Free(path1); |
| 645 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 646 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 647 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 648 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 649 | Py_INCREF(Py_None); |
| 650 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 653 | #ifdef Py_WIN_WIDE_FILENAMES |
| 654 | static PyObject* |
| 655 | win32_1str(PyObject* args, char* func, |
| 656 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 657 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
| 658 | { |
| 659 | PyObject *uni; |
| 660 | char *ansi; |
| 661 | BOOL result; |
| 662 | if (unicode_file_names()) { |
| 663 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 664 | PyErr_Clear(); |
| 665 | else { |
| 666 | Py_BEGIN_ALLOW_THREADS |
| 667 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 668 | Py_END_ALLOW_THREADS |
| 669 | if (!result) |
| 670 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 671 | Py_INCREF(Py_None); |
| 672 | return Py_None; |
| 673 | } |
| 674 | } |
| 675 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 676 | return NULL; |
| 677 | Py_BEGIN_ALLOW_THREADS |
| 678 | result = funcA(ansi); |
| 679 | Py_END_ALLOW_THREADS |
| 680 | if (!result) |
| 681 | return win32_error(func, ansi); |
| 682 | Py_INCREF(Py_None); |
| 683 | return Py_None; |
| 684 | |
| 685 | } |
| 686 | |
| 687 | /* This is a reimplementation of the C library's chdir function, |
| 688 | but one that produces Win32 errors instead of DOS error codes. |
| 689 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 690 | it also needs to set "magic" environment variables indicating |
| 691 | the per-drive current directory, which are of the form =<drive>: */ |
| 692 | BOOL __stdcall |
| 693 | win32_chdir(LPCSTR path) |
| 694 | { |
| 695 | char new_path[MAX_PATH+1]; |
| 696 | int result; |
| 697 | char env[4] = "=x:"; |
| 698 | |
| 699 | if(!SetCurrentDirectoryA(path)) |
| 700 | return FALSE; |
| 701 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 702 | if (!result) |
| 703 | return FALSE; |
| 704 | /* In the ANSI API, there should not be any paths longer |
| 705 | than MAX_PATH. */ |
| 706 | assert(result <= MAX_PATH+1); |
| 707 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 708 | strncmp(new_path, "//", 2) == 0) |
| 709 | /* UNC path, nothing to do. */ |
| 710 | return TRUE; |
| 711 | env[1] = new_path[0]; |
| 712 | return SetEnvironmentVariableA(env, new_path); |
| 713 | } |
| 714 | |
| 715 | /* The Unicode version differs from the ANSI version |
| 716 | since the current directory might exceed MAX_PATH characters */ |
| 717 | BOOL __stdcall |
| 718 | win32_wchdir(LPCWSTR path) |
| 719 | { |
| 720 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 721 | int result; |
| 722 | wchar_t env[4] = L"=x:"; |
| 723 | |
| 724 | if(!SetCurrentDirectoryW(path)) |
| 725 | return FALSE; |
| 726 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 727 | if (!result) |
| 728 | return FALSE; |
| 729 | if (result > MAX_PATH+1) { |
| 730 | new_path = malloc(result); |
| 731 | if (!new_path) { |
| 732 | SetLastError(ERROR_OUTOFMEMORY); |
| 733 | return FALSE; |
| 734 | } |
| 735 | } |
| 736 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 737 | wcsncmp(new_path, L"//", 2) == 0) |
| 738 | /* UNC path, nothing to do. */ |
| 739 | return TRUE; |
| 740 | env[1] = new_path[0]; |
| 741 | result = SetEnvironmentVariableW(env, new_path); |
| 742 | if (new_path != _new_path) |
| 743 | free(new_path); |
| 744 | return result; |
| 745 | } |
| 746 | #endif |
| 747 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 748 | #ifdef MS_WINDOWS |
| 749 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 750 | - time stamps are restricted to second resolution |
| 751 | - file modification times suffer from forth-and-back conversions between |
| 752 | UTC and local time |
| 753 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 754 | */ |
| 755 | #define HAVE_STAT_NSEC 1 |
| 756 | |
| 757 | struct win32_stat{ |
| 758 | int st_dev; |
| 759 | __int64 st_ino; |
| 760 | unsigned short st_mode; |
| 761 | int st_nlink; |
| 762 | int st_uid; |
| 763 | int st_gid; |
| 764 | int st_rdev; |
| 765 | __int64 st_size; |
| 766 | int st_atime; |
| 767 | int st_atime_nsec; |
| 768 | int st_mtime; |
| 769 | int st_mtime_nsec; |
| 770 | int st_ctime; |
| 771 | int st_ctime_nsec; |
| 772 | }; |
| 773 | |
| 774 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 775 | |
| 776 | static void |
| 777 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 778 | { |
Martin v. Löwis | 77c176d | 2006-05-12 17:22:04 +0000 | [diff] [blame] | 779 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 780 | /* Cannot simply cast and dereference in_ptr, |
| 781 | since it might not be aligned properly */ |
| 782 | __int64 in; |
| 783 | memcpy(&in, in_ptr, sizeof(in)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 784 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 785 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 786 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 787 | } |
| 788 | |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 789 | static void |
| 790 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) |
| 791 | { |
| 792 | /* XXX endianness */ |
| 793 | __int64 out; |
| 794 | out = time_in + secs_between_epochs; |
Martin v. Löwis | 463a42b | 2006-10-09 20:44:50 +0000 | [diff] [blame] | 795 | out = out * 10000000 + nsec_in / 100; |
Martin v. Löwis | 77c176d | 2006-05-12 17:22:04 +0000 | [diff] [blame] | 796 | memcpy(out_ptr, &out, sizeof(out)); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 799 | /* Below, we *know* that ugo+r is 0444 */ |
| 800 | #if _S_IREAD != 0400 |
| 801 | #error Unsupported C library |
| 802 | #endif |
| 803 | static int |
| 804 | attributes_to_mode(DWORD attr) |
| 805 | { |
| 806 | int m = 0; |
| 807 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 808 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 809 | else |
| 810 | m |= _S_IFREG; |
| 811 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 812 | m |= 0444; |
| 813 | else |
| 814 | m |= 0666; |
| 815 | return m; |
| 816 | } |
| 817 | |
| 818 | static int |
| 819 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 820 | { |
| 821 | memset(result, 0, sizeof(*result)); |
| 822 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 823 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 824 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 825 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 826 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 827 | |
| 828 | return 0; |
| 829 | } |
| 830 | |
Martin v. Löwis | 2c7aa63 | 2006-10-15 09:44:02 +0000 | [diff] [blame] | 831 | /* Emulate GetFileAttributesEx[AW] on Windows 95 */ |
| 832 | static int checked = 0; |
| 833 | static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 834 | static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID); |
| 835 | static void |
| 836 | check_gfax() |
| 837 | { |
| 838 | HINSTANCE hKernel32; |
| 839 | if (checked) |
| 840 | return; |
| 841 | checked = 1; |
| 842 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 843 | *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA"); |
| 844 | *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW"); |
| 845 | } |
| 846 | |
Martin v. Löwis | 8863544 | 2007-04-04 18:30:56 +0000 | [diff] [blame] | 847 | static BOOL |
| 848 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 849 | { |
| 850 | HANDLE hFindFile; |
| 851 | WIN32_FIND_DATAA FileData; |
| 852 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 853 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 854 | return FALSE; |
| 855 | FindClose(hFindFile); |
| 856 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 857 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 858 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 859 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 860 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 861 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 862 | return TRUE; |
| 863 | } |
| 864 | |
| 865 | static BOOL |
| 866 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 867 | { |
| 868 | HANDLE hFindFile; |
| 869 | WIN32_FIND_DATAW FileData; |
| 870 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 871 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 872 | return FALSE; |
| 873 | FindClose(hFindFile); |
| 874 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 875 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 876 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 877 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 878 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 879 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 880 | return TRUE; |
| 881 | } |
| 882 | |
Martin v. Löwis | 2c7aa63 | 2006-10-15 09:44:02 +0000 | [diff] [blame] | 883 | static BOOL WINAPI |
| 884 | Py_GetFileAttributesExA(LPCSTR pszFile, |
| 885 | GET_FILEEX_INFO_LEVELS level, |
| 886 | LPVOID pv) |
| 887 | { |
| 888 | BOOL result; |
Martin v. Löwis | 2c7aa63 | 2006-10-15 09:44:02 +0000 | [diff] [blame] | 889 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 890 | /* First try to use the system's implementation, if that is |
| 891 | available and either succeeds to gives an error other than |
| 892 | that it isn't implemented. */ |
| 893 | check_gfax(); |
| 894 | if (gfaxa) { |
| 895 | result = gfaxa(pszFile, level, pv); |
| 896 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 897 | return result; |
| 898 | } |
| 899 | /* It's either not present, or not implemented. |
| 900 | Emulate using FindFirstFile. */ |
| 901 | if (level != GetFileExInfoStandard) { |
| 902 | SetLastError(ERROR_INVALID_PARAMETER); |
| 903 | return FALSE; |
| 904 | } |
| 905 | /* Use GetFileAttributes to validate that the file name |
| 906 | does not contain wildcards (which FindFirstFile would |
| 907 | accept). */ |
| 908 | if (GetFileAttributesA(pszFile) == 0xFFFFFFFF) |
| 909 | return FALSE; |
Martin v. Löwis | 8863544 | 2007-04-04 18:30:56 +0000 | [diff] [blame] | 910 | return attributes_from_dir(pszFile, pfad); |
Martin v. Löwis | 2c7aa63 | 2006-10-15 09:44:02 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | static BOOL WINAPI |
| 914 | Py_GetFileAttributesExW(LPCWSTR pszFile, |
| 915 | GET_FILEEX_INFO_LEVELS level, |
| 916 | LPVOID pv) |
| 917 | { |
| 918 | BOOL result; |
Martin v. Löwis | 2c7aa63 | 2006-10-15 09:44:02 +0000 | [diff] [blame] | 919 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 920 | /* First try to use the system's implementation, if that is |
| 921 | available and either succeeds to gives an error other than |
| 922 | that it isn't implemented. */ |
| 923 | check_gfax(); |
| 924 | if (gfaxa) { |
| 925 | result = gfaxw(pszFile, level, pv); |
| 926 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 927 | return result; |
| 928 | } |
| 929 | /* It's either not present, or not implemented. |
| 930 | Emulate using FindFirstFile. */ |
| 931 | if (level != GetFileExInfoStandard) { |
| 932 | SetLastError(ERROR_INVALID_PARAMETER); |
| 933 | return FALSE; |
| 934 | } |
| 935 | /* Use GetFileAttributes to validate that the file name |
| 936 | does not contain wildcards (which FindFirstFile would |
| 937 | accept). */ |
| 938 | if (GetFileAttributesW(pszFile) == 0xFFFFFFFF) |
| 939 | return FALSE; |
Martin v. Löwis | 8863544 | 2007-04-04 18:30:56 +0000 | [diff] [blame] | 940 | return attributes_from_dir_w(pszFile, pfad); |
Martin v. Löwis | 2c7aa63 | 2006-10-15 09:44:02 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 943 | static int |
| 944 | win32_stat(const char* path, struct win32_stat *result) |
| 945 | { |
| 946 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 947 | int code; |
| 948 | char *dot; |
| 949 | /* XXX not supported on Win95 and NT 3.x */ |
Martin v. Löwis | 2c7aa63 | 2006-10-15 09:44:02 +0000 | [diff] [blame] | 950 | if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
Martin v. Löwis | 8863544 | 2007-04-04 18:30:56 +0000 | [diff] [blame] | 951 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 952 | /* Protocol violation: we explicitly clear errno, instead of |
| 953 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 954 | errno = 0; |
| 955 | return -1; |
| 956 | } else { |
| 957 | /* Could not get attributes on open file. Fall back to |
| 958 | reading the directory. */ |
| 959 | if (!attributes_from_dir(path, &info)) { |
| 960 | /* Very strange. This should not fail now */ |
| 961 | errno = 0; |
| 962 | return -1; |
| 963 | } |
| 964 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 965 | } |
| 966 | code = attribute_data_to_stat(&info, result); |
| 967 | if (code != 0) |
| 968 | return code; |
| 969 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 970 | dot = strrchr(path, '.'); |
| 971 | if (dot) { |
| 972 | if (stricmp(dot, ".bat") == 0 || |
| 973 | stricmp(dot, ".cmd") == 0 || |
| 974 | stricmp(dot, ".exe") == 0 || |
| 975 | stricmp(dot, ".com") == 0) |
| 976 | result->st_mode |= 0111; |
| 977 | } |
| 978 | return code; |
| 979 | } |
| 980 | |
| 981 | static int |
| 982 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 983 | { |
| 984 | int code; |
| 985 | const wchar_t *dot; |
| 986 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 987 | /* XXX not supported on Win95 and NT 3.x */ |
Martin v. Löwis | 2c7aa63 | 2006-10-15 09:44:02 +0000 | [diff] [blame] | 988 | if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
Martin v. Löwis | 8863544 | 2007-04-04 18:30:56 +0000 | [diff] [blame] | 989 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 990 | /* Protocol violation: we explicitly clear errno, instead of |
| 991 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 992 | errno = 0; |
| 993 | return -1; |
| 994 | } else { |
| 995 | /* Could not get attributes on open file. Fall back to |
| 996 | reading the directory. */ |
| 997 | if (!attributes_from_dir_w(path, &info)) { |
| 998 | /* Very strange. This should not fail now */ |
| 999 | errno = 0; |
| 1000 | return -1; |
| 1001 | } |
| 1002 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1003 | } |
| 1004 | code = attribute_data_to_stat(&info, result); |
| 1005 | if (code < 0) |
| 1006 | return code; |
| 1007 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 1008 | dot = wcsrchr(path, '.'); |
| 1009 | if (dot) { |
| 1010 | if (_wcsicmp(dot, L".bat") == 0 || |
| 1011 | _wcsicmp(dot, L".cmd") == 0 || |
| 1012 | _wcsicmp(dot, L".exe") == 0 || |
| 1013 | _wcsicmp(dot, L".com") == 0) |
| 1014 | result->st_mode |= 0111; |
| 1015 | } |
| 1016 | return code; |
| 1017 | } |
| 1018 | |
| 1019 | static int |
| 1020 | win32_fstat(int file_number, struct win32_stat *result) |
| 1021 | { |
| 1022 | BY_HANDLE_FILE_INFORMATION info; |
| 1023 | HANDLE h; |
| 1024 | int type; |
| 1025 | |
| 1026 | h = (HANDLE)_get_osfhandle(file_number); |
| 1027 | |
| 1028 | /* Protocol violation: we explicitly clear errno, instead of |
| 1029 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1030 | errno = 0; |
| 1031 | |
| 1032 | if (h == INVALID_HANDLE_VALUE) { |
| 1033 | /* This is really a C library error (invalid file handle). |
| 1034 | We set the Win32 error to the closes one matching. */ |
| 1035 | SetLastError(ERROR_INVALID_HANDLE); |
| 1036 | return -1; |
| 1037 | } |
| 1038 | memset(result, 0, sizeof(*result)); |
| 1039 | |
| 1040 | type = GetFileType(h); |
| 1041 | if (type == FILE_TYPE_UNKNOWN) { |
| 1042 | DWORD error = GetLastError(); |
| 1043 | if (error != 0) { |
| 1044 | return -1; |
| 1045 | } |
| 1046 | /* else: valid but unknown file */ |
| 1047 | } |
| 1048 | |
| 1049 | if (type != FILE_TYPE_DISK) { |
| 1050 | if (type == FILE_TYPE_CHAR) |
| 1051 | result->st_mode = _S_IFCHR; |
| 1052 | else if (type == FILE_TYPE_PIPE) |
| 1053 | result->st_mode = _S_IFIFO; |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | if (!GetFileInformationByHandle(h, &info)) { |
| 1058 | return -1; |
| 1059 | } |
| 1060 | |
| 1061 | /* similar to stat() */ |
| 1062 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1063 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1064 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1065 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1066 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1067 | /* specific to fstat() */ |
| 1068 | result->st_nlink = info.nNumberOfLinks; |
| 1069 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
| 1073 | #endif /* MS_WINDOWS */ |
| 1074 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1075 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1076 | "stat_result: Result from stat or lstat.\n\n\ |
| 1077 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1078 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1079 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1080 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1081 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1082 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1083 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1084 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1085 | |
| 1086 | static PyStructSequence_Field stat_result_fields[] = { |
| 1087 | {"st_mode", "protection bits"}, |
| 1088 | {"st_ino", "inode"}, |
| 1089 | {"st_dev", "device"}, |
| 1090 | {"st_nlink", "number of hard links"}, |
| 1091 | {"st_uid", "user ID of owner"}, |
| 1092 | {"st_gid", "group ID of owner"}, |
| 1093 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1094 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1095 | {NULL, "integer time of last access"}, |
| 1096 | {NULL, "integer time of last modification"}, |
| 1097 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1098 | {"st_atime", "time of last access"}, |
| 1099 | {"st_mtime", "time of last modification"}, |
| 1100 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1101 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1102 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 1103 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1104 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1105 | {"st_blocks", "number of blocks allocated"}, |
| 1106 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1107 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1108 | {"st_rdev", "device type (if inode device)"}, |
| 1109 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1110 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1111 | {"st_flags", "user defined flags for file"}, |
| 1112 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1113 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1114 | {"st_gen", "generation number"}, |
| 1115 | #endif |
| 1116 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1117 | {"st_birthtime", "time of creation"}, |
| 1118 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1119 | {0} |
| 1120 | }; |
| 1121 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1122 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1123 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1124 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1125 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1126 | #endif |
| 1127 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1128 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1129 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1130 | #else |
| 1131 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1132 | #endif |
| 1133 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1134 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1135 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1136 | #else |
| 1137 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1138 | #endif |
| 1139 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1140 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1141 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1142 | #else |
| 1143 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1144 | #endif |
| 1145 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1146 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1147 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1148 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1149 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1150 | #endif |
| 1151 | |
| 1152 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1153 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1154 | #else |
| 1155 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1156 | #endif |
| 1157 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1158 | static PyStructSequence_Desc stat_result_desc = { |
| 1159 | "stat_result", /* name */ |
| 1160 | stat_result__doc__, /* doc */ |
| 1161 | stat_result_fields, |
| 1162 | 10 |
| 1163 | }; |
| 1164 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1165 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1166 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1167 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1168 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1169 | 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] | 1170 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1171 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1172 | |
| 1173 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 1174 | {"f_bsize", }, |
| 1175 | {"f_frsize", }, |
| 1176 | {"f_blocks", }, |
| 1177 | {"f_bfree", }, |
| 1178 | {"f_bavail", }, |
| 1179 | {"f_files", }, |
| 1180 | {"f_ffree", }, |
| 1181 | {"f_favail", }, |
| 1182 | {"f_flag", }, |
| 1183 | {"f_namemax",}, |
| 1184 | {0} |
| 1185 | }; |
| 1186 | |
| 1187 | static PyStructSequence_Desc statvfs_result_desc = { |
| 1188 | "statvfs_result", /* name */ |
| 1189 | statvfs_result__doc__, /* doc */ |
| 1190 | statvfs_result_fields, |
| 1191 | 10 |
| 1192 | }; |
| 1193 | |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 1194 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1195 | static PyTypeObject StatResultType; |
| 1196 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1197 | static newfunc structseq_new; |
| 1198 | |
| 1199 | static PyObject * |
| 1200 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1201 | { |
| 1202 | PyStructSequence *result; |
| 1203 | int i; |
| 1204 | |
| 1205 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1206 | if (!result) |
| 1207 | return NULL; |
| 1208 | /* If we have been initialized from a tuple, |
| 1209 | st_?time might be set to None. Initialize it |
| 1210 | from the int slots. */ |
| 1211 | for (i = 7; i <= 9; i++) { |
| 1212 | if (result->ob_item[i+3] == Py_None) { |
| 1213 | Py_DECREF(Py_None); |
| 1214 | Py_INCREF(result->ob_item[i]); |
| 1215 | result->ob_item[i+3] = result->ob_item[i]; |
| 1216 | } |
| 1217 | } |
| 1218 | return (PyObject*)result; |
| 1219 | } |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1224 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1225 | |
| 1226 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1227 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1228 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1229 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1230 | future calls return ints. \n\ |
| 1231 | If newval is omitted, return the current setting.\n"); |
| 1232 | |
| 1233 | static PyObject* |
| 1234 | stat_float_times(PyObject* self, PyObject *args) |
| 1235 | { |
| 1236 | int newval = -1; |
| 1237 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1238 | return NULL; |
| 1239 | if (newval == -1) |
| 1240 | /* Return old value */ |
| 1241 | return PyBool_FromLong(_stat_float_times); |
| 1242 | _stat_float_times = newval; |
| 1243 | Py_INCREF(Py_None); |
| 1244 | return Py_None; |
| 1245 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1246 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1247 | static void |
| 1248 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1249 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1250 | PyObject *fval,*ival; |
| 1251 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1252 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1253 | #else |
| 1254 | ival = PyInt_FromLong((long)sec); |
| 1255 | #endif |
Neal Norwitz | e0a81af | 2006-08-12 01:50:38 +0000 | [diff] [blame] | 1256 | if (!ival) |
| 1257 | return; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1258 | if (_stat_float_times) { |
| 1259 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1260 | } else { |
| 1261 | fval = ival; |
| 1262 | Py_INCREF(fval); |
| 1263 | } |
| 1264 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1265 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1268 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1269 | (used by posix_stat() and posix_fstat()) */ |
| 1270 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1271 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1272 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1273 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1274 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1275 | if (v == NULL) |
| 1276 | return NULL; |
| 1277 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1278 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1279 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1280 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1281 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1282 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1283 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1284 | #endif |
| 1285 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1286 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1287 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1288 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1289 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1290 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1291 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); |
| 1292 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); |
| 1293 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1294 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1295 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1296 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1297 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1298 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1299 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1300 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1301 | #if defined(HAVE_STAT_TV_NSEC) |
| 1302 | ansec = st->st_atim.tv_nsec; |
| 1303 | mnsec = st->st_mtim.tv_nsec; |
| 1304 | cnsec = st->st_ctim.tv_nsec; |
| 1305 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1306 | ansec = st->st_atimespec.tv_nsec; |
| 1307 | mnsec = st->st_mtimespec.tv_nsec; |
| 1308 | cnsec = st->st_ctimespec.tv_nsec; |
| 1309 | #elif defined(HAVE_STAT_NSEC) |
| 1310 | ansec = st->st_atime_nsec; |
| 1311 | mnsec = st->st_mtime_nsec; |
| 1312 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1313 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1314 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1315 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1316 | fill_time(v, 7, st->st_atime, ansec); |
| 1317 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1318 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1319 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1320 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1321 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1322 | PyInt_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1323 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1324 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1325 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1326 | PyInt_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1327 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1328 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1329 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1330 | PyInt_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1331 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1332 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1333 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1334 | PyInt_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1335 | #endif |
| 1336 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1337 | { |
| 1338 | PyObject *val; |
| 1339 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1340 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1341 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1342 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1343 | #else |
| 1344 | bnsec = 0; |
| 1345 | #endif |
| 1346 | if (_stat_float_times) { |
| 1347 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1348 | } else { |
| 1349 | val = PyInt_FromLong((long)bsec); |
| 1350 | } |
| 1351 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1352 | val); |
| 1353 | } |
| 1354 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1355 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1356 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1357 | PyInt_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1358 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1359 | |
| 1360 | if (PyErr_Occurred()) { |
| 1361 | Py_DECREF(v); |
| 1362 | return NULL; |
| 1363 | } |
| 1364 | |
| 1365 | return v; |
| 1366 | } |
| 1367 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1368 | #ifdef MS_WINDOWS |
| 1369 | |
| 1370 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1371 | where / can be used in place of \ and the trailing slash is optional. |
| 1372 | Both SERVER and SHARE must have at least one character. |
| 1373 | */ |
| 1374 | |
| 1375 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1376 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Kristján Valur Jónsson | dbeaa69 | 2006-06-09 16:28:01 +0000 | [diff] [blame] | 1377 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1378 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Kristján Valur Jónsson | dbeaa69 | 2006-06-09 16:28:01 +0000 | [diff] [blame] | 1379 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1380 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1381 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1382 | IsUNCRootA(char *path, int pathlen) |
| 1383 | { |
| 1384 | #define ISSLASH ISSLASHA |
| 1385 | |
| 1386 | int i, share; |
| 1387 | |
| 1388 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1389 | /* minimum UNCRoot is \\x\y */ |
| 1390 | return FALSE; |
| 1391 | for (i = 2; i < pathlen ; i++) |
| 1392 | if (ISSLASH(path[i])) break; |
| 1393 | if (i == 2 || i == pathlen) |
| 1394 | /* do not allow \\\SHARE or \\SERVER */ |
| 1395 | return FALSE; |
| 1396 | share = i+1; |
| 1397 | for (i = share; i < pathlen; i++) |
| 1398 | if (ISSLASH(path[i])) break; |
| 1399 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1400 | |
| 1401 | #undef ISSLASH |
| 1402 | } |
| 1403 | |
| 1404 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1405 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1406 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1407 | { |
| 1408 | #define ISSLASH ISSLASHW |
| 1409 | |
| 1410 | int i, share; |
| 1411 | |
| 1412 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1413 | /* minimum UNCRoot is \\x\y */ |
| 1414 | return FALSE; |
| 1415 | for (i = 2; i < pathlen ; i++) |
| 1416 | if (ISSLASH(path[i])) break; |
| 1417 | if (i == 2 || i == pathlen) |
| 1418 | /* do not allow \\\SHARE or \\SERVER */ |
| 1419 | return FALSE; |
| 1420 | share = i+1; |
| 1421 | for (i = share; i < pathlen; i++) |
| 1422 | if (ISSLASH(path[i])) break; |
| 1423 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1424 | |
| 1425 | #undef ISSLASH |
| 1426 | } |
| 1427 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1428 | #endif /* MS_WINDOWS */ |
| 1429 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1430 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1431 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1432 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1433 | #ifdef __VMS |
| 1434 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1435 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1436 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1437 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1438 | char *wformat, |
| 1439 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1440 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1441 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1442 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1443 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1444 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1445 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1446 | |
| 1447 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1448 | /* If on wide-character-capable OS see if argument |
| 1449 | is Unicode and if so use wide API. */ |
| 1450 | if (unicode_file_names()) { |
| 1451 | PyUnicodeObject *po; |
| 1452 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1453 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1454 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1455 | Py_BEGIN_ALLOW_THREADS |
| 1456 | /* PyUnicode_AS_UNICODE result OK without |
| 1457 | thread lock as it is a simple dereference. */ |
| 1458 | res = wstatfunc(wpath, &st); |
| 1459 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1460 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1461 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1462 | return win32_error_unicode("stat", wpath); |
| 1463 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1464 | } |
| 1465 | /* Drop the argument parsing error as narrow strings |
| 1466 | are also valid. */ |
| 1467 | PyErr_Clear(); |
| 1468 | } |
| 1469 | #endif |
| 1470 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1471 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1472 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1473 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1474 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1475 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1476 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1477 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1478 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1479 | |
| 1480 | if (res != 0) { |
| 1481 | #ifdef MS_WINDOWS |
| 1482 | result = win32_error("stat", pathfree); |
| 1483 | #else |
| 1484 | result = posix_error_with_filename(pathfree); |
| 1485 | #endif |
| 1486 | } |
| 1487 | else |
| 1488 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1489 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1490 | PyMem_Free(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1491 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1494 | /* POSIX methods */ |
| 1495 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1496 | PyDoc_STRVAR(posix_access__doc__, |
Georg Brandl | 3d26ef1 | 2007-01-27 19:38:56 +0000 | [diff] [blame] | 1497 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1498 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1499 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1500 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1501 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1502 | 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] | 1503 | |
| 1504 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1505 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1506 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1507 | char *path; |
| 1508 | int mode; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1509 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1510 | #ifdef Py_WIN_WIDE_FILENAMES |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1511 | DWORD attr; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1512 | if (unicode_file_names()) { |
| 1513 | PyUnicodeObject *po; |
| 1514 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1515 | Py_BEGIN_ALLOW_THREADS |
| 1516 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1517 | it is a simple dereference. */ |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1518 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1519 | Py_END_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1520 | goto finish; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1521 | } |
| 1522 | /* Drop the argument parsing error as narrow strings |
| 1523 | are also valid. */ |
| 1524 | PyErr_Clear(); |
| 1525 | } |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1526 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1527 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 1528 | return 0; |
| 1529 | Py_BEGIN_ALLOW_THREADS |
| 1530 | attr = GetFileAttributesA(path); |
| 1531 | Py_END_ALLOW_THREADS |
| 1532 | PyMem_Free(path); |
| 1533 | finish: |
| 1534 | if (attr == 0xFFFFFFFF) |
| 1535 | /* File does not exist, or cannot read attributes */ |
| 1536 | return PyBool_FromLong(0); |
| 1537 | /* Access is possible if either write access wasn't requested, or |
| 1538 | the file isn't read-only. */ |
Martin v. Löwis | ee1e06d | 2006-07-02 18:44:00 +0000 | [diff] [blame] | 1539 | return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY)); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1540 | #else |
| 1541 | int res; |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame] | 1542 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1543 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1544 | return NULL; |
| 1545 | Py_BEGIN_ALLOW_THREADS |
| 1546 | res = access(path, mode); |
| 1547 | Py_END_ALLOW_THREADS |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 1548 | PyMem_Free(path); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1549 | return PyBool_FromLong(res == 0); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1550 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1553 | #ifndef F_OK |
| 1554 | #define F_OK 0 |
| 1555 | #endif |
| 1556 | #ifndef R_OK |
| 1557 | #define R_OK 4 |
| 1558 | #endif |
| 1559 | #ifndef W_OK |
| 1560 | #define W_OK 2 |
| 1561 | #endif |
| 1562 | #ifndef X_OK |
| 1563 | #define X_OK 1 |
| 1564 | #endif |
| 1565 | |
| 1566 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1567 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1568 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1569 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1570 | |
| 1571 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1572 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1573 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1574 | int id; |
| 1575 | char *ret; |
| 1576 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1577 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1578 | return NULL; |
| 1579 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1580 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1581 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1582 | if (id == 0) { |
| 1583 | ret = ttyname(); |
| 1584 | } |
| 1585 | else { |
| 1586 | ret = NULL; |
| 1587 | } |
| 1588 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1589 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1590 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1591 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1592 | return posix_error(); |
| 1593 | return PyString_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1594 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1595 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1596 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1597 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1598 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1599 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1600 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1601 | |
| 1602 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1603 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1604 | { |
| 1605 | char *ret; |
| 1606 | char buffer[L_ctermid]; |
| 1607 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1608 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1609 | ret = ctermid_r(buffer); |
| 1610 | #else |
| 1611 | ret = ctermid(buffer); |
| 1612 | #endif |
| 1613 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1614 | return posix_error(); |
| 1615 | return PyString_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1616 | } |
| 1617 | #endif |
| 1618 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1619 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1620 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1621 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1622 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1623 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1624 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1625 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1626 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1627 | 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] | 1628 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1629 | return posix_1str(args, "et:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1630 | #elif defined(__VMS) |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1631 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1632 | #else |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1633 | return posix_1str(args, "et:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1634 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1637 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1638 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1639 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1640 | 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] | 1641 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1642 | |
| 1643 | static PyObject * |
| 1644 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1645 | { |
| 1646 | return posix_fildes(fdobj, fchdir); |
| 1647 | } |
| 1648 | #endif /* HAVE_FCHDIR */ |
| 1649 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1650 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1651 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1652 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1653 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1654 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1655 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1656 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1657 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1658 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1659 | int i; |
| 1660 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1661 | #ifdef Py_WIN_WIDE_FILENAMES |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1662 | DWORD attr; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1663 | if (unicode_file_names()) { |
| 1664 | PyUnicodeObject *po; |
| 1665 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1666 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1667 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1668 | if (attr != 0xFFFFFFFF) { |
| 1669 | if (i & _S_IWRITE) |
| 1670 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1671 | else |
| 1672 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1673 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1674 | } |
| 1675 | else |
| 1676 | res = 0; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1677 | Py_END_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1678 | if (!res) |
| 1679 | return win32_error_unicode("chmod", |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1680 | PyUnicode_AS_UNICODE(po)); |
| 1681 | Py_INCREF(Py_None); |
| 1682 | return Py_None; |
| 1683 | } |
| 1684 | /* Drop the argument parsing error as narrow strings |
| 1685 | are also valid. */ |
| 1686 | PyErr_Clear(); |
| 1687 | } |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1688 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
| 1689 | &path, &i)) |
| 1690 | return NULL; |
| 1691 | Py_BEGIN_ALLOW_THREADS |
| 1692 | attr = GetFileAttributesA(path); |
| 1693 | if (attr != 0xFFFFFFFF) { |
| 1694 | if (i & _S_IWRITE) |
| 1695 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1696 | else |
| 1697 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1698 | res = SetFileAttributesA(path, attr); |
| 1699 | } |
| 1700 | else |
| 1701 | res = 0; |
| 1702 | Py_END_ALLOW_THREADS |
| 1703 | if (!res) { |
| 1704 | win32_error("chmod", path); |
| 1705 | PyMem_Free(path); |
| 1706 | return NULL; |
| 1707 | } |
Martin v. Löwis | 9f485bc | 2006-05-08 05:25:56 +0000 | [diff] [blame] | 1708 | PyMem_Free(path); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1709 | Py_INCREF(Py_None); |
| 1710 | return Py_None; |
| 1711 | #else /* Py_WIN_WIDE_FILENAMES */ |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1712 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1713 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1714 | return NULL; |
| 1715 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1716 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1717 | Py_END_ALLOW_THREADS |
| 1718 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1719 | return posix_error_with_allocated_filename(path); |
| 1720 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1721 | Py_INCREF(Py_None); |
| 1722 | return Py_None; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1723 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1726 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1727 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1728 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1729 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1730 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1731 | |
| 1732 | static PyObject * |
| 1733 | posix_chroot(PyObject *self, PyObject *args) |
| 1734 | { |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1735 | return posix_1str(args, "et:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1736 | } |
| 1737 | #endif |
| 1738 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1739 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1740 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1741 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1742 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1743 | |
| 1744 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1745 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1746 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1747 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1748 | } |
| 1749 | #endif /* HAVE_FSYNC */ |
| 1750 | |
| 1751 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1752 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1753 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1754 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1755 | #endif |
| 1756 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1757 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1758 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1759 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1760 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1761 | |
| 1762 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1763 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1764 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1765 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1766 | } |
| 1767 | #endif /* HAVE_FDATASYNC */ |
| 1768 | |
| 1769 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1770 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1771 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1772 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1773 | 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] | 1774 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1775 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1776 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1777 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1778 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1779 | int uid, gid; |
| 1780 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1781 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1782 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1783 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1784 | return NULL; |
| 1785 | Py_BEGIN_ALLOW_THREADS |
| 1786 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1787 | Py_END_ALLOW_THREADS |
| 1788 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1789 | return posix_error_with_allocated_filename(path); |
| 1790 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1791 | Py_INCREF(Py_None); |
| 1792 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1793 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1794 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1795 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1796 | #ifdef HAVE_LCHOWN |
| 1797 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1798 | "lchown(path, uid, gid)\n\n\ |
| 1799 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1800 | This function will not follow symbolic links."); |
| 1801 | |
| 1802 | static PyObject * |
| 1803 | posix_lchown(PyObject *self, PyObject *args) |
| 1804 | { |
| 1805 | char *path = NULL; |
| 1806 | int uid, gid; |
| 1807 | int res; |
| 1808 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1809 | Py_FileSystemDefaultEncoding, &path, |
| 1810 | &uid, &gid)) |
| 1811 | return NULL; |
| 1812 | Py_BEGIN_ALLOW_THREADS |
| 1813 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1814 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1815 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1816 | return posix_error_with_allocated_filename(path); |
| 1817 | PyMem_Free(path); |
| 1818 | Py_INCREF(Py_None); |
| 1819 | return Py_None; |
| 1820 | } |
| 1821 | #endif /* HAVE_LCHOWN */ |
| 1822 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1823 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1824 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1825 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1826 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1827 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1828 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1829 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1830 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1831 | { |
| 1832 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1833 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1834 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1835 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1836 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1837 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1838 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1839 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1840 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1841 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1842 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1843 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1844 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1845 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1846 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1847 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1848 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1849 | "getcwdu() -> path\n\n\ |
| 1850 | Return a unicode string representing the current working directory."); |
| 1851 | |
| 1852 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1853 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1854 | { |
| 1855 | char buf[1026]; |
| 1856 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1857 | |
| 1858 | #ifdef Py_WIN_WIDE_FILENAMES |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1859 | DWORD len; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1860 | if (unicode_file_names()) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1861 | wchar_t wbuf[1026]; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1862 | wchar_t *wbuf2 = wbuf; |
| 1863 | PyObject *resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1864 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1865 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 1866 | /* If the buffer is large enough, len does not include the |
| 1867 | terminating \0. If the buffer is too small, len includes |
| 1868 | the space needed for the terminator. */ |
| 1869 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 1870 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 1871 | if (wbuf2) |
| 1872 | len = GetCurrentDirectoryW(len, wbuf2); |
| 1873 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1874 | Py_END_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1875 | if (!wbuf2) { |
| 1876 | PyErr_NoMemory(); |
| 1877 | return NULL; |
| 1878 | } |
| 1879 | if (!len) { |
| 1880 | if (wbuf2 != wbuf) free(wbuf2); |
| 1881 | return win32_error("getcwdu", NULL); |
| 1882 | } |
| 1883 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 1884 | if (wbuf2 != wbuf) free(wbuf2); |
| 1885 | return resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1886 | } |
| 1887 | #endif |
| 1888 | |
| 1889 | Py_BEGIN_ALLOW_THREADS |
| 1890 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1891 | res = _getcwd2(buf, sizeof buf); |
| 1892 | #else |
| 1893 | res = getcwd(buf, sizeof buf); |
| 1894 | #endif |
| 1895 | Py_END_ALLOW_THREADS |
| 1896 | if (res == NULL) |
| 1897 | return posix_error(); |
| 1898 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1899 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1900 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1901 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1902 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1903 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1904 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1905 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1906 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1907 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1908 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1909 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1910 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1911 | { |
Martin v. Löwis | 4fc2bda | 2006-05-04 12:04:27 +0000 | [diff] [blame] | 1912 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1913 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1914 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1915 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1916 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1917 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1918 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1919 | Return a list containing the names of the entries in the directory.\n\ |
| 1920 | \n\ |
| 1921 | path: path of directory to list\n\ |
| 1922 | \n\ |
| 1923 | 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] | 1924 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1925 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1926 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1927 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1928 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1929 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1930 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1931 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1932 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1933 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1934 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1935 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1936 | WIN32_FIND_DATA FileData; |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1937 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1938 | char *bufptr = namebuf; |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1939 | Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1940 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1941 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1942 | /* If on wide-character-capable OS see if argument |
| 1943 | is Unicode and if so use wide API. */ |
| 1944 | if (unicode_file_names()) { |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1945 | PyObject *po; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1946 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1947 | WIN32_FIND_DATAW wFileData; |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1948 | Py_UNICODE *wnamebuf; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1949 | Py_UNICODE wch; |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1950 | /* Overallocate for \\*.*\0 */ |
| 1951 | len = PyUnicode_GET_SIZE(po); |
| 1952 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 1953 | if (!wnamebuf) { |
| 1954 | PyErr_NoMemory(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1955 | return NULL; |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1956 | } |
| 1957 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
| 1958 | wch = len > 0 ? wnamebuf[len-1] : '\0'; |
| 1959 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1960 | wnamebuf[len++] = L'\\'; |
| 1961 | wcscpy(wnamebuf + len, L"*.*"); |
| 1962 | if ((d = PyList_New(0)) == NULL) { |
| 1963 | free(wnamebuf); |
| 1964 | return NULL; |
| 1965 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1966 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1967 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1968 | int error = GetLastError(); |
| 1969 | if (error == ERROR_FILE_NOT_FOUND) { |
| 1970 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1971 | return d; |
| 1972 | } |
| 1973 | Py_DECREF(d); |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1974 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1975 | free(wnamebuf); |
| 1976 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1977 | } |
| 1978 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1979 | /* Skip over . and .. */ |
| 1980 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 1981 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 1982 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1983 | if (v == NULL) { |
| 1984 | Py_DECREF(d); |
| 1985 | d = NULL; |
| 1986 | break; |
| 1987 | } |
| 1988 | if (PyList_Append(d, v) != 0) { |
| 1989 | Py_DECREF(v); |
| 1990 | Py_DECREF(d); |
| 1991 | d = NULL; |
| 1992 | break; |
| 1993 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1994 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1995 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1996 | Py_BEGIN_ALLOW_THREADS |
| 1997 | result = FindNextFileW(hFindFile, &wFileData); |
| 1998 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 982e9fe | 2006-07-24 12:54:17 +0000 | [diff] [blame] | 1999 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2000 | it got to the end of the directory. */ |
| 2001 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2002 | Py_DECREF(d); |
| 2003 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2004 | FindClose(hFindFile); |
| 2005 | free(wnamebuf); |
| 2006 | return NULL; |
| 2007 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2008 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2009 | |
| 2010 | if (FindClose(hFindFile) == FALSE) { |
| 2011 | Py_DECREF(d); |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 2012 | win32_error_unicode("FindClose", wnamebuf); |
| 2013 | free(wnamebuf); |
| 2014 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2015 | } |
Martin v. Löwis | e3edaea | 2006-05-15 05:51:36 +0000 | [diff] [blame] | 2016 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2017 | return d; |
| 2018 | } |
| 2019 | /* Drop the argument parsing error as narrow strings |
| 2020 | are also valid. */ |
| 2021 | PyErr_Clear(); |
| 2022 | } |
| 2023 | #endif |
| 2024 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2025 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2026 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2027 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2028 | if (len > 0) { |
| 2029 | char ch = namebuf[len-1]; |
| 2030 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2031 | namebuf[len++] = '/'; |
| 2032 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2033 | strcpy(namebuf + len, "*.*"); |
| 2034 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2035 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2036 | return NULL; |
| 2037 | |
| 2038 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 2039 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 2040 | int error = GetLastError(); |
| 2041 | if (error == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2042 | return d; |
| 2043 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2044 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2045 | } |
| 2046 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2047 | /* Skip over . and .. */ |
| 2048 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2049 | strcmp(FileData.cFileName, "..") != 0) { |
| 2050 | v = PyString_FromString(FileData.cFileName); |
| 2051 | if (v == NULL) { |
| 2052 | Py_DECREF(d); |
| 2053 | d = NULL; |
| 2054 | break; |
| 2055 | } |
| 2056 | if (PyList_Append(d, v) != 0) { |
| 2057 | Py_DECREF(v); |
| 2058 | Py_DECREF(d); |
| 2059 | d = NULL; |
| 2060 | break; |
| 2061 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2062 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2063 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2064 | Py_BEGIN_ALLOW_THREADS |
| 2065 | result = FindNextFile(hFindFile, &FileData); |
| 2066 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 982e9fe | 2006-07-24 12:54:17 +0000 | [diff] [blame] | 2067 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2068 | it got to the end of the directory. */ |
| 2069 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2070 | Py_DECREF(d); |
| 2071 | win32_error("FindNextFile", namebuf); |
| 2072 | FindClose(hFindFile); |
| 2073 | return NULL; |
| 2074 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2075 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2076 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2077 | if (FindClose(hFindFile) == FALSE) { |
| 2078 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2079 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2080 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2081 | |
| 2082 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2083 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2084 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2085 | |
| 2086 | #ifndef MAX_PATH |
| 2087 | #define MAX_PATH CCHMAXPATH |
| 2088 | #endif |
| 2089 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2090 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2091 | PyObject *d, *v; |
| 2092 | char namebuf[MAX_PATH+5]; |
| 2093 | HDIR hdir = 1; |
| 2094 | ULONG srchcnt = 1; |
| 2095 | FILEFINDBUF3 ep; |
| 2096 | APIRET rc; |
| 2097 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2098 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2099 | return NULL; |
| 2100 | if (len >= MAX_PATH) { |
| 2101 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2102 | return NULL; |
| 2103 | } |
| 2104 | strcpy(namebuf, name); |
| 2105 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2106 | if (*pt == ALTSEP) |
| 2107 | *pt = SEP; |
| 2108 | if (namebuf[len-1] != SEP) |
| 2109 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2110 | strcpy(namebuf + len, "*.*"); |
| 2111 | |
| 2112 | if ((d = PyList_New(0)) == NULL) |
| 2113 | return NULL; |
| 2114 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2115 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2116 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2117 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2118 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2119 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2120 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2121 | |
| 2122 | if (rc != NO_ERROR) { |
| 2123 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 2124 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2127 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2128 | do { |
| 2129 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2130 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2131 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2132 | |
| 2133 | strcpy(namebuf, ep.achName); |
| 2134 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2135 | /* Leave Case of Name Alone -- In Native Form */ |
| 2136 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2137 | |
| 2138 | v = PyString_FromString(namebuf); |
| 2139 | if (v == NULL) { |
| 2140 | Py_DECREF(d); |
| 2141 | d = NULL; |
| 2142 | break; |
| 2143 | } |
| 2144 | if (PyList_Append(d, v) != 0) { |
| 2145 | Py_DECREF(v); |
| 2146 | Py_DECREF(d); |
| 2147 | d = NULL; |
| 2148 | break; |
| 2149 | } |
| 2150 | Py_DECREF(v); |
| 2151 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2152 | } |
| 2153 | |
| 2154 | return d; |
| 2155 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2156 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2157 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2158 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2159 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2160 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2161 | int arg_is_unicode = 1; |
| 2162 | |
Georg Brandl | 05e89b8 | 2006-04-11 07:04:06 +0000 | [diff] [blame] | 2163 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2164 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2165 | arg_is_unicode = 0; |
| 2166 | PyErr_Clear(); |
| 2167 | } |
| 2168 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2169 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2170 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2171 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2172 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2173 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2174 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2175 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2176 | return NULL; |
| 2177 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2178 | for (;;) { |
| 2179 | Py_BEGIN_ALLOW_THREADS |
| 2180 | ep = readdir(dirp); |
| 2181 | Py_END_ALLOW_THREADS |
| 2182 | if (ep == NULL) |
| 2183 | break; |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2184 | if (ep->d_name[0] == '.' && |
| 2185 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2186 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2187 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2188 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2189 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2190 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2191 | d = NULL; |
| 2192 | break; |
| 2193 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2194 | #ifdef Py_USING_UNICODE |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2195 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2196 | PyObject *w; |
| 2197 | |
| 2198 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2199 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2200 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2201 | if (w != NULL) { |
| 2202 | Py_DECREF(v); |
| 2203 | v = w; |
| 2204 | } |
| 2205 | else { |
| 2206 | /* fall back to the original byte string, as |
| 2207 | discussed in patch #683592 */ |
| 2208 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2209 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2210 | } |
| 2211 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2212 | if (PyList_Append(d, v) != 0) { |
| 2213 | Py_DECREF(v); |
| 2214 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2215 | d = NULL; |
| 2216 | break; |
| 2217 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2218 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2219 | } |
Georg Brandl | bbfe4fa | 2006-04-11 06:47:43 +0000 | [diff] [blame] | 2220 | if (errno != 0 && d != NULL) { |
| 2221 | /* readdir() returned NULL and set errno */ |
| 2222 | closedir(dirp); |
| 2223 | Py_DECREF(d); |
| 2224 | return posix_error_with_allocated_filename(name); |
| 2225 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2226 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2227 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2228 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2229 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2230 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2231 | #endif /* which OS */ |
| 2232 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2233 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2234 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2235 | /* A helper function for abspath on win32 */ |
| 2236 | static PyObject * |
| 2237 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2238 | { |
| 2239 | /* assume encoded strings wont more than double no of chars */ |
| 2240 | char inbuf[MAX_PATH*2]; |
| 2241 | char *inbufp = inbuf; |
Tim Peters | 67d70eb | 2006-03-01 04:35:45 +0000 | [diff] [blame] | 2242 | Py_ssize_t insize = sizeof(inbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2243 | char outbuf[MAX_PATH*2]; |
| 2244 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2245 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2246 | if (unicode_file_names()) { |
| 2247 | PyUnicodeObject *po; |
| 2248 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2249 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 2250 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2251 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2252 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2253 | woutbuf, &wtemp)) |
| 2254 | return win32_error("GetFullPathName", ""); |
| 2255 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 2256 | } |
| 2257 | /* Drop the argument parsing error as narrow strings |
| 2258 | are also valid. */ |
| 2259 | PyErr_Clear(); |
| 2260 | } |
| 2261 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2262 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 2263 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2264 | &insize)) |
| 2265 | return NULL; |
| 2266 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2267 | outbuf, &temp)) |
| 2268 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2269 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2270 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2271 | Py_FileSystemDefaultEncoding, NULL); |
| 2272 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2273 | return PyString_FromString(outbuf); |
| 2274 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2275 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2276 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2277 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2278 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2279 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2280 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2281 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2282 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2283 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2284 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2285 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2286 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2287 | |
| 2288 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2289 | if (unicode_file_names()) { |
| 2290 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 2291 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2292 | Py_BEGIN_ALLOW_THREADS |
| 2293 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2294 | it is a simple dereference. */ |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2295 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2296 | Py_END_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2297 | if (!res) |
| 2298 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2299 | Py_INCREF(Py_None); |
| 2300 | return Py_None; |
| 2301 | } |
| 2302 | /* Drop the argument parsing error as narrow strings |
| 2303 | are also valid. */ |
| 2304 | PyErr_Clear(); |
| 2305 | } |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2306 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 2307 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 2308 | return NULL; |
| 2309 | Py_BEGIN_ALLOW_THREADS |
| 2310 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2311 | it is a simple dereference. */ |
| 2312 | res = CreateDirectoryA(path, NULL); |
| 2313 | Py_END_ALLOW_THREADS |
| 2314 | if (!res) { |
| 2315 | win32_error("mkdir", path); |
| 2316 | PyMem_Free(path); |
| 2317 | return NULL; |
| 2318 | } |
| 2319 | PyMem_Free(path); |
| 2320 | Py_INCREF(Py_None); |
| 2321 | return Py_None; |
| 2322 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2323 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2324 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2325 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2326 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2327 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2328 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2329 | res = mkdir(path); |
| 2330 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2331 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2332 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2333 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2334 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2335 | return posix_error_with_allocated_filename(path); |
| 2336 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2337 | Py_INCREF(Py_None); |
| 2338 | return Py_None; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2339 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2342 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2343 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2344 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2345 | #include <sys/resource.h> |
| 2346 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2347 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2348 | |
| 2349 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2350 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2351 | "nice(inc) -> new_priority\n\n\ |
| 2352 | 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] | 2353 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2354 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2355 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2356 | { |
| 2357 | int increment, value; |
| 2358 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2359 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2360 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2361 | |
| 2362 | /* There are two flavours of 'nice': one that returns the new |
| 2363 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2364 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2365 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2366 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2367 | If we are of the nice family that returns the new priority, we |
| 2368 | need to clear errno before the call, and check if errno is filled |
| 2369 | before calling posix_error() on a returnvalue of -1, because the |
| 2370 | -1 may be the actual new priority! */ |
| 2371 | |
| 2372 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2373 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2374 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2375 | if (value == 0) |
| 2376 | value = getpriority(PRIO_PROCESS, 0); |
| 2377 | #endif |
| 2378 | if (value == -1 && errno != 0) |
| 2379 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2380 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2381 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2382 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2383 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2384 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2385 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2386 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2387 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2388 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2389 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2390 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2391 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2392 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2393 | PyObject *o1, *o2; |
| 2394 | char *p1, *p2; |
| 2395 | BOOL result; |
| 2396 | if (unicode_file_names()) { |
| 2397 | if (!PyArg_ParseTuple(args, "O&O&:rename", |
| 2398 | convert_to_unicode, &o1, |
| 2399 | convert_to_unicode, &o2)) |
| 2400 | PyErr_Clear(); |
| 2401 | else { |
| 2402 | Py_BEGIN_ALLOW_THREADS |
| 2403 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2404 | PyUnicode_AsUnicode(o2)); |
| 2405 | Py_END_ALLOW_THREADS |
| 2406 | Py_DECREF(o1); |
| 2407 | Py_DECREF(o2); |
| 2408 | if (!result) |
| 2409 | return win32_error("rename", NULL); |
| 2410 | Py_INCREF(Py_None); |
| 2411 | return Py_None; |
| 2412 | } |
| 2413 | } |
| 2414 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2415 | return NULL; |
| 2416 | Py_BEGIN_ALLOW_THREADS |
| 2417 | result = MoveFileA(p1, p2); |
| 2418 | Py_END_ALLOW_THREADS |
| 2419 | if (!result) |
| 2420 | return win32_error("rename", NULL); |
| 2421 | Py_INCREF(Py_None); |
| 2422 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2423 | #else |
Martin v. Löwis | 4fc2bda | 2006-05-04 12:04:27 +0000 | [diff] [blame] | 2424 | return posix_2str(args, "etet:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2425 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2428 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2429 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2430 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2431 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2432 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2433 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2434 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2435 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2436 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2437 | return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2438 | #else |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2439 | return posix_1str(args, "et:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2440 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2443 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2444 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2445 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2446 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2447 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2448 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2449 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2450 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2451 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2452 | 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] | 2453 | #else |
| 2454 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 2455 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2458 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2459 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2460 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2461 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2462 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2463 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2464 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2465 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2466 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2467 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2468 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2469 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2470 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2471 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2472 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2473 | Py_END_ALLOW_THREADS |
| 2474 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2475 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2476 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2477 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2478 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2479 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2480 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2481 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2482 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2483 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2484 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2485 | { |
| 2486 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2487 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2488 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2489 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2490 | if (i < 0) |
| 2491 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2492 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2495 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2496 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2497 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2498 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2499 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2500 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2501 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2502 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2503 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2504 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2505 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2506 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2507 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2508 | return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2509 | #else |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2510 | return posix_1str(args, "et:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2511 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2514 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2515 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2516 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2517 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2518 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2519 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2520 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2521 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2522 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2523 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2524 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2525 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2526 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2527 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2528 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2529 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2530 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2531 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2532 | u.sysname, |
| 2533 | u.nodename, |
| 2534 | u.release, |
| 2535 | u.version, |
| 2536 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2537 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2538 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2539 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2540 | static int |
| 2541 | extract_time(PyObject *t, long* sec, long* usec) |
| 2542 | { |
| 2543 | long intval; |
| 2544 | if (PyFloat_Check(t)) { |
| 2545 | double tval = PyFloat_AsDouble(t); |
| 2546 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 2547 | if (!intobj) |
| 2548 | return -1; |
| 2549 | intval = PyInt_AsLong(intobj); |
| 2550 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2551 | if (intval == -1 && PyErr_Occurred()) |
| 2552 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2553 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2554 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2555 | if (*usec < 0) |
| 2556 | /* If rounding gave us a negative number, |
| 2557 | truncate. */ |
| 2558 | *usec = 0; |
| 2559 | return 0; |
| 2560 | } |
| 2561 | intval = PyInt_AsLong(t); |
| 2562 | if (intval == -1 && PyErr_Occurred()) |
| 2563 | return -1; |
| 2564 | *sec = intval; |
| 2565 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2566 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2567 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2568 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2569 | PyDoc_STRVAR(posix_utime__doc__, |
Georg Brandl | 9e5b5e4 | 2006-05-17 14:18:20 +0000 | [diff] [blame] | 2570 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2571 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2572 | 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] | 2573 | 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] | 2574 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2575 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2576 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2577 | { |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2578 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2579 | PyObject *arg; |
| 2580 | PyUnicodeObject *obwpath; |
| 2581 | wchar_t *wpath = NULL; |
| 2582 | char *apath = NULL; |
| 2583 | HANDLE hFile; |
| 2584 | long atimesec, mtimesec, ausec, musec; |
| 2585 | FILETIME atime, mtime; |
| 2586 | PyObject *result = NULL; |
| 2587 | |
| 2588 | if (unicode_file_names()) { |
| 2589 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2590 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2591 | Py_BEGIN_ALLOW_THREADS |
| 2592 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 2593 | NULL, OPEN_EXISTING, 0, NULL); |
| 2594 | Py_END_ALLOW_THREADS |
| 2595 | if (hFile == INVALID_HANDLE_VALUE) |
| 2596 | return win32_error_unicode("utime", wpath); |
| 2597 | } else |
| 2598 | /* Drop the argument parsing error as narrow strings |
| 2599 | are also valid. */ |
| 2600 | PyErr_Clear(); |
| 2601 | } |
| 2602 | if (!wpath) { |
| 2603 | if (!PyArg_ParseTuple(args, "etO:utime", |
| 2604 | Py_FileSystemDefaultEncoding, &apath, &arg)) |
| 2605 | return NULL; |
| 2606 | Py_BEGIN_ALLOW_THREADS |
| 2607 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 2608 | NULL, OPEN_EXISTING, 0, NULL); |
| 2609 | Py_END_ALLOW_THREADS |
| 2610 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2611 | win32_error("utime", apath); |
| 2612 | PyMem_Free(apath); |
| 2613 | return NULL; |
| 2614 | } |
| 2615 | PyMem_Free(apath); |
| 2616 | } |
| 2617 | |
| 2618 | if (arg == Py_None) { |
| 2619 | SYSTEMTIME now; |
| 2620 | GetSystemTime(&now); |
| 2621 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2622 | !SystemTimeToFileTime(&now, &atime)) { |
| 2623 | win32_error("utime", NULL); |
| 2624 | goto done; |
| 2625 | } |
| 2626 | } |
| 2627 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2628 | PyErr_SetString(PyExc_TypeError, |
| 2629 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2630 | goto done; |
| 2631 | } |
| 2632 | else { |
| 2633 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2634 | &atimesec, &ausec) == -1) |
| 2635 | goto done; |
Martin v. Löwis | 463a42b | 2006-10-09 20:44:50 +0000 | [diff] [blame] | 2636 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2637 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2638 | &mtimesec, &musec) == -1) |
| 2639 | goto done; |
Martin v. Löwis | 463a42b | 2006-10-09 20:44:50 +0000 | [diff] [blame] | 2640 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2641 | } |
| 2642 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2643 | /* Avoid putting the file name into the error here, |
| 2644 | as that may confuse the user into believing that |
| 2645 | something is wrong with the file, when it also |
| 2646 | could be the time stamp that gives a problem. */ |
| 2647 | win32_error("utime", NULL); |
| 2648 | } |
| 2649 | Py_INCREF(Py_None); |
| 2650 | result = Py_None; |
| 2651 | done: |
| 2652 | CloseHandle(hFile); |
| 2653 | return result; |
| 2654 | #else /* Py_WIN_WIDE_FILENAMES */ |
| 2655 | |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2656 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2657 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2658 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2659 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2660 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2661 | #if defined(HAVE_UTIMES) |
| 2662 | struct timeval buf[2]; |
| 2663 | #define ATIME buf[0].tv_sec |
| 2664 | #define MTIME buf[1].tv_sec |
| 2665 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2666 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2667 | struct utimbuf buf; |
| 2668 | #define ATIME buf.actime |
| 2669 | #define MTIME buf.modtime |
| 2670 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2671 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2672 | time_t buf[2]; |
| 2673 | #define ATIME buf[0] |
| 2674 | #define MTIME buf[1] |
| 2675 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2676 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2677 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2678 | |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2679 | if (!PyArg_ParseTuple(args, "etO:utime", |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2680 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2681 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2682 | if (arg == Py_None) { |
| 2683 | /* optional time values not given */ |
| 2684 | Py_BEGIN_ALLOW_THREADS |
| 2685 | res = utime(path, NULL); |
| 2686 | Py_END_ALLOW_THREADS |
| 2687 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2688 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2689 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2690 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2691 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2692 | return NULL; |
| 2693 | } |
| 2694 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2695 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2696 | &atime, &ausec) == -1) { |
| 2697 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2698 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2699 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2700 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2701 | &mtime, &musec) == -1) { |
| 2702 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2703 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2704 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2705 | ATIME = atime; |
| 2706 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2707 | #ifdef HAVE_UTIMES |
| 2708 | buf[0].tv_usec = ausec; |
| 2709 | buf[1].tv_usec = musec; |
| 2710 | Py_BEGIN_ALLOW_THREADS |
| 2711 | res = utimes(path, buf); |
| 2712 | Py_END_ALLOW_THREADS |
| 2713 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2714 | Py_BEGIN_ALLOW_THREADS |
| 2715 | res = utime(path, UTIME_ARG); |
| 2716 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2717 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2718 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2719 | if (res < 0) { |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2720 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2721 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2722 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2723 | Py_INCREF(Py_None); |
| 2724 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2725 | #undef UTIME_ARG |
| 2726 | #undef ATIME |
| 2727 | #undef MTIME |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2728 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2731 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2732 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2733 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2734 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2735 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2736 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2737 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2738 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2739 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2740 | { |
| 2741 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2742 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2743 | return NULL; |
| 2744 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2745 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2748 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2749 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2750 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2751 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2752 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2753 | for (i = 0; i < count; i++) |
| 2754 | PyMem_Free(array[i]); |
| 2755 | PyMem_DEL(array); |
| 2756 | } |
| 2757 | #endif |
| 2758 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2759 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2760 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2761 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2762 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2763 | Execute an executable path with arguments, replacing current process.\n\ |
| 2764 | \n\ |
| 2765 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2766 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2767 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2768 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2769 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2770 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2771 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2772 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2773 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2774 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2775 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2776 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2777 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2778 | argv is a list or tuple of strings. */ |
| 2779 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2780 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2781 | Py_FileSystemDefaultEncoding, |
| 2782 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2783 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2784 | if (PyList_Check(argv)) { |
| 2785 | argc = PyList_Size(argv); |
| 2786 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2787 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2788 | else if (PyTuple_Check(argv)) { |
| 2789 | argc = PyTuple_Size(argv); |
| 2790 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2791 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2792 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2793 | 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] | 2794 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2795 | return NULL; |
| 2796 | } |
| 2797 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2798 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2799 | if (argvlist == NULL) { |
| 2800 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2801 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2802 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2803 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2804 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2805 | Py_FileSystemDefaultEncoding, |
| 2806 | &argvlist[i])) { |
| 2807 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2808 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2809 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2810 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2811 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2812 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2813 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2814 | } |
| 2815 | argvlist[argc] = NULL; |
| 2816 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2817 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2818 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2819 | /* If we get here it's definitely an error */ |
| 2820 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2821 | free_string_array(argvlist, argc); |
| 2822 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2823 | return posix_error(); |
| 2824 | } |
| 2825 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2826 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2827 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2828 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2829 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2830 | \n\ |
| 2831 | path: path of executable file\n\ |
| 2832 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2833 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2834 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2835 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2836 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2837 | { |
| 2838 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2839 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2840 | char **argvlist; |
| 2841 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2842 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2843 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2844 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 2845 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2846 | |
| 2847 | /* execve has three arguments: (path, argv, env), where |
| 2848 | argv is a list or tuple of strings and env is a dictionary |
| 2849 | like posix.environ. */ |
| 2850 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2851 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2852 | Py_FileSystemDefaultEncoding, |
| 2853 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2854 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2855 | if (PyList_Check(argv)) { |
| 2856 | argc = PyList_Size(argv); |
| 2857 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2858 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2859 | else if (PyTuple_Check(argv)) { |
| 2860 | argc = PyTuple_Size(argv); |
| 2861 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2862 | } |
| 2863 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2864 | PyErr_SetString(PyExc_TypeError, |
| 2865 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2866 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2867 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2868 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2869 | PyErr_SetString(PyExc_TypeError, |
| 2870 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2871 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2874 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2875 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2876 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2877 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2878 | } |
| 2879 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2880 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2881 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2882 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2883 | &argvlist[i])) |
| 2884 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2885 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2886 | goto fail_1; |
| 2887 | } |
| 2888 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2889 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2890 | argvlist[argc] = NULL; |
| 2891 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2892 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2893 | if (i < 0) |
| 2894 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2895 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2896 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2897 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2898 | goto fail_1; |
| 2899 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2900 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2901 | keys = PyMapping_Keys(env); |
| 2902 | vals = PyMapping_Values(env); |
| 2903 | if (!keys || !vals) |
| 2904 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2905 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2906 | PyErr_SetString(PyExc_TypeError, |
| 2907 | "execve(): env.keys() or env.values() is not a list"); |
| 2908 | goto fail_2; |
| 2909 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2910 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2911 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2912 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2913 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2914 | |
| 2915 | key = PyList_GetItem(keys, pos); |
| 2916 | val = PyList_GetItem(vals, pos); |
| 2917 | if (!key || !val) |
| 2918 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2919 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2920 | if (!PyArg_Parse( |
| 2921 | key, |
| 2922 | "s;execve() arg 3 contains a non-string key", |
| 2923 | &k) || |
| 2924 | !PyArg_Parse( |
| 2925 | val, |
| 2926 | "s;execve() arg 3 contains a non-string value", |
| 2927 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2928 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2929 | goto fail_2; |
| 2930 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2931 | |
| 2932 | #if defined(PYOS_OS2) |
| 2933 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2934 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2935 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2936 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2937 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2938 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2939 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2940 | goto fail_2; |
| 2941 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2942 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2943 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2944 | #if defined(PYOS_OS2) |
| 2945 | } |
| 2946 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2947 | } |
| 2948 | envlist[envc] = 0; |
| 2949 | |
| 2950 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2951 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2952 | /* If we get here it's definitely an error */ |
| 2953 | |
| 2954 | (void) posix_error(); |
| 2955 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2956 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2957 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2958 | PyMem_DEL(envlist[envc]); |
| 2959 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2960 | fail_1: |
| 2961 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2962 | Py_XDECREF(vals); |
| 2963 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2964 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2965 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2966 | return NULL; |
| 2967 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2968 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2969 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2970 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2971 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2972 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2973 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2974 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2975 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2976 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2977 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2978 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2979 | |
| 2980 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2981 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2982 | { |
| 2983 | char *path; |
| 2984 | PyObject *argv; |
| 2985 | char **argvlist; |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 2986 | int mode, i; |
| 2987 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 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); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2990 | |
| 2991 | /* spawnv has three arguments: (mode, path, argv), where |
| 2992 | argv is a list or tuple of strings. */ |
| 2993 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2994 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2995 | Py_FileSystemDefaultEncoding, |
| 2996 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2997 | return NULL; |
| 2998 | if (PyList_Check(argv)) { |
| 2999 | argc = PyList_Size(argv); |
| 3000 | getitem = PyList_GetItem; |
| 3001 | } |
| 3002 | else if (PyTuple_Check(argv)) { |
| 3003 | argc = PyTuple_Size(argv); |
| 3004 | getitem = PyTuple_GetItem; |
| 3005 | } |
| 3006 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3007 | PyErr_SetString(PyExc_TypeError, |
| 3008 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3009 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3010 | return NULL; |
| 3011 | } |
| 3012 | |
| 3013 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3014 | if (argvlist == NULL) { |
| 3015 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3016 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3017 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3018 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3019 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3020 | Py_FileSystemDefaultEncoding, |
| 3021 | &argvlist[i])) { |
| 3022 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3023 | PyErr_SetString( |
| 3024 | PyExc_TypeError, |
| 3025 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3026 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 3027 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3028 | } |
| 3029 | } |
| 3030 | argvlist[argc] = NULL; |
| 3031 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3032 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3033 | Py_BEGIN_ALLOW_THREADS |
| 3034 | spawnval = spawnv(mode, path, argvlist); |
| 3035 | Py_END_ALLOW_THREADS |
| 3036 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3037 | if (mode == _OLD_P_OVERLAY) |
| 3038 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3039 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3040 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3041 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3042 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3043 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3044 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3045 | free_string_array(argvlist, argc); |
| 3046 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3047 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3048 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3049 | return posix_error(); |
| 3050 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3051 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3052 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3053 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3054 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3055 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3059 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3060 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3061 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3062 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3063 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3064 | path: path of executable file\n\ |
| 3065 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3066 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3067 | |
| 3068 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3069 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3070 | { |
| 3071 | char *path; |
| 3072 | PyObject *argv, *env; |
| 3073 | char **argvlist; |
| 3074 | char **envlist; |
| 3075 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 3076 | int mode, pos, envc; |
| 3077 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3078 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3079 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 3080 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3081 | |
| 3082 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3083 | argv is a list or tuple of strings and env is a dictionary |
| 3084 | like posix.environ. */ |
| 3085 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3086 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 3087 | Py_FileSystemDefaultEncoding, |
| 3088 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3089 | return NULL; |
| 3090 | if (PyList_Check(argv)) { |
| 3091 | argc = PyList_Size(argv); |
| 3092 | getitem = PyList_GetItem; |
| 3093 | } |
| 3094 | else if (PyTuple_Check(argv)) { |
| 3095 | argc = PyTuple_Size(argv); |
| 3096 | getitem = PyTuple_GetItem; |
| 3097 | } |
| 3098 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3099 | PyErr_SetString(PyExc_TypeError, |
| 3100 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3101 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3102 | } |
| 3103 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3104 | PyErr_SetString(PyExc_TypeError, |
| 3105 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3106 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | argvlist = PyMem_NEW(char *, argc+1); |
| 3110 | if (argvlist == NULL) { |
| 3111 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3112 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3113 | } |
| 3114 | for (i = 0; i < argc; i++) { |
| 3115 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3116 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3117 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3118 | &argvlist[i])) |
| 3119 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3120 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3121 | goto fail_1; |
| 3122 | } |
| 3123 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3124 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3125 | argvlist[argc] = NULL; |
| 3126 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3127 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3128 | if (i < 0) |
| 3129 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3130 | envlist = PyMem_NEW(char *, i + 1); |
| 3131 | if (envlist == NULL) { |
| 3132 | PyErr_NoMemory(); |
| 3133 | goto fail_1; |
| 3134 | } |
| 3135 | envc = 0; |
| 3136 | keys = PyMapping_Keys(env); |
| 3137 | vals = PyMapping_Values(env); |
| 3138 | if (!keys || !vals) |
| 3139 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3140 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3141 | PyErr_SetString(PyExc_TypeError, |
| 3142 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3143 | goto fail_2; |
| 3144 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3145 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3146 | for (pos = 0; pos < i; pos++) { |
| 3147 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3148 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3149 | |
| 3150 | key = PyList_GetItem(keys, pos); |
| 3151 | val = PyList_GetItem(vals, pos); |
| 3152 | if (!key || !val) |
| 3153 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3154 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3155 | if (!PyArg_Parse( |
| 3156 | key, |
| 3157 | "s;spawnve() arg 3 contains a non-string key", |
| 3158 | &k) || |
| 3159 | !PyArg_Parse( |
| 3160 | val, |
| 3161 | "s;spawnve() arg 3 contains a non-string value", |
| 3162 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3163 | { |
| 3164 | goto fail_2; |
| 3165 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3166 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3167 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3168 | if (p == NULL) { |
| 3169 | PyErr_NoMemory(); |
| 3170 | goto fail_2; |
| 3171 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3172 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3173 | envlist[envc++] = p; |
| 3174 | } |
| 3175 | envlist[envc] = 0; |
| 3176 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3177 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3178 | Py_BEGIN_ALLOW_THREADS |
| 3179 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3180 | Py_END_ALLOW_THREADS |
| 3181 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3182 | if (mode == _OLD_P_OVERLAY) |
| 3183 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3184 | |
| 3185 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3186 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3187 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3188 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3189 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3190 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3191 | (void) posix_error(); |
| 3192 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3193 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3194 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3195 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3196 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3197 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3198 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3199 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3200 | while (--envc >= 0) |
| 3201 | PyMem_DEL(envlist[envc]); |
| 3202 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3203 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3204 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3205 | Py_XDECREF(vals); |
| 3206 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3207 | fail_0: |
| 3208 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3209 | return res; |
| 3210 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3211 | |
| 3212 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3213 | #if defined(PYOS_OS2) |
| 3214 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3215 | "spawnvp(mode, file, args)\n\n\ |
| 3216 | Execute the program 'file' in a new process, using the environment\n\ |
| 3217 | search path to find the file.\n\ |
| 3218 | \n\ |
| 3219 | mode: mode of process creation\n\ |
| 3220 | file: executable file name\n\ |
| 3221 | args: tuple or list of strings"); |
| 3222 | |
| 3223 | static PyObject * |
| 3224 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3225 | { |
| 3226 | char *path; |
| 3227 | PyObject *argv; |
| 3228 | char **argvlist; |
| 3229 | int mode, i, argc; |
| 3230 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3231 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3232 | |
| 3233 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3234 | argv is a list or tuple of strings. */ |
| 3235 | |
| 3236 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 3237 | Py_FileSystemDefaultEncoding, |
| 3238 | &path, &argv)) |
| 3239 | return NULL; |
| 3240 | if (PyList_Check(argv)) { |
| 3241 | argc = PyList_Size(argv); |
| 3242 | getitem = PyList_GetItem; |
| 3243 | } |
| 3244 | else if (PyTuple_Check(argv)) { |
| 3245 | argc = PyTuple_Size(argv); |
| 3246 | getitem = PyTuple_GetItem; |
| 3247 | } |
| 3248 | else { |
| 3249 | PyErr_SetString(PyExc_TypeError, |
| 3250 | "spawnvp() arg 2 must be a tuple or list"); |
| 3251 | PyMem_Free(path); |
| 3252 | return NULL; |
| 3253 | } |
| 3254 | |
| 3255 | argvlist = PyMem_NEW(char *, argc+1); |
| 3256 | if (argvlist == NULL) { |
| 3257 | PyMem_Free(path); |
| 3258 | return PyErr_NoMemory(); |
| 3259 | } |
| 3260 | for (i = 0; i < argc; i++) { |
| 3261 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3262 | Py_FileSystemDefaultEncoding, |
| 3263 | &argvlist[i])) { |
| 3264 | free_string_array(argvlist, i); |
| 3265 | PyErr_SetString( |
| 3266 | PyExc_TypeError, |
| 3267 | "spawnvp() arg 2 must contain only strings"); |
| 3268 | PyMem_Free(path); |
| 3269 | return NULL; |
| 3270 | } |
| 3271 | } |
| 3272 | argvlist[argc] = NULL; |
| 3273 | |
| 3274 | Py_BEGIN_ALLOW_THREADS |
| 3275 | #if defined(PYCC_GCC) |
| 3276 | spawnval = spawnvp(mode, path, argvlist); |
| 3277 | #else |
| 3278 | spawnval = _spawnvp(mode, path, argvlist); |
| 3279 | #endif |
| 3280 | Py_END_ALLOW_THREADS |
| 3281 | |
| 3282 | free_string_array(argvlist, argc); |
| 3283 | PyMem_Free(path); |
| 3284 | |
| 3285 | if (spawnval == -1) |
| 3286 | return posix_error(); |
| 3287 | else |
| 3288 | return Py_BuildValue("l", (long) spawnval); |
| 3289 | } |
| 3290 | |
| 3291 | |
| 3292 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3293 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3294 | Execute the program 'file' in a new process, using the environment\n\ |
| 3295 | search path to find the file.\n\ |
| 3296 | \n\ |
| 3297 | mode: mode of process creation\n\ |
| 3298 | file: executable file name\n\ |
| 3299 | args: tuple or list of arguments\n\ |
| 3300 | env: dictionary of strings mapping to strings"); |
| 3301 | |
| 3302 | static PyObject * |
| 3303 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3304 | { |
| 3305 | char *path; |
| 3306 | PyObject *argv, *env; |
| 3307 | char **argvlist; |
| 3308 | char **envlist; |
| 3309 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3310 | int mode, i, pos, argc, envc; |
| 3311 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3312 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3313 | int lastarg = 0; |
| 3314 | |
| 3315 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3316 | argv is a list or tuple of strings and env is a dictionary |
| 3317 | like posix.environ. */ |
| 3318 | |
| 3319 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 3320 | Py_FileSystemDefaultEncoding, |
| 3321 | &path, &argv, &env)) |
| 3322 | return NULL; |
| 3323 | if (PyList_Check(argv)) { |
| 3324 | argc = PyList_Size(argv); |
| 3325 | getitem = PyList_GetItem; |
| 3326 | } |
| 3327 | else if (PyTuple_Check(argv)) { |
| 3328 | argc = PyTuple_Size(argv); |
| 3329 | getitem = PyTuple_GetItem; |
| 3330 | } |
| 3331 | else { |
| 3332 | PyErr_SetString(PyExc_TypeError, |
| 3333 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3334 | goto fail_0; |
| 3335 | } |
| 3336 | if (!PyMapping_Check(env)) { |
| 3337 | PyErr_SetString(PyExc_TypeError, |
| 3338 | "spawnvpe() arg 3 must be a mapping object"); |
| 3339 | goto fail_0; |
| 3340 | } |
| 3341 | |
| 3342 | argvlist = PyMem_NEW(char *, argc+1); |
| 3343 | if (argvlist == NULL) { |
| 3344 | PyErr_NoMemory(); |
| 3345 | goto fail_0; |
| 3346 | } |
| 3347 | for (i = 0; i < argc; i++) { |
| 3348 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3349 | "et;spawnvpe() arg 2 must contain only strings", |
| 3350 | Py_FileSystemDefaultEncoding, |
| 3351 | &argvlist[i])) |
| 3352 | { |
| 3353 | lastarg = i; |
| 3354 | goto fail_1; |
| 3355 | } |
| 3356 | } |
| 3357 | lastarg = argc; |
| 3358 | argvlist[argc] = NULL; |
| 3359 | |
| 3360 | i = PyMapping_Size(env); |
| 3361 | if (i < 0) |
| 3362 | goto fail_1; |
| 3363 | envlist = PyMem_NEW(char *, i + 1); |
| 3364 | if (envlist == NULL) { |
| 3365 | PyErr_NoMemory(); |
| 3366 | goto fail_1; |
| 3367 | } |
| 3368 | envc = 0; |
| 3369 | keys = PyMapping_Keys(env); |
| 3370 | vals = PyMapping_Values(env); |
| 3371 | if (!keys || !vals) |
| 3372 | goto fail_2; |
| 3373 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3374 | PyErr_SetString(PyExc_TypeError, |
| 3375 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3376 | goto fail_2; |
| 3377 | } |
| 3378 | |
| 3379 | for (pos = 0; pos < i; pos++) { |
| 3380 | char *p, *k, *v; |
| 3381 | size_t len; |
| 3382 | |
| 3383 | key = PyList_GetItem(keys, pos); |
| 3384 | val = PyList_GetItem(vals, pos); |
| 3385 | if (!key || !val) |
| 3386 | goto fail_2; |
| 3387 | |
| 3388 | if (!PyArg_Parse( |
| 3389 | key, |
| 3390 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3391 | &k) || |
| 3392 | !PyArg_Parse( |
| 3393 | val, |
| 3394 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3395 | &v)) |
| 3396 | { |
| 3397 | goto fail_2; |
| 3398 | } |
| 3399 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3400 | p = PyMem_NEW(char, len); |
| 3401 | if (p == NULL) { |
| 3402 | PyErr_NoMemory(); |
| 3403 | goto fail_2; |
| 3404 | } |
| 3405 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3406 | envlist[envc++] = p; |
| 3407 | } |
| 3408 | envlist[envc] = 0; |
| 3409 | |
| 3410 | Py_BEGIN_ALLOW_THREADS |
| 3411 | #if defined(PYCC_GCC) |
| 3412 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3413 | #else |
| 3414 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3415 | #endif |
| 3416 | Py_END_ALLOW_THREADS |
| 3417 | |
| 3418 | if (spawnval == -1) |
| 3419 | (void) posix_error(); |
| 3420 | else |
| 3421 | res = Py_BuildValue("l", (long) spawnval); |
| 3422 | |
| 3423 | fail_2: |
| 3424 | while (--envc >= 0) |
| 3425 | PyMem_DEL(envlist[envc]); |
| 3426 | PyMem_DEL(envlist); |
| 3427 | fail_1: |
| 3428 | free_string_array(argvlist, lastarg); |
| 3429 | Py_XDECREF(vals); |
| 3430 | Py_XDECREF(keys); |
| 3431 | fail_0: |
| 3432 | PyMem_Free(path); |
| 3433 | return res; |
| 3434 | } |
| 3435 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3436 | #endif /* HAVE_SPAWNV */ |
| 3437 | |
| 3438 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3439 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3440 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3441 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3442 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3443 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3444 | 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] | 3445 | |
| 3446 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3447 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3448 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3449 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3450 | if (pid == -1) |
| 3451 | return posix_error(); |
| 3452 | PyOS_AfterFork(); |
| 3453 | return PyInt_FromLong((long)pid); |
| 3454 | } |
| 3455 | #endif |
| 3456 | |
| 3457 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3458 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3459 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3460 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3461 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3462 | 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] | 3463 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3464 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3465 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3466 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3467 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3468 | if (pid == -1) |
| 3469 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3470 | if (pid == 0) |
| 3471 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3472 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3473 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3474 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3475 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3476 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3477 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3478 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3479 | #define DEV_PTY_FILE "/dev/ptc" |
| 3480 | #define HAVE_DEV_PTMX |
| 3481 | #else |
| 3482 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3483 | #endif |
| 3484 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3485 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3486 | #ifdef HAVE_PTY_H |
| 3487 | #include <pty.h> |
| 3488 | #else |
| 3489 | #ifdef HAVE_LIBUTIL_H |
| 3490 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3491 | #endif /* HAVE_LIBUTIL_H */ |
| 3492 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3493 | #ifdef HAVE_STROPTS_H |
| 3494 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3495 | #endif |
| 3496 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3497 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3498 | #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] | 3499 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3500 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3501 | 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] | 3502 | |
| 3503 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3504 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3505 | { |
| 3506 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3507 | #ifndef HAVE_OPENPTY |
| 3508 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3509 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3510 | #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] | 3511 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3512 | #ifdef sun |
Neal Norwitz | 84a98e0 | 2006-04-10 07:44:23 +0000 | [diff] [blame] | 3513 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3514 | #endif |
| 3515 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3516 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3517 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3518 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3519 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3520 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3521 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3522 | if (slave_name == NULL) |
| 3523 | return posix_error(); |
| 3524 | |
| 3525 | slave_fd = open(slave_name, O_RDWR); |
| 3526 | if (slave_fd < 0) |
| 3527 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3528 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3529 | 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] | 3530 | if (master_fd < 0) |
| 3531 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3532 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3533 | /* change permission of slave */ |
| 3534 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3535 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3536 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3537 | } |
| 3538 | /* unlock slave */ |
| 3539 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3540 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3541 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3542 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3543 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3544 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3545 | if (slave_name == NULL) |
| 3546 | return posix_error(); |
| 3547 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3548 | if (slave_fd < 0) |
| 3549 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3550 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3551 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3552 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3553 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3554 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3555 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3556 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3557 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3558 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3559 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3560 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3561 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3562 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3563 | |
| 3564 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3565 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3566 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3567 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3568 | 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] | 3569 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3570 | |
| 3571 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3572 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3573 | { |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 3574 | int master_fd = -1, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3575 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3576 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3577 | if (pid == -1) |
| 3578 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3579 | if (pid == 0) |
| 3580 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3581 | return Py_BuildValue("(ii)", pid, master_fd); |
| 3582 | } |
| 3583 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3584 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3585 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3586 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3587 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3588 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3589 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3590 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3591 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3592 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3593 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3594 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3595 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3596 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3597 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3598 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3599 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3600 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3601 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3602 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3603 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3604 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3605 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3606 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3607 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3608 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3609 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3610 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3611 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3612 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3613 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3614 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3615 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3616 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3617 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3618 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3619 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3620 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3621 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3622 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3623 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3624 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3625 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3626 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3627 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3628 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3629 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3630 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3631 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3634 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3635 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3636 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3637 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3638 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3639 | |
| 3640 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3641 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3642 | { |
| 3643 | PyObject *result = NULL; |
| 3644 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3645 | #ifdef NGROUPS_MAX |
| 3646 | #define MAX_GROUPS NGROUPS_MAX |
| 3647 | #else |
| 3648 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3649 | #define MAX_GROUPS 64 |
| 3650 | #endif |
| 3651 | gid_t grouplist[MAX_GROUPS]; |
| 3652 | int n; |
| 3653 | |
| 3654 | n = getgroups(MAX_GROUPS, grouplist); |
| 3655 | if (n < 0) |
| 3656 | posix_error(); |
| 3657 | else { |
| 3658 | result = PyList_New(n); |
| 3659 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3660 | int i; |
| 3661 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3662 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3663 | if (o == NULL) { |
| 3664 | Py_DECREF(result); |
| 3665 | result = NULL; |
| 3666 | break; |
| 3667 | } |
| 3668 | PyList_SET_ITEM(result, i, o); |
| 3669 | } |
| 3670 | } |
| 3671 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3672 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3673 | return result; |
| 3674 | } |
| 3675 | #endif |
| 3676 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3677 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3678 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3679 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3680 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3681 | |
| 3682 | static PyObject * |
| 3683 | posix_getpgid(PyObject *self, PyObject *args) |
| 3684 | { |
| 3685 | int pid, pgid; |
| 3686 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3687 | return NULL; |
| 3688 | pgid = getpgid(pid); |
| 3689 | if (pgid < 0) |
| 3690 | return posix_error(); |
| 3691 | return PyInt_FromLong((long)pgid); |
| 3692 | } |
| 3693 | #endif /* HAVE_GETPGID */ |
| 3694 | |
| 3695 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3696 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3697 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3698 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3699 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3700 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3701 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3702 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3703 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3704 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3705 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3706 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3707 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3708 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3709 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3710 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3711 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3712 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3713 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3714 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3715 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3716 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3717 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3718 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3719 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3720 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3721 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3722 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3723 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3724 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3725 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3726 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3727 | Py_INCREF(Py_None); |
| 3728 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3731 | #endif /* HAVE_SETPGRP */ |
| 3732 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3733 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3734 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3735 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3736 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3737 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3738 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3739 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3740 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3741 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3742 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3743 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3744 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3745 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3746 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3747 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3748 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3749 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3750 | |
| 3751 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3752 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3753 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3754 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3755 | char *name; |
| 3756 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3757 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3758 | errno = 0; |
| 3759 | name = getlogin(); |
| 3760 | if (name == NULL) { |
| 3761 | if (errno) |
| 3762 | posix_error(); |
| 3763 | else |
| 3764 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3765 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3766 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3767 | else |
| 3768 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3769 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3770 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3771 | return result; |
| 3772 | } |
| 3773 | #endif |
| 3774 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3775 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3776 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3777 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3778 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3779 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3780 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3781 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3782 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3783 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3784 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3785 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3786 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3787 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3788 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3789 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3790 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3791 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3792 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3793 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3794 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3795 | { |
| 3796 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3797 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3798 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3799 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3800 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3801 | APIRET rc; |
| 3802 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3803 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3804 | |
| 3805 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3806 | APIRET rc; |
| 3807 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3808 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3809 | |
| 3810 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3811 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3812 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3813 | if (kill(pid, sig) == -1) |
| 3814 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3815 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3816 | Py_INCREF(Py_None); |
| 3817 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3818 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3819 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3820 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3821 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3822 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3823 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3824 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3825 | |
| 3826 | static PyObject * |
| 3827 | posix_killpg(PyObject *self, PyObject *args) |
| 3828 | { |
| 3829 | int pgid, sig; |
| 3830 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3831 | return NULL; |
| 3832 | if (killpg(pgid, sig) == -1) |
| 3833 | return posix_error(); |
| 3834 | Py_INCREF(Py_None); |
| 3835 | return Py_None; |
| 3836 | } |
| 3837 | #endif |
| 3838 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3839 | #ifdef HAVE_PLOCK |
| 3840 | |
| 3841 | #ifdef HAVE_SYS_LOCK_H |
| 3842 | #include <sys/lock.h> |
| 3843 | #endif |
| 3844 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3845 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3846 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3847 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3848 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3849 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3850 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3851 | { |
| 3852 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3853 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3854 | return NULL; |
| 3855 | if (plock(op) == -1) |
| 3856 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3857 | Py_INCREF(Py_None); |
| 3858 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3859 | } |
| 3860 | #endif |
| 3861 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3862 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3863 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3864 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3865 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3866 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3867 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3868 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3869 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3870 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3871 | async_system(const char *command) |
| 3872 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3873 | char errormsg[256], args[1024]; |
| 3874 | RESULTCODES rcodes; |
| 3875 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3876 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3877 | char *shell = getenv("COMSPEC"); |
| 3878 | if (!shell) |
| 3879 | shell = "cmd"; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3880 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3881 | /* avoid overflowing the argument buffer */ |
| 3882 | if (strlen(shell) + 3 + strlen(command) >= 1024) |
| 3883 | return ERROR_NOT_ENOUGH_MEMORY |
| 3884 | |
| 3885 | args[0] = '\0'; |
| 3886 | strcat(args, shell); |
| 3887 | strcat(args, "/c "); |
| 3888 | strcat(args, command); |
| 3889 | |
| 3890 | /* execute asynchronously, inheriting the environment */ |
| 3891 | rc = DosExecPgm(errormsg, |
| 3892 | sizeof(errormsg), |
| 3893 | EXEC_ASYNC, |
| 3894 | args, |
| 3895 | NULL, |
| 3896 | &rcodes, |
| 3897 | shell); |
| 3898 | return rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3901 | static FILE * |
| 3902 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3903 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3904 | int oldfd, tgtfd; |
| 3905 | HFILE pipeh[2]; |
| 3906 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3907 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3908 | /* mode determines which of stdin or stdout is reconnected to |
| 3909 | * the pipe to the child |
| 3910 | */ |
| 3911 | if (strchr(mode, 'r') != NULL) { |
| 3912 | tgt_fd = 1; /* stdout */ |
| 3913 | } else if (strchr(mode, 'w')) { |
| 3914 | tgt_fd = 0; /* stdin */ |
| 3915 | } else { |
| 3916 | *err = ERROR_INVALID_ACCESS; |
| 3917 | return NULL; |
| 3918 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3919 | |
Andrew MacIntyre | a3be258 | 2004-12-18 09:51:05 +0000 | [diff] [blame] | 3920 | /* setup the pipe */ |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3921 | if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) { |
| 3922 | *err = rc; |
| 3923 | return NULL; |
| 3924 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3925 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3926 | /* prevent other threads accessing stdio */ |
| 3927 | DosEnterCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3928 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3929 | /* reconnect stdio and execute child */ |
| 3930 | oldfd = dup(tgtfd); |
| 3931 | close(tgtfd); |
| 3932 | if (dup2(pipeh[tgtfd], tgtfd) == 0) { |
| 3933 | DosClose(pipeh[tgtfd]); |
| 3934 | rc = async_system(command); |
| 3935 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3936 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3937 | /* restore stdio */ |
| 3938 | dup2(oldfd, tgtfd); |
| 3939 | close(oldfd); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3940 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3941 | /* allow other threads access to stdio */ |
| 3942 | DosExitCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3943 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3944 | /* if execution of child was successful return file stream */ |
| 3945 | if (rc == NO_ERROR) |
| 3946 | return fdopen(pipeh[1 - tgtfd], mode); |
| 3947 | else { |
| 3948 | DosClose(pipeh[1 - tgtfd]); |
| 3949 | *err = rc; |
| 3950 | return NULL; |
| 3951 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3952 | } |
| 3953 | |
| 3954 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3955 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3956 | { |
| 3957 | char *name; |
| 3958 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3959 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3960 | FILE *fp; |
| 3961 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3962 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3963 | return NULL; |
| 3964 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3965 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3966 | Py_END_ALLOW_THREADS |
| 3967 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3968 | return os2_error(err); |
| 3969 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3970 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 3971 | if (f != NULL) |
| 3972 | PyFile_SetBufSize(f, bufsize); |
| 3973 | return f; |
| 3974 | } |
| 3975 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3976 | #elif defined(PYCC_GCC) |
| 3977 | |
| 3978 | /* standard posix version of popen() support */ |
| 3979 | static PyObject * |
| 3980 | posix_popen(PyObject *self, PyObject *args) |
| 3981 | { |
| 3982 | char *name; |
| 3983 | char *mode = "r"; |
| 3984 | int bufsize = -1; |
| 3985 | FILE *fp; |
| 3986 | PyObject *f; |
| 3987 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 3988 | return NULL; |
| 3989 | Py_BEGIN_ALLOW_THREADS |
| 3990 | fp = popen(name, mode); |
| 3991 | Py_END_ALLOW_THREADS |
| 3992 | if (fp == NULL) |
| 3993 | return posix_error(); |
| 3994 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 3995 | if (f != NULL) |
| 3996 | PyFile_SetBufSize(f, bufsize); |
| 3997 | return f; |
| 3998 | } |
| 3999 | |
| 4000 | /* fork() under OS/2 has lots'o'warts |
| 4001 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 4002 | * most of this code is a ripoff of the win32 code, but using the |
| 4003 | * capabilities of EMX's C library routines |
| 4004 | */ |
| 4005 | |
| 4006 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 4007 | #define POPEN_1 1 |
| 4008 | #define POPEN_2 2 |
| 4009 | #define POPEN_3 3 |
| 4010 | #define POPEN_4 4 |
| 4011 | |
| 4012 | static PyObject *_PyPopen(char *, int, int, int); |
| 4013 | static int _PyPclose(FILE *file); |
| 4014 | |
| 4015 | /* |
| 4016 | * Internal dictionary mapping popen* file pointers to process handles, |
| 4017 | * for use when retrieving the process exit code. See _PyPclose() below |
| 4018 | * for more information on this dictionary's use. |
| 4019 | */ |
| 4020 | static PyObject *_PyPopenProcs = NULL; |
| 4021 | |
| 4022 | /* os2emx version of popen2() |
| 4023 | * |
| 4024 | * The result of this function is a pipe (file) connected to the |
| 4025 | * process's stdin, and a pipe connected to the process's stdout. |
| 4026 | */ |
| 4027 | |
| 4028 | static PyObject * |
| 4029 | os2emx_popen2(PyObject *self, PyObject *args) |
| 4030 | { |
| 4031 | PyObject *f; |
| 4032 | int tm=0; |
| 4033 | |
| 4034 | char *cmdstring; |
| 4035 | char *mode = "t"; |
| 4036 | int bufsize = -1; |
| 4037 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 4038 | return NULL; |
| 4039 | |
| 4040 | if (*mode == 't') |
| 4041 | tm = O_TEXT; |
| 4042 | else if (*mode != 'b') { |
| 4043 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 4044 | return NULL; |
| 4045 | } else |
| 4046 | tm = O_BINARY; |
| 4047 | |
| 4048 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 4049 | |
| 4050 | return f; |
| 4051 | } |
| 4052 | |
| 4053 | /* |
| 4054 | * Variation on os2emx.popen2 |
| 4055 | * |
| 4056 | * The result of this function is 3 pipes - the process's stdin, |
| 4057 | * stdout and stderr |
| 4058 | */ |
| 4059 | |
| 4060 | static PyObject * |
| 4061 | os2emx_popen3(PyObject *self, PyObject *args) |
| 4062 | { |
| 4063 | PyObject *f; |
| 4064 | int tm = 0; |
| 4065 | |
| 4066 | char *cmdstring; |
| 4067 | char *mode = "t"; |
| 4068 | int bufsize = -1; |
| 4069 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 4070 | return NULL; |
| 4071 | |
| 4072 | if (*mode == 't') |
| 4073 | tm = O_TEXT; |
| 4074 | else if (*mode != 'b') { |
| 4075 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 4076 | return NULL; |
| 4077 | } else |
| 4078 | tm = O_BINARY; |
| 4079 | |
| 4080 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 4081 | |
| 4082 | return f; |
| 4083 | } |
| 4084 | |
| 4085 | /* |
| 4086 | * Variation on os2emx.popen2 |
| 4087 | * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4088 | * The result of this function is 2 pipes - the processes stdin, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4089 | * and stdout+stderr combined as a single pipe. |
| 4090 | */ |
| 4091 | |
| 4092 | static PyObject * |
| 4093 | os2emx_popen4(PyObject *self, PyObject *args) |
| 4094 | { |
| 4095 | PyObject *f; |
| 4096 | int tm = 0; |
| 4097 | |
| 4098 | char *cmdstring; |
| 4099 | char *mode = "t"; |
| 4100 | int bufsize = -1; |
| 4101 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 4102 | return NULL; |
| 4103 | |
| 4104 | if (*mode == 't') |
| 4105 | tm = O_TEXT; |
| 4106 | else if (*mode != 'b') { |
| 4107 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 4108 | return NULL; |
| 4109 | } else |
| 4110 | tm = O_BINARY; |
| 4111 | |
| 4112 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 4113 | |
| 4114 | return f; |
| 4115 | } |
| 4116 | |
| 4117 | /* a couple of structures for convenient handling of multiple |
| 4118 | * file handles and pipes |
| 4119 | */ |
| 4120 | struct file_ref |
| 4121 | { |
| 4122 | int handle; |
| 4123 | int flags; |
| 4124 | }; |
| 4125 | |
| 4126 | struct pipe_ref |
| 4127 | { |
| 4128 | int rd; |
| 4129 | int wr; |
| 4130 | }; |
| 4131 | |
| 4132 | /* The following code is derived from the win32 code */ |
| 4133 | |
| 4134 | static PyObject * |
| 4135 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 4136 | { |
| 4137 | struct file_ref stdio[3]; |
| 4138 | struct pipe_ref p_fd[3]; |
| 4139 | FILE *p_s[3]; |
| 4140 | int file_count, i, pipe_err, pipe_pid; |
| 4141 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 4142 | PyObject *f, *p_f[3]; |
| 4143 | |
| 4144 | /* file modes for subsequent fdopen's on pipe handles */ |
| 4145 | if (mode == O_TEXT) |
| 4146 | { |
| 4147 | rd_mode = "rt"; |
| 4148 | wr_mode = "wt"; |
| 4149 | } |
| 4150 | else |
| 4151 | { |
| 4152 | rd_mode = "rb"; |
| 4153 | wr_mode = "wb"; |
| 4154 | } |
| 4155 | |
| 4156 | /* prepare shell references */ |
| 4157 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 4158 | if ((shell = getenv("COMSPEC")) == NULL) |
| 4159 | { |
| 4160 | errno = ENOENT; |
| 4161 | return posix_error(); |
| 4162 | } |
| 4163 | |
| 4164 | sh_name = _getname(shell); |
| 4165 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 4166 | opt = "/c"; |
| 4167 | else |
| 4168 | opt = "-c"; |
| 4169 | |
| 4170 | /* save current stdio fds + their flags, and set not inheritable */ |
| 4171 | i = pipe_err = 0; |
| 4172 | while (pipe_err >= 0 && i < 3) |
| 4173 | { |
| 4174 | pipe_err = stdio[i].handle = dup(i); |
| 4175 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 4176 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 4177 | i++; |
| 4178 | } |
| 4179 | if (pipe_err < 0) |
| 4180 | { |
| 4181 | /* didn't get them all saved - clean up and bail out */ |
| 4182 | int saved_err = errno; |
| 4183 | while (i-- > 0) |
| 4184 | { |
| 4185 | close(stdio[i].handle); |
| 4186 | } |
| 4187 | errno = saved_err; |
| 4188 | return posix_error(); |
| 4189 | } |
| 4190 | |
| 4191 | /* create pipe ends */ |
| 4192 | file_count = 2; |
| 4193 | if (n == POPEN_3) |
| 4194 | file_count = 3; |
| 4195 | i = pipe_err = 0; |
| 4196 | while ((pipe_err == 0) && (i < file_count)) |
| 4197 | pipe_err = pipe((int *)&p_fd[i++]); |
| 4198 | if (pipe_err < 0) |
| 4199 | { |
| 4200 | /* didn't get them all made - clean up and bail out */ |
| 4201 | while (i-- > 0) |
| 4202 | { |
| 4203 | close(p_fd[i].wr); |
| 4204 | close(p_fd[i].rd); |
| 4205 | } |
| 4206 | errno = EPIPE; |
| 4207 | return posix_error(); |
| 4208 | } |
| 4209 | |
| 4210 | /* change the actual standard IO streams over temporarily, |
| 4211 | * making the retained pipe ends non-inheritable |
| 4212 | */ |
| 4213 | pipe_err = 0; |
| 4214 | |
| 4215 | /* - stdin */ |
| 4216 | if (dup2(p_fd[0].rd, 0) == 0) |
| 4217 | { |
| 4218 | close(p_fd[0].rd); |
| 4219 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 4220 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 4221 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 4222 | { |
| 4223 | close(p_fd[0].wr); |
| 4224 | pipe_err = -1; |
| 4225 | } |
| 4226 | } |
| 4227 | else |
| 4228 | { |
| 4229 | pipe_err = -1; |
| 4230 | } |
| 4231 | |
| 4232 | /* - stdout */ |
| 4233 | if (pipe_err == 0) |
| 4234 | { |
| 4235 | if (dup2(p_fd[1].wr, 1) == 1) |
| 4236 | { |
| 4237 | close(p_fd[1].wr); |
| 4238 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 4239 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 4240 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 4241 | { |
| 4242 | close(p_fd[1].rd); |
| 4243 | pipe_err = -1; |
| 4244 | } |
| 4245 | } |
| 4246 | else |
| 4247 | { |
| 4248 | pipe_err = -1; |
| 4249 | } |
| 4250 | } |
| 4251 | |
| 4252 | /* - stderr, as required */ |
| 4253 | if (pipe_err == 0) |
| 4254 | switch (n) |
| 4255 | { |
| 4256 | case POPEN_3: |
| 4257 | { |
| 4258 | if (dup2(p_fd[2].wr, 2) == 2) |
| 4259 | { |
| 4260 | close(p_fd[2].wr); |
| 4261 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 4262 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 4263 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 4264 | { |
| 4265 | close(p_fd[2].rd); |
| 4266 | pipe_err = -1; |
| 4267 | } |
| 4268 | } |
| 4269 | else |
| 4270 | { |
| 4271 | pipe_err = -1; |
| 4272 | } |
| 4273 | break; |
| 4274 | } |
| 4275 | |
| 4276 | case POPEN_4: |
| 4277 | { |
| 4278 | if (dup2(1, 2) != 2) |
| 4279 | { |
| 4280 | pipe_err = -1; |
| 4281 | } |
| 4282 | break; |
| 4283 | } |
| 4284 | } |
| 4285 | |
| 4286 | /* spawn the child process */ |
| 4287 | if (pipe_err == 0) |
| 4288 | { |
| 4289 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 4290 | if (pipe_pid == -1) |
| 4291 | { |
| 4292 | pipe_err = -1; |
| 4293 | } |
| 4294 | else |
| 4295 | { |
| 4296 | /* save the PID into the FILE structure |
| 4297 | * NOTE: this implementation doesn't actually |
| 4298 | * take advantage of this, but do it for |
| 4299 | * completeness - AIM Apr01 |
| 4300 | */ |
| 4301 | for (i = 0; i < file_count; i++) |
| 4302 | p_s[i]->_pid = pipe_pid; |
| 4303 | } |
| 4304 | } |
| 4305 | |
| 4306 | /* reset standard IO to normal */ |
| 4307 | for (i = 0; i < 3; i++) |
| 4308 | { |
| 4309 | dup2(stdio[i].handle, i); |
| 4310 | fcntl(i, F_SETFD, stdio[i].flags); |
| 4311 | close(stdio[i].handle); |
| 4312 | } |
| 4313 | |
| 4314 | /* if any remnant problems, clean up and bail out */ |
| 4315 | if (pipe_err < 0) |
| 4316 | { |
| 4317 | for (i = 0; i < 3; i++) |
| 4318 | { |
| 4319 | close(p_fd[i].rd); |
| 4320 | close(p_fd[i].wr); |
| 4321 | } |
| 4322 | errno = EPIPE; |
| 4323 | return posix_error_with_filename(cmdstring); |
| 4324 | } |
| 4325 | |
| 4326 | /* build tuple of file objects to return */ |
| 4327 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 4328 | PyFile_SetBufSize(p_f[0], bufsize); |
| 4329 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 4330 | PyFile_SetBufSize(p_f[1], bufsize); |
| 4331 | if (n == POPEN_3) |
| 4332 | { |
| 4333 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 4334 | PyFile_SetBufSize(p_f[0], bufsize); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4335 | 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] | 4336 | } |
| 4337 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4338 | f = PyTuple_Pack(2, p_f[0], p_f[1]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4339 | |
| 4340 | /* |
| 4341 | * Insert the files we've created into the process dictionary |
| 4342 | * all referencing the list with the process handle and the |
| 4343 | * initial number of files (see description below in _PyPclose). |
| 4344 | * Since if _PyPclose later tried to wait on a process when all |
| 4345 | * handles weren't closed, it could create a deadlock with the |
| 4346 | * child, we spend some energy here to try to ensure that we |
| 4347 | * either insert all file handles into the dictionary or none |
| 4348 | * at all. It's a little clumsy with the various popen modes |
| 4349 | * and variable number of files involved. |
| 4350 | */ |
| 4351 | if (!_PyPopenProcs) |
| 4352 | { |
| 4353 | _PyPopenProcs = PyDict_New(); |
| 4354 | } |
| 4355 | |
| 4356 | if (_PyPopenProcs) |
| 4357 | { |
| 4358 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 4359 | int ins_rc[3]; |
| 4360 | |
| 4361 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4362 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4363 | |
| 4364 | procObj = PyList_New(2); |
| 4365 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 4366 | intObj = PyInt_FromLong((long) file_count); |
| 4367 | |
| 4368 | if (procObj && pidObj && intObj) |
| 4369 | { |
| 4370 | PyList_SetItem(procObj, 0, pidObj); |
| 4371 | PyList_SetItem(procObj, 1, intObj); |
| 4372 | |
| 4373 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 4374 | if (fileObj[0]) |
| 4375 | { |
| 4376 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4377 | fileObj[0], |
| 4378 | procObj); |
| 4379 | } |
| 4380 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 4381 | if (fileObj[1]) |
| 4382 | { |
| 4383 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4384 | fileObj[1], |
| 4385 | procObj); |
| 4386 | } |
| 4387 | if (file_count >= 3) |
| 4388 | { |
| 4389 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 4390 | if (fileObj[2]) |
| 4391 | { |
| 4392 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4393 | fileObj[2], |
| 4394 | procObj); |
| 4395 | } |
| 4396 | } |
| 4397 | |
| 4398 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4399 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4400 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 4401 | { |
| 4402 | /* Something failed - remove any dictionary |
| 4403 | * entries that did make it. |
| 4404 | */ |
| 4405 | if (!ins_rc[0] && fileObj[0]) |
| 4406 | { |
| 4407 | PyDict_DelItem(_PyPopenProcs, |
| 4408 | fileObj[0]); |
| 4409 | } |
| 4410 | if (!ins_rc[1] && fileObj[1]) |
| 4411 | { |
| 4412 | PyDict_DelItem(_PyPopenProcs, |
| 4413 | fileObj[1]); |
| 4414 | } |
| 4415 | if (!ins_rc[2] && fileObj[2]) |
| 4416 | { |
| 4417 | PyDict_DelItem(_PyPopenProcs, |
| 4418 | fileObj[2]); |
| 4419 | } |
| 4420 | } |
| 4421 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4422 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4423 | /* |
| 4424 | * Clean up our localized references for the dictionary keys |
| 4425 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4426 | * that got placed in the dictionary. |
| 4427 | */ |
| 4428 | Py_XDECREF(procObj); |
| 4429 | Py_XDECREF(fileObj[0]); |
| 4430 | Py_XDECREF(fileObj[1]); |
| 4431 | Py_XDECREF(fileObj[2]); |
| 4432 | } |
| 4433 | |
| 4434 | /* Child is launched. */ |
| 4435 | return f; |
| 4436 | } |
| 4437 | |
| 4438 | /* |
| 4439 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4440 | * exit code for the child process and return as a result of the close. |
| 4441 | * |
| 4442 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4443 | * input file pointer to information about the process that was |
| 4444 | * originally created by the popen* call that created the file pointer. |
| 4445 | * The dictionary uses the file pointer as a key (with one entry |
| 4446 | * inserted for each file returned by the original popen* call) and a |
| 4447 | * single list object as the value for all files from a single call. |
| 4448 | * The list object contains the Win32 process handle at [0], and a file |
| 4449 | * count at [1], which is initialized to the total number of file |
| 4450 | * handles using that list. |
| 4451 | * |
| 4452 | * This function closes whichever handle it is passed, and decrements |
| 4453 | * the file count in the dictionary for the process handle pointed to |
| 4454 | * by this file. On the last close (when the file count reaches zero), |
| 4455 | * this function will wait for the child process and then return its |
| 4456 | * exit code as the result of the close() operation. This permits the |
| 4457 | * files to be closed in any order - it is always the close() of the |
| 4458 | * final handle that will return the exit code. |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4459 | * |
| 4460 | * NOTE: This function is currently called with the GIL released. |
| 4461 | * hence we use the GILState API to manage our state. |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4462 | */ |
| 4463 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4464 | static int _PyPclose(FILE *file) |
| 4465 | { |
| 4466 | int result; |
| 4467 | int exit_code; |
| 4468 | int pipe_pid; |
| 4469 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 4470 | int file_count; |
| 4471 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4472 | PyGILState_STATE state; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4473 | #endif |
| 4474 | |
| 4475 | /* Close the file handle first, to ensure it can't block the |
| 4476 | * child from exiting if it's the last handle. |
| 4477 | */ |
| 4478 | result = fclose(file); |
| 4479 | |
| 4480 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4481 | state = PyGILState_Ensure(); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4482 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4483 | if (_PyPopenProcs) |
| 4484 | { |
| 4485 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4486 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4487 | fileObj)) != NULL && |
| 4488 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 4489 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 4490 | { |
| 4491 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 4492 | file_count = (int) PyInt_AsLong(intObj); |
| 4493 | |
| 4494 | if (file_count > 1) |
| 4495 | { |
| 4496 | /* Still other files referencing process */ |
| 4497 | file_count--; |
| 4498 | PyList_SetItem(procObj,1, |
| 4499 | PyInt_FromLong((long) file_count)); |
| 4500 | } |
| 4501 | else |
| 4502 | { |
| 4503 | /* Last file for this process */ |
| 4504 | if (result != EOF && |
| 4505 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 4506 | { |
| 4507 | /* extract exit status */ |
| 4508 | if (WIFEXITED(exit_code)) |
| 4509 | { |
| 4510 | result = WEXITSTATUS(exit_code); |
| 4511 | } |
| 4512 | else |
| 4513 | { |
| 4514 | errno = EPIPE; |
| 4515 | result = -1; |
| 4516 | } |
| 4517 | } |
| 4518 | else |
| 4519 | { |
| 4520 | /* Indicate failure - this will cause the file object |
| 4521 | * to raise an I/O error and translate the last |
| 4522 | * error code from errno. We do have a problem with |
| 4523 | * last errors that overlap the normal errno table, |
| 4524 | * but that's a consistent problem with the file object. |
| 4525 | */ |
| 4526 | result = -1; |
| 4527 | } |
| 4528 | } |
| 4529 | |
| 4530 | /* Remove this file pointer from dictionary */ |
| 4531 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4532 | |
| 4533 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 4534 | { |
| 4535 | Py_DECREF(_PyPopenProcs); |
| 4536 | _PyPopenProcs = NULL; |
| 4537 | } |
| 4538 | |
| 4539 | } /* if object retrieval ok */ |
| 4540 | |
| 4541 | Py_XDECREF(fileObj); |
| 4542 | } /* if _PyPopenProcs */ |
| 4543 | |
| 4544 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4545 | PyGILState_Release(state); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4546 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4547 | return result; |
| 4548 | } |
| 4549 | |
| 4550 | #endif /* PYCC_??? */ |
| 4551 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4552 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4553 | |
| 4554 | /* |
| 4555 | * Portable 'popen' replacement for Win32. |
| 4556 | * |
| 4557 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 4558 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4559 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4560 | */ |
| 4561 | |
| 4562 | #include <malloc.h> |
| 4563 | #include <io.h> |
| 4564 | #include <fcntl.h> |
| 4565 | |
| 4566 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 4567 | #define POPEN_1 1 |
| 4568 | #define POPEN_2 2 |
| 4569 | #define POPEN_3 3 |
| 4570 | #define POPEN_4 4 |
| 4571 | |
| 4572 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4573 | static int _PyPclose(FILE *file); |
| 4574 | |
| 4575 | /* |
| 4576 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4577 | * for use when retrieving the process exit code. See _PyPclose() below |
| 4578 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4579 | */ |
| 4580 | static PyObject *_PyPopenProcs = NULL; |
| 4581 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4582 | |
| 4583 | /* popen that works from a GUI. |
| 4584 | * |
| 4585 | * The result of this function is a pipe (file) connected to the |
| 4586 | * processes stdin or stdout, depending on the requested mode. |
| 4587 | */ |
| 4588 | |
| 4589 | static PyObject * |
| 4590 | posix_popen(PyObject *self, PyObject *args) |
| 4591 | { |
Raymond Hettinger | b5cb665 | 2003-09-01 22:25:41 +0000 | [diff] [blame] | 4592 | PyObject *f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4593 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4594 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4595 | char *cmdstring; |
| 4596 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4597 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4598 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4599 | return NULL; |
| 4600 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4601 | if (*mode == 'r') |
| 4602 | tm = _O_RDONLY; |
| 4603 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4604 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4605 | return NULL; |
| 4606 | } else |
| 4607 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4608 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4609 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4610 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4611 | return NULL; |
| 4612 | } |
| 4613 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4614 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4615 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4616 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4617 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4618 | else |
| 4619 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 4620 | |
| 4621 | return f; |
| 4622 | } |
| 4623 | |
| 4624 | /* Variation on win32pipe.popen |
| 4625 | * |
| 4626 | * The result of this function is a pipe (file) connected to the |
| 4627 | * process's stdin, and a pipe connected to the process's stdout. |
| 4628 | */ |
| 4629 | |
| 4630 | static PyObject * |
| 4631 | win32_popen2(PyObject *self, PyObject *args) |
| 4632 | { |
| 4633 | PyObject *f; |
| 4634 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4635 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4636 | char *cmdstring; |
| 4637 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4638 | int bufsize = -1; |
| 4639 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4640 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4641 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4642 | if (*mode == 't') |
| 4643 | tm = _O_TEXT; |
| 4644 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4645 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4646 | return NULL; |
| 4647 | } else |
| 4648 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4649 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4650 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4651 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4652 | return NULL; |
| 4653 | } |
| 4654 | |
| 4655 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4656 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4657 | return f; |
| 4658 | } |
| 4659 | |
| 4660 | /* |
| 4661 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4662 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4663 | * The result of this function is 3 pipes - the process's stdin, |
| 4664 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4665 | */ |
| 4666 | |
| 4667 | static PyObject * |
| 4668 | win32_popen3(PyObject *self, PyObject *args) |
| 4669 | { |
| 4670 | PyObject *f; |
| 4671 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4672 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4673 | char *cmdstring; |
| 4674 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4675 | int bufsize = -1; |
| 4676 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4677 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4678 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4679 | if (*mode == 't') |
| 4680 | tm = _O_TEXT; |
| 4681 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4682 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4683 | return NULL; |
| 4684 | } else |
| 4685 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4686 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4687 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4688 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4689 | return NULL; |
| 4690 | } |
| 4691 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4692 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4693 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4694 | return f; |
| 4695 | } |
| 4696 | |
| 4697 | /* |
| 4698 | * Variation on win32pipe.popen |
| 4699 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4700 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4701 | * and stdout+stderr combined as a single pipe. |
| 4702 | */ |
| 4703 | |
| 4704 | static PyObject * |
| 4705 | win32_popen4(PyObject *self, PyObject *args) |
| 4706 | { |
| 4707 | PyObject *f; |
| 4708 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4709 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4710 | char *cmdstring; |
| 4711 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4712 | int bufsize = -1; |
| 4713 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4714 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4715 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4716 | if (*mode == 't') |
| 4717 | tm = _O_TEXT; |
| 4718 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4719 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4720 | return NULL; |
| 4721 | } else |
| 4722 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4723 | |
| 4724 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4725 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4726 | return NULL; |
| 4727 | } |
| 4728 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4729 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4730 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4731 | return f; |
| 4732 | } |
| 4733 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4734 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4735 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4736 | HANDLE hStdin, |
| 4737 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4738 | HANDLE hStderr, |
| 4739 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4740 | { |
| 4741 | PROCESS_INFORMATION piProcInfo; |
| 4742 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4743 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4744 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4745 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4746 | int i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4747 | Py_ssize_t x; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4748 | |
| 4749 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4750 | char *comshell; |
| 4751 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4752 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4753 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4754 | /* x < i, so x fits into an integer */ |
| 4755 | return (int)x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4756 | |
| 4757 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 4758 | * then use the w9xpopen hack. |
| 4759 | */ |
| 4760 | comshell = s1 + x; |
| 4761 | while (comshell >= s1 && *comshell != '\\') |
| 4762 | --comshell; |
| 4763 | ++comshell; |
| 4764 | |
| 4765 | if (GetVersion() < 0x80000000 && |
| 4766 | _stricmp(comshell, "command.com") != 0) { |
| 4767 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4768 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4769 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4770 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4771 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4772 | } |
| 4773 | else { |
| 4774 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4775 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 4776 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4777 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4778 | char modulepath[_MAX_PATH]; |
| 4779 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4780 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 4781 | for (x = i = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4782 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4783 | x = i+1; |
| 4784 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4785 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4786 | strncat(modulepath, |
| 4787 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4788 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4789 | -strlen(modulepath)); |
| 4790 | if (stat(modulepath, &statinfo) != 0) { |
Kristján Valur Jónsson | 5e4e31f | 2007-04-21 12:46:49 +0000 | [diff] [blame^] | 4791 | size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4792 | /* Eeek - file-not-found - possibly an embedding |
| 4793 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4794 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4795 | strncpy(modulepath, |
| 4796 | Py_GetExecPrefix(), |
Kristján Valur Jónsson | 5e4e31f | 2007-04-21 12:46:49 +0000 | [diff] [blame^] | 4797 | mplen); |
| 4798 | modulepath[mplen-1] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4799 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 4800 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4801 | strncat(modulepath, |
| 4802 | szConsoleSpawn, |
Kristján Valur Jónsson | 5e4e31f | 2007-04-21 12:46:49 +0000 | [diff] [blame^] | 4803 | mplen-strlen(modulepath)); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4804 | /* No where else to look - raise an easily identifiable |
| 4805 | error, rather than leaving Windows to report |
| 4806 | "file not found" - as the user is probably blissfully |
| 4807 | unaware this shim EXE is used, and it will confuse them. |
| 4808 | (well, it confused me for a while ;-) |
| 4809 | */ |
| 4810 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4811 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4812 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4813 | "for popen to work with your shell " |
| 4814 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4815 | szConsoleSpawn); |
| 4816 | return FALSE; |
| 4817 | } |
| 4818 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4819 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4820 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4821 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4822 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4823 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4824 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4825 | /* To maintain correct argument passing semantics, |
| 4826 | we pass the command-line as it stands, and allow |
| 4827 | quoting to be applied. w9xpopen.exe will then |
| 4828 | use its argv vector, and re-quote the necessary |
| 4829 | args for the ultimate child process. |
| 4830 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4831 | PyOS_snprintf( |
| 4832 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4833 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4834 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4835 | s1, |
| 4836 | s3, |
| 4837 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4838 | /* Not passing CREATE_NEW_CONSOLE has been known to |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4839 | cause random failures on win9x. Specifically a |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4840 | dialog: |
| 4841 | "Your program accessed mem currently in use at xxx" |
| 4842 | and a hopeful warning about the stability of your |
| 4843 | system. |
| 4844 | Cost is Ctrl+C wont kill children, but anyone |
| 4845 | who cares can have a go! |
| 4846 | */ |
| 4847 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4848 | } |
| 4849 | } |
| 4850 | |
| 4851 | /* Could be an else here to try cmd.exe / command.com in the path |
| 4852 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4853 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4854 | PyErr_SetString(PyExc_RuntimeError, |
| 4855 | "Cannot locate a COMSPEC environment variable to " |
| 4856 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4857 | return FALSE; |
| 4858 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4859 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4860 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 4861 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 4862 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 4863 | siStartInfo.hStdInput = hStdin; |
| 4864 | siStartInfo.hStdOutput = hStdout; |
| 4865 | siStartInfo.hStdError = hStderr; |
| 4866 | siStartInfo.wShowWindow = SW_HIDE; |
| 4867 | |
| 4868 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4869 | s2, |
| 4870 | NULL, |
| 4871 | NULL, |
| 4872 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4873 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4874 | NULL, |
| 4875 | NULL, |
| 4876 | &siStartInfo, |
| 4877 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4878 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4879 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4880 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4881 | /* Return process handle */ |
| 4882 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4883 | return TRUE; |
| 4884 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4885 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4886 | return FALSE; |
| 4887 | } |
| 4888 | |
| 4889 | /* The following code is based off of KB: Q190351 */ |
| 4890 | |
| 4891 | static PyObject * |
| 4892 | _PyPopen(char *cmdstring, int mode, int n) |
| 4893 | { |
| 4894 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 4895 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4896 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4897 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4898 | SECURITY_ATTRIBUTES saAttr; |
| 4899 | BOOL fSuccess; |
| 4900 | int fd1, fd2, fd3; |
| 4901 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4902 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4903 | PyObject *f; |
| 4904 | |
| 4905 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 4906 | saAttr.bInheritHandle = TRUE; |
| 4907 | saAttr.lpSecurityDescriptor = NULL; |
| 4908 | |
| 4909 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 4910 | return win32_error("CreatePipe", NULL); |
| 4911 | |
| 4912 | /* Create new output read handle and the input write handle. Set |
| 4913 | * the inheritance properties to FALSE. Otherwise, the child inherits |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 4914 | * these handles; resulting in non-closeable handles to the pipes |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4915 | * being created. */ |
| 4916 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4917 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 4918 | FALSE, |
| 4919 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4920 | if (!fSuccess) |
| 4921 | return win32_error("DuplicateHandle", NULL); |
| 4922 | |
| 4923 | /* Close the inheritable version of ChildStdin |
| 4924 | that we're using. */ |
| 4925 | CloseHandle(hChildStdinWr); |
| 4926 | |
| 4927 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 4928 | return win32_error("CreatePipe", NULL); |
| 4929 | |
| 4930 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4931 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 4932 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4933 | if (!fSuccess) |
| 4934 | return win32_error("DuplicateHandle", NULL); |
| 4935 | |
| 4936 | /* Close the inheritable version of ChildStdout |
| 4937 | that we're using. */ |
| 4938 | CloseHandle(hChildStdoutRd); |
| 4939 | |
| 4940 | if (n != POPEN_4) { |
| 4941 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 4942 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4943 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 4944 | hChildStderrRd, |
| 4945 | GetCurrentProcess(), |
| 4946 | &hChildStderrRdDup, 0, |
| 4947 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4948 | if (!fSuccess) |
| 4949 | return win32_error("DuplicateHandle", NULL); |
| 4950 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 4951 | CloseHandle(hChildStderrRd); |
| 4952 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4953 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4954 | switch (n) { |
| 4955 | case POPEN_1: |
| 4956 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 4957 | case _O_WRONLY | _O_TEXT: |
| 4958 | /* Case for writing to child Stdin in text mode. */ |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4959 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4960 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4961 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4962 | PyFile_SetBufSize(f, 0); |
| 4963 | /* We don't care about these pipes anymore, so close them. */ |
| 4964 | CloseHandle(hChildStdoutRdDup); |
| 4965 | CloseHandle(hChildStderrRdDup); |
| 4966 | break; |
| 4967 | |
| 4968 | case _O_RDONLY | _O_TEXT: |
| 4969 | /* Case for reading from child Stdout in text mode. */ |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4970 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4971 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4972 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4973 | PyFile_SetBufSize(f, 0); |
| 4974 | /* We don't care about these pipes anymore, so close them. */ |
| 4975 | CloseHandle(hChildStdinWrDup); |
| 4976 | CloseHandle(hChildStderrRdDup); |
| 4977 | break; |
| 4978 | |
| 4979 | case _O_RDONLY | _O_BINARY: |
| 4980 | /* Case for readinig from child Stdout in binary mode. */ |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4981 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4982 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4983 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4984 | PyFile_SetBufSize(f, 0); |
| 4985 | /* We don't care about these pipes anymore, so close them. */ |
| 4986 | CloseHandle(hChildStdinWrDup); |
| 4987 | CloseHandle(hChildStderrRdDup); |
| 4988 | break; |
| 4989 | |
| 4990 | case _O_WRONLY | _O_BINARY: |
| 4991 | /* Case for writing to child Stdin in binary mode. */ |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4992 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4993 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4994 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4995 | PyFile_SetBufSize(f, 0); |
| 4996 | /* We don't care about these pipes anymore, so close them. */ |
| 4997 | CloseHandle(hChildStdoutRdDup); |
| 4998 | CloseHandle(hChildStderrRdDup); |
| 4999 | break; |
| 5000 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5001 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5002 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5003 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5004 | case POPEN_2: |
| 5005 | case POPEN_4: |
| 5006 | { |
| 5007 | char *m1, *m2; |
| 5008 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5009 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 5010 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5011 | m1 = "r"; |
| 5012 | m2 = "w"; |
| 5013 | } else { |
| 5014 | m1 = "rb"; |
| 5015 | m2 = "wb"; |
| 5016 | } |
| 5017 | |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 5018 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5019 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 5020 | fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5021 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5022 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5023 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5024 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5025 | PyFile_SetBufSize(p2, 0); |
| 5026 | |
| 5027 | if (n != 4) |
| 5028 | CloseHandle(hChildStderrRdDup); |
| 5029 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 5030 | f = PyTuple_Pack(2,p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 5031 | Py_XDECREF(p1); |
| 5032 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5033 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5034 | break; |
| 5035 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5036 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5037 | case POPEN_3: |
| 5038 | { |
| 5039 | char *m1, *m2; |
| 5040 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5041 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 5042 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5043 | m1 = "r"; |
| 5044 | m2 = "w"; |
| 5045 | } else { |
| 5046 | m1 = "rb"; |
| 5047 | m2 = "wb"; |
| 5048 | } |
| 5049 | |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 5050 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5051 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 5052 | fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5053 | f2 = _fdopen(fd2, m1); |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 5054 | fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5055 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5056 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5057 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 5058 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5059 | PyFile_SetBufSize(p1, 0); |
| 5060 | PyFile_SetBufSize(p2, 0); |
| 5061 | PyFile_SetBufSize(p3, 0); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 5062 | f = PyTuple_Pack(3,p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 5063 | Py_XDECREF(p1); |
| 5064 | Py_XDECREF(p2); |
| 5065 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5066 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5067 | break; |
| 5068 | } |
| 5069 | } |
| 5070 | |
| 5071 | if (n == POPEN_4) { |
| 5072 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 5073 | hChildStdinRd, |
| 5074 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5075 | hChildStdoutWr, |
| 5076 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 5077 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5078 | } |
| 5079 | else { |
| 5080 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 5081 | hChildStdinRd, |
| 5082 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5083 | hChildStderrWr, |
| 5084 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 5085 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5088 | /* |
| 5089 | * Insert the files we've created into the process dictionary |
| 5090 | * all referencing the list with the process handle and the |
| 5091 | * initial number of files (see description below in _PyPclose). |
| 5092 | * Since if _PyPclose later tried to wait on a process when all |
| 5093 | * handles weren't closed, it could create a deadlock with the |
| 5094 | * child, we spend some energy here to try to ensure that we |
| 5095 | * either insert all file handles into the dictionary or none |
| 5096 | * at all. It's a little clumsy with the various popen modes |
| 5097 | * and variable number of files involved. |
| 5098 | */ |
| 5099 | if (!_PyPopenProcs) { |
| 5100 | _PyPopenProcs = PyDict_New(); |
| 5101 | } |
| 5102 | |
| 5103 | if (_PyPopenProcs) { |
| 5104 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 5105 | int ins_rc[3]; |
| 5106 | |
| 5107 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 5108 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 5109 | |
| 5110 | procObj = PyList_New(2); |
| 5111 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 5112 | intObj = PyInt_FromLong(file_count); |
| 5113 | |
| 5114 | if (procObj && hProcessObj && intObj) { |
| 5115 | PyList_SetItem(procObj,0,hProcessObj); |
| 5116 | PyList_SetItem(procObj,1,intObj); |
| 5117 | |
| 5118 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 5119 | if (fileObj[0]) { |
| 5120 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 5121 | fileObj[0], |
| 5122 | procObj); |
| 5123 | } |
| 5124 | if (file_count >= 2) { |
| 5125 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 5126 | if (fileObj[1]) { |
| 5127 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 5128 | fileObj[1], |
| 5129 | procObj); |
| 5130 | } |
| 5131 | } |
| 5132 | if (file_count >= 3) { |
| 5133 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 5134 | if (fileObj[2]) { |
| 5135 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 5136 | fileObj[2], |
| 5137 | procObj); |
| 5138 | } |
| 5139 | } |
| 5140 | |
| 5141 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 5142 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 5143 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 5144 | /* Something failed - remove any dictionary |
| 5145 | * entries that did make it. |
| 5146 | */ |
| 5147 | if (!ins_rc[0] && fileObj[0]) { |
| 5148 | PyDict_DelItem(_PyPopenProcs, |
| 5149 | fileObj[0]); |
| 5150 | } |
| 5151 | if (!ins_rc[1] && fileObj[1]) { |
| 5152 | PyDict_DelItem(_PyPopenProcs, |
| 5153 | fileObj[1]); |
| 5154 | } |
| 5155 | if (!ins_rc[2] && fileObj[2]) { |
| 5156 | PyDict_DelItem(_PyPopenProcs, |
| 5157 | fileObj[2]); |
| 5158 | } |
| 5159 | } |
| 5160 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5161 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5162 | /* |
| 5163 | * Clean up our localized references for the dictionary keys |
| 5164 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 5165 | * that got placed in the dictionary. |
| 5166 | */ |
| 5167 | Py_XDECREF(procObj); |
| 5168 | Py_XDECREF(fileObj[0]); |
| 5169 | Py_XDECREF(fileObj[1]); |
| 5170 | Py_XDECREF(fileObj[2]); |
| 5171 | } |
| 5172 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5173 | /* Child is launched. Close the parents copy of those pipe |
| 5174 | * handles that only the child should have open. You need to |
| 5175 | * make sure that no handles to the write end of the output pipe |
| 5176 | * are maintained in this process or else the pipe will not close |
| 5177 | * when the child process exits and the ReadFile will hang. */ |
| 5178 | |
| 5179 | if (!CloseHandle(hChildStdinRd)) |
| 5180 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5181 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5182 | if (!CloseHandle(hChildStdoutWr)) |
| 5183 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5184 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5185 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 5186 | return win32_error("CloseHandle", NULL); |
| 5187 | |
| 5188 | return f; |
| 5189 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5190 | |
| 5191 | /* |
| 5192 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 5193 | * 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] | 5194 | * |
| 5195 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 5196 | * input file pointer to information about the process that was |
| 5197 | * originally created by the popen* call that created the file pointer. |
| 5198 | * The dictionary uses the file pointer as a key (with one entry |
| 5199 | * inserted for each file returned by the original popen* call) and a |
| 5200 | * single list object as the value for all files from a single call. |
| 5201 | * The list object contains the Win32 process handle at [0], and a file |
| 5202 | * count at [1], which is initialized to the total number of file |
| 5203 | * handles using that list. |
| 5204 | * |
| 5205 | * This function closes whichever handle it is passed, and decrements |
| 5206 | * the file count in the dictionary for the process handle pointed to |
| 5207 | * by this file. On the last close (when the file count reaches zero), |
| 5208 | * this function will wait for the child process and then return its |
| 5209 | * exit code as the result of the close() operation. This permits the |
| 5210 | * files to be closed in any order - it is always the close() of the |
| 5211 | * final handle that will return the exit code. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 5212 | * |
| 5213 | * NOTE: This function is currently called with the GIL released. |
| 5214 | * hence we use the GILState API to manage our state. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5215 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5216 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5217 | static int _PyPclose(FILE *file) |
| 5218 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5219 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5220 | DWORD exit_code; |
| 5221 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5222 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 5223 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5224 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 5225 | PyGILState_STATE state; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5226 | #endif |
| 5227 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5228 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5229 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5230 | */ |
| 5231 | result = fclose(file); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5232 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 5233 | state = PyGILState_Ensure(); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5234 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5235 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5236 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 5237 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 5238 | fileObj)) != NULL && |
| 5239 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 5240 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 5241 | |
| 5242 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 5243 | file_count = PyInt_AsLong(intObj); |
| 5244 | |
| 5245 | if (file_count > 1) { |
| 5246 | /* Still other files referencing process */ |
| 5247 | file_count--; |
| 5248 | PyList_SetItem(procObj,1, |
| 5249 | PyInt_FromLong(file_count)); |
| 5250 | } else { |
| 5251 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5252 | if (result != EOF && |
| 5253 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 5254 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5255 | /* Possible truncation here in 16-bit environments, but |
| 5256 | * real exit codes are just the lower byte in any event. |
| 5257 | */ |
| 5258 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5259 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5260 | /* Indicate failure - this will cause the file object |
| 5261 | * to raise an I/O error and translate the last Win32 |
| 5262 | * error code from errno. We do have a problem with |
| 5263 | * last errors that overlap the normal errno table, |
| 5264 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5265 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5266 | if (result != EOF) { |
| 5267 | /* If the error wasn't from the fclose(), then |
| 5268 | * set errno for the file object error handling. |
| 5269 | */ |
| 5270 | errno = GetLastError(); |
| 5271 | } |
| 5272 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5273 | } |
| 5274 | |
| 5275 | /* Free up the native handle at this point */ |
| 5276 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5277 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5278 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5279 | /* Remove this file pointer from dictionary */ |
| 5280 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 5281 | |
| 5282 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 5283 | Py_DECREF(_PyPopenProcs); |
| 5284 | _PyPopenProcs = NULL; |
| 5285 | } |
| 5286 | |
| 5287 | } /* if object retrieval ok */ |
| 5288 | |
| 5289 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5290 | } /* if _PyPopenProcs */ |
| 5291 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5292 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 5293 | PyGILState_Release(state); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5294 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5295 | return result; |
| 5296 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 5297 | |
| 5298 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5299 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5300 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5301 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5302 | char *name; |
| 5303 | char *mode = "r"; |
| 5304 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5305 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5306 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5307 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5308 | return NULL; |
Martin v. Löwis | 9ad853b | 2003-10-31 10:01:53 +0000 | [diff] [blame] | 5309 | /* Strip mode of binary or text modifiers */ |
| 5310 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) |
| 5311 | mode = "r"; |
| 5312 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) |
| 5313 | mode = "w"; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5314 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 5315 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5316 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5317 | if (fp == NULL) |
| 5318 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5319 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5320 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5321 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5322 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5323 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5324 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5325 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5326 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5327 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5328 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5329 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5330 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5331 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5332 | Set the current process's user id."); |
| 5333 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5334 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5335 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5336 | { |
| 5337 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5338 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5339 | return NULL; |
| 5340 | if (setuid(uid) < 0) |
| 5341 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5342 | Py_INCREF(Py_None); |
| 5343 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5344 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5345 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5346 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5347 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5348 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5349 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5350 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5351 | Set the current process's effective user id."); |
| 5352 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5353 | static PyObject * |
| 5354 | posix_seteuid (PyObject *self, PyObject *args) |
| 5355 | { |
| 5356 | int euid; |
| 5357 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 5358 | return NULL; |
| 5359 | } else if (seteuid(euid) < 0) { |
| 5360 | return posix_error(); |
| 5361 | } else { |
| 5362 | Py_INCREF(Py_None); |
| 5363 | return Py_None; |
| 5364 | } |
| 5365 | } |
| 5366 | #endif /* HAVE_SETEUID */ |
| 5367 | |
| 5368 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5369 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5370 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5371 | Set the current process's effective group id."); |
| 5372 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5373 | static PyObject * |
| 5374 | posix_setegid (PyObject *self, PyObject *args) |
| 5375 | { |
| 5376 | int egid; |
| 5377 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 5378 | return NULL; |
| 5379 | } else if (setegid(egid) < 0) { |
| 5380 | return posix_error(); |
| 5381 | } else { |
| 5382 | Py_INCREF(Py_None); |
| 5383 | return Py_None; |
| 5384 | } |
| 5385 | } |
| 5386 | #endif /* HAVE_SETEGID */ |
| 5387 | |
| 5388 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5389 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5390 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5391 | Set the current process's real and effective user ids."); |
| 5392 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5393 | static PyObject * |
| 5394 | posix_setreuid (PyObject *self, PyObject *args) |
| 5395 | { |
| 5396 | int ruid, euid; |
| 5397 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 5398 | return NULL; |
| 5399 | } else if (setreuid(ruid, euid) < 0) { |
| 5400 | return posix_error(); |
| 5401 | } else { |
| 5402 | Py_INCREF(Py_None); |
| 5403 | return Py_None; |
| 5404 | } |
| 5405 | } |
| 5406 | #endif /* HAVE_SETREUID */ |
| 5407 | |
| 5408 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5409 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5410 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5411 | Set the current process's real and effective group ids."); |
| 5412 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5413 | static PyObject * |
| 5414 | posix_setregid (PyObject *self, PyObject *args) |
| 5415 | { |
| 5416 | int rgid, egid; |
| 5417 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 5418 | return NULL; |
| 5419 | } else if (setregid(rgid, egid) < 0) { |
| 5420 | return posix_error(); |
| 5421 | } else { |
| 5422 | Py_INCREF(Py_None); |
| 5423 | return Py_None; |
| 5424 | } |
| 5425 | } |
| 5426 | #endif /* HAVE_SETREGID */ |
| 5427 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5428 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5429 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5430 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5431 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5432 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5433 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5434 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5435 | { |
| 5436 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5437 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5438 | return NULL; |
| 5439 | if (setgid(gid) < 0) |
| 5440 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5441 | Py_INCREF(Py_None); |
| 5442 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5443 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5444 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5445 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5446 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5447 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5448 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5449 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5450 | |
| 5451 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5452 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5453 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5454 | int i, len; |
| 5455 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5456 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5457 | if (!PySequence_Check(groups)) { |
| 5458 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 5459 | return NULL; |
| 5460 | } |
| 5461 | len = PySequence_Size(groups); |
| 5462 | if (len > MAX_GROUPS) { |
| 5463 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 5464 | return NULL; |
| 5465 | } |
| 5466 | for(i = 0; i < len; i++) { |
| 5467 | PyObject *elem; |
| 5468 | elem = PySequence_GetItem(groups, i); |
| 5469 | if (!elem) |
| 5470 | return NULL; |
| 5471 | if (!PyInt_Check(elem)) { |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 5472 | if (!PyLong_Check(elem)) { |
| 5473 | PyErr_SetString(PyExc_TypeError, |
| 5474 | "groups must be integers"); |
| 5475 | Py_DECREF(elem); |
| 5476 | return NULL; |
| 5477 | } else { |
| 5478 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 5479 | if (PyErr_Occurred()) { |
| 5480 | PyErr_SetString(PyExc_TypeError, |
| 5481 | "group id too big"); |
| 5482 | Py_DECREF(elem); |
| 5483 | return NULL; |
| 5484 | } |
| 5485 | grouplist[i] = x; |
| 5486 | /* read back the value to see if it fitted in gid_t */ |
| 5487 | if (grouplist[i] != x) { |
| 5488 | PyErr_SetString(PyExc_TypeError, |
| 5489 | "group id too big"); |
| 5490 | Py_DECREF(elem); |
| 5491 | return NULL; |
| 5492 | } |
| 5493 | } |
| 5494 | } else { |
| 5495 | long x = PyInt_AsLong(elem); |
| 5496 | grouplist[i] = x; |
| 5497 | if (grouplist[i] != x) { |
| 5498 | PyErr_SetString(PyExc_TypeError, |
| 5499 | "group id too big"); |
| 5500 | Py_DECREF(elem); |
| 5501 | return NULL; |
| 5502 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5503 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5504 | Py_DECREF(elem); |
| 5505 | } |
| 5506 | |
| 5507 | if (setgroups(len, grouplist) < 0) |
| 5508 | return posix_error(); |
| 5509 | Py_INCREF(Py_None); |
| 5510 | return Py_None; |
| 5511 | } |
| 5512 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5513 | |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 5514 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5515 | static PyObject * |
| 5516 | wait_helper(int pid, int status, struct rusage *ru) |
| 5517 | { |
| 5518 | PyObject *result; |
| 5519 | static PyObject *struct_rusage; |
| 5520 | |
| 5521 | if (pid == -1) |
| 5522 | return posix_error(); |
| 5523 | |
| 5524 | if (struct_rusage == NULL) { |
| 5525 | PyObject *m = PyImport_ImportModule("resource"); |
| 5526 | if (m == NULL) |
| 5527 | return NULL; |
| 5528 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 5529 | Py_DECREF(m); |
| 5530 | if (struct_rusage == NULL) |
| 5531 | return NULL; |
| 5532 | } |
| 5533 | |
| 5534 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 5535 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 5536 | if (!result) |
| 5537 | return NULL; |
| 5538 | |
| 5539 | #ifndef doubletime |
| 5540 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 5541 | #endif |
| 5542 | |
| 5543 | PyStructSequence_SET_ITEM(result, 0, |
| 5544 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 5545 | PyStructSequence_SET_ITEM(result, 1, |
| 5546 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 5547 | #define SET_INT(result, index, value)\ |
| 5548 | PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value)) |
| 5549 | SET_INT(result, 2, ru->ru_maxrss); |
| 5550 | SET_INT(result, 3, ru->ru_ixrss); |
| 5551 | SET_INT(result, 4, ru->ru_idrss); |
| 5552 | SET_INT(result, 5, ru->ru_isrss); |
| 5553 | SET_INT(result, 6, ru->ru_minflt); |
| 5554 | SET_INT(result, 7, ru->ru_majflt); |
| 5555 | SET_INT(result, 8, ru->ru_nswap); |
| 5556 | SET_INT(result, 9, ru->ru_inblock); |
| 5557 | SET_INT(result, 10, ru->ru_oublock); |
| 5558 | SET_INT(result, 11, ru->ru_msgsnd); |
| 5559 | SET_INT(result, 12, ru->ru_msgrcv); |
| 5560 | SET_INT(result, 13, ru->ru_nsignals); |
| 5561 | SET_INT(result, 14, ru->ru_nvcsw); |
| 5562 | SET_INT(result, 15, ru->ru_nivcsw); |
| 5563 | #undef SET_INT |
| 5564 | |
| 5565 | if (PyErr_Occurred()) { |
| 5566 | Py_DECREF(result); |
| 5567 | return NULL; |
| 5568 | } |
| 5569 | |
Neal Norwitz | 9b00a56 | 2006-03-20 08:47:12 +0000 | [diff] [blame] | 5570 | return Py_BuildValue("iiN", pid, status, result); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5571 | } |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 5572 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5573 | |
| 5574 | #ifdef HAVE_WAIT3 |
| 5575 | PyDoc_STRVAR(posix_wait3__doc__, |
| 5576 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 5577 | Wait for completion of a child process."); |
| 5578 | |
| 5579 | static PyObject * |
| 5580 | posix_wait3(PyObject *self, PyObject *args) |
| 5581 | { |
| 5582 | int pid, options; |
| 5583 | struct rusage ru; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5584 | WAIT_TYPE status; |
| 5585 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5586 | |
| 5587 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 5588 | return NULL; |
| 5589 | |
| 5590 | Py_BEGIN_ALLOW_THREADS |
| 5591 | pid = wait3(&status, options, &ru); |
| 5592 | Py_END_ALLOW_THREADS |
| 5593 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5594 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5595 | } |
| 5596 | #endif /* HAVE_WAIT3 */ |
| 5597 | |
| 5598 | #ifdef HAVE_WAIT4 |
| 5599 | PyDoc_STRVAR(posix_wait4__doc__, |
| 5600 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 5601 | Wait for completion of a given child process."); |
| 5602 | |
| 5603 | static PyObject * |
| 5604 | posix_wait4(PyObject *self, PyObject *args) |
| 5605 | { |
| 5606 | int pid, options; |
| 5607 | struct rusage ru; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5608 | WAIT_TYPE status; |
| 5609 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5610 | |
| 5611 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 5612 | return NULL; |
| 5613 | |
| 5614 | Py_BEGIN_ALLOW_THREADS |
| 5615 | pid = wait4(pid, &status, options, &ru); |
| 5616 | Py_END_ALLOW_THREADS |
| 5617 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5618 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5619 | } |
| 5620 | #endif /* HAVE_WAIT4 */ |
| 5621 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5622 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5623 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5624 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5625 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5626 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5627 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5628 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5629 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5630 | int pid, options; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5631 | WAIT_TYPE status; |
| 5632 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5633 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5634 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5635 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5636 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 5637 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5638 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5639 | if (pid == -1) |
| 5640 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5641 | |
| 5642 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5643 | } |
| 5644 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5645 | #elif defined(HAVE_CWAIT) |
| 5646 | |
| 5647 | /* 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] | 5648 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5649 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5650 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5651 | |
| 5652 | static PyObject * |
| 5653 | posix_waitpid(PyObject *self, PyObject *args) |
| 5654 | { |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 5655 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5656 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5657 | |
| 5658 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 5659 | return NULL; |
| 5660 | Py_BEGIN_ALLOW_THREADS |
| 5661 | pid = _cwait(&status, pid, options); |
| 5662 | Py_END_ALLOW_THREADS |
| 5663 | if (pid == -1) |
| 5664 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5665 | |
| 5666 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 5667 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5668 | } |
| 5669 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5670 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5671 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5672 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5673 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5674 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5675 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5676 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5677 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5678 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5679 | int pid; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5680 | WAIT_TYPE status; |
| 5681 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5682 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5683 | Py_BEGIN_ALLOW_THREADS |
| 5684 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5685 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5686 | if (pid == -1) |
| 5687 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5688 | |
| 5689 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5690 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5691 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5692 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5693 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5694 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5695 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5696 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5697 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5698 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5699 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5700 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5701 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5702 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5703 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5704 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5705 | 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] | 5706 | #else |
| 5707 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 5708 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5709 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5710 | } |
| 5711 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5712 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5713 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5714 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5715 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5716 | 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] | 5717 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5718 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5719 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5720 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5721 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 5722 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5723 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5724 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5725 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5726 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 5727 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5728 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5729 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 5730 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5731 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5732 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5733 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5734 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5735 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5736 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5737 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5738 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 5739 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5740 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5741 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5742 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5743 | { |
Martin v. Löwis | 4fc2bda | 2006-05-04 12:04:27 +0000 | [diff] [blame] | 5744 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5745 | } |
| 5746 | #endif /* HAVE_SYMLINK */ |
| 5747 | |
| 5748 | |
| 5749 | #ifdef HAVE_TIMES |
| 5750 | #ifndef HZ |
| 5751 | #define HZ 60 /* Universal constant :-) */ |
| 5752 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5753 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5754 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 5755 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5756 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5757 | { |
| 5758 | ULONG value = 0; |
| 5759 | |
| 5760 | Py_BEGIN_ALLOW_THREADS |
| 5761 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 5762 | Py_END_ALLOW_THREADS |
| 5763 | |
| 5764 | return value; |
| 5765 | } |
| 5766 | |
| 5767 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5768 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5769 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5770 | /* Currently Only Uptime is Provided -- Others Later */ |
| 5771 | return Py_BuildValue("ddddd", |
| 5772 | (double)0 /* t.tms_utime / HZ */, |
| 5773 | (double)0 /* t.tms_stime / HZ */, |
| 5774 | (double)0 /* t.tms_cutime / HZ */, |
| 5775 | (double)0 /* t.tms_cstime / HZ */, |
| 5776 | (double)system_uptime() / 1000); |
| 5777 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5778 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5779 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5780 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5781 | { |
| 5782 | struct tms t; |
| 5783 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5784 | errno = 0; |
| 5785 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5786 | if (c == (clock_t) -1) |
| 5787 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5788 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5789 | (double)t.tms_utime / HZ, |
| 5790 | (double)t.tms_stime / HZ, |
| 5791 | (double)t.tms_cutime / HZ, |
| 5792 | (double)t.tms_cstime / HZ, |
| 5793 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5794 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5795 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5796 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5797 | |
| 5798 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5799 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5800 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5801 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5802 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5803 | { |
| 5804 | FILETIME create, exit, kernel, user; |
| 5805 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5806 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5807 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 5808 | /* The fields of a FILETIME structure are the hi and lo part |
| 5809 | of a 64-bit value expressed in 100 nanosecond units. |
| 5810 | 1e7 is one second in such units; 1e-7 the inverse. |
| 5811 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 5812 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5813 | return Py_BuildValue( |
| 5814 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5815 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 5816 | kernel.dwLowDateTime*1e-7), |
| 5817 | (double)(user.dwHighDateTime*429.4967296 + |
| 5818 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5819 | (double)0, |
| 5820 | (double)0, |
| 5821 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5822 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5823 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5824 | |
| 5825 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5826 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5827 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5828 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5829 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5830 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5831 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5832 | #ifdef HAVE_GETSID |
| 5833 | PyDoc_STRVAR(posix_getsid__doc__, |
| 5834 | "getsid(pid) -> sid\n\n\ |
| 5835 | Call the system call getsid()."); |
| 5836 | |
| 5837 | static PyObject * |
| 5838 | posix_getsid(PyObject *self, PyObject *args) |
| 5839 | { |
| 5840 | int pid, sid; |
| 5841 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 5842 | return NULL; |
| 5843 | sid = getsid(pid); |
| 5844 | if (sid < 0) |
| 5845 | return posix_error(); |
| 5846 | return PyInt_FromLong((long)sid); |
| 5847 | } |
| 5848 | #endif /* HAVE_GETSID */ |
| 5849 | |
| 5850 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5851 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5852 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5853 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5854 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5855 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5856 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5857 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5858 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5859 | if (setsid() < 0) |
| 5860 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5861 | Py_INCREF(Py_None); |
| 5862 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5863 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5864 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5865 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5866 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5867 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5868 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5869 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5870 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5871 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5872 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5873 | { |
| 5874 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5875 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5876 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5877 | if (setpgid(pid, pgrp) < 0) |
| 5878 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5879 | Py_INCREF(Py_None); |
| 5880 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5881 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5882 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5883 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5884 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5885 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5886 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5887 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5888 | 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] | 5889 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5890 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5891 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5892 | { |
| 5893 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5894 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5895 | return NULL; |
| 5896 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5897 | if (pgid < 0) |
| 5898 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5899 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5900 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5901 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5902 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5903 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5904 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5905 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5906 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5907 | 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] | 5908 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5909 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5910 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5911 | { |
| 5912 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5913 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5914 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5915 | if (tcsetpgrp(fd, pgid) < 0) |
| 5916 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5917 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5918 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5919 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5920 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5921 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5922 | /* Functions acting on file descriptors */ |
| 5923 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5924 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5925 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5926 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5927 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5928 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5929 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5930 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5931 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5932 | int flag; |
| 5933 | int mode = 0777; |
| 5934 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5935 | |
| 5936 | #ifdef MS_WINDOWS |
| 5937 | if (unicode_file_names()) { |
| 5938 | PyUnicodeObject *po; |
| 5939 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 5940 | Py_BEGIN_ALLOW_THREADS |
| 5941 | /* PyUnicode_AS_UNICODE OK without thread |
| 5942 | lock as it is a simple dereference. */ |
| 5943 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 5944 | Py_END_ALLOW_THREADS |
| 5945 | if (fd < 0) |
| 5946 | return posix_error(); |
| 5947 | return PyInt_FromLong((long)fd); |
| 5948 | } |
| 5949 | /* Drop the argument parsing error as narrow strings |
| 5950 | are also valid. */ |
| 5951 | PyErr_Clear(); |
| 5952 | } |
| 5953 | #endif |
| 5954 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5955 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5956 | Py_FileSystemDefaultEncoding, &file, |
| 5957 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5958 | return NULL; |
| 5959 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5960 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5961 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5962 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5963 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5964 | return posix_error_with_allocated_filename(file); |
| 5965 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5966 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5967 | } |
| 5968 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5969 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5970 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5971 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5972 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5973 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5974 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5975 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5976 | { |
| 5977 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5978 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5979 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5980 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5981 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5982 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5983 | if (res < 0) |
| 5984 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5985 | Py_INCREF(Py_None); |
| 5986 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5987 | } |
| 5988 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5990 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5991 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5992 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5993 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5994 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5995 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5996 | { |
| 5997 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5998 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5999 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6000 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6001 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6002 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6003 | if (fd < 0) |
| 6004 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6005 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6006 | } |
| 6007 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6008 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6009 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 6010 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6011 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6012 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6013 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6014 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6015 | { |
| 6016 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6017 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6018 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6019 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6020 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6021 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6022 | if (res < 0) |
| 6023 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6024 | Py_INCREF(Py_None); |
| 6025 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6026 | } |
| 6027 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6028 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6029 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6030 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6031 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6032 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6033 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6034 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6035 | { |
| 6036 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6037 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6038 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6039 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6040 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6041 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6042 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6043 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6044 | return NULL; |
| 6045 | #ifdef SEEK_SET |
| 6046 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 6047 | switch (how) { |
| 6048 | case 0: how = SEEK_SET; break; |
| 6049 | case 1: how = SEEK_CUR; break; |
| 6050 | case 2: how = SEEK_END; break; |
| 6051 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6052 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6053 | |
| 6054 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6055 | pos = PyInt_AsLong(posobj); |
| 6056 | #else |
| 6057 | pos = PyLong_Check(posobj) ? |
| 6058 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 6059 | #endif |
| 6060 | if (PyErr_Occurred()) |
| 6061 | return NULL; |
| 6062 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6063 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6064 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6065 | res = _lseeki64(fd, pos, how); |
| 6066 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6067 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6068 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6069 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6070 | if (res < 0) |
| 6071 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6072 | |
| 6073 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6074 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6075 | #else |
| 6076 | return PyLong_FromLongLong(res); |
| 6077 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6078 | } |
| 6079 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6080 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6081 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6082 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6083 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6084 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6085 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6086 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6087 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 6088 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6089 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6090 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6091 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 6092 | if (size < 0) { |
| 6093 | errno = EINVAL; |
| 6094 | return posix_error(); |
| 6095 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6096 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6097 | if (buffer == NULL) |
| 6098 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6099 | Py_BEGIN_ALLOW_THREADS |
| 6100 | n = read(fd, PyString_AsString(buffer), size); |
| 6101 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 6102 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6103 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6104 | return posix_error(); |
| 6105 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 6106 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6107 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6108 | return buffer; |
| 6109 | } |
| 6110 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6111 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6112 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6113 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6114 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6115 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6116 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6117 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6118 | { |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 6119 | int fd; |
| 6120 | Py_ssize_t size; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6121 | char *buffer; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 6122 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6123 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6124 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6125 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 6126 | size = write(fd, buffer, (size_t)size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6127 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6128 | if (size < 0) |
| 6129 | return posix_error(); |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 6130 | return PyInt_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6131 | } |
| 6132 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6133 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6134 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6135 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6136 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6137 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6138 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6139 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6140 | { |
| 6141 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6142 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6143 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6144 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6145 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 6146 | #ifdef __VMS |
| 6147 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 6148 | fsync(fd); |
| 6149 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6150 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6151 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6152 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6153 | if (res != 0) { |
| 6154 | #ifdef MS_WINDOWS |
| 6155 | return win32_error("fstat", NULL); |
| 6156 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6157 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6158 | #endif |
| 6159 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6160 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6161 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6162 | } |
| 6163 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6164 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6165 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6166 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6167 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6168 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6169 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6170 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6171 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6172 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 6173 | char *mode = "r"; |
| 6174 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6175 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6176 | PyObject *f; |
| 6177 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6178 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 6179 | |
Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 6180 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 6181 | PyErr_Format(PyExc_ValueError, |
| 6182 | "invalid file mode '%s'", mode); |
| 6183 | return NULL; |
| 6184 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6185 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 6186 | #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H) |
Georg Brandl | 54a188a | 2006-03-31 20:00:11 +0000 | [diff] [blame] | 6187 | if (mode[0] == 'a') { |
| 6188 | /* try to make sure the O_APPEND flag is set */ |
| 6189 | int flags; |
| 6190 | flags = fcntl(fd, F_GETFL); |
| 6191 | if (flags != -1) |
| 6192 | fcntl(fd, F_SETFL, flags | O_APPEND); |
| 6193 | fp = fdopen(fd, mode); |
Thomas Wouters | 2a9a6b0 | 2006-03-31 22:38:19 +0000 | [diff] [blame] | 6194 | if (fp == NULL && flags != -1) |
Georg Brandl | 54a188a | 2006-03-31 20:00:11 +0000 | [diff] [blame] | 6195 | /* restore old mode if fdopen failed */ |
| 6196 | fcntl(fd, F_SETFL, flags); |
| 6197 | } else { |
| 6198 | fp = fdopen(fd, mode); |
| 6199 | } |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 6200 | #else |
| 6201 | fp = fdopen(fd, mode); |
| 6202 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6203 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6204 | if (fp == NULL) |
| 6205 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 6206 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 6207 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6208 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 6209 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6210 | } |
| 6211 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6212 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6213 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6214 | 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] | 6215 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6216 | |
| 6217 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 6218 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6219 | { |
| 6220 | int fd; |
| 6221 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 6222 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6223 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6224 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6225 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6226 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6227 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6228 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6229 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6230 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6231 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6232 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6233 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6234 | #if defined(PYOS_OS2) |
| 6235 | HFILE read, write; |
| 6236 | APIRET rc; |
| 6237 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6238 | Py_BEGIN_ALLOW_THREADS |
| 6239 | rc = DosCreatePipe( &read, &write, 4096); |
| 6240 | Py_END_ALLOW_THREADS |
| 6241 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6242 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6243 | |
| 6244 | return Py_BuildValue("(ii)", read, write); |
| 6245 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6246 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6247 | int fds[2]; |
| 6248 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6249 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6250 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6251 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6252 | if (res != 0) |
| 6253 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6254 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6255 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 6256 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 6257 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 6258 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6259 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 6260 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6261 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 6262 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 6263 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 6264 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 6265 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 6266 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6267 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6268 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6269 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6270 | #endif /* HAVE_PIPE */ |
| 6271 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6272 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6273 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6274 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6275 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6276 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6277 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6278 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6279 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6280 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6281 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6282 | int mode = 0666; |
| 6283 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6284 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6285 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6286 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6287 | res = mkfifo(filename, mode); |
| 6288 | Py_END_ALLOW_THREADS |
| 6289 | if (res < 0) |
| 6290 | return posix_error(); |
| 6291 | Py_INCREF(Py_None); |
| 6292 | return Py_None; |
| 6293 | } |
| 6294 | #endif |
| 6295 | |
| 6296 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6297 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6298 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6299 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6300 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 6301 | named filename. mode specifies both the permissions to use and the\n\ |
| 6302 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 6303 | 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] | 6304 | device defines the newly created device special file (probably using\n\ |
| 6305 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6306 | |
| 6307 | |
| 6308 | static PyObject * |
| 6309 | posix_mknod(PyObject *self, PyObject *args) |
| 6310 | { |
| 6311 | char *filename; |
| 6312 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6313 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6314 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 6315 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6316 | return NULL; |
| 6317 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6318 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6319 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6320 | if (res < 0) |
| 6321 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6322 | Py_INCREF(Py_None); |
| 6323 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6324 | } |
| 6325 | #endif |
| 6326 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6327 | #ifdef HAVE_DEVICE_MACROS |
| 6328 | PyDoc_STRVAR(posix_major__doc__, |
| 6329 | "major(device) -> major number\n\ |
| 6330 | Extracts a device major number from a raw device number."); |
| 6331 | |
| 6332 | static PyObject * |
| 6333 | posix_major(PyObject *self, PyObject *args) |
| 6334 | { |
| 6335 | int device; |
| 6336 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 6337 | return NULL; |
| 6338 | return PyInt_FromLong((long)major(device)); |
| 6339 | } |
| 6340 | |
| 6341 | PyDoc_STRVAR(posix_minor__doc__, |
| 6342 | "minor(device) -> minor number\n\ |
| 6343 | Extracts a device minor number from a raw device number."); |
| 6344 | |
| 6345 | static PyObject * |
| 6346 | posix_minor(PyObject *self, PyObject *args) |
| 6347 | { |
| 6348 | int device; |
| 6349 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 6350 | return NULL; |
| 6351 | return PyInt_FromLong((long)minor(device)); |
| 6352 | } |
| 6353 | |
| 6354 | PyDoc_STRVAR(posix_makedev__doc__, |
| 6355 | "makedev(major, minor) -> device number\n\ |
| 6356 | Composes a raw device number from the major and minor device numbers."); |
| 6357 | |
| 6358 | static PyObject * |
| 6359 | posix_makedev(PyObject *self, PyObject *args) |
| 6360 | { |
| 6361 | int major, minor; |
| 6362 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 6363 | return NULL; |
| 6364 | return PyInt_FromLong((long)makedev(major, minor)); |
| 6365 | } |
| 6366 | #endif /* device macros */ |
| 6367 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6368 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6369 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6370 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6371 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6372 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6373 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6374 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6375 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6376 | { |
| 6377 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6378 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6379 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6380 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6381 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6382 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6383 | return NULL; |
| 6384 | |
| 6385 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6386 | length = PyInt_AsLong(lenobj); |
| 6387 | #else |
| 6388 | length = PyLong_Check(lenobj) ? |
| 6389 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 6390 | #endif |
| 6391 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6392 | return NULL; |
| 6393 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6394 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6395 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6396 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6397 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6398 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6399 | return NULL; |
| 6400 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6401 | Py_INCREF(Py_None); |
| 6402 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6403 | } |
| 6404 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6405 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6406 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6407 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6408 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6409 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6410 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6411 | /* Save putenv() parameters as values here, so we can collect them when they |
| 6412 | * get re-set with another call for the same key. */ |
| 6413 | static PyObject *posix_putenv_garbage; |
| 6414 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6415 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6416 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6417 | { |
| 6418 | char *s1, *s2; |
Anthony Baxter | 64182fe | 2006-04-11 12:14:09 +0000 | [diff] [blame] | 6419 | char *newenv; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6420 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 6421 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6422 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6423 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6424 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6425 | |
| 6426 | #if defined(PYOS_OS2) |
| 6427 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 6428 | APIRET rc; |
| 6429 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6430 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 6431 | if (rc != NO_ERROR) |
| 6432 | return os2_error(rc); |
| 6433 | |
| 6434 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 6435 | APIRET rc; |
| 6436 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6437 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 6438 | if (rc != NO_ERROR) |
| 6439 | return os2_error(rc); |
| 6440 | } else { |
| 6441 | #endif |
| 6442 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6443 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 6444 | len = strlen(s1) + strlen(s2) + 2; |
| 6445 | /* len includes space for a trailing \0; the size arg to |
| 6446 | PyString_FromStringAndSize does not count that */ |
| 6447 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6448 | if (newstr == NULL) |
| 6449 | return PyErr_NoMemory(); |
Anthony Baxter | 64182fe | 2006-04-11 12:14:09 +0000 | [diff] [blame] | 6450 | newenv = PyString_AS_STRING(newstr); |
| 6451 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 6452 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 6453 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6454 | posix_error(); |
| 6455 | return NULL; |
| 6456 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6457 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 6458 | * this will cause previous value to be collected. This has to |
| 6459 | * happen after the real putenv() call because the old value |
| 6460 | * was still accessible until then. */ |
| 6461 | if (PyDict_SetItem(posix_putenv_garbage, |
| 6462 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 6463 | /* really not much we can do; just leak */ |
| 6464 | PyErr_Clear(); |
| 6465 | } |
| 6466 | else { |
| 6467 | Py_DECREF(newstr); |
| 6468 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6469 | |
| 6470 | #if defined(PYOS_OS2) |
| 6471 | } |
| 6472 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6473 | Py_INCREF(Py_None); |
| 6474 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6475 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6476 | #endif /* putenv */ |
| 6477 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6478 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6479 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6480 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6481 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6482 | |
| 6483 | static PyObject * |
| 6484 | posix_unsetenv(PyObject *self, PyObject *args) |
| 6485 | { |
| 6486 | char *s1; |
| 6487 | |
| 6488 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 6489 | return NULL; |
| 6490 | |
| 6491 | unsetenv(s1); |
| 6492 | |
| 6493 | /* Remove the key from posix_putenv_garbage; |
| 6494 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6495 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6496 | * old value was still accessible until then. |
| 6497 | */ |
| 6498 | if (PyDict_DelItem(posix_putenv_garbage, |
| 6499 | PyTuple_GET_ITEM(args, 0))) { |
| 6500 | /* really not much we can do; just leak */ |
| 6501 | PyErr_Clear(); |
| 6502 | } |
| 6503 | |
| 6504 | Py_INCREF(Py_None); |
| 6505 | return Py_None; |
| 6506 | } |
| 6507 | #endif /* unsetenv */ |
| 6508 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6509 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6510 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6511 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6512 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6513 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6514 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6515 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6516 | { |
| 6517 | int code; |
| 6518 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6519 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6520 | return NULL; |
| 6521 | message = strerror(code); |
| 6522 | if (message == NULL) { |
| 6523 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 6524 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6525 | return NULL; |
| 6526 | } |
| 6527 | return PyString_FromString(message); |
| 6528 | } |
| 6529 | #endif /* strerror */ |
| 6530 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6531 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6532 | #ifdef HAVE_SYS_WAIT_H |
| 6533 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6534 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6535 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6536 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6537 | 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] | 6538 | |
| 6539 | static PyObject * |
| 6540 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 6541 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6542 | WAIT_TYPE status; |
| 6543 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6544 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6545 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6546 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6547 | |
| 6548 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6549 | } |
| 6550 | #endif /* WCOREDUMP */ |
| 6551 | |
| 6552 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6553 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6554 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6555 | 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] | 6556 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6557 | |
| 6558 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6559 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6560 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6561 | WAIT_TYPE status; |
| 6562 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6563 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6564 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6565 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6566 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6567 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6568 | } |
| 6569 | #endif /* WIFCONTINUED */ |
| 6570 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6571 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6572 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6573 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6574 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6575 | |
| 6576 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6577 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6578 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6579 | WAIT_TYPE status; |
| 6580 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6581 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6582 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6583 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6584 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6585 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6586 | } |
| 6587 | #endif /* WIFSTOPPED */ |
| 6588 | |
| 6589 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6590 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6591 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6592 | 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] | 6593 | |
| 6594 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6595 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6596 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6597 | WAIT_TYPE status; |
| 6598 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6599 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6600 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6601 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6602 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6603 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6604 | } |
| 6605 | #endif /* WIFSIGNALED */ |
| 6606 | |
| 6607 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6608 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6609 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6610 | 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] | 6611 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6612 | |
| 6613 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6614 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6615 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6616 | WAIT_TYPE status; |
| 6617 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6618 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6619 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6620 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6621 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6622 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6623 | } |
| 6624 | #endif /* WIFEXITED */ |
| 6625 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6626 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6627 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6628 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6629 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6630 | |
| 6631 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6632 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6633 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6634 | WAIT_TYPE status; |
| 6635 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6636 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6637 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6638 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6639 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6640 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 6641 | } |
| 6642 | #endif /* WEXITSTATUS */ |
| 6643 | |
| 6644 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6645 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6646 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6647 | 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] | 6648 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6649 | |
| 6650 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6651 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6652 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6653 | WAIT_TYPE status; |
| 6654 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6655 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6656 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6657 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6658 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6659 | return Py_BuildValue("i", WTERMSIG(status)); |
| 6660 | } |
| 6661 | #endif /* WTERMSIG */ |
| 6662 | |
| 6663 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6664 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6665 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6666 | Return the signal that stopped the process that provided\n\ |
| 6667 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6668 | |
| 6669 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6670 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6671 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6672 | WAIT_TYPE status; |
| 6673 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6674 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6675 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6676 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6677 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6678 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 6679 | } |
| 6680 | #endif /* WSTOPSIG */ |
| 6681 | |
| 6682 | #endif /* HAVE_SYS_WAIT_H */ |
| 6683 | |
| 6684 | |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 6685 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 6686 | #ifdef _SCO_DS |
| 6687 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 6688 | needed definitions in sys/statvfs.h */ |
| 6689 | #define _SVID3 |
| 6690 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6691 | #include <sys/statvfs.h> |
| 6692 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6693 | static PyObject* |
| 6694 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 6695 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 6696 | if (v == NULL) |
| 6697 | return NULL; |
| 6698 | |
| 6699 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6700 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6701 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 6702 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 6703 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 6704 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 6705 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 6706 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 6707 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 6708 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6709 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6710 | #else |
| 6711 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6712 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6713 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6714 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6715 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6716 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6717 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6718 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6719 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6720 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6721 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6722 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6723 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6724 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6725 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6726 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6727 | #endif |
| 6728 | |
| 6729 | return v; |
| 6730 | } |
| 6731 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6732 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6733 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6734 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6735 | |
| 6736 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6737 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6738 | { |
| 6739 | int fd, res; |
| 6740 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6741 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6742 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6743 | return NULL; |
| 6744 | Py_BEGIN_ALLOW_THREADS |
| 6745 | res = fstatvfs(fd, &st); |
| 6746 | Py_END_ALLOW_THREADS |
| 6747 | if (res != 0) |
| 6748 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6749 | |
| 6750 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6751 | } |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 6752 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6753 | |
| 6754 | |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 6755 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6756 | #include <sys/statvfs.h> |
| 6757 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6758 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6759 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6760 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6761 | |
| 6762 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6763 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6764 | { |
| 6765 | char *path; |
| 6766 | int res; |
| 6767 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6768 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6769 | return NULL; |
| 6770 | Py_BEGIN_ALLOW_THREADS |
| 6771 | res = statvfs(path, &st); |
| 6772 | Py_END_ALLOW_THREADS |
| 6773 | if (res != 0) |
| 6774 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6775 | |
| 6776 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6777 | } |
| 6778 | #endif /* HAVE_STATVFS */ |
| 6779 | |
| 6780 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6781 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6782 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6783 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6784 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 6785 | 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] | 6786 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6787 | |
| 6788 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6789 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6790 | { |
| 6791 | PyObject *result = NULL; |
| 6792 | char *dir = NULL; |
| 6793 | char *pfx = NULL; |
| 6794 | char *name; |
| 6795 | |
| 6796 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 6797 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6798 | |
| 6799 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6800 | "tempnam is a potential security risk to your program") < 0) |
| 6801 | return NULL; |
| 6802 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6803 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6804 | name = _tempnam(dir, pfx); |
| 6805 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6806 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6807 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6808 | if (name == NULL) |
| 6809 | return PyErr_NoMemory(); |
| 6810 | result = PyString_FromString(name); |
| 6811 | free(name); |
| 6812 | return result; |
| 6813 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 6814 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6815 | |
| 6816 | |
| 6817 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6818 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6819 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6820 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6821 | |
| 6822 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6823 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6824 | { |
| 6825 | FILE *fp; |
| 6826 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6827 | fp = tmpfile(); |
| 6828 | if (fp == NULL) |
| 6829 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 6830 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6831 | } |
| 6832 | #endif |
| 6833 | |
| 6834 | |
| 6835 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6836 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6837 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6838 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6839 | |
| 6840 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6841 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6842 | { |
| 6843 | char buffer[L_tmpnam]; |
| 6844 | char *name; |
| 6845 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6846 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6847 | "tmpnam is a potential security risk to your program") < 0) |
| 6848 | return NULL; |
| 6849 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6850 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6851 | name = tmpnam_r(buffer); |
| 6852 | #else |
| 6853 | name = tmpnam(buffer); |
| 6854 | #endif |
| 6855 | if (name == NULL) { |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 6856 | PyObject *err = Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6857 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6858 | "unexpected NULL from tmpnam_r" |
| 6859 | #else |
| 6860 | "unexpected NULL from tmpnam" |
| 6861 | #endif |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 6862 | ); |
| 6863 | PyErr_SetObject(PyExc_OSError, err); |
| 6864 | Py_XDECREF(err); |
| 6865 | return NULL; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6866 | } |
| 6867 | return PyString_FromString(buffer); |
| 6868 | } |
| 6869 | #endif |
| 6870 | |
| 6871 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6872 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 6873 | * It maps strings representing configuration variable names to |
| 6874 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6875 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6876 | * rarely-used constants. There are three separate tables that use |
| 6877 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6878 | * |
| 6879 | * This code is always included, even if none of the interfaces that |
| 6880 | * need it are included. The #if hackery needed to avoid it would be |
| 6881 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6882 | */ |
| 6883 | struct constdef { |
| 6884 | char *name; |
| 6885 | long value; |
| 6886 | }; |
| 6887 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6888 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6889 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 6890 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6891 | { |
| 6892 | if (PyInt_Check(arg)) { |
| 6893 | *valuep = PyInt_AS_LONG(arg); |
| 6894 | return 1; |
| 6895 | } |
| 6896 | if (PyString_Check(arg)) { |
| 6897 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6898 | size_t lo = 0; |
| 6899 | size_t mid; |
| 6900 | size_t hi = tablesize; |
| 6901 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6902 | char *confname = PyString_AS_STRING(arg); |
| 6903 | while (lo < hi) { |
| 6904 | mid = (lo + hi) / 2; |
| 6905 | cmp = strcmp(confname, table[mid].name); |
| 6906 | if (cmp < 0) |
| 6907 | hi = mid; |
| 6908 | else if (cmp > 0) |
| 6909 | lo = mid + 1; |
| 6910 | else { |
| 6911 | *valuep = table[mid].value; |
| 6912 | return 1; |
| 6913 | } |
| 6914 | } |
| 6915 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 6916 | } |
| 6917 | else |
| 6918 | PyErr_SetString(PyExc_TypeError, |
| 6919 | "configuration names must be strings or integers"); |
| 6920 | return 0; |
| 6921 | } |
| 6922 | |
| 6923 | |
| 6924 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 6925 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6926 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 6927 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 6928 | #endif |
| 6929 | #ifdef _PC_ABI_ASYNC_IO |
| 6930 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 6931 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6932 | #ifdef _PC_ASYNC_IO |
| 6933 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 6934 | #endif |
| 6935 | #ifdef _PC_CHOWN_RESTRICTED |
| 6936 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 6937 | #endif |
| 6938 | #ifdef _PC_FILESIZEBITS |
| 6939 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 6940 | #endif |
| 6941 | #ifdef _PC_LAST |
| 6942 | {"PC_LAST", _PC_LAST}, |
| 6943 | #endif |
| 6944 | #ifdef _PC_LINK_MAX |
| 6945 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 6946 | #endif |
| 6947 | #ifdef _PC_MAX_CANON |
| 6948 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 6949 | #endif |
| 6950 | #ifdef _PC_MAX_INPUT |
| 6951 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 6952 | #endif |
| 6953 | #ifdef _PC_NAME_MAX |
| 6954 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 6955 | #endif |
| 6956 | #ifdef _PC_NO_TRUNC |
| 6957 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 6958 | #endif |
| 6959 | #ifdef _PC_PATH_MAX |
| 6960 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 6961 | #endif |
| 6962 | #ifdef _PC_PIPE_BUF |
| 6963 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 6964 | #endif |
| 6965 | #ifdef _PC_PRIO_IO |
| 6966 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 6967 | #endif |
| 6968 | #ifdef _PC_SOCK_MAXBUF |
| 6969 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 6970 | #endif |
| 6971 | #ifdef _PC_SYNC_IO |
| 6972 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 6973 | #endif |
| 6974 | #ifdef _PC_VDISABLE |
| 6975 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 6976 | #endif |
| 6977 | }; |
| 6978 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6979 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6980 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6981 | { |
| 6982 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 6983 | sizeof(posix_constants_pathconf) |
| 6984 | / sizeof(struct constdef)); |
| 6985 | } |
| 6986 | #endif |
| 6987 | |
| 6988 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6989 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6990 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6991 | 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] | 6992 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6993 | |
| 6994 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6995 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6996 | { |
| 6997 | PyObject *result = NULL; |
| 6998 | int name, fd; |
| 6999 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7000 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 7001 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7002 | long limit; |
| 7003 | |
| 7004 | errno = 0; |
| 7005 | limit = fpathconf(fd, name); |
| 7006 | if (limit == -1 && errno != 0) |
| 7007 | posix_error(); |
| 7008 | else |
| 7009 | result = PyInt_FromLong(limit); |
| 7010 | } |
| 7011 | return result; |
| 7012 | } |
| 7013 | #endif |
| 7014 | |
| 7015 | |
| 7016 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7017 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7018 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7019 | 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] | 7020 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7021 | |
| 7022 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7023 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7024 | { |
| 7025 | PyObject *result = NULL; |
| 7026 | int name; |
| 7027 | char *path; |
| 7028 | |
| 7029 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 7030 | conv_path_confname, &name)) { |
| 7031 | long limit; |
| 7032 | |
| 7033 | errno = 0; |
| 7034 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7035 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7036 | if (errno == EINVAL) |
| 7037 | /* could be a path or name problem */ |
| 7038 | posix_error(); |
| 7039 | else |
| 7040 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7041 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7042 | else |
| 7043 | result = PyInt_FromLong(limit); |
| 7044 | } |
| 7045 | return result; |
| 7046 | } |
| 7047 | #endif |
| 7048 | |
| 7049 | #ifdef HAVE_CONFSTR |
| 7050 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7051 | #ifdef _CS_ARCHITECTURE |
| 7052 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 7053 | #endif |
| 7054 | #ifdef _CS_HOSTNAME |
| 7055 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 7056 | #endif |
| 7057 | #ifdef _CS_HW_PROVIDER |
| 7058 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 7059 | #endif |
| 7060 | #ifdef _CS_HW_SERIAL |
| 7061 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 7062 | #endif |
| 7063 | #ifdef _CS_INITTAB_NAME |
| 7064 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 7065 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7066 | #ifdef _CS_LFS64_CFLAGS |
| 7067 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 7068 | #endif |
| 7069 | #ifdef _CS_LFS64_LDFLAGS |
| 7070 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 7071 | #endif |
| 7072 | #ifdef _CS_LFS64_LIBS |
| 7073 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 7074 | #endif |
| 7075 | #ifdef _CS_LFS64_LINTFLAGS |
| 7076 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 7077 | #endif |
| 7078 | #ifdef _CS_LFS_CFLAGS |
| 7079 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 7080 | #endif |
| 7081 | #ifdef _CS_LFS_LDFLAGS |
| 7082 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 7083 | #endif |
| 7084 | #ifdef _CS_LFS_LIBS |
| 7085 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 7086 | #endif |
| 7087 | #ifdef _CS_LFS_LINTFLAGS |
| 7088 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 7089 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7090 | #ifdef _CS_MACHINE |
| 7091 | {"CS_MACHINE", _CS_MACHINE}, |
| 7092 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7093 | #ifdef _CS_PATH |
| 7094 | {"CS_PATH", _CS_PATH}, |
| 7095 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7096 | #ifdef _CS_RELEASE |
| 7097 | {"CS_RELEASE", _CS_RELEASE}, |
| 7098 | #endif |
| 7099 | #ifdef _CS_SRPC_DOMAIN |
| 7100 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 7101 | #endif |
| 7102 | #ifdef _CS_SYSNAME |
| 7103 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 7104 | #endif |
| 7105 | #ifdef _CS_VERSION |
| 7106 | {"CS_VERSION", _CS_VERSION}, |
| 7107 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7108 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 7109 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 7110 | #endif |
| 7111 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 7112 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 7113 | #endif |
| 7114 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 7115 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 7116 | #endif |
| 7117 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 7118 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 7119 | #endif |
| 7120 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 7121 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 7122 | #endif |
| 7123 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 7124 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 7125 | #endif |
| 7126 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 7127 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 7128 | #endif |
| 7129 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 7130 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 7131 | #endif |
| 7132 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 7133 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 7134 | #endif |
| 7135 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 7136 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 7137 | #endif |
| 7138 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 7139 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 7140 | #endif |
| 7141 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 7142 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 7143 | #endif |
| 7144 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 7145 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 7146 | #endif |
| 7147 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 7148 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 7149 | #endif |
| 7150 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 7151 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 7152 | #endif |
| 7153 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 7154 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 7155 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7156 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 7157 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 7158 | #endif |
| 7159 | #ifdef _MIPS_CS_BASE |
| 7160 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 7161 | #endif |
| 7162 | #ifdef _MIPS_CS_HOSTID |
| 7163 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 7164 | #endif |
| 7165 | #ifdef _MIPS_CS_HW_NAME |
| 7166 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 7167 | #endif |
| 7168 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 7169 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 7170 | #endif |
| 7171 | #ifdef _MIPS_CS_OSREL_MAJ |
| 7172 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 7173 | #endif |
| 7174 | #ifdef _MIPS_CS_OSREL_MIN |
| 7175 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 7176 | #endif |
| 7177 | #ifdef _MIPS_CS_OSREL_PATCH |
| 7178 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 7179 | #endif |
| 7180 | #ifdef _MIPS_CS_OS_NAME |
| 7181 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 7182 | #endif |
| 7183 | #ifdef _MIPS_CS_OS_PROVIDER |
| 7184 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 7185 | #endif |
| 7186 | #ifdef _MIPS_CS_PROCESSORS |
| 7187 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 7188 | #endif |
| 7189 | #ifdef _MIPS_CS_SERIAL |
| 7190 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 7191 | #endif |
| 7192 | #ifdef _MIPS_CS_VENDOR |
| 7193 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 7194 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7195 | }; |
| 7196 | |
| 7197 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7198 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7199 | { |
| 7200 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 7201 | sizeof(posix_constants_confstr) |
| 7202 | / sizeof(struct constdef)); |
| 7203 | } |
| 7204 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7205 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7206 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7207 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7208 | |
| 7209 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7210 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7211 | { |
| 7212 | PyObject *result = NULL; |
| 7213 | int name; |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 7214 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7215 | |
| 7216 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Skip Montanaro | dd527fc | 2006-04-18 00:49:49 +0000 | [diff] [blame] | 7217 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7218 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7219 | errno = 0; |
Skip Montanaro | dd527fc | 2006-04-18 00:49:49 +0000 | [diff] [blame] | 7220 | len = confstr(name, buffer, sizeof(buffer)); |
Skip Montanaro | 94785ef | 2006-04-20 01:29:48 +0000 | [diff] [blame] | 7221 | if (len == 0) { |
| 7222 | if (errno) { |
| 7223 | posix_error(); |
| 7224 | } |
| 7225 | else { |
| 7226 | result = Py_None; |
| 7227 | Py_INCREF(Py_None); |
| 7228 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7229 | } |
| 7230 | else { |
Neal Norwitz | 0d21b1e | 2006-04-20 06:44:42 +0000 | [diff] [blame] | 7231 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 7232 | result = PyString_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7233 | if (result != NULL) |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 7234 | confstr(name, PyString_AS_STRING(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7235 | } |
| 7236 | else |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 7237 | result = PyString_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7238 | } |
| 7239 | } |
| 7240 | return result; |
| 7241 | } |
| 7242 | #endif |
| 7243 | |
| 7244 | |
| 7245 | #ifdef HAVE_SYSCONF |
| 7246 | static struct constdef posix_constants_sysconf[] = { |
| 7247 | #ifdef _SC_2_CHAR_TERM |
| 7248 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 7249 | #endif |
| 7250 | #ifdef _SC_2_C_BIND |
| 7251 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 7252 | #endif |
| 7253 | #ifdef _SC_2_C_DEV |
| 7254 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 7255 | #endif |
| 7256 | #ifdef _SC_2_C_VERSION |
| 7257 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 7258 | #endif |
| 7259 | #ifdef _SC_2_FORT_DEV |
| 7260 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 7261 | #endif |
| 7262 | #ifdef _SC_2_FORT_RUN |
| 7263 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 7264 | #endif |
| 7265 | #ifdef _SC_2_LOCALEDEF |
| 7266 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 7267 | #endif |
| 7268 | #ifdef _SC_2_SW_DEV |
| 7269 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 7270 | #endif |
| 7271 | #ifdef _SC_2_UPE |
| 7272 | {"SC_2_UPE", _SC_2_UPE}, |
| 7273 | #endif |
| 7274 | #ifdef _SC_2_VERSION |
| 7275 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 7276 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7277 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 7278 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 7279 | #endif |
| 7280 | #ifdef _SC_ACL |
| 7281 | {"SC_ACL", _SC_ACL}, |
| 7282 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7283 | #ifdef _SC_AIO_LISTIO_MAX |
| 7284 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 7285 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7286 | #ifdef _SC_AIO_MAX |
| 7287 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 7288 | #endif |
| 7289 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 7290 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 7291 | #endif |
| 7292 | #ifdef _SC_ARG_MAX |
| 7293 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 7294 | #endif |
| 7295 | #ifdef _SC_ASYNCHRONOUS_IO |
| 7296 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 7297 | #endif |
| 7298 | #ifdef _SC_ATEXIT_MAX |
| 7299 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 7300 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7301 | #ifdef _SC_AUDIT |
| 7302 | {"SC_AUDIT", _SC_AUDIT}, |
| 7303 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7304 | #ifdef _SC_AVPHYS_PAGES |
| 7305 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 7306 | #endif |
| 7307 | #ifdef _SC_BC_BASE_MAX |
| 7308 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 7309 | #endif |
| 7310 | #ifdef _SC_BC_DIM_MAX |
| 7311 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 7312 | #endif |
| 7313 | #ifdef _SC_BC_SCALE_MAX |
| 7314 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 7315 | #endif |
| 7316 | #ifdef _SC_BC_STRING_MAX |
| 7317 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 7318 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7319 | #ifdef _SC_CAP |
| 7320 | {"SC_CAP", _SC_CAP}, |
| 7321 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7322 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 7323 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 7324 | #endif |
| 7325 | #ifdef _SC_CHAR_BIT |
| 7326 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 7327 | #endif |
| 7328 | #ifdef _SC_CHAR_MAX |
| 7329 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 7330 | #endif |
| 7331 | #ifdef _SC_CHAR_MIN |
| 7332 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 7333 | #endif |
| 7334 | #ifdef _SC_CHILD_MAX |
| 7335 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 7336 | #endif |
| 7337 | #ifdef _SC_CLK_TCK |
| 7338 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 7339 | #endif |
| 7340 | #ifdef _SC_COHER_BLKSZ |
| 7341 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 7342 | #endif |
| 7343 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 7344 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 7345 | #endif |
| 7346 | #ifdef _SC_DCACHE_ASSOC |
| 7347 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 7348 | #endif |
| 7349 | #ifdef _SC_DCACHE_BLKSZ |
| 7350 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 7351 | #endif |
| 7352 | #ifdef _SC_DCACHE_LINESZ |
| 7353 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 7354 | #endif |
| 7355 | #ifdef _SC_DCACHE_SZ |
| 7356 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 7357 | #endif |
| 7358 | #ifdef _SC_DCACHE_TBLKSZ |
| 7359 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 7360 | #endif |
| 7361 | #ifdef _SC_DELAYTIMER_MAX |
| 7362 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 7363 | #endif |
| 7364 | #ifdef _SC_EQUIV_CLASS_MAX |
| 7365 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 7366 | #endif |
| 7367 | #ifdef _SC_EXPR_NEST_MAX |
| 7368 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 7369 | #endif |
| 7370 | #ifdef _SC_FSYNC |
| 7371 | {"SC_FSYNC", _SC_FSYNC}, |
| 7372 | #endif |
| 7373 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 7374 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 7375 | #endif |
| 7376 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 7377 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 7378 | #endif |
| 7379 | #ifdef _SC_ICACHE_ASSOC |
| 7380 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 7381 | #endif |
| 7382 | #ifdef _SC_ICACHE_BLKSZ |
| 7383 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 7384 | #endif |
| 7385 | #ifdef _SC_ICACHE_LINESZ |
| 7386 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 7387 | #endif |
| 7388 | #ifdef _SC_ICACHE_SZ |
| 7389 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 7390 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7391 | #ifdef _SC_INF |
| 7392 | {"SC_INF", _SC_INF}, |
| 7393 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7394 | #ifdef _SC_INT_MAX |
| 7395 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 7396 | #endif |
| 7397 | #ifdef _SC_INT_MIN |
| 7398 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 7399 | #endif |
| 7400 | #ifdef _SC_IOV_MAX |
| 7401 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 7402 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7403 | #ifdef _SC_IP_SECOPTS |
| 7404 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 7405 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7406 | #ifdef _SC_JOB_CONTROL |
| 7407 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 7408 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7409 | #ifdef _SC_KERN_POINTERS |
| 7410 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 7411 | #endif |
| 7412 | #ifdef _SC_KERN_SIM |
| 7413 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 7414 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7415 | #ifdef _SC_LINE_MAX |
| 7416 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 7417 | #endif |
| 7418 | #ifdef _SC_LOGIN_NAME_MAX |
| 7419 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 7420 | #endif |
| 7421 | #ifdef _SC_LOGNAME_MAX |
| 7422 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 7423 | #endif |
| 7424 | #ifdef _SC_LONG_BIT |
| 7425 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 7426 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7427 | #ifdef _SC_MAC |
| 7428 | {"SC_MAC", _SC_MAC}, |
| 7429 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7430 | #ifdef _SC_MAPPED_FILES |
| 7431 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 7432 | #endif |
| 7433 | #ifdef _SC_MAXPID |
| 7434 | {"SC_MAXPID", _SC_MAXPID}, |
| 7435 | #endif |
| 7436 | #ifdef _SC_MB_LEN_MAX |
| 7437 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 7438 | #endif |
| 7439 | #ifdef _SC_MEMLOCK |
| 7440 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 7441 | #endif |
| 7442 | #ifdef _SC_MEMLOCK_RANGE |
| 7443 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 7444 | #endif |
| 7445 | #ifdef _SC_MEMORY_PROTECTION |
| 7446 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 7447 | #endif |
| 7448 | #ifdef _SC_MESSAGE_PASSING |
| 7449 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 7450 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7451 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 7452 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 7453 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7454 | #ifdef _SC_MQ_OPEN_MAX |
| 7455 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 7456 | #endif |
| 7457 | #ifdef _SC_MQ_PRIO_MAX |
| 7458 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 7459 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7460 | #ifdef _SC_NACLS_MAX |
| 7461 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 7462 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7463 | #ifdef _SC_NGROUPS_MAX |
| 7464 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 7465 | #endif |
| 7466 | #ifdef _SC_NL_ARGMAX |
| 7467 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 7468 | #endif |
| 7469 | #ifdef _SC_NL_LANGMAX |
| 7470 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 7471 | #endif |
| 7472 | #ifdef _SC_NL_MSGMAX |
| 7473 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 7474 | #endif |
| 7475 | #ifdef _SC_NL_NMAX |
| 7476 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 7477 | #endif |
| 7478 | #ifdef _SC_NL_SETMAX |
| 7479 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 7480 | #endif |
| 7481 | #ifdef _SC_NL_TEXTMAX |
| 7482 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 7483 | #endif |
| 7484 | #ifdef _SC_NPROCESSORS_CONF |
| 7485 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 7486 | #endif |
| 7487 | #ifdef _SC_NPROCESSORS_ONLN |
| 7488 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 7489 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7490 | #ifdef _SC_NPROC_CONF |
| 7491 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 7492 | #endif |
| 7493 | #ifdef _SC_NPROC_ONLN |
| 7494 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 7495 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7496 | #ifdef _SC_NZERO |
| 7497 | {"SC_NZERO", _SC_NZERO}, |
| 7498 | #endif |
| 7499 | #ifdef _SC_OPEN_MAX |
| 7500 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 7501 | #endif |
| 7502 | #ifdef _SC_PAGESIZE |
| 7503 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 7504 | #endif |
| 7505 | #ifdef _SC_PAGE_SIZE |
| 7506 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 7507 | #endif |
| 7508 | #ifdef _SC_PASS_MAX |
| 7509 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 7510 | #endif |
| 7511 | #ifdef _SC_PHYS_PAGES |
| 7512 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 7513 | #endif |
| 7514 | #ifdef _SC_PII |
| 7515 | {"SC_PII", _SC_PII}, |
| 7516 | #endif |
| 7517 | #ifdef _SC_PII_INTERNET |
| 7518 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 7519 | #endif |
| 7520 | #ifdef _SC_PII_INTERNET_DGRAM |
| 7521 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 7522 | #endif |
| 7523 | #ifdef _SC_PII_INTERNET_STREAM |
| 7524 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 7525 | #endif |
| 7526 | #ifdef _SC_PII_OSI |
| 7527 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 7528 | #endif |
| 7529 | #ifdef _SC_PII_OSI_CLTS |
| 7530 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 7531 | #endif |
| 7532 | #ifdef _SC_PII_OSI_COTS |
| 7533 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 7534 | #endif |
| 7535 | #ifdef _SC_PII_OSI_M |
| 7536 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 7537 | #endif |
| 7538 | #ifdef _SC_PII_SOCKET |
| 7539 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 7540 | #endif |
| 7541 | #ifdef _SC_PII_XTI |
| 7542 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 7543 | #endif |
| 7544 | #ifdef _SC_POLL |
| 7545 | {"SC_POLL", _SC_POLL}, |
| 7546 | #endif |
| 7547 | #ifdef _SC_PRIORITIZED_IO |
| 7548 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 7549 | #endif |
| 7550 | #ifdef _SC_PRIORITY_SCHEDULING |
| 7551 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 7552 | #endif |
| 7553 | #ifdef _SC_REALTIME_SIGNALS |
| 7554 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 7555 | #endif |
| 7556 | #ifdef _SC_RE_DUP_MAX |
| 7557 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 7558 | #endif |
| 7559 | #ifdef _SC_RTSIG_MAX |
| 7560 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 7561 | #endif |
| 7562 | #ifdef _SC_SAVED_IDS |
| 7563 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 7564 | #endif |
| 7565 | #ifdef _SC_SCHAR_MAX |
| 7566 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 7567 | #endif |
| 7568 | #ifdef _SC_SCHAR_MIN |
| 7569 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 7570 | #endif |
| 7571 | #ifdef _SC_SELECT |
| 7572 | {"SC_SELECT", _SC_SELECT}, |
| 7573 | #endif |
| 7574 | #ifdef _SC_SEMAPHORES |
| 7575 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 7576 | #endif |
| 7577 | #ifdef _SC_SEM_NSEMS_MAX |
| 7578 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 7579 | #endif |
| 7580 | #ifdef _SC_SEM_VALUE_MAX |
| 7581 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 7582 | #endif |
| 7583 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 7584 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 7585 | #endif |
| 7586 | #ifdef _SC_SHRT_MAX |
| 7587 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 7588 | #endif |
| 7589 | #ifdef _SC_SHRT_MIN |
| 7590 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 7591 | #endif |
| 7592 | #ifdef _SC_SIGQUEUE_MAX |
| 7593 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 7594 | #endif |
| 7595 | #ifdef _SC_SIGRT_MAX |
| 7596 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 7597 | #endif |
| 7598 | #ifdef _SC_SIGRT_MIN |
| 7599 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 7600 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7601 | #ifdef _SC_SOFTPOWER |
| 7602 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 7603 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7604 | #ifdef _SC_SPLIT_CACHE |
| 7605 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 7606 | #endif |
| 7607 | #ifdef _SC_SSIZE_MAX |
| 7608 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 7609 | #endif |
| 7610 | #ifdef _SC_STACK_PROT |
| 7611 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 7612 | #endif |
| 7613 | #ifdef _SC_STREAM_MAX |
| 7614 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 7615 | #endif |
| 7616 | #ifdef _SC_SYNCHRONIZED_IO |
| 7617 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 7618 | #endif |
| 7619 | #ifdef _SC_THREADS |
| 7620 | {"SC_THREADS", _SC_THREADS}, |
| 7621 | #endif |
| 7622 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 7623 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 7624 | #endif |
| 7625 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 7626 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 7627 | #endif |
| 7628 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 7629 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 7630 | #endif |
| 7631 | #ifdef _SC_THREAD_KEYS_MAX |
| 7632 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 7633 | #endif |
| 7634 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 7635 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 7636 | #endif |
| 7637 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 7638 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 7639 | #endif |
| 7640 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 7641 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 7642 | #endif |
| 7643 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 7644 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 7645 | #endif |
| 7646 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 7647 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 7648 | #endif |
| 7649 | #ifdef _SC_THREAD_STACK_MIN |
| 7650 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 7651 | #endif |
| 7652 | #ifdef _SC_THREAD_THREADS_MAX |
| 7653 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 7654 | #endif |
| 7655 | #ifdef _SC_TIMERS |
| 7656 | {"SC_TIMERS", _SC_TIMERS}, |
| 7657 | #endif |
| 7658 | #ifdef _SC_TIMER_MAX |
| 7659 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 7660 | #endif |
| 7661 | #ifdef _SC_TTY_NAME_MAX |
| 7662 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 7663 | #endif |
| 7664 | #ifdef _SC_TZNAME_MAX |
| 7665 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 7666 | #endif |
| 7667 | #ifdef _SC_T_IOV_MAX |
| 7668 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 7669 | #endif |
| 7670 | #ifdef _SC_UCHAR_MAX |
| 7671 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 7672 | #endif |
| 7673 | #ifdef _SC_UINT_MAX |
| 7674 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 7675 | #endif |
| 7676 | #ifdef _SC_UIO_MAXIOV |
| 7677 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 7678 | #endif |
| 7679 | #ifdef _SC_ULONG_MAX |
| 7680 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 7681 | #endif |
| 7682 | #ifdef _SC_USHRT_MAX |
| 7683 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 7684 | #endif |
| 7685 | #ifdef _SC_VERSION |
| 7686 | {"SC_VERSION", _SC_VERSION}, |
| 7687 | #endif |
| 7688 | #ifdef _SC_WORD_BIT |
| 7689 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 7690 | #endif |
| 7691 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 7692 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 7693 | #endif |
| 7694 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 7695 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 7696 | #endif |
| 7697 | #ifdef _SC_XBS5_LP64_OFF64 |
| 7698 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 7699 | #endif |
| 7700 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 7701 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 7702 | #endif |
| 7703 | #ifdef _SC_XOPEN_CRYPT |
| 7704 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 7705 | #endif |
| 7706 | #ifdef _SC_XOPEN_ENH_I18N |
| 7707 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 7708 | #endif |
| 7709 | #ifdef _SC_XOPEN_LEGACY |
| 7710 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 7711 | #endif |
| 7712 | #ifdef _SC_XOPEN_REALTIME |
| 7713 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 7714 | #endif |
| 7715 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 7716 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 7717 | #endif |
| 7718 | #ifdef _SC_XOPEN_SHM |
| 7719 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 7720 | #endif |
| 7721 | #ifdef _SC_XOPEN_UNIX |
| 7722 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 7723 | #endif |
| 7724 | #ifdef _SC_XOPEN_VERSION |
| 7725 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 7726 | #endif |
| 7727 | #ifdef _SC_XOPEN_XCU_VERSION |
| 7728 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 7729 | #endif |
| 7730 | #ifdef _SC_XOPEN_XPG2 |
| 7731 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 7732 | #endif |
| 7733 | #ifdef _SC_XOPEN_XPG3 |
| 7734 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 7735 | #endif |
| 7736 | #ifdef _SC_XOPEN_XPG4 |
| 7737 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 7738 | #endif |
| 7739 | }; |
| 7740 | |
| 7741 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7742 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7743 | { |
| 7744 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 7745 | sizeof(posix_constants_sysconf) |
| 7746 | / sizeof(struct constdef)); |
| 7747 | } |
| 7748 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7749 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7750 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7751 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7752 | |
| 7753 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7754 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7755 | { |
| 7756 | PyObject *result = NULL; |
| 7757 | int name; |
| 7758 | |
| 7759 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 7760 | int value; |
| 7761 | |
| 7762 | errno = 0; |
| 7763 | value = sysconf(name); |
| 7764 | if (value == -1 && errno != 0) |
| 7765 | posix_error(); |
| 7766 | else |
| 7767 | result = PyInt_FromLong(value); |
| 7768 | } |
| 7769 | return result; |
| 7770 | } |
| 7771 | #endif |
| 7772 | |
| 7773 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7774 | /* This code is used to ensure that the tables of configuration value names |
| 7775 | * are in sorted order as required by conv_confname(), and also to build the |
| 7776 | * the exported dictionaries that are used to publish information about the |
| 7777 | * names available on the host platform. |
| 7778 | * |
| 7779 | * Sorting the table at runtime ensures that the table is properly ordered |
| 7780 | * when used, even for platforms we're not able to test on. It also makes |
| 7781 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7782 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7783 | |
| 7784 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7785 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7786 | { |
| 7787 | const struct constdef *c1 = |
| 7788 | (const struct constdef *) v1; |
| 7789 | const struct constdef *c2 = |
| 7790 | (const struct constdef *) v2; |
| 7791 | |
| 7792 | return strcmp(c1->name, c2->name); |
| 7793 | } |
| 7794 | |
| 7795 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7796 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7797 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7798 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7799 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7800 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7801 | |
| 7802 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 7803 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7804 | if (d == NULL) |
| 7805 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7806 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7807 | for (i=0; i < tablesize; ++i) { |
| 7808 | PyObject *o = PyInt_FromLong(table[i].value); |
| 7809 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 7810 | Py_XDECREF(o); |
| 7811 | Py_DECREF(d); |
| 7812 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7813 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7814 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7815 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7816 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7817 | } |
| 7818 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7819 | /* Return -1 on failure, 0 on success. */ |
| 7820 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7821 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7822 | { |
| 7823 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7824 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7825 | sizeof(posix_constants_pathconf) |
| 7826 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7827 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7828 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7829 | #endif |
| 7830 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7831 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7832 | sizeof(posix_constants_confstr) |
| 7833 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7834 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7835 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7836 | #endif |
| 7837 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7838 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7839 | sizeof(posix_constants_sysconf) |
| 7840 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7841 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7842 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7843 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7844 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7845 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7846 | |
| 7847 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7848 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7849 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7850 | 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] | 7851 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7852 | |
| 7853 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7854 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7855 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7856 | abort(); |
| 7857 | /*NOTREACHED*/ |
| 7858 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 7859 | return NULL; |
| 7860 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7861 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7862 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7863 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7864 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 7865 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7866 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7867 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 7868 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 7869 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 7870 | application (if any) its extension is associated.\n\ |
| 7871 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 7872 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7873 | \n\ |
| 7874 | startfile returns as soon as the associated application is launched.\n\ |
| 7875 | There is no option to wait for the application to close, and no way\n\ |
| 7876 | to retrieve the application's exit status.\n\ |
| 7877 | \n\ |
| 7878 | The filepath is relative to the current directory. If you want to use\n\ |
| 7879 | 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] | 7880 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7881 | |
| 7882 | static PyObject * |
| 7883 | win32_startfile(PyObject *self, PyObject *args) |
| 7884 | { |
| 7885 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7886 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7887 | HINSTANCE rc; |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7888 | #ifdef Py_WIN_WIDE_FILENAMES |
| 7889 | if (unicode_file_names()) { |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7890 | PyObject *unipath, *woperation = NULL; |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7891 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 7892 | &unipath, &operation)) { |
| 7893 | PyErr_Clear(); |
| 7894 | goto normal; |
| 7895 | } |
| 7896 | |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7897 | |
| 7898 | if (operation) { |
| 7899 | woperation = PyUnicode_DecodeASCII(operation, |
| 7900 | strlen(operation), NULL); |
| 7901 | if (!woperation) { |
| 7902 | PyErr_Clear(); |
| 7903 | operation = NULL; |
| 7904 | goto normal; |
| 7905 | } |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7906 | } |
| 7907 | |
| 7908 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7909 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7910 | PyUnicode_AS_UNICODE(unipath), |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7911 | NULL, NULL, SW_SHOWNORMAL); |
| 7912 | Py_END_ALLOW_THREADS |
| 7913 | |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7914 | Py_XDECREF(woperation); |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7915 | if (rc <= (HINSTANCE)32) { |
| 7916 | PyObject *errval = win32_error_unicode("startfile", |
| 7917 | PyUnicode_AS_UNICODE(unipath)); |
| 7918 | return errval; |
| 7919 | } |
| 7920 | Py_INCREF(Py_None); |
| 7921 | return Py_None; |
| 7922 | } |
| 7923 | #endif |
| 7924 | |
| 7925 | normal: |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7926 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 7927 | Py_FileSystemDefaultEncoding, &filepath, |
| 7928 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7929 | return NULL; |
| 7930 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7931 | rc = ShellExecute((HWND)0, operation, filepath, |
| 7932 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7933 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 7934 | if (rc <= (HINSTANCE)32) { |
| 7935 | PyObject *errval = win32_error("startfile", filepath); |
| 7936 | PyMem_Free(filepath); |
| 7937 | return errval; |
| 7938 | } |
| 7939 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7940 | Py_INCREF(Py_None); |
| 7941 | return Py_None; |
| 7942 | } |
| 7943 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7944 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7945 | #ifdef HAVE_GETLOADAVG |
| 7946 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 7947 | "getloadavg() -> (float, float, float)\n\n\ |
| 7948 | Return the number of processes in the system run queue averaged over\n\ |
| 7949 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 7950 | was unobtainable"); |
| 7951 | |
| 7952 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7953 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7954 | { |
| 7955 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7956 | if (getloadavg(loadavg, 3)!=3) { |
| 7957 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 7958 | return NULL; |
| 7959 | } else |
| 7960 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 7961 | } |
| 7962 | #endif |
| 7963 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7964 | #ifdef MS_WINDOWS |
| 7965 | |
| 7966 | PyDoc_STRVAR(win32_urandom__doc__, |
| 7967 | "urandom(n) -> str\n\n\ |
| 7968 | Return a string of n random bytes suitable for cryptographic use."); |
| 7969 | |
| 7970 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 7971 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 7972 | DWORD dwFlags ); |
| 7973 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 7974 | BYTE *pbBuffer ); |
| 7975 | |
| 7976 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
| 7977 | static HCRYPTPROV hCryptProv = 0; |
| 7978 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7979 | static PyObject* |
| 7980 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7981 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7982 | int howMany; |
| 7983 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7984 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7985 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 7986 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7987 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 7988 | if (howMany < 0) |
| 7989 | return PyErr_Format(PyExc_ValueError, |
| 7990 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7991 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7992 | if (hCryptProv == 0) { |
| 7993 | HINSTANCE hAdvAPI32 = NULL; |
| 7994 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7995 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7996 | /* Obtain handle to the DLL containing CryptoAPI |
| 7997 | This should not fail */ |
| 7998 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 7999 | if(hAdvAPI32 == NULL) |
| 8000 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8001 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 8002 | /* Obtain pointers to the CryptoAPI functions |
| 8003 | This will fail on some early versions of Win95 */ |
| 8004 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 8005 | hAdvAPI32, |
| 8006 | "CryptAcquireContextA"); |
| 8007 | if (pCryptAcquireContext == NULL) |
| 8008 | return PyErr_Format(PyExc_NotImplementedError, |
| 8009 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8010 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 8011 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 8012 | hAdvAPI32, "CryptGenRandom"); |
Georg Brandl | b20cb33 | 2006-09-06 06:04:06 +0000 | [diff] [blame] | 8013 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 8014 | return PyErr_Format(PyExc_NotImplementedError, |
| 8015 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8016 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 8017 | /* Acquire context */ |
| 8018 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 8019 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 8020 | return win32_error("CryptAcquireContext", NULL); |
| 8021 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8022 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 8023 | /* Allocate bytes */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 8024 | result = PyString_FromStringAndSize(NULL, howMany); |
| 8025 | if (result != NULL) { |
| 8026 | /* Get random data */ |
| 8027 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 8028 | PyString_AS_STRING(result))) { |
| 8029 | Py_DECREF(result); |
| 8030 | return win32_error("CryptGenRandom", NULL); |
| 8031 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 8032 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 8033 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8034 | } |
| 8035 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8036 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 8037 | #ifdef __VMS |
| 8038 | /* Use openssl random routine */ |
| 8039 | #include <openssl/rand.h> |
| 8040 | PyDoc_STRVAR(vms_urandom__doc__, |
| 8041 | "urandom(n) -> str\n\n\ |
| 8042 | Return a string of n random bytes suitable for cryptographic use."); |
| 8043 | |
| 8044 | static PyObject* |
| 8045 | vms_urandom(PyObject *self, PyObject *args) |
| 8046 | { |
| 8047 | int howMany; |
| 8048 | PyObject* result; |
| 8049 | |
| 8050 | /* Read arguments */ |
| 8051 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 8052 | return NULL; |
| 8053 | if (howMany < 0) |
| 8054 | return PyErr_Format(PyExc_ValueError, |
| 8055 | "negative argument not allowed"); |
| 8056 | |
| 8057 | /* Allocate bytes */ |
| 8058 | result = PyString_FromStringAndSize(NULL, howMany); |
| 8059 | if (result != NULL) { |
| 8060 | /* Get random data */ |
| 8061 | if (RAND_pseudo_bytes((unsigned char*) |
| 8062 | PyString_AS_STRING(result), |
| 8063 | howMany) < 0) { |
| 8064 | Py_DECREF(result); |
| 8065 | return PyErr_Format(PyExc_ValueError, |
| 8066 | "RAND_pseudo_bytes"); |
| 8067 | } |
| 8068 | } |
| 8069 | return result; |
| 8070 | } |
| 8071 | #endif |
| 8072 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8073 | static PyMethodDef posix_methods[] = { |
| 8074 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 8075 | #ifdef HAVE_TTYNAME |
| 8076 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 8077 | #endif |
| 8078 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 8079 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8080 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8081 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8082 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 8083 | #ifdef HAVE_LCHOWN |
| 8084 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 8085 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 8086 | #ifdef HAVE_CHROOT |
| 8087 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 8088 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8089 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8090 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8091 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 8092 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8093 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 8094 | #ifdef Py_USING_UNICODE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8095 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 8096 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 8097 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8098 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8099 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8100 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8101 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 8102 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 8103 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8104 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8105 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8106 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8107 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8108 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8109 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8110 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 8111 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 8112 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 8113 | {"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] | 8114 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8115 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8116 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8117 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8118 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8119 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8120 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8121 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8122 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8123 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8124 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 8125 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 8126 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8127 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8128 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8129 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8130 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8131 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8132 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 8133 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8134 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 8135 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8136 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 8137 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 8138 | #if defined(PYOS_OS2) |
| 8139 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 8140 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 8141 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 8142 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 8143 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8144 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 8145 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8146 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8147 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8148 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 8149 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8150 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 8151 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 8152 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8153 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 8154 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8155 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8156 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8157 | #endif /* HAVE_GETEGID */ |
| 8158 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8159 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8160 | #endif /* HAVE_GETEUID */ |
| 8161 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8162 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8163 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8164 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8165 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8166 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8167 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8168 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8169 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8170 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8171 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8172 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8173 | #endif /* HAVE_GETPPID */ |
| 8174 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8175 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8176 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8177 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8178 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8179 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8180 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8181 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8182 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 8183 | #ifdef HAVE_KILLPG |
| 8184 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 8185 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8186 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8187 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8188 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8189 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8190 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8191 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 8192 | {"popen2", win32_popen2, METH_VARARGS}, |
| 8193 | {"popen3", win32_popen3, METH_VARARGS}, |
| 8194 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 8195 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8196 | #else |
| 8197 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 8198 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 8199 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 8200 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 8201 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 8202 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8203 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8204 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8205 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8206 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8207 | #ifdef HAVE_SETEUID |
| 8208 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 8209 | #endif /* HAVE_SETEUID */ |
| 8210 | #ifdef HAVE_SETEGID |
| 8211 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 8212 | #endif /* HAVE_SETEGID */ |
| 8213 | #ifdef HAVE_SETREUID |
| 8214 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 8215 | #endif /* HAVE_SETREUID */ |
| 8216 | #ifdef HAVE_SETREGID |
| 8217 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 8218 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8219 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8220 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8221 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8222 | #ifdef HAVE_SETGROUPS |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 8223 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8224 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 8225 | #ifdef HAVE_GETPGID |
| 8226 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 8227 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8228 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8229 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8230 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8231 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8232 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8233 | #endif /* HAVE_WAIT */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 8234 | #ifdef HAVE_WAIT3 |
| 8235 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 8236 | #endif /* HAVE_WAIT3 */ |
| 8237 | #ifdef HAVE_WAIT4 |
| 8238 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 8239 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 8240 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8241 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8242 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8243 | #ifdef HAVE_GETSID |
| 8244 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 8245 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8246 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8247 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8248 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8249 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8250 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8251 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8252 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8253 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8254 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8255 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8256 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8257 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8258 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 8259 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 8260 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 8261 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 8262 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 8263 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 8264 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 8265 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 8266 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8267 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8268 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8269 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8270 | #endif |
| 8271 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8272 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8273 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 8274 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8275 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 8276 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8277 | #ifdef HAVE_DEVICE_MACROS |
| 8278 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 8279 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 8280 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 8281 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8282 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8283 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8284 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8285 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8286 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8287 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8288 | #ifdef HAVE_UNSETENV |
| 8289 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 8290 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8291 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8292 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8293 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8294 | #ifdef HAVE_FCHDIR |
| 8295 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 8296 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8297 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8298 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8299 | #endif |
| 8300 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8301 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8302 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8303 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8304 | #ifdef WCOREDUMP |
| 8305 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 8306 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8307 | #ifdef WIFCONTINUED |
| 8308 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 8309 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8310 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8311 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8312 | #endif /* WIFSTOPPED */ |
| 8313 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8314 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8315 | #endif /* WIFSIGNALED */ |
| 8316 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8317 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8318 | #endif /* WIFEXITED */ |
| 8319 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8320 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8321 | #endif /* WEXITSTATUS */ |
| 8322 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8323 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8324 | #endif /* WTERMSIG */ |
| 8325 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8326 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8327 | #endif /* WSTOPSIG */ |
| 8328 | #endif /* HAVE_SYS_WAIT_H */ |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 8329 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8330 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8331 | #endif |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 8332 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8333 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8334 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 8335 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8336 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8337 | #endif |
| 8338 | #ifdef HAVE_TEMPNAM |
| 8339 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 8340 | #endif |
| 8341 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8342 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8343 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8344 | #ifdef HAVE_CONFSTR |
| 8345 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 8346 | #endif |
| 8347 | #ifdef HAVE_SYSCONF |
| 8348 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 8349 | #endif |
| 8350 | #ifdef HAVE_FPATHCONF |
| 8351 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 8352 | #endif |
| 8353 | #ifdef HAVE_PATHCONF |
| 8354 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 8355 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8356 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8357 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 8358 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 8359 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8360 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8361 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8362 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8363 | #ifdef MS_WINDOWS |
| 8364 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 8365 | #endif |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 8366 | #ifdef __VMS |
| 8367 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 8368 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8369 | {NULL, NULL} /* Sentinel */ |
| 8370 | }; |
| 8371 | |
| 8372 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8373 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8374 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8375 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8376 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8377 | } |
| 8378 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8379 | #if defined(PYOS_OS2) |
| 8380 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8381 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8382 | { |
| 8383 | APIRET rc; |
| 8384 | ULONG values[QSV_MAX+1]; |
| 8385 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 8386 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8387 | |
| 8388 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 8389 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8390 | Py_END_ALLOW_THREADS |
| 8391 | |
| 8392 | if (rc != NO_ERROR) { |
| 8393 | os2_error(rc); |
| 8394 | return -1; |
| 8395 | } |
| 8396 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8397 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 8398 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 8399 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 8400 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 8401 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 8402 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 8403 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8404 | |
| 8405 | switch (values[QSV_VERSION_MINOR]) { |
| 8406 | case 0: ver = "2.00"; break; |
| 8407 | case 10: ver = "2.10"; break; |
| 8408 | case 11: ver = "2.11"; break; |
| 8409 | case 30: ver = "3.00"; break; |
| 8410 | case 40: ver = "4.00"; break; |
| 8411 | case 50: ver = "5.00"; break; |
| 8412 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 8413 | PyOS_snprintf(tmp, sizeof(tmp), |
| 8414 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 8415 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8416 | ver = &tmp[0]; |
| 8417 | } |
| 8418 | |
| 8419 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8420 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8421 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8422 | |
| 8423 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 8424 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 8425 | tmp[1] = ':'; |
| 8426 | tmp[2] = '\0'; |
| 8427 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8428 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8429 | } |
| 8430 | #endif |
| 8431 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8432 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 8433 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8434 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8435 | #ifdef F_OK |
| 8436 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8437 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8438 | #ifdef R_OK |
| 8439 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8440 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8441 | #ifdef W_OK |
| 8442 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8443 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8444 | #ifdef X_OK |
| 8445 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8446 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8447 | #ifdef NGROUPS_MAX |
| 8448 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 8449 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8450 | #ifdef TMP_MAX |
| 8451 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 8452 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8453 | #ifdef WCONTINUED |
| 8454 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 8455 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8456 | #ifdef WNOHANG |
| 8457 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8458 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8459 | #ifdef WUNTRACED |
| 8460 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 8461 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8462 | #ifdef O_RDONLY |
| 8463 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 8464 | #endif |
| 8465 | #ifdef O_WRONLY |
| 8466 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 8467 | #endif |
| 8468 | #ifdef O_RDWR |
| 8469 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 8470 | #endif |
| 8471 | #ifdef O_NDELAY |
| 8472 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 8473 | #endif |
| 8474 | #ifdef O_NONBLOCK |
| 8475 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 8476 | #endif |
| 8477 | #ifdef O_APPEND |
| 8478 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 8479 | #endif |
| 8480 | #ifdef O_DSYNC |
| 8481 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 8482 | #endif |
| 8483 | #ifdef O_RSYNC |
| 8484 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 8485 | #endif |
| 8486 | #ifdef O_SYNC |
| 8487 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 8488 | #endif |
| 8489 | #ifdef O_NOCTTY |
| 8490 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 8491 | #endif |
| 8492 | #ifdef O_CREAT |
| 8493 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 8494 | #endif |
| 8495 | #ifdef O_EXCL |
| 8496 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 8497 | #endif |
| 8498 | #ifdef O_TRUNC |
| 8499 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 8500 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 8501 | #ifdef O_BINARY |
| 8502 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 8503 | #endif |
| 8504 | #ifdef O_TEXT |
| 8505 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 8506 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8507 | #ifdef O_LARGEFILE |
| 8508 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 8509 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 8510 | #ifdef O_SHLOCK |
| 8511 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 8512 | #endif |
| 8513 | #ifdef O_EXLOCK |
| 8514 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 8515 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8516 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8517 | /* MS Windows */ |
| 8518 | #ifdef O_NOINHERIT |
| 8519 | /* Don't inherit in child processes. */ |
| 8520 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 8521 | #endif |
| 8522 | #ifdef _O_SHORT_LIVED |
| 8523 | /* Optimize for short life (keep in memory). */ |
| 8524 | /* MS forgot to define this one with a non-underscore form too. */ |
| 8525 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 8526 | #endif |
| 8527 | #ifdef O_TEMPORARY |
| 8528 | /* Automatically delete when last handle is closed. */ |
| 8529 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 8530 | #endif |
| 8531 | #ifdef O_RANDOM |
| 8532 | /* Optimize for random access. */ |
| 8533 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 8534 | #endif |
| 8535 | #ifdef O_SEQUENTIAL |
| 8536 | /* Optimize for sequential access. */ |
| 8537 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 8538 | #endif |
| 8539 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8540 | /* GNU extensions. */ |
| 8541 | #ifdef O_DIRECT |
| 8542 | /* Direct disk access. */ |
| 8543 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 8544 | #endif |
| 8545 | #ifdef O_DIRECTORY |
| 8546 | /* Must be a directory. */ |
| 8547 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 8548 | #endif |
| 8549 | #ifdef O_NOFOLLOW |
| 8550 | /* Do not follow links. */ |
| 8551 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 8552 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8553 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8554 | /* These come from sysexits.h */ |
| 8555 | #ifdef EX_OK |
| 8556 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8557 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8558 | #ifdef EX_USAGE |
| 8559 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8560 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8561 | #ifdef EX_DATAERR |
| 8562 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8563 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8564 | #ifdef EX_NOINPUT |
| 8565 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8566 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8567 | #ifdef EX_NOUSER |
| 8568 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8569 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8570 | #ifdef EX_NOHOST |
| 8571 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8572 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8573 | #ifdef EX_UNAVAILABLE |
| 8574 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8575 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8576 | #ifdef EX_SOFTWARE |
| 8577 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8578 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8579 | #ifdef EX_OSERR |
| 8580 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8581 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8582 | #ifdef EX_OSFILE |
| 8583 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8584 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8585 | #ifdef EX_CANTCREAT |
| 8586 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8587 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8588 | #ifdef EX_IOERR |
| 8589 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8590 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8591 | #ifdef EX_TEMPFAIL |
| 8592 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8593 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8594 | #ifdef EX_PROTOCOL |
| 8595 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8596 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8597 | #ifdef EX_NOPERM |
| 8598 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8599 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8600 | #ifdef EX_CONFIG |
| 8601 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8602 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8603 | #ifdef EX_NOTFOUND |
| 8604 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8605 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8606 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8607 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8608 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 8609 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 8610 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 8611 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 8612 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 8613 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 8614 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 8615 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 8616 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 8617 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 8618 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 8619 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 8620 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 8621 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 8622 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 8623 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 8624 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 8625 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 8626 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 8627 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 8628 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 8629 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 8630 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 8631 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 8632 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 8633 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 8634 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8635 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8636 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8637 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8638 | #if defined(PYOS_OS2) |
| 8639 | if (insertvalues(d)) return -1; |
| 8640 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8641 | return 0; |
| 8642 | } |
| 8643 | |
| 8644 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8645 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8646 | #define INITFUNC initnt |
| 8647 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8648 | |
| 8649 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8650 | #define INITFUNC initos2 |
| 8651 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8652 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8653 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8654 | #define INITFUNC initposix |
| 8655 | #define MODNAME "posix" |
| 8656 | #endif |
| 8657 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 8658 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 8659 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8660 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8661 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8662 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8663 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8664 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8665 | posix__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 8666 | if (m == NULL) |
| 8667 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8668 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8669 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8670 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8671 | Py_XINCREF(v); |
| 8672 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8673 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8674 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8675 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8676 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8677 | return; |
| 8678 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8679 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8680 | return; |
| 8681 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8682 | Py_INCREF(PyExc_OSError); |
| 8683 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8684 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8685 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 8686 | if (posix_putenv_garbage == NULL) |
| 8687 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8688 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8689 | |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 8690 | if (!initialized) { |
| 8691 | stat_result_desc.name = MODNAME ".stat_result"; |
| 8692 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 8693 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 8694 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 8695 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 8696 | structseq_new = StatResultType.tp_new; |
| 8697 | StatResultType.tp_new = statresult_new; |
| 8698 | |
| 8699 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 8700 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
| 8701 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8702 | Py_INCREF((PyObject*) &StatResultType); |
| 8703 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8704 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 8705 | PyModule_AddObject(m, "statvfs_result", |
| 8706 | (PyObject*) &StatVFSResultType); |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 8707 | initialized = 1; |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 8708 | |
| 8709 | #ifdef __APPLE__ |
| 8710 | /* |
| 8711 | * Step 2 of weak-linking support on Mac OS X. |
| 8712 | * |
| 8713 | * The code below removes functions that are not available on the |
| 8714 | * currently active platform. |
| 8715 | * |
| 8716 | * This block allow one to use a python binary that was build on |
| 8717 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 8718 | * OSX 10.4. |
| 8719 | */ |
| 8720 | #ifdef HAVE_FSTATVFS |
| 8721 | if (fstatvfs == NULL) { |
| 8722 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 8723 | return; |
| 8724 | } |
| 8725 | } |
| 8726 | #endif /* HAVE_FSTATVFS */ |
| 8727 | |
| 8728 | #ifdef HAVE_STATVFS |
| 8729 | if (statvfs == NULL) { |
| 8730 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 8731 | return; |
| 8732 | } |
| 8733 | } |
| 8734 | #endif /* HAVE_STATVFS */ |
| 8735 | |
| 8736 | # ifdef HAVE_LCHOWN |
| 8737 | if (lchown == NULL) { |
| 8738 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 8739 | return; |
| 8740 | } |
| 8741 | } |
| 8742 | #endif /* HAVE_LCHOWN */ |
| 8743 | |
| 8744 | |
| 8745 | #endif /* __APPLE__ */ |
| 8746 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8747 | } |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 8748 | |
| 8749 | #ifdef __cplusplus |
| 8750 | } |
| 8751 | #endif |
| 8752 | |
Martin v. Löwis | 2c7aa63 | 2006-10-15 09:44:02 +0000 | [diff] [blame] | 8753 | |