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 | f43893a | 2006-10-09 20:44:25 +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 | 012bc72 | 2006-10-15 09:43:39 +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 | |
| 847 | static BOOL WINAPI |
| 848 | Py_GetFileAttributesExA(LPCSTR pszFile, |
| 849 | GET_FILEEX_INFO_LEVELS level, |
| 850 | LPVOID pv) |
| 851 | { |
| 852 | BOOL result; |
| 853 | HANDLE hFindFile; |
| 854 | WIN32_FIND_DATAA FileData; |
| 855 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 856 | /* First try to use the system's implementation, if that is |
| 857 | available and either succeeds to gives an error other than |
| 858 | that it isn't implemented. */ |
| 859 | check_gfax(); |
| 860 | if (gfaxa) { |
| 861 | result = gfaxa(pszFile, level, pv); |
| 862 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 863 | return result; |
| 864 | } |
| 865 | /* It's either not present, or not implemented. |
| 866 | Emulate using FindFirstFile. */ |
| 867 | if (level != GetFileExInfoStandard) { |
| 868 | SetLastError(ERROR_INVALID_PARAMETER); |
| 869 | return FALSE; |
| 870 | } |
| 871 | /* Use GetFileAttributes to validate that the file name |
| 872 | does not contain wildcards (which FindFirstFile would |
| 873 | accept). */ |
| 874 | if (GetFileAttributesA(pszFile) == 0xFFFFFFFF) |
| 875 | return FALSE; |
| 876 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 877 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 878 | return FALSE; |
| 879 | FindClose(hFindFile); |
| 880 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 881 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 882 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 883 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 884 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 885 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 886 | return TRUE; |
| 887 | } |
| 888 | |
| 889 | static BOOL WINAPI |
| 890 | Py_GetFileAttributesExW(LPCWSTR pszFile, |
| 891 | GET_FILEEX_INFO_LEVELS level, |
| 892 | LPVOID pv) |
| 893 | { |
| 894 | BOOL result; |
| 895 | HANDLE hFindFile; |
| 896 | WIN32_FIND_DATAW FileData; |
| 897 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; |
| 898 | /* First try to use the system's implementation, if that is |
| 899 | available and either succeeds to gives an error other than |
| 900 | that it isn't implemented. */ |
| 901 | check_gfax(); |
| 902 | if (gfaxa) { |
| 903 | result = gfaxw(pszFile, level, pv); |
| 904 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 905 | return result; |
| 906 | } |
| 907 | /* It's either not present, or not implemented. |
| 908 | Emulate using FindFirstFile. */ |
| 909 | if (level != GetFileExInfoStandard) { |
| 910 | SetLastError(ERROR_INVALID_PARAMETER); |
| 911 | return FALSE; |
| 912 | } |
| 913 | /* Use GetFileAttributes to validate that the file name |
| 914 | does not contain wildcards (which FindFirstFile would |
| 915 | accept). */ |
| 916 | if (GetFileAttributesW(pszFile) == 0xFFFFFFFF) |
| 917 | return FALSE; |
| 918 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 919 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 920 | return FALSE; |
| 921 | FindClose(hFindFile); |
| 922 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 923 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 924 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 925 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 926 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 927 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 928 | return TRUE; |
| 929 | } |
| 930 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 931 | static int |
| 932 | win32_stat(const char* path, struct win32_stat *result) |
| 933 | { |
| 934 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 935 | int code; |
| 936 | char *dot; |
| 937 | /* XXX not supported on Win95 and NT 3.x */ |
Martin v. Löwis | 012bc72 | 2006-10-15 09:43:39 +0000 | [diff] [blame] | 938 | if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 939 | /* Protocol violation: we explicitly clear errno, instead of |
| 940 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 941 | errno = 0; |
| 942 | return -1; |
| 943 | } |
| 944 | code = attribute_data_to_stat(&info, result); |
| 945 | if (code != 0) |
| 946 | return code; |
| 947 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 948 | dot = strrchr(path, '.'); |
| 949 | if (dot) { |
| 950 | if (stricmp(dot, ".bat") == 0 || |
| 951 | stricmp(dot, ".cmd") == 0 || |
| 952 | stricmp(dot, ".exe") == 0 || |
| 953 | stricmp(dot, ".com") == 0) |
| 954 | result->st_mode |= 0111; |
| 955 | } |
| 956 | return code; |
| 957 | } |
| 958 | |
| 959 | static int |
| 960 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 961 | { |
| 962 | int code; |
| 963 | const wchar_t *dot; |
| 964 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 965 | /* XXX not supported on Win95 and NT 3.x */ |
Martin v. Löwis | 012bc72 | 2006-10-15 09:43:39 +0000 | [diff] [blame] | 966 | if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 967 | /* Protocol violation: we explicitly clear errno, instead of |
| 968 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 969 | errno = 0; |
| 970 | return -1; |
| 971 | } |
| 972 | code = attribute_data_to_stat(&info, result); |
| 973 | if (code < 0) |
| 974 | return code; |
| 975 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 976 | dot = wcsrchr(path, '.'); |
| 977 | if (dot) { |
| 978 | if (_wcsicmp(dot, L".bat") == 0 || |
| 979 | _wcsicmp(dot, L".cmd") == 0 || |
| 980 | _wcsicmp(dot, L".exe") == 0 || |
| 981 | _wcsicmp(dot, L".com") == 0) |
| 982 | result->st_mode |= 0111; |
| 983 | } |
| 984 | return code; |
| 985 | } |
| 986 | |
| 987 | static int |
| 988 | win32_fstat(int file_number, struct win32_stat *result) |
| 989 | { |
| 990 | BY_HANDLE_FILE_INFORMATION info; |
| 991 | HANDLE h; |
| 992 | int type; |
| 993 | |
| 994 | h = (HANDLE)_get_osfhandle(file_number); |
| 995 | |
| 996 | /* Protocol violation: we explicitly clear errno, instead of |
| 997 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 998 | errno = 0; |
| 999 | |
| 1000 | if (h == INVALID_HANDLE_VALUE) { |
| 1001 | /* This is really a C library error (invalid file handle). |
| 1002 | We set the Win32 error to the closes one matching. */ |
| 1003 | SetLastError(ERROR_INVALID_HANDLE); |
| 1004 | return -1; |
| 1005 | } |
| 1006 | memset(result, 0, sizeof(*result)); |
| 1007 | |
| 1008 | type = GetFileType(h); |
| 1009 | if (type == FILE_TYPE_UNKNOWN) { |
| 1010 | DWORD error = GetLastError(); |
| 1011 | if (error != 0) { |
| 1012 | return -1; |
| 1013 | } |
| 1014 | /* else: valid but unknown file */ |
| 1015 | } |
| 1016 | |
| 1017 | if (type != FILE_TYPE_DISK) { |
| 1018 | if (type == FILE_TYPE_CHAR) |
| 1019 | result->st_mode = _S_IFCHR; |
| 1020 | else if (type == FILE_TYPE_PIPE) |
| 1021 | result->st_mode = _S_IFIFO; |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
| 1025 | if (!GetFileInformationByHandle(h, &info)) { |
| 1026 | return -1; |
| 1027 | } |
| 1028 | |
| 1029 | /* similar to stat() */ |
| 1030 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1031 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1032 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1033 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1034 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1035 | /* specific to fstat() */ |
| 1036 | result->st_nlink = info.nNumberOfLinks; |
| 1037 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
| 1041 | #endif /* MS_WINDOWS */ |
| 1042 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1043 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1044 | "stat_result: Result from stat or lstat.\n\n\ |
| 1045 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1046 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1047 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1048 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1049 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1050 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1051 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1052 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1053 | |
| 1054 | static PyStructSequence_Field stat_result_fields[] = { |
| 1055 | {"st_mode", "protection bits"}, |
| 1056 | {"st_ino", "inode"}, |
| 1057 | {"st_dev", "device"}, |
| 1058 | {"st_nlink", "number of hard links"}, |
| 1059 | {"st_uid", "user ID of owner"}, |
| 1060 | {"st_gid", "group ID of owner"}, |
| 1061 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1062 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1063 | {NULL, "integer time of last access"}, |
| 1064 | {NULL, "integer time of last modification"}, |
| 1065 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1066 | {"st_atime", "time of last access"}, |
| 1067 | {"st_mtime", "time of last modification"}, |
| 1068 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1069 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1070 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 1071 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1072 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1073 | {"st_blocks", "number of blocks allocated"}, |
| 1074 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1075 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1076 | {"st_rdev", "device type (if inode device)"}, |
| 1077 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1078 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1079 | {"st_flags", "user defined flags for file"}, |
| 1080 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1081 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1082 | {"st_gen", "generation number"}, |
| 1083 | #endif |
| 1084 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1085 | {"st_birthtime", "time of creation"}, |
| 1086 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1087 | {0} |
| 1088 | }; |
| 1089 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1090 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1091 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1092 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1093 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1094 | #endif |
| 1095 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1096 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1097 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1098 | #else |
| 1099 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1100 | #endif |
| 1101 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1102 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1103 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1104 | #else |
| 1105 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1106 | #endif |
| 1107 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1108 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1109 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1110 | #else |
| 1111 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1112 | #endif |
| 1113 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1114 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1115 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1116 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1117 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1118 | #endif |
| 1119 | |
| 1120 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1121 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1122 | #else |
| 1123 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1124 | #endif |
| 1125 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1126 | static PyStructSequence_Desc stat_result_desc = { |
| 1127 | "stat_result", /* name */ |
| 1128 | stat_result__doc__, /* doc */ |
| 1129 | stat_result_fields, |
| 1130 | 10 |
| 1131 | }; |
| 1132 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1133 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1134 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1135 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1136 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1137 | 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] | 1138 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1139 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1140 | |
| 1141 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 1142 | {"f_bsize", }, |
| 1143 | {"f_frsize", }, |
| 1144 | {"f_blocks", }, |
| 1145 | {"f_bfree", }, |
| 1146 | {"f_bavail", }, |
| 1147 | {"f_files", }, |
| 1148 | {"f_ffree", }, |
| 1149 | {"f_favail", }, |
| 1150 | {"f_flag", }, |
| 1151 | {"f_namemax",}, |
| 1152 | {0} |
| 1153 | }; |
| 1154 | |
| 1155 | static PyStructSequence_Desc statvfs_result_desc = { |
| 1156 | "statvfs_result", /* name */ |
| 1157 | statvfs_result__doc__, /* doc */ |
| 1158 | statvfs_result_fields, |
| 1159 | 10 |
| 1160 | }; |
| 1161 | |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 1162 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1163 | static PyTypeObject StatResultType; |
| 1164 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1165 | static newfunc structseq_new; |
| 1166 | |
| 1167 | static PyObject * |
| 1168 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1169 | { |
| 1170 | PyStructSequence *result; |
| 1171 | int i; |
| 1172 | |
| 1173 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1174 | if (!result) |
| 1175 | return NULL; |
| 1176 | /* If we have been initialized from a tuple, |
| 1177 | st_?time might be set to None. Initialize it |
| 1178 | from the int slots. */ |
| 1179 | for (i = 7; i <= 9; i++) { |
| 1180 | if (result->ob_item[i+3] == Py_None) { |
| 1181 | Py_DECREF(Py_None); |
| 1182 | Py_INCREF(result->ob_item[i]); |
| 1183 | result->ob_item[i+3] = result->ob_item[i]; |
| 1184 | } |
| 1185 | } |
| 1186 | return (PyObject*)result; |
| 1187 | } |
| 1188 | |
| 1189 | |
| 1190 | |
| 1191 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1192 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1193 | |
| 1194 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1195 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1196 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1197 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1198 | future calls return ints. \n\ |
| 1199 | If newval is omitted, return the current setting.\n"); |
| 1200 | |
| 1201 | static PyObject* |
| 1202 | stat_float_times(PyObject* self, PyObject *args) |
| 1203 | { |
| 1204 | int newval = -1; |
| 1205 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1206 | return NULL; |
| 1207 | if (newval == -1) |
| 1208 | /* Return old value */ |
| 1209 | return PyBool_FromLong(_stat_float_times); |
| 1210 | _stat_float_times = newval; |
| 1211 | Py_INCREF(Py_None); |
| 1212 | return Py_None; |
| 1213 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1214 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1215 | static void |
| 1216 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1217 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1218 | PyObject *fval,*ival; |
| 1219 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1220 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1221 | #else |
| 1222 | ival = PyInt_FromLong((long)sec); |
| 1223 | #endif |
Neal Norwitz | e0a81af | 2006-08-12 01:50:38 +0000 | [diff] [blame] | 1224 | if (!ival) |
| 1225 | return; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1226 | if (_stat_float_times) { |
| 1227 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1228 | } else { |
| 1229 | fval = ival; |
| 1230 | Py_INCREF(fval); |
| 1231 | } |
| 1232 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1233 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1236 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1237 | (used by posix_stat() and posix_fstat()) */ |
| 1238 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1239 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1240 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1241 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1242 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1243 | if (v == NULL) |
| 1244 | return NULL; |
| 1245 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1246 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1247 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1248 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1249 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1250 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1251 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1252 | #endif |
| 1253 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1254 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1255 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1256 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1257 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1258 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1259 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); |
| 1260 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); |
| 1261 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1262 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1263 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1264 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1265 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1266 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1267 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1268 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1269 | #if defined(HAVE_STAT_TV_NSEC) |
| 1270 | ansec = st->st_atim.tv_nsec; |
| 1271 | mnsec = st->st_mtim.tv_nsec; |
| 1272 | cnsec = st->st_ctim.tv_nsec; |
| 1273 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1274 | ansec = st->st_atimespec.tv_nsec; |
| 1275 | mnsec = st->st_mtimespec.tv_nsec; |
| 1276 | cnsec = st->st_ctimespec.tv_nsec; |
| 1277 | #elif defined(HAVE_STAT_NSEC) |
| 1278 | ansec = st->st_atime_nsec; |
| 1279 | mnsec = st->st_mtime_nsec; |
| 1280 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1281 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1282 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1283 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1284 | fill_time(v, 7, st->st_atime, ansec); |
| 1285 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1286 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1287 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1288 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1289 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1290 | PyInt_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1291 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1292 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1293 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1294 | PyInt_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1295 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1296 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1297 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1298 | PyInt_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1299 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1300 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1301 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1302 | PyInt_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1303 | #endif |
| 1304 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1305 | { |
| 1306 | PyObject *val; |
| 1307 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1308 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1309 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1310 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1311 | #else |
| 1312 | bnsec = 0; |
| 1313 | #endif |
| 1314 | if (_stat_float_times) { |
| 1315 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1316 | } else { |
| 1317 | val = PyInt_FromLong((long)bsec); |
| 1318 | } |
| 1319 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1320 | val); |
| 1321 | } |
| 1322 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1323 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1324 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1325 | PyInt_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1326 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1327 | |
| 1328 | if (PyErr_Occurred()) { |
| 1329 | Py_DECREF(v); |
| 1330 | return NULL; |
| 1331 | } |
| 1332 | |
| 1333 | return v; |
| 1334 | } |
| 1335 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1336 | #ifdef MS_WINDOWS |
| 1337 | |
| 1338 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1339 | where / can be used in place of \ and the trailing slash is optional. |
| 1340 | Both SERVER and SHARE must have at least one character. |
| 1341 | */ |
| 1342 | |
| 1343 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1344 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Kristján Valur Jónsson | dbeaa69 | 2006-06-09 16:28:01 +0000 | [diff] [blame] | 1345 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1346 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Kristján Valur Jónsson | dbeaa69 | 2006-06-09 16:28:01 +0000 | [diff] [blame] | 1347 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1348 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1349 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1350 | IsUNCRootA(char *path, int pathlen) |
| 1351 | { |
| 1352 | #define ISSLASH ISSLASHA |
| 1353 | |
| 1354 | int i, share; |
| 1355 | |
| 1356 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1357 | /* minimum UNCRoot is \\x\y */ |
| 1358 | return FALSE; |
| 1359 | for (i = 2; i < pathlen ; i++) |
| 1360 | if (ISSLASH(path[i])) break; |
| 1361 | if (i == 2 || i == pathlen) |
| 1362 | /* do not allow \\\SHARE or \\SERVER */ |
| 1363 | return FALSE; |
| 1364 | share = i+1; |
| 1365 | for (i = share; i < pathlen; i++) |
| 1366 | if (ISSLASH(path[i])) break; |
| 1367 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1368 | |
| 1369 | #undef ISSLASH |
| 1370 | } |
| 1371 | |
| 1372 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1373 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1374 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1375 | { |
| 1376 | #define ISSLASH ISSLASHW |
| 1377 | |
| 1378 | int i, share; |
| 1379 | |
| 1380 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1381 | /* minimum UNCRoot is \\x\y */ |
| 1382 | return FALSE; |
| 1383 | for (i = 2; i < pathlen ; i++) |
| 1384 | if (ISSLASH(path[i])) break; |
| 1385 | if (i == 2 || i == pathlen) |
| 1386 | /* do not allow \\\SHARE or \\SERVER */ |
| 1387 | return FALSE; |
| 1388 | share = i+1; |
| 1389 | for (i = share; i < pathlen; i++) |
| 1390 | if (ISSLASH(path[i])) break; |
| 1391 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1392 | |
| 1393 | #undef ISSLASH |
| 1394 | } |
| 1395 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1396 | #endif /* MS_WINDOWS */ |
| 1397 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1398 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1399 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1400 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1401 | #ifdef __VMS |
| 1402 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1403 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1404 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1405 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1406 | char *wformat, |
| 1407 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1408 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1409 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1410 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1411 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1412 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1413 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1414 | |
| 1415 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1416 | /* If on wide-character-capable OS see if argument |
| 1417 | is Unicode and if so use wide API. */ |
| 1418 | if (unicode_file_names()) { |
| 1419 | PyUnicodeObject *po; |
| 1420 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1421 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1422 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1423 | Py_BEGIN_ALLOW_THREADS |
| 1424 | /* PyUnicode_AS_UNICODE result OK without |
| 1425 | thread lock as it is a simple dereference. */ |
| 1426 | res = wstatfunc(wpath, &st); |
| 1427 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1428 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1429 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1430 | return win32_error_unicode("stat", wpath); |
| 1431 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1432 | } |
| 1433 | /* Drop the argument parsing error as narrow strings |
| 1434 | are also valid. */ |
| 1435 | PyErr_Clear(); |
| 1436 | } |
| 1437 | #endif |
| 1438 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1439 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1440 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1441 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1442 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1443 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1444 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1445 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1446 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1447 | |
| 1448 | if (res != 0) { |
| 1449 | #ifdef MS_WINDOWS |
| 1450 | result = win32_error("stat", pathfree); |
| 1451 | #else |
| 1452 | result = posix_error_with_filename(pathfree); |
| 1453 | #endif |
| 1454 | } |
| 1455 | else |
| 1456 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1457 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1458 | PyMem_Free(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1459 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1462 | /* POSIX methods */ |
| 1463 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1464 | PyDoc_STRVAR(posix_access__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1465 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1466 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1467 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1468 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1469 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1470 | 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] | 1471 | |
| 1472 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1473 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1474 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1475 | char *path; |
| 1476 | int mode; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1477 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1478 | #ifdef Py_WIN_WIDE_FILENAMES |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1479 | DWORD attr; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1480 | if (unicode_file_names()) { |
| 1481 | PyUnicodeObject *po; |
| 1482 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1483 | Py_BEGIN_ALLOW_THREADS |
| 1484 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1485 | it is a simple dereference. */ |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1486 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1487 | Py_END_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1488 | goto finish; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1489 | } |
| 1490 | /* Drop the argument parsing error as narrow strings |
| 1491 | are also valid. */ |
| 1492 | PyErr_Clear(); |
| 1493 | } |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1494 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1495 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 1496 | return 0; |
| 1497 | Py_BEGIN_ALLOW_THREADS |
| 1498 | attr = GetFileAttributesA(path); |
| 1499 | Py_END_ALLOW_THREADS |
| 1500 | PyMem_Free(path); |
| 1501 | finish: |
| 1502 | if (attr == 0xFFFFFFFF) |
| 1503 | /* File does not exist, or cannot read attributes */ |
| 1504 | return PyBool_FromLong(0); |
| 1505 | /* Access is possible if either write access wasn't requested, or |
| 1506 | the file isn't read-only. */ |
Martin v. Löwis | ee1e06d | 2006-07-02 18:44:00 +0000 | [diff] [blame] | 1507 | return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY)); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1508 | #else |
| 1509 | int res; |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame] | 1510 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1511 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1512 | return NULL; |
| 1513 | Py_BEGIN_ALLOW_THREADS |
| 1514 | res = access(path, mode); |
| 1515 | Py_END_ALLOW_THREADS |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 1516 | PyMem_Free(path); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1517 | return PyBool_FromLong(res == 0); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1518 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1521 | #ifndef F_OK |
| 1522 | #define F_OK 0 |
| 1523 | #endif |
| 1524 | #ifndef R_OK |
| 1525 | #define R_OK 4 |
| 1526 | #endif |
| 1527 | #ifndef W_OK |
| 1528 | #define W_OK 2 |
| 1529 | #endif |
| 1530 | #ifndef X_OK |
| 1531 | #define X_OK 1 |
| 1532 | #endif |
| 1533 | |
| 1534 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1535 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1536 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1537 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1538 | |
| 1539 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1540 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1541 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1542 | int id; |
| 1543 | char *ret; |
| 1544 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1545 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1546 | return NULL; |
| 1547 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1548 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1549 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1550 | if (id == 0) { |
| 1551 | ret = ttyname(); |
| 1552 | } |
| 1553 | else { |
| 1554 | ret = NULL; |
| 1555 | } |
| 1556 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1557 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1558 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1559 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1560 | return posix_error(); |
| 1561 | return PyString_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1562 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1563 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1564 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1565 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1566 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1567 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1568 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1569 | |
| 1570 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1571 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1572 | { |
| 1573 | char *ret; |
| 1574 | char buffer[L_ctermid]; |
| 1575 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1576 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1577 | ret = ctermid_r(buffer); |
| 1578 | #else |
| 1579 | ret = ctermid(buffer); |
| 1580 | #endif |
| 1581 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1582 | return posix_error(); |
| 1583 | return PyString_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1584 | } |
| 1585 | #endif |
| 1586 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1587 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1588 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1589 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1590 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1591 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1592 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1593 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1594 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1595 | 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] | 1596 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1597 | return posix_1str(args, "et:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1598 | #elif defined(__VMS) |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1599 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1600 | #else |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1601 | return posix_1str(args, "et:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1602 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1605 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1606 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1607 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1608 | 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] | 1609 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1610 | |
| 1611 | static PyObject * |
| 1612 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1613 | { |
| 1614 | return posix_fildes(fdobj, fchdir); |
| 1615 | } |
| 1616 | #endif /* HAVE_FCHDIR */ |
| 1617 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1618 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1619 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1620 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1621 | Change the access permissions of a file."); |
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_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1625 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1626 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1627 | int i; |
| 1628 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1629 | #ifdef Py_WIN_WIDE_FILENAMES |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1630 | DWORD attr; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1631 | if (unicode_file_names()) { |
| 1632 | PyUnicodeObject *po; |
| 1633 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1634 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1635 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1636 | if (attr != 0xFFFFFFFF) { |
| 1637 | if (i & _S_IWRITE) |
| 1638 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1639 | else |
| 1640 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1641 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1642 | } |
| 1643 | else |
| 1644 | res = 0; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1645 | Py_END_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1646 | if (!res) |
| 1647 | return win32_error_unicode("chmod", |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1648 | PyUnicode_AS_UNICODE(po)); |
| 1649 | Py_INCREF(Py_None); |
| 1650 | return Py_None; |
| 1651 | } |
| 1652 | /* Drop the argument parsing error as narrow strings |
| 1653 | are also valid. */ |
| 1654 | PyErr_Clear(); |
| 1655 | } |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1656 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
| 1657 | &path, &i)) |
| 1658 | return NULL; |
| 1659 | Py_BEGIN_ALLOW_THREADS |
| 1660 | attr = GetFileAttributesA(path); |
| 1661 | if (attr != 0xFFFFFFFF) { |
| 1662 | if (i & _S_IWRITE) |
| 1663 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1664 | else |
| 1665 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1666 | res = SetFileAttributesA(path, attr); |
| 1667 | } |
| 1668 | else |
| 1669 | res = 0; |
| 1670 | Py_END_ALLOW_THREADS |
| 1671 | if (!res) { |
| 1672 | win32_error("chmod", path); |
| 1673 | PyMem_Free(path); |
| 1674 | return NULL; |
| 1675 | } |
Martin v. Löwis | 9f485bc | 2006-05-08 05:25:56 +0000 | [diff] [blame] | 1676 | PyMem_Free(path); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1677 | Py_INCREF(Py_None); |
| 1678 | return Py_None; |
| 1679 | #else /* Py_WIN_WIDE_FILENAMES */ |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1680 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1681 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1682 | return NULL; |
| 1683 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1684 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1685 | Py_END_ALLOW_THREADS |
| 1686 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1687 | return posix_error_with_allocated_filename(path); |
| 1688 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1689 | Py_INCREF(Py_None); |
| 1690 | return Py_None; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1691 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1694 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1695 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1696 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1697 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1698 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1699 | |
| 1700 | static PyObject * |
| 1701 | posix_chroot(PyObject *self, PyObject *args) |
| 1702 | { |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1703 | return posix_1str(args, "et:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1704 | } |
| 1705 | #endif |
| 1706 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1707 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1708 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1709 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1710 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1711 | |
| 1712 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1713 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1714 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1715 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1716 | } |
| 1717 | #endif /* HAVE_FSYNC */ |
| 1718 | |
| 1719 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1720 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1721 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1722 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1723 | #endif |
| 1724 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1725 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1726 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1727 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1728 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1729 | |
| 1730 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1731 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1732 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1733 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1734 | } |
| 1735 | #endif /* HAVE_FDATASYNC */ |
| 1736 | |
| 1737 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1738 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1739 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1740 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1741 | 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] | 1742 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1743 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1744 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1745 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1746 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1747 | int uid, gid; |
| 1748 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1749 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1750 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1751 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1752 | return NULL; |
| 1753 | Py_BEGIN_ALLOW_THREADS |
| 1754 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1755 | Py_END_ALLOW_THREADS |
| 1756 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1757 | return posix_error_with_allocated_filename(path); |
| 1758 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1759 | Py_INCREF(Py_None); |
| 1760 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1761 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1762 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1763 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1764 | #ifdef HAVE_LCHOWN |
| 1765 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1766 | "lchown(path, uid, gid)\n\n\ |
| 1767 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1768 | This function will not follow symbolic links."); |
| 1769 | |
| 1770 | static PyObject * |
| 1771 | posix_lchown(PyObject *self, PyObject *args) |
| 1772 | { |
| 1773 | char *path = NULL; |
| 1774 | int uid, gid; |
| 1775 | int res; |
| 1776 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1777 | Py_FileSystemDefaultEncoding, &path, |
| 1778 | &uid, &gid)) |
| 1779 | return NULL; |
| 1780 | Py_BEGIN_ALLOW_THREADS |
| 1781 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1782 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1783 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1784 | return posix_error_with_allocated_filename(path); |
| 1785 | PyMem_Free(path); |
| 1786 | Py_INCREF(Py_None); |
| 1787 | return Py_None; |
| 1788 | } |
| 1789 | #endif /* HAVE_LCHOWN */ |
| 1790 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1791 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1792 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1793 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1794 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1795 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1796 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1797 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1798 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1799 | { |
| 1800 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1801 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1802 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1803 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1804 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1805 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1806 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1807 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1808 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1809 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1810 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1811 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1812 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1813 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1814 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1815 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1816 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1817 | "getcwdu() -> path\n\n\ |
| 1818 | Return a unicode string representing the current working directory."); |
| 1819 | |
| 1820 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1821 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1822 | { |
| 1823 | char buf[1026]; |
| 1824 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1825 | |
| 1826 | #ifdef Py_WIN_WIDE_FILENAMES |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1827 | DWORD len; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1828 | if (unicode_file_names()) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1829 | wchar_t wbuf[1026]; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1830 | wchar_t *wbuf2 = wbuf; |
| 1831 | PyObject *resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1832 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1833 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 1834 | /* If the buffer is large enough, len does not include the |
| 1835 | terminating \0. If the buffer is too small, len includes |
| 1836 | the space needed for the terminator. */ |
| 1837 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 1838 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 1839 | if (wbuf2) |
| 1840 | len = GetCurrentDirectoryW(len, wbuf2); |
| 1841 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1842 | Py_END_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1843 | if (!wbuf2) { |
| 1844 | PyErr_NoMemory(); |
| 1845 | return NULL; |
| 1846 | } |
| 1847 | if (!len) { |
| 1848 | if (wbuf2 != wbuf) free(wbuf2); |
| 1849 | return win32_error("getcwdu", NULL); |
| 1850 | } |
| 1851 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 1852 | if (wbuf2 != wbuf) free(wbuf2); |
| 1853 | return resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1854 | } |
| 1855 | #endif |
| 1856 | |
| 1857 | Py_BEGIN_ALLOW_THREADS |
| 1858 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1859 | res = _getcwd2(buf, sizeof buf); |
| 1860 | #else |
| 1861 | res = getcwd(buf, sizeof buf); |
| 1862 | #endif |
| 1863 | Py_END_ALLOW_THREADS |
| 1864 | if (res == NULL) |
| 1865 | return posix_error(); |
| 1866 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1867 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1868 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1869 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1870 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1871 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1872 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1873 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1874 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1875 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1876 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1877 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1878 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1879 | { |
Martin v. Löwis | 4fc2bda | 2006-05-04 12:04:27 +0000 | [diff] [blame] | 1880 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1881 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1882 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1883 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1884 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1885 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1886 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1887 | Return a list containing the names of the entries in the directory.\n\ |
| 1888 | \n\ |
| 1889 | path: path of directory to list\n\ |
| 1890 | \n\ |
| 1891 | 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] | 1892 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1893 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1894 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1895 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1896 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1897 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1898 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1899 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1900 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1901 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1902 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1903 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1904 | WIN32_FIND_DATA FileData; |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1905 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1906 | char *bufptr = namebuf; |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1907 | 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] | 1908 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1909 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1910 | /* If on wide-character-capable OS see if argument |
| 1911 | is Unicode and if so use wide API. */ |
| 1912 | if (unicode_file_names()) { |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1913 | PyObject *po; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1914 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1915 | WIN32_FIND_DATAW wFileData; |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1916 | Py_UNICODE *wnamebuf; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1917 | Py_UNICODE wch; |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1918 | /* Overallocate for \\*.*\0 */ |
| 1919 | len = PyUnicode_GET_SIZE(po); |
| 1920 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 1921 | if (!wnamebuf) { |
| 1922 | PyErr_NoMemory(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1923 | return NULL; |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1924 | } |
| 1925 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
| 1926 | wch = len > 0 ? wnamebuf[len-1] : '\0'; |
| 1927 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1928 | wnamebuf[len++] = L'\\'; |
| 1929 | wcscpy(wnamebuf + len, L"*.*"); |
| 1930 | if ((d = PyList_New(0)) == NULL) { |
| 1931 | free(wnamebuf); |
| 1932 | return NULL; |
| 1933 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1934 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1935 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1936 | int error = GetLastError(); |
| 1937 | if (error == ERROR_FILE_NOT_FOUND) { |
| 1938 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1939 | return d; |
| 1940 | } |
| 1941 | Py_DECREF(d); |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1942 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1943 | free(wnamebuf); |
| 1944 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1945 | } |
| 1946 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1947 | /* Skip over . and .. */ |
| 1948 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 1949 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 1950 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1951 | if (v == NULL) { |
| 1952 | Py_DECREF(d); |
| 1953 | d = NULL; |
| 1954 | break; |
| 1955 | } |
| 1956 | if (PyList_Append(d, v) != 0) { |
| 1957 | Py_DECREF(v); |
| 1958 | Py_DECREF(d); |
| 1959 | d = NULL; |
| 1960 | break; |
| 1961 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1962 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1963 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1964 | Py_BEGIN_ALLOW_THREADS |
| 1965 | result = FindNextFileW(hFindFile, &wFileData); |
| 1966 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 982e9fe | 2006-07-24 12:54:17 +0000 | [diff] [blame] | 1967 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 1968 | it got to the end of the directory. */ |
| 1969 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 1970 | Py_DECREF(d); |
| 1971 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 1972 | FindClose(hFindFile); |
| 1973 | free(wnamebuf); |
| 1974 | return NULL; |
| 1975 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1976 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1977 | |
| 1978 | if (FindClose(hFindFile) == FALSE) { |
| 1979 | Py_DECREF(d); |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1980 | win32_error_unicode("FindClose", wnamebuf); |
| 1981 | free(wnamebuf); |
| 1982 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1983 | } |
Martin v. Löwis | e3edaea | 2006-05-15 05:51:36 +0000 | [diff] [blame] | 1984 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1985 | return d; |
| 1986 | } |
| 1987 | /* Drop the argument parsing error as narrow strings |
| 1988 | are also valid. */ |
| 1989 | PyErr_Clear(); |
| 1990 | } |
| 1991 | #endif |
| 1992 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1993 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1994 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1995 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1996 | if (len > 0) { |
| 1997 | char ch = namebuf[len-1]; |
| 1998 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 1999 | namebuf[len++] = '/'; |
| 2000 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2001 | strcpy(namebuf + len, "*.*"); |
| 2002 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2003 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2004 | return NULL; |
| 2005 | |
| 2006 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 2007 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 2008 | int error = GetLastError(); |
| 2009 | if (error == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2010 | return d; |
| 2011 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2012 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2013 | } |
| 2014 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2015 | /* Skip over . and .. */ |
| 2016 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2017 | strcmp(FileData.cFileName, "..") != 0) { |
| 2018 | v = PyString_FromString(FileData.cFileName); |
| 2019 | if (v == NULL) { |
| 2020 | Py_DECREF(d); |
| 2021 | d = NULL; |
| 2022 | break; |
| 2023 | } |
| 2024 | if (PyList_Append(d, v) != 0) { |
| 2025 | Py_DECREF(v); |
| 2026 | Py_DECREF(d); |
| 2027 | d = NULL; |
| 2028 | break; |
| 2029 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2030 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2031 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2032 | Py_BEGIN_ALLOW_THREADS |
| 2033 | result = FindNextFile(hFindFile, &FileData); |
| 2034 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 982e9fe | 2006-07-24 12:54:17 +0000 | [diff] [blame] | 2035 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2036 | it got to the end of the directory. */ |
| 2037 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2038 | Py_DECREF(d); |
| 2039 | win32_error("FindNextFile", namebuf); |
| 2040 | FindClose(hFindFile); |
| 2041 | return NULL; |
| 2042 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2043 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2044 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2045 | if (FindClose(hFindFile) == FALSE) { |
| 2046 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2047 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2048 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2049 | |
| 2050 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2051 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2052 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2053 | |
| 2054 | #ifndef MAX_PATH |
| 2055 | #define MAX_PATH CCHMAXPATH |
| 2056 | #endif |
| 2057 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2058 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2059 | PyObject *d, *v; |
| 2060 | char namebuf[MAX_PATH+5]; |
| 2061 | HDIR hdir = 1; |
| 2062 | ULONG srchcnt = 1; |
| 2063 | FILEFINDBUF3 ep; |
| 2064 | APIRET rc; |
| 2065 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2066 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2067 | return NULL; |
| 2068 | if (len >= MAX_PATH) { |
| 2069 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2070 | return NULL; |
| 2071 | } |
| 2072 | strcpy(namebuf, name); |
| 2073 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2074 | if (*pt == ALTSEP) |
| 2075 | *pt = SEP; |
| 2076 | if (namebuf[len-1] != SEP) |
| 2077 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2078 | strcpy(namebuf + len, "*.*"); |
| 2079 | |
| 2080 | if ((d = PyList_New(0)) == NULL) |
| 2081 | return NULL; |
| 2082 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2083 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2084 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2085 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2086 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2087 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2088 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2089 | |
| 2090 | if (rc != NO_ERROR) { |
| 2091 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 2092 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2095 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2096 | do { |
| 2097 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2098 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2099 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2100 | |
| 2101 | strcpy(namebuf, ep.achName); |
| 2102 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2103 | /* Leave Case of Name Alone -- In Native Form */ |
| 2104 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2105 | |
| 2106 | v = PyString_FromString(namebuf); |
| 2107 | if (v == NULL) { |
| 2108 | Py_DECREF(d); |
| 2109 | d = NULL; |
| 2110 | break; |
| 2111 | } |
| 2112 | if (PyList_Append(d, v) != 0) { |
| 2113 | Py_DECREF(v); |
| 2114 | Py_DECREF(d); |
| 2115 | d = NULL; |
| 2116 | break; |
| 2117 | } |
| 2118 | Py_DECREF(v); |
| 2119 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2120 | } |
| 2121 | |
| 2122 | return d; |
| 2123 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2124 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2125 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2126 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2127 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2128 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2129 | int arg_is_unicode = 1; |
| 2130 | |
Georg Brandl | 05e89b8 | 2006-04-11 07:04:06 +0000 | [diff] [blame] | 2131 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2132 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2133 | arg_is_unicode = 0; |
| 2134 | PyErr_Clear(); |
| 2135 | } |
| 2136 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2137 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2138 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2139 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2140 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2141 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2142 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2143 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2144 | return NULL; |
| 2145 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2146 | for (;;) { |
| 2147 | Py_BEGIN_ALLOW_THREADS |
| 2148 | ep = readdir(dirp); |
| 2149 | Py_END_ALLOW_THREADS |
| 2150 | if (ep == NULL) |
| 2151 | break; |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2152 | if (ep->d_name[0] == '.' && |
| 2153 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2154 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2155 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2156 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2157 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2158 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2159 | d = NULL; |
| 2160 | break; |
| 2161 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2162 | #ifdef Py_USING_UNICODE |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2163 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2164 | PyObject *w; |
| 2165 | |
| 2166 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2167 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2168 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2169 | if (w != NULL) { |
| 2170 | Py_DECREF(v); |
| 2171 | v = w; |
| 2172 | } |
| 2173 | else { |
| 2174 | /* fall back to the original byte string, as |
| 2175 | discussed in patch #683592 */ |
| 2176 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2177 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2178 | } |
| 2179 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2180 | if (PyList_Append(d, v) != 0) { |
| 2181 | Py_DECREF(v); |
| 2182 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2183 | d = NULL; |
| 2184 | break; |
| 2185 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2186 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2187 | } |
Georg Brandl | bbfe4fa | 2006-04-11 06:47:43 +0000 | [diff] [blame] | 2188 | if (errno != 0 && d != NULL) { |
| 2189 | /* readdir() returned NULL and set errno */ |
| 2190 | closedir(dirp); |
| 2191 | Py_DECREF(d); |
| 2192 | return posix_error_with_allocated_filename(name); |
| 2193 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2194 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2195 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2196 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2197 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2198 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2199 | #endif /* which OS */ |
| 2200 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2201 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2202 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2203 | /* A helper function for abspath on win32 */ |
| 2204 | static PyObject * |
| 2205 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2206 | { |
| 2207 | /* assume encoded strings wont more than double no of chars */ |
| 2208 | char inbuf[MAX_PATH*2]; |
| 2209 | char *inbufp = inbuf; |
Tim Peters | 67d70eb | 2006-03-01 04:35:45 +0000 | [diff] [blame] | 2210 | Py_ssize_t insize = sizeof(inbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2211 | char outbuf[MAX_PATH*2]; |
| 2212 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2213 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2214 | if (unicode_file_names()) { |
| 2215 | PyUnicodeObject *po; |
| 2216 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2217 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 2218 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2219 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2220 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2221 | woutbuf, &wtemp)) |
| 2222 | return win32_error("GetFullPathName", ""); |
| 2223 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 2224 | } |
| 2225 | /* Drop the argument parsing error as narrow strings |
| 2226 | are also valid. */ |
| 2227 | PyErr_Clear(); |
| 2228 | } |
| 2229 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2230 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 2231 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2232 | &insize)) |
| 2233 | return NULL; |
| 2234 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2235 | outbuf, &temp)) |
| 2236 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2237 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2238 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2239 | Py_FileSystemDefaultEncoding, NULL); |
| 2240 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2241 | return PyString_FromString(outbuf); |
| 2242 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2243 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2244 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2245 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2246 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2247 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2248 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2249 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2250 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2251 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2252 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2253 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2254 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2255 | |
| 2256 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2257 | if (unicode_file_names()) { |
| 2258 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 2259 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2260 | Py_BEGIN_ALLOW_THREADS |
| 2261 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2262 | it is a simple dereference. */ |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2263 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2264 | Py_END_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2265 | if (!res) |
| 2266 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2267 | Py_INCREF(Py_None); |
| 2268 | return Py_None; |
| 2269 | } |
| 2270 | /* Drop the argument parsing error as narrow strings |
| 2271 | are also valid. */ |
| 2272 | PyErr_Clear(); |
| 2273 | } |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2274 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 2275 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 2276 | return NULL; |
| 2277 | Py_BEGIN_ALLOW_THREADS |
| 2278 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2279 | it is a simple dereference. */ |
| 2280 | res = CreateDirectoryA(path, NULL); |
| 2281 | Py_END_ALLOW_THREADS |
| 2282 | if (!res) { |
| 2283 | win32_error("mkdir", path); |
| 2284 | PyMem_Free(path); |
| 2285 | return NULL; |
| 2286 | } |
| 2287 | PyMem_Free(path); |
| 2288 | Py_INCREF(Py_None); |
| 2289 | return Py_None; |
| 2290 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2291 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2292 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2293 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2294 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2295 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2296 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2297 | res = mkdir(path); |
| 2298 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2299 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2300 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2301 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2302 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2303 | return posix_error_with_allocated_filename(path); |
| 2304 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2305 | Py_INCREF(Py_None); |
| 2306 | return Py_None; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2307 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2310 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2311 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2312 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2313 | #include <sys/resource.h> |
| 2314 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2315 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2316 | |
| 2317 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2318 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2319 | "nice(inc) -> new_priority\n\n\ |
| 2320 | 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] | 2321 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2322 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2323 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2324 | { |
| 2325 | int increment, value; |
| 2326 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2327 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2328 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2329 | |
| 2330 | /* There are two flavours of 'nice': one that returns the new |
| 2331 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2332 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2333 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2334 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2335 | If we are of the nice family that returns the new priority, we |
| 2336 | need to clear errno before the call, and check if errno is filled |
| 2337 | before calling posix_error() on a returnvalue of -1, because the |
| 2338 | -1 may be the actual new priority! */ |
| 2339 | |
| 2340 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2341 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2342 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2343 | if (value == 0) |
| 2344 | value = getpriority(PRIO_PROCESS, 0); |
| 2345 | #endif |
| 2346 | if (value == -1 && errno != 0) |
| 2347 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2348 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2349 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2350 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2351 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2352 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2353 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2354 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2355 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2356 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2357 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2358 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2359 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2360 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2361 | PyObject *o1, *o2; |
| 2362 | char *p1, *p2; |
| 2363 | BOOL result; |
| 2364 | if (unicode_file_names()) { |
| 2365 | if (!PyArg_ParseTuple(args, "O&O&:rename", |
| 2366 | convert_to_unicode, &o1, |
| 2367 | convert_to_unicode, &o2)) |
| 2368 | PyErr_Clear(); |
| 2369 | else { |
| 2370 | Py_BEGIN_ALLOW_THREADS |
| 2371 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2372 | PyUnicode_AsUnicode(o2)); |
| 2373 | Py_END_ALLOW_THREADS |
| 2374 | Py_DECREF(o1); |
| 2375 | Py_DECREF(o2); |
| 2376 | if (!result) |
| 2377 | return win32_error("rename", NULL); |
| 2378 | Py_INCREF(Py_None); |
| 2379 | return Py_None; |
| 2380 | } |
| 2381 | } |
| 2382 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2383 | return NULL; |
| 2384 | Py_BEGIN_ALLOW_THREADS |
| 2385 | result = MoveFileA(p1, p2); |
| 2386 | Py_END_ALLOW_THREADS |
| 2387 | if (!result) |
| 2388 | return win32_error("rename", NULL); |
| 2389 | Py_INCREF(Py_None); |
| 2390 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2391 | #else |
Martin v. Löwis | 4fc2bda | 2006-05-04 12:04:27 +0000 | [diff] [blame] | 2392 | return posix_2str(args, "etet:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2393 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2396 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2397 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2398 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2399 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2400 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2401 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2402 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2403 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2404 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2405 | return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2406 | #else |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2407 | return posix_1str(args, "et:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2408 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2411 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2412 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2413 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2414 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2415 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2416 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2417 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2418 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2419 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2420 | 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] | 2421 | #else |
| 2422 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 2423 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2426 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2427 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2428 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2429 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2430 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2431 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2432 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2433 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2434 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2435 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2436 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2437 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2438 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2439 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2440 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2441 | Py_END_ALLOW_THREADS |
| 2442 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2443 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2444 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2445 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2446 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2447 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2448 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2449 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2450 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2451 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2452 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2453 | { |
| 2454 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2455 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2456 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2457 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2458 | if (i < 0) |
| 2459 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2460 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2463 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2464 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2465 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2466 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2467 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2468 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2469 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2470 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2471 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2472 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2473 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2474 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2475 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2476 | return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2477 | #else |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2478 | return posix_1str(args, "et:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2479 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2482 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2483 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2484 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2485 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2486 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2487 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2488 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2489 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2490 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2491 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2492 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2493 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2494 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2495 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2496 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2497 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2498 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2499 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2500 | u.sysname, |
| 2501 | u.nodename, |
| 2502 | u.release, |
| 2503 | u.version, |
| 2504 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2505 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2506 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2507 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2508 | static int |
| 2509 | extract_time(PyObject *t, long* sec, long* usec) |
| 2510 | { |
| 2511 | long intval; |
| 2512 | if (PyFloat_Check(t)) { |
| 2513 | double tval = PyFloat_AsDouble(t); |
| 2514 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 2515 | if (!intobj) |
| 2516 | return -1; |
| 2517 | intval = PyInt_AsLong(intobj); |
| 2518 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2519 | if (intval == -1 && PyErr_Occurred()) |
| 2520 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2521 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2522 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2523 | if (*usec < 0) |
| 2524 | /* If rounding gave us a negative number, |
| 2525 | truncate. */ |
| 2526 | *usec = 0; |
| 2527 | return 0; |
| 2528 | } |
| 2529 | intval = PyInt_AsLong(t); |
| 2530 | if (intval == -1 && PyErr_Occurred()) |
| 2531 | return -1; |
| 2532 | *sec = intval; |
| 2533 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2534 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2535 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2536 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2537 | PyDoc_STRVAR(posix_utime__doc__, |
Georg Brandl | 9e5b5e4 | 2006-05-17 14:18:20 +0000 | [diff] [blame] | 2538 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2539 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2540 | 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] | 2541 | 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] | 2542 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2543 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2544 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2545 | { |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2546 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2547 | PyObject *arg; |
| 2548 | PyUnicodeObject *obwpath; |
| 2549 | wchar_t *wpath = NULL; |
| 2550 | char *apath = NULL; |
| 2551 | HANDLE hFile; |
| 2552 | long atimesec, mtimesec, ausec, musec; |
| 2553 | FILETIME atime, mtime; |
| 2554 | PyObject *result = NULL; |
| 2555 | |
| 2556 | if (unicode_file_names()) { |
| 2557 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2558 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2559 | Py_BEGIN_ALLOW_THREADS |
| 2560 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
Martin v. Löwis | 18aaa56 | 2006-10-15 08:43:33 +0000 | [diff] [blame] | 2561 | NULL, OPEN_EXISTING, |
| 2562 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2563 | Py_END_ALLOW_THREADS |
| 2564 | if (hFile == INVALID_HANDLE_VALUE) |
| 2565 | return win32_error_unicode("utime", wpath); |
| 2566 | } else |
| 2567 | /* Drop the argument parsing error as narrow strings |
| 2568 | are also valid. */ |
| 2569 | PyErr_Clear(); |
| 2570 | } |
| 2571 | if (!wpath) { |
| 2572 | if (!PyArg_ParseTuple(args, "etO:utime", |
| 2573 | Py_FileSystemDefaultEncoding, &apath, &arg)) |
| 2574 | return NULL; |
| 2575 | Py_BEGIN_ALLOW_THREADS |
| 2576 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
Martin v. Löwis | 18aaa56 | 2006-10-15 08:43:33 +0000 | [diff] [blame] | 2577 | NULL, OPEN_EXISTING, |
| 2578 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2579 | Py_END_ALLOW_THREADS |
| 2580 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2581 | win32_error("utime", apath); |
| 2582 | PyMem_Free(apath); |
| 2583 | return NULL; |
| 2584 | } |
| 2585 | PyMem_Free(apath); |
| 2586 | } |
| 2587 | |
| 2588 | if (arg == Py_None) { |
| 2589 | SYSTEMTIME now; |
| 2590 | GetSystemTime(&now); |
| 2591 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2592 | !SystemTimeToFileTime(&now, &atime)) { |
| 2593 | win32_error("utime", NULL); |
| 2594 | goto done; |
| 2595 | } |
| 2596 | } |
| 2597 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2598 | PyErr_SetString(PyExc_TypeError, |
| 2599 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2600 | goto done; |
| 2601 | } |
| 2602 | else { |
| 2603 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2604 | &atimesec, &ausec) == -1) |
| 2605 | goto done; |
Martin v. Löwis | f43893a | 2006-10-09 20:44:25 +0000 | [diff] [blame] | 2606 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2607 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2608 | &mtimesec, &musec) == -1) |
| 2609 | goto done; |
Martin v. Löwis | f43893a | 2006-10-09 20:44:25 +0000 | [diff] [blame] | 2610 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2611 | } |
| 2612 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2613 | /* Avoid putting the file name into the error here, |
| 2614 | as that may confuse the user into believing that |
| 2615 | something is wrong with the file, when it also |
| 2616 | could be the time stamp that gives a problem. */ |
| 2617 | win32_error("utime", NULL); |
| 2618 | } |
| 2619 | Py_INCREF(Py_None); |
| 2620 | result = Py_None; |
| 2621 | done: |
| 2622 | CloseHandle(hFile); |
| 2623 | return result; |
| 2624 | #else /* Py_WIN_WIDE_FILENAMES */ |
| 2625 | |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2626 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2627 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2628 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2629 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2630 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2631 | #if defined(HAVE_UTIMES) |
| 2632 | struct timeval buf[2]; |
| 2633 | #define ATIME buf[0].tv_sec |
| 2634 | #define MTIME buf[1].tv_sec |
| 2635 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2636 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2637 | struct utimbuf buf; |
| 2638 | #define ATIME buf.actime |
| 2639 | #define MTIME buf.modtime |
| 2640 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2641 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2642 | time_t buf[2]; |
| 2643 | #define ATIME buf[0] |
| 2644 | #define MTIME buf[1] |
| 2645 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2646 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2647 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2648 | |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2649 | if (!PyArg_ParseTuple(args, "etO:utime", |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2650 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2651 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2652 | if (arg == Py_None) { |
| 2653 | /* optional time values not given */ |
| 2654 | Py_BEGIN_ALLOW_THREADS |
| 2655 | res = utime(path, NULL); |
| 2656 | Py_END_ALLOW_THREADS |
| 2657 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2658 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2659 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2660 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2661 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2662 | return NULL; |
| 2663 | } |
| 2664 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2665 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2666 | &atime, &ausec) == -1) { |
| 2667 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2668 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2669 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2670 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2671 | &mtime, &musec) == -1) { |
| 2672 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2673 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2674 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2675 | ATIME = atime; |
| 2676 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2677 | #ifdef HAVE_UTIMES |
| 2678 | buf[0].tv_usec = ausec; |
| 2679 | buf[1].tv_usec = musec; |
| 2680 | Py_BEGIN_ALLOW_THREADS |
| 2681 | res = utimes(path, buf); |
| 2682 | Py_END_ALLOW_THREADS |
| 2683 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2684 | Py_BEGIN_ALLOW_THREADS |
| 2685 | res = utime(path, UTIME_ARG); |
| 2686 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2687 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2688 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2689 | if (res < 0) { |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2690 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2691 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2692 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2693 | Py_INCREF(Py_None); |
| 2694 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2695 | #undef UTIME_ARG |
| 2696 | #undef ATIME |
| 2697 | #undef MTIME |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2698 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2701 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2702 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2703 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2704 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2705 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2706 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2707 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2708 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2709 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2710 | { |
| 2711 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2712 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2713 | return NULL; |
| 2714 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2715 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2718 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2719 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2720 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2721 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2722 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2723 | for (i = 0; i < count; i++) |
| 2724 | PyMem_Free(array[i]); |
| 2725 | PyMem_DEL(array); |
| 2726 | } |
| 2727 | #endif |
| 2728 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2729 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2730 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2731 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2732 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2733 | Execute an executable path with arguments, replacing current process.\n\ |
| 2734 | \n\ |
| 2735 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2736 | args: tuple or list of strings"); |
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_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2740 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2741 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2742 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2743 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2744 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2745 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2746 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2747 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2748 | argv is a list or tuple of strings. */ |
| 2749 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2750 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2751 | Py_FileSystemDefaultEncoding, |
| 2752 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2753 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2754 | if (PyList_Check(argv)) { |
| 2755 | argc = PyList_Size(argv); |
| 2756 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2757 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2758 | else if (PyTuple_Check(argv)) { |
| 2759 | argc = PyTuple_Size(argv); |
| 2760 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2761 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2762 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2763 | 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] | 2764 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2765 | return NULL; |
| 2766 | } |
| 2767 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2768 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2769 | if (argvlist == NULL) { |
| 2770 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2771 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2772 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2773 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2774 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2775 | Py_FileSystemDefaultEncoding, |
| 2776 | &argvlist[i])) { |
| 2777 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2778 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2779 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2780 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2781 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2782 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2783 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2784 | } |
| 2785 | argvlist[argc] = NULL; |
| 2786 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2787 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2788 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2789 | /* If we get here it's definitely an error */ |
| 2790 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2791 | free_string_array(argvlist, argc); |
| 2792 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2793 | return posix_error(); |
| 2794 | } |
| 2795 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2796 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2797 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2798 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2799 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2800 | \n\ |
| 2801 | path: path of executable file\n\ |
| 2802 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2803 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2804 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2805 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2806 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2807 | { |
| 2808 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2809 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2810 | char **argvlist; |
| 2811 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2812 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2813 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2814 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 2815 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2816 | |
| 2817 | /* execve has three arguments: (path, argv, env), where |
| 2818 | argv is a list or tuple of strings and env is a dictionary |
| 2819 | like posix.environ. */ |
| 2820 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2821 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2822 | Py_FileSystemDefaultEncoding, |
| 2823 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2824 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2825 | if (PyList_Check(argv)) { |
| 2826 | argc = PyList_Size(argv); |
| 2827 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2828 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2829 | else if (PyTuple_Check(argv)) { |
| 2830 | argc = PyTuple_Size(argv); |
| 2831 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2832 | } |
| 2833 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2834 | PyErr_SetString(PyExc_TypeError, |
| 2835 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2836 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2837 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2838 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2839 | PyErr_SetString(PyExc_TypeError, |
| 2840 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2841 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2844 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2845 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2846 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2847 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2848 | } |
| 2849 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2850 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2851 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2852 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2853 | &argvlist[i])) |
| 2854 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2855 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2856 | goto fail_1; |
| 2857 | } |
| 2858 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2859 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2860 | argvlist[argc] = NULL; |
| 2861 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2862 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2863 | if (i < 0) |
| 2864 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2865 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2866 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2867 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2868 | goto fail_1; |
| 2869 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2870 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2871 | keys = PyMapping_Keys(env); |
| 2872 | vals = PyMapping_Values(env); |
| 2873 | if (!keys || !vals) |
| 2874 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2875 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2876 | PyErr_SetString(PyExc_TypeError, |
| 2877 | "execve(): env.keys() or env.values() is not a list"); |
| 2878 | goto fail_2; |
| 2879 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2880 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2881 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2882 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2883 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2884 | |
| 2885 | key = PyList_GetItem(keys, pos); |
| 2886 | val = PyList_GetItem(vals, pos); |
| 2887 | if (!key || !val) |
| 2888 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2889 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2890 | if (!PyArg_Parse( |
| 2891 | key, |
| 2892 | "s;execve() arg 3 contains a non-string key", |
| 2893 | &k) || |
| 2894 | !PyArg_Parse( |
| 2895 | val, |
| 2896 | "s;execve() arg 3 contains a non-string value", |
| 2897 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2898 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2899 | goto fail_2; |
| 2900 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2901 | |
| 2902 | #if defined(PYOS_OS2) |
| 2903 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2904 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2905 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2906 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2907 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2908 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2909 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2910 | goto fail_2; |
| 2911 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2912 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2913 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2914 | #if defined(PYOS_OS2) |
| 2915 | } |
| 2916 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2917 | } |
| 2918 | envlist[envc] = 0; |
| 2919 | |
| 2920 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2921 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2922 | /* If we get here it's definitely an error */ |
| 2923 | |
| 2924 | (void) posix_error(); |
| 2925 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2926 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2927 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2928 | PyMem_DEL(envlist[envc]); |
| 2929 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2930 | fail_1: |
| 2931 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2932 | Py_XDECREF(vals); |
| 2933 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2934 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2935 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2936 | return NULL; |
| 2937 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2938 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2939 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2940 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2941 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2942 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2943 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2944 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2945 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2946 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2947 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2948 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2949 | |
| 2950 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2951 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2952 | { |
| 2953 | char *path; |
| 2954 | PyObject *argv; |
| 2955 | char **argvlist; |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 2956 | int mode, i; |
| 2957 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2958 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2959 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2960 | |
| 2961 | /* spawnv has three arguments: (mode, path, argv), where |
| 2962 | argv is a list or tuple of strings. */ |
| 2963 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2964 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2965 | Py_FileSystemDefaultEncoding, |
| 2966 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2967 | return NULL; |
| 2968 | if (PyList_Check(argv)) { |
| 2969 | argc = PyList_Size(argv); |
| 2970 | getitem = PyList_GetItem; |
| 2971 | } |
| 2972 | else if (PyTuple_Check(argv)) { |
| 2973 | argc = PyTuple_Size(argv); |
| 2974 | getitem = PyTuple_GetItem; |
| 2975 | } |
| 2976 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2977 | PyErr_SetString(PyExc_TypeError, |
| 2978 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2979 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2980 | return NULL; |
| 2981 | } |
| 2982 | |
| 2983 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2984 | if (argvlist == NULL) { |
| 2985 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2986 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2987 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2988 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2989 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2990 | Py_FileSystemDefaultEncoding, |
| 2991 | &argvlist[i])) { |
| 2992 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2993 | PyErr_SetString( |
| 2994 | PyExc_TypeError, |
| 2995 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2996 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2997 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2998 | } |
| 2999 | } |
| 3000 | argvlist[argc] = NULL; |
| 3001 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3002 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3003 | Py_BEGIN_ALLOW_THREADS |
| 3004 | spawnval = spawnv(mode, path, argvlist); |
| 3005 | Py_END_ALLOW_THREADS |
| 3006 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3007 | if (mode == _OLD_P_OVERLAY) |
| 3008 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3009 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3010 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3011 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3012 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3013 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3014 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3015 | free_string_array(argvlist, argc); |
| 3016 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3017 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3018 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3019 | return posix_error(); |
| 3020 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3021 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3022 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3023 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3024 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3025 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
| 3028 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3029 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3030 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3031 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3032 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3033 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3034 | path: path of executable file\n\ |
| 3035 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3036 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3037 | |
| 3038 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3039 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3040 | { |
| 3041 | char *path; |
| 3042 | PyObject *argv, *env; |
| 3043 | char **argvlist; |
| 3044 | char **envlist; |
| 3045 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 3046 | int mode, pos, envc; |
| 3047 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3048 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3049 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 3050 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3051 | |
| 3052 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3053 | argv is a list or tuple of strings and env is a dictionary |
| 3054 | like posix.environ. */ |
| 3055 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3056 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 3057 | Py_FileSystemDefaultEncoding, |
| 3058 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3059 | return NULL; |
| 3060 | if (PyList_Check(argv)) { |
| 3061 | argc = PyList_Size(argv); |
| 3062 | getitem = PyList_GetItem; |
| 3063 | } |
| 3064 | else if (PyTuple_Check(argv)) { |
| 3065 | argc = PyTuple_Size(argv); |
| 3066 | getitem = PyTuple_GetItem; |
| 3067 | } |
| 3068 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3069 | PyErr_SetString(PyExc_TypeError, |
| 3070 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3071 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3072 | } |
| 3073 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3074 | PyErr_SetString(PyExc_TypeError, |
| 3075 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3076 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | argvlist = PyMem_NEW(char *, argc+1); |
| 3080 | if (argvlist == NULL) { |
| 3081 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3082 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3083 | } |
| 3084 | for (i = 0; i < argc; i++) { |
| 3085 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3086 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3087 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3088 | &argvlist[i])) |
| 3089 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3090 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3091 | goto fail_1; |
| 3092 | } |
| 3093 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3094 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3095 | argvlist[argc] = NULL; |
| 3096 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3097 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3098 | if (i < 0) |
| 3099 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3100 | envlist = PyMem_NEW(char *, i + 1); |
| 3101 | if (envlist == NULL) { |
| 3102 | PyErr_NoMemory(); |
| 3103 | goto fail_1; |
| 3104 | } |
| 3105 | envc = 0; |
| 3106 | keys = PyMapping_Keys(env); |
| 3107 | vals = PyMapping_Values(env); |
| 3108 | if (!keys || !vals) |
| 3109 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3110 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3111 | PyErr_SetString(PyExc_TypeError, |
| 3112 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3113 | goto fail_2; |
| 3114 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3115 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3116 | for (pos = 0; pos < i; pos++) { |
| 3117 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3118 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3119 | |
| 3120 | key = PyList_GetItem(keys, pos); |
| 3121 | val = PyList_GetItem(vals, pos); |
| 3122 | if (!key || !val) |
| 3123 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3124 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3125 | if (!PyArg_Parse( |
| 3126 | key, |
| 3127 | "s;spawnve() arg 3 contains a non-string key", |
| 3128 | &k) || |
| 3129 | !PyArg_Parse( |
| 3130 | val, |
| 3131 | "s;spawnve() arg 3 contains a non-string value", |
| 3132 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3133 | { |
| 3134 | goto fail_2; |
| 3135 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3136 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3137 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3138 | if (p == NULL) { |
| 3139 | PyErr_NoMemory(); |
| 3140 | goto fail_2; |
| 3141 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3142 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3143 | envlist[envc++] = p; |
| 3144 | } |
| 3145 | envlist[envc] = 0; |
| 3146 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3147 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3148 | Py_BEGIN_ALLOW_THREADS |
| 3149 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3150 | Py_END_ALLOW_THREADS |
| 3151 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3152 | if (mode == _OLD_P_OVERLAY) |
| 3153 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3154 | |
| 3155 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3156 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3157 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3158 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3159 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3160 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3161 | (void) posix_error(); |
| 3162 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3163 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3164 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3165 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3166 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3167 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3168 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3169 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3170 | while (--envc >= 0) |
| 3171 | PyMem_DEL(envlist[envc]); |
| 3172 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3173 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3174 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3175 | Py_XDECREF(vals); |
| 3176 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3177 | fail_0: |
| 3178 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3179 | return res; |
| 3180 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3181 | |
| 3182 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3183 | #if defined(PYOS_OS2) |
| 3184 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3185 | "spawnvp(mode, file, args)\n\n\ |
| 3186 | Execute the program 'file' in a new process, using the environment\n\ |
| 3187 | search path to find the file.\n\ |
| 3188 | \n\ |
| 3189 | mode: mode of process creation\n\ |
| 3190 | file: executable file name\n\ |
| 3191 | args: tuple or list of strings"); |
| 3192 | |
| 3193 | static PyObject * |
| 3194 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3195 | { |
| 3196 | char *path; |
| 3197 | PyObject *argv; |
| 3198 | char **argvlist; |
| 3199 | int mode, i, argc; |
| 3200 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3201 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3202 | |
| 3203 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3204 | argv is a list or tuple of strings. */ |
| 3205 | |
| 3206 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 3207 | Py_FileSystemDefaultEncoding, |
| 3208 | &path, &argv)) |
| 3209 | return NULL; |
| 3210 | if (PyList_Check(argv)) { |
| 3211 | argc = PyList_Size(argv); |
| 3212 | getitem = PyList_GetItem; |
| 3213 | } |
| 3214 | else if (PyTuple_Check(argv)) { |
| 3215 | argc = PyTuple_Size(argv); |
| 3216 | getitem = PyTuple_GetItem; |
| 3217 | } |
| 3218 | else { |
| 3219 | PyErr_SetString(PyExc_TypeError, |
| 3220 | "spawnvp() arg 2 must be a tuple or list"); |
| 3221 | PyMem_Free(path); |
| 3222 | return NULL; |
| 3223 | } |
| 3224 | |
| 3225 | argvlist = PyMem_NEW(char *, argc+1); |
| 3226 | if (argvlist == NULL) { |
| 3227 | PyMem_Free(path); |
| 3228 | return PyErr_NoMemory(); |
| 3229 | } |
| 3230 | for (i = 0; i < argc; i++) { |
| 3231 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3232 | Py_FileSystemDefaultEncoding, |
| 3233 | &argvlist[i])) { |
| 3234 | free_string_array(argvlist, i); |
| 3235 | PyErr_SetString( |
| 3236 | PyExc_TypeError, |
| 3237 | "spawnvp() arg 2 must contain only strings"); |
| 3238 | PyMem_Free(path); |
| 3239 | return NULL; |
| 3240 | } |
| 3241 | } |
| 3242 | argvlist[argc] = NULL; |
| 3243 | |
| 3244 | Py_BEGIN_ALLOW_THREADS |
| 3245 | #if defined(PYCC_GCC) |
| 3246 | spawnval = spawnvp(mode, path, argvlist); |
| 3247 | #else |
| 3248 | spawnval = _spawnvp(mode, path, argvlist); |
| 3249 | #endif |
| 3250 | Py_END_ALLOW_THREADS |
| 3251 | |
| 3252 | free_string_array(argvlist, argc); |
| 3253 | PyMem_Free(path); |
| 3254 | |
| 3255 | if (spawnval == -1) |
| 3256 | return posix_error(); |
| 3257 | else |
| 3258 | return Py_BuildValue("l", (long) spawnval); |
| 3259 | } |
| 3260 | |
| 3261 | |
| 3262 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3263 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3264 | Execute the program 'file' in a new process, using the environment\n\ |
| 3265 | search path to find the file.\n\ |
| 3266 | \n\ |
| 3267 | mode: mode of process creation\n\ |
| 3268 | file: executable file name\n\ |
| 3269 | args: tuple or list of arguments\n\ |
| 3270 | env: dictionary of strings mapping to strings"); |
| 3271 | |
| 3272 | static PyObject * |
| 3273 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3274 | { |
| 3275 | char *path; |
| 3276 | PyObject *argv, *env; |
| 3277 | char **argvlist; |
| 3278 | char **envlist; |
| 3279 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3280 | int mode, i, pos, argc, envc; |
| 3281 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3282 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3283 | int lastarg = 0; |
| 3284 | |
| 3285 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3286 | argv is a list or tuple of strings and env is a dictionary |
| 3287 | like posix.environ. */ |
| 3288 | |
| 3289 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 3290 | Py_FileSystemDefaultEncoding, |
| 3291 | &path, &argv, &env)) |
| 3292 | return NULL; |
| 3293 | if (PyList_Check(argv)) { |
| 3294 | argc = PyList_Size(argv); |
| 3295 | getitem = PyList_GetItem; |
| 3296 | } |
| 3297 | else if (PyTuple_Check(argv)) { |
| 3298 | argc = PyTuple_Size(argv); |
| 3299 | getitem = PyTuple_GetItem; |
| 3300 | } |
| 3301 | else { |
| 3302 | PyErr_SetString(PyExc_TypeError, |
| 3303 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3304 | goto fail_0; |
| 3305 | } |
| 3306 | if (!PyMapping_Check(env)) { |
| 3307 | PyErr_SetString(PyExc_TypeError, |
| 3308 | "spawnvpe() arg 3 must be a mapping object"); |
| 3309 | goto fail_0; |
| 3310 | } |
| 3311 | |
| 3312 | argvlist = PyMem_NEW(char *, argc+1); |
| 3313 | if (argvlist == NULL) { |
| 3314 | PyErr_NoMemory(); |
| 3315 | goto fail_0; |
| 3316 | } |
| 3317 | for (i = 0; i < argc; i++) { |
| 3318 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3319 | "et;spawnvpe() arg 2 must contain only strings", |
| 3320 | Py_FileSystemDefaultEncoding, |
| 3321 | &argvlist[i])) |
| 3322 | { |
| 3323 | lastarg = i; |
| 3324 | goto fail_1; |
| 3325 | } |
| 3326 | } |
| 3327 | lastarg = argc; |
| 3328 | argvlist[argc] = NULL; |
| 3329 | |
| 3330 | i = PyMapping_Size(env); |
| 3331 | if (i < 0) |
| 3332 | goto fail_1; |
| 3333 | envlist = PyMem_NEW(char *, i + 1); |
| 3334 | if (envlist == NULL) { |
| 3335 | PyErr_NoMemory(); |
| 3336 | goto fail_1; |
| 3337 | } |
| 3338 | envc = 0; |
| 3339 | keys = PyMapping_Keys(env); |
| 3340 | vals = PyMapping_Values(env); |
| 3341 | if (!keys || !vals) |
| 3342 | goto fail_2; |
| 3343 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3344 | PyErr_SetString(PyExc_TypeError, |
| 3345 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3346 | goto fail_2; |
| 3347 | } |
| 3348 | |
| 3349 | for (pos = 0; pos < i; pos++) { |
| 3350 | char *p, *k, *v; |
| 3351 | size_t len; |
| 3352 | |
| 3353 | key = PyList_GetItem(keys, pos); |
| 3354 | val = PyList_GetItem(vals, pos); |
| 3355 | if (!key || !val) |
| 3356 | goto fail_2; |
| 3357 | |
| 3358 | if (!PyArg_Parse( |
| 3359 | key, |
| 3360 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3361 | &k) || |
| 3362 | !PyArg_Parse( |
| 3363 | val, |
| 3364 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3365 | &v)) |
| 3366 | { |
| 3367 | goto fail_2; |
| 3368 | } |
| 3369 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3370 | p = PyMem_NEW(char, len); |
| 3371 | if (p == NULL) { |
| 3372 | PyErr_NoMemory(); |
| 3373 | goto fail_2; |
| 3374 | } |
| 3375 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3376 | envlist[envc++] = p; |
| 3377 | } |
| 3378 | envlist[envc] = 0; |
| 3379 | |
| 3380 | Py_BEGIN_ALLOW_THREADS |
| 3381 | #if defined(PYCC_GCC) |
| 3382 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3383 | #else |
| 3384 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3385 | #endif |
| 3386 | Py_END_ALLOW_THREADS |
| 3387 | |
| 3388 | if (spawnval == -1) |
| 3389 | (void) posix_error(); |
| 3390 | else |
| 3391 | res = Py_BuildValue("l", (long) spawnval); |
| 3392 | |
| 3393 | fail_2: |
| 3394 | while (--envc >= 0) |
| 3395 | PyMem_DEL(envlist[envc]); |
| 3396 | PyMem_DEL(envlist); |
| 3397 | fail_1: |
| 3398 | free_string_array(argvlist, lastarg); |
| 3399 | Py_XDECREF(vals); |
| 3400 | Py_XDECREF(keys); |
| 3401 | fail_0: |
| 3402 | PyMem_Free(path); |
| 3403 | return res; |
| 3404 | } |
| 3405 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3406 | #endif /* HAVE_SPAWNV */ |
| 3407 | |
| 3408 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3409 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3410 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3411 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3412 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3413 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3414 | 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] | 3415 | |
| 3416 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3417 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3418 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3419 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3420 | if (pid == -1) |
| 3421 | return posix_error(); |
| 3422 | PyOS_AfterFork(); |
| 3423 | return PyInt_FromLong((long)pid); |
| 3424 | } |
| 3425 | #endif |
| 3426 | |
| 3427 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3428 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3429 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3430 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3431 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3432 | 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] | 3433 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3434 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3435 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3436 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3437 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3438 | if (pid == -1) |
| 3439 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3440 | if (pid == 0) |
| 3441 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3442 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3443 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3444 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3445 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3446 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3447 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3448 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3449 | #define DEV_PTY_FILE "/dev/ptc" |
| 3450 | #define HAVE_DEV_PTMX |
| 3451 | #else |
| 3452 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3453 | #endif |
| 3454 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3455 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3456 | #ifdef HAVE_PTY_H |
| 3457 | #include <pty.h> |
| 3458 | #else |
| 3459 | #ifdef HAVE_LIBUTIL_H |
| 3460 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3461 | #endif /* HAVE_LIBUTIL_H */ |
| 3462 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3463 | #ifdef HAVE_STROPTS_H |
| 3464 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3465 | #endif |
| 3466 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3467 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3468 | #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] | 3469 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3470 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3471 | 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] | 3472 | |
| 3473 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3474 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3475 | { |
| 3476 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3477 | #ifndef HAVE_OPENPTY |
| 3478 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3479 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3480 | #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] | 3481 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3482 | #ifdef sun |
Neal Norwitz | 84a98e0 | 2006-04-10 07:44:23 +0000 | [diff] [blame] | 3483 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3484 | #endif |
| 3485 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3486 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3487 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3488 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3489 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3490 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3491 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3492 | if (slave_name == NULL) |
| 3493 | return posix_error(); |
| 3494 | |
| 3495 | slave_fd = open(slave_name, O_RDWR); |
| 3496 | if (slave_fd < 0) |
| 3497 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3498 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3499 | 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] | 3500 | if (master_fd < 0) |
| 3501 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3502 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3503 | /* change permission of slave */ |
| 3504 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3505 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3506 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3507 | } |
| 3508 | /* unlock slave */ |
| 3509 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3510 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3511 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3512 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3513 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3514 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3515 | if (slave_name == NULL) |
| 3516 | return posix_error(); |
| 3517 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3518 | if (slave_fd < 0) |
| 3519 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3520 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3521 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3522 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3523 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3524 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3525 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3526 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3527 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3528 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3529 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3530 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3531 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3532 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3533 | |
| 3534 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3535 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3536 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3537 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3538 | 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] | 3539 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3540 | |
| 3541 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3542 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3543 | { |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 3544 | int master_fd = -1, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3545 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3546 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3547 | if (pid == -1) |
| 3548 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3549 | if (pid == 0) |
| 3550 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3551 | return Py_BuildValue("(ii)", pid, master_fd); |
| 3552 | } |
| 3553 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3554 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3555 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3556 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3557 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3558 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3559 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3560 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3561 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3562 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3563 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3564 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3565 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3566 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3567 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3568 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3569 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3570 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3571 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3572 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3573 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3574 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3575 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3576 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3577 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3578 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3579 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3580 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3581 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3582 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3583 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3584 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3585 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3586 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3587 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3588 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3589 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3590 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3591 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3592 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3593 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3594 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3595 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3596 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3597 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3598 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3599 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3600 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3601 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3602 | } |
| 3603 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3604 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3605 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3606 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3607 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3608 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3609 | |
| 3610 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3611 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3612 | { |
| 3613 | PyObject *result = NULL; |
| 3614 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3615 | #ifdef NGROUPS_MAX |
| 3616 | #define MAX_GROUPS NGROUPS_MAX |
| 3617 | #else |
| 3618 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3619 | #define MAX_GROUPS 64 |
| 3620 | #endif |
| 3621 | gid_t grouplist[MAX_GROUPS]; |
| 3622 | int n; |
| 3623 | |
| 3624 | n = getgroups(MAX_GROUPS, grouplist); |
| 3625 | if (n < 0) |
| 3626 | posix_error(); |
| 3627 | else { |
| 3628 | result = PyList_New(n); |
| 3629 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3630 | int i; |
| 3631 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3632 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3633 | if (o == NULL) { |
| 3634 | Py_DECREF(result); |
| 3635 | result = NULL; |
| 3636 | break; |
| 3637 | } |
| 3638 | PyList_SET_ITEM(result, i, o); |
| 3639 | } |
| 3640 | } |
| 3641 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3642 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3643 | return result; |
| 3644 | } |
| 3645 | #endif |
| 3646 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3647 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3648 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3649 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3650 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3651 | |
| 3652 | static PyObject * |
| 3653 | posix_getpgid(PyObject *self, PyObject *args) |
| 3654 | { |
| 3655 | int pid, pgid; |
| 3656 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3657 | return NULL; |
| 3658 | pgid = getpgid(pid); |
| 3659 | if (pgid < 0) |
| 3660 | return posix_error(); |
| 3661 | return PyInt_FromLong((long)pgid); |
| 3662 | } |
| 3663 | #endif /* HAVE_GETPGID */ |
| 3664 | |
| 3665 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3666 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3667 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3668 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3669 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3670 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3671 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3672 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3673 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3674 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3675 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3676 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3677 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3678 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3679 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3680 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3681 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3682 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3683 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3684 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3685 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3686 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3687 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3688 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3689 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3690 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3691 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3692 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3693 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3694 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3695 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3696 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3697 | Py_INCREF(Py_None); |
| 3698 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3699 | } |
| 3700 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3701 | #endif /* HAVE_SETPGRP */ |
| 3702 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3703 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3704 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3705 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3706 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3707 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3708 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3709 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3710 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3711 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3712 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3713 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3714 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3715 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3716 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3717 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3718 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3719 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3720 | |
| 3721 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3722 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3723 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3724 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3725 | char *name; |
| 3726 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3727 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3728 | errno = 0; |
| 3729 | name = getlogin(); |
| 3730 | if (name == NULL) { |
| 3731 | if (errno) |
| 3732 | posix_error(); |
| 3733 | else |
| 3734 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3735 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3736 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3737 | else |
| 3738 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3739 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3740 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3741 | return result; |
| 3742 | } |
| 3743 | #endif |
| 3744 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3745 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3746 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3747 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3748 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3749 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3750 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3751 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3752 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3753 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3754 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3755 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3756 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3757 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3758 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3759 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3760 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3761 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3762 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3763 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3764 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3765 | { |
| 3766 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3767 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3768 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3769 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3770 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3771 | APIRET rc; |
| 3772 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3773 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3774 | |
| 3775 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3776 | APIRET rc; |
| 3777 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3778 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3779 | |
| 3780 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3781 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3782 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3783 | if (kill(pid, sig) == -1) |
| 3784 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3785 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3786 | Py_INCREF(Py_None); |
| 3787 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3788 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3789 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3790 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3791 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3792 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3793 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3794 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3795 | |
| 3796 | static PyObject * |
| 3797 | posix_killpg(PyObject *self, PyObject *args) |
| 3798 | { |
| 3799 | int pgid, sig; |
| 3800 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3801 | return NULL; |
| 3802 | if (killpg(pgid, sig) == -1) |
| 3803 | return posix_error(); |
| 3804 | Py_INCREF(Py_None); |
| 3805 | return Py_None; |
| 3806 | } |
| 3807 | #endif |
| 3808 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3809 | #ifdef HAVE_PLOCK |
| 3810 | |
| 3811 | #ifdef HAVE_SYS_LOCK_H |
| 3812 | #include <sys/lock.h> |
| 3813 | #endif |
| 3814 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3815 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3816 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3817 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3818 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3819 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3820 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3821 | { |
| 3822 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3823 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3824 | return NULL; |
| 3825 | if (plock(op) == -1) |
| 3826 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3827 | Py_INCREF(Py_None); |
| 3828 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3829 | } |
| 3830 | #endif |
| 3831 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3832 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3833 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3834 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3835 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3836 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3837 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3838 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3839 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3840 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3841 | async_system(const char *command) |
| 3842 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3843 | char errormsg[256], args[1024]; |
| 3844 | RESULTCODES rcodes; |
| 3845 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3846 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3847 | char *shell = getenv("COMSPEC"); |
| 3848 | if (!shell) |
| 3849 | shell = "cmd"; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3850 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3851 | /* avoid overflowing the argument buffer */ |
| 3852 | if (strlen(shell) + 3 + strlen(command) >= 1024) |
| 3853 | return ERROR_NOT_ENOUGH_MEMORY |
| 3854 | |
| 3855 | args[0] = '\0'; |
| 3856 | strcat(args, shell); |
| 3857 | strcat(args, "/c "); |
| 3858 | strcat(args, command); |
| 3859 | |
| 3860 | /* execute asynchronously, inheriting the environment */ |
| 3861 | rc = DosExecPgm(errormsg, |
| 3862 | sizeof(errormsg), |
| 3863 | EXEC_ASYNC, |
| 3864 | args, |
| 3865 | NULL, |
| 3866 | &rcodes, |
| 3867 | shell); |
| 3868 | return rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3869 | } |
| 3870 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3871 | static FILE * |
| 3872 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3873 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3874 | int oldfd, tgtfd; |
| 3875 | HFILE pipeh[2]; |
| 3876 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3877 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3878 | /* mode determines which of stdin or stdout is reconnected to |
| 3879 | * the pipe to the child |
| 3880 | */ |
| 3881 | if (strchr(mode, 'r') != NULL) { |
| 3882 | tgt_fd = 1; /* stdout */ |
| 3883 | } else if (strchr(mode, 'w')) { |
| 3884 | tgt_fd = 0; /* stdin */ |
| 3885 | } else { |
| 3886 | *err = ERROR_INVALID_ACCESS; |
| 3887 | return NULL; |
| 3888 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3889 | |
Andrew MacIntyre | a3be258 | 2004-12-18 09:51:05 +0000 | [diff] [blame] | 3890 | /* setup the pipe */ |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3891 | if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) { |
| 3892 | *err = rc; |
| 3893 | return NULL; |
| 3894 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3895 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3896 | /* prevent other threads accessing stdio */ |
| 3897 | DosEnterCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3898 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3899 | /* reconnect stdio and execute child */ |
| 3900 | oldfd = dup(tgtfd); |
| 3901 | close(tgtfd); |
| 3902 | if (dup2(pipeh[tgtfd], tgtfd) == 0) { |
| 3903 | DosClose(pipeh[tgtfd]); |
| 3904 | rc = async_system(command); |
| 3905 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3906 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3907 | /* restore stdio */ |
| 3908 | dup2(oldfd, tgtfd); |
| 3909 | close(oldfd); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3910 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3911 | /* allow other threads access to stdio */ |
| 3912 | DosExitCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3913 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3914 | /* if execution of child was successful return file stream */ |
| 3915 | if (rc == NO_ERROR) |
| 3916 | return fdopen(pipeh[1 - tgtfd], mode); |
| 3917 | else { |
| 3918 | DosClose(pipeh[1 - tgtfd]); |
| 3919 | *err = rc; |
| 3920 | return NULL; |
| 3921 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3922 | } |
| 3923 | |
| 3924 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3925 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3926 | { |
| 3927 | char *name; |
| 3928 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3929 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3930 | FILE *fp; |
| 3931 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3932 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3933 | return NULL; |
| 3934 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3935 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3936 | Py_END_ALLOW_THREADS |
| 3937 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3938 | return os2_error(err); |
| 3939 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3940 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 3941 | if (f != NULL) |
| 3942 | PyFile_SetBufSize(f, bufsize); |
| 3943 | return f; |
| 3944 | } |
| 3945 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3946 | #elif defined(PYCC_GCC) |
| 3947 | |
| 3948 | /* standard posix version of popen() support */ |
| 3949 | static PyObject * |
| 3950 | posix_popen(PyObject *self, PyObject *args) |
| 3951 | { |
| 3952 | char *name; |
| 3953 | char *mode = "r"; |
| 3954 | int bufsize = -1; |
| 3955 | FILE *fp; |
| 3956 | PyObject *f; |
| 3957 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 3958 | return NULL; |
| 3959 | Py_BEGIN_ALLOW_THREADS |
| 3960 | fp = popen(name, mode); |
| 3961 | Py_END_ALLOW_THREADS |
| 3962 | if (fp == NULL) |
| 3963 | return posix_error(); |
| 3964 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 3965 | if (f != NULL) |
| 3966 | PyFile_SetBufSize(f, bufsize); |
| 3967 | return f; |
| 3968 | } |
| 3969 | |
| 3970 | /* fork() under OS/2 has lots'o'warts |
| 3971 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 3972 | * most of this code is a ripoff of the win32 code, but using the |
| 3973 | * capabilities of EMX's C library routines |
| 3974 | */ |
| 3975 | |
| 3976 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 3977 | #define POPEN_1 1 |
| 3978 | #define POPEN_2 2 |
| 3979 | #define POPEN_3 3 |
| 3980 | #define POPEN_4 4 |
| 3981 | |
| 3982 | static PyObject *_PyPopen(char *, int, int, int); |
| 3983 | static int _PyPclose(FILE *file); |
| 3984 | |
| 3985 | /* |
| 3986 | * Internal dictionary mapping popen* file pointers to process handles, |
| 3987 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3988 | * for more information on this dictionary's use. |
| 3989 | */ |
| 3990 | static PyObject *_PyPopenProcs = NULL; |
| 3991 | |
| 3992 | /* os2emx version of popen2() |
| 3993 | * |
| 3994 | * The result of this function is a pipe (file) connected to the |
| 3995 | * process's stdin, and a pipe connected to the process's stdout. |
| 3996 | */ |
| 3997 | |
| 3998 | static PyObject * |
| 3999 | os2emx_popen2(PyObject *self, PyObject *args) |
| 4000 | { |
| 4001 | PyObject *f; |
| 4002 | int tm=0; |
| 4003 | |
| 4004 | char *cmdstring; |
| 4005 | char *mode = "t"; |
| 4006 | int bufsize = -1; |
| 4007 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 4008 | return NULL; |
| 4009 | |
| 4010 | if (*mode == 't') |
| 4011 | tm = O_TEXT; |
| 4012 | else if (*mode != 'b') { |
| 4013 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 4014 | return NULL; |
| 4015 | } else |
| 4016 | tm = O_BINARY; |
| 4017 | |
| 4018 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 4019 | |
| 4020 | return f; |
| 4021 | } |
| 4022 | |
| 4023 | /* |
| 4024 | * Variation on os2emx.popen2 |
| 4025 | * |
| 4026 | * The result of this function is 3 pipes - the process's stdin, |
| 4027 | * stdout and stderr |
| 4028 | */ |
| 4029 | |
| 4030 | static PyObject * |
| 4031 | os2emx_popen3(PyObject *self, PyObject *args) |
| 4032 | { |
| 4033 | PyObject *f; |
| 4034 | int tm = 0; |
| 4035 | |
| 4036 | char *cmdstring; |
| 4037 | char *mode = "t"; |
| 4038 | int bufsize = -1; |
| 4039 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 4040 | return NULL; |
| 4041 | |
| 4042 | if (*mode == 't') |
| 4043 | tm = O_TEXT; |
| 4044 | else if (*mode != 'b') { |
| 4045 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 4046 | return NULL; |
| 4047 | } else |
| 4048 | tm = O_BINARY; |
| 4049 | |
| 4050 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 4051 | |
| 4052 | return f; |
| 4053 | } |
| 4054 | |
| 4055 | /* |
| 4056 | * Variation on os2emx.popen2 |
| 4057 | * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4058 | * The result of this function is 2 pipes - the processes stdin, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4059 | * and stdout+stderr combined as a single pipe. |
| 4060 | */ |
| 4061 | |
| 4062 | static PyObject * |
| 4063 | os2emx_popen4(PyObject *self, PyObject *args) |
| 4064 | { |
| 4065 | PyObject *f; |
| 4066 | int tm = 0; |
| 4067 | |
| 4068 | char *cmdstring; |
| 4069 | char *mode = "t"; |
| 4070 | int bufsize = -1; |
| 4071 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 4072 | return NULL; |
| 4073 | |
| 4074 | if (*mode == 't') |
| 4075 | tm = O_TEXT; |
| 4076 | else if (*mode != 'b') { |
| 4077 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 4078 | return NULL; |
| 4079 | } else |
| 4080 | tm = O_BINARY; |
| 4081 | |
| 4082 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 4083 | |
| 4084 | return f; |
| 4085 | } |
| 4086 | |
| 4087 | /* a couple of structures for convenient handling of multiple |
| 4088 | * file handles and pipes |
| 4089 | */ |
| 4090 | struct file_ref |
| 4091 | { |
| 4092 | int handle; |
| 4093 | int flags; |
| 4094 | }; |
| 4095 | |
| 4096 | struct pipe_ref |
| 4097 | { |
| 4098 | int rd; |
| 4099 | int wr; |
| 4100 | }; |
| 4101 | |
| 4102 | /* The following code is derived from the win32 code */ |
| 4103 | |
| 4104 | static PyObject * |
| 4105 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 4106 | { |
| 4107 | struct file_ref stdio[3]; |
| 4108 | struct pipe_ref p_fd[3]; |
| 4109 | FILE *p_s[3]; |
| 4110 | int file_count, i, pipe_err, pipe_pid; |
| 4111 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 4112 | PyObject *f, *p_f[3]; |
| 4113 | |
| 4114 | /* file modes for subsequent fdopen's on pipe handles */ |
| 4115 | if (mode == O_TEXT) |
| 4116 | { |
| 4117 | rd_mode = "rt"; |
| 4118 | wr_mode = "wt"; |
| 4119 | } |
| 4120 | else |
| 4121 | { |
| 4122 | rd_mode = "rb"; |
| 4123 | wr_mode = "wb"; |
| 4124 | } |
| 4125 | |
| 4126 | /* prepare shell references */ |
| 4127 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 4128 | if ((shell = getenv("COMSPEC")) == NULL) |
| 4129 | { |
| 4130 | errno = ENOENT; |
| 4131 | return posix_error(); |
| 4132 | } |
| 4133 | |
| 4134 | sh_name = _getname(shell); |
| 4135 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 4136 | opt = "/c"; |
| 4137 | else |
| 4138 | opt = "-c"; |
| 4139 | |
| 4140 | /* save current stdio fds + their flags, and set not inheritable */ |
| 4141 | i = pipe_err = 0; |
| 4142 | while (pipe_err >= 0 && i < 3) |
| 4143 | { |
| 4144 | pipe_err = stdio[i].handle = dup(i); |
| 4145 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 4146 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 4147 | i++; |
| 4148 | } |
| 4149 | if (pipe_err < 0) |
| 4150 | { |
| 4151 | /* didn't get them all saved - clean up and bail out */ |
| 4152 | int saved_err = errno; |
| 4153 | while (i-- > 0) |
| 4154 | { |
| 4155 | close(stdio[i].handle); |
| 4156 | } |
| 4157 | errno = saved_err; |
| 4158 | return posix_error(); |
| 4159 | } |
| 4160 | |
| 4161 | /* create pipe ends */ |
| 4162 | file_count = 2; |
| 4163 | if (n == POPEN_3) |
| 4164 | file_count = 3; |
| 4165 | i = pipe_err = 0; |
| 4166 | while ((pipe_err == 0) && (i < file_count)) |
| 4167 | pipe_err = pipe((int *)&p_fd[i++]); |
| 4168 | if (pipe_err < 0) |
| 4169 | { |
| 4170 | /* didn't get them all made - clean up and bail out */ |
| 4171 | while (i-- > 0) |
| 4172 | { |
| 4173 | close(p_fd[i].wr); |
| 4174 | close(p_fd[i].rd); |
| 4175 | } |
| 4176 | errno = EPIPE; |
| 4177 | return posix_error(); |
| 4178 | } |
| 4179 | |
| 4180 | /* change the actual standard IO streams over temporarily, |
| 4181 | * making the retained pipe ends non-inheritable |
| 4182 | */ |
| 4183 | pipe_err = 0; |
| 4184 | |
| 4185 | /* - stdin */ |
| 4186 | if (dup2(p_fd[0].rd, 0) == 0) |
| 4187 | { |
| 4188 | close(p_fd[0].rd); |
| 4189 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 4190 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 4191 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 4192 | { |
| 4193 | close(p_fd[0].wr); |
| 4194 | pipe_err = -1; |
| 4195 | } |
| 4196 | } |
| 4197 | else |
| 4198 | { |
| 4199 | pipe_err = -1; |
| 4200 | } |
| 4201 | |
| 4202 | /* - stdout */ |
| 4203 | if (pipe_err == 0) |
| 4204 | { |
| 4205 | if (dup2(p_fd[1].wr, 1) == 1) |
| 4206 | { |
| 4207 | close(p_fd[1].wr); |
| 4208 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 4209 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 4210 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 4211 | { |
| 4212 | close(p_fd[1].rd); |
| 4213 | pipe_err = -1; |
| 4214 | } |
| 4215 | } |
| 4216 | else |
| 4217 | { |
| 4218 | pipe_err = -1; |
| 4219 | } |
| 4220 | } |
| 4221 | |
| 4222 | /* - stderr, as required */ |
| 4223 | if (pipe_err == 0) |
| 4224 | switch (n) |
| 4225 | { |
| 4226 | case POPEN_3: |
| 4227 | { |
| 4228 | if (dup2(p_fd[2].wr, 2) == 2) |
| 4229 | { |
| 4230 | close(p_fd[2].wr); |
| 4231 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 4232 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 4233 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 4234 | { |
| 4235 | close(p_fd[2].rd); |
| 4236 | pipe_err = -1; |
| 4237 | } |
| 4238 | } |
| 4239 | else |
| 4240 | { |
| 4241 | pipe_err = -1; |
| 4242 | } |
| 4243 | break; |
| 4244 | } |
| 4245 | |
| 4246 | case POPEN_4: |
| 4247 | { |
| 4248 | if (dup2(1, 2) != 2) |
| 4249 | { |
| 4250 | pipe_err = -1; |
| 4251 | } |
| 4252 | break; |
| 4253 | } |
| 4254 | } |
| 4255 | |
| 4256 | /* spawn the child process */ |
| 4257 | if (pipe_err == 0) |
| 4258 | { |
| 4259 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 4260 | if (pipe_pid == -1) |
| 4261 | { |
| 4262 | pipe_err = -1; |
| 4263 | } |
| 4264 | else |
| 4265 | { |
| 4266 | /* save the PID into the FILE structure |
| 4267 | * NOTE: this implementation doesn't actually |
| 4268 | * take advantage of this, but do it for |
| 4269 | * completeness - AIM Apr01 |
| 4270 | */ |
| 4271 | for (i = 0; i < file_count; i++) |
| 4272 | p_s[i]->_pid = pipe_pid; |
| 4273 | } |
| 4274 | } |
| 4275 | |
| 4276 | /* reset standard IO to normal */ |
| 4277 | for (i = 0; i < 3; i++) |
| 4278 | { |
| 4279 | dup2(stdio[i].handle, i); |
| 4280 | fcntl(i, F_SETFD, stdio[i].flags); |
| 4281 | close(stdio[i].handle); |
| 4282 | } |
| 4283 | |
| 4284 | /* if any remnant problems, clean up and bail out */ |
| 4285 | if (pipe_err < 0) |
| 4286 | { |
| 4287 | for (i = 0; i < 3; i++) |
| 4288 | { |
| 4289 | close(p_fd[i].rd); |
| 4290 | close(p_fd[i].wr); |
| 4291 | } |
| 4292 | errno = EPIPE; |
| 4293 | return posix_error_with_filename(cmdstring); |
| 4294 | } |
| 4295 | |
| 4296 | /* build tuple of file objects to return */ |
| 4297 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 4298 | PyFile_SetBufSize(p_f[0], bufsize); |
| 4299 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 4300 | PyFile_SetBufSize(p_f[1], bufsize); |
| 4301 | if (n == POPEN_3) |
| 4302 | { |
| 4303 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 4304 | PyFile_SetBufSize(p_f[0], bufsize); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4305 | 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] | 4306 | } |
| 4307 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4308 | f = PyTuple_Pack(2, p_f[0], p_f[1]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4309 | |
| 4310 | /* |
| 4311 | * Insert the files we've created into the process dictionary |
| 4312 | * all referencing the list with the process handle and the |
| 4313 | * initial number of files (see description below in _PyPclose). |
| 4314 | * Since if _PyPclose later tried to wait on a process when all |
| 4315 | * handles weren't closed, it could create a deadlock with the |
| 4316 | * child, we spend some energy here to try to ensure that we |
| 4317 | * either insert all file handles into the dictionary or none |
| 4318 | * at all. It's a little clumsy with the various popen modes |
| 4319 | * and variable number of files involved. |
| 4320 | */ |
| 4321 | if (!_PyPopenProcs) |
| 4322 | { |
| 4323 | _PyPopenProcs = PyDict_New(); |
| 4324 | } |
| 4325 | |
| 4326 | if (_PyPopenProcs) |
| 4327 | { |
| 4328 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 4329 | int ins_rc[3]; |
| 4330 | |
| 4331 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4332 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4333 | |
| 4334 | procObj = PyList_New(2); |
| 4335 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 4336 | intObj = PyInt_FromLong((long) file_count); |
| 4337 | |
| 4338 | if (procObj && pidObj && intObj) |
| 4339 | { |
| 4340 | PyList_SetItem(procObj, 0, pidObj); |
| 4341 | PyList_SetItem(procObj, 1, intObj); |
| 4342 | |
| 4343 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 4344 | if (fileObj[0]) |
| 4345 | { |
| 4346 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4347 | fileObj[0], |
| 4348 | procObj); |
| 4349 | } |
| 4350 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 4351 | if (fileObj[1]) |
| 4352 | { |
| 4353 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4354 | fileObj[1], |
| 4355 | procObj); |
| 4356 | } |
| 4357 | if (file_count >= 3) |
| 4358 | { |
| 4359 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 4360 | if (fileObj[2]) |
| 4361 | { |
| 4362 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4363 | fileObj[2], |
| 4364 | procObj); |
| 4365 | } |
| 4366 | } |
| 4367 | |
| 4368 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4369 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4370 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 4371 | { |
| 4372 | /* Something failed - remove any dictionary |
| 4373 | * entries that did make it. |
| 4374 | */ |
| 4375 | if (!ins_rc[0] && fileObj[0]) |
| 4376 | { |
| 4377 | PyDict_DelItem(_PyPopenProcs, |
| 4378 | fileObj[0]); |
| 4379 | } |
| 4380 | if (!ins_rc[1] && fileObj[1]) |
| 4381 | { |
| 4382 | PyDict_DelItem(_PyPopenProcs, |
| 4383 | fileObj[1]); |
| 4384 | } |
| 4385 | if (!ins_rc[2] && fileObj[2]) |
| 4386 | { |
| 4387 | PyDict_DelItem(_PyPopenProcs, |
| 4388 | fileObj[2]); |
| 4389 | } |
| 4390 | } |
| 4391 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4392 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4393 | /* |
| 4394 | * Clean up our localized references for the dictionary keys |
| 4395 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4396 | * that got placed in the dictionary. |
| 4397 | */ |
| 4398 | Py_XDECREF(procObj); |
| 4399 | Py_XDECREF(fileObj[0]); |
| 4400 | Py_XDECREF(fileObj[1]); |
| 4401 | Py_XDECREF(fileObj[2]); |
| 4402 | } |
| 4403 | |
| 4404 | /* Child is launched. */ |
| 4405 | return f; |
| 4406 | } |
| 4407 | |
| 4408 | /* |
| 4409 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4410 | * exit code for the child process and return as a result of the close. |
| 4411 | * |
| 4412 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4413 | * input file pointer to information about the process that was |
| 4414 | * originally created by the popen* call that created the file pointer. |
| 4415 | * The dictionary uses the file pointer as a key (with one entry |
| 4416 | * inserted for each file returned by the original popen* call) and a |
| 4417 | * single list object as the value for all files from a single call. |
| 4418 | * The list object contains the Win32 process handle at [0], and a file |
| 4419 | * count at [1], which is initialized to the total number of file |
| 4420 | * handles using that list. |
| 4421 | * |
| 4422 | * This function closes whichever handle it is passed, and decrements |
| 4423 | * the file count in the dictionary for the process handle pointed to |
| 4424 | * by this file. On the last close (when the file count reaches zero), |
| 4425 | * this function will wait for the child process and then return its |
| 4426 | * exit code as the result of the close() operation. This permits the |
| 4427 | * files to be closed in any order - it is always the close() of the |
| 4428 | * final handle that will return the exit code. |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4429 | * |
| 4430 | * NOTE: This function is currently called with the GIL released. |
| 4431 | * hence we use the GILState API to manage our state. |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4432 | */ |
| 4433 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4434 | static int _PyPclose(FILE *file) |
| 4435 | { |
| 4436 | int result; |
| 4437 | int exit_code; |
| 4438 | int pipe_pid; |
| 4439 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 4440 | int file_count; |
| 4441 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4442 | PyGILState_STATE state; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4443 | #endif |
| 4444 | |
| 4445 | /* Close the file handle first, to ensure it can't block the |
| 4446 | * child from exiting if it's the last handle. |
| 4447 | */ |
| 4448 | result = fclose(file); |
| 4449 | |
| 4450 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4451 | state = PyGILState_Ensure(); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4452 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4453 | if (_PyPopenProcs) |
| 4454 | { |
| 4455 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4456 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4457 | fileObj)) != NULL && |
| 4458 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 4459 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 4460 | { |
| 4461 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 4462 | file_count = (int) PyInt_AsLong(intObj); |
| 4463 | |
| 4464 | if (file_count > 1) |
| 4465 | { |
| 4466 | /* Still other files referencing process */ |
| 4467 | file_count--; |
| 4468 | PyList_SetItem(procObj,1, |
| 4469 | PyInt_FromLong((long) file_count)); |
| 4470 | } |
| 4471 | else |
| 4472 | { |
| 4473 | /* Last file for this process */ |
| 4474 | if (result != EOF && |
| 4475 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 4476 | { |
| 4477 | /* extract exit status */ |
| 4478 | if (WIFEXITED(exit_code)) |
| 4479 | { |
| 4480 | result = WEXITSTATUS(exit_code); |
| 4481 | } |
| 4482 | else |
| 4483 | { |
| 4484 | errno = EPIPE; |
| 4485 | result = -1; |
| 4486 | } |
| 4487 | } |
| 4488 | else |
| 4489 | { |
| 4490 | /* Indicate failure - this will cause the file object |
| 4491 | * to raise an I/O error and translate the last |
| 4492 | * error code from errno. We do have a problem with |
| 4493 | * last errors that overlap the normal errno table, |
| 4494 | * but that's a consistent problem with the file object. |
| 4495 | */ |
| 4496 | result = -1; |
| 4497 | } |
| 4498 | } |
| 4499 | |
| 4500 | /* Remove this file pointer from dictionary */ |
| 4501 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4502 | |
| 4503 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 4504 | { |
| 4505 | Py_DECREF(_PyPopenProcs); |
| 4506 | _PyPopenProcs = NULL; |
| 4507 | } |
| 4508 | |
| 4509 | } /* if object retrieval ok */ |
| 4510 | |
| 4511 | Py_XDECREF(fileObj); |
| 4512 | } /* if _PyPopenProcs */ |
| 4513 | |
| 4514 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4515 | PyGILState_Release(state); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4516 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4517 | return result; |
| 4518 | } |
| 4519 | |
| 4520 | #endif /* PYCC_??? */ |
| 4521 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4522 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4523 | |
| 4524 | /* |
| 4525 | * Portable 'popen' replacement for Win32. |
| 4526 | * |
| 4527 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 4528 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4529 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4530 | */ |
| 4531 | |
| 4532 | #include <malloc.h> |
| 4533 | #include <io.h> |
| 4534 | #include <fcntl.h> |
| 4535 | |
| 4536 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 4537 | #define POPEN_1 1 |
| 4538 | #define POPEN_2 2 |
| 4539 | #define POPEN_3 3 |
| 4540 | #define POPEN_4 4 |
| 4541 | |
| 4542 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4543 | static int _PyPclose(FILE *file); |
| 4544 | |
| 4545 | /* |
| 4546 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4547 | * for use when retrieving the process exit code. See _PyPclose() below |
| 4548 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4549 | */ |
| 4550 | static PyObject *_PyPopenProcs = NULL; |
| 4551 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4552 | |
| 4553 | /* popen that works from a GUI. |
| 4554 | * |
| 4555 | * The result of this function is a pipe (file) connected to the |
| 4556 | * processes stdin or stdout, depending on the requested mode. |
| 4557 | */ |
| 4558 | |
| 4559 | static PyObject * |
| 4560 | posix_popen(PyObject *self, PyObject *args) |
| 4561 | { |
Raymond Hettinger | b5cb665 | 2003-09-01 22:25:41 +0000 | [diff] [blame] | 4562 | PyObject *f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4563 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4564 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4565 | char *cmdstring; |
| 4566 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4567 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4568 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4569 | return NULL; |
| 4570 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4571 | if (*mode == 'r') |
| 4572 | tm = _O_RDONLY; |
| 4573 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4574 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4575 | return NULL; |
| 4576 | } else |
| 4577 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4578 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4579 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4580 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4581 | return NULL; |
| 4582 | } |
| 4583 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4584 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4585 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4586 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4587 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4588 | else |
| 4589 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 4590 | |
| 4591 | return f; |
| 4592 | } |
| 4593 | |
| 4594 | /* Variation on win32pipe.popen |
| 4595 | * |
| 4596 | * The result of this function is a pipe (file) connected to the |
| 4597 | * process's stdin, and a pipe connected to the process's stdout. |
| 4598 | */ |
| 4599 | |
| 4600 | static PyObject * |
| 4601 | win32_popen2(PyObject *self, PyObject *args) |
| 4602 | { |
| 4603 | PyObject *f; |
| 4604 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4605 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4606 | char *cmdstring; |
| 4607 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4608 | int bufsize = -1; |
| 4609 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4610 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4611 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4612 | if (*mode == 't') |
| 4613 | tm = _O_TEXT; |
| 4614 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4615 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4616 | return NULL; |
| 4617 | } else |
| 4618 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4619 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4620 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4621 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4622 | return NULL; |
| 4623 | } |
| 4624 | |
| 4625 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4626 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4627 | return f; |
| 4628 | } |
| 4629 | |
| 4630 | /* |
| 4631 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4632 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4633 | * The result of this function is 3 pipes - the process's stdin, |
| 4634 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4635 | */ |
| 4636 | |
| 4637 | static PyObject * |
| 4638 | win32_popen3(PyObject *self, PyObject *args) |
| 4639 | { |
| 4640 | PyObject *f; |
| 4641 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4642 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4643 | char *cmdstring; |
| 4644 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4645 | int bufsize = -1; |
| 4646 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4647 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4648 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4649 | if (*mode == 't') |
| 4650 | tm = _O_TEXT; |
| 4651 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4652 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4653 | return NULL; |
| 4654 | } else |
| 4655 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4656 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4657 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4658 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4659 | return NULL; |
| 4660 | } |
| 4661 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4662 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4663 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4664 | return f; |
| 4665 | } |
| 4666 | |
| 4667 | /* |
| 4668 | * Variation on win32pipe.popen |
| 4669 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4670 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4671 | * and stdout+stderr combined as a single pipe. |
| 4672 | */ |
| 4673 | |
| 4674 | static PyObject * |
| 4675 | win32_popen4(PyObject *self, PyObject *args) |
| 4676 | { |
| 4677 | PyObject *f; |
| 4678 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4679 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4680 | char *cmdstring; |
| 4681 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4682 | int bufsize = -1; |
| 4683 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4684 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4685 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4686 | if (*mode == 't') |
| 4687 | tm = _O_TEXT; |
| 4688 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4689 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4690 | return NULL; |
| 4691 | } else |
| 4692 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4693 | |
| 4694 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4695 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4696 | return NULL; |
| 4697 | } |
| 4698 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4699 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4700 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4701 | return f; |
| 4702 | } |
| 4703 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4704 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4705 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4706 | HANDLE hStdin, |
| 4707 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4708 | HANDLE hStderr, |
| 4709 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4710 | { |
| 4711 | PROCESS_INFORMATION piProcInfo; |
| 4712 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4713 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4714 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4715 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4716 | int i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4717 | Py_ssize_t x; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4718 | |
| 4719 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4720 | char *comshell; |
| 4721 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4722 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4723 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4724 | /* x < i, so x fits into an integer */ |
| 4725 | return (int)x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4726 | |
| 4727 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 4728 | * then use the w9xpopen hack. |
| 4729 | */ |
| 4730 | comshell = s1 + x; |
| 4731 | while (comshell >= s1 && *comshell != '\\') |
| 4732 | --comshell; |
| 4733 | ++comshell; |
| 4734 | |
| 4735 | if (GetVersion() < 0x80000000 && |
| 4736 | _stricmp(comshell, "command.com") != 0) { |
| 4737 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4738 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4739 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4740 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4741 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4742 | } |
| 4743 | else { |
| 4744 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4745 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 4746 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4747 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4748 | char modulepath[_MAX_PATH]; |
| 4749 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4750 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 4751 | for (x = i = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4752 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4753 | x = i+1; |
| 4754 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4755 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4756 | strncat(modulepath, |
| 4757 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4758 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4759 | -strlen(modulepath)); |
| 4760 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4761 | /* Eeek - file-not-found - possibly an embedding |
| 4762 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4763 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4764 | strncpy(modulepath, |
| 4765 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4766 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 4767 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 4768 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4769 | strncat(modulepath, |
| 4770 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4771 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4772 | -strlen(modulepath)); |
| 4773 | /* No where else to look - raise an easily identifiable |
| 4774 | error, rather than leaving Windows to report |
| 4775 | "file not found" - as the user is probably blissfully |
| 4776 | unaware this shim EXE is used, and it will confuse them. |
| 4777 | (well, it confused me for a while ;-) |
| 4778 | */ |
| 4779 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4780 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4781 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4782 | "for popen to work with your shell " |
| 4783 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4784 | szConsoleSpawn); |
| 4785 | return FALSE; |
| 4786 | } |
| 4787 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4788 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4789 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4790 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4791 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4792 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4793 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4794 | /* To maintain correct argument passing semantics, |
| 4795 | we pass the command-line as it stands, and allow |
| 4796 | quoting to be applied. w9xpopen.exe will then |
| 4797 | use its argv vector, and re-quote the necessary |
| 4798 | args for the ultimate child process. |
| 4799 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4800 | PyOS_snprintf( |
| 4801 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4802 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4803 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4804 | s1, |
| 4805 | s3, |
| 4806 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4807 | /* Not passing CREATE_NEW_CONSOLE has been known to |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4808 | cause random failures on win9x. Specifically a |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4809 | dialog: |
| 4810 | "Your program accessed mem currently in use at xxx" |
| 4811 | and a hopeful warning about the stability of your |
| 4812 | system. |
| 4813 | Cost is Ctrl+C wont kill children, but anyone |
| 4814 | who cares can have a go! |
| 4815 | */ |
| 4816 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4817 | } |
| 4818 | } |
| 4819 | |
| 4820 | /* Could be an else here to try cmd.exe / command.com in the path |
| 4821 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4822 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4823 | PyErr_SetString(PyExc_RuntimeError, |
| 4824 | "Cannot locate a COMSPEC environment variable to " |
| 4825 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4826 | return FALSE; |
| 4827 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4828 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4829 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 4830 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 4831 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 4832 | siStartInfo.hStdInput = hStdin; |
| 4833 | siStartInfo.hStdOutput = hStdout; |
| 4834 | siStartInfo.hStdError = hStderr; |
| 4835 | siStartInfo.wShowWindow = SW_HIDE; |
| 4836 | |
| 4837 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4838 | s2, |
| 4839 | NULL, |
| 4840 | NULL, |
| 4841 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4842 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4843 | NULL, |
| 4844 | NULL, |
| 4845 | &siStartInfo, |
| 4846 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4847 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4848 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4849 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4850 | /* Return process handle */ |
| 4851 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4852 | return TRUE; |
| 4853 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4854 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4855 | return FALSE; |
| 4856 | } |
| 4857 | |
| 4858 | /* The following code is based off of KB: Q190351 */ |
| 4859 | |
| 4860 | static PyObject * |
| 4861 | _PyPopen(char *cmdstring, int mode, int n) |
| 4862 | { |
| 4863 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 4864 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4865 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4866 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4867 | SECURITY_ATTRIBUTES saAttr; |
| 4868 | BOOL fSuccess; |
| 4869 | int fd1, fd2, fd3; |
| 4870 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4871 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4872 | PyObject *f; |
| 4873 | |
| 4874 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 4875 | saAttr.bInheritHandle = TRUE; |
| 4876 | saAttr.lpSecurityDescriptor = NULL; |
| 4877 | |
| 4878 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 4879 | return win32_error("CreatePipe", NULL); |
| 4880 | |
| 4881 | /* Create new output read handle and the input write handle. Set |
| 4882 | * the inheritance properties to FALSE. Otherwise, the child inherits |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 4883 | * these handles; resulting in non-closeable handles to the pipes |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4884 | * being created. */ |
| 4885 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4886 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 4887 | FALSE, |
| 4888 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4889 | if (!fSuccess) |
| 4890 | return win32_error("DuplicateHandle", NULL); |
| 4891 | |
| 4892 | /* Close the inheritable version of ChildStdin |
| 4893 | that we're using. */ |
| 4894 | CloseHandle(hChildStdinWr); |
| 4895 | |
| 4896 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 4897 | return win32_error("CreatePipe", NULL); |
| 4898 | |
| 4899 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4900 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 4901 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4902 | if (!fSuccess) |
| 4903 | return win32_error("DuplicateHandle", NULL); |
| 4904 | |
| 4905 | /* Close the inheritable version of ChildStdout |
| 4906 | that we're using. */ |
| 4907 | CloseHandle(hChildStdoutRd); |
| 4908 | |
| 4909 | if (n != POPEN_4) { |
| 4910 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 4911 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4912 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 4913 | hChildStderrRd, |
| 4914 | GetCurrentProcess(), |
| 4915 | &hChildStderrRdDup, 0, |
| 4916 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4917 | if (!fSuccess) |
| 4918 | return win32_error("DuplicateHandle", NULL); |
| 4919 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 4920 | CloseHandle(hChildStderrRd); |
| 4921 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4922 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4923 | switch (n) { |
| 4924 | case POPEN_1: |
| 4925 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 4926 | case _O_WRONLY | _O_TEXT: |
| 4927 | /* Case for writing to child Stdin in text mode. */ |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4928 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4929 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4930 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4931 | PyFile_SetBufSize(f, 0); |
| 4932 | /* We don't care about these pipes anymore, so close them. */ |
| 4933 | CloseHandle(hChildStdoutRdDup); |
| 4934 | CloseHandle(hChildStderrRdDup); |
| 4935 | break; |
| 4936 | |
| 4937 | case _O_RDONLY | _O_TEXT: |
| 4938 | /* Case for reading from child Stdout in text mode. */ |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4939 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4940 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4941 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4942 | PyFile_SetBufSize(f, 0); |
| 4943 | /* We don't care about these pipes anymore, so close them. */ |
| 4944 | CloseHandle(hChildStdinWrDup); |
| 4945 | CloseHandle(hChildStderrRdDup); |
| 4946 | break; |
| 4947 | |
| 4948 | case _O_RDONLY | _O_BINARY: |
| 4949 | /* Case for readinig from child Stdout in binary mode. */ |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4950 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4951 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4952 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4953 | PyFile_SetBufSize(f, 0); |
| 4954 | /* We don't care about these pipes anymore, so close them. */ |
| 4955 | CloseHandle(hChildStdinWrDup); |
| 4956 | CloseHandle(hChildStderrRdDup); |
| 4957 | break; |
| 4958 | |
| 4959 | case _O_WRONLY | _O_BINARY: |
| 4960 | /* Case for writing to child Stdin in binary mode. */ |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4961 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4962 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4963 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4964 | PyFile_SetBufSize(f, 0); |
| 4965 | /* We don't care about these pipes anymore, so close them. */ |
| 4966 | CloseHandle(hChildStdoutRdDup); |
| 4967 | CloseHandle(hChildStderrRdDup); |
| 4968 | break; |
| 4969 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4970 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4971 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4972 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4973 | case POPEN_2: |
| 4974 | case POPEN_4: |
| 4975 | { |
| 4976 | char *m1, *m2; |
| 4977 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4978 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4979 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4980 | m1 = "r"; |
| 4981 | m2 = "w"; |
| 4982 | } else { |
| 4983 | m1 = "rb"; |
| 4984 | m2 = "wb"; |
| 4985 | } |
| 4986 | |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4987 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4988 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4989 | fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4990 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4991 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4992 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4993 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4994 | PyFile_SetBufSize(p2, 0); |
| 4995 | |
| 4996 | if (n != 4) |
| 4997 | CloseHandle(hChildStderrRdDup); |
| 4998 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4999 | f = PyTuple_Pack(2,p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 5000 | Py_XDECREF(p1); |
| 5001 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5002 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5003 | break; |
| 5004 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5005 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5006 | case POPEN_3: |
| 5007 | { |
| 5008 | char *m1, *m2; |
| 5009 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5010 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 5011 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5012 | m1 = "r"; |
| 5013 | m2 = "w"; |
| 5014 | } else { |
| 5015 | m1 = "rb"; |
| 5016 | m2 = "wb"; |
| 5017 | } |
| 5018 | |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 5019 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5020 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 5021 | fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5022 | f2 = _fdopen(fd2, m1); |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 5023 | fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5024 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5025 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5026 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 5027 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5028 | PyFile_SetBufSize(p1, 0); |
| 5029 | PyFile_SetBufSize(p2, 0); |
| 5030 | PyFile_SetBufSize(p3, 0); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 5031 | f = PyTuple_Pack(3,p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 5032 | Py_XDECREF(p1); |
| 5033 | Py_XDECREF(p2); |
| 5034 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5035 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5036 | break; |
| 5037 | } |
| 5038 | } |
| 5039 | |
| 5040 | if (n == POPEN_4) { |
| 5041 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 5042 | hChildStdinRd, |
| 5043 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5044 | hChildStdoutWr, |
| 5045 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 5046 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5047 | } |
| 5048 | else { |
| 5049 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 5050 | hChildStdinRd, |
| 5051 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5052 | hChildStderrWr, |
| 5053 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 5054 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5055 | } |
| 5056 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5057 | /* |
| 5058 | * Insert the files we've created into the process dictionary |
| 5059 | * all referencing the list with the process handle and the |
| 5060 | * initial number of files (see description below in _PyPclose). |
| 5061 | * Since if _PyPclose later tried to wait on a process when all |
| 5062 | * handles weren't closed, it could create a deadlock with the |
| 5063 | * child, we spend some energy here to try to ensure that we |
| 5064 | * either insert all file handles into the dictionary or none |
| 5065 | * at all. It's a little clumsy with the various popen modes |
| 5066 | * and variable number of files involved. |
| 5067 | */ |
| 5068 | if (!_PyPopenProcs) { |
| 5069 | _PyPopenProcs = PyDict_New(); |
| 5070 | } |
| 5071 | |
| 5072 | if (_PyPopenProcs) { |
| 5073 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 5074 | int ins_rc[3]; |
| 5075 | |
| 5076 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 5077 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 5078 | |
| 5079 | procObj = PyList_New(2); |
| 5080 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 5081 | intObj = PyInt_FromLong(file_count); |
| 5082 | |
| 5083 | if (procObj && hProcessObj && intObj) { |
| 5084 | PyList_SetItem(procObj,0,hProcessObj); |
| 5085 | PyList_SetItem(procObj,1,intObj); |
| 5086 | |
| 5087 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 5088 | if (fileObj[0]) { |
| 5089 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 5090 | fileObj[0], |
| 5091 | procObj); |
| 5092 | } |
| 5093 | if (file_count >= 2) { |
| 5094 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 5095 | if (fileObj[1]) { |
| 5096 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 5097 | fileObj[1], |
| 5098 | procObj); |
| 5099 | } |
| 5100 | } |
| 5101 | if (file_count >= 3) { |
| 5102 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 5103 | if (fileObj[2]) { |
| 5104 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 5105 | fileObj[2], |
| 5106 | procObj); |
| 5107 | } |
| 5108 | } |
| 5109 | |
| 5110 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 5111 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 5112 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 5113 | /* Something failed - remove any dictionary |
| 5114 | * entries that did make it. |
| 5115 | */ |
| 5116 | if (!ins_rc[0] && fileObj[0]) { |
| 5117 | PyDict_DelItem(_PyPopenProcs, |
| 5118 | fileObj[0]); |
| 5119 | } |
| 5120 | if (!ins_rc[1] && fileObj[1]) { |
| 5121 | PyDict_DelItem(_PyPopenProcs, |
| 5122 | fileObj[1]); |
| 5123 | } |
| 5124 | if (!ins_rc[2] && fileObj[2]) { |
| 5125 | PyDict_DelItem(_PyPopenProcs, |
| 5126 | fileObj[2]); |
| 5127 | } |
| 5128 | } |
| 5129 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5130 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5131 | /* |
| 5132 | * Clean up our localized references for the dictionary keys |
| 5133 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 5134 | * that got placed in the dictionary. |
| 5135 | */ |
| 5136 | Py_XDECREF(procObj); |
| 5137 | Py_XDECREF(fileObj[0]); |
| 5138 | Py_XDECREF(fileObj[1]); |
| 5139 | Py_XDECREF(fileObj[2]); |
| 5140 | } |
| 5141 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5142 | /* Child is launched. Close the parents copy of those pipe |
| 5143 | * handles that only the child should have open. You need to |
| 5144 | * make sure that no handles to the write end of the output pipe |
| 5145 | * are maintained in this process or else the pipe will not close |
| 5146 | * when the child process exits and the ReadFile will hang. */ |
| 5147 | |
| 5148 | if (!CloseHandle(hChildStdinRd)) |
| 5149 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5150 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5151 | if (!CloseHandle(hChildStdoutWr)) |
| 5152 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5153 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5154 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 5155 | return win32_error("CloseHandle", NULL); |
| 5156 | |
| 5157 | return f; |
| 5158 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5159 | |
| 5160 | /* |
| 5161 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 5162 | * 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] | 5163 | * |
| 5164 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 5165 | * input file pointer to information about the process that was |
| 5166 | * originally created by the popen* call that created the file pointer. |
| 5167 | * The dictionary uses the file pointer as a key (with one entry |
| 5168 | * inserted for each file returned by the original popen* call) and a |
| 5169 | * single list object as the value for all files from a single call. |
| 5170 | * The list object contains the Win32 process handle at [0], and a file |
| 5171 | * count at [1], which is initialized to the total number of file |
| 5172 | * handles using that list. |
| 5173 | * |
| 5174 | * This function closes whichever handle it is passed, and decrements |
| 5175 | * the file count in the dictionary for the process handle pointed to |
| 5176 | * by this file. On the last close (when the file count reaches zero), |
| 5177 | * this function will wait for the child process and then return its |
| 5178 | * exit code as the result of the close() operation. This permits the |
| 5179 | * files to be closed in any order - it is always the close() of the |
| 5180 | * final handle that will return the exit code. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 5181 | * |
| 5182 | * NOTE: This function is currently called with the GIL released. |
| 5183 | * hence we use the GILState API to manage our state. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5184 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5185 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5186 | static int _PyPclose(FILE *file) |
| 5187 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5188 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5189 | DWORD exit_code; |
| 5190 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5191 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 5192 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5193 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 5194 | PyGILState_STATE state; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5195 | #endif |
| 5196 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5197 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5198 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5199 | */ |
| 5200 | result = fclose(file); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5201 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 5202 | state = PyGILState_Ensure(); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5203 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5204 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5205 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 5206 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 5207 | fileObj)) != NULL && |
| 5208 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 5209 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 5210 | |
| 5211 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 5212 | file_count = PyInt_AsLong(intObj); |
| 5213 | |
| 5214 | if (file_count > 1) { |
| 5215 | /* Still other files referencing process */ |
| 5216 | file_count--; |
| 5217 | PyList_SetItem(procObj,1, |
| 5218 | PyInt_FromLong(file_count)); |
| 5219 | } else { |
| 5220 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5221 | if (result != EOF && |
| 5222 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 5223 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5224 | /* Possible truncation here in 16-bit environments, but |
| 5225 | * real exit codes are just the lower byte in any event. |
| 5226 | */ |
| 5227 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5228 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5229 | /* Indicate failure - this will cause the file object |
| 5230 | * to raise an I/O error and translate the last Win32 |
| 5231 | * error code from errno. We do have a problem with |
| 5232 | * last errors that overlap the normal errno table, |
| 5233 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5234 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5235 | if (result != EOF) { |
| 5236 | /* If the error wasn't from the fclose(), then |
| 5237 | * set errno for the file object error handling. |
| 5238 | */ |
| 5239 | errno = GetLastError(); |
| 5240 | } |
| 5241 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5242 | } |
| 5243 | |
| 5244 | /* Free up the native handle at this point */ |
| 5245 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5246 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5247 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5248 | /* Remove this file pointer from dictionary */ |
| 5249 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 5250 | |
| 5251 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 5252 | Py_DECREF(_PyPopenProcs); |
| 5253 | _PyPopenProcs = NULL; |
| 5254 | } |
| 5255 | |
| 5256 | } /* if object retrieval ok */ |
| 5257 | |
| 5258 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5259 | } /* if _PyPopenProcs */ |
| 5260 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5261 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 5262 | PyGILState_Release(state); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5263 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5264 | return result; |
| 5265 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 5266 | |
| 5267 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5268 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5269 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5270 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5271 | char *name; |
| 5272 | char *mode = "r"; |
| 5273 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5274 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5275 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5276 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5277 | return NULL; |
Martin v. Löwis | 9ad853b | 2003-10-31 10:01:53 +0000 | [diff] [blame] | 5278 | /* Strip mode of binary or text modifiers */ |
| 5279 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) |
| 5280 | mode = "r"; |
| 5281 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) |
| 5282 | mode = "w"; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5283 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 5284 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5285 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5286 | if (fp == NULL) |
| 5287 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5288 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5289 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5290 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5291 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5292 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5293 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5294 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5295 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5296 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5297 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5298 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5299 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5300 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5301 | Set the current process's user id."); |
| 5302 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5303 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5304 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5305 | { |
| 5306 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5307 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5308 | return NULL; |
| 5309 | if (setuid(uid) < 0) |
| 5310 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5311 | Py_INCREF(Py_None); |
| 5312 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5313 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5314 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5315 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5316 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5317 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5318 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5319 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5320 | Set the current process's effective user id."); |
| 5321 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5322 | static PyObject * |
| 5323 | posix_seteuid (PyObject *self, PyObject *args) |
| 5324 | { |
| 5325 | int euid; |
| 5326 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 5327 | return NULL; |
| 5328 | } else if (seteuid(euid) < 0) { |
| 5329 | return posix_error(); |
| 5330 | } else { |
| 5331 | Py_INCREF(Py_None); |
| 5332 | return Py_None; |
| 5333 | } |
| 5334 | } |
| 5335 | #endif /* HAVE_SETEUID */ |
| 5336 | |
| 5337 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5338 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5339 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5340 | Set the current process's effective group id."); |
| 5341 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5342 | static PyObject * |
| 5343 | posix_setegid (PyObject *self, PyObject *args) |
| 5344 | { |
| 5345 | int egid; |
| 5346 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 5347 | return NULL; |
| 5348 | } else if (setegid(egid) < 0) { |
| 5349 | return posix_error(); |
| 5350 | } else { |
| 5351 | Py_INCREF(Py_None); |
| 5352 | return Py_None; |
| 5353 | } |
| 5354 | } |
| 5355 | #endif /* HAVE_SETEGID */ |
| 5356 | |
| 5357 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5358 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5359 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5360 | Set the current process's real and effective user ids."); |
| 5361 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5362 | static PyObject * |
| 5363 | posix_setreuid (PyObject *self, PyObject *args) |
| 5364 | { |
| 5365 | int ruid, euid; |
| 5366 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 5367 | return NULL; |
| 5368 | } else if (setreuid(ruid, euid) < 0) { |
| 5369 | return posix_error(); |
| 5370 | } else { |
| 5371 | Py_INCREF(Py_None); |
| 5372 | return Py_None; |
| 5373 | } |
| 5374 | } |
| 5375 | #endif /* HAVE_SETREUID */ |
| 5376 | |
| 5377 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5378 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5379 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5380 | Set the current process's real and effective group ids."); |
| 5381 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5382 | static PyObject * |
| 5383 | posix_setregid (PyObject *self, PyObject *args) |
| 5384 | { |
| 5385 | int rgid, egid; |
| 5386 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 5387 | return NULL; |
| 5388 | } else if (setregid(rgid, egid) < 0) { |
| 5389 | return posix_error(); |
| 5390 | } else { |
| 5391 | Py_INCREF(Py_None); |
| 5392 | return Py_None; |
| 5393 | } |
| 5394 | } |
| 5395 | #endif /* HAVE_SETREGID */ |
| 5396 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5397 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5398 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5399 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5400 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5401 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5402 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5403 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5404 | { |
| 5405 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5406 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5407 | return NULL; |
| 5408 | if (setgid(gid) < 0) |
| 5409 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5410 | Py_INCREF(Py_None); |
| 5411 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5412 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5413 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5414 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5415 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5416 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5417 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5418 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5419 | |
| 5420 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5421 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5422 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5423 | int i, len; |
| 5424 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5425 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5426 | if (!PySequence_Check(groups)) { |
| 5427 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 5428 | return NULL; |
| 5429 | } |
| 5430 | len = PySequence_Size(groups); |
| 5431 | if (len > MAX_GROUPS) { |
| 5432 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 5433 | return NULL; |
| 5434 | } |
| 5435 | for(i = 0; i < len; i++) { |
| 5436 | PyObject *elem; |
| 5437 | elem = PySequence_GetItem(groups, i); |
| 5438 | if (!elem) |
| 5439 | return NULL; |
| 5440 | if (!PyInt_Check(elem)) { |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 5441 | if (!PyLong_Check(elem)) { |
| 5442 | PyErr_SetString(PyExc_TypeError, |
| 5443 | "groups must be integers"); |
| 5444 | Py_DECREF(elem); |
| 5445 | return NULL; |
| 5446 | } else { |
| 5447 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 5448 | if (PyErr_Occurred()) { |
| 5449 | PyErr_SetString(PyExc_TypeError, |
| 5450 | "group id too big"); |
| 5451 | Py_DECREF(elem); |
| 5452 | return NULL; |
| 5453 | } |
| 5454 | grouplist[i] = x; |
| 5455 | /* read back the value to see if it fitted in gid_t */ |
| 5456 | if (grouplist[i] != x) { |
| 5457 | PyErr_SetString(PyExc_TypeError, |
| 5458 | "group id too big"); |
| 5459 | Py_DECREF(elem); |
| 5460 | return NULL; |
| 5461 | } |
| 5462 | } |
| 5463 | } else { |
| 5464 | long x = PyInt_AsLong(elem); |
| 5465 | grouplist[i] = x; |
| 5466 | if (grouplist[i] != x) { |
| 5467 | PyErr_SetString(PyExc_TypeError, |
| 5468 | "group id too big"); |
| 5469 | Py_DECREF(elem); |
| 5470 | return NULL; |
| 5471 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5472 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5473 | Py_DECREF(elem); |
| 5474 | } |
| 5475 | |
| 5476 | if (setgroups(len, grouplist) < 0) |
| 5477 | return posix_error(); |
| 5478 | Py_INCREF(Py_None); |
| 5479 | return Py_None; |
| 5480 | } |
| 5481 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5482 | |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 5483 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5484 | static PyObject * |
| 5485 | wait_helper(int pid, int status, struct rusage *ru) |
| 5486 | { |
| 5487 | PyObject *result; |
| 5488 | static PyObject *struct_rusage; |
| 5489 | |
| 5490 | if (pid == -1) |
| 5491 | return posix_error(); |
| 5492 | |
| 5493 | if (struct_rusage == NULL) { |
| 5494 | PyObject *m = PyImport_ImportModule("resource"); |
| 5495 | if (m == NULL) |
| 5496 | return NULL; |
| 5497 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 5498 | Py_DECREF(m); |
| 5499 | if (struct_rusage == NULL) |
| 5500 | return NULL; |
| 5501 | } |
| 5502 | |
| 5503 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 5504 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 5505 | if (!result) |
| 5506 | return NULL; |
| 5507 | |
| 5508 | #ifndef doubletime |
| 5509 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 5510 | #endif |
| 5511 | |
| 5512 | PyStructSequence_SET_ITEM(result, 0, |
| 5513 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 5514 | PyStructSequence_SET_ITEM(result, 1, |
| 5515 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 5516 | #define SET_INT(result, index, value)\ |
| 5517 | PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value)) |
| 5518 | SET_INT(result, 2, ru->ru_maxrss); |
| 5519 | SET_INT(result, 3, ru->ru_ixrss); |
| 5520 | SET_INT(result, 4, ru->ru_idrss); |
| 5521 | SET_INT(result, 5, ru->ru_isrss); |
| 5522 | SET_INT(result, 6, ru->ru_minflt); |
| 5523 | SET_INT(result, 7, ru->ru_majflt); |
| 5524 | SET_INT(result, 8, ru->ru_nswap); |
| 5525 | SET_INT(result, 9, ru->ru_inblock); |
| 5526 | SET_INT(result, 10, ru->ru_oublock); |
| 5527 | SET_INT(result, 11, ru->ru_msgsnd); |
| 5528 | SET_INT(result, 12, ru->ru_msgrcv); |
| 5529 | SET_INT(result, 13, ru->ru_nsignals); |
| 5530 | SET_INT(result, 14, ru->ru_nvcsw); |
| 5531 | SET_INT(result, 15, ru->ru_nivcsw); |
| 5532 | #undef SET_INT |
| 5533 | |
| 5534 | if (PyErr_Occurred()) { |
| 5535 | Py_DECREF(result); |
| 5536 | return NULL; |
| 5537 | } |
| 5538 | |
Neal Norwitz | 9b00a56 | 2006-03-20 08:47:12 +0000 | [diff] [blame] | 5539 | return Py_BuildValue("iiN", pid, status, result); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5540 | } |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 5541 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5542 | |
| 5543 | #ifdef HAVE_WAIT3 |
| 5544 | PyDoc_STRVAR(posix_wait3__doc__, |
| 5545 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 5546 | Wait for completion of a child process."); |
| 5547 | |
| 5548 | static PyObject * |
| 5549 | posix_wait3(PyObject *self, PyObject *args) |
| 5550 | { |
| 5551 | int pid, options; |
| 5552 | struct rusage ru; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5553 | WAIT_TYPE status; |
| 5554 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5555 | |
| 5556 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 5557 | return NULL; |
| 5558 | |
| 5559 | Py_BEGIN_ALLOW_THREADS |
| 5560 | pid = wait3(&status, options, &ru); |
| 5561 | Py_END_ALLOW_THREADS |
| 5562 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5563 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5564 | } |
| 5565 | #endif /* HAVE_WAIT3 */ |
| 5566 | |
| 5567 | #ifdef HAVE_WAIT4 |
| 5568 | PyDoc_STRVAR(posix_wait4__doc__, |
| 5569 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 5570 | Wait for completion of a given child process."); |
| 5571 | |
| 5572 | static PyObject * |
| 5573 | posix_wait4(PyObject *self, PyObject *args) |
| 5574 | { |
| 5575 | int pid, options; |
| 5576 | struct rusage ru; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5577 | WAIT_TYPE status; |
| 5578 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5579 | |
| 5580 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 5581 | return NULL; |
| 5582 | |
| 5583 | Py_BEGIN_ALLOW_THREADS |
| 5584 | pid = wait4(pid, &status, options, &ru); |
| 5585 | Py_END_ALLOW_THREADS |
| 5586 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5587 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5588 | } |
| 5589 | #endif /* HAVE_WAIT4 */ |
| 5590 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5591 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5592 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5593 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5594 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5595 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5596 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5597 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5598 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5599 | int pid, options; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5600 | WAIT_TYPE status; |
| 5601 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5602 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5603 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5604 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5605 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 5606 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5607 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5608 | if (pid == -1) |
| 5609 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5610 | |
| 5611 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5614 | #elif defined(HAVE_CWAIT) |
| 5615 | |
| 5616 | /* 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] | 5617 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5618 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5619 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5620 | |
| 5621 | static PyObject * |
| 5622 | posix_waitpid(PyObject *self, PyObject *args) |
| 5623 | { |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 5624 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5625 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5626 | |
| 5627 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 5628 | return NULL; |
| 5629 | Py_BEGIN_ALLOW_THREADS |
| 5630 | pid = _cwait(&status, pid, options); |
| 5631 | Py_END_ALLOW_THREADS |
| 5632 | if (pid == -1) |
| 5633 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5634 | |
| 5635 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 5636 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5637 | } |
| 5638 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5639 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5640 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5641 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5642 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5643 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5644 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5645 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5646 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5647 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5648 | int pid; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5649 | WAIT_TYPE status; |
| 5650 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5651 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5652 | Py_BEGIN_ALLOW_THREADS |
| 5653 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5654 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5655 | if (pid == -1) |
| 5656 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5657 | |
| 5658 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5659 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5660 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5661 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5662 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5663 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5664 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5665 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5666 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5667 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5668 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5669 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5670 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5671 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5672 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5673 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5674 | 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] | 5675 | #else |
| 5676 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 5677 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5678 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5679 | } |
| 5680 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5681 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5682 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5683 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5684 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5685 | 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] | 5686 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5687 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5688 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5689 | { |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 5690 | PyObject* v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5691 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 5692 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5693 | int n; |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 5694 | #ifdef Py_USING_UNICODE |
| 5695 | int arg_is_unicode = 0; |
| 5696 | #endif |
| 5697 | |
| 5698 | if (!PyArg_ParseTuple(args, "et:readlink", |
| 5699 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5700 | return NULL; |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 5701 | #ifdef Py_USING_UNICODE |
| 5702 | v = PySequence_GetItem(args, 0); |
| 5703 | if (v == NULL) return NULL; |
| 5704 | |
| 5705 | if (PyUnicode_Check(v)) { |
| 5706 | arg_is_unicode = 1; |
| 5707 | } |
| 5708 | Py_DECREF(v); |
| 5709 | #endif |
| 5710 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5711 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 5712 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5713 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5714 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 5715 | return posix_error_with_filename(path); |
Ronald Oussoren | 10168f2 | 2006-10-22 10:45:18 +0000 | [diff] [blame] | 5716 | |
| 5717 | v = PyString_FromStringAndSize(buf, n); |
| 5718 | #ifdef Py_USING_UNICODE |
| 5719 | if (arg_is_unicode) { |
| 5720 | PyObject *w; |
| 5721 | |
| 5722 | w = PyUnicode_FromEncodedObject(v, |
| 5723 | Py_FileSystemDefaultEncoding, |
| 5724 | "strict"); |
| 5725 | if (w != NULL) { |
| 5726 | Py_DECREF(v); |
| 5727 | v = w; |
| 5728 | } |
| 5729 | else { |
| 5730 | /* fall back to the original byte string, as |
| 5731 | discussed in patch #683592 */ |
| 5732 | PyErr_Clear(); |
| 5733 | } |
| 5734 | } |
| 5735 | #endif |
| 5736 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5737 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5738 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5739 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5740 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5741 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5742 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5743 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 5744 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5745 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5746 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5747 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5748 | { |
Martin v. Löwis | 4fc2bda | 2006-05-04 12:04:27 +0000 | [diff] [blame] | 5749 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5750 | } |
| 5751 | #endif /* HAVE_SYMLINK */ |
| 5752 | |
| 5753 | |
| 5754 | #ifdef HAVE_TIMES |
| 5755 | #ifndef HZ |
| 5756 | #define HZ 60 /* Universal constant :-) */ |
| 5757 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5758 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5759 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 5760 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5761 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5762 | { |
| 5763 | ULONG value = 0; |
| 5764 | |
| 5765 | Py_BEGIN_ALLOW_THREADS |
| 5766 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 5767 | Py_END_ALLOW_THREADS |
| 5768 | |
| 5769 | return value; |
| 5770 | } |
| 5771 | |
| 5772 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5773 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5774 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5775 | /* Currently Only Uptime is Provided -- Others Later */ |
| 5776 | return Py_BuildValue("ddddd", |
| 5777 | (double)0 /* t.tms_utime / HZ */, |
| 5778 | (double)0 /* t.tms_stime / HZ */, |
| 5779 | (double)0 /* t.tms_cutime / HZ */, |
| 5780 | (double)0 /* t.tms_cstime / HZ */, |
| 5781 | (double)system_uptime() / 1000); |
| 5782 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5783 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5784 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5785 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5786 | { |
| 5787 | struct tms t; |
| 5788 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5789 | errno = 0; |
| 5790 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5791 | if (c == (clock_t) -1) |
| 5792 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5793 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5794 | (double)t.tms_utime / HZ, |
| 5795 | (double)t.tms_stime / HZ, |
| 5796 | (double)t.tms_cutime / HZ, |
| 5797 | (double)t.tms_cstime / HZ, |
| 5798 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5799 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5800 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5801 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5802 | |
| 5803 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5804 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5805 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5806 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5807 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5808 | { |
| 5809 | FILETIME create, exit, kernel, user; |
| 5810 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5811 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5812 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 5813 | /* The fields of a FILETIME structure are the hi and lo part |
| 5814 | of a 64-bit value expressed in 100 nanosecond units. |
| 5815 | 1e7 is one second in such units; 1e-7 the inverse. |
| 5816 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 5817 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5818 | return Py_BuildValue( |
| 5819 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5820 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 5821 | kernel.dwLowDateTime*1e-7), |
| 5822 | (double)(user.dwHighDateTime*429.4967296 + |
| 5823 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5824 | (double)0, |
| 5825 | (double)0, |
| 5826 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5827 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5828 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5829 | |
| 5830 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5831 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5832 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5833 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5834 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5835 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5836 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5837 | #ifdef HAVE_GETSID |
| 5838 | PyDoc_STRVAR(posix_getsid__doc__, |
| 5839 | "getsid(pid) -> sid\n\n\ |
| 5840 | Call the system call getsid()."); |
| 5841 | |
| 5842 | static PyObject * |
| 5843 | posix_getsid(PyObject *self, PyObject *args) |
| 5844 | { |
| 5845 | int pid, sid; |
| 5846 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 5847 | return NULL; |
| 5848 | sid = getsid(pid); |
| 5849 | if (sid < 0) |
| 5850 | return posix_error(); |
| 5851 | return PyInt_FromLong((long)sid); |
| 5852 | } |
| 5853 | #endif /* HAVE_GETSID */ |
| 5854 | |
| 5855 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5856 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5857 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5858 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5859 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5860 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5861 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5862 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5863 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5864 | if (setsid() < 0) |
| 5865 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5866 | Py_INCREF(Py_None); |
| 5867 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5868 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5869 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5870 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5871 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5872 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5873 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5874 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5875 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5876 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5877 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5878 | { |
| 5879 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5880 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5881 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5882 | if (setpgid(pid, pgrp) < 0) |
| 5883 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5884 | Py_INCREF(Py_None); |
| 5885 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5886 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5887 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5888 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5889 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5890 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5891 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5892 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5893 | 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] | 5894 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5895 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5896 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5897 | { |
| 5898 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5899 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5900 | return NULL; |
| 5901 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5902 | if (pgid < 0) |
| 5903 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5904 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5905 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5906 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5907 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5908 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5909 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5910 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5911 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5912 | 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] | 5913 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5914 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5915 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5916 | { |
| 5917 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5918 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5919 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5920 | if (tcsetpgrp(fd, pgid) < 0) |
| 5921 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5922 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5923 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5924 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5925 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5926 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5927 | /* Functions acting on file descriptors */ |
| 5928 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5929 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5930 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5931 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5932 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5933 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5934 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5935 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5936 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5937 | int flag; |
| 5938 | int mode = 0777; |
| 5939 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5940 | |
| 5941 | #ifdef MS_WINDOWS |
| 5942 | if (unicode_file_names()) { |
| 5943 | PyUnicodeObject *po; |
| 5944 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 5945 | Py_BEGIN_ALLOW_THREADS |
| 5946 | /* PyUnicode_AS_UNICODE OK without thread |
| 5947 | lock as it is a simple dereference. */ |
| 5948 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 5949 | Py_END_ALLOW_THREADS |
| 5950 | if (fd < 0) |
| 5951 | return posix_error(); |
| 5952 | return PyInt_FromLong((long)fd); |
| 5953 | } |
| 5954 | /* Drop the argument parsing error as narrow strings |
| 5955 | are also valid. */ |
| 5956 | PyErr_Clear(); |
| 5957 | } |
| 5958 | #endif |
| 5959 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5960 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5961 | Py_FileSystemDefaultEncoding, &file, |
| 5962 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5963 | return NULL; |
| 5964 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5965 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5966 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5967 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5968 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5969 | return posix_error_with_allocated_filename(file); |
| 5970 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5971 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5972 | } |
| 5973 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5974 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5975 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5976 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5977 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5978 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5979 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5980 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5981 | { |
| 5982 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5983 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5984 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5985 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5986 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5987 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5988 | if (res < 0) |
| 5989 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5990 | Py_INCREF(Py_None); |
| 5991 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5992 | } |
| 5993 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5994 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5995 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5996 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5997 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5998 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5999 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6000 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6001 | { |
| 6002 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6003 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6004 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6005 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6006 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6007 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6008 | if (fd < 0) |
| 6009 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6010 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6011 | } |
| 6012 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6013 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6014 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 6015 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6016 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6017 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6018 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6019 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6020 | { |
| 6021 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6022 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6023 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6024 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6025 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6026 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6027 | if (res < 0) |
| 6028 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6029 | Py_INCREF(Py_None); |
| 6030 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6031 | } |
| 6032 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6033 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6034 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6035 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6036 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6037 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6038 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6039 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6040 | { |
| 6041 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6042 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6043 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6044 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6045 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6046 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6047 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6048 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6049 | return NULL; |
| 6050 | #ifdef SEEK_SET |
| 6051 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 6052 | switch (how) { |
| 6053 | case 0: how = SEEK_SET; break; |
| 6054 | case 1: how = SEEK_CUR; break; |
| 6055 | case 2: how = SEEK_END; break; |
| 6056 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6057 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6058 | |
| 6059 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6060 | pos = PyInt_AsLong(posobj); |
| 6061 | #else |
| 6062 | pos = PyLong_Check(posobj) ? |
| 6063 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 6064 | #endif |
| 6065 | if (PyErr_Occurred()) |
| 6066 | return NULL; |
| 6067 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6068 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6069 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6070 | res = _lseeki64(fd, pos, how); |
| 6071 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6072 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6073 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6074 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6075 | if (res < 0) |
| 6076 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6077 | |
| 6078 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6079 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6080 | #else |
| 6081 | return PyLong_FromLongLong(res); |
| 6082 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6083 | } |
| 6084 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6085 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6086 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6087 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6088 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6089 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6090 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6091 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6092 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 6093 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6094 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6095 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6096 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 6097 | if (size < 0) { |
| 6098 | errno = EINVAL; |
| 6099 | return posix_error(); |
| 6100 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6101 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6102 | if (buffer == NULL) |
| 6103 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6104 | Py_BEGIN_ALLOW_THREADS |
| 6105 | n = read(fd, PyString_AsString(buffer), size); |
| 6106 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 6107 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6108 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6109 | return posix_error(); |
| 6110 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 6111 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6112 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6113 | return buffer; |
| 6114 | } |
| 6115 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6116 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6117 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6118 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6119 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6120 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6121 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6122 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6123 | { |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 6124 | int fd; |
| 6125 | Py_ssize_t size; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6126 | char *buffer; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 6127 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6128 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6129 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6130 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 6131 | size = write(fd, buffer, (size_t)size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6132 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6133 | if (size < 0) |
| 6134 | return posix_error(); |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 6135 | return PyInt_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6136 | } |
| 6137 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6138 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6139 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6140 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6141 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6142 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6143 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6144 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6145 | { |
| 6146 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6147 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6148 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6149 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6150 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 6151 | #ifdef __VMS |
| 6152 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 6153 | fsync(fd); |
| 6154 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6155 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6156 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6157 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6158 | if (res != 0) { |
| 6159 | #ifdef MS_WINDOWS |
| 6160 | return win32_error("fstat", NULL); |
| 6161 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6162 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6163 | #endif |
| 6164 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6165 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6166 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6167 | } |
| 6168 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6169 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6170 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6171 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6172 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6173 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6174 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6175 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6176 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6177 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 6178 | char *mode = "r"; |
| 6179 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6180 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6181 | PyObject *f; |
| 6182 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6183 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 6184 | |
Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 6185 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 6186 | PyErr_Format(PyExc_ValueError, |
| 6187 | "invalid file mode '%s'", mode); |
| 6188 | return NULL; |
| 6189 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6190 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 6191 | #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H) |
Georg Brandl | 54a188a | 2006-03-31 20:00:11 +0000 | [diff] [blame] | 6192 | if (mode[0] == 'a') { |
| 6193 | /* try to make sure the O_APPEND flag is set */ |
| 6194 | int flags; |
| 6195 | flags = fcntl(fd, F_GETFL); |
| 6196 | if (flags != -1) |
| 6197 | fcntl(fd, F_SETFL, flags | O_APPEND); |
| 6198 | fp = fdopen(fd, mode); |
Thomas Wouters | 2a9a6b0 | 2006-03-31 22:38:19 +0000 | [diff] [blame] | 6199 | if (fp == NULL && flags != -1) |
Georg Brandl | 54a188a | 2006-03-31 20:00:11 +0000 | [diff] [blame] | 6200 | /* restore old mode if fdopen failed */ |
| 6201 | fcntl(fd, F_SETFL, flags); |
| 6202 | } else { |
| 6203 | fp = fdopen(fd, mode); |
| 6204 | } |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 6205 | #else |
| 6206 | fp = fdopen(fd, mode); |
| 6207 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6208 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6209 | if (fp == NULL) |
| 6210 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 6211 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 6212 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6213 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 6214 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6215 | } |
| 6216 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6217 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6218 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6219 | 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] | 6220 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6221 | |
| 6222 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 6223 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6224 | { |
| 6225 | int fd; |
| 6226 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 6227 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6228 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6229 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6230 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6231 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6232 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6233 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6234 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6235 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6236 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6237 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6238 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6239 | #if defined(PYOS_OS2) |
| 6240 | HFILE read, write; |
| 6241 | APIRET rc; |
| 6242 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6243 | Py_BEGIN_ALLOW_THREADS |
| 6244 | rc = DosCreatePipe( &read, &write, 4096); |
| 6245 | Py_END_ALLOW_THREADS |
| 6246 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6247 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6248 | |
| 6249 | return Py_BuildValue("(ii)", read, write); |
| 6250 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6251 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6252 | int fds[2]; |
| 6253 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6254 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6255 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6256 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6257 | if (res != 0) |
| 6258 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6259 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6260 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 6261 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 6262 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 6263 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6264 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 6265 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6266 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 6267 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 6268 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 6269 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 6270 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 6271 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6272 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6273 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6274 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6275 | #endif /* HAVE_PIPE */ |
| 6276 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6277 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6278 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6279 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6280 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6281 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6282 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6283 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6284 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6285 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6286 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6287 | int mode = 0666; |
| 6288 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6289 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6290 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6291 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6292 | res = mkfifo(filename, mode); |
| 6293 | Py_END_ALLOW_THREADS |
| 6294 | if (res < 0) |
| 6295 | return posix_error(); |
| 6296 | Py_INCREF(Py_None); |
| 6297 | return Py_None; |
| 6298 | } |
| 6299 | #endif |
| 6300 | |
| 6301 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6302 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6303 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6304 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6305 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 6306 | named filename. mode specifies both the permissions to use and the\n\ |
| 6307 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 6308 | 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] | 6309 | device defines the newly created device special file (probably using\n\ |
| 6310 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6311 | |
| 6312 | |
| 6313 | static PyObject * |
| 6314 | posix_mknod(PyObject *self, PyObject *args) |
| 6315 | { |
| 6316 | char *filename; |
| 6317 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6318 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6319 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 6320 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6321 | return NULL; |
| 6322 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6323 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6324 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6325 | if (res < 0) |
| 6326 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6327 | Py_INCREF(Py_None); |
| 6328 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6329 | } |
| 6330 | #endif |
| 6331 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6332 | #ifdef HAVE_DEVICE_MACROS |
| 6333 | PyDoc_STRVAR(posix_major__doc__, |
| 6334 | "major(device) -> major number\n\ |
| 6335 | Extracts a device major number from a raw device number."); |
| 6336 | |
| 6337 | static PyObject * |
| 6338 | posix_major(PyObject *self, PyObject *args) |
| 6339 | { |
| 6340 | int device; |
| 6341 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 6342 | return NULL; |
| 6343 | return PyInt_FromLong((long)major(device)); |
| 6344 | } |
| 6345 | |
| 6346 | PyDoc_STRVAR(posix_minor__doc__, |
| 6347 | "minor(device) -> minor number\n\ |
| 6348 | Extracts a device minor number from a raw device number."); |
| 6349 | |
| 6350 | static PyObject * |
| 6351 | posix_minor(PyObject *self, PyObject *args) |
| 6352 | { |
| 6353 | int device; |
| 6354 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 6355 | return NULL; |
| 6356 | return PyInt_FromLong((long)minor(device)); |
| 6357 | } |
| 6358 | |
| 6359 | PyDoc_STRVAR(posix_makedev__doc__, |
| 6360 | "makedev(major, minor) -> device number\n\ |
| 6361 | Composes a raw device number from the major and minor device numbers."); |
| 6362 | |
| 6363 | static PyObject * |
| 6364 | posix_makedev(PyObject *self, PyObject *args) |
| 6365 | { |
| 6366 | int major, minor; |
| 6367 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 6368 | return NULL; |
| 6369 | return PyInt_FromLong((long)makedev(major, minor)); |
| 6370 | } |
| 6371 | #endif /* device macros */ |
| 6372 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6373 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6374 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6375 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6376 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6377 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6378 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6379 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6380 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6381 | { |
| 6382 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6383 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6384 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6385 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6386 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6387 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6388 | return NULL; |
| 6389 | |
| 6390 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6391 | length = PyInt_AsLong(lenobj); |
| 6392 | #else |
| 6393 | length = PyLong_Check(lenobj) ? |
| 6394 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 6395 | #endif |
| 6396 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6397 | return NULL; |
| 6398 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6399 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6400 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6401 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6402 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6403 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6404 | return NULL; |
| 6405 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6406 | Py_INCREF(Py_None); |
| 6407 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6408 | } |
| 6409 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6410 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6411 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6412 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6413 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6414 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6415 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6416 | /* Save putenv() parameters as values here, so we can collect them when they |
| 6417 | * get re-set with another call for the same key. */ |
| 6418 | static PyObject *posix_putenv_garbage; |
| 6419 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6420 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6421 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6422 | { |
| 6423 | char *s1, *s2; |
Anthony Baxter | 64182fe | 2006-04-11 12:14:09 +0000 | [diff] [blame] | 6424 | char *newenv; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6425 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 6426 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6427 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6428 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6429 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6430 | |
| 6431 | #if defined(PYOS_OS2) |
| 6432 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 6433 | APIRET rc; |
| 6434 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6435 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 6436 | if (rc != NO_ERROR) |
| 6437 | return os2_error(rc); |
| 6438 | |
| 6439 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 6440 | APIRET rc; |
| 6441 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6442 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 6443 | if (rc != NO_ERROR) |
| 6444 | return os2_error(rc); |
| 6445 | } else { |
| 6446 | #endif |
| 6447 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6448 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 6449 | len = strlen(s1) + strlen(s2) + 2; |
| 6450 | /* len includes space for a trailing \0; the size arg to |
| 6451 | PyString_FromStringAndSize does not count that */ |
| 6452 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6453 | if (newstr == NULL) |
| 6454 | return PyErr_NoMemory(); |
Anthony Baxter | 64182fe | 2006-04-11 12:14:09 +0000 | [diff] [blame] | 6455 | newenv = PyString_AS_STRING(newstr); |
| 6456 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 6457 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 6458 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6459 | posix_error(); |
| 6460 | return NULL; |
| 6461 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6462 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 6463 | * this will cause previous value to be collected. This has to |
| 6464 | * happen after the real putenv() call because the old value |
| 6465 | * was still accessible until then. */ |
| 6466 | if (PyDict_SetItem(posix_putenv_garbage, |
| 6467 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 6468 | /* really not much we can do; just leak */ |
| 6469 | PyErr_Clear(); |
| 6470 | } |
| 6471 | else { |
| 6472 | Py_DECREF(newstr); |
| 6473 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6474 | |
| 6475 | #if defined(PYOS_OS2) |
| 6476 | } |
| 6477 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6478 | Py_INCREF(Py_None); |
| 6479 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6480 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6481 | #endif /* putenv */ |
| 6482 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6483 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6484 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6485 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6486 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6487 | |
| 6488 | static PyObject * |
| 6489 | posix_unsetenv(PyObject *self, PyObject *args) |
| 6490 | { |
| 6491 | char *s1; |
| 6492 | |
| 6493 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 6494 | return NULL; |
| 6495 | |
| 6496 | unsetenv(s1); |
| 6497 | |
| 6498 | /* Remove the key from posix_putenv_garbage; |
| 6499 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6500 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6501 | * old value was still accessible until then. |
| 6502 | */ |
| 6503 | if (PyDict_DelItem(posix_putenv_garbage, |
| 6504 | PyTuple_GET_ITEM(args, 0))) { |
| 6505 | /* really not much we can do; just leak */ |
| 6506 | PyErr_Clear(); |
| 6507 | } |
| 6508 | |
| 6509 | Py_INCREF(Py_None); |
| 6510 | return Py_None; |
| 6511 | } |
| 6512 | #endif /* unsetenv */ |
| 6513 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6514 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6515 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6516 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6517 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6518 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6519 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6520 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6521 | { |
| 6522 | int code; |
| 6523 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6524 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6525 | return NULL; |
| 6526 | message = strerror(code); |
| 6527 | if (message == NULL) { |
| 6528 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 6529 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6530 | return NULL; |
| 6531 | } |
| 6532 | return PyString_FromString(message); |
| 6533 | } |
| 6534 | #endif /* strerror */ |
| 6535 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6536 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6537 | #ifdef HAVE_SYS_WAIT_H |
| 6538 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6539 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6540 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6541 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6542 | 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] | 6543 | |
| 6544 | static PyObject * |
| 6545 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 6546 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6547 | WAIT_TYPE status; |
| 6548 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6549 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6550 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6551 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6552 | |
| 6553 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6554 | } |
| 6555 | #endif /* WCOREDUMP */ |
| 6556 | |
| 6557 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6558 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6559 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6560 | 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] | 6561 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6562 | |
| 6563 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6564 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6565 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6566 | WAIT_TYPE status; |
| 6567 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6568 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6569 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6570 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6571 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6572 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6573 | } |
| 6574 | #endif /* WIFCONTINUED */ |
| 6575 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6576 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6577 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6578 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6579 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6580 | |
| 6581 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6582 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6583 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6584 | WAIT_TYPE status; |
| 6585 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6586 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6587 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6588 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6589 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6590 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6591 | } |
| 6592 | #endif /* WIFSTOPPED */ |
| 6593 | |
| 6594 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6595 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6596 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6597 | 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] | 6598 | |
| 6599 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6600 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6601 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6602 | WAIT_TYPE status; |
| 6603 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6604 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6605 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6606 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6607 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6608 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6609 | } |
| 6610 | #endif /* WIFSIGNALED */ |
| 6611 | |
| 6612 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6613 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6614 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6615 | 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] | 6616 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6617 | |
| 6618 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6619 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6620 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6621 | WAIT_TYPE status; |
| 6622 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6623 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6624 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6625 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6626 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6627 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6628 | } |
| 6629 | #endif /* WIFEXITED */ |
| 6630 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6631 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6632 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6633 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6634 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6635 | |
| 6636 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6637 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6638 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6639 | WAIT_TYPE status; |
| 6640 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6641 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6642 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6643 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6644 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6645 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 6646 | } |
| 6647 | #endif /* WEXITSTATUS */ |
| 6648 | |
| 6649 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6650 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6651 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6652 | 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] | 6653 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6654 | |
| 6655 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6656 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6657 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6658 | WAIT_TYPE status; |
| 6659 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6660 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6661 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6662 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6663 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6664 | return Py_BuildValue("i", WTERMSIG(status)); |
| 6665 | } |
| 6666 | #endif /* WTERMSIG */ |
| 6667 | |
| 6668 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6669 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6670 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6671 | Return the signal that stopped the process that provided\n\ |
| 6672 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6673 | |
| 6674 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6675 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6676 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6677 | WAIT_TYPE status; |
| 6678 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6679 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6680 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6681 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6682 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6683 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 6684 | } |
| 6685 | #endif /* WSTOPSIG */ |
| 6686 | |
| 6687 | #endif /* HAVE_SYS_WAIT_H */ |
| 6688 | |
| 6689 | |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 6690 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 6691 | #ifdef _SCO_DS |
| 6692 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 6693 | needed definitions in sys/statvfs.h */ |
| 6694 | #define _SVID3 |
| 6695 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6696 | #include <sys/statvfs.h> |
| 6697 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6698 | static PyObject* |
| 6699 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 6700 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 6701 | if (v == NULL) |
| 6702 | return NULL; |
| 6703 | |
| 6704 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6705 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6706 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 6707 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 6708 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 6709 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 6710 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 6711 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 6712 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 6713 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6714 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6715 | #else |
| 6716 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6717 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6718 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6719 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6720 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6721 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6722 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6723 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6724 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6725 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6726 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6727 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6728 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6729 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6730 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6731 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6732 | #endif |
| 6733 | |
| 6734 | return v; |
| 6735 | } |
| 6736 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6737 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6738 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6739 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6740 | |
| 6741 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6742 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6743 | { |
| 6744 | int fd, res; |
| 6745 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6746 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6747 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6748 | return NULL; |
| 6749 | Py_BEGIN_ALLOW_THREADS |
| 6750 | res = fstatvfs(fd, &st); |
| 6751 | Py_END_ALLOW_THREADS |
| 6752 | if (res != 0) |
| 6753 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6754 | |
| 6755 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6756 | } |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 6757 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6758 | |
| 6759 | |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 6760 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6761 | #include <sys/statvfs.h> |
| 6762 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6763 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6764 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6765 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6766 | |
| 6767 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6768 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6769 | { |
| 6770 | char *path; |
| 6771 | int res; |
| 6772 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6773 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6774 | return NULL; |
| 6775 | Py_BEGIN_ALLOW_THREADS |
| 6776 | res = statvfs(path, &st); |
| 6777 | Py_END_ALLOW_THREADS |
| 6778 | if (res != 0) |
| 6779 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6780 | |
| 6781 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6782 | } |
| 6783 | #endif /* HAVE_STATVFS */ |
| 6784 | |
| 6785 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6786 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6787 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6788 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6789 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 6790 | 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] | 6791 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6792 | |
| 6793 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6794 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6795 | { |
| 6796 | PyObject *result = NULL; |
| 6797 | char *dir = NULL; |
| 6798 | char *pfx = NULL; |
| 6799 | char *name; |
| 6800 | |
| 6801 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 6802 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6803 | |
| 6804 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6805 | "tempnam is a potential security risk to your program") < 0) |
| 6806 | return NULL; |
| 6807 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6808 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6809 | name = _tempnam(dir, pfx); |
| 6810 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6811 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6812 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6813 | if (name == NULL) |
| 6814 | return PyErr_NoMemory(); |
| 6815 | result = PyString_FromString(name); |
| 6816 | free(name); |
| 6817 | return result; |
| 6818 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 6819 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6820 | |
| 6821 | |
| 6822 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6823 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6824 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6825 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6826 | |
| 6827 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6828 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6829 | { |
| 6830 | FILE *fp; |
| 6831 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6832 | fp = tmpfile(); |
| 6833 | if (fp == NULL) |
| 6834 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 6835 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6836 | } |
| 6837 | #endif |
| 6838 | |
| 6839 | |
| 6840 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6841 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6842 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6843 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6844 | |
| 6845 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6846 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6847 | { |
| 6848 | char buffer[L_tmpnam]; |
| 6849 | char *name; |
| 6850 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6851 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6852 | "tmpnam is a potential security risk to your program") < 0) |
| 6853 | return NULL; |
| 6854 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6855 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6856 | name = tmpnam_r(buffer); |
| 6857 | #else |
| 6858 | name = tmpnam(buffer); |
| 6859 | #endif |
| 6860 | if (name == NULL) { |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 6861 | PyObject *err = Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6862 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6863 | "unexpected NULL from tmpnam_r" |
| 6864 | #else |
| 6865 | "unexpected NULL from tmpnam" |
| 6866 | #endif |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 6867 | ); |
| 6868 | PyErr_SetObject(PyExc_OSError, err); |
| 6869 | Py_XDECREF(err); |
| 6870 | return NULL; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6871 | } |
| 6872 | return PyString_FromString(buffer); |
| 6873 | } |
| 6874 | #endif |
| 6875 | |
| 6876 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6877 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 6878 | * It maps strings representing configuration variable names to |
| 6879 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6880 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6881 | * rarely-used constants. There are three separate tables that use |
| 6882 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6883 | * |
| 6884 | * This code is always included, even if none of the interfaces that |
| 6885 | * need it are included. The #if hackery needed to avoid it would be |
| 6886 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6887 | */ |
| 6888 | struct constdef { |
| 6889 | char *name; |
| 6890 | long value; |
| 6891 | }; |
| 6892 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6893 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6894 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 6895 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6896 | { |
| 6897 | if (PyInt_Check(arg)) { |
| 6898 | *valuep = PyInt_AS_LONG(arg); |
| 6899 | return 1; |
| 6900 | } |
| 6901 | if (PyString_Check(arg)) { |
| 6902 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6903 | size_t lo = 0; |
| 6904 | size_t mid; |
| 6905 | size_t hi = tablesize; |
| 6906 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6907 | char *confname = PyString_AS_STRING(arg); |
| 6908 | while (lo < hi) { |
| 6909 | mid = (lo + hi) / 2; |
| 6910 | cmp = strcmp(confname, table[mid].name); |
| 6911 | if (cmp < 0) |
| 6912 | hi = mid; |
| 6913 | else if (cmp > 0) |
| 6914 | lo = mid + 1; |
| 6915 | else { |
| 6916 | *valuep = table[mid].value; |
| 6917 | return 1; |
| 6918 | } |
| 6919 | } |
| 6920 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 6921 | } |
| 6922 | else |
| 6923 | PyErr_SetString(PyExc_TypeError, |
| 6924 | "configuration names must be strings or integers"); |
| 6925 | return 0; |
| 6926 | } |
| 6927 | |
| 6928 | |
| 6929 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 6930 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6931 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 6932 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 6933 | #endif |
| 6934 | #ifdef _PC_ABI_ASYNC_IO |
| 6935 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 6936 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6937 | #ifdef _PC_ASYNC_IO |
| 6938 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 6939 | #endif |
| 6940 | #ifdef _PC_CHOWN_RESTRICTED |
| 6941 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 6942 | #endif |
| 6943 | #ifdef _PC_FILESIZEBITS |
| 6944 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 6945 | #endif |
| 6946 | #ifdef _PC_LAST |
| 6947 | {"PC_LAST", _PC_LAST}, |
| 6948 | #endif |
| 6949 | #ifdef _PC_LINK_MAX |
| 6950 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 6951 | #endif |
| 6952 | #ifdef _PC_MAX_CANON |
| 6953 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 6954 | #endif |
| 6955 | #ifdef _PC_MAX_INPUT |
| 6956 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 6957 | #endif |
| 6958 | #ifdef _PC_NAME_MAX |
| 6959 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 6960 | #endif |
| 6961 | #ifdef _PC_NO_TRUNC |
| 6962 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 6963 | #endif |
| 6964 | #ifdef _PC_PATH_MAX |
| 6965 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 6966 | #endif |
| 6967 | #ifdef _PC_PIPE_BUF |
| 6968 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 6969 | #endif |
| 6970 | #ifdef _PC_PRIO_IO |
| 6971 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 6972 | #endif |
| 6973 | #ifdef _PC_SOCK_MAXBUF |
| 6974 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 6975 | #endif |
| 6976 | #ifdef _PC_SYNC_IO |
| 6977 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 6978 | #endif |
| 6979 | #ifdef _PC_VDISABLE |
| 6980 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 6981 | #endif |
| 6982 | }; |
| 6983 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6984 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6985 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6986 | { |
| 6987 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 6988 | sizeof(posix_constants_pathconf) |
| 6989 | / sizeof(struct constdef)); |
| 6990 | } |
| 6991 | #endif |
| 6992 | |
| 6993 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6994 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6995 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6996 | 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] | 6997 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6998 | |
| 6999 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7000 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7001 | { |
| 7002 | PyObject *result = NULL; |
| 7003 | int name, fd; |
| 7004 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7005 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 7006 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7007 | long limit; |
| 7008 | |
| 7009 | errno = 0; |
| 7010 | limit = fpathconf(fd, name); |
| 7011 | if (limit == -1 && errno != 0) |
| 7012 | posix_error(); |
| 7013 | else |
| 7014 | result = PyInt_FromLong(limit); |
| 7015 | } |
| 7016 | return result; |
| 7017 | } |
| 7018 | #endif |
| 7019 | |
| 7020 | |
| 7021 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7022 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7023 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7024 | 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] | 7025 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7026 | |
| 7027 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7028 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7029 | { |
| 7030 | PyObject *result = NULL; |
| 7031 | int name; |
| 7032 | char *path; |
| 7033 | |
| 7034 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 7035 | conv_path_confname, &name)) { |
| 7036 | long limit; |
| 7037 | |
| 7038 | errno = 0; |
| 7039 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7040 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7041 | if (errno == EINVAL) |
| 7042 | /* could be a path or name problem */ |
| 7043 | posix_error(); |
| 7044 | else |
| 7045 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7046 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7047 | else |
| 7048 | result = PyInt_FromLong(limit); |
| 7049 | } |
| 7050 | return result; |
| 7051 | } |
| 7052 | #endif |
| 7053 | |
| 7054 | #ifdef HAVE_CONFSTR |
| 7055 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7056 | #ifdef _CS_ARCHITECTURE |
| 7057 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 7058 | #endif |
| 7059 | #ifdef _CS_HOSTNAME |
| 7060 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 7061 | #endif |
| 7062 | #ifdef _CS_HW_PROVIDER |
| 7063 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 7064 | #endif |
| 7065 | #ifdef _CS_HW_SERIAL |
| 7066 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 7067 | #endif |
| 7068 | #ifdef _CS_INITTAB_NAME |
| 7069 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 7070 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7071 | #ifdef _CS_LFS64_CFLAGS |
| 7072 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 7073 | #endif |
| 7074 | #ifdef _CS_LFS64_LDFLAGS |
| 7075 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 7076 | #endif |
| 7077 | #ifdef _CS_LFS64_LIBS |
| 7078 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 7079 | #endif |
| 7080 | #ifdef _CS_LFS64_LINTFLAGS |
| 7081 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 7082 | #endif |
| 7083 | #ifdef _CS_LFS_CFLAGS |
| 7084 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 7085 | #endif |
| 7086 | #ifdef _CS_LFS_LDFLAGS |
| 7087 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 7088 | #endif |
| 7089 | #ifdef _CS_LFS_LIBS |
| 7090 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 7091 | #endif |
| 7092 | #ifdef _CS_LFS_LINTFLAGS |
| 7093 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 7094 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7095 | #ifdef _CS_MACHINE |
| 7096 | {"CS_MACHINE", _CS_MACHINE}, |
| 7097 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7098 | #ifdef _CS_PATH |
| 7099 | {"CS_PATH", _CS_PATH}, |
| 7100 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7101 | #ifdef _CS_RELEASE |
| 7102 | {"CS_RELEASE", _CS_RELEASE}, |
| 7103 | #endif |
| 7104 | #ifdef _CS_SRPC_DOMAIN |
| 7105 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 7106 | #endif |
| 7107 | #ifdef _CS_SYSNAME |
| 7108 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 7109 | #endif |
| 7110 | #ifdef _CS_VERSION |
| 7111 | {"CS_VERSION", _CS_VERSION}, |
| 7112 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7113 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 7114 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 7115 | #endif |
| 7116 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 7117 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 7118 | #endif |
| 7119 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 7120 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 7121 | #endif |
| 7122 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 7123 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 7124 | #endif |
| 7125 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 7126 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 7127 | #endif |
| 7128 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 7129 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 7130 | #endif |
| 7131 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 7132 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 7133 | #endif |
| 7134 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 7135 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 7136 | #endif |
| 7137 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 7138 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 7139 | #endif |
| 7140 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 7141 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 7142 | #endif |
| 7143 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 7144 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 7145 | #endif |
| 7146 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 7147 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 7148 | #endif |
| 7149 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 7150 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 7151 | #endif |
| 7152 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 7153 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 7154 | #endif |
| 7155 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 7156 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 7157 | #endif |
| 7158 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 7159 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 7160 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7161 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 7162 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 7163 | #endif |
| 7164 | #ifdef _MIPS_CS_BASE |
| 7165 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 7166 | #endif |
| 7167 | #ifdef _MIPS_CS_HOSTID |
| 7168 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 7169 | #endif |
| 7170 | #ifdef _MIPS_CS_HW_NAME |
| 7171 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 7172 | #endif |
| 7173 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 7174 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 7175 | #endif |
| 7176 | #ifdef _MIPS_CS_OSREL_MAJ |
| 7177 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 7178 | #endif |
| 7179 | #ifdef _MIPS_CS_OSREL_MIN |
| 7180 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 7181 | #endif |
| 7182 | #ifdef _MIPS_CS_OSREL_PATCH |
| 7183 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 7184 | #endif |
| 7185 | #ifdef _MIPS_CS_OS_NAME |
| 7186 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 7187 | #endif |
| 7188 | #ifdef _MIPS_CS_OS_PROVIDER |
| 7189 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 7190 | #endif |
| 7191 | #ifdef _MIPS_CS_PROCESSORS |
| 7192 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 7193 | #endif |
| 7194 | #ifdef _MIPS_CS_SERIAL |
| 7195 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 7196 | #endif |
| 7197 | #ifdef _MIPS_CS_VENDOR |
| 7198 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 7199 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7200 | }; |
| 7201 | |
| 7202 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7203 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7204 | { |
| 7205 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 7206 | sizeof(posix_constants_confstr) |
| 7207 | / sizeof(struct constdef)); |
| 7208 | } |
| 7209 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7210 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7211 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7212 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7213 | |
| 7214 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7215 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7216 | { |
| 7217 | PyObject *result = NULL; |
| 7218 | int name; |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 7219 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7220 | |
| 7221 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Skip Montanaro | dd527fc | 2006-04-18 00:49:49 +0000 | [diff] [blame] | 7222 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7223 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7224 | errno = 0; |
Skip Montanaro | dd527fc | 2006-04-18 00:49:49 +0000 | [diff] [blame] | 7225 | len = confstr(name, buffer, sizeof(buffer)); |
Skip Montanaro | 94785ef | 2006-04-20 01:29:48 +0000 | [diff] [blame] | 7226 | if (len == 0) { |
| 7227 | if (errno) { |
| 7228 | posix_error(); |
| 7229 | } |
| 7230 | else { |
| 7231 | result = Py_None; |
| 7232 | Py_INCREF(Py_None); |
| 7233 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7234 | } |
| 7235 | else { |
Neal Norwitz | 0d21b1e | 2006-04-20 06:44:42 +0000 | [diff] [blame] | 7236 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 7237 | result = PyString_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7238 | if (result != NULL) |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 7239 | confstr(name, PyString_AS_STRING(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7240 | } |
| 7241 | else |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 7242 | result = PyString_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7243 | } |
| 7244 | } |
| 7245 | return result; |
| 7246 | } |
| 7247 | #endif |
| 7248 | |
| 7249 | |
| 7250 | #ifdef HAVE_SYSCONF |
| 7251 | static struct constdef posix_constants_sysconf[] = { |
| 7252 | #ifdef _SC_2_CHAR_TERM |
| 7253 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 7254 | #endif |
| 7255 | #ifdef _SC_2_C_BIND |
| 7256 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 7257 | #endif |
| 7258 | #ifdef _SC_2_C_DEV |
| 7259 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 7260 | #endif |
| 7261 | #ifdef _SC_2_C_VERSION |
| 7262 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 7263 | #endif |
| 7264 | #ifdef _SC_2_FORT_DEV |
| 7265 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 7266 | #endif |
| 7267 | #ifdef _SC_2_FORT_RUN |
| 7268 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 7269 | #endif |
| 7270 | #ifdef _SC_2_LOCALEDEF |
| 7271 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 7272 | #endif |
| 7273 | #ifdef _SC_2_SW_DEV |
| 7274 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 7275 | #endif |
| 7276 | #ifdef _SC_2_UPE |
| 7277 | {"SC_2_UPE", _SC_2_UPE}, |
| 7278 | #endif |
| 7279 | #ifdef _SC_2_VERSION |
| 7280 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 7281 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7282 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 7283 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 7284 | #endif |
| 7285 | #ifdef _SC_ACL |
| 7286 | {"SC_ACL", _SC_ACL}, |
| 7287 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7288 | #ifdef _SC_AIO_LISTIO_MAX |
| 7289 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 7290 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7291 | #ifdef _SC_AIO_MAX |
| 7292 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 7293 | #endif |
| 7294 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 7295 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 7296 | #endif |
| 7297 | #ifdef _SC_ARG_MAX |
| 7298 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 7299 | #endif |
| 7300 | #ifdef _SC_ASYNCHRONOUS_IO |
| 7301 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 7302 | #endif |
| 7303 | #ifdef _SC_ATEXIT_MAX |
| 7304 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 7305 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7306 | #ifdef _SC_AUDIT |
| 7307 | {"SC_AUDIT", _SC_AUDIT}, |
| 7308 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7309 | #ifdef _SC_AVPHYS_PAGES |
| 7310 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 7311 | #endif |
| 7312 | #ifdef _SC_BC_BASE_MAX |
| 7313 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 7314 | #endif |
| 7315 | #ifdef _SC_BC_DIM_MAX |
| 7316 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 7317 | #endif |
| 7318 | #ifdef _SC_BC_SCALE_MAX |
| 7319 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 7320 | #endif |
| 7321 | #ifdef _SC_BC_STRING_MAX |
| 7322 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 7323 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7324 | #ifdef _SC_CAP |
| 7325 | {"SC_CAP", _SC_CAP}, |
| 7326 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7327 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 7328 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 7329 | #endif |
| 7330 | #ifdef _SC_CHAR_BIT |
| 7331 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 7332 | #endif |
| 7333 | #ifdef _SC_CHAR_MAX |
| 7334 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 7335 | #endif |
| 7336 | #ifdef _SC_CHAR_MIN |
| 7337 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 7338 | #endif |
| 7339 | #ifdef _SC_CHILD_MAX |
| 7340 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 7341 | #endif |
| 7342 | #ifdef _SC_CLK_TCK |
| 7343 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 7344 | #endif |
| 7345 | #ifdef _SC_COHER_BLKSZ |
| 7346 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 7347 | #endif |
| 7348 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 7349 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 7350 | #endif |
| 7351 | #ifdef _SC_DCACHE_ASSOC |
| 7352 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 7353 | #endif |
| 7354 | #ifdef _SC_DCACHE_BLKSZ |
| 7355 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 7356 | #endif |
| 7357 | #ifdef _SC_DCACHE_LINESZ |
| 7358 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 7359 | #endif |
| 7360 | #ifdef _SC_DCACHE_SZ |
| 7361 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 7362 | #endif |
| 7363 | #ifdef _SC_DCACHE_TBLKSZ |
| 7364 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 7365 | #endif |
| 7366 | #ifdef _SC_DELAYTIMER_MAX |
| 7367 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 7368 | #endif |
| 7369 | #ifdef _SC_EQUIV_CLASS_MAX |
| 7370 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 7371 | #endif |
| 7372 | #ifdef _SC_EXPR_NEST_MAX |
| 7373 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 7374 | #endif |
| 7375 | #ifdef _SC_FSYNC |
| 7376 | {"SC_FSYNC", _SC_FSYNC}, |
| 7377 | #endif |
| 7378 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 7379 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 7380 | #endif |
| 7381 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 7382 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 7383 | #endif |
| 7384 | #ifdef _SC_ICACHE_ASSOC |
| 7385 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 7386 | #endif |
| 7387 | #ifdef _SC_ICACHE_BLKSZ |
| 7388 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 7389 | #endif |
| 7390 | #ifdef _SC_ICACHE_LINESZ |
| 7391 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 7392 | #endif |
| 7393 | #ifdef _SC_ICACHE_SZ |
| 7394 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 7395 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7396 | #ifdef _SC_INF |
| 7397 | {"SC_INF", _SC_INF}, |
| 7398 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7399 | #ifdef _SC_INT_MAX |
| 7400 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 7401 | #endif |
| 7402 | #ifdef _SC_INT_MIN |
| 7403 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 7404 | #endif |
| 7405 | #ifdef _SC_IOV_MAX |
| 7406 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 7407 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7408 | #ifdef _SC_IP_SECOPTS |
| 7409 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 7410 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7411 | #ifdef _SC_JOB_CONTROL |
| 7412 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 7413 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7414 | #ifdef _SC_KERN_POINTERS |
| 7415 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 7416 | #endif |
| 7417 | #ifdef _SC_KERN_SIM |
| 7418 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 7419 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7420 | #ifdef _SC_LINE_MAX |
| 7421 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 7422 | #endif |
| 7423 | #ifdef _SC_LOGIN_NAME_MAX |
| 7424 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 7425 | #endif |
| 7426 | #ifdef _SC_LOGNAME_MAX |
| 7427 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 7428 | #endif |
| 7429 | #ifdef _SC_LONG_BIT |
| 7430 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 7431 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7432 | #ifdef _SC_MAC |
| 7433 | {"SC_MAC", _SC_MAC}, |
| 7434 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7435 | #ifdef _SC_MAPPED_FILES |
| 7436 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 7437 | #endif |
| 7438 | #ifdef _SC_MAXPID |
| 7439 | {"SC_MAXPID", _SC_MAXPID}, |
| 7440 | #endif |
| 7441 | #ifdef _SC_MB_LEN_MAX |
| 7442 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 7443 | #endif |
| 7444 | #ifdef _SC_MEMLOCK |
| 7445 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 7446 | #endif |
| 7447 | #ifdef _SC_MEMLOCK_RANGE |
| 7448 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 7449 | #endif |
| 7450 | #ifdef _SC_MEMORY_PROTECTION |
| 7451 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 7452 | #endif |
| 7453 | #ifdef _SC_MESSAGE_PASSING |
| 7454 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 7455 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7456 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 7457 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 7458 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7459 | #ifdef _SC_MQ_OPEN_MAX |
| 7460 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 7461 | #endif |
| 7462 | #ifdef _SC_MQ_PRIO_MAX |
| 7463 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 7464 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7465 | #ifdef _SC_NACLS_MAX |
| 7466 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 7467 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7468 | #ifdef _SC_NGROUPS_MAX |
| 7469 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 7470 | #endif |
| 7471 | #ifdef _SC_NL_ARGMAX |
| 7472 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 7473 | #endif |
| 7474 | #ifdef _SC_NL_LANGMAX |
| 7475 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 7476 | #endif |
| 7477 | #ifdef _SC_NL_MSGMAX |
| 7478 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 7479 | #endif |
| 7480 | #ifdef _SC_NL_NMAX |
| 7481 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 7482 | #endif |
| 7483 | #ifdef _SC_NL_SETMAX |
| 7484 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 7485 | #endif |
| 7486 | #ifdef _SC_NL_TEXTMAX |
| 7487 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 7488 | #endif |
| 7489 | #ifdef _SC_NPROCESSORS_CONF |
| 7490 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 7491 | #endif |
| 7492 | #ifdef _SC_NPROCESSORS_ONLN |
| 7493 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 7494 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7495 | #ifdef _SC_NPROC_CONF |
| 7496 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 7497 | #endif |
| 7498 | #ifdef _SC_NPROC_ONLN |
| 7499 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 7500 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7501 | #ifdef _SC_NZERO |
| 7502 | {"SC_NZERO", _SC_NZERO}, |
| 7503 | #endif |
| 7504 | #ifdef _SC_OPEN_MAX |
| 7505 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 7506 | #endif |
| 7507 | #ifdef _SC_PAGESIZE |
| 7508 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 7509 | #endif |
| 7510 | #ifdef _SC_PAGE_SIZE |
| 7511 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 7512 | #endif |
| 7513 | #ifdef _SC_PASS_MAX |
| 7514 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 7515 | #endif |
| 7516 | #ifdef _SC_PHYS_PAGES |
| 7517 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 7518 | #endif |
| 7519 | #ifdef _SC_PII |
| 7520 | {"SC_PII", _SC_PII}, |
| 7521 | #endif |
| 7522 | #ifdef _SC_PII_INTERNET |
| 7523 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 7524 | #endif |
| 7525 | #ifdef _SC_PII_INTERNET_DGRAM |
| 7526 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 7527 | #endif |
| 7528 | #ifdef _SC_PII_INTERNET_STREAM |
| 7529 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 7530 | #endif |
| 7531 | #ifdef _SC_PII_OSI |
| 7532 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 7533 | #endif |
| 7534 | #ifdef _SC_PII_OSI_CLTS |
| 7535 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 7536 | #endif |
| 7537 | #ifdef _SC_PII_OSI_COTS |
| 7538 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 7539 | #endif |
| 7540 | #ifdef _SC_PII_OSI_M |
| 7541 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 7542 | #endif |
| 7543 | #ifdef _SC_PII_SOCKET |
| 7544 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 7545 | #endif |
| 7546 | #ifdef _SC_PII_XTI |
| 7547 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 7548 | #endif |
| 7549 | #ifdef _SC_POLL |
| 7550 | {"SC_POLL", _SC_POLL}, |
| 7551 | #endif |
| 7552 | #ifdef _SC_PRIORITIZED_IO |
| 7553 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 7554 | #endif |
| 7555 | #ifdef _SC_PRIORITY_SCHEDULING |
| 7556 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 7557 | #endif |
| 7558 | #ifdef _SC_REALTIME_SIGNALS |
| 7559 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 7560 | #endif |
| 7561 | #ifdef _SC_RE_DUP_MAX |
| 7562 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 7563 | #endif |
| 7564 | #ifdef _SC_RTSIG_MAX |
| 7565 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 7566 | #endif |
| 7567 | #ifdef _SC_SAVED_IDS |
| 7568 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 7569 | #endif |
| 7570 | #ifdef _SC_SCHAR_MAX |
| 7571 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 7572 | #endif |
| 7573 | #ifdef _SC_SCHAR_MIN |
| 7574 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 7575 | #endif |
| 7576 | #ifdef _SC_SELECT |
| 7577 | {"SC_SELECT", _SC_SELECT}, |
| 7578 | #endif |
| 7579 | #ifdef _SC_SEMAPHORES |
| 7580 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 7581 | #endif |
| 7582 | #ifdef _SC_SEM_NSEMS_MAX |
| 7583 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 7584 | #endif |
| 7585 | #ifdef _SC_SEM_VALUE_MAX |
| 7586 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 7587 | #endif |
| 7588 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 7589 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 7590 | #endif |
| 7591 | #ifdef _SC_SHRT_MAX |
| 7592 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 7593 | #endif |
| 7594 | #ifdef _SC_SHRT_MIN |
| 7595 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 7596 | #endif |
| 7597 | #ifdef _SC_SIGQUEUE_MAX |
| 7598 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 7599 | #endif |
| 7600 | #ifdef _SC_SIGRT_MAX |
| 7601 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 7602 | #endif |
| 7603 | #ifdef _SC_SIGRT_MIN |
| 7604 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 7605 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7606 | #ifdef _SC_SOFTPOWER |
| 7607 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 7608 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7609 | #ifdef _SC_SPLIT_CACHE |
| 7610 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 7611 | #endif |
| 7612 | #ifdef _SC_SSIZE_MAX |
| 7613 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 7614 | #endif |
| 7615 | #ifdef _SC_STACK_PROT |
| 7616 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 7617 | #endif |
| 7618 | #ifdef _SC_STREAM_MAX |
| 7619 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 7620 | #endif |
| 7621 | #ifdef _SC_SYNCHRONIZED_IO |
| 7622 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 7623 | #endif |
| 7624 | #ifdef _SC_THREADS |
| 7625 | {"SC_THREADS", _SC_THREADS}, |
| 7626 | #endif |
| 7627 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 7628 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 7629 | #endif |
| 7630 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 7631 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 7632 | #endif |
| 7633 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 7634 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 7635 | #endif |
| 7636 | #ifdef _SC_THREAD_KEYS_MAX |
| 7637 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 7638 | #endif |
| 7639 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 7640 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 7641 | #endif |
| 7642 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 7643 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 7644 | #endif |
| 7645 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 7646 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 7647 | #endif |
| 7648 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 7649 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 7650 | #endif |
| 7651 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 7652 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 7653 | #endif |
| 7654 | #ifdef _SC_THREAD_STACK_MIN |
| 7655 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 7656 | #endif |
| 7657 | #ifdef _SC_THREAD_THREADS_MAX |
| 7658 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 7659 | #endif |
| 7660 | #ifdef _SC_TIMERS |
| 7661 | {"SC_TIMERS", _SC_TIMERS}, |
| 7662 | #endif |
| 7663 | #ifdef _SC_TIMER_MAX |
| 7664 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 7665 | #endif |
| 7666 | #ifdef _SC_TTY_NAME_MAX |
| 7667 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 7668 | #endif |
| 7669 | #ifdef _SC_TZNAME_MAX |
| 7670 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 7671 | #endif |
| 7672 | #ifdef _SC_T_IOV_MAX |
| 7673 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 7674 | #endif |
| 7675 | #ifdef _SC_UCHAR_MAX |
| 7676 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 7677 | #endif |
| 7678 | #ifdef _SC_UINT_MAX |
| 7679 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 7680 | #endif |
| 7681 | #ifdef _SC_UIO_MAXIOV |
| 7682 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 7683 | #endif |
| 7684 | #ifdef _SC_ULONG_MAX |
| 7685 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 7686 | #endif |
| 7687 | #ifdef _SC_USHRT_MAX |
| 7688 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 7689 | #endif |
| 7690 | #ifdef _SC_VERSION |
| 7691 | {"SC_VERSION", _SC_VERSION}, |
| 7692 | #endif |
| 7693 | #ifdef _SC_WORD_BIT |
| 7694 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 7695 | #endif |
| 7696 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 7697 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 7698 | #endif |
| 7699 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 7700 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 7701 | #endif |
| 7702 | #ifdef _SC_XBS5_LP64_OFF64 |
| 7703 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 7704 | #endif |
| 7705 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 7706 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 7707 | #endif |
| 7708 | #ifdef _SC_XOPEN_CRYPT |
| 7709 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 7710 | #endif |
| 7711 | #ifdef _SC_XOPEN_ENH_I18N |
| 7712 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 7713 | #endif |
| 7714 | #ifdef _SC_XOPEN_LEGACY |
| 7715 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 7716 | #endif |
| 7717 | #ifdef _SC_XOPEN_REALTIME |
| 7718 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 7719 | #endif |
| 7720 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 7721 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 7722 | #endif |
| 7723 | #ifdef _SC_XOPEN_SHM |
| 7724 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 7725 | #endif |
| 7726 | #ifdef _SC_XOPEN_UNIX |
| 7727 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 7728 | #endif |
| 7729 | #ifdef _SC_XOPEN_VERSION |
| 7730 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 7731 | #endif |
| 7732 | #ifdef _SC_XOPEN_XCU_VERSION |
| 7733 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 7734 | #endif |
| 7735 | #ifdef _SC_XOPEN_XPG2 |
| 7736 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 7737 | #endif |
| 7738 | #ifdef _SC_XOPEN_XPG3 |
| 7739 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 7740 | #endif |
| 7741 | #ifdef _SC_XOPEN_XPG4 |
| 7742 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 7743 | #endif |
| 7744 | }; |
| 7745 | |
| 7746 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7747 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7748 | { |
| 7749 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 7750 | sizeof(posix_constants_sysconf) |
| 7751 | / sizeof(struct constdef)); |
| 7752 | } |
| 7753 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7754 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7755 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7756 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7757 | |
| 7758 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7759 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7760 | { |
| 7761 | PyObject *result = NULL; |
| 7762 | int name; |
| 7763 | |
| 7764 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 7765 | int value; |
| 7766 | |
| 7767 | errno = 0; |
| 7768 | value = sysconf(name); |
| 7769 | if (value == -1 && errno != 0) |
| 7770 | posix_error(); |
| 7771 | else |
| 7772 | result = PyInt_FromLong(value); |
| 7773 | } |
| 7774 | return result; |
| 7775 | } |
| 7776 | #endif |
| 7777 | |
| 7778 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7779 | /* This code is used to ensure that the tables of configuration value names |
| 7780 | * are in sorted order as required by conv_confname(), and also to build the |
| 7781 | * the exported dictionaries that are used to publish information about the |
| 7782 | * names available on the host platform. |
| 7783 | * |
| 7784 | * Sorting the table at runtime ensures that the table is properly ordered |
| 7785 | * when used, even for platforms we're not able to test on. It also makes |
| 7786 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7787 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7788 | |
| 7789 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7790 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7791 | { |
| 7792 | const struct constdef *c1 = |
| 7793 | (const struct constdef *) v1; |
| 7794 | const struct constdef *c2 = |
| 7795 | (const struct constdef *) v2; |
| 7796 | |
| 7797 | return strcmp(c1->name, c2->name); |
| 7798 | } |
| 7799 | |
| 7800 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7801 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7802 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7803 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7804 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7805 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7806 | |
| 7807 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 7808 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7809 | if (d == NULL) |
| 7810 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7811 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7812 | for (i=0; i < tablesize; ++i) { |
| 7813 | PyObject *o = PyInt_FromLong(table[i].value); |
| 7814 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 7815 | Py_XDECREF(o); |
| 7816 | Py_DECREF(d); |
| 7817 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7818 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7819 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7820 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7821 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7822 | } |
| 7823 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7824 | /* Return -1 on failure, 0 on success. */ |
| 7825 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7826 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7827 | { |
| 7828 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7829 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7830 | sizeof(posix_constants_pathconf) |
| 7831 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7832 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7833 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7834 | #endif |
| 7835 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7836 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7837 | sizeof(posix_constants_confstr) |
| 7838 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7839 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7840 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7841 | #endif |
| 7842 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7843 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7844 | sizeof(posix_constants_sysconf) |
| 7845 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7846 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7847 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7848 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7849 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7850 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7851 | |
| 7852 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7853 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7854 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7855 | 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] | 7856 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7857 | |
| 7858 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7859 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7860 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7861 | abort(); |
| 7862 | /*NOTREACHED*/ |
| 7863 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 7864 | return NULL; |
| 7865 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7866 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7867 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7868 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7869 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 7870 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7871 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7872 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 7873 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 7874 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 7875 | application (if any) its extension is associated.\n\ |
| 7876 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 7877 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7878 | \n\ |
| 7879 | startfile returns as soon as the associated application is launched.\n\ |
| 7880 | There is no option to wait for the application to close, and no way\n\ |
| 7881 | to retrieve the application's exit status.\n\ |
| 7882 | \n\ |
| 7883 | The filepath is relative to the current directory. If you want to use\n\ |
| 7884 | 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] | 7885 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7886 | |
| 7887 | static PyObject * |
| 7888 | win32_startfile(PyObject *self, PyObject *args) |
| 7889 | { |
| 7890 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7891 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7892 | HINSTANCE rc; |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7893 | #ifdef Py_WIN_WIDE_FILENAMES |
| 7894 | if (unicode_file_names()) { |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7895 | PyObject *unipath, *woperation = NULL; |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7896 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 7897 | &unipath, &operation)) { |
| 7898 | PyErr_Clear(); |
| 7899 | goto normal; |
| 7900 | } |
| 7901 | |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7902 | |
| 7903 | if (operation) { |
| 7904 | woperation = PyUnicode_DecodeASCII(operation, |
| 7905 | strlen(operation), NULL); |
| 7906 | if (!woperation) { |
| 7907 | PyErr_Clear(); |
| 7908 | operation = NULL; |
| 7909 | goto normal; |
| 7910 | } |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7911 | } |
| 7912 | |
| 7913 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7914 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7915 | PyUnicode_AS_UNICODE(unipath), |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7916 | NULL, NULL, SW_SHOWNORMAL); |
| 7917 | Py_END_ALLOW_THREADS |
| 7918 | |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7919 | Py_XDECREF(woperation); |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7920 | if (rc <= (HINSTANCE)32) { |
| 7921 | PyObject *errval = win32_error_unicode("startfile", |
| 7922 | PyUnicode_AS_UNICODE(unipath)); |
| 7923 | return errval; |
| 7924 | } |
| 7925 | Py_INCREF(Py_None); |
| 7926 | return Py_None; |
| 7927 | } |
| 7928 | #endif |
| 7929 | |
| 7930 | normal: |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7931 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 7932 | Py_FileSystemDefaultEncoding, &filepath, |
| 7933 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7934 | return NULL; |
| 7935 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7936 | rc = ShellExecute((HWND)0, operation, filepath, |
| 7937 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7938 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 7939 | if (rc <= (HINSTANCE)32) { |
| 7940 | PyObject *errval = win32_error("startfile", filepath); |
| 7941 | PyMem_Free(filepath); |
| 7942 | return errval; |
| 7943 | } |
| 7944 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7945 | Py_INCREF(Py_None); |
| 7946 | return Py_None; |
| 7947 | } |
| 7948 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7949 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7950 | #ifdef HAVE_GETLOADAVG |
| 7951 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 7952 | "getloadavg() -> (float, float, float)\n\n\ |
| 7953 | Return the number of processes in the system run queue averaged over\n\ |
| 7954 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 7955 | was unobtainable"); |
| 7956 | |
| 7957 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7958 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7959 | { |
| 7960 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7961 | if (getloadavg(loadavg, 3)!=3) { |
| 7962 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 7963 | return NULL; |
| 7964 | } else |
| 7965 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 7966 | } |
| 7967 | #endif |
| 7968 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7969 | #ifdef MS_WINDOWS |
| 7970 | |
| 7971 | PyDoc_STRVAR(win32_urandom__doc__, |
| 7972 | "urandom(n) -> str\n\n\ |
| 7973 | Return a string of n random bytes suitable for cryptographic use."); |
| 7974 | |
| 7975 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 7976 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 7977 | DWORD dwFlags ); |
| 7978 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 7979 | BYTE *pbBuffer ); |
| 7980 | |
| 7981 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
| 7982 | static HCRYPTPROV hCryptProv = 0; |
| 7983 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7984 | static PyObject* |
| 7985 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7986 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7987 | int howMany; |
| 7988 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7989 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7990 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 7991 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7992 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 7993 | if (howMany < 0) |
| 7994 | return PyErr_Format(PyExc_ValueError, |
| 7995 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7996 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7997 | if (hCryptProv == 0) { |
| 7998 | HINSTANCE hAdvAPI32 = NULL; |
| 7999 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8000 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 8001 | /* Obtain handle to the DLL containing CryptoAPI |
| 8002 | This should not fail */ |
| 8003 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 8004 | if(hAdvAPI32 == NULL) |
| 8005 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8006 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 8007 | /* Obtain pointers to the CryptoAPI functions |
| 8008 | This will fail on some early versions of Win95 */ |
| 8009 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 8010 | hAdvAPI32, |
| 8011 | "CryptAcquireContextA"); |
| 8012 | if (pCryptAcquireContext == NULL) |
| 8013 | return PyErr_Format(PyExc_NotImplementedError, |
| 8014 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8015 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 8016 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 8017 | hAdvAPI32, "CryptGenRandom"); |
Georg Brandl | 74bb783 | 2006-09-06 06:03:59 +0000 | [diff] [blame] | 8018 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 8019 | return PyErr_Format(PyExc_NotImplementedError, |
| 8020 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8021 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 8022 | /* Acquire context */ |
| 8023 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 8024 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 8025 | return win32_error("CryptAcquireContext", NULL); |
| 8026 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8027 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 8028 | /* Allocate bytes */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 8029 | result = PyString_FromStringAndSize(NULL, howMany); |
| 8030 | if (result != NULL) { |
| 8031 | /* Get random data */ |
| 8032 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 8033 | PyString_AS_STRING(result))) { |
| 8034 | Py_DECREF(result); |
| 8035 | return win32_error("CryptGenRandom", NULL); |
| 8036 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 8037 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 8038 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8039 | } |
| 8040 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8041 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 8042 | #ifdef __VMS |
| 8043 | /* Use openssl random routine */ |
| 8044 | #include <openssl/rand.h> |
| 8045 | PyDoc_STRVAR(vms_urandom__doc__, |
| 8046 | "urandom(n) -> str\n\n\ |
| 8047 | Return a string of n random bytes suitable for cryptographic use."); |
| 8048 | |
| 8049 | static PyObject* |
| 8050 | vms_urandom(PyObject *self, PyObject *args) |
| 8051 | { |
| 8052 | int howMany; |
| 8053 | PyObject* result; |
| 8054 | |
| 8055 | /* Read arguments */ |
| 8056 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 8057 | return NULL; |
| 8058 | if (howMany < 0) |
| 8059 | return PyErr_Format(PyExc_ValueError, |
| 8060 | "negative argument not allowed"); |
| 8061 | |
| 8062 | /* Allocate bytes */ |
| 8063 | result = PyString_FromStringAndSize(NULL, howMany); |
| 8064 | if (result != NULL) { |
| 8065 | /* Get random data */ |
| 8066 | if (RAND_pseudo_bytes((unsigned char*) |
| 8067 | PyString_AS_STRING(result), |
| 8068 | howMany) < 0) { |
| 8069 | Py_DECREF(result); |
| 8070 | return PyErr_Format(PyExc_ValueError, |
| 8071 | "RAND_pseudo_bytes"); |
| 8072 | } |
| 8073 | } |
| 8074 | return result; |
| 8075 | } |
| 8076 | #endif |
| 8077 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8078 | static PyMethodDef posix_methods[] = { |
| 8079 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 8080 | #ifdef HAVE_TTYNAME |
| 8081 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 8082 | #endif |
| 8083 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 8084 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8085 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8086 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8087 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 8088 | #ifdef HAVE_LCHOWN |
| 8089 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 8090 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 8091 | #ifdef HAVE_CHROOT |
| 8092 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 8093 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8094 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8095 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8096 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 8097 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8098 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 8099 | #ifdef Py_USING_UNICODE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8100 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 8101 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 8102 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8103 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8104 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8105 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8106 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 8107 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 8108 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8109 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8110 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8111 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8112 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8113 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8114 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8115 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 8116 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 8117 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 8118 | {"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] | 8119 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8120 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8121 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8122 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8123 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8124 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8125 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8126 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8127 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8128 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8129 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 8130 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 8131 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8132 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8133 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8134 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8135 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8136 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8137 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 8138 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8139 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 8140 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8141 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 8142 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 8143 | #if defined(PYOS_OS2) |
| 8144 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 8145 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 8146 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 8147 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 8148 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8149 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 8150 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8151 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8152 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8153 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 8154 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8155 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 8156 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 8157 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8158 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 8159 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8160 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8161 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8162 | #endif /* HAVE_GETEGID */ |
| 8163 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8164 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8165 | #endif /* HAVE_GETEUID */ |
| 8166 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8167 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8168 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8169 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8170 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8171 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8172 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8173 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8174 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8175 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8176 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8177 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8178 | #endif /* HAVE_GETPPID */ |
| 8179 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8180 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8181 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8182 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8183 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8184 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8185 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8186 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8187 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 8188 | #ifdef HAVE_KILLPG |
| 8189 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 8190 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8191 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8192 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8193 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8194 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8195 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8196 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 8197 | {"popen2", win32_popen2, METH_VARARGS}, |
| 8198 | {"popen3", win32_popen3, METH_VARARGS}, |
| 8199 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 8200 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8201 | #else |
| 8202 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 8203 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 8204 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 8205 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 8206 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 8207 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8208 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8209 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8210 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8211 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8212 | #ifdef HAVE_SETEUID |
| 8213 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 8214 | #endif /* HAVE_SETEUID */ |
| 8215 | #ifdef HAVE_SETEGID |
| 8216 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 8217 | #endif /* HAVE_SETEGID */ |
| 8218 | #ifdef HAVE_SETREUID |
| 8219 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 8220 | #endif /* HAVE_SETREUID */ |
| 8221 | #ifdef HAVE_SETREGID |
| 8222 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 8223 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8224 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8225 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8226 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8227 | #ifdef HAVE_SETGROUPS |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 8228 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8229 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 8230 | #ifdef HAVE_GETPGID |
| 8231 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 8232 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8233 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8234 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8235 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8236 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8237 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8238 | #endif /* HAVE_WAIT */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 8239 | #ifdef HAVE_WAIT3 |
| 8240 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 8241 | #endif /* HAVE_WAIT3 */ |
| 8242 | #ifdef HAVE_WAIT4 |
| 8243 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 8244 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 8245 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8246 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8247 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8248 | #ifdef HAVE_GETSID |
| 8249 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 8250 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8251 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8252 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8253 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8254 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8255 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8256 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8257 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8258 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8259 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8260 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8261 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8262 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8263 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 8264 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 8265 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 8266 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 8267 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 8268 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 8269 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 8270 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 8271 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8272 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8273 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8274 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8275 | #endif |
| 8276 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8277 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8278 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 8279 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8280 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 8281 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8282 | #ifdef HAVE_DEVICE_MACROS |
| 8283 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 8284 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 8285 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 8286 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8287 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8288 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8289 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8290 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8291 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8292 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8293 | #ifdef HAVE_UNSETENV |
| 8294 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 8295 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8296 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8297 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8298 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8299 | #ifdef HAVE_FCHDIR |
| 8300 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 8301 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8302 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8303 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8304 | #endif |
| 8305 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8306 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8307 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8308 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8309 | #ifdef WCOREDUMP |
| 8310 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 8311 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8312 | #ifdef WIFCONTINUED |
| 8313 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 8314 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8315 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8316 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8317 | #endif /* WIFSTOPPED */ |
| 8318 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8319 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8320 | #endif /* WIFSIGNALED */ |
| 8321 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8322 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8323 | #endif /* WIFEXITED */ |
| 8324 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8325 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8326 | #endif /* WEXITSTATUS */ |
| 8327 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8328 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8329 | #endif /* WTERMSIG */ |
| 8330 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8331 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8332 | #endif /* WSTOPSIG */ |
| 8333 | #endif /* HAVE_SYS_WAIT_H */ |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 8334 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8335 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8336 | #endif |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 8337 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8338 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8339 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 8340 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8341 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8342 | #endif |
| 8343 | #ifdef HAVE_TEMPNAM |
| 8344 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 8345 | #endif |
| 8346 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8347 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8348 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8349 | #ifdef HAVE_CONFSTR |
| 8350 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 8351 | #endif |
| 8352 | #ifdef HAVE_SYSCONF |
| 8353 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 8354 | #endif |
| 8355 | #ifdef HAVE_FPATHCONF |
| 8356 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 8357 | #endif |
| 8358 | #ifdef HAVE_PATHCONF |
| 8359 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 8360 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8361 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8362 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 8363 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 8364 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8365 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8366 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8367 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8368 | #ifdef MS_WINDOWS |
| 8369 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 8370 | #endif |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 8371 | #ifdef __VMS |
| 8372 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 8373 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8374 | {NULL, NULL} /* Sentinel */ |
| 8375 | }; |
| 8376 | |
| 8377 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8378 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8379 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8380 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8381 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8382 | } |
| 8383 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8384 | #if defined(PYOS_OS2) |
| 8385 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8386 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8387 | { |
| 8388 | APIRET rc; |
| 8389 | ULONG values[QSV_MAX+1]; |
| 8390 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 8391 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8392 | |
| 8393 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 8394 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8395 | Py_END_ALLOW_THREADS |
| 8396 | |
| 8397 | if (rc != NO_ERROR) { |
| 8398 | os2_error(rc); |
| 8399 | return -1; |
| 8400 | } |
| 8401 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8402 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 8403 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 8404 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 8405 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 8406 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 8407 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 8408 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8409 | |
| 8410 | switch (values[QSV_VERSION_MINOR]) { |
| 8411 | case 0: ver = "2.00"; break; |
| 8412 | case 10: ver = "2.10"; break; |
| 8413 | case 11: ver = "2.11"; break; |
| 8414 | case 30: ver = "3.00"; break; |
| 8415 | case 40: ver = "4.00"; break; |
| 8416 | case 50: ver = "5.00"; break; |
| 8417 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 8418 | PyOS_snprintf(tmp, sizeof(tmp), |
| 8419 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 8420 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8421 | ver = &tmp[0]; |
| 8422 | } |
| 8423 | |
| 8424 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8425 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8426 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8427 | |
| 8428 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 8429 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 8430 | tmp[1] = ':'; |
| 8431 | tmp[2] = '\0'; |
| 8432 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8433 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8434 | } |
| 8435 | #endif |
| 8436 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8437 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 8438 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8439 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8440 | #ifdef F_OK |
| 8441 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8442 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8443 | #ifdef R_OK |
| 8444 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8445 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8446 | #ifdef W_OK |
| 8447 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8448 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8449 | #ifdef X_OK |
| 8450 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8451 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8452 | #ifdef NGROUPS_MAX |
| 8453 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 8454 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8455 | #ifdef TMP_MAX |
| 8456 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 8457 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8458 | #ifdef WCONTINUED |
| 8459 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 8460 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8461 | #ifdef WNOHANG |
| 8462 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8463 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8464 | #ifdef WUNTRACED |
| 8465 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 8466 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8467 | #ifdef O_RDONLY |
| 8468 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 8469 | #endif |
| 8470 | #ifdef O_WRONLY |
| 8471 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 8472 | #endif |
| 8473 | #ifdef O_RDWR |
| 8474 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 8475 | #endif |
| 8476 | #ifdef O_NDELAY |
| 8477 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 8478 | #endif |
| 8479 | #ifdef O_NONBLOCK |
| 8480 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 8481 | #endif |
| 8482 | #ifdef O_APPEND |
| 8483 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 8484 | #endif |
| 8485 | #ifdef O_DSYNC |
| 8486 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 8487 | #endif |
| 8488 | #ifdef O_RSYNC |
| 8489 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 8490 | #endif |
| 8491 | #ifdef O_SYNC |
| 8492 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 8493 | #endif |
| 8494 | #ifdef O_NOCTTY |
| 8495 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 8496 | #endif |
| 8497 | #ifdef O_CREAT |
| 8498 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 8499 | #endif |
| 8500 | #ifdef O_EXCL |
| 8501 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 8502 | #endif |
| 8503 | #ifdef O_TRUNC |
| 8504 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 8505 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 8506 | #ifdef O_BINARY |
| 8507 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 8508 | #endif |
| 8509 | #ifdef O_TEXT |
| 8510 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 8511 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8512 | #ifdef O_LARGEFILE |
| 8513 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 8514 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 8515 | #ifdef O_SHLOCK |
| 8516 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 8517 | #endif |
| 8518 | #ifdef O_EXLOCK |
| 8519 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 8520 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8521 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8522 | /* MS Windows */ |
| 8523 | #ifdef O_NOINHERIT |
| 8524 | /* Don't inherit in child processes. */ |
| 8525 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 8526 | #endif |
| 8527 | #ifdef _O_SHORT_LIVED |
| 8528 | /* Optimize for short life (keep in memory). */ |
| 8529 | /* MS forgot to define this one with a non-underscore form too. */ |
| 8530 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 8531 | #endif |
| 8532 | #ifdef O_TEMPORARY |
| 8533 | /* Automatically delete when last handle is closed. */ |
| 8534 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 8535 | #endif |
| 8536 | #ifdef O_RANDOM |
| 8537 | /* Optimize for random access. */ |
| 8538 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 8539 | #endif |
| 8540 | #ifdef O_SEQUENTIAL |
| 8541 | /* Optimize for sequential access. */ |
| 8542 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 8543 | #endif |
| 8544 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8545 | /* GNU extensions. */ |
| 8546 | #ifdef O_DIRECT |
| 8547 | /* Direct disk access. */ |
| 8548 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 8549 | #endif |
| 8550 | #ifdef O_DIRECTORY |
| 8551 | /* Must be a directory. */ |
| 8552 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 8553 | #endif |
| 8554 | #ifdef O_NOFOLLOW |
| 8555 | /* Do not follow links. */ |
| 8556 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 8557 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8558 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8559 | /* These come from sysexits.h */ |
| 8560 | #ifdef EX_OK |
| 8561 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8562 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8563 | #ifdef EX_USAGE |
| 8564 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8565 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8566 | #ifdef EX_DATAERR |
| 8567 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8568 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8569 | #ifdef EX_NOINPUT |
| 8570 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8571 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8572 | #ifdef EX_NOUSER |
| 8573 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8574 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8575 | #ifdef EX_NOHOST |
| 8576 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8577 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8578 | #ifdef EX_UNAVAILABLE |
| 8579 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8580 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8581 | #ifdef EX_SOFTWARE |
| 8582 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8583 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8584 | #ifdef EX_OSERR |
| 8585 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8586 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8587 | #ifdef EX_OSFILE |
| 8588 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8589 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8590 | #ifdef EX_CANTCREAT |
| 8591 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8592 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8593 | #ifdef EX_IOERR |
| 8594 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8595 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8596 | #ifdef EX_TEMPFAIL |
| 8597 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8598 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8599 | #ifdef EX_PROTOCOL |
| 8600 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8601 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8602 | #ifdef EX_NOPERM |
| 8603 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8604 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8605 | #ifdef EX_CONFIG |
| 8606 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8607 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8608 | #ifdef EX_NOTFOUND |
| 8609 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8610 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8611 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8612 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8613 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 8614 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 8615 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 8616 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 8617 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 8618 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 8619 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 8620 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 8621 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 8622 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 8623 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 8624 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 8625 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 8626 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 8627 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 8628 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 8629 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 8630 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 8631 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 8632 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 8633 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 8634 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 8635 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 8636 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 8637 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 8638 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 8639 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8640 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8641 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8642 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8643 | #if defined(PYOS_OS2) |
| 8644 | if (insertvalues(d)) return -1; |
| 8645 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8646 | return 0; |
| 8647 | } |
| 8648 | |
| 8649 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8650 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8651 | #define INITFUNC initnt |
| 8652 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8653 | |
| 8654 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8655 | #define INITFUNC initos2 |
| 8656 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8657 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8658 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8659 | #define INITFUNC initposix |
| 8660 | #define MODNAME "posix" |
| 8661 | #endif |
| 8662 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 8663 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 8664 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8665 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8666 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8667 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8668 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8669 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8670 | posix__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 8671 | if (m == NULL) |
| 8672 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8673 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8674 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8675 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8676 | Py_XINCREF(v); |
| 8677 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8678 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8679 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8680 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8681 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8682 | return; |
| 8683 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8684 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8685 | return; |
| 8686 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8687 | Py_INCREF(PyExc_OSError); |
| 8688 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8689 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8690 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 8691 | if (posix_putenv_garbage == NULL) |
| 8692 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8693 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8694 | |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 8695 | if (!initialized) { |
| 8696 | stat_result_desc.name = MODNAME ".stat_result"; |
| 8697 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 8698 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 8699 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 8700 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 8701 | structseq_new = StatResultType.tp_new; |
| 8702 | StatResultType.tp_new = statresult_new; |
| 8703 | |
| 8704 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 8705 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
| 8706 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8707 | Py_INCREF((PyObject*) &StatResultType); |
| 8708 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8709 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 8710 | PyModule_AddObject(m, "statvfs_result", |
| 8711 | (PyObject*) &StatVFSResultType); |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 8712 | initialized = 1; |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 8713 | |
| 8714 | #ifdef __APPLE__ |
| 8715 | /* |
| 8716 | * Step 2 of weak-linking support on Mac OS X. |
| 8717 | * |
| 8718 | * The code below removes functions that are not available on the |
| 8719 | * currently active platform. |
| 8720 | * |
| 8721 | * This block allow one to use a python binary that was build on |
| 8722 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 8723 | * OSX 10.4. |
| 8724 | */ |
| 8725 | #ifdef HAVE_FSTATVFS |
| 8726 | if (fstatvfs == NULL) { |
| 8727 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 8728 | return; |
| 8729 | } |
| 8730 | } |
| 8731 | #endif /* HAVE_FSTATVFS */ |
| 8732 | |
| 8733 | #ifdef HAVE_STATVFS |
| 8734 | if (statvfs == NULL) { |
| 8735 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 8736 | return; |
| 8737 | } |
| 8738 | } |
| 8739 | #endif /* HAVE_STATVFS */ |
| 8740 | |
| 8741 | # ifdef HAVE_LCHOWN |
| 8742 | if (lchown == NULL) { |
| 8743 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 8744 | return; |
| 8745 | } |
| 8746 | } |
| 8747 | #endif /* HAVE_LCHOWN */ |
| 8748 | |
| 8749 | |
| 8750 | #endif /* __APPLE__ */ |
| 8751 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8752 | } |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 8753 | |
| 8754 | #ifdef __cplusplus |
| 8755 | } |
| 8756 | #endif |
| 8757 | |
Martin v. Löwis | 18aaa56 | 2006-10-15 08:43:33 +0000 | [diff] [blame] | 8758 | |