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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 16 | #include "Python.h" |
| 17 | #include "structseq.h" |
| 18 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 19 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 20 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 21 | #endif /* defined(__VMS) */ |
| 22 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 23 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 24 | "This module provides access to operating system functionality that is\n\ |
| 25 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 26 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 27 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 29 | #ifndef Py_USING_UNICODE |
| 30 | /* This is used in signatures of functions. */ |
| 31 | #define Py_UNICODE void |
| 32 | #endif |
| 33 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 34 | #if defined(PYOS_OS2) |
| 35 | #define INCL_DOS |
| 36 | #define INCL_DOSERRORS |
| 37 | #define INCL_DOSPROCESS |
| 38 | #define INCL_NOPMAPI |
| 39 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 40 | #if defined(PYCC_GCC) |
| 41 | #include <ctype.h> |
| 42 | #include <io.h> |
| 43 | #include <stdio.h> |
| 44 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 45 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 46 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 49 | #include <sys/types.h> |
| 50 | #include <sys/stat.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 52 | #ifdef HAVE_SYS_WAIT_H |
| 53 | #include <sys/wait.h> /* For WNOHANG */ |
| 54 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 56 | #include <signal.h> |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 58 | #ifdef HAVE_FCNTL_H |
| 59 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 60 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 62 | #ifdef HAVE_GRP_H |
| 63 | #include <grp.h> |
| 64 | #endif |
| 65 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 66 | #ifdef HAVE_SYSEXITS_H |
| 67 | #include <sysexits.h> |
| 68 | #endif /* HAVE_SYSEXITS_H */ |
| 69 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 70 | #ifdef HAVE_SYS_LOADAVG_H |
| 71 | #include <sys/loadavg.h> |
| 72 | #endif |
| 73 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 74 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 75 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 76 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 77 | #include <process.h> |
| 78 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 79 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 80 | #define HAVE_GETCWD 1 |
| 81 | #define HAVE_OPENDIR 1 |
| 82 | #define HAVE_SYSTEM 1 |
| 83 | #if defined(__OS2__) |
| 84 | #define HAVE_EXECV 1 |
| 85 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 86 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 87 | #include <process.h> |
| 88 | #else |
| 89 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 90 | #define HAVE_EXECV 1 |
| 91 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 92 | #define HAVE_OPENDIR 1 |
| 93 | #define HAVE_PIPE 1 |
| 94 | #define HAVE_POPEN 1 |
| 95 | #define HAVE_SYSTEM 1 |
| 96 | #define HAVE_WAIT 1 |
| 97 | #else |
| 98 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 99 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 100 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 101 | #define HAVE_EXECV 1 |
| 102 | #define HAVE_PIPE 1 |
| 103 | #define HAVE_POPEN 1 |
| 104 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 105 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 106 | #define HAVE_FSYNC 1 |
| 107 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 108 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 109 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 110 | /* 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] | 111 | #else /* all other compilers */ |
| 112 | /* Unix functions that the configure script doesn't check for */ |
| 113 | #define HAVE_EXECV 1 |
| 114 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 115 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 116 | #define HAVE_FORK1 1 |
| 117 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 118 | #define HAVE_GETCWD 1 |
| 119 | #define HAVE_GETEGID 1 |
| 120 | #define HAVE_GETEUID 1 |
| 121 | #define HAVE_GETGID 1 |
| 122 | #define HAVE_GETPPID 1 |
| 123 | #define HAVE_GETUID 1 |
| 124 | #define HAVE_KILL 1 |
| 125 | #define HAVE_OPENDIR 1 |
| 126 | #define HAVE_PIPE 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 127 | #ifndef __rtems__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 128 | #define HAVE_POPEN 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 129 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 130 | #define HAVE_SYSTEM 1 |
| 131 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 132 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 133 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #endif /* _MSC_VER */ |
| 135 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 136 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 137 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 139 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 140 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 141 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 142 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 143 | (default) */ |
| 144 | extern char *ctermid_r(char *); |
| 145 | #endif |
| 146 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 147 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 148 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 149 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 150 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 151 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 152 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 153 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 154 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 155 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 156 | #endif |
| 157 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 158 | extern int chdir(char *); |
| 159 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 160 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 161 | extern int chdir(const char *); |
| 162 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 163 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 164 | #ifdef __BORLANDC__ |
| 165 | extern int chmod(const char *, int); |
| 166 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 167 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 168 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int chown(const char *, uid_t, gid_t); |
| 170 | extern char *getcwd(char *, int); |
| 171 | extern char *strerror(int); |
| 172 | extern int link(const char *, const char *); |
| 173 | extern int rename(const char *, const char *); |
| 174 | extern int stat(const char *, struct stat *); |
| 175 | extern int unlink(const char *); |
| 176 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 177 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 178 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 179 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 180 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 181 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 182 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 183 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 185 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 187 | #ifdef HAVE_UTIME_H |
| 188 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 189 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 191 | #ifdef HAVE_SYS_UTIME_H |
| 192 | #include <sys/utime.h> |
| 193 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 194 | #endif /* HAVE_SYS_UTIME_H */ |
| 195 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 196 | #ifdef HAVE_SYS_TIMES_H |
| 197 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 198 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 199 | |
| 200 | #ifdef HAVE_SYS_PARAM_H |
| 201 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 202 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 203 | |
| 204 | #ifdef HAVE_SYS_UTSNAME_H |
| 205 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 206 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 207 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 208 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 210 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 211 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 212 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 213 | #include <direct.h> |
| 214 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 215 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 216 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 217 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 218 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 219 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 220 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 221 | #endif |
| 222 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 223 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 224 | #endif |
| 225 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 227 | #endif |
| 228 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 230 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 231 | #include <direct.h> |
| 232 | #include <io.h> |
| 233 | #include <process.h> |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 234 | #include "osdefs.h" |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 235 | #define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 236 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 237 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 238 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 239 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 240 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 242 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 244 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 245 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 246 | #ifndef MAXPATHLEN |
| 247 | #define MAXPATHLEN 1024 |
| 248 | #endif /* MAXPATHLEN */ |
| 249 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 250 | #ifdef UNION_WAIT |
| 251 | /* Emulate some macros on systems that have a union instead of macros */ |
| 252 | |
| 253 | #ifndef WIFEXITED |
| 254 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 255 | #endif |
| 256 | |
| 257 | #ifndef WEXITSTATUS |
| 258 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 259 | #endif |
| 260 | |
| 261 | #ifndef WTERMSIG |
| 262 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 263 | #endif |
| 264 | |
| 265 | #endif /* UNION_WAIT */ |
| 266 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 267 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 268 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 269 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 270 | #define USE_CTERMID_R |
| 271 | #endif |
| 272 | |
| 273 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 274 | #define USE_TMPNAM_R |
| 275 | #endif |
| 276 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 277 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 278 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 279 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 280 | # define STAT _stati64 |
| 281 | # define FSTAT _fstati64 |
| 282 | # define STRUCT_STAT struct _stati64 |
| 283 | #else |
| 284 | # define STAT stat |
| 285 | # define FSTAT fstat |
| 286 | # define STRUCT_STAT struct stat |
| 287 | #endif |
| 288 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 289 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 290 | #include <sys/mkdev.h> |
| 291 | #else |
| 292 | #if defined(MAJOR_IN_SYSMACROS) |
| 293 | #include <sys/sysmacros.h> |
| 294 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 295 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 296 | #include <sys/mkdev.h> |
| 297 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 298 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 299 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 300 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 301 | #ifdef WITH_NEXT_FRAMEWORK |
| 302 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 303 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 304 | */ |
| 305 | #include <crt_externs.h> |
| 306 | static char **environ; |
| 307 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 308 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 309 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 310 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 311 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 312 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 313 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 314 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 316 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 317 | if (d == NULL) |
| 318 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 319 | #ifdef WITH_NEXT_FRAMEWORK |
| 320 | if (environ == NULL) |
| 321 | environ = *_NSGetEnviron(); |
| 322 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 323 | if (environ == NULL) |
| 324 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 325 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 326 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 327 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 328 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 329 | char *p = strchr(*e, '='); |
| 330 | if (p == NULL) |
| 331 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 332 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 333 | if (k == NULL) { |
| 334 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 335 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 336 | } |
| 337 | v = PyString_FromString(p+1); |
| 338 | if (v == NULL) { |
| 339 | PyErr_Clear(); |
| 340 | Py_DECREF(k); |
| 341 | continue; |
| 342 | } |
| 343 | if (PyDict_GetItem(d, k) == NULL) { |
| 344 | if (PyDict_SetItem(d, k, v) != 0) |
| 345 | PyErr_Clear(); |
| 346 | } |
| 347 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 348 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 349 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 350 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 351 | { |
| 352 | APIRET rc; |
| 353 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 354 | |
| 355 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 356 | 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] | 357 | PyObject *v = PyString_FromString(buffer); |
| 358 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 359 | Py_DECREF(v); |
| 360 | } |
| 361 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 362 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 363 | PyObject *v = PyString_FromString(buffer); |
| 364 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 365 | Py_DECREF(v); |
| 366 | } |
| 367 | } |
| 368 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 369 | return d; |
| 370 | } |
| 371 | |
| 372 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 373 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 374 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 375 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 376 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 377 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 378 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 379 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 380 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 381 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 382 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 383 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 386 | #ifdef Py_WIN_WIDE_FILENAMES |
| 387 | static PyObject * |
| 388 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 389 | { |
| 390 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 391 | } |
| 392 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 393 | |
| 394 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 395 | static PyObject * |
| 396 | posix_error_with_allocated_filename(char* name) |
| 397 | { |
| 398 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 399 | PyMem_Free(name); |
| 400 | return rc; |
| 401 | } |
| 402 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 403 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 404 | static PyObject * |
| 405 | win32_error(char* function, char* filename) |
| 406 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 407 | /* XXX We should pass the function name along in the future. |
| 408 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 409 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 410 | Windows error object, which is non-trivial. |
| 411 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 412 | errno = GetLastError(); |
| 413 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 414 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 415 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 416 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 417 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 418 | |
| 419 | #ifdef Py_WIN_WIDE_FILENAMES |
| 420 | static PyObject * |
| 421 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 422 | { |
| 423 | /* XXX - see win32_error for comments on 'function' */ |
| 424 | errno = GetLastError(); |
| 425 | if (filename) |
| 426 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 427 | else |
| 428 | return PyErr_SetFromWindowsErr(errno); |
| 429 | } |
| 430 | |
| 431 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 432 | { |
| 433 | /* XXX Perhaps we should make this API an alias of |
| 434 | PyObject_Unicode() instead ?! */ |
| 435 | if (PyUnicode_CheckExact(obj)) { |
| 436 | Py_INCREF(obj); |
| 437 | return obj; |
| 438 | } |
| 439 | if (PyUnicode_Check(obj)) { |
| 440 | /* For a Unicode subtype that's not a Unicode object, |
| 441 | return a true Unicode object with the same data. */ |
| 442 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 443 | PyUnicode_GET_SIZE(obj)); |
| 444 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 445 | return PyUnicode_FromEncodedObject(obj, |
| 446 | Py_FileSystemDefaultEncoding, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 447 | "strict"); |
| 448 | } |
| 449 | |
| 450 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 451 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 452 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 453 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 454 | #if defined(PYOS_OS2) |
| 455 | /********************************************************************** |
| 456 | * Helper Function to Trim and Format OS/2 Messages |
| 457 | **********************************************************************/ |
| 458 | static void |
| 459 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 460 | { |
| 461 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 462 | |
| 463 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 464 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 465 | |
| 466 | while (lastc > msgbuf && isspace(*lastc)) |
| 467 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 468 | } |
| 469 | |
| 470 | /* Add Optional Reason Text */ |
| 471 | if (reason) { |
| 472 | strcat(msgbuf, " : "); |
| 473 | strcat(msgbuf, reason); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /********************************************************************** |
| 478 | * Decode an OS/2 Operating System Error Code |
| 479 | * |
| 480 | * A convenience function to lookup an OS/2 error code and return a |
| 481 | * text message we can use to raise a Python exception. |
| 482 | * |
| 483 | * Notes: |
| 484 | * The messages for errors returned from the OS/2 kernel reside in |
| 485 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 486 | * |
| 487 | **********************************************************************/ |
| 488 | static char * |
| 489 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 490 | { |
| 491 | APIRET rc; |
| 492 | ULONG msglen; |
| 493 | |
| 494 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 495 | Py_BEGIN_ALLOW_THREADS |
| 496 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 497 | errorcode, "oso001.msg", &msglen); |
| 498 | Py_END_ALLOW_THREADS |
| 499 | |
| 500 | if (rc == NO_ERROR) |
| 501 | os2_formatmsg(msgbuf, msglen, reason); |
| 502 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 503 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 504 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 505 | |
| 506 | return msgbuf; |
| 507 | } |
| 508 | |
| 509 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 510 | errors are not in a global variable e.g. 'errno' nor are |
| 511 | they congruent with posix error numbers. */ |
| 512 | |
| 513 | static PyObject * os2_error(int code) |
| 514 | { |
| 515 | char text[1024]; |
| 516 | PyObject *v; |
| 517 | |
| 518 | os2_strerror(text, sizeof(text), code, ""); |
| 519 | |
| 520 | v = Py_BuildValue("(is)", code, text); |
| 521 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 522 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 523 | Py_DECREF(v); |
| 524 | } |
| 525 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 526 | } |
| 527 | |
| 528 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 529 | |
| 530 | /* POSIX generic methods */ |
| 531 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 532 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 533 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 534 | { |
| 535 | int fd; |
| 536 | int res; |
| 537 | fd = PyObject_AsFileDescriptor(fdobj); |
| 538 | if (fd < 0) |
| 539 | return NULL; |
| 540 | Py_BEGIN_ALLOW_THREADS |
| 541 | res = (*func)(fd); |
| 542 | Py_END_ALLOW_THREADS |
| 543 | if (res < 0) |
| 544 | return posix_error(); |
| 545 | Py_INCREF(Py_None); |
| 546 | return Py_None; |
| 547 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 548 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 549 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 550 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 551 | unicode_file_names(void) |
| 552 | { |
| 553 | static int canusewide = -1; |
| 554 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 555 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 556 | the Windows NT family. */ |
| 557 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 558 | } |
| 559 | return canusewide; |
| 560 | } |
| 561 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 562 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 563 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 564 | posix_1str(PyObject *args, char *format, int (*func)(const char*), |
| 565 | char *wformat, int (*wfunc)(const Py_UNICODE*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 566 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 567 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 568 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 569 | #ifdef Py_WIN_WIDE_FILENAMES |
| 570 | if (unicode_file_names()) { |
| 571 | PyUnicodeObject *po; |
| 572 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 573 | Py_BEGIN_ALLOW_THREADS |
| 574 | /* PyUnicode_AS_UNICODE OK without thread |
| 575 | lock as it is a simple dereference. */ |
| 576 | res = (*wfunc)(PyUnicode_AS_UNICODE(po)); |
| 577 | Py_END_ALLOW_THREADS |
| 578 | if (res < 0) |
Mark Hammond | d389036 | 2002-10-03 07:24:48 +0000 | [diff] [blame] | 579 | return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 580 | Py_INCREF(Py_None); |
| 581 | return Py_None; |
| 582 | } |
| 583 | /* Drop the argument parsing error as narrow |
| 584 | strings are also valid. */ |
| 585 | PyErr_Clear(); |
| 586 | } |
| 587 | #else |
| 588 | /* Platforms that don't support Unicode filenames |
| 589 | shouldn't be passing these extra params */ |
| 590 | assert(wformat==NULL && wfunc == NULL); |
| 591 | #endif |
| 592 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 593 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 594 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 595 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 596 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 597 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 598 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 599 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 600 | return posix_error_with_allocated_filename(path1); |
| 601 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 602 | Py_INCREF(Py_None); |
| 603 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 606 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 607 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 608 | char *format, |
| 609 | int (*func)(const char *, const char *), |
| 610 | char *wformat, |
| 611 | int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 612 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 613 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 614 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 615 | #ifdef Py_WIN_WIDE_FILENAMES |
| 616 | if (unicode_file_names()) { |
| 617 | PyObject *po1; |
| 618 | PyObject *po2; |
| 619 | if (PyArg_ParseTuple(args, wformat, &po1, &po2)) { |
| 620 | if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) { |
| 621 | PyObject *wpath1; |
| 622 | PyObject *wpath2; |
| 623 | wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1); |
| 624 | wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2); |
| 625 | if (!wpath1 || !wpath2) { |
| 626 | Py_XDECREF(wpath1); |
| 627 | Py_XDECREF(wpath2); |
| 628 | return NULL; |
| 629 | } |
| 630 | Py_BEGIN_ALLOW_THREADS |
| 631 | /* PyUnicode_AS_UNICODE OK without thread |
| 632 | lock as it is a simple dereference. */ |
| 633 | res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1), |
| 634 | PyUnicode_AS_UNICODE(wpath2)); |
| 635 | Py_END_ALLOW_THREADS |
| 636 | Py_XDECREF(wpath1); |
| 637 | Py_XDECREF(wpath2); |
| 638 | if (res != 0) |
| 639 | return posix_error(); |
| 640 | Py_INCREF(Py_None); |
| 641 | return Py_None; |
| 642 | } |
| 643 | /* Else flow through as neither is Unicode. */ |
| 644 | } |
| 645 | /* Drop the argument parsing error as narrow |
| 646 | strings are also valid. */ |
| 647 | PyErr_Clear(); |
| 648 | } |
| 649 | #else |
| 650 | /* Platforms that don't support Unicode filenames |
| 651 | shouldn't be passing these extra params */ |
| 652 | assert(wformat==NULL && wfunc == NULL); |
| 653 | #endif |
| 654 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 655 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 656 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 657 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 658 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 659 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 660 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 661 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 662 | PyMem_Free(path1); |
| 663 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 664 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 665 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 666 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 667 | Py_INCREF(Py_None); |
| 668 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 671 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 672 | "stat_result: Result from stat or lstat.\n\n\ |
| 673 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 674 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 675 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 676 | \n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 677 | Posix/windows: If your platform supports st_blksize, st_blocks, or st_rdev,\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 678 | they are available as attributes only.\n\ |
| 679 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 680 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 681 | |
| 682 | static PyStructSequence_Field stat_result_fields[] = { |
| 683 | {"st_mode", "protection bits"}, |
| 684 | {"st_ino", "inode"}, |
| 685 | {"st_dev", "device"}, |
| 686 | {"st_nlink", "number of hard links"}, |
| 687 | {"st_uid", "user ID of owner"}, |
| 688 | {"st_gid", "group ID of owner"}, |
| 689 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 690 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 691 | {NULL, "integer time of last access"}, |
| 692 | {NULL, "integer time of last modification"}, |
| 693 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 694 | {"st_atime", "time of last access"}, |
| 695 | {"st_mtime", "time of last modification"}, |
| 696 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 697 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 698 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 699 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 700 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 701 | {"st_blocks", "number of blocks allocated"}, |
| 702 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 703 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 704 | {"st_rdev", "device type (if inode device)"}, |
| 705 | #endif |
| 706 | {0} |
| 707 | }; |
| 708 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 709 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 710 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 711 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 712 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 713 | #endif |
| 714 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 715 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 716 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 717 | #else |
| 718 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 719 | #endif |
| 720 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 721 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 722 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 723 | #else |
| 724 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 725 | #endif |
| 726 | |
| 727 | static PyStructSequence_Desc stat_result_desc = { |
| 728 | "stat_result", /* name */ |
| 729 | stat_result__doc__, /* doc */ |
| 730 | stat_result_fields, |
| 731 | 10 |
| 732 | }; |
| 733 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 734 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 735 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 736 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 737 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 738 | 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] | 739 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 740 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 741 | |
| 742 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 743 | {"f_bsize", }, |
| 744 | {"f_frsize", }, |
| 745 | {"f_blocks", }, |
| 746 | {"f_bfree", }, |
| 747 | {"f_bavail", }, |
| 748 | {"f_files", }, |
| 749 | {"f_ffree", }, |
| 750 | {"f_favail", }, |
| 751 | {"f_flag", }, |
| 752 | {"f_namemax",}, |
| 753 | {0} |
| 754 | }; |
| 755 | |
| 756 | static PyStructSequence_Desc statvfs_result_desc = { |
| 757 | "statvfs_result", /* name */ |
| 758 | statvfs_result__doc__, /* doc */ |
| 759 | statvfs_result_fields, |
| 760 | 10 |
| 761 | }; |
| 762 | |
| 763 | static PyTypeObject StatResultType; |
| 764 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 765 | static newfunc structseq_new; |
| 766 | |
| 767 | static PyObject * |
| 768 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 769 | { |
| 770 | PyStructSequence *result; |
| 771 | int i; |
| 772 | |
| 773 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 774 | if (!result) |
| 775 | return NULL; |
| 776 | /* If we have been initialized from a tuple, |
| 777 | st_?time might be set to None. Initialize it |
| 778 | from the int slots. */ |
| 779 | for (i = 7; i <= 9; i++) { |
| 780 | if (result->ob_item[i+3] == Py_None) { |
| 781 | Py_DECREF(Py_None); |
| 782 | Py_INCREF(result->ob_item[i]); |
| 783 | result->ob_item[i+3] = result->ob_item[i]; |
| 784 | } |
| 785 | } |
| 786 | return (PyObject*)result; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | |
| 791 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 792 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 793 | |
| 794 | PyDoc_STRVAR(stat_float_times__doc__, |
| 795 | "stat_float_times([newval]) -> oldval\n\n\ |
| 796 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 797 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 798 | future calls return ints. \n\ |
| 799 | If newval is omitted, return the current setting.\n"); |
| 800 | |
| 801 | static PyObject* |
| 802 | stat_float_times(PyObject* self, PyObject *args) |
| 803 | { |
| 804 | int newval = -1; |
| 805 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 806 | return NULL; |
| 807 | if (newval == -1) |
| 808 | /* Return old value */ |
| 809 | return PyBool_FromLong(_stat_float_times); |
| 810 | _stat_float_times = newval; |
| 811 | Py_INCREF(Py_None); |
| 812 | return Py_None; |
| 813 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 814 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 815 | static void |
| 816 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 817 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 818 | PyObject *fval,*ival; |
| 819 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 820 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 821 | #else |
| 822 | ival = PyInt_FromLong((long)sec); |
| 823 | #endif |
| 824 | if (_stat_float_times) { |
| 825 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 826 | } else { |
| 827 | fval = ival; |
| 828 | Py_INCREF(fval); |
| 829 | } |
| 830 | PyStructSequence_SET_ITEM(v, index, ival); |
| 831 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 834 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 835 | (used by posix_stat() and posix_fstat()) */ |
| 836 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 837 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 838 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 839 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 840 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 841 | if (v == NULL) |
| 842 | return NULL; |
| 843 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 844 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 845 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 846 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 847 | PyLong_FromLongLong((PY_LONG_LONG)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 848 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 849 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 850 | #endif |
| 851 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 852 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 853 | PyLong_FromLongLong((PY_LONG_LONG)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 854 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 855 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 856 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 857 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 858 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 859 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 860 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 861 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 862 | PyLong_FromLongLong((PY_LONG_LONG)st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 863 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 864 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 865 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 866 | |
| 867 | #ifdef HAVE_STAT_TV_NSEC |
| 868 | ansec = st.st_atim.tv_nsec; |
| 869 | mnsec = st.st_mtim.tv_nsec; |
| 870 | cnsec = st.st_ctim.tv_nsec; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 871 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 872 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 873 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 874 | fill_time(v, 7, st.st_atime, ansec); |
| 875 | fill_time(v, 8, st.st_mtime, mnsec); |
| 876 | fill_time(v, 9, st.st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 877 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 878 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 879 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 880 | PyInt_FromLong((long)st.st_blksize)); |
| 881 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 882 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 883 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 884 | PyInt_FromLong((long)st.st_blocks)); |
| 885 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 886 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 887 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 888 | PyInt_FromLong((long)st.st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 889 | #endif |
| 890 | |
| 891 | if (PyErr_Occurred()) { |
| 892 | Py_DECREF(v); |
| 893 | return NULL; |
| 894 | } |
| 895 | |
| 896 | return v; |
| 897 | } |
| 898 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 899 | #ifdef MS_WINDOWS |
| 900 | |
| 901 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 902 | where / can be used in place of \ and the trailing slash is optional. |
| 903 | Both SERVER and SHARE must have at least one character. |
| 904 | */ |
| 905 | |
| 906 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 907 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
| 908 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
| 909 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 910 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 911 | IsUNCRootA(char *path, int pathlen) |
| 912 | { |
| 913 | #define ISSLASH ISSLASHA |
| 914 | |
| 915 | int i, share; |
| 916 | |
| 917 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 918 | /* minimum UNCRoot is \\x\y */ |
| 919 | return FALSE; |
| 920 | for (i = 2; i < pathlen ; i++) |
| 921 | if (ISSLASH(path[i])) break; |
| 922 | if (i == 2 || i == pathlen) |
| 923 | /* do not allow \\\SHARE or \\SERVER */ |
| 924 | return FALSE; |
| 925 | share = i+1; |
| 926 | for (i = share; i < pathlen; i++) |
| 927 | if (ISSLASH(path[i])) break; |
| 928 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 929 | |
| 930 | #undef ISSLASH |
| 931 | } |
| 932 | |
| 933 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 934 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 935 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 936 | { |
| 937 | #define ISSLASH ISSLASHW |
| 938 | |
| 939 | int i, share; |
| 940 | |
| 941 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 942 | /* minimum UNCRoot is \\x\y */ |
| 943 | return FALSE; |
| 944 | for (i = 2; i < pathlen ; i++) |
| 945 | if (ISSLASH(path[i])) break; |
| 946 | if (i == 2 || i == pathlen) |
| 947 | /* do not allow \\\SHARE or \\SERVER */ |
| 948 | return FALSE; |
| 949 | share = i+1; |
| 950 | for (i = share; i < pathlen; i++) |
| 951 | if (ISSLASH(path[i])) break; |
| 952 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 953 | |
| 954 | #undef ISSLASH |
| 955 | } |
| 956 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 957 | #endif /* MS_WINDOWS */ |
| 958 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 959 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 960 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 961 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 962 | #ifdef __VMS |
| 963 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 964 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 965 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 966 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 967 | char *wformat, |
| 968 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 969 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 970 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 971 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 972 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 973 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 974 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 975 | #ifdef MS_WINDOWS |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 976 | int pathlen; |
| 977 | char pathcopy[MAX_PATH]; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 978 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 979 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 980 | |
| 981 | #ifdef Py_WIN_WIDE_FILENAMES |
| 982 | /* If on wide-character-capable OS see if argument |
| 983 | is Unicode and if so use wide API. */ |
| 984 | if (unicode_file_names()) { |
| 985 | PyUnicodeObject *po; |
| 986 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 987 | Py_UNICODE wpath[MAX_PATH+1]; |
| 988 | pathlen = wcslen(PyUnicode_AS_UNICODE(po)); |
| 989 | /* the library call can blow up if the file name is too long! */ |
| 990 | if (pathlen > MAX_PATH) { |
| 991 | errno = ENAMETOOLONG; |
| 992 | return posix_error(); |
| 993 | } |
| 994 | wcscpy(wpath, PyUnicode_AS_UNICODE(po)); |
| 995 | /* Remove trailing slash or backslash, unless it's the current |
| 996 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 997 | */ |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 998 | if (pathlen > 0) { |
| 999 | if (ISSLASHW(wpath[pathlen-1])) { |
| 1000 | /* It does end with a slash -- exempt the root drive cases. */ |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1001 | if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':') || |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1002 | IsUNCRootW(wpath, pathlen)) |
| 1003 | /* leave it alone */; |
| 1004 | else { |
| 1005 | /* nuke the trailing backslash */ |
| 1006 | wpath[pathlen-1] = L'\0'; |
| 1007 | } |
| 1008 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1009 | else if (ISSLASHW(wpath[1]) && pathlen < ARRAYSIZE(wpath)-1 && |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1010 | IsUNCRootW(wpath, pathlen)) { |
| 1011 | /* UNC root w/o trailing slash: add one when there's room */ |
| 1012 | wpath[pathlen++] = L'\\'; |
| 1013 | wpath[pathlen] = L'\0'; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | Py_BEGIN_ALLOW_THREADS |
| 1017 | /* PyUnicode_AS_UNICODE result OK without |
| 1018 | thread lock as it is a simple dereference. */ |
| 1019 | res = wstatfunc(wpath, &st); |
| 1020 | Py_END_ALLOW_THREADS |
| 1021 | if (res != 0) |
| 1022 | return posix_error_with_unicode_filename(wpath); |
| 1023 | return _pystat_fromstructstat(st); |
| 1024 | } |
| 1025 | /* Drop the argument parsing error as narrow strings |
| 1026 | are also valid. */ |
| 1027 | PyErr_Clear(); |
| 1028 | } |
| 1029 | #endif |
| 1030 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1031 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1032 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1033 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1034 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1035 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1036 | #ifdef MS_WINDOWS |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1037 | pathlen = strlen(path); |
| 1038 | /* the library call can blow up if the file name is too long! */ |
| 1039 | if (pathlen > MAX_PATH) { |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1040 | PyMem_Free(pathfree); |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1041 | errno = ENAMETOOLONG; |
| 1042 | return posix_error(); |
| 1043 | } |
| 1044 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1045 | /* Remove trailing slash or backslash, unless it's the current |
| 1046 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 1047 | */ |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1048 | if (pathlen > 0) { |
| 1049 | if (ISSLASHA(path[pathlen-1])) { |
| 1050 | /* It does end with a slash -- exempt the root drive cases. */ |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1051 | if (pathlen == 1 || (pathlen == 3 && path[1] == ':') || |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1052 | IsUNCRootA(path, pathlen)) |
| 1053 | /* leave it alone */; |
| 1054 | else { |
| 1055 | /* nuke the trailing backslash */ |
| 1056 | strncpy(pathcopy, path, pathlen); |
| 1057 | pathcopy[pathlen-1] = '\0'; |
| 1058 | path = pathcopy; |
| 1059 | } |
| 1060 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1061 | else if (ISSLASHA(path[1]) && pathlen < ARRAYSIZE(pathcopy)-1 && |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1062 | IsUNCRootA(path, pathlen)) { |
| 1063 | /* UNC root w/o trailing slash: add one when there's room */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1064 | strncpy(pathcopy, path, pathlen); |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1065 | pathcopy[pathlen++] = '\\'; |
| 1066 | pathcopy[pathlen] = '\0'; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1067 | path = pathcopy; |
| 1068 | } |
| 1069 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1070 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1071 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1072 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1073 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1074 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1075 | if (res != 0) |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1076 | return posix_error_with_allocated_filename(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1077 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1078 | PyMem_Free(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1079 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | |
| 1083 | /* POSIX methods */ |
| 1084 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1085 | PyDoc_STRVAR(posix_access__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1086 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1087 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1088 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1089 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1090 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1091 | 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] | 1092 | |
| 1093 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1094 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1095 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1096 | char *path; |
| 1097 | int mode; |
| 1098 | int res; |
| 1099 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1100 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1101 | if (unicode_file_names()) { |
| 1102 | PyUnicodeObject *po; |
| 1103 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1104 | Py_BEGIN_ALLOW_THREADS |
| 1105 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1106 | it is a simple dereference. */ |
| 1107 | res = _waccess(PyUnicode_AS_UNICODE(po), mode); |
| 1108 | Py_END_ALLOW_THREADS |
| 1109 | return(PyBool_FromLong(res == 0)); |
| 1110 | } |
| 1111 | /* Drop the argument parsing error as narrow strings |
| 1112 | are also valid. */ |
| 1113 | PyErr_Clear(); |
| 1114 | } |
| 1115 | #endif |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame^] | 1116 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1117 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1118 | return NULL; |
| 1119 | Py_BEGIN_ALLOW_THREADS |
| 1120 | res = access(path, mode); |
| 1121 | Py_END_ALLOW_THREADS |
Guido van Rossum | 674deb2 | 2002-09-01 15:06:28 +0000 | [diff] [blame] | 1122 | return(PyBool_FromLong(res == 0)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1125 | #ifndef F_OK |
| 1126 | #define F_OK 0 |
| 1127 | #endif |
| 1128 | #ifndef R_OK |
| 1129 | #define R_OK 4 |
| 1130 | #endif |
| 1131 | #ifndef W_OK |
| 1132 | #define W_OK 2 |
| 1133 | #endif |
| 1134 | #ifndef X_OK |
| 1135 | #define X_OK 1 |
| 1136 | #endif |
| 1137 | |
| 1138 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1139 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1140 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1141 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1142 | |
| 1143 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1144 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1145 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1146 | int id; |
| 1147 | char *ret; |
| 1148 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1149 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1150 | return NULL; |
| 1151 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1152 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1153 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1154 | if (id == 0) { |
| 1155 | ret = ttyname(); |
| 1156 | } |
| 1157 | else { |
| 1158 | ret = NULL; |
| 1159 | } |
| 1160 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1161 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1162 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1163 | if (ret == NULL) |
| 1164 | return(posix_error()); |
| 1165 | return(PyString_FromString(ret)); |
| 1166 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1167 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1168 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1169 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1170 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1171 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1172 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1173 | |
| 1174 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1175 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1176 | { |
| 1177 | char *ret; |
| 1178 | char buffer[L_ctermid]; |
| 1179 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1180 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1181 | ret = ctermid_r(buffer); |
| 1182 | #else |
| 1183 | ret = ctermid(buffer); |
| 1184 | #endif |
| 1185 | if (ret == NULL) |
| 1186 | return(posix_error()); |
| 1187 | return(PyString_FromString(buffer)); |
| 1188 | } |
| 1189 | #endif |
| 1190 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1191 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1192 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1193 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1194 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1195 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1196 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1197 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1198 | #ifdef MS_WINDOWS |
| 1199 | return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir); |
| 1200 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1201 | return posix_1str(args, "et:chdir", _chdir2, NULL, NULL); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1202 | #elif defined(__VMS) |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1203 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1204 | NULL, NULL); |
| 1205 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1206 | return posix_1str(args, "et:chdir", chdir, NULL, NULL); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1207 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1210 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1211 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1212 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1213 | 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] | 1214 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1215 | |
| 1216 | static PyObject * |
| 1217 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1218 | { |
| 1219 | return posix_fildes(fdobj, fchdir); |
| 1220 | } |
| 1221 | #endif /* HAVE_FCHDIR */ |
| 1222 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1223 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1224 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1225 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1226 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1227 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1228 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1229 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1230 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1231 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1232 | int i; |
| 1233 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1234 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1235 | if (unicode_file_names()) { |
| 1236 | PyUnicodeObject *po; |
| 1237 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1238 | Py_BEGIN_ALLOW_THREADS |
| 1239 | res = _wchmod(PyUnicode_AS_UNICODE(po), i); |
| 1240 | Py_END_ALLOW_THREADS |
| 1241 | if (res < 0) |
| 1242 | return posix_error_with_unicode_filename( |
| 1243 | PyUnicode_AS_UNICODE(po)); |
| 1244 | Py_INCREF(Py_None); |
| 1245 | return Py_None; |
| 1246 | } |
| 1247 | /* Drop the argument parsing error as narrow strings |
| 1248 | are also valid. */ |
| 1249 | PyErr_Clear(); |
| 1250 | } |
| 1251 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1252 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1253 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1254 | return NULL; |
| 1255 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1256 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1257 | Py_END_ALLOW_THREADS |
| 1258 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1259 | return posix_error_with_allocated_filename(path); |
| 1260 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1261 | Py_INCREF(Py_None); |
| 1262 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1265 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1266 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1267 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1268 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1269 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1270 | |
| 1271 | static PyObject * |
| 1272 | posix_chroot(PyObject *self, PyObject *args) |
| 1273 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1274 | return posix_1str(args, "et:chroot", chroot, NULL, NULL); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1275 | } |
| 1276 | #endif |
| 1277 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1278 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1279 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1280 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1281 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1282 | |
| 1283 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1284 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1285 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1286 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1287 | } |
| 1288 | #endif /* HAVE_FSYNC */ |
| 1289 | |
| 1290 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1291 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1292 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1293 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1294 | #endif |
| 1295 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1296 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1297 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1298 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1299 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1300 | |
| 1301 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1302 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1303 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1304 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1305 | } |
| 1306 | #endif /* HAVE_FDATASYNC */ |
| 1307 | |
| 1308 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1309 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1310 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1311 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1312 | 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] | 1313 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1314 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1315 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1316 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1317 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1318 | int uid, gid; |
| 1319 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1320 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1321 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1322 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1323 | return NULL; |
| 1324 | Py_BEGIN_ALLOW_THREADS |
| 1325 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1326 | Py_END_ALLOW_THREADS |
| 1327 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1328 | return posix_error_with_allocated_filename(path); |
| 1329 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1330 | Py_INCREF(Py_None); |
| 1331 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1332 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1333 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1334 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1335 | #ifdef HAVE_LCHOWN |
| 1336 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1337 | "lchown(path, uid, gid)\n\n\ |
| 1338 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1339 | This function will not follow symbolic links."); |
| 1340 | |
| 1341 | static PyObject * |
| 1342 | posix_lchown(PyObject *self, PyObject *args) |
| 1343 | { |
| 1344 | char *path = NULL; |
| 1345 | int uid, gid; |
| 1346 | int res; |
| 1347 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1348 | Py_FileSystemDefaultEncoding, &path, |
| 1349 | &uid, &gid)) |
| 1350 | return NULL; |
| 1351 | Py_BEGIN_ALLOW_THREADS |
| 1352 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1353 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1354 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1355 | return posix_error_with_allocated_filename(path); |
| 1356 | PyMem_Free(path); |
| 1357 | Py_INCREF(Py_None); |
| 1358 | return Py_None; |
| 1359 | } |
| 1360 | #endif /* HAVE_LCHOWN */ |
| 1361 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1362 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1363 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1364 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1365 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1366 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1367 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1368 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1369 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1370 | { |
| 1371 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1372 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1373 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1374 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1375 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1376 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1377 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1378 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1379 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1380 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1381 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1382 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1383 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1384 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1385 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1386 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1387 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1388 | "getcwdu() -> path\n\n\ |
| 1389 | Return a unicode string representing the current working directory."); |
| 1390 | |
| 1391 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1392 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1393 | { |
| 1394 | char buf[1026]; |
| 1395 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1396 | |
| 1397 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1398 | if (unicode_file_names()) { |
| 1399 | wchar_t *wres; |
| 1400 | wchar_t wbuf[1026]; |
| 1401 | Py_BEGIN_ALLOW_THREADS |
| 1402 | wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]); |
| 1403 | Py_END_ALLOW_THREADS |
| 1404 | if (wres == NULL) |
| 1405 | return posix_error(); |
| 1406 | return PyUnicode_FromWideChar(wbuf, wcslen(wbuf)); |
| 1407 | } |
| 1408 | #endif |
| 1409 | |
| 1410 | Py_BEGIN_ALLOW_THREADS |
| 1411 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1412 | res = _getcwd2(buf, sizeof buf); |
| 1413 | #else |
| 1414 | res = getcwd(buf, sizeof buf); |
| 1415 | #endif |
| 1416 | Py_END_ALLOW_THREADS |
| 1417 | if (res == NULL) |
| 1418 | return posix_error(); |
| 1419 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1420 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1421 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1422 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1423 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1424 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1425 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1426 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1427 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1428 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1429 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1430 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1431 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1432 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1433 | return posix_2str(args, "etet:link", link, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1434 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1435 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1436 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1437 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1438 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1439 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1440 | Return a list containing the names of the entries in the directory.\n\ |
| 1441 | \n\ |
| 1442 | path: path of directory to list\n\ |
| 1443 | \n\ |
| 1444 | 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] | 1445 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1446 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1447 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1448 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1449 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1450 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1451 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1452 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1453 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1454 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1455 | HANDLE hFindFile; |
| 1456 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1457 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 1458 | char namebuf[MAX_PATH*2+5]; |
| 1459 | char *bufptr = namebuf; |
| 1460 | int len = sizeof(namebuf)/sizeof(namebuf[0]); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1461 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1462 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1463 | /* If on wide-character-capable OS see if argument |
| 1464 | is Unicode and if so use wide API. */ |
| 1465 | if (unicode_file_names()) { |
| 1466 | PyUnicodeObject *po; |
| 1467 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1468 | WIN32_FIND_DATAW wFileData; |
| 1469 | Py_UNICODE wnamebuf[MAX_PATH*2+5]; |
| 1470 | Py_UNICODE wch; |
| 1471 | wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH); |
| 1472 | wnamebuf[MAX_PATH] = L'\0'; |
| 1473 | len = wcslen(wnamebuf); |
| 1474 | wch = (len > 0) ? wnamebuf[len-1] : L'\0'; |
| 1475 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1476 | wnamebuf[len++] = L'/'; |
| 1477 | wcscpy(wnamebuf + len, L"*.*"); |
| 1478 | if ((d = PyList_New(0)) == NULL) |
| 1479 | return NULL; |
| 1480 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1481 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1482 | errno = GetLastError(); |
| 1483 | if (errno == ERROR_FILE_NOT_FOUND) { |
| 1484 | return d; |
| 1485 | } |
| 1486 | Py_DECREF(d); |
| 1487 | return win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1488 | } |
| 1489 | do { |
| 1490 | if (wFileData.cFileName[0] == L'.' && |
| 1491 | (wFileData.cFileName[1] == L'\0' || |
| 1492 | wFileData.cFileName[1] == L'.' && |
| 1493 | wFileData.cFileName[2] == L'\0')) |
| 1494 | continue; |
| 1495 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1496 | if (v == NULL) { |
| 1497 | Py_DECREF(d); |
| 1498 | d = NULL; |
| 1499 | break; |
| 1500 | } |
| 1501 | if (PyList_Append(d, v) != 0) { |
| 1502 | Py_DECREF(v); |
| 1503 | Py_DECREF(d); |
| 1504 | d = NULL; |
| 1505 | break; |
| 1506 | } |
| 1507 | Py_DECREF(v); |
| 1508 | } while (FindNextFileW(hFindFile, &wFileData) == TRUE); |
| 1509 | |
| 1510 | if (FindClose(hFindFile) == FALSE) { |
| 1511 | Py_DECREF(d); |
| 1512 | return win32_error_unicode("FindClose", wnamebuf); |
| 1513 | } |
| 1514 | return d; |
| 1515 | } |
| 1516 | /* Drop the argument parsing error as narrow strings |
| 1517 | are also valid. */ |
| 1518 | PyErr_Clear(); |
| 1519 | } |
| 1520 | #endif |
| 1521 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1522 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1523 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1524 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1525 | if (len > 0) { |
| 1526 | char ch = namebuf[len-1]; |
| 1527 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 1528 | namebuf[len++] = '/'; |
| 1529 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1530 | strcpy(namebuf + len, "*.*"); |
| 1531 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1532 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1533 | return NULL; |
| 1534 | |
| 1535 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 1536 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1537 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 1538 | if (errno == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1539 | return d; |
| 1540 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1541 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1542 | } |
| 1543 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1544 | if (FileData.cFileName[0] == '.' && |
| 1545 | (FileData.cFileName[1] == '\0' || |
| 1546 | FileData.cFileName[1] == '.' && |
| 1547 | FileData.cFileName[2] == '\0')) |
| 1548 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1549 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1550 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1551 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1552 | d = NULL; |
| 1553 | break; |
| 1554 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1555 | if (PyList_Append(d, v) != 0) { |
| 1556 | Py_DECREF(v); |
| 1557 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1558 | d = NULL; |
| 1559 | break; |
| 1560 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1561 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1562 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 1563 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1564 | if (FindClose(hFindFile) == FALSE) { |
| 1565 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1566 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1567 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1568 | |
| 1569 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1570 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1571 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1572 | |
| 1573 | #ifndef MAX_PATH |
| 1574 | #define MAX_PATH CCHMAXPATH |
| 1575 | #endif |
| 1576 | char *name, *pt; |
| 1577 | int len; |
| 1578 | PyObject *d, *v; |
| 1579 | char namebuf[MAX_PATH+5]; |
| 1580 | HDIR hdir = 1; |
| 1581 | ULONG srchcnt = 1; |
| 1582 | FILEFINDBUF3 ep; |
| 1583 | APIRET rc; |
| 1584 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1585 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1586 | return NULL; |
| 1587 | if (len >= MAX_PATH) { |
| 1588 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1589 | return NULL; |
| 1590 | } |
| 1591 | strcpy(namebuf, name); |
| 1592 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1593 | if (*pt == ALTSEP) |
| 1594 | *pt = SEP; |
| 1595 | if (namebuf[len-1] != SEP) |
| 1596 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1597 | strcpy(namebuf + len, "*.*"); |
| 1598 | |
| 1599 | if ((d = PyList_New(0)) == NULL) |
| 1600 | return NULL; |
| 1601 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1602 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1603 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1604 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1605 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1606 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1607 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1608 | |
| 1609 | if (rc != NO_ERROR) { |
| 1610 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1611 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1614 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1615 | do { |
| 1616 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1617 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1618 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1619 | |
| 1620 | strcpy(namebuf, ep.achName); |
| 1621 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1622 | /* Leave Case of Name Alone -- In Native Form */ |
| 1623 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1624 | |
| 1625 | v = PyString_FromString(namebuf); |
| 1626 | if (v == NULL) { |
| 1627 | Py_DECREF(d); |
| 1628 | d = NULL; |
| 1629 | break; |
| 1630 | } |
| 1631 | if (PyList_Append(d, v) != 0) { |
| 1632 | Py_DECREF(v); |
| 1633 | Py_DECREF(d); |
| 1634 | d = NULL; |
| 1635 | break; |
| 1636 | } |
| 1637 | Py_DECREF(v); |
| 1638 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1639 | } |
| 1640 | |
| 1641 | return d; |
| 1642 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1643 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1644 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1645 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1646 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1647 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1648 | int arg_is_unicode = 1; |
| 1649 | |
| 1650 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 1651 | arg_is_unicode = 0; |
| 1652 | PyErr_Clear(); |
| 1653 | } |
| 1654 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1655 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1656 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1657 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1658 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1659 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1660 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1661 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1662 | return NULL; |
| 1663 | } |
| 1664 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1665 | if (ep->d_name[0] == '.' && |
| 1666 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1667 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1668 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1669 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1670 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1671 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1672 | d = NULL; |
| 1673 | break; |
| 1674 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1675 | #ifdef Py_USING_UNICODE |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1676 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1677 | PyObject *w; |
| 1678 | |
| 1679 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1680 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1681 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 1682 | if (w != NULL) { |
| 1683 | Py_DECREF(v); |
| 1684 | v = w; |
| 1685 | } |
| 1686 | else { |
| 1687 | /* fall back to the original byte string, as |
| 1688 | discussed in patch #683592 */ |
| 1689 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1690 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1691 | } |
| 1692 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1693 | if (PyList_Append(d, v) != 0) { |
| 1694 | Py_DECREF(v); |
| 1695 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1696 | d = NULL; |
| 1697 | break; |
| 1698 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1699 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1700 | } |
| 1701 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1702 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1703 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1704 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1705 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1706 | #endif /* which OS */ |
| 1707 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1708 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1709 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1710 | /* A helper function for abspath on win32 */ |
| 1711 | static PyObject * |
| 1712 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1713 | { |
| 1714 | /* assume encoded strings wont more than double no of chars */ |
| 1715 | char inbuf[MAX_PATH*2]; |
| 1716 | char *inbufp = inbuf; |
| 1717 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); |
| 1718 | char outbuf[MAX_PATH*2]; |
| 1719 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1720 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1721 | if (unicode_file_names()) { |
| 1722 | PyUnicodeObject *po; |
| 1723 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 1724 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 1725 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1726 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1727 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 1728 | woutbuf, &wtemp)) |
| 1729 | return win32_error("GetFullPathName", ""); |
| 1730 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 1731 | } |
| 1732 | /* Drop the argument parsing error as narrow strings |
| 1733 | are also valid. */ |
| 1734 | PyErr_Clear(); |
| 1735 | } |
| 1736 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1737 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1738 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1739 | &insize)) |
| 1740 | return NULL; |
| 1741 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1742 | outbuf, &temp)) |
| 1743 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 1744 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 1745 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 1746 | Py_FileSystemDefaultEncoding, NULL); |
| 1747 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1748 | return PyString_FromString(outbuf); |
| 1749 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1750 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1751 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1752 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1753 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1754 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1755 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1756 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1757 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1758 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1759 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1760 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1761 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1762 | |
| 1763 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1764 | if (unicode_file_names()) { |
| 1765 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 1766 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1767 | Py_BEGIN_ALLOW_THREADS |
| 1768 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1769 | it is a simple dereference. */ |
| 1770 | res = _wmkdir(PyUnicode_AS_UNICODE(po)); |
| 1771 | Py_END_ALLOW_THREADS |
| 1772 | if (res < 0) |
| 1773 | return posix_error(); |
| 1774 | Py_INCREF(Py_None); |
| 1775 | return Py_None; |
| 1776 | } |
| 1777 | /* Drop the argument parsing error as narrow strings |
| 1778 | are also valid. */ |
| 1779 | PyErr_Clear(); |
| 1780 | } |
| 1781 | #endif |
| 1782 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1783 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1784 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1785 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1786 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1787 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1788 | res = mkdir(path); |
| 1789 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1790 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1791 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1792 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1793 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1794 | return posix_error_with_allocated_filename(path); |
| 1795 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1796 | Py_INCREF(Py_None); |
| 1797 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1800 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1801 | #ifdef HAVE_NICE |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1802 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) |
| 1803 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) |
| 1804 | #include <sys/resource.h> |
| 1805 | #endif |
| 1806 | #endif |
| 1807 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1808 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1809 | "nice(inc) -> new_priority\n\n\ |
| 1810 | 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] | 1811 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1812 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1813 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1814 | { |
| 1815 | int increment, value; |
| 1816 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1817 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1818 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1819 | |
| 1820 | /* There are two flavours of 'nice': one that returns the new |
| 1821 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1822 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 1823 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1824 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1825 | If we are of the nice family that returns the new priority, we |
| 1826 | need to clear errno before the call, and check if errno is filled |
| 1827 | before calling posix_error() on a returnvalue of -1, because the |
| 1828 | -1 may be the actual new priority! */ |
| 1829 | |
| 1830 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1831 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1832 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1833 | if (value == 0) |
| 1834 | value = getpriority(PRIO_PROCESS, 0); |
| 1835 | #endif |
| 1836 | if (value == -1 && errno != 0) |
| 1837 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1838 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1839 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1840 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1841 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1842 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1843 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1844 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1845 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1846 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1847 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1848 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1849 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1850 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1851 | #ifdef MS_WINDOWS |
| 1852 | return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename); |
| 1853 | #else |
| 1854 | return posix_2str(args, "etet:rename", rename, NULL, NULL); |
| 1855 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1858 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1859 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1860 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1861 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1862 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1863 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1864 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1865 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1866 | #ifdef MS_WINDOWS |
| 1867 | return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir); |
| 1868 | #else |
| 1869 | return posix_1str(args, "et:rmdir", rmdir, NULL, NULL); |
| 1870 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1873 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1874 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1875 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1876 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1877 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1878 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1879 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1880 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1881 | #ifdef MS_WINDOWS |
| 1882 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64); |
| 1883 | #else |
| 1884 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 1885 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1888 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1889 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1890 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1891 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1892 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1893 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1894 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1895 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1896 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1897 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1898 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1899 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1900 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1901 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1902 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1903 | Py_END_ALLOW_THREADS |
| 1904 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1905 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1906 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1907 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1908 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1909 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1910 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1911 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1912 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1913 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1914 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1915 | { |
| 1916 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1917 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1918 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 1919 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1920 | if (i < 0) |
| 1921 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1922 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1925 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1926 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1927 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1928 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1929 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1930 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1931 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1932 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1933 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1934 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1935 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1936 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1937 | #ifdef MS_WINDOWS |
| 1938 | return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink); |
| 1939 | #else |
| 1940 | return posix_1str(args, "et:remove", unlink, NULL, NULL); |
| 1941 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1944 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1945 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1946 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1947 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1948 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1949 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1950 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1951 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1952 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1953 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1954 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1955 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1956 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1957 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1958 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1959 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1960 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1961 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1962 | u.sysname, |
| 1963 | u.nodename, |
| 1964 | u.release, |
| 1965 | u.version, |
| 1966 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1967 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1968 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1969 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1970 | static int |
| 1971 | extract_time(PyObject *t, long* sec, long* usec) |
| 1972 | { |
| 1973 | long intval; |
| 1974 | if (PyFloat_Check(t)) { |
| 1975 | double tval = PyFloat_AsDouble(t); |
| 1976 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 1977 | if (!intobj) |
| 1978 | return -1; |
| 1979 | intval = PyInt_AsLong(intobj); |
| 1980 | Py_DECREF(intobj); |
| 1981 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 1982 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1983 | if (*usec < 0) |
| 1984 | /* If rounding gave us a negative number, |
| 1985 | truncate. */ |
| 1986 | *usec = 0; |
| 1987 | return 0; |
| 1988 | } |
| 1989 | intval = PyInt_AsLong(t); |
| 1990 | if (intval == -1 && PyErr_Occurred()) |
| 1991 | return -1; |
| 1992 | *sec = intval; |
| 1993 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 1994 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1995 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1996 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1997 | PyDoc_STRVAR(posix_utime__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1998 | "utime(path, (atime, utime))\n\ |
| 1999 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2000 | 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] | 2001 | 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] | 2002 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2003 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2004 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2005 | { |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2006 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2007 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2008 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2009 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2010 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2011 | #if defined(HAVE_UTIMES) |
| 2012 | struct timeval buf[2]; |
| 2013 | #define ATIME buf[0].tv_sec |
| 2014 | #define MTIME buf[1].tv_sec |
| 2015 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2016 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2017 | struct utimbuf buf; |
| 2018 | #define ATIME buf.actime |
| 2019 | #define MTIME buf.modtime |
| 2020 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2021 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2022 | time_t buf[2]; |
| 2023 | #define ATIME buf[0] |
| 2024 | #define MTIME buf[1] |
| 2025 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2026 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2027 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2028 | int have_unicode_filename = 0; |
| 2029 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2030 | PyUnicodeObject *obwpath; |
| 2031 | wchar_t *wpath; |
| 2032 | if (unicode_file_names()) { |
| 2033 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2034 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2035 | have_unicode_filename = 1; |
| 2036 | } else |
| 2037 | /* Drop the argument parsing error as narrow strings |
| 2038 | are also valid. */ |
| 2039 | PyErr_Clear(); |
| 2040 | } |
| 2041 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 2042 | |
| 2043 | if (!have_unicode_filename && \ |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2044 | !PyArg_ParseTuple(args, "etO:utime", |
| 2045 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2046 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2047 | if (arg == Py_None) { |
| 2048 | /* optional time values not given */ |
| 2049 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2050 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2051 | if (have_unicode_filename) |
| 2052 | res = _wutime(wpath, NULL); |
| 2053 | else |
| 2054 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2055 | res = utime(path, NULL); |
| 2056 | Py_END_ALLOW_THREADS |
| 2057 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2058 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2059 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2060 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2061 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2062 | return NULL; |
| 2063 | } |
| 2064 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2065 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2066 | &atime, &ausec) == -1) { |
| 2067 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2068 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2069 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2070 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2071 | &mtime, &musec) == -1) { |
| 2072 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2073 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2074 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2075 | ATIME = atime; |
| 2076 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2077 | #ifdef HAVE_UTIMES |
| 2078 | buf[0].tv_usec = ausec; |
| 2079 | buf[1].tv_usec = musec; |
| 2080 | Py_BEGIN_ALLOW_THREADS |
| 2081 | res = utimes(path, buf); |
| 2082 | Py_END_ALLOW_THREADS |
| 2083 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2084 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2085 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2086 | if (have_unicode_filename) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 2087 | /* utime is OK with utimbuf, but _wutime insists |
| 2088 | on _utimbuf (the msvc headers assert the |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2089 | underscore version is ansi) */ |
| 2090 | res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG); |
| 2091 | else |
| 2092 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2093 | res = utime(path, UTIME_ARG); |
| 2094 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2095 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2096 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2097 | if (res < 0) { |
| 2098 | #ifdef Py_WIN_WIDE_FILENAMES |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2099 | if (have_unicode_filename) |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2100 | return posix_error_with_unicode_filename(wpath); |
| 2101 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2102 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2103 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2104 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2105 | Py_INCREF(Py_None); |
| 2106 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2107 | #undef UTIME_ARG |
| 2108 | #undef ATIME |
| 2109 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2112 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2113 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2114 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2115 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2116 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2117 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2118 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2119 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2120 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2121 | { |
| 2122 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2123 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2124 | return NULL; |
| 2125 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2126 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2129 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2130 | static void |
| 2131 | free_string_array(char **array, int count) |
| 2132 | { |
| 2133 | int i; |
| 2134 | for (i = 0; i < count; i++) |
| 2135 | PyMem_Free(array[i]); |
| 2136 | PyMem_DEL(array); |
| 2137 | } |
| 2138 | #endif |
| 2139 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2140 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2141 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2142 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2143 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2144 | Execute an executable path with arguments, replacing current process.\n\ |
| 2145 | \n\ |
| 2146 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2147 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2148 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2149 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2150 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2151 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2152 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2153 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2154 | char **argvlist; |
| 2155 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2156 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2157 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2158 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2159 | argv is a list or tuple of strings. */ |
| 2160 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2161 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2162 | Py_FileSystemDefaultEncoding, |
| 2163 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2164 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2165 | if (PyList_Check(argv)) { |
| 2166 | argc = PyList_Size(argv); |
| 2167 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2168 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2169 | else if (PyTuple_Check(argv)) { |
| 2170 | argc = PyTuple_Size(argv); |
| 2171 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2172 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2173 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2174 | 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] | 2175 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2176 | return NULL; |
| 2177 | } |
| 2178 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2179 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2180 | if (argvlist == NULL) { |
| 2181 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2182 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2183 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2184 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2185 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2186 | Py_FileSystemDefaultEncoding, |
| 2187 | &argvlist[i])) { |
| 2188 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2189 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2190 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2191 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2192 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2193 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2194 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2195 | } |
| 2196 | argvlist[argc] = NULL; |
| 2197 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2198 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2199 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2200 | /* If we get here it's definitely an error */ |
| 2201 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2202 | free_string_array(argvlist, argc); |
| 2203 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2204 | return posix_error(); |
| 2205 | } |
| 2206 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2207 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2208 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2209 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2210 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2211 | \n\ |
| 2212 | path: path of executable file\n\ |
| 2213 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2214 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2215 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2216 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2217 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2218 | { |
| 2219 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2220 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2221 | char **argvlist; |
| 2222 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2223 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2224 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2225 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2226 | int lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2227 | |
| 2228 | /* execve has three arguments: (path, argv, env), where |
| 2229 | argv is a list or tuple of strings and env is a dictionary |
| 2230 | like posix.environ. */ |
| 2231 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2232 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2233 | Py_FileSystemDefaultEncoding, |
| 2234 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2235 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2236 | if (PyList_Check(argv)) { |
| 2237 | argc = PyList_Size(argv); |
| 2238 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2239 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2240 | else if (PyTuple_Check(argv)) { |
| 2241 | argc = PyTuple_Size(argv); |
| 2242 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2243 | } |
| 2244 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2245 | PyErr_SetString(PyExc_TypeError, |
| 2246 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2247 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2248 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2249 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2250 | PyErr_SetString(PyExc_TypeError, |
| 2251 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2252 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2255 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2256 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2257 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2258 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2259 | } |
| 2260 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2261 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2262 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2263 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2264 | &argvlist[i])) |
| 2265 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2266 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2267 | goto fail_1; |
| 2268 | } |
| 2269 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2270 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2271 | argvlist[argc] = NULL; |
| 2272 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2273 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2274 | if (i < 0) |
| 2275 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2276 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2277 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2278 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2279 | goto fail_1; |
| 2280 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2281 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2282 | keys = PyMapping_Keys(env); |
| 2283 | vals = PyMapping_Values(env); |
| 2284 | if (!keys || !vals) |
| 2285 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2286 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2287 | PyErr_SetString(PyExc_TypeError, |
| 2288 | "execve(): env.keys() or env.values() is not a list"); |
| 2289 | goto fail_2; |
| 2290 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2291 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2292 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2293 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2294 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2295 | |
| 2296 | key = PyList_GetItem(keys, pos); |
| 2297 | val = PyList_GetItem(vals, pos); |
| 2298 | if (!key || !val) |
| 2299 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2300 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2301 | if (!PyArg_Parse( |
| 2302 | key, |
| 2303 | "s;execve() arg 3 contains a non-string key", |
| 2304 | &k) || |
| 2305 | !PyArg_Parse( |
| 2306 | val, |
| 2307 | "s;execve() arg 3 contains a non-string value", |
| 2308 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2309 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2310 | goto fail_2; |
| 2311 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2312 | |
| 2313 | #if defined(PYOS_OS2) |
| 2314 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2315 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2316 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2317 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2318 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2319 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2320 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2321 | goto fail_2; |
| 2322 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2323 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2324 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2325 | #if defined(PYOS_OS2) |
| 2326 | } |
| 2327 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2328 | } |
| 2329 | envlist[envc] = 0; |
| 2330 | |
| 2331 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2332 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2333 | /* If we get here it's definitely an error */ |
| 2334 | |
| 2335 | (void) posix_error(); |
| 2336 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2337 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2338 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2339 | PyMem_DEL(envlist[envc]); |
| 2340 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2341 | fail_1: |
| 2342 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2343 | Py_XDECREF(vals); |
| 2344 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2345 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2346 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2347 | return NULL; |
| 2348 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2349 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2350 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2351 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2352 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2353 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2354 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2355 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2356 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2357 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2358 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2359 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2360 | |
| 2361 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2362 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2363 | { |
| 2364 | char *path; |
| 2365 | PyObject *argv; |
| 2366 | char **argvlist; |
| 2367 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2368 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2369 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2370 | |
| 2371 | /* spawnv has three arguments: (mode, path, argv), where |
| 2372 | argv is a list or tuple of strings. */ |
| 2373 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2374 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2375 | Py_FileSystemDefaultEncoding, |
| 2376 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2377 | return NULL; |
| 2378 | if (PyList_Check(argv)) { |
| 2379 | argc = PyList_Size(argv); |
| 2380 | getitem = PyList_GetItem; |
| 2381 | } |
| 2382 | else if (PyTuple_Check(argv)) { |
| 2383 | argc = PyTuple_Size(argv); |
| 2384 | getitem = PyTuple_GetItem; |
| 2385 | } |
| 2386 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2387 | PyErr_SetString(PyExc_TypeError, |
| 2388 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2389 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2390 | return NULL; |
| 2391 | } |
| 2392 | |
| 2393 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2394 | if (argvlist == NULL) { |
| 2395 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2396 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2397 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2398 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2399 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2400 | Py_FileSystemDefaultEncoding, |
| 2401 | &argvlist[i])) { |
| 2402 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2403 | PyErr_SetString( |
| 2404 | PyExc_TypeError, |
| 2405 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2406 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2407 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2408 | } |
| 2409 | } |
| 2410 | argvlist[argc] = NULL; |
| 2411 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2412 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2413 | Py_BEGIN_ALLOW_THREADS |
| 2414 | spawnval = spawnv(mode, path, argvlist); |
| 2415 | Py_END_ALLOW_THREADS |
| 2416 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2417 | if (mode == _OLD_P_OVERLAY) |
| 2418 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2419 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2420 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2421 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2422 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2423 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2424 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2425 | free_string_array(argvlist, argc); |
| 2426 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2427 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2428 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2429 | return posix_error(); |
| 2430 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2431 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2432 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2433 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2434 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2435 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2439 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2440 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2441 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2442 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2443 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2444 | path: path of executable file\n\ |
| 2445 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2446 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2447 | |
| 2448 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2449 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2450 | { |
| 2451 | char *path; |
| 2452 | PyObject *argv, *env; |
| 2453 | char **argvlist; |
| 2454 | char **envlist; |
| 2455 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2456 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2457 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2458 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2459 | int lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2460 | |
| 2461 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 2462 | argv is a list or tuple of strings and env is a dictionary |
| 2463 | like posix.environ. */ |
| 2464 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2465 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 2466 | Py_FileSystemDefaultEncoding, |
| 2467 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2468 | return NULL; |
| 2469 | if (PyList_Check(argv)) { |
| 2470 | argc = PyList_Size(argv); |
| 2471 | getitem = PyList_GetItem; |
| 2472 | } |
| 2473 | else if (PyTuple_Check(argv)) { |
| 2474 | argc = PyTuple_Size(argv); |
| 2475 | getitem = PyTuple_GetItem; |
| 2476 | } |
| 2477 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2478 | PyErr_SetString(PyExc_TypeError, |
| 2479 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2480 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2481 | } |
| 2482 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2483 | PyErr_SetString(PyExc_TypeError, |
| 2484 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2485 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | argvlist = PyMem_NEW(char *, argc+1); |
| 2489 | if (argvlist == NULL) { |
| 2490 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2491 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2492 | } |
| 2493 | for (i = 0; i < argc; i++) { |
| 2494 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2495 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2496 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2497 | &argvlist[i])) |
| 2498 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2499 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2500 | goto fail_1; |
| 2501 | } |
| 2502 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2503 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2504 | argvlist[argc] = NULL; |
| 2505 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2506 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2507 | if (i < 0) |
| 2508 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2509 | envlist = PyMem_NEW(char *, i + 1); |
| 2510 | if (envlist == NULL) { |
| 2511 | PyErr_NoMemory(); |
| 2512 | goto fail_1; |
| 2513 | } |
| 2514 | envc = 0; |
| 2515 | keys = PyMapping_Keys(env); |
| 2516 | vals = PyMapping_Values(env); |
| 2517 | if (!keys || !vals) |
| 2518 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2519 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2520 | PyErr_SetString(PyExc_TypeError, |
| 2521 | "spawnve(): env.keys() or env.values() is not a list"); |
| 2522 | goto fail_2; |
| 2523 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2524 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2525 | for (pos = 0; pos < i; pos++) { |
| 2526 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2527 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2528 | |
| 2529 | key = PyList_GetItem(keys, pos); |
| 2530 | val = PyList_GetItem(vals, pos); |
| 2531 | if (!key || !val) |
| 2532 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2533 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2534 | if (!PyArg_Parse( |
| 2535 | key, |
| 2536 | "s;spawnve() arg 3 contains a non-string key", |
| 2537 | &k) || |
| 2538 | !PyArg_Parse( |
| 2539 | val, |
| 2540 | "s;spawnve() arg 3 contains a non-string value", |
| 2541 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2542 | { |
| 2543 | goto fail_2; |
| 2544 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2545 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2546 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2547 | if (p == NULL) { |
| 2548 | PyErr_NoMemory(); |
| 2549 | goto fail_2; |
| 2550 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2551 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2552 | envlist[envc++] = p; |
| 2553 | } |
| 2554 | envlist[envc] = 0; |
| 2555 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2556 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2557 | Py_BEGIN_ALLOW_THREADS |
| 2558 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2559 | Py_END_ALLOW_THREADS |
| 2560 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2561 | if (mode == _OLD_P_OVERLAY) |
| 2562 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2563 | |
| 2564 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2565 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2566 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2567 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2568 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2569 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2570 | (void) posix_error(); |
| 2571 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2572 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2573 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2574 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2575 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2576 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2577 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2578 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2579 | while (--envc >= 0) |
| 2580 | PyMem_DEL(envlist[envc]); |
| 2581 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2582 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2583 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2584 | Py_XDECREF(vals); |
| 2585 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2586 | fail_0: |
| 2587 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2588 | return res; |
| 2589 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2590 | |
| 2591 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 2592 | #if defined(PYOS_OS2) |
| 2593 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 2594 | "spawnvp(mode, file, args)\n\n\ |
| 2595 | Execute the program 'file' in a new process, using the environment\n\ |
| 2596 | search path to find the file.\n\ |
| 2597 | \n\ |
| 2598 | mode: mode of process creation\n\ |
| 2599 | file: executable file name\n\ |
| 2600 | args: tuple or list of strings"); |
| 2601 | |
| 2602 | static PyObject * |
| 2603 | posix_spawnvp(PyObject *self, PyObject *args) |
| 2604 | { |
| 2605 | char *path; |
| 2606 | PyObject *argv; |
| 2607 | char **argvlist; |
| 2608 | int mode, i, argc; |
| 2609 | Py_intptr_t spawnval; |
| 2610 | PyObject *(*getitem)(PyObject *, int); |
| 2611 | |
| 2612 | /* spawnvp has three arguments: (mode, path, argv), where |
| 2613 | argv is a list or tuple of strings. */ |
| 2614 | |
| 2615 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 2616 | Py_FileSystemDefaultEncoding, |
| 2617 | &path, &argv)) |
| 2618 | return NULL; |
| 2619 | if (PyList_Check(argv)) { |
| 2620 | argc = PyList_Size(argv); |
| 2621 | getitem = PyList_GetItem; |
| 2622 | } |
| 2623 | else if (PyTuple_Check(argv)) { |
| 2624 | argc = PyTuple_Size(argv); |
| 2625 | getitem = PyTuple_GetItem; |
| 2626 | } |
| 2627 | else { |
| 2628 | PyErr_SetString(PyExc_TypeError, |
| 2629 | "spawnvp() arg 2 must be a tuple or list"); |
| 2630 | PyMem_Free(path); |
| 2631 | return NULL; |
| 2632 | } |
| 2633 | |
| 2634 | argvlist = PyMem_NEW(char *, argc+1); |
| 2635 | if (argvlist == NULL) { |
| 2636 | PyMem_Free(path); |
| 2637 | return PyErr_NoMemory(); |
| 2638 | } |
| 2639 | for (i = 0; i < argc; i++) { |
| 2640 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2641 | Py_FileSystemDefaultEncoding, |
| 2642 | &argvlist[i])) { |
| 2643 | free_string_array(argvlist, i); |
| 2644 | PyErr_SetString( |
| 2645 | PyExc_TypeError, |
| 2646 | "spawnvp() arg 2 must contain only strings"); |
| 2647 | PyMem_Free(path); |
| 2648 | return NULL; |
| 2649 | } |
| 2650 | } |
| 2651 | argvlist[argc] = NULL; |
| 2652 | |
| 2653 | Py_BEGIN_ALLOW_THREADS |
| 2654 | #if defined(PYCC_GCC) |
| 2655 | spawnval = spawnvp(mode, path, argvlist); |
| 2656 | #else |
| 2657 | spawnval = _spawnvp(mode, path, argvlist); |
| 2658 | #endif |
| 2659 | Py_END_ALLOW_THREADS |
| 2660 | |
| 2661 | free_string_array(argvlist, argc); |
| 2662 | PyMem_Free(path); |
| 2663 | |
| 2664 | if (spawnval == -1) |
| 2665 | return posix_error(); |
| 2666 | else |
| 2667 | return Py_BuildValue("l", (long) spawnval); |
| 2668 | } |
| 2669 | |
| 2670 | |
| 2671 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 2672 | "spawnvpe(mode, file, args, env)\n\n\ |
| 2673 | Execute the program 'file' in a new process, using the environment\n\ |
| 2674 | search path to find the file.\n\ |
| 2675 | \n\ |
| 2676 | mode: mode of process creation\n\ |
| 2677 | file: executable file name\n\ |
| 2678 | args: tuple or list of arguments\n\ |
| 2679 | env: dictionary of strings mapping to strings"); |
| 2680 | |
| 2681 | static PyObject * |
| 2682 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 2683 | { |
| 2684 | char *path; |
| 2685 | PyObject *argv, *env; |
| 2686 | char **argvlist; |
| 2687 | char **envlist; |
| 2688 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2689 | int mode, i, pos, argc, envc; |
| 2690 | Py_intptr_t spawnval; |
| 2691 | PyObject *(*getitem)(PyObject *, int); |
| 2692 | int lastarg = 0; |
| 2693 | |
| 2694 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 2695 | argv is a list or tuple of strings and env is a dictionary |
| 2696 | like posix.environ. */ |
| 2697 | |
| 2698 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 2699 | Py_FileSystemDefaultEncoding, |
| 2700 | &path, &argv, &env)) |
| 2701 | return NULL; |
| 2702 | if (PyList_Check(argv)) { |
| 2703 | argc = PyList_Size(argv); |
| 2704 | getitem = PyList_GetItem; |
| 2705 | } |
| 2706 | else if (PyTuple_Check(argv)) { |
| 2707 | argc = PyTuple_Size(argv); |
| 2708 | getitem = PyTuple_GetItem; |
| 2709 | } |
| 2710 | else { |
| 2711 | PyErr_SetString(PyExc_TypeError, |
| 2712 | "spawnvpe() arg 2 must be a tuple or list"); |
| 2713 | goto fail_0; |
| 2714 | } |
| 2715 | if (!PyMapping_Check(env)) { |
| 2716 | PyErr_SetString(PyExc_TypeError, |
| 2717 | "spawnvpe() arg 3 must be a mapping object"); |
| 2718 | goto fail_0; |
| 2719 | } |
| 2720 | |
| 2721 | argvlist = PyMem_NEW(char *, argc+1); |
| 2722 | if (argvlist == NULL) { |
| 2723 | PyErr_NoMemory(); |
| 2724 | goto fail_0; |
| 2725 | } |
| 2726 | for (i = 0; i < argc; i++) { |
| 2727 | if (!PyArg_Parse((*getitem)(argv, i), |
| 2728 | "et;spawnvpe() arg 2 must contain only strings", |
| 2729 | Py_FileSystemDefaultEncoding, |
| 2730 | &argvlist[i])) |
| 2731 | { |
| 2732 | lastarg = i; |
| 2733 | goto fail_1; |
| 2734 | } |
| 2735 | } |
| 2736 | lastarg = argc; |
| 2737 | argvlist[argc] = NULL; |
| 2738 | |
| 2739 | i = PyMapping_Size(env); |
| 2740 | if (i < 0) |
| 2741 | goto fail_1; |
| 2742 | envlist = PyMem_NEW(char *, i + 1); |
| 2743 | if (envlist == NULL) { |
| 2744 | PyErr_NoMemory(); |
| 2745 | goto fail_1; |
| 2746 | } |
| 2747 | envc = 0; |
| 2748 | keys = PyMapping_Keys(env); |
| 2749 | vals = PyMapping_Values(env); |
| 2750 | if (!keys || !vals) |
| 2751 | goto fail_2; |
| 2752 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2753 | PyErr_SetString(PyExc_TypeError, |
| 2754 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 2755 | goto fail_2; |
| 2756 | } |
| 2757 | |
| 2758 | for (pos = 0; pos < i; pos++) { |
| 2759 | char *p, *k, *v; |
| 2760 | size_t len; |
| 2761 | |
| 2762 | key = PyList_GetItem(keys, pos); |
| 2763 | val = PyList_GetItem(vals, pos); |
| 2764 | if (!key || !val) |
| 2765 | goto fail_2; |
| 2766 | |
| 2767 | if (!PyArg_Parse( |
| 2768 | key, |
| 2769 | "s;spawnvpe() arg 3 contains a non-string key", |
| 2770 | &k) || |
| 2771 | !PyArg_Parse( |
| 2772 | val, |
| 2773 | "s;spawnvpe() arg 3 contains a non-string value", |
| 2774 | &v)) |
| 2775 | { |
| 2776 | goto fail_2; |
| 2777 | } |
| 2778 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2779 | p = PyMem_NEW(char, len); |
| 2780 | if (p == NULL) { |
| 2781 | PyErr_NoMemory(); |
| 2782 | goto fail_2; |
| 2783 | } |
| 2784 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 2785 | envlist[envc++] = p; |
| 2786 | } |
| 2787 | envlist[envc] = 0; |
| 2788 | |
| 2789 | Py_BEGIN_ALLOW_THREADS |
| 2790 | #if defined(PYCC_GCC) |
| 2791 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2792 | #else |
| 2793 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 2794 | #endif |
| 2795 | Py_END_ALLOW_THREADS |
| 2796 | |
| 2797 | if (spawnval == -1) |
| 2798 | (void) posix_error(); |
| 2799 | else |
| 2800 | res = Py_BuildValue("l", (long) spawnval); |
| 2801 | |
| 2802 | fail_2: |
| 2803 | while (--envc >= 0) |
| 2804 | PyMem_DEL(envlist[envc]); |
| 2805 | PyMem_DEL(envlist); |
| 2806 | fail_1: |
| 2807 | free_string_array(argvlist, lastarg); |
| 2808 | Py_XDECREF(vals); |
| 2809 | Py_XDECREF(keys); |
| 2810 | fail_0: |
| 2811 | PyMem_Free(path); |
| 2812 | return res; |
| 2813 | } |
| 2814 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2815 | #endif /* HAVE_SPAWNV */ |
| 2816 | |
| 2817 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2818 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2819 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2820 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2821 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 2822 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2823 | 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] | 2824 | |
| 2825 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2826 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2827 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2828 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2829 | if (pid == -1) |
| 2830 | return posix_error(); |
| 2831 | PyOS_AfterFork(); |
| 2832 | return PyInt_FromLong((long)pid); |
| 2833 | } |
| 2834 | #endif |
| 2835 | |
| 2836 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2837 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2838 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2839 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2840 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2841 | 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] | 2842 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2843 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2844 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2845 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2846 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2847 | if (pid == -1) |
| 2848 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2849 | if (pid == 0) |
| 2850 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2851 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2852 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2853 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2854 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2855 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 2856 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 2857 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2858 | #define DEV_PTY_FILE "/dev/ptc" |
| 2859 | #define HAVE_DEV_PTMX |
| 2860 | #else |
| 2861 | #define DEV_PTY_FILE "/dev/ptmx" |
| 2862 | #endif |
| 2863 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2864 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2865 | #ifdef HAVE_PTY_H |
| 2866 | #include <pty.h> |
| 2867 | #else |
| 2868 | #ifdef HAVE_LIBUTIL_H |
| 2869 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2870 | #endif /* HAVE_LIBUTIL_H */ |
| 2871 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 2872 | #ifdef HAVE_STROPTS_H |
| 2873 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2874 | #endif |
| 2875 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2876 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2877 | #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] | 2878 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2879 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2880 | 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] | 2881 | |
| 2882 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2883 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2884 | { |
| 2885 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2886 | #ifndef HAVE_OPENPTY |
| 2887 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2888 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2889 | #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] | 2890 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2891 | #ifdef sun |
| 2892 | extern char *ptsname(); |
| 2893 | #endif |
| 2894 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2895 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2896 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2897 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 2898 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2899 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2900 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 2901 | if (slave_name == NULL) |
| 2902 | return posix_error(); |
| 2903 | |
| 2904 | slave_fd = open(slave_name, O_RDWR); |
| 2905 | if (slave_fd < 0) |
| 2906 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2907 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2908 | 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] | 2909 | if (master_fd < 0) |
| 2910 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 2911 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2912 | /* change permission of slave */ |
| 2913 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 2914 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2915 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2916 | } |
| 2917 | /* unlock slave */ |
| 2918 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 2919 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2920 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2921 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 2922 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2923 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 2924 | if (slave_name == NULL) |
| 2925 | return posix_error(); |
| 2926 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 2927 | if (slave_fd < 0) |
| 2928 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2929 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2930 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 2931 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 2932 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2933 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 2934 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2935 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 2936 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2937 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2938 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2939 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2940 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2941 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2942 | |
| 2943 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2944 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2945 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2946 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 2947 | 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] | 2948 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2949 | |
| 2950 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2951 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2952 | { |
| 2953 | int master_fd, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2954 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2955 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 2956 | if (pid == -1) |
| 2957 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2958 | if (pid == 0) |
| 2959 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2960 | return Py_BuildValue("(ii)", pid, master_fd); |
| 2961 | } |
| 2962 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2963 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2964 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2965 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2966 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2967 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2968 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2969 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2970 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2971 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2972 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2973 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2974 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2975 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2976 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2977 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2978 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2979 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2980 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2981 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2982 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2983 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2984 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2985 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2986 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2987 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2988 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2989 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2990 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2991 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2992 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2993 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2994 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2995 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2996 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2997 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2998 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2999 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3000 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3001 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3002 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3003 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3004 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3005 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3006 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3007 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3008 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3009 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3010 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3013 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3014 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3015 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3016 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3017 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3018 | |
| 3019 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3020 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3021 | { |
| 3022 | PyObject *result = NULL; |
| 3023 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3024 | #ifdef NGROUPS_MAX |
| 3025 | #define MAX_GROUPS NGROUPS_MAX |
| 3026 | #else |
| 3027 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3028 | #define MAX_GROUPS 64 |
| 3029 | #endif |
| 3030 | gid_t grouplist[MAX_GROUPS]; |
| 3031 | int n; |
| 3032 | |
| 3033 | n = getgroups(MAX_GROUPS, grouplist); |
| 3034 | if (n < 0) |
| 3035 | posix_error(); |
| 3036 | else { |
| 3037 | result = PyList_New(n); |
| 3038 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3039 | int i; |
| 3040 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3041 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3042 | if (o == NULL) { |
| 3043 | Py_DECREF(result); |
| 3044 | result = NULL; |
| 3045 | break; |
| 3046 | } |
| 3047 | PyList_SET_ITEM(result, i, o); |
| 3048 | } |
| 3049 | } |
| 3050 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3051 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3052 | return result; |
| 3053 | } |
| 3054 | #endif |
| 3055 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3056 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3057 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3058 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3059 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3060 | |
| 3061 | static PyObject * |
| 3062 | posix_getpgid(PyObject *self, PyObject *args) |
| 3063 | { |
| 3064 | int pid, pgid; |
| 3065 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3066 | return NULL; |
| 3067 | pgid = getpgid(pid); |
| 3068 | if (pgid < 0) |
| 3069 | return posix_error(); |
| 3070 | return PyInt_FromLong((long)pgid); |
| 3071 | } |
| 3072 | #endif /* HAVE_GETPGID */ |
| 3073 | |
| 3074 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3075 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3076 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3077 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3078 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3079 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3080 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3081 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3082 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3083 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3084 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3085 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3086 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3087 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3088 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3089 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3090 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3091 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3092 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3093 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3094 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3095 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3096 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3097 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3098 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3099 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3100 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3101 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3102 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3103 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3104 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3105 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3106 | Py_INCREF(Py_None); |
| 3107 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3110 | #endif /* HAVE_SETPGRP */ |
| 3111 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3112 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3113 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3114 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3115 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3116 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3117 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3118 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3119 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3120 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3121 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3122 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3123 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3124 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3125 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3126 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3127 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3128 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3129 | |
| 3130 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3131 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3132 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3133 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3134 | char *name; |
| 3135 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3136 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3137 | errno = 0; |
| 3138 | name = getlogin(); |
| 3139 | if (name == NULL) { |
| 3140 | if (errno) |
| 3141 | posix_error(); |
| 3142 | else |
| 3143 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3144 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3145 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3146 | else |
| 3147 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3148 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3149 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3150 | return result; |
| 3151 | } |
| 3152 | #endif |
| 3153 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3154 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3155 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3156 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3157 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3158 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3159 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3160 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3161 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3162 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3163 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3164 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3165 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3166 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3167 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3168 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3169 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3170 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3171 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3172 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3173 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3174 | { |
| 3175 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3176 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3177 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3178 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3179 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3180 | APIRET rc; |
| 3181 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3182 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3183 | |
| 3184 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3185 | APIRET rc; |
| 3186 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3187 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3188 | |
| 3189 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3190 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3191 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3192 | if (kill(pid, sig) == -1) |
| 3193 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3194 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3195 | Py_INCREF(Py_None); |
| 3196 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3197 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3198 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3199 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3200 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3201 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3202 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3203 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3204 | |
| 3205 | static PyObject * |
| 3206 | posix_killpg(PyObject *self, PyObject *args) |
| 3207 | { |
| 3208 | int pgid, sig; |
| 3209 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3210 | return NULL; |
| 3211 | if (killpg(pgid, sig) == -1) |
| 3212 | return posix_error(); |
| 3213 | Py_INCREF(Py_None); |
| 3214 | return Py_None; |
| 3215 | } |
| 3216 | #endif |
| 3217 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3218 | #ifdef HAVE_PLOCK |
| 3219 | |
| 3220 | #ifdef HAVE_SYS_LOCK_H |
| 3221 | #include <sys/lock.h> |
| 3222 | #endif |
| 3223 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3224 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3225 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3226 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3227 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3228 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3229 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3230 | { |
| 3231 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3232 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3233 | return NULL; |
| 3234 | if (plock(op) == -1) |
| 3235 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3236 | Py_INCREF(Py_None); |
| 3237 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3238 | } |
| 3239 | #endif |
| 3240 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3241 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3242 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3243 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3244 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3245 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3246 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3247 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3248 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3249 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3250 | async_system(const char *command) |
| 3251 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3252 | char errormsg[256], args[1024]; |
| 3253 | RESULTCODES rcodes; |
| 3254 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3255 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3256 | char *shell = getenv("COMSPEC"); |
| 3257 | if (!shell) |
| 3258 | shell = "cmd"; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3259 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3260 | /* avoid overflowing the argument buffer */ |
| 3261 | if (strlen(shell) + 3 + strlen(command) >= 1024) |
| 3262 | return ERROR_NOT_ENOUGH_MEMORY |
| 3263 | |
| 3264 | args[0] = '\0'; |
| 3265 | strcat(args, shell); |
| 3266 | strcat(args, "/c "); |
| 3267 | strcat(args, command); |
| 3268 | |
| 3269 | /* execute asynchronously, inheriting the environment */ |
| 3270 | rc = DosExecPgm(errormsg, |
| 3271 | sizeof(errormsg), |
| 3272 | EXEC_ASYNC, |
| 3273 | args, |
| 3274 | NULL, |
| 3275 | &rcodes, |
| 3276 | shell); |
| 3277 | return rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3280 | static FILE * |
| 3281 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3282 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3283 | int oldfd, tgtfd; |
| 3284 | HFILE pipeh[2]; |
| 3285 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3286 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3287 | /* mode determines which of stdin or stdout is reconnected to |
| 3288 | * the pipe to the child |
| 3289 | */ |
| 3290 | if (strchr(mode, 'r') != NULL) { |
| 3291 | tgt_fd = 1; /* stdout */ |
| 3292 | } else if (strchr(mode, 'w')) { |
| 3293 | tgt_fd = 0; /* stdin */ |
| 3294 | } else { |
| 3295 | *err = ERROR_INVALID_ACCESS; |
| 3296 | return NULL; |
| 3297 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3298 | |
Andrew MacIntyre | a3be258 | 2004-12-18 09:51:05 +0000 | [diff] [blame] | 3299 | /* setup the pipe */ |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3300 | if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) { |
| 3301 | *err = rc; |
| 3302 | return NULL; |
| 3303 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3304 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3305 | /* prevent other threads accessing stdio */ |
| 3306 | DosEnterCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3307 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3308 | /* reconnect stdio and execute child */ |
| 3309 | oldfd = dup(tgtfd); |
| 3310 | close(tgtfd); |
| 3311 | if (dup2(pipeh[tgtfd], tgtfd) == 0) { |
| 3312 | DosClose(pipeh[tgtfd]); |
| 3313 | rc = async_system(command); |
| 3314 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3315 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3316 | /* restore stdio */ |
| 3317 | dup2(oldfd, tgtfd); |
| 3318 | close(oldfd); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3319 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3320 | /* allow other threads access to stdio */ |
| 3321 | DosExitCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3322 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3323 | /* if execution of child was successful return file stream */ |
| 3324 | if (rc == NO_ERROR) |
| 3325 | return fdopen(pipeh[1 - tgtfd], mode); |
| 3326 | else { |
| 3327 | DosClose(pipeh[1 - tgtfd]); |
| 3328 | *err = rc; |
| 3329 | return NULL; |
| 3330 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3334 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3335 | { |
| 3336 | char *name; |
| 3337 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3338 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3339 | FILE *fp; |
| 3340 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3341 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3342 | return NULL; |
| 3343 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3344 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3345 | Py_END_ALLOW_THREADS |
| 3346 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3347 | return os2_error(err); |
| 3348 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3349 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 3350 | if (f != NULL) |
| 3351 | PyFile_SetBufSize(f, bufsize); |
| 3352 | return f; |
| 3353 | } |
| 3354 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3355 | #elif defined(PYCC_GCC) |
| 3356 | |
| 3357 | /* standard posix version of popen() support */ |
| 3358 | static PyObject * |
| 3359 | posix_popen(PyObject *self, PyObject *args) |
| 3360 | { |
| 3361 | char *name; |
| 3362 | char *mode = "r"; |
| 3363 | int bufsize = -1; |
| 3364 | FILE *fp; |
| 3365 | PyObject *f; |
| 3366 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 3367 | return NULL; |
| 3368 | Py_BEGIN_ALLOW_THREADS |
| 3369 | fp = popen(name, mode); |
| 3370 | Py_END_ALLOW_THREADS |
| 3371 | if (fp == NULL) |
| 3372 | return posix_error(); |
| 3373 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 3374 | if (f != NULL) |
| 3375 | PyFile_SetBufSize(f, bufsize); |
| 3376 | return f; |
| 3377 | } |
| 3378 | |
| 3379 | /* fork() under OS/2 has lots'o'warts |
| 3380 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 3381 | * most of this code is a ripoff of the win32 code, but using the |
| 3382 | * capabilities of EMX's C library routines |
| 3383 | */ |
| 3384 | |
| 3385 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 3386 | #define POPEN_1 1 |
| 3387 | #define POPEN_2 2 |
| 3388 | #define POPEN_3 3 |
| 3389 | #define POPEN_4 4 |
| 3390 | |
| 3391 | static PyObject *_PyPopen(char *, int, int, int); |
| 3392 | static int _PyPclose(FILE *file); |
| 3393 | |
| 3394 | /* |
| 3395 | * Internal dictionary mapping popen* file pointers to process handles, |
| 3396 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3397 | * for more information on this dictionary's use. |
| 3398 | */ |
| 3399 | static PyObject *_PyPopenProcs = NULL; |
| 3400 | |
| 3401 | /* os2emx version of popen2() |
| 3402 | * |
| 3403 | * The result of this function is a pipe (file) connected to the |
| 3404 | * process's stdin, and a pipe connected to the process's stdout. |
| 3405 | */ |
| 3406 | |
| 3407 | static PyObject * |
| 3408 | os2emx_popen2(PyObject *self, PyObject *args) |
| 3409 | { |
| 3410 | PyObject *f; |
| 3411 | int tm=0; |
| 3412 | |
| 3413 | char *cmdstring; |
| 3414 | char *mode = "t"; |
| 3415 | int bufsize = -1; |
| 3416 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 3417 | return NULL; |
| 3418 | |
| 3419 | if (*mode == 't') |
| 3420 | tm = O_TEXT; |
| 3421 | else if (*mode != 'b') { |
| 3422 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3423 | return NULL; |
| 3424 | } else |
| 3425 | tm = O_BINARY; |
| 3426 | |
| 3427 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 3428 | |
| 3429 | return f; |
| 3430 | } |
| 3431 | |
| 3432 | /* |
| 3433 | * Variation on os2emx.popen2 |
| 3434 | * |
| 3435 | * The result of this function is 3 pipes - the process's stdin, |
| 3436 | * stdout and stderr |
| 3437 | */ |
| 3438 | |
| 3439 | static PyObject * |
| 3440 | os2emx_popen3(PyObject *self, PyObject *args) |
| 3441 | { |
| 3442 | PyObject *f; |
| 3443 | int tm = 0; |
| 3444 | |
| 3445 | char *cmdstring; |
| 3446 | char *mode = "t"; |
| 3447 | int bufsize = -1; |
| 3448 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 3449 | return NULL; |
| 3450 | |
| 3451 | if (*mode == 't') |
| 3452 | tm = O_TEXT; |
| 3453 | else if (*mode != 'b') { |
| 3454 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3455 | return NULL; |
| 3456 | } else |
| 3457 | tm = O_BINARY; |
| 3458 | |
| 3459 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 3460 | |
| 3461 | return f; |
| 3462 | } |
| 3463 | |
| 3464 | /* |
| 3465 | * Variation on os2emx.popen2 |
| 3466 | * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3467 | * The result of this function is 2 pipes - the processes stdin, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3468 | * and stdout+stderr combined as a single pipe. |
| 3469 | */ |
| 3470 | |
| 3471 | static PyObject * |
| 3472 | os2emx_popen4(PyObject *self, PyObject *args) |
| 3473 | { |
| 3474 | PyObject *f; |
| 3475 | int tm = 0; |
| 3476 | |
| 3477 | char *cmdstring; |
| 3478 | char *mode = "t"; |
| 3479 | int bufsize = -1; |
| 3480 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 3481 | return NULL; |
| 3482 | |
| 3483 | if (*mode == 't') |
| 3484 | tm = O_TEXT; |
| 3485 | else if (*mode != 'b') { |
| 3486 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3487 | return NULL; |
| 3488 | } else |
| 3489 | tm = O_BINARY; |
| 3490 | |
| 3491 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 3492 | |
| 3493 | return f; |
| 3494 | } |
| 3495 | |
| 3496 | /* a couple of structures for convenient handling of multiple |
| 3497 | * file handles and pipes |
| 3498 | */ |
| 3499 | struct file_ref |
| 3500 | { |
| 3501 | int handle; |
| 3502 | int flags; |
| 3503 | }; |
| 3504 | |
| 3505 | struct pipe_ref |
| 3506 | { |
| 3507 | int rd; |
| 3508 | int wr; |
| 3509 | }; |
| 3510 | |
| 3511 | /* The following code is derived from the win32 code */ |
| 3512 | |
| 3513 | static PyObject * |
| 3514 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 3515 | { |
| 3516 | struct file_ref stdio[3]; |
| 3517 | struct pipe_ref p_fd[3]; |
| 3518 | FILE *p_s[3]; |
| 3519 | int file_count, i, pipe_err, pipe_pid; |
| 3520 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 3521 | PyObject *f, *p_f[3]; |
| 3522 | |
| 3523 | /* file modes for subsequent fdopen's on pipe handles */ |
| 3524 | if (mode == O_TEXT) |
| 3525 | { |
| 3526 | rd_mode = "rt"; |
| 3527 | wr_mode = "wt"; |
| 3528 | } |
| 3529 | else |
| 3530 | { |
| 3531 | rd_mode = "rb"; |
| 3532 | wr_mode = "wb"; |
| 3533 | } |
| 3534 | |
| 3535 | /* prepare shell references */ |
| 3536 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 3537 | if ((shell = getenv("COMSPEC")) == NULL) |
| 3538 | { |
| 3539 | errno = ENOENT; |
| 3540 | return posix_error(); |
| 3541 | } |
| 3542 | |
| 3543 | sh_name = _getname(shell); |
| 3544 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 3545 | opt = "/c"; |
| 3546 | else |
| 3547 | opt = "-c"; |
| 3548 | |
| 3549 | /* save current stdio fds + their flags, and set not inheritable */ |
| 3550 | i = pipe_err = 0; |
| 3551 | while (pipe_err >= 0 && i < 3) |
| 3552 | { |
| 3553 | pipe_err = stdio[i].handle = dup(i); |
| 3554 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 3555 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 3556 | i++; |
| 3557 | } |
| 3558 | if (pipe_err < 0) |
| 3559 | { |
| 3560 | /* didn't get them all saved - clean up and bail out */ |
| 3561 | int saved_err = errno; |
| 3562 | while (i-- > 0) |
| 3563 | { |
| 3564 | close(stdio[i].handle); |
| 3565 | } |
| 3566 | errno = saved_err; |
| 3567 | return posix_error(); |
| 3568 | } |
| 3569 | |
| 3570 | /* create pipe ends */ |
| 3571 | file_count = 2; |
| 3572 | if (n == POPEN_3) |
| 3573 | file_count = 3; |
| 3574 | i = pipe_err = 0; |
| 3575 | while ((pipe_err == 0) && (i < file_count)) |
| 3576 | pipe_err = pipe((int *)&p_fd[i++]); |
| 3577 | if (pipe_err < 0) |
| 3578 | { |
| 3579 | /* didn't get them all made - clean up and bail out */ |
| 3580 | while (i-- > 0) |
| 3581 | { |
| 3582 | close(p_fd[i].wr); |
| 3583 | close(p_fd[i].rd); |
| 3584 | } |
| 3585 | errno = EPIPE; |
| 3586 | return posix_error(); |
| 3587 | } |
| 3588 | |
| 3589 | /* change the actual standard IO streams over temporarily, |
| 3590 | * making the retained pipe ends non-inheritable |
| 3591 | */ |
| 3592 | pipe_err = 0; |
| 3593 | |
| 3594 | /* - stdin */ |
| 3595 | if (dup2(p_fd[0].rd, 0) == 0) |
| 3596 | { |
| 3597 | close(p_fd[0].rd); |
| 3598 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 3599 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 3600 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 3601 | { |
| 3602 | close(p_fd[0].wr); |
| 3603 | pipe_err = -1; |
| 3604 | } |
| 3605 | } |
| 3606 | else |
| 3607 | { |
| 3608 | pipe_err = -1; |
| 3609 | } |
| 3610 | |
| 3611 | /* - stdout */ |
| 3612 | if (pipe_err == 0) |
| 3613 | { |
| 3614 | if (dup2(p_fd[1].wr, 1) == 1) |
| 3615 | { |
| 3616 | close(p_fd[1].wr); |
| 3617 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 3618 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 3619 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 3620 | { |
| 3621 | close(p_fd[1].rd); |
| 3622 | pipe_err = -1; |
| 3623 | } |
| 3624 | } |
| 3625 | else |
| 3626 | { |
| 3627 | pipe_err = -1; |
| 3628 | } |
| 3629 | } |
| 3630 | |
| 3631 | /* - stderr, as required */ |
| 3632 | if (pipe_err == 0) |
| 3633 | switch (n) |
| 3634 | { |
| 3635 | case POPEN_3: |
| 3636 | { |
| 3637 | if (dup2(p_fd[2].wr, 2) == 2) |
| 3638 | { |
| 3639 | close(p_fd[2].wr); |
| 3640 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 3641 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 3642 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 3643 | { |
| 3644 | close(p_fd[2].rd); |
| 3645 | pipe_err = -1; |
| 3646 | } |
| 3647 | } |
| 3648 | else |
| 3649 | { |
| 3650 | pipe_err = -1; |
| 3651 | } |
| 3652 | break; |
| 3653 | } |
| 3654 | |
| 3655 | case POPEN_4: |
| 3656 | { |
| 3657 | if (dup2(1, 2) != 2) |
| 3658 | { |
| 3659 | pipe_err = -1; |
| 3660 | } |
| 3661 | break; |
| 3662 | } |
| 3663 | } |
| 3664 | |
| 3665 | /* spawn the child process */ |
| 3666 | if (pipe_err == 0) |
| 3667 | { |
| 3668 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 3669 | if (pipe_pid == -1) |
| 3670 | { |
| 3671 | pipe_err = -1; |
| 3672 | } |
| 3673 | else |
| 3674 | { |
| 3675 | /* save the PID into the FILE structure |
| 3676 | * NOTE: this implementation doesn't actually |
| 3677 | * take advantage of this, but do it for |
| 3678 | * completeness - AIM Apr01 |
| 3679 | */ |
| 3680 | for (i = 0; i < file_count; i++) |
| 3681 | p_s[i]->_pid = pipe_pid; |
| 3682 | } |
| 3683 | } |
| 3684 | |
| 3685 | /* reset standard IO to normal */ |
| 3686 | for (i = 0; i < 3; i++) |
| 3687 | { |
| 3688 | dup2(stdio[i].handle, i); |
| 3689 | fcntl(i, F_SETFD, stdio[i].flags); |
| 3690 | close(stdio[i].handle); |
| 3691 | } |
| 3692 | |
| 3693 | /* if any remnant problems, clean up and bail out */ |
| 3694 | if (pipe_err < 0) |
| 3695 | { |
| 3696 | for (i = 0; i < 3; i++) |
| 3697 | { |
| 3698 | close(p_fd[i].rd); |
| 3699 | close(p_fd[i].wr); |
| 3700 | } |
| 3701 | errno = EPIPE; |
| 3702 | return posix_error_with_filename(cmdstring); |
| 3703 | } |
| 3704 | |
| 3705 | /* build tuple of file objects to return */ |
| 3706 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 3707 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3708 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3709 | PyFile_SetBufSize(p_f[1], bufsize); |
| 3710 | if (n == POPEN_3) |
| 3711 | { |
| 3712 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3713 | PyFile_SetBufSize(p_f[0], bufsize); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3714 | 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] | 3715 | } |
| 3716 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3717 | f = PyTuple_Pack(2, p_f[0], p_f[1]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3718 | |
| 3719 | /* |
| 3720 | * Insert the files we've created into the process dictionary |
| 3721 | * all referencing the list with the process handle and the |
| 3722 | * initial number of files (see description below in _PyPclose). |
| 3723 | * Since if _PyPclose later tried to wait on a process when all |
| 3724 | * handles weren't closed, it could create a deadlock with the |
| 3725 | * child, we spend some energy here to try to ensure that we |
| 3726 | * either insert all file handles into the dictionary or none |
| 3727 | * at all. It's a little clumsy with the various popen modes |
| 3728 | * and variable number of files involved. |
| 3729 | */ |
| 3730 | if (!_PyPopenProcs) |
| 3731 | { |
| 3732 | _PyPopenProcs = PyDict_New(); |
| 3733 | } |
| 3734 | |
| 3735 | if (_PyPopenProcs) |
| 3736 | { |
| 3737 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 3738 | int ins_rc[3]; |
| 3739 | |
| 3740 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 3741 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 3742 | |
| 3743 | procObj = PyList_New(2); |
| 3744 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 3745 | intObj = PyInt_FromLong((long) file_count); |
| 3746 | |
| 3747 | if (procObj && pidObj && intObj) |
| 3748 | { |
| 3749 | PyList_SetItem(procObj, 0, pidObj); |
| 3750 | PyList_SetItem(procObj, 1, intObj); |
| 3751 | |
| 3752 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 3753 | if (fileObj[0]) |
| 3754 | { |
| 3755 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 3756 | fileObj[0], |
| 3757 | procObj); |
| 3758 | } |
| 3759 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 3760 | if (fileObj[1]) |
| 3761 | { |
| 3762 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 3763 | fileObj[1], |
| 3764 | procObj); |
| 3765 | } |
| 3766 | if (file_count >= 3) |
| 3767 | { |
| 3768 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 3769 | if (fileObj[2]) |
| 3770 | { |
| 3771 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 3772 | fileObj[2], |
| 3773 | procObj); |
| 3774 | } |
| 3775 | } |
| 3776 | |
| 3777 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 3778 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 3779 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 3780 | { |
| 3781 | /* Something failed - remove any dictionary |
| 3782 | * entries that did make it. |
| 3783 | */ |
| 3784 | if (!ins_rc[0] && fileObj[0]) |
| 3785 | { |
| 3786 | PyDict_DelItem(_PyPopenProcs, |
| 3787 | fileObj[0]); |
| 3788 | } |
| 3789 | if (!ins_rc[1] && fileObj[1]) |
| 3790 | { |
| 3791 | PyDict_DelItem(_PyPopenProcs, |
| 3792 | fileObj[1]); |
| 3793 | } |
| 3794 | if (!ins_rc[2] && fileObj[2]) |
| 3795 | { |
| 3796 | PyDict_DelItem(_PyPopenProcs, |
| 3797 | fileObj[2]); |
| 3798 | } |
| 3799 | } |
| 3800 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3801 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3802 | /* |
| 3803 | * Clean up our localized references for the dictionary keys |
| 3804 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 3805 | * that got placed in the dictionary. |
| 3806 | */ |
| 3807 | Py_XDECREF(procObj); |
| 3808 | Py_XDECREF(fileObj[0]); |
| 3809 | Py_XDECREF(fileObj[1]); |
| 3810 | Py_XDECREF(fileObj[2]); |
| 3811 | } |
| 3812 | |
| 3813 | /* Child is launched. */ |
| 3814 | return f; |
| 3815 | } |
| 3816 | |
| 3817 | /* |
| 3818 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 3819 | * exit code for the child process and return as a result of the close. |
| 3820 | * |
| 3821 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 3822 | * input file pointer to information about the process that was |
| 3823 | * originally created by the popen* call that created the file pointer. |
| 3824 | * The dictionary uses the file pointer as a key (with one entry |
| 3825 | * inserted for each file returned by the original popen* call) and a |
| 3826 | * single list object as the value for all files from a single call. |
| 3827 | * The list object contains the Win32 process handle at [0], and a file |
| 3828 | * count at [1], which is initialized to the total number of file |
| 3829 | * handles using that list. |
| 3830 | * |
| 3831 | * This function closes whichever handle it is passed, and decrements |
| 3832 | * the file count in the dictionary for the process handle pointed to |
| 3833 | * by this file. On the last close (when the file count reaches zero), |
| 3834 | * this function will wait for the child process and then return its |
| 3835 | * exit code as the result of the close() operation. This permits the |
| 3836 | * files to be closed in any order - it is always the close() of the |
| 3837 | * final handle that will return the exit code. |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3838 | * |
| 3839 | * NOTE: This function is currently called with the GIL released. |
| 3840 | * hence we use the GILState API to manage our state. |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3841 | */ |
| 3842 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3843 | static int _PyPclose(FILE *file) |
| 3844 | { |
| 3845 | int result; |
| 3846 | int exit_code; |
| 3847 | int pipe_pid; |
| 3848 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 3849 | int file_count; |
| 3850 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3851 | PyGILState_STATE state; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3852 | #endif |
| 3853 | |
| 3854 | /* Close the file handle first, to ensure it can't block the |
| 3855 | * child from exiting if it's the last handle. |
| 3856 | */ |
| 3857 | result = fclose(file); |
| 3858 | |
| 3859 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3860 | state = PyGILState_Ensure(); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3861 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3862 | if (_PyPopenProcs) |
| 3863 | { |
| 3864 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 3865 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 3866 | fileObj)) != NULL && |
| 3867 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 3868 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 3869 | { |
| 3870 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 3871 | file_count = (int) PyInt_AsLong(intObj); |
| 3872 | |
| 3873 | if (file_count > 1) |
| 3874 | { |
| 3875 | /* Still other files referencing process */ |
| 3876 | file_count--; |
| 3877 | PyList_SetItem(procObj,1, |
| 3878 | PyInt_FromLong((long) file_count)); |
| 3879 | } |
| 3880 | else |
| 3881 | { |
| 3882 | /* Last file for this process */ |
| 3883 | if (result != EOF && |
| 3884 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 3885 | { |
| 3886 | /* extract exit status */ |
| 3887 | if (WIFEXITED(exit_code)) |
| 3888 | { |
| 3889 | result = WEXITSTATUS(exit_code); |
| 3890 | } |
| 3891 | else |
| 3892 | { |
| 3893 | errno = EPIPE; |
| 3894 | result = -1; |
| 3895 | } |
| 3896 | } |
| 3897 | else |
| 3898 | { |
| 3899 | /* Indicate failure - this will cause the file object |
| 3900 | * to raise an I/O error and translate the last |
| 3901 | * error code from errno. We do have a problem with |
| 3902 | * last errors that overlap the normal errno table, |
| 3903 | * but that's a consistent problem with the file object. |
| 3904 | */ |
| 3905 | result = -1; |
| 3906 | } |
| 3907 | } |
| 3908 | |
| 3909 | /* Remove this file pointer from dictionary */ |
| 3910 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 3911 | |
| 3912 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 3913 | { |
| 3914 | Py_DECREF(_PyPopenProcs); |
| 3915 | _PyPopenProcs = NULL; |
| 3916 | } |
| 3917 | |
| 3918 | } /* if object retrieval ok */ |
| 3919 | |
| 3920 | Py_XDECREF(fileObj); |
| 3921 | } /* if _PyPopenProcs */ |
| 3922 | |
| 3923 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3924 | PyGILState_Release(state); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3925 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3926 | return result; |
| 3927 | } |
| 3928 | |
| 3929 | #endif /* PYCC_??? */ |
| 3930 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3931 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3932 | |
| 3933 | /* |
| 3934 | * Portable 'popen' replacement for Win32. |
| 3935 | * |
| 3936 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 3937 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3938 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3939 | */ |
| 3940 | |
| 3941 | #include <malloc.h> |
| 3942 | #include <io.h> |
| 3943 | #include <fcntl.h> |
| 3944 | |
| 3945 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 3946 | #define POPEN_1 1 |
| 3947 | #define POPEN_2 2 |
| 3948 | #define POPEN_3 3 |
| 3949 | #define POPEN_4 4 |
| 3950 | |
| 3951 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3952 | static int _PyPclose(FILE *file); |
| 3953 | |
| 3954 | /* |
| 3955 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3956 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3957 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3958 | */ |
| 3959 | static PyObject *_PyPopenProcs = NULL; |
| 3960 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3961 | |
| 3962 | /* popen that works from a GUI. |
| 3963 | * |
| 3964 | * The result of this function is a pipe (file) connected to the |
| 3965 | * processes stdin or stdout, depending on the requested mode. |
| 3966 | */ |
| 3967 | |
| 3968 | static PyObject * |
| 3969 | posix_popen(PyObject *self, PyObject *args) |
| 3970 | { |
Raymond Hettinger | b5cb665 | 2003-09-01 22:25:41 +0000 | [diff] [blame] | 3971 | PyObject *f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3972 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3973 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3974 | char *cmdstring; |
| 3975 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3976 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3977 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3978 | return NULL; |
| 3979 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3980 | if (*mode == 'r') |
| 3981 | tm = _O_RDONLY; |
| 3982 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3983 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3984 | return NULL; |
| 3985 | } else |
| 3986 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3987 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3988 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3989 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3990 | return NULL; |
| 3991 | } |
| 3992 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3993 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3994 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3995 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3996 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3997 | else |
| 3998 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 3999 | |
| 4000 | return f; |
| 4001 | } |
| 4002 | |
| 4003 | /* Variation on win32pipe.popen |
| 4004 | * |
| 4005 | * The result of this function is a pipe (file) connected to the |
| 4006 | * process's stdin, and a pipe connected to the process's stdout. |
| 4007 | */ |
| 4008 | |
| 4009 | static PyObject * |
| 4010 | win32_popen2(PyObject *self, PyObject *args) |
| 4011 | { |
| 4012 | PyObject *f; |
| 4013 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4014 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4015 | char *cmdstring; |
| 4016 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4017 | int bufsize = -1; |
| 4018 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4019 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4020 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4021 | if (*mode == 't') |
| 4022 | tm = _O_TEXT; |
| 4023 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4024 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4025 | return NULL; |
| 4026 | } else |
| 4027 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4028 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4029 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4030 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4031 | return NULL; |
| 4032 | } |
| 4033 | |
| 4034 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4035 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4036 | return f; |
| 4037 | } |
| 4038 | |
| 4039 | /* |
| 4040 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4041 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4042 | * The result of this function is 3 pipes - the process's stdin, |
| 4043 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4044 | */ |
| 4045 | |
| 4046 | static PyObject * |
| 4047 | win32_popen3(PyObject *self, PyObject *args) |
| 4048 | { |
| 4049 | PyObject *f; |
| 4050 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4051 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4052 | char *cmdstring; |
| 4053 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4054 | int bufsize = -1; |
| 4055 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4056 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4057 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4058 | if (*mode == 't') |
| 4059 | tm = _O_TEXT; |
| 4060 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4061 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4062 | return NULL; |
| 4063 | } else |
| 4064 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4065 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4066 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4067 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4068 | return NULL; |
| 4069 | } |
| 4070 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4071 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4072 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4073 | return f; |
| 4074 | } |
| 4075 | |
| 4076 | /* |
| 4077 | * Variation on win32pipe.popen |
| 4078 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4079 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4080 | * and stdout+stderr combined as a single pipe. |
| 4081 | */ |
| 4082 | |
| 4083 | static PyObject * |
| 4084 | win32_popen4(PyObject *self, PyObject *args) |
| 4085 | { |
| 4086 | PyObject *f; |
| 4087 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4088 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4089 | char *cmdstring; |
| 4090 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4091 | int bufsize = -1; |
| 4092 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4093 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4094 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4095 | if (*mode == 't') |
| 4096 | tm = _O_TEXT; |
| 4097 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4098 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4099 | return NULL; |
| 4100 | } else |
| 4101 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4102 | |
| 4103 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4104 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4105 | return NULL; |
| 4106 | } |
| 4107 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4108 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4109 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4110 | return f; |
| 4111 | } |
| 4112 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4113 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4114 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4115 | HANDLE hStdin, |
| 4116 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4117 | HANDLE hStderr, |
| 4118 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4119 | { |
| 4120 | PROCESS_INFORMATION piProcInfo; |
| 4121 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4122 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4123 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4124 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4125 | int i; |
| 4126 | int x; |
| 4127 | |
| 4128 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4129 | char *comshell; |
| 4130 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4131 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4132 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 4133 | return x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4134 | |
| 4135 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 4136 | * then use the w9xpopen hack. |
| 4137 | */ |
| 4138 | comshell = s1 + x; |
| 4139 | while (comshell >= s1 && *comshell != '\\') |
| 4140 | --comshell; |
| 4141 | ++comshell; |
| 4142 | |
| 4143 | if (GetVersion() < 0x80000000 && |
| 4144 | _stricmp(comshell, "command.com") != 0) { |
| 4145 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4146 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4147 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4148 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4149 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4150 | } |
| 4151 | else { |
| 4152 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4153 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 4154 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4155 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4156 | char modulepath[_MAX_PATH]; |
| 4157 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4158 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 4159 | for (i = x = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4160 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4161 | x = i+1; |
| 4162 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4163 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4164 | strncat(modulepath, |
| 4165 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4166 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4167 | -strlen(modulepath)); |
| 4168 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4169 | /* Eeek - file-not-found - possibly an embedding |
| 4170 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4171 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4172 | strncpy(modulepath, |
| 4173 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4174 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 4175 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 4176 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4177 | strncat(modulepath, |
| 4178 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4179 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4180 | -strlen(modulepath)); |
| 4181 | /* No where else to look - raise an easily identifiable |
| 4182 | error, rather than leaving Windows to report |
| 4183 | "file not found" - as the user is probably blissfully |
| 4184 | unaware this shim EXE is used, and it will confuse them. |
| 4185 | (well, it confused me for a while ;-) |
| 4186 | */ |
| 4187 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4188 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4189 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4190 | "for popen to work with your shell " |
| 4191 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4192 | szConsoleSpawn); |
| 4193 | return FALSE; |
| 4194 | } |
| 4195 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4196 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4197 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4198 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4199 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4200 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4201 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4202 | /* To maintain correct argument passing semantics, |
| 4203 | we pass the command-line as it stands, and allow |
| 4204 | quoting to be applied. w9xpopen.exe will then |
| 4205 | use its argv vector, and re-quote the necessary |
| 4206 | args for the ultimate child process. |
| 4207 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4208 | PyOS_snprintf( |
| 4209 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4210 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4211 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4212 | s1, |
| 4213 | s3, |
| 4214 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4215 | /* Not passing CREATE_NEW_CONSOLE has been known to |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4216 | cause random failures on win9x. Specifically a |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4217 | dialog: |
| 4218 | "Your program accessed mem currently in use at xxx" |
| 4219 | and a hopeful warning about the stability of your |
| 4220 | system. |
| 4221 | Cost is Ctrl+C wont kill children, but anyone |
| 4222 | who cares can have a go! |
| 4223 | */ |
| 4224 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4225 | } |
| 4226 | } |
| 4227 | |
| 4228 | /* Could be an else here to try cmd.exe / command.com in the path |
| 4229 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4230 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4231 | PyErr_SetString(PyExc_RuntimeError, |
| 4232 | "Cannot locate a COMSPEC environment variable to " |
| 4233 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4234 | return FALSE; |
| 4235 | } |
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 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 4238 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 4239 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 4240 | siStartInfo.hStdInput = hStdin; |
| 4241 | siStartInfo.hStdOutput = hStdout; |
| 4242 | siStartInfo.hStdError = hStderr; |
| 4243 | siStartInfo.wShowWindow = SW_HIDE; |
| 4244 | |
| 4245 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4246 | s2, |
| 4247 | NULL, |
| 4248 | NULL, |
| 4249 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4250 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4251 | NULL, |
| 4252 | NULL, |
| 4253 | &siStartInfo, |
| 4254 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4255 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4256 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4257 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4258 | /* Return process handle */ |
| 4259 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4260 | return TRUE; |
| 4261 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4262 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4263 | return FALSE; |
| 4264 | } |
| 4265 | |
| 4266 | /* The following code is based off of KB: Q190351 */ |
| 4267 | |
| 4268 | static PyObject * |
| 4269 | _PyPopen(char *cmdstring, int mode, int n) |
| 4270 | { |
| 4271 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 4272 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4273 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4274 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4275 | SECURITY_ATTRIBUTES saAttr; |
| 4276 | BOOL fSuccess; |
| 4277 | int fd1, fd2, fd3; |
| 4278 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4279 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4280 | PyObject *f; |
| 4281 | |
| 4282 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 4283 | saAttr.bInheritHandle = TRUE; |
| 4284 | saAttr.lpSecurityDescriptor = NULL; |
| 4285 | |
| 4286 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 4287 | return win32_error("CreatePipe", NULL); |
| 4288 | |
| 4289 | /* Create new output read handle and the input write handle. Set |
| 4290 | * the inheritance properties to FALSE. Otherwise, the child inherits |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 4291 | * these handles; resulting in non-closeable handles to the pipes |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4292 | * being created. */ |
| 4293 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4294 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 4295 | FALSE, |
| 4296 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4297 | if (!fSuccess) |
| 4298 | return win32_error("DuplicateHandle", NULL); |
| 4299 | |
| 4300 | /* Close the inheritable version of ChildStdin |
| 4301 | that we're using. */ |
| 4302 | CloseHandle(hChildStdinWr); |
| 4303 | |
| 4304 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 4305 | return win32_error("CreatePipe", NULL); |
| 4306 | |
| 4307 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4308 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 4309 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4310 | if (!fSuccess) |
| 4311 | return win32_error("DuplicateHandle", NULL); |
| 4312 | |
| 4313 | /* Close the inheritable version of ChildStdout |
| 4314 | that we're using. */ |
| 4315 | CloseHandle(hChildStdoutRd); |
| 4316 | |
| 4317 | if (n != POPEN_4) { |
| 4318 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 4319 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4320 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 4321 | hChildStderrRd, |
| 4322 | GetCurrentProcess(), |
| 4323 | &hChildStderrRdDup, 0, |
| 4324 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4325 | if (!fSuccess) |
| 4326 | return win32_error("DuplicateHandle", NULL); |
| 4327 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 4328 | CloseHandle(hChildStderrRd); |
| 4329 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4330 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4331 | switch (n) { |
| 4332 | case POPEN_1: |
| 4333 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 4334 | case _O_WRONLY | _O_TEXT: |
| 4335 | /* Case for writing to child Stdin in text mode. */ |
| 4336 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4337 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4338 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4339 | PyFile_SetBufSize(f, 0); |
| 4340 | /* We don't care about these pipes anymore, so close them. */ |
| 4341 | CloseHandle(hChildStdoutRdDup); |
| 4342 | CloseHandle(hChildStderrRdDup); |
| 4343 | break; |
| 4344 | |
| 4345 | case _O_RDONLY | _O_TEXT: |
| 4346 | /* Case for reading from child Stdout in text mode. */ |
| 4347 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4348 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4349 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4350 | PyFile_SetBufSize(f, 0); |
| 4351 | /* We don't care about these pipes anymore, so close them. */ |
| 4352 | CloseHandle(hChildStdinWrDup); |
| 4353 | CloseHandle(hChildStderrRdDup); |
| 4354 | break; |
| 4355 | |
| 4356 | case _O_RDONLY | _O_BINARY: |
| 4357 | /* Case for readinig from child Stdout in binary mode. */ |
| 4358 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4359 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4360 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4361 | PyFile_SetBufSize(f, 0); |
| 4362 | /* We don't care about these pipes anymore, so close them. */ |
| 4363 | CloseHandle(hChildStdinWrDup); |
| 4364 | CloseHandle(hChildStderrRdDup); |
| 4365 | break; |
| 4366 | |
| 4367 | case _O_WRONLY | _O_BINARY: |
| 4368 | /* Case for writing to child Stdin in binary mode. */ |
| 4369 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4370 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4371 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4372 | PyFile_SetBufSize(f, 0); |
| 4373 | /* We don't care about these pipes anymore, so close them. */ |
| 4374 | CloseHandle(hChildStdoutRdDup); |
| 4375 | CloseHandle(hChildStderrRdDup); |
| 4376 | break; |
| 4377 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4378 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4379 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4380 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4381 | case POPEN_2: |
| 4382 | case POPEN_4: |
| 4383 | { |
| 4384 | char *m1, *m2; |
| 4385 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4386 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4387 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4388 | m1 = "r"; |
| 4389 | m2 = "w"; |
| 4390 | } else { |
| 4391 | m1 = "rb"; |
| 4392 | m2 = "wb"; |
| 4393 | } |
| 4394 | |
| 4395 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4396 | f1 = _fdopen(fd1, m2); |
| 4397 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4398 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4399 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4400 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4401 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4402 | PyFile_SetBufSize(p2, 0); |
| 4403 | |
| 4404 | if (n != 4) |
| 4405 | CloseHandle(hChildStderrRdDup); |
| 4406 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4407 | f = PyTuple_Pack(2,p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4408 | Py_XDECREF(p1); |
| 4409 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4410 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4411 | break; |
| 4412 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4413 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4414 | case POPEN_3: |
| 4415 | { |
| 4416 | char *m1, *m2; |
| 4417 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4418 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4419 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4420 | m1 = "r"; |
| 4421 | m2 = "w"; |
| 4422 | } else { |
| 4423 | m1 = "rb"; |
| 4424 | m2 = "wb"; |
| 4425 | } |
| 4426 | |
| 4427 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4428 | f1 = _fdopen(fd1, m2); |
| 4429 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4430 | f2 = _fdopen(fd2, m1); |
| 4431 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 4432 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4433 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4434 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 4435 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4436 | PyFile_SetBufSize(p1, 0); |
| 4437 | PyFile_SetBufSize(p2, 0); |
| 4438 | PyFile_SetBufSize(p3, 0); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4439 | f = PyTuple_Pack(3,p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4440 | Py_XDECREF(p1); |
| 4441 | Py_XDECREF(p2); |
| 4442 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4443 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4444 | break; |
| 4445 | } |
| 4446 | } |
| 4447 | |
| 4448 | if (n == POPEN_4) { |
| 4449 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4450 | hChildStdinRd, |
| 4451 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4452 | hChildStdoutWr, |
| 4453 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4454 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4455 | } |
| 4456 | else { |
| 4457 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4458 | hChildStdinRd, |
| 4459 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4460 | hChildStderrWr, |
| 4461 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4462 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4465 | /* |
| 4466 | * Insert the files we've created into the process dictionary |
| 4467 | * all referencing the list with the process handle and the |
| 4468 | * initial number of files (see description below in _PyPclose). |
| 4469 | * Since if _PyPclose later tried to wait on a process when all |
| 4470 | * handles weren't closed, it could create a deadlock with the |
| 4471 | * child, we spend some energy here to try to ensure that we |
| 4472 | * either insert all file handles into the dictionary or none |
| 4473 | * at all. It's a little clumsy with the various popen modes |
| 4474 | * and variable number of files involved. |
| 4475 | */ |
| 4476 | if (!_PyPopenProcs) { |
| 4477 | _PyPopenProcs = PyDict_New(); |
| 4478 | } |
| 4479 | |
| 4480 | if (_PyPopenProcs) { |
| 4481 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 4482 | int ins_rc[3]; |
| 4483 | |
| 4484 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4485 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4486 | |
| 4487 | procObj = PyList_New(2); |
| 4488 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 4489 | intObj = PyInt_FromLong(file_count); |
| 4490 | |
| 4491 | if (procObj && hProcessObj && intObj) { |
| 4492 | PyList_SetItem(procObj,0,hProcessObj); |
| 4493 | PyList_SetItem(procObj,1,intObj); |
| 4494 | |
| 4495 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 4496 | if (fileObj[0]) { |
| 4497 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4498 | fileObj[0], |
| 4499 | procObj); |
| 4500 | } |
| 4501 | if (file_count >= 2) { |
| 4502 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 4503 | if (fileObj[1]) { |
| 4504 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4505 | fileObj[1], |
| 4506 | procObj); |
| 4507 | } |
| 4508 | } |
| 4509 | if (file_count >= 3) { |
| 4510 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 4511 | if (fileObj[2]) { |
| 4512 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4513 | fileObj[2], |
| 4514 | procObj); |
| 4515 | } |
| 4516 | } |
| 4517 | |
| 4518 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4519 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4520 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 4521 | /* Something failed - remove any dictionary |
| 4522 | * entries that did make it. |
| 4523 | */ |
| 4524 | if (!ins_rc[0] && fileObj[0]) { |
| 4525 | PyDict_DelItem(_PyPopenProcs, |
| 4526 | fileObj[0]); |
| 4527 | } |
| 4528 | if (!ins_rc[1] && fileObj[1]) { |
| 4529 | PyDict_DelItem(_PyPopenProcs, |
| 4530 | fileObj[1]); |
| 4531 | } |
| 4532 | if (!ins_rc[2] && fileObj[2]) { |
| 4533 | PyDict_DelItem(_PyPopenProcs, |
| 4534 | fileObj[2]); |
| 4535 | } |
| 4536 | } |
| 4537 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4538 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4539 | /* |
| 4540 | * Clean up our localized references for the dictionary keys |
| 4541 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4542 | * that got placed in the dictionary. |
| 4543 | */ |
| 4544 | Py_XDECREF(procObj); |
| 4545 | Py_XDECREF(fileObj[0]); |
| 4546 | Py_XDECREF(fileObj[1]); |
| 4547 | Py_XDECREF(fileObj[2]); |
| 4548 | } |
| 4549 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4550 | /* Child is launched. Close the parents copy of those pipe |
| 4551 | * handles that only the child should have open. You need to |
| 4552 | * make sure that no handles to the write end of the output pipe |
| 4553 | * are maintained in this process or else the pipe will not close |
| 4554 | * when the child process exits and the ReadFile will hang. */ |
| 4555 | |
| 4556 | if (!CloseHandle(hChildStdinRd)) |
| 4557 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4558 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4559 | if (!CloseHandle(hChildStdoutWr)) |
| 4560 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4561 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4562 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 4563 | return win32_error("CloseHandle", NULL); |
| 4564 | |
| 4565 | return f; |
| 4566 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4567 | |
| 4568 | /* |
| 4569 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4570 | * 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] | 4571 | * |
| 4572 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4573 | * input file pointer to information about the process that was |
| 4574 | * originally created by the popen* call that created the file pointer. |
| 4575 | * The dictionary uses the file pointer as a key (with one entry |
| 4576 | * inserted for each file returned by the original popen* call) and a |
| 4577 | * single list object as the value for all files from a single call. |
| 4578 | * The list object contains the Win32 process handle at [0], and a file |
| 4579 | * count at [1], which is initialized to the total number of file |
| 4580 | * handles using that list. |
| 4581 | * |
| 4582 | * This function closes whichever handle it is passed, and decrements |
| 4583 | * the file count in the dictionary for the process handle pointed to |
| 4584 | * by this file. On the last close (when the file count reaches zero), |
| 4585 | * this function will wait for the child process and then return its |
| 4586 | * exit code as the result of the close() operation. This permits the |
| 4587 | * files to be closed in any order - it is always the close() of the |
| 4588 | * final handle that will return the exit code. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4589 | * |
| 4590 | * NOTE: This function is currently called with the GIL released. |
| 4591 | * hence we use the GILState API to manage our state. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4592 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4593 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4594 | static int _PyPclose(FILE *file) |
| 4595 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4596 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4597 | DWORD exit_code; |
| 4598 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4599 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 4600 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4601 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4602 | PyGILState_STATE state; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4603 | #endif |
| 4604 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4605 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4606 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4607 | */ |
| 4608 | result = fclose(file); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4609 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4610 | state = PyGILState_Ensure(); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4611 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4612 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4613 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4614 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4615 | fileObj)) != NULL && |
| 4616 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 4617 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 4618 | |
| 4619 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 4620 | file_count = PyInt_AsLong(intObj); |
| 4621 | |
| 4622 | if (file_count > 1) { |
| 4623 | /* Still other files referencing process */ |
| 4624 | file_count--; |
| 4625 | PyList_SetItem(procObj,1, |
| 4626 | PyInt_FromLong(file_count)); |
| 4627 | } else { |
| 4628 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4629 | if (result != EOF && |
| 4630 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 4631 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4632 | /* Possible truncation here in 16-bit environments, but |
| 4633 | * real exit codes are just the lower byte in any event. |
| 4634 | */ |
| 4635 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4636 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4637 | /* Indicate failure - this will cause the file object |
| 4638 | * to raise an I/O error and translate the last Win32 |
| 4639 | * error code from errno. We do have a problem with |
| 4640 | * last errors that overlap the normal errno table, |
| 4641 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4642 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4643 | if (result != EOF) { |
| 4644 | /* If the error wasn't from the fclose(), then |
| 4645 | * set errno for the file object error handling. |
| 4646 | */ |
| 4647 | errno = GetLastError(); |
| 4648 | } |
| 4649 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4650 | } |
| 4651 | |
| 4652 | /* Free up the native handle at this point */ |
| 4653 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4654 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4655 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4656 | /* Remove this file pointer from dictionary */ |
| 4657 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4658 | |
| 4659 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 4660 | Py_DECREF(_PyPopenProcs); |
| 4661 | _PyPopenProcs = NULL; |
| 4662 | } |
| 4663 | |
| 4664 | } /* if object retrieval ok */ |
| 4665 | |
| 4666 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4667 | } /* if _PyPopenProcs */ |
| 4668 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4669 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4670 | PyGILState_Release(state); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4671 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4672 | return result; |
| 4673 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4674 | |
| 4675 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4676 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4677 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4678 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4679 | char *name; |
| 4680 | char *mode = "r"; |
| 4681 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4682 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4683 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4684 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4685 | return NULL; |
Martin v. Löwis | 9ad853b | 2003-10-31 10:01:53 +0000 | [diff] [blame] | 4686 | /* Strip mode of binary or text modifiers */ |
| 4687 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) |
| 4688 | mode = "r"; |
| 4689 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) |
| 4690 | mode = "w"; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4691 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4692 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4693 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4694 | if (fp == NULL) |
| 4695 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4696 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4697 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4698 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4699 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4700 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4701 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4702 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4703 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4704 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4705 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4706 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4707 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4708 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4709 | Set the current process's user id."); |
| 4710 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4711 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4712 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4713 | { |
| 4714 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4715 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4716 | return NULL; |
| 4717 | if (setuid(uid) < 0) |
| 4718 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4719 | Py_INCREF(Py_None); |
| 4720 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4721 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4722 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4723 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4724 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4725 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4726 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4727 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4728 | Set the current process's effective user id."); |
| 4729 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4730 | static PyObject * |
| 4731 | posix_seteuid (PyObject *self, PyObject *args) |
| 4732 | { |
| 4733 | int euid; |
| 4734 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 4735 | return NULL; |
| 4736 | } else if (seteuid(euid) < 0) { |
| 4737 | return posix_error(); |
| 4738 | } else { |
| 4739 | Py_INCREF(Py_None); |
| 4740 | return Py_None; |
| 4741 | } |
| 4742 | } |
| 4743 | #endif /* HAVE_SETEUID */ |
| 4744 | |
| 4745 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4746 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4747 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4748 | Set the current process's effective group id."); |
| 4749 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4750 | static PyObject * |
| 4751 | posix_setegid (PyObject *self, PyObject *args) |
| 4752 | { |
| 4753 | int egid; |
| 4754 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 4755 | return NULL; |
| 4756 | } else if (setegid(egid) < 0) { |
| 4757 | return posix_error(); |
| 4758 | } else { |
| 4759 | Py_INCREF(Py_None); |
| 4760 | return Py_None; |
| 4761 | } |
| 4762 | } |
| 4763 | #endif /* HAVE_SETEGID */ |
| 4764 | |
| 4765 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4766 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4767 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4768 | Set the current process's real and effective user ids."); |
| 4769 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4770 | static PyObject * |
| 4771 | posix_setreuid (PyObject *self, PyObject *args) |
| 4772 | { |
| 4773 | int ruid, euid; |
| 4774 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4775 | return NULL; |
| 4776 | } else if (setreuid(ruid, euid) < 0) { |
| 4777 | return posix_error(); |
| 4778 | } else { |
| 4779 | Py_INCREF(Py_None); |
| 4780 | return Py_None; |
| 4781 | } |
| 4782 | } |
| 4783 | #endif /* HAVE_SETREUID */ |
| 4784 | |
| 4785 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4786 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4787 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4788 | Set the current process's real and effective group ids."); |
| 4789 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4790 | static PyObject * |
| 4791 | posix_setregid (PyObject *self, PyObject *args) |
| 4792 | { |
| 4793 | int rgid, egid; |
| 4794 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 4795 | return NULL; |
| 4796 | } else if (setregid(rgid, egid) < 0) { |
| 4797 | return posix_error(); |
| 4798 | } else { |
| 4799 | Py_INCREF(Py_None); |
| 4800 | return Py_None; |
| 4801 | } |
| 4802 | } |
| 4803 | #endif /* HAVE_SETREGID */ |
| 4804 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4805 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4806 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4807 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4808 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4809 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4810 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4811 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4812 | { |
| 4813 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4814 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4815 | return NULL; |
| 4816 | if (setgid(gid) < 0) |
| 4817 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4818 | Py_INCREF(Py_None); |
| 4819 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4820 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4821 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4822 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4823 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4824 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4825 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4826 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4827 | |
| 4828 | static PyObject * |
| 4829 | posix_setgroups(PyObject *self, PyObject *args) |
| 4830 | { |
| 4831 | PyObject *groups; |
| 4832 | int i, len; |
| 4833 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4834 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4835 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 4836 | return NULL; |
| 4837 | if (!PySequence_Check(groups)) { |
| 4838 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4839 | return NULL; |
| 4840 | } |
| 4841 | len = PySequence_Size(groups); |
| 4842 | if (len > MAX_GROUPS) { |
| 4843 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4844 | return NULL; |
| 4845 | } |
| 4846 | for(i = 0; i < len; i++) { |
| 4847 | PyObject *elem; |
| 4848 | elem = PySequence_GetItem(groups, i); |
| 4849 | if (!elem) |
| 4850 | return NULL; |
| 4851 | if (!PyInt_Check(elem)) { |
| 4852 | PyErr_SetString(PyExc_TypeError, |
| 4853 | "groups must be integers"); |
| 4854 | Py_DECREF(elem); |
| 4855 | return NULL; |
| 4856 | } |
| 4857 | /* XXX: check that value fits into gid_t. */ |
| 4858 | grouplist[i] = PyInt_AsLong(elem); |
| 4859 | Py_DECREF(elem); |
| 4860 | } |
| 4861 | |
| 4862 | if (setgroups(len, grouplist) < 0) |
| 4863 | return posix_error(); |
| 4864 | Py_INCREF(Py_None); |
| 4865 | return Py_None; |
| 4866 | } |
| 4867 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4868 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4869 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4870 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4871 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4872 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4873 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4874 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4875 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4876 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4877 | int pid, options; |
| 4878 | #ifdef UNION_WAIT |
| 4879 | union wait status; |
| 4880 | #define status_i (status.w_status) |
| 4881 | #else |
| 4882 | int status; |
| 4883 | #define status_i status |
| 4884 | #endif |
| 4885 | status_i = 0; |
| 4886 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4887 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4888 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4889 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4890 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4891 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4892 | if (pid == -1) |
| 4893 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4894 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4895 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4896 | } |
| 4897 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4898 | #elif defined(HAVE_CWAIT) |
| 4899 | |
| 4900 | /* 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] | 4901 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4902 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4903 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4904 | |
| 4905 | static PyObject * |
| 4906 | posix_waitpid(PyObject *self, PyObject *args) |
| 4907 | { |
| 4908 | int pid, options; |
| 4909 | int status; |
| 4910 | |
| 4911 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4912 | return NULL; |
| 4913 | Py_BEGIN_ALLOW_THREADS |
| 4914 | pid = _cwait(&status, pid, options); |
| 4915 | Py_END_ALLOW_THREADS |
| 4916 | if (pid == -1) |
| 4917 | return posix_error(); |
| 4918 | else |
| 4919 | /* shift the status left a byte so this is more like the |
| 4920 | POSIX waitpid */ |
| 4921 | return Py_BuildValue("ii", pid, status << 8); |
| 4922 | } |
| 4923 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4924 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4925 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4926 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4927 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4928 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4929 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4930 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4931 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4932 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4933 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4934 | #ifdef UNION_WAIT |
| 4935 | union wait status; |
| 4936 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4937 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4938 | int status; |
| 4939 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4940 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4941 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4942 | status_i = 0; |
| 4943 | Py_BEGIN_ALLOW_THREADS |
| 4944 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4945 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4946 | if (pid == -1) |
| 4947 | return posix_error(); |
| 4948 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4949 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4950 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4951 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4952 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4953 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4954 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4955 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4956 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4957 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4958 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4959 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4960 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4961 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4962 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4963 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4964 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4965 | #ifdef MS_WINDOWS |
Mark Hammond | 7edd0a9 | 2003-08-06 02:46:58 +0000 | [diff] [blame] | 4966 | return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", _wstati64); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4967 | #else |
| 4968 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 4969 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4970 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4971 | } |
| 4972 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4973 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4974 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4975 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4976 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4977 | 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] | 4978 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4979 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4980 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4981 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4982 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4983 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4984 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4985 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4986 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4987 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4988 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4989 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4990 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 4991 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4992 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4993 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4994 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4995 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4996 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4997 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4998 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4999 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 5000 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5001 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5002 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5003 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5004 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5005 | return posix_2str(args, "etet:symlink", symlink, NULL, NULL); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5006 | } |
| 5007 | #endif /* HAVE_SYMLINK */ |
| 5008 | |
| 5009 | |
| 5010 | #ifdef HAVE_TIMES |
| 5011 | #ifndef HZ |
| 5012 | #define HZ 60 /* Universal constant :-) */ |
| 5013 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5014 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5015 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 5016 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5017 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5018 | { |
| 5019 | ULONG value = 0; |
| 5020 | |
| 5021 | Py_BEGIN_ALLOW_THREADS |
| 5022 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 5023 | Py_END_ALLOW_THREADS |
| 5024 | |
| 5025 | return value; |
| 5026 | } |
| 5027 | |
| 5028 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5029 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5030 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5031 | /* Currently Only Uptime is Provided -- Others Later */ |
| 5032 | return Py_BuildValue("ddddd", |
| 5033 | (double)0 /* t.tms_utime / HZ */, |
| 5034 | (double)0 /* t.tms_stime / HZ */, |
| 5035 | (double)0 /* t.tms_cutime / HZ */, |
| 5036 | (double)0 /* t.tms_cstime / HZ */, |
| 5037 | (double)system_uptime() / 1000); |
| 5038 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5039 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5040 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5041 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5042 | { |
| 5043 | struct tms t; |
| 5044 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5045 | errno = 0; |
| 5046 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5047 | if (c == (clock_t) -1) |
| 5048 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5049 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5050 | (double)t.tms_utime / HZ, |
| 5051 | (double)t.tms_stime / HZ, |
| 5052 | (double)t.tms_cutime / HZ, |
| 5053 | (double)t.tms_cstime / HZ, |
| 5054 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5055 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5056 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5057 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5058 | |
| 5059 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5060 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5061 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5062 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5063 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5064 | { |
| 5065 | FILETIME create, exit, kernel, user; |
| 5066 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5067 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5068 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 5069 | /* The fields of a FILETIME structure are the hi and lo part |
| 5070 | of a 64-bit value expressed in 100 nanosecond units. |
| 5071 | 1e7 is one second in such units; 1e-7 the inverse. |
| 5072 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 5073 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5074 | return Py_BuildValue( |
| 5075 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5076 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 5077 | kernel.dwLowDateTime*1e-7), |
| 5078 | (double)(user.dwHighDateTime*429.4967296 + |
| 5079 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5080 | (double)0, |
| 5081 | (double)0, |
| 5082 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5083 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5084 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5085 | |
| 5086 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5087 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5088 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5089 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5090 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5091 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5092 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5093 | #ifdef HAVE_GETSID |
| 5094 | PyDoc_STRVAR(posix_getsid__doc__, |
| 5095 | "getsid(pid) -> sid\n\n\ |
| 5096 | Call the system call getsid()."); |
| 5097 | |
| 5098 | static PyObject * |
| 5099 | posix_getsid(PyObject *self, PyObject *args) |
| 5100 | { |
| 5101 | int pid, sid; |
| 5102 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 5103 | return NULL; |
| 5104 | sid = getsid(pid); |
| 5105 | if (sid < 0) |
| 5106 | return posix_error(); |
| 5107 | return PyInt_FromLong((long)sid); |
| 5108 | } |
| 5109 | #endif /* HAVE_GETSID */ |
| 5110 | |
| 5111 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5112 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5113 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5114 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5115 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5116 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5117 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5118 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5119 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5120 | if (setsid() < 0) |
| 5121 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5122 | Py_INCREF(Py_None); |
| 5123 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5124 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5125 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5126 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5127 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5128 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5129 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5130 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5131 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5132 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5133 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5134 | { |
| 5135 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5136 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5137 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5138 | if (setpgid(pid, pgrp) < 0) |
| 5139 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5140 | Py_INCREF(Py_None); |
| 5141 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5142 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5143 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5144 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5145 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5146 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5147 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5148 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5149 | 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] | 5150 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5151 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5152 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5153 | { |
| 5154 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5155 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5156 | return NULL; |
| 5157 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5158 | if (pgid < 0) |
| 5159 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5160 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5161 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5162 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5163 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5164 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5165 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5166 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5167 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5168 | 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] | 5169 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5170 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5171 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5172 | { |
| 5173 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5174 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5175 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5176 | if (tcsetpgrp(fd, pgid) < 0) |
| 5177 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5178 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5179 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5180 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5181 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5182 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5183 | /* Functions acting on file descriptors */ |
| 5184 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5185 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5186 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5187 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5188 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5189 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5190 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5191 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5192 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5193 | int flag; |
| 5194 | int mode = 0777; |
| 5195 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5196 | |
| 5197 | #ifdef MS_WINDOWS |
| 5198 | if (unicode_file_names()) { |
| 5199 | PyUnicodeObject *po; |
| 5200 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 5201 | Py_BEGIN_ALLOW_THREADS |
| 5202 | /* PyUnicode_AS_UNICODE OK without thread |
| 5203 | lock as it is a simple dereference. */ |
| 5204 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 5205 | Py_END_ALLOW_THREADS |
| 5206 | if (fd < 0) |
| 5207 | return posix_error(); |
| 5208 | return PyInt_FromLong((long)fd); |
| 5209 | } |
| 5210 | /* Drop the argument parsing error as narrow strings |
| 5211 | are also valid. */ |
| 5212 | PyErr_Clear(); |
| 5213 | } |
| 5214 | #endif |
| 5215 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5216 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5217 | Py_FileSystemDefaultEncoding, &file, |
| 5218 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5219 | return NULL; |
| 5220 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5221 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5222 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5223 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5224 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5225 | return posix_error_with_allocated_filename(file); |
| 5226 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5227 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5228 | } |
| 5229 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5230 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5231 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5232 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5233 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5234 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5235 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5236 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5237 | { |
| 5238 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5239 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5240 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5241 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5242 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5243 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5244 | if (res < 0) |
| 5245 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5246 | Py_INCREF(Py_None); |
| 5247 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5248 | } |
| 5249 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5250 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5251 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5252 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5253 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5254 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5255 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5256 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5257 | { |
| 5258 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5259 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5260 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5261 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5262 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5263 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5264 | if (fd < 0) |
| 5265 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5266 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5267 | } |
| 5268 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5269 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5270 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5271 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5272 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5273 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5274 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5275 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5276 | { |
| 5277 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5278 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5279 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5280 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5281 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5282 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5283 | if (res < 0) |
| 5284 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5285 | Py_INCREF(Py_None); |
| 5286 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5287 | } |
| 5288 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5289 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5290 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5291 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5292 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5293 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5294 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5295 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5296 | { |
| 5297 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5298 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5299 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5300 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5301 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5302 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5303 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5304 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5305 | return NULL; |
| 5306 | #ifdef SEEK_SET |
| 5307 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5308 | switch (how) { |
| 5309 | case 0: how = SEEK_SET; break; |
| 5310 | case 1: how = SEEK_CUR; break; |
| 5311 | case 2: how = SEEK_END; break; |
| 5312 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5313 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5314 | |
| 5315 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5316 | pos = PyInt_AsLong(posobj); |
| 5317 | #else |
| 5318 | pos = PyLong_Check(posobj) ? |
| 5319 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 5320 | #endif |
| 5321 | if (PyErr_Occurred()) |
| 5322 | return NULL; |
| 5323 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5324 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5325 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5326 | res = _lseeki64(fd, pos, how); |
| 5327 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5328 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5329 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5330 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5331 | if (res < 0) |
| 5332 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5333 | |
| 5334 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5335 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5336 | #else |
| 5337 | return PyLong_FromLongLong(res); |
| 5338 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5339 | } |
| 5340 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5341 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5342 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5343 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5344 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5345 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5346 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5347 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5348 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5349 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5350 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5351 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5352 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 5353 | if (size < 0) { |
| 5354 | errno = EINVAL; |
| 5355 | return posix_error(); |
| 5356 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5357 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5358 | if (buffer == NULL) |
| 5359 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5360 | Py_BEGIN_ALLOW_THREADS |
| 5361 | n = read(fd, PyString_AsString(buffer), size); |
| 5362 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5363 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5364 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5365 | return posix_error(); |
| 5366 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5367 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5368 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5369 | return buffer; |
| 5370 | } |
| 5371 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5372 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5373 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5374 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5375 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5376 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5377 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5378 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5379 | { |
| 5380 | int fd, size; |
| 5381 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5382 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5383 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5384 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5385 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5386 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5387 | if (size < 0) |
| 5388 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5389 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5390 | } |
| 5391 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5392 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5393 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5394 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5395 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5396 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5397 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5398 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5399 | { |
| 5400 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5401 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5402 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5403 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5404 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5405 | #ifdef __VMS |
| 5406 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5407 | fsync(fd); |
| 5408 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5409 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5410 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5411 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5412 | if (res != 0) |
| 5413 | return posix_error(); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5414 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5415 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5416 | } |
| 5417 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5418 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5419 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5420 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5421 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5422 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5423 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5424 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5425 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5426 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5427 | char *mode = "r"; |
| 5428 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5429 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5430 | PyObject *f; |
| 5431 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5432 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5433 | |
Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 5434 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 5435 | PyErr_Format(PyExc_ValueError, |
| 5436 | "invalid file mode '%s'", mode); |
| 5437 | return NULL; |
| 5438 | } |
| 5439 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5440 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5441 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5442 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5443 | if (fp == NULL) |
| 5444 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 5445 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5446 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5447 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5448 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5449 | } |
| 5450 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5451 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5452 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5453 | 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] | 5454 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5455 | |
| 5456 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5457 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5458 | { |
| 5459 | int fd; |
| 5460 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5461 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5462 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5463 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5464 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5465 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5466 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5467 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5468 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5469 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5470 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5471 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5472 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5473 | #if defined(PYOS_OS2) |
| 5474 | HFILE read, write; |
| 5475 | APIRET rc; |
| 5476 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5477 | Py_BEGIN_ALLOW_THREADS |
| 5478 | rc = DosCreatePipe( &read, &write, 4096); |
| 5479 | Py_END_ALLOW_THREADS |
| 5480 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5481 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5482 | |
| 5483 | return Py_BuildValue("(ii)", read, write); |
| 5484 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5485 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5486 | int fds[2]; |
| 5487 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5488 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5489 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5490 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5491 | if (res != 0) |
| 5492 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5493 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5494 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5495 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5496 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5497 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5498 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5499 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5500 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5501 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5502 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5503 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5504 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5505 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5506 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5507 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5508 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5509 | #endif /* HAVE_PIPE */ |
| 5510 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5511 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5512 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5513 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5514 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5515 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5516 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5517 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5518 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5519 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5520 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5521 | int mode = 0666; |
| 5522 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5523 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5524 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5525 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5526 | res = mkfifo(filename, mode); |
| 5527 | Py_END_ALLOW_THREADS |
| 5528 | if (res < 0) |
| 5529 | return posix_error(); |
| 5530 | Py_INCREF(Py_None); |
| 5531 | return Py_None; |
| 5532 | } |
| 5533 | #endif |
| 5534 | |
| 5535 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5536 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5537 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5538 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5539 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5540 | named filename. mode specifies both the permissions to use and the\n\ |
| 5541 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5542 | 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] | 5543 | device defines the newly created device special file (probably using\n\ |
| 5544 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5545 | |
| 5546 | |
| 5547 | static PyObject * |
| 5548 | posix_mknod(PyObject *self, PyObject *args) |
| 5549 | { |
| 5550 | char *filename; |
| 5551 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5552 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5553 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5554 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5555 | return NULL; |
| 5556 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5557 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5558 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5559 | if (res < 0) |
| 5560 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5561 | Py_INCREF(Py_None); |
| 5562 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5563 | } |
| 5564 | #endif |
| 5565 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5566 | #ifdef HAVE_DEVICE_MACROS |
| 5567 | PyDoc_STRVAR(posix_major__doc__, |
| 5568 | "major(device) -> major number\n\ |
| 5569 | Extracts a device major number from a raw device number."); |
| 5570 | |
| 5571 | static PyObject * |
| 5572 | posix_major(PyObject *self, PyObject *args) |
| 5573 | { |
| 5574 | int device; |
| 5575 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5576 | return NULL; |
| 5577 | return PyInt_FromLong((long)major(device)); |
| 5578 | } |
| 5579 | |
| 5580 | PyDoc_STRVAR(posix_minor__doc__, |
| 5581 | "minor(device) -> minor number\n\ |
| 5582 | Extracts a device minor number from a raw device number."); |
| 5583 | |
| 5584 | static PyObject * |
| 5585 | posix_minor(PyObject *self, PyObject *args) |
| 5586 | { |
| 5587 | int device; |
| 5588 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5589 | return NULL; |
| 5590 | return PyInt_FromLong((long)minor(device)); |
| 5591 | } |
| 5592 | |
| 5593 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5594 | "makedev(major, minor) -> device number\n\ |
| 5595 | Composes a raw device number from the major and minor device numbers."); |
| 5596 | |
| 5597 | static PyObject * |
| 5598 | posix_makedev(PyObject *self, PyObject *args) |
| 5599 | { |
| 5600 | int major, minor; |
| 5601 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5602 | return NULL; |
| 5603 | return PyInt_FromLong((long)makedev(major, minor)); |
| 5604 | } |
| 5605 | #endif /* device macros */ |
| 5606 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5607 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5608 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5609 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5610 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5611 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5612 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5613 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5614 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5615 | { |
| 5616 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5617 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5618 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5619 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5620 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5621 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5622 | return NULL; |
| 5623 | |
| 5624 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5625 | length = PyInt_AsLong(lenobj); |
| 5626 | #else |
| 5627 | length = PyLong_Check(lenobj) ? |
| 5628 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 5629 | #endif |
| 5630 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5631 | return NULL; |
| 5632 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5633 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5634 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5635 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5636 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5637 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5638 | return NULL; |
| 5639 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5640 | Py_INCREF(Py_None); |
| 5641 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5642 | } |
| 5643 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5644 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5645 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5646 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5647 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5648 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5649 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5650 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5651 | * get re-set with another call for the same key. */ |
| 5652 | static PyObject *posix_putenv_garbage; |
| 5653 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5654 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5655 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5656 | { |
| 5657 | char *s1, *s2; |
| 5658 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5659 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5660 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5661 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5662 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5663 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5664 | |
| 5665 | #if defined(PYOS_OS2) |
| 5666 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5667 | APIRET rc; |
| 5668 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5669 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5670 | if (rc != NO_ERROR) |
| 5671 | return os2_error(rc); |
| 5672 | |
| 5673 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5674 | APIRET rc; |
| 5675 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5676 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5677 | if (rc != NO_ERROR) |
| 5678 | return os2_error(rc); |
| 5679 | } else { |
| 5680 | #endif |
| 5681 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5682 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5683 | len = strlen(s1) + strlen(s2) + 2; |
| 5684 | /* len includes space for a trailing \0; the size arg to |
| 5685 | PyString_FromStringAndSize does not count that */ |
| 5686 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5687 | if (newstr == NULL) |
| 5688 | return PyErr_NoMemory(); |
| 5689 | new = PyString_AS_STRING(newstr); |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5690 | PyOS_snprintf(new, len, "%s=%s", s1, s2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5691 | if (putenv(new)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5692 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5693 | posix_error(); |
| 5694 | return NULL; |
| 5695 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5696 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5697 | * this will cause previous value to be collected. This has to |
| 5698 | * happen after the real putenv() call because the old value |
| 5699 | * was still accessible until then. */ |
| 5700 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5701 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5702 | /* really not much we can do; just leak */ |
| 5703 | PyErr_Clear(); |
| 5704 | } |
| 5705 | else { |
| 5706 | Py_DECREF(newstr); |
| 5707 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5708 | |
| 5709 | #if defined(PYOS_OS2) |
| 5710 | } |
| 5711 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5712 | Py_INCREF(Py_None); |
| 5713 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5714 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5715 | #endif /* putenv */ |
| 5716 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5717 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5718 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5719 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5720 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5721 | |
| 5722 | static PyObject * |
| 5723 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5724 | { |
| 5725 | char *s1; |
| 5726 | |
| 5727 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5728 | return NULL; |
| 5729 | |
| 5730 | unsetenv(s1); |
| 5731 | |
| 5732 | /* Remove the key from posix_putenv_garbage; |
| 5733 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5734 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5735 | * old value was still accessible until then. |
| 5736 | */ |
| 5737 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5738 | PyTuple_GET_ITEM(args, 0))) { |
| 5739 | /* really not much we can do; just leak */ |
| 5740 | PyErr_Clear(); |
| 5741 | } |
| 5742 | |
| 5743 | Py_INCREF(Py_None); |
| 5744 | return Py_None; |
| 5745 | } |
| 5746 | #endif /* unsetenv */ |
| 5747 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5748 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5749 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5750 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5751 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5752 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5753 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5754 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5755 | { |
| 5756 | int code; |
| 5757 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5758 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5759 | return NULL; |
| 5760 | message = strerror(code); |
| 5761 | if (message == NULL) { |
| 5762 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5763 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5764 | return NULL; |
| 5765 | } |
| 5766 | return PyString_FromString(message); |
| 5767 | } |
| 5768 | #endif /* strerror */ |
| 5769 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5770 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5771 | #ifdef HAVE_SYS_WAIT_H |
| 5772 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5773 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5774 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5775 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5776 | 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] | 5777 | |
| 5778 | static PyObject * |
| 5779 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5780 | { |
| 5781 | #ifdef UNION_WAIT |
| 5782 | union wait status; |
| 5783 | #define status_i (status.w_status) |
| 5784 | #else |
| 5785 | int status; |
| 5786 | #define status_i status |
| 5787 | #endif |
| 5788 | status_i = 0; |
| 5789 | |
| 5790 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i)) |
| 5791 | { |
| 5792 | return NULL; |
| 5793 | } |
| 5794 | |
| 5795 | return PyBool_FromLong(WCOREDUMP(status)); |
| 5796 | #undef status_i |
| 5797 | } |
| 5798 | #endif /* WCOREDUMP */ |
| 5799 | |
| 5800 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5801 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5802 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5803 | 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] | 5804 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5805 | |
| 5806 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5807 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5808 | { |
| 5809 | #ifdef UNION_WAIT |
| 5810 | union wait status; |
| 5811 | #define status_i (status.w_status) |
| 5812 | #else |
| 5813 | int status; |
| 5814 | #define status_i status |
| 5815 | #endif |
| 5816 | status_i = 0; |
| 5817 | |
| 5818 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i)) |
| 5819 | { |
| 5820 | return NULL; |
| 5821 | } |
| 5822 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5823 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5824 | #undef status_i |
| 5825 | } |
| 5826 | #endif /* WIFCONTINUED */ |
| 5827 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5828 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5829 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5830 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5831 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5832 | |
| 5833 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5834 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5835 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5836 | #ifdef UNION_WAIT |
| 5837 | union wait status; |
| 5838 | #define status_i (status.w_status) |
| 5839 | #else |
| 5840 | int status; |
| 5841 | #define status_i status |
| 5842 | #endif |
| 5843 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5844 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5845 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5846 | { |
| 5847 | return NULL; |
| 5848 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5849 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5850 | return PyBool_FromLong(WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5851 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5852 | } |
| 5853 | #endif /* WIFSTOPPED */ |
| 5854 | |
| 5855 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5856 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5857 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5858 | 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] | 5859 | |
| 5860 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5861 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5862 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5863 | #ifdef UNION_WAIT |
| 5864 | union wait status; |
| 5865 | #define status_i (status.w_status) |
| 5866 | #else |
| 5867 | int status; |
| 5868 | #define status_i status |
| 5869 | #endif |
| 5870 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5871 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5872 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5873 | { |
| 5874 | return NULL; |
| 5875 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5876 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5877 | return PyBool_FromLong(WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5878 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5879 | } |
| 5880 | #endif /* WIFSIGNALED */ |
| 5881 | |
| 5882 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5883 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5884 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5885 | 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] | 5886 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5887 | |
| 5888 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5889 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5890 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5891 | #ifdef UNION_WAIT |
| 5892 | union wait status; |
| 5893 | #define status_i (status.w_status) |
| 5894 | #else |
| 5895 | int status; |
| 5896 | #define status_i status |
| 5897 | #endif |
| 5898 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5899 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5900 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5901 | { |
| 5902 | return NULL; |
| 5903 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5904 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5905 | return PyBool_FromLong(WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5906 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5907 | } |
| 5908 | #endif /* WIFEXITED */ |
| 5909 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5910 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5911 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5912 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5913 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5914 | |
| 5915 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5916 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5917 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5918 | #ifdef UNION_WAIT |
| 5919 | union wait status; |
| 5920 | #define status_i (status.w_status) |
| 5921 | #else |
| 5922 | int status; |
| 5923 | #define status_i status |
| 5924 | #endif |
| 5925 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5926 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5927 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5928 | { |
| 5929 | return NULL; |
| 5930 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5931 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5932 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5933 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5934 | } |
| 5935 | #endif /* WEXITSTATUS */ |
| 5936 | |
| 5937 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5938 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5939 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5940 | 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] | 5941 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5942 | |
| 5943 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5944 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5945 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5946 | #ifdef UNION_WAIT |
| 5947 | union wait status; |
| 5948 | #define status_i (status.w_status) |
| 5949 | #else |
| 5950 | int status; |
| 5951 | #define status_i status |
| 5952 | #endif |
| 5953 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5954 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5955 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5956 | { |
| 5957 | return NULL; |
| 5958 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5959 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5960 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5961 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5962 | } |
| 5963 | #endif /* WTERMSIG */ |
| 5964 | |
| 5965 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5966 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5967 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5968 | Return the signal that stopped the process that provided\n\ |
| 5969 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5970 | |
| 5971 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5972 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5973 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5974 | #ifdef UNION_WAIT |
| 5975 | union wait status; |
| 5976 | #define status_i (status.w_status) |
| 5977 | #else |
| 5978 | int status; |
| 5979 | #define status_i status |
| 5980 | #endif |
| 5981 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5982 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5983 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5984 | { |
| 5985 | return NULL; |
| 5986 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5987 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5988 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5989 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5990 | } |
| 5991 | #endif /* WSTOPSIG */ |
| 5992 | |
| 5993 | #endif /* HAVE_SYS_WAIT_H */ |
| 5994 | |
| 5995 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5996 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5997 | #ifdef _SCO_DS |
| 5998 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5999 | needed definitions in sys/statvfs.h */ |
| 6000 | #define _SVID3 |
| 6001 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6002 | #include <sys/statvfs.h> |
| 6003 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6004 | static PyObject* |
| 6005 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 6006 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 6007 | if (v == NULL) |
| 6008 | return NULL; |
| 6009 | |
| 6010 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6011 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6012 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 6013 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 6014 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 6015 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 6016 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 6017 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 6018 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 6019 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6020 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6021 | #else |
| 6022 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6023 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6024 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6025 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6026 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6027 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6028 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6029 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6030 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6031 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6032 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6033 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6034 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6035 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6036 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6037 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6038 | #endif |
| 6039 | |
| 6040 | return v; |
| 6041 | } |
| 6042 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6043 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6044 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6045 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6046 | |
| 6047 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6048 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6049 | { |
| 6050 | int fd, res; |
| 6051 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6052 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6053 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6054 | return NULL; |
| 6055 | Py_BEGIN_ALLOW_THREADS |
| 6056 | res = fstatvfs(fd, &st); |
| 6057 | Py_END_ALLOW_THREADS |
| 6058 | if (res != 0) |
| 6059 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6060 | |
| 6061 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6062 | } |
| 6063 | #endif /* HAVE_FSTATVFS */ |
| 6064 | |
| 6065 | |
| 6066 | #if defined(HAVE_STATVFS) |
| 6067 | #include <sys/statvfs.h> |
| 6068 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6069 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6070 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6071 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6072 | |
| 6073 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6074 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6075 | { |
| 6076 | char *path; |
| 6077 | int res; |
| 6078 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6079 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6080 | return NULL; |
| 6081 | Py_BEGIN_ALLOW_THREADS |
| 6082 | res = statvfs(path, &st); |
| 6083 | Py_END_ALLOW_THREADS |
| 6084 | if (res != 0) |
| 6085 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6086 | |
| 6087 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6088 | } |
| 6089 | #endif /* HAVE_STATVFS */ |
| 6090 | |
| 6091 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6092 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6093 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6094 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6095 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 6096 | 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] | 6097 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6098 | |
| 6099 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6100 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6101 | { |
| 6102 | PyObject *result = NULL; |
| 6103 | char *dir = NULL; |
| 6104 | char *pfx = NULL; |
| 6105 | char *name; |
| 6106 | |
| 6107 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 6108 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6109 | |
| 6110 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6111 | "tempnam is a potential security risk to your program") < 0) |
| 6112 | return NULL; |
| 6113 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6114 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6115 | name = _tempnam(dir, pfx); |
| 6116 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6117 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6118 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6119 | if (name == NULL) |
| 6120 | return PyErr_NoMemory(); |
| 6121 | result = PyString_FromString(name); |
| 6122 | free(name); |
| 6123 | return result; |
| 6124 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 6125 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6126 | |
| 6127 | |
| 6128 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6129 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6130 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6131 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6132 | |
| 6133 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6134 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6135 | { |
| 6136 | FILE *fp; |
| 6137 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6138 | fp = tmpfile(); |
| 6139 | if (fp == NULL) |
| 6140 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 6141 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6142 | } |
| 6143 | #endif |
| 6144 | |
| 6145 | |
| 6146 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6147 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6148 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6149 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6150 | |
| 6151 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6152 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6153 | { |
| 6154 | char buffer[L_tmpnam]; |
| 6155 | char *name; |
| 6156 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6157 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6158 | "tmpnam is a potential security risk to your program") < 0) |
| 6159 | return NULL; |
| 6160 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6161 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6162 | name = tmpnam_r(buffer); |
| 6163 | #else |
| 6164 | name = tmpnam(buffer); |
| 6165 | #endif |
| 6166 | if (name == NULL) { |
| 6167 | PyErr_SetObject(PyExc_OSError, |
| 6168 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6169 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6170 | "unexpected NULL from tmpnam_r" |
| 6171 | #else |
| 6172 | "unexpected NULL from tmpnam" |
| 6173 | #endif |
| 6174 | )); |
| 6175 | return NULL; |
| 6176 | } |
| 6177 | return PyString_FromString(buffer); |
| 6178 | } |
| 6179 | #endif |
| 6180 | |
| 6181 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6182 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 6183 | * It maps strings representing configuration variable names to |
| 6184 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6185 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6186 | * rarely-used constants. There are three separate tables that use |
| 6187 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6188 | * |
| 6189 | * This code is always included, even if none of the interfaces that |
| 6190 | * need it are included. The #if hackery needed to avoid it would be |
| 6191 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6192 | */ |
| 6193 | struct constdef { |
| 6194 | char *name; |
| 6195 | long value; |
| 6196 | }; |
| 6197 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6198 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6199 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 6200 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6201 | { |
| 6202 | if (PyInt_Check(arg)) { |
| 6203 | *valuep = PyInt_AS_LONG(arg); |
| 6204 | return 1; |
| 6205 | } |
| 6206 | if (PyString_Check(arg)) { |
| 6207 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6208 | size_t lo = 0; |
| 6209 | size_t mid; |
| 6210 | size_t hi = tablesize; |
| 6211 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6212 | char *confname = PyString_AS_STRING(arg); |
| 6213 | while (lo < hi) { |
| 6214 | mid = (lo + hi) / 2; |
| 6215 | cmp = strcmp(confname, table[mid].name); |
| 6216 | if (cmp < 0) |
| 6217 | hi = mid; |
| 6218 | else if (cmp > 0) |
| 6219 | lo = mid + 1; |
| 6220 | else { |
| 6221 | *valuep = table[mid].value; |
| 6222 | return 1; |
| 6223 | } |
| 6224 | } |
| 6225 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 6226 | } |
| 6227 | else |
| 6228 | PyErr_SetString(PyExc_TypeError, |
| 6229 | "configuration names must be strings or integers"); |
| 6230 | return 0; |
| 6231 | } |
| 6232 | |
| 6233 | |
| 6234 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 6235 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6236 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 6237 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 6238 | #endif |
| 6239 | #ifdef _PC_ABI_ASYNC_IO |
| 6240 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 6241 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6242 | #ifdef _PC_ASYNC_IO |
| 6243 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 6244 | #endif |
| 6245 | #ifdef _PC_CHOWN_RESTRICTED |
| 6246 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 6247 | #endif |
| 6248 | #ifdef _PC_FILESIZEBITS |
| 6249 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 6250 | #endif |
| 6251 | #ifdef _PC_LAST |
| 6252 | {"PC_LAST", _PC_LAST}, |
| 6253 | #endif |
| 6254 | #ifdef _PC_LINK_MAX |
| 6255 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 6256 | #endif |
| 6257 | #ifdef _PC_MAX_CANON |
| 6258 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 6259 | #endif |
| 6260 | #ifdef _PC_MAX_INPUT |
| 6261 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 6262 | #endif |
| 6263 | #ifdef _PC_NAME_MAX |
| 6264 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 6265 | #endif |
| 6266 | #ifdef _PC_NO_TRUNC |
| 6267 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 6268 | #endif |
| 6269 | #ifdef _PC_PATH_MAX |
| 6270 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 6271 | #endif |
| 6272 | #ifdef _PC_PIPE_BUF |
| 6273 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 6274 | #endif |
| 6275 | #ifdef _PC_PRIO_IO |
| 6276 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 6277 | #endif |
| 6278 | #ifdef _PC_SOCK_MAXBUF |
| 6279 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 6280 | #endif |
| 6281 | #ifdef _PC_SYNC_IO |
| 6282 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 6283 | #endif |
| 6284 | #ifdef _PC_VDISABLE |
| 6285 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 6286 | #endif |
| 6287 | }; |
| 6288 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6289 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6290 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6291 | { |
| 6292 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 6293 | sizeof(posix_constants_pathconf) |
| 6294 | / sizeof(struct constdef)); |
| 6295 | } |
| 6296 | #endif |
| 6297 | |
| 6298 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6299 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6300 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6301 | 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] | 6302 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6303 | |
| 6304 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6305 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6306 | { |
| 6307 | PyObject *result = NULL; |
| 6308 | int name, fd; |
| 6309 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6310 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 6311 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6312 | long limit; |
| 6313 | |
| 6314 | errno = 0; |
| 6315 | limit = fpathconf(fd, name); |
| 6316 | if (limit == -1 && errno != 0) |
| 6317 | posix_error(); |
| 6318 | else |
| 6319 | result = PyInt_FromLong(limit); |
| 6320 | } |
| 6321 | return result; |
| 6322 | } |
| 6323 | #endif |
| 6324 | |
| 6325 | |
| 6326 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6327 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6328 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6329 | 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] | 6330 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6331 | |
| 6332 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6333 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6334 | { |
| 6335 | PyObject *result = NULL; |
| 6336 | int name; |
| 6337 | char *path; |
| 6338 | |
| 6339 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 6340 | conv_path_confname, &name)) { |
| 6341 | long limit; |
| 6342 | |
| 6343 | errno = 0; |
| 6344 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6345 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6346 | if (errno == EINVAL) |
| 6347 | /* could be a path or name problem */ |
| 6348 | posix_error(); |
| 6349 | else |
| 6350 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6351 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6352 | else |
| 6353 | result = PyInt_FromLong(limit); |
| 6354 | } |
| 6355 | return result; |
| 6356 | } |
| 6357 | #endif |
| 6358 | |
| 6359 | #ifdef HAVE_CONFSTR |
| 6360 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6361 | #ifdef _CS_ARCHITECTURE |
| 6362 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 6363 | #endif |
| 6364 | #ifdef _CS_HOSTNAME |
| 6365 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 6366 | #endif |
| 6367 | #ifdef _CS_HW_PROVIDER |
| 6368 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 6369 | #endif |
| 6370 | #ifdef _CS_HW_SERIAL |
| 6371 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 6372 | #endif |
| 6373 | #ifdef _CS_INITTAB_NAME |
| 6374 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 6375 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6376 | #ifdef _CS_LFS64_CFLAGS |
| 6377 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 6378 | #endif |
| 6379 | #ifdef _CS_LFS64_LDFLAGS |
| 6380 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 6381 | #endif |
| 6382 | #ifdef _CS_LFS64_LIBS |
| 6383 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 6384 | #endif |
| 6385 | #ifdef _CS_LFS64_LINTFLAGS |
| 6386 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 6387 | #endif |
| 6388 | #ifdef _CS_LFS_CFLAGS |
| 6389 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 6390 | #endif |
| 6391 | #ifdef _CS_LFS_LDFLAGS |
| 6392 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6393 | #endif |
| 6394 | #ifdef _CS_LFS_LIBS |
| 6395 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6396 | #endif |
| 6397 | #ifdef _CS_LFS_LINTFLAGS |
| 6398 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6399 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6400 | #ifdef _CS_MACHINE |
| 6401 | {"CS_MACHINE", _CS_MACHINE}, |
| 6402 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6403 | #ifdef _CS_PATH |
| 6404 | {"CS_PATH", _CS_PATH}, |
| 6405 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6406 | #ifdef _CS_RELEASE |
| 6407 | {"CS_RELEASE", _CS_RELEASE}, |
| 6408 | #endif |
| 6409 | #ifdef _CS_SRPC_DOMAIN |
| 6410 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6411 | #endif |
| 6412 | #ifdef _CS_SYSNAME |
| 6413 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6414 | #endif |
| 6415 | #ifdef _CS_VERSION |
| 6416 | {"CS_VERSION", _CS_VERSION}, |
| 6417 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6418 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6419 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6420 | #endif |
| 6421 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6422 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6423 | #endif |
| 6424 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6425 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6426 | #endif |
| 6427 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6428 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6429 | #endif |
| 6430 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6431 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6432 | #endif |
| 6433 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6434 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6435 | #endif |
| 6436 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6437 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6438 | #endif |
| 6439 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6440 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6441 | #endif |
| 6442 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6443 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6444 | #endif |
| 6445 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6446 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6447 | #endif |
| 6448 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6449 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6450 | #endif |
| 6451 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6452 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6453 | #endif |
| 6454 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6455 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6456 | #endif |
| 6457 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6458 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6459 | #endif |
| 6460 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6461 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6462 | #endif |
| 6463 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6464 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6465 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6466 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6467 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6468 | #endif |
| 6469 | #ifdef _MIPS_CS_BASE |
| 6470 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6471 | #endif |
| 6472 | #ifdef _MIPS_CS_HOSTID |
| 6473 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6474 | #endif |
| 6475 | #ifdef _MIPS_CS_HW_NAME |
| 6476 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6477 | #endif |
| 6478 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6479 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6480 | #endif |
| 6481 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6482 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6483 | #endif |
| 6484 | #ifdef _MIPS_CS_OSREL_MIN |
| 6485 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6486 | #endif |
| 6487 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6488 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6489 | #endif |
| 6490 | #ifdef _MIPS_CS_OS_NAME |
| 6491 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6492 | #endif |
| 6493 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6494 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6495 | #endif |
| 6496 | #ifdef _MIPS_CS_PROCESSORS |
| 6497 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6498 | #endif |
| 6499 | #ifdef _MIPS_CS_SERIAL |
| 6500 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6501 | #endif |
| 6502 | #ifdef _MIPS_CS_VENDOR |
| 6503 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6504 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6505 | }; |
| 6506 | |
| 6507 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6508 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6509 | { |
| 6510 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6511 | sizeof(posix_constants_confstr) |
| 6512 | / sizeof(struct constdef)); |
| 6513 | } |
| 6514 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6515 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6516 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6517 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6518 | |
| 6519 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6520 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6521 | { |
| 6522 | PyObject *result = NULL; |
| 6523 | int name; |
| 6524 | char buffer[64]; |
| 6525 | |
| 6526 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 6527 | int len = confstr(name, buffer, sizeof(buffer)); |
| 6528 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6529 | errno = 0; |
| 6530 | if (len == 0) { |
| 6531 | if (errno != 0) |
| 6532 | posix_error(); |
| 6533 | else |
| 6534 | result = PyString_FromString(""); |
| 6535 | } |
| 6536 | else { |
| 6537 | if (len >= sizeof(buffer)) { |
| 6538 | result = PyString_FromStringAndSize(NULL, len); |
| 6539 | if (result != NULL) |
| 6540 | confstr(name, PyString_AS_STRING(result), len+1); |
| 6541 | } |
| 6542 | else |
| 6543 | result = PyString_FromString(buffer); |
| 6544 | } |
| 6545 | } |
| 6546 | return result; |
| 6547 | } |
| 6548 | #endif |
| 6549 | |
| 6550 | |
| 6551 | #ifdef HAVE_SYSCONF |
| 6552 | static struct constdef posix_constants_sysconf[] = { |
| 6553 | #ifdef _SC_2_CHAR_TERM |
| 6554 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6555 | #endif |
| 6556 | #ifdef _SC_2_C_BIND |
| 6557 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6558 | #endif |
| 6559 | #ifdef _SC_2_C_DEV |
| 6560 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6561 | #endif |
| 6562 | #ifdef _SC_2_C_VERSION |
| 6563 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6564 | #endif |
| 6565 | #ifdef _SC_2_FORT_DEV |
| 6566 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6567 | #endif |
| 6568 | #ifdef _SC_2_FORT_RUN |
| 6569 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6570 | #endif |
| 6571 | #ifdef _SC_2_LOCALEDEF |
| 6572 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6573 | #endif |
| 6574 | #ifdef _SC_2_SW_DEV |
| 6575 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6576 | #endif |
| 6577 | #ifdef _SC_2_UPE |
| 6578 | {"SC_2_UPE", _SC_2_UPE}, |
| 6579 | #endif |
| 6580 | #ifdef _SC_2_VERSION |
| 6581 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6582 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6583 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6584 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6585 | #endif |
| 6586 | #ifdef _SC_ACL |
| 6587 | {"SC_ACL", _SC_ACL}, |
| 6588 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6589 | #ifdef _SC_AIO_LISTIO_MAX |
| 6590 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6591 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6592 | #ifdef _SC_AIO_MAX |
| 6593 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6594 | #endif |
| 6595 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6596 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6597 | #endif |
| 6598 | #ifdef _SC_ARG_MAX |
| 6599 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6600 | #endif |
| 6601 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6602 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6603 | #endif |
| 6604 | #ifdef _SC_ATEXIT_MAX |
| 6605 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6606 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6607 | #ifdef _SC_AUDIT |
| 6608 | {"SC_AUDIT", _SC_AUDIT}, |
| 6609 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6610 | #ifdef _SC_AVPHYS_PAGES |
| 6611 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6612 | #endif |
| 6613 | #ifdef _SC_BC_BASE_MAX |
| 6614 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6615 | #endif |
| 6616 | #ifdef _SC_BC_DIM_MAX |
| 6617 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6618 | #endif |
| 6619 | #ifdef _SC_BC_SCALE_MAX |
| 6620 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6621 | #endif |
| 6622 | #ifdef _SC_BC_STRING_MAX |
| 6623 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6624 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6625 | #ifdef _SC_CAP |
| 6626 | {"SC_CAP", _SC_CAP}, |
| 6627 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6628 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6629 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6630 | #endif |
| 6631 | #ifdef _SC_CHAR_BIT |
| 6632 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6633 | #endif |
| 6634 | #ifdef _SC_CHAR_MAX |
| 6635 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6636 | #endif |
| 6637 | #ifdef _SC_CHAR_MIN |
| 6638 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6639 | #endif |
| 6640 | #ifdef _SC_CHILD_MAX |
| 6641 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6642 | #endif |
| 6643 | #ifdef _SC_CLK_TCK |
| 6644 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6645 | #endif |
| 6646 | #ifdef _SC_COHER_BLKSZ |
| 6647 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6648 | #endif |
| 6649 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6650 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6651 | #endif |
| 6652 | #ifdef _SC_DCACHE_ASSOC |
| 6653 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6654 | #endif |
| 6655 | #ifdef _SC_DCACHE_BLKSZ |
| 6656 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6657 | #endif |
| 6658 | #ifdef _SC_DCACHE_LINESZ |
| 6659 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6660 | #endif |
| 6661 | #ifdef _SC_DCACHE_SZ |
| 6662 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6663 | #endif |
| 6664 | #ifdef _SC_DCACHE_TBLKSZ |
| 6665 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6666 | #endif |
| 6667 | #ifdef _SC_DELAYTIMER_MAX |
| 6668 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6669 | #endif |
| 6670 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6671 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6672 | #endif |
| 6673 | #ifdef _SC_EXPR_NEST_MAX |
| 6674 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6675 | #endif |
| 6676 | #ifdef _SC_FSYNC |
| 6677 | {"SC_FSYNC", _SC_FSYNC}, |
| 6678 | #endif |
| 6679 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6680 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6681 | #endif |
| 6682 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6683 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6684 | #endif |
| 6685 | #ifdef _SC_ICACHE_ASSOC |
| 6686 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6687 | #endif |
| 6688 | #ifdef _SC_ICACHE_BLKSZ |
| 6689 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6690 | #endif |
| 6691 | #ifdef _SC_ICACHE_LINESZ |
| 6692 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6693 | #endif |
| 6694 | #ifdef _SC_ICACHE_SZ |
| 6695 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6696 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6697 | #ifdef _SC_INF |
| 6698 | {"SC_INF", _SC_INF}, |
| 6699 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6700 | #ifdef _SC_INT_MAX |
| 6701 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6702 | #endif |
| 6703 | #ifdef _SC_INT_MIN |
| 6704 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6705 | #endif |
| 6706 | #ifdef _SC_IOV_MAX |
| 6707 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6708 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6709 | #ifdef _SC_IP_SECOPTS |
| 6710 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6711 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6712 | #ifdef _SC_JOB_CONTROL |
| 6713 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6714 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6715 | #ifdef _SC_KERN_POINTERS |
| 6716 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6717 | #endif |
| 6718 | #ifdef _SC_KERN_SIM |
| 6719 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6720 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6721 | #ifdef _SC_LINE_MAX |
| 6722 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6723 | #endif |
| 6724 | #ifdef _SC_LOGIN_NAME_MAX |
| 6725 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6726 | #endif |
| 6727 | #ifdef _SC_LOGNAME_MAX |
| 6728 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6729 | #endif |
| 6730 | #ifdef _SC_LONG_BIT |
| 6731 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6732 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6733 | #ifdef _SC_MAC |
| 6734 | {"SC_MAC", _SC_MAC}, |
| 6735 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6736 | #ifdef _SC_MAPPED_FILES |
| 6737 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6738 | #endif |
| 6739 | #ifdef _SC_MAXPID |
| 6740 | {"SC_MAXPID", _SC_MAXPID}, |
| 6741 | #endif |
| 6742 | #ifdef _SC_MB_LEN_MAX |
| 6743 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6744 | #endif |
| 6745 | #ifdef _SC_MEMLOCK |
| 6746 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6747 | #endif |
| 6748 | #ifdef _SC_MEMLOCK_RANGE |
| 6749 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6750 | #endif |
| 6751 | #ifdef _SC_MEMORY_PROTECTION |
| 6752 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6753 | #endif |
| 6754 | #ifdef _SC_MESSAGE_PASSING |
| 6755 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6756 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6757 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6758 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6759 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6760 | #ifdef _SC_MQ_OPEN_MAX |
| 6761 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6762 | #endif |
| 6763 | #ifdef _SC_MQ_PRIO_MAX |
| 6764 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6765 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6766 | #ifdef _SC_NACLS_MAX |
| 6767 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6768 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6769 | #ifdef _SC_NGROUPS_MAX |
| 6770 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6771 | #endif |
| 6772 | #ifdef _SC_NL_ARGMAX |
| 6773 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6774 | #endif |
| 6775 | #ifdef _SC_NL_LANGMAX |
| 6776 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6777 | #endif |
| 6778 | #ifdef _SC_NL_MSGMAX |
| 6779 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6780 | #endif |
| 6781 | #ifdef _SC_NL_NMAX |
| 6782 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6783 | #endif |
| 6784 | #ifdef _SC_NL_SETMAX |
| 6785 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6786 | #endif |
| 6787 | #ifdef _SC_NL_TEXTMAX |
| 6788 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6789 | #endif |
| 6790 | #ifdef _SC_NPROCESSORS_CONF |
| 6791 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6792 | #endif |
| 6793 | #ifdef _SC_NPROCESSORS_ONLN |
| 6794 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6795 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6796 | #ifdef _SC_NPROC_CONF |
| 6797 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6798 | #endif |
| 6799 | #ifdef _SC_NPROC_ONLN |
| 6800 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6801 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6802 | #ifdef _SC_NZERO |
| 6803 | {"SC_NZERO", _SC_NZERO}, |
| 6804 | #endif |
| 6805 | #ifdef _SC_OPEN_MAX |
| 6806 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6807 | #endif |
| 6808 | #ifdef _SC_PAGESIZE |
| 6809 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6810 | #endif |
| 6811 | #ifdef _SC_PAGE_SIZE |
| 6812 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6813 | #endif |
| 6814 | #ifdef _SC_PASS_MAX |
| 6815 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6816 | #endif |
| 6817 | #ifdef _SC_PHYS_PAGES |
| 6818 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6819 | #endif |
| 6820 | #ifdef _SC_PII |
| 6821 | {"SC_PII", _SC_PII}, |
| 6822 | #endif |
| 6823 | #ifdef _SC_PII_INTERNET |
| 6824 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6825 | #endif |
| 6826 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6827 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6828 | #endif |
| 6829 | #ifdef _SC_PII_INTERNET_STREAM |
| 6830 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6831 | #endif |
| 6832 | #ifdef _SC_PII_OSI |
| 6833 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6834 | #endif |
| 6835 | #ifdef _SC_PII_OSI_CLTS |
| 6836 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6837 | #endif |
| 6838 | #ifdef _SC_PII_OSI_COTS |
| 6839 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6840 | #endif |
| 6841 | #ifdef _SC_PII_OSI_M |
| 6842 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6843 | #endif |
| 6844 | #ifdef _SC_PII_SOCKET |
| 6845 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6846 | #endif |
| 6847 | #ifdef _SC_PII_XTI |
| 6848 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6849 | #endif |
| 6850 | #ifdef _SC_POLL |
| 6851 | {"SC_POLL", _SC_POLL}, |
| 6852 | #endif |
| 6853 | #ifdef _SC_PRIORITIZED_IO |
| 6854 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6855 | #endif |
| 6856 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6857 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6858 | #endif |
| 6859 | #ifdef _SC_REALTIME_SIGNALS |
| 6860 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6861 | #endif |
| 6862 | #ifdef _SC_RE_DUP_MAX |
| 6863 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6864 | #endif |
| 6865 | #ifdef _SC_RTSIG_MAX |
| 6866 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6867 | #endif |
| 6868 | #ifdef _SC_SAVED_IDS |
| 6869 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6870 | #endif |
| 6871 | #ifdef _SC_SCHAR_MAX |
| 6872 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6873 | #endif |
| 6874 | #ifdef _SC_SCHAR_MIN |
| 6875 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6876 | #endif |
| 6877 | #ifdef _SC_SELECT |
| 6878 | {"SC_SELECT", _SC_SELECT}, |
| 6879 | #endif |
| 6880 | #ifdef _SC_SEMAPHORES |
| 6881 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6882 | #endif |
| 6883 | #ifdef _SC_SEM_NSEMS_MAX |
| 6884 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6885 | #endif |
| 6886 | #ifdef _SC_SEM_VALUE_MAX |
| 6887 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6888 | #endif |
| 6889 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6890 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6891 | #endif |
| 6892 | #ifdef _SC_SHRT_MAX |
| 6893 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6894 | #endif |
| 6895 | #ifdef _SC_SHRT_MIN |
| 6896 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6897 | #endif |
| 6898 | #ifdef _SC_SIGQUEUE_MAX |
| 6899 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6900 | #endif |
| 6901 | #ifdef _SC_SIGRT_MAX |
| 6902 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6903 | #endif |
| 6904 | #ifdef _SC_SIGRT_MIN |
| 6905 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6906 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6907 | #ifdef _SC_SOFTPOWER |
| 6908 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6909 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6910 | #ifdef _SC_SPLIT_CACHE |
| 6911 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6912 | #endif |
| 6913 | #ifdef _SC_SSIZE_MAX |
| 6914 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6915 | #endif |
| 6916 | #ifdef _SC_STACK_PROT |
| 6917 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6918 | #endif |
| 6919 | #ifdef _SC_STREAM_MAX |
| 6920 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6921 | #endif |
| 6922 | #ifdef _SC_SYNCHRONIZED_IO |
| 6923 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6924 | #endif |
| 6925 | #ifdef _SC_THREADS |
| 6926 | {"SC_THREADS", _SC_THREADS}, |
| 6927 | #endif |
| 6928 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6929 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6930 | #endif |
| 6931 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6932 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6933 | #endif |
| 6934 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6935 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6936 | #endif |
| 6937 | #ifdef _SC_THREAD_KEYS_MAX |
| 6938 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6939 | #endif |
| 6940 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6941 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6942 | #endif |
| 6943 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6944 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6945 | #endif |
| 6946 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6947 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6948 | #endif |
| 6949 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6950 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6951 | #endif |
| 6952 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6953 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6954 | #endif |
| 6955 | #ifdef _SC_THREAD_STACK_MIN |
| 6956 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6957 | #endif |
| 6958 | #ifdef _SC_THREAD_THREADS_MAX |
| 6959 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6960 | #endif |
| 6961 | #ifdef _SC_TIMERS |
| 6962 | {"SC_TIMERS", _SC_TIMERS}, |
| 6963 | #endif |
| 6964 | #ifdef _SC_TIMER_MAX |
| 6965 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6966 | #endif |
| 6967 | #ifdef _SC_TTY_NAME_MAX |
| 6968 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6969 | #endif |
| 6970 | #ifdef _SC_TZNAME_MAX |
| 6971 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6972 | #endif |
| 6973 | #ifdef _SC_T_IOV_MAX |
| 6974 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6975 | #endif |
| 6976 | #ifdef _SC_UCHAR_MAX |
| 6977 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6978 | #endif |
| 6979 | #ifdef _SC_UINT_MAX |
| 6980 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6981 | #endif |
| 6982 | #ifdef _SC_UIO_MAXIOV |
| 6983 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6984 | #endif |
| 6985 | #ifdef _SC_ULONG_MAX |
| 6986 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6987 | #endif |
| 6988 | #ifdef _SC_USHRT_MAX |
| 6989 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6990 | #endif |
| 6991 | #ifdef _SC_VERSION |
| 6992 | {"SC_VERSION", _SC_VERSION}, |
| 6993 | #endif |
| 6994 | #ifdef _SC_WORD_BIT |
| 6995 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6996 | #endif |
| 6997 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6998 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6999 | #endif |
| 7000 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 7001 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 7002 | #endif |
| 7003 | #ifdef _SC_XBS5_LP64_OFF64 |
| 7004 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 7005 | #endif |
| 7006 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 7007 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 7008 | #endif |
| 7009 | #ifdef _SC_XOPEN_CRYPT |
| 7010 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 7011 | #endif |
| 7012 | #ifdef _SC_XOPEN_ENH_I18N |
| 7013 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 7014 | #endif |
| 7015 | #ifdef _SC_XOPEN_LEGACY |
| 7016 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 7017 | #endif |
| 7018 | #ifdef _SC_XOPEN_REALTIME |
| 7019 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 7020 | #endif |
| 7021 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 7022 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 7023 | #endif |
| 7024 | #ifdef _SC_XOPEN_SHM |
| 7025 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 7026 | #endif |
| 7027 | #ifdef _SC_XOPEN_UNIX |
| 7028 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 7029 | #endif |
| 7030 | #ifdef _SC_XOPEN_VERSION |
| 7031 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 7032 | #endif |
| 7033 | #ifdef _SC_XOPEN_XCU_VERSION |
| 7034 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 7035 | #endif |
| 7036 | #ifdef _SC_XOPEN_XPG2 |
| 7037 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 7038 | #endif |
| 7039 | #ifdef _SC_XOPEN_XPG3 |
| 7040 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 7041 | #endif |
| 7042 | #ifdef _SC_XOPEN_XPG4 |
| 7043 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 7044 | #endif |
| 7045 | }; |
| 7046 | |
| 7047 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7048 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7049 | { |
| 7050 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 7051 | sizeof(posix_constants_sysconf) |
| 7052 | / sizeof(struct constdef)); |
| 7053 | } |
| 7054 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7055 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7056 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7057 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7058 | |
| 7059 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7060 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7061 | { |
| 7062 | PyObject *result = NULL; |
| 7063 | int name; |
| 7064 | |
| 7065 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 7066 | int value; |
| 7067 | |
| 7068 | errno = 0; |
| 7069 | value = sysconf(name); |
| 7070 | if (value == -1 && errno != 0) |
| 7071 | posix_error(); |
| 7072 | else |
| 7073 | result = PyInt_FromLong(value); |
| 7074 | } |
| 7075 | return result; |
| 7076 | } |
| 7077 | #endif |
| 7078 | |
| 7079 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7080 | /* This code is used to ensure that the tables of configuration value names |
| 7081 | * are in sorted order as required by conv_confname(), and also to build the |
| 7082 | * the exported dictionaries that are used to publish information about the |
| 7083 | * names available on the host platform. |
| 7084 | * |
| 7085 | * Sorting the table at runtime ensures that the table is properly ordered |
| 7086 | * when used, even for platforms we're not able to test on. It also makes |
| 7087 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7088 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7089 | |
| 7090 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7091 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7092 | { |
| 7093 | const struct constdef *c1 = |
| 7094 | (const struct constdef *) v1; |
| 7095 | const struct constdef *c2 = |
| 7096 | (const struct constdef *) v2; |
| 7097 | |
| 7098 | return strcmp(c1->name, c2->name); |
| 7099 | } |
| 7100 | |
| 7101 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7102 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7103 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7104 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7105 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7106 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7107 | |
| 7108 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 7109 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7110 | if (d == NULL) |
| 7111 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7112 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7113 | for (i=0; i < tablesize; ++i) { |
| 7114 | PyObject *o = PyInt_FromLong(table[i].value); |
| 7115 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 7116 | Py_XDECREF(o); |
| 7117 | Py_DECREF(d); |
| 7118 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7119 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7120 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7121 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7122 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7123 | } |
| 7124 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7125 | /* Return -1 on failure, 0 on success. */ |
| 7126 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7127 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7128 | { |
| 7129 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7130 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7131 | sizeof(posix_constants_pathconf) |
| 7132 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7133 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7134 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7135 | #endif |
| 7136 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7137 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7138 | sizeof(posix_constants_confstr) |
| 7139 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7140 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7141 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7142 | #endif |
| 7143 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7144 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7145 | sizeof(posix_constants_sysconf) |
| 7146 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7147 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7148 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7149 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7150 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7151 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7152 | |
| 7153 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7154 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7155 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7156 | 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] | 7157 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7158 | |
| 7159 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7160 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7161 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7162 | abort(); |
| 7163 | /*NOTREACHED*/ |
| 7164 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 7165 | return NULL; |
| 7166 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7167 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7168 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7169 | PyDoc_STRVAR(win32_startfile__doc__, |
| 7170 | "startfile(filepath) - Start a file with its associated application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7171 | \n\ |
| 7172 | This acts like double-clicking the file in Explorer, or giving the file\n\ |
| 7173 | name as an argument to the DOS \"start\" command: the file is opened\n\ |
| 7174 | with whatever application (if any) its extension is associated.\n\ |
| 7175 | \n\ |
| 7176 | startfile returns as soon as the associated application is launched.\n\ |
| 7177 | There is no option to wait for the application to close, and no way\n\ |
| 7178 | to retrieve the application's exit status.\n\ |
| 7179 | \n\ |
| 7180 | The filepath is relative to the current directory. If you want to use\n\ |
| 7181 | 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] | 7182 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7183 | |
| 7184 | static PyObject * |
| 7185 | win32_startfile(PyObject *self, PyObject *args) |
| 7186 | { |
| 7187 | char *filepath; |
| 7188 | HINSTANCE rc; |
| 7189 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) |
| 7190 | return NULL; |
| 7191 | Py_BEGIN_ALLOW_THREADS |
| 7192 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); |
| 7193 | Py_END_ALLOW_THREADS |
| 7194 | if (rc <= (HINSTANCE)32) |
| 7195 | return win32_error("startfile", filepath); |
| 7196 | Py_INCREF(Py_None); |
| 7197 | return Py_None; |
| 7198 | } |
| 7199 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7200 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7201 | #ifdef HAVE_GETLOADAVG |
| 7202 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 7203 | "getloadavg() -> (float, float, float)\n\n\ |
| 7204 | Return the number of processes in the system run queue averaged over\n\ |
| 7205 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 7206 | was unobtainable"); |
| 7207 | |
| 7208 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7209 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7210 | { |
| 7211 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7212 | if (getloadavg(loadavg, 3)!=3) { |
| 7213 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 7214 | return NULL; |
| 7215 | } else |
| 7216 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 7217 | } |
| 7218 | #endif |
| 7219 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7220 | #ifdef MS_WINDOWS |
| 7221 | |
| 7222 | PyDoc_STRVAR(win32_urandom__doc__, |
| 7223 | "urandom(n) -> str\n\n\ |
| 7224 | Return a string of n random bytes suitable for cryptographic use."); |
| 7225 | |
| 7226 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 7227 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 7228 | DWORD dwFlags ); |
| 7229 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 7230 | BYTE *pbBuffer ); |
| 7231 | |
| 7232 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
| 7233 | static HCRYPTPROV hCryptProv = 0; |
| 7234 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7235 | static PyObject* |
| 7236 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7237 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7238 | int howMany; |
| 7239 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7240 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7241 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 7242 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7243 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 7244 | if (howMany < 0) |
| 7245 | return PyErr_Format(PyExc_ValueError, |
| 7246 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7247 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7248 | if (hCryptProv == 0) { |
| 7249 | HINSTANCE hAdvAPI32 = NULL; |
| 7250 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7251 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7252 | /* Obtain handle to the DLL containing CryptoAPI |
| 7253 | This should not fail */ |
| 7254 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 7255 | if(hAdvAPI32 == NULL) |
| 7256 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7257 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7258 | /* Obtain pointers to the CryptoAPI functions |
| 7259 | This will fail on some early versions of Win95 */ |
| 7260 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 7261 | hAdvAPI32, |
| 7262 | "CryptAcquireContextA"); |
| 7263 | if (pCryptAcquireContext == NULL) |
| 7264 | return PyErr_Format(PyExc_NotImplementedError, |
| 7265 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7266 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7267 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 7268 | hAdvAPI32, "CryptGenRandom"); |
| 7269 | if (pCryptAcquireContext == NULL) |
| 7270 | return PyErr_Format(PyExc_NotImplementedError, |
| 7271 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7272 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7273 | /* Acquire context */ |
| 7274 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 7275 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 7276 | return win32_error("CryptAcquireContext", NULL); |
| 7277 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7278 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7279 | /* Allocate bytes */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7280 | result = PyString_FromStringAndSize(NULL, howMany); |
| 7281 | if (result != NULL) { |
| 7282 | /* Get random data */ |
| 7283 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 7284 | PyString_AS_STRING(result))) { |
| 7285 | Py_DECREF(result); |
| 7286 | return win32_error("CryptGenRandom", NULL); |
| 7287 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7288 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7289 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7290 | } |
| 7291 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7292 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7293 | static PyMethodDef posix_methods[] = { |
| 7294 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 7295 | #ifdef HAVE_TTYNAME |
| 7296 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 7297 | #endif |
| 7298 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 7299 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7300 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7301 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7302 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7303 | #ifdef HAVE_LCHOWN |
| 7304 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 7305 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7306 | #ifdef HAVE_CHROOT |
| 7307 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 7308 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7309 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7310 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7311 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7312 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7313 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7314 | #ifdef Py_USING_UNICODE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7315 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7316 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7317 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7318 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7319 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7320 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7321 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7322 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7323 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7324 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7325 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7326 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7327 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7328 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7329 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7330 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7331 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7332 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7333 | {"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] | 7334 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7335 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7336 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7337 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7338 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7339 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7340 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7341 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7342 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7343 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7344 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7345 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7346 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7347 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7348 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7349 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7350 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7351 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7352 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7353 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7354 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7355 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7356 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7357 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7358 | #if defined(PYOS_OS2) |
| 7359 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 7360 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 7361 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7362 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7363 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7364 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7365 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7366 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7367 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7368 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7369 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7370 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7371 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7372 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7373 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7374 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7375 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7376 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7377 | #endif /* HAVE_GETEGID */ |
| 7378 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7379 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7380 | #endif /* HAVE_GETEUID */ |
| 7381 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7382 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7383 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7384 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7385 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7386 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7387 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7388 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7389 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7390 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7391 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7392 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7393 | #endif /* HAVE_GETPPID */ |
| 7394 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7395 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7396 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7397 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7398 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7399 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7400 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7401 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7402 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7403 | #ifdef HAVE_KILLPG |
| 7404 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7405 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7406 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7407 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7408 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7409 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7410 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7411 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7412 | {"popen2", win32_popen2, METH_VARARGS}, |
| 7413 | {"popen3", win32_popen3, METH_VARARGS}, |
| 7414 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7415 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7416 | #else |
| 7417 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7418 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 7419 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 7420 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 7421 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7422 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7423 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7424 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7425 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7426 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7427 | #ifdef HAVE_SETEUID |
| 7428 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7429 | #endif /* HAVE_SETEUID */ |
| 7430 | #ifdef HAVE_SETEGID |
| 7431 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7432 | #endif /* HAVE_SETEGID */ |
| 7433 | #ifdef HAVE_SETREUID |
| 7434 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7435 | #endif /* HAVE_SETREUID */ |
| 7436 | #ifdef HAVE_SETREGID |
| 7437 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7438 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7439 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7440 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7441 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7442 | #ifdef HAVE_SETGROUPS |
| 7443 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 7444 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7445 | #ifdef HAVE_GETPGID |
| 7446 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7447 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7448 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7449 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7450 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7451 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7452 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7453 | #endif /* HAVE_WAIT */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7454 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7455 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7456 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7457 | #ifdef HAVE_GETSID |
| 7458 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 7459 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7460 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7461 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7462 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7463 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7464 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7465 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7466 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7467 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7468 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7469 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7470 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7471 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7472 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7473 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 7474 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7475 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7476 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7477 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7478 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7479 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 7480 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7481 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7482 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7483 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7484 | #endif |
| 7485 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7486 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7487 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7488 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7489 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7490 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7491 | #ifdef HAVE_DEVICE_MACROS |
| 7492 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7493 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7494 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7495 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7496 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7497 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7498 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7499 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7500 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7501 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7502 | #ifdef HAVE_UNSETENV |
| 7503 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7504 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7505 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7506 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7507 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7508 | #ifdef HAVE_FCHDIR |
| 7509 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7510 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7511 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7512 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7513 | #endif |
| 7514 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7515 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7516 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7517 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7518 | #ifdef WCOREDUMP |
| 7519 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7520 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7521 | #ifdef WIFCONTINUED |
| 7522 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7523 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7524 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7525 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7526 | #endif /* WIFSTOPPED */ |
| 7527 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7528 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7529 | #endif /* WIFSIGNALED */ |
| 7530 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7531 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7532 | #endif /* WIFEXITED */ |
| 7533 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7534 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7535 | #endif /* WEXITSTATUS */ |
| 7536 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7537 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7538 | #endif /* WTERMSIG */ |
| 7539 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7540 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7541 | #endif /* WSTOPSIG */ |
| 7542 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7543 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7544 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7545 | #endif |
| 7546 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7547 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7548 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 7549 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7550 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7551 | #endif |
| 7552 | #ifdef HAVE_TEMPNAM |
| 7553 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 7554 | #endif |
| 7555 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7556 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7557 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7558 | #ifdef HAVE_CONFSTR |
| 7559 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7560 | #endif |
| 7561 | #ifdef HAVE_SYSCONF |
| 7562 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7563 | #endif |
| 7564 | #ifdef HAVE_FPATHCONF |
| 7565 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7566 | #endif |
| 7567 | #ifdef HAVE_PATHCONF |
| 7568 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7569 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7570 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7571 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7572 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7573 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7574 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7575 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7576 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7577 | #ifdef MS_WINDOWS |
| 7578 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 7579 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7580 | {NULL, NULL} /* Sentinel */ |
| 7581 | }; |
| 7582 | |
| 7583 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7584 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7585 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7586 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7587 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7588 | } |
| 7589 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7590 | #if defined(PYOS_OS2) |
| 7591 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7592 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7593 | { |
| 7594 | APIRET rc; |
| 7595 | ULONG values[QSV_MAX+1]; |
| 7596 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7597 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7598 | |
| 7599 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7600 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7601 | Py_END_ALLOW_THREADS |
| 7602 | |
| 7603 | if (rc != NO_ERROR) { |
| 7604 | os2_error(rc); |
| 7605 | return -1; |
| 7606 | } |
| 7607 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7608 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7609 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7610 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7611 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7612 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7613 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7614 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7615 | |
| 7616 | switch (values[QSV_VERSION_MINOR]) { |
| 7617 | case 0: ver = "2.00"; break; |
| 7618 | case 10: ver = "2.10"; break; |
| 7619 | case 11: ver = "2.11"; break; |
| 7620 | case 30: ver = "3.00"; break; |
| 7621 | case 40: ver = "4.00"; break; |
| 7622 | case 50: ver = "5.00"; break; |
| 7623 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7624 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7625 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7626 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7627 | ver = &tmp[0]; |
| 7628 | } |
| 7629 | |
| 7630 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7631 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7632 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7633 | |
| 7634 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7635 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7636 | tmp[1] = ':'; |
| 7637 | tmp[2] = '\0'; |
| 7638 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7639 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7640 | } |
| 7641 | #endif |
| 7642 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7643 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7644 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7645 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7646 | #ifdef F_OK |
| 7647 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7648 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7649 | #ifdef R_OK |
| 7650 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7651 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7652 | #ifdef W_OK |
| 7653 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7654 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7655 | #ifdef X_OK |
| 7656 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7657 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7658 | #ifdef NGROUPS_MAX |
| 7659 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7660 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7661 | #ifdef TMP_MAX |
| 7662 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7663 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7664 | #ifdef WCONTINUED |
| 7665 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7666 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7667 | #ifdef WNOHANG |
| 7668 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7669 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7670 | #ifdef WUNTRACED |
| 7671 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7672 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7673 | #ifdef O_RDONLY |
| 7674 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7675 | #endif |
| 7676 | #ifdef O_WRONLY |
| 7677 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7678 | #endif |
| 7679 | #ifdef O_RDWR |
| 7680 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7681 | #endif |
| 7682 | #ifdef O_NDELAY |
| 7683 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7684 | #endif |
| 7685 | #ifdef O_NONBLOCK |
| 7686 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7687 | #endif |
| 7688 | #ifdef O_APPEND |
| 7689 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7690 | #endif |
| 7691 | #ifdef O_DSYNC |
| 7692 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7693 | #endif |
| 7694 | #ifdef O_RSYNC |
| 7695 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7696 | #endif |
| 7697 | #ifdef O_SYNC |
| 7698 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7699 | #endif |
| 7700 | #ifdef O_NOCTTY |
| 7701 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7702 | #endif |
| 7703 | #ifdef O_CREAT |
| 7704 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7705 | #endif |
| 7706 | #ifdef O_EXCL |
| 7707 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7708 | #endif |
| 7709 | #ifdef O_TRUNC |
| 7710 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7711 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7712 | #ifdef O_BINARY |
| 7713 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7714 | #endif |
| 7715 | #ifdef O_TEXT |
| 7716 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7717 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7718 | #ifdef O_LARGEFILE |
| 7719 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7720 | #endif |
| 7721 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7722 | /* MS Windows */ |
| 7723 | #ifdef O_NOINHERIT |
| 7724 | /* Don't inherit in child processes. */ |
| 7725 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7726 | #endif |
| 7727 | #ifdef _O_SHORT_LIVED |
| 7728 | /* Optimize for short life (keep in memory). */ |
| 7729 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7730 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7731 | #endif |
| 7732 | #ifdef O_TEMPORARY |
| 7733 | /* Automatically delete when last handle is closed. */ |
| 7734 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7735 | #endif |
| 7736 | #ifdef O_RANDOM |
| 7737 | /* Optimize for random access. */ |
| 7738 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7739 | #endif |
| 7740 | #ifdef O_SEQUENTIAL |
| 7741 | /* Optimize for sequential access. */ |
| 7742 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7743 | #endif |
| 7744 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7745 | /* GNU extensions. */ |
| 7746 | #ifdef O_DIRECT |
| 7747 | /* Direct disk access. */ |
| 7748 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7749 | #endif |
| 7750 | #ifdef O_DIRECTORY |
| 7751 | /* Must be a directory. */ |
| 7752 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7753 | #endif |
| 7754 | #ifdef O_NOFOLLOW |
| 7755 | /* Do not follow links. */ |
| 7756 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7757 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7758 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7759 | /* These come from sysexits.h */ |
| 7760 | #ifdef EX_OK |
| 7761 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7762 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7763 | #ifdef EX_USAGE |
| 7764 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7765 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7766 | #ifdef EX_DATAERR |
| 7767 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7768 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7769 | #ifdef EX_NOINPUT |
| 7770 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7771 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7772 | #ifdef EX_NOUSER |
| 7773 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7774 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7775 | #ifdef EX_NOHOST |
| 7776 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7777 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7778 | #ifdef EX_UNAVAILABLE |
| 7779 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7780 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7781 | #ifdef EX_SOFTWARE |
| 7782 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7783 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7784 | #ifdef EX_OSERR |
| 7785 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7786 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7787 | #ifdef EX_OSFILE |
| 7788 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7789 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7790 | #ifdef EX_CANTCREAT |
| 7791 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7792 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7793 | #ifdef EX_IOERR |
| 7794 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7795 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7796 | #ifdef EX_TEMPFAIL |
| 7797 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7798 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7799 | #ifdef EX_PROTOCOL |
| 7800 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7801 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7802 | #ifdef EX_NOPERM |
| 7803 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7804 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7805 | #ifdef EX_CONFIG |
| 7806 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7807 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7808 | #ifdef EX_NOTFOUND |
| 7809 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7810 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7811 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7812 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7813 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7814 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7815 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7816 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7817 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7818 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7819 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7820 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7821 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7822 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7823 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7824 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7825 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7826 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7827 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7828 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7829 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7830 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7831 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7832 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7833 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7834 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7835 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7836 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7837 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7838 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7839 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7840 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7841 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7842 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7843 | #if defined(PYOS_OS2) |
| 7844 | if (insertvalues(d)) return -1; |
| 7845 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7846 | return 0; |
| 7847 | } |
| 7848 | |
| 7849 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7850 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7851 | #define INITFUNC initnt |
| 7852 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7853 | |
| 7854 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7855 | #define INITFUNC initos2 |
| 7856 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7857 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7858 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7859 | #define INITFUNC initposix |
| 7860 | #define MODNAME "posix" |
| 7861 | #endif |
| 7862 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7863 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7864 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7865 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7866 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7867 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7868 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7869 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7870 | posix__doc__); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7871 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7872 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7873 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7874 | Py_XINCREF(v); |
| 7875 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7876 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7877 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7878 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7879 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7880 | return; |
| 7881 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7882 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7883 | return; |
| 7884 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7885 | Py_INCREF(PyExc_OSError); |
| 7886 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7887 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7888 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7889 | if (posix_putenv_garbage == NULL) |
| 7890 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7891 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7892 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7893 | stat_result_desc.name = MODNAME ".stat_result"; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7894 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7895 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7896 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7897 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7898 | structseq_new = StatResultType.tp_new; |
| 7899 | StatResultType.tp_new = statresult_new; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7900 | Py_INCREF((PyObject*) &StatResultType); |
| 7901 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7902 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7903 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7904 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7905 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7906 | PyModule_AddObject(m, "statvfs_result", |
| 7907 | (PyObject*) &StatVFSResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7908 | } |