Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 14 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 15 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 16 | #define PY_SSIZE_T_CLEAN |
| 17 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 18 | #include "Python.h" |
| 19 | #include "structseq.h" |
| 20 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 21 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 22 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 23 | #endif /* defined(__VMS) */ |
| 24 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 29 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 30 | "This module provides access to operating system functionality that is\n\ |
| 31 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 32 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 33 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 34 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 35 | #ifndef Py_USING_UNICODE |
| 36 | /* This is used in signatures of functions. */ |
| 37 | #define Py_UNICODE void |
| 38 | #endif |
| 39 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 40 | #if defined(PYOS_OS2) |
| 41 | #define INCL_DOS |
| 42 | #define INCL_DOSERRORS |
| 43 | #define INCL_DOSPROCESS |
| 44 | #define INCL_NOPMAPI |
| 45 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 46 | #if defined(PYCC_GCC) |
| 47 | #include <ctype.h> |
| 48 | #include <io.h> |
| 49 | #include <stdio.h> |
| 50 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 51 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 52 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 55 | #include <sys/types.h> |
| 56 | #include <sys/stat.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 58 | #ifdef HAVE_SYS_WAIT_H |
| 59 | #include <sys/wait.h> /* For WNOHANG */ |
| 60 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 62 | #include <signal.h> |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 64 | #ifdef HAVE_FCNTL_H |
| 65 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 66 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 68 | #ifdef HAVE_GRP_H |
| 69 | #include <grp.h> |
| 70 | #endif |
| 71 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 72 | #ifdef HAVE_SYSEXITS_H |
| 73 | #include <sysexits.h> |
| 74 | #endif /* HAVE_SYSEXITS_H */ |
| 75 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 76 | #ifdef HAVE_SYS_LOADAVG_H |
| 77 | #include <sys/loadavg.h> |
| 78 | #endif |
| 79 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 80 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 81 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 82 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 83 | #include <process.h> |
| 84 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 85 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 86 | #define HAVE_GETCWD 1 |
| 87 | #define HAVE_OPENDIR 1 |
| 88 | #define HAVE_SYSTEM 1 |
| 89 | #if defined(__OS2__) |
| 90 | #define HAVE_EXECV 1 |
| 91 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 92 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 93 | #include <process.h> |
| 94 | #else |
| 95 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 96 | #define HAVE_EXECV 1 |
| 97 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 98 | #define HAVE_OPENDIR 1 |
| 99 | #define HAVE_PIPE 1 |
| 100 | #define HAVE_POPEN 1 |
| 101 | #define HAVE_SYSTEM 1 |
| 102 | #define HAVE_WAIT 1 |
| 103 | #else |
| 104 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 105 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 106 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 107 | #define HAVE_EXECV 1 |
| 108 | #define HAVE_PIPE 1 |
| 109 | #define HAVE_POPEN 1 |
| 110 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 111 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 112 | #define HAVE_FSYNC 1 |
| 113 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 114 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 115 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 116 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 117 | #else /* all other compilers */ |
| 118 | /* Unix functions that the configure script doesn't check for */ |
| 119 | #define HAVE_EXECV 1 |
| 120 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 121 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 122 | #define HAVE_FORK1 1 |
| 123 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 124 | #define HAVE_GETCWD 1 |
| 125 | #define HAVE_GETEGID 1 |
| 126 | #define HAVE_GETEUID 1 |
| 127 | #define HAVE_GETGID 1 |
| 128 | #define HAVE_GETPPID 1 |
| 129 | #define HAVE_GETUID 1 |
| 130 | #define HAVE_KILL 1 |
| 131 | #define HAVE_OPENDIR 1 |
| 132 | #define HAVE_PIPE 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 133 | #ifndef __rtems__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #define HAVE_POPEN 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 135 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 136 | #define HAVE_SYSTEM 1 |
| 137 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 138 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 139 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 140 | #endif /* _MSC_VER */ |
| 141 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 142 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 143 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 144 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 145 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 146 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 147 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 148 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 149 | (default) */ |
| 150 | extern char *ctermid_r(char *); |
| 151 | #endif |
| 152 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 153 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 154 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 155 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 156 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 157 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 158 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 159 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 160 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 161 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 162 | #endif |
| 163 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 164 | extern int chdir(char *); |
| 165 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 166 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 167 | extern int chdir(const char *); |
| 168 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 169 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 170 | #ifdef __BORLANDC__ |
| 171 | extern int chmod(const char *, int); |
| 172 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 173 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 174 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 175 | extern int chown(const char *, uid_t, gid_t); |
| 176 | extern char *getcwd(char *, int); |
| 177 | extern char *strerror(int); |
| 178 | extern int link(const char *, const char *); |
| 179 | extern int rename(const char *, const char *); |
| 180 | extern int stat(const char *, struct stat *); |
| 181 | extern int unlink(const char *); |
| 182 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 183 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 184 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 185 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 186 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 187 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 188 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 189 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 191 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 193 | #ifdef HAVE_UTIME_H |
| 194 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 195 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 196 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 197 | #ifdef HAVE_SYS_UTIME_H |
| 198 | #include <sys/utime.h> |
| 199 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 200 | #endif /* HAVE_SYS_UTIME_H */ |
| 201 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #ifdef HAVE_SYS_TIMES_H |
| 203 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 204 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | |
| 206 | #ifdef HAVE_SYS_PARAM_H |
| 207 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 208 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | |
| 210 | #ifdef HAVE_SYS_UTSNAME_H |
| 211 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 212 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 214 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 216 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 217 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 218 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 219 | #include <direct.h> |
| 220 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 221 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 223 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 224 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 225 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 227 | #endif |
| 228 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 229 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 230 | #endif |
| 231 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 233 | #endif |
| 234 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 236 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 237 | #include <direct.h> |
| 238 | #include <io.h> |
| 239 | #include <process.h> |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 240 | #include "osdefs.h" |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 241 | #define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 242 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 243 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 244 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 245 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 246 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 248 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 249 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 250 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 251 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 252 | #ifndef MAXPATHLEN |
| 253 | #define MAXPATHLEN 1024 |
| 254 | #endif /* MAXPATHLEN */ |
| 255 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 256 | #ifdef UNION_WAIT |
| 257 | /* Emulate some macros on systems that have a union instead of macros */ |
| 258 | |
| 259 | #ifndef WIFEXITED |
| 260 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 261 | #endif |
| 262 | |
| 263 | #ifndef WEXITSTATUS |
| 264 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 265 | #endif |
| 266 | |
| 267 | #ifndef WTERMSIG |
| 268 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 269 | #endif |
| 270 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 271 | #define WAIT_TYPE union wait |
| 272 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 273 | |
| 274 | #else /* !UNION_WAIT */ |
| 275 | #define WAIT_TYPE int |
| 276 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 277 | #endif /* UNION_WAIT */ |
| 278 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 279 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 280 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 281 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 282 | #define USE_CTERMID_R |
| 283 | #endif |
| 284 | |
| 285 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 286 | #define USE_TMPNAM_R |
| 287 | #endif |
| 288 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 289 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 290 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 291 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 292 | # define STAT win32_stat |
| 293 | # define FSTAT win32_fstat |
| 294 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 295 | #else |
| 296 | # define STAT stat |
| 297 | # define FSTAT fstat |
| 298 | # define STRUCT_STAT struct stat |
| 299 | #endif |
| 300 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 301 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 302 | #include <sys/mkdev.h> |
| 303 | #else |
| 304 | #if defined(MAJOR_IN_SYSMACROS) |
| 305 | #include <sys/sysmacros.h> |
| 306 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 307 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 308 | #include <sys/mkdev.h> |
| 309 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 310 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 311 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 312 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 313 | #ifdef WITH_NEXT_FRAMEWORK |
| 314 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 315 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 316 | */ |
| 317 | #include <crt_externs.h> |
| 318 | static char **environ; |
| 319 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 320 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 321 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 322 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 323 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 324 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 325 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 326 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 327 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 328 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 329 | if (d == NULL) |
| 330 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 331 | #ifdef WITH_NEXT_FRAMEWORK |
| 332 | if (environ == NULL) |
| 333 | environ = *_NSGetEnviron(); |
| 334 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 335 | if (environ == NULL) |
| 336 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 337 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 338 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 339 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 340 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 341 | char *p = strchr(*e, '='); |
| 342 | if (p == NULL) |
| 343 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 344 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 345 | if (k == NULL) { |
| 346 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 347 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 348 | } |
| 349 | v = PyString_FromString(p+1); |
| 350 | if (v == NULL) { |
| 351 | PyErr_Clear(); |
| 352 | Py_DECREF(k); |
| 353 | continue; |
| 354 | } |
| 355 | if (PyDict_GetItem(d, k) == NULL) { |
| 356 | if (PyDict_SetItem(d, k, v) != 0) |
| 357 | PyErr_Clear(); |
| 358 | } |
| 359 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 360 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 361 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 362 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 363 | { |
| 364 | APIRET rc; |
| 365 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 366 | |
| 367 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 368 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 369 | PyObject *v = PyString_FromString(buffer); |
| 370 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 371 | Py_DECREF(v); |
| 372 | } |
| 373 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 374 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 375 | PyObject *v = PyString_FromString(buffer); |
| 376 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 377 | Py_DECREF(v); |
| 378 | } |
| 379 | } |
| 380 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 381 | return d; |
| 382 | } |
| 383 | |
| 384 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 385 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 386 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 387 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 388 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 389 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 390 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 391 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 392 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 393 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 394 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 395 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 398 | #ifdef Py_WIN_WIDE_FILENAMES |
| 399 | static PyObject * |
| 400 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 401 | { |
| 402 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 403 | } |
| 404 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 405 | |
| 406 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 407 | static PyObject * |
| 408 | posix_error_with_allocated_filename(char* name) |
| 409 | { |
| 410 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 411 | PyMem_Free(name); |
| 412 | return rc; |
| 413 | } |
| 414 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 415 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 416 | static PyObject * |
| 417 | win32_error(char* function, char* filename) |
| 418 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 419 | /* XXX We should pass the function name along in the future. |
| 420 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 421 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 422 | Windows error object, which is non-trivial. |
| 423 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 424 | errno = GetLastError(); |
| 425 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 426 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 427 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 428 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 429 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 430 | |
| 431 | #ifdef Py_WIN_WIDE_FILENAMES |
| 432 | static PyObject * |
| 433 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 434 | { |
| 435 | /* XXX - see win32_error for comments on 'function' */ |
| 436 | errno = GetLastError(); |
| 437 | if (filename) |
| 438 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 439 | else |
| 440 | return PyErr_SetFromWindowsErr(errno); |
| 441 | } |
| 442 | |
| 443 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 444 | { |
| 445 | /* XXX Perhaps we should make this API an alias of |
| 446 | PyObject_Unicode() instead ?! */ |
| 447 | if (PyUnicode_CheckExact(obj)) { |
| 448 | Py_INCREF(obj); |
| 449 | return obj; |
| 450 | } |
| 451 | if (PyUnicode_Check(obj)) { |
| 452 | /* For a Unicode subtype that's not a Unicode object, |
| 453 | return a true Unicode object with the same data. */ |
| 454 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 455 | PyUnicode_GET_SIZE(obj)); |
| 456 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 457 | return PyUnicode_FromEncodedObject(obj, |
| 458 | Py_FileSystemDefaultEncoding, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 459 | "strict"); |
| 460 | } |
| 461 | |
| 462 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 463 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 464 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 465 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 466 | #if defined(PYOS_OS2) |
| 467 | /********************************************************************** |
| 468 | * Helper Function to Trim and Format OS/2 Messages |
| 469 | **********************************************************************/ |
| 470 | static void |
| 471 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 472 | { |
| 473 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 474 | |
| 475 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 476 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 477 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 478 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 479 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 480 | } |
| 481 | |
| 482 | /* Add Optional Reason Text */ |
| 483 | if (reason) { |
| 484 | strcat(msgbuf, " : "); |
| 485 | strcat(msgbuf, reason); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /********************************************************************** |
| 490 | * Decode an OS/2 Operating System Error Code |
| 491 | * |
| 492 | * A convenience function to lookup an OS/2 error code and return a |
| 493 | * text message we can use to raise a Python exception. |
| 494 | * |
| 495 | * Notes: |
| 496 | * The messages for errors returned from the OS/2 kernel reside in |
| 497 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 498 | * |
| 499 | **********************************************************************/ |
| 500 | static char * |
| 501 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 502 | { |
| 503 | APIRET rc; |
| 504 | ULONG msglen; |
| 505 | |
| 506 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 507 | Py_BEGIN_ALLOW_THREADS |
| 508 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 509 | errorcode, "oso001.msg", &msglen); |
| 510 | Py_END_ALLOW_THREADS |
| 511 | |
| 512 | if (rc == NO_ERROR) |
| 513 | os2_formatmsg(msgbuf, msglen, reason); |
| 514 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 515 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 516 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 517 | |
| 518 | return msgbuf; |
| 519 | } |
| 520 | |
| 521 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 522 | errors are not in a global variable e.g. 'errno' nor are |
| 523 | they congruent with posix error numbers. */ |
| 524 | |
| 525 | static PyObject * os2_error(int code) |
| 526 | { |
| 527 | char text[1024]; |
| 528 | PyObject *v; |
| 529 | |
| 530 | os2_strerror(text, sizeof(text), code, ""); |
| 531 | |
| 532 | v = Py_BuildValue("(is)", code, text); |
| 533 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 534 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 535 | Py_DECREF(v); |
| 536 | } |
| 537 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 538 | } |
| 539 | |
| 540 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 541 | |
| 542 | /* POSIX generic methods */ |
| 543 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 544 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 545 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 546 | { |
| 547 | int fd; |
| 548 | int res; |
| 549 | fd = PyObject_AsFileDescriptor(fdobj); |
| 550 | if (fd < 0) |
| 551 | return NULL; |
| 552 | Py_BEGIN_ALLOW_THREADS |
| 553 | res = (*func)(fd); |
| 554 | Py_END_ALLOW_THREADS |
| 555 | if (res < 0) |
| 556 | return posix_error(); |
| 557 | Py_INCREF(Py_None); |
| 558 | return Py_None; |
| 559 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 560 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 561 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 562 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 563 | unicode_file_names(void) |
| 564 | { |
| 565 | static int canusewide = -1; |
| 566 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 567 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 568 | the Windows NT family. */ |
| 569 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 570 | } |
| 571 | return canusewide; |
| 572 | } |
| 573 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 574 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 575 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 576 | posix_1str(PyObject *args, char *format, int (*func)(const char*), |
| 577 | char *wformat, int (*wfunc)(const Py_UNICODE*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 578 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 579 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 580 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 581 | #ifdef Py_WIN_WIDE_FILENAMES |
| 582 | if (unicode_file_names()) { |
| 583 | PyUnicodeObject *po; |
| 584 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 585 | Py_BEGIN_ALLOW_THREADS |
| 586 | /* PyUnicode_AS_UNICODE OK without thread |
| 587 | lock as it is a simple dereference. */ |
| 588 | res = (*wfunc)(PyUnicode_AS_UNICODE(po)); |
| 589 | Py_END_ALLOW_THREADS |
| 590 | if (res < 0) |
Mark Hammond | d389036 | 2002-10-03 07:24:48 +0000 | [diff] [blame] | 591 | return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 592 | Py_INCREF(Py_None); |
| 593 | return Py_None; |
| 594 | } |
| 595 | /* Drop the argument parsing error as narrow |
| 596 | strings are also valid. */ |
| 597 | PyErr_Clear(); |
| 598 | } |
| 599 | #else |
| 600 | /* Platforms that don't support Unicode filenames |
| 601 | shouldn't be passing these extra params */ |
| 602 | assert(wformat==NULL && wfunc == NULL); |
| 603 | #endif |
| 604 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 605 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 606 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 607 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 608 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 609 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 610 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 611 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 612 | return posix_error_with_allocated_filename(path1); |
| 613 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 614 | Py_INCREF(Py_None); |
| 615 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 618 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 619 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 620 | char *format, |
| 621 | int (*func)(const char *, const char *), |
| 622 | char *wformat, |
| 623 | int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 624 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 625 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 626 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 627 | #ifdef Py_WIN_WIDE_FILENAMES |
| 628 | if (unicode_file_names()) { |
| 629 | PyObject *po1; |
| 630 | PyObject *po2; |
| 631 | if (PyArg_ParseTuple(args, wformat, &po1, &po2)) { |
| 632 | if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) { |
| 633 | PyObject *wpath1; |
| 634 | PyObject *wpath2; |
| 635 | wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1); |
| 636 | wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2); |
| 637 | if (!wpath1 || !wpath2) { |
| 638 | Py_XDECREF(wpath1); |
| 639 | Py_XDECREF(wpath2); |
| 640 | return NULL; |
| 641 | } |
| 642 | Py_BEGIN_ALLOW_THREADS |
| 643 | /* PyUnicode_AS_UNICODE OK without thread |
| 644 | lock as it is a simple dereference. */ |
| 645 | res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1), |
| 646 | PyUnicode_AS_UNICODE(wpath2)); |
| 647 | Py_END_ALLOW_THREADS |
| 648 | Py_XDECREF(wpath1); |
| 649 | Py_XDECREF(wpath2); |
| 650 | if (res != 0) |
| 651 | return posix_error(); |
| 652 | Py_INCREF(Py_None); |
| 653 | return Py_None; |
| 654 | } |
| 655 | /* Else flow through as neither is Unicode. */ |
| 656 | } |
| 657 | /* Drop the argument parsing error as narrow |
| 658 | strings are also valid. */ |
| 659 | PyErr_Clear(); |
| 660 | } |
| 661 | #else |
| 662 | /* Platforms that don't support Unicode filenames |
| 663 | shouldn't be passing these extra params */ |
| 664 | assert(wformat==NULL && wfunc == NULL); |
| 665 | #endif |
| 666 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 667 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 668 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 669 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 670 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 671 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 672 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 673 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 674 | PyMem_Free(path1); |
| 675 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 676 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 677 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 678 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 679 | Py_INCREF(Py_None); |
| 680 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 683 | #ifdef MS_WINDOWS |
| 684 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 685 | - time stamps are restricted to second resolution |
| 686 | - file modification times suffer from forth-and-back conversions between |
| 687 | UTC and local time |
| 688 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 689 | */ |
| 690 | #define HAVE_STAT_NSEC 1 |
| 691 | |
| 692 | struct win32_stat{ |
| 693 | int st_dev; |
| 694 | __int64 st_ino; |
| 695 | unsigned short st_mode; |
| 696 | int st_nlink; |
| 697 | int st_uid; |
| 698 | int st_gid; |
| 699 | int st_rdev; |
| 700 | __int64 st_size; |
| 701 | int st_atime; |
| 702 | int st_atime_nsec; |
| 703 | int st_mtime; |
| 704 | int st_mtime_nsec; |
| 705 | int st_ctime; |
| 706 | int st_ctime_nsec; |
| 707 | }; |
| 708 | |
| 709 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 710 | |
| 711 | static void |
| 712 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 713 | { |
| 714 | /* XXX endianness */ |
| 715 | __int64 in = *(__int64*)in_ptr; |
| 716 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 717 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 718 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 719 | } |
| 720 | |
| 721 | /* Below, we *know* that ugo+r is 0444 */ |
| 722 | #if _S_IREAD != 0400 |
| 723 | #error Unsupported C library |
| 724 | #endif |
| 725 | static int |
| 726 | attributes_to_mode(DWORD attr) |
| 727 | { |
| 728 | int m = 0; |
| 729 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 730 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 731 | else |
| 732 | m |= _S_IFREG; |
| 733 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 734 | m |= 0444; |
| 735 | else |
| 736 | m |= 0666; |
| 737 | return m; |
| 738 | } |
| 739 | |
| 740 | static int |
| 741 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 742 | { |
| 743 | memset(result, 0, sizeof(*result)); |
| 744 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 745 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 746 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 747 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 748 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 749 | |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | static int |
| 754 | win32_stat(const char* path, struct win32_stat *result) |
| 755 | { |
| 756 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 757 | int code; |
| 758 | char *dot; |
| 759 | /* XXX not supported on Win95 and NT 3.x */ |
| 760 | if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
| 761 | /* Protocol violation: we explicitly clear errno, instead of |
| 762 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 763 | errno = 0; |
| 764 | return -1; |
| 765 | } |
| 766 | code = attribute_data_to_stat(&info, result); |
| 767 | if (code != 0) |
| 768 | return code; |
| 769 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 770 | dot = strrchr(path, '.'); |
| 771 | if (dot) { |
| 772 | if (stricmp(dot, ".bat") == 0 || |
| 773 | stricmp(dot, ".cmd") == 0 || |
| 774 | stricmp(dot, ".exe") == 0 || |
| 775 | stricmp(dot, ".com") == 0) |
| 776 | result->st_mode |= 0111; |
| 777 | } |
| 778 | return code; |
| 779 | } |
| 780 | |
| 781 | static int |
| 782 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 783 | { |
| 784 | int code; |
| 785 | const wchar_t *dot; |
| 786 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 787 | /* XXX not supported on Win95 and NT 3.x */ |
| 788 | if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
| 789 | /* Protocol violation: we explicitly clear errno, instead of |
| 790 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 791 | errno = 0; |
| 792 | return -1; |
| 793 | } |
| 794 | code = attribute_data_to_stat(&info, result); |
| 795 | if (code < 0) |
| 796 | return code; |
| 797 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 798 | dot = wcsrchr(path, '.'); |
| 799 | if (dot) { |
| 800 | if (_wcsicmp(dot, L".bat") == 0 || |
| 801 | _wcsicmp(dot, L".cmd") == 0 || |
| 802 | _wcsicmp(dot, L".exe") == 0 || |
| 803 | _wcsicmp(dot, L".com") == 0) |
| 804 | result->st_mode |= 0111; |
| 805 | } |
| 806 | return code; |
| 807 | } |
| 808 | |
| 809 | static int |
| 810 | win32_fstat(int file_number, struct win32_stat *result) |
| 811 | { |
| 812 | BY_HANDLE_FILE_INFORMATION info; |
| 813 | HANDLE h; |
| 814 | int type; |
| 815 | |
| 816 | h = (HANDLE)_get_osfhandle(file_number); |
| 817 | |
| 818 | /* Protocol violation: we explicitly clear errno, instead of |
| 819 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 820 | errno = 0; |
| 821 | |
| 822 | if (h == INVALID_HANDLE_VALUE) { |
| 823 | /* This is really a C library error (invalid file handle). |
| 824 | We set the Win32 error to the closes one matching. */ |
| 825 | SetLastError(ERROR_INVALID_HANDLE); |
| 826 | return -1; |
| 827 | } |
| 828 | memset(result, 0, sizeof(*result)); |
| 829 | |
| 830 | type = GetFileType(h); |
| 831 | if (type == FILE_TYPE_UNKNOWN) { |
| 832 | DWORD error = GetLastError(); |
| 833 | if (error != 0) { |
| 834 | return -1; |
| 835 | } |
| 836 | /* else: valid but unknown file */ |
| 837 | } |
| 838 | |
| 839 | if (type != FILE_TYPE_DISK) { |
| 840 | if (type == FILE_TYPE_CHAR) |
| 841 | result->st_mode = _S_IFCHR; |
| 842 | else if (type == FILE_TYPE_PIPE) |
| 843 | result->st_mode = _S_IFIFO; |
| 844 | return 0; |
| 845 | } |
| 846 | |
| 847 | if (!GetFileInformationByHandle(h, &info)) { |
| 848 | return -1; |
| 849 | } |
| 850 | |
| 851 | /* similar to stat() */ |
| 852 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 853 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 854 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 855 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 856 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 857 | /* specific to fstat() */ |
| 858 | result->st_nlink = info.nNumberOfLinks; |
| 859 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | #endif /* MS_WINDOWS */ |
| 864 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 865 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 866 | "stat_result: Result from stat or lstat.\n\n\ |
| 867 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 868 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 869 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 870 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 871 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 872 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 873 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 874 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 875 | |
| 876 | static PyStructSequence_Field stat_result_fields[] = { |
| 877 | {"st_mode", "protection bits"}, |
| 878 | {"st_ino", "inode"}, |
| 879 | {"st_dev", "device"}, |
| 880 | {"st_nlink", "number of hard links"}, |
| 881 | {"st_uid", "user ID of owner"}, |
| 882 | {"st_gid", "group ID of owner"}, |
| 883 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 884 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 885 | {NULL, "integer time of last access"}, |
| 886 | {NULL, "integer time of last modification"}, |
| 887 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 888 | {"st_atime", "time of last access"}, |
| 889 | {"st_mtime", "time of last modification"}, |
| 890 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 891 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 892 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 893 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 894 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 895 | {"st_blocks", "number of blocks allocated"}, |
| 896 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 897 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 898 | {"st_rdev", "device type (if inode device)"}, |
| 899 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 900 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 901 | {"st_flags", "user defined flags for file"}, |
| 902 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 903 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 904 | {"st_gen", "generation number"}, |
| 905 | #endif |
| 906 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 907 | {"st_birthtime", "time of creation"}, |
| 908 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 909 | {0} |
| 910 | }; |
| 911 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 912 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 913 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 914 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 915 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 916 | #endif |
| 917 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 918 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 919 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 920 | #else |
| 921 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 922 | #endif |
| 923 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 924 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 925 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 926 | #else |
| 927 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 928 | #endif |
| 929 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 930 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 931 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 932 | #else |
| 933 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 934 | #endif |
| 935 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 936 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 937 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 938 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 939 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 940 | #endif |
| 941 | |
| 942 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 943 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 944 | #else |
| 945 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 946 | #endif |
| 947 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 948 | static PyStructSequence_Desc stat_result_desc = { |
| 949 | "stat_result", /* name */ |
| 950 | stat_result__doc__, /* doc */ |
| 951 | stat_result_fields, |
| 952 | 10 |
| 953 | }; |
| 954 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 955 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 956 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 957 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 958 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 959 | or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 960 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 961 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 962 | |
| 963 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 964 | {"f_bsize", }, |
| 965 | {"f_frsize", }, |
| 966 | {"f_blocks", }, |
| 967 | {"f_bfree", }, |
| 968 | {"f_bavail", }, |
| 969 | {"f_files", }, |
| 970 | {"f_ffree", }, |
| 971 | {"f_favail", }, |
| 972 | {"f_flag", }, |
| 973 | {"f_namemax",}, |
| 974 | {0} |
| 975 | }; |
| 976 | |
| 977 | static PyStructSequence_Desc statvfs_result_desc = { |
| 978 | "statvfs_result", /* name */ |
| 979 | statvfs_result__doc__, /* doc */ |
| 980 | statvfs_result_fields, |
| 981 | 10 |
| 982 | }; |
| 983 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 984 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 985 | static PyTypeObject StatResultType; |
| 986 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 987 | static newfunc structseq_new; |
| 988 | |
| 989 | static PyObject * |
| 990 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 991 | { |
| 992 | PyStructSequence *result; |
| 993 | int i; |
| 994 | |
| 995 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 996 | if (!result) |
| 997 | return NULL; |
| 998 | /* If we have been initialized from a tuple, |
| 999 | st_?time might be set to None. Initialize it |
| 1000 | from the int slots. */ |
| 1001 | for (i = 7; i <= 9; i++) { |
| 1002 | if (result->ob_item[i+3] == Py_None) { |
| 1003 | Py_DECREF(Py_None); |
| 1004 | Py_INCREF(result->ob_item[i]); |
| 1005 | result->ob_item[i+3] = result->ob_item[i]; |
| 1006 | } |
| 1007 | } |
| 1008 | return (PyObject*)result; |
| 1009 | } |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1014 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1015 | |
| 1016 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1017 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1018 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1019 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1020 | future calls return ints. \n\ |
| 1021 | If newval is omitted, return the current setting.\n"); |
| 1022 | |
| 1023 | static PyObject* |
| 1024 | stat_float_times(PyObject* self, PyObject *args) |
| 1025 | { |
| 1026 | int newval = -1; |
| 1027 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1028 | return NULL; |
| 1029 | if (newval == -1) |
| 1030 | /* Return old value */ |
| 1031 | return PyBool_FromLong(_stat_float_times); |
| 1032 | _stat_float_times = newval; |
| 1033 | Py_INCREF(Py_None); |
| 1034 | return Py_None; |
| 1035 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1036 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1037 | static void |
| 1038 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1039 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1040 | PyObject *fval,*ival; |
| 1041 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1042 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1043 | #else |
| 1044 | ival = PyInt_FromLong((long)sec); |
| 1045 | #endif |
| 1046 | if (_stat_float_times) { |
| 1047 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1048 | } else { |
| 1049 | fval = ival; |
| 1050 | Py_INCREF(fval); |
| 1051 | } |
| 1052 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1053 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1056 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1057 | (used by posix_stat() and posix_fstat()) */ |
| 1058 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1059 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1060 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1061 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1062 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1063 | if (v == NULL) |
| 1064 | return NULL; |
| 1065 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1066 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1067 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1068 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1069 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1070 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1071 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1072 | #endif |
| 1073 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1074 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1075 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1076 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1077 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1078 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1079 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); |
| 1080 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); |
| 1081 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1082 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1083 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1084 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1085 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1086 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1087 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1088 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1089 | #if defined(HAVE_STAT_TV_NSEC) |
| 1090 | ansec = st->st_atim.tv_nsec; |
| 1091 | mnsec = st->st_mtim.tv_nsec; |
| 1092 | cnsec = st->st_ctim.tv_nsec; |
| 1093 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1094 | ansec = st->st_atimespec.tv_nsec; |
| 1095 | mnsec = st->st_mtimespec.tv_nsec; |
| 1096 | cnsec = st->st_ctimespec.tv_nsec; |
| 1097 | #elif defined(HAVE_STAT_NSEC) |
| 1098 | ansec = st->st_atime_nsec; |
| 1099 | mnsec = st->st_mtime_nsec; |
| 1100 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1101 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1102 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1103 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1104 | fill_time(v, 7, st->st_atime, ansec); |
| 1105 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1106 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1107 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1108 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1109 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1110 | PyInt_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1111 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1112 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1113 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1114 | PyInt_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1115 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1116 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1117 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1118 | PyInt_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1119 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1120 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1121 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1122 | PyInt_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1123 | #endif |
| 1124 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1125 | { |
| 1126 | PyObject *val; |
| 1127 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1128 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1129 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1130 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1131 | #else |
| 1132 | bnsec = 0; |
| 1133 | #endif |
| 1134 | if (_stat_float_times) { |
| 1135 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1136 | } else { |
| 1137 | val = PyInt_FromLong((long)bsec); |
| 1138 | } |
| 1139 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1140 | val); |
| 1141 | } |
| 1142 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1143 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1144 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1145 | PyInt_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1146 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1147 | |
| 1148 | if (PyErr_Occurred()) { |
| 1149 | Py_DECREF(v); |
| 1150 | return NULL; |
| 1151 | } |
| 1152 | |
| 1153 | return v; |
| 1154 | } |
| 1155 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1156 | #ifdef MS_WINDOWS |
| 1157 | |
| 1158 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1159 | where / can be used in place of \ and the trailing slash is optional. |
| 1160 | Both SERVER and SHARE must have at least one character. |
| 1161 | */ |
| 1162 | |
| 1163 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1164 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
| 1165 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
| 1166 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1167 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1168 | IsUNCRootA(char *path, int pathlen) |
| 1169 | { |
| 1170 | #define ISSLASH ISSLASHA |
| 1171 | |
| 1172 | int i, share; |
| 1173 | |
| 1174 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1175 | /* minimum UNCRoot is \\x\y */ |
| 1176 | return FALSE; |
| 1177 | for (i = 2; i < pathlen ; i++) |
| 1178 | if (ISSLASH(path[i])) break; |
| 1179 | if (i == 2 || i == pathlen) |
| 1180 | /* do not allow \\\SHARE or \\SERVER */ |
| 1181 | return FALSE; |
| 1182 | share = i+1; |
| 1183 | for (i = share; i < pathlen; i++) |
| 1184 | if (ISSLASH(path[i])) break; |
| 1185 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1186 | |
| 1187 | #undef ISSLASH |
| 1188 | } |
| 1189 | |
| 1190 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1191 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1192 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1193 | { |
| 1194 | #define ISSLASH ISSLASHW |
| 1195 | |
| 1196 | int i, share; |
| 1197 | |
| 1198 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1199 | /* minimum UNCRoot is \\x\y */ |
| 1200 | return FALSE; |
| 1201 | for (i = 2; i < pathlen ; i++) |
| 1202 | if (ISSLASH(path[i])) break; |
| 1203 | if (i == 2 || i == pathlen) |
| 1204 | /* do not allow \\\SHARE or \\SERVER */ |
| 1205 | return FALSE; |
| 1206 | share = i+1; |
| 1207 | for (i = share; i < pathlen; i++) |
| 1208 | if (ISSLASH(path[i])) break; |
| 1209 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1210 | |
| 1211 | #undef ISSLASH |
| 1212 | } |
| 1213 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1214 | #endif /* MS_WINDOWS */ |
| 1215 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1216 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1217 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1218 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1219 | #ifdef __VMS |
| 1220 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1221 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1222 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1223 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1224 | char *wformat, |
| 1225 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1226 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1227 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1228 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1229 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1230 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1231 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1232 | |
| 1233 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1234 | /* If on wide-character-capable OS see if argument |
| 1235 | is Unicode and if so use wide API. */ |
| 1236 | if (unicode_file_names()) { |
| 1237 | PyUnicodeObject *po; |
| 1238 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1239 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1240 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1241 | Py_BEGIN_ALLOW_THREADS |
| 1242 | /* PyUnicode_AS_UNICODE result OK without |
| 1243 | thread lock as it is a simple dereference. */ |
| 1244 | res = wstatfunc(wpath, &st); |
| 1245 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1246 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1247 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1248 | return win32_error_unicode("stat", wpath); |
| 1249 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1250 | } |
| 1251 | /* Drop the argument parsing error as narrow strings |
| 1252 | are also valid. */ |
| 1253 | PyErr_Clear(); |
| 1254 | } |
| 1255 | #endif |
| 1256 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1257 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1258 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1259 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1260 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1261 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1262 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1263 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1264 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1265 | |
| 1266 | if (res != 0) { |
| 1267 | #ifdef MS_WINDOWS |
| 1268 | result = win32_error("stat", pathfree); |
| 1269 | #else |
| 1270 | result = posix_error_with_filename(pathfree); |
| 1271 | #endif |
| 1272 | } |
| 1273 | else |
| 1274 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1275 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1276 | PyMem_Free(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1277 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1280 | /* POSIX methods */ |
| 1281 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1282 | PyDoc_STRVAR(posix_access__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1283 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1284 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1285 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1286 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1287 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1288 | 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] | 1289 | |
| 1290 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1291 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1292 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1293 | char *path; |
| 1294 | int mode; |
| 1295 | int res; |
| 1296 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1297 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1298 | if (unicode_file_names()) { |
| 1299 | PyUnicodeObject *po; |
| 1300 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1301 | Py_BEGIN_ALLOW_THREADS |
| 1302 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1303 | it is a simple dereference. */ |
| 1304 | res = _waccess(PyUnicode_AS_UNICODE(po), mode); |
| 1305 | Py_END_ALLOW_THREADS |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1306 | return PyBool_FromLong(res == 0); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1307 | } |
| 1308 | /* Drop the argument parsing error as narrow strings |
| 1309 | are also valid. */ |
| 1310 | PyErr_Clear(); |
| 1311 | } |
| 1312 | #endif |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame] | 1313 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1314 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1315 | return NULL; |
| 1316 | Py_BEGIN_ALLOW_THREADS |
| 1317 | res = access(path, mode); |
| 1318 | Py_END_ALLOW_THREADS |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 1319 | PyMem_Free(path); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1320 | return PyBool_FromLong(res == 0); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1323 | #ifndef F_OK |
| 1324 | #define F_OK 0 |
| 1325 | #endif |
| 1326 | #ifndef R_OK |
| 1327 | #define R_OK 4 |
| 1328 | #endif |
| 1329 | #ifndef W_OK |
| 1330 | #define W_OK 2 |
| 1331 | #endif |
| 1332 | #ifndef X_OK |
| 1333 | #define X_OK 1 |
| 1334 | #endif |
| 1335 | |
| 1336 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1337 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1338 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1339 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1340 | |
| 1341 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1342 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1343 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1344 | int id; |
| 1345 | char *ret; |
| 1346 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1347 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1348 | return NULL; |
| 1349 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1350 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1351 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1352 | if (id == 0) { |
| 1353 | ret = ttyname(); |
| 1354 | } |
| 1355 | else { |
| 1356 | ret = NULL; |
| 1357 | } |
| 1358 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1359 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1360 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1361 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1362 | return posix_error(); |
| 1363 | return PyString_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1364 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1365 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1366 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1367 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1368 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1369 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1370 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1371 | |
| 1372 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1373 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1374 | { |
| 1375 | char *ret; |
| 1376 | char buffer[L_ctermid]; |
| 1377 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1378 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1379 | ret = ctermid_r(buffer); |
| 1380 | #else |
| 1381 | ret = ctermid(buffer); |
| 1382 | #endif |
| 1383 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1384 | return posix_error(); |
| 1385 | return PyString_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1386 | } |
| 1387 | #endif |
| 1388 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1389 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1390 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1391 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1392 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1393 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1394 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1395 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1396 | #ifdef MS_WINDOWS |
| 1397 | return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir); |
| 1398 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1399 | return posix_1str(args, "et:chdir", _chdir2, NULL, NULL); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1400 | #elif defined(__VMS) |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1401 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1402 | NULL, NULL); |
| 1403 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1404 | return posix_1str(args, "et:chdir", chdir, NULL, NULL); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1405 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1408 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1409 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1410 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1411 | 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] | 1412 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1413 | |
| 1414 | static PyObject * |
| 1415 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1416 | { |
| 1417 | return posix_fildes(fdobj, fchdir); |
| 1418 | } |
| 1419 | #endif /* HAVE_FCHDIR */ |
| 1420 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1421 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1422 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1423 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1424 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1425 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1426 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1427 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1428 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1429 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1430 | int i; |
| 1431 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1432 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1433 | if (unicode_file_names()) { |
| 1434 | PyUnicodeObject *po; |
| 1435 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1436 | Py_BEGIN_ALLOW_THREADS |
| 1437 | res = _wchmod(PyUnicode_AS_UNICODE(po), i); |
| 1438 | Py_END_ALLOW_THREADS |
| 1439 | if (res < 0) |
| 1440 | return posix_error_with_unicode_filename( |
| 1441 | PyUnicode_AS_UNICODE(po)); |
| 1442 | Py_INCREF(Py_None); |
| 1443 | return Py_None; |
| 1444 | } |
| 1445 | /* Drop the argument parsing error as narrow strings |
| 1446 | are also valid. */ |
| 1447 | PyErr_Clear(); |
| 1448 | } |
| 1449 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1450 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1451 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1452 | return NULL; |
| 1453 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1454 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1455 | Py_END_ALLOW_THREADS |
| 1456 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1457 | return posix_error_with_allocated_filename(path); |
| 1458 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1459 | Py_INCREF(Py_None); |
| 1460 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1463 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1464 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1465 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1466 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1467 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1468 | |
| 1469 | static PyObject * |
| 1470 | posix_chroot(PyObject *self, PyObject *args) |
| 1471 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1472 | return posix_1str(args, "et:chroot", chroot, NULL, NULL); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1473 | } |
| 1474 | #endif |
| 1475 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1476 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1477 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1478 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1479 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1480 | |
| 1481 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1482 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1483 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1484 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1485 | } |
| 1486 | #endif /* HAVE_FSYNC */ |
| 1487 | |
| 1488 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1489 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1490 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1491 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1492 | #endif |
| 1493 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1494 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1495 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1496 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1497 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1498 | |
| 1499 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1500 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1501 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1502 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1503 | } |
| 1504 | #endif /* HAVE_FDATASYNC */ |
| 1505 | |
| 1506 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1507 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1508 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1509 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1510 | 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] | 1511 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1512 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1513 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1514 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1515 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1516 | int uid, gid; |
| 1517 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1518 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1519 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1520 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1521 | return NULL; |
| 1522 | Py_BEGIN_ALLOW_THREADS |
| 1523 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1524 | Py_END_ALLOW_THREADS |
| 1525 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1526 | return posix_error_with_allocated_filename(path); |
| 1527 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1528 | Py_INCREF(Py_None); |
| 1529 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1530 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1531 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1532 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1533 | #ifdef HAVE_LCHOWN |
| 1534 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1535 | "lchown(path, uid, gid)\n\n\ |
| 1536 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1537 | This function will not follow symbolic links."); |
| 1538 | |
| 1539 | static PyObject * |
| 1540 | posix_lchown(PyObject *self, PyObject *args) |
| 1541 | { |
| 1542 | char *path = NULL; |
| 1543 | int uid, gid; |
| 1544 | int res; |
| 1545 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1546 | Py_FileSystemDefaultEncoding, &path, |
| 1547 | &uid, &gid)) |
| 1548 | return NULL; |
| 1549 | Py_BEGIN_ALLOW_THREADS |
| 1550 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1551 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1552 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1553 | return posix_error_with_allocated_filename(path); |
| 1554 | PyMem_Free(path); |
| 1555 | Py_INCREF(Py_None); |
| 1556 | return Py_None; |
| 1557 | } |
| 1558 | #endif /* HAVE_LCHOWN */ |
| 1559 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1560 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1561 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1562 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1563 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1564 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1565 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1566 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1567 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1568 | { |
| 1569 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1570 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1571 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1572 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1573 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1574 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1575 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1576 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1577 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1578 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1579 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1580 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1581 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1582 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1583 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1584 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1585 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1586 | "getcwdu() -> path\n\n\ |
| 1587 | Return a unicode string representing the current working directory."); |
| 1588 | |
| 1589 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1590 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1591 | { |
| 1592 | char buf[1026]; |
| 1593 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1594 | |
| 1595 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1596 | if (unicode_file_names()) { |
| 1597 | wchar_t *wres; |
| 1598 | wchar_t wbuf[1026]; |
| 1599 | Py_BEGIN_ALLOW_THREADS |
| 1600 | wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]); |
| 1601 | Py_END_ALLOW_THREADS |
| 1602 | if (wres == NULL) |
| 1603 | return posix_error(); |
| 1604 | return PyUnicode_FromWideChar(wbuf, wcslen(wbuf)); |
| 1605 | } |
| 1606 | #endif |
| 1607 | |
| 1608 | Py_BEGIN_ALLOW_THREADS |
| 1609 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1610 | res = _getcwd2(buf, sizeof buf); |
| 1611 | #else |
| 1612 | res = getcwd(buf, sizeof buf); |
| 1613 | #endif |
| 1614 | Py_END_ALLOW_THREADS |
| 1615 | if (res == NULL) |
| 1616 | return posix_error(); |
| 1617 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1618 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1619 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1620 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1621 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1622 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1623 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1624 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1625 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1626 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1627 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1628 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1629 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1630 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1631 | return posix_2str(args, "etet:link", link, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1632 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1633 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1634 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1635 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1636 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1637 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1638 | Return a list containing the names of the entries in the directory.\n\ |
| 1639 | \n\ |
| 1640 | path: path of directory to list\n\ |
| 1641 | \n\ |
| 1642 | 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] | 1643 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1644 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1645 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1646 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1647 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1648 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1649 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1650 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1651 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1652 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1653 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1654 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1655 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1656 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 1657 | char namebuf[MAX_PATH*2+5]; |
| 1658 | char *bufptr = namebuf; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1659 | Py_ssize_t len = sizeof(namebuf)/sizeof(namebuf[0]); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1660 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1661 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1662 | /* If on wide-character-capable OS see if argument |
| 1663 | is Unicode and if so use wide API. */ |
| 1664 | if (unicode_file_names()) { |
| 1665 | PyUnicodeObject *po; |
| 1666 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1667 | WIN32_FIND_DATAW wFileData; |
| 1668 | Py_UNICODE wnamebuf[MAX_PATH*2+5]; |
| 1669 | Py_UNICODE wch; |
| 1670 | wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH); |
| 1671 | wnamebuf[MAX_PATH] = L'\0'; |
| 1672 | len = wcslen(wnamebuf); |
| 1673 | wch = (len > 0) ? wnamebuf[len-1] : L'\0'; |
| 1674 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1675 | wnamebuf[len++] = L'/'; |
| 1676 | wcscpy(wnamebuf + len, L"*.*"); |
| 1677 | if ((d = PyList_New(0)) == NULL) |
| 1678 | return NULL; |
| 1679 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1680 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1681 | errno = GetLastError(); |
| 1682 | if (errno == ERROR_FILE_NOT_FOUND) { |
| 1683 | return d; |
| 1684 | } |
| 1685 | Py_DECREF(d); |
| 1686 | return win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1687 | } |
| 1688 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1689 | /* Skip over . and .. */ |
| 1690 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 1691 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 1692 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1693 | if (v == NULL) { |
| 1694 | Py_DECREF(d); |
| 1695 | d = NULL; |
| 1696 | break; |
| 1697 | } |
| 1698 | if (PyList_Append(d, v) != 0) { |
| 1699 | Py_DECREF(v); |
| 1700 | Py_DECREF(d); |
| 1701 | d = NULL; |
| 1702 | break; |
| 1703 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1704 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1705 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1706 | Py_BEGIN_ALLOW_THREADS |
| 1707 | result = FindNextFileW(hFindFile, &wFileData); |
| 1708 | Py_END_ALLOW_THREADS |
| 1709 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1710 | |
| 1711 | if (FindClose(hFindFile) == FALSE) { |
| 1712 | Py_DECREF(d); |
| 1713 | return win32_error_unicode("FindClose", wnamebuf); |
| 1714 | } |
| 1715 | return d; |
| 1716 | } |
| 1717 | /* Drop the argument parsing error as narrow strings |
| 1718 | are also valid. */ |
| 1719 | PyErr_Clear(); |
| 1720 | } |
| 1721 | #endif |
| 1722 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1723 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1724 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1725 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1726 | if (len > 0) { |
| 1727 | char ch = namebuf[len-1]; |
| 1728 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 1729 | namebuf[len++] = '/'; |
| 1730 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1731 | strcpy(namebuf + len, "*.*"); |
| 1732 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1733 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1734 | return NULL; |
| 1735 | |
| 1736 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 1737 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1738 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 1739 | if (errno == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1740 | return d; |
| 1741 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1742 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1743 | } |
| 1744 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1745 | /* Skip over . and .. */ |
| 1746 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 1747 | strcmp(FileData.cFileName, "..") != 0) { |
| 1748 | v = PyString_FromString(FileData.cFileName); |
| 1749 | if (v == NULL) { |
| 1750 | Py_DECREF(d); |
| 1751 | d = NULL; |
| 1752 | break; |
| 1753 | } |
| 1754 | if (PyList_Append(d, v) != 0) { |
| 1755 | Py_DECREF(v); |
| 1756 | Py_DECREF(d); |
| 1757 | d = NULL; |
| 1758 | break; |
| 1759 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1760 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1761 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1762 | Py_BEGIN_ALLOW_THREADS |
| 1763 | result = FindNextFile(hFindFile, &FileData); |
| 1764 | Py_END_ALLOW_THREADS |
| 1765 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1766 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1767 | if (FindClose(hFindFile) == FALSE) { |
| 1768 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1769 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1770 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1771 | |
| 1772 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1773 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1774 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1775 | |
| 1776 | #ifndef MAX_PATH |
| 1777 | #define MAX_PATH CCHMAXPATH |
| 1778 | #endif |
| 1779 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 1780 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1781 | PyObject *d, *v; |
| 1782 | char namebuf[MAX_PATH+5]; |
| 1783 | HDIR hdir = 1; |
| 1784 | ULONG srchcnt = 1; |
| 1785 | FILEFINDBUF3 ep; |
| 1786 | APIRET rc; |
| 1787 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1788 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1789 | return NULL; |
| 1790 | if (len >= MAX_PATH) { |
| 1791 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1792 | return NULL; |
| 1793 | } |
| 1794 | strcpy(namebuf, name); |
| 1795 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1796 | if (*pt == ALTSEP) |
| 1797 | *pt = SEP; |
| 1798 | if (namebuf[len-1] != SEP) |
| 1799 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1800 | strcpy(namebuf + len, "*.*"); |
| 1801 | |
| 1802 | if ((d = PyList_New(0)) == NULL) |
| 1803 | return NULL; |
| 1804 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1805 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1806 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1807 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1808 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1809 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1810 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1811 | |
| 1812 | if (rc != NO_ERROR) { |
| 1813 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1814 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1817 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1818 | do { |
| 1819 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1820 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1821 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1822 | |
| 1823 | strcpy(namebuf, ep.achName); |
| 1824 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1825 | /* Leave Case of Name Alone -- In Native Form */ |
| 1826 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1827 | |
| 1828 | v = PyString_FromString(namebuf); |
| 1829 | if (v == NULL) { |
| 1830 | Py_DECREF(d); |
| 1831 | d = NULL; |
| 1832 | break; |
| 1833 | } |
| 1834 | if (PyList_Append(d, v) != 0) { |
| 1835 | Py_DECREF(v); |
| 1836 | Py_DECREF(d); |
| 1837 | d = NULL; |
| 1838 | break; |
| 1839 | } |
| 1840 | Py_DECREF(v); |
| 1841 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1842 | } |
| 1843 | |
| 1844 | return d; |
| 1845 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1846 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1847 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1848 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1849 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1850 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1851 | int arg_is_unicode = 1; |
| 1852 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 1853 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1854 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 1855 | arg_is_unicode = 0; |
| 1856 | PyErr_Clear(); |
| 1857 | } |
| 1858 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1859 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1860 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1861 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1862 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1863 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1864 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1865 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1866 | return NULL; |
| 1867 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1868 | for (;;) { |
| 1869 | Py_BEGIN_ALLOW_THREADS |
| 1870 | ep = readdir(dirp); |
| 1871 | Py_END_ALLOW_THREADS |
| 1872 | if (ep == NULL) |
| 1873 | break; |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1874 | if (ep->d_name[0] == '.' && |
| 1875 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1876 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1877 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1878 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1879 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1880 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1881 | d = NULL; |
| 1882 | break; |
| 1883 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1884 | #ifdef Py_USING_UNICODE |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1885 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1886 | PyObject *w; |
| 1887 | |
| 1888 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1889 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1890 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 1891 | if (w != NULL) { |
| 1892 | Py_DECREF(v); |
| 1893 | v = w; |
| 1894 | } |
| 1895 | else { |
| 1896 | /* fall back to the original byte string, as |
| 1897 | discussed in patch #683592 */ |
| 1898 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1899 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1900 | } |
| 1901 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1902 | if (PyList_Append(d, v) != 0) { |
| 1903 | Py_DECREF(v); |
| 1904 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1905 | d = NULL; |
| 1906 | break; |
| 1907 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1908 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1909 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 1910 | if (errno != 0 && d != NULL) { |
| 1911 | /* readdir() returned NULL and set errno */ |
| 1912 | closedir(dirp); |
| 1913 | Py_DECREF(d); |
| 1914 | return posix_error_with_allocated_filename(name); |
| 1915 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1916 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1917 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1918 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1919 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1920 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1921 | #endif /* which OS */ |
| 1922 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1923 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1924 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1925 | /* A helper function for abspath on win32 */ |
| 1926 | static PyObject * |
| 1927 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1928 | { |
| 1929 | /* assume encoded strings wont more than double no of chars */ |
| 1930 | char inbuf[MAX_PATH*2]; |
| 1931 | char *inbufp = inbuf; |
Tim Peters | 67d70eb | 2006-03-01 04:35:45 +0000 | [diff] [blame] | 1932 | Py_ssize_t insize = sizeof(inbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1933 | char outbuf[MAX_PATH*2]; |
| 1934 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1935 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1936 | if (unicode_file_names()) { |
| 1937 | PyUnicodeObject *po; |
| 1938 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 1939 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 1940 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1941 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1942 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 1943 | woutbuf, &wtemp)) |
| 1944 | return win32_error("GetFullPathName", ""); |
| 1945 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 1946 | } |
| 1947 | /* Drop the argument parsing error as narrow strings |
| 1948 | are also valid. */ |
| 1949 | PyErr_Clear(); |
| 1950 | } |
| 1951 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1952 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1953 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1954 | &insize)) |
| 1955 | return NULL; |
| 1956 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1957 | outbuf, &temp)) |
| 1958 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 1959 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 1960 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 1961 | Py_FileSystemDefaultEncoding, NULL); |
| 1962 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1963 | return PyString_FromString(outbuf); |
| 1964 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1965 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1966 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1967 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1968 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1969 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1970 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1971 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1972 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1973 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1974 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1975 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1976 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1977 | |
| 1978 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1979 | if (unicode_file_names()) { |
| 1980 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 1981 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1982 | Py_BEGIN_ALLOW_THREADS |
| 1983 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1984 | it is a simple dereference. */ |
| 1985 | res = _wmkdir(PyUnicode_AS_UNICODE(po)); |
| 1986 | Py_END_ALLOW_THREADS |
| 1987 | if (res < 0) |
| 1988 | return posix_error(); |
| 1989 | Py_INCREF(Py_None); |
| 1990 | return Py_None; |
| 1991 | } |
| 1992 | /* Drop the argument parsing error as narrow strings |
| 1993 | are also valid. */ |
| 1994 | PyErr_Clear(); |
| 1995 | } |
| 1996 | #endif |
| 1997 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1998 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1999 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2000 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2001 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2002 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2003 | res = mkdir(path); |
| 2004 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2005 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2006 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2007 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2008 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2009 | return posix_error_with_allocated_filename(path); |
| 2010 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2011 | Py_INCREF(Py_None); |
| 2012 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2015 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 2016 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2017 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2018 | #include <sys/resource.h> |
| 2019 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2020 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 2021 | |
| 2022 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2023 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2024 | "nice(inc) -> new_priority\n\n\ |
| 2025 | 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] | 2026 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2027 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2028 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2029 | { |
| 2030 | int increment, value; |
| 2031 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2032 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2033 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2034 | |
| 2035 | /* There are two flavours of 'nice': one that returns the new |
| 2036 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2037 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2038 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2039 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2040 | If we are of the nice family that returns the new priority, we |
| 2041 | need to clear errno before the call, and check if errno is filled |
| 2042 | before calling posix_error() on a returnvalue of -1, because the |
| 2043 | -1 may be the actual new priority! */ |
| 2044 | |
| 2045 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2046 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2047 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2048 | if (value == 0) |
| 2049 | value = getpriority(PRIO_PROCESS, 0); |
| 2050 | #endif |
| 2051 | if (value == -1 && errno != 0) |
| 2052 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2053 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2054 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2055 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2056 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2057 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2058 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2059 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2060 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2061 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2062 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2063 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2064 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2065 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2066 | #ifdef MS_WINDOWS |
| 2067 | return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename); |
| 2068 | #else |
| 2069 | return posix_2str(args, "etet:rename", rename, NULL, NULL); |
| 2070 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2073 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2074 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2075 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2076 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2077 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2078 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2079 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2080 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2081 | #ifdef MS_WINDOWS |
| 2082 | return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir); |
| 2083 | #else |
| 2084 | return posix_1str(args, "et:rmdir", rmdir, NULL, NULL); |
| 2085 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2088 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2089 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2090 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2091 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2092 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2093 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2094 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2095 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2096 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2097 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2098 | #else |
| 2099 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 2100 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2103 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2104 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2105 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2106 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2107 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2108 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2109 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2110 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2111 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2112 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2113 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2114 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2115 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2116 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2117 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2118 | Py_END_ALLOW_THREADS |
| 2119 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2120 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2121 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2122 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2123 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2124 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2125 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2126 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2127 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2128 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2129 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2130 | { |
| 2131 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2132 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2133 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2134 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2135 | if (i < 0) |
| 2136 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2137 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2140 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2141 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2142 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2143 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2144 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2145 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2146 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2147 | Remove a file (same as unlink(path))."); |
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_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2151 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2152 | #ifdef MS_WINDOWS |
| 2153 | return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink); |
| 2154 | #else |
| 2155 | return posix_1str(args, "et:remove", unlink, NULL, NULL); |
| 2156 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2159 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2160 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2161 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2162 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2163 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2164 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2165 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2166 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2167 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2168 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2169 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2170 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2171 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2172 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2173 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2174 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2175 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2176 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2177 | u.sysname, |
| 2178 | u.nodename, |
| 2179 | u.release, |
| 2180 | u.version, |
| 2181 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2182 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2183 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2184 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2185 | static int |
| 2186 | extract_time(PyObject *t, long* sec, long* usec) |
| 2187 | { |
| 2188 | long intval; |
| 2189 | if (PyFloat_Check(t)) { |
| 2190 | double tval = PyFloat_AsDouble(t); |
| 2191 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 2192 | if (!intobj) |
| 2193 | return -1; |
| 2194 | intval = PyInt_AsLong(intobj); |
| 2195 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2196 | if (intval == -1 && PyErr_Occurred()) |
| 2197 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2198 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2199 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2200 | if (*usec < 0) |
| 2201 | /* If rounding gave us a negative number, |
| 2202 | truncate. */ |
| 2203 | *usec = 0; |
| 2204 | return 0; |
| 2205 | } |
| 2206 | intval = PyInt_AsLong(t); |
| 2207 | if (intval == -1 && PyErr_Occurred()) |
| 2208 | return -1; |
| 2209 | *sec = intval; |
| 2210 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2211 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2212 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2213 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2214 | PyDoc_STRVAR(posix_utime__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2215 | "utime(path, (atime, utime))\n\ |
| 2216 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2217 | 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] | 2218 | 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] | 2219 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2220 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2221 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2222 | { |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2223 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2224 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2225 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2226 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2227 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2228 | #if defined(HAVE_UTIMES) |
| 2229 | struct timeval buf[2]; |
| 2230 | #define ATIME buf[0].tv_sec |
| 2231 | #define MTIME buf[1].tv_sec |
| 2232 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2233 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2234 | struct utimbuf buf; |
| 2235 | #define ATIME buf.actime |
| 2236 | #define MTIME buf.modtime |
| 2237 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2238 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2239 | time_t buf[2]; |
| 2240 | #define ATIME buf[0] |
| 2241 | #define MTIME buf[1] |
| 2242 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2243 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2244 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2245 | int have_unicode_filename = 0; |
| 2246 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2247 | PyUnicodeObject *obwpath; |
| 2248 | wchar_t *wpath; |
| 2249 | if (unicode_file_names()) { |
| 2250 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2251 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2252 | have_unicode_filename = 1; |
| 2253 | } else |
| 2254 | /* Drop the argument parsing error as narrow strings |
| 2255 | are also valid. */ |
| 2256 | PyErr_Clear(); |
| 2257 | } |
| 2258 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 2259 | |
| 2260 | if (!have_unicode_filename && \ |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2261 | !PyArg_ParseTuple(args, "etO:utime", |
| 2262 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2263 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2264 | if (arg == Py_None) { |
| 2265 | /* optional time values not given */ |
| 2266 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2267 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2268 | if (have_unicode_filename) |
| 2269 | res = _wutime(wpath, NULL); |
| 2270 | else |
| 2271 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2272 | res = utime(path, NULL); |
| 2273 | Py_END_ALLOW_THREADS |
| 2274 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2275 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2276 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2277 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2278 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2279 | return NULL; |
| 2280 | } |
| 2281 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2282 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2283 | &atime, &ausec) == -1) { |
| 2284 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2285 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2286 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2287 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2288 | &mtime, &musec) == -1) { |
| 2289 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2290 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2291 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2292 | ATIME = atime; |
| 2293 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2294 | #ifdef HAVE_UTIMES |
| 2295 | buf[0].tv_usec = ausec; |
| 2296 | buf[1].tv_usec = musec; |
| 2297 | Py_BEGIN_ALLOW_THREADS |
| 2298 | res = utimes(path, buf); |
| 2299 | Py_END_ALLOW_THREADS |
| 2300 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2301 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2302 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2303 | if (have_unicode_filename) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 2304 | /* utime is OK with utimbuf, but _wutime insists |
| 2305 | on _utimbuf (the msvc headers assert the |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2306 | underscore version is ansi) */ |
| 2307 | res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG); |
| 2308 | else |
| 2309 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2310 | res = utime(path, UTIME_ARG); |
| 2311 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2312 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2313 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2314 | if (res < 0) { |
| 2315 | #ifdef Py_WIN_WIDE_FILENAMES |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2316 | if (have_unicode_filename) |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2317 | return posix_error_with_unicode_filename(wpath); |
| 2318 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2319 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2320 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2321 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2322 | Py_INCREF(Py_None); |
| 2323 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2324 | #undef UTIME_ARG |
| 2325 | #undef ATIME |
| 2326 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2329 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2330 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2331 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2332 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2333 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2334 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2335 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2336 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2337 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2338 | { |
| 2339 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2340 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2341 | return NULL; |
| 2342 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2343 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2346 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2347 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2348 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2349 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2350 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2351 | for (i = 0; i < count; i++) |
| 2352 | PyMem_Free(array[i]); |
| 2353 | PyMem_DEL(array); |
| 2354 | } |
| 2355 | #endif |
| 2356 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2357 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2358 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2359 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2360 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2361 | Execute an executable path with arguments, replacing current process.\n\ |
| 2362 | \n\ |
| 2363 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2364 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2365 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2366 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2367 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2368 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2369 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2370 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2371 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2372 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2373 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2374 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2375 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2376 | argv is a list or tuple of strings. */ |
| 2377 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2378 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2379 | Py_FileSystemDefaultEncoding, |
| 2380 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2381 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2382 | if (PyList_Check(argv)) { |
| 2383 | argc = PyList_Size(argv); |
| 2384 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2385 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2386 | else if (PyTuple_Check(argv)) { |
| 2387 | argc = PyTuple_Size(argv); |
| 2388 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2389 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2390 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2391 | 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] | 2392 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2393 | return NULL; |
| 2394 | } |
| 2395 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2396 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2397 | if (argvlist == NULL) { |
| 2398 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2399 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2400 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2401 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2402 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2403 | Py_FileSystemDefaultEncoding, |
| 2404 | &argvlist[i])) { |
| 2405 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2406 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2407 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2408 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2409 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2410 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2411 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2412 | } |
| 2413 | argvlist[argc] = NULL; |
| 2414 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2415 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2416 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2417 | /* If we get here it's definitely an error */ |
| 2418 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2419 | free_string_array(argvlist, argc); |
| 2420 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2421 | return posix_error(); |
| 2422 | } |
| 2423 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2424 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2425 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2426 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2427 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2428 | \n\ |
| 2429 | path: path of executable file\n\ |
| 2430 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2431 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2432 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2433 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2434 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2435 | { |
| 2436 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2437 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2438 | char **argvlist; |
| 2439 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2440 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2441 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2442 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2443 | int lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2444 | |
| 2445 | /* execve has three arguments: (path, argv, env), where |
| 2446 | argv is a list or tuple of strings and env is a dictionary |
| 2447 | like posix.environ. */ |
| 2448 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2449 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2450 | Py_FileSystemDefaultEncoding, |
| 2451 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2452 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2453 | if (PyList_Check(argv)) { |
| 2454 | argc = PyList_Size(argv); |
| 2455 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2456 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2457 | else if (PyTuple_Check(argv)) { |
| 2458 | argc = PyTuple_Size(argv); |
| 2459 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2460 | } |
| 2461 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2462 | PyErr_SetString(PyExc_TypeError, |
| 2463 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2464 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2465 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2466 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2467 | PyErr_SetString(PyExc_TypeError, |
| 2468 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2469 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2472 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2473 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2474 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2475 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2476 | } |
| 2477 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2478 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2479 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2480 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2481 | &argvlist[i])) |
| 2482 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2483 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2484 | goto fail_1; |
| 2485 | } |
| 2486 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2487 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2488 | argvlist[argc] = NULL; |
| 2489 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2490 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2491 | if (i < 0) |
| 2492 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2493 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2494 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2495 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2496 | goto fail_1; |
| 2497 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2498 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2499 | keys = PyMapping_Keys(env); |
| 2500 | vals = PyMapping_Values(env); |
| 2501 | if (!keys || !vals) |
| 2502 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2503 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2504 | PyErr_SetString(PyExc_TypeError, |
| 2505 | "execve(): env.keys() or env.values() is not a list"); |
| 2506 | goto fail_2; |
| 2507 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2508 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2509 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2510 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2511 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2512 | |
| 2513 | key = PyList_GetItem(keys, pos); |
| 2514 | val = PyList_GetItem(vals, pos); |
| 2515 | if (!key || !val) |
| 2516 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2517 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2518 | if (!PyArg_Parse( |
| 2519 | key, |
| 2520 | "s;execve() arg 3 contains a non-string key", |
| 2521 | &k) || |
| 2522 | !PyArg_Parse( |
| 2523 | val, |
| 2524 | "s;execve() arg 3 contains a non-string value", |
| 2525 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2526 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2527 | goto fail_2; |
| 2528 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2529 | |
| 2530 | #if defined(PYOS_OS2) |
| 2531 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2532 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2533 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2534 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2535 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2536 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2537 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2538 | goto fail_2; |
| 2539 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2540 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2541 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2542 | #if defined(PYOS_OS2) |
| 2543 | } |
| 2544 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2545 | } |
| 2546 | envlist[envc] = 0; |
| 2547 | |
| 2548 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2549 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2550 | /* If we get here it's definitely an error */ |
| 2551 | |
| 2552 | (void) posix_error(); |
| 2553 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2554 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2555 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2556 | PyMem_DEL(envlist[envc]); |
| 2557 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2558 | fail_1: |
| 2559 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2560 | Py_XDECREF(vals); |
| 2561 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2562 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2563 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2564 | return NULL; |
| 2565 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2566 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2567 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2568 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2569 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2570 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2571 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2572 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2573 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2574 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2575 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2576 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2577 | |
| 2578 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2579 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2580 | { |
| 2581 | char *path; |
| 2582 | PyObject *argv; |
| 2583 | char **argvlist; |
| 2584 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2585 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2586 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2587 | |
| 2588 | /* spawnv has three arguments: (mode, path, argv), where |
| 2589 | argv is a list or tuple of strings. */ |
| 2590 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2591 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2592 | Py_FileSystemDefaultEncoding, |
| 2593 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2594 | return NULL; |
| 2595 | if (PyList_Check(argv)) { |
| 2596 | argc = PyList_Size(argv); |
| 2597 | getitem = PyList_GetItem; |
| 2598 | } |
| 2599 | else if (PyTuple_Check(argv)) { |
| 2600 | argc = PyTuple_Size(argv); |
| 2601 | getitem = PyTuple_GetItem; |
| 2602 | } |
| 2603 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2604 | PyErr_SetString(PyExc_TypeError, |
| 2605 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2606 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2607 | return NULL; |
| 2608 | } |
| 2609 | |
| 2610 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2611 | if (argvlist == NULL) { |
| 2612 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2613 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2614 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2615 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2616 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2617 | Py_FileSystemDefaultEncoding, |
| 2618 | &argvlist[i])) { |
| 2619 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2620 | PyErr_SetString( |
| 2621 | PyExc_TypeError, |
| 2622 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2623 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2624 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2625 | } |
| 2626 | } |
| 2627 | argvlist[argc] = NULL; |
| 2628 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2629 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2630 | Py_BEGIN_ALLOW_THREADS |
| 2631 | spawnval = spawnv(mode, path, argvlist); |
| 2632 | Py_END_ALLOW_THREADS |
| 2633 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2634 | if (mode == _OLD_P_OVERLAY) |
| 2635 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2636 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2637 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2638 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2639 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2640 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2641 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2642 | free_string_array(argvlist, argc); |
| 2643 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2644 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2645 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2646 | return posix_error(); |
| 2647 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2648 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2649 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2650 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2651 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2652 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2656 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2657 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2658 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2659 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2660 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2661 | path: path of executable file\n\ |
| 2662 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2663 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2664 | |
| 2665 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2666 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2667 | { |
| 2668 | char *path; |
| 2669 | PyObject *argv, *env; |
| 2670 | char **argvlist; |
| 2671 | char **envlist; |
| 2672 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2673 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2674 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2675 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2676 | int lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2677 | |
| 2678 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 2679 | argv is a list or tuple of strings and env is a dictionary |
| 2680 | like posix.environ. */ |
| 2681 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2682 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 2683 | Py_FileSystemDefaultEncoding, |
| 2684 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2685 | return NULL; |
| 2686 | if (PyList_Check(argv)) { |
| 2687 | argc = PyList_Size(argv); |
| 2688 | getitem = PyList_GetItem; |
| 2689 | } |
| 2690 | else if (PyTuple_Check(argv)) { |
| 2691 | argc = PyTuple_Size(argv); |
| 2692 | getitem = PyTuple_GetItem; |
| 2693 | } |
| 2694 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2695 | PyErr_SetString(PyExc_TypeError, |
| 2696 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2697 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2698 | } |
| 2699 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2700 | PyErr_SetString(PyExc_TypeError, |
| 2701 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2702 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | argvlist = PyMem_NEW(char *, argc+1); |
| 2706 | if (argvlist == NULL) { |
| 2707 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2708 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2709 | } |
| 2710 | for (i = 0; i < argc; i++) { |
| 2711 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2712 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2713 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2714 | &argvlist[i])) |
| 2715 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2716 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2717 | goto fail_1; |
| 2718 | } |
| 2719 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2720 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2721 | argvlist[argc] = NULL; |
| 2722 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2723 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2724 | if (i < 0) |
| 2725 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2726 | envlist = PyMem_NEW(char *, i + 1); |
| 2727 | if (envlist == NULL) { |
| 2728 | PyErr_NoMemory(); |
| 2729 | goto fail_1; |
| 2730 | } |
| 2731 | envc = 0; |
| 2732 | keys = PyMapping_Keys(env); |
| 2733 | vals = PyMapping_Values(env); |
| 2734 | if (!keys || !vals) |
| 2735 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2736 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2737 | PyErr_SetString(PyExc_TypeError, |
| 2738 | "spawnve(): env.keys() or env.values() is not a list"); |
| 2739 | goto fail_2; |
| 2740 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2741 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2742 | for (pos = 0; pos < i; pos++) { |
| 2743 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2744 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2745 | |
| 2746 | key = PyList_GetItem(keys, pos); |
| 2747 | val = PyList_GetItem(vals, pos); |
| 2748 | if (!key || !val) |
| 2749 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2750 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2751 | if (!PyArg_Parse( |
| 2752 | key, |
| 2753 | "s;spawnve() arg 3 contains a non-string key", |
| 2754 | &k) || |
| 2755 | !PyArg_Parse( |
| 2756 | val, |
| 2757 | "s;spawnve() arg 3 contains a non-string value", |
| 2758 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2759 | { |
| 2760 | goto fail_2; |
| 2761 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2762 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2763 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2764 | if (p == NULL) { |
| 2765 | PyErr_NoMemory(); |
| 2766 | goto fail_2; |
| 2767 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2768 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2769 | envlist[envc++] = p; |
| 2770 | } |
| 2771 | envlist[envc] = 0; |
| 2772 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2773 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2774 | Py_BEGIN_ALLOW_THREADS |
| 2775 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2776 | Py_END_ALLOW_THREADS |
| 2777 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2778 | if (mode == _OLD_P_OVERLAY) |
| 2779 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2780 | |
| 2781 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2782 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2783 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2784 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2785 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2786 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2787 | (void) posix_error(); |
| 2788 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2789 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2790 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2791 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2792 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2793 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2794 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2795 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2796 | while (--envc >= 0) |
| 2797 | PyMem_DEL(envlist[envc]); |
| 2798 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2799 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2800 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2801 | Py_XDECREF(vals); |
| 2802 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2803 | fail_0: |
| 2804 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2805 | return res; |
| 2806 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2807 | |
| 2808 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 2809 | #if defined(PYOS_OS2) |
| 2810 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 2811 | "spawnvp(mode, file, args)\n\n\ |
| 2812 | Execute the program 'file' in a new process, using the environment\n\ |
| 2813 | search path to find the file.\n\ |
| 2814 | \n\ |
| 2815 | mode: mode of process creation\n\ |
| 2816 | file: executable file name\n\ |
| 2817 | args: tuple or list of strings"); |
| 2818 | |
| 2819 | static PyObject * |
| 2820 | posix_spawnvp(PyObject *self, PyObject *args) |
| 2821 | { |
| 2822 | char *path; |
| 2823 | PyObject *argv; |
| 2824 | char **argvlist; |
| 2825 | int mode, i, argc; |
| 2826 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2827 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2828 | |
| 2829 | /* spawnvp has three arguments: (mode, path, argv), where |
| 2830 | argv is a list or tuple of strings. */ |
| 2831 | |
| 2832 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 2833 | Py_FileSystemDefaultEncoding, |
| 2834 | &path, &argv)) |
| 2835 | return NULL; |
| 2836 | if (PyList_Check(argv)) { |
| 2837 | argc = PyList_Size(argv); |
| 2838 | getitem = PyList_GetItem; |
| 2839 | } |
| 2840 | else if (PyTuple_Check(argv)) { |
| 2841 | argc = PyTuple_Size(argv); |
| 2842 | getitem = PyTuple_GetItem; |
| 2843 | } |
| 2844 | else { |
| 2845 | PyErr_SetString(PyExc_TypeError, |
| 2846 | "spawnvp() arg 2 must be a tuple or list"); |
| 2847 | PyMem_Free(path); |
| 2848 | return NULL; |
| 2849 | } |
| 2850 | |
| 2851 | argvlist = PyMem_NEW(char *, argc+1); |
| 2852 | if (argvlist == NULL) { |
| 2853 | PyMem_Free(path); |
| 2854 | return PyErr_NoMemory(); |
| 2855 | } |
| 2856 | for (i = 0; i < argc; i++) { |
| 2857 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2858 | Py_FileSystemDefaultEncoding, |
| 2859 | &argvlist[i])) { |
| 2860 | free_string_array(argvlist, i); |
| 2861 | PyErr_SetString( |
| 2862 | PyExc_TypeError, |
| 2863 | "spawnvp() arg 2 must contain only strings"); |
| 2864 | PyMem_Free(path); |
| 2865 | return NULL; |
| 2866 | } |
| 2867 | } |
| 2868 | argvlist[argc] = NULL; |
| 2869 | |
| 2870 | Py_BEGIN_ALLOW_THREADS |
| 2871 | #if defined(PYCC_GCC) |
| 2872 | spawnval = spawnvp(mode, path, argvlist); |
| 2873 | #else |
| 2874 | spawnval = _spawnvp(mode, path, argvlist); |
| 2875 | #endif |
| 2876 | Py_END_ALLOW_THREADS |
| 2877 | |
| 2878 | free_string_array(argvlist, argc); |
| 2879 | PyMem_Free(path); |
| 2880 | |
| 2881 | if (spawnval == -1) |
| 2882 | return posix_error(); |
| 2883 | else |
| 2884 | return Py_BuildValue("l", (long) spawnval); |
| 2885 | } |
| 2886 | |
| 2887 | |
| 2888 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 2889 | "spawnvpe(mode, file, args, env)\n\n\ |
| 2890 | Execute the program 'file' in a new process, using the environment\n\ |
| 2891 | search path to find the file.\n\ |
| 2892 | \n\ |
| 2893 | mode: mode of process creation\n\ |
| 2894 | file: executable file name\n\ |
| 2895 | args: tuple or list of arguments\n\ |
| 2896 | env: dictionary of strings mapping to strings"); |
| 2897 | |
| 2898 | static PyObject * |
| 2899 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 2900 | { |
| 2901 | char *path; |
| 2902 | PyObject *argv, *env; |
| 2903 | char **argvlist; |
| 2904 | char **envlist; |
| 2905 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2906 | int mode, i, pos, argc, envc; |
| 2907 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2908 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2909 | int lastarg = 0; |
| 2910 | |
| 2911 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 2912 | argv is a list or tuple of strings and env is a dictionary |
| 2913 | like posix.environ. */ |
| 2914 | |
| 2915 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 2916 | Py_FileSystemDefaultEncoding, |
| 2917 | &path, &argv, &env)) |
| 2918 | return NULL; |
| 2919 | if (PyList_Check(argv)) { |
| 2920 | argc = PyList_Size(argv); |
| 2921 | getitem = PyList_GetItem; |
| 2922 | } |
| 2923 | else if (PyTuple_Check(argv)) { |
| 2924 | argc = PyTuple_Size(argv); |
| 2925 | getitem = PyTuple_GetItem; |
| 2926 | } |
| 2927 | else { |
| 2928 | PyErr_SetString(PyExc_TypeError, |
| 2929 | "spawnvpe() arg 2 must be a tuple or list"); |
| 2930 | goto fail_0; |
| 2931 | } |
| 2932 | if (!PyMapping_Check(env)) { |
| 2933 | PyErr_SetString(PyExc_TypeError, |
| 2934 | "spawnvpe() arg 3 must be a mapping object"); |
| 2935 | goto fail_0; |
| 2936 | } |
| 2937 | |
| 2938 | argvlist = PyMem_NEW(char *, argc+1); |
| 2939 | if (argvlist == NULL) { |
| 2940 | PyErr_NoMemory(); |
| 2941 | goto fail_0; |
| 2942 | } |
| 2943 | for (i = 0; i < argc; i++) { |
| 2944 | if (!PyArg_Parse((*getitem)(argv, i), |
| 2945 | "et;spawnvpe() arg 2 must contain only strings", |
| 2946 | Py_FileSystemDefaultEncoding, |
| 2947 | &argvlist[i])) |
| 2948 | { |
| 2949 | lastarg = i; |
| 2950 | goto fail_1; |
| 2951 | } |
| 2952 | } |
| 2953 | lastarg = argc; |
| 2954 | argvlist[argc] = NULL; |
| 2955 | |
| 2956 | i = PyMapping_Size(env); |
| 2957 | if (i < 0) |
| 2958 | goto fail_1; |
| 2959 | envlist = PyMem_NEW(char *, i + 1); |
| 2960 | if (envlist == NULL) { |
| 2961 | PyErr_NoMemory(); |
| 2962 | goto fail_1; |
| 2963 | } |
| 2964 | envc = 0; |
| 2965 | keys = PyMapping_Keys(env); |
| 2966 | vals = PyMapping_Values(env); |
| 2967 | if (!keys || !vals) |
| 2968 | goto fail_2; |
| 2969 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2970 | PyErr_SetString(PyExc_TypeError, |
| 2971 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 2972 | goto fail_2; |
| 2973 | } |
| 2974 | |
| 2975 | for (pos = 0; pos < i; pos++) { |
| 2976 | char *p, *k, *v; |
| 2977 | size_t len; |
| 2978 | |
| 2979 | key = PyList_GetItem(keys, pos); |
| 2980 | val = PyList_GetItem(vals, pos); |
| 2981 | if (!key || !val) |
| 2982 | goto fail_2; |
| 2983 | |
| 2984 | if (!PyArg_Parse( |
| 2985 | key, |
| 2986 | "s;spawnvpe() arg 3 contains a non-string key", |
| 2987 | &k) || |
| 2988 | !PyArg_Parse( |
| 2989 | val, |
| 2990 | "s;spawnvpe() arg 3 contains a non-string value", |
| 2991 | &v)) |
| 2992 | { |
| 2993 | goto fail_2; |
| 2994 | } |
| 2995 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2996 | p = PyMem_NEW(char, len); |
| 2997 | if (p == NULL) { |
| 2998 | PyErr_NoMemory(); |
| 2999 | goto fail_2; |
| 3000 | } |
| 3001 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3002 | envlist[envc++] = p; |
| 3003 | } |
| 3004 | envlist[envc] = 0; |
| 3005 | |
| 3006 | Py_BEGIN_ALLOW_THREADS |
| 3007 | #if defined(PYCC_GCC) |
| 3008 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3009 | #else |
| 3010 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3011 | #endif |
| 3012 | Py_END_ALLOW_THREADS |
| 3013 | |
| 3014 | if (spawnval == -1) |
| 3015 | (void) posix_error(); |
| 3016 | else |
| 3017 | res = Py_BuildValue("l", (long) spawnval); |
| 3018 | |
| 3019 | fail_2: |
| 3020 | while (--envc >= 0) |
| 3021 | PyMem_DEL(envlist[envc]); |
| 3022 | PyMem_DEL(envlist); |
| 3023 | fail_1: |
| 3024 | free_string_array(argvlist, lastarg); |
| 3025 | Py_XDECREF(vals); |
| 3026 | Py_XDECREF(keys); |
| 3027 | fail_0: |
| 3028 | PyMem_Free(path); |
| 3029 | return res; |
| 3030 | } |
| 3031 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3032 | #endif /* HAVE_SPAWNV */ |
| 3033 | |
| 3034 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3035 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3036 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3037 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3038 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3039 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3040 | 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] | 3041 | |
| 3042 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3043 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3044 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3045 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3046 | if (pid == -1) |
| 3047 | return posix_error(); |
| 3048 | PyOS_AfterFork(); |
| 3049 | return PyInt_FromLong((long)pid); |
| 3050 | } |
| 3051 | #endif |
| 3052 | |
| 3053 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3054 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3055 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3056 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3057 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3058 | 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] | 3059 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3060 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3061 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3062 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3063 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3064 | if (pid == -1) |
| 3065 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3066 | if (pid == 0) |
| 3067 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3068 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3069 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3070 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3071 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3072 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3073 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3074 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3075 | #define DEV_PTY_FILE "/dev/ptc" |
| 3076 | #define HAVE_DEV_PTMX |
| 3077 | #else |
| 3078 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3079 | #endif |
| 3080 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3081 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3082 | #ifdef HAVE_PTY_H |
| 3083 | #include <pty.h> |
| 3084 | #else |
| 3085 | #ifdef HAVE_LIBUTIL_H |
| 3086 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3087 | #endif /* HAVE_LIBUTIL_H */ |
| 3088 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3089 | #ifdef HAVE_STROPTS_H |
| 3090 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3091 | #endif |
| 3092 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3093 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3094 | #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] | 3095 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3096 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3097 | 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] | 3098 | |
| 3099 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3100 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3101 | { |
| 3102 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3103 | #ifndef HAVE_OPENPTY |
| 3104 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3105 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3106 | #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] | 3107 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3108 | #ifdef sun |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 3109 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3110 | #endif |
| 3111 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3112 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3113 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3114 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3115 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3116 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3117 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3118 | if (slave_name == NULL) |
| 3119 | return posix_error(); |
| 3120 | |
| 3121 | slave_fd = open(slave_name, O_RDWR); |
| 3122 | if (slave_fd < 0) |
| 3123 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3124 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3125 | 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] | 3126 | if (master_fd < 0) |
| 3127 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3128 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3129 | /* change permission of slave */ |
| 3130 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3131 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3132 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3133 | } |
| 3134 | /* unlock slave */ |
| 3135 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3136 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3137 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3138 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3139 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3140 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3141 | if (slave_name == NULL) |
| 3142 | return posix_error(); |
| 3143 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3144 | if (slave_fd < 0) |
| 3145 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3146 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3147 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3148 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3149 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3150 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3151 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3152 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3153 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3154 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3155 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3156 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3157 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3158 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3159 | |
| 3160 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3161 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3162 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3163 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3164 | 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] | 3165 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3166 | |
| 3167 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3168 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3169 | { |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 3170 | int master_fd = -1, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3171 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3172 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3173 | if (pid == -1) |
| 3174 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3175 | if (pid == 0) |
| 3176 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3177 | return Py_BuildValue("(ii)", pid, master_fd); |
| 3178 | } |
| 3179 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3180 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3181 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3182 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3183 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3184 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3185 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3186 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3187 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3188 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3189 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3190 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3191 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3192 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3193 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3194 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3195 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3196 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3197 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3198 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3199 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3200 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3201 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3202 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3203 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3204 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3205 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3206 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3207 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3208 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3209 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3210 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3211 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3212 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3213 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3214 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3215 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3216 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3217 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3218 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3219 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3220 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3221 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3222 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3223 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3224 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3225 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3226 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3227 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3230 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3231 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3232 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3233 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3234 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3235 | |
| 3236 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3237 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3238 | { |
| 3239 | PyObject *result = NULL; |
| 3240 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3241 | #ifdef NGROUPS_MAX |
| 3242 | #define MAX_GROUPS NGROUPS_MAX |
| 3243 | #else |
| 3244 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3245 | #define MAX_GROUPS 64 |
| 3246 | #endif |
| 3247 | gid_t grouplist[MAX_GROUPS]; |
| 3248 | int n; |
| 3249 | |
| 3250 | n = getgroups(MAX_GROUPS, grouplist); |
| 3251 | if (n < 0) |
| 3252 | posix_error(); |
| 3253 | else { |
| 3254 | result = PyList_New(n); |
| 3255 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3256 | int i; |
| 3257 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3258 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3259 | if (o == NULL) { |
| 3260 | Py_DECREF(result); |
| 3261 | result = NULL; |
| 3262 | break; |
| 3263 | } |
| 3264 | PyList_SET_ITEM(result, i, o); |
| 3265 | } |
| 3266 | } |
| 3267 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3268 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3269 | return result; |
| 3270 | } |
| 3271 | #endif |
| 3272 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3273 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3274 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3275 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3276 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3277 | |
| 3278 | static PyObject * |
| 3279 | posix_getpgid(PyObject *self, PyObject *args) |
| 3280 | { |
| 3281 | int pid, pgid; |
| 3282 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3283 | return NULL; |
| 3284 | pgid = getpgid(pid); |
| 3285 | if (pgid < 0) |
| 3286 | return posix_error(); |
| 3287 | return PyInt_FromLong((long)pgid); |
| 3288 | } |
| 3289 | #endif /* HAVE_GETPGID */ |
| 3290 | |
| 3291 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3292 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3293 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3294 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3295 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3296 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3297 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3298 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3299 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3300 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3301 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3302 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3303 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3304 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3305 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3306 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3307 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3308 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3309 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3310 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3311 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3312 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3313 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3314 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3315 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3316 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3317 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3318 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3319 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3320 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3321 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3322 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3323 | Py_INCREF(Py_None); |
| 3324 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3327 | #endif /* HAVE_SETPGRP */ |
| 3328 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3329 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3330 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3331 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3332 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3333 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3334 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3335 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3336 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3337 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3338 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3339 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3340 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3341 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3342 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3343 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3344 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3345 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3346 | |
| 3347 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3348 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3349 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3350 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3351 | char *name; |
| 3352 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3353 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3354 | errno = 0; |
| 3355 | name = getlogin(); |
| 3356 | if (name == NULL) { |
| 3357 | if (errno) |
| 3358 | posix_error(); |
| 3359 | else |
| 3360 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3361 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3362 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3363 | else |
| 3364 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3365 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3366 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3367 | return result; |
| 3368 | } |
| 3369 | #endif |
| 3370 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3371 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3372 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3373 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3374 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3375 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3376 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3377 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3378 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3379 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3380 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3381 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3382 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3383 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3384 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3385 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3386 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3387 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3388 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3389 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3390 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3391 | { |
| 3392 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3393 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3394 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3395 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3396 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3397 | APIRET rc; |
| 3398 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3399 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3400 | |
| 3401 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3402 | APIRET rc; |
| 3403 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3404 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3405 | |
| 3406 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3407 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3408 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3409 | if (kill(pid, sig) == -1) |
| 3410 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3411 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3412 | Py_INCREF(Py_None); |
| 3413 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3414 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3415 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3416 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3417 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3418 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3419 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3420 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3421 | |
| 3422 | static PyObject * |
| 3423 | posix_killpg(PyObject *self, PyObject *args) |
| 3424 | { |
| 3425 | int pgid, sig; |
| 3426 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3427 | return NULL; |
| 3428 | if (killpg(pgid, sig) == -1) |
| 3429 | return posix_error(); |
| 3430 | Py_INCREF(Py_None); |
| 3431 | return Py_None; |
| 3432 | } |
| 3433 | #endif |
| 3434 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3435 | #ifdef HAVE_PLOCK |
| 3436 | |
| 3437 | #ifdef HAVE_SYS_LOCK_H |
| 3438 | #include <sys/lock.h> |
| 3439 | #endif |
| 3440 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3441 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3442 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3443 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3444 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3445 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3446 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3447 | { |
| 3448 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3449 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3450 | return NULL; |
| 3451 | if (plock(op) == -1) |
| 3452 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3453 | Py_INCREF(Py_None); |
| 3454 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3455 | } |
| 3456 | #endif |
| 3457 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3458 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3459 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3460 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3461 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3462 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3463 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3464 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3465 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3466 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3467 | async_system(const char *command) |
| 3468 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3469 | char errormsg[256], args[1024]; |
| 3470 | RESULTCODES rcodes; |
| 3471 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3472 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3473 | char *shell = getenv("COMSPEC"); |
| 3474 | if (!shell) |
| 3475 | shell = "cmd"; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3476 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3477 | /* avoid overflowing the argument buffer */ |
| 3478 | if (strlen(shell) + 3 + strlen(command) >= 1024) |
| 3479 | return ERROR_NOT_ENOUGH_MEMORY |
| 3480 | |
| 3481 | args[0] = '\0'; |
| 3482 | strcat(args, shell); |
| 3483 | strcat(args, "/c "); |
| 3484 | strcat(args, command); |
| 3485 | |
| 3486 | /* execute asynchronously, inheriting the environment */ |
| 3487 | rc = DosExecPgm(errormsg, |
| 3488 | sizeof(errormsg), |
| 3489 | EXEC_ASYNC, |
| 3490 | args, |
| 3491 | NULL, |
| 3492 | &rcodes, |
| 3493 | shell); |
| 3494 | return rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3497 | static FILE * |
| 3498 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3499 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3500 | int oldfd, tgtfd; |
| 3501 | HFILE pipeh[2]; |
| 3502 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3503 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3504 | /* mode determines which of stdin or stdout is reconnected to |
| 3505 | * the pipe to the child |
| 3506 | */ |
| 3507 | if (strchr(mode, 'r') != NULL) { |
| 3508 | tgt_fd = 1; /* stdout */ |
| 3509 | } else if (strchr(mode, 'w')) { |
| 3510 | tgt_fd = 0; /* stdin */ |
| 3511 | } else { |
| 3512 | *err = ERROR_INVALID_ACCESS; |
| 3513 | return NULL; |
| 3514 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3515 | |
Andrew MacIntyre | a3be258 | 2004-12-18 09:51:05 +0000 | [diff] [blame] | 3516 | /* setup the pipe */ |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3517 | if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) { |
| 3518 | *err = rc; |
| 3519 | return NULL; |
| 3520 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3521 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3522 | /* prevent other threads accessing stdio */ |
| 3523 | DosEnterCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3524 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3525 | /* reconnect stdio and execute child */ |
| 3526 | oldfd = dup(tgtfd); |
| 3527 | close(tgtfd); |
| 3528 | if (dup2(pipeh[tgtfd], tgtfd) == 0) { |
| 3529 | DosClose(pipeh[tgtfd]); |
| 3530 | rc = async_system(command); |
| 3531 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3532 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3533 | /* restore stdio */ |
| 3534 | dup2(oldfd, tgtfd); |
| 3535 | close(oldfd); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3536 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3537 | /* allow other threads access to stdio */ |
| 3538 | DosExitCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3539 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3540 | /* if execution of child was successful return file stream */ |
| 3541 | if (rc == NO_ERROR) |
| 3542 | return fdopen(pipeh[1 - tgtfd], mode); |
| 3543 | else { |
| 3544 | DosClose(pipeh[1 - tgtfd]); |
| 3545 | *err = rc; |
| 3546 | return NULL; |
| 3547 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
| 3550 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3551 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3552 | { |
| 3553 | char *name; |
| 3554 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3555 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3556 | FILE *fp; |
| 3557 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3558 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3559 | return NULL; |
| 3560 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3561 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3562 | Py_END_ALLOW_THREADS |
| 3563 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3564 | return os2_error(err); |
| 3565 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3566 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 3567 | if (f != NULL) |
| 3568 | PyFile_SetBufSize(f, bufsize); |
| 3569 | return f; |
| 3570 | } |
| 3571 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3572 | #elif defined(PYCC_GCC) |
| 3573 | |
| 3574 | /* standard posix version of popen() support */ |
| 3575 | static PyObject * |
| 3576 | posix_popen(PyObject *self, PyObject *args) |
| 3577 | { |
| 3578 | char *name; |
| 3579 | char *mode = "r"; |
| 3580 | int bufsize = -1; |
| 3581 | FILE *fp; |
| 3582 | PyObject *f; |
| 3583 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 3584 | return NULL; |
| 3585 | Py_BEGIN_ALLOW_THREADS |
| 3586 | fp = popen(name, mode); |
| 3587 | Py_END_ALLOW_THREADS |
| 3588 | if (fp == NULL) |
| 3589 | return posix_error(); |
| 3590 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 3591 | if (f != NULL) |
| 3592 | PyFile_SetBufSize(f, bufsize); |
| 3593 | return f; |
| 3594 | } |
| 3595 | |
| 3596 | /* fork() under OS/2 has lots'o'warts |
| 3597 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 3598 | * most of this code is a ripoff of the win32 code, but using the |
| 3599 | * capabilities of EMX's C library routines |
| 3600 | */ |
| 3601 | |
| 3602 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 3603 | #define POPEN_1 1 |
| 3604 | #define POPEN_2 2 |
| 3605 | #define POPEN_3 3 |
| 3606 | #define POPEN_4 4 |
| 3607 | |
| 3608 | static PyObject *_PyPopen(char *, int, int, int); |
| 3609 | static int _PyPclose(FILE *file); |
| 3610 | |
| 3611 | /* |
| 3612 | * Internal dictionary mapping popen* file pointers to process handles, |
| 3613 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3614 | * for more information on this dictionary's use. |
| 3615 | */ |
| 3616 | static PyObject *_PyPopenProcs = NULL; |
| 3617 | |
| 3618 | /* os2emx version of popen2() |
| 3619 | * |
| 3620 | * The result of this function is a pipe (file) connected to the |
| 3621 | * process's stdin, and a pipe connected to the process's stdout. |
| 3622 | */ |
| 3623 | |
| 3624 | static PyObject * |
| 3625 | os2emx_popen2(PyObject *self, PyObject *args) |
| 3626 | { |
| 3627 | PyObject *f; |
| 3628 | int tm=0; |
| 3629 | |
| 3630 | char *cmdstring; |
| 3631 | char *mode = "t"; |
| 3632 | int bufsize = -1; |
| 3633 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 3634 | return NULL; |
| 3635 | |
| 3636 | if (*mode == 't') |
| 3637 | tm = O_TEXT; |
| 3638 | else if (*mode != 'b') { |
| 3639 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3640 | return NULL; |
| 3641 | } else |
| 3642 | tm = O_BINARY; |
| 3643 | |
| 3644 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 3645 | |
| 3646 | return f; |
| 3647 | } |
| 3648 | |
| 3649 | /* |
| 3650 | * Variation on os2emx.popen2 |
| 3651 | * |
| 3652 | * The result of this function is 3 pipes - the process's stdin, |
| 3653 | * stdout and stderr |
| 3654 | */ |
| 3655 | |
| 3656 | static PyObject * |
| 3657 | os2emx_popen3(PyObject *self, PyObject *args) |
| 3658 | { |
| 3659 | PyObject *f; |
| 3660 | int tm = 0; |
| 3661 | |
| 3662 | char *cmdstring; |
| 3663 | char *mode = "t"; |
| 3664 | int bufsize = -1; |
| 3665 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 3666 | return NULL; |
| 3667 | |
| 3668 | if (*mode == 't') |
| 3669 | tm = O_TEXT; |
| 3670 | else if (*mode != 'b') { |
| 3671 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3672 | return NULL; |
| 3673 | } else |
| 3674 | tm = O_BINARY; |
| 3675 | |
| 3676 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 3677 | |
| 3678 | return f; |
| 3679 | } |
| 3680 | |
| 3681 | /* |
| 3682 | * Variation on os2emx.popen2 |
| 3683 | * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3684 | * The result of this function is 2 pipes - the processes stdin, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3685 | * and stdout+stderr combined as a single pipe. |
| 3686 | */ |
| 3687 | |
| 3688 | static PyObject * |
| 3689 | os2emx_popen4(PyObject *self, PyObject *args) |
| 3690 | { |
| 3691 | PyObject *f; |
| 3692 | int tm = 0; |
| 3693 | |
| 3694 | char *cmdstring; |
| 3695 | char *mode = "t"; |
| 3696 | int bufsize = -1; |
| 3697 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 3698 | return NULL; |
| 3699 | |
| 3700 | if (*mode == 't') |
| 3701 | tm = O_TEXT; |
| 3702 | else if (*mode != 'b') { |
| 3703 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3704 | return NULL; |
| 3705 | } else |
| 3706 | tm = O_BINARY; |
| 3707 | |
| 3708 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 3709 | |
| 3710 | return f; |
| 3711 | } |
| 3712 | |
| 3713 | /* a couple of structures for convenient handling of multiple |
| 3714 | * file handles and pipes |
| 3715 | */ |
| 3716 | struct file_ref |
| 3717 | { |
| 3718 | int handle; |
| 3719 | int flags; |
| 3720 | }; |
| 3721 | |
| 3722 | struct pipe_ref |
| 3723 | { |
| 3724 | int rd; |
| 3725 | int wr; |
| 3726 | }; |
| 3727 | |
| 3728 | /* The following code is derived from the win32 code */ |
| 3729 | |
| 3730 | static PyObject * |
| 3731 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 3732 | { |
| 3733 | struct file_ref stdio[3]; |
| 3734 | struct pipe_ref p_fd[3]; |
| 3735 | FILE *p_s[3]; |
| 3736 | int file_count, i, pipe_err, pipe_pid; |
| 3737 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 3738 | PyObject *f, *p_f[3]; |
| 3739 | |
| 3740 | /* file modes for subsequent fdopen's on pipe handles */ |
| 3741 | if (mode == O_TEXT) |
| 3742 | { |
| 3743 | rd_mode = "rt"; |
| 3744 | wr_mode = "wt"; |
| 3745 | } |
| 3746 | else |
| 3747 | { |
| 3748 | rd_mode = "rb"; |
| 3749 | wr_mode = "wb"; |
| 3750 | } |
| 3751 | |
| 3752 | /* prepare shell references */ |
| 3753 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 3754 | if ((shell = getenv("COMSPEC")) == NULL) |
| 3755 | { |
| 3756 | errno = ENOENT; |
| 3757 | return posix_error(); |
| 3758 | } |
| 3759 | |
| 3760 | sh_name = _getname(shell); |
| 3761 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 3762 | opt = "/c"; |
| 3763 | else |
| 3764 | opt = "-c"; |
| 3765 | |
| 3766 | /* save current stdio fds + their flags, and set not inheritable */ |
| 3767 | i = pipe_err = 0; |
| 3768 | while (pipe_err >= 0 && i < 3) |
| 3769 | { |
| 3770 | pipe_err = stdio[i].handle = dup(i); |
| 3771 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 3772 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 3773 | i++; |
| 3774 | } |
| 3775 | if (pipe_err < 0) |
| 3776 | { |
| 3777 | /* didn't get them all saved - clean up and bail out */ |
| 3778 | int saved_err = errno; |
| 3779 | while (i-- > 0) |
| 3780 | { |
| 3781 | close(stdio[i].handle); |
| 3782 | } |
| 3783 | errno = saved_err; |
| 3784 | return posix_error(); |
| 3785 | } |
| 3786 | |
| 3787 | /* create pipe ends */ |
| 3788 | file_count = 2; |
| 3789 | if (n == POPEN_3) |
| 3790 | file_count = 3; |
| 3791 | i = pipe_err = 0; |
| 3792 | while ((pipe_err == 0) && (i < file_count)) |
| 3793 | pipe_err = pipe((int *)&p_fd[i++]); |
| 3794 | if (pipe_err < 0) |
| 3795 | { |
| 3796 | /* didn't get them all made - clean up and bail out */ |
| 3797 | while (i-- > 0) |
| 3798 | { |
| 3799 | close(p_fd[i].wr); |
| 3800 | close(p_fd[i].rd); |
| 3801 | } |
| 3802 | errno = EPIPE; |
| 3803 | return posix_error(); |
| 3804 | } |
| 3805 | |
| 3806 | /* change the actual standard IO streams over temporarily, |
| 3807 | * making the retained pipe ends non-inheritable |
| 3808 | */ |
| 3809 | pipe_err = 0; |
| 3810 | |
| 3811 | /* - stdin */ |
| 3812 | if (dup2(p_fd[0].rd, 0) == 0) |
| 3813 | { |
| 3814 | close(p_fd[0].rd); |
| 3815 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 3816 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 3817 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 3818 | { |
| 3819 | close(p_fd[0].wr); |
| 3820 | pipe_err = -1; |
| 3821 | } |
| 3822 | } |
| 3823 | else |
| 3824 | { |
| 3825 | pipe_err = -1; |
| 3826 | } |
| 3827 | |
| 3828 | /* - stdout */ |
| 3829 | if (pipe_err == 0) |
| 3830 | { |
| 3831 | if (dup2(p_fd[1].wr, 1) == 1) |
| 3832 | { |
| 3833 | close(p_fd[1].wr); |
| 3834 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 3835 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 3836 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 3837 | { |
| 3838 | close(p_fd[1].rd); |
| 3839 | pipe_err = -1; |
| 3840 | } |
| 3841 | } |
| 3842 | else |
| 3843 | { |
| 3844 | pipe_err = -1; |
| 3845 | } |
| 3846 | } |
| 3847 | |
| 3848 | /* - stderr, as required */ |
| 3849 | if (pipe_err == 0) |
| 3850 | switch (n) |
| 3851 | { |
| 3852 | case POPEN_3: |
| 3853 | { |
| 3854 | if (dup2(p_fd[2].wr, 2) == 2) |
| 3855 | { |
| 3856 | close(p_fd[2].wr); |
| 3857 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 3858 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 3859 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 3860 | { |
| 3861 | close(p_fd[2].rd); |
| 3862 | pipe_err = -1; |
| 3863 | } |
| 3864 | } |
| 3865 | else |
| 3866 | { |
| 3867 | pipe_err = -1; |
| 3868 | } |
| 3869 | break; |
| 3870 | } |
| 3871 | |
| 3872 | case POPEN_4: |
| 3873 | { |
| 3874 | if (dup2(1, 2) != 2) |
| 3875 | { |
| 3876 | pipe_err = -1; |
| 3877 | } |
| 3878 | break; |
| 3879 | } |
| 3880 | } |
| 3881 | |
| 3882 | /* spawn the child process */ |
| 3883 | if (pipe_err == 0) |
| 3884 | { |
| 3885 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 3886 | if (pipe_pid == -1) |
| 3887 | { |
| 3888 | pipe_err = -1; |
| 3889 | } |
| 3890 | else |
| 3891 | { |
| 3892 | /* save the PID into the FILE structure |
| 3893 | * NOTE: this implementation doesn't actually |
| 3894 | * take advantage of this, but do it for |
| 3895 | * completeness - AIM Apr01 |
| 3896 | */ |
| 3897 | for (i = 0; i < file_count; i++) |
| 3898 | p_s[i]->_pid = pipe_pid; |
| 3899 | } |
| 3900 | } |
| 3901 | |
| 3902 | /* reset standard IO to normal */ |
| 3903 | for (i = 0; i < 3; i++) |
| 3904 | { |
| 3905 | dup2(stdio[i].handle, i); |
| 3906 | fcntl(i, F_SETFD, stdio[i].flags); |
| 3907 | close(stdio[i].handle); |
| 3908 | } |
| 3909 | |
| 3910 | /* if any remnant problems, clean up and bail out */ |
| 3911 | if (pipe_err < 0) |
| 3912 | { |
| 3913 | for (i = 0; i < 3; i++) |
| 3914 | { |
| 3915 | close(p_fd[i].rd); |
| 3916 | close(p_fd[i].wr); |
| 3917 | } |
| 3918 | errno = EPIPE; |
| 3919 | return posix_error_with_filename(cmdstring); |
| 3920 | } |
| 3921 | |
| 3922 | /* build tuple of file objects to return */ |
| 3923 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 3924 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3925 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3926 | PyFile_SetBufSize(p_f[1], bufsize); |
| 3927 | if (n == POPEN_3) |
| 3928 | { |
| 3929 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3930 | PyFile_SetBufSize(p_f[0], bufsize); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3931 | 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] | 3932 | } |
| 3933 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3934 | f = PyTuple_Pack(2, p_f[0], p_f[1]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3935 | |
| 3936 | /* |
| 3937 | * Insert the files we've created into the process dictionary |
| 3938 | * all referencing the list with the process handle and the |
| 3939 | * initial number of files (see description below in _PyPclose). |
| 3940 | * Since if _PyPclose later tried to wait on a process when all |
| 3941 | * handles weren't closed, it could create a deadlock with the |
| 3942 | * child, we spend some energy here to try to ensure that we |
| 3943 | * either insert all file handles into the dictionary or none |
| 3944 | * at all. It's a little clumsy with the various popen modes |
| 3945 | * and variable number of files involved. |
| 3946 | */ |
| 3947 | if (!_PyPopenProcs) |
| 3948 | { |
| 3949 | _PyPopenProcs = PyDict_New(); |
| 3950 | } |
| 3951 | |
| 3952 | if (_PyPopenProcs) |
| 3953 | { |
| 3954 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 3955 | int ins_rc[3]; |
| 3956 | |
| 3957 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 3958 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 3959 | |
| 3960 | procObj = PyList_New(2); |
| 3961 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 3962 | intObj = PyInt_FromLong((long) file_count); |
| 3963 | |
| 3964 | if (procObj && pidObj && intObj) |
| 3965 | { |
| 3966 | PyList_SetItem(procObj, 0, pidObj); |
| 3967 | PyList_SetItem(procObj, 1, intObj); |
| 3968 | |
| 3969 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 3970 | if (fileObj[0]) |
| 3971 | { |
| 3972 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 3973 | fileObj[0], |
| 3974 | procObj); |
| 3975 | } |
| 3976 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 3977 | if (fileObj[1]) |
| 3978 | { |
| 3979 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 3980 | fileObj[1], |
| 3981 | procObj); |
| 3982 | } |
| 3983 | if (file_count >= 3) |
| 3984 | { |
| 3985 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 3986 | if (fileObj[2]) |
| 3987 | { |
| 3988 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 3989 | fileObj[2], |
| 3990 | procObj); |
| 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 3995 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 3996 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 3997 | { |
| 3998 | /* Something failed - remove any dictionary |
| 3999 | * entries that did make it. |
| 4000 | */ |
| 4001 | if (!ins_rc[0] && fileObj[0]) |
| 4002 | { |
| 4003 | PyDict_DelItem(_PyPopenProcs, |
| 4004 | fileObj[0]); |
| 4005 | } |
| 4006 | if (!ins_rc[1] && fileObj[1]) |
| 4007 | { |
| 4008 | PyDict_DelItem(_PyPopenProcs, |
| 4009 | fileObj[1]); |
| 4010 | } |
| 4011 | if (!ins_rc[2] && fileObj[2]) |
| 4012 | { |
| 4013 | PyDict_DelItem(_PyPopenProcs, |
| 4014 | fileObj[2]); |
| 4015 | } |
| 4016 | } |
| 4017 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4018 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4019 | /* |
| 4020 | * Clean up our localized references for the dictionary keys |
| 4021 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4022 | * that got placed in the dictionary. |
| 4023 | */ |
| 4024 | Py_XDECREF(procObj); |
| 4025 | Py_XDECREF(fileObj[0]); |
| 4026 | Py_XDECREF(fileObj[1]); |
| 4027 | Py_XDECREF(fileObj[2]); |
| 4028 | } |
| 4029 | |
| 4030 | /* Child is launched. */ |
| 4031 | return f; |
| 4032 | } |
| 4033 | |
| 4034 | /* |
| 4035 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4036 | * exit code for the child process and return as a result of the close. |
| 4037 | * |
| 4038 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4039 | * input file pointer to information about the process that was |
| 4040 | * originally created by the popen* call that created the file pointer. |
| 4041 | * The dictionary uses the file pointer as a key (with one entry |
| 4042 | * inserted for each file returned by the original popen* call) and a |
| 4043 | * single list object as the value for all files from a single call. |
| 4044 | * The list object contains the Win32 process handle at [0], and a file |
| 4045 | * count at [1], which is initialized to the total number of file |
| 4046 | * handles using that list. |
| 4047 | * |
| 4048 | * This function closes whichever handle it is passed, and decrements |
| 4049 | * the file count in the dictionary for the process handle pointed to |
| 4050 | * by this file. On the last close (when the file count reaches zero), |
| 4051 | * this function will wait for the child process and then return its |
| 4052 | * exit code as the result of the close() operation. This permits the |
| 4053 | * files to be closed in any order - it is always the close() of the |
| 4054 | * final handle that will return the exit code. |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4055 | * |
| 4056 | * NOTE: This function is currently called with the GIL released. |
| 4057 | * hence we use the GILState API to manage our state. |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4058 | */ |
| 4059 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4060 | static int _PyPclose(FILE *file) |
| 4061 | { |
| 4062 | int result; |
| 4063 | int exit_code; |
| 4064 | int pipe_pid; |
| 4065 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 4066 | int file_count; |
| 4067 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4068 | PyGILState_STATE state; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4069 | #endif |
| 4070 | |
| 4071 | /* Close the file handle first, to ensure it can't block the |
| 4072 | * child from exiting if it's the last handle. |
| 4073 | */ |
| 4074 | result = fclose(file); |
| 4075 | |
| 4076 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4077 | state = PyGILState_Ensure(); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4078 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4079 | if (_PyPopenProcs) |
| 4080 | { |
| 4081 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4082 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4083 | fileObj)) != NULL && |
| 4084 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 4085 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 4086 | { |
| 4087 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 4088 | file_count = (int) PyInt_AsLong(intObj); |
| 4089 | |
| 4090 | if (file_count > 1) |
| 4091 | { |
| 4092 | /* Still other files referencing process */ |
| 4093 | file_count--; |
| 4094 | PyList_SetItem(procObj,1, |
| 4095 | PyInt_FromLong((long) file_count)); |
| 4096 | } |
| 4097 | else |
| 4098 | { |
| 4099 | /* Last file for this process */ |
| 4100 | if (result != EOF && |
| 4101 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 4102 | { |
| 4103 | /* extract exit status */ |
| 4104 | if (WIFEXITED(exit_code)) |
| 4105 | { |
| 4106 | result = WEXITSTATUS(exit_code); |
| 4107 | } |
| 4108 | else |
| 4109 | { |
| 4110 | errno = EPIPE; |
| 4111 | result = -1; |
| 4112 | } |
| 4113 | } |
| 4114 | else |
| 4115 | { |
| 4116 | /* Indicate failure - this will cause the file object |
| 4117 | * to raise an I/O error and translate the last |
| 4118 | * error code from errno. We do have a problem with |
| 4119 | * last errors that overlap the normal errno table, |
| 4120 | * but that's a consistent problem with the file object. |
| 4121 | */ |
| 4122 | result = -1; |
| 4123 | } |
| 4124 | } |
| 4125 | |
| 4126 | /* Remove this file pointer from dictionary */ |
| 4127 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4128 | |
| 4129 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 4130 | { |
| 4131 | Py_DECREF(_PyPopenProcs); |
| 4132 | _PyPopenProcs = NULL; |
| 4133 | } |
| 4134 | |
| 4135 | } /* if object retrieval ok */ |
| 4136 | |
| 4137 | Py_XDECREF(fileObj); |
| 4138 | } /* if _PyPopenProcs */ |
| 4139 | |
| 4140 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4141 | PyGILState_Release(state); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4142 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4143 | return result; |
| 4144 | } |
| 4145 | |
| 4146 | #endif /* PYCC_??? */ |
| 4147 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4148 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4149 | |
| 4150 | /* |
| 4151 | * Portable 'popen' replacement for Win32. |
| 4152 | * |
| 4153 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 4154 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4155 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4156 | */ |
| 4157 | |
| 4158 | #include <malloc.h> |
| 4159 | #include <io.h> |
| 4160 | #include <fcntl.h> |
| 4161 | |
| 4162 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 4163 | #define POPEN_1 1 |
| 4164 | #define POPEN_2 2 |
| 4165 | #define POPEN_3 3 |
| 4166 | #define POPEN_4 4 |
| 4167 | |
| 4168 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4169 | static int _PyPclose(FILE *file); |
| 4170 | |
| 4171 | /* |
| 4172 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4173 | * for use when retrieving the process exit code. See _PyPclose() below |
| 4174 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4175 | */ |
| 4176 | static PyObject *_PyPopenProcs = NULL; |
| 4177 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4178 | |
| 4179 | /* popen that works from a GUI. |
| 4180 | * |
| 4181 | * The result of this function is a pipe (file) connected to the |
| 4182 | * processes stdin or stdout, depending on the requested mode. |
| 4183 | */ |
| 4184 | |
| 4185 | static PyObject * |
| 4186 | posix_popen(PyObject *self, PyObject *args) |
| 4187 | { |
Raymond Hettinger | b5cb665 | 2003-09-01 22:25:41 +0000 | [diff] [blame] | 4188 | PyObject *f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4189 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4190 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4191 | char *cmdstring; |
| 4192 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4193 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4194 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4195 | return NULL; |
| 4196 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4197 | if (*mode == 'r') |
| 4198 | tm = _O_RDONLY; |
| 4199 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4200 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4201 | return NULL; |
| 4202 | } else |
| 4203 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4204 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4205 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4206 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4207 | return NULL; |
| 4208 | } |
| 4209 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4210 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4211 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4212 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4213 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4214 | else |
| 4215 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 4216 | |
| 4217 | return f; |
| 4218 | } |
| 4219 | |
| 4220 | /* Variation on win32pipe.popen |
| 4221 | * |
| 4222 | * The result of this function is a pipe (file) connected to the |
| 4223 | * process's stdin, and a pipe connected to the process's stdout. |
| 4224 | */ |
| 4225 | |
| 4226 | static PyObject * |
| 4227 | win32_popen2(PyObject *self, PyObject *args) |
| 4228 | { |
| 4229 | PyObject *f; |
| 4230 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4231 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4232 | char *cmdstring; |
| 4233 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4234 | int bufsize = -1; |
| 4235 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4236 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4237 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4238 | if (*mode == 't') |
| 4239 | tm = _O_TEXT; |
| 4240 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4241 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4242 | return NULL; |
| 4243 | } else |
| 4244 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4245 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4246 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4247 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4248 | return NULL; |
| 4249 | } |
| 4250 | |
| 4251 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4252 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4253 | return f; |
| 4254 | } |
| 4255 | |
| 4256 | /* |
| 4257 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4258 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4259 | * The result of this function is 3 pipes - the process's stdin, |
| 4260 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4261 | */ |
| 4262 | |
| 4263 | static PyObject * |
| 4264 | win32_popen3(PyObject *self, PyObject *args) |
| 4265 | { |
| 4266 | PyObject *f; |
| 4267 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4268 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4269 | char *cmdstring; |
| 4270 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4271 | int bufsize = -1; |
| 4272 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4273 | return NULL; |
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 | if (*mode == 't') |
| 4276 | tm = _O_TEXT; |
| 4277 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4278 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4279 | return NULL; |
| 4280 | } else |
| 4281 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4282 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4283 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4284 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4285 | return NULL; |
| 4286 | } |
| 4287 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4288 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4289 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4290 | return f; |
| 4291 | } |
| 4292 | |
| 4293 | /* |
| 4294 | * Variation on win32pipe.popen |
| 4295 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4296 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4297 | * and stdout+stderr combined as a single pipe. |
| 4298 | */ |
| 4299 | |
| 4300 | static PyObject * |
| 4301 | win32_popen4(PyObject *self, PyObject *args) |
| 4302 | { |
| 4303 | PyObject *f; |
| 4304 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4305 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4306 | char *cmdstring; |
| 4307 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4308 | int bufsize = -1; |
| 4309 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4310 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4311 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4312 | if (*mode == 't') |
| 4313 | tm = _O_TEXT; |
| 4314 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4315 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4316 | return NULL; |
| 4317 | } else |
| 4318 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4319 | |
| 4320 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4321 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4322 | return NULL; |
| 4323 | } |
| 4324 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4325 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4326 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4327 | return f; |
| 4328 | } |
| 4329 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4330 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4331 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4332 | HANDLE hStdin, |
| 4333 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4334 | HANDLE hStderr, |
| 4335 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4336 | { |
| 4337 | PROCESS_INFORMATION piProcInfo; |
| 4338 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4339 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4340 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4341 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4342 | int i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4343 | Py_ssize_t x; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4344 | |
| 4345 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4346 | char *comshell; |
| 4347 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4348 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4349 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4350 | /* x < i, so x fits into an integer */ |
| 4351 | return (int)x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4352 | |
| 4353 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 4354 | * then use the w9xpopen hack. |
| 4355 | */ |
| 4356 | comshell = s1 + x; |
| 4357 | while (comshell >= s1 && *comshell != '\\') |
| 4358 | --comshell; |
| 4359 | ++comshell; |
| 4360 | |
| 4361 | if (GetVersion() < 0x80000000 && |
| 4362 | _stricmp(comshell, "command.com") != 0) { |
| 4363 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4364 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4365 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4366 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4367 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4368 | } |
| 4369 | else { |
| 4370 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4371 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 4372 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4373 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4374 | char modulepath[_MAX_PATH]; |
| 4375 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4376 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 4377 | for (i = x = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4378 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4379 | x = i+1; |
| 4380 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4381 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4382 | strncat(modulepath, |
| 4383 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4384 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4385 | -strlen(modulepath)); |
| 4386 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4387 | /* Eeek - file-not-found - possibly an embedding |
| 4388 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4389 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4390 | strncpy(modulepath, |
| 4391 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4392 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 4393 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 4394 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4395 | strncat(modulepath, |
| 4396 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4397 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4398 | -strlen(modulepath)); |
| 4399 | /* No where else to look - raise an easily identifiable |
| 4400 | error, rather than leaving Windows to report |
| 4401 | "file not found" - as the user is probably blissfully |
| 4402 | unaware this shim EXE is used, and it will confuse them. |
| 4403 | (well, it confused me for a while ;-) |
| 4404 | */ |
| 4405 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4406 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4407 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4408 | "for popen to work with your shell " |
| 4409 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4410 | szConsoleSpawn); |
| 4411 | return FALSE; |
| 4412 | } |
| 4413 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4414 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4415 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4416 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4417 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4418 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4419 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4420 | /* To maintain correct argument passing semantics, |
| 4421 | we pass the command-line as it stands, and allow |
| 4422 | quoting to be applied. w9xpopen.exe will then |
| 4423 | use its argv vector, and re-quote the necessary |
| 4424 | args for the ultimate child process. |
| 4425 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4426 | PyOS_snprintf( |
| 4427 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4428 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4429 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4430 | s1, |
| 4431 | s3, |
| 4432 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4433 | /* Not passing CREATE_NEW_CONSOLE has been known to |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4434 | cause random failures on win9x. Specifically a |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4435 | dialog: |
| 4436 | "Your program accessed mem currently in use at xxx" |
| 4437 | and a hopeful warning about the stability of your |
| 4438 | system. |
| 4439 | Cost is Ctrl+C wont kill children, but anyone |
| 4440 | who cares can have a go! |
| 4441 | */ |
| 4442 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4443 | } |
| 4444 | } |
| 4445 | |
| 4446 | /* Could be an else here to try cmd.exe / command.com in the path |
| 4447 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4448 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4449 | PyErr_SetString(PyExc_RuntimeError, |
| 4450 | "Cannot locate a COMSPEC environment variable to " |
| 4451 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4452 | return FALSE; |
| 4453 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4454 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4455 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 4456 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 4457 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 4458 | siStartInfo.hStdInput = hStdin; |
| 4459 | siStartInfo.hStdOutput = hStdout; |
| 4460 | siStartInfo.hStdError = hStderr; |
| 4461 | siStartInfo.wShowWindow = SW_HIDE; |
| 4462 | |
| 4463 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4464 | s2, |
| 4465 | NULL, |
| 4466 | NULL, |
| 4467 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4468 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4469 | NULL, |
| 4470 | NULL, |
| 4471 | &siStartInfo, |
| 4472 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4473 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4474 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4475 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4476 | /* Return process handle */ |
| 4477 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4478 | return TRUE; |
| 4479 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4480 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4481 | return FALSE; |
| 4482 | } |
| 4483 | |
| 4484 | /* The following code is based off of KB: Q190351 */ |
| 4485 | |
| 4486 | static PyObject * |
| 4487 | _PyPopen(char *cmdstring, int mode, int n) |
| 4488 | { |
| 4489 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 4490 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4491 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4492 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4493 | SECURITY_ATTRIBUTES saAttr; |
| 4494 | BOOL fSuccess; |
| 4495 | int fd1, fd2, fd3; |
| 4496 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4497 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4498 | PyObject *f; |
| 4499 | |
| 4500 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 4501 | saAttr.bInheritHandle = TRUE; |
| 4502 | saAttr.lpSecurityDescriptor = NULL; |
| 4503 | |
| 4504 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 4505 | return win32_error("CreatePipe", NULL); |
| 4506 | |
| 4507 | /* Create new output read handle and the input write handle. Set |
| 4508 | * the inheritance properties to FALSE. Otherwise, the child inherits |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 4509 | * these handles; resulting in non-closeable handles to the pipes |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4510 | * being created. */ |
| 4511 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4512 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 4513 | FALSE, |
| 4514 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4515 | if (!fSuccess) |
| 4516 | return win32_error("DuplicateHandle", NULL); |
| 4517 | |
| 4518 | /* Close the inheritable version of ChildStdin |
| 4519 | that we're using. */ |
| 4520 | CloseHandle(hChildStdinWr); |
| 4521 | |
| 4522 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 4523 | return win32_error("CreatePipe", NULL); |
| 4524 | |
| 4525 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4526 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 4527 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4528 | if (!fSuccess) |
| 4529 | return win32_error("DuplicateHandle", NULL); |
| 4530 | |
| 4531 | /* Close the inheritable version of ChildStdout |
| 4532 | that we're using. */ |
| 4533 | CloseHandle(hChildStdoutRd); |
| 4534 | |
| 4535 | if (n != POPEN_4) { |
| 4536 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 4537 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4538 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 4539 | hChildStderrRd, |
| 4540 | GetCurrentProcess(), |
| 4541 | &hChildStderrRdDup, 0, |
| 4542 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4543 | if (!fSuccess) |
| 4544 | return win32_error("DuplicateHandle", NULL); |
| 4545 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 4546 | CloseHandle(hChildStderrRd); |
| 4547 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4548 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4549 | switch (n) { |
| 4550 | case POPEN_1: |
| 4551 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 4552 | case _O_WRONLY | _O_TEXT: |
| 4553 | /* Case for writing to child Stdin in text mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4554 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4555 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4556 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4557 | PyFile_SetBufSize(f, 0); |
| 4558 | /* We don't care about these pipes anymore, so close them. */ |
| 4559 | CloseHandle(hChildStdoutRdDup); |
| 4560 | CloseHandle(hChildStderrRdDup); |
| 4561 | break; |
| 4562 | |
| 4563 | case _O_RDONLY | _O_TEXT: |
| 4564 | /* Case for reading from child Stdout in text mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4565 | fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4566 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4567 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4568 | PyFile_SetBufSize(f, 0); |
| 4569 | /* We don't care about these pipes anymore, so close them. */ |
| 4570 | CloseHandle(hChildStdinWrDup); |
| 4571 | CloseHandle(hChildStderrRdDup); |
| 4572 | break; |
| 4573 | |
| 4574 | case _O_RDONLY | _O_BINARY: |
| 4575 | /* Case for readinig from child Stdout in binary mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4576 | fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4577 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4578 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4579 | PyFile_SetBufSize(f, 0); |
| 4580 | /* We don't care about these pipes anymore, so close them. */ |
| 4581 | CloseHandle(hChildStdinWrDup); |
| 4582 | CloseHandle(hChildStderrRdDup); |
| 4583 | break; |
| 4584 | |
| 4585 | case _O_WRONLY | _O_BINARY: |
| 4586 | /* Case for writing to child Stdin in binary mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4587 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4588 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4589 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4590 | PyFile_SetBufSize(f, 0); |
| 4591 | /* We don't care about these pipes anymore, so close them. */ |
| 4592 | CloseHandle(hChildStdoutRdDup); |
| 4593 | CloseHandle(hChildStderrRdDup); |
| 4594 | break; |
| 4595 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4596 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4597 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4598 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4599 | case POPEN_2: |
| 4600 | case POPEN_4: |
| 4601 | { |
| 4602 | char *m1, *m2; |
| 4603 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4604 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4605 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4606 | m1 = "r"; |
| 4607 | m2 = "w"; |
| 4608 | } else { |
| 4609 | m1 = "rb"; |
| 4610 | m2 = "wb"; |
| 4611 | } |
| 4612 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4613 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4614 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4615 | fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4616 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4617 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4618 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4619 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4620 | PyFile_SetBufSize(p2, 0); |
| 4621 | |
| 4622 | if (n != 4) |
| 4623 | CloseHandle(hChildStderrRdDup); |
| 4624 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4625 | f = PyTuple_Pack(2,p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4626 | Py_XDECREF(p1); |
| 4627 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4628 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4629 | break; |
| 4630 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4631 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4632 | case POPEN_3: |
| 4633 | { |
| 4634 | char *m1, *m2; |
| 4635 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4636 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4637 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4638 | m1 = "r"; |
| 4639 | m2 = "w"; |
| 4640 | } else { |
| 4641 | m1 = "rb"; |
| 4642 | m2 = "wb"; |
| 4643 | } |
| 4644 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4645 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4646 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4647 | fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4648 | f2 = _fdopen(fd2, m1); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4649 | fd3 = _open_osfhandle((intptr_t)hChildStderrRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4650 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4651 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4652 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 4653 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4654 | PyFile_SetBufSize(p1, 0); |
| 4655 | PyFile_SetBufSize(p2, 0); |
| 4656 | PyFile_SetBufSize(p3, 0); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4657 | f = PyTuple_Pack(3,p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4658 | Py_XDECREF(p1); |
| 4659 | Py_XDECREF(p2); |
| 4660 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4661 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4662 | break; |
| 4663 | } |
| 4664 | } |
| 4665 | |
| 4666 | if (n == POPEN_4) { |
| 4667 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4668 | hChildStdinRd, |
| 4669 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4670 | hChildStdoutWr, |
| 4671 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4672 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4673 | } |
| 4674 | else { |
| 4675 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4676 | hChildStdinRd, |
| 4677 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4678 | hChildStderrWr, |
| 4679 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4680 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4683 | /* |
| 4684 | * Insert the files we've created into the process dictionary |
| 4685 | * all referencing the list with the process handle and the |
| 4686 | * initial number of files (see description below in _PyPclose). |
| 4687 | * Since if _PyPclose later tried to wait on a process when all |
| 4688 | * handles weren't closed, it could create a deadlock with the |
| 4689 | * child, we spend some energy here to try to ensure that we |
| 4690 | * either insert all file handles into the dictionary or none |
| 4691 | * at all. It's a little clumsy with the various popen modes |
| 4692 | * and variable number of files involved. |
| 4693 | */ |
| 4694 | if (!_PyPopenProcs) { |
| 4695 | _PyPopenProcs = PyDict_New(); |
| 4696 | } |
| 4697 | |
| 4698 | if (_PyPopenProcs) { |
| 4699 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 4700 | int ins_rc[3]; |
| 4701 | |
| 4702 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4703 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4704 | |
| 4705 | procObj = PyList_New(2); |
| 4706 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 4707 | intObj = PyInt_FromLong(file_count); |
| 4708 | |
| 4709 | if (procObj && hProcessObj && intObj) { |
| 4710 | PyList_SetItem(procObj,0,hProcessObj); |
| 4711 | PyList_SetItem(procObj,1,intObj); |
| 4712 | |
| 4713 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 4714 | if (fileObj[0]) { |
| 4715 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4716 | fileObj[0], |
| 4717 | procObj); |
| 4718 | } |
| 4719 | if (file_count >= 2) { |
| 4720 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 4721 | if (fileObj[1]) { |
| 4722 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4723 | fileObj[1], |
| 4724 | procObj); |
| 4725 | } |
| 4726 | } |
| 4727 | if (file_count >= 3) { |
| 4728 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 4729 | if (fileObj[2]) { |
| 4730 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4731 | fileObj[2], |
| 4732 | procObj); |
| 4733 | } |
| 4734 | } |
| 4735 | |
| 4736 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4737 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4738 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 4739 | /* Something failed - remove any dictionary |
| 4740 | * entries that did make it. |
| 4741 | */ |
| 4742 | if (!ins_rc[0] && fileObj[0]) { |
| 4743 | PyDict_DelItem(_PyPopenProcs, |
| 4744 | fileObj[0]); |
| 4745 | } |
| 4746 | if (!ins_rc[1] && fileObj[1]) { |
| 4747 | PyDict_DelItem(_PyPopenProcs, |
| 4748 | fileObj[1]); |
| 4749 | } |
| 4750 | if (!ins_rc[2] && fileObj[2]) { |
| 4751 | PyDict_DelItem(_PyPopenProcs, |
| 4752 | fileObj[2]); |
| 4753 | } |
| 4754 | } |
| 4755 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4756 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4757 | /* |
| 4758 | * Clean up our localized references for the dictionary keys |
| 4759 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4760 | * that got placed in the dictionary. |
| 4761 | */ |
| 4762 | Py_XDECREF(procObj); |
| 4763 | Py_XDECREF(fileObj[0]); |
| 4764 | Py_XDECREF(fileObj[1]); |
| 4765 | Py_XDECREF(fileObj[2]); |
| 4766 | } |
| 4767 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4768 | /* Child is launched. Close the parents copy of those pipe |
| 4769 | * handles that only the child should have open. You need to |
| 4770 | * make sure that no handles to the write end of the output pipe |
| 4771 | * are maintained in this process or else the pipe will not close |
| 4772 | * when the child process exits and the ReadFile will hang. */ |
| 4773 | |
| 4774 | if (!CloseHandle(hChildStdinRd)) |
| 4775 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4776 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4777 | if (!CloseHandle(hChildStdoutWr)) |
| 4778 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4779 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4780 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 4781 | return win32_error("CloseHandle", NULL); |
| 4782 | |
| 4783 | return f; |
| 4784 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4785 | |
| 4786 | /* |
| 4787 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4788 | * 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] | 4789 | * |
| 4790 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4791 | * input file pointer to information about the process that was |
| 4792 | * originally created by the popen* call that created the file pointer. |
| 4793 | * The dictionary uses the file pointer as a key (with one entry |
| 4794 | * inserted for each file returned by the original popen* call) and a |
| 4795 | * single list object as the value for all files from a single call. |
| 4796 | * The list object contains the Win32 process handle at [0], and a file |
| 4797 | * count at [1], which is initialized to the total number of file |
| 4798 | * handles using that list. |
| 4799 | * |
| 4800 | * This function closes whichever handle it is passed, and decrements |
| 4801 | * the file count in the dictionary for the process handle pointed to |
| 4802 | * by this file. On the last close (when the file count reaches zero), |
| 4803 | * this function will wait for the child process and then return its |
| 4804 | * exit code as the result of the close() operation. This permits the |
| 4805 | * files to be closed in any order - it is always the close() of the |
| 4806 | * final handle that will return the exit code. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4807 | * |
| 4808 | * NOTE: This function is currently called with the GIL released. |
| 4809 | * hence we use the GILState API to manage our state. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4810 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4811 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4812 | static int _PyPclose(FILE *file) |
| 4813 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4814 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4815 | DWORD exit_code; |
| 4816 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4817 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 4818 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4819 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4820 | PyGILState_STATE state; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4821 | #endif |
| 4822 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4823 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4824 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4825 | */ |
| 4826 | result = fclose(file); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4827 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4828 | state = PyGILState_Ensure(); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4829 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4830 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4831 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4832 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4833 | fileObj)) != NULL && |
| 4834 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 4835 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 4836 | |
| 4837 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 4838 | file_count = PyInt_AsLong(intObj); |
| 4839 | |
| 4840 | if (file_count > 1) { |
| 4841 | /* Still other files referencing process */ |
| 4842 | file_count--; |
| 4843 | PyList_SetItem(procObj,1, |
| 4844 | PyInt_FromLong(file_count)); |
| 4845 | } else { |
| 4846 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4847 | if (result != EOF && |
| 4848 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 4849 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4850 | /* Possible truncation here in 16-bit environments, but |
| 4851 | * real exit codes are just the lower byte in any event. |
| 4852 | */ |
| 4853 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4854 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4855 | /* Indicate failure - this will cause the file object |
| 4856 | * to raise an I/O error and translate the last Win32 |
| 4857 | * error code from errno. We do have a problem with |
| 4858 | * last errors that overlap the normal errno table, |
| 4859 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4860 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4861 | if (result != EOF) { |
| 4862 | /* If the error wasn't from the fclose(), then |
| 4863 | * set errno for the file object error handling. |
| 4864 | */ |
| 4865 | errno = GetLastError(); |
| 4866 | } |
| 4867 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4868 | } |
| 4869 | |
| 4870 | /* Free up the native handle at this point */ |
| 4871 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4872 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4873 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4874 | /* Remove this file pointer from dictionary */ |
| 4875 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4876 | |
| 4877 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 4878 | Py_DECREF(_PyPopenProcs); |
| 4879 | _PyPopenProcs = NULL; |
| 4880 | } |
| 4881 | |
| 4882 | } /* if object retrieval ok */ |
| 4883 | |
| 4884 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4885 | } /* if _PyPopenProcs */ |
| 4886 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4887 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4888 | PyGILState_Release(state); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4889 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4890 | return result; |
| 4891 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4892 | |
| 4893 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4894 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4895 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4896 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4897 | char *name; |
| 4898 | char *mode = "r"; |
| 4899 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4900 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4901 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4902 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4903 | return NULL; |
Martin v. Löwis | 9ad853b | 2003-10-31 10:01:53 +0000 | [diff] [blame] | 4904 | /* Strip mode of binary or text modifiers */ |
| 4905 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) |
| 4906 | mode = "r"; |
| 4907 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) |
| 4908 | mode = "w"; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4909 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4910 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4911 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4912 | if (fp == NULL) |
| 4913 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4914 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4915 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4916 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4917 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4918 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4919 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4920 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4921 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4922 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4923 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4924 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4925 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4926 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4927 | Set the current process's user id."); |
| 4928 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4929 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4930 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4931 | { |
| 4932 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4933 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4934 | return NULL; |
| 4935 | if (setuid(uid) < 0) |
| 4936 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4937 | Py_INCREF(Py_None); |
| 4938 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4939 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4940 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4941 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4942 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4943 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4944 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4945 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4946 | Set the current process's effective user id."); |
| 4947 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4948 | static PyObject * |
| 4949 | posix_seteuid (PyObject *self, PyObject *args) |
| 4950 | { |
| 4951 | int euid; |
| 4952 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 4953 | return NULL; |
| 4954 | } else if (seteuid(euid) < 0) { |
| 4955 | return posix_error(); |
| 4956 | } else { |
| 4957 | Py_INCREF(Py_None); |
| 4958 | return Py_None; |
| 4959 | } |
| 4960 | } |
| 4961 | #endif /* HAVE_SETEUID */ |
| 4962 | |
| 4963 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4964 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4965 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4966 | Set the current process's effective group id."); |
| 4967 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4968 | static PyObject * |
| 4969 | posix_setegid (PyObject *self, PyObject *args) |
| 4970 | { |
| 4971 | int egid; |
| 4972 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 4973 | return NULL; |
| 4974 | } else if (setegid(egid) < 0) { |
| 4975 | return posix_error(); |
| 4976 | } else { |
| 4977 | Py_INCREF(Py_None); |
| 4978 | return Py_None; |
| 4979 | } |
| 4980 | } |
| 4981 | #endif /* HAVE_SETEGID */ |
| 4982 | |
| 4983 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4984 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4985 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4986 | Set the current process's real and effective user ids."); |
| 4987 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4988 | static PyObject * |
| 4989 | posix_setreuid (PyObject *self, PyObject *args) |
| 4990 | { |
| 4991 | int ruid, euid; |
| 4992 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4993 | return NULL; |
| 4994 | } else if (setreuid(ruid, euid) < 0) { |
| 4995 | return posix_error(); |
| 4996 | } else { |
| 4997 | Py_INCREF(Py_None); |
| 4998 | return Py_None; |
| 4999 | } |
| 5000 | } |
| 5001 | #endif /* HAVE_SETREUID */ |
| 5002 | |
| 5003 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5004 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5005 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5006 | Set the current process's real and effective group ids."); |
| 5007 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5008 | static PyObject * |
| 5009 | posix_setregid (PyObject *self, PyObject *args) |
| 5010 | { |
| 5011 | int rgid, egid; |
| 5012 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 5013 | return NULL; |
| 5014 | } else if (setregid(rgid, egid) < 0) { |
| 5015 | return posix_error(); |
| 5016 | } else { |
| 5017 | Py_INCREF(Py_None); |
| 5018 | return Py_None; |
| 5019 | } |
| 5020 | } |
| 5021 | #endif /* HAVE_SETREGID */ |
| 5022 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5023 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5024 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5025 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5026 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5027 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5028 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5029 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5030 | { |
| 5031 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5032 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5033 | return NULL; |
| 5034 | if (setgid(gid) < 0) |
| 5035 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5036 | Py_INCREF(Py_None); |
| 5037 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5038 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5039 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5040 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5041 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5042 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5043 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5044 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5045 | |
| 5046 | static PyObject * |
| 5047 | posix_setgroups(PyObject *self, PyObject *args) |
| 5048 | { |
| 5049 | PyObject *groups; |
| 5050 | int i, len; |
| 5051 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5052 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5053 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 5054 | return NULL; |
| 5055 | if (!PySequence_Check(groups)) { |
| 5056 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 5057 | return NULL; |
| 5058 | } |
| 5059 | len = PySequence_Size(groups); |
| 5060 | if (len > MAX_GROUPS) { |
| 5061 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 5062 | return NULL; |
| 5063 | } |
| 5064 | for(i = 0; i < len; i++) { |
| 5065 | PyObject *elem; |
| 5066 | elem = PySequence_GetItem(groups, i); |
| 5067 | if (!elem) |
| 5068 | return NULL; |
| 5069 | if (!PyInt_Check(elem)) { |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 5070 | if (!PyLong_Check(elem)) { |
| 5071 | PyErr_SetString(PyExc_TypeError, |
| 5072 | "groups must be integers"); |
| 5073 | Py_DECREF(elem); |
| 5074 | return NULL; |
| 5075 | } else { |
| 5076 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 5077 | if (PyErr_Occurred()) { |
| 5078 | PyErr_SetString(PyExc_TypeError, |
| 5079 | "group id too big"); |
| 5080 | Py_DECREF(elem); |
| 5081 | return NULL; |
| 5082 | } |
| 5083 | grouplist[i] = x; |
| 5084 | /* read back the value to see if it fitted in gid_t */ |
| 5085 | if (grouplist[i] != x) { |
| 5086 | PyErr_SetString(PyExc_TypeError, |
| 5087 | "group id too big"); |
| 5088 | Py_DECREF(elem); |
| 5089 | return NULL; |
| 5090 | } |
| 5091 | } |
| 5092 | } else { |
| 5093 | long x = PyInt_AsLong(elem); |
| 5094 | grouplist[i] = x; |
| 5095 | if (grouplist[i] != x) { |
| 5096 | PyErr_SetString(PyExc_TypeError, |
| 5097 | "group id too big"); |
| 5098 | Py_DECREF(elem); |
| 5099 | return NULL; |
| 5100 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5101 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5102 | Py_DECREF(elem); |
| 5103 | } |
| 5104 | |
| 5105 | if (setgroups(len, grouplist) < 0) |
| 5106 | return posix_error(); |
| 5107 | Py_INCREF(Py_None); |
| 5108 | return Py_None; |
| 5109 | } |
| 5110 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5111 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 5112 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 5113 | static PyObject * |
| 5114 | wait_helper(int pid, int status, struct rusage *ru) |
| 5115 | { |
| 5116 | PyObject *result; |
| 5117 | static PyObject *struct_rusage; |
| 5118 | |
| 5119 | if (pid == -1) |
| 5120 | return posix_error(); |
| 5121 | |
| 5122 | if (struct_rusage == NULL) { |
| 5123 | PyObject *m = PyImport_ImportModule("resource"); |
| 5124 | if (m == NULL) |
| 5125 | return NULL; |
| 5126 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 5127 | Py_DECREF(m); |
| 5128 | if (struct_rusage == NULL) |
| 5129 | return NULL; |
| 5130 | } |
| 5131 | |
| 5132 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 5133 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 5134 | if (!result) |
| 5135 | return NULL; |
| 5136 | |
| 5137 | #ifndef doubletime |
| 5138 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 5139 | #endif |
| 5140 | |
| 5141 | PyStructSequence_SET_ITEM(result, 0, |
| 5142 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 5143 | PyStructSequence_SET_ITEM(result, 1, |
| 5144 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 5145 | #define SET_INT(result, index, value)\ |
| 5146 | PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value)) |
| 5147 | SET_INT(result, 2, ru->ru_maxrss); |
| 5148 | SET_INT(result, 3, ru->ru_ixrss); |
| 5149 | SET_INT(result, 4, ru->ru_idrss); |
| 5150 | SET_INT(result, 5, ru->ru_isrss); |
| 5151 | SET_INT(result, 6, ru->ru_minflt); |
| 5152 | SET_INT(result, 7, ru->ru_majflt); |
| 5153 | SET_INT(result, 8, ru->ru_nswap); |
| 5154 | SET_INT(result, 9, ru->ru_inblock); |
| 5155 | SET_INT(result, 10, ru->ru_oublock); |
| 5156 | SET_INT(result, 11, ru->ru_msgsnd); |
| 5157 | SET_INT(result, 12, ru->ru_msgrcv); |
| 5158 | SET_INT(result, 13, ru->ru_nsignals); |
| 5159 | SET_INT(result, 14, ru->ru_nvcsw); |
| 5160 | SET_INT(result, 15, ru->ru_nivcsw); |
| 5161 | #undef SET_INT |
| 5162 | |
| 5163 | if (PyErr_Occurred()) { |
| 5164 | Py_DECREF(result); |
| 5165 | return NULL; |
| 5166 | } |
| 5167 | |
| 5168 | return Py_BuildValue("iiN", pid, status, result); |
| 5169 | } |
| 5170 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 5171 | |
| 5172 | #ifdef HAVE_WAIT3 |
| 5173 | PyDoc_STRVAR(posix_wait3__doc__, |
| 5174 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 5175 | Wait for completion of a child process."); |
| 5176 | |
| 5177 | static PyObject * |
| 5178 | posix_wait3(PyObject *self, PyObject *args) |
| 5179 | { |
| 5180 | int pid, options; |
| 5181 | struct rusage ru; |
| 5182 | WAIT_TYPE status; |
| 5183 | WAIT_STATUS_INT(status) = 0; |
| 5184 | |
| 5185 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 5186 | return NULL; |
| 5187 | |
| 5188 | Py_BEGIN_ALLOW_THREADS |
| 5189 | pid = wait3(&status, options, &ru); |
| 5190 | Py_END_ALLOW_THREADS |
| 5191 | |
| 5192 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 5193 | } |
| 5194 | #endif /* HAVE_WAIT3 */ |
| 5195 | |
| 5196 | #ifdef HAVE_WAIT4 |
| 5197 | PyDoc_STRVAR(posix_wait4__doc__, |
| 5198 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 5199 | Wait for completion of a given child process."); |
| 5200 | |
| 5201 | static PyObject * |
| 5202 | posix_wait4(PyObject *self, PyObject *args) |
| 5203 | { |
| 5204 | int pid, options; |
| 5205 | struct rusage ru; |
| 5206 | WAIT_TYPE status; |
| 5207 | WAIT_STATUS_INT(status) = 0; |
| 5208 | |
| 5209 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 5210 | return NULL; |
| 5211 | |
| 5212 | Py_BEGIN_ALLOW_THREADS |
| 5213 | pid = wait4(pid, &status, options, &ru); |
| 5214 | Py_END_ALLOW_THREADS |
| 5215 | |
| 5216 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 5217 | } |
| 5218 | #endif /* HAVE_WAIT4 */ |
| 5219 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5220 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5221 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5222 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5223 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5224 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5225 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5226 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5227 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5228 | int pid, options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 5229 | WAIT_TYPE status; |
| 5230 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5231 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5232 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5233 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5234 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 5235 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5236 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5237 | if (pid == -1) |
| 5238 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 5239 | |
| 5240 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5241 | } |
| 5242 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5243 | #elif defined(HAVE_CWAIT) |
| 5244 | |
| 5245 | /* 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] | 5246 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5247 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5248 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5249 | |
| 5250 | static PyObject * |
| 5251 | posix_waitpid(PyObject *self, PyObject *args) |
| 5252 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5253 | intptr_t pid; |
| 5254 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5255 | |
| 5256 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 5257 | return NULL; |
| 5258 | Py_BEGIN_ALLOW_THREADS |
| 5259 | pid = _cwait(&status, pid, options); |
| 5260 | Py_END_ALLOW_THREADS |
| 5261 | if (pid == -1) |
| 5262 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 5263 | |
| 5264 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 5265 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5266 | } |
| 5267 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5268 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5269 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5270 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5271 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5272 | Wait for completion of a child process."); |
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 * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5275 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5276 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5277 | int pid; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 5278 | WAIT_TYPE status; |
| 5279 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5280 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5281 | Py_BEGIN_ALLOW_THREADS |
| 5282 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5283 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5284 | if (pid == -1) |
| 5285 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 5286 | |
| 5287 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5288 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5289 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5290 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5291 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5292 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5293 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5294 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5295 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5296 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5297 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5298 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5299 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5300 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5301 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5302 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5303 | return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5304 | #else |
| 5305 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 5306 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5307 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5308 | } |
| 5309 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5310 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5311 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5312 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5313 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5314 | 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] | 5315 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5316 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5317 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5318 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5319 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 5320 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5321 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5322 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5323 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5324 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 5325 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5326 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5327 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 5328 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5329 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5330 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5331 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5332 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5333 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5334 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5335 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5336 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 5337 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5338 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5339 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5340 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5341 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5342 | return posix_2str(args, "etet:symlink", symlink, NULL, NULL); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5343 | } |
| 5344 | #endif /* HAVE_SYMLINK */ |
| 5345 | |
| 5346 | |
| 5347 | #ifdef HAVE_TIMES |
| 5348 | #ifndef HZ |
| 5349 | #define HZ 60 /* Universal constant :-) */ |
| 5350 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5351 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5352 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 5353 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5354 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5355 | { |
| 5356 | ULONG value = 0; |
| 5357 | |
| 5358 | Py_BEGIN_ALLOW_THREADS |
| 5359 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 5360 | Py_END_ALLOW_THREADS |
| 5361 | |
| 5362 | return value; |
| 5363 | } |
| 5364 | |
| 5365 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5366 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5367 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5368 | /* Currently Only Uptime is Provided -- Others Later */ |
| 5369 | return Py_BuildValue("ddddd", |
| 5370 | (double)0 /* t.tms_utime / HZ */, |
| 5371 | (double)0 /* t.tms_stime / HZ */, |
| 5372 | (double)0 /* t.tms_cutime / HZ */, |
| 5373 | (double)0 /* t.tms_cstime / HZ */, |
| 5374 | (double)system_uptime() / 1000); |
| 5375 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5376 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5377 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5378 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5379 | { |
| 5380 | struct tms t; |
| 5381 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5382 | errno = 0; |
| 5383 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5384 | if (c == (clock_t) -1) |
| 5385 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5386 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5387 | (double)t.tms_utime / HZ, |
| 5388 | (double)t.tms_stime / HZ, |
| 5389 | (double)t.tms_cutime / HZ, |
| 5390 | (double)t.tms_cstime / HZ, |
| 5391 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5392 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5393 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5394 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5395 | |
| 5396 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5397 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5398 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5399 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5400 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5401 | { |
| 5402 | FILETIME create, exit, kernel, user; |
| 5403 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5404 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5405 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 5406 | /* The fields of a FILETIME structure are the hi and lo part |
| 5407 | of a 64-bit value expressed in 100 nanosecond units. |
| 5408 | 1e7 is one second in such units; 1e-7 the inverse. |
| 5409 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 5410 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5411 | return Py_BuildValue( |
| 5412 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5413 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 5414 | kernel.dwLowDateTime*1e-7), |
| 5415 | (double)(user.dwHighDateTime*429.4967296 + |
| 5416 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5417 | (double)0, |
| 5418 | (double)0, |
| 5419 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5420 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5421 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5422 | |
| 5423 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5424 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5425 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5426 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5427 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5428 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5429 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5430 | #ifdef HAVE_GETSID |
| 5431 | PyDoc_STRVAR(posix_getsid__doc__, |
| 5432 | "getsid(pid) -> sid\n\n\ |
| 5433 | Call the system call getsid()."); |
| 5434 | |
| 5435 | static PyObject * |
| 5436 | posix_getsid(PyObject *self, PyObject *args) |
| 5437 | { |
| 5438 | int pid, sid; |
| 5439 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 5440 | return NULL; |
| 5441 | sid = getsid(pid); |
| 5442 | if (sid < 0) |
| 5443 | return posix_error(); |
| 5444 | return PyInt_FromLong((long)sid); |
| 5445 | } |
| 5446 | #endif /* HAVE_GETSID */ |
| 5447 | |
| 5448 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5449 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5450 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5451 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5452 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5453 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5454 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5455 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5456 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5457 | if (setsid() < 0) |
| 5458 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5459 | Py_INCREF(Py_None); |
| 5460 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5461 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5462 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5463 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5464 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5465 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5466 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5467 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5468 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5469 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5470 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5471 | { |
| 5472 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5473 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5474 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5475 | if (setpgid(pid, pgrp) < 0) |
| 5476 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5477 | Py_INCREF(Py_None); |
| 5478 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5479 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5480 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5481 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5482 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5483 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5484 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5485 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5486 | 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] | 5487 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5488 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5489 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5490 | { |
| 5491 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5492 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5493 | return NULL; |
| 5494 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5495 | if (pgid < 0) |
| 5496 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5497 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5498 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5499 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5500 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5501 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5502 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5503 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5504 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5505 | 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] | 5506 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5507 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5508 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5509 | { |
| 5510 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5511 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5512 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5513 | if (tcsetpgrp(fd, pgid) < 0) |
| 5514 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5515 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5516 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5517 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5518 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5519 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5520 | /* Functions acting on file descriptors */ |
| 5521 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5522 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5523 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5524 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5525 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5526 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5527 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5528 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5529 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5530 | int flag; |
| 5531 | int mode = 0777; |
| 5532 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5533 | |
| 5534 | #ifdef MS_WINDOWS |
| 5535 | if (unicode_file_names()) { |
| 5536 | PyUnicodeObject *po; |
| 5537 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 5538 | Py_BEGIN_ALLOW_THREADS |
| 5539 | /* PyUnicode_AS_UNICODE OK without thread |
| 5540 | lock as it is a simple dereference. */ |
| 5541 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 5542 | Py_END_ALLOW_THREADS |
| 5543 | if (fd < 0) |
| 5544 | return posix_error(); |
| 5545 | return PyInt_FromLong((long)fd); |
| 5546 | } |
| 5547 | /* Drop the argument parsing error as narrow strings |
| 5548 | are also valid. */ |
| 5549 | PyErr_Clear(); |
| 5550 | } |
| 5551 | #endif |
| 5552 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5553 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5554 | Py_FileSystemDefaultEncoding, &file, |
| 5555 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5556 | return NULL; |
| 5557 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5558 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5559 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5560 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5561 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5562 | return posix_error_with_allocated_filename(file); |
| 5563 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5564 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5565 | } |
| 5566 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5567 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5568 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5569 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5570 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5571 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5572 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5573 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5574 | { |
| 5575 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5576 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5577 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5578 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5579 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5580 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5581 | if (res < 0) |
| 5582 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5583 | Py_INCREF(Py_None); |
| 5584 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5585 | } |
| 5586 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5587 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5588 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5589 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5590 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5591 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5592 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5593 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5594 | { |
| 5595 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5596 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5597 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5598 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5599 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5600 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5601 | if (fd < 0) |
| 5602 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5603 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5604 | } |
| 5605 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5606 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5607 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5608 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5609 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5610 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5611 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5612 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5613 | { |
| 5614 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5615 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5616 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5617 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5618 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5619 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5620 | if (res < 0) |
| 5621 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5622 | Py_INCREF(Py_None); |
| 5623 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5624 | } |
| 5625 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5626 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5627 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5628 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5629 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5630 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5631 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5632 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5633 | { |
| 5634 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5635 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5636 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5637 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5638 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5639 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5640 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5641 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5642 | return NULL; |
| 5643 | #ifdef SEEK_SET |
| 5644 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5645 | switch (how) { |
| 5646 | case 0: how = SEEK_SET; break; |
| 5647 | case 1: how = SEEK_CUR; break; |
| 5648 | case 2: how = SEEK_END; break; |
| 5649 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5650 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5651 | |
| 5652 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5653 | pos = PyInt_AsLong(posobj); |
| 5654 | #else |
| 5655 | pos = PyLong_Check(posobj) ? |
| 5656 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 5657 | #endif |
| 5658 | if (PyErr_Occurred()) |
| 5659 | return NULL; |
| 5660 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5661 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5662 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5663 | res = _lseeki64(fd, pos, how); |
| 5664 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5665 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5666 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5667 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5668 | if (res < 0) |
| 5669 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5670 | |
| 5671 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5672 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5673 | #else |
| 5674 | return PyLong_FromLongLong(res); |
| 5675 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5676 | } |
| 5677 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5678 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5679 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5680 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5681 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5682 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5683 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5684 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5685 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5686 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5687 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5688 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5689 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 5690 | if (size < 0) { |
| 5691 | errno = EINVAL; |
| 5692 | return posix_error(); |
| 5693 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5694 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5695 | if (buffer == NULL) |
| 5696 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5697 | Py_BEGIN_ALLOW_THREADS |
| 5698 | n = read(fd, PyString_AsString(buffer), size); |
| 5699 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5700 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5701 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5702 | return posix_error(); |
| 5703 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5704 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5705 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5706 | return buffer; |
| 5707 | } |
| 5708 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5709 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5710 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5711 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5712 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5713 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5714 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5715 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5716 | { |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5717 | int fd; |
| 5718 | Py_ssize_t size; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5719 | char *buffer; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5720 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5721 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5722 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5723 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5724 | size = write(fd, buffer, (size_t)size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5725 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5726 | if (size < 0) |
| 5727 | return posix_error(); |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5728 | return PyInt_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5729 | } |
| 5730 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5731 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5732 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5733 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5734 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5735 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5736 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5737 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5738 | { |
| 5739 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5740 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5741 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5742 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5743 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5744 | #ifdef __VMS |
| 5745 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5746 | fsync(fd); |
| 5747 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5748 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5749 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5750 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5751 | if (res != 0) { |
| 5752 | #ifdef MS_WINDOWS |
| 5753 | return win32_error("fstat", NULL); |
| 5754 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5755 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5756 | #endif |
| 5757 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5758 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5759 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5760 | } |
| 5761 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5762 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5763 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5764 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5765 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5766 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5767 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5768 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5769 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5770 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5771 | char *mode = "r"; |
| 5772 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5773 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5774 | PyObject *f; |
| 5775 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5776 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5777 | |
Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 5778 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 5779 | PyErr_Format(PyExc_ValueError, |
| 5780 | "invalid file mode '%s'", mode); |
| 5781 | return NULL; |
| 5782 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5783 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 5784 | #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H) |
| 5785 | if (mode[0] == 'a') { |
| 5786 | /* try to make sure the O_APPEND flag is set */ |
| 5787 | int flags; |
| 5788 | flags = fcntl(fd, F_GETFL); |
| 5789 | if (flags != -1) |
| 5790 | fcntl(fd, F_SETFL, flags | O_APPEND); |
| 5791 | fp = fdopen(fd, mode); |
| 5792 | if (fp == NULL && flags != -1) |
| 5793 | /* restore old mode if fdopen failed */ |
| 5794 | fcntl(fd, F_SETFL, flags); |
| 5795 | } else { |
| 5796 | fp = fdopen(fd, mode); |
| 5797 | } |
| 5798 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5799 | fp = fdopen(fd, mode); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 5800 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5801 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5802 | if (fp == NULL) |
| 5803 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 5804 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5805 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5806 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5807 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5808 | } |
| 5809 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5810 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5811 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5812 | 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] | 5813 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5814 | |
| 5815 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5816 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5817 | { |
| 5818 | int fd; |
| 5819 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5820 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5821 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5822 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5823 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5824 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5825 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5826 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5827 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5828 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5829 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5830 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5831 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5832 | #if defined(PYOS_OS2) |
| 5833 | HFILE read, write; |
| 5834 | APIRET rc; |
| 5835 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5836 | Py_BEGIN_ALLOW_THREADS |
| 5837 | rc = DosCreatePipe( &read, &write, 4096); |
| 5838 | Py_END_ALLOW_THREADS |
| 5839 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5840 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5841 | |
| 5842 | return Py_BuildValue("(ii)", read, write); |
| 5843 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5844 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5845 | int fds[2]; |
| 5846 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5847 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5848 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5849 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5850 | if (res != 0) |
| 5851 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5852 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5853 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5854 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5855 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5856 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5857 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5858 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5859 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5860 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5861 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5862 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5863 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5864 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5865 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5866 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5867 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5868 | #endif /* HAVE_PIPE */ |
| 5869 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5870 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5871 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5872 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5873 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5874 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5875 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5876 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5877 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5878 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5879 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5880 | int mode = 0666; |
| 5881 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5882 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5883 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5884 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5885 | res = mkfifo(filename, mode); |
| 5886 | Py_END_ALLOW_THREADS |
| 5887 | if (res < 0) |
| 5888 | return posix_error(); |
| 5889 | Py_INCREF(Py_None); |
| 5890 | return Py_None; |
| 5891 | } |
| 5892 | #endif |
| 5893 | |
| 5894 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5895 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5896 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5897 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5898 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5899 | named filename. mode specifies both the permissions to use and the\n\ |
| 5900 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5901 | 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] | 5902 | device defines the newly created device special file (probably using\n\ |
| 5903 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5904 | |
| 5905 | |
| 5906 | static PyObject * |
| 5907 | posix_mknod(PyObject *self, PyObject *args) |
| 5908 | { |
| 5909 | char *filename; |
| 5910 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5911 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5912 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5913 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5914 | return NULL; |
| 5915 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5916 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5917 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5918 | if (res < 0) |
| 5919 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5920 | Py_INCREF(Py_None); |
| 5921 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5922 | } |
| 5923 | #endif |
| 5924 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5925 | #ifdef HAVE_DEVICE_MACROS |
| 5926 | PyDoc_STRVAR(posix_major__doc__, |
| 5927 | "major(device) -> major number\n\ |
| 5928 | Extracts a device major number from a raw device number."); |
| 5929 | |
| 5930 | static PyObject * |
| 5931 | posix_major(PyObject *self, PyObject *args) |
| 5932 | { |
| 5933 | int device; |
| 5934 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5935 | return NULL; |
| 5936 | return PyInt_FromLong((long)major(device)); |
| 5937 | } |
| 5938 | |
| 5939 | PyDoc_STRVAR(posix_minor__doc__, |
| 5940 | "minor(device) -> minor number\n\ |
| 5941 | Extracts a device minor number from a raw device number."); |
| 5942 | |
| 5943 | static PyObject * |
| 5944 | posix_minor(PyObject *self, PyObject *args) |
| 5945 | { |
| 5946 | int device; |
| 5947 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5948 | return NULL; |
| 5949 | return PyInt_FromLong((long)minor(device)); |
| 5950 | } |
| 5951 | |
| 5952 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5953 | "makedev(major, minor) -> device number\n\ |
| 5954 | Composes a raw device number from the major and minor device numbers."); |
| 5955 | |
| 5956 | static PyObject * |
| 5957 | posix_makedev(PyObject *self, PyObject *args) |
| 5958 | { |
| 5959 | int major, minor; |
| 5960 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5961 | return NULL; |
| 5962 | return PyInt_FromLong((long)makedev(major, minor)); |
| 5963 | } |
| 5964 | #endif /* device macros */ |
| 5965 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5966 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5967 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5968 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5969 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5970 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5971 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5972 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5973 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5974 | { |
| 5975 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5976 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5977 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5978 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5979 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5980 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5981 | return NULL; |
| 5982 | |
| 5983 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5984 | length = PyInt_AsLong(lenobj); |
| 5985 | #else |
| 5986 | length = PyLong_Check(lenobj) ? |
| 5987 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 5988 | #endif |
| 5989 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5990 | return NULL; |
| 5991 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5992 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5993 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5994 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5995 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5996 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5997 | return NULL; |
| 5998 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5999 | Py_INCREF(Py_None); |
| 6000 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6001 | } |
| 6002 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6003 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6004 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6005 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6006 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6007 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6008 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6009 | /* Save putenv() parameters as values here, so we can collect them when they |
| 6010 | * get re-set with another call for the same key. */ |
| 6011 | static PyObject *posix_putenv_garbage; |
| 6012 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6013 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6014 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6015 | { |
| 6016 | char *s1, *s2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6017 | char *newenv; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6018 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 6019 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6020 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6021 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6022 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6023 | |
| 6024 | #if defined(PYOS_OS2) |
| 6025 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 6026 | APIRET rc; |
| 6027 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6028 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 6029 | if (rc != NO_ERROR) |
| 6030 | return os2_error(rc); |
| 6031 | |
| 6032 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 6033 | APIRET rc; |
| 6034 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6035 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 6036 | if (rc != NO_ERROR) |
| 6037 | return os2_error(rc); |
| 6038 | } else { |
| 6039 | #endif |
| 6040 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6041 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 6042 | len = strlen(s1) + strlen(s2) + 2; |
| 6043 | /* len includes space for a trailing \0; the size arg to |
| 6044 | PyString_FromStringAndSize does not count that */ |
| 6045 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6046 | if (newstr == NULL) |
| 6047 | return PyErr_NoMemory(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6048 | newenv = PyString_AS_STRING(newstr); |
| 6049 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 6050 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 6051 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6052 | posix_error(); |
| 6053 | return NULL; |
| 6054 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6055 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 6056 | * this will cause previous value to be collected. This has to |
| 6057 | * happen after the real putenv() call because the old value |
| 6058 | * was still accessible until then. */ |
| 6059 | if (PyDict_SetItem(posix_putenv_garbage, |
| 6060 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 6061 | /* really not much we can do; just leak */ |
| 6062 | PyErr_Clear(); |
| 6063 | } |
| 6064 | else { |
| 6065 | Py_DECREF(newstr); |
| 6066 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6067 | |
| 6068 | #if defined(PYOS_OS2) |
| 6069 | } |
| 6070 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6071 | Py_INCREF(Py_None); |
| 6072 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6073 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6074 | #endif /* putenv */ |
| 6075 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6076 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6077 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6078 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6079 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6080 | |
| 6081 | static PyObject * |
| 6082 | posix_unsetenv(PyObject *self, PyObject *args) |
| 6083 | { |
| 6084 | char *s1; |
| 6085 | |
| 6086 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 6087 | return NULL; |
| 6088 | |
| 6089 | unsetenv(s1); |
| 6090 | |
| 6091 | /* Remove the key from posix_putenv_garbage; |
| 6092 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6093 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6094 | * old value was still accessible until then. |
| 6095 | */ |
| 6096 | if (PyDict_DelItem(posix_putenv_garbage, |
| 6097 | PyTuple_GET_ITEM(args, 0))) { |
| 6098 | /* really not much we can do; just leak */ |
| 6099 | PyErr_Clear(); |
| 6100 | } |
| 6101 | |
| 6102 | Py_INCREF(Py_None); |
| 6103 | return Py_None; |
| 6104 | } |
| 6105 | #endif /* unsetenv */ |
| 6106 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6107 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6108 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6109 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6110 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6111 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6112 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6113 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6114 | { |
| 6115 | int code; |
| 6116 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6117 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6118 | return NULL; |
| 6119 | message = strerror(code); |
| 6120 | if (message == NULL) { |
| 6121 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 6122 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6123 | return NULL; |
| 6124 | } |
| 6125 | return PyString_FromString(message); |
| 6126 | } |
| 6127 | #endif /* strerror */ |
| 6128 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6129 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6130 | #ifdef HAVE_SYS_WAIT_H |
| 6131 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6132 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6133 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6134 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6135 | 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] | 6136 | |
| 6137 | static PyObject * |
| 6138 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 6139 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6140 | WAIT_TYPE status; |
| 6141 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6142 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6143 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6144 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6145 | |
| 6146 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6147 | } |
| 6148 | #endif /* WCOREDUMP */ |
| 6149 | |
| 6150 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6151 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6152 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6153 | 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] | 6154 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6155 | |
| 6156 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6157 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6158 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6159 | WAIT_TYPE status; |
| 6160 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6161 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6162 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6163 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6164 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6165 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6166 | } |
| 6167 | #endif /* WIFCONTINUED */ |
| 6168 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6169 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6170 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6171 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6172 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6173 | |
| 6174 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6175 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6176 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6177 | WAIT_TYPE status; |
| 6178 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6179 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6180 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6181 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6182 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6183 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6184 | } |
| 6185 | #endif /* WIFSTOPPED */ |
| 6186 | |
| 6187 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6188 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6189 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6190 | 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] | 6191 | |
| 6192 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6193 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6194 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6195 | WAIT_TYPE status; |
| 6196 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6197 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6198 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6199 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6200 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6201 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6202 | } |
| 6203 | #endif /* WIFSIGNALED */ |
| 6204 | |
| 6205 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6206 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6207 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6208 | 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] | 6209 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6210 | |
| 6211 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6212 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6213 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6214 | WAIT_TYPE status; |
| 6215 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6216 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6217 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6218 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6219 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6220 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6221 | } |
| 6222 | #endif /* WIFEXITED */ |
| 6223 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6224 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6225 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6226 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6227 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6228 | |
| 6229 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6230 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6231 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6232 | WAIT_TYPE status; |
| 6233 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6234 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6235 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6236 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6237 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6238 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 6239 | } |
| 6240 | #endif /* WEXITSTATUS */ |
| 6241 | |
| 6242 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6243 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6244 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6245 | 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] | 6246 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6247 | |
| 6248 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6249 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6250 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6251 | WAIT_TYPE status; |
| 6252 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6253 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6254 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6255 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6256 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6257 | return Py_BuildValue("i", WTERMSIG(status)); |
| 6258 | } |
| 6259 | #endif /* WTERMSIG */ |
| 6260 | |
| 6261 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6262 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6263 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6264 | Return the signal that stopped the process that provided\n\ |
| 6265 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6266 | |
| 6267 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6268 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6269 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6270 | WAIT_TYPE status; |
| 6271 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6272 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6273 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6274 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6275 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6276 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 6277 | } |
| 6278 | #endif /* WSTOPSIG */ |
| 6279 | |
| 6280 | #endif /* HAVE_SYS_WAIT_H */ |
| 6281 | |
| 6282 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6283 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 6284 | #ifdef _SCO_DS |
| 6285 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 6286 | needed definitions in sys/statvfs.h */ |
| 6287 | #define _SVID3 |
| 6288 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6289 | #include <sys/statvfs.h> |
| 6290 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6291 | static PyObject* |
| 6292 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 6293 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 6294 | if (v == NULL) |
| 6295 | return NULL; |
| 6296 | |
| 6297 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6298 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6299 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 6300 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 6301 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 6302 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 6303 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 6304 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 6305 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 6306 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6307 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6308 | #else |
| 6309 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6310 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6311 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6312 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6313 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6314 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6315 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6316 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6317 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6318 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6319 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6320 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6321 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6322 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6323 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6324 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6325 | #endif |
| 6326 | |
| 6327 | return v; |
| 6328 | } |
| 6329 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6330 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6331 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6332 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6333 | |
| 6334 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6335 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6336 | { |
| 6337 | int fd, res; |
| 6338 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6339 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6340 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6341 | return NULL; |
| 6342 | Py_BEGIN_ALLOW_THREADS |
| 6343 | res = fstatvfs(fd, &st); |
| 6344 | Py_END_ALLOW_THREADS |
| 6345 | if (res != 0) |
| 6346 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6347 | |
| 6348 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6349 | } |
| 6350 | #endif /* HAVE_FSTATVFS */ |
| 6351 | |
| 6352 | |
| 6353 | #if defined(HAVE_STATVFS) |
| 6354 | #include <sys/statvfs.h> |
| 6355 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6356 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6357 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6358 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6359 | |
| 6360 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6361 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6362 | { |
| 6363 | char *path; |
| 6364 | int res; |
| 6365 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6366 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6367 | return NULL; |
| 6368 | Py_BEGIN_ALLOW_THREADS |
| 6369 | res = statvfs(path, &st); |
| 6370 | Py_END_ALLOW_THREADS |
| 6371 | if (res != 0) |
| 6372 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6373 | |
| 6374 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6375 | } |
| 6376 | #endif /* HAVE_STATVFS */ |
| 6377 | |
| 6378 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6379 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6380 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6381 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6382 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 6383 | 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] | 6384 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6385 | |
| 6386 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6387 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6388 | { |
| 6389 | PyObject *result = NULL; |
| 6390 | char *dir = NULL; |
| 6391 | char *pfx = NULL; |
| 6392 | char *name; |
| 6393 | |
| 6394 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 6395 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6396 | |
| 6397 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6398 | "tempnam is a potential security risk to your program") < 0) |
| 6399 | return NULL; |
| 6400 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6401 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6402 | name = _tempnam(dir, pfx); |
| 6403 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6404 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6405 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6406 | if (name == NULL) |
| 6407 | return PyErr_NoMemory(); |
| 6408 | result = PyString_FromString(name); |
| 6409 | free(name); |
| 6410 | return result; |
| 6411 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 6412 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6413 | |
| 6414 | |
| 6415 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6416 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6417 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6418 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6419 | |
| 6420 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6421 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6422 | { |
| 6423 | FILE *fp; |
| 6424 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6425 | fp = tmpfile(); |
| 6426 | if (fp == NULL) |
| 6427 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 6428 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6429 | } |
| 6430 | #endif |
| 6431 | |
| 6432 | |
| 6433 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6434 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6435 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6436 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6437 | |
| 6438 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6439 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6440 | { |
| 6441 | char buffer[L_tmpnam]; |
| 6442 | char *name; |
| 6443 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6444 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6445 | "tmpnam is a potential security risk to your program") < 0) |
| 6446 | return NULL; |
| 6447 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6448 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6449 | name = tmpnam_r(buffer); |
| 6450 | #else |
| 6451 | name = tmpnam(buffer); |
| 6452 | #endif |
| 6453 | if (name == NULL) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6454 | PyObject *err = Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6455 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6456 | "unexpected NULL from tmpnam_r" |
| 6457 | #else |
| 6458 | "unexpected NULL from tmpnam" |
| 6459 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6460 | ); |
| 6461 | PyErr_SetObject(PyExc_OSError, err); |
| 6462 | Py_XDECREF(err); |
| 6463 | return NULL; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6464 | } |
| 6465 | return PyString_FromString(buffer); |
| 6466 | } |
| 6467 | #endif |
| 6468 | |
| 6469 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6470 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 6471 | * It maps strings representing configuration variable names to |
| 6472 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6473 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6474 | * rarely-used constants. There are three separate tables that use |
| 6475 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6476 | * |
| 6477 | * This code is always included, even if none of the interfaces that |
| 6478 | * need it are included. The #if hackery needed to avoid it would be |
| 6479 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6480 | */ |
| 6481 | struct constdef { |
| 6482 | char *name; |
| 6483 | long value; |
| 6484 | }; |
| 6485 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6486 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6487 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 6488 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6489 | { |
| 6490 | if (PyInt_Check(arg)) { |
| 6491 | *valuep = PyInt_AS_LONG(arg); |
| 6492 | return 1; |
| 6493 | } |
| 6494 | if (PyString_Check(arg)) { |
| 6495 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6496 | size_t lo = 0; |
| 6497 | size_t mid; |
| 6498 | size_t hi = tablesize; |
| 6499 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6500 | char *confname = PyString_AS_STRING(arg); |
| 6501 | while (lo < hi) { |
| 6502 | mid = (lo + hi) / 2; |
| 6503 | cmp = strcmp(confname, table[mid].name); |
| 6504 | if (cmp < 0) |
| 6505 | hi = mid; |
| 6506 | else if (cmp > 0) |
| 6507 | lo = mid + 1; |
| 6508 | else { |
| 6509 | *valuep = table[mid].value; |
| 6510 | return 1; |
| 6511 | } |
| 6512 | } |
| 6513 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 6514 | } |
| 6515 | else |
| 6516 | PyErr_SetString(PyExc_TypeError, |
| 6517 | "configuration names must be strings or integers"); |
| 6518 | return 0; |
| 6519 | } |
| 6520 | |
| 6521 | |
| 6522 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 6523 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6524 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 6525 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 6526 | #endif |
| 6527 | #ifdef _PC_ABI_ASYNC_IO |
| 6528 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 6529 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6530 | #ifdef _PC_ASYNC_IO |
| 6531 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 6532 | #endif |
| 6533 | #ifdef _PC_CHOWN_RESTRICTED |
| 6534 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 6535 | #endif |
| 6536 | #ifdef _PC_FILESIZEBITS |
| 6537 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 6538 | #endif |
| 6539 | #ifdef _PC_LAST |
| 6540 | {"PC_LAST", _PC_LAST}, |
| 6541 | #endif |
| 6542 | #ifdef _PC_LINK_MAX |
| 6543 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 6544 | #endif |
| 6545 | #ifdef _PC_MAX_CANON |
| 6546 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 6547 | #endif |
| 6548 | #ifdef _PC_MAX_INPUT |
| 6549 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 6550 | #endif |
| 6551 | #ifdef _PC_NAME_MAX |
| 6552 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 6553 | #endif |
| 6554 | #ifdef _PC_NO_TRUNC |
| 6555 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 6556 | #endif |
| 6557 | #ifdef _PC_PATH_MAX |
| 6558 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 6559 | #endif |
| 6560 | #ifdef _PC_PIPE_BUF |
| 6561 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 6562 | #endif |
| 6563 | #ifdef _PC_PRIO_IO |
| 6564 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 6565 | #endif |
| 6566 | #ifdef _PC_SOCK_MAXBUF |
| 6567 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 6568 | #endif |
| 6569 | #ifdef _PC_SYNC_IO |
| 6570 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 6571 | #endif |
| 6572 | #ifdef _PC_VDISABLE |
| 6573 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 6574 | #endif |
| 6575 | }; |
| 6576 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6577 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6578 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6579 | { |
| 6580 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 6581 | sizeof(posix_constants_pathconf) |
| 6582 | / sizeof(struct constdef)); |
| 6583 | } |
| 6584 | #endif |
| 6585 | |
| 6586 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6587 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6588 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6589 | 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] | 6590 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6591 | |
| 6592 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6593 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6594 | { |
| 6595 | PyObject *result = NULL; |
| 6596 | int name, fd; |
| 6597 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6598 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 6599 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6600 | long limit; |
| 6601 | |
| 6602 | errno = 0; |
| 6603 | limit = fpathconf(fd, name); |
| 6604 | if (limit == -1 && errno != 0) |
| 6605 | posix_error(); |
| 6606 | else |
| 6607 | result = PyInt_FromLong(limit); |
| 6608 | } |
| 6609 | return result; |
| 6610 | } |
| 6611 | #endif |
| 6612 | |
| 6613 | |
| 6614 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6615 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6616 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6617 | 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] | 6618 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6619 | |
| 6620 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6621 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6622 | { |
| 6623 | PyObject *result = NULL; |
| 6624 | int name; |
| 6625 | char *path; |
| 6626 | |
| 6627 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 6628 | conv_path_confname, &name)) { |
| 6629 | long limit; |
| 6630 | |
| 6631 | errno = 0; |
| 6632 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6633 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6634 | if (errno == EINVAL) |
| 6635 | /* could be a path or name problem */ |
| 6636 | posix_error(); |
| 6637 | else |
| 6638 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6639 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6640 | else |
| 6641 | result = PyInt_FromLong(limit); |
| 6642 | } |
| 6643 | return result; |
| 6644 | } |
| 6645 | #endif |
| 6646 | |
| 6647 | #ifdef HAVE_CONFSTR |
| 6648 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6649 | #ifdef _CS_ARCHITECTURE |
| 6650 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 6651 | #endif |
| 6652 | #ifdef _CS_HOSTNAME |
| 6653 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 6654 | #endif |
| 6655 | #ifdef _CS_HW_PROVIDER |
| 6656 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 6657 | #endif |
| 6658 | #ifdef _CS_HW_SERIAL |
| 6659 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 6660 | #endif |
| 6661 | #ifdef _CS_INITTAB_NAME |
| 6662 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 6663 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6664 | #ifdef _CS_LFS64_CFLAGS |
| 6665 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 6666 | #endif |
| 6667 | #ifdef _CS_LFS64_LDFLAGS |
| 6668 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 6669 | #endif |
| 6670 | #ifdef _CS_LFS64_LIBS |
| 6671 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 6672 | #endif |
| 6673 | #ifdef _CS_LFS64_LINTFLAGS |
| 6674 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 6675 | #endif |
| 6676 | #ifdef _CS_LFS_CFLAGS |
| 6677 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 6678 | #endif |
| 6679 | #ifdef _CS_LFS_LDFLAGS |
| 6680 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6681 | #endif |
| 6682 | #ifdef _CS_LFS_LIBS |
| 6683 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6684 | #endif |
| 6685 | #ifdef _CS_LFS_LINTFLAGS |
| 6686 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6687 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6688 | #ifdef _CS_MACHINE |
| 6689 | {"CS_MACHINE", _CS_MACHINE}, |
| 6690 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6691 | #ifdef _CS_PATH |
| 6692 | {"CS_PATH", _CS_PATH}, |
| 6693 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6694 | #ifdef _CS_RELEASE |
| 6695 | {"CS_RELEASE", _CS_RELEASE}, |
| 6696 | #endif |
| 6697 | #ifdef _CS_SRPC_DOMAIN |
| 6698 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6699 | #endif |
| 6700 | #ifdef _CS_SYSNAME |
| 6701 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6702 | #endif |
| 6703 | #ifdef _CS_VERSION |
| 6704 | {"CS_VERSION", _CS_VERSION}, |
| 6705 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6706 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6707 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6708 | #endif |
| 6709 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6710 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6711 | #endif |
| 6712 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6713 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6714 | #endif |
| 6715 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6716 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6717 | #endif |
| 6718 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6719 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6720 | #endif |
| 6721 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6722 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6723 | #endif |
| 6724 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6725 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6726 | #endif |
| 6727 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6728 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6729 | #endif |
| 6730 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6731 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6732 | #endif |
| 6733 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6734 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6735 | #endif |
| 6736 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6737 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6738 | #endif |
| 6739 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6740 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6741 | #endif |
| 6742 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6743 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6744 | #endif |
| 6745 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6746 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6747 | #endif |
| 6748 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6749 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6750 | #endif |
| 6751 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6752 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6753 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6754 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6755 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6756 | #endif |
| 6757 | #ifdef _MIPS_CS_BASE |
| 6758 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6759 | #endif |
| 6760 | #ifdef _MIPS_CS_HOSTID |
| 6761 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6762 | #endif |
| 6763 | #ifdef _MIPS_CS_HW_NAME |
| 6764 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6765 | #endif |
| 6766 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6767 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6768 | #endif |
| 6769 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6770 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6771 | #endif |
| 6772 | #ifdef _MIPS_CS_OSREL_MIN |
| 6773 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6774 | #endif |
| 6775 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6776 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6777 | #endif |
| 6778 | #ifdef _MIPS_CS_OS_NAME |
| 6779 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6780 | #endif |
| 6781 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6782 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6783 | #endif |
| 6784 | #ifdef _MIPS_CS_PROCESSORS |
| 6785 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6786 | #endif |
| 6787 | #ifdef _MIPS_CS_SERIAL |
| 6788 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6789 | #endif |
| 6790 | #ifdef _MIPS_CS_VENDOR |
| 6791 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6792 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6793 | }; |
| 6794 | |
| 6795 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6796 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6797 | { |
| 6798 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6799 | sizeof(posix_constants_confstr) |
| 6800 | / sizeof(struct constdef)); |
| 6801 | } |
| 6802 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6803 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6804 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6805 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6806 | |
| 6807 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6808 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6809 | { |
| 6810 | PyObject *result = NULL; |
| 6811 | int name; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6812 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6813 | |
| 6814 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6815 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6816 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6817 | errno = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6818 | len = confstr(name, buffer, sizeof(buffer)); |
| 6819 | if (len == 0) { |
| 6820 | if (errno) { |
| 6821 | posix_error(); |
| 6822 | } |
| 6823 | else { |
| 6824 | result = Py_None; |
| 6825 | Py_INCREF(Py_None); |
| 6826 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6827 | } |
| 6828 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6829 | if ((unsigned int)len >= sizeof(buffer)) { |
| 6830 | result = PyString_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6831 | if (result != NULL) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6832 | confstr(name, PyString_AS_STRING(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6833 | } |
| 6834 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 6835 | result = PyString_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6836 | } |
| 6837 | } |
| 6838 | return result; |
| 6839 | } |
| 6840 | #endif |
| 6841 | |
| 6842 | |
| 6843 | #ifdef HAVE_SYSCONF |
| 6844 | static struct constdef posix_constants_sysconf[] = { |
| 6845 | #ifdef _SC_2_CHAR_TERM |
| 6846 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6847 | #endif |
| 6848 | #ifdef _SC_2_C_BIND |
| 6849 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6850 | #endif |
| 6851 | #ifdef _SC_2_C_DEV |
| 6852 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6853 | #endif |
| 6854 | #ifdef _SC_2_C_VERSION |
| 6855 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6856 | #endif |
| 6857 | #ifdef _SC_2_FORT_DEV |
| 6858 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6859 | #endif |
| 6860 | #ifdef _SC_2_FORT_RUN |
| 6861 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6862 | #endif |
| 6863 | #ifdef _SC_2_LOCALEDEF |
| 6864 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6865 | #endif |
| 6866 | #ifdef _SC_2_SW_DEV |
| 6867 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6868 | #endif |
| 6869 | #ifdef _SC_2_UPE |
| 6870 | {"SC_2_UPE", _SC_2_UPE}, |
| 6871 | #endif |
| 6872 | #ifdef _SC_2_VERSION |
| 6873 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6874 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6875 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6876 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6877 | #endif |
| 6878 | #ifdef _SC_ACL |
| 6879 | {"SC_ACL", _SC_ACL}, |
| 6880 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6881 | #ifdef _SC_AIO_LISTIO_MAX |
| 6882 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6883 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6884 | #ifdef _SC_AIO_MAX |
| 6885 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6886 | #endif |
| 6887 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6888 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6889 | #endif |
| 6890 | #ifdef _SC_ARG_MAX |
| 6891 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6892 | #endif |
| 6893 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6894 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6895 | #endif |
| 6896 | #ifdef _SC_ATEXIT_MAX |
| 6897 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6898 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6899 | #ifdef _SC_AUDIT |
| 6900 | {"SC_AUDIT", _SC_AUDIT}, |
| 6901 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6902 | #ifdef _SC_AVPHYS_PAGES |
| 6903 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6904 | #endif |
| 6905 | #ifdef _SC_BC_BASE_MAX |
| 6906 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6907 | #endif |
| 6908 | #ifdef _SC_BC_DIM_MAX |
| 6909 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6910 | #endif |
| 6911 | #ifdef _SC_BC_SCALE_MAX |
| 6912 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6913 | #endif |
| 6914 | #ifdef _SC_BC_STRING_MAX |
| 6915 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6916 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6917 | #ifdef _SC_CAP |
| 6918 | {"SC_CAP", _SC_CAP}, |
| 6919 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6920 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6921 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6922 | #endif |
| 6923 | #ifdef _SC_CHAR_BIT |
| 6924 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6925 | #endif |
| 6926 | #ifdef _SC_CHAR_MAX |
| 6927 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6928 | #endif |
| 6929 | #ifdef _SC_CHAR_MIN |
| 6930 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6931 | #endif |
| 6932 | #ifdef _SC_CHILD_MAX |
| 6933 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6934 | #endif |
| 6935 | #ifdef _SC_CLK_TCK |
| 6936 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6937 | #endif |
| 6938 | #ifdef _SC_COHER_BLKSZ |
| 6939 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6940 | #endif |
| 6941 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6942 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6943 | #endif |
| 6944 | #ifdef _SC_DCACHE_ASSOC |
| 6945 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6946 | #endif |
| 6947 | #ifdef _SC_DCACHE_BLKSZ |
| 6948 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6949 | #endif |
| 6950 | #ifdef _SC_DCACHE_LINESZ |
| 6951 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6952 | #endif |
| 6953 | #ifdef _SC_DCACHE_SZ |
| 6954 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6955 | #endif |
| 6956 | #ifdef _SC_DCACHE_TBLKSZ |
| 6957 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6958 | #endif |
| 6959 | #ifdef _SC_DELAYTIMER_MAX |
| 6960 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6961 | #endif |
| 6962 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6963 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6964 | #endif |
| 6965 | #ifdef _SC_EXPR_NEST_MAX |
| 6966 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6967 | #endif |
| 6968 | #ifdef _SC_FSYNC |
| 6969 | {"SC_FSYNC", _SC_FSYNC}, |
| 6970 | #endif |
| 6971 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6972 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6973 | #endif |
| 6974 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6975 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6976 | #endif |
| 6977 | #ifdef _SC_ICACHE_ASSOC |
| 6978 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6979 | #endif |
| 6980 | #ifdef _SC_ICACHE_BLKSZ |
| 6981 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6982 | #endif |
| 6983 | #ifdef _SC_ICACHE_LINESZ |
| 6984 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6985 | #endif |
| 6986 | #ifdef _SC_ICACHE_SZ |
| 6987 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6988 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6989 | #ifdef _SC_INF |
| 6990 | {"SC_INF", _SC_INF}, |
| 6991 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6992 | #ifdef _SC_INT_MAX |
| 6993 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6994 | #endif |
| 6995 | #ifdef _SC_INT_MIN |
| 6996 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6997 | #endif |
| 6998 | #ifdef _SC_IOV_MAX |
| 6999 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 7000 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7001 | #ifdef _SC_IP_SECOPTS |
| 7002 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 7003 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7004 | #ifdef _SC_JOB_CONTROL |
| 7005 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 7006 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7007 | #ifdef _SC_KERN_POINTERS |
| 7008 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 7009 | #endif |
| 7010 | #ifdef _SC_KERN_SIM |
| 7011 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 7012 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7013 | #ifdef _SC_LINE_MAX |
| 7014 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 7015 | #endif |
| 7016 | #ifdef _SC_LOGIN_NAME_MAX |
| 7017 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 7018 | #endif |
| 7019 | #ifdef _SC_LOGNAME_MAX |
| 7020 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 7021 | #endif |
| 7022 | #ifdef _SC_LONG_BIT |
| 7023 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 7024 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7025 | #ifdef _SC_MAC |
| 7026 | {"SC_MAC", _SC_MAC}, |
| 7027 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7028 | #ifdef _SC_MAPPED_FILES |
| 7029 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 7030 | #endif |
| 7031 | #ifdef _SC_MAXPID |
| 7032 | {"SC_MAXPID", _SC_MAXPID}, |
| 7033 | #endif |
| 7034 | #ifdef _SC_MB_LEN_MAX |
| 7035 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 7036 | #endif |
| 7037 | #ifdef _SC_MEMLOCK |
| 7038 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 7039 | #endif |
| 7040 | #ifdef _SC_MEMLOCK_RANGE |
| 7041 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 7042 | #endif |
| 7043 | #ifdef _SC_MEMORY_PROTECTION |
| 7044 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 7045 | #endif |
| 7046 | #ifdef _SC_MESSAGE_PASSING |
| 7047 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 7048 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7049 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 7050 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 7051 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7052 | #ifdef _SC_MQ_OPEN_MAX |
| 7053 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 7054 | #endif |
| 7055 | #ifdef _SC_MQ_PRIO_MAX |
| 7056 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 7057 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7058 | #ifdef _SC_NACLS_MAX |
| 7059 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 7060 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7061 | #ifdef _SC_NGROUPS_MAX |
| 7062 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 7063 | #endif |
| 7064 | #ifdef _SC_NL_ARGMAX |
| 7065 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 7066 | #endif |
| 7067 | #ifdef _SC_NL_LANGMAX |
| 7068 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 7069 | #endif |
| 7070 | #ifdef _SC_NL_MSGMAX |
| 7071 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 7072 | #endif |
| 7073 | #ifdef _SC_NL_NMAX |
| 7074 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 7075 | #endif |
| 7076 | #ifdef _SC_NL_SETMAX |
| 7077 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 7078 | #endif |
| 7079 | #ifdef _SC_NL_TEXTMAX |
| 7080 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 7081 | #endif |
| 7082 | #ifdef _SC_NPROCESSORS_CONF |
| 7083 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 7084 | #endif |
| 7085 | #ifdef _SC_NPROCESSORS_ONLN |
| 7086 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 7087 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7088 | #ifdef _SC_NPROC_CONF |
| 7089 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 7090 | #endif |
| 7091 | #ifdef _SC_NPROC_ONLN |
| 7092 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 7093 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7094 | #ifdef _SC_NZERO |
| 7095 | {"SC_NZERO", _SC_NZERO}, |
| 7096 | #endif |
| 7097 | #ifdef _SC_OPEN_MAX |
| 7098 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 7099 | #endif |
| 7100 | #ifdef _SC_PAGESIZE |
| 7101 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 7102 | #endif |
| 7103 | #ifdef _SC_PAGE_SIZE |
| 7104 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 7105 | #endif |
| 7106 | #ifdef _SC_PASS_MAX |
| 7107 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 7108 | #endif |
| 7109 | #ifdef _SC_PHYS_PAGES |
| 7110 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 7111 | #endif |
| 7112 | #ifdef _SC_PII |
| 7113 | {"SC_PII", _SC_PII}, |
| 7114 | #endif |
| 7115 | #ifdef _SC_PII_INTERNET |
| 7116 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 7117 | #endif |
| 7118 | #ifdef _SC_PII_INTERNET_DGRAM |
| 7119 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 7120 | #endif |
| 7121 | #ifdef _SC_PII_INTERNET_STREAM |
| 7122 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 7123 | #endif |
| 7124 | #ifdef _SC_PII_OSI |
| 7125 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 7126 | #endif |
| 7127 | #ifdef _SC_PII_OSI_CLTS |
| 7128 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 7129 | #endif |
| 7130 | #ifdef _SC_PII_OSI_COTS |
| 7131 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 7132 | #endif |
| 7133 | #ifdef _SC_PII_OSI_M |
| 7134 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 7135 | #endif |
| 7136 | #ifdef _SC_PII_SOCKET |
| 7137 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 7138 | #endif |
| 7139 | #ifdef _SC_PII_XTI |
| 7140 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 7141 | #endif |
| 7142 | #ifdef _SC_POLL |
| 7143 | {"SC_POLL", _SC_POLL}, |
| 7144 | #endif |
| 7145 | #ifdef _SC_PRIORITIZED_IO |
| 7146 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 7147 | #endif |
| 7148 | #ifdef _SC_PRIORITY_SCHEDULING |
| 7149 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 7150 | #endif |
| 7151 | #ifdef _SC_REALTIME_SIGNALS |
| 7152 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 7153 | #endif |
| 7154 | #ifdef _SC_RE_DUP_MAX |
| 7155 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 7156 | #endif |
| 7157 | #ifdef _SC_RTSIG_MAX |
| 7158 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 7159 | #endif |
| 7160 | #ifdef _SC_SAVED_IDS |
| 7161 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 7162 | #endif |
| 7163 | #ifdef _SC_SCHAR_MAX |
| 7164 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 7165 | #endif |
| 7166 | #ifdef _SC_SCHAR_MIN |
| 7167 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 7168 | #endif |
| 7169 | #ifdef _SC_SELECT |
| 7170 | {"SC_SELECT", _SC_SELECT}, |
| 7171 | #endif |
| 7172 | #ifdef _SC_SEMAPHORES |
| 7173 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 7174 | #endif |
| 7175 | #ifdef _SC_SEM_NSEMS_MAX |
| 7176 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 7177 | #endif |
| 7178 | #ifdef _SC_SEM_VALUE_MAX |
| 7179 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 7180 | #endif |
| 7181 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 7182 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 7183 | #endif |
| 7184 | #ifdef _SC_SHRT_MAX |
| 7185 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 7186 | #endif |
| 7187 | #ifdef _SC_SHRT_MIN |
| 7188 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 7189 | #endif |
| 7190 | #ifdef _SC_SIGQUEUE_MAX |
| 7191 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 7192 | #endif |
| 7193 | #ifdef _SC_SIGRT_MAX |
| 7194 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 7195 | #endif |
| 7196 | #ifdef _SC_SIGRT_MIN |
| 7197 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 7198 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7199 | #ifdef _SC_SOFTPOWER |
| 7200 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 7201 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7202 | #ifdef _SC_SPLIT_CACHE |
| 7203 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 7204 | #endif |
| 7205 | #ifdef _SC_SSIZE_MAX |
| 7206 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 7207 | #endif |
| 7208 | #ifdef _SC_STACK_PROT |
| 7209 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 7210 | #endif |
| 7211 | #ifdef _SC_STREAM_MAX |
| 7212 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 7213 | #endif |
| 7214 | #ifdef _SC_SYNCHRONIZED_IO |
| 7215 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 7216 | #endif |
| 7217 | #ifdef _SC_THREADS |
| 7218 | {"SC_THREADS", _SC_THREADS}, |
| 7219 | #endif |
| 7220 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 7221 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 7222 | #endif |
| 7223 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 7224 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 7225 | #endif |
| 7226 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 7227 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 7228 | #endif |
| 7229 | #ifdef _SC_THREAD_KEYS_MAX |
| 7230 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 7231 | #endif |
| 7232 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 7233 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 7234 | #endif |
| 7235 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 7236 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 7237 | #endif |
| 7238 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 7239 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 7240 | #endif |
| 7241 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 7242 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 7243 | #endif |
| 7244 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 7245 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 7246 | #endif |
| 7247 | #ifdef _SC_THREAD_STACK_MIN |
| 7248 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 7249 | #endif |
| 7250 | #ifdef _SC_THREAD_THREADS_MAX |
| 7251 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 7252 | #endif |
| 7253 | #ifdef _SC_TIMERS |
| 7254 | {"SC_TIMERS", _SC_TIMERS}, |
| 7255 | #endif |
| 7256 | #ifdef _SC_TIMER_MAX |
| 7257 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 7258 | #endif |
| 7259 | #ifdef _SC_TTY_NAME_MAX |
| 7260 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 7261 | #endif |
| 7262 | #ifdef _SC_TZNAME_MAX |
| 7263 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 7264 | #endif |
| 7265 | #ifdef _SC_T_IOV_MAX |
| 7266 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 7267 | #endif |
| 7268 | #ifdef _SC_UCHAR_MAX |
| 7269 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 7270 | #endif |
| 7271 | #ifdef _SC_UINT_MAX |
| 7272 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 7273 | #endif |
| 7274 | #ifdef _SC_UIO_MAXIOV |
| 7275 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 7276 | #endif |
| 7277 | #ifdef _SC_ULONG_MAX |
| 7278 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 7279 | #endif |
| 7280 | #ifdef _SC_USHRT_MAX |
| 7281 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 7282 | #endif |
| 7283 | #ifdef _SC_VERSION |
| 7284 | {"SC_VERSION", _SC_VERSION}, |
| 7285 | #endif |
| 7286 | #ifdef _SC_WORD_BIT |
| 7287 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 7288 | #endif |
| 7289 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 7290 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 7291 | #endif |
| 7292 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 7293 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 7294 | #endif |
| 7295 | #ifdef _SC_XBS5_LP64_OFF64 |
| 7296 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 7297 | #endif |
| 7298 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 7299 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 7300 | #endif |
| 7301 | #ifdef _SC_XOPEN_CRYPT |
| 7302 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 7303 | #endif |
| 7304 | #ifdef _SC_XOPEN_ENH_I18N |
| 7305 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 7306 | #endif |
| 7307 | #ifdef _SC_XOPEN_LEGACY |
| 7308 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 7309 | #endif |
| 7310 | #ifdef _SC_XOPEN_REALTIME |
| 7311 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 7312 | #endif |
| 7313 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 7314 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 7315 | #endif |
| 7316 | #ifdef _SC_XOPEN_SHM |
| 7317 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 7318 | #endif |
| 7319 | #ifdef _SC_XOPEN_UNIX |
| 7320 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 7321 | #endif |
| 7322 | #ifdef _SC_XOPEN_VERSION |
| 7323 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 7324 | #endif |
| 7325 | #ifdef _SC_XOPEN_XCU_VERSION |
| 7326 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 7327 | #endif |
| 7328 | #ifdef _SC_XOPEN_XPG2 |
| 7329 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 7330 | #endif |
| 7331 | #ifdef _SC_XOPEN_XPG3 |
| 7332 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 7333 | #endif |
| 7334 | #ifdef _SC_XOPEN_XPG4 |
| 7335 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 7336 | #endif |
| 7337 | }; |
| 7338 | |
| 7339 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7340 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7341 | { |
| 7342 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 7343 | sizeof(posix_constants_sysconf) |
| 7344 | / sizeof(struct constdef)); |
| 7345 | } |
| 7346 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7347 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7348 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7349 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7350 | |
| 7351 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7352 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7353 | { |
| 7354 | PyObject *result = NULL; |
| 7355 | int name; |
| 7356 | |
| 7357 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 7358 | int value; |
| 7359 | |
| 7360 | errno = 0; |
| 7361 | value = sysconf(name); |
| 7362 | if (value == -1 && errno != 0) |
| 7363 | posix_error(); |
| 7364 | else |
| 7365 | result = PyInt_FromLong(value); |
| 7366 | } |
| 7367 | return result; |
| 7368 | } |
| 7369 | #endif |
| 7370 | |
| 7371 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7372 | /* This code is used to ensure that the tables of configuration value names |
| 7373 | * are in sorted order as required by conv_confname(), and also to build the |
| 7374 | * the exported dictionaries that are used to publish information about the |
| 7375 | * names available on the host platform. |
| 7376 | * |
| 7377 | * Sorting the table at runtime ensures that the table is properly ordered |
| 7378 | * when used, even for platforms we're not able to test on. It also makes |
| 7379 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7380 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7381 | |
| 7382 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7383 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7384 | { |
| 7385 | const struct constdef *c1 = |
| 7386 | (const struct constdef *) v1; |
| 7387 | const struct constdef *c2 = |
| 7388 | (const struct constdef *) v2; |
| 7389 | |
| 7390 | return strcmp(c1->name, c2->name); |
| 7391 | } |
| 7392 | |
| 7393 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7394 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7395 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7396 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7397 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7398 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7399 | |
| 7400 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 7401 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7402 | if (d == NULL) |
| 7403 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7404 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7405 | for (i=0; i < tablesize; ++i) { |
| 7406 | PyObject *o = PyInt_FromLong(table[i].value); |
| 7407 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 7408 | Py_XDECREF(o); |
| 7409 | Py_DECREF(d); |
| 7410 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7411 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7412 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7413 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7414 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7415 | } |
| 7416 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7417 | /* Return -1 on failure, 0 on success. */ |
| 7418 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7419 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7420 | { |
| 7421 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7422 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7423 | sizeof(posix_constants_pathconf) |
| 7424 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7425 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7426 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7427 | #endif |
| 7428 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7429 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7430 | sizeof(posix_constants_confstr) |
| 7431 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7432 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7433 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7434 | #endif |
| 7435 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7436 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7437 | sizeof(posix_constants_sysconf) |
| 7438 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7439 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7440 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7441 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7442 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7443 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7444 | |
| 7445 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7446 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7447 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7448 | 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] | 7449 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7450 | |
| 7451 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7452 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7453 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7454 | abort(); |
| 7455 | /*NOTREACHED*/ |
| 7456 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 7457 | return NULL; |
| 7458 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7459 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7460 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7461 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7462 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 7463 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7464 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7465 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 7466 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 7467 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 7468 | application (if any) its extension is associated.\n\ |
| 7469 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 7470 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7471 | \n\ |
| 7472 | startfile returns as soon as the associated application is launched.\n\ |
| 7473 | There is no option to wait for the application to close, and no way\n\ |
| 7474 | to retrieve the application's exit status.\n\ |
| 7475 | \n\ |
| 7476 | The filepath is relative to the current directory. If you want to use\n\ |
| 7477 | 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] | 7478 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7479 | |
| 7480 | static PyObject * |
| 7481 | win32_startfile(PyObject *self, PyObject *args) |
| 7482 | { |
| 7483 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7484 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7485 | HINSTANCE rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 7486 | #ifdef Py_WIN_WIDE_FILENAMES |
| 7487 | if (unicode_file_names()) { |
| 7488 | PyObject *unipath, *woperation = NULL; |
| 7489 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 7490 | &unipath, &operation)) { |
| 7491 | PyErr_Clear(); |
| 7492 | goto normal; |
| 7493 | } |
| 7494 | |
| 7495 | |
| 7496 | if (operation) { |
| 7497 | woperation = PyUnicode_DecodeASCII(operation, |
| 7498 | strlen(operation), NULL); |
| 7499 | if (!woperation) { |
| 7500 | PyErr_Clear(); |
| 7501 | operation = NULL; |
| 7502 | goto normal; |
| 7503 | } |
| 7504 | } |
| 7505 | |
| 7506 | Py_BEGIN_ALLOW_THREADS |
| 7507 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 7508 | PyUnicode_AS_UNICODE(unipath), |
| 7509 | NULL, NULL, SW_SHOWNORMAL); |
| 7510 | Py_END_ALLOW_THREADS |
| 7511 | |
| 7512 | Py_XDECREF(woperation); |
| 7513 | if (rc <= (HINSTANCE)32) { |
| 7514 | PyObject *errval = win32_error_unicode("startfile", |
| 7515 | PyUnicode_AS_UNICODE(unipath)); |
| 7516 | return errval; |
| 7517 | } |
| 7518 | Py_INCREF(Py_None); |
| 7519 | return Py_None; |
| 7520 | } |
| 7521 | #endif |
| 7522 | |
| 7523 | normal: |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7524 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 7525 | Py_FileSystemDefaultEncoding, &filepath, |
| 7526 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7527 | return NULL; |
| 7528 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7529 | rc = ShellExecute((HWND)0, operation, filepath, |
| 7530 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7531 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 7532 | if (rc <= (HINSTANCE)32) { |
| 7533 | PyObject *errval = win32_error("startfile", filepath); |
| 7534 | PyMem_Free(filepath); |
| 7535 | return errval; |
| 7536 | } |
| 7537 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7538 | Py_INCREF(Py_None); |
| 7539 | return Py_None; |
| 7540 | } |
| 7541 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7542 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7543 | #ifdef HAVE_GETLOADAVG |
| 7544 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 7545 | "getloadavg() -> (float, float, float)\n\n\ |
| 7546 | Return the number of processes in the system run queue averaged over\n\ |
| 7547 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 7548 | was unobtainable"); |
| 7549 | |
| 7550 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7551 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7552 | { |
| 7553 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7554 | if (getloadavg(loadavg, 3)!=3) { |
| 7555 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 7556 | return NULL; |
| 7557 | } else |
| 7558 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 7559 | } |
| 7560 | #endif |
| 7561 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7562 | #ifdef MS_WINDOWS |
| 7563 | |
| 7564 | PyDoc_STRVAR(win32_urandom__doc__, |
| 7565 | "urandom(n) -> str\n\n\ |
| 7566 | Return a string of n random bytes suitable for cryptographic use."); |
| 7567 | |
| 7568 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 7569 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 7570 | DWORD dwFlags ); |
| 7571 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 7572 | BYTE *pbBuffer ); |
| 7573 | |
| 7574 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
| 7575 | static HCRYPTPROV hCryptProv = 0; |
| 7576 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7577 | static PyObject* |
| 7578 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7579 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7580 | int howMany; |
| 7581 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7582 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7583 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 7584 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7585 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 7586 | if (howMany < 0) |
| 7587 | return PyErr_Format(PyExc_ValueError, |
| 7588 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7589 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7590 | if (hCryptProv == 0) { |
| 7591 | HINSTANCE hAdvAPI32 = NULL; |
| 7592 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7593 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7594 | /* Obtain handle to the DLL containing CryptoAPI |
| 7595 | This should not fail */ |
| 7596 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 7597 | if(hAdvAPI32 == NULL) |
| 7598 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7599 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7600 | /* Obtain pointers to the CryptoAPI functions |
| 7601 | This will fail on some early versions of Win95 */ |
| 7602 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 7603 | hAdvAPI32, |
| 7604 | "CryptAcquireContextA"); |
| 7605 | if (pCryptAcquireContext == NULL) |
| 7606 | return PyErr_Format(PyExc_NotImplementedError, |
| 7607 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7608 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7609 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 7610 | hAdvAPI32, "CryptGenRandom"); |
| 7611 | if (pCryptAcquireContext == NULL) |
| 7612 | return PyErr_Format(PyExc_NotImplementedError, |
| 7613 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7614 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7615 | /* Acquire context */ |
| 7616 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 7617 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 7618 | return win32_error("CryptAcquireContext", NULL); |
| 7619 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7620 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7621 | /* Allocate bytes */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7622 | result = PyString_FromStringAndSize(NULL, howMany); |
| 7623 | if (result != NULL) { |
| 7624 | /* Get random data */ |
| 7625 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 7626 | PyString_AS_STRING(result))) { |
| 7627 | Py_DECREF(result); |
| 7628 | return win32_error("CryptGenRandom", NULL); |
| 7629 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7630 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7631 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7632 | } |
| 7633 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7634 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7635 | static PyMethodDef posix_methods[] = { |
| 7636 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 7637 | #ifdef HAVE_TTYNAME |
| 7638 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 7639 | #endif |
| 7640 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 7641 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7642 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7643 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7644 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7645 | #ifdef HAVE_LCHOWN |
| 7646 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 7647 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7648 | #ifdef HAVE_CHROOT |
| 7649 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 7650 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7651 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7652 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7653 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7654 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7655 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7656 | #ifdef Py_USING_UNICODE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7657 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7658 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7659 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7660 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7661 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7662 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7663 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7664 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7665 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7666 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7667 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7668 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7669 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7670 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7671 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7672 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7673 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7674 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7675 | {"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] | 7676 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7677 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7678 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7679 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7680 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7681 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7682 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7683 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7684 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7685 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7686 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7687 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7688 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7689 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7690 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7691 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7692 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7693 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7694 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7695 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7696 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7697 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7698 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7699 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7700 | #if defined(PYOS_OS2) |
| 7701 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 7702 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 7703 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7704 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7705 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7706 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7707 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7708 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7709 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7710 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7711 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7712 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7713 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7714 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7715 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7716 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7717 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7718 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7719 | #endif /* HAVE_GETEGID */ |
| 7720 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7721 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7722 | #endif /* HAVE_GETEUID */ |
| 7723 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7724 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7725 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7726 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7727 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7728 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7729 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7730 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7731 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7732 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7733 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7734 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7735 | #endif /* HAVE_GETPPID */ |
| 7736 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7737 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7738 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7739 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7740 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7741 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7742 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7743 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7744 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7745 | #ifdef HAVE_KILLPG |
| 7746 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7747 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7748 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7749 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7750 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7751 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7752 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7753 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7754 | {"popen2", win32_popen2, METH_VARARGS}, |
| 7755 | {"popen3", win32_popen3, METH_VARARGS}, |
| 7756 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7757 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7758 | #else |
| 7759 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7760 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 7761 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 7762 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 7763 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7764 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7765 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7766 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7767 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7768 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7769 | #ifdef HAVE_SETEUID |
| 7770 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7771 | #endif /* HAVE_SETEUID */ |
| 7772 | #ifdef HAVE_SETEGID |
| 7773 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7774 | #endif /* HAVE_SETEGID */ |
| 7775 | #ifdef HAVE_SETREUID |
| 7776 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7777 | #endif /* HAVE_SETREUID */ |
| 7778 | #ifdef HAVE_SETREGID |
| 7779 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7780 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7781 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7782 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7783 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7784 | #ifdef HAVE_SETGROUPS |
| 7785 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 7786 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7787 | #ifdef HAVE_GETPGID |
| 7788 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7789 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7790 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7791 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7792 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7793 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7794 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7795 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 7796 | #ifdef HAVE_WAIT3 |
| 7797 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 7798 | #endif /* HAVE_WAIT3 */ |
| 7799 | #ifdef HAVE_WAIT4 |
| 7800 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 7801 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7802 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7803 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7804 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7805 | #ifdef HAVE_GETSID |
| 7806 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 7807 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7808 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7809 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7810 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7811 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7812 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7813 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7814 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7815 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7816 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7817 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7818 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7819 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7820 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7821 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 7822 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7823 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7824 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7825 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7826 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7827 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 7828 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7829 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7830 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7831 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7832 | #endif |
| 7833 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7834 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7835 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7836 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7837 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7838 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7839 | #ifdef HAVE_DEVICE_MACROS |
| 7840 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7841 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7842 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7843 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7844 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7845 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7846 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7847 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7848 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7849 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7850 | #ifdef HAVE_UNSETENV |
| 7851 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7852 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7853 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7854 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7855 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7856 | #ifdef HAVE_FCHDIR |
| 7857 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7858 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7859 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7860 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7861 | #endif |
| 7862 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7863 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7864 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7865 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7866 | #ifdef WCOREDUMP |
| 7867 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7868 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7869 | #ifdef WIFCONTINUED |
| 7870 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7871 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7872 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7873 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7874 | #endif /* WIFSTOPPED */ |
| 7875 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7876 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7877 | #endif /* WIFSIGNALED */ |
| 7878 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7879 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7880 | #endif /* WIFEXITED */ |
| 7881 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7882 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7883 | #endif /* WEXITSTATUS */ |
| 7884 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7885 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7886 | #endif /* WTERMSIG */ |
| 7887 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7888 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7889 | #endif /* WSTOPSIG */ |
| 7890 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7891 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7892 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7893 | #endif |
| 7894 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7895 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7896 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 7897 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7898 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7899 | #endif |
| 7900 | #ifdef HAVE_TEMPNAM |
| 7901 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 7902 | #endif |
| 7903 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7904 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7905 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7906 | #ifdef HAVE_CONFSTR |
| 7907 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7908 | #endif |
| 7909 | #ifdef HAVE_SYSCONF |
| 7910 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7911 | #endif |
| 7912 | #ifdef HAVE_FPATHCONF |
| 7913 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7914 | #endif |
| 7915 | #ifdef HAVE_PATHCONF |
| 7916 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7917 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7918 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7919 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7920 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7921 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7922 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7923 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7924 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7925 | #ifdef MS_WINDOWS |
| 7926 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 7927 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7928 | {NULL, NULL} /* Sentinel */ |
| 7929 | }; |
| 7930 | |
| 7931 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7932 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7933 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7934 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7935 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7936 | } |
| 7937 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7938 | #if defined(PYOS_OS2) |
| 7939 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7940 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7941 | { |
| 7942 | APIRET rc; |
| 7943 | ULONG values[QSV_MAX+1]; |
| 7944 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7945 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7946 | |
| 7947 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7948 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7949 | Py_END_ALLOW_THREADS |
| 7950 | |
| 7951 | if (rc != NO_ERROR) { |
| 7952 | os2_error(rc); |
| 7953 | return -1; |
| 7954 | } |
| 7955 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7956 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7957 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7958 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7959 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7960 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7961 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7962 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7963 | |
| 7964 | switch (values[QSV_VERSION_MINOR]) { |
| 7965 | case 0: ver = "2.00"; break; |
| 7966 | case 10: ver = "2.10"; break; |
| 7967 | case 11: ver = "2.11"; break; |
| 7968 | case 30: ver = "3.00"; break; |
| 7969 | case 40: ver = "4.00"; break; |
| 7970 | case 50: ver = "5.00"; break; |
| 7971 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7972 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7973 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7974 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7975 | ver = &tmp[0]; |
| 7976 | } |
| 7977 | |
| 7978 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7979 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7980 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7981 | |
| 7982 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7983 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7984 | tmp[1] = ':'; |
| 7985 | tmp[2] = '\0'; |
| 7986 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7987 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7988 | } |
| 7989 | #endif |
| 7990 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7991 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7992 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7993 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7994 | #ifdef F_OK |
| 7995 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7996 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7997 | #ifdef R_OK |
| 7998 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7999 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8000 | #ifdef W_OK |
| 8001 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8002 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8003 | #ifdef X_OK |
| 8004 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8005 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8006 | #ifdef NGROUPS_MAX |
| 8007 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 8008 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8009 | #ifdef TMP_MAX |
| 8010 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 8011 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8012 | #ifdef WCONTINUED |
| 8013 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 8014 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8015 | #ifdef WNOHANG |
| 8016 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8017 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8018 | #ifdef WUNTRACED |
| 8019 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 8020 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8021 | #ifdef O_RDONLY |
| 8022 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 8023 | #endif |
| 8024 | #ifdef O_WRONLY |
| 8025 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 8026 | #endif |
| 8027 | #ifdef O_RDWR |
| 8028 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 8029 | #endif |
| 8030 | #ifdef O_NDELAY |
| 8031 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 8032 | #endif |
| 8033 | #ifdef O_NONBLOCK |
| 8034 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 8035 | #endif |
| 8036 | #ifdef O_APPEND |
| 8037 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 8038 | #endif |
| 8039 | #ifdef O_DSYNC |
| 8040 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 8041 | #endif |
| 8042 | #ifdef O_RSYNC |
| 8043 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 8044 | #endif |
| 8045 | #ifdef O_SYNC |
| 8046 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 8047 | #endif |
| 8048 | #ifdef O_NOCTTY |
| 8049 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 8050 | #endif |
| 8051 | #ifdef O_CREAT |
| 8052 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 8053 | #endif |
| 8054 | #ifdef O_EXCL |
| 8055 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 8056 | #endif |
| 8057 | #ifdef O_TRUNC |
| 8058 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 8059 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 8060 | #ifdef O_BINARY |
| 8061 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 8062 | #endif |
| 8063 | #ifdef O_TEXT |
| 8064 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 8065 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8066 | #ifdef O_LARGEFILE |
| 8067 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 8068 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 8069 | #ifdef O_SHLOCK |
| 8070 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 8071 | #endif |
| 8072 | #ifdef O_EXLOCK |
| 8073 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 8074 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8075 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8076 | /* MS Windows */ |
| 8077 | #ifdef O_NOINHERIT |
| 8078 | /* Don't inherit in child processes. */ |
| 8079 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 8080 | #endif |
| 8081 | #ifdef _O_SHORT_LIVED |
| 8082 | /* Optimize for short life (keep in memory). */ |
| 8083 | /* MS forgot to define this one with a non-underscore form too. */ |
| 8084 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 8085 | #endif |
| 8086 | #ifdef O_TEMPORARY |
| 8087 | /* Automatically delete when last handle is closed. */ |
| 8088 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 8089 | #endif |
| 8090 | #ifdef O_RANDOM |
| 8091 | /* Optimize for random access. */ |
| 8092 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 8093 | #endif |
| 8094 | #ifdef O_SEQUENTIAL |
| 8095 | /* Optimize for sequential access. */ |
| 8096 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 8097 | #endif |
| 8098 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8099 | /* GNU extensions. */ |
| 8100 | #ifdef O_DIRECT |
| 8101 | /* Direct disk access. */ |
| 8102 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 8103 | #endif |
| 8104 | #ifdef O_DIRECTORY |
| 8105 | /* Must be a directory. */ |
| 8106 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 8107 | #endif |
| 8108 | #ifdef O_NOFOLLOW |
| 8109 | /* Do not follow links. */ |
| 8110 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 8111 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8112 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8113 | /* These come from sysexits.h */ |
| 8114 | #ifdef EX_OK |
| 8115 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8116 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8117 | #ifdef EX_USAGE |
| 8118 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8119 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8120 | #ifdef EX_DATAERR |
| 8121 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8122 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8123 | #ifdef EX_NOINPUT |
| 8124 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8125 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8126 | #ifdef EX_NOUSER |
| 8127 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8128 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8129 | #ifdef EX_NOHOST |
| 8130 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8131 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8132 | #ifdef EX_UNAVAILABLE |
| 8133 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8134 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8135 | #ifdef EX_SOFTWARE |
| 8136 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8137 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8138 | #ifdef EX_OSERR |
| 8139 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8140 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8141 | #ifdef EX_OSFILE |
| 8142 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8143 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8144 | #ifdef EX_CANTCREAT |
| 8145 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8146 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8147 | #ifdef EX_IOERR |
| 8148 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8149 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8150 | #ifdef EX_TEMPFAIL |
| 8151 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8152 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8153 | #ifdef EX_PROTOCOL |
| 8154 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8155 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8156 | #ifdef EX_NOPERM |
| 8157 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8158 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8159 | #ifdef EX_CONFIG |
| 8160 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8161 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8162 | #ifdef EX_NOTFOUND |
| 8163 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8164 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8165 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8166 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8167 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 8168 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 8169 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 8170 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 8171 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 8172 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 8173 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 8174 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 8175 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 8176 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 8177 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 8178 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 8179 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 8180 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 8181 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 8182 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 8183 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 8184 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 8185 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 8186 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 8187 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 8188 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 8189 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 8190 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 8191 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 8192 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 8193 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8194 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8195 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8196 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8197 | #if defined(PYOS_OS2) |
| 8198 | if (insertvalues(d)) return -1; |
| 8199 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8200 | return 0; |
| 8201 | } |
| 8202 | |
| 8203 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8204 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8205 | #define INITFUNC initnt |
| 8206 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8207 | |
| 8208 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8209 | #define INITFUNC initos2 |
| 8210 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8211 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8212 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8213 | #define INITFUNC initposix |
| 8214 | #define MODNAME "posix" |
| 8215 | #endif |
| 8216 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 8217 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 8218 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8219 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8220 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8221 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8222 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8223 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8224 | posix__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 8225 | if (m == NULL) |
| 8226 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8227 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8228 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8229 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8230 | Py_XINCREF(v); |
| 8231 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8232 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8233 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8234 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8235 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8236 | return; |
| 8237 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8238 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8239 | return; |
| 8240 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8241 | Py_INCREF(PyExc_OSError); |
| 8242 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8243 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8244 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 8245 | if (posix_putenv_garbage == NULL) |
| 8246 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8247 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8248 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 8249 | if (!initialized) { |
| 8250 | stat_result_desc.name = MODNAME ".stat_result"; |
| 8251 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 8252 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 8253 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 8254 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 8255 | structseq_new = StatResultType.tp_new; |
| 8256 | StatResultType.tp_new = statresult_new; |
| 8257 | |
| 8258 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 8259 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
| 8260 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8261 | Py_INCREF((PyObject*) &StatResultType); |
| 8262 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8263 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 8264 | PyModule_AddObject(m, "statvfs_result", |
| 8265 | (PyObject*) &StatVFSResultType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 8266 | initialized = 1; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8267 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 8268 | |
| 8269 | #ifdef __cplusplus |
| 8270 | } |
| 8271 | #endif |
| 8272 | |