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 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 16 | #define PY_SSIZE_T_CLEAN |
| 17 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 18 | #include "Python.h" |
| 19 | #include "structseq.h" |
| 20 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 21 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 22 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 23 | #endif /* defined(__VMS) */ |
| 24 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 29 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 30 | "This module provides access to operating system functionality that is\n\ |
| 31 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 32 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 33 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 34 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 35 | #ifndef Py_USING_UNICODE |
| 36 | /* This is used in signatures of functions. */ |
| 37 | #define Py_UNICODE void |
| 38 | #endif |
| 39 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 40 | #if defined(PYOS_OS2) |
| 41 | #define INCL_DOS |
| 42 | #define INCL_DOSERRORS |
| 43 | #define INCL_DOSPROCESS |
| 44 | #define INCL_NOPMAPI |
| 45 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 46 | #if defined(PYCC_GCC) |
| 47 | #include <ctype.h> |
| 48 | #include <io.h> |
| 49 | #include <stdio.h> |
| 50 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 51 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 52 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 55 | #include <sys/types.h> |
| 56 | #include <sys/stat.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 58 | #ifdef HAVE_SYS_WAIT_H |
| 59 | #include <sys/wait.h> /* For WNOHANG */ |
| 60 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 62 | #include <signal.h> |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 64 | #ifdef HAVE_FCNTL_H |
| 65 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 66 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 68 | #ifdef HAVE_GRP_H |
| 69 | #include <grp.h> |
| 70 | #endif |
| 71 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 72 | #ifdef HAVE_SYSEXITS_H |
| 73 | #include <sysexits.h> |
| 74 | #endif /* HAVE_SYSEXITS_H */ |
| 75 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 76 | #ifdef HAVE_SYS_LOADAVG_H |
| 77 | #include <sys/loadavg.h> |
| 78 | #endif |
| 79 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 80 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 81 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 82 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 83 | #include <process.h> |
| 84 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 85 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 86 | #define HAVE_GETCWD 1 |
| 87 | #define HAVE_OPENDIR 1 |
| 88 | #define HAVE_SYSTEM 1 |
| 89 | #if defined(__OS2__) |
| 90 | #define HAVE_EXECV 1 |
| 91 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 92 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 93 | #include <process.h> |
| 94 | #else |
| 95 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 96 | #define HAVE_EXECV 1 |
| 97 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 98 | #define HAVE_OPENDIR 1 |
| 99 | #define HAVE_PIPE 1 |
| 100 | #define HAVE_POPEN 1 |
| 101 | #define HAVE_SYSTEM 1 |
| 102 | #define HAVE_WAIT 1 |
| 103 | #else |
| 104 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 105 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 106 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 107 | #define HAVE_EXECV 1 |
| 108 | #define HAVE_PIPE 1 |
| 109 | #define HAVE_POPEN 1 |
| 110 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 111 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 112 | #define HAVE_FSYNC 1 |
| 113 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 114 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 115 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 116 | /* 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] | 117 | #else /* all other compilers */ |
| 118 | /* Unix functions that the configure script doesn't check for */ |
| 119 | #define HAVE_EXECV 1 |
| 120 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 121 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 122 | #define HAVE_FORK1 1 |
| 123 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 124 | #define HAVE_GETCWD 1 |
| 125 | #define HAVE_GETEGID 1 |
| 126 | #define HAVE_GETEUID 1 |
| 127 | #define HAVE_GETGID 1 |
| 128 | #define HAVE_GETPPID 1 |
| 129 | #define HAVE_GETUID 1 |
| 130 | #define HAVE_KILL 1 |
| 131 | #define HAVE_OPENDIR 1 |
| 132 | #define HAVE_PIPE 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 133 | #ifndef __rtems__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #define HAVE_POPEN 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 135 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 136 | #define HAVE_SYSTEM 1 |
| 137 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 138 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 139 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 140 | #endif /* _MSC_VER */ |
| 141 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 142 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 143 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 144 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 145 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 146 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 147 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 148 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 149 | (default) */ |
| 150 | extern char *ctermid_r(char *); |
| 151 | #endif |
| 152 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 153 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 154 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 155 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 156 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 157 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 158 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 159 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 160 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 161 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 162 | #endif |
| 163 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 164 | extern int chdir(char *); |
| 165 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 166 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 167 | extern int chdir(const char *); |
| 168 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 169 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 170 | #ifdef __BORLANDC__ |
| 171 | extern int chmod(const char *, int); |
| 172 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 173 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 174 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 175 | extern int chown(const char *, uid_t, gid_t); |
| 176 | extern char *getcwd(char *, int); |
| 177 | extern char *strerror(int); |
| 178 | extern int link(const char *, const char *); |
| 179 | extern int rename(const char *, const char *); |
| 180 | extern int stat(const char *, struct stat *); |
| 181 | extern int unlink(const char *); |
| 182 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 183 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 184 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 185 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 186 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 187 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 188 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 189 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 191 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 193 | #ifdef HAVE_UTIME_H |
| 194 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 195 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 196 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 197 | #ifdef HAVE_SYS_UTIME_H |
| 198 | #include <sys/utime.h> |
| 199 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 200 | #endif /* HAVE_SYS_UTIME_H */ |
| 201 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #ifdef HAVE_SYS_TIMES_H |
| 203 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 204 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | |
| 206 | #ifdef HAVE_SYS_PARAM_H |
| 207 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 208 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | |
| 210 | #ifdef HAVE_SYS_UTSNAME_H |
| 211 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 212 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 214 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 216 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 217 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 218 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 219 | #include <direct.h> |
| 220 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 221 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 223 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 224 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 225 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 227 | #endif |
| 228 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 229 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 230 | #endif |
| 231 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 233 | #endif |
| 234 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 236 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 237 | #include <direct.h> |
| 238 | #include <io.h> |
| 239 | #include <process.h> |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 240 | #include "osdefs.h" |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 241 | #define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 242 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 243 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 244 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 245 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 246 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 248 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 249 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 250 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 251 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 252 | #ifndef MAXPATHLEN |
| 253 | #define MAXPATHLEN 1024 |
| 254 | #endif /* MAXPATHLEN */ |
| 255 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 256 | #ifdef UNION_WAIT |
| 257 | /* Emulate some macros on systems that have a union instead of macros */ |
| 258 | |
| 259 | #ifndef WIFEXITED |
| 260 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 261 | #endif |
| 262 | |
| 263 | #ifndef WEXITSTATUS |
| 264 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 265 | #endif |
| 266 | |
| 267 | #ifndef WTERMSIG |
| 268 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 269 | #endif |
| 270 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 271 | #define WAIT_TYPE union wait |
| 272 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 273 | |
| 274 | #else /* !UNION_WAIT */ |
| 275 | #define WAIT_TYPE int |
| 276 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 277 | #endif /* UNION_WAIT */ |
| 278 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 279 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 280 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 281 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 282 | #define USE_CTERMID_R |
| 283 | #endif |
| 284 | |
| 285 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 286 | #define USE_TMPNAM_R |
| 287 | #endif |
| 288 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 289 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 290 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 291 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 292 | # define STAT win32_stat |
| 293 | # define FSTAT win32_fstat |
| 294 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 295 | #else |
| 296 | # define STAT stat |
| 297 | # define FSTAT fstat |
| 298 | # define STRUCT_STAT struct stat |
| 299 | #endif |
| 300 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 301 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 302 | #include <sys/mkdev.h> |
| 303 | #else |
| 304 | #if defined(MAJOR_IN_SYSMACROS) |
| 305 | #include <sys/sysmacros.h> |
| 306 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 307 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 308 | #include <sys/mkdev.h> |
| 309 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 310 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 311 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 312 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 313 | #ifdef WITH_NEXT_FRAMEWORK |
| 314 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 315 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 316 | */ |
| 317 | #include <crt_externs.h> |
| 318 | static char **environ; |
| 319 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 320 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 321 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 322 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 323 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 324 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 325 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 326 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 327 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 328 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 329 | if (d == NULL) |
| 330 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 331 | #ifdef WITH_NEXT_FRAMEWORK |
| 332 | if (environ == NULL) |
| 333 | environ = *_NSGetEnviron(); |
| 334 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 335 | if (environ == NULL) |
| 336 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 337 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 338 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 339 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 340 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 341 | char *p = strchr(*e, '='); |
| 342 | if (p == NULL) |
| 343 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 344 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 345 | if (k == NULL) { |
| 346 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 347 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 348 | } |
| 349 | v = PyString_FromString(p+1); |
| 350 | if (v == NULL) { |
| 351 | PyErr_Clear(); |
| 352 | Py_DECREF(k); |
| 353 | continue; |
| 354 | } |
| 355 | if (PyDict_GetItem(d, k) == NULL) { |
| 356 | if (PyDict_SetItem(d, k, v) != 0) |
| 357 | PyErr_Clear(); |
| 358 | } |
| 359 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 360 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 361 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 362 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 363 | { |
| 364 | APIRET rc; |
| 365 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 366 | |
| 367 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 368 | 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] | 369 | PyObject *v = PyString_FromString(buffer); |
| 370 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 371 | Py_DECREF(v); |
| 372 | } |
| 373 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 374 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 375 | PyObject *v = PyString_FromString(buffer); |
| 376 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 377 | Py_DECREF(v); |
| 378 | } |
| 379 | } |
| 380 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 381 | return d; |
| 382 | } |
| 383 | |
| 384 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 385 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 386 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 387 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 388 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 389 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 390 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 391 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 392 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 393 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 394 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 395 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 398 | #ifdef Py_WIN_WIDE_FILENAMES |
| 399 | static PyObject * |
| 400 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 401 | { |
| 402 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 403 | } |
| 404 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 405 | |
| 406 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 407 | static PyObject * |
| 408 | posix_error_with_allocated_filename(char* name) |
| 409 | { |
| 410 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 411 | PyMem_Free(name); |
| 412 | return rc; |
| 413 | } |
| 414 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 415 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 416 | static PyObject * |
| 417 | win32_error(char* function, char* filename) |
| 418 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 419 | /* XXX We should pass the function name along in the future. |
| 420 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 421 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 422 | Windows error object, which is non-trivial. |
| 423 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 424 | errno = GetLastError(); |
| 425 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 426 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 427 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 428 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 429 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 430 | |
| 431 | #ifdef Py_WIN_WIDE_FILENAMES |
| 432 | static PyObject * |
| 433 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 434 | { |
| 435 | /* XXX - see win32_error for comments on 'function' */ |
| 436 | errno = GetLastError(); |
| 437 | if (filename) |
| 438 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 439 | else |
| 440 | return PyErr_SetFromWindowsErr(errno); |
| 441 | } |
| 442 | |
| 443 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 444 | { |
| 445 | /* XXX Perhaps we should make this API an alias of |
| 446 | PyObject_Unicode() instead ?! */ |
| 447 | if (PyUnicode_CheckExact(obj)) { |
| 448 | Py_INCREF(obj); |
| 449 | return obj; |
| 450 | } |
| 451 | if (PyUnicode_Check(obj)) { |
| 452 | /* For a Unicode subtype that's not a Unicode object, |
| 453 | return a true Unicode object with the same data. */ |
| 454 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 455 | PyUnicode_GET_SIZE(obj)); |
| 456 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 457 | return PyUnicode_FromEncodedObject(obj, |
| 458 | Py_FileSystemDefaultEncoding, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 459 | "strict"); |
| 460 | } |
| 461 | |
| 462 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 463 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 464 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 465 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 466 | #if defined(PYOS_OS2) |
| 467 | /********************************************************************** |
| 468 | * Helper Function to Trim and Format OS/2 Messages |
| 469 | **********************************************************************/ |
| 470 | static void |
| 471 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 472 | { |
| 473 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 474 | |
| 475 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 476 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 477 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 478 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 479 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 480 | } |
| 481 | |
| 482 | /* Add Optional Reason Text */ |
| 483 | if (reason) { |
| 484 | strcat(msgbuf, " : "); |
| 485 | strcat(msgbuf, reason); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /********************************************************************** |
| 490 | * Decode an OS/2 Operating System Error Code |
| 491 | * |
| 492 | * A convenience function to lookup an OS/2 error code and return a |
| 493 | * text message we can use to raise a Python exception. |
| 494 | * |
| 495 | * Notes: |
| 496 | * The messages for errors returned from the OS/2 kernel reside in |
| 497 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 498 | * |
| 499 | **********************************************************************/ |
| 500 | static char * |
| 501 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 502 | { |
| 503 | APIRET rc; |
| 504 | ULONG msglen; |
| 505 | |
| 506 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 507 | Py_BEGIN_ALLOW_THREADS |
| 508 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 509 | errorcode, "oso001.msg", &msglen); |
| 510 | Py_END_ALLOW_THREADS |
| 511 | |
| 512 | if (rc == NO_ERROR) |
| 513 | os2_formatmsg(msgbuf, msglen, reason); |
| 514 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 515 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 516 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 517 | |
| 518 | return msgbuf; |
| 519 | } |
| 520 | |
| 521 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 522 | errors are not in a global variable e.g. 'errno' nor are |
| 523 | they congruent with posix error numbers. */ |
| 524 | |
| 525 | static PyObject * os2_error(int code) |
| 526 | { |
| 527 | char text[1024]; |
| 528 | PyObject *v; |
| 529 | |
| 530 | os2_strerror(text, sizeof(text), code, ""); |
| 531 | |
| 532 | v = Py_BuildValue("(is)", code, text); |
| 533 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 534 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 535 | Py_DECREF(v); |
| 536 | } |
| 537 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 538 | } |
| 539 | |
| 540 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 541 | |
| 542 | /* POSIX generic methods */ |
| 543 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 544 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 545 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 546 | { |
| 547 | int fd; |
| 548 | int res; |
| 549 | fd = PyObject_AsFileDescriptor(fdobj); |
| 550 | if (fd < 0) |
| 551 | return NULL; |
| 552 | Py_BEGIN_ALLOW_THREADS |
| 553 | res = (*func)(fd); |
| 554 | Py_END_ALLOW_THREADS |
| 555 | if (res < 0) |
| 556 | return posix_error(); |
| 557 | Py_INCREF(Py_None); |
| 558 | return Py_None; |
| 559 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 560 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 561 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 562 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 563 | unicode_file_names(void) |
| 564 | { |
| 565 | static int canusewide = -1; |
| 566 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 567 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 568 | the Windows NT family. */ |
| 569 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 570 | } |
| 571 | return canusewide; |
| 572 | } |
| 573 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 574 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 575 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 576 | posix_1str(PyObject *args, char *format, int (*func)(const char*), |
| 577 | char *wformat, int (*wfunc)(const Py_UNICODE*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 578 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 579 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 580 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 581 | #ifdef Py_WIN_WIDE_FILENAMES |
| 582 | if (unicode_file_names()) { |
| 583 | PyUnicodeObject *po; |
| 584 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 585 | Py_BEGIN_ALLOW_THREADS |
| 586 | /* PyUnicode_AS_UNICODE OK without thread |
| 587 | lock as it is a simple dereference. */ |
| 588 | res = (*wfunc)(PyUnicode_AS_UNICODE(po)); |
| 589 | Py_END_ALLOW_THREADS |
| 590 | if (res < 0) |
Mark Hammond | d389036 | 2002-10-03 07:24:48 +0000 | [diff] [blame] | 591 | return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 592 | Py_INCREF(Py_None); |
| 593 | return Py_None; |
| 594 | } |
| 595 | /* Drop the argument parsing error as narrow |
| 596 | strings are also valid. */ |
| 597 | PyErr_Clear(); |
| 598 | } |
| 599 | #else |
| 600 | /* Platforms that don't support Unicode filenames |
| 601 | shouldn't be passing these extra params */ |
| 602 | assert(wformat==NULL && wfunc == NULL); |
| 603 | #endif |
| 604 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 605 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 606 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 607 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 608 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 609 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 610 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 611 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 612 | return posix_error_with_allocated_filename(path1); |
| 613 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 614 | Py_INCREF(Py_None); |
| 615 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 618 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 619 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 620 | char *format, |
| 621 | int (*func)(const char *, const char *), |
| 622 | char *wformat, |
| 623 | int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 624 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 625 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 626 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 627 | #ifdef Py_WIN_WIDE_FILENAMES |
| 628 | if (unicode_file_names()) { |
| 629 | PyObject *po1; |
| 630 | PyObject *po2; |
| 631 | if (PyArg_ParseTuple(args, wformat, &po1, &po2)) { |
| 632 | if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) { |
| 633 | PyObject *wpath1; |
| 634 | PyObject *wpath2; |
| 635 | wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1); |
| 636 | wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2); |
| 637 | if (!wpath1 || !wpath2) { |
| 638 | Py_XDECREF(wpath1); |
| 639 | Py_XDECREF(wpath2); |
| 640 | return NULL; |
| 641 | } |
| 642 | Py_BEGIN_ALLOW_THREADS |
| 643 | /* PyUnicode_AS_UNICODE OK without thread |
| 644 | lock as it is a simple dereference. */ |
| 645 | res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1), |
| 646 | PyUnicode_AS_UNICODE(wpath2)); |
| 647 | Py_END_ALLOW_THREADS |
| 648 | Py_XDECREF(wpath1); |
| 649 | Py_XDECREF(wpath2); |
| 650 | if (res != 0) |
| 651 | return posix_error(); |
| 652 | Py_INCREF(Py_None); |
| 653 | return Py_None; |
| 654 | } |
| 655 | /* Else flow through as neither is Unicode. */ |
| 656 | } |
| 657 | /* Drop the argument parsing error as narrow |
| 658 | strings are also valid. */ |
| 659 | PyErr_Clear(); |
| 660 | } |
| 661 | #else |
| 662 | /* Platforms that don't support Unicode filenames |
| 663 | shouldn't be passing these extra params */ |
| 664 | assert(wformat==NULL && wfunc == NULL); |
| 665 | #endif |
| 666 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 667 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 668 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 669 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 670 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 671 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 672 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 673 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 674 | PyMem_Free(path1); |
| 675 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 676 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 677 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 678 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 679 | Py_INCREF(Py_None); |
| 680 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 683 | #ifdef MS_WINDOWS |
| 684 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 685 | - time stamps are restricted to second resolution |
| 686 | - file modification times suffer from forth-and-back conversions between |
| 687 | UTC and local time |
| 688 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 689 | */ |
| 690 | #define HAVE_STAT_NSEC 1 |
| 691 | |
| 692 | struct win32_stat{ |
| 693 | int st_dev; |
| 694 | __int64 st_ino; |
| 695 | unsigned short st_mode; |
| 696 | int st_nlink; |
| 697 | int st_uid; |
| 698 | int st_gid; |
| 699 | int st_rdev; |
| 700 | __int64 st_size; |
| 701 | int st_atime; |
| 702 | int st_atime_nsec; |
| 703 | int st_mtime; |
| 704 | int st_mtime_nsec; |
| 705 | int st_ctime; |
| 706 | int st_ctime_nsec; |
| 707 | }; |
| 708 | |
| 709 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 710 | |
| 711 | static void |
| 712 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 713 | { |
| 714 | /* XXX endianness */ |
| 715 | __int64 in = *(__int64*)in_ptr; |
| 716 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 717 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 718 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 719 | } |
| 720 | |
| 721 | /* Below, we *know* that ugo+r is 0444 */ |
| 722 | #if _S_IREAD != 0400 |
| 723 | #error Unsupported C library |
| 724 | #endif |
| 725 | static int |
| 726 | attributes_to_mode(DWORD attr) |
| 727 | { |
| 728 | int m = 0; |
| 729 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 730 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 731 | else |
| 732 | m |= _S_IFREG; |
| 733 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 734 | m |= 0444; |
| 735 | else |
| 736 | m |= 0666; |
| 737 | return m; |
| 738 | } |
| 739 | |
| 740 | static int |
| 741 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 742 | { |
| 743 | memset(result, 0, sizeof(*result)); |
| 744 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 745 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 746 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 747 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 748 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 749 | |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | static int |
| 754 | win32_stat(const char* path, struct win32_stat *result) |
| 755 | { |
| 756 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 757 | int code; |
| 758 | char *dot; |
| 759 | /* XXX not supported on Win95 and NT 3.x */ |
| 760 | if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
| 761 | /* Protocol violation: we explicitly clear errno, instead of |
| 762 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 763 | errno = 0; |
| 764 | return -1; |
| 765 | } |
| 766 | code = attribute_data_to_stat(&info, result); |
| 767 | if (code != 0) |
| 768 | return code; |
| 769 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 770 | dot = strrchr(path, '.'); |
| 771 | if (dot) { |
| 772 | if (stricmp(dot, ".bat") == 0 || |
| 773 | stricmp(dot, ".cmd") == 0 || |
| 774 | stricmp(dot, ".exe") == 0 || |
| 775 | stricmp(dot, ".com") == 0) |
| 776 | result->st_mode |= 0111; |
| 777 | } |
| 778 | return code; |
| 779 | } |
| 780 | |
| 781 | static int |
| 782 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 783 | { |
| 784 | int code; |
| 785 | const wchar_t *dot; |
| 786 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 787 | /* XXX not supported on Win95 and NT 3.x */ |
| 788 | if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
| 789 | /* Protocol violation: we explicitly clear errno, instead of |
| 790 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 791 | errno = 0; |
| 792 | return -1; |
| 793 | } |
| 794 | code = attribute_data_to_stat(&info, result); |
| 795 | if (code < 0) |
| 796 | return code; |
| 797 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 798 | dot = wcsrchr(path, '.'); |
| 799 | if (dot) { |
| 800 | if (_wcsicmp(dot, L".bat") == 0 || |
| 801 | _wcsicmp(dot, L".cmd") == 0 || |
| 802 | _wcsicmp(dot, L".exe") == 0 || |
| 803 | _wcsicmp(dot, L".com") == 0) |
| 804 | result->st_mode |= 0111; |
| 805 | } |
| 806 | return code; |
| 807 | } |
| 808 | |
| 809 | static int |
| 810 | win32_fstat(int file_number, struct win32_stat *result) |
| 811 | { |
| 812 | BY_HANDLE_FILE_INFORMATION info; |
| 813 | HANDLE h; |
| 814 | int type; |
| 815 | |
| 816 | h = (HANDLE)_get_osfhandle(file_number); |
| 817 | |
| 818 | /* Protocol violation: we explicitly clear errno, instead of |
| 819 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 820 | errno = 0; |
| 821 | |
| 822 | if (h == INVALID_HANDLE_VALUE) { |
| 823 | /* This is really a C library error (invalid file handle). |
| 824 | We set the Win32 error to the closes one matching. */ |
| 825 | SetLastError(ERROR_INVALID_HANDLE); |
| 826 | return -1; |
| 827 | } |
| 828 | memset(result, 0, sizeof(*result)); |
| 829 | |
| 830 | type = GetFileType(h); |
| 831 | if (type == FILE_TYPE_UNKNOWN) { |
| 832 | DWORD error = GetLastError(); |
| 833 | if (error != 0) { |
| 834 | return -1; |
| 835 | } |
| 836 | /* else: valid but unknown file */ |
| 837 | } |
| 838 | |
| 839 | if (type != FILE_TYPE_DISK) { |
| 840 | if (type == FILE_TYPE_CHAR) |
| 841 | result->st_mode = _S_IFCHR; |
| 842 | else if (type == FILE_TYPE_PIPE) |
| 843 | result->st_mode = _S_IFIFO; |
| 844 | return 0; |
| 845 | } |
| 846 | |
| 847 | if (!GetFileInformationByHandle(h, &info)) { |
| 848 | return -1; |
| 849 | } |
| 850 | |
| 851 | /* similar to stat() */ |
| 852 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 853 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 854 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 855 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 856 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 857 | /* specific to fstat() */ |
| 858 | result->st_nlink = info.nNumberOfLinks; |
| 859 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | #endif /* MS_WINDOWS */ |
| 864 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 865 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 866 | "stat_result: Result from stat or lstat.\n\n\ |
| 867 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 868 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 869 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 870 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 871 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 872 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 873 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 874 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 875 | |
| 876 | static PyStructSequence_Field stat_result_fields[] = { |
| 877 | {"st_mode", "protection bits"}, |
| 878 | {"st_ino", "inode"}, |
| 879 | {"st_dev", "device"}, |
| 880 | {"st_nlink", "number of hard links"}, |
| 881 | {"st_uid", "user ID of owner"}, |
| 882 | {"st_gid", "group ID of owner"}, |
| 883 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 884 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 885 | {NULL, "integer time of last access"}, |
| 886 | {NULL, "integer time of last modification"}, |
| 887 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 888 | {"st_atime", "time of last access"}, |
| 889 | {"st_mtime", "time of last modification"}, |
| 890 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 891 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 892 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 893 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 894 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 895 | {"st_blocks", "number of blocks allocated"}, |
| 896 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 897 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 898 | {"st_rdev", "device type (if inode device)"}, |
| 899 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 900 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 901 | {"st_flags", "user defined flags for file"}, |
| 902 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 903 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 904 | {"st_gen", "generation number"}, |
| 905 | #endif |
| 906 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 907 | {"st_birthtime", "time of creation"}, |
| 908 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 909 | {0} |
| 910 | }; |
| 911 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 912 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 913 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 914 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 915 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 916 | #endif |
| 917 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 918 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 919 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 920 | #else |
| 921 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 922 | #endif |
| 923 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 924 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 925 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 926 | #else |
| 927 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 928 | #endif |
| 929 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 930 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 931 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 932 | #else |
| 933 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 934 | #endif |
| 935 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 936 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 937 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 938 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 939 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 940 | #endif |
| 941 | |
| 942 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 943 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 944 | #else |
| 945 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 946 | #endif |
| 947 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 948 | static PyStructSequence_Desc stat_result_desc = { |
| 949 | "stat_result", /* name */ |
| 950 | stat_result__doc__, /* doc */ |
| 951 | stat_result_fields, |
| 952 | 10 |
| 953 | }; |
| 954 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 955 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 956 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 957 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 958 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 959 | 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] | 960 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 961 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 962 | |
| 963 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 964 | {"f_bsize", }, |
| 965 | {"f_frsize", }, |
| 966 | {"f_blocks", }, |
| 967 | {"f_bfree", }, |
| 968 | {"f_bavail", }, |
| 969 | {"f_files", }, |
| 970 | {"f_ffree", }, |
| 971 | {"f_favail", }, |
| 972 | {"f_flag", }, |
| 973 | {"f_namemax",}, |
| 974 | {0} |
| 975 | }; |
| 976 | |
| 977 | static PyStructSequence_Desc statvfs_result_desc = { |
| 978 | "statvfs_result", /* name */ |
| 979 | statvfs_result__doc__, /* doc */ |
| 980 | statvfs_result_fields, |
| 981 | 10 |
| 982 | }; |
| 983 | |
| 984 | static PyTypeObject StatResultType; |
| 985 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 986 | static newfunc structseq_new; |
| 987 | |
| 988 | static PyObject * |
| 989 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 990 | { |
| 991 | PyStructSequence *result; |
| 992 | int i; |
| 993 | |
| 994 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 995 | if (!result) |
| 996 | return NULL; |
| 997 | /* If we have been initialized from a tuple, |
| 998 | st_?time might be set to None. Initialize it |
| 999 | from the int slots. */ |
| 1000 | for (i = 7; i <= 9; i++) { |
| 1001 | if (result->ob_item[i+3] == Py_None) { |
| 1002 | Py_DECREF(Py_None); |
| 1003 | Py_INCREF(result->ob_item[i]); |
| 1004 | result->ob_item[i+3] = result->ob_item[i]; |
| 1005 | } |
| 1006 | } |
| 1007 | return (PyObject*)result; |
| 1008 | } |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1013 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1014 | |
| 1015 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1016 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1017 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1018 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1019 | future calls return ints. \n\ |
| 1020 | If newval is omitted, return the current setting.\n"); |
| 1021 | |
| 1022 | static PyObject* |
| 1023 | stat_float_times(PyObject* self, PyObject *args) |
| 1024 | { |
| 1025 | int newval = -1; |
| 1026 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1027 | return NULL; |
| 1028 | if (newval == -1) |
| 1029 | /* Return old value */ |
| 1030 | return PyBool_FromLong(_stat_float_times); |
| 1031 | _stat_float_times = newval; |
| 1032 | Py_INCREF(Py_None); |
| 1033 | return Py_None; |
| 1034 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1035 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1036 | static void |
| 1037 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1038 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1039 | PyObject *fval,*ival; |
| 1040 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1041 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1042 | #else |
| 1043 | ival = PyInt_FromLong((long)sec); |
| 1044 | #endif |
| 1045 | if (_stat_float_times) { |
| 1046 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1047 | } else { |
| 1048 | fval = ival; |
| 1049 | Py_INCREF(fval); |
| 1050 | } |
| 1051 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1052 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1055 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1056 | (used by posix_stat() and posix_fstat()) */ |
| 1057 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1058 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1059 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1060 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1061 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1062 | if (v == NULL) |
| 1063 | return NULL; |
| 1064 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1065 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1066 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1067 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1068 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1069 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1070 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1071 | #endif |
| 1072 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1073 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1074 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1075 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1076 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1077 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1078 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); |
| 1079 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); |
| 1080 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1081 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1082 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1083 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1084 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1085 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1086 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1087 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1088 | #if defined(HAVE_STAT_TV_NSEC) |
| 1089 | ansec = st->st_atim.tv_nsec; |
| 1090 | mnsec = st->st_mtim.tv_nsec; |
| 1091 | cnsec = st->st_ctim.tv_nsec; |
| 1092 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1093 | ansec = st->st_atimespec.tv_nsec; |
| 1094 | mnsec = st->st_mtimespec.tv_nsec; |
| 1095 | cnsec = st->st_ctimespec.tv_nsec; |
| 1096 | #elif defined(HAVE_STAT_NSEC) |
| 1097 | ansec = st->st_atime_nsec; |
| 1098 | mnsec = st->st_mtime_nsec; |
| 1099 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1100 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1101 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1102 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1103 | fill_time(v, 7, st->st_atime, ansec); |
| 1104 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1105 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1106 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1107 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1108 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1109 | PyInt_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1110 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1111 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1112 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1113 | PyInt_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1114 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1115 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1116 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1117 | PyInt_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1118 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1119 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1120 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1121 | PyInt_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1122 | #endif |
| 1123 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1124 | { |
| 1125 | PyObject *val; |
| 1126 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1127 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1128 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1129 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1130 | #else |
| 1131 | bnsec = 0; |
| 1132 | #endif |
| 1133 | if (_stat_float_times) { |
| 1134 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1135 | } else { |
| 1136 | val = PyInt_FromLong((long)bsec); |
| 1137 | } |
| 1138 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1139 | val); |
| 1140 | } |
| 1141 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1142 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1143 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1144 | PyInt_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1145 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1146 | |
| 1147 | if (PyErr_Occurred()) { |
| 1148 | Py_DECREF(v); |
| 1149 | return NULL; |
| 1150 | } |
| 1151 | |
| 1152 | return v; |
| 1153 | } |
| 1154 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1155 | #ifdef MS_WINDOWS |
| 1156 | |
| 1157 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1158 | where / can be used in place of \ and the trailing slash is optional. |
| 1159 | Both SERVER and SHARE must have at least one character. |
| 1160 | */ |
| 1161 | |
| 1162 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1163 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
| 1164 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
| 1165 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1166 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1167 | IsUNCRootA(char *path, int pathlen) |
| 1168 | { |
| 1169 | #define ISSLASH ISSLASHA |
| 1170 | |
| 1171 | int i, share; |
| 1172 | |
| 1173 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1174 | /* minimum UNCRoot is \\x\y */ |
| 1175 | return FALSE; |
| 1176 | for (i = 2; i < pathlen ; i++) |
| 1177 | if (ISSLASH(path[i])) break; |
| 1178 | if (i == 2 || i == pathlen) |
| 1179 | /* do not allow \\\SHARE or \\SERVER */ |
| 1180 | return FALSE; |
| 1181 | share = i+1; |
| 1182 | for (i = share; i < pathlen; i++) |
| 1183 | if (ISSLASH(path[i])) break; |
| 1184 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1185 | |
| 1186 | #undef ISSLASH |
| 1187 | } |
| 1188 | |
| 1189 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1190 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1191 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1192 | { |
| 1193 | #define ISSLASH ISSLASHW |
| 1194 | |
| 1195 | int i, share; |
| 1196 | |
| 1197 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1198 | /* minimum UNCRoot is \\x\y */ |
| 1199 | return FALSE; |
| 1200 | for (i = 2; i < pathlen ; i++) |
| 1201 | if (ISSLASH(path[i])) break; |
| 1202 | if (i == 2 || i == pathlen) |
| 1203 | /* do not allow \\\SHARE or \\SERVER */ |
| 1204 | return FALSE; |
| 1205 | share = i+1; |
| 1206 | for (i = share; i < pathlen; i++) |
| 1207 | if (ISSLASH(path[i])) break; |
| 1208 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1209 | |
| 1210 | #undef ISSLASH |
| 1211 | } |
| 1212 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1213 | #endif /* MS_WINDOWS */ |
| 1214 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1215 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1216 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1217 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1218 | #ifdef __VMS |
| 1219 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1220 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1221 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1222 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1223 | char *wformat, |
| 1224 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1225 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1226 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1227 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1228 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1229 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1230 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1231 | |
| 1232 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1233 | /* If on wide-character-capable OS see if argument |
| 1234 | is Unicode and if so use wide API. */ |
| 1235 | if (unicode_file_names()) { |
| 1236 | PyUnicodeObject *po; |
| 1237 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1238 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1239 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1240 | Py_BEGIN_ALLOW_THREADS |
| 1241 | /* PyUnicode_AS_UNICODE result OK without |
| 1242 | thread lock as it is a simple dereference. */ |
| 1243 | res = wstatfunc(wpath, &st); |
| 1244 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1245 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1246 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1247 | return win32_error_unicode("stat", wpath); |
| 1248 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1249 | } |
| 1250 | /* Drop the argument parsing error as narrow strings |
| 1251 | are also valid. */ |
| 1252 | PyErr_Clear(); |
| 1253 | } |
| 1254 | #endif |
| 1255 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1256 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1257 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1258 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1259 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1260 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1261 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1262 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1263 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1264 | |
| 1265 | if (res != 0) { |
| 1266 | #ifdef MS_WINDOWS |
| 1267 | result = win32_error("stat", pathfree); |
| 1268 | #else |
| 1269 | result = posix_error_with_filename(pathfree); |
| 1270 | #endif |
| 1271 | } |
| 1272 | else |
| 1273 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1274 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1275 | PyMem_Free(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1276 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1279 | /* POSIX methods */ |
| 1280 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1281 | PyDoc_STRVAR(posix_access__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1282 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1283 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1284 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1285 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1286 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1287 | 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] | 1288 | |
| 1289 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1290 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1291 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1292 | char *path; |
| 1293 | int mode; |
| 1294 | int res; |
| 1295 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1296 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1297 | if (unicode_file_names()) { |
| 1298 | PyUnicodeObject *po; |
| 1299 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1300 | Py_BEGIN_ALLOW_THREADS |
| 1301 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1302 | it is a simple dereference. */ |
| 1303 | res = _waccess(PyUnicode_AS_UNICODE(po), mode); |
| 1304 | Py_END_ALLOW_THREADS |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1305 | return PyBool_FromLong(res == 0); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1306 | } |
| 1307 | /* Drop the argument parsing error as narrow strings |
| 1308 | are also valid. */ |
| 1309 | PyErr_Clear(); |
| 1310 | } |
| 1311 | #endif |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame] | 1312 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1313 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1314 | return NULL; |
| 1315 | Py_BEGIN_ALLOW_THREADS |
| 1316 | res = access(path, mode); |
| 1317 | Py_END_ALLOW_THREADS |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 1318 | PyMem_Free(path); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1319 | return PyBool_FromLong(res == 0); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1322 | #ifndef F_OK |
| 1323 | #define F_OK 0 |
| 1324 | #endif |
| 1325 | #ifndef R_OK |
| 1326 | #define R_OK 4 |
| 1327 | #endif |
| 1328 | #ifndef W_OK |
| 1329 | #define W_OK 2 |
| 1330 | #endif |
| 1331 | #ifndef X_OK |
| 1332 | #define X_OK 1 |
| 1333 | #endif |
| 1334 | |
| 1335 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1336 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1337 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1338 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1339 | |
| 1340 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1341 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1342 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1343 | int id; |
| 1344 | char *ret; |
| 1345 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1346 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1347 | return NULL; |
| 1348 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1349 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1350 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1351 | if (id == 0) { |
| 1352 | ret = ttyname(); |
| 1353 | } |
| 1354 | else { |
| 1355 | ret = NULL; |
| 1356 | } |
| 1357 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1358 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1359 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1360 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1361 | return posix_error(); |
| 1362 | return PyString_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1363 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1364 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1365 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1366 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1367 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1368 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1369 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1370 | |
| 1371 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1372 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1373 | { |
| 1374 | char *ret; |
| 1375 | char buffer[L_ctermid]; |
| 1376 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1377 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1378 | ret = ctermid_r(buffer); |
| 1379 | #else |
| 1380 | ret = ctermid(buffer); |
| 1381 | #endif |
| 1382 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1383 | return posix_error(); |
| 1384 | return PyString_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1385 | } |
| 1386 | #endif |
| 1387 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1388 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1389 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1390 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1391 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1392 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1393 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1394 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1395 | #ifdef MS_WINDOWS |
| 1396 | return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir); |
| 1397 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1398 | return posix_1str(args, "et:chdir", _chdir2, NULL, NULL); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1399 | #elif defined(__VMS) |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1400 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1401 | NULL, NULL); |
| 1402 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1403 | return posix_1str(args, "et:chdir", chdir, NULL, NULL); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1404 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1407 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1408 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1409 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1410 | 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] | 1411 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1412 | |
| 1413 | static PyObject * |
| 1414 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1415 | { |
| 1416 | return posix_fildes(fdobj, fchdir); |
| 1417 | } |
| 1418 | #endif /* HAVE_FCHDIR */ |
| 1419 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1420 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1421 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1422 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1423 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1424 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1425 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1426 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1427 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1428 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1429 | int i; |
| 1430 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1431 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1432 | if (unicode_file_names()) { |
| 1433 | PyUnicodeObject *po; |
| 1434 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1435 | Py_BEGIN_ALLOW_THREADS |
| 1436 | res = _wchmod(PyUnicode_AS_UNICODE(po), i); |
| 1437 | Py_END_ALLOW_THREADS |
| 1438 | if (res < 0) |
| 1439 | return posix_error_with_unicode_filename( |
| 1440 | PyUnicode_AS_UNICODE(po)); |
| 1441 | Py_INCREF(Py_None); |
| 1442 | return Py_None; |
| 1443 | } |
| 1444 | /* Drop the argument parsing error as narrow strings |
| 1445 | are also valid. */ |
| 1446 | PyErr_Clear(); |
| 1447 | } |
| 1448 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1449 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1450 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1451 | return NULL; |
| 1452 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1453 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1454 | Py_END_ALLOW_THREADS |
| 1455 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1456 | return posix_error_with_allocated_filename(path); |
| 1457 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1458 | Py_INCREF(Py_None); |
| 1459 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1462 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1463 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1464 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1465 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1466 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1467 | |
| 1468 | static PyObject * |
| 1469 | posix_chroot(PyObject *self, PyObject *args) |
| 1470 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1471 | return posix_1str(args, "et:chroot", chroot, NULL, NULL); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1472 | } |
| 1473 | #endif |
| 1474 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1475 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1476 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1477 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1478 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1479 | |
| 1480 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1481 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1482 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1483 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1484 | } |
| 1485 | #endif /* HAVE_FSYNC */ |
| 1486 | |
| 1487 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1488 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1489 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1490 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1491 | #endif |
| 1492 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1493 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1494 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1495 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1496 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1497 | |
| 1498 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1499 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1500 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1501 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1502 | } |
| 1503 | #endif /* HAVE_FDATASYNC */ |
| 1504 | |
| 1505 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1506 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1507 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1508 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1509 | 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] | 1510 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1511 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1512 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1513 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1514 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1515 | int uid, gid; |
| 1516 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1517 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1518 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1519 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1520 | return NULL; |
| 1521 | Py_BEGIN_ALLOW_THREADS |
| 1522 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1523 | Py_END_ALLOW_THREADS |
| 1524 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1525 | return posix_error_with_allocated_filename(path); |
| 1526 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1527 | Py_INCREF(Py_None); |
| 1528 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1529 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1530 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1531 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1532 | #ifdef HAVE_LCHOWN |
| 1533 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1534 | "lchown(path, uid, gid)\n\n\ |
| 1535 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1536 | This function will not follow symbolic links."); |
| 1537 | |
| 1538 | static PyObject * |
| 1539 | posix_lchown(PyObject *self, PyObject *args) |
| 1540 | { |
| 1541 | char *path = NULL; |
| 1542 | int uid, gid; |
| 1543 | int res; |
| 1544 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1545 | Py_FileSystemDefaultEncoding, &path, |
| 1546 | &uid, &gid)) |
| 1547 | return NULL; |
| 1548 | Py_BEGIN_ALLOW_THREADS |
| 1549 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1550 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1551 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1552 | return posix_error_with_allocated_filename(path); |
| 1553 | PyMem_Free(path); |
| 1554 | Py_INCREF(Py_None); |
| 1555 | return Py_None; |
| 1556 | } |
| 1557 | #endif /* HAVE_LCHOWN */ |
| 1558 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1559 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1560 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1561 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1562 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1563 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1564 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1565 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1566 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1567 | { |
| 1568 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1569 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1570 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1571 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1572 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1573 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1574 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1575 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1576 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1577 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1578 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1579 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1580 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1581 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1582 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1583 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1584 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1585 | "getcwdu() -> path\n\n\ |
| 1586 | Return a unicode string representing the current working directory."); |
| 1587 | |
| 1588 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1589 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1590 | { |
| 1591 | char buf[1026]; |
| 1592 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1593 | |
| 1594 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1595 | if (unicode_file_names()) { |
| 1596 | wchar_t *wres; |
| 1597 | wchar_t wbuf[1026]; |
| 1598 | Py_BEGIN_ALLOW_THREADS |
| 1599 | wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]); |
| 1600 | Py_END_ALLOW_THREADS |
| 1601 | if (wres == NULL) |
| 1602 | return posix_error(); |
| 1603 | return PyUnicode_FromWideChar(wbuf, wcslen(wbuf)); |
| 1604 | } |
| 1605 | #endif |
| 1606 | |
| 1607 | Py_BEGIN_ALLOW_THREADS |
| 1608 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1609 | res = _getcwd2(buf, sizeof buf); |
| 1610 | #else |
| 1611 | res = getcwd(buf, sizeof buf); |
| 1612 | #endif |
| 1613 | Py_END_ALLOW_THREADS |
| 1614 | if (res == NULL) |
| 1615 | return posix_error(); |
| 1616 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1617 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1618 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1619 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1620 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1621 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1622 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1623 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1624 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1625 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1626 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1627 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1628 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1629 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1630 | return posix_2str(args, "etet:link", link, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1631 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1632 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1633 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1634 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1635 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1636 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1637 | Return a list containing the names of the entries in the directory.\n\ |
| 1638 | \n\ |
| 1639 | path: path of directory to list\n\ |
| 1640 | \n\ |
| 1641 | 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] | 1642 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1643 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1644 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1645 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1646 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1647 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1648 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1649 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1650 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1651 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1652 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1653 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1654 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1655 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 1656 | char namebuf[MAX_PATH*2+5]; |
| 1657 | char *bufptr = namebuf; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1658 | Py_ssize_t len = sizeof(namebuf)/sizeof(namebuf[0]); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1659 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1660 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1661 | /* If on wide-character-capable OS see if argument |
| 1662 | is Unicode and if so use wide API. */ |
| 1663 | if (unicode_file_names()) { |
| 1664 | PyUnicodeObject *po; |
| 1665 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1666 | WIN32_FIND_DATAW wFileData; |
| 1667 | Py_UNICODE wnamebuf[MAX_PATH*2+5]; |
| 1668 | Py_UNICODE wch; |
| 1669 | wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH); |
| 1670 | wnamebuf[MAX_PATH] = L'\0'; |
| 1671 | len = wcslen(wnamebuf); |
| 1672 | wch = (len > 0) ? wnamebuf[len-1] : L'\0'; |
| 1673 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1674 | wnamebuf[len++] = L'/'; |
| 1675 | wcscpy(wnamebuf + len, L"*.*"); |
| 1676 | if ((d = PyList_New(0)) == NULL) |
| 1677 | return NULL; |
| 1678 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1679 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1680 | errno = GetLastError(); |
| 1681 | if (errno == ERROR_FILE_NOT_FOUND) { |
| 1682 | return d; |
| 1683 | } |
| 1684 | Py_DECREF(d); |
| 1685 | return win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1686 | } |
| 1687 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1688 | /* Skip over . and .. */ |
| 1689 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 1690 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 1691 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1692 | if (v == NULL) { |
| 1693 | Py_DECREF(d); |
| 1694 | d = NULL; |
| 1695 | break; |
| 1696 | } |
| 1697 | if (PyList_Append(d, v) != 0) { |
| 1698 | Py_DECREF(v); |
| 1699 | Py_DECREF(d); |
| 1700 | d = NULL; |
| 1701 | break; |
| 1702 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1703 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1704 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1705 | Py_BEGIN_ALLOW_THREADS |
| 1706 | result = FindNextFileW(hFindFile, &wFileData); |
| 1707 | Py_END_ALLOW_THREADS |
| 1708 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1709 | |
| 1710 | if (FindClose(hFindFile) == FALSE) { |
| 1711 | Py_DECREF(d); |
| 1712 | return win32_error_unicode("FindClose", wnamebuf); |
| 1713 | } |
| 1714 | return d; |
| 1715 | } |
| 1716 | /* Drop the argument parsing error as narrow strings |
| 1717 | are also valid. */ |
| 1718 | PyErr_Clear(); |
| 1719 | } |
| 1720 | #endif |
| 1721 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1722 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1723 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1724 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1725 | if (len > 0) { |
| 1726 | char ch = namebuf[len-1]; |
| 1727 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 1728 | namebuf[len++] = '/'; |
| 1729 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1730 | strcpy(namebuf + len, "*.*"); |
| 1731 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1732 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1733 | return NULL; |
| 1734 | |
| 1735 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 1736 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1737 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 1738 | if (errno == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1739 | return d; |
| 1740 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1741 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1742 | } |
| 1743 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1744 | /* Skip over . and .. */ |
| 1745 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 1746 | strcmp(FileData.cFileName, "..") != 0) { |
| 1747 | v = PyString_FromString(FileData.cFileName); |
| 1748 | if (v == NULL) { |
| 1749 | Py_DECREF(d); |
| 1750 | d = NULL; |
| 1751 | break; |
| 1752 | } |
| 1753 | if (PyList_Append(d, v) != 0) { |
| 1754 | Py_DECREF(v); |
| 1755 | Py_DECREF(d); |
| 1756 | d = NULL; |
| 1757 | break; |
| 1758 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1759 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1760 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1761 | Py_BEGIN_ALLOW_THREADS |
| 1762 | result = FindNextFile(hFindFile, &FileData); |
| 1763 | Py_END_ALLOW_THREADS |
| 1764 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1765 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1766 | if (FindClose(hFindFile) == FALSE) { |
| 1767 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1768 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1769 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1770 | |
| 1771 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1772 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1773 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1774 | |
| 1775 | #ifndef MAX_PATH |
| 1776 | #define MAX_PATH CCHMAXPATH |
| 1777 | #endif |
| 1778 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 1779 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1780 | PyObject *d, *v; |
| 1781 | char namebuf[MAX_PATH+5]; |
| 1782 | HDIR hdir = 1; |
| 1783 | ULONG srchcnt = 1; |
| 1784 | FILEFINDBUF3 ep; |
| 1785 | APIRET rc; |
| 1786 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1787 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1788 | return NULL; |
| 1789 | if (len >= MAX_PATH) { |
| 1790 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1791 | return NULL; |
| 1792 | } |
| 1793 | strcpy(namebuf, name); |
| 1794 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1795 | if (*pt == ALTSEP) |
| 1796 | *pt = SEP; |
| 1797 | if (namebuf[len-1] != SEP) |
| 1798 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1799 | strcpy(namebuf + len, "*.*"); |
| 1800 | |
| 1801 | if ((d = PyList_New(0)) == NULL) |
| 1802 | return NULL; |
| 1803 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1804 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1805 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1806 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1807 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1808 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1809 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1810 | |
| 1811 | if (rc != NO_ERROR) { |
| 1812 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1813 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1816 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1817 | do { |
| 1818 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1819 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1820 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1821 | |
| 1822 | strcpy(namebuf, ep.achName); |
| 1823 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1824 | /* Leave Case of Name Alone -- In Native Form */ |
| 1825 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1826 | |
| 1827 | v = PyString_FromString(namebuf); |
| 1828 | if (v == NULL) { |
| 1829 | Py_DECREF(d); |
| 1830 | d = NULL; |
| 1831 | break; |
| 1832 | } |
| 1833 | if (PyList_Append(d, v) != 0) { |
| 1834 | Py_DECREF(v); |
| 1835 | Py_DECREF(d); |
| 1836 | d = NULL; |
| 1837 | break; |
| 1838 | } |
| 1839 | Py_DECREF(v); |
| 1840 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1841 | } |
| 1842 | |
| 1843 | return d; |
| 1844 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1845 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1846 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1847 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1848 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1849 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1850 | int arg_is_unicode = 1; |
| 1851 | |
Georg Brandl | 05e89b8 | 2006-04-11 07:04:06 +0000 | [diff] [blame] | 1852 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1853 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 1854 | arg_is_unicode = 0; |
| 1855 | PyErr_Clear(); |
| 1856 | } |
| 1857 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1858 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1859 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1860 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1861 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1862 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1863 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1864 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1865 | return NULL; |
| 1866 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1867 | for (;;) { |
| 1868 | Py_BEGIN_ALLOW_THREADS |
| 1869 | ep = readdir(dirp); |
| 1870 | Py_END_ALLOW_THREADS |
| 1871 | if (ep == NULL) |
| 1872 | break; |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1873 | if (ep->d_name[0] == '.' && |
| 1874 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1875 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1876 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1877 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1878 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1879 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1880 | d = NULL; |
| 1881 | break; |
| 1882 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1883 | #ifdef Py_USING_UNICODE |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1884 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1885 | PyObject *w; |
| 1886 | |
| 1887 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1888 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1889 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 1890 | if (w != NULL) { |
| 1891 | Py_DECREF(v); |
| 1892 | v = w; |
| 1893 | } |
| 1894 | else { |
| 1895 | /* fall back to the original byte string, as |
| 1896 | discussed in patch #683592 */ |
| 1897 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1898 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1899 | } |
| 1900 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1901 | if (PyList_Append(d, v) != 0) { |
| 1902 | Py_DECREF(v); |
| 1903 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1904 | d = NULL; |
| 1905 | break; |
| 1906 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1907 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1908 | } |
Georg Brandl | bbfe4fa | 2006-04-11 06:47:43 +0000 | [diff] [blame] | 1909 | if (errno != 0 && d != NULL) { |
| 1910 | /* readdir() returned NULL and set errno */ |
| 1911 | closedir(dirp); |
| 1912 | Py_DECREF(d); |
| 1913 | return posix_error_with_allocated_filename(name); |
| 1914 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1915 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1916 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1917 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1918 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1919 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1920 | #endif /* which OS */ |
| 1921 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1922 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1923 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1924 | /* A helper function for abspath on win32 */ |
| 1925 | static PyObject * |
| 1926 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1927 | { |
| 1928 | /* assume encoded strings wont more than double no of chars */ |
| 1929 | char inbuf[MAX_PATH*2]; |
| 1930 | char *inbufp = inbuf; |
Tim Peters | 67d70eb | 2006-03-01 04:35:45 +0000 | [diff] [blame] | 1931 | Py_ssize_t insize = sizeof(inbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1932 | char outbuf[MAX_PATH*2]; |
| 1933 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1934 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1935 | if (unicode_file_names()) { |
| 1936 | PyUnicodeObject *po; |
| 1937 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 1938 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 1939 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1940 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1941 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 1942 | woutbuf, &wtemp)) |
| 1943 | return win32_error("GetFullPathName", ""); |
| 1944 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 1945 | } |
| 1946 | /* Drop the argument parsing error as narrow strings |
| 1947 | are also valid. */ |
| 1948 | PyErr_Clear(); |
| 1949 | } |
| 1950 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1951 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1952 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1953 | &insize)) |
| 1954 | return NULL; |
| 1955 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1956 | outbuf, &temp)) |
| 1957 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 1958 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 1959 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 1960 | Py_FileSystemDefaultEncoding, NULL); |
| 1961 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1962 | return PyString_FromString(outbuf); |
| 1963 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1964 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1965 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1966 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1967 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1968 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1969 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1970 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1971 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1972 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1973 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1974 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1975 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1976 | |
| 1977 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1978 | if (unicode_file_names()) { |
| 1979 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 1980 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1981 | Py_BEGIN_ALLOW_THREADS |
| 1982 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1983 | it is a simple dereference. */ |
| 1984 | res = _wmkdir(PyUnicode_AS_UNICODE(po)); |
| 1985 | Py_END_ALLOW_THREADS |
| 1986 | if (res < 0) |
| 1987 | return posix_error(); |
| 1988 | Py_INCREF(Py_None); |
| 1989 | return Py_None; |
| 1990 | } |
| 1991 | /* Drop the argument parsing error as narrow strings |
| 1992 | are also valid. */ |
| 1993 | PyErr_Clear(); |
| 1994 | } |
| 1995 | #endif |
| 1996 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1997 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1998 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1999 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2000 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2001 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2002 | res = mkdir(path); |
| 2003 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2004 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2005 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2006 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2007 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2008 | return posix_error_with_allocated_filename(path); |
| 2009 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2010 | Py_INCREF(Py_None); |
| 2011 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2014 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2015 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2016 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2017 | #include <sys/resource.h> |
| 2018 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2019 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2020 | |
| 2021 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2022 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2023 | "nice(inc) -> new_priority\n\n\ |
| 2024 | 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] | 2025 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2026 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2027 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2028 | { |
| 2029 | int increment, value; |
| 2030 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2031 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2032 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2033 | |
| 2034 | /* There are two flavours of 'nice': one that returns the new |
| 2035 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2036 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2037 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2038 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2039 | If we are of the nice family that returns the new priority, we |
| 2040 | need to clear errno before the call, and check if errno is filled |
| 2041 | before calling posix_error() on a returnvalue of -1, because the |
| 2042 | -1 may be the actual new priority! */ |
| 2043 | |
| 2044 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2045 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2046 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2047 | if (value == 0) |
| 2048 | value = getpriority(PRIO_PROCESS, 0); |
| 2049 | #endif |
| 2050 | if (value == -1 && errno != 0) |
| 2051 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2052 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2053 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2054 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2055 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2056 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2057 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2058 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2059 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2060 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2061 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2062 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2063 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2064 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2065 | #ifdef MS_WINDOWS |
| 2066 | return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename); |
| 2067 | #else |
| 2068 | return posix_2str(args, "etet:rename", rename, NULL, NULL); |
| 2069 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2072 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2073 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2074 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2075 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2076 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2077 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2078 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2079 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2080 | #ifdef MS_WINDOWS |
| 2081 | return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir); |
| 2082 | #else |
| 2083 | return posix_1str(args, "et:rmdir", rmdir, NULL, NULL); |
| 2084 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2087 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2088 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2089 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2090 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2091 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2092 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2093 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2094 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2095 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2096 | 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] | 2097 | #else |
| 2098 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 2099 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2102 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2103 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2104 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2105 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2106 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2107 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2108 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2109 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2110 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2111 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2112 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2113 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2114 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2115 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2116 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2117 | Py_END_ALLOW_THREADS |
| 2118 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2119 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2120 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2121 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2122 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2123 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2124 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2125 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2126 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2127 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2128 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2129 | { |
| 2130 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2131 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2132 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2133 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2134 | if (i < 0) |
| 2135 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2136 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2139 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2140 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2141 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2142 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2143 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2144 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2145 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2146 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2147 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2148 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2149 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2150 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2151 | #ifdef MS_WINDOWS |
| 2152 | return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink); |
| 2153 | #else |
| 2154 | return posix_1str(args, "et:remove", unlink, NULL, NULL); |
| 2155 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2158 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2159 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2160 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2161 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2162 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2163 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2164 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2165 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2166 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2167 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2168 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2169 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2170 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2171 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2172 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2173 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2174 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2175 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2176 | u.sysname, |
| 2177 | u.nodename, |
| 2178 | u.release, |
| 2179 | u.version, |
| 2180 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2181 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2182 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2183 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2184 | static int |
| 2185 | extract_time(PyObject *t, long* sec, long* usec) |
| 2186 | { |
| 2187 | long intval; |
| 2188 | if (PyFloat_Check(t)) { |
| 2189 | double tval = PyFloat_AsDouble(t); |
| 2190 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 2191 | if (!intobj) |
| 2192 | return -1; |
| 2193 | intval = PyInt_AsLong(intobj); |
| 2194 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2195 | if (intval == -1 && PyErr_Occurred()) |
| 2196 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2197 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2198 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2199 | if (*usec < 0) |
| 2200 | /* If rounding gave us a negative number, |
| 2201 | truncate. */ |
| 2202 | *usec = 0; |
| 2203 | return 0; |
| 2204 | } |
| 2205 | intval = PyInt_AsLong(t); |
| 2206 | if (intval == -1 && PyErr_Occurred()) |
| 2207 | return -1; |
| 2208 | *sec = intval; |
| 2209 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2210 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2211 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2212 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2213 | PyDoc_STRVAR(posix_utime__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2214 | "utime(path, (atime, utime))\n\ |
| 2215 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2216 | 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] | 2217 | 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] | 2218 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2219 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2220 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2221 | { |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2222 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2223 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2224 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2225 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2226 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2227 | #if defined(HAVE_UTIMES) |
| 2228 | struct timeval buf[2]; |
| 2229 | #define ATIME buf[0].tv_sec |
| 2230 | #define MTIME buf[1].tv_sec |
| 2231 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2232 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2233 | struct utimbuf buf; |
| 2234 | #define ATIME buf.actime |
| 2235 | #define MTIME buf.modtime |
| 2236 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2237 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2238 | time_t buf[2]; |
| 2239 | #define ATIME buf[0] |
| 2240 | #define MTIME buf[1] |
| 2241 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2242 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2243 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2244 | int have_unicode_filename = 0; |
| 2245 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2246 | PyUnicodeObject *obwpath; |
| 2247 | wchar_t *wpath; |
| 2248 | if (unicode_file_names()) { |
| 2249 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2250 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2251 | have_unicode_filename = 1; |
| 2252 | } else |
| 2253 | /* Drop the argument parsing error as narrow strings |
| 2254 | are also valid. */ |
| 2255 | PyErr_Clear(); |
| 2256 | } |
| 2257 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 2258 | |
| 2259 | if (!have_unicode_filename && \ |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2260 | !PyArg_ParseTuple(args, "etO:utime", |
| 2261 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2262 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2263 | if (arg == Py_None) { |
| 2264 | /* optional time values not given */ |
| 2265 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2266 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2267 | if (have_unicode_filename) |
| 2268 | res = _wutime(wpath, NULL); |
| 2269 | else |
| 2270 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2271 | res = utime(path, NULL); |
| 2272 | Py_END_ALLOW_THREADS |
| 2273 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2274 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2275 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2276 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2277 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2278 | return NULL; |
| 2279 | } |
| 2280 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2281 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2282 | &atime, &ausec) == -1) { |
| 2283 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2284 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2285 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2286 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2287 | &mtime, &musec) == -1) { |
| 2288 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2289 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2290 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2291 | ATIME = atime; |
| 2292 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2293 | #ifdef HAVE_UTIMES |
| 2294 | buf[0].tv_usec = ausec; |
| 2295 | buf[1].tv_usec = musec; |
| 2296 | Py_BEGIN_ALLOW_THREADS |
| 2297 | res = utimes(path, buf); |
| 2298 | Py_END_ALLOW_THREADS |
| 2299 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2300 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2301 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2302 | if (have_unicode_filename) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 2303 | /* utime is OK with utimbuf, but _wutime insists |
| 2304 | on _utimbuf (the msvc headers assert the |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2305 | underscore version is ansi) */ |
| 2306 | res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG); |
| 2307 | else |
| 2308 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2309 | res = utime(path, UTIME_ARG); |
| 2310 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2311 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2312 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2313 | if (res < 0) { |
| 2314 | #ifdef Py_WIN_WIDE_FILENAMES |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2315 | if (have_unicode_filename) |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2316 | return posix_error_with_unicode_filename(wpath); |
| 2317 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2318 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2319 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2320 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2321 | Py_INCREF(Py_None); |
| 2322 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2323 | #undef UTIME_ARG |
| 2324 | #undef ATIME |
| 2325 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2328 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2329 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2330 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2331 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2332 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2333 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2334 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2335 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2336 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2337 | { |
| 2338 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2339 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2340 | return NULL; |
| 2341 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2342 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2345 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2346 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2347 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2348 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2349 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2350 | for (i = 0; i < count; i++) |
| 2351 | PyMem_Free(array[i]); |
| 2352 | PyMem_DEL(array); |
| 2353 | } |
| 2354 | #endif |
| 2355 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2356 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2357 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2358 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2359 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2360 | Execute an executable path with arguments, replacing current process.\n\ |
| 2361 | \n\ |
| 2362 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2363 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2364 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2365 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2366 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2367 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2368 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2369 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2370 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2371 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2372 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2373 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2374 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2375 | argv is a list or tuple of strings. */ |
| 2376 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2377 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2378 | Py_FileSystemDefaultEncoding, |
| 2379 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2380 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2381 | if (PyList_Check(argv)) { |
| 2382 | argc = PyList_Size(argv); |
| 2383 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2384 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2385 | else if (PyTuple_Check(argv)) { |
| 2386 | argc = PyTuple_Size(argv); |
| 2387 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2388 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2389 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2390 | 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] | 2391 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2392 | return NULL; |
| 2393 | } |
| 2394 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2395 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2396 | if (argvlist == NULL) { |
| 2397 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2398 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2399 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2400 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2401 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2402 | Py_FileSystemDefaultEncoding, |
| 2403 | &argvlist[i])) { |
| 2404 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2405 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2406 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2407 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2408 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2409 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2410 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2411 | } |
| 2412 | argvlist[argc] = NULL; |
| 2413 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2414 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2415 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2416 | /* If we get here it's definitely an error */ |
| 2417 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2418 | free_string_array(argvlist, argc); |
| 2419 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2420 | return posix_error(); |
| 2421 | } |
| 2422 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2423 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2424 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2425 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2426 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2427 | \n\ |
| 2428 | path: path of executable file\n\ |
| 2429 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2430 | env: dictionary of strings mapping to strings"); |
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_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2434 | { |
| 2435 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2436 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2437 | char **argvlist; |
| 2438 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2439 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2440 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2441 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2442 | int lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2443 | |
| 2444 | /* execve has three arguments: (path, argv, env), where |
| 2445 | argv is a list or tuple of strings and env is a dictionary |
| 2446 | like posix.environ. */ |
| 2447 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2448 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2449 | Py_FileSystemDefaultEncoding, |
| 2450 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2451 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2452 | if (PyList_Check(argv)) { |
| 2453 | argc = PyList_Size(argv); |
| 2454 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2455 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2456 | else if (PyTuple_Check(argv)) { |
| 2457 | argc = PyTuple_Size(argv); |
| 2458 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2459 | } |
| 2460 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2461 | PyErr_SetString(PyExc_TypeError, |
| 2462 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2463 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2464 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2465 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2466 | PyErr_SetString(PyExc_TypeError, |
| 2467 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2468 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2471 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2472 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2473 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2474 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2475 | } |
| 2476 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2477 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2478 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2479 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2480 | &argvlist[i])) |
| 2481 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2482 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2483 | goto fail_1; |
| 2484 | } |
| 2485 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2486 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2487 | argvlist[argc] = NULL; |
| 2488 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2489 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2490 | if (i < 0) |
| 2491 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2492 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2493 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2494 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2495 | goto fail_1; |
| 2496 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2497 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2498 | keys = PyMapping_Keys(env); |
| 2499 | vals = PyMapping_Values(env); |
| 2500 | if (!keys || !vals) |
| 2501 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2502 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2503 | PyErr_SetString(PyExc_TypeError, |
| 2504 | "execve(): env.keys() or env.values() is not a list"); |
| 2505 | goto fail_2; |
| 2506 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2507 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2508 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2509 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2510 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2511 | |
| 2512 | key = PyList_GetItem(keys, pos); |
| 2513 | val = PyList_GetItem(vals, pos); |
| 2514 | if (!key || !val) |
| 2515 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2516 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2517 | if (!PyArg_Parse( |
| 2518 | key, |
| 2519 | "s;execve() arg 3 contains a non-string key", |
| 2520 | &k) || |
| 2521 | !PyArg_Parse( |
| 2522 | val, |
| 2523 | "s;execve() arg 3 contains a non-string value", |
| 2524 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2525 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2526 | goto fail_2; |
| 2527 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2528 | |
| 2529 | #if defined(PYOS_OS2) |
| 2530 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2531 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2532 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2533 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2534 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2535 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2536 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2537 | goto fail_2; |
| 2538 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2539 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2540 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2541 | #if defined(PYOS_OS2) |
| 2542 | } |
| 2543 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2544 | } |
| 2545 | envlist[envc] = 0; |
| 2546 | |
| 2547 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2548 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2549 | /* If we get here it's definitely an error */ |
| 2550 | |
| 2551 | (void) posix_error(); |
| 2552 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2553 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2554 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2555 | PyMem_DEL(envlist[envc]); |
| 2556 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2557 | fail_1: |
| 2558 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2559 | Py_XDECREF(vals); |
| 2560 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2561 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2562 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2563 | return NULL; |
| 2564 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2565 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2566 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2567 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2568 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2569 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2570 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2571 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2572 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2573 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2574 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2575 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2576 | |
| 2577 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2578 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2579 | { |
| 2580 | char *path; |
| 2581 | PyObject *argv; |
| 2582 | char **argvlist; |
| 2583 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2584 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2585 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2586 | |
| 2587 | /* spawnv has three arguments: (mode, path, argv), where |
| 2588 | argv is a list or tuple of strings. */ |
| 2589 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2590 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2591 | Py_FileSystemDefaultEncoding, |
| 2592 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2593 | return NULL; |
| 2594 | if (PyList_Check(argv)) { |
| 2595 | argc = PyList_Size(argv); |
| 2596 | getitem = PyList_GetItem; |
| 2597 | } |
| 2598 | else if (PyTuple_Check(argv)) { |
| 2599 | argc = PyTuple_Size(argv); |
| 2600 | getitem = PyTuple_GetItem; |
| 2601 | } |
| 2602 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2603 | PyErr_SetString(PyExc_TypeError, |
| 2604 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2605 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2606 | return NULL; |
| 2607 | } |
| 2608 | |
| 2609 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2610 | if (argvlist == NULL) { |
| 2611 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2612 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2613 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2614 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2615 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2616 | Py_FileSystemDefaultEncoding, |
| 2617 | &argvlist[i])) { |
| 2618 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2619 | PyErr_SetString( |
| 2620 | PyExc_TypeError, |
| 2621 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2622 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2623 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2624 | } |
| 2625 | } |
| 2626 | argvlist[argc] = NULL; |
| 2627 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2628 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2629 | Py_BEGIN_ALLOW_THREADS |
| 2630 | spawnval = spawnv(mode, path, argvlist); |
| 2631 | Py_END_ALLOW_THREADS |
| 2632 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2633 | if (mode == _OLD_P_OVERLAY) |
| 2634 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2635 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2636 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2637 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2638 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2639 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2640 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2641 | free_string_array(argvlist, argc); |
| 2642 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2643 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2644 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2645 | return posix_error(); |
| 2646 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2647 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2648 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2649 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2650 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2651 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2655 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2656 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2657 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2658 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2659 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2660 | path: path of executable file\n\ |
| 2661 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2662 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2663 | |
| 2664 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2665 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2666 | { |
| 2667 | char *path; |
| 2668 | PyObject *argv, *env; |
| 2669 | char **argvlist; |
| 2670 | char **envlist; |
| 2671 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2672 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2673 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2674 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2675 | int lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2676 | |
| 2677 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 2678 | argv is a list or tuple of strings and env is a dictionary |
| 2679 | like posix.environ. */ |
| 2680 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2681 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 2682 | Py_FileSystemDefaultEncoding, |
| 2683 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2684 | return NULL; |
| 2685 | if (PyList_Check(argv)) { |
| 2686 | argc = PyList_Size(argv); |
| 2687 | getitem = PyList_GetItem; |
| 2688 | } |
| 2689 | else if (PyTuple_Check(argv)) { |
| 2690 | argc = PyTuple_Size(argv); |
| 2691 | getitem = PyTuple_GetItem; |
| 2692 | } |
| 2693 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2694 | PyErr_SetString(PyExc_TypeError, |
| 2695 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2696 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2697 | } |
| 2698 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2699 | PyErr_SetString(PyExc_TypeError, |
| 2700 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2701 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | argvlist = PyMem_NEW(char *, argc+1); |
| 2705 | if (argvlist == NULL) { |
| 2706 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2707 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2708 | } |
| 2709 | for (i = 0; i < argc; i++) { |
| 2710 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2711 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2712 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2713 | &argvlist[i])) |
| 2714 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2715 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2716 | goto fail_1; |
| 2717 | } |
| 2718 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2719 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2720 | argvlist[argc] = NULL; |
| 2721 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2722 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2723 | if (i < 0) |
| 2724 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2725 | envlist = PyMem_NEW(char *, i + 1); |
| 2726 | if (envlist == NULL) { |
| 2727 | PyErr_NoMemory(); |
| 2728 | goto fail_1; |
| 2729 | } |
| 2730 | envc = 0; |
| 2731 | keys = PyMapping_Keys(env); |
| 2732 | vals = PyMapping_Values(env); |
| 2733 | if (!keys || !vals) |
| 2734 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2735 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2736 | PyErr_SetString(PyExc_TypeError, |
| 2737 | "spawnve(): env.keys() or env.values() is not a list"); |
| 2738 | goto fail_2; |
| 2739 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2740 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2741 | for (pos = 0; pos < i; pos++) { |
| 2742 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2743 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2744 | |
| 2745 | key = PyList_GetItem(keys, pos); |
| 2746 | val = PyList_GetItem(vals, pos); |
| 2747 | if (!key || !val) |
| 2748 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2749 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2750 | if (!PyArg_Parse( |
| 2751 | key, |
| 2752 | "s;spawnve() arg 3 contains a non-string key", |
| 2753 | &k) || |
| 2754 | !PyArg_Parse( |
| 2755 | val, |
| 2756 | "s;spawnve() arg 3 contains a non-string value", |
| 2757 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2758 | { |
| 2759 | goto fail_2; |
| 2760 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2761 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2762 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2763 | if (p == NULL) { |
| 2764 | PyErr_NoMemory(); |
| 2765 | goto fail_2; |
| 2766 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2767 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2768 | envlist[envc++] = p; |
| 2769 | } |
| 2770 | envlist[envc] = 0; |
| 2771 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2772 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2773 | Py_BEGIN_ALLOW_THREADS |
| 2774 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2775 | Py_END_ALLOW_THREADS |
| 2776 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2777 | if (mode == _OLD_P_OVERLAY) |
| 2778 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2779 | |
| 2780 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2781 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2782 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2783 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2784 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2785 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2786 | (void) posix_error(); |
| 2787 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2788 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2789 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2790 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2791 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2792 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2793 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2794 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2795 | while (--envc >= 0) |
| 2796 | PyMem_DEL(envlist[envc]); |
| 2797 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2798 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2799 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2800 | Py_XDECREF(vals); |
| 2801 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2802 | fail_0: |
| 2803 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2804 | return res; |
| 2805 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2806 | |
| 2807 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 2808 | #if defined(PYOS_OS2) |
| 2809 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 2810 | "spawnvp(mode, file, args)\n\n\ |
| 2811 | Execute the program 'file' in a new process, using the environment\n\ |
| 2812 | search path to find the file.\n\ |
| 2813 | \n\ |
| 2814 | mode: mode of process creation\n\ |
| 2815 | file: executable file name\n\ |
| 2816 | args: tuple or list of strings"); |
| 2817 | |
| 2818 | static PyObject * |
| 2819 | posix_spawnvp(PyObject *self, PyObject *args) |
| 2820 | { |
| 2821 | char *path; |
| 2822 | PyObject *argv; |
| 2823 | char **argvlist; |
| 2824 | int mode, i, argc; |
| 2825 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2826 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2827 | |
| 2828 | /* spawnvp has three arguments: (mode, path, argv), where |
| 2829 | argv is a list or tuple of strings. */ |
| 2830 | |
| 2831 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 2832 | Py_FileSystemDefaultEncoding, |
| 2833 | &path, &argv)) |
| 2834 | return NULL; |
| 2835 | if (PyList_Check(argv)) { |
| 2836 | argc = PyList_Size(argv); |
| 2837 | getitem = PyList_GetItem; |
| 2838 | } |
| 2839 | else if (PyTuple_Check(argv)) { |
| 2840 | argc = PyTuple_Size(argv); |
| 2841 | getitem = PyTuple_GetItem; |
| 2842 | } |
| 2843 | else { |
| 2844 | PyErr_SetString(PyExc_TypeError, |
| 2845 | "spawnvp() arg 2 must be a tuple or list"); |
| 2846 | PyMem_Free(path); |
| 2847 | return NULL; |
| 2848 | } |
| 2849 | |
| 2850 | argvlist = PyMem_NEW(char *, argc+1); |
| 2851 | if (argvlist == NULL) { |
| 2852 | PyMem_Free(path); |
| 2853 | return PyErr_NoMemory(); |
| 2854 | } |
| 2855 | for (i = 0; i < argc; i++) { |
| 2856 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2857 | Py_FileSystemDefaultEncoding, |
| 2858 | &argvlist[i])) { |
| 2859 | free_string_array(argvlist, i); |
| 2860 | PyErr_SetString( |
| 2861 | PyExc_TypeError, |
| 2862 | "spawnvp() arg 2 must contain only strings"); |
| 2863 | PyMem_Free(path); |
| 2864 | return NULL; |
| 2865 | } |
| 2866 | } |
| 2867 | argvlist[argc] = NULL; |
| 2868 | |
| 2869 | Py_BEGIN_ALLOW_THREADS |
| 2870 | #if defined(PYCC_GCC) |
| 2871 | spawnval = spawnvp(mode, path, argvlist); |
| 2872 | #else |
| 2873 | spawnval = _spawnvp(mode, path, argvlist); |
| 2874 | #endif |
| 2875 | Py_END_ALLOW_THREADS |
| 2876 | |
| 2877 | free_string_array(argvlist, argc); |
| 2878 | PyMem_Free(path); |
| 2879 | |
| 2880 | if (spawnval == -1) |
| 2881 | return posix_error(); |
| 2882 | else |
| 2883 | return Py_BuildValue("l", (long) spawnval); |
| 2884 | } |
| 2885 | |
| 2886 | |
| 2887 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 2888 | "spawnvpe(mode, file, args, env)\n\n\ |
| 2889 | Execute the program 'file' in a new process, using the environment\n\ |
| 2890 | search path to find the file.\n\ |
| 2891 | \n\ |
| 2892 | mode: mode of process creation\n\ |
| 2893 | file: executable file name\n\ |
| 2894 | args: tuple or list of arguments\n\ |
| 2895 | env: dictionary of strings mapping to strings"); |
| 2896 | |
| 2897 | static PyObject * |
| 2898 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 2899 | { |
| 2900 | char *path; |
| 2901 | PyObject *argv, *env; |
| 2902 | char **argvlist; |
| 2903 | char **envlist; |
| 2904 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2905 | int mode, i, pos, argc, envc; |
| 2906 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2907 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2908 | int lastarg = 0; |
| 2909 | |
| 2910 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 2911 | argv is a list or tuple of strings and env is a dictionary |
| 2912 | like posix.environ. */ |
| 2913 | |
| 2914 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 2915 | Py_FileSystemDefaultEncoding, |
| 2916 | &path, &argv, &env)) |
| 2917 | return NULL; |
| 2918 | if (PyList_Check(argv)) { |
| 2919 | argc = PyList_Size(argv); |
| 2920 | getitem = PyList_GetItem; |
| 2921 | } |
| 2922 | else if (PyTuple_Check(argv)) { |
| 2923 | argc = PyTuple_Size(argv); |
| 2924 | getitem = PyTuple_GetItem; |
| 2925 | } |
| 2926 | else { |
| 2927 | PyErr_SetString(PyExc_TypeError, |
| 2928 | "spawnvpe() arg 2 must be a tuple or list"); |
| 2929 | goto fail_0; |
| 2930 | } |
| 2931 | if (!PyMapping_Check(env)) { |
| 2932 | PyErr_SetString(PyExc_TypeError, |
| 2933 | "spawnvpe() arg 3 must be a mapping object"); |
| 2934 | goto fail_0; |
| 2935 | } |
| 2936 | |
| 2937 | argvlist = PyMem_NEW(char *, argc+1); |
| 2938 | if (argvlist == NULL) { |
| 2939 | PyErr_NoMemory(); |
| 2940 | goto fail_0; |
| 2941 | } |
| 2942 | for (i = 0; i < argc; i++) { |
| 2943 | if (!PyArg_Parse((*getitem)(argv, i), |
| 2944 | "et;spawnvpe() arg 2 must contain only strings", |
| 2945 | Py_FileSystemDefaultEncoding, |
| 2946 | &argvlist[i])) |
| 2947 | { |
| 2948 | lastarg = i; |
| 2949 | goto fail_1; |
| 2950 | } |
| 2951 | } |
| 2952 | lastarg = argc; |
| 2953 | argvlist[argc] = NULL; |
| 2954 | |
| 2955 | i = PyMapping_Size(env); |
| 2956 | if (i < 0) |
| 2957 | goto fail_1; |
| 2958 | envlist = PyMem_NEW(char *, i + 1); |
| 2959 | if (envlist == NULL) { |
| 2960 | PyErr_NoMemory(); |
| 2961 | goto fail_1; |
| 2962 | } |
| 2963 | envc = 0; |
| 2964 | keys = PyMapping_Keys(env); |
| 2965 | vals = PyMapping_Values(env); |
| 2966 | if (!keys || !vals) |
| 2967 | goto fail_2; |
| 2968 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2969 | PyErr_SetString(PyExc_TypeError, |
| 2970 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 2971 | goto fail_2; |
| 2972 | } |
| 2973 | |
| 2974 | for (pos = 0; pos < i; pos++) { |
| 2975 | char *p, *k, *v; |
| 2976 | size_t len; |
| 2977 | |
| 2978 | key = PyList_GetItem(keys, pos); |
| 2979 | val = PyList_GetItem(vals, pos); |
| 2980 | if (!key || !val) |
| 2981 | goto fail_2; |
| 2982 | |
| 2983 | if (!PyArg_Parse( |
| 2984 | key, |
| 2985 | "s;spawnvpe() arg 3 contains a non-string key", |
| 2986 | &k) || |
| 2987 | !PyArg_Parse( |
| 2988 | val, |
| 2989 | "s;spawnvpe() arg 3 contains a non-string value", |
| 2990 | &v)) |
| 2991 | { |
| 2992 | goto fail_2; |
| 2993 | } |
| 2994 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2995 | p = PyMem_NEW(char, len); |
| 2996 | if (p == NULL) { |
| 2997 | PyErr_NoMemory(); |
| 2998 | goto fail_2; |
| 2999 | } |
| 3000 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3001 | envlist[envc++] = p; |
| 3002 | } |
| 3003 | envlist[envc] = 0; |
| 3004 | |
| 3005 | Py_BEGIN_ALLOW_THREADS |
| 3006 | #if defined(PYCC_GCC) |
| 3007 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3008 | #else |
| 3009 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3010 | #endif |
| 3011 | Py_END_ALLOW_THREADS |
| 3012 | |
| 3013 | if (spawnval == -1) |
| 3014 | (void) posix_error(); |
| 3015 | else |
| 3016 | res = Py_BuildValue("l", (long) spawnval); |
| 3017 | |
| 3018 | fail_2: |
| 3019 | while (--envc >= 0) |
| 3020 | PyMem_DEL(envlist[envc]); |
| 3021 | PyMem_DEL(envlist); |
| 3022 | fail_1: |
| 3023 | free_string_array(argvlist, lastarg); |
| 3024 | Py_XDECREF(vals); |
| 3025 | Py_XDECREF(keys); |
| 3026 | fail_0: |
| 3027 | PyMem_Free(path); |
| 3028 | return res; |
| 3029 | } |
| 3030 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3031 | #endif /* HAVE_SPAWNV */ |
| 3032 | |
| 3033 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3034 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3035 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3036 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3037 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3038 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3039 | 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] | 3040 | |
| 3041 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3042 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3043 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3044 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3045 | if (pid == -1) |
| 3046 | return posix_error(); |
| 3047 | PyOS_AfterFork(); |
| 3048 | return PyInt_FromLong((long)pid); |
| 3049 | } |
| 3050 | #endif |
| 3051 | |
| 3052 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3053 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3054 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3055 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3056 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3057 | 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] | 3058 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3059 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3060 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3061 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3062 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3063 | if (pid == -1) |
| 3064 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3065 | if (pid == 0) |
| 3066 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3067 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3068 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3069 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3070 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3071 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3072 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3073 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3074 | #define DEV_PTY_FILE "/dev/ptc" |
| 3075 | #define HAVE_DEV_PTMX |
| 3076 | #else |
| 3077 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3078 | #endif |
| 3079 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3080 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3081 | #ifdef HAVE_PTY_H |
| 3082 | #include <pty.h> |
| 3083 | #else |
| 3084 | #ifdef HAVE_LIBUTIL_H |
| 3085 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3086 | #endif /* HAVE_LIBUTIL_H */ |
| 3087 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3088 | #ifdef HAVE_STROPTS_H |
| 3089 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3090 | #endif |
| 3091 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3092 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3093 | #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] | 3094 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3095 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3096 | 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] | 3097 | |
| 3098 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3099 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3100 | { |
| 3101 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3102 | #ifndef HAVE_OPENPTY |
| 3103 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3104 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3105 | #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] | 3106 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3107 | #ifdef sun |
Neal Norwitz | 84a98e0 | 2006-04-10 07:44:23 +0000 | [diff] [blame] | 3108 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3109 | #endif |
| 3110 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3111 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3112 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3113 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3114 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3115 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3116 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3117 | if (slave_name == NULL) |
| 3118 | return posix_error(); |
| 3119 | |
| 3120 | slave_fd = open(slave_name, O_RDWR); |
| 3121 | if (slave_fd < 0) |
| 3122 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3123 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3124 | 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] | 3125 | if (master_fd < 0) |
| 3126 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3127 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3128 | /* change permission of slave */ |
| 3129 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3130 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3131 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3132 | } |
| 3133 | /* unlock slave */ |
| 3134 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3135 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3136 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3137 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3138 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3139 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3140 | if (slave_name == NULL) |
| 3141 | return posix_error(); |
| 3142 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3143 | if (slave_fd < 0) |
| 3144 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3145 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3146 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3147 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3148 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3149 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3150 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3151 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3152 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3153 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3154 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3155 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3156 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3157 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3158 | |
| 3159 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3160 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3161 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3162 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3163 | 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] | 3164 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3165 | |
| 3166 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3167 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3168 | { |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 3169 | int master_fd = -1, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3170 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3171 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3172 | if (pid == -1) |
| 3173 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3174 | if (pid == 0) |
| 3175 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3176 | return Py_BuildValue("(ii)", pid, master_fd); |
| 3177 | } |
| 3178 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3179 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3180 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3181 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3182 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3183 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3184 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3185 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3186 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3187 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3188 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3189 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3190 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3191 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3192 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3193 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3194 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3195 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3196 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3197 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3198 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3199 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3200 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3201 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3202 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3203 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3204 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3205 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3206 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3207 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3208 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3209 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3210 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3211 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3212 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3213 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3214 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3215 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3216 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3217 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3218 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3219 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3220 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3221 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3222 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3223 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3224 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3225 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3226 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3229 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3230 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3231 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3232 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3233 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3234 | |
| 3235 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3236 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3237 | { |
| 3238 | PyObject *result = NULL; |
| 3239 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3240 | #ifdef NGROUPS_MAX |
| 3241 | #define MAX_GROUPS NGROUPS_MAX |
| 3242 | #else |
| 3243 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3244 | #define MAX_GROUPS 64 |
| 3245 | #endif |
| 3246 | gid_t grouplist[MAX_GROUPS]; |
| 3247 | int n; |
| 3248 | |
| 3249 | n = getgroups(MAX_GROUPS, grouplist); |
| 3250 | if (n < 0) |
| 3251 | posix_error(); |
| 3252 | else { |
| 3253 | result = PyList_New(n); |
| 3254 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3255 | int i; |
| 3256 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3257 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3258 | if (o == NULL) { |
| 3259 | Py_DECREF(result); |
| 3260 | result = NULL; |
| 3261 | break; |
| 3262 | } |
| 3263 | PyList_SET_ITEM(result, i, o); |
| 3264 | } |
| 3265 | } |
| 3266 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3267 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3268 | return result; |
| 3269 | } |
| 3270 | #endif |
| 3271 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3272 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3273 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3274 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3275 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3276 | |
| 3277 | static PyObject * |
| 3278 | posix_getpgid(PyObject *self, PyObject *args) |
| 3279 | { |
| 3280 | int pid, pgid; |
| 3281 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3282 | return NULL; |
| 3283 | pgid = getpgid(pid); |
| 3284 | if (pgid < 0) |
| 3285 | return posix_error(); |
| 3286 | return PyInt_FromLong((long)pgid); |
| 3287 | } |
| 3288 | #endif /* HAVE_GETPGID */ |
| 3289 | |
| 3290 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3291 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3292 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3293 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3294 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3295 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3296 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3297 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3298 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3299 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3300 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3301 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3302 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3303 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3304 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3305 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3306 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3307 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3308 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3309 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3310 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3311 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3312 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3313 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3314 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3315 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3316 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3317 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3318 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3319 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3320 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3321 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3322 | Py_INCREF(Py_None); |
| 3323 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3326 | #endif /* HAVE_SETPGRP */ |
| 3327 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3328 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3329 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3330 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3331 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3332 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3333 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3334 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3335 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3336 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3337 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3338 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3339 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3340 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3341 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3342 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3343 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3344 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3345 | |
| 3346 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3347 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3348 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3349 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3350 | char *name; |
| 3351 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3352 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3353 | errno = 0; |
| 3354 | name = getlogin(); |
| 3355 | if (name == NULL) { |
| 3356 | if (errno) |
| 3357 | posix_error(); |
| 3358 | else |
| 3359 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3360 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3361 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3362 | else |
| 3363 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3364 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3365 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3366 | return result; |
| 3367 | } |
| 3368 | #endif |
| 3369 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3370 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3371 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3372 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3373 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3374 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3375 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3376 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3377 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3378 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3379 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3380 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3381 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3382 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3383 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3384 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3385 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3386 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3387 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3388 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3389 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3390 | { |
| 3391 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3392 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3393 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3394 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3395 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3396 | APIRET rc; |
| 3397 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3398 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3399 | |
| 3400 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3401 | APIRET rc; |
| 3402 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3403 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3404 | |
| 3405 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3406 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3407 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3408 | if (kill(pid, sig) == -1) |
| 3409 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3410 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3411 | Py_INCREF(Py_None); |
| 3412 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3413 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3414 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3415 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3416 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3417 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3418 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3419 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3420 | |
| 3421 | static PyObject * |
| 3422 | posix_killpg(PyObject *self, PyObject *args) |
| 3423 | { |
| 3424 | int pgid, sig; |
| 3425 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3426 | return NULL; |
| 3427 | if (killpg(pgid, sig) == -1) |
| 3428 | return posix_error(); |
| 3429 | Py_INCREF(Py_None); |
| 3430 | return Py_None; |
| 3431 | } |
| 3432 | #endif |
| 3433 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3434 | #ifdef HAVE_PLOCK |
| 3435 | |
| 3436 | #ifdef HAVE_SYS_LOCK_H |
| 3437 | #include <sys/lock.h> |
| 3438 | #endif |
| 3439 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3440 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3441 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3442 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3443 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3444 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3445 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3446 | { |
| 3447 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3448 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3449 | return NULL; |
| 3450 | if (plock(op) == -1) |
| 3451 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3452 | Py_INCREF(Py_None); |
| 3453 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3454 | } |
| 3455 | #endif |
| 3456 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3457 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3458 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3459 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3460 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3461 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3462 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3463 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3464 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3465 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3466 | async_system(const char *command) |
| 3467 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3468 | char errormsg[256], args[1024]; |
| 3469 | RESULTCODES rcodes; |
| 3470 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3471 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3472 | char *shell = getenv("COMSPEC"); |
| 3473 | if (!shell) |
| 3474 | shell = "cmd"; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3475 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3476 | /* avoid overflowing the argument buffer */ |
| 3477 | if (strlen(shell) + 3 + strlen(command) >= 1024) |
| 3478 | return ERROR_NOT_ENOUGH_MEMORY |
| 3479 | |
| 3480 | args[0] = '\0'; |
| 3481 | strcat(args, shell); |
| 3482 | strcat(args, "/c "); |
| 3483 | strcat(args, command); |
| 3484 | |
| 3485 | /* execute asynchronously, inheriting the environment */ |
| 3486 | rc = DosExecPgm(errormsg, |
| 3487 | sizeof(errormsg), |
| 3488 | EXEC_ASYNC, |
| 3489 | args, |
| 3490 | NULL, |
| 3491 | &rcodes, |
| 3492 | shell); |
| 3493 | return rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3496 | static FILE * |
| 3497 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3498 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3499 | int oldfd, tgtfd; |
| 3500 | HFILE pipeh[2]; |
| 3501 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3502 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3503 | /* mode determines which of stdin or stdout is reconnected to |
| 3504 | * the pipe to the child |
| 3505 | */ |
| 3506 | if (strchr(mode, 'r') != NULL) { |
| 3507 | tgt_fd = 1; /* stdout */ |
| 3508 | } else if (strchr(mode, 'w')) { |
| 3509 | tgt_fd = 0; /* stdin */ |
| 3510 | } else { |
| 3511 | *err = ERROR_INVALID_ACCESS; |
| 3512 | return NULL; |
| 3513 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3514 | |
Andrew MacIntyre | a3be258 | 2004-12-18 09:51:05 +0000 | [diff] [blame] | 3515 | /* setup the pipe */ |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3516 | if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) { |
| 3517 | *err = rc; |
| 3518 | return NULL; |
| 3519 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3520 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3521 | /* prevent other threads accessing stdio */ |
| 3522 | DosEnterCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3523 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3524 | /* reconnect stdio and execute child */ |
| 3525 | oldfd = dup(tgtfd); |
| 3526 | close(tgtfd); |
| 3527 | if (dup2(pipeh[tgtfd], tgtfd) == 0) { |
| 3528 | DosClose(pipeh[tgtfd]); |
| 3529 | rc = async_system(command); |
| 3530 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3531 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3532 | /* restore stdio */ |
| 3533 | dup2(oldfd, tgtfd); |
| 3534 | close(oldfd); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3535 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3536 | /* allow other threads access to stdio */ |
| 3537 | DosExitCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3538 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3539 | /* if execution of child was successful return file stream */ |
| 3540 | if (rc == NO_ERROR) |
| 3541 | return fdopen(pipeh[1 - tgtfd], mode); |
| 3542 | else { |
| 3543 | DosClose(pipeh[1 - tgtfd]); |
| 3544 | *err = rc; |
| 3545 | return NULL; |
| 3546 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3550 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3551 | { |
| 3552 | char *name; |
| 3553 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3554 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3555 | FILE *fp; |
| 3556 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3557 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3558 | return NULL; |
| 3559 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3560 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3561 | Py_END_ALLOW_THREADS |
| 3562 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3563 | return os2_error(err); |
| 3564 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3565 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 3566 | if (f != NULL) |
| 3567 | PyFile_SetBufSize(f, bufsize); |
| 3568 | return f; |
| 3569 | } |
| 3570 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3571 | #elif defined(PYCC_GCC) |
| 3572 | |
| 3573 | /* standard posix version of popen() support */ |
| 3574 | static PyObject * |
| 3575 | posix_popen(PyObject *self, PyObject *args) |
| 3576 | { |
| 3577 | char *name; |
| 3578 | char *mode = "r"; |
| 3579 | int bufsize = -1; |
| 3580 | FILE *fp; |
| 3581 | PyObject *f; |
| 3582 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 3583 | return NULL; |
| 3584 | Py_BEGIN_ALLOW_THREADS |
| 3585 | fp = popen(name, mode); |
| 3586 | Py_END_ALLOW_THREADS |
| 3587 | if (fp == NULL) |
| 3588 | return posix_error(); |
| 3589 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 3590 | if (f != NULL) |
| 3591 | PyFile_SetBufSize(f, bufsize); |
| 3592 | return f; |
| 3593 | } |
| 3594 | |
| 3595 | /* fork() under OS/2 has lots'o'warts |
| 3596 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 3597 | * most of this code is a ripoff of the win32 code, but using the |
| 3598 | * capabilities of EMX's C library routines |
| 3599 | */ |
| 3600 | |
| 3601 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 3602 | #define POPEN_1 1 |
| 3603 | #define POPEN_2 2 |
| 3604 | #define POPEN_3 3 |
| 3605 | #define POPEN_4 4 |
| 3606 | |
| 3607 | static PyObject *_PyPopen(char *, int, int, int); |
| 3608 | static int _PyPclose(FILE *file); |
| 3609 | |
| 3610 | /* |
| 3611 | * Internal dictionary mapping popen* file pointers to process handles, |
| 3612 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3613 | * for more information on this dictionary's use. |
| 3614 | */ |
| 3615 | static PyObject *_PyPopenProcs = NULL; |
| 3616 | |
| 3617 | /* os2emx version of popen2() |
| 3618 | * |
| 3619 | * The result of this function is a pipe (file) connected to the |
| 3620 | * process's stdin, and a pipe connected to the process's stdout. |
| 3621 | */ |
| 3622 | |
| 3623 | static PyObject * |
| 3624 | os2emx_popen2(PyObject *self, PyObject *args) |
| 3625 | { |
| 3626 | PyObject *f; |
| 3627 | int tm=0; |
| 3628 | |
| 3629 | char *cmdstring; |
| 3630 | char *mode = "t"; |
| 3631 | int bufsize = -1; |
| 3632 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 3633 | return NULL; |
| 3634 | |
| 3635 | if (*mode == 't') |
| 3636 | tm = O_TEXT; |
| 3637 | else if (*mode != 'b') { |
| 3638 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3639 | return NULL; |
| 3640 | } else |
| 3641 | tm = O_BINARY; |
| 3642 | |
| 3643 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 3644 | |
| 3645 | return f; |
| 3646 | } |
| 3647 | |
| 3648 | /* |
| 3649 | * Variation on os2emx.popen2 |
| 3650 | * |
| 3651 | * The result of this function is 3 pipes - the process's stdin, |
| 3652 | * stdout and stderr |
| 3653 | */ |
| 3654 | |
| 3655 | static PyObject * |
| 3656 | os2emx_popen3(PyObject *self, PyObject *args) |
| 3657 | { |
| 3658 | PyObject *f; |
| 3659 | int tm = 0; |
| 3660 | |
| 3661 | char *cmdstring; |
| 3662 | char *mode = "t"; |
| 3663 | int bufsize = -1; |
| 3664 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 3665 | return NULL; |
| 3666 | |
| 3667 | if (*mode == 't') |
| 3668 | tm = O_TEXT; |
| 3669 | else if (*mode != 'b') { |
| 3670 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3671 | return NULL; |
| 3672 | } else |
| 3673 | tm = O_BINARY; |
| 3674 | |
| 3675 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 3676 | |
| 3677 | return f; |
| 3678 | } |
| 3679 | |
| 3680 | /* |
| 3681 | * Variation on os2emx.popen2 |
| 3682 | * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3683 | * The result of this function is 2 pipes - the processes stdin, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3684 | * and stdout+stderr combined as a single pipe. |
| 3685 | */ |
| 3686 | |
| 3687 | static PyObject * |
| 3688 | os2emx_popen4(PyObject *self, PyObject *args) |
| 3689 | { |
| 3690 | PyObject *f; |
| 3691 | int tm = 0; |
| 3692 | |
| 3693 | char *cmdstring; |
| 3694 | char *mode = "t"; |
| 3695 | int bufsize = -1; |
| 3696 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 3697 | return NULL; |
| 3698 | |
| 3699 | if (*mode == 't') |
| 3700 | tm = O_TEXT; |
| 3701 | else if (*mode != 'b') { |
| 3702 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3703 | return NULL; |
| 3704 | } else |
| 3705 | tm = O_BINARY; |
| 3706 | |
| 3707 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 3708 | |
| 3709 | return f; |
| 3710 | } |
| 3711 | |
| 3712 | /* a couple of structures for convenient handling of multiple |
| 3713 | * file handles and pipes |
| 3714 | */ |
| 3715 | struct file_ref |
| 3716 | { |
| 3717 | int handle; |
| 3718 | int flags; |
| 3719 | }; |
| 3720 | |
| 3721 | struct pipe_ref |
| 3722 | { |
| 3723 | int rd; |
| 3724 | int wr; |
| 3725 | }; |
| 3726 | |
| 3727 | /* The following code is derived from the win32 code */ |
| 3728 | |
| 3729 | static PyObject * |
| 3730 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 3731 | { |
| 3732 | struct file_ref stdio[3]; |
| 3733 | struct pipe_ref p_fd[3]; |
| 3734 | FILE *p_s[3]; |
| 3735 | int file_count, i, pipe_err, pipe_pid; |
| 3736 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 3737 | PyObject *f, *p_f[3]; |
| 3738 | |
| 3739 | /* file modes for subsequent fdopen's on pipe handles */ |
| 3740 | if (mode == O_TEXT) |
| 3741 | { |
| 3742 | rd_mode = "rt"; |
| 3743 | wr_mode = "wt"; |
| 3744 | } |
| 3745 | else |
| 3746 | { |
| 3747 | rd_mode = "rb"; |
| 3748 | wr_mode = "wb"; |
| 3749 | } |
| 3750 | |
| 3751 | /* prepare shell references */ |
| 3752 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 3753 | if ((shell = getenv("COMSPEC")) == NULL) |
| 3754 | { |
| 3755 | errno = ENOENT; |
| 3756 | return posix_error(); |
| 3757 | } |
| 3758 | |
| 3759 | sh_name = _getname(shell); |
| 3760 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 3761 | opt = "/c"; |
| 3762 | else |
| 3763 | opt = "-c"; |
| 3764 | |
| 3765 | /* save current stdio fds + their flags, and set not inheritable */ |
| 3766 | i = pipe_err = 0; |
| 3767 | while (pipe_err >= 0 && i < 3) |
| 3768 | { |
| 3769 | pipe_err = stdio[i].handle = dup(i); |
| 3770 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 3771 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 3772 | i++; |
| 3773 | } |
| 3774 | if (pipe_err < 0) |
| 3775 | { |
| 3776 | /* didn't get them all saved - clean up and bail out */ |
| 3777 | int saved_err = errno; |
| 3778 | while (i-- > 0) |
| 3779 | { |
| 3780 | close(stdio[i].handle); |
| 3781 | } |
| 3782 | errno = saved_err; |
| 3783 | return posix_error(); |
| 3784 | } |
| 3785 | |
| 3786 | /* create pipe ends */ |
| 3787 | file_count = 2; |
| 3788 | if (n == POPEN_3) |
| 3789 | file_count = 3; |
| 3790 | i = pipe_err = 0; |
| 3791 | while ((pipe_err == 0) && (i < file_count)) |
| 3792 | pipe_err = pipe((int *)&p_fd[i++]); |
| 3793 | if (pipe_err < 0) |
| 3794 | { |
| 3795 | /* didn't get them all made - clean up and bail out */ |
| 3796 | while (i-- > 0) |
| 3797 | { |
| 3798 | close(p_fd[i].wr); |
| 3799 | close(p_fd[i].rd); |
| 3800 | } |
| 3801 | errno = EPIPE; |
| 3802 | return posix_error(); |
| 3803 | } |
| 3804 | |
| 3805 | /* change the actual standard IO streams over temporarily, |
| 3806 | * making the retained pipe ends non-inheritable |
| 3807 | */ |
| 3808 | pipe_err = 0; |
| 3809 | |
| 3810 | /* - stdin */ |
| 3811 | if (dup2(p_fd[0].rd, 0) == 0) |
| 3812 | { |
| 3813 | close(p_fd[0].rd); |
| 3814 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 3815 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 3816 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 3817 | { |
| 3818 | close(p_fd[0].wr); |
| 3819 | pipe_err = -1; |
| 3820 | } |
| 3821 | } |
| 3822 | else |
| 3823 | { |
| 3824 | pipe_err = -1; |
| 3825 | } |
| 3826 | |
| 3827 | /* - stdout */ |
| 3828 | if (pipe_err == 0) |
| 3829 | { |
| 3830 | if (dup2(p_fd[1].wr, 1) == 1) |
| 3831 | { |
| 3832 | close(p_fd[1].wr); |
| 3833 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 3834 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 3835 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 3836 | { |
| 3837 | close(p_fd[1].rd); |
| 3838 | pipe_err = -1; |
| 3839 | } |
| 3840 | } |
| 3841 | else |
| 3842 | { |
| 3843 | pipe_err = -1; |
| 3844 | } |
| 3845 | } |
| 3846 | |
| 3847 | /* - stderr, as required */ |
| 3848 | if (pipe_err == 0) |
| 3849 | switch (n) |
| 3850 | { |
| 3851 | case POPEN_3: |
| 3852 | { |
| 3853 | if (dup2(p_fd[2].wr, 2) == 2) |
| 3854 | { |
| 3855 | close(p_fd[2].wr); |
| 3856 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 3857 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 3858 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 3859 | { |
| 3860 | close(p_fd[2].rd); |
| 3861 | pipe_err = -1; |
| 3862 | } |
| 3863 | } |
| 3864 | else |
| 3865 | { |
| 3866 | pipe_err = -1; |
| 3867 | } |
| 3868 | break; |
| 3869 | } |
| 3870 | |
| 3871 | case POPEN_4: |
| 3872 | { |
| 3873 | if (dup2(1, 2) != 2) |
| 3874 | { |
| 3875 | pipe_err = -1; |
| 3876 | } |
| 3877 | break; |
| 3878 | } |
| 3879 | } |
| 3880 | |
| 3881 | /* spawn the child process */ |
| 3882 | if (pipe_err == 0) |
| 3883 | { |
| 3884 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 3885 | if (pipe_pid == -1) |
| 3886 | { |
| 3887 | pipe_err = -1; |
| 3888 | } |
| 3889 | else |
| 3890 | { |
| 3891 | /* save the PID into the FILE structure |
| 3892 | * NOTE: this implementation doesn't actually |
| 3893 | * take advantage of this, but do it for |
| 3894 | * completeness - AIM Apr01 |
| 3895 | */ |
| 3896 | for (i = 0; i < file_count; i++) |
| 3897 | p_s[i]->_pid = pipe_pid; |
| 3898 | } |
| 3899 | } |
| 3900 | |
| 3901 | /* reset standard IO to normal */ |
| 3902 | for (i = 0; i < 3; i++) |
| 3903 | { |
| 3904 | dup2(stdio[i].handle, i); |
| 3905 | fcntl(i, F_SETFD, stdio[i].flags); |
| 3906 | close(stdio[i].handle); |
| 3907 | } |
| 3908 | |
| 3909 | /* if any remnant problems, clean up and bail out */ |
| 3910 | if (pipe_err < 0) |
| 3911 | { |
| 3912 | for (i = 0; i < 3; i++) |
| 3913 | { |
| 3914 | close(p_fd[i].rd); |
| 3915 | close(p_fd[i].wr); |
| 3916 | } |
| 3917 | errno = EPIPE; |
| 3918 | return posix_error_with_filename(cmdstring); |
| 3919 | } |
| 3920 | |
| 3921 | /* build tuple of file objects to return */ |
| 3922 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 3923 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3924 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3925 | PyFile_SetBufSize(p_f[1], bufsize); |
| 3926 | if (n == POPEN_3) |
| 3927 | { |
| 3928 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3929 | PyFile_SetBufSize(p_f[0], bufsize); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3930 | 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] | 3931 | } |
| 3932 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3933 | f = PyTuple_Pack(2, p_f[0], p_f[1]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3934 | |
| 3935 | /* |
| 3936 | * Insert the files we've created into the process dictionary |
| 3937 | * all referencing the list with the process handle and the |
| 3938 | * initial number of files (see description below in _PyPclose). |
| 3939 | * Since if _PyPclose later tried to wait on a process when all |
| 3940 | * handles weren't closed, it could create a deadlock with the |
| 3941 | * child, we spend some energy here to try to ensure that we |
| 3942 | * either insert all file handles into the dictionary or none |
| 3943 | * at all. It's a little clumsy with the various popen modes |
| 3944 | * and variable number of files involved. |
| 3945 | */ |
| 3946 | if (!_PyPopenProcs) |
| 3947 | { |
| 3948 | _PyPopenProcs = PyDict_New(); |
| 3949 | } |
| 3950 | |
| 3951 | if (_PyPopenProcs) |
| 3952 | { |
| 3953 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 3954 | int ins_rc[3]; |
| 3955 | |
| 3956 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 3957 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 3958 | |
| 3959 | procObj = PyList_New(2); |
| 3960 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 3961 | intObj = PyInt_FromLong((long) file_count); |
| 3962 | |
| 3963 | if (procObj && pidObj && intObj) |
| 3964 | { |
| 3965 | PyList_SetItem(procObj, 0, pidObj); |
| 3966 | PyList_SetItem(procObj, 1, intObj); |
| 3967 | |
| 3968 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 3969 | if (fileObj[0]) |
| 3970 | { |
| 3971 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 3972 | fileObj[0], |
| 3973 | procObj); |
| 3974 | } |
| 3975 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 3976 | if (fileObj[1]) |
| 3977 | { |
| 3978 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 3979 | fileObj[1], |
| 3980 | procObj); |
| 3981 | } |
| 3982 | if (file_count >= 3) |
| 3983 | { |
| 3984 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 3985 | if (fileObj[2]) |
| 3986 | { |
| 3987 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 3988 | fileObj[2], |
| 3989 | procObj); |
| 3990 | } |
| 3991 | } |
| 3992 | |
| 3993 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 3994 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 3995 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 3996 | { |
| 3997 | /* Something failed - remove any dictionary |
| 3998 | * entries that did make it. |
| 3999 | */ |
| 4000 | if (!ins_rc[0] && fileObj[0]) |
| 4001 | { |
| 4002 | PyDict_DelItem(_PyPopenProcs, |
| 4003 | fileObj[0]); |
| 4004 | } |
| 4005 | if (!ins_rc[1] && fileObj[1]) |
| 4006 | { |
| 4007 | PyDict_DelItem(_PyPopenProcs, |
| 4008 | fileObj[1]); |
| 4009 | } |
| 4010 | if (!ins_rc[2] && fileObj[2]) |
| 4011 | { |
| 4012 | PyDict_DelItem(_PyPopenProcs, |
| 4013 | fileObj[2]); |
| 4014 | } |
| 4015 | } |
| 4016 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4017 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4018 | /* |
| 4019 | * Clean up our localized references for the dictionary keys |
| 4020 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4021 | * that got placed in the dictionary. |
| 4022 | */ |
| 4023 | Py_XDECREF(procObj); |
| 4024 | Py_XDECREF(fileObj[0]); |
| 4025 | Py_XDECREF(fileObj[1]); |
| 4026 | Py_XDECREF(fileObj[2]); |
| 4027 | } |
| 4028 | |
| 4029 | /* Child is launched. */ |
| 4030 | return f; |
| 4031 | } |
| 4032 | |
| 4033 | /* |
| 4034 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4035 | * exit code for the child process and return as a result of the close. |
| 4036 | * |
| 4037 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4038 | * input file pointer to information about the process that was |
| 4039 | * originally created by the popen* call that created the file pointer. |
| 4040 | * The dictionary uses the file pointer as a key (with one entry |
| 4041 | * inserted for each file returned by the original popen* call) and a |
| 4042 | * single list object as the value for all files from a single call. |
| 4043 | * The list object contains the Win32 process handle at [0], and a file |
| 4044 | * count at [1], which is initialized to the total number of file |
| 4045 | * handles using that list. |
| 4046 | * |
| 4047 | * This function closes whichever handle it is passed, and decrements |
| 4048 | * the file count in the dictionary for the process handle pointed to |
| 4049 | * by this file. On the last close (when the file count reaches zero), |
| 4050 | * this function will wait for the child process and then return its |
| 4051 | * exit code as the result of the close() operation. This permits the |
| 4052 | * files to be closed in any order - it is always the close() of the |
| 4053 | * final handle that will return the exit code. |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4054 | * |
| 4055 | * NOTE: This function is currently called with the GIL released. |
| 4056 | * hence we use the GILState API to manage our state. |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4057 | */ |
| 4058 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4059 | static int _PyPclose(FILE *file) |
| 4060 | { |
| 4061 | int result; |
| 4062 | int exit_code; |
| 4063 | int pipe_pid; |
| 4064 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 4065 | int file_count; |
| 4066 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4067 | PyGILState_STATE state; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4068 | #endif |
| 4069 | |
| 4070 | /* Close the file handle first, to ensure it can't block the |
| 4071 | * child from exiting if it's the last handle. |
| 4072 | */ |
| 4073 | result = fclose(file); |
| 4074 | |
| 4075 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4076 | state = PyGILState_Ensure(); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4077 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4078 | if (_PyPopenProcs) |
| 4079 | { |
| 4080 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4081 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4082 | fileObj)) != NULL && |
| 4083 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 4084 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 4085 | { |
| 4086 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 4087 | file_count = (int) PyInt_AsLong(intObj); |
| 4088 | |
| 4089 | if (file_count > 1) |
| 4090 | { |
| 4091 | /* Still other files referencing process */ |
| 4092 | file_count--; |
| 4093 | PyList_SetItem(procObj,1, |
| 4094 | PyInt_FromLong((long) file_count)); |
| 4095 | } |
| 4096 | else |
| 4097 | { |
| 4098 | /* Last file for this process */ |
| 4099 | if (result != EOF && |
| 4100 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 4101 | { |
| 4102 | /* extract exit status */ |
| 4103 | if (WIFEXITED(exit_code)) |
| 4104 | { |
| 4105 | result = WEXITSTATUS(exit_code); |
| 4106 | } |
| 4107 | else |
| 4108 | { |
| 4109 | errno = EPIPE; |
| 4110 | result = -1; |
| 4111 | } |
| 4112 | } |
| 4113 | else |
| 4114 | { |
| 4115 | /* Indicate failure - this will cause the file object |
| 4116 | * to raise an I/O error and translate the last |
| 4117 | * error code from errno. We do have a problem with |
| 4118 | * last errors that overlap the normal errno table, |
| 4119 | * but that's a consistent problem with the file object. |
| 4120 | */ |
| 4121 | result = -1; |
| 4122 | } |
| 4123 | } |
| 4124 | |
| 4125 | /* Remove this file pointer from dictionary */ |
| 4126 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4127 | |
| 4128 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 4129 | { |
| 4130 | Py_DECREF(_PyPopenProcs); |
| 4131 | _PyPopenProcs = NULL; |
| 4132 | } |
| 4133 | |
| 4134 | } /* if object retrieval ok */ |
| 4135 | |
| 4136 | Py_XDECREF(fileObj); |
| 4137 | } /* if _PyPopenProcs */ |
| 4138 | |
| 4139 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4140 | PyGILState_Release(state); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4141 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4142 | return result; |
| 4143 | } |
| 4144 | |
| 4145 | #endif /* PYCC_??? */ |
| 4146 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4147 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4148 | |
| 4149 | /* |
| 4150 | * Portable 'popen' replacement for Win32. |
| 4151 | * |
| 4152 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 4153 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4154 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4155 | */ |
| 4156 | |
| 4157 | #include <malloc.h> |
| 4158 | #include <io.h> |
| 4159 | #include <fcntl.h> |
| 4160 | |
| 4161 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 4162 | #define POPEN_1 1 |
| 4163 | #define POPEN_2 2 |
| 4164 | #define POPEN_3 3 |
| 4165 | #define POPEN_4 4 |
| 4166 | |
| 4167 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4168 | static int _PyPclose(FILE *file); |
| 4169 | |
| 4170 | /* |
| 4171 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4172 | * for use when retrieving the process exit code. See _PyPclose() below |
| 4173 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4174 | */ |
| 4175 | static PyObject *_PyPopenProcs = NULL; |
| 4176 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4177 | |
| 4178 | /* popen that works from a GUI. |
| 4179 | * |
| 4180 | * The result of this function is a pipe (file) connected to the |
| 4181 | * processes stdin or stdout, depending on the requested mode. |
| 4182 | */ |
| 4183 | |
| 4184 | static PyObject * |
| 4185 | posix_popen(PyObject *self, PyObject *args) |
| 4186 | { |
Raymond Hettinger | b5cb665 | 2003-09-01 22:25:41 +0000 | [diff] [blame] | 4187 | PyObject *f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4188 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4189 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4190 | char *cmdstring; |
| 4191 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4192 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4193 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4194 | return NULL; |
| 4195 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4196 | if (*mode == 'r') |
| 4197 | tm = _O_RDONLY; |
| 4198 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4199 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4200 | return NULL; |
| 4201 | } else |
| 4202 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4203 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4204 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4205 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4206 | return NULL; |
| 4207 | } |
| 4208 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4209 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4210 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4211 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4212 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4213 | else |
| 4214 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 4215 | |
| 4216 | return f; |
| 4217 | } |
| 4218 | |
| 4219 | /* Variation on win32pipe.popen |
| 4220 | * |
| 4221 | * The result of this function is a pipe (file) connected to the |
| 4222 | * process's stdin, and a pipe connected to the process's stdout. |
| 4223 | */ |
| 4224 | |
| 4225 | static PyObject * |
| 4226 | win32_popen2(PyObject *self, PyObject *args) |
| 4227 | { |
| 4228 | PyObject *f; |
| 4229 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4230 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4231 | char *cmdstring; |
| 4232 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4233 | int bufsize = -1; |
| 4234 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4235 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4236 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4237 | if (*mode == 't') |
| 4238 | tm = _O_TEXT; |
| 4239 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4240 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4241 | return NULL; |
| 4242 | } else |
| 4243 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4244 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4245 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4246 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4247 | return NULL; |
| 4248 | } |
| 4249 | |
| 4250 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4251 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4252 | return f; |
| 4253 | } |
| 4254 | |
| 4255 | /* |
| 4256 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4257 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4258 | * The result of this function is 3 pipes - the process's stdin, |
| 4259 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4260 | */ |
| 4261 | |
| 4262 | static PyObject * |
| 4263 | win32_popen3(PyObject *self, PyObject *args) |
| 4264 | { |
| 4265 | PyObject *f; |
| 4266 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4267 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4268 | char *cmdstring; |
| 4269 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4270 | int bufsize = -1; |
| 4271 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4272 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4273 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4274 | if (*mode == 't') |
| 4275 | tm = _O_TEXT; |
| 4276 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4277 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4278 | return NULL; |
| 4279 | } else |
| 4280 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4281 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4282 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4283 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4284 | return NULL; |
| 4285 | } |
| 4286 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4287 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4288 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4289 | return f; |
| 4290 | } |
| 4291 | |
| 4292 | /* |
| 4293 | * Variation on win32pipe.popen |
| 4294 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4295 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4296 | * and stdout+stderr combined as a single pipe. |
| 4297 | */ |
| 4298 | |
| 4299 | static PyObject * |
| 4300 | win32_popen4(PyObject *self, PyObject *args) |
| 4301 | { |
| 4302 | PyObject *f; |
| 4303 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4304 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4305 | char *cmdstring; |
| 4306 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4307 | int bufsize = -1; |
| 4308 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4309 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4310 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4311 | if (*mode == 't') |
| 4312 | tm = _O_TEXT; |
| 4313 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4314 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4315 | return NULL; |
| 4316 | } else |
| 4317 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4318 | |
| 4319 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4320 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4321 | return NULL; |
| 4322 | } |
| 4323 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4324 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4325 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4326 | return f; |
| 4327 | } |
| 4328 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4329 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4330 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4331 | HANDLE hStdin, |
| 4332 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4333 | HANDLE hStderr, |
| 4334 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4335 | { |
| 4336 | PROCESS_INFORMATION piProcInfo; |
| 4337 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4338 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4339 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4340 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4341 | int i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4342 | Py_ssize_t x; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4343 | |
| 4344 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4345 | char *comshell; |
| 4346 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4347 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4348 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4349 | /* x < i, so x fits into an integer */ |
| 4350 | return (int)x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4351 | |
| 4352 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 4353 | * then use the w9xpopen hack. |
| 4354 | */ |
| 4355 | comshell = s1 + x; |
| 4356 | while (comshell >= s1 && *comshell != '\\') |
| 4357 | --comshell; |
| 4358 | ++comshell; |
| 4359 | |
| 4360 | if (GetVersion() < 0x80000000 && |
| 4361 | _stricmp(comshell, "command.com") != 0) { |
| 4362 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4363 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4364 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4365 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4366 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4367 | } |
| 4368 | else { |
| 4369 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4370 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 4371 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4372 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4373 | char modulepath[_MAX_PATH]; |
| 4374 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4375 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 4376 | for (i = x = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4377 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4378 | x = i+1; |
| 4379 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4380 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4381 | strncat(modulepath, |
| 4382 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4383 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4384 | -strlen(modulepath)); |
| 4385 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4386 | /* Eeek - file-not-found - possibly an embedding |
| 4387 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4388 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4389 | strncpy(modulepath, |
| 4390 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4391 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 4392 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 4393 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4394 | strncat(modulepath, |
| 4395 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4396 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4397 | -strlen(modulepath)); |
| 4398 | /* No where else to look - raise an easily identifiable |
| 4399 | error, rather than leaving Windows to report |
| 4400 | "file not found" - as the user is probably blissfully |
| 4401 | unaware this shim EXE is used, and it will confuse them. |
| 4402 | (well, it confused me for a while ;-) |
| 4403 | */ |
| 4404 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4405 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4406 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4407 | "for popen to work with your shell " |
| 4408 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4409 | szConsoleSpawn); |
| 4410 | return FALSE; |
| 4411 | } |
| 4412 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4413 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4414 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4415 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4416 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4417 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4418 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4419 | /* To maintain correct argument passing semantics, |
| 4420 | we pass the command-line as it stands, and allow |
| 4421 | quoting to be applied. w9xpopen.exe will then |
| 4422 | use its argv vector, and re-quote the necessary |
| 4423 | args for the ultimate child process. |
| 4424 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4425 | PyOS_snprintf( |
| 4426 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4427 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4428 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4429 | s1, |
| 4430 | s3, |
| 4431 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4432 | /* Not passing CREATE_NEW_CONSOLE has been known to |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4433 | cause random failures on win9x. Specifically a |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4434 | dialog: |
| 4435 | "Your program accessed mem currently in use at xxx" |
| 4436 | and a hopeful warning about the stability of your |
| 4437 | system. |
| 4438 | Cost is Ctrl+C wont kill children, but anyone |
| 4439 | who cares can have a go! |
| 4440 | */ |
| 4441 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4442 | } |
| 4443 | } |
| 4444 | |
| 4445 | /* Could be an else here to try cmd.exe / command.com in the path |
| 4446 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4447 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4448 | PyErr_SetString(PyExc_RuntimeError, |
| 4449 | "Cannot locate a COMSPEC environment variable to " |
| 4450 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4451 | return FALSE; |
| 4452 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4453 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4454 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 4455 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 4456 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 4457 | siStartInfo.hStdInput = hStdin; |
| 4458 | siStartInfo.hStdOutput = hStdout; |
| 4459 | siStartInfo.hStdError = hStderr; |
| 4460 | siStartInfo.wShowWindow = SW_HIDE; |
| 4461 | |
| 4462 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4463 | s2, |
| 4464 | NULL, |
| 4465 | NULL, |
| 4466 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4467 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4468 | NULL, |
| 4469 | NULL, |
| 4470 | &siStartInfo, |
| 4471 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4472 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4473 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4474 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4475 | /* Return process handle */ |
| 4476 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4477 | return TRUE; |
| 4478 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4479 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4480 | return FALSE; |
| 4481 | } |
| 4482 | |
| 4483 | /* The following code is based off of KB: Q190351 */ |
| 4484 | |
| 4485 | static PyObject * |
| 4486 | _PyPopen(char *cmdstring, int mode, int n) |
| 4487 | { |
| 4488 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 4489 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4490 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4491 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4492 | SECURITY_ATTRIBUTES saAttr; |
| 4493 | BOOL fSuccess; |
| 4494 | int fd1, fd2, fd3; |
| 4495 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4496 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4497 | PyObject *f; |
| 4498 | |
| 4499 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 4500 | saAttr.bInheritHandle = TRUE; |
| 4501 | saAttr.lpSecurityDescriptor = NULL; |
| 4502 | |
| 4503 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 4504 | return win32_error("CreatePipe", NULL); |
| 4505 | |
| 4506 | /* Create new output read handle and the input write handle. Set |
| 4507 | * the inheritance properties to FALSE. Otherwise, the child inherits |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 4508 | * these handles; resulting in non-closeable handles to the pipes |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4509 | * being created. */ |
| 4510 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4511 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 4512 | FALSE, |
| 4513 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4514 | if (!fSuccess) |
| 4515 | return win32_error("DuplicateHandle", NULL); |
| 4516 | |
| 4517 | /* Close the inheritable version of ChildStdin |
| 4518 | that we're using. */ |
| 4519 | CloseHandle(hChildStdinWr); |
| 4520 | |
| 4521 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 4522 | return win32_error("CreatePipe", NULL); |
| 4523 | |
| 4524 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4525 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 4526 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4527 | if (!fSuccess) |
| 4528 | return win32_error("DuplicateHandle", NULL); |
| 4529 | |
| 4530 | /* Close the inheritable version of ChildStdout |
| 4531 | that we're using. */ |
| 4532 | CloseHandle(hChildStdoutRd); |
| 4533 | |
| 4534 | if (n != POPEN_4) { |
| 4535 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 4536 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4537 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 4538 | hChildStderrRd, |
| 4539 | GetCurrentProcess(), |
| 4540 | &hChildStderrRdDup, 0, |
| 4541 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4542 | if (!fSuccess) |
| 4543 | return win32_error("DuplicateHandle", NULL); |
| 4544 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 4545 | CloseHandle(hChildStderrRd); |
| 4546 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4547 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4548 | switch (n) { |
| 4549 | case POPEN_1: |
| 4550 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 4551 | case _O_WRONLY | _O_TEXT: |
| 4552 | /* Case for writing to child Stdin in text mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4553 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4554 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4555 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4556 | PyFile_SetBufSize(f, 0); |
| 4557 | /* We don't care about these pipes anymore, so close them. */ |
| 4558 | CloseHandle(hChildStdoutRdDup); |
| 4559 | CloseHandle(hChildStderrRdDup); |
| 4560 | break; |
| 4561 | |
| 4562 | case _O_RDONLY | _O_TEXT: |
| 4563 | /* Case for reading from child Stdout in text mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4564 | fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4565 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4566 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4567 | PyFile_SetBufSize(f, 0); |
| 4568 | /* We don't care about these pipes anymore, so close them. */ |
| 4569 | CloseHandle(hChildStdinWrDup); |
| 4570 | CloseHandle(hChildStderrRdDup); |
| 4571 | break; |
| 4572 | |
| 4573 | case _O_RDONLY | _O_BINARY: |
| 4574 | /* Case for readinig from child Stdout in binary mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4575 | fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4576 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4577 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4578 | PyFile_SetBufSize(f, 0); |
| 4579 | /* We don't care about these pipes anymore, so close them. */ |
| 4580 | CloseHandle(hChildStdinWrDup); |
| 4581 | CloseHandle(hChildStderrRdDup); |
| 4582 | break; |
| 4583 | |
| 4584 | case _O_WRONLY | _O_BINARY: |
| 4585 | /* Case for writing to child Stdin in binary mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4586 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4587 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4588 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4589 | PyFile_SetBufSize(f, 0); |
| 4590 | /* We don't care about these pipes anymore, so close them. */ |
| 4591 | CloseHandle(hChildStdoutRdDup); |
| 4592 | CloseHandle(hChildStderrRdDup); |
| 4593 | break; |
| 4594 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4595 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4596 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4597 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4598 | case POPEN_2: |
| 4599 | case POPEN_4: |
| 4600 | { |
| 4601 | char *m1, *m2; |
| 4602 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4603 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4604 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4605 | m1 = "r"; |
| 4606 | m2 = "w"; |
| 4607 | } else { |
| 4608 | m1 = "rb"; |
| 4609 | m2 = "wb"; |
| 4610 | } |
| 4611 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4612 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4613 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4614 | fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4615 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4616 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4617 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4618 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4619 | PyFile_SetBufSize(p2, 0); |
| 4620 | |
| 4621 | if (n != 4) |
| 4622 | CloseHandle(hChildStderrRdDup); |
| 4623 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4624 | f = PyTuple_Pack(2,p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4625 | Py_XDECREF(p1); |
| 4626 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4627 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4628 | break; |
| 4629 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4630 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4631 | case POPEN_3: |
| 4632 | { |
| 4633 | char *m1, *m2; |
| 4634 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4635 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4636 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4637 | m1 = "r"; |
| 4638 | m2 = "w"; |
| 4639 | } else { |
| 4640 | m1 = "rb"; |
| 4641 | m2 = "wb"; |
| 4642 | } |
| 4643 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4644 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4645 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4646 | fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4647 | f2 = _fdopen(fd2, m1); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4648 | fd3 = _open_osfhandle((intptr_t)hChildStderrRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4649 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4650 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4651 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 4652 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4653 | PyFile_SetBufSize(p1, 0); |
| 4654 | PyFile_SetBufSize(p2, 0); |
| 4655 | PyFile_SetBufSize(p3, 0); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4656 | f = PyTuple_Pack(3,p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4657 | Py_XDECREF(p1); |
| 4658 | Py_XDECREF(p2); |
| 4659 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4660 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4661 | break; |
| 4662 | } |
| 4663 | } |
| 4664 | |
| 4665 | if (n == POPEN_4) { |
| 4666 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4667 | hChildStdinRd, |
| 4668 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4669 | hChildStdoutWr, |
| 4670 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4671 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4672 | } |
| 4673 | else { |
| 4674 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4675 | hChildStdinRd, |
| 4676 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4677 | hChildStderrWr, |
| 4678 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4679 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4680 | } |
| 4681 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4682 | /* |
| 4683 | * Insert the files we've created into the process dictionary |
| 4684 | * all referencing the list with the process handle and the |
| 4685 | * initial number of files (see description below in _PyPclose). |
| 4686 | * Since if _PyPclose later tried to wait on a process when all |
| 4687 | * handles weren't closed, it could create a deadlock with the |
| 4688 | * child, we spend some energy here to try to ensure that we |
| 4689 | * either insert all file handles into the dictionary or none |
| 4690 | * at all. It's a little clumsy with the various popen modes |
| 4691 | * and variable number of files involved. |
| 4692 | */ |
| 4693 | if (!_PyPopenProcs) { |
| 4694 | _PyPopenProcs = PyDict_New(); |
| 4695 | } |
| 4696 | |
| 4697 | if (_PyPopenProcs) { |
| 4698 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 4699 | int ins_rc[3]; |
| 4700 | |
| 4701 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4702 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4703 | |
| 4704 | procObj = PyList_New(2); |
| 4705 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 4706 | intObj = PyInt_FromLong(file_count); |
| 4707 | |
| 4708 | if (procObj && hProcessObj && intObj) { |
| 4709 | PyList_SetItem(procObj,0,hProcessObj); |
| 4710 | PyList_SetItem(procObj,1,intObj); |
| 4711 | |
| 4712 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 4713 | if (fileObj[0]) { |
| 4714 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4715 | fileObj[0], |
| 4716 | procObj); |
| 4717 | } |
| 4718 | if (file_count >= 2) { |
| 4719 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 4720 | if (fileObj[1]) { |
| 4721 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4722 | fileObj[1], |
| 4723 | procObj); |
| 4724 | } |
| 4725 | } |
| 4726 | if (file_count >= 3) { |
| 4727 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 4728 | if (fileObj[2]) { |
| 4729 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4730 | fileObj[2], |
| 4731 | procObj); |
| 4732 | } |
| 4733 | } |
| 4734 | |
| 4735 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4736 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4737 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 4738 | /* Something failed - remove any dictionary |
| 4739 | * entries that did make it. |
| 4740 | */ |
| 4741 | if (!ins_rc[0] && fileObj[0]) { |
| 4742 | PyDict_DelItem(_PyPopenProcs, |
| 4743 | fileObj[0]); |
| 4744 | } |
| 4745 | if (!ins_rc[1] && fileObj[1]) { |
| 4746 | PyDict_DelItem(_PyPopenProcs, |
| 4747 | fileObj[1]); |
| 4748 | } |
| 4749 | if (!ins_rc[2] && fileObj[2]) { |
| 4750 | PyDict_DelItem(_PyPopenProcs, |
| 4751 | fileObj[2]); |
| 4752 | } |
| 4753 | } |
| 4754 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4755 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4756 | /* |
| 4757 | * Clean up our localized references for the dictionary keys |
| 4758 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4759 | * that got placed in the dictionary. |
| 4760 | */ |
| 4761 | Py_XDECREF(procObj); |
| 4762 | Py_XDECREF(fileObj[0]); |
| 4763 | Py_XDECREF(fileObj[1]); |
| 4764 | Py_XDECREF(fileObj[2]); |
| 4765 | } |
| 4766 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4767 | /* Child is launched. Close the parents copy of those pipe |
| 4768 | * handles that only the child should have open. You need to |
| 4769 | * make sure that no handles to the write end of the output pipe |
| 4770 | * are maintained in this process or else the pipe will not close |
| 4771 | * when the child process exits and the ReadFile will hang. */ |
| 4772 | |
| 4773 | if (!CloseHandle(hChildStdinRd)) |
| 4774 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4775 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4776 | if (!CloseHandle(hChildStdoutWr)) |
| 4777 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4778 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4779 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 4780 | return win32_error("CloseHandle", NULL); |
| 4781 | |
| 4782 | return f; |
| 4783 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4784 | |
| 4785 | /* |
| 4786 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4787 | * 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] | 4788 | * |
| 4789 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4790 | * input file pointer to information about the process that was |
| 4791 | * originally created by the popen* call that created the file pointer. |
| 4792 | * The dictionary uses the file pointer as a key (with one entry |
| 4793 | * inserted for each file returned by the original popen* call) and a |
| 4794 | * single list object as the value for all files from a single call. |
| 4795 | * The list object contains the Win32 process handle at [0], and a file |
| 4796 | * count at [1], which is initialized to the total number of file |
| 4797 | * handles using that list. |
| 4798 | * |
| 4799 | * This function closes whichever handle it is passed, and decrements |
| 4800 | * the file count in the dictionary for the process handle pointed to |
| 4801 | * by this file. On the last close (when the file count reaches zero), |
| 4802 | * this function will wait for the child process and then return its |
| 4803 | * exit code as the result of the close() operation. This permits the |
| 4804 | * files to be closed in any order - it is always the close() of the |
| 4805 | * final handle that will return the exit code. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4806 | * |
| 4807 | * NOTE: This function is currently called with the GIL released. |
| 4808 | * hence we use the GILState API to manage our state. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4809 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4810 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4811 | static int _PyPclose(FILE *file) |
| 4812 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4813 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4814 | DWORD exit_code; |
| 4815 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4816 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 4817 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4818 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4819 | PyGILState_STATE state; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4820 | #endif |
| 4821 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4822 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4823 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4824 | */ |
| 4825 | result = fclose(file); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4826 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4827 | state = PyGILState_Ensure(); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4828 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4829 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4830 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4831 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4832 | fileObj)) != NULL && |
| 4833 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 4834 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 4835 | |
| 4836 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 4837 | file_count = PyInt_AsLong(intObj); |
| 4838 | |
| 4839 | if (file_count > 1) { |
| 4840 | /* Still other files referencing process */ |
| 4841 | file_count--; |
| 4842 | PyList_SetItem(procObj,1, |
| 4843 | PyInt_FromLong(file_count)); |
| 4844 | } else { |
| 4845 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4846 | if (result != EOF && |
| 4847 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 4848 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4849 | /* Possible truncation here in 16-bit environments, but |
| 4850 | * real exit codes are just the lower byte in any event. |
| 4851 | */ |
| 4852 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4853 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4854 | /* Indicate failure - this will cause the file object |
| 4855 | * to raise an I/O error and translate the last Win32 |
| 4856 | * error code from errno. We do have a problem with |
| 4857 | * last errors that overlap the normal errno table, |
| 4858 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4859 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4860 | if (result != EOF) { |
| 4861 | /* If the error wasn't from the fclose(), then |
| 4862 | * set errno for the file object error handling. |
| 4863 | */ |
| 4864 | errno = GetLastError(); |
| 4865 | } |
| 4866 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4867 | } |
| 4868 | |
| 4869 | /* Free up the native handle at this point */ |
| 4870 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4871 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4872 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4873 | /* Remove this file pointer from dictionary */ |
| 4874 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4875 | |
| 4876 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 4877 | Py_DECREF(_PyPopenProcs); |
| 4878 | _PyPopenProcs = NULL; |
| 4879 | } |
| 4880 | |
| 4881 | } /* if object retrieval ok */ |
| 4882 | |
| 4883 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4884 | } /* if _PyPopenProcs */ |
| 4885 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4886 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4887 | PyGILState_Release(state); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4888 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4889 | return result; |
| 4890 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4891 | |
| 4892 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4893 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4894 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4895 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4896 | char *name; |
| 4897 | char *mode = "r"; |
| 4898 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4899 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4900 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4901 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4902 | return NULL; |
Martin v. Löwis | 9ad853b | 2003-10-31 10:01:53 +0000 | [diff] [blame] | 4903 | /* Strip mode of binary or text modifiers */ |
| 4904 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) |
| 4905 | mode = "r"; |
| 4906 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) |
| 4907 | mode = "w"; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4908 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4909 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4910 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4911 | if (fp == NULL) |
| 4912 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4913 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4914 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4915 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4916 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4917 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4918 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4919 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4920 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4921 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4922 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4923 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4924 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4925 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4926 | Set the current process's user id."); |
| 4927 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4928 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4929 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4930 | { |
| 4931 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4932 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4933 | return NULL; |
| 4934 | if (setuid(uid) < 0) |
| 4935 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4936 | Py_INCREF(Py_None); |
| 4937 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4938 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4939 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4940 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4941 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4942 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4943 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4944 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4945 | Set the current process's effective user id."); |
| 4946 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4947 | static PyObject * |
| 4948 | posix_seteuid (PyObject *self, PyObject *args) |
| 4949 | { |
| 4950 | int euid; |
| 4951 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 4952 | return NULL; |
| 4953 | } else if (seteuid(euid) < 0) { |
| 4954 | return posix_error(); |
| 4955 | } else { |
| 4956 | Py_INCREF(Py_None); |
| 4957 | return Py_None; |
| 4958 | } |
| 4959 | } |
| 4960 | #endif /* HAVE_SETEUID */ |
| 4961 | |
| 4962 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4963 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4964 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4965 | Set the current process's effective group id."); |
| 4966 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4967 | static PyObject * |
| 4968 | posix_setegid (PyObject *self, PyObject *args) |
| 4969 | { |
| 4970 | int egid; |
| 4971 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 4972 | return NULL; |
| 4973 | } else if (setegid(egid) < 0) { |
| 4974 | return posix_error(); |
| 4975 | } else { |
| 4976 | Py_INCREF(Py_None); |
| 4977 | return Py_None; |
| 4978 | } |
| 4979 | } |
| 4980 | #endif /* HAVE_SETEGID */ |
| 4981 | |
| 4982 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4983 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4984 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4985 | Set the current process's real and effective user ids."); |
| 4986 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4987 | static PyObject * |
| 4988 | posix_setreuid (PyObject *self, PyObject *args) |
| 4989 | { |
| 4990 | int ruid, euid; |
| 4991 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4992 | return NULL; |
| 4993 | } else if (setreuid(ruid, euid) < 0) { |
| 4994 | return posix_error(); |
| 4995 | } else { |
| 4996 | Py_INCREF(Py_None); |
| 4997 | return Py_None; |
| 4998 | } |
| 4999 | } |
| 5000 | #endif /* HAVE_SETREUID */ |
| 5001 | |
| 5002 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5003 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5004 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5005 | Set the current process's real and effective group ids."); |
| 5006 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5007 | static PyObject * |
| 5008 | posix_setregid (PyObject *self, PyObject *args) |
| 5009 | { |
| 5010 | int rgid, egid; |
| 5011 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 5012 | return NULL; |
| 5013 | } else if (setregid(rgid, egid) < 0) { |
| 5014 | return posix_error(); |
| 5015 | } else { |
| 5016 | Py_INCREF(Py_None); |
| 5017 | return Py_None; |
| 5018 | } |
| 5019 | } |
| 5020 | #endif /* HAVE_SETREGID */ |
| 5021 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5022 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5023 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5024 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5025 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5026 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5027 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5028 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5029 | { |
| 5030 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5031 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5032 | return NULL; |
| 5033 | if (setgid(gid) < 0) |
| 5034 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5035 | Py_INCREF(Py_None); |
| 5036 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5037 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5038 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5039 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5040 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5041 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5042 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5043 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5044 | |
| 5045 | static PyObject * |
| 5046 | posix_setgroups(PyObject *self, PyObject *args) |
| 5047 | { |
| 5048 | PyObject *groups; |
| 5049 | int i, len; |
| 5050 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5051 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5052 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 5053 | return NULL; |
| 5054 | if (!PySequence_Check(groups)) { |
| 5055 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 5056 | return NULL; |
| 5057 | } |
| 5058 | len = PySequence_Size(groups); |
| 5059 | if (len > MAX_GROUPS) { |
| 5060 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 5061 | return NULL; |
| 5062 | } |
| 5063 | for(i = 0; i < len; i++) { |
| 5064 | PyObject *elem; |
| 5065 | elem = PySequence_GetItem(groups, i); |
| 5066 | if (!elem) |
| 5067 | return NULL; |
| 5068 | if (!PyInt_Check(elem)) { |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 5069 | if (!PyLong_Check(elem)) { |
| 5070 | PyErr_SetString(PyExc_TypeError, |
| 5071 | "groups must be integers"); |
| 5072 | Py_DECREF(elem); |
| 5073 | return NULL; |
| 5074 | } else { |
| 5075 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 5076 | if (PyErr_Occurred()) { |
| 5077 | PyErr_SetString(PyExc_TypeError, |
| 5078 | "group id too big"); |
| 5079 | Py_DECREF(elem); |
| 5080 | return NULL; |
| 5081 | } |
| 5082 | grouplist[i] = x; |
| 5083 | /* read back the value to see if it fitted in gid_t */ |
| 5084 | if (grouplist[i] != x) { |
| 5085 | PyErr_SetString(PyExc_TypeError, |
| 5086 | "group id too big"); |
| 5087 | Py_DECREF(elem); |
| 5088 | return NULL; |
| 5089 | } |
| 5090 | } |
| 5091 | } else { |
| 5092 | long x = PyInt_AsLong(elem); |
| 5093 | grouplist[i] = x; |
| 5094 | if (grouplist[i] != x) { |
| 5095 | PyErr_SetString(PyExc_TypeError, |
| 5096 | "group id too big"); |
| 5097 | Py_DECREF(elem); |
| 5098 | return NULL; |
| 5099 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5100 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5101 | Py_DECREF(elem); |
| 5102 | } |
| 5103 | |
| 5104 | if (setgroups(len, grouplist) < 0) |
| 5105 | return posix_error(); |
| 5106 | Py_INCREF(Py_None); |
| 5107 | return Py_None; |
| 5108 | } |
| 5109 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5110 | |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 5111 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5112 | static PyObject * |
| 5113 | wait_helper(int pid, int status, struct rusage *ru) |
| 5114 | { |
| 5115 | PyObject *result; |
| 5116 | static PyObject *struct_rusage; |
| 5117 | |
| 5118 | if (pid == -1) |
| 5119 | return posix_error(); |
| 5120 | |
| 5121 | if (struct_rusage == NULL) { |
| 5122 | PyObject *m = PyImport_ImportModule("resource"); |
| 5123 | if (m == NULL) |
| 5124 | return NULL; |
| 5125 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 5126 | Py_DECREF(m); |
| 5127 | if (struct_rusage == NULL) |
| 5128 | return NULL; |
| 5129 | } |
| 5130 | |
| 5131 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 5132 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 5133 | if (!result) |
| 5134 | return NULL; |
| 5135 | |
| 5136 | #ifndef doubletime |
| 5137 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 5138 | #endif |
| 5139 | |
| 5140 | PyStructSequence_SET_ITEM(result, 0, |
| 5141 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 5142 | PyStructSequence_SET_ITEM(result, 1, |
| 5143 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 5144 | #define SET_INT(result, index, value)\ |
| 5145 | PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value)) |
| 5146 | SET_INT(result, 2, ru->ru_maxrss); |
| 5147 | SET_INT(result, 3, ru->ru_ixrss); |
| 5148 | SET_INT(result, 4, ru->ru_idrss); |
| 5149 | SET_INT(result, 5, ru->ru_isrss); |
| 5150 | SET_INT(result, 6, ru->ru_minflt); |
| 5151 | SET_INT(result, 7, ru->ru_majflt); |
| 5152 | SET_INT(result, 8, ru->ru_nswap); |
| 5153 | SET_INT(result, 9, ru->ru_inblock); |
| 5154 | SET_INT(result, 10, ru->ru_oublock); |
| 5155 | SET_INT(result, 11, ru->ru_msgsnd); |
| 5156 | SET_INT(result, 12, ru->ru_msgrcv); |
| 5157 | SET_INT(result, 13, ru->ru_nsignals); |
| 5158 | SET_INT(result, 14, ru->ru_nvcsw); |
| 5159 | SET_INT(result, 15, ru->ru_nivcsw); |
| 5160 | #undef SET_INT |
| 5161 | |
| 5162 | if (PyErr_Occurred()) { |
| 5163 | Py_DECREF(result); |
| 5164 | return NULL; |
| 5165 | } |
| 5166 | |
Neal Norwitz | 9b00a56 | 2006-03-20 08:47:12 +0000 | [diff] [blame] | 5167 | return Py_BuildValue("iiN", pid, status, result); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5168 | } |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 5169 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5170 | |
| 5171 | #ifdef HAVE_WAIT3 |
| 5172 | PyDoc_STRVAR(posix_wait3__doc__, |
| 5173 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 5174 | Wait for completion of a child process."); |
| 5175 | |
| 5176 | static PyObject * |
| 5177 | posix_wait3(PyObject *self, PyObject *args) |
| 5178 | { |
| 5179 | int pid, options; |
| 5180 | struct rusage ru; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5181 | WAIT_TYPE status; |
| 5182 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5183 | |
| 5184 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 5185 | return NULL; |
| 5186 | |
| 5187 | Py_BEGIN_ALLOW_THREADS |
| 5188 | pid = wait3(&status, options, &ru); |
| 5189 | Py_END_ALLOW_THREADS |
| 5190 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5191 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5192 | } |
| 5193 | #endif /* HAVE_WAIT3 */ |
| 5194 | |
| 5195 | #ifdef HAVE_WAIT4 |
| 5196 | PyDoc_STRVAR(posix_wait4__doc__, |
| 5197 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 5198 | Wait for completion of a given child process."); |
| 5199 | |
| 5200 | static PyObject * |
| 5201 | posix_wait4(PyObject *self, PyObject *args) |
| 5202 | { |
| 5203 | int pid, options; |
| 5204 | struct rusage ru; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5205 | WAIT_TYPE status; |
| 5206 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5207 | |
| 5208 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 5209 | return NULL; |
| 5210 | |
| 5211 | Py_BEGIN_ALLOW_THREADS |
| 5212 | pid = wait4(pid, &status, options, &ru); |
| 5213 | Py_END_ALLOW_THREADS |
| 5214 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5215 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5216 | } |
| 5217 | #endif /* HAVE_WAIT4 */ |
| 5218 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5219 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5220 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5221 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5222 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5223 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5224 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5225 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5226 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5227 | int pid, options; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5228 | WAIT_TYPE status; |
| 5229 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5230 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5231 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5232 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5233 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 5234 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5235 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5236 | if (pid == -1) |
| 5237 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5238 | |
| 5239 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5240 | } |
| 5241 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5242 | #elif defined(HAVE_CWAIT) |
| 5243 | |
| 5244 | /* 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] | 5245 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5246 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5247 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5248 | |
| 5249 | static PyObject * |
| 5250 | posix_waitpid(PyObject *self, PyObject *args) |
| 5251 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5252 | intptr_t pid; |
| 5253 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5254 | |
| 5255 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 5256 | return NULL; |
| 5257 | Py_BEGIN_ALLOW_THREADS |
| 5258 | pid = _cwait(&status, pid, options); |
| 5259 | Py_END_ALLOW_THREADS |
| 5260 | if (pid == -1) |
| 5261 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5262 | |
| 5263 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 5264 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5265 | } |
| 5266 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5267 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5268 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5269 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5270 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5271 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5272 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5273 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5274 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5275 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5276 | int pid; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5277 | WAIT_TYPE status; |
| 5278 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5279 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5280 | Py_BEGIN_ALLOW_THREADS |
| 5281 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5282 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5283 | if (pid == -1) |
| 5284 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5285 | |
| 5286 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5287 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5288 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5289 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5290 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5291 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5292 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5293 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5294 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5295 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5296 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5297 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5298 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5299 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5300 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5301 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5302 | 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] | 5303 | #else |
| 5304 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 5305 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5306 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5307 | } |
| 5308 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5309 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5310 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5311 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5312 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5313 | 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] | 5314 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5315 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5316 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5317 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5318 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 5319 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5320 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5321 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5322 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5323 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 5324 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5325 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5326 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 5327 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5328 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5329 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5330 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5331 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5332 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5333 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5334 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5335 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 5336 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5337 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5338 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5339 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5340 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5341 | return posix_2str(args, "etet:symlink", symlink, NULL, NULL); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5342 | } |
| 5343 | #endif /* HAVE_SYMLINK */ |
| 5344 | |
| 5345 | |
| 5346 | #ifdef HAVE_TIMES |
| 5347 | #ifndef HZ |
| 5348 | #define HZ 60 /* Universal constant :-) */ |
| 5349 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5350 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5351 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 5352 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5353 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5354 | { |
| 5355 | ULONG value = 0; |
| 5356 | |
| 5357 | Py_BEGIN_ALLOW_THREADS |
| 5358 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 5359 | Py_END_ALLOW_THREADS |
| 5360 | |
| 5361 | return value; |
| 5362 | } |
| 5363 | |
| 5364 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5365 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5366 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5367 | /* Currently Only Uptime is Provided -- Others Later */ |
| 5368 | return Py_BuildValue("ddddd", |
| 5369 | (double)0 /* t.tms_utime / HZ */, |
| 5370 | (double)0 /* t.tms_stime / HZ */, |
| 5371 | (double)0 /* t.tms_cutime / HZ */, |
| 5372 | (double)0 /* t.tms_cstime / HZ */, |
| 5373 | (double)system_uptime() / 1000); |
| 5374 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5375 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5376 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5377 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5378 | { |
| 5379 | struct tms t; |
| 5380 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5381 | errno = 0; |
| 5382 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5383 | if (c == (clock_t) -1) |
| 5384 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5385 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5386 | (double)t.tms_utime / HZ, |
| 5387 | (double)t.tms_stime / HZ, |
| 5388 | (double)t.tms_cutime / HZ, |
| 5389 | (double)t.tms_cstime / HZ, |
| 5390 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5391 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5392 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5393 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5394 | |
| 5395 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5396 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5397 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5398 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5399 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5400 | { |
| 5401 | FILETIME create, exit, kernel, user; |
| 5402 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5403 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5404 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 5405 | /* The fields of a FILETIME structure are the hi and lo part |
| 5406 | of a 64-bit value expressed in 100 nanosecond units. |
| 5407 | 1e7 is one second in such units; 1e-7 the inverse. |
| 5408 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 5409 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5410 | return Py_BuildValue( |
| 5411 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5412 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 5413 | kernel.dwLowDateTime*1e-7), |
| 5414 | (double)(user.dwHighDateTime*429.4967296 + |
| 5415 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5416 | (double)0, |
| 5417 | (double)0, |
| 5418 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5419 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5420 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5421 | |
| 5422 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5423 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5424 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5425 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5426 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5427 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5428 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5429 | #ifdef HAVE_GETSID |
| 5430 | PyDoc_STRVAR(posix_getsid__doc__, |
| 5431 | "getsid(pid) -> sid\n\n\ |
| 5432 | Call the system call getsid()."); |
| 5433 | |
| 5434 | static PyObject * |
| 5435 | posix_getsid(PyObject *self, PyObject *args) |
| 5436 | { |
| 5437 | int pid, sid; |
| 5438 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 5439 | return NULL; |
| 5440 | sid = getsid(pid); |
| 5441 | if (sid < 0) |
| 5442 | return posix_error(); |
| 5443 | return PyInt_FromLong((long)sid); |
| 5444 | } |
| 5445 | #endif /* HAVE_GETSID */ |
| 5446 | |
| 5447 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5448 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5449 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5450 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5451 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5452 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5453 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5454 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5455 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5456 | if (setsid() < 0) |
| 5457 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5458 | Py_INCREF(Py_None); |
| 5459 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5460 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5461 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5462 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5463 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5464 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5465 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5466 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5467 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5468 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5469 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5470 | { |
| 5471 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5472 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5473 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5474 | if (setpgid(pid, pgrp) < 0) |
| 5475 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5476 | Py_INCREF(Py_None); |
| 5477 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5478 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5479 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5480 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5481 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5482 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5483 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5484 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5485 | 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] | 5486 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5487 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5488 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5489 | { |
| 5490 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5491 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5492 | return NULL; |
| 5493 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5494 | if (pgid < 0) |
| 5495 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5496 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5497 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5498 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5499 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5500 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5501 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5502 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5503 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5504 | 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] | 5505 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5506 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5507 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5508 | { |
| 5509 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5510 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5511 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5512 | if (tcsetpgrp(fd, pgid) < 0) |
| 5513 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5514 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5515 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5516 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5517 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5518 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5519 | /* Functions acting on file descriptors */ |
| 5520 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5521 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5522 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5523 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5524 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5525 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5526 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5527 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5528 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5529 | int flag; |
| 5530 | int mode = 0777; |
| 5531 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5532 | |
| 5533 | #ifdef MS_WINDOWS |
| 5534 | if (unicode_file_names()) { |
| 5535 | PyUnicodeObject *po; |
| 5536 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 5537 | Py_BEGIN_ALLOW_THREADS |
| 5538 | /* PyUnicode_AS_UNICODE OK without thread |
| 5539 | lock as it is a simple dereference. */ |
| 5540 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 5541 | Py_END_ALLOW_THREADS |
| 5542 | if (fd < 0) |
| 5543 | return posix_error(); |
| 5544 | return PyInt_FromLong((long)fd); |
| 5545 | } |
| 5546 | /* Drop the argument parsing error as narrow strings |
| 5547 | are also valid. */ |
| 5548 | PyErr_Clear(); |
| 5549 | } |
| 5550 | #endif |
| 5551 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5552 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5553 | Py_FileSystemDefaultEncoding, &file, |
| 5554 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5555 | return NULL; |
| 5556 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5557 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5558 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5559 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5560 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5561 | return posix_error_with_allocated_filename(file); |
| 5562 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5563 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5564 | } |
| 5565 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5566 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5567 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5568 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5569 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5570 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5571 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5572 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5573 | { |
| 5574 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5575 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5576 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5577 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5578 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5579 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5580 | if (res < 0) |
| 5581 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5582 | Py_INCREF(Py_None); |
| 5583 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5584 | } |
| 5585 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5586 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5587 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5588 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5589 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5590 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5591 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5592 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5593 | { |
| 5594 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5595 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5596 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5597 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5598 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5599 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5600 | if (fd < 0) |
| 5601 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5602 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5605 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5606 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5607 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5608 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5609 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5610 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5611 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5612 | { |
| 5613 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5614 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5615 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5616 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5617 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5618 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5619 | if (res < 0) |
| 5620 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5621 | Py_INCREF(Py_None); |
| 5622 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5623 | } |
| 5624 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5625 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5626 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5627 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5628 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5629 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5630 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5631 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5632 | { |
| 5633 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5634 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5635 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5636 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5637 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5638 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5639 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5640 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5641 | return NULL; |
| 5642 | #ifdef SEEK_SET |
| 5643 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5644 | switch (how) { |
| 5645 | case 0: how = SEEK_SET; break; |
| 5646 | case 1: how = SEEK_CUR; break; |
| 5647 | case 2: how = SEEK_END; break; |
| 5648 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5649 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5650 | |
| 5651 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5652 | pos = PyInt_AsLong(posobj); |
| 5653 | #else |
| 5654 | pos = PyLong_Check(posobj) ? |
| 5655 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 5656 | #endif |
| 5657 | if (PyErr_Occurred()) |
| 5658 | return NULL; |
| 5659 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5660 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5661 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5662 | res = _lseeki64(fd, pos, how); |
| 5663 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5664 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5665 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5666 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5667 | if (res < 0) |
| 5668 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5669 | |
| 5670 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5671 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5672 | #else |
| 5673 | return PyLong_FromLongLong(res); |
| 5674 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5675 | } |
| 5676 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5677 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5678 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5679 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5680 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5681 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5682 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5683 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5684 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5685 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5686 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5687 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5688 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 5689 | if (size < 0) { |
| 5690 | errno = EINVAL; |
| 5691 | return posix_error(); |
| 5692 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5693 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5694 | if (buffer == NULL) |
| 5695 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5696 | Py_BEGIN_ALLOW_THREADS |
| 5697 | n = read(fd, PyString_AsString(buffer), size); |
| 5698 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5699 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5700 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5701 | return posix_error(); |
| 5702 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5703 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5704 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5705 | return buffer; |
| 5706 | } |
| 5707 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5708 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5709 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5710 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5711 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5712 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5713 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5714 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5715 | { |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5716 | int fd; |
| 5717 | Py_ssize_t size; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5718 | char *buffer; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5719 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5720 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5721 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5722 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5723 | size = write(fd, buffer, (size_t)size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5724 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5725 | if (size < 0) |
| 5726 | return posix_error(); |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5727 | return PyInt_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5728 | } |
| 5729 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5730 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5731 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5732 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5733 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5734 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5735 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5736 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5737 | { |
| 5738 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5739 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5740 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5741 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5742 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5743 | #ifdef __VMS |
| 5744 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5745 | fsync(fd); |
| 5746 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5747 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5748 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5749 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5750 | if (res != 0) { |
| 5751 | #ifdef MS_WINDOWS |
| 5752 | return win32_error("fstat", NULL); |
| 5753 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5754 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5755 | #endif |
| 5756 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5757 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5758 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5759 | } |
| 5760 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5762 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5763 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5764 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5765 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5766 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5767 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5768 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5769 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5770 | char *mode = "r"; |
| 5771 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5772 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5773 | PyObject *f; |
| 5774 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5775 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5776 | |
Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 5777 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 5778 | PyErr_Format(PyExc_ValueError, |
| 5779 | "invalid file mode '%s'", mode); |
| 5780 | return NULL; |
| 5781 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5782 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 5783 | #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H) |
Georg Brandl | 54a188a | 2006-03-31 20:00:11 +0000 | [diff] [blame] | 5784 | if (mode[0] == 'a') { |
| 5785 | /* try to make sure the O_APPEND flag is set */ |
| 5786 | int flags; |
| 5787 | flags = fcntl(fd, F_GETFL); |
| 5788 | if (flags != -1) |
| 5789 | fcntl(fd, F_SETFL, flags | O_APPEND); |
| 5790 | fp = fdopen(fd, mode); |
Thomas Wouters | 2a9a6b0 | 2006-03-31 22:38:19 +0000 | [diff] [blame] | 5791 | if (fp == NULL && flags != -1) |
Georg Brandl | 54a188a | 2006-03-31 20:00:11 +0000 | [diff] [blame] | 5792 | /* restore old mode if fdopen failed */ |
| 5793 | fcntl(fd, F_SETFL, flags); |
| 5794 | } else { |
| 5795 | fp = fdopen(fd, mode); |
| 5796 | } |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 5797 | #else |
| 5798 | fp = fdopen(fd, mode); |
| 5799 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5800 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5801 | if (fp == NULL) |
| 5802 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 5803 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5804 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5805 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5806 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5807 | } |
| 5808 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5809 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5810 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5811 | 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] | 5812 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5813 | |
| 5814 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5815 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5816 | { |
| 5817 | int fd; |
| 5818 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5819 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5820 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5821 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5822 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5823 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5824 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5825 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5826 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5827 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5828 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5829 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5830 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5831 | #if defined(PYOS_OS2) |
| 5832 | HFILE read, write; |
| 5833 | APIRET rc; |
| 5834 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5835 | Py_BEGIN_ALLOW_THREADS |
| 5836 | rc = DosCreatePipe( &read, &write, 4096); |
| 5837 | Py_END_ALLOW_THREADS |
| 5838 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5839 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5840 | |
| 5841 | return Py_BuildValue("(ii)", read, write); |
| 5842 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5843 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5844 | int fds[2]; |
| 5845 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5846 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5847 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5848 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5849 | if (res != 0) |
| 5850 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5851 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5852 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5853 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5854 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5855 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5856 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5857 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5858 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5859 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5860 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5861 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5862 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5863 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5864 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5865 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5866 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5867 | #endif /* HAVE_PIPE */ |
| 5868 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5869 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5870 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5871 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5872 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5873 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5874 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5875 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5876 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5877 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5878 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5879 | int mode = 0666; |
| 5880 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5881 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5882 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5883 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5884 | res = mkfifo(filename, mode); |
| 5885 | Py_END_ALLOW_THREADS |
| 5886 | if (res < 0) |
| 5887 | return posix_error(); |
| 5888 | Py_INCREF(Py_None); |
| 5889 | return Py_None; |
| 5890 | } |
| 5891 | #endif |
| 5892 | |
| 5893 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5894 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5895 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5896 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5897 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5898 | named filename. mode specifies both the permissions to use and the\n\ |
| 5899 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5900 | 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] | 5901 | device defines the newly created device special file (probably using\n\ |
| 5902 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5903 | |
| 5904 | |
| 5905 | static PyObject * |
| 5906 | posix_mknod(PyObject *self, PyObject *args) |
| 5907 | { |
| 5908 | char *filename; |
| 5909 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5910 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5911 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5912 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5913 | return NULL; |
| 5914 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5915 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5916 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5917 | if (res < 0) |
| 5918 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5919 | Py_INCREF(Py_None); |
| 5920 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5921 | } |
| 5922 | #endif |
| 5923 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5924 | #ifdef HAVE_DEVICE_MACROS |
| 5925 | PyDoc_STRVAR(posix_major__doc__, |
| 5926 | "major(device) -> major number\n\ |
| 5927 | Extracts a device major number from a raw device number."); |
| 5928 | |
| 5929 | static PyObject * |
| 5930 | posix_major(PyObject *self, PyObject *args) |
| 5931 | { |
| 5932 | int device; |
| 5933 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5934 | return NULL; |
| 5935 | return PyInt_FromLong((long)major(device)); |
| 5936 | } |
| 5937 | |
| 5938 | PyDoc_STRVAR(posix_minor__doc__, |
| 5939 | "minor(device) -> minor number\n\ |
| 5940 | Extracts a device minor number from a raw device number."); |
| 5941 | |
| 5942 | static PyObject * |
| 5943 | posix_minor(PyObject *self, PyObject *args) |
| 5944 | { |
| 5945 | int device; |
| 5946 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5947 | return NULL; |
| 5948 | return PyInt_FromLong((long)minor(device)); |
| 5949 | } |
| 5950 | |
| 5951 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5952 | "makedev(major, minor) -> device number\n\ |
| 5953 | Composes a raw device number from the major and minor device numbers."); |
| 5954 | |
| 5955 | static PyObject * |
| 5956 | posix_makedev(PyObject *self, PyObject *args) |
| 5957 | { |
| 5958 | int major, minor; |
| 5959 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5960 | return NULL; |
| 5961 | return PyInt_FromLong((long)makedev(major, minor)); |
| 5962 | } |
| 5963 | #endif /* device macros */ |
| 5964 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5965 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5966 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5967 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5968 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5969 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5970 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5971 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5972 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5973 | { |
| 5974 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5975 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5976 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5977 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5978 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5979 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5980 | return NULL; |
| 5981 | |
| 5982 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5983 | length = PyInt_AsLong(lenobj); |
| 5984 | #else |
| 5985 | length = PyLong_Check(lenobj) ? |
| 5986 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 5987 | #endif |
| 5988 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5989 | return NULL; |
| 5990 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5991 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5992 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5993 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5994 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5995 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5996 | return NULL; |
| 5997 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5998 | Py_INCREF(Py_None); |
| 5999 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6000 | } |
| 6001 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6002 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6003 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6004 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6005 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6006 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6007 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6008 | /* Save putenv() parameters as values here, so we can collect them when they |
| 6009 | * get re-set with another call for the same key. */ |
| 6010 | static PyObject *posix_putenv_garbage; |
| 6011 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6012 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6013 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6014 | { |
| 6015 | char *s1, *s2; |
Anthony Baxter | 64182fe | 2006-04-11 12:14:09 +0000 | [diff] [blame] | 6016 | char *newenv; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6017 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 6018 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6019 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6020 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6021 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6022 | |
| 6023 | #if defined(PYOS_OS2) |
| 6024 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 6025 | APIRET rc; |
| 6026 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6027 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 6028 | if (rc != NO_ERROR) |
| 6029 | return os2_error(rc); |
| 6030 | |
| 6031 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 6032 | APIRET rc; |
| 6033 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6034 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 6035 | if (rc != NO_ERROR) |
| 6036 | return os2_error(rc); |
| 6037 | } else { |
| 6038 | #endif |
| 6039 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6040 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 6041 | len = strlen(s1) + strlen(s2) + 2; |
| 6042 | /* len includes space for a trailing \0; the size arg to |
| 6043 | PyString_FromStringAndSize does not count that */ |
| 6044 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6045 | if (newstr == NULL) |
| 6046 | return PyErr_NoMemory(); |
Anthony Baxter | 64182fe | 2006-04-11 12:14:09 +0000 | [diff] [blame] | 6047 | newenv = PyString_AS_STRING(newstr); |
| 6048 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 6049 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 6050 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6051 | posix_error(); |
| 6052 | return NULL; |
| 6053 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6054 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 6055 | * this will cause previous value to be collected. This has to |
| 6056 | * happen after the real putenv() call because the old value |
| 6057 | * was still accessible until then. */ |
| 6058 | if (PyDict_SetItem(posix_putenv_garbage, |
| 6059 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 6060 | /* really not much we can do; just leak */ |
| 6061 | PyErr_Clear(); |
| 6062 | } |
| 6063 | else { |
| 6064 | Py_DECREF(newstr); |
| 6065 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6066 | |
| 6067 | #if defined(PYOS_OS2) |
| 6068 | } |
| 6069 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6070 | Py_INCREF(Py_None); |
| 6071 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6072 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6073 | #endif /* putenv */ |
| 6074 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6075 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6076 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6077 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6078 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6079 | |
| 6080 | static PyObject * |
| 6081 | posix_unsetenv(PyObject *self, PyObject *args) |
| 6082 | { |
| 6083 | char *s1; |
| 6084 | |
| 6085 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 6086 | return NULL; |
| 6087 | |
| 6088 | unsetenv(s1); |
| 6089 | |
| 6090 | /* Remove the key from posix_putenv_garbage; |
| 6091 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6092 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6093 | * old value was still accessible until then. |
| 6094 | */ |
| 6095 | if (PyDict_DelItem(posix_putenv_garbage, |
| 6096 | PyTuple_GET_ITEM(args, 0))) { |
| 6097 | /* really not much we can do; just leak */ |
| 6098 | PyErr_Clear(); |
| 6099 | } |
| 6100 | |
| 6101 | Py_INCREF(Py_None); |
| 6102 | return Py_None; |
| 6103 | } |
| 6104 | #endif /* unsetenv */ |
| 6105 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6106 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6107 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6108 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6109 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6110 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6111 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6112 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6113 | { |
| 6114 | int code; |
| 6115 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6116 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6117 | return NULL; |
| 6118 | message = strerror(code); |
| 6119 | if (message == NULL) { |
| 6120 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 6121 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6122 | return NULL; |
| 6123 | } |
| 6124 | return PyString_FromString(message); |
| 6125 | } |
| 6126 | #endif /* strerror */ |
| 6127 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6128 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6129 | #ifdef HAVE_SYS_WAIT_H |
| 6130 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6131 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6132 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6133 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6134 | 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] | 6135 | |
| 6136 | static PyObject * |
| 6137 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 6138 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6139 | WAIT_TYPE status; |
| 6140 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6141 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6142 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6143 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6144 | |
| 6145 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6146 | } |
| 6147 | #endif /* WCOREDUMP */ |
| 6148 | |
| 6149 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6150 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6151 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6152 | 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] | 6153 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6154 | |
| 6155 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6156 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6157 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6158 | WAIT_TYPE status; |
| 6159 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6160 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6161 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6162 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6163 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6164 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6165 | } |
| 6166 | #endif /* WIFCONTINUED */ |
| 6167 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6168 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6169 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6170 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6171 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6172 | |
| 6173 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6174 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6175 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6176 | WAIT_TYPE status; |
| 6177 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6178 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6179 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6180 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6181 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6182 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6183 | } |
| 6184 | #endif /* WIFSTOPPED */ |
| 6185 | |
| 6186 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6187 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6188 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6189 | 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] | 6190 | |
| 6191 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6192 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6193 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6194 | WAIT_TYPE status; |
| 6195 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6196 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6197 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6198 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6199 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6200 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6201 | } |
| 6202 | #endif /* WIFSIGNALED */ |
| 6203 | |
| 6204 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6205 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6206 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6207 | 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] | 6208 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6209 | |
| 6210 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6211 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6212 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6213 | WAIT_TYPE status; |
| 6214 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6215 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6216 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6217 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6218 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6219 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6220 | } |
| 6221 | #endif /* WIFEXITED */ |
| 6222 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6223 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6224 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6225 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6226 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6227 | |
| 6228 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6229 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6230 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6231 | WAIT_TYPE status; |
| 6232 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6233 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6234 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6235 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6236 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6237 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 6238 | } |
| 6239 | #endif /* WEXITSTATUS */ |
| 6240 | |
| 6241 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6242 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6243 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6244 | 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] | 6245 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6246 | |
| 6247 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6248 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6249 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6250 | WAIT_TYPE status; |
| 6251 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6252 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6253 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6254 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6255 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6256 | return Py_BuildValue("i", WTERMSIG(status)); |
| 6257 | } |
| 6258 | #endif /* WTERMSIG */ |
| 6259 | |
| 6260 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6261 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6262 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6263 | Return the signal that stopped the process that provided\n\ |
| 6264 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6265 | |
| 6266 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6267 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6268 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6269 | WAIT_TYPE status; |
| 6270 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6271 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6272 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6273 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6274 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6275 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 6276 | } |
| 6277 | #endif /* WSTOPSIG */ |
| 6278 | |
| 6279 | #endif /* HAVE_SYS_WAIT_H */ |
| 6280 | |
| 6281 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6282 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 6283 | #ifdef _SCO_DS |
| 6284 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 6285 | needed definitions in sys/statvfs.h */ |
| 6286 | #define _SVID3 |
| 6287 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6288 | #include <sys/statvfs.h> |
| 6289 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6290 | static PyObject* |
| 6291 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 6292 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 6293 | if (v == NULL) |
| 6294 | return NULL; |
| 6295 | |
| 6296 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6297 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6298 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 6299 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 6300 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 6301 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 6302 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 6303 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 6304 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 6305 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6306 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6307 | #else |
| 6308 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6309 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6310 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6311 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6312 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6313 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6314 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6315 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6316 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6317 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6318 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6319 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6320 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6321 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6322 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6323 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6324 | #endif |
| 6325 | |
| 6326 | return v; |
| 6327 | } |
| 6328 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6329 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6330 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6331 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6332 | |
| 6333 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6334 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6335 | { |
| 6336 | int fd, res; |
| 6337 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6338 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6339 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6340 | return NULL; |
| 6341 | Py_BEGIN_ALLOW_THREADS |
| 6342 | res = fstatvfs(fd, &st); |
| 6343 | Py_END_ALLOW_THREADS |
| 6344 | if (res != 0) |
| 6345 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6346 | |
| 6347 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6348 | } |
| 6349 | #endif /* HAVE_FSTATVFS */ |
| 6350 | |
| 6351 | |
| 6352 | #if defined(HAVE_STATVFS) |
| 6353 | #include <sys/statvfs.h> |
| 6354 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6355 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6356 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6357 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6358 | |
| 6359 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6360 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6361 | { |
| 6362 | char *path; |
| 6363 | int res; |
| 6364 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6365 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6366 | return NULL; |
| 6367 | Py_BEGIN_ALLOW_THREADS |
| 6368 | res = statvfs(path, &st); |
| 6369 | Py_END_ALLOW_THREADS |
| 6370 | if (res != 0) |
| 6371 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6372 | |
| 6373 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6374 | } |
| 6375 | #endif /* HAVE_STATVFS */ |
| 6376 | |
| 6377 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6378 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6379 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6380 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6381 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 6382 | 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] | 6383 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6384 | |
| 6385 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6386 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6387 | { |
| 6388 | PyObject *result = NULL; |
| 6389 | char *dir = NULL; |
| 6390 | char *pfx = NULL; |
| 6391 | char *name; |
| 6392 | |
| 6393 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 6394 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6395 | |
| 6396 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6397 | "tempnam is a potential security risk to your program") < 0) |
| 6398 | return NULL; |
| 6399 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6400 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6401 | name = _tempnam(dir, pfx); |
| 6402 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6403 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6404 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6405 | if (name == NULL) |
| 6406 | return PyErr_NoMemory(); |
| 6407 | result = PyString_FromString(name); |
| 6408 | free(name); |
| 6409 | return result; |
| 6410 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 6411 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6412 | |
| 6413 | |
| 6414 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6415 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6416 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6417 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6418 | |
| 6419 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6420 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6421 | { |
| 6422 | FILE *fp; |
| 6423 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6424 | fp = tmpfile(); |
| 6425 | if (fp == NULL) |
| 6426 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 6427 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6428 | } |
| 6429 | #endif |
| 6430 | |
| 6431 | |
| 6432 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6433 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6434 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6435 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6436 | |
| 6437 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6438 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6439 | { |
| 6440 | char buffer[L_tmpnam]; |
| 6441 | char *name; |
| 6442 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6443 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6444 | "tmpnam is a potential security risk to your program") < 0) |
| 6445 | return NULL; |
| 6446 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6447 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6448 | name = tmpnam_r(buffer); |
| 6449 | #else |
| 6450 | name = tmpnam(buffer); |
| 6451 | #endif |
| 6452 | if (name == NULL) { |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 6453 | PyObject *err = Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6454 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6455 | "unexpected NULL from tmpnam_r" |
| 6456 | #else |
| 6457 | "unexpected NULL from tmpnam" |
| 6458 | #endif |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 6459 | ); |
| 6460 | PyErr_SetObject(PyExc_OSError, err); |
| 6461 | Py_XDECREF(err); |
| 6462 | return NULL; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6463 | } |
| 6464 | return PyString_FromString(buffer); |
| 6465 | } |
| 6466 | #endif |
| 6467 | |
| 6468 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6469 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 6470 | * It maps strings representing configuration variable names to |
| 6471 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6472 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6473 | * rarely-used constants. There are three separate tables that use |
| 6474 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6475 | * |
| 6476 | * This code is always included, even if none of the interfaces that |
| 6477 | * need it are included. The #if hackery needed to avoid it would be |
| 6478 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6479 | */ |
| 6480 | struct constdef { |
| 6481 | char *name; |
| 6482 | long value; |
| 6483 | }; |
| 6484 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6485 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6486 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 6487 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6488 | { |
| 6489 | if (PyInt_Check(arg)) { |
| 6490 | *valuep = PyInt_AS_LONG(arg); |
| 6491 | return 1; |
| 6492 | } |
| 6493 | if (PyString_Check(arg)) { |
| 6494 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6495 | size_t lo = 0; |
| 6496 | size_t mid; |
| 6497 | size_t hi = tablesize; |
| 6498 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6499 | char *confname = PyString_AS_STRING(arg); |
| 6500 | while (lo < hi) { |
| 6501 | mid = (lo + hi) / 2; |
| 6502 | cmp = strcmp(confname, table[mid].name); |
| 6503 | if (cmp < 0) |
| 6504 | hi = mid; |
| 6505 | else if (cmp > 0) |
| 6506 | lo = mid + 1; |
| 6507 | else { |
| 6508 | *valuep = table[mid].value; |
| 6509 | return 1; |
| 6510 | } |
| 6511 | } |
| 6512 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 6513 | } |
| 6514 | else |
| 6515 | PyErr_SetString(PyExc_TypeError, |
| 6516 | "configuration names must be strings or integers"); |
| 6517 | return 0; |
| 6518 | } |
| 6519 | |
| 6520 | |
| 6521 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 6522 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6523 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 6524 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 6525 | #endif |
| 6526 | #ifdef _PC_ABI_ASYNC_IO |
| 6527 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 6528 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6529 | #ifdef _PC_ASYNC_IO |
| 6530 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 6531 | #endif |
| 6532 | #ifdef _PC_CHOWN_RESTRICTED |
| 6533 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 6534 | #endif |
| 6535 | #ifdef _PC_FILESIZEBITS |
| 6536 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 6537 | #endif |
| 6538 | #ifdef _PC_LAST |
| 6539 | {"PC_LAST", _PC_LAST}, |
| 6540 | #endif |
| 6541 | #ifdef _PC_LINK_MAX |
| 6542 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 6543 | #endif |
| 6544 | #ifdef _PC_MAX_CANON |
| 6545 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 6546 | #endif |
| 6547 | #ifdef _PC_MAX_INPUT |
| 6548 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 6549 | #endif |
| 6550 | #ifdef _PC_NAME_MAX |
| 6551 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 6552 | #endif |
| 6553 | #ifdef _PC_NO_TRUNC |
| 6554 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 6555 | #endif |
| 6556 | #ifdef _PC_PATH_MAX |
| 6557 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 6558 | #endif |
| 6559 | #ifdef _PC_PIPE_BUF |
| 6560 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 6561 | #endif |
| 6562 | #ifdef _PC_PRIO_IO |
| 6563 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 6564 | #endif |
| 6565 | #ifdef _PC_SOCK_MAXBUF |
| 6566 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 6567 | #endif |
| 6568 | #ifdef _PC_SYNC_IO |
| 6569 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 6570 | #endif |
| 6571 | #ifdef _PC_VDISABLE |
| 6572 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 6573 | #endif |
| 6574 | }; |
| 6575 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6576 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6577 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6578 | { |
| 6579 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 6580 | sizeof(posix_constants_pathconf) |
| 6581 | / sizeof(struct constdef)); |
| 6582 | } |
| 6583 | #endif |
| 6584 | |
| 6585 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6586 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6587 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6588 | 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] | 6589 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6590 | |
| 6591 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6592 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6593 | { |
| 6594 | PyObject *result = NULL; |
| 6595 | int name, fd; |
| 6596 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6597 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 6598 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6599 | long limit; |
| 6600 | |
| 6601 | errno = 0; |
| 6602 | limit = fpathconf(fd, name); |
| 6603 | if (limit == -1 && errno != 0) |
| 6604 | posix_error(); |
| 6605 | else |
| 6606 | result = PyInt_FromLong(limit); |
| 6607 | } |
| 6608 | return result; |
| 6609 | } |
| 6610 | #endif |
| 6611 | |
| 6612 | |
| 6613 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6614 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6615 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6616 | 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] | 6617 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6618 | |
| 6619 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6620 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6621 | { |
| 6622 | PyObject *result = NULL; |
| 6623 | int name; |
| 6624 | char *path; |
| 6625 | |
| 6626 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 6627 | conv_path_confname, &name)) { |
| 6628 | long limit; |
| 6629 | |
| 6630 | errno = 0; |
| 6631 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6632 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6633 | if (errno == EINVAL) |
| 6634 | /* could be a path or name problem */ |
| 6635 | posix_error(); |
| 6636 | else |
| 6637 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6638 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6639 | else |
| 6640 | result = PyInt_FromLong(limit); |
| 6641 | } |
| 6642 | return result; |
| 6643 | } |
| 6644 | #endif |
| 6645 | |
| 6646 | #ifdef HAVE_CONFSTR |
| 6647 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6648 | #ifdef _CS_ARCHITECTURE |
| 6649 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 6650 | #endif |
| 6651 | #ifdef _CS_HOSTNAME |
| 6652 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 6653 | #endif |
| 6654 | #ifdef _CS_HW_PROVIDER |
| 6655 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 6656 | #endif |
| 6657 | #ifdef _CS_HW_SERIAL |
| 6658 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 6659 | #endif |
| 6660 | #ifdef _CS_INITTAB_NAME |
| 6661 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 6662 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6663 | #ifdef _CS_LFS64_CFLAGS |
| 6664 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 6665 | #endif |
| 6666 | #ifdef _CS_LFS64_LDFLAGS |
| 6667 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 6668 | #endif |
| 6669 | #ifdef _CS_LFS64_LIBS |
| 6670 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 6671 | #endif |
| 6672 | #ifdef _CS_LFS64_LINTFLAGS |
| 6673 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 6674 | #endif |
| 6675 | #ifdef _CS_LFS_CFLAGS |
| 6676 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 6677 | #endif |
| 6678 | #ifdef _CS_LFS_LDFLAGS |
| 6679 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6680 | #endif |
| 6681 | #ifdef _CS_LFS_LIBS |
| 6682 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6683 | #endif |
| 6684 | #ifdef _CS_LFS_LINTFLAGS |
| 6685 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6686 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6687 | #ifdef _CS_MACHINE |
| 6688 | {"CS_MACHINE", _CS_MACHINE}, |
| 6689 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6690 | #ifdef _CS_PATH |
| 6691 | {"CS_PATH", _CS_PATH}, |
| 6692 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6693 | #ifdef _CS_RELEASE |
| 6694 | {"CS_RELEASE", _CS_RELEASE}, |
| 6695 | #endif |
| 6696 | #ifdef _CS_SRPC_DOMAIN |
| 6697 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6698 | #endif |
| 6699 | #ifdef _CS_SYSNAME |
| 6700 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6701 | #endif |
| 6702 | #ifdef _CS_VERSION |
| 6703 | {"CS_VERSION", _CS_VERSION}, |
| 6704 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6705 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6706 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6707 | #endif |
| 6708 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6709 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6710 | #endif |
| 6711 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6712 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6713 | #endif |
| 6714 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6715 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6716 | #endif |
| 6717 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6718 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6719 | #endif |
| 6720 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6721 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6722 | #endif |
| 6723 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6724 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6725 | #endif |
| 6726 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6727 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6728 | #endif |
| 6729 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6730 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6731 | #endif |
| 6732 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6733 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6734 | #endif |
| 6735 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6736 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6737 | #endif |
| 6738 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6739 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6740 | #endif |
| 6741 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6742 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6743 | #endif |
| 6744 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6745 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6746 | #endif |
| 6747 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6748 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6749 | #endif |
| 6750 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6751 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6752 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6753 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6754 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6755 | #endif |
| 6756 | #ifdef _MIPS_CS_BASE |
| 6757 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6758 | #endif |
| 6759 | #ifdef _MIPS_CS_HOSTID |
| 6760 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6761 | #endif |
| 6762 | #ifdef _MIPS_CS_HW_NAME |
| 6763 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6764 | #endif |
| 6765 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6766 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6767 | #endif |
| 6768 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6769 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6770 | #endif |
| 6771 | #ifdef _MIPS_CS_OSREL_MIN |
| 6772 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6773 | #endif |
| 6774 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6775 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6776 | #endif |
| 6777 | #ifdef _MIPS_CS_OS_NAME |
| 6778 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6779 | #endif |
| 6780 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6781 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6782 | #endif |
| 6783 | #ifdef _MIPS_CS_PROCESSORS |
| 6784 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6785 | #endif |
| 6786 | #ifdef _MIPS_CS_SERIAL |
| 6787 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6788 | #endif |
| 6789 | #ifdef _MIPS_CS_VENDOR |
| 6790 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6791 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6792 | }; |
| 6793 | |
| 6794 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6795 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6796 | { |
| 6797 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6798 | sizeof(posix_constants_confstr) |
| 6799 | / sizeof(struct constdef)); |
| 6800 | } |
| 6801 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6802 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6803 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6804 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6805 | |
| 6806 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6807 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6808 | { |
| 6809 | PyObject *result = NULL; |
| 6810 | int name; |
| 6811 | char buffer[64]; |
| 6812 | |
| 6813 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 6814 | int len = confstr(name, buffer, sizeof(buffer)); |
| 6815 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6816 | errno = 0; |
| 6817 | if (len == 0) { |
| 6818 | if (errno != 0) |
| 6819 | posix_error(); |
| 6820 | else |
| 6821 | result = PyString_FromString(""); |
| 6822 | } |
| 6823 | else { |
| 6824 | if (len >= sizeof(buffer)) { |
| 6825 | result = PyString_FromStringAndSize(NULL, len); |
| 6826 | if (result != NULL) |
| 6827 | confstr(name, PyString_AS_STRING(result), len+1); |
| 6828 | } |
| 6829 | else |
| 6830 | result = PyString_FromString(buffer); |
| 6831 | } |
| 6832 | } |
| 6833 | return result; |
| 6834 | } |
| 6835 | #endif |
| 6836 | |
| 6837 | |
| 6838 | #ifdef HAVE_SYSCONF |
| 6839 | static struct constdef posix_constants_sysconf[] = { |
| 6840 | #ifdef _SC_2_CHAR_TERM |
| 6841 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6842 | #endif |
| 6843 | #ifdef _SC_2_C_BIND |
| 6844 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6845 | #endif |
| 6846 | #ifdef _SC_2_C_DEV |
| 6847 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6848 | #endif |
| 6849 | #ifdef _SC_2_C_VERSION |
| 6850 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6851 | #endif |
| 6852 | #ifdef _SC_2_FORT_DEV |
| 6853 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6854 | #endif |
| 6855 | #ifdef _SC_2_FORT_RUN |
| 6856 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6857 | #endif |
| 6858 | #ifdef _SC_2_LOCALEDEF |
| 6859 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6860 | #endif |
| 6861 | #ifdef _SC_2_SW_DEV |
| 6862 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6863 | #endif |
| 6864 | #ifdef _SC_2_UPE |
| 6865 | {"SC_2_UPE", _SC_2_UPE}, |
| 6866 | #endif |
| 6867 | #ifdef _SC_2_VERSION |
| 6868 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6869 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6870 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6871 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6872 | #endif |
| 6873 | #ifdef _SC_ACL |
| 6874 | {"SC_ACL", _SC_ACL}, |
| 6875 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6876 | #ifdef _SC_AIO_LISTIO_MAX |
| 6877 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6878 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6879 | #ifdef _SC_AIO_MAX |
| 6880 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6881 | #endif |
| 6882 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6883 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6884 | #endif |
| 6885 | #ifdef _SC_ARG_MAX |
| 6886 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6887 | #endif |
| 6888 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6889 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6890 | #endif |
| 6891 | #ifdef _SC_ATEXIT_MAX |
| 6892 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6893 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6894 | #ifdef _SC_AUDIT |
| 6895 | {"SC_AUDIT", _SC_AUDIT}, |
| 6896 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6897 | #ifdef _SC_AVPHYS_PAGES |
| 6898 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6899 | #endif |
| 6900 | #ifdef _SC_BC_BASE_MAX |
| 6901 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6902 | #endif |
| 6903 | #ifdef _SC_BC_DIM_MAX |
| 6904 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6905 | #endif |
| 6906 | #ifdef _SC_BC_SCALE_MAX |
| 6907 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6908 | #endif |
| 6909 | #ifdef _SC_BC_STRING_MAX |
| 6910 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6911 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6912 | #ifdef _SC_CAP |
| 6913 | {"SC_CAP", _SC_CAP}, |
| 6914 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6915 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6916 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6917 | #endif |
| 6918 | #ifdef _SC_CHAR_BIT |
| 6919 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6920 | #endif |
| 6921 | #ifdef _SC_CHAR_MAX |
| 6922 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6923 | #endif |
| 6924 | #ifdef _SC_CHAR_MIN |
| 6925 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6926 | #endif |
| 6927 | #ifdef _SC_CHILD_MAX |
| 6928 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6929 | #endif |
| 6930 | #ifdef _SC_CLK_TCK |
| 6931 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6932 | #endif |
| 6933 | #ifdef _SC_COHER_BLKSZ |
| 6934 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6935 | #endif |
| 6936 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6937 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6938 | #endif |
| 6939 | #ifdef _SC_DCACHE_ASSOC |
| 6940 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6941 | #endif |
| 6942 | #ifdef _SC_DCACHE_BLKSZ |
| 6943 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6944 | #endif |
| 6945 | #ifdef _SC_DCACHE_LINESZ |
| 6946 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6947 | #endif |
| 6948 | #ifdef _SC_DCACHE_SZ |
| 6949 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6950 | #endif |
| 6951 | #ifdef _SC_DCACHE_TBLKSZ |
| 6952 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6953 | #endif |
| 6954 | #ifdef _SC_DELAYTIMER_MAX |
| 6955 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6956 | #endif |
| 6957 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6958 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6959 | #endif |
| 6960 | #ifdef _SC_EXPR_NEST_MAX |
| 6961 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6962 | #endif |
| 6963 | #ifdef _SC_FSYNC |
| 6964 | {"SC_FSYNC", _SC_FSYNC}, |
| 6965 | #endif |
| 6966 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6967 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6968 | #endif |
| 6969 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6970 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6971 | #endif |
| 6972 | #ifdef _SC_ICACHE_ASSOC |
| 6973 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6974 | #endif |
| 6975 | #ifdef _SC_ICACHE_BLKSZ |
| 6976 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6977 | #endif |
| 6978 | #ifdef _SC_ICACHE_LINESZ |
| 6979 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6980 | #endif |
| 6981 | #ifdef _SC_ICACHE_SZ |
| 6982 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6983 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6984 | #ifdef _SC_INF |
| 6985 | {"SC_INF", _SC_INF}, |
| 6986 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6987 | #ifdef _SC_INT_MAX |
| 6988 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6989 | #endif |
| 6990 | #ifdef _SC_INT_MIN |
| 6991 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6992 | #endif |
| 6993 | #ifdef _SC_IOV_MAX |
| 6994 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6995 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6996 | #ifdef _SC_IP_SECOPTS |
| 6997 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6998 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6999 | #ifdef _SC_JOB_CONTROL |
| 7000 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 7001 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7002 | #ifdef _SC_KERN_POINTERS |
| 7003 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 7004 | #endif |
| 7005 | #ifdef _SC_KERN_SIM |
| 7006 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 7007 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7008 | #ifdef _SC_LINE_MAX |
| 7009 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 7010 | #endif |
| 7011 | #ifdef _SC_LOGIN_NAME_MAX |
| 7012 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 7013 | #endif |
| 7014 | #ifdef _SC_LOGNAME_MAX |
| 7015 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 7016 | #endif |
| 7017 | #ifdef _SC_LONG_BIT |
| 7018 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 7019 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7020 | #ifdef _SC_MAC |
| 7021 | {"SC_MAC", _SC_MAC}, |
| 7022 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7023 | #ifdef _SC_MAPPED_FILES |
| 7024 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 7025 | #endif |
| 7026 | #ifdef _SC_MAXPID |
| 7027 | {"SC_MAXPID", _SC_MAXPID}, |
| 7028 | #endif |
| 7029 | #ifdef _SC_MB_LEN_MAX |
| 7030 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 7031 | #endif |
| 7032 | #ifdef _SC_MEMLOCK |
| 7033 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 7034 | #endif |
| 7035 | #ifdef _SC_MEMLOCK_RANGE |
| 7036 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 7037 | #endif |
| 7038 | #ifdef _SC_MEMORY_PROTECTION |
| 7039 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 7040 | #endif |
| 7041 | #ifdef _SC_MESSAGE_PASSING |
| 7042 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 7043 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7044 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 7045 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 7046 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7047 | #ifdef _SC_MQ_OPEN_MAX |
| 7048 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 7049 | #endif |
| 7050 | #ifdef _SC_MQ_PRIO_MAX |
| 7051 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 7052 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7053 | #ifdef _SC_NACLS_MAX |
| 7054 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 7055 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7056 | #ifdef _SC_NGROUPS_MAX |
| 7057 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 7058 | #endif |
| 7059 | #ifdef _SC_NL_ARGMAX |
| 7060 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 7061 | #endif |
| 7062 | #ifdef _SC_NL_LANGMAX |
| 7063 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 7064 | #endif |
| 7065 | #ifdef _SC_NL_MSGMAX |
| 7066 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 7067 | #endif |
| 7068 | #ifdef _SC_NL_NMAX |
| 7069 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 7070 | #endif |
| 7071 | #ifdef _SC_NL_SETMAX |
| 7072 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 7073 | #endif |
| 7074 | #ifdef _SC_NL_TEXTMAX |
| 7075 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 7076 | #endif |
| 7077 | #ifdef _SC_NPROCESSORS_CONF |
| 7078 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 7079 | #endif |
| 7080 | #ifdef _SC_NPROCESSORS_ONLN |
| 7081 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 7082 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7083 | #ifdef _SC_NPROC_CONF |
| 7084 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 7085 | #endif |
| 7086 | #ifdef _SC_NPROC_ONLN |
| 7087 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 7088 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7089 | #ifdef _SC_NZERO |
| 7090 | {"SC_NZERO", _SC_NZERO}, |
| 7091 | #endif |
| 7092 | #ifdef _SC_OPEN_MAX |
| 7093 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 7094 | #endif |
| 7095 | #ifdef _SC_PAGESIZE |
| 7096 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 7097 | #endif |
| 7098 | #ifdef _SC_PAGE_SIZE |
| 7099 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 7100 | #endif |
| 7101 | #ifdef _SC_PASS_MAX |
| 7102 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 7103 | #endif |
| 7104 | #ifdef _SC_PHYS_PAGES |
| 7105 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 7106 | #endif |
| 7107 | #ifdef _SC_PII |
| 7108 | {"SC_PII", _SC_PII}, |
| 7109 | #endif |
| 7110 | #ifdef _SC_PII_INTERNET |
| 7111 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 7112 | #endif |
| 7113 | #ifdef _SC_PII_INTERNET_DGRAM |
| 7114 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 7115 | #endif |
| 7116 | #ifdef _SC_PII_INTERNET_STREAM |
| 7117 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 7118 | #endif |
| 7119 | #ifdef _SC_PII_OSI |
| 7120 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 7121 | #endif |
| 7122 | #ifdef _SC_PII_OSI_CLTS |
| 7123 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 7124 | #endif |
| 7125 | #ifdef _SC_PII_OSI_COTS |
| 7126 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 7127 | #endif |
| 7128 | #ifdef _SC_PII_OSI_M |
| 7129 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 7130 | #endif |
| 7131 | #ifdef _SC_PII_SOCKET |
| 7132 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 7133 | #endif |
| 7134 | #ifdef _SC_PII_XTI |
| 7135 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 7136 | #endif |
| 7137 | #ifdef _SC_POLL |
| 7138 | {"SC_POLL", _SC_POLL}, |
| 7139 | #endif |
| 7140 | #ifdef _SC_PRIORITIZED_IO |
| 7141 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 7142 | #endif |
| 7143 | #ifdef _SC_PRIORITY_SCHEDULING |
| 7144 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 7145 | #endif |
| 7146 | #ifdef _SC_REALTIME_SIGNALS |
| 7147 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 7148 | #endif |
| 7149 | #ifdef _SC_RE_DUP_MAX |
| 7150 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 7151 | #endif |
| 7152 | #ifdef _SC_RTSIG_MAX |
| 7153 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 7154 | #endif |
| 7155 | #ifdef _SC_SAVED_IDS |
| 7156 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 7157 | #endif |
| 7158 | #ifdef _SC_SCHAR_MAX |
| 7159 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 7160 | #endif |
| 7161 | #ifdef _SC_SCHAR_MIN |
| 7162 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 7163 | #endif |
| 7164 | #ifdef _SC_SELECT |
| 7165 | {"SC_SELECT", _SC_SELECT}, |
| 7166 | #endif |
| 7167 | #ifdef _SC_SEMAPHORES |
| 7168 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 7169 | #endif |
| 7170 | #ifdef _SC_SEM_NSEMS_MAX |
| 7171 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 7172 | #endif |
| 7173 | #ifdef _SC_SEM_VALUE_MAX |
| 7174 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 7175 | #endif |
| 7176 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 7177 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 7178 | #endif |
| 7179 | #ifdef _SC_SHRT_MAX |
| 7180 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 7181 | #endif |
| 7182 | #ifdef _SC_SHRT_MIN |
| 7183 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 7184 | #endif |
| 7185 | #ifdef _SC_SIGQUEUE_MAX |
| 7186 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 7187 | #endif |
| 7188 | #ifdef _SC_SIGRT_MAX |
| 7189 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 7190 | #endif |
| 7191 | #ifdef _SC_SIGRT_MIN |
| 7192 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 7193 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7194 | #ifdef _SC_SOFTPOWER |
| 7195 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 7196 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7197 | #ifdef _SC_SPLIT_CACHE |
| 7198 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 7199 | #endif |
| 7200 | #ifdef _SC_SSIZE_MAX |
| 7201 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 7202 | #endif |
| 7203 | #ifdef _SC_STACK_PROT |
| 7204 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 7205 | #endif |
| 7206 | #ifdef _SC_STREAM_MAX |
| 7207 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 7208 | #endif |
| 7209 | #ifdef _SC_SYNCHRONIZED_IO |
| 7210 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 7211 | #endif |
| 7212 | #ifdef _SC_THREADS |
| 7213 | {"SC_THREADS", _SC_THREADS}, |
| 7214 | #endif |
| 7215 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 7216 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 7217 | #endif |
| 7218 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 7219 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 7220 | #endif |
| 7221 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 7222 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 7223 | #endif |
| 7224 | #ifdef _SC_THREAD_KEYS_MAX |
| 7225 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 7226 | #endif |
| 7227 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 7228 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 7229 | #endif |
| 7230 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 7231 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 7232 | #endif |
| 7233 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 7234 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 7235 | #endif |
| 7236 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 7237 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 7238 | #endif |
| 7239 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 7240 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 7241 | #endif |
| 7242 | #ifdef _SC_THREAD_STACK_MIN |
| 7243 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 7244 | #endif |
| 7245 | #ifdef _SC_THREAD_THREADS_MAX |
| 7246 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 7247 | #endif |
| 7248 | #ifdef _SC_TIMERS |
| 7249 | {"SC_TIMERS", _SC_TIMERS}, |
| 7250 | #endif |
| 7251 | #ifdef _SC_TIMER_MAX |
| 7252 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 7253 | #endif |
| 7254 | #ifdef _SC_TTY_NAME_MAX |
| 7255 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 7256 | #endif |
| 7257 | #ifdef _SC_TZNAME_MAX |
| 7258 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 7259 | #endif |
| 7260 | #ifdef _SC_T_IOV_MAX |
| 7261 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 7262 | #endif |
| 7263 | #ifdef _SC_UCHAR_MAX |
| 7264 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 7265 | #endif |
| 7266 | #ifdef _SC_UINT_MAX |
| 7267 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 7268 | #endif |
| 7269 | #ifdef _SC_UIO_MAXIOV |
| 7270 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 7271 | #endif |
| 7272 | #ifdef _SC_ULONG_MAX |
| 7273 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 7274 | #endif |
| 7275 | #ifdef _SC_USHRT_MAX |
| 7276 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 7277 | #endif |
| 7278 | #ifdef _SC_VERSION |
| 7279 | {"SC_VERSION", _SC_VERSION}, |
| 7280 | #endif |
| 7281 | #ifdef _SC_WORD_BIT |
| 7282 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 7283 | #endif |
| 7284 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 7285 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 7286 | #endif |
| 7287 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 7288 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 7289 | #endif |
| 7290 | #ifdef _SC_XBS5_LP64_OFF64 |
| 7291 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 7292 | #endif |
| 7293 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 7294 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 7295 | #endif |
| 7296 | #ifdef _SC_XOPEN_CRYPT |
| 7297 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 7298 | #endif |
| 7299 | #ifdef _SC_XOPEN_ENH_I18N |
| 7300 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 7301 | #endif |
| 7302 | #ifdef _SC_XOPEN_LEGACY |
| 7303 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 7304 | #endif |
| 7305 | #ifdef _SC_XOPEN_REALTIME |
| 7306 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 7307 | #endif |
| 7308 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 7309 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 7310 | #endif |
| 7311 | #ifdef _SC_XOPEN_SHM |
| 7312 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 7313 | #endif |
| 7314 | #ifdef _SC_XOPEN_UNIX |
| 7315 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 7316 | #endif |
| 7317 | #ifdef _SC_XOPEN_VERSION |
| 7318 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 7319 | #endif |
| 7320 | #ifdef _SC_XOPEN_XCU_VERSION |
| 7321 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 7322 | #endif |
| 7323 | #ifdef _SC_XOPEN_XPG2 |
| 7324 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 7325 | #endif |
| 7326 | #ifdef _SC_XOPEN_XPG3 |
| 7327 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 7328 | #endif |
| 7329 | #ifdef _SC_XOPEN_XPG4 |
| 7330 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 7331 | #endif |
| 7332 | }; |
| 7333 | |
| 7334 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7335 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7336 | { |
| 7337 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 7338 | sizeof(posix_constants_sysconf) |
| 7339 | / sizeof(struct constdef)); |
| 7340 | } |
| 7341 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7342 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7343 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7344 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7345 | |
| 7346 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7347 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7348 | { |
| 7349 | PyObject *result = NULL; |
| 7350 | int name; |
| 7351 | |
| 7352 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 7353 | int value; |
| 7354 | |
| 7355 | errno = 0; |
| 7356 | value = sysconf(name); |
| 7357 | if (value == -1 && errno != 0) |
| 7358 | posix_error(); |
| 7359 | else |
| 7360 | result = PyInt_FromLong(value); |
| 7361 | } |
| 7362 | return result; |
| 7363 | } |
| 7364 | #endif |
| 7365 | |
| 7366 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7367 | /* This code is used to ensure that the tables of configuration value names |
| 7368 | * are in sorted order as required by conv_confname(), and also to build the |
| 7369 | * the exported dictionaries that are used to publish information about the |
| 7370 | * names available on the host platform. |
| 7371 | * |
| 7372 | * Sorting the table at runtime ensures that the table is properly ordered |
| 7373 | * when used, even for platforms we're not able to test on. It also makes |
| 7374 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7375 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7376 | |
| 7377 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7378 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7379 | { |
| 7380 | const struct constdef *c1 = |
| 7381 | (const struct constdef *) v1; |
| 7382 | const struct constdef *c2 = |
| 7383 | (const struct constdef *) v2; |
| 7384 | |
| 7385 | return strcmp(c1->name, c2->name); |
| 7386 | } |
| 7387 | |
| 7388 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7389 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7390 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7391 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7392 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7393 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7394 | |
| 7395 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 7396 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7397 | if (d == NULL) |
| 7398 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7399 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7400 | for (i=0; i < tablesize; ++i) { |
| 7401 | PyObject *o = PyInt_FromLong(table[i].value); |
| 7402 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 7403 | Py_XDECREF(o); |
| 7404 | Py_DECREF(d); |
| 7405 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7406 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7407 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7408 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7409 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7410 | } |
| 7411 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7412 | /* Return -1 on failure, 0 on success. */ |
| 7413 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7414 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7415 | { |
| 7416 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7417 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7418 | sizeof(posix_constants_pathconf) |
| 7419 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7420 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7421 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7422 | #endif |
| 7423 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7424 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7425 | sizeof(posix_constants_confstr) |
| 7426 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7427 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7428 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7429 | #endif |
| 7430 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7431 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7432 | sizeof(posix_constants_sysconf) |
| 7433 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7434 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7435 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7436 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7437 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7438 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7439 | |
| 7440 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7441 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7442 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7443 | 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] | 7444 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7445 | |
| 7446 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7447 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7448 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7449 | abort(); |
| 7450 | /*NOTREACHED*/ |
| 7451 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 7452 | return NULL; |
| 7453 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7454 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7455 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7456 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7457 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 7458 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7459 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7460 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 7461 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 7462 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 7463 | application (if any) its extension is associated.\n\ |
| 7464 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 7465 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7466 | \n\ |
| 7467 | startfile returns as soon as the associated application is launched.\n\ |
| 7468 | There is no option to wait for the application to close, and no way\n\ |
| 7469 | to retrieve the application's exit status.\n\ |
| 7470 | \n\ |
| 7471 | The filepath is relative to the current directory. If you want to use\n\ |
| 7472 | 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] | 7473 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7474 | |
| 7475 | static PyObject * |
| 7476 | win32_startfile(PyObject *self, PyObject *args) |
| 7477 | { |
| 7478 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7479 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7480 | HINSTANCE rc; |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7481 | #ifdef Py_WIN_WIDE_FILENAMES |
| 7482 | if (unicode_file_names()) { |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7483 | PyObject *unipath, *woperation = NULL; |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7484 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 7485 | &unipath, &operation)) { |
| 7486 | PyErr_Clear(); |
| 7487 | goto normal; |
| 7488 | } |
| 7489 | |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7490 | |
| 7491 | if (operation) { |
| 7492 | woperation = PyUnicode_DecodeASCII(operation, |
| 7493 | strlen(operation), NULL); |
| 7494 | if (!woperation) { |
| 7495 | PyErr_Clear(); |
| 7496 | operation = NULL; |
| 7497 | goto normal; |
| 7498 | } |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7499 | } |
| 7500 | |
| 7501 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7502 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7503 | PyUnicode_AS_UNICODE(unipath), |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7504 | NULL, NULL, SW_SHOWNORMAL); |
| 7505 | Py_END_ALLOW_THREADS |
| 7506 | |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7507 | Py_XDECREF(woperation); |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7508 | if (rc <= (HINSTANCE)32) { |
| 7509 | PyObject *errval = win32_error_unicode("startfile", |
| 7510 | PyUnicode_AS_UNICODE(unipath)); |
| 7511 | return errval; |
| 7512 | } |
| 7513 | Py_INCREF(Py_None); |
| 7514 | return Py_None; |
| 7515 | } |
| 7516 | #endif |
| 7517 | |
| 7518 | normal: |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7519 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 7520 | Py_FileSystemDefaultEncoding, &filepath, |
| 7521 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7522 | return NULL; |
| 7523 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7524 | rc = ShellExecute((HWND)0, operation, filepath, |
| 7525 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7526 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 7527 | if (rc <= (HINSTANCE)32) { |
| 7528 | PyObject *errval = win32_error("startfile", filepath); |
| 7529 | PyMem_Free(filepath); |
| 7530 | return errval; |
| 7531 | } |
| 7532 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7533 | Py_INCREF(Py_None); |
| 7534 | return Py_None; |
| 7535 | } |
| 7536 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7537 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7538 | #ifdef HAVE_GETLOADAVG |
| 7539 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 7540 | "getloadavg() -> (float, float, float)\n\n\ |
| 7541 | Return the number of processes in the system run queue averaged over\n\ |
| 7542 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 7543 | was unobtainable"); |
| 7544 | |
| 7545 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7546 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7547 | { |
| 7548 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7549 | if (getloadavg(loadavg, 3)!=3) { |
| 7550 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 7551 | return NULL; |
| 7552 | } else |
| 7553 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 7554 | } |
| 7555 | #endif |
| 7556 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7557 | #ifdef MS_WINDOWS |
| 7558 | |
| 7559 | PyDoc_STRVAR(win32_urandom__doc__, |
| 7560 | "urandom(n) -> str\n\n\ |
| 7561 | Return a string of n random bytes suitable for cryptographic use."); |
| 7562 | |
| 7563 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 7564 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 7565 | DWORD dwFlags ); |
| 7566 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 7567 | BYTE *pbBuffer ); |
| 7568 | |
| 7569 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
| 7570 | static HCRYPTPROV hCryptProv = 0; |
| 7571 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7572 | static PyObject* |
| 7573 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7574 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7575 | int howMany; |
| 7576 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7577 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7578 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 7579 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7580 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 7581 | if (howMany < 0) |
| 7582 | return PyErr_Format(PyExc_ValueError, |
| 7583 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7584 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7585 | if (hCryptProv == 0) { |
| 7586 | HINSTANCE hAdvAPI32 = NULL; |
| 7587 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7588 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7589 | /* Obtain handle to the DLL containing CryptoAPI |
| 7590 | This should not fail */ |
| 7591 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 7592 | if(hAdvAPI32 == NULL) |
| 7593 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7594 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7595 | /* Obtain pointers to the CryptoAPI functions |
| 7596 | This will fail on some early versions of Win95 */ |
| 7597 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 7598 | hAdvAPI32, |
| 7599 | "CryptAcquireContextA"); |
| 7600 | if (pCryptAcquireContext == NULL) |
| 7601 | return PyErr_Format(PyExc_NotImplementedError, |
| 7602 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7603 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7604 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 7605 | hAdvAPI32, "CryptGenRandom"); |
| 7606 | if (pCryptAcquireContext == NULL) |
| 7607 | return PyErr_Format(PyExc_NotImplementedError, |
| 7608 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7609 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7610 | /* Acquire context */ |
| 7611 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 7612 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 7613 | return win32_error("CryptAcquireContext", NULL); |
| 7614 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7615 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7616 | /* Allocate bytes */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7617 | result = PyString_FromStringAndSize(NULL, howMany); |
| 7618 | if (result != NULL) { |
| 7619 | /* Get random data */ |
| 7620 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 7621 | PyString_AS_STRING(result))) { |
| 7622 | Py_DECREF(result); |
| 7623 | return win32_error("CryptGenRandom", NULL); |
| 7624 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7625 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7626 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7627 | } |
| 7628 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7629 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7630 | static PyMethodDef posix_methods[] = { |
| 7631 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 7632 | #ifdef HAVE_TTYNAME |
| 7633 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 7634 | #endif |
| 7635 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 7636 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7637 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7638 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7639 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7640 | #ifdef HAVE_LCHOWN |
| 7641 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 7642 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7643 | #ifdef HAVE_CHROOT |
| 7644 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 7645 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7646 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7647 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7648 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7649 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7650 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7651 | #ifdef Py_USING_UNICODE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7652 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7653 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7654 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7655 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7656 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7657 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7658 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7659 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7660 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7661 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7662 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7663 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7664 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7665 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7666 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7667 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7668 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7669 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7670 | {"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] | 7671 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7672 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7673 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7674 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7675 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7676 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7677 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7678 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7679 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7680 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7681 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7682 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7683 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7684 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7685 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7686 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7687 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7688 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7689 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7690 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7691 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7692 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7693 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7694 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7695 | #if defined(PYOS_OS2) |
| 7696 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 7697 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 7698 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7699 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7700 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7701 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7702 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7703 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7704 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7705 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7706 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7707 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7708 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7709 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7710 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7711 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7712 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7713 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7714 | #endif /* HAVE_GETEGID */ |
| 7715 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7716 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7717 | #endif /* HAVE_GETEUID */ |
| 7718 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7719 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7720 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7721 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7722 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7723 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7724 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7725 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7726 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7727 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7728 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7729 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7730 | #endif /* HAVE_GETPPID */ |
| 7731 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7732 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7733 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7734 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7735 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7736 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7737 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7738 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7739 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7740 | #ifdef HAVE_KILLPG |
| 7741 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7742 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7743 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7744 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7745 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7746 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7747 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7748 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7749 | {"popen2", win32_popen2, METH_VARARGS}, |
| 7750 | {"popen3", win32_popen3, METH_VARARGS}, |
| 7751 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7752 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7753 | #else |
| 7754 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7755 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 7756 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 7757 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 7758 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7759 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7760 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7761 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7762 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7763 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7764 | #ifdef HAVE_SETEUID |
| 7765 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7766 | #endif /* HAVE_SETEUID */ |
| 7767 | #ifdef HAVE_SETEGID |
| 7768 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7769 | #endif /* HAVE_SETEGID */ |
| 7770 | #ifdef HAVE_SETREUID |
| 7771 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7772 | #endif /* HAVE_SETREUID */ |
| 7773 | #ifdef HAVE_SETREGID |
| 7774 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7775 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7776 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7777 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7778 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7779 | #ifdef HAVE_SETGROUPS |
| 7780 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 7781 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7782 | #ifdef HAVE_GETPGID |
| 7783 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7784 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7785 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7786 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7787 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7788 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7789 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7790 | #endif /* HAVE_WAIT */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 7791 | #ifdef HAVE_WAIT3 |
| 7792 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 7793 | #endif /* HAVE_WAIT3 */ |
| 7794 | #ifdef HAVE_WAIT4 |
| 7795 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 7796 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7797 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7798 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7799 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7800 | #ifdef HAVE_GETSID |
| 7801 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 7802 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7803 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7804 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7805 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7806 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7807 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7808 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7809 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7810 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7811 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7812 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7813 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7814 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7815 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7816 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 7817 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7818 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7819 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7820 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7821 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7822 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 7823 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7824 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7825 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7826 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7827 | #endif |
| 7828 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7829 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7830 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7831 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7832 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7833 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7834 | #ifdef HAVE_DEVICE_MACROS |
| 7835 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7836 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7837 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7838 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7839 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7840 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7841 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7842 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7843 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7844 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7845 | #ifdef HAVE_UNSETENV |
| 7846 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7847 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7848 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7849 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7850 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7851 | #ifdef HAVE_FCHDIR |
| 7852 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7853 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7854 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7855 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7856 | #endif |
| 7857 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7858 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7859 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7860 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7861 | #ifdef WCOREDUMP |
| 7862 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7863 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7864 | #ifdef WIFCONTINUED |
| 7865 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7866 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7867 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7868 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7869 | #endif /* WIFSTOPPED */ |
| 7870 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7871 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7872 | #endif /* WIFSIGNALED */ |
| 7873 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7874 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7875 | #endif /* WIFEXITED */ |
| 7876 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7877 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7878 | #endif /* WEXITSTATUS */ |
| 7879 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7880 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7881 | #endif /* WTERMSIG */ |
| 7882 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7883 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7884 | #endif /* WSTOPSIG */ |
| 7885 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7886 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7887 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7888 | #endif |
| 7889 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7890 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7891 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 7892 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7893 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7894 | #endif |
| 7895 | #ifdef HAVE_TEMPNAM |
| 7896 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 7897 | #endif |
| 7898 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7899 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7900 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7901 | #ifdef HAVE_CONFSTR |
| 7902 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7903 | #endif |
| 7904 | #ifdef HAVE_SYSCONF |
| 7905 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7906 | #endif |
| 7907 | #ifdef HAVE_FPATHCONF |
| 7908 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7909 | #endif |
| 7910 | #ifdef HAVE_PATHCONF |
| 7911 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7912 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7913 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7914 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7915 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7916 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7917 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7918 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7919 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7920 | #ifdef MS_WINDOWS |
| 7921 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 7922 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7923 | {NULL, NULL} /* Sentinel */ |
| 7924 | }; |
| 7925 | |
| 7926 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7927 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7928 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7929 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7930 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7931 | } |
| 7932 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7933 | #if defined(PYOS_OS2) |
| 7934 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7935 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7936 | { |
| 7937 | APIRET rc; |
| 7938 | ULONG values[QSV_MAX+1]; |
| 7939 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7940 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7941 | |
| 7942 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7943 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7944 | Py_END_ALLOW_THREADS |
| 7945 | |
| 7946 | if (rc != NO_ERROR) { |
| 7947 | os2_error(rc); |
| 7948 | return -1; |
| 7949 | } |
| 7950 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7951 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7952 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7953 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7954 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7955 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7956 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7957 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7958 | |
| 7959 | switch (values[QSV_VERSION_MINOR]) { |
| 7960 | case 0: ver = "2.00"; break; |
| 7961 | case 10: ver = "2.10"; break; |
| 7962 | case 11: ver = "2.11"; break; |
| 7963 | case 30: ver = "3.00"; break; |
| 7964 | case 40: ver = "4.00"; break; |
| 7965 | case 50: ver = "5.00"; break; |
| 7966 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7967 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7968 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7969 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7970 | ver = &tmp[0]; |
| 7971 | } |
| 7972 | |
| 7973 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7974 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7975 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7976 | |
| 7977 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7978 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7979 | tmp[1] = ':'; |
| 7980 | tmp[2] = '\0'; |
| 7981 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7982 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7983 | } |
| 7984 | #endif |
| 7985 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7986 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7987 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7988 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7989 | #ifdef F_OK |
| 7990 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7991 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7992 | #ifdef R_OK |
| 7993 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7994 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7995 | #ifdef W_OK |
| 7996 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7997 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7998 | #ifdef X_OK |
| 7999 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8000 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8001 | #ifdef NGROUPS_MAX |
| 8002 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 8003 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8004 | #ifdef TMP_MAX |
| 8005 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 8006 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8007 | #ifdef WCONTINUED |
| 8008 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 8009 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8010 | #ifdef WNOHANG |
| 8011 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8012 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8013 | #ifdef WUNTRACED |
| 8014 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 8015 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8016 | #ifdef O_RDONLY |
| 8017 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 8018 | #endif |
| 8019 | #ifdef O_WRONLY |
| 8020 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 8021 | #endif |
| 8022 | #ifdef O_RDWR |
| 8023 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 8024 | #endif |
| 8025 | #ifdef O_NDELAY |
| 8026 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 8027 | #endif |
| 8028 | #ifdef O_NONBLOCK |
| 8029 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 8030 | #endif |
| 8031 | #ifdef O_APPEND |
| 8032 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 8033 | #endif |
| 8034 | #ifdef O_DSYNC |
| 8035 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 8036 | #endif |
| 8037 | #ifdef O_RSYNC |
| 8038 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 8039 | #endif |
| 8040 | #ifdef O_SYNC |
| 8041 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 8042 | #endif |
| 8043 | #ifdef O_NOCTTY |
| 8044 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 8045 | #endif |
| 8046 | #ifdef O_CREAT |
| 8047 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 8048 | #endif |
| 8049 | #ifdef O_EXCL |
| 8050 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 8051 | #endif |
| 8052 | #ifdef O_TRUNC |
| 8053 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 8054 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 8055 | #ifdef O_BINARY |
| 8056 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 8057 | #endif |
| 8058 | #ifdef O_TEXT |
| 8059 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 8060 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8061 | #ifdef O_LARGEFILE |
| 8062 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 8063 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 8064 | #ifdef O_SHLOCK |
| 8065 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 8066 | #endif |
| 8067 | #ifdef O_EXLOCK |
| 8068 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 8069 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8070 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8071 | /* MS Windows */ |
| 8072 | #ifdef O_NOINHERIT |
| 8073 | /* Don't inherit in child processes. */ |
| 8074 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 8075 | #endif |
| 8076 | #ifdef _O_SHORT_LIVED |
| 8077 | /* Optimize for short life (keep in memory). */ |
| 8078 | /* MS forgot to define this one with a non-underscore form too. */ |
| 8079 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 8080 | #endif |
| 8081 | #ifdef O_TEMPORARY |
| 8082 | /* Automatically delete when last handle is closed. */ |
| 8083 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 8084 | #endif |
| 8085 | #ifdef O_RANDOM |
| 8086 | /* Optimize for random access. */ |
| 8087 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 8088 | #endif |
| 8089 | #ifdef O_SEQUENTIAL |
| 8090 | /* Optimize for sequential access. */ |
| 8091 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 8092 | #endif |
| 8093 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8094 | /* GNU extensions. */ |
| 8095 | #ifdef O_DIRECT |
| 8096 | /* Direct disk access. */ |
| 8097 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 8098 | #endif |
| 8099 | #ifdef O_DIRECTORY |
| 8100 | /* Must be a directory. */ |
| 8101 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 8102 | #endif |
| 8103 | #ifdef O_NOFOLLOW |
| 8104 | /* Do not follow links. */ |
| 8105 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 8106 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8107 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8108 | /* These come from sysexits.h */ |
| 8109 | #ifdef EX_OK |
| 8110 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8111 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8112 | #ifdef EX_USAGE |
| 8113 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8114 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8115 | #ifdef EX_DATAERR |
| 8116 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8117 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8118 | #ifdef EX_NOINPUT |
| 8119 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8120 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8121 | #ifdef EX_NOUSER |
| 8122 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8123 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8124 | #ifdef EX_NOHOST |
| 8125 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8126 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8127 | #ifdef EX_UNAVAILABLE |
| 8128 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8129 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8130 | #ifdef EX_SOFTWARE |
| 8131 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8132 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8133 | #ifdef EX_OSERR |
| 8134 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8135 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8136 | #ifdef EX_OSFILE |
| 8137 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8138 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8139 | #ifdef EX_CANTCREAT |
| 8140 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8141 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8142 | #ifdef EX_IOERR |
| 8143 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8144 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8145 | #ifdef EX_TEMPFAIL |
| 8146 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8147 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8148 | #ifdef EX_PROTOCOL |
| 8149 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8150 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8151 | #ifdef EX_NOPERM |
| 8152 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8153 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8154 | #ifdef EX_CONFIG |
| 8155 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8156 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8157 | #ifdef EX_NOTFOUND |
| 8158 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8159 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8160 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8161 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8162 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 8163 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 8164 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 8165 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 8166 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 8167 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 8168 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 8169 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 8170 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 8171 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 8172 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 8173 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 8174 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 8175 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 8176 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 8177 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 8178 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 8179 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 8180 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 8181 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 8182 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 8183 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 8184 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 8185 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 8186 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 8187 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 8188 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8189 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8190 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8191 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8192 | #if defined(PYOS_OS2) |
| 8193 | if (insertvalues(d)) return -1; |
| 8194 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8195 | return 0; |
| 8196 | } |
| 8197 | |
| 8198 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8199 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8200 | #define INITFUNC initnt |
| 8201 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8202 | |
| 8203 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8204 | #define INITFUNC initos2 |
| 8205 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8206 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8207 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8208 | #define INITFUNC initposix |
| 8209 | #define MODNAME "posix" |
| 8210 | #endif |
| 8211 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 8212 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 8213 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8214 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8215 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8216 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8217 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8218 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8219 | posix__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 8220 | if (m == NULL) |
| 8221 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8222 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8223 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8224 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8225 | Py_XINCREF(v); |
| 8226 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8227 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8228 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8229 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8230 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8231 | return; |
| 8232 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8233 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8234 | return; |
| 8235 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8236 | Py_INCREF(PyExc_OSError); |
| 8237 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8238 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8239 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 8240 | if (posix_putenv_garbage == NULL) |
| 8241 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8242 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8243 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 8244 | stat_result_desc.name = MODNAME ".stat_result"; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 8245 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 8246 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 8247 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8248 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 8249 | structseq_new = StatResultType.tp_new; |
| 8250 | StatResultType.tp_new = statresult_new; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8251 | Py_INCREF((PyObject*) &StatResultType); |
| 8252 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8253 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 8254 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8255 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8256 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 8257 | PyModule_AddObject(m, "statvfs_result", |
| 8258 | (PyObject*) &StatVFSResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8259 | } |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 8260 | |
| 8261 | #ifdef __cplusplus |
| 8262 | } |
| 8263 | #endif |
| 8264 | |