Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 14 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 15 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 16 | #include "Python.h" |
| 17 | #include "structseq.h" |
| 18 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 19 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 20 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 21 | #endif /* defined(__VMS) */ |
| 22 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 23 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 24 | "This module provides access to operating system functionality that is\n\ |
| 25 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 26 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 27 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 29 | #ifndef Py_USING_UNICODE |
| 30 | /* This is used in signatures of functions. */ |
| 31 | #define Py_UNICODE void |
| 32 | #endif |
| 33 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 34 | #if defined(PYOS_OS2) |
| 35 | #define INCL_DOS |
| 36 | #define INCL_DOSERRORS |
| 37 | #define INCL_DOSPROCESS |
| 38 | #define INCL_NOPMAPI |
| 39 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 40 | #if defined(PYCC_GCC) |
| 41 | #include <ctype.h> |
| 42 | #include <io.h> |
| 43 | #include <stdio.h> |
| 44 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 45 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 46 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 49 | #include <sys/types.h> |
| 50 | #include <sys/stat.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 52 | #ifdef HAVE_SYS_WAIT_H |
| 53 | #include <sys/wait.h> /* For WNOHANG */ |
| 54 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 56 | #include <signal.h> |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 58 | #ifdef HAVE_FCNTL_H |
| 59 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 60 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 62 | #ifdef HAVE_GRP_H |
| 63 | #include <grp.h> |
| 64 | #endif |
| 65 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 66 | #ifdef HAVE_SYSEXITS_H |
| 67 | #include <sysexits.h> |
| 68 | #endif /* HAVE_SYSEXITS_H */ |
| 69 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 70 | #ifdef HAVE_SYS_LOADAVG_H |
| 71 | #include <sys/loadavg.h> |
| 72 | #endif |
| 73 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 74 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 75 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 76 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 77 | #include <process.h> |
| 78 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 79 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 80 | #define HAVE_GETCWD 1 |
| 81 | #define HAVE_OPENDIR 1 |
| 82 | #define HAVE_SYSTEM 1 |
| 83 | #if defined(__OS2__) |
| 84 | #define HAVE_EXECV 1 |
| 85 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 86 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 87 | #include <process.h> |
| 88 | #else |
| 89 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 90 | #define HAVE_EXECV 1 |
| 91 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 92 | #define HAVE_OPENDIR 1 |
| 93 | #define HAVE_PIPE 1 |
| 94 | #define HAVE_POPEN 1 |
| 95 | #define HAVE_SYSTEM 1 |
| 96 | #define HAVE_WAIT 1 |
| 97 | #else |
| 98 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 99 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 100 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 101 | #define HAVE_EXECV 1 |
| 102 | #define HAVE_PIPE 1 |
| 103 | #define HAVE_POPEN 1 |
| 104 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 105 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 106 | #define HAVE_FSYNC 1 |
| 107 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 108 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 109 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 110 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 111 | #else /* all other compilers */ |
| 112 | /* Unix functions that the configure script doesn't check for */ |
| 113 | #define HAVE_EXECV 1 |
| 114 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 115 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 116 | #define HAVE_FORK1 1 |
| 117 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 118 | #define HAVE_GETCWD 1 |
| 119 | #define HAVE_GETEGID 1 |
| 120 | #define HAVE_GETEUID 1 |
| 121 | #define HAVE_GETGID 1 |
| 122 | #define HAVE_GETPPID 1 |
| 123 | #define HAVE_GETUID 1 |
| 124 | #define HAVE_KILL 1 |
| 125 | #define HAVE_OPENDIR 1 |
| 126 | #define HAVE_PIPE 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 127 | #ifndef __rtems__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 128 | #define HAVE_POPEN 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 129 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 130 | #define HAVE_SYSTEM 1 |
| 131 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 132 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 133 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #endif /* _MSC_VER */ |
| 135 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 136 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 137 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 139 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 140 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 141 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 142 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 143 | (default) */ |
| 144 | extern char *ctermid_r(char *); |
| 145 | #endif |
| 146 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 147 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 148 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 149 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 150 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 151 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 152 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 153 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 154 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 155 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 156 | #endif |
| 157 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 158 | extern int chdir(char *); |
| 159 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 160 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 161 | extern int chdir(const char *); |
| 162 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 163 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 164 | #ifdef __BORLANDC__ |
| 165 | extern int chmod(const char *, int); |
| 166 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 167 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 168 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int chown(const char *, uid_t, gid_t); |
| 170 | extern char *getcwd(char *, int); |
| 171 | extern char *strerror(int); |
| 172 | extern int link(const char *, const char *); |
| 173 | extern int rename(const char *, const char *); |
| 174 | extern int stat(const char *, struct stat *); |
| 175 | extern int unlink(const char *); |
| 176 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 177 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 178 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 179 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 180 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 181 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 182 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 183 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 185 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 187 | #ifdef HAVE_UTIME_H |
| 188 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 189 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 191 | #ifdef HAVE_SYS_UTIME_H |
| 192 | #include <sys/utime.h> |
| 193 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 194 | #endif /* HAVE_SYS_UTIME_H */ |
| 195 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 196 | #ifdef HAVE_SYS_TIMES_H |
| 197 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 198 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 199 | |
| 200 | #ifdef HAVE_SYS_PARAM_H |
| 201 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 202 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 203 | |
| 204 | #ifdef HAVE_SYS_UTSNAME_H |
| 205 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 206 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 207 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 208 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 210 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 211 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 212 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 213 | #include <direct.h> |
| 214 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 215 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 216 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 217 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 218 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 219 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 220 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 221 | #endif |
| 222 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 223 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 224 | #endif |
| 225 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 227 | #endif |
| 228 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 230 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 231 | #include <direct.h> |
| 232 | #include <io.h> |
| 233 | #include <process.h> |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 234 | #include "osdefs.h" |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 235 | #define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 236 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 237 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 238 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 239 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 240 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 242 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 244 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 245 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 246 | #ifndef MAXPATHLEN |
| 247 | #define MAXPATHLEN 1024 |
| 248 | #endif /* MAXPATHLEN */ |
| 249 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 250 | #ifdef UNION_WAIT |
| 251 | /* Emulate some macros on systems that have a union instead of macros */ |
| 252 | |
| 253 | #ifndef WIFEXITED |
| 254 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 255 | #endif |
| 256 | |
| 257 | #ifndef WEXITSTATUS |
| 258 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 259 | #endif |
| 260 | |
| 261 | #ifndef WTERMSIG |
| 262 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 263 | #endif |
| 264 | |
| 265 | #endif /* UNION_WAIT */ |
| 266 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 267 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 268 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 269 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 270 | #define USE_CTERMID_R |
| 271 | #endif |
| 272 | |
| 273 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 274 | #define USE_TMPNAM_R |
| 275 | #endif |
| 276 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 277 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 278 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 279 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 280 | # define STAT win32_stat |
| 281 | # define FSTAT win32_fstat |
| 282 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 283 | #else |
| 284 | # define STAT stat |
| 285 | # define FSTAT fstat |
| 286 | # define STRUCT_STAT struct stat |
| 287 | #endif |
| 288 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 289 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 290 | #include <sys/mkdev.h> |
| 291 | #else |
| 292 | #if defined(MAJOR_IN_SYSMACROS) |
| 293 | #include <sys/sysmacros.h> |
| 294 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 295 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 296 | #include <sys/mkdev.h> |
| 297 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 298 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 299 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 300 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 301 | #ifdef WITH_NEXT_FRAMEWORK |
| 302 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 303 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 304 | */ |
| 305 | #include <crt_externs.h> |
| 306 | static char **environ; |
| 307 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 308 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 309 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 310 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 311 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 312 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 313 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 314 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 316 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 317 | if (d == NULL) |
| 318 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 319 | #ifdef WITH_NEXT_FRAMEWORK |
| 320 | if (environ == NULL) |
| 321 | environ = *_NSGetEnviron(); |
| 322 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 323 | if (environ == NULL) |
| 324 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 325 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 326 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 327 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 328 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 329 | char *p = strchr(*e, '='); |
| 330 | if (p == NULL) |
| 331 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 332 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 333 | if (k == NULL) { |
| 334 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 335 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 336 | } |
| 337 | v = PyString_FromString(p+1); |
| 338 | if (v == NULL) { |
| 339 | PyErr_Clear(); |
| 340 | Py_DECREF(k); |
| 341 | continue; |
| 342 | } |
| 343 | if (PyDict_GetItem(d, k) == NULL) { |
| 344 | if (PyDict_SetItem(d, k, v) != 0) |
| 345 | PyErr_Clear(); |
| 346 | } |
| 347 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 348 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 349 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 350 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 351 | { |
| 352 | APIRET rc; |
| 353 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 354 | |
| 355 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 356 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 357 | PyObject *v = PyString_FromString(buffer); |
| 358 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 359 | Py_DECREF(v); |
| 360 | } |
| 361 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 362 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 363 | PyObject *v = PyString_FromString(buffer); |
| 364 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 365 | Py_DECREF(v); |
| 366 | } |
| 367 | } |
| 368 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 369 | return d; |
| 370 | } |
| 371 | |
| 372 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 373 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 374 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 375 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 376 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 377 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 378 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 379 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 380 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 381 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 382 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 383 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 386 | #ifdef Py_WIN_WIDE_FILENAMES |
| 387 | static PyObject * |
| 388 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 389 | { |
| 390 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 391 | } |
| 392 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 393 | |
| 394 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 395 | static PyObject * |
| 396 | posix_error_with_allocated_filename(char* name) |
| 397 | { |
| 398 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 399 | PyMem_Free(name); |
| 400 | return rc; |
| 401 | } |
| 402 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 403 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 404 | static PyObject * |
| 405 | win32_error(char* function, char* filename) |
| 406 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 407 | /* XXX We should pass the function name along in the future. |
| 408 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 409 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 410 | Windows error object, which is non-trivial. |
| 411 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 412 | errno = GetLastError(); |
| 413 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 414 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 415 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 416 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 417 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 418 | |
| 419 | #ifdef Py_WIN_WIDE_FILENAMES |
| 420 | static PyObject * |
| 421 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 422 | { |
| 423 | /* XXX - see win32_error for comments on 'function' */ |
| 424 | errno = GetLastError(); |
| 425 | if (filename) |
| 426 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 427 | else |
| 428 | return PyErr_SetFromWindowsErr(errno); |
| 429 | } |
| 430 | |
| 431 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 432 | { |
| 433 | /* XXX Perhaps we should make this API an alias of |
| 434 | PyObject_Unicode() instead ?! */ |
| 435 | if (PyUnicode_CheckExact(obj)) { |
| 436 | Py_INCREF(obj); |
| 437 | return obj; |
| 438 | } |
| 439 | if (PyUnicode_Check(obj)) { |
| 440 | /* For a Unicode subtype that's not a Unicode object, |
| 441 | return a true Unicode object with the same data. */ |
| 442 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 443 | PyUnicode_GET_SIZE(obj)); |
| 444 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 445 | return PyUnicode_FromEncodedObject(obj, |
| 446 | Py_FileSystemDefaultEncoding, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 447 | "strict"); |
| 448 | } |
| 449 | |
| 450 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 451 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 452 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 453 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 454 | #if defined(PYOS_OS2) |
| 455 | /********************************************************************** |
| 456 | * Helper Function to Trim and Format OS/2 Messages |
| 457 | **********************************************************************/ |
| 458 | static void |
| 459 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 460 | { |
| 461 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 462 | |
| 463 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 464 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 465 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 466 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 467 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 468 | } |
| 469 | |
| 470 | /* Add Optional Reason Text */ |
| 471 | if (reason) { |
| 472 | strcat(msgbuf, " : "); |
| 473 | strcat(msgbuf, reason); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /********************************************************************** |
| 478 | * Decode an OS/2 Operating System Error Code |
| 479 | * |
| 480 | * A convenience function to lookup an OS/2 error code and return a |
| 481 | * text message we can use to raise a Python exception. |
| 482 | * |
| 483 | * Notes: |
| 484 | * The messages for errors returned from the OS/2 kernel reside in |
| 485 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 486 | * |
| 487 | **********************************************************************/ |
| 488 | static char * |
| 489 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 490 | { |
| 491 | APIRET rc; |
| 492 | ULONG msglen; |
| 493 | |
| 494 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 495 | Py_BEGIN_ALLOW_THREADS |
| 496 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 497 | errorcode, "oso001.msg", &msglen); |
| 498 | Py_END_ALLOW_THREADS |
| 499 | |
| 500 | if (rc == NO_ERROR) |
| 501 | os2_formatmsg(msgbuf, msglen, reason); |
| 502 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 503 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 504 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 505 | |
| 506 | return msgbuf; |
| 507 | } |
| 508 | |
| 509 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 510 | errors are not in a global variable e.g. 'errno' nor are |
| 511 | they congruent with posix error numbers. */ |
| 512 | |
| 513 | static PyObject * os2_error(int code) |
| 514 | { |
| 515 | char text[1024]; |
| 516 | PyObject *v; |
| 517 | |
| 518 | os2_strerror(text, sizeof(text), code, ""); |
| 519 | |
| 520 | v = Py_BuildValue("(is)", code, text); |
| 521 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 522 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 523 | Py_DECREF(v); |
| 524 | } |
| 525 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 526 | } |
| 527 | |
| 528 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 529 | |
| 530 | /* POSIX generic methods */ |
| 531 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 532 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 533 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 534 | { |
| 535 | int fd; |
| 536 | int res; |
| 537 | fd = PyObject_AsFileDescriptor(fdobj); |
| 538 | if (fd < 0) |
| 539 | return NULL; |
| 540 | Py_BEGIN_ALLOW_THREADS |
| 541 | res = (*func)(fd); |
| 542 | Py_END_ALLOW_THREADS |
| 543 | if (res < 0) |
| 544 | return posix_error(); |
| 545 | Py_INCREF(Py_None); |
| 546 | return Py_None; |
| 547 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 548 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 549 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 550 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 551 | unicode_file_names(void) |
| 552 | { |
| 553 | static int canusewide = -1; |
| 554 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 555 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 556 | the Windows NT family. */ |
| 557 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 558 | } |
| 559 | return canusewide; |
| 560 | } |
| 561 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 562 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 563 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 564 | posix_1str(PyObject *args, char *format, int (*func)(const char*), |
| 565 | char *wformat, int (*wfunc)(const Py_UNICODE*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 566 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 567 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 568 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 569 | #ifdef Py_WIN_WIDE_FILENAMES |
| 570 | if (unicode_file_names()) { |
| 571 | PyUnicodeObject *po; |
| 572 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 573 | Py_BEGIN_ALLOW_THREADS |
| 574 | /* PyUnicode_AS_UNICODE OK without thread |
| 575 | lock as it is a simple dereference. */ |
| 576 | res = (*wfunc)(PyUnicode_AS_UNICODE(po)); |
| 577 | Py_END_ALLOW_THREADS |
| 578 | if (res < 0) |
Mark Hammond | d389036 | 2002-10-03 07:24:48 +0000 | [diff] [blame] | 579 | return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 580 | Py_INCREF(Py_None); |
| 581 | return Py_None; |
| 582 | } |
| 583 | /* Drop the argument parsing error as narrow |
| 584 | strings are also valid. */ |
| 585 | PyErr_Clear(); |
| 586 | } |
| 587 | #else |
| 588 | /* Platforms that don't support Unicode filenames |
| 589 | shouldn't be passing these extra params */ |
| 590 | assert(wformat==NULL && wfunc == NULL); |
| 591 | #endif |
| 592 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 593 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 594 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 595 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 596 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 597 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 598 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 599 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 600 | return posix_error_with_allocated_filename(path1); |
| 601 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 602 | Py_INCREF(Py_None); |
| 603 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 606 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 607 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 608 | char *format, |
| 609 | int (*func)(const char *, const char *), |
| 610 | char *wformat, |
| 611 | int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 612 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 613 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 614 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 615 | #ifdef Py_WIN_WIDE_FILENAMES |
| 616 | if (unicode_file_names()) { |
| 617 | PyObject *po1; |
| 618 | PyObject *po2; |
| 619 | if (PyArg_ParseTuple(args, wformat, &po1, &po2)) { |
| 620 | if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) { |
| 621 | PyObject *wpath1; |
| 622 | PyObject *wpath2; |
| 623 | wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1); |
| 624 | wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2); |
| 625 | if (!wpath1 || !wpath2) { |
| 626 | Py_XDECREF(wpath1); |
| 627 | Py_XDECREF(wpath2); |
| 628 | return NULL; |
| 629 | } |
| 630 | Py_BEGIN_ALLOW_THREADS |
| 631 | /* PyUnicode_AS_UNICODE OK without thread |
| 632 | lock as it is a simple dereference. */ |
| 633 | res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1), |
| 634 | PyUnicode_AS_UNICODE(wpath2)); |
| 635 | Py_END_ALLOW_THREADS |
| 636 | Py_XDECREF(wpath1); |
| 637 | Py_XDECREF(wpath2); |
| 638 | if (res != 0) |
| 639 | return posix_error(); |
| 640 | Py_INCREF(Py_None); |
| 641 | return Py_None; |
| 642 | } |
| 643 | /* Else flow through as neither is Unicode. */ |
| 644 | } |
| 645 | /* Drop the argument parsing error as narrow |
| 646 | strings are also valid. */ |
| 647 | PyErr_Clear(); |
| 648 | } |
| 649 | #else |
| 650 | /* Platforms that don't support Unicode filenames |
| 651 | shouldn't be passing these extra params */ |
| 652 | assert(wformat==NULL && wfunc == NULL); |
| 653 | #endif |
| 654 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 655 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 656 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 657 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 658 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 659 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 660 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 661 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 662 | PyMem_Free(path1); |
| 663 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 664 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 665 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 666 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 667 | Py_INCREF(Py_None); |
| 668 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 671 | #ifdef MS_WINDOWS |
| 672 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 673 | - time stamps are restricted to second resolution |
| 674 | - file modification times suffer from forth-and-back conversions between |
| 675 | UTC and local time |
| 676 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 677 | */ |
| 678 | #define HAVE_STAT_NSEC 1 |
| 679 | |
| 680 | struct win32_stat{ |
| 681 | int st_dev; |
| 682 | __int64 st_ino; |
| 683 | unsigned short st_mode; |
| 684 | int st_nlink; |
| 685 | int st_uid; |
| 686 | int st_gid; |
| 687 | int st_rdev; |
| 688 | __int64 st_size; |
| 689 | int st_atime; |
| 690 | int st_atime_nsec; |
| 691 | int st_mtime; |
| 692 | int st_mtime_nsec; |
| 693 | int st_ctime; |
| 694 | int st_ctime_nsec; |
| 695 | }; |
| 696 | |
| 697 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 698 | |
| 699 | static void |
| 700 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 701 | { |
| 702 | /* XXX endianness */ |
| 703 | __int64 in = *(__int64*)in_ptr; |
| 704 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 705 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 706 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 707 | } |
| 708 | |
| 709 | /* Below, we *know* that ugo+r is 0444 */ |
| 710 | #if _S_IREAD != 0400 |
| 711 | #error Unsupported C library |
| 712 | #endif |
| 713 | static int |
| 714 | attributes_to_mode(DWORD attr) |
| 715 | { |
| 716 | int m = 0; |
| 717 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 718 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 719 | else |
| 720 | m |= _S_IFREG; |
| 721 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 722 | m |= 0444; |
| 723 | else |
| 724 | m |= 0666; |
| 725 | return m; |
| 726 | } |
| 727 | |
| 728 | static int |
| 729 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 730 | { |
| 731 | memset(result, 0, sizeof(*result)); |
| 732 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 733 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 734 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 735 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 736 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | static int |
| 742 | win32_stat(const char* path, struct win32_stat *result) |
| 743 | { |
| 744 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 745 | int code; |
| 746 | char *dot; |
| 747 | /* XXX not supported on Win95 and NT 3.x */ |
| 748 | if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
| 749 | /* Protocol violation: we explicitly clear errno, instead of |
| 750 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 751 | errno = 0; |
| 752 | return -1; |
| 753 | } |
| 754 | code = attribute_data_to_stat(&info, result); |
| 755 | if (code != 0) |
| 756 | return code; |
| 757 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 758 | dot = strrchr(path, '.'); |
| 759 | if (dot) { |
| 760 | if (stricmp(dot, ".bat") == 0 || |
| 761 | stricmp(dot, ".cmd") == 0 || |
| 762 | stricmp(dot, ".exe") == 0 || |
| 763 | stricmp(dot, ".com") == 0) |
| 764 | result->st_mode |= 0111; |
| 765 | } |
| 766 | return code; |
| 767 | } |
| 768 | |
| 769 | static int |
| 770 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 771 | { |
| 772 | int code; |
| 773 | const wchar_t *dot; |
| 774 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 775 | /* XXX not supported on Win95 and NT 3.x */ |
| 776 | if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
| 777 | /* Protocol violation: we explicitly clear errno, instead of |
| 778 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 779 | errno = 0; |
| 780 | return -1; |
| 781 | } |
| 782 | code = attribute_data_to_stat(&info, result); |
| 783 | if (code < 0) |
| 784 | return code; |
| 785 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 786 | dot = wcsrchr(path, '.'); |
| 787 | if (dot) { |
| 788 | if (_wcsicmp(dot, L".bat") == 0 || |
| 789 | _wcsicmp(dot, L".cmd") == 0 || |
| 790 | _wcsicmp(dot, L".exe") == 0 || |
| 791 | _wcsicmp(dot, L".com") == 0) |
| 792 | result->st_mode |= 0111; |
| 793 | } |
| 794 | return code; |
| 795 | } |
| 796 | |
| 797 | static int |
| 798 | win32_fstat(int file_number, struct win32_stat *result) |
| 799 | { |
| 800 | BY_HANDLE_FILE_INFORMATION info; |
| 801 | HANDLE h; |
| 802 | int type; |
| 803 | |
| 804 | h = (HANDLE)_get_osfhandle(file_number); |
| 805 | |
| 806 | /* Protocol violation: we explicitly clear errno, instead of |
| 807 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 808 | errno = 0; |
| 809 | |
| 810 | if (h == INVALID_HANDLE_VALUE) { |
| 811 | /* This is really a C library error (invalid file handle). |
| 812 | We set the Win32 error to the closes one matching. */ |
| 813 | SetLastError(ERROR_INVALID_HANDLE); |
| 814 | return -1; |
| 815 | } |
| 816 | memset(result, 0, sizeof(*result)); |
| 817 | |
| 818 | type = GetFileType(h); |
| 819 | if (type == FILE_TYPE_UNKNOWN) { |
| 820 | DWORD error = GetLastError(); |
| 821 | if (error != 0) { |
| 822 | return -1; |
| 823 | } |
| 824 | /* else: valid but unknown file */ |
| 825 | } |
| 826 | |
| 827 | if (type != FILE_TYPE_DISK) { |
| 828 | if (type == FILE_TYPE_CHAR) |
| 829 | result->st_mode = _S_IFCHR; |
| 830 | else if (type == FILE_TYPE_PIPE) |
| 831 | result->st_mode = _S_IFIFO; |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | if (!GetFileInformationByHandle(h, &info)) { |
| 836 | return -1; |
| 837 | } |
| 838 | |
| 839 | /* similar to stat() */ |
| 840 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 841 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 842 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 843 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 844 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 845 | /* specific to fstat() */ |
| 846 | result->st_nlink = info.nNumberOfLinks; |
| 847 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | #endif /* MS_WINDOWS */ |
| 852 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 853 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 854 | "stat_result: Result from stat or lstat.\n\n\ |
| 855 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 856 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 857 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 858 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 859 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 860 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 861 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 862 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 863 | |
| 864 | static PyStructSequence_Field stat_result_fields[] = { |
| 865 | {"st_mode", "protection bits"}, |
| 866 | {"st_ino", "inode"}, |
| 867 | {"st_dev", "device"}, |
| 868 | {"st_nlink", "number of hard links"}, |
| 869 | {"st_uid", "user ID of owner"}, |
| 870 | {"st_gid", "group ID of owner"}, |
| 871 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 872 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 873 | {NULL, "integer time of last access"}, |
| 874 | {NULL, "integer time of last modification"}, |
| 875 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 876 | {"st_atime", "time of last access"}, |
| 877 | {"st_mtime", "time of last modification"}, |
| 878 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 879 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 880 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 881 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 882 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 883 | {"st_blocks", "number of blocks allocated"}, |
| 884 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 885 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 886 | {"st_rdev", "device type (if inode device)"}, |
| 887 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 888 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 889 | {"st_flags", "user defined flags for file"}, |
| 890 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 891 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 892 | {"st_gen", "generation number"}, |
| 893 | #endif |
| 894 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 895 | {"st_birthtime", "time of creation"}, |
| 896 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 897 | {0} |
| 898 | }; |
| 899 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 900 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 901 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 902 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 903 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 904 | #endif |
| 905 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 906 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 907 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 908 | #else |
| 909 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 910 | #endif |
| 911 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 912 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 913 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 914 | #else |
| 915 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 916 | #endif |
| 917 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 918 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 919 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 920 | #else |
| 921 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 922 | #endif |
| 923 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 924 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 925 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 926 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 927 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 928 | #endif |
| 929 | |
| 930 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 931 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 932 | #else |
| 933 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 934 | #endif |
| 935 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 936 | static PyStructSequence_Desc stat_result_desc = { |
| 937 | "stat_result", /* name */ |
| 938 | stat_result__doc__, /* doc */ |
| 939 | stat_result_fields, |
| 940 | 10 |
| 941 | }; |
| 942 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 943 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 944 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 945 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 946 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 947 | 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] | 948 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 949 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 950 | |
| 951 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 952 | {"f_bsize", }, |
| 953 | {"f_frsize", }, |
| 954 | {"f_blocks", }, |
| 955 | {"f_bfree", }, |
| 956 | {"f_bavail", }, |
| 957 | {"f_files", }, |
| 958 | {"f_ffree", }, |
| 959 | {"f_favail", }, |
| 960 | {"f_flag", }, |
| 961 | {"f_namemax",}, |
| 962 | {0} |
| 963 | }; |
| 964 | |
| 965 | static PyStructSequence_Desc statvfs_result_desc = { |
| 966 | "statvfs_result", /* name */ |
| 967 | statvfs_result__doc__, /* doc */ |
| 968 | statvfs_result_fields, |
| 969 | 10 |
| 970 | }; |
| 971 | |
| 972 | static PyTypeObject StatResultType; |
| 973 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 974 | static newfunc structseq_new; |
| 975 | |
| 976 | static PyObject * |
| 977 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 978 | { |
| 979 | PyStructSequence *result; |
| 980 | int i; |
| 981 | |
| 982 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 983 | if (!result) |
| 984 | return NULL; |
| 985 | /* If we have been initialized from a tuple, |
| 986 | st_?time might be set to None. Initialize it |
| 987 | from the int slots. */ |
| 988 | for (i = 7; i <= 9; i++) { |
| 989 | if (result->ob_item[i+3] == Py_None) { |
| 990 | Py_DECREF(Py_None); |
| 991 | Py_INCREF(result->ob_item[i]); |
| 992 | result->ob_item[i+3] = result->ob_item[i]; |
| 993 | } |
| 994 | } |
| 995 | return (PyObject*)result; |
| 996 | } |
| 997 | |
| 998 | |
| 999 | |
| 1000 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1001 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1002 | |
| 1003 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1004 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1005 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1006 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1007 | future calls return ints. \n\ |
| 1008 | If newval is omitted, return the current setting.\n"); |
| 1009 | |
| 1010 | static PyObject* |
| 1011 | stat_float_times(PyObject* self, PyObject *args) |
| 1012 | { |
| 1013 | int newval = -1; |
| 1014 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1015 | return NULL; |
| 1016 | if (newval == -1) |
| 1017 | /* Return old value */ |
| 1018 | return PyBool_FromLong(_stat_float_times); |
| 1019 | _stat_float_times = newval; |
| 1020 | Py_INCREF(Py_None); |
| 1021 | return Py_None; |
| 1022 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1023 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1024 | static void |
| 1025 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1026 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1027 | PyObject *fval,*ival; |
| 1028 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1029 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1030 | #else |
| 1031 | ival = PyInt_FromLong((long)sec); |
| 1032 | #endif |
| 1033 | if (_stat_float_times) { |
| 1034 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1035 | } else { |
| 1036 | fval = ival; |
| 1037 | Py_INCREF(fval); |
| 1038 | } |
| 1039 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1040 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1043 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1044 | (used by posix_stat() and posix_fstat()) */ |
| 1045 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1046 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1047 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1048 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1049 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1050 | if (v == NULL) |
| 1051 | return NULL; |
| 1052 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1053 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1054 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1055 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1056 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1057 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1058 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1059 | #endif |
| 1060 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1061 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1062 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1063 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1064 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1065 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1066 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); |
| 1067 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); |
| 1068 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1069 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1070 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1071 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1072 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1073 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1074 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1075 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1076 | #if defined(HAVE_STAT_TV_NSEC) |
| 1077 | ansec = st->st_atim.tv_nsec; |
| 1078 | mnsec = st->st_mtim.tv_nsec; |
| 1079 | cnsec = st->st_ctim.tv_nsec; |
| 1080 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1081 | ansec = st->st_atimespec.tv_nsec; |
| 1082 | mnsec = st->st_mtimespec.tv_nsec; |
| 1083 | cnsec = st->st_ctimespec.tv_nsec; |
| 1084 | #elif defined(HAVE_STAT_NSEC) |
| 1085 | ansec = st->st_atime_nsec; |
| 1086 | mnsec = st->st_mtime_nsec; |
| 1087 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1088 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1089 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1090 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1091 | fill_time(v, 7, st->st_atime, ansec); |
| 1092 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1093 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1094 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1095 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1096 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1097 | PyInt_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1098 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1099 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1100 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1101 | PyInt_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1102 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1103 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1104 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1105 | PyInt_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1106 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1107 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1108 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1109 | PyInt_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1110 | #endif |
| 1111 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1112 | { |
| 1113 | PyObject *val; |
| 1114 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1115 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1116 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1117 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1118 | #else |
| 1119 | bnsec = 0; |
| 1120 | #endif |
| 1121 | if (_stat_float_times) { |
| 1122 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1123 | } else { |
| 1124 | val = PyInt_FromLong((long)bsec); |
| 1125 | } |
| 1126 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1127 | val); |
| 1128 | } |
| 1129 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1130 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1131 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1132 | PyInt_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1133 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1134 | |
| 1135 | if (PyErr_Occurred()) { |
| 1136 | Py_DECREF(v); |
| 1137 | return NULL; |
| 1138 | } |
| 1139 | |
| 1140 | return v; |
| 1141 | } |
| 1142 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1143 | #ifdef MS_WINDOWS |
| 1144 | |
| 1145 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1146 | where / can be used in place of \ and the trailing slash is optional. |
| 1147 | Both SERVER and SHARE must have at least one character. |
| 1148 | */ |
| 1149 | |
| 1150 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1151 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
| 1152 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
| 1153 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1154 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1155 | IsUNCRootA(char *path, int pathlen) |
| 1156 | { |
| 1157 | #define ISSLASH ISSLASHA |
| 1158 | |
| 1159 | int i, share; |
| 1160 | |
| 1161 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1162 | /* minimum UNCRoot is \\x\y */ |
| 1163 | return FALSE; |
| 1164 | for (i = 2; i < pathlen ; i++) |
| 1165 | if (ISSLASH(path[i])) break; |
| 1166 | if (i == 2 || i == pathlen) |
| 1167 | /* do not allow \\\SHARE or \\SERVER */ |
| 1168 | return FALSE; |
| 1169 | share = i+1; |
| 1170 | for (i = share; i < pathlen; i++) |
| 1171 | if (ISSLASH(path[i])) break; |
| 1172 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1173 | |
| 1174 | #undef ISSLASH |
| 1175 | } |
| 1176 | |
| 1177 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1178 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1179 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1180 | { |
| 1181 | #define ISSLASH ISSLASHW |
| 1182 | |
| 1183 | int i, share; |
| 1184 | |
| 1185 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1186 | /* minimum UNCRoot is \\x\y */ |
| 1187 | return FALSE; |
| 1188 | for (i = 2; i < pathlen ; i++) |
| 1189 | if (ISSLASH(path[i])) break; |
| 1190 | if (i == 2 || i == pathlen) |
| 1191 | /* do not allow \\\SHARE or \\SERVER */ |
| 1192 | return FALSE; |
| 1193 | share = i+1; |
| 1194 | for (i = share; i < pathlen; i++) |
| 1195 | if (ISSLASH(path[i])) break; |
| 1196 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1197 | |
| 1198 | #undef ISSLASH |
| 1199 | } |
| 1200 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1201 | #endif /* MS_WINDOWS */ |
| 1202 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1203 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1204 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1205 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1206 | #ifdef __VMS |
| 1207 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1208 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1209 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1210 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1211 | char *wformat, |
| 1212 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1213 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1214 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1215 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1216 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1217 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1218 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1219 | |
| 1220 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1221 | /* If on wide-character-capable OS see if argument |
| 1222 | is Unicode and if so use wide API. */ |
| 1223 | if (unicode_file_names()) { |
| 1224 | PyUnicodeObject *po; |
| 1225 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1226 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1227 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1228 | Py_BEGIN_ALLOW_THREADS |
| 1229 | /* PyUnicode_AS_UNICODE result OK without |
| 1230 | thread lock as it is a simple dereference. */ |
| 1231 | res = wstatfunc(wpath, &st); |
| 1232 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1233 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1234 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1235 | return win32_error_unicode("stat", wpath); |
| 1236 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1237 | } |
| 1238 | /* Drop the argument parsing error as narrow strings |
| 1239 | are also valid. */ |
| 1240 | PyErr_Clear(); |
| 1241 | } |
| 1242 | #endif |
| 1243 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1244 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1245 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1246 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1247 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1248 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1249 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1250 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1251 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1252 | |
| 1253 | if (res != 0) { |
| 1254 | #ifdef MS_WINDOWS |
| 1255 | result = win32_error("stat", pathfree); |
| 1256 | #else |
| 1257 | result = posix_error_with_filename(pathfree); |
| 1258 | #endif |
| 1259 | } |
| 1260 | else |
| 1261 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1262 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1263 | PyMem_Free(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1264 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1267 | /* POSIX methods */ |
| 1268 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1269 | PyDoc_STRVAR(posix_access__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1270 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1271 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1272 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1273 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1274 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1275 | 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] | 1276 | |
| 1277 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1278 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1279 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1280 | char *path; |
| 1281 | int mode; |
| 1282 | int res; |
| 1283 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1284 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1285 | if (unicode_file_names()) { |
| 1286 | PyUnicodeObject *po; |
| 1287 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1288 | Py_BEGIN_ALLOW_THREADS |
| 1289 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1290 | it is a simple dereference. */ |
| 1291 | res = _waccess(PyUnicode_AS_UNICODE(po), mode); |
| 1292 | Py_END_ALLOW_THREADS |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1293 | return PyBool_FromLong(res == 0); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1294 | } |
| 1295 | /* Drop the argument parsing error as narrow strings |
| 1296 | are also valid. */ |
| 1297 | PyErr_Clear(); |
| 1298 | } |
| 1299 | #endif |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame] | 1300 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1301 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1302 | return NULL; |
| 1303 | Py_BEGIN_ALLOW_THREADS |
| 1304 | res = access(path, mode); |
| 1305 | Py_END_ALLOW_THREADS |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 1306 | PyMem_Free(path); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1307 | return PyBool_FromLong(res == 0); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1310 | #ifndef F_OK |
| 1311 | #define F_OK 0 |
| 1312 | #endif |
| 1313 | #ifndef R_OK |
| 1314 | #define R_OK 4 |
| 1315 | #endif |
| 1316 | #ifndef W_OK |
| 1317 | #define W_OK 2 |
| 1318 | #endif |
| 1319 | #ifndef X_OK |
| 1320 | #define X_OK 1 |
| 1321 | #endif |
| 1322 | |
| 1323 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1324 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1325 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1326 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1327 | |
| 1328 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1329 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1330 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1331 | int id; |
| 1332 | char *ret; |
| 1333 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1334 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1335 | return NULL; |
| 1336 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1337 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1338 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1339 | if (id == 0) { |
| 1340 | ret = ttyname(); |
| 1341 | } |
| 1342 | else { |
| 1343 | ret = NULL; |
| 1344 | } |
| 1345 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1346 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1347 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1348 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1349 | return posix_error(); |
| 1350 | return PyString_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1351 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1352 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1353 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1354 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1355 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1356 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1357 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1358 | |
| 1359 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1360 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1361 | { |
| 1362 | char *ret; |
| 1363 | char buffer[L_ctermid]; |
| 1364 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1365 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1366 | ret = ctermid_r(buffer); |
| 1367 | #else |
| 1368 | ret = ctermid(buffer); |
| 1369 | #endif |
| 1370 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1371 | return posix_error(); |
| 1372 | return PyString_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1373 | } |
| 1374 | #endif |
| 1375 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1376 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1377 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1378 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1379 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1380 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1381 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1382 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1383 | #ifdef MS_WINDOWS |
| 1384 | return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir); |
| 1385 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1386 | return posix_1str(args, "et:chdir", _chdir2, NULL, NULL); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1387 | #elif defined(__VMS) |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1388 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1389 | NULL, NULL); |
| 1390 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1391 | return posix_1str(args, "et:chdir", chdir, NULL, NULL); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1392 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1395 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1396 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1397 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1398 | 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] | 1399 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1400 | |
| 1401 | static PyObject * |
| 1402 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1403 | { |
| 1404 | return posix_fildes(fdobj, fchdir); |
| 1405 | } |
| 1406 | #endif /* HAVE_FCHDIR */ |
| 1407 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1408 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1409 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1410 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1411 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1412 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1413 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1414 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1415 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1416 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1417 | int i; |
| 1418 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1419 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1420 | if (unicode_file_names()) { |
| 1421 | PyUnicodeObject *po; |
| 1422 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1423 | Py_BEGIN_ALLOW_THREADS |
| 1424 | res = _wchmod(PyUnicode_AS_UNICODE(po), i); |
| 1425 | Py_END_ALLOW_THREADS |
| 1426 | if (res < 0) |
| 1427 | return posix_error_with_unicode_filename( |
| 1428 | PyUnicode_AS_UNICODE(po)); |
| 1429 | Py_INCREF(Py_None); |
| 1430 | return Py_None; |
| 1431 | } |
| 1432 | /* Drop the argument parsing error as narrow strings |
| 1433 | are also valid. */ |
| 1434 | PyErr_Clear(); |
| 1435 | } |
| 1436 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1437 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1438 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1439 | return NULL; |
| 1440 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1441 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1442 | Py_END_ALLOW_THREADS |
| 1443 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1444 | return posix_error_with_allocated_filename(path); |
| 1445 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1446 | Py_INCREF(Py_None); |
| 1447 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1450 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1451 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1452 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1453 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1454 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1455 | |
| 1456 | static PyObject * |
| 1457 | posix_chroot(PyObject *self, PyObject *args) |
| 1458 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1459 | return posix_1str(args, "et:chroot", chroot, NULL, NULL); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1460 | } |
| 1461 | #endif |
| 1462 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1463 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1464 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1465 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1466 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1467 | |
| 1468 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1469 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1470 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1471 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1472 | } |
| 1473 | #endif /* HAVE_FSYNC */ |
| 1474 | |
| 1475 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1476 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1477 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1478 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1479 | #endif |
| 1480 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1481 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1482 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1483 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1484 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1485 | |
| 1486 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1487 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1488 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1489 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1490 | } |
| 1491 | #endif /* HAVE_FDATASYNC */ |
| 1492 | |
| 1493 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1494 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1495 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1496 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1497 | 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] | 1498 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1499 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1500 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1501 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1502 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1503 | int uid, gid; |
| 1504 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1505 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1506 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1507 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1508 | return NULL; |
| 1509 | Py_BEGIN_ALLOW_THREADS |
| 1510 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1511 | Py_END_ALLOW_THREADS |
| 1512 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1513 | return posix_error_with_allocated_filename(path); |
| 1514 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1515 | Py_INCREF(Py_None); |
| 1516 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1517 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1518 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1519 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1520 | #ifdef HAVE_LCHOWN |
| 1521 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1522 | "lchown(path, uid, gid)\n\n\ |
| 1523 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1524 | This function will not follow symbolic links."); |
| 1525 | |
| 1526 | static PyObject * |
| 1527 | posix_lchown(PyObject *self, PyObject *args) |
| 1528 | { |
| 1529 | char *path = NULL; |
| 1530 | int uid, gid; |
| 1531 | int res; |
| 1532 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1533 | Py_FileSystemDefaultEncoding, &path, |
| 1534 | &uid, &gid)) |
| 1535 | return NULL; |
| 1536 | Py_BEGIN_ALLOW_THREADS |
| 1537 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1538 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1539 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1540 | return posix_error_with_allocated_filename(path); |
| 1541 | PyMem_Free(path); |
| 1542 | Py_INCREF(Py_None); |
| 1543 | return Py_None; |
| 1544 | } |
| 1545 | #endif /* HAVE_LCHOWN */ |
| 1546 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1547 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1548 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1549 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1550 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1551 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1552 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1553 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1554 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1555 | { |
| 1556 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1557 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1558 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1559 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1560 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1561 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1562 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1563 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1564 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1565 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1566 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1567 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1568 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1569 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1570 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1571 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1572 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1573 | "getcwdu() -> path\n\n\ |
| 1574 | Return a unicode string representing the current working directory."); |
| 1575 | |
| 1576 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1577 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1578 | { |
| 1579 | char buf[1026]; |
| 1580 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1581 | |
| 1582 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1583 | if (unicode_file_names()) { |
| 1584 | wchar_t *wres; |
| 1585 | wchar_t wbuf[1026]; |
| 1586 | Py_BEGIN_ALLOW_THREADS |
| 1587 | wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]); |
| 1588 | Py_END_ALLOW_THREADS |
| 1589 | if (wres == NULL) |
| 1590 | return posix_error(); |
| 1591 | return PyUnicode_FromWideChar(wbuf, wcslen(wbuf)); |
| 1592 | } |
| 1593 | #endif |
| 1594 | |
| 1595 | Py_BEGIN_ALLOW_THREADS |
| 1596 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1597 | res = _getcwd2(buf, sizeof buf); |
| 1598 | #else |
| 1599 | res = getcwd(buf, sizeof buf); |
| 1600 | #endif |
| 1601 | Py_END_ALLOW_THREADS |
| 1602 | if (res == NULL) |
| 1603 | return posix_error(); |
| 1604 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1605 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1606 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1607 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1608 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1609 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1610 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1611 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1612 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1613 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1614 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1615 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1616 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1617 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1618 | return posix_2str(args, "etet:link", link, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1619 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1620 | #endif /* HAVE_LINK */ |
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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1623 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1624 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1625 | Return a list containing the names of the entries in the directory.\n\ |
| 1626 | \n\ |
| 1627 | path: path of directory to list\n\ |
| 1628 | \n\ |
| 1629 | 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] | 1630 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1631 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1632 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1633 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1634 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1635 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1636 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1637 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1638 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1639 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1640 | HANDLE hFindFile; |
| 1641 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1642 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 1643 | char namebuf[MAX_PATH*2+5]; |
| 1644 | char *bufptr = namebuf; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1645 | Py_ssize_t len = sizeof(namebuf)/sizeof(namebuf[0]); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1646 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1647 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1648 | /* If on wide-character-capable OS see if argument |
| 1649 | is Unicode and if so use wide API. */ |
| 1650 | if (unicode_file_names()) { |
| 1651 | PyUnicodeObject *po; |
| 1652 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1653 | WIN32_FIND_DATAW wFileData; |
| 1654 | Py_UNICODE wnamebuf[MAX_PATH*2+5]; |
| 1655 | Py_UNICODE wch; |
| 1656 | wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH); |
| 1657 | wnamebuf[MAX_PATH] = L'\0'; |
| 1658 | len = wcslen(wnamebuf); |
| 1659 | wch = (len > 0) ? wnamebuf[len-1] : L'\0'; |
| 1660 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1661 | wnamebuf[len++] = L'/'; |
| 1662 | wcscpy(wnamebuf + len, L"*.*"); |
| 1663 | if ((d = PyList_New(0)) == NULL) |
| 1664 | return NULL; |
| 1665 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1666 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1667 | errno = GetLastError(); |
| 1668 | if (errno == ERROR_FILE_NOT_FOUND) { |
| 1669 | return d; |
| 1670 | } |
| 1671 | Py_DECREF(d); |
| 1672 | return win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1673 | } |
| 1674 | do { |
| 1675 | if (wFileData.cFileName[0] == L'.' && |
| 1676 | (wFileData.cFileName[1] == L'\0' || |
| 1677 | wFileData.cFileName[1] == L'.' && |
| 1678 | wFileData.cFileName[2] == L'\0')) |
| 1679 | continue; |
| 1680 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1681 | if (v == NULL) { |
| 1682 | Py_DECREF(d); |
| 1683 | d = NULL; |
| 1684 | break; |
| 1685 | } |
| 1686 | if (PyList_Append(d, v) != 0) { |
| 1687 | Py_DECREF(v); |
| 1688 | Py_DECREF(d); |
| 1689 | d = NULL; |
| 1690 | break; |
| 1691 | } |
| 1692 | Py_DECREF(v); |
| 1693 | } while (FindNextFileW(hFindFile, &wFileData) == TRUE); |
| 1694 | |
| 1695 | if (FindClose(hFindFile) == FALSE) { |
| 1696 | Py_DECREF(d); |
| 1697 | return win32_error_unicode("FindClose", wnamebuf); |
| 1698 | } |
| 1699 | return d; |
| 1700 | } |
| 1701 | /* Drop the argument parsing error as narrow strings |
| 1702 | are also valid. */ |
| 1703 | PyErr_Clear(); |
| 1704 | } |
| 1705 | #endif |
| 1706 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1707 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1708 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1709 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1710 | if (len > 0) { |
| 1711 | char ch = namebuf[len-1]; |
| 1712 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 1713 | namebuf[len++] = '/'; |
| 1714 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1715 | strcpy(namebuf + len, "*.*"); |
| 1716 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1717 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1718 | return NULL; |
| 1719 | |
| 1720 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 1721 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1722 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 1723 | if (errno == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1724 | return d; |
| 1725 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1726 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1727 | } |
| 1728 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1729 | if (FileData.cFileName[0] == '.' && |
| 1730 | (FileData.cFileName[1] == '\0' || |
| 1731 | FileData.cFileName[1] == '.' && |
| 1732 | FileData.cFileName[2] == '\0')) |
| 1733 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1734 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1735 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1736 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1737 | d = NULL; |
| 1738 | break; |
| 1739 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1740 | if (PyList_Append(d, v) != 0) { |
| 1741 | Py_DECREF(v); |
| 1742 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1743 | d = NULL; |
| 1744 | break; |
| 1745 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1746 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1747 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 1748 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1749 | if (FindClose(hFindFile) == FALSE) { |
| 1750 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1751 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1752 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1753 | |
| 1754 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1755 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1756 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1757 | |
| 1758 | #ifndef MAX_PATH |
| 1759 | #define MAX_PATH CCHMAXPATH |
| 1760 | #endif |
| 1761 | char *name, *pt; |
| 1762 | int len; |
| 1763 | PyObject *d, *v; |
| 1764 | char namebuf[MAX_PATH+5]; |
| 1765 | HDIR hdir = 1; |
| 1766 | ULONG srchcnt = 1; |
| 1767 | FILEFINDBUF3 ep; |
| 1768 | APIRET rc; |
| 1769 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1770 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1771 | return NULL; |
| 1772 | if (len >= MAX_PATH) { |
| 1773 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1774 | return NULL; |
| 1775 | } |
| 1776 | strcpy(namebuf, name); |
| 1777 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1778 | if (*pt == ALTSEP) |
| 1779 | *pt = SEP; |
| 1780 | if (namebuf[len-1] != SEP) |
| 1781 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1782 | strcpy(namebuf + len, "*.*"); |
| 1783 | |
| 1784 | if ((d = PyList_New(0)) == NULL) |
| 1785 | return NULL; |
| 1786 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1787 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1788 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1789 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1790 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1791 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1792 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1793 | |
| 1794 | if (rc != NO_ERROR) { |
| 1795 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1796 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1799 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1800 | do { |
| 1801 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1802 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1803 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1804 | |
| 1805 | strcpy(namebuf, ep.achName); |
| 1806 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1807 | /* Leave Case of Name Alone -- In Native Form */ |
| 1808 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1809 | |
| 1810 | v = PyString_FromString(namebuf); |
| 1811 | if (v == NULL) { |
| 1812 | Py_DECREF(d); |
| 1813 | d = NULL; |
| 1814 | break; |
| 1815 | } |
| 1816 | if (PyList_Append(d, v) != 0) { |
| 1817 | Py_DECREF(v); |
| 1818 | Py_DECREF(d); |
| 1819 | d = NULL; |
| 1820 | break; |
| 1821 | } |
| 1822 | Py_DECREF(v); |
| 1823 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1824 | } |
| 1825 | |
| 1826 | return d; |
| 1827 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1828 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1829 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1830 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1831 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1832 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1833 | int arg_is_unicode = 1; |
| 1834 | |
| 1835 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 1836 | arg_is_unicode = 0; |
| 1837 | PyErr_Clear(); |
| 1838 | } |
| 1839 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1840 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1841 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1842 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1843 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1844 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1845 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1846 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1847 | return NULL; |
| 1848 | } |
| 1849 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1850 | if (ep->d_name[0] == '.' && |
| 1851 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1852 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1853 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1854 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1855 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1856 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1857 | d = NULL; |
| 1858 | break; |
| 1859 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1860 | #ifdef Py_USING_UNICODE |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1861 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1862 | PyObject *w; |
| 1863 | |
| 1864 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1865 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1866 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 1867 | if (w != NULL) { |
| 1868 | Py_DECREF(v); |
| 1869 | v = w; |
| 1870 | } |
| 1871 | else { |
| 1872 | /* fall back to the original byte string, as |
| 1873 | discussed in patch #683592 */ |
| 1874 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1875 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1876 | } |
| 1877 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1878 | if (PyList_Append(d, v) != 0) { |
| 1879 | Py_DECREF(v); |
| 1880 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1881 | d = NULL; |
| 1882 | break; |
| 1883 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1884 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1885 | } |
| 1886 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1887 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1888 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1889 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1890 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1891 | #endif /* which OS */ |
| 1892 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1893 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1894 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1895 | /* A helper function for abspath on win32 */ |
| 1896 | static PyObject * |
| 1897 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1898 | { |
| 1899 | /* assume encoded strings wont more than double no of chars */ |
| 1900 | char inbuf[MAX_PATH*2]; |
| 1901 | char *inbufp = inbuf; |
| 1902 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); |
| 1903 | char outbuf[MAX_PATH*2]; |
| 1904 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1905 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1906 | if (unicode_file_names()) { |
| 1907 | PyUnicodeObject *po; |
| 1908 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 1909 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 1910 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1911 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1912 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 1913 | woutbuf, &wtemp)) |
| 1914 | return win32_error("GetFullPathName", ""); |
| 1915 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 1916 | } |
| 1917 | /* Drop the argument parsing error as narrow strings |
| 1918 | are also valid. */ |
| 1919 | PyErr_Clear(); |
| 1920 | } |
| 1921 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1922 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1923 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1924 | &insize)) |
| 1925 | return NULL; |
| 1926 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1927 | outbuf, &temp)) |
| 1928 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 1929 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 1930 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 1931 | Py_FileSystemDefaultEncoding, NULL); |
| 1932 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1933 | return PyString_FromString(outbuf); |
| 1934 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1935 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1936 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1937 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1938 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1939 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1940 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1941 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1942 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1943 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1944 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1945 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1946 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1947 | |
| 1948 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1949 | if (unicode_file_names()) { |
| 1950 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 1951 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1952 | Py_BEGIN_ALLOW_THREADS |
| 1953 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1954 | it is a simple dereference. */ |
| 1955 | res = _wmkdir(PyUnicode_AS_UNICODE(po)); |
| 1956 | Py_END_ALLOW_THREADS |
| 1957 | if (res < 0) |
| 1958 | return posix_error(); |
| 1959 | Py_INCREF(Py_None); |
| 1960 | return Py_None; |
| 1961 | } |
| 1962 | /* Drop the argument parsing error as narrow strings |
| 1963 | are also valid. */ |
| 1964 | PyErr_Clear(); |
| 1965 | } |
| 1966 | #endif |
| 1967 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1968 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1969 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1970 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1971 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1972 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1973 | res = mkdir(path); |
| 1974 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1975 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1976 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1977 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1978 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1979 | return posix_error_with_allocated_filename(path); |
| 1980 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1981 | Py_INCREF(Py_None); |
| 1982 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1985 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1986 | #ifdef HAVE_NICE |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1987 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) |
| 1988 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) |
| 1989 | #include <sys/resource.h> |
| 1990 | #endif |
| 1991 | #endif |
| 1992 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1993 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1994 | "nice(inc) -> new_priority\n\n\ |
| 1995 | 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] | 1996 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1997 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1998 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1999 | { |
| 2000 | int increment, value; |
| 2001 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2002 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2003 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2004 | |
| 2005 | /* There are two flavours of 'nice': one that returns the new |
| 2006 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2007 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2008 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2009 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2010 | If we are of the nice family that returns the new priority, we |
| 2011 | need to clear errno before the call, and check if errno is filled |
| 2012 | before calling posix_error() on a returnvalue of -1, because the |
| 2013 | -1 may be the actual new priority! */ |
| 2014 | |
| 2015 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2016 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2017 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2018 | if (value == 0) |
| 2019 | value = getpriority(PRIO_PROCESS, 0); |
| 2020 | #endif |
| 2021 | if (value == -1 && errno != 0) |
| 2022 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2023 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2024 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2025 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2026 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2027 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2028 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2029 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2030 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2031 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2032 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2033 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2034 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2035 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2036 | #ifdef MS_WINDOWS |
| 2037 | return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename); |
| 2038 | #else |
| 2039 | return posix_2str(args, "etet:rename", rename, NULL, NULL); |
| 2040 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2043 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2044 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2045 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2046 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2047 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2048 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2049 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2050 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2051 | #ifdef MS_WINDOWS |
| 2052 | return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir); |
| 2053 | #else |
| 2054 | return posix_1str(args, "et:rmdir", rmdir, NULL, NULL); |
| 2055 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2056 | } |
| 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_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2060 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2061 | Perform a stat system call on the given path."); |
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_stat(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 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2067 | 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] | 2068 | #else |
| 2069 | return posix_do_stat(self, args, "et:stat", STAT, 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 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2074 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2075 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2076 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2077 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2078 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2079 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2080 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2081 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2082 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2083 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2084 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2085 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2086 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2087 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2088 | Py_END_ALLOW_THREADS |
| 2089 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2090 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2091 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2092 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2093 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2094 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2095 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2096 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2097 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2098 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2099 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2100 | { |
| 2101 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2102 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2103 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2104 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2105 | if (i < 0) |
| 2106 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2107 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2110 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2111 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2112 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2113 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2114 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2115 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2116 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2117 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2118 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2119 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2120 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2121 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2122 | #ifdef MS_WINDOWS |
| 2123 | return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink); |
| 2124 | #else |
| 2125 | return posix_1str(args, "et:remove", unlink, NULL, NULL); |
| 2126 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2129 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2130 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2131 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2132 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2133 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2134 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2135 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2136 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2137 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2138 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2139 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2140 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2141 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2142 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2143 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2144 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2145 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2146 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2147 | u.sysname, |
| 2148 | u.nodename, |
| 2149 | u.release, |
| 2150 | u.version, |
| 2151 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2152 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2153 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2154 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2155 | static int |
| 2156 | extract_time(PyObject *t, long* sec, long* usec) |
| 2157 | { |
| 2158 | long intval; |
| 2159 | if (PyFloat_Check(t)) { |
| 2160 | double tval = PyFloat_AsDouble(t); |
| 2161 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 2162 | if (!intobj) |
| 2163 | return -1; |
| 2164 | intval = PyInt_AsLong(intobj); |
| 2165 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2166 | if (intval == -1 && PyErr_Occurred()) |
| 2167 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2168 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2169 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2170 | if (*usec < 0) |
| 2171 | /* If rounding gave us a negative number, |
| 2172 | truncate. */ |
| 2173 | *usec = 0; |
| 2174 | return 0; |
| 2175 | } |
| 2176 | intval = PyInt_AsLong(t); |
| 2177 | if (intval == -1 && PyErr_Occurred()) |
| 2178 | return -1; |
| 2179 | *sec = intval; |
| 2180 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2181 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2182 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2183 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2184 | PyDoc_STRVAR(posix_utime__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2185 | "utime(path, (atime, utime))\n\ |
| 2186 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2187 | 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] | 2188 | 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] | 2189 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2190 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2191 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2192 | { |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2193 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2194 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2195 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2196 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2197 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2198 | #if defined(HAVE_UTIMES) |
| 2199 | struct timeval buf[2]; |
| 2200 | #define ATIME buf[0].tv_sec |
| 2201 | #define MTIME buf[1].tv_sec |
| 2202 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2203 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2204 | struct utimbuf buf; |
| 2205 | #define ATIME buf.actime |
| 2206 | #define MTIME buf.modtime |
| 2207 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2208 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2209 | time_t buf[2]; |
| 2210 | #define ATIME buf[0] |
| 2211 | #define MTIME buf[1] |
| 2212 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2213 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2214 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2215 | int have_unicode_filename = 0; |
| 2216 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2217 | PyUnicodeObject *obwpath; |
| 2218 | wchar_t *wpath; |
| 2219 | if (unicode_file_names()) { |
| 2220 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2221 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2222 | have_unicode_filename = 1; |
| 2223 | } else |
| 2224 | /* Drop the argument parsing error as narrow strings |
| 2225 | are also valid. */ |
| 2226 | PyErr_Clear(); |
| 2227 | } |
| 2228 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 2229 | |
| 2230 | if (!have_unicode_filename && \ |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2231 | !PyArg_ParseTuple(args, "etO:utime", |
| 2232 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2233 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2234 | if (arg == Py_None) { |
| 2235 | /* optional time values not given */ |
| 2236 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2237 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2238 | if (have_unicode_filename) |
| 2239 | res = _wutime(wpath, NULL); |
| 2240 | else |
| 2241 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2242 | res = utime(path, NULL); |
| 2243 | Py_END_ALLOW_THREADS |
| 2244 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2245 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2246 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2247 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2248 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2249 | return NULL; |
| 2250 | } |
| 2251 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2252 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2253 | &atime, &ausec) == -1) { |
| 2254 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2255 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2256 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2257 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2258 | &mtime, &musec) == -1) { |
| 2259 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2260 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2261 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2262 | ATIME = atime; |
| 2263 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2264 | #ifdef HAVE_UTIMES |
| 2265 | buf[0].tv_usec = ausec; |
| 2266 | buf[1].tv_usec = musec; |
| 2267 | Py_BEGIN_ALLOW_THREADS |
| 2268 | res = utimes(path, buf); |
| 2269 | Py_END_ALLOW_THREADS |
| 2270 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2271 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2272 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2273 | if (have_unicode_filename) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 2274 | /* utime is OK with utimbuf, but _wutime insists |
| 2275 | on _utimbuf (the msvc headers assert the |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2276 | underscore version is ansi) */ |
| 2277 | res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG); |
| 2278 | else |
| 2279 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2280 | res = utime(path, UTIME_ARG); |
| 2281 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2282 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2283 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2284 | if (res < 0) { |
| 2285 | #ifdef Py_WIN_WIDE_FILENAMES |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2286 | if (have_unicode_filename) |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2287 | return posix_error_with_unicode_filename(wpath); |
| 2288 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2289 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2290 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2291 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2292 | Py_INCREF(Py_None); |
| 2293 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2294 | #undef UTIME_ARG |
| 2295 | #undef ATIME |
| 2296 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2299 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2300 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2301 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2302 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2303 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2304 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2305 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2306 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2307 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2308 | { |
| 2309 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2310 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2311 | return NULL; |
| 2312 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2313 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2316 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2317 | static void |
| 2318 | free_string_array(char **array, int count) |
| 2319 | { |
| 2320 | int i; |
| 2321 | for (i = 0; i < count; i++) |
| 2322 | PyMem_Free(array[i]); |
| 2323 | PyMem_DEL(array); |
| 2324 | } |
| 2325 | #endif |
| 2326 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2327 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2328 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2329 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2330 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2331 | Execute an executable path with arguments, replacing current process.\n\ |
| 2332 | \n\ |
| 2333 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2334 | args: tuple or list of strings"); |
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_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2338 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2339 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2340 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2341 | char **argvlist; |
| 2342 | int i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2343 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2344 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2345 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2346 | argv is a list or tuple of strings. */ |
| 2347 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2348 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2349 | Py_FileSystemDefaultEncoding, |
| 2350 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2351 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2352 | if (PyList_Check(argv)) { |
| 2353 | argc = PyList_Size(argv); |
| 2354 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2355 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2356 | else if (PyTuple_Check(argv)) { |
| 2357 | argc = PyTuple_Size(argv); |
| 2358 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2359 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2360 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2361 | 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] | 2362 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2363 | return NULL; |
| 2364 | } |
| 2365 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2366 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2367 | if (argvlist == NULL) { |
| 2368 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2369 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2370 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2371 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2372 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2373 | Py_FileSystemDefaultEncoding, |
| 2374 | &argvlist[i])) { |
| 2375 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2376 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2377 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2378 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2379 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2380 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2381 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2382 | } |
| 2383 | argvlist[argc] = NULL; |
| 2384 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2385 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2386 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2387 | /* If we get here it's definitely an error */ |
| 2388 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2389 | free_string_array(argvlist, argc); |
| 2390 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2391 | return posix_error(); |
| 2392 | } |
| 2393 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2394 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2395 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2396 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2397 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2398 | \n\ |
| 2399 | path: path of executable file\n\ |
| 2400 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2401 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2402 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2403 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2404 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2405 | { |
| 2406 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2407 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2408 | char **argvlist; |
| 2409 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2410 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2411 | int i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2412 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2413 | int lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2414 | |
| 2415 | /* execve has three arguments: (path, argv, env), where |
| 2416 | argv is a list or tuple of strings and env is a dictionary |
| 2417 | like posix.environ. */ |
| 2418 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2419 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2420 | Py_FileSystemDefaultEncoding, |
| 2421 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2422 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2423 | if (PyList_Check(argv)) { |
| 2424 | argc = PyList_Size(argv); |
| 2425 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2426 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2427 | else if (PyTuple_Check(argv)) { |
| 2428 | argc = PyTuple_Size(argv); |
| 2429 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2430 | } |
| 2431 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2432 | PyErr_SetString(PyExc_TypeError, |
| 2433 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2434 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2435 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2436 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2437 | PyErr_SetString(PyExc_TypeError, |
| 2438 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2439 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2442 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2443 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2444 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2445 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2446 | } |
| 2447 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2448 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2449 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2450 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2451 | &argvlist[i])) |
| 2452 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2453 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2454 | goto fail_1; |
| 2455 | } |
| 2456 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2457 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2458 | argvlist[argc] = NULL; |
| 2459 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2460 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2461 | if (i < 0) |
| 2462 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2463 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2464 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2465 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2466 | goto fail_1; |
| 2467 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2468 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2469 | keys = PyMapping_Keys(env); |
| 2470 | vals = PyMapping_Values(env); |
| 2471 | if (!keys || !vals) |
| 2472 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2473 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2474 | PyErr_SetString(PyExc_TypeError, |
| 2475 | "execve(): env.keys() or env.values() is not a list"); |
| 2476 | goto fail_2; |
| 2477 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2478 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2479 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2480 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2481 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2482 | |
| 2483 | key = PyList_GetItem(keys, pos); |
| 2484 | val = PyList_GetItem(vals, pos); |
| 2485 | if (!key || !val) |
| 2486 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2487 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2488 | if (!PyArg_Parse( |
| 2489 | key, |
| 2490 | "s;execve() arg 3 contains a non-string key", |
| 2491 | &k) || |
| 2492 | !PyArg_Parse( |
| 2493 | val, |
| 2494 | "s;execve() arg 3 contains a non-string value", |
| 2495 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2496 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2497 | goto fail_2; |
| 2498 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2499 | |
| 2500 | #if defined(PYOS_OS2) |
| 2501 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2502 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2503 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2504 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2505 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2506 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2507 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2508 | goto fail_2; |
| 2509 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2510 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2511 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2512 | #if defined(PYOS_OS2) |
| 2513 | } |
| 2514 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2515 | } |
| 2516 | envlist[envc] = 0; |
| 2517 | |
| 2518 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2519 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2520 | /* If we get here it's definitely an error */ |
| 2521 | |
| 2522 | (void) posix_error(); |
| 2523 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2524 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2525 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2526 | PyMem_DEL(envlist[envc]); |
| 2527 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2528 | fail_1: |
| 2529 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2530 | Py_XDECREF(vals); |
| 2531 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2532 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2533 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2534 | return NULL; |
| 2535 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2536 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2537 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2538 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2539 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2540 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2541 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2542 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2543 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2544 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2545 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2546 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2547 | |
| 2548 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2549 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2550 | { |
| 2551 | char *path; |
| 2552 | PyObject *argv; |
| 2553 | char **argvlist; |
| 2554 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2555 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2556 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2557 | |
| 2558 | /* spawnv has three arguments: (mode, path, argv), where |
| 2559 | argv is a list or tuple of strings. */ |
| 2560 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2561 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2562 | Py_FileSystemDefaultEncoding, |
| 2563 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2564 | return NULL; |
| 2565 | if (PyList_Check(argv)) { |
| 2566 | argc = PyList_Size(argv); |
| 2567 | getitem = PyList_GetItem; |
| 2568 | } |
| 2569 | else if (PyTuple_Check(argv)) { |
| 2570 | argc = PyTuple_Size(argv); |
| 2571 | getitem = PyTuple_GetItem; |
| 2572 | } |
| 2573 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2574 | PyErr_SetString(PyExc_TypeError, |
| 2575 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2576 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2577 | return NULL; |
| 2578 | } |
| 2579 | |
| 2580 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2581 | if (argvlist == NULL) { |
| 2582 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2583 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2584 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2585 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2586 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2587 | Py_FileSystemDefaultEncoding, |
| 2588 | &argvlist[i])) { |
| 2589 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2590 | PyErr_SetString( |
| 2591 | PyExc_TypeError, |
| 2592 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2593 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2594 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2595 | } |
| 2596 | } |
| 2597 | argvlist[argc] = NULL; |
| 2598 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2599 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2600 | Py_BEGIN_ALLOW_THREADS |
| 2601 | spawnval = spawnv(mode, path, argvlist); |
| 2602 | Py_END_ALLOW_THREADS |
| 2603 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2604 | if (mode == _OLD_P_OVERLAY) |
| 2605 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2606 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2607 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2608 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2609 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2610 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2611 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2612 | free_string_array(argvlist, argc); |
| 2613 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2614 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2615 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2616 | return posix_error(); |
| 2617 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2618 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2619 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2620 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2621 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2622 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2626 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2627 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2628 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2629 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2630 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2631 | path: path of executable file\n\ |
| 2632 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2633 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2634 | |
| 2635 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2636 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2637 | { |
| 2638 | char *path; |
| 2639 | PyObject *argv, *env; |
| 2640 | char **argvlist; |
| 2641 | char **envlist; |
| 2642 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2643 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2644 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2645 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2646 | int lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2647 | |
| 2648 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 2649 | argv is a list or tuple of strings and env is a dictionary |
| 2650 | like posix.environ. */ |
| 2651 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2652 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 2653 | Py_FileSystemDefaultEncoding, |
| 2654 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2655 | return NULL; |
| 2656 | if (PyList_Check(argv)) { |
| 2657 | argc = PyList_Size(argv); |
| 2658 | getitem = PyList_GetItem; |
| 2659 | } |
| 2660 | else if (PyTuple_Check(argv)) { |
| 2661 | argc = PyTuple_Size(argv); |
| 2662 | getitem = PyTuple_GetItem; |
| 2663 | } |
| 2664 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2665 | PyErr_SetString(PyExc_TypeError, |
| 2666 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2667 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2668 | } |
| 2669 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2670 | PyErr_SetString(PyExc_TypeError, |
| 2671 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2672 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | argvlist = PyMem_NEW(char *, argc+1); |
| 2676 | if (argvlist == NULL) { |
| 2677 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2678 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2679 | } |
| 2680 | for (i = 0; i < argc; i++) { |
| 2681 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2682 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2683 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2684 | &argvlist[i])) |
| 2685 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2686 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2687 | goto fail_1; |
| 2688 | } |
| 2689 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2690 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2691 | argvlist[argc] = NULL; |
| 2692 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2693 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2694 | if (i < 0) |
| 2695 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2696 | envlist = PyMem_NEW(char *, i + 1); |
| 2697 | if (envlist == NULL) { |
| 2698 | PyErr_NoMemory(); |
| 2699 | goto fail_1; |
| 2700 | } |
| 2701 | envc = 0; |
| 2702 | keys = PyMapping_Keys(env); |
| 2703 | vals = PyMapping_Values(env); |
| 2704 | if (!keys || !vals) |
| 2705 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2706 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2707 | PyErr_SetString(PyExc_TypeError, |
| 2708 | "spawnve(): env.keys() or env.values() is not a list"); |
| 2709 | goto fail_2; |
| 2710 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2711 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2712 | for (pos = 0; pos < i; pos++) { |
| 2713 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2714 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2715 | |
| 2716 | key = PyList_GetItem(keys, pos); |
| 2717 | val = PyList_GetItem(vals, pos); |
| 2718 | if (!key || !val) |
| 2719 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2720 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2721 | if (!PyArg_Parse( |
| 2722 | key, |
| 2723 | "s;spawnve() arg 3 contains a non-string key", |
| 2724 | &k) || |
| 2725 | !PyArg_Parse( |
| 2726 | val, |
| 2727 | "s;spawnve() arg 3 contains a non-string value", |
| 2728 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2729 | { |
| 2730 | goto fail_2; |
| 2731 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2732 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2733 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2734 | if (p == NULL) { |
| 2735 | PyErr_NoMemory(); |
| 2736 | goto fail_2; |
| 2737 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2738 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2739 | envlist[envc++] = p; |
| 2740 | } |
| 2741 | envlist[envc] = 0; |
| 2742 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2743 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2744 | Py_BEGIN_ALLOW_THREADS |
| 2745 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2746 | Py_END_ALLOW_THREADS |
| 2747 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2748 | if (mode == _OLD_P_OVERLAY) |
| 2749 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2750 | |
| 2751 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2752 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2753 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2754 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2755 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2756 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2757 | (void) posix_error(); |
| 2758 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2759 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2760 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2761 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2762 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2763 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2764 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2765 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2766 | while (--envc >= 0) |
| 2767 | PyMem_DEL(envlist[envc]); |
| 2768 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2769 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2770 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2771 | Py_XDECREF(vals); |
| 2772 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2773 | fail_0: |
| 2774 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2775 | return res; |
| 2776 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2777 | |
| 2778 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 2779 | #if defined(PYOS_OS2) |
| 2780 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 2781 | "spawnvp(mode, file, args)\n\n\ |
| 2782 | Execute the program 'file' in a new process, using the environment\n\ |
| 2783 | search path to find the file.\n\ |
| 2784 | \n\ |
| 2785 | mode: mode of process creation\n\ |
| 2786 | file: executable file name\n\ |
| 2787 | args: tuple or list of strings"); |
| 2788 | |
| 2789 | static PyObject * |
| 2790 | posix_spawnvp(PyObject *self, PyObject *args) |
| 2791 | { |
| 2792 | char *path; |
| 2793 | PyObject *argv; |
| 2794 | char **argvlist; |
| 2795 | int mode, i, argc; |
| 2796 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2797 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2798 | |
| 2799 | /* spawnvp has three arguments: (mode, path, argv), where |
| 2800 | argv is a list or tuple of strings. */ |
| 2801 | |
| 2802 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 2803 | Py_FileSystemDefaultEncoding, |
| 2804 | &path, &argv)) |
| 2805 | return NULL; |
| 2806 | if (PyList_Check(argv)) { |
| 2807 | argc = PyList_Size(argv); |
| 2808 | getitem = PyList_GetItem; |
| 2809 | } |
| 2810 | else if (PyTuple_Check(argv)) { |
| 2811 | argc = PyTuple_Size(argv); |
| 2812 | getitem = PyTuple_GetItem; |
| 2813 | } |
| 2814 | else { |
| 2815 | PyErr_SetString(PyExc_TypeError, |
| 2816 | "spawnvp() arg 2 must be a tuple or list"); |
| 2817 | PyMem_Free(path); |
| 2818 | return NULL; |
| 2819 | } |
| 2820 | |
| 2821 | argvlist = PyMem_NEW(char *, argc+1); |
| 2822 | if (argvlist == NULL) { |
| 2823 | PyMem_Free(path); |
| 2824 | return PyErr_NoMemory(); |
| 2825 | } |
| 2826 | for (i = 0; i < argc; i++) { |
| 2827 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2828 | Py_FileSystemDefaultEncoding, |
| 2829 | &argvlist[i])) { |
| 2830 | free_string_array(argvlist, i); |
| 2831 | PyErr_SetString( |
| 2832 | PyExc_TypeError, |
| 2833 | "spawnvp() arg 2 must contain only strings"); |
| 2834 | PyMem_Free(path); |
| 2835 | return NULL; |
| 2836 | } |
| 2837 | } |
| 2838 | argvlist[argc] = NULL; |
| 2839 | |
| 2840 | Py_BEGIN_ALLOW_THREADS |
| 2841 | #if defined(PYCC_GCC) |
| 2842 | spawnval = spawnvp(mode, path, argvlist); |
| 2843 | #else |
| 2844 | spawnval = _spawnvp(mode, path, argvlist); |
| 2845 | #endif |
| 2846 | Py_END_ALLOW_THREADS |
| 2847 | |
| 2848 | free_string_array(argvlist, argc); |
| 2849 | PyMem_Free(path); |
| 2850 | |
| 2851 | if (spawnval == -1) |
| 2852 | return posix_error(); |
| 2853 | else |
| 2854 | return Py_BuildValue("l", (long) spawnval); |
| 2855 | } |
| 2856 | |
| 2857 | |
| 2858 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 2859 | "spawnvpe(mode, file, args, env)\n\n\ |
| 2860 | Execute the program 'file' in a new process, using the environment\n\ |
| 2861 | search path to find the file.\n\ |
| 2862 | \n\ |
| 2863 | mode: mode of process creation\n\ |
| 2864 | file: executable file name\n\ |
| 2865 | args: tuple or list of arguments\n\ |
| 2866 | env: dictionary of strings mapping to strings"); |
| 2867 | |
| 2868 | static PyObject * |
| 2869 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 2870 | { |
| 2871 | char *path; |
| 2872 | PyObject *argv, *env; |
| 2873 | char **argvlist; |
| 2874 | char **envlist; |
| 2875 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2876 | int mode, i, pos, argc, envc; |
| 2877 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2878 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2879 | int lastarg = 0; |
| 2880 | |
| 2881 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 2882 | argv is a list or tuple of strings and env is a dictionary |
| 2883 | like posix.environ. */ |
| 2884 | |
| 2885 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 2886 | Py_FileSystemDefaultEncoding, |
| 2887 | &path, &argv, &env)) |
| 2888 | return NULL; |
| 2889 | if (PyList_Check(argv)) { |
| 2890 | argc = PyList_Size(argv); |
| 2891 | getitem = PyList_GetItem; |
| 2892 | } |
| 2893 | else if (PyTuple_Check(argv)) { |
| 2894 | argc = PyTuple_Size(argv); |
| 2895 | getitem = PyTuple_GetItem; |
| 2896 | } |
| 2897 | else { |
| 2898 | PyErr_SetString(PyExc_TypeError, |
| 2899 | "spawnvpe() arg 2 must be a tuple or list"); |
| 2900 | goto fail_0; |
| 2901 | } |
| 2902 | if (!PyMapping_Check(env)) { |
| 2903 | PyErr_SetString(PyExc_TypeError, |
| 2904 | "spawnvpe() arg 3 must be a mapping object"); |
| 2905 | goto fail_0; |
| 2906 | } |
| 2907 | |
| 2908 | argvlist = PyMem_NEW(char *, argc+1); |
| 2909 | if (argvlist == NULL) { |
| 2910 | PyErr_NoMemory(); |
| 2911 | goto fail_0; |
| 2912 | } |
| 2913 | for (i = 0; i < argc; i++) { |
| 2914 | if (!PyArg_Parse((*getitem)(argv, i), |
| 2915 | "et;spawnvpe() arg 2 must contain only strings", |
| 2916 | Py_FileSystemDefaultEncoding, |
| 2917 | &argvlist[i])) |
| 2918 | { |
| 2919 | lastarg = i; |
| 2920 | goto fail_1; |
| 2921 | } |
| 2922 | } |
| 2923 | lastarg = argc; |
| 2924 | argvlist[argc] = NULL; |
| 2925 | |
| 2926 | i = PyMapping_Size(env); |
| 2927 | if (i < 0) |
| 2928 | goto fail_1; |
| 2929 | envlist = PyMem_NEW(char *, i + 1); |
| 2930 | if (envlist == NULL) { |
| 2931 | PyErr_NoMemory(); |
| 2932 | goto fail_1; |
| 2933 | } |
| 2934 | envc = 0; |
| 2935 | keys = PyMapping_Keys(env); |
| 2936 | vals = PyMapping_Values(env); |
| 2937 | if (!keys || !vals) |
| 2938 | goto fail_2; |
| 2939 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2940 | PyErr_SetString(PyExc_TypeError, |
| 2941 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 2942 | goto fail_2; |
| 2943 | } |
| 2944 | |
| 2945 | for (pos = 0; pos < i; pos++) { |
| 2946 | char *p, *k, *v; |
| 2947 | size_t len; |
| 2948 | |
| 2949 | key = PyList_GetItem(keys, pos); |
| 2950 | val = PyList_GetItem(vals, pos); |
| 2951 | if (!key || !val) |
| 2952 | goto fail_2; |
| 2953 | |
| 2954 | if (!PyArg_Parse( |
| 2955 | key, |
| 2956 | "s;spawnvpe() arg 3 contains a non-string key", |
| 2957 | &k) || |
| 2958 | !PyArg_Parse( |
| 2959 | val, |
| 2960 | "s;spawnvpe() arg 3 contains a non-string value", |
| 2961 | &v)) |
| 2962 | { |
| 2963 | goto fail_2; |
| 2964 | } |
| 2965 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2966 | p = PyMem_NEW(char, len); |
| 2967 | if (p == NULL) { |
| 2968 | PyErr_NoMemory(); |
| 2969 | goto fail_2; |
| 2970 | } |
| 2971 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 2972 | envlist[envc++] = p; |
| 2973 | } |
| 2974 | envlist[envc] = 0; |
| 2975 | |
| 2976 | Py_BEGIN_ALLOW_THREADS |
| 2977 | #if defined(PYCC_GCC) |
| 2978 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2979 | #else |
| 2980 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 2981 | #endif |
| 2982 | Py_END_ALLOW_THREADS |
| 2983 | |
| 2984 | if (spawnval == -1) |
| 2985 | (void) posix_error(); |
| 2986 | else |
| 2987 | res = Py_BuildValue("l", (long) spawnval); |
| 2988 | |
| 2989 | fail_2: |
| 2990 | while (--envc >= 0) |
| 2991 | PyMem_DEL(envlist[envc]); |
| 2992 | PyMem_DEL(envlist); |
| 2993 | fail_1: |
| 2994 | free_string_array(argvlist, lastarg); |
| 2995 | Py_XDECREF(vals); |
| 2996 | Py_XDECREF(keys); |
| 2997 | fail_0: |
| 2998 | PyMem_Free(path); |
| 2999 | return res; |
| 3000 | } |
| 3001 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3002 | #endif /* HAVE_SPAWNV */ |
| 3003 | |
| 3004 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3005 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3006 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3007 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3008 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3009 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3010 | 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] | 3011 | |
| 3012 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3013 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3014 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3015 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3016 | if (pid == -1) |
| 3017 | return posix_error(); |
| 3018 | PyOS_AfterFork(); |
| 3019 | return PyInt_FromLong((long)pid); |
| 3020 | } |
| 3021 | #endif |
| 3022 | |
| 3023 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3024 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3025 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3026 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3027 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3028 | 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] | 3029 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3030 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3031 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3032 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3033 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3034 | if (pid == -1) |
| 3035 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3036 | if (pid == 0) |
| 3037 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3038 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3039 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3040 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3041 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3042 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3043 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3044 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3045 | #define DEV_PTY_FILE "/dev/ptc" |
| 3046 | #define HAVE_DEV_PTMX |
| 3047 | #else |
| 3048 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3049 | #endif |
| 3050 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3051 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3052 | #ifdef HAVE_PTY_H |
| 3053 | #include <pty.h> |
| 3054 | #else |
| 3055 | #ifdef HAVE_LIBUTIL_H |
| 3056 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3057 | #endif /* HAVE_LIBUTIL_H */ |
| 3058 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3059 | #ifdef HAVE_STROPTS_H |
| 3060 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3061 | #endif |
| 3062 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3063 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3064 | #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] | 3065 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3066 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3067 | 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] | 3068 | |
| 3069 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3070 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3071 | { |
| 3072 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3073 | #ifndef HAVE_OPENPTY |
| 3074 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3075 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3076 | #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] | 3077 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3078 | #ifdef sun |
| 3079 | extern char *ptsname(); |
| 3080 | #endif |
| 3081 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3082 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3083 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3084 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3085 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3086 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3087 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3088 | if (slave_name == NULL) |
| 3089 | return posix_error(); |
| 3090 | |
| 3091 | slave_fd = open(slave_name, O_RDWR); |
| 3092 | if (slave_fd < 0) |
| 3093 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3094 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3095 | 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] | 3096 | if (master_fd < 0) |
| 3097 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3098 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3099 | /* change permission of slave */ |
| 3100 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3101 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3102 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3103 | } |
| 3104 | /* unlock slave */ |
| 3105 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3106 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3107 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3108 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3109 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3110 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3111 | if (slave_name == NULL) |
| 3112 | return posix_error(); |
| 3113 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3114 | if (slave_fd < 0) |
| 3115 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3116 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3117 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3118 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3119 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3120 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3121 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3122 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3123 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3124 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3125 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3126 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3127 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3128 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3129 | |
| 3130 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3131 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3132 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3133 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3134 | 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] | 3135 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3136 | |
| 3137 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3138 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3139 | { |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 3140 | int master_fd = -1, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3141 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3142 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3143 | if (pid == -1) |
| 3144 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3145 | if (pid == 0) |
| 3146 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3147 | return Py_BuildValue("(ii)", pid, master_fd); |
| 3148 | } |
| 3149 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3150 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3151 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3152 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3153 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3154 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3155 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3156 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3157 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3158 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3159 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3160 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3161 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3162 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3163 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3164 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3165 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3166 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3167 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3168 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3169 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3170 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3171 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3172 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3173 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3174 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3175 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3176 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3177 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3178 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3179 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3180 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3181 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3182 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3183 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3184 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3185 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3186 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3187 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3188 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3189 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3190 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3191 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3192 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3193 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3194 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3195 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3196 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3197 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3200 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3201 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3202 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3203 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3204 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3205 | |
| 3206 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3207 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3208 | { |
| 3209 | PyObject *result = NULL; |
| 3210 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3211 | #ifdef NGROUPS_MAX |
| 3212 | #define MAX_GROUPS NGROUPS_MAX |
| 3213 | #else |
| 3214 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3215 | #define MAX_GROUPS 64 |
| 3216 | #endif |
| 3217 | gid_t grouplist[MAX_GROUPS]; |
| 3218 | int n; |
| 3219 | |
| 3220 | n = getgroups(MAX_GROUPS, grouplist); |
| 3221 | if (n < 0) |
| 3222 | posix_error(); |
| 3223 | else { |
| 3224 | result = PyList_New(n); |
| 3225 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3226 | int i; |
| 3227 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3228 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3229 | if (o == NULL) { |
| 3230 | Py_DECREF(result); |
| 3231 | result = NULL; |
| 3232 | break; |
| 3233 | } |
| 3234 | PyList_SET_ITEM(result, i, o); |
| 3235 | } |
| 3236 | } |
| 3237 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3238 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3239 | return result; |
| 3240 | } |
| 3241 | #endif |
| 3242 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3243 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3244 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3245 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3246 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3247 | |
| 3248 | static PyObject * |
| 3249 | posix_getpgid(PyObject *self, PyObject *args) |
| 3250 | { |
| 3251 | int pid, pgid; |
| 3252 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3253 | return NULL; |
| 3254 | pgid = getpgid(pid); |
| 3255 | if (pgid < 0) |
| 3256 | return posix_error(); |
| 3257 | return PyInt_FromLong((long)pgid); |
| 3258 | } |
| 3259 | #endif /* HAVE_GETPGID */ |
| 3260 | |
| 3261 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3262 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3263 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3264 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3265 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3266 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3267 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3268 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3269 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3270 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3271 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3272 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3273 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3274 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3275 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3276 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3277 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3278 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3279 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3280 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3281 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3282 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3283 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3284 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3285 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3286 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3287 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3288 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3289 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3290 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3291 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3292 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3293 | Py_INCREF(Py_None); |
| 3294 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3295 | } |
| 3296 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3297 | #endif /* HAVE_SETPGRP */ |
| 3298 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3299 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3300 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3301 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3302 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3303 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3304 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3305 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3306 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3307 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3308 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3309 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3310 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3311 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3312 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3313 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3314 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3315 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3316 | |
| 3317 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3318 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3319 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3320 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3321 | char *name; |
| 3322 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3323 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3324 | errno = 0; |
| 3325 | name = getlogin(); |
| 3326 | if (name == NULL) { |
| 3327 | if (errno) |
| 3328 | posix_error(); |
| 3329 | else |
| 3330 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3331 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3332 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3333 | else |
| 3334 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3335 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3336 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3337 | return result; |
| 3338 | } |
| 3339 | #endif |
| 3340 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3341 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3342 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3343 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3344 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3345 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3346 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3347 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3348 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3349 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3350 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3351 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3352 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3353 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3354 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3355 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3356 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3357 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3358 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3359 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3360 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3361 | { |
| 3362 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3363 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3364 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3365 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3366 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3367 | APIRET rc; |
| 3368 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3369 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3370 | |
| 3371 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3372 | APIRET rc; |
| 3373 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3374 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3375 | |
| 3376 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3377 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3378 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3379 | if (kill(pid, sig) == -1) |
| 3380 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3381 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3382 | Py_INCREF(Py_None); |
| 3383 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3384 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3385 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3386 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3387 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3388 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3389 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3390 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3391 | |
| 3392 | static PyObject * |
| 3393 | posix_killpg(PyObject *self, PyObject *args) |
| 3394 | { |
| 3395 | int pgid, sig; |
| 3396 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3397 | return NULL; |
| 3398 | if (killpg(pgid, sig) == -1) |
| 3399 | return posix_error(); |
| 3400 | Py_INCREF(Py_None); |
| 3401 | return Py_None; |
| 3402 | } |
| 3403 | #endif |
| 3404 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3405 | #ifdef HAVE_PLOCK |
| 3406 | |
| 3407 | #ifdef HAVE_SYS_LOCK_H |
| 3408 | #include <sys/lock.h> |
| 3409 | #endif |
| 3410 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3411 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3412 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3413 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3414 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3415 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3416 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3417 | { |
| 3418 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3419 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3420 | return NULL; |
| 3421 | if (plock(op) == -1) |
| 3422 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3423 | Py_INCREF(Py_None); |
| 3424 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3425 | } |
| 3426 | #endif |
| 3427 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3428 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3429 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3430 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3431 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3432 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3433 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3434 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3435 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3436 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3437 | async_system(const char *command) |
| 3438 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3439 | char errormsg[256], args[1024]; |
| 3440 | RESULTCODES rcodes; |
| 3441 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3442 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3443 | char *shell = getenv("COMSPEC"); |
| 3444 | if (!shell) |
| 3445 | shell = "cmd"; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3446 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3447 | /* avoid overflowing the argument buffer */ |
| 3448 | if (strlen(shell) + 3 + strlen(command) >= 1024) |
| 3449 | return ERROR_NOT_ENOUGH_MEMORY |
| 3450 | |
| 3451 | args[0] = '\0'; |
| 3452 | strcat(args, shell); |
| 3453 | strcat(args, "/c "); |
| 3454 | strcat(args, command); |
| 3455 | |
| 3456 | /* execute asynchronously, inheriting the environment */ |
| 3457 | rc = DosExecPgm(errormsg, |
| 3458 | sizeof(errormsg), |
| 3459 | EXEC_ASYNC, |
| 3460 | args, |
| 3461 | NULL, |
| 3462 | &rcodes, |
| 3463 | shell); |
| 3464 | return rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3467 | static FILE * |
| 3468 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3469 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3470 | int oldfd, tgtfd; |
| 3471 | HFILE pipeh[2]; |
| 3472 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3473 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3474 | /* mode determines which of stdin or stdout is reconnected to |
| 3475 | * the pipe to the child |
| 3476 | */ |
| 3477 | if (strchr(mode, 'r') != NULL) { |
| 3478 | tgt_fd = 1; /* stdout */ |
| 3479 | } else if (strchr(mode, 'w')) { |
| 3480 | tgt_fd = 0; /* stdin */ |
| 3481 | } else { |
| 3482 | *err = ERROR_INVALID_ACCESS; |
| 3483 | return NULL; |
| 3484 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3485 | |
Andrew MacIntyre | a3be258 | 2004-12-18 09:51:05 +0000 | [diff] [blame] | 3486 | /* setup the pipe */ |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3487 | if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) { |
| 3488 | *err = rc; |
| 3489 | return NULL; |
| 3490 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3491 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3492 | /* prevent other threads accessing stdio */ |
| 3493 | DosEnterCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3494 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3495 | /* reconnect stdio and execute child */ |
| 3496 | oldfd = dup(tgtfd); |
| 3497 | close(tgtfd); |
| 3498 | if (dup2(pipeh[tgtfd], tgtfd) == 0) { |
| 3499 | DosClose(pipeh[tgtfd]); |
| 3500 | rc = async_system(command); |
| 3501 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3502 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3503 | /* restore stdio */ |
| 3504 | dup2(oldfd, tgtfd); |
| 3505 | close(oldfd); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3506 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3507 | /* allow other threads access to stdio */ |
| 3508 | DosExitCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3509 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3510 | /* if execution of child was successful return file stream */ |
| 3511 | if (rc == NO_ERROR) |
| 3512 | return fdopen(pipeh[1 - tgtfd], mode); |
| 3513 | else { |
| 3514 | DosClose(pipeh[1 - tgtfd]); |
| 3515 | *err = rc; |
| 3516 | return NULL; |
| 3517 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3521 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3522 | { |
| 3523 | char *name; |
| 3524 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3525 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3526 | FILE *fp; |
| 3527 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3528 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3529 | return NULL; |
| 3530 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3531 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3532 | Py_END_ALLOW_THREADS |
| 3533 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3534 | return os2_error(err); |
| 3535 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3536 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 3537 | if (f != NULL) |
| 3538 | PyFile_SetBufSize(f, bufsize); |
| 3539 | return f; |
| 3540 | } |
| 3541 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3542 | #elif defined(PYCC_GCC) |
| 3543 | |
| 3544 | /* standard posix version of popen() support */ |
| 3545 | static PyObject * |
| 3546 | posix_popen(PyObject *self, PyObject *args) |
| 3547 | { |
| 3548 | char *name; |
| 3549 | char *mode = "r"; |
| 3550 | int bufsize = -1; |
| 3551 | FILE *fp; |
| 3552 | PyObject *f; |
| 3553 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 3554 | return NULL; |
| 3555 | Py_BEGIN_ALLOW_THREADS |
| 3556 | fp = popen(name, mode); |
| 3557 | Py_END_ALLOW_THREADS |
| 3558 | if (fp == NULL) |
| 3559 | return posix_error(); |
| 3560 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 3561 | if (f != NULL) |
| 3562 | PyFile_SetBufSize(f, bufsize); |
| 3563 | return f; |
| 3564 | } |
| 3565 | |
| 3566 | /* fork() under OS/2 has lots'o'warts |
| 3567 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 3568 | * most of this code is a ripoff of the win32 code, but using the |
| 3569 | * capabilities of EMX's C library routines |
| 3570 | */ |
| 3571 | |
| 3572 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 3573 | #define POPEN_1 1 |
| 3574 | #define POPEN_2 2 |
| 3575 | #define POPEN_3 3 |
| 3576 | #define POPEN_4 4 |
| 3577 | |
| 3578 | static PyObject *_PyPopen(char *, int, int, int); |
| 3579 | static int _PyPclose(FILE *file); |
| 3580 | |
| 3581 | /* |
| 3582 | * Internal dictionary mapping popen* file pointers to process handles, |
| 3583 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3584 | * for more information on this dictionary's use. |
| 3585 | */ |
| 3586 | static PyObject *_PyPopenProcs = NULL; |
| 3587 | |
| 3588 | /* os2emx version of popen2() |
| 3589 | * |
| 3590 | * The result of this function is a pipe (file) connected to the |
| 3591 | * process's stdin, and a pipe connected to the process's stdout. |
| 3592 | */ |
| 3593 | |
| 3594 | static PyObject * |
| 3595 | os2emx_popen2(PyObject *self, PyObject *args) |
| 3596 | { |
| 3597 | PyObject *f; |
| 3598 | int tm=0; |
| 3599 | |
| 3600 | char *cmdstring; |
| 3601 | char *mode = "t"; |
| 3602 | int bufsize = -1; |
| 3603 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 3604 | return NULL; |
| 3605 | |
| 3606 | if (*mode == 't') |
| 3607 | tm = O_TEXT; |
| 3608 | else if (*mode != 'b') { |
| 3609 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3610 | return NULL; |
| 3611 | } else |
| 3612 | tm = O_BINARY; |
| 3613 | |
| 3614 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 3615 | |
| 3616 | return f; |
| 3617 | } |
| 3618 | |
| 3619 | /* |
| 3620 | * Variation on os2emx.popen2 |
| 3621 | * |
| 3622 | * The result of this function is 3 pipes - the process's stdin, |
| 3623 | * stdout and stderr |
| 3624 | */ |
| 3625 | |
| 3626 | static PyObject * |
| 3627 | os2emx_popen3(PyObject *self, PyObject *args) |
| 3628 | { |
| 3629 | PyObject *f; |
| 3630 | int tm = 0; |
| 3631 | |
| 3632 | char *cmdstring; |
| 3633 | char *mode = "t"; |
| 3634 | int bufsize = -1; |
| 3635 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 3636 | return NULL; |
| 3637 | |
| 3638 | if (*mode == 't') |
| 3639 | tm = O_TEXT; |
| 3640 | else if (*mode != 'b') { |
| 3641 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3642 | return NULL; |
| 3643 | } else |
| 3644 | tm = O_BINARY; |
| 3645 | |
| 3646 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 3647 | |
| 3648 | return f; |
| 3649 | } |
| 3650 | |
| 3651 | /* |
| 3652 | * Variation on os2emx.popen2 |
| 3653 | * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3654 | * The result of this function is 2 pipes - the processes stdin, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3655 | * and stdout+stderr combined as a single pipe. |
| 3656 | */ |
| 3657 | |
| 3658 | static PyObject * |
| 3659 | os2emx_popen4(PyObject *self, PyObject *args) |
| 3660 | { |
| 3661 | PyObject *f; |
| 3662 | int tm = 0; |
| 3663 | |
| 3664 | char *cmdstring; |
| 3665 | char *mode = "t"; |
| 3666 | int bufsize = -1; |
| 3667 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 3668 | return NULL; |
| 3669 | |
| 3670 | if (*mode == 't') |
| 3671 | tm = O_TEXT; |
| 3672 | else if (*mode != 'b') { |
| 3673 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3674 | return NULL; |
| 3675 | } else |
| 3676 | tm = O_BINARY; |
| 3677 | |
| 3678 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 3679 | |
| 3680 | return f; |
| 3681 | } |
| 3682 | |
| 3683 | /* a couple of structures for convenient handling of multiple |
| 3684 | * file handles and pipes |
| 3685 | */ |
| 3686 | struct file_ref |
| 3687 | { |
| 3688 | int handle; |
| 3689 | int flags; |
| 3690 | }; |
| 3691 | |
| 3692 | struct pipe_ref |
| 3693 | { |
| 3694 | int rd; |
| 3695 | int wr; |
| 3696 | }; |
| 3697 | |
| 3698 | /* The following code is derived from the win32 code */ |
| 3699 | |
| 3700 | static PyObject * |
| 3701 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 3702 | { |
| 3703 | struct file_ref stdio[3]; |
| 3704 | struct pipe_ref p_fd[3]; |
| 3705 | FILE *p_s[3]; |
| 3706 | int file_count, i, pipe_err, pipe_pid; |
| 3707 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 3708 | PyObject *f, *p_f[3]; |
| 3709 | |
| 3710 | /* file modes for subsequent fdopen's on pipe handles */ |
| 3711 | if (mode == O_TEXT) |
| 3712 | { |
| 3713 | rd_mode = "rt"; |
| 3714 | wr_mode = "wt"; |
| 3715 | } |
| 3716 | else |
| 3717 | { |
| 3718 | rd_mode = "rb"; |
| 3719 | wr_mode = "wb"; |
| 3720 | } |
| 3721 | |
| 3722 | /* prepare shell references */ |
| 3723 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 3724 | if ((shell = getenv("COMSPEC")) == NULL) |
| 3725 | { |
| 3726 | errno = ENOENT; |
| 3727 | return posix_error(); |
| 3728 | } |
| 3729 | |
| 3730 | sh_name = _getname(shell); |
| 3731 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 3732 | opt = "/c"; |
| 3733 | else |
| 3734 | opt = "-c"; |
| 3735 | |
| 3736 | /* save current stdio fds + their flags, and set not inheritable */ |
| 3737 | i = pipe_err = 0; |
| 3738 | while (pipe_err >= 0 && i < 3) |
| 3739 | { |
| 3740 | pipe_err = stdio[i].handle = dup(i); |
| 3741 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 3742 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 3743 | i++; |
| 3744 | } |
| 3745 | if (pipe_err < 0) |
| 3746 | { |
| 3747 | /* didn't get them all saved - clean up and bail out */ |
| 3748 | int saved_err = errno; |
| 3749 | while (i-- > 0) |
| 3750 | { |
| 3751 | close(stdio[i].handle); |
| 3752 | } |
| 3753 | errno = saved_err; |
| 3754 | return posix_error(); |
| 3755 | } |
| 3756 | |
| 3757 | /* create pipe ends */ |
| 3758 | file_count = 2; |
| 3759 | if (n == POPEN_3) |
| 3760 | file_count = 3; |
| 3761 | i = pipe_err = 0; |
| 3762 | while ((pipe_err == 0) && (i < file_count)) |
| 3763 | pipe_err = pipe((int *)&p_fd[i++]); |
| 3764 | if (pipe_err < 0) |
| 3765 | { |
| 3766 | /* didn't get them all made - clean up and bail out */ |
| 3767 | while (i-- > 0) |
| 3768 | { |
| 3769 | close(p_fd[i].wr); |
| 3770 | close(p_fd[i].rd); |
| 3771 | } |
| 3772 | errno = EPIPE; |
| 3773 | return posix_error(); |
| 3774 | } |
| 3775 | |
| 3776 | /* change the actual standard IO streams over temporarily, |
| 3777 | * making the retained pipe ends non-inheritable |
| 3778 | */ |
| 3779 | pipe_err = 0; |
| 3780 | |
| 3781 | /* - stdin */ |
| 3782 | if (dup2(p_fd[0].rd, 0) == 0) |
| 3783 | { |
| 3784 | close(p_fd[0].rd); |
| 3785 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 3786 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 3787 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 3788 | { |
| 3789 | close(p_fd[0].wr); |
| 3790 | pipe_err = -1; |
| 3791 | } |
| 3792 | } |
| 3793 | else |
| 3794 | { |
| 3795 | pipe_err = -1; |
| 3796 | } |
| 3797 | |
| 3798 | /* - stdout */ |
| 3799 | if (pipe_err == 0) |
| 3800 | { |
| 3801 | if (dup2(p_fd[1].wr, 1) == 1) |
| 3802 | { |
| 3803 | close(p_fd[1].wr); |
| 3804 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 3805 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 3806 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 3807 | { |
| 3808 | close(p_fd[1].rd); |
| 3809 | pipe_err = -1; |
| 3810 | } |
| 3811 | } |
| 3812 | else |
| 3813 | { |
| 3814 | pipe_err = -1; |
| 3815 | } |
| 3816 | } |
| 3817 | |
| 3818 | /* - stderr, as required */ |
| 3819 | if (pipe_err == 0) |
| 3820 | switch (n) |
| 3821 | { |
| 3822 | case POPEN_3: |
| 3823 | { |
| 3824 | if (dup2(p_fd[2].wr, 2) == 2) |
| 3825 | { |
| 3826 | close(p_fd[2].wr); |
| 3827 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 3828 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 3829 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 3830 | { |
| 3831 | close(p_fd[2].rd); |
| 3832 | pipe_err = -1; |
| 3833 | } |
| 3834 | } |
| 3835 | else |
| 3836 | { |
| 3837 | pipe_err = -1; |
| 3838 | } |
| 3839 | break; |
| 3840 | } |
| 3841 | |
| 3842 | case POPEN_4: |
| 3843 | { |
| 3844 | if (dup2(1, 2) != 2) |
| 3845 | { |
| 3846 | pipe_err = -1; |
| 3847 | } |
| 3848 | break; |
| 3849 | } |
| 3850 | } |
| 3851 | |
| 3852 | /* spawn the child process */ |
| 3853 | if (pipe_err == 0) |
| 3854 | { |
| 3855 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 3856 | if (pipe_pid == -1) |
| 3857 | { |
| 3858 | pipe_err = -1; |
| 3859 | } |
| 3860 | else |
| 3861 | { |
| 3862 | /* save the PID into the FILE structure |
| 3863 | * NOTE: this implementation doesn't actually |
| 3864 | * take advantage of this, but do it for |
| 3865 | * completeness - AIM Apr01 |
| 3866 | */ |
| 3867 | for (i = 0; i < file_count; i++) |
| 3868 | p_s[i]->_pid = pipe_pid; |
| 3869 | } |
| 3870 | } |
| 3871 | |
| 3872 | /* reset standard IO to normal */ |
| 3873 | for (i = 0; i < 3; i++) |
| 3874 | { |
| 3875 | dup2(stdio[i].handle, i); |
| 3876 | fcntl(i, F_SETFD, stdio[i].flags); |
| 3877 | close(stdio[i].handle); |
| 3878 | } |
| 3879 | |
| 3880 | /* if any remnant problems, clean up and bail out */ |
| 3881 | if (pipe_err < 0) |
| 3882 | { |
| 3883 | for (i = 0; i < 3; i++) |
| 3884 | { |
| 3885 | close(p_fd[i].rd); |
| 3886 | close(p_fd[i].wr); |
| 3887 | } |
| 3888 | errno = EPIPE; |
| 3889 | return posix_error_with_filename(cmdstring); |
| 3890 | } |
| 3891 | |
| 3892 | /* build tuple of file objects to return */ |
| 3893 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 3894 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3895 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3896 | PyFile_SetBufSize(p_f[1], bufsize); |
| 3897 | if (n == POPEN_3) |
| 3898 | { |
| 3899 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3900 | PyFile_SetBufSize(p_f[0], bufsize); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3901 | 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] | 3902 | } |
| 3903 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3904 | f = PyTuple_Pack(2, p_f[0], p_f[1]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3905 | |
| 3906 | /* |
| 3907 | * Insert the files we've created into the process dictionary |
| 3908 | * all referencing the list with the process handle and the |
| 3909 | * initial number of files (see description below in _PyPclose). |
| 3910 | * Since if _PyPclose later tried to wait on a process when all |
| 3911 | * handles weren't closed, it could create a deadlock with the |
| 3912 | * child, we spend some energy here to try to ensure that we |
| 3913 | * either insert all file handles into the dictionary or none |
| 3914 | * at all. It's a little clumsy with the various popen modes |
| 3915 | * and variable number of files involved. |
| 3916 | */ |
| 3917 | if (!_PyPopenProcs) |
| 3918 | { |
| 3919 | _PyPopenProcs = PyDict_New(); |
| 3920 | } |
| 3921 | |
| 3922 | if (_PyPopenProcs) |
| 3923 | { |
| 3924 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 3925 | int ins_rc[3]; |
| 3926 | |
| 3927 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 3928 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 3929 | |
| 3930 | procObj = PyList_New(2); |
| 3931 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 3932 | intObj = PyInt_FromLong((long) file_count); |
| 3933 | |
| 3934 | if (procObj && pidObj && intObj) |
| 3935 | { |
| 3936 | PyList_SetItem(procObj, 0, pidObj); |
| 3937 | PyList_SetItem(procObj, 1, intObj); |
| 3938 | |
| 3939 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 3940 | if (fileObj[0]) |
| 3941 | { |
| 3942 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 3943 | fileObj[0], |
| 3944 | procObj); |
| 3945 | } |
| 3946 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 3947 | if (fileObj[1]) |
| 3948 | { |
| 3949 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 3950 | fileObj[1], |
| 3951 | procObj); |
| 3952 | } |
| 3953 | if (file_count >= 3) |
| 3954 | { |
| 3955 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 3956 | if (fileObj[2]) |
| 3957 | { |
| 3958 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 3959 | fileObj[2], |
| 3960 | procObj); |
| 3961 | } |
| 3962 | } |
| 3963 | |
| 3964 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 3965 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 3966 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 3967 | { |
| 3968 | /* Something failed - remove any dictionary |
| 3969 | * entries that did make it. |
| 3970 | */ |
| 3971 | if (!ins_rc[0] && fileObj[0]) |
| 3972 | { |
| 3973 | PyDict_DelItem(_PyPopenProcs, |
| 3974 | fileObj[0]); |
| 3975 | } |
| 3976 | if (!ins_rc[1] && fileObj[1]) |
| 3977 | { |
| 3978 | PyDict_DelItem(_PyPopenProcs, |
| 3979 | fileObj[1]); |
| 3980 | } |
| 3981 | if (!ins_rc[2] && fileObj[2]) |
| 3982 | { |
| 3983 | PyDict_DelItem(_PyPopenProcs, |
| 3984 | fileObj[2]); |
| 3985 | } |
| 3986 | } |
| 3987 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3988 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3989 | /* |
| 3990 | * Clean up our localized references for the dictionary keys |
| 3991 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 3992 | * that got placed in the dictionary. |
| 3993 | */ |
| 3994 | Py_XDECREF(procObj); |
| 3995 | Py_XDECREF(fileObj[0]); |
| 3996 | Py_XDECREF(fileObj[1]); |
| 3997 | Py_XDECREF(fileObj[2]); |
| 3998 | } |
| 3999 | |
| 4000 | /* Child is launched. */ |
| 4001 | return f; |
| 4002 | } |
| 4003 | |
| 4004 | /* |
| 4005 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4006 | * exit code for the child process and return as a result of the close. |
| 4007 | * |
| 4008 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4009 | * input file pointer to information about the process that was |
| 4010 | * originally created by the popen* call that created the file pointer. |
| 4011 | * The dictionary uses the file pointer as a key (with one entry |
| 4012 | * inserted for each file returned by the original popen* call) and a |
| 4013 | * single list object as the value for all files from a single call. |
| 4014 | * The list object contains the Win32 process handle at [0], and a file |
| 4015 | * count at [1], which is initialized to the total number of file |
| 4016 | * handles using that list. |
| 4017 | * |
| 4018 | * This function closes whichever handle it is passed, and decrements |
| 4019 | * the file count in the dictionary for the process handle pointed to |
| 4020 | * by this file. On the last close (when the file count reaches zero), |
| 4021 | * this function will wait for the child process and then return its |
| 4022 | * exit code as the result of the close() operation. This permits the |
| 4023 | * files to be closed in any order - it is always the close() of the |
| 4024 | * final handle that will return the exit code. |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4025 | * |
| 4026 | * NOTE: This function is currently called with the GIL released. |
| 4027 | * hence we use the GILState API to manage our state. |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4028 | */ |
| 4029 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4030 | static int _PyPclose(FILE *file) |
| 4031 | { |
| 4032 | int result; |
| 4033 | int exit_code; |
| 4034 | int pipe_pid; |
| 4035 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 4036 | int file_count; |
| 4037 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4038 | PyGILState_STATE state; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4039 | #endif |
| 4040 | |
| 4041 | /* Close the file handle first, to ensure it can't block the |
| 4042 | * child from exiting if it's the last handle. |
| 4043 | */ |
| 4044 | result = fclose(file); |
| 4045 | |
| 4046 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4047 | state = PyGILState_Ensure(); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4048 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4049 | if (_PyPopenProcs) |
| 4050 | { |
| 4051 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4052 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4053 | fileObj)) != NULL && |
| 4054 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 4055 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 4056 | { |
| 4057 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 4058 | file_count = (int) PyInt_AsLong(intObj); |
| 4059 | |
| 4060 | if (file_count > 1) |
| 4061 | { |
| 4062 | /* Still other files referencing process */ |
| 4063 | file_count--; |
| 4064 | PyList_SetItem(procObj,1, |
| 4065 | PyInt_FromLong((long) file_count)); |
| 4066 | } |
| 4067 | else |
| 4068 | { |
| 4069 | /* Last file for this process */ |
| 4070 | if (result != EOF && |
| 4071 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 4072 | { |
| 4073 | /* extract exit status */ |
| 4074 | if (WIFEXITED(exit_code)) |
| 4075 | { |
| 4076 | result = WEXITSTATUS(exit_code); |
| 4077 | } |
| 4078 | else |
| 4079 | { |
| 4080 | errno = EPIPE; |
| 4081 | result = -1; |
| 4082 | } |
| 4083 | } |
| 4084 | else |
| 4085 | { |
| 4086 | /* Indicate failure - this will cause the file object |
| 4087 | * to raise an I/O error and translate the last |
| 4088 | * error code from errno. We do have a problem with |
| 4089 | * last errors that overlap the normal errno table, |
| 4090 | * but that's a consistent problem with the file object. |
| 4091 | */ |
| 4092 | result = -1; |
| 4093 | } |
| 4094 | } |
| 4095 | |
| 4096 | /* Remove this file pointer from dictionary */ |
| 4097 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4098 | |
| 4099 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 4100 | { |
| 4101 | Py_DECREF(_PyPopenProcs); |
| 4102 | _PyPopenProcs = NULL; |
| 4103 | } |
| 4104 | |
| 4105 | } /* if object retrieval ok */ |
| 4106 | |
| 4107 | Py_XDECREF(fileObj); |
| 4108 | } /* if _PyPopenProcs */ |
| 4109 | |
| 4110 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4111 | PyGILState_Release(state); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4112 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4113 | return result; |
| 4114 | } |
| 4115 | |
| 4116 | #endif /* PYCC_??? */ |
| 4117 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4118 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4119 | |
| 4120 | /* |
| 4121 | * Portable 'popen' replacement for Win32. |
| 4122 | * |
| 4123 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 4124 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4125 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4126 | */ |
| 4127 | |
| 4128 | #include <malloc.h> |
| 4129 | #include <io.h> |
| 4130 | #include <fcntl.h> |
| 4131 | |
| 4132 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 4133 | #define POPEN_1 1 |
| 4134 | #define POPEN_2 2 |
| 4135 | #define POPEN_3 3 |
| 4136 | #define POPEN_4 4 |
| 4137 | |
| 4138 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4139 | static int _PyPclose(FILE *file); |
| 4140 | |
| 4141 | /* |
| 4142 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4143 | * for use when retrieving the process exit code. See _PyPclose() below |
| 4144 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4145 | */ |
| 4146 | static PyObject *_PyPopenProcs = NULL; |
| 4147 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4148 | |
| 4149 | /* popen that works from a GUI. |
| 4150 | * |
| 4151 | * The result of this function is a pipe (file) connected to the |
| 4152 | * processes stdin or stdout, depending on the requested mode. |
| 4153 | */ |
| 4154 | |
| 4155 | static PyObject * |
| 4156 | posix_popen(PyObject *self, PyObject *args) |
| 4157 | { |
Raymond Hettinger | b5cb665 | 2003-09-01 22:25:41 +0000 | [diff] [blame] | 4158 | PyObject *f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4159 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4160 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4161 | char *cmdstring; |
| 4162 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4163 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4164 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4165 | return NULL; |
| 4166 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4167 | if (*mode == 'r') |
| 4168 | tm = _O_RDONLY; |
| 4169 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4170 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4171 | return NULL; |
| 4172 | } else |
| 4173 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4174 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4175 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4176 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4177 | return NULL; |
| 4178 | } |
| 4179 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4180 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4181 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4182 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4183 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4184 | else |
| 4185 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 4186 | |
| 4187 | return f; |
| 4188 | } |
| 4189 | |
| 4190 | /* Variation on win32pipe.popen |
| 4191 | * |
| 4192 | * The result of this function is a pipe (file) connected to the |
| 4193 | * process's stdin, and a pipe connected to the process's stdout. |
| 4194 | */ |
| 4195 | |
| 4196 | static PyObject * |
| 4197 | win32_popen2(PyObject *self, PyObject *args) |
| 4198 | { |
| 4199 | PyObject *f; |
| 4200 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4201 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4202 | char *cmdstring; |
| 4203 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4204 | int bufsize = -1; |
| 4205 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4206 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4207 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4208 | if (*mode == 't') |
| 4209 | tm = _O_TEXT; |
| 4210 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4211 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4212 | return NULL; |
| 4213 | } else |
| 4214 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4215 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4216 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4217 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4218 | return NULL; |
| 4219 | } |
| 4220 | |
| 4221 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4222 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4223 | return f; |
| 4224 | } |
| 4225 | |
| 4226 | /* |
| 4227 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4228 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4229 | * The result of this function is 3 pipes - the process's stdin, |
| 4230 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4231 | */ |
| 4232 | |
| 4233 | static PyObject * |
| 4234 | win32_popen3(PyObject *self, PyObject *args) |
| 4235 | { |
| 4236 | PyObject *f; |
| 4237 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4238 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4239 | char *cmdstring; |
| 4240 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4241 | int bufsize = -1; |
| 4242 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4243 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4244 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4245 | if (*mode == 't') |
| 4246 | tm = _O_TEXT; |
| 4247 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4248 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4249 | return NULL; |
| 4250 | } else |
| 4251 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4252 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4253 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4254 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4255 | return NULL; |
| 4256 | } |
| 4257 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4258 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4259 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4260 | return f; |
| 4261 | } |
| 4262 | |
| 4263 | /* |
| 4264 | * Variation on win32pipe.popen |
| 4265 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4266 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4267 | * and stdout+stderr combined as a single pipe. |
| 4268 | */ |
| 4269 | |
| 4270 | static PyObject * |
| 4271 | win32_popen4(PyObject *self, PyObject *args) |
| 4272 | { |
| 4273 | PyObject *f; |
| 4274 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4275 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4276 | char *cmdstring; |
| 4277 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4278 | int bufsize = -1; |
| 4279 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4280 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4281 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4282 | if (*mode == 't') |
| 4283 | tm = _O_TEXT; |
| 4284 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4285 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4286 | return NULL; |
| 4287 | } else |
| 4288 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4289 | |
| 4290 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4291 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4292 | return NULL; |
| 4293 | } |
| 4294 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4295 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4296 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4297 | return f; |
| 4298 | } |
| 4299 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4300 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4301 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4302 | HANDLE hStdin, |
| 4303 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4304 | HANDLE hStderr, |
| 4305 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4306 | { |
| 4307 | PROCESS_INFORMATION piProcInfo; |
| 4308 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4309 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4310 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4311 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4312 | int i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4313 | Py_ssize_t x; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4314 | |
| 4315 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4316 | char *comshell; |
| 4317 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4318 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4319 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4320 | /* x < i, so x fits into an integer */ |
| 4321 | return (int)x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4322 | |
| 4323 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 4324 | * then use the w9xpopen hack. |
| 4325 | */ |
| 4326 | comshell = s1 + x; |
| 4327 | while (comshell >= s1 && *comshell != '\\') |
| 4328 | --comshell; |
| 4329 | ++comshell; |
| 4330 | |
| 4331 | if (GetVersion() < 0x80000000 && |
| 4332 | _stricmp(comshell, "command.com") != 0) { |
| 4333 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4334 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4335 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4336 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4337 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4338 | } |
| 4339 | else { |
| 4340 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4341 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 4342 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4343 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4344 | char modulepath[_MAX_PATH]; |
| 4345 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4346 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 4347 | for (i = x = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4348 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4349 | x = i+1; |
| 4350 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4351 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4352 | strncat(modulepath, |
| 4353 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4354 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4355 | -strlen(modulepath)); |
| 4356 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4357 | /* Eeek - file-not-found - possibly an embedding |
| 4358 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4359 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4360 | strncpy(modulepath, |
| 4361 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4362 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 4363 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 4364 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4365 | strncat(modulepath, |
| 4366 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4367 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4368 | -strlen(modulepath)); |
| 4369 | /* No where else to look - raise an easily identifiable |
| 4370 | error, rather than leaving Windows to report |
| 4371 | "file not found" - as the user is probably blissfully |
| 4372 | unaware this shim EXE is used, and it will confuse them. |
| 4373 | (well, it confused me for a while ;-) |
| 4374 | */ |
| 4375 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4376 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4377 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4378 | "for popen to work with your shell " |
| 4379 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4380 | szConsoleSpawn); |
| 4381 | return FALSE; |
| 4382 | } |
| 4383 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4384 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4385 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4386 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4387 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4388 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4389 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4390 | /* To maintain correct argument passing semantics, |
| 4391 | we pass the command-line as it stands, and allow |
| 4392 | quoting to be applied. w9xpopen.exe will then |
| 4393 | use its argv vector, and re-quote the necessary |
| 4394 | args for the ultimate child process. |
| 4395 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4396 | PyOS_snprintf( |
| 4397 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4398 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4399 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4400 | s1, |
| 4401 | s3, |
| 4402 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4403 | /* Not passing CREATE_NEW_CONSOLE has been known to |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4404 | cause random failures on win9x. Specifically a |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4405 | dialog: |
| 4406 | "Your program accessed mem currently in use at xxx" |
| 4407 | and a hopeful warning about the stability of your |
| 4408 | system. |
| 4409 | Cost is Ctrl+C wont kill children, but anyone |
| 4410 | who cares can have a go! |
| 4411 | */ |
| 4412 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4413 | } |
| 4414 | } |
| 4415 | |
| 4416 | /* Could be an else here to try cmd.exe / command.com in the path |
| 4417 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4418 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4419 | PyErr_SetString(PyExc_RuntimeError, |
| 4420 | "Cannot locate a COMSPEC environment variable to " |
| 4421 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4422 | return FALSE; |
| 4423 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4424 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4425 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 4426 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 4427 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 4428 | siStartInfo.hStdInput = hStdin; |
| 4429 | siStartInfo.hStdOutput = hStdout; |
| 4430 | siStartInfo.hStdError = hStderr; |
| 4431 | siStartInfo.wShowWindow = SW_HIDE; |
| 4432 | |
| 4433 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4434 | s2, |
| 4435 | NULL, |
| 4436 | NULL, |
| 4437 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4438 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4439 | NULL, |
| 4440 | NULL, |
| 4441 | &siStartInfo, |
| 4442 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4443 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4444 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4445 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4446 | /* Return process handle */ |
| 4447 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4448 | return TRUE; |
| 4449 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4450 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4451 | return FALSE; |
| 4452 | } |
| 4453 | |
| 4454 | /* The following code is based off of KB: Q190351 */ |
| 4455 | |
| 4456 | static PyObject * |
| 4457 | _PyPopen(char *cmdstring, int mode, int n) |
| 4458 | { |
| 4459 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 4460 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4461 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4462 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4463 | SECURITY_ATTRIBUTES saAttr; |
| 4464 | BOOL fSuccess; |
| 4465 | int fd1, fd2, fd3; |
| 4466 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4467 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4468 | PyObject *f; |
| 4469 | |
| 4470 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 4471 | saAttr.bInheritHandle = TRUE; |
| 4472 | saAttr.lpSecurityDescriptor = NULL; |
| 4473 | |
| 4474 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 4475 | return win32_error("CreatePipe", NULL); |
| 4476 | |
| 4477 | /* Create new output read handle and the input write handle. Set |
| 4478 | * the inheritance properties to FALSE. Otherwise, the child inherits |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 4479 | * these handles; resulting in non-closeable handles to the pipes |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4480 | * being created. */ |
| 4481 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4482 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 4483 | FALSE, |
| 4484 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4485 | if (!fSuccess) |
| 4486 | return win32_error("DuplicateHandle", NULL); |
| 4487 | |
| 4488 | /* Close the inheritable version of ChildStdin |
| 4489 | that we're using. */ |
| 4490 | CloseHandle(hChildStdinWr); |
| 4491 | |
| 4492 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 4493 | return win32_error("CreatePipe", NULL); |
| 4494 | |
| 4495 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4496 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 4497 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4498 | if (!fSuccess) |
| 4499 | return win32_error("DuplicateHandle", NULL); |
| 4500 | |
| 4501 | /* Close the inheritable version of ChildStdout |
| 4502 | that we're using. */ |
| 4503 | CloseHandle(hChildStdoutRd); |
| 4504 | |
| 4505 | if (n != POPEN_4) { |
| 4506 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 4507 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4508 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 4509 | hChildStderrRd, |
| 4510 | GetCurrentProcess(), |
| 4511 | &hChildStderrRdDup, 0, |
| 4512 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4513 | if (!fSuccess) |
| 4514 | return win32_error("DuplicateHandle", NULL); |
| 4515 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 4516 | CloseHandle(hChildStderrRd); |
| 4517 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4518 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4519 | switch (n) { |
| 4520 | case POPEN_1: |
| 4521 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 4522 | case _O_WRONLY | _O_TEXT: |
| 4523 | /* Case for writing to child Stdin in text mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4524 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4525 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4526 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4527 | PyFile_SetBufSize(f, 0); |
| 4528 | /* We don't care about these pipes anymore, so close them. */ |
| 4529 | CloseHandle(hChildStdoutRdDup); |
| 4530 | CloseHandle(hChildStderrRdDup); |
| 4531 | break; |
| 4532 | |
| 4533 | case _O_RDONLY | _O_TEXT: |
| 4534 | /* Case for reading from child Stdout in text mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4535 | fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4536 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4537 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4538 | PyFile_SetBufSize(f, 0); |
| 4539 | /* We don't care about these pipes anymore, so close them. */ |
| 4540 | CloseHandle(hChildStdinWrDup); |
| 4541 | CloseHandle(hChildStderrRdDup); |
| 4542 | break; |
| 4543 | |
| 4544 | case _O_RDONLY | _O_BINARY: |
| 4545 | /* Case for readinig from child Stdout in binary mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4546 | fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4547 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4548 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4549 | PyFile_SetBufSize(f, 0); |
| 4550 | /* We don't care about these pipes anymore, so close them. */ |
| 4551 | CloseHandle(hChildStdinWrDup); |
| 4552 | CloseHandle(hChildStderrRdDup); |
| 4553 | break; |
| 4554 | |
| 4555 | case _O_WRONLY | _O_BINARY: |
| 4556 | /* Case for writing to child Stdin in binary mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4557 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4558 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4559 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4560 | PyFile_SetBufSize(f, 0); |
| 4561 | /* We don't care about these pipes anymore, so close them. */ |
| 4562 | CloseHandle(hChildStdoutRdDup); |
| 4563 | CloseHandle(hChildStderrRdDup); |
| 4564 | break; |
| 4565 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4566 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4567 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4568 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4569 | case POPEN_2: |
| 4570 | case POPEN_4: |
| 4571 | { |
| 4572 | char *m1, *m2; |
| 4573 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4574 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4575 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4576 | m1 = "r"; |
| 4577 | m2 = "w"; |
| 4578 | } else { |
| 4579 | m1 = "rb"; |
| 4580 | m2 = "wb"; |
| 4581 | } |
| 4582 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4583 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4584 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4585 | fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4586 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4587 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4588 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4589 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4590 | PyFile_SetBufSize(p2, 0); |
| 4591 | |
| 4592 | if (n != 4) |
| 4593 | CloseHandle(hChildStderrRdDup); |
| 4594 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4595 | f = PyTuple_Pack(2,p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4596 | Py_XDECREF(p1); |
| 4597 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4598 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4599 | break; |
| 4600 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4601 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4602 | case POPEN_3: |
| 4603 | { |
| 4604 | char *m1, *m2; |
| 4605 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4606 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4607 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4608 | m1 = "r"; |
| 4609 | m2 = "w"; |
| 4610 | } else { |
| 4611 | m1 = "rb"; |
| 4612 | m2 = "wb"; |
| 4613 | } |
| 4614 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4615 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4616 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4617 | fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4618 | f2 = _fdopen(fd2, m1); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4619 | fd3 = _open_osfhandle((intptr_t)hChildStderrRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4620 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4621 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4622 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 4623 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4624 | PyFile_SetBufSize(p1, 0); |
| 4625 | PyFile_SetBufSize(p2, 0); |
| 4626 | PyFile_SetBufSize(p3, 0); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4627 | f = PyTuple_Pack(3,p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4628 | Py_XDECREF(p1); |
| 4629 | Py_XDECREF(p2); |
| 4630 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4631 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4632 | break; |
| 4633 | } |
| 4634 | } |
| 4635 | |
| 4636 | if (n == POPEN_4) { |
| 4637 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4638 | hChildStdinRd, |
| 4639 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4640 | hChildStdoutWr, |
| 4641 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4642 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4643 | } |
| 4644 | else { |
| 4645 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4646 | hChildStdinRd, |
| 4647 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4648 | hChildStderrWr, |
| 4649 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4650 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4651 | } |
| 4652 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4653 | /* |
| 4654 | * Insert the files we've created into the process dictionary |
| 4655 | * all referencing the list with the process handle and the |
| 4656 | * initial number of files (see description below in _PyPclose). |
| 4657 | * Since if _PyPclose later tried to wait on a process when all |
| 4658 | * handles weren't closed, it could create a deadlock with the |
| 4659 | * child, we spend some energy here to try to ensure that we |
| 4660 | * either insert all file handles into the dictionary or none |
| 4661 | * at all. It's a little clumsy with the various popen modes |
| 4662 | * and variable number of files involved. |
| 4663 | */ |
| 4664 | if (!_PyPopenProcs) { |
| 4665 | _PyPopenProcs = PyDict_New(); |
| 4666 | } |
| 4667 | |
| 4668 | if (_PyPopenProcs) { |
| 4669 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 4670 | int ins_rc[3]; |
| 4671 | |
| 4672 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4673 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4674 | |
| 4675 | procObj = PyList_New(2); |
| 4676 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 4677 | intObj = PyInt_FromLong(file_count); |
| 4678 | |
| 4679 | if (procObj && hProcessObj && intObj) { |
| 4680 | PyList_SetItem(procObj,0,hProcessObj); |
| 4681 | PyList_SetItem(procObj,1,intObj); |
| 4682 | |
| 4683 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 4684 | if (fileObj[0]) { |
| 4685 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4686 | fileObj[0], |
| 4687 | procObj); |
| 4688 | } |
| 4689 | if (file_count >= 2) { |
| 4690 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 4691 | if (fileObj[1]) { |
| 4692 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4693 | fileObj[1], |
| 4694 | procObj); |
| 4695 | } |
| 4696 | } |
| 4697 | if (file_count >= 3) { |
| 4698 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 4699 | if (fileObj[2]) { |
| 4700 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4701 | fileObj[2], |
| 4702 | procObj); |
| 4703 | } |
| 4704 | } |
| 4705 | |
| 4706 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4707 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4708 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 4709 | /* Something failed - remove any dictionary |
| 4710 | * entries that did make it. |
| 4711 | */ |
| 4712 | if (!ins_rc[0] && fileObj[0]) { |
| 4713 | PyDict_DelItem(_PyPopenProcs, |
| 4714 | fileObj[0]); |
| 4715 | } |
| 4716 | if (!ins_rc[1] && fileObj[1]) { |
| 4717 | PyDict_DelItem(_PyPopenProcs, |
| 4718 | fileObj[1]); |
| 4719 | } |
| 4720 | if (!ins_rc[2] && fileObj[2]) { |
| 4721 | PyDict_DelItem(_PyPopenProcs, |
| 4722 | fileObj[2]); |
| 4723 | } |
| 4724 | } |
| 4725 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4726 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4727 | /* |
| 4728 | * Clean up our localized references for the dictionary keys |
| 4729 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4730 | * that got placed in the dictionary. |
| 4731 | */ |
| 4732 | Py_XDECREF(procObj); |
| 4733 | Py_XDECREF(fileObj[0]); |
| 4734 | Py_XDECREF(fileObj[1]); |
| 4735 | Py_XDECREF(fileObj[2]); |
| 4736 | } |
| 4737 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4738 | /* Child is launched. Close the parents copy of those pipe |
| 4739 | * handles that only the child should have open. You need to |
| 4740 | * make sure that no handles to the write end of the output pipe |
| 4741 | * are maintained in this process or else the pipe will not close |
| 4742 | * when the child process exits and the ReadFile will hang. */ |
| 4743 | |
| 4744 | if (!CloseHandle(hChildStdinRd)) |
| 4745 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4746 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4747 | if (!CloseHandle(hChildStdoutWr)) |
| 4748 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4749 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4750 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 4751 | return win32_error("CloseHandle", NULL); |
| 4752 | |
| 4753 | return f; |
| 4754 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4755 | |
| 4756 | /* |
| 4757 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4758 | * 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] | 4759 | * |
| 4760 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4761 | * input file pointer to information about the process that was |
| 4762 | * originally created by the popen* call that created the file pointer. |
| 4763 | * The dictionary uses the file pointer as a key (with one entry |
| 4764 | * inserted for each file returned by the original popen* call) and a |
| 4765 | * single list object as the value for all files from a single call. |
| 4766 | * The list object contains the Win32 process handle at [0], and a file |
| 4767 | * count at [1], which is initialized to the total number of file |
| 4768 | * handles using that list. |
| 4769 | * |
| 4770 | * This function closes whichever handle it is passed, and decrements |
| 4771 | * the file count in the dictionary for the process handle pointed to |
| 4772 | * by this file. On the last close (when the file count reaches zero), |
| 4773 | * this function will wait for the child process and then return its |
| 4774 | * exit code as the result of the close() operation. This permits the |
| 4775 | * files to be closed in any order - it is always the close() of the |
| 4776 | * final handle that will return the exit code. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4777 | * |
| 4778 | * NOTE: This function is currently called with the GIL released. |
| 4779 | * hence we use the GILState API to manage our state. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4780 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4781 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4782 | static int _PyPclose(FILE *file) |
| 4783 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4784 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4785 | DWORD exit_code; |
| 4786 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4787 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 4788 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4789 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4790 | PyGILState_STATE state; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4791 | #endif |
| 4792 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4793 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4794 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4795 | */ |
| 4796 | result = fclose(file); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4797 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4798 | state = PyGILState_Ensure(); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4799 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4800 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4801 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4802 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4803 | fileObj)) != NULL && |
| 4804 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 4805 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 4806 | |
| 4807 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 4808 | file_count = PyInt_AsLong(intObj); |
| 4809 | |
| 4810 | if (file_count > 1) { |
| 4811 | /* Still other files referencing process */ |
| 4812 | file_count--; |
| 4813 | PyList_SetItem(procObj,1, |
| 4814 | PyInt_FromLong(file_count)); |
| 4815 | } else { |
| 4816 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4817 | if (result != EOF && |
| 4818 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 4819 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4820 | /* Possible truncation here in 16-bit environments, but |
| 4821 | * real exit codes are just the lower byte in any event. |
| 4822 | */ |
| 4823 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4824 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4825 | /* Indicate failure - this will cause the file object |
| 4826 | * to raise an I/O error and translate the last Win32 |
| 4827 | * error code from errno. We do have a problem with |
| 4828 | * last errors that overlap the normal errno table, |
| 4829 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4830 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4831 | if (result != EOF) { |
| 4832 | /* If the error wasn't from the fclose(), then |
| 4833 | * set errno for the file object error handling. |
| 4834 | */ |
| 4835 | errno = GetLastError(); |
| 4836 | } |
| 4837 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4838 | } |
| 4839 | |
| 4840 | /* Free up the native handle at this point */ |
| 4841 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4842 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4843 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4844 | /* Remove this file pointer from dictionary */ |
| 4845 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4846 | |
| 4847 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 4848 | Py_DECREF(_PyPopenProcs); |
| 4849 | _PyPopenProcs = NULL; |
| 4850 | } |
| 4851 | |
| 4852 | } /* if object retrieval ok */ |
| 4853 | |
| 4854 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4855 | } /* if _PyPopenProcs */ |
| 4856 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4857 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4858 | PyGILState_Release(state); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4859 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4860 | return result; |
| 4861 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4862 | |
| 4863 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4864 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4865 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4866 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4867 | char *name; |
| 4868 | char *mode = "r"; |
| 4869 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4870 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4871 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4872 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4873 | return NULL; |
Martin v. Löwis | 9ad853b | 2003-10-31 10:01:53 +0000 | [diff] [blame] | 4874 | /* Strip mode of binary or text modifiers */ |
| 4875 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) |
| 4876 | mode = "r"; |
| 4877 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) |
| 4878 | mode = "w"; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4879 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4880 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4881 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4882 | if (fp == NULL) |
| 4883 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4884 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4885 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4886 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4887 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4888 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4889 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4890 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4891 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4892 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4893 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4894 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4895 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4896 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4897 | Set the current process's user id."); |
| 4898 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4899 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4900 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4901 | { |
| 4902 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4903 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4904 | return NULL; |
| 4905 | if (setuid(uid) < 0) |
| 4906 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4907 | Py_INCREF(Py_None); |
| 4908 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4909 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4910 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4911 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4912 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4913 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4914 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4915 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4916 | Set the current process's effective user id."); |
| 4917 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4918 | static PyObject * |
| 4919 | posix_seteuid (PyObject *self, PyObject *args) |
| 4920 | { |
| 4921 | int euid; |
| 4922 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 4923 | return NULL; |
| 4924 | } else if (seteuid(euid) < 0) { |
| 4925 | return posix_error(); |
| 4926 | } else { |
| 4927 | Py_INCREF(Py_None); |
| 4928 | return Py_None; |
| 4929 | } |
| 4930 | } |
| 4931 | #endif /* HAVE_SETEUID */ |
| 4932 | |
| 4933 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4934 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4935 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4936 | Set the current process's effective group id."); |
| 4937 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4938 | static PyObject * |
| 4939 | posix_setegid (PyObject *self, PyObject *args) |
| 4940 | { |
| 4941 | int egid; |
| 4942 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 4943 | return NULL; |
| 4944 | } else if (setegid(egid) < 0) { |
| 4945 | return posix_error(); |
| 4946 | } else { |
| 4947 | Py_INCREF(Py_None); |
| 4948 | return Py_None; |
| 4949 | } |
| 4950 | } |
| 4951 | #endif /* HAVE_SETEGID */ |
| 4952 | |
| 4953 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4954 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4955 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4956 | Set the current process's real and effective user ids."); |
| 4957 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4958 | static PyObject * |
| 4959 | posix_setreuid (PyObject *self, PyObject *args) |
| 4960 | { |
| 4961 | int ruid, euid; |
| 4962 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4963 | return NULL; |
| 4964 | } else if (setreuid(ruid, euid) < 0) { |
| 4965 | return posix_error(); |
| 4966 | } else { |
| 4967 | Py_INCREF(Py_None); |
| 4968 | return Py_None; |
| 4969 | } |
| 4970 | } |
| 4971 | #endif /* HAVE_SETREUID */ |
| 4972 | |
| 4973 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4974 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4975 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4976 | Set the current process's real and effective group ids."); |
| 4977 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4978 | static PyObject * |
| 4979 | posix_setregid (PyObject *self, PyObject *args) |
| 4980 | { |
| 4981 | int rgid, egid; |
| 4982 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 4983 | return NULL; |
| 4984 | } else if (setregid(rgid, egid) < 0) { |
| 4985 | return posix_error(); |
| 4986 | } else { |
| 4987 | Py_INCREF(Py_None); |
| 4988 | return Py_None; |
| 4989 | } |
| 4990 | } |
| 4991 | #endif /* HAVE_SETREGID */ |
| 4992 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4993 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4994 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4995 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4996 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4997 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4998 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4999 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5000 | { |
| 5001 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5002 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5003 | return NULL; |
| 5004 | if (setgid(gid) < 0) |
| 5005 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5006 | Py_INCREF(Py_None); |
| 5007 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5008 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5009 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5010 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5011 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5012 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5013 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5014 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5015 | |
| 5016 | static PyObject * |
| 5017 | posix_setgroups(PyObject *self, PyObject *args) |
| 5018 | { |
| 5019 | PyObject *groups; |
| 5020 | int i, len; |
| 5021 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5022 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5023 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 5024 | return NULL; |
| 5025 | if (!PySequence_Check(groups)) { |
| 5026 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 5027 | return NULL; |
| 5028 | } |
| 5029 | len = PySequence_Size(groups); |
| 5030 | if (len > MAX_GROUPS) { |
| 5031 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 5032 | return NULL; |
| 5033 | } |
| 5034 | for(i = 0; i < len; i++) { |
| 5035 | PyObject *elem; |
| 5036 | elem = PySequence_GetItem(groups, i); |
| 5037 | if (!elem) |
| 5038 | return NULL; |
| 5039 | if (!PyInt_Check(elem)) { |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 5040 | if (!PyLong_Check(elem)) { |
| 5041 | PyErr_SetString(PyExc_TypeError, |
| 5042 | "groups must be integers"); |
| 5043 | Py_DECREF(elem); |
| 5044 | return NULL; |
| 5045 | } else { |
| 5046 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 5047 | if (PyErr_Occurred()) { |
| 5048 | PyErr_SetString(PyExc_TypeError, |
| 5049 | "group id too big"); |
| 5050 | Py_DECREF(elem); |
| 5051 | return NULL; |
| 5052 | } |
| 5053 | grouplist[i] = x; |
| 5054 | /* read back the value to see if it fitted in gid_t */ |
| 5055 | if (grouplist[i] != x) { |
| 5056 | PyErr_SetString(PyExc_TypeError, |
| 5057 | "group id too big"); |
| 5058 | Py_DECREF(elem); |
| 5059 | return NULL; |
| 5060 | } |
| 5061 | } |
| 5062 | } else { |
| 5063 | long x = PyInt_AsLong(elem); |
| 5064 | grouplist[i] = x; |
| 5065 | if (grouplist[i] != x) { |
| 5066 | PyErr_SetString(PyExc_TypeError, |
| 5067 | "group id too big"); |
| 5068 | Py_DECREF(elem); |
| 5069 | return NULL; |
| 5070 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5071 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5072 | Py_DECREF(elem); |
| 5073 | } |
| 5074 | |
| 5075 | if (setgroups(len, grouplist) < 0) |
| 5076 | return posix_error(); |
| 5077 | Py_INCREF(Py_None); |
| 5078 | return Py_None; |
| 5079 | } |
| 5080 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5081 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5082 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5083 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5084 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5085 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5086 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5087 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5088 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5089 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5090 | int pid, options; |
| 5091 | #ifdef UNION_WAIT |
| 5092 | union wait status; |
| 5093 | #define status_i (status.w_status) |
| 5094 | #else |
| 5095 | int status; |
| 5096 | #define status_i status |
| 5097 | #endif |
| 5098 | status_i = 0; |
| 5099 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5100 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5101 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5102 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 5103 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5104 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5105 | if (pid == -1) |
| 5106 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5107 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5108 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5111 | #elif defined(HAVE_CWAIT) |
| 5112 | |
| 5113 | /* 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] | 5114 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5115 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5116 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5117 | |
| 5118 | static PyObject * |
| 5119 | posix_waitpid(PyObject *self, PyObject *args) |
| 5120 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5121 | intptr_t pid; |
| 5122 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5123 | |
| 5124 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 5125 | return NULL; |
| 5126 | Py_BEGIN_ALLOW_THREADS |
| 5127 | pid = _cwait(&status, pid, options); |
| 5128 | Py_END_ALLOW_THREADS |
| 5129 | if (pid == -1) |
| 5130 | return posix_error(); |
| 5131 | else |
| 5132 | /* shift the status left a byte so this is more like the |
| 5133 | POSIX waitpid */ |
| 5134 | return Py_BuildValue("ii", pid, status << 8); |
| 5135 | } |
| 5136 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5137 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5138 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5139 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5140 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5141 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5142 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5143 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5144 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5145 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5146 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5147 | #ifdef UNION_WAIT |
| 5148 | union wait status; |
| 5149 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 5150 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5151 | int status; |
| 5152 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 5153 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5154 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5155 | status_i = 0; |
| 5156 | Py_BEGIN_ALLOW_THREADS |
| 5157 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5158 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5159 | if (pid == -1) |
| 5160 | return posix_error(); |
| 5161 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5162 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5163 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5164 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5165 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5166 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5167 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5168 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5169 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5170 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5171 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5172 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5173 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5174 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5175 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5176 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5177 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5178 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5179 | 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] | 5180 | #else |
| 5181 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 5182 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5183 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5184 | } |
| 5185 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5186 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5187 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5188 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5189 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5190 | 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] | 5191 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5192 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5193 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5194 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5195 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 5196 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5197 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5198 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5199 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5200 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 5201 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5202 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5203 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 5204 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5205 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5206 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5207 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5208 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5209 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5210 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5211 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5212 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 5213 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5214 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5215 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5216 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5217 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5218 | return posix_2str(args, "etet:symlink", symlink, NULL, NULL); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5219 | } |
| 5220 | #endif /* HAVE_SYMLINK */ |
| 5221 | |
| 5222 | |
| 5223 | #ifdef HAVE_TIMES |
| 5224 | #ifndef HZ |
| 5225 | #define HZ 60 /* Universal constant :-) */ |
| 5226 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5227 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5228 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 5229 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5230 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5231 | { |
| 5232 | ULONG value = 0; |
| 5233 | |
| 5234 | Py_BEGIN_ALLOW_THREADS |
| 5235 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 5236 | Py_END_ALLOW_THREADS |
| 5237 | |
| 5238 | return value; |
| 5239 | } |
| 5240 | |
| 5241 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5242 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5243 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5244 | /* Currently Only Uptime is Provided -- Others Later */ |
| 5245 | return Py_BuildValue("ddddd", |
| 5246 | (double)0 /* t.tms_utime / HZ */, |
| 5247 | (double)0 /* t.tms_stime / HZ */, |
| 5248 | (double)0 /* t.tms_cutime / HZ */, |
| 5249 | (double)0 /* t.tms_cstime / HZ */, |
| 5250 | (double)system_uptime() / 1000); |
| 5251 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5252 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5253 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5254 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5255 | { |
| 5256 | struct tms t; |
| 5257 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5258 | errno = 0; |
| 5259 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5260 | if (c == (clock_t) -1) |
| 5261 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5262 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5263 | (double)t.tms_utime / HZ, |
| 5264 | (double)t.tms_stime / HZ, |
| 5265 | (double)t.tms_cutime / HZ, |
| 5266 | (double)t.tms_cstime / HZ, |
| 5267 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5268 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5269 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5270 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5271 | |
| 5272 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5273 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5274 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5275 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5276 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5277 | { |
| 5278 | FILETIME create, exit, kernel, user; |
| 5279 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5280 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5281 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 5282 | /* The fields of a FILETIME structure are the hi and lo part |
| 5283 | of a 64-bit value expressed in 100 nanosecond units. |
| 5284 | 1e7 is one second in such units; 1e-7 the inverse. |
| 5285 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 5286 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5287 | return Py_BuildValue( |
| 5288 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5289 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 5290 | kernel.dwLowDateTime*1e-7), |
| 5291 | (double)(user.dwHighDateTime*429.4967296 + |
| 5292 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5293 | (double)0, |
| 5294 | (double)0, |
| 5295 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5296 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5297 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5298 | |
| 5299 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5300 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5301 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5302 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5303 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5304 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5305 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5306 | #ifdef HAVE_GETSID |
| 5307 | PyDoc_STRVAR(posix_getsid__doc__, |
| 5308 | "getsid(pid) -> sid\n\n\ |
| 5309 | Call the system call getsid()."); |
| 5310 | |
| 5311 | static PyObject * |
| 5312 | posix_getsid(PyObject *self, PyObject *args) |
| 5313 | { |
| 5314 | int pid, sid; |
| 5315 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 5316 | return NULL; |
| 5317 | sid = getsid(pid); |
| 5318 | if (sid < 0) |
| 5319 | return posix_error(); |
| 5320 | return PyInt_FromLong((long)sid); |
| 5321 | } |
| 5322 | #endif /* HAVE_GETSID */ |
| 5323 | |
| 5324 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5325 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5326 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5327 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5328 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5329 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5330 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5331 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5332 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5333 | if (setsid() < 0) |
| 5334 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5335 | Py_INCREF(Py_None); |
| 5336 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5337 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5338 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5339 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5340 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5341 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5342 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5343 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5344 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5345 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5346 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5347 | { |
| 5348 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5349 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5350 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5351 | if (setpgid(pid, pgrp) < 0) |
| 5352 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5353 | Py_INCREF(Py_None); |
| 5354 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5355 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5356 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5357 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5358 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5359 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5360 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5361 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5362 | 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] | 5363 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5364 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5365 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5366 | { |
| 5367 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5368 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5369 | return NULL; |
| 5370 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5371 | if (pgid < 0) |
| 5372 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5373 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5374 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5375 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5376 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5377 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5378 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5379 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5380 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5381 | 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] | 5382 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5383 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5384 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5385 | { |
| 5386 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5387 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5388 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5389 | if (tcsetpgrp(fd, pgid) < 0) |
| 5390 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5391 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5392 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5393 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5394 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5395 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5396 | /* Functions acting on file descriptors */ |
| 5397 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5398 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5399 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5400 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5401 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5402 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5403 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5404 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5405 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5406 | int flag; |
| 5407 | int mode = 0777; |
| 5408 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5409 | |
| 5410 | #ifdef MS_WINDOWS |
| 5411 | if (unicode_file_names()) { |
| 5412 | PyUnicodeObject *po; |
| 5413 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 5414 | Py_BEGIN_ALLOW_THREADS |
| 5415 | /* PyUnicode_AS_UNICODE OK without thread |
| 5416 | lock as it is a simple dereference. */ |
| 5417 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 5418 | Py_END_ALLOW_THREADS |
| 5419 | if (fd < 0) |
| 5420 | return posix_error(); |
| 5421 | return PyInt_FromLong((long)fd); |
| 5422 | } |
| 5423 | /* Drop the argument parsing error as narrow strings |
| 5424 | are also valid. */ |
| 5425 | PyErr_Clear(); |
| 5426 | } |
| 5427 | #endif |
| 5428 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5429 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5430 | Py_FileSystemDefaultEncoding, &file, |
| 5431 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5432 | return NULL; |
| 5433 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5434 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5435 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5436 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5437 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5438 | return posix_error_with_allocated_filename(file); |
| 5439 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5440 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5441 | } |
| 5442 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5443 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5444 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5445 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5446 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5447 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5448 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5449 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5450 | { |
| 5451 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5452 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5453 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5454 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5455 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5456 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5457 | if (res < 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 | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5461 | } |
| 5462 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5463 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5464 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5465 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5466 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5467 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5468 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5469 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5470 | { |
| 5471 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5472 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5473 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5474 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5475 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5476 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5477 | if (fd < 0) |
| 5478 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5479 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5480 | } |
| 5481 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5482 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5483 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5484 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5485 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5486 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5487 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5488 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5489 | { |
| 5490 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5491 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5492 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5493 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5494 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5495 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5496 | if (res < 0) |
| 5497 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5498 | Py_INCREF(Py_None); |
| 5499 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5500 | } |
| 5501 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5502 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5503 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5504 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5505 | Set the current position of a file descriptor."); |
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_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5509 | { |
| 5510 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5511 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5512 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5513 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5514 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5515 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5516 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5517 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5518 | return NULL; |
| 5519 | #ifdef SEEK_SET |
| 5520 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5521 | switch (how) { |
| 5522 | case 0: how = SEEK_SET; break; |
| 5523 | case 1: how = SEEK_CUR; break; |
| 5524 | case 2: how = SEEK_END; break; |
| 5525 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5526 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5527 | |
| 5528 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5529 | pos = PyInt_AsLong(posobj); |
| 5530 | #else |
| 5531 | pos = PyLong_Check(posobj) ? |
| 5532 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 5533 | #endif |
| 5534 | if (PyErr_Occurred()) |
| 5535 | return NULL; |
| 5536 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5537 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5538 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5539 | res = _lseeki64(fd, pos, how); |
| 5540 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5541 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5542 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5543 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5544 | if (res < 0) |
| 5545 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5546 | |
| 5547 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5548 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5549 | #else |
| 5550 | return PyLong_FromLongLong(res); |
| 5551 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5552 | } |
| 5553 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5554 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5555 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5556 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5557 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5558 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5559 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5560 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5561 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5562 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5563 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5564 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5565 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 5566 | if (size < 0) { |
| 5567 | errno = EINVAL; |
| 5568 | return posix_error(); |
| 5569 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5570 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5571 | if (buffer == NULL) |
| 5572 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5573 | Py_BEGIN_ALLOW_THREADS |
| 5574 | n = read(fd, PyString_AsString(buffer), size); |
| 5575 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5576 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5577 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5578 | return posix_error(); |
| 5579 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5580 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5581 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5582 | return buffer; |
| 5583 | } |
| 5584 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5585 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5586 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5587 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5588 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5589 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5590 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5591 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5592 | { |
| 5593 | int fd, size; |
| 5594 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5595 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5596 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5597 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5598 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5599 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5600 | if (size < 0) |
| 5601 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5602 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5605 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5606 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5607 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5608 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5609 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5610 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5611 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5612 | { |
| 5613 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5614 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5615 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5616 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5617 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5618 | #ifdef __VMS |
| 5619 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5620 | fsync(fd); |
| 5621 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5622 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5623 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5624 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5625 | if (res != 0) { |
| 5626 | #ifdef MS_WINDOWS |
| 5627 | return win32_error("fstat", NULL); |
| 5628 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5629 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5630 | #endif |
| 5631 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5632 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5633 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5634 | } |
| 5635 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5636 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5637 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5638 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5639 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5640 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5641 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5642 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5643 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5644 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5645 | char *mode = "r"; |
| 5646 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5647 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5648 | PyObject *f; |
| 5649 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5650 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5651 | |
Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 5652 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 5653 | PyErr_Format(PyExc_ValueError, |
| 5654 | "invalid file mode '%s'", mode); |
| 5655 | return NULL; |
| 5656 | } |
| 5657 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5658 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5659 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5660 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5661 | if (fp == NULL) |
| 5662 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 5663 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5664 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5665 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5666 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5667 | } |
| 5668 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5669 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5670 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5671 | 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] | 5672 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5673 | |
| 5674 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5675 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5676 | { |
| 5677 | int fd; |
| 5678 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5679 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5680 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5681 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5682 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5683 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5684 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5685 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5686 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5687 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5688 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5689 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5690 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5691 | #if defined(PYOS_OS2) |
| 5692 | HFILE read, write; |
| 5693 | APIRET rc; |
| 5694 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5695 | Py_BEGIN_ALLOW_THREADS |
| 5696 | rc = DosCreatePipe( &read, &write, 4096); |
| 5697 | Py_END_ALLOW_THREADS |
| 5698 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5699 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5700 | |
| 5701 | return Py_BuildValue("(ii)", read, write); |
| 5702 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5703 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5704 | int fds[2]; |
| 5705 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5706 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5707 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5708 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5709 | if (res != 0) |
| 5710 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5711 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5712 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5713 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5714 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5715 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5716 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5717 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5718 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5719 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5720 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5721 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5722 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5723 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5724 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5725 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5726 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5727 | #endif /* HAVE_PIPE */ |
| 5728 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5729 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5730 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5731 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5732 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5733 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5734 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5735 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5736 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5737 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5738 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5739 | int mode = 0666; |
| 5740 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5741 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5742 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5743 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5744 | res = mkfifo(filename, mode); |
| 5745 | Py_END_ALLOW_THREADS |
| 5746 | if (res < 0) |
| 5747 | return posix_error(); |
| 5748 | Py_INCREF(Py_None); |
| 5749 | return Py_None; |
| 5750 | } |
| 5751 | #endif |
| 5752 | |
| 5753 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5754 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5755 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5756 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5757 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5758 | named filename. mode specifies both the permissions to use and the\n\ |
| 5759 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5760 | 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] | 5761 | device defines the newly created device special file (probably using\n\ |
| 5762 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5763 | |
| 5764 | |
| 5765 | static PyObject * |
| 5766 | posix_mknod(PyObject *self, PyObject *args) |
| 5767 | { |
| 5768 | char *filename; |
| 5769 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5770 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5771 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5772 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5773 | return NULL; |
| 5774 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5775 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5776 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5777 | if (res < 0) |
| 5778 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5779 | Py_INCREF(Py_None); |
| 5780 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5781 | } |
| 5782 | #endif |
| 5783 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5784 | #ifdef HAVE_DEVICE_MACROS |
| 5785 | PyDoc_STRVAR(posix_major__doc__, |
| 5786 | "major(device) -> major number\n\ |
| 5787 | Extracts a device major number from a raw device number."); |
| 5788 | |
| 5789 | static PyObject * |
| 5790 | posix_major(PyObject *self, PyObject *args) |
| 5791 | { |
| 5792 | int device; |
| 5793 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5794 | return NULL; |
| 5795 | return PyInt_FromLong((long)major(device)); |
| 5796 | } |
| 5797 | |
| 5798 | PyDoc_STRVAR(posix_minor__doc__, |
| 5799 | "minor(device) -> minor number\n\ |
| 5800 | Extracts a device minor number from a raw device number."); |
| 5801 | |
| 5802 | static PyObject * |
| 5803 | posix_minor(PyObject *self, PyObject *args) |
| 5804 | { |
| 5805 | int device; |
| 5806 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5807 | return NULL; |
| 5808 | return PyInt_FromLong((long)minor(device)); |
| 5809 | } |
| 5810 | |
| 5811 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5812 | "makedev(major, minor) -> device number\n\ |
| 5813 | Composes a raw device number from the major and minor device numbers."); |
| 5814 | |
| 5815 | static PyObject * |
| 5816 | posix_makedev(PyObject *self, PyObject *args) |
| 5817 | { |
| 5818 | int major, minor; |
| 5819 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5820 | return NULL; |
| 5821 | return PyInt_FromLong((long)makedev(major, minor)); |
| 5822 | } |
| 5823 | #endif /* device macros */ |
| 5824 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5825 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5826 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5827 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5828 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5829 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5830 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5831 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5832 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5833 | { |
| 5834 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5835 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5836 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5837 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5838 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5839 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5840 | return NULL; |
| 5841 | |
| 5842 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5843 | length = PyInt_AsLong(lenobj); |
| 5844 | #else |
| 5845 | length = PyLong_Check(lenobj) ? |
| 5846 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 5847 | #endif |
| 5848 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5849 | return NULL; |
| 5850 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5851 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5852 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5853 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5854 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5855 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5856 | return NULL; |
| 5857 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5858 | Py_INCREF(Py_None); |
| 5859 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5860 | } |
| 5861 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5862 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5863 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5864 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5865 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5866 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5867 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5868 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5869 | * get re-set with another call for the same key. */ |
| 5870 | static PyObject *posix_putenv_garbage; |
| 5871 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5872 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5873 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5874 | { |
| 5875 | char *s1, *s2; |
| 5876 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5877 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5878 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5879 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5880 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5881 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5882 | |
| 5883 | #if defined(PYOS_OS2) |
| 5884 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5885 | APIRET rc; |
| 5886 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5887 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5888 | if (rc != NO_ERROR) |
| 5889 | return os2_error(rc); |
| 5890 | |
| 5891 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5892 | APIRET rc; |
| 5893 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5894 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5895 | if (rc != NO_ERROR) |
| 5896 | return os2_error(rc); |
| 5897 | } else { |
| 5898 | #endif |
| 5899 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5900 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5901 | len = strlen(s1) + strlen(s2) + 2; |
| 5902 | /* len includes space for a trailing \0; the size arg to |
| 5903 | PyString_FromStringAndSize does not count that */ |
| 5904 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5905 | if (newstr == NULL) |
| 5906 | return PyErr_NoMemory(); |
| 5907 | new = PyString_AS_STRING(newstr); |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5908 | PyOS_snprintf(new, len, "%s=%s", s1, s2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5909 | if (putenv(new)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5910 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5911 | posix_error(); |
| 5912 | return NULL; |
| 5913 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5914 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5915 | * this will cause previous value to be collected. This has to |
| 5916 | * happen after the real putenv() call because the old value |
| 5917 | * was still accessible until then. */ |
| 5918 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5919 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5920 | /* really not much we can do; just leak */ |
| 5921 | PyErr_Clear(); |
| 5922 | } |
| 5923 | else { |
| 5924 | Py_DECREF(newstr); |
| 5925 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5926 | |
| 5927 | #if defined(PYOS_OS2) |
| 5928 | } |
| 5929 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5930 | Py_INCREF(Py_None); |
| 5931 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5932 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5933 | #endif /* putenv */ |
| 5934 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5935 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5936 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5937 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5938 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5939 | |
| 5940 | static PyObject * |
| 5941 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5942 | { |
| 5943 | char *s1; |
| 5944 | |
| 5945 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5946 | return NULL; |
| 5947 | |
| 5948 | unsetenv(s1); |
| 5949 | |
| 5950 | /* Remove the key from posix_putenv_garbage; |
| 5951 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5952 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5953 | * old value was still accessible until then. |
| 5954 | */ |
| 5955 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5956 | PyTuple_GET_ITEM(args, 0))) { |
| 5957 | /* really not much we can do; just leak */ |
| 5958 | PyErr_Clear(); |
| 5959 | } |
| 5960 | |
| 5961 | Py_INCREF(Py_None); |
| 5962 | return Py_None; |
| 5963 | } |
| 5964 | #endif /* unsetenv */ |
| 5965 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5966 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5967 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5968 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5969 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5970 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5971 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5972 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5973 | { |
| 5974 | int code; |
| 5975 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5976 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5977 | return NULL; |
| 5978 | message = strerror(code); |
| 5979 | if (message == NULL) { |
| 5980 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5981 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5982 | return NULL; |
| 5983 | } |
| 5984 | return PyString_FromString(message); |
| 5985 | } |
| 5986 | #endif /* strerror */ |
| 5987 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5988 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5989 | #ifdef HAVE_SYS_WAIT_H |
| 5990 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5991 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5992 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5993 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5994 | 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] | 5995 | |
| 5996 | static PyObject * |
| 5997 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5998 | { |
| 5999 | #ifdef UNION_WAIT |
| 6000 | union wait status; |
| 6001 | #define status_i (status.w_status) |
| 6002 | #else |
| 6003 | int status; |
| 6004 | #define status_i status |
| 6005 | #endif |
| 6006 | status_i = 0; |
| 6007 | |
| 6008 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i)) |
| 6009 | { |
| 6010 | return NULL; |
| 6011 | } |
| 6012 | |
| 6013 | return PyBool_FromLong(WCOREDUMP(status)); |
| 6014 | #undef status_i |
| 6015 | } |
| 6016 | #endif /* WCOREDUMP */ |
| 6017 | |
| 6018 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6019 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6020 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6021 | 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] | 6022 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6023 | |
| 6024 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6025 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6026 | { |
| 6027 | #ifdef UNION_WAIT |
| 6028 | union wait status; |
| 6029 | #define status_i (status.w_status) |
| 6030 | #else |
| 6031 | int status; |
| 6032 | #define status_i status |
| 6033 | #endif |
| 6034 | status_i = 0; |
| 6035 | |
| 6036 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i)) |
| 6037 | { |
| 6038 | return NULL; |
| 6039 | } |
| 6040 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6041 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6042 | #undef status_i |
| 6043 | } |
| 6044 | #endif /* WIFCONTINUED */ |
| 6045 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6046 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6047 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6048 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6049 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6050 | |
| 6051 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6052 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6053 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6054 | #ifdef UNION_WAIT |
| 6055 | union wait status; |
| 6056 | #define status_i (status.w_status) |
| 6057 | #else |
| 6058 | int status; |
| 6059 | #define status_i status |
| 6060 | #endif |
| 6061 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6062 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6063 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6064 | { |
| 6065 | return NULL; |
| 6066 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6067 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6068 | return PyBool_FromLong(WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6069 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6070 | } |
| 6071 | #endif /* WIFSTOPPED */ |
| 6072 | |
| 6073 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6074 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6075 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6076 | 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] | 6077 | |
| 6078 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6079 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6080 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6081 | #ifdef UNION_WAIT |
| 6082 | union wait status; |
| 6083 | #define status_i (status.w_status) |
| 6084 | #else |
| 6085 | int status; |
| 6086 | #define status_i status |
| 6087 | #endif |
| 6088 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6089 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6090 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6091 | { |
| 6092 | return NULL; |
| 6093 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6094 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6095 | return PyBool_FromLong(WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6096 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6097 | } |
| 6098 | #endif /* WIFSIGNALED */ |
| 6099 | |
| 6100 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6101 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6102 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6103 | 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] | 6104 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6105 | |
| 6106 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6107 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6108 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6109 | #ifdef UNION_WAIT |
| 6110 | union wait status; |
| 6111 | #define status_i (status.w_status) |
| 6112 | #else |
| 6113 | int status; |
| 6114 | #define status_i status |
| 6115 | #endif |
| 6116 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6117 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6118 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6119 | { |
| 6120 | return NULL; |
| 6121 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6122 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6123 | return PyBool_FromLong(WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6124 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6125 | } |
| 6126 | #endif /* WIFEXITED */ |
| 6127 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6128 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6129 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6130 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6131 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6132 | |
| 6133 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6134 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6135 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6136 | #ifdef UNION_WAIT |
| 6137 | union wait status; |
| 6138 | #define status_i (status.w_status) |
| 6139 | #else |
| 6140 | int status; |
| 6141 | #define status_i status |
| 6142 | #endif |
| 6143 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6144 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6145 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6146 | { |
| 6147 | return NULL; |
| 6148 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6149 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6150 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6151 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6152 | } |
| 6153 | #endif /* WEXITSTATUS */ |
| 6154 | |
| 6155 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6156 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6157 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6158 | 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] | 6159 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6160 | |
| 6161 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6162 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6163 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6164 | #ifdef UNION_WAIT |
| 6165 | union wait status; |
| 6166 | #define status_i (status.w_status) |
| 6167 | #else |
| 6168 | int status; |
| 6169 | #define status_i status |
| 6170 | #endif |
| 6171 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6172 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6173 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6174 | { |
| 6175 | return NULL; |
| 6176 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6177 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6178 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6179 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6180 | } |
| 6181 | #endif /* WTERMSIG */ |
| 6182 | |
| 6183 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6184 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6185 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6186 | Return the signal that stopped the process that provided\n\ |
| 6187 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6188 | |
| 6189 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6190 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6191 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6192 | #ifdef UNION_WAIT |
| 6193 | union wait status; |
| 6194 | #define status_i (status.w_status) |
| 6195 | #else |
| 6196 | int status; |
| 6197 | #define status_i status |
| 6198 | #endif |
| 6199 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6200 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6201 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6202 | { |
| 6203 | return NULL; |
| 6204 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6205 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6206 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6207 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6208 | } |
| 6209 | #endif /* WSTOPSIG */ |
| 6210 | |
| 6211 | #endif /* HAVE_SYS_WAIT_H */ |
| 6212 | |
| 6213 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6214 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 6215 | #ifdef _SCO_DS |
| 6216 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 6217 | needed definitions in sys/statvfs.h */ |
| 6218 | #define _SVID3 |
| 6219 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6220 | #include <sys/statvfs.h> |
| 6221 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6222 | static PyObject* |
| 6223 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 6224 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 6225 | if (v == NULL) |
| 6226 | return NULL; |
| 6227 | |
| 6228 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6229 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6230 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 6231 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 6232 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 6233 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 6234 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 6235 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 6236 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 6237 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6238 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6239 | #else |
| 6240 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6241 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6242 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6243 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6244 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6245 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6246 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6247 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6248 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6249 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6250 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6251 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6252 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6253 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6254 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6255 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6256 | #endif |
| 6257 | |
| 6258 | return v; |
| 6259 | } |
| 6260 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6261 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6262 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6263 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6264 | |
| 6265 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6266 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6267 | { |
| 6268 | int fd, res; |
| 6269 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6270 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6271 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6272 | return NULL; |
| 6273 | Py_BEGIN_ALLOW_THREADS |
| 6274 | res = fstatvfs(fd, &st); |
| 6275 | Py_END_ALLOW_THREADS |
| 6276 | if (res != 0) |
| 6277 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6278 | |
| 6279 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6280 | } |
| 6281 | #endif /* HAVE_FSTATVFS */ |
| 6282 | |
| 6283 | |
| 6284 | #if defined(HAVE_STATVFS) |
| 6285 | #include <sys/statvfs.h> |
| 6286 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6287 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6288 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6289 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6290 | |
| 6291 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6292 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6293 | { |
| 6294 | char *path; |
| 6295 | int res; |
| 6296 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6297 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6298 | return NULL; |
| 6299 | Py_BEGIN_ALLOW_THREADS |
| 6300 | res = statvfs(path, &st); |
| 6301 | Py_END_ALLOW_THREADS |
| 6302 | if (res != 0) |
| 6303 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6304 | |
| 6305 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6306 | } |
| 6307 | #endif /* HAVE_STATVFS */ |
| 6308 | |
| 6309 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6310 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6311 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6312 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6313 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 6314 | 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] | 6315 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6316 | |
| 6317 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6318 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6319 | { |
| 6320 | PyObject *result = NULL; |
| 6321 | char *dir = NULL; |
| 6322 | char *pfx = NULL; |
| 6323 | char *name; |
| 6324 | |
| 6325 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 6326 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6327 | |
| 6328 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6329 | "tempnam is a potential security risk to your program") < 0) |
| 6330 | return NULL; |
| 6331 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6332 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6333 | name = _tempnam(dir, pfx); |
| 6334 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6335 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6336 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6337 | if (name == NULL) |
| 6338 | return PyErr_NoMemory(); |
| 6339 | result = PyString_FromString(name); |
| 6340 | free(name); |
| 6341 | return result; |
| 6342 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 6343 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6344 | |
| 6345 | |
| 6346 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6347 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6348 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6349 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6350 | |
| 6351 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6352 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6353 | { |
| 6354 | FILE *fp; |
| 6355 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6356 | fp = tmpfile(); |
| 6357 | if (fp == NULL) |
| 6358 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 6359 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6360 | } |
| 6361 | #endif |
| 6362 | |
| 6363 | |
| 6364 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6365 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6366 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6367 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6368 | |
| 6369 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6370 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6371 | { |
| 6372 | char buffer[L_tmpnam]; |
| 6373 | char *name; |
| 6374 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6375 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6376 | "tmpnam is a potential security risk to your program") < 0) |
| 6377 | return NULL; |
| 6378 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6379 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6380 | name = tmpnam_r(buffer); |
| 6381 | #else |
| 6382 | name = tmpnam(buffer); |
| 6383 | #endif |
| 6384 | if (name == NULL) { |
| 6385 | PyErr_SetObject(PyExc_OSError, |
| 6386 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6387 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6388 | "unexpected NULL from tmpnam_r" |
| 6389 | #else |
| 6390 | "unexpected NULL from tmpnam" |
| 6391 | #endif |
| 6392 | )); |
| 6393 | return NULL; |
| 6394 | } |
| 6395 | return PyString_FromString(buffer); |
| 6396 | } |
| 6397 | #endif |
| 6398 | |
| 6399 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6400 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 6401 | * It maps strings representing configuration variable names to |
| 6402 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6403 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6404 | * rarely-used constants. There are three separate tables that use |
| 6405 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6406 | * |
| 6407 | * This code is always included, even if none of the interfaces that |
| 6408 | * need it are included. The #if hackery needed to avoid it would be |
| 6409 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6410 | */ |
| 6411 | struct constdef { |
| 6412 | char *name; |
| 6413 | long value; |
| 6414 | }; |
| 6415 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6416 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6417 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 6418 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6419 | { |
| 6420 | if (PyInt_Check(arg)) { |
| 6421 | *valuep = PyInt_AS_LONG(arg); |
| 6422 | return 1; |
| 6423 | } |
| 6424 | if (PyString_Check(arg)) { |
| 6425 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6426 | size_t lo = 0; |
| 6427 | size_t mid; |
| 6428 | size_t hi = tablesize; |
| 6429 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6430 | char *confname = PyString_AS_STRING(arg); |
| 6431 | while (lo < hi) { |
| 6432 | mid = (lo + hi) / 2; |
| 6433 | cmp = strcmp(confname, table[mid].name); |
| 6434 | if (cmp < 0) |
| 6435 | hi = mid; |
| 6436 | else if (cmp > 0) |
| 6437 | lo = mid + 1; |
| 6438 | else { |
| 6439 | *valuep = table[mid].value; |
| 6440 | return 1; |
| 6441 | } |
| 6442 | } |
| 6443 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 6444 | } |
| 6445 | else |
| 6446 | PyErr_SetString(PyExc_TypeError, |
| 6447 | "configuration names must be strings or integers"); |
| 6448 | return 0; |
| 6449 | } |
| 6450 | |
| 6451 | |
| 6452 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 6453 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6454 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 6455 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 6456 | #endif |
| 6457 | #ifdef _PC_ABI_ASYNC_IO |
| 6458 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 6459 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6460 | #ifdef _PC_ASYNC_IO |
| 6461 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 6462 | #endif |
| 6463 | #ifdef _PC_CHOWN_RESTRICTED |
| 6464 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 6465 | #endif |
| 6466 | #ifdef _PC_FILESIZEBITS |
| 6467 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 6468 | #endif |
| 6469 | #ifdef _PC_LAST |
| 6470 | {"PC_LAST", _PC_LAST}, |
| 6471 | #endif |
| 6472 | #ifdef _PC_LINK_MAX |
| 6473 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 6474 | #endif |
| 6475 | #ifdef _PC_MAX_CANON |
| 6476 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 6477 | #endif |
| 6478 | #ifdef _PC_MAX_INPUT |
| 6479 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 6480 | #endif |
| 6481 | #ifdef _PC_NAME_MAX |
| 6482 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 6483 | #endif |
| 6484 | #ifdef _PC_NO_TRUNC |
| 6485 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 6486 | #endif |
| 6487 | #ifdef _PC_PATH_MAX |
| 6488 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 6489 | #endif |
| 6490 | #ifdef _PC_PIPE_BUF |
| 6491 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 6492 | #endif |
| 6493 | #ifdef _PC_PRIO_IO |
| 6494 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 6495 | #endif |
| 6496 | #ifdef _PC_SOCK_MAXBUF |
| 6497 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 6498 | #endif |
| 6499 | #ifdef _PC_SYNC_IO |
| 6500 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 6501 | #endif |
| 6502 | #ifdef _PC_VDISABLE |
| 6503 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 6504 | #endif |
| 6505 | }; |
| 6506 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6507 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6508 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6509 | { |
| 6510 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 6511 | sizeof(posix_constants_pathconf) |
| 6512 | / sizeof(struct constdef)); |
| 6513 | } |
| 6514 | #endif |
| 6515 | |
| 6516 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6517 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6518 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6519 | 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] | 6520 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6521 | |
| 6522 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6523 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6524 | { |
| 6525 | PyObject *result = NULL; |
| 6526 | int name, fd; |
| 6527 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6528 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 6529 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6530 | long limit; |
| 6531 | |
| 6532 | errno = 0; |
| 6533 | limit = fpathconf(fd, name); |
| 6534 | if (limit == -1 && errno != 0) |
| 6535 | posix_error(); |
| 6536 | else |
| 6537 | result = PyInt_FromLong(limit); |
| 6538 | } |
| 6539 | return result; |
| 6540 | } |
| 6541 | #endif |
| 6542 | |
| 6543 | |
| 6544 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6545 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6546 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6547 | 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] | 6548 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6549 | |
| 6550 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6551 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6552 | { |
| 6553 | PyObject *result = NULL; |
| 6554 | int name; |
| 6555 | char *path; |
| 6556 | |
| 6557 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 6558 | conv_path_confname, &name)) { |
| 6559 | long limit; |
| 6560 | |
| 6561 | errno = 0; |
| 6562 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6563 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6564 | if (errno == EINVAL) |
| 6565 | /* could be a path or name problem */ |
| 6566 | posix_error(); |
| 6567 | else |
| 6568 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6569 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6570 | else |
| 6571 | result = PyInt_FromLong(limit); |
| 6572 | } |
| 6573 | return result; |
| 6574 | } |
| 6575 | #endif |
| 6576 | |
| 6577 | #ifdef HAVE_CONFSTR |
| 6578 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6579 | #ifdef _CS_ARCHITECTURE |
| 6580 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 6581 | #endif |
| 6582 | #ifdef _CS_HOSTNAME |
| 6583 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 6584 | #endif |
| 6585 | #ifdef _CS_HW_PROVIDER |
| 6586 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 6587 | #endif |
| 6588 | #ifdef _CS_HW_SERIAL |
| 6589 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 6590 | #endif |
| 6591 | #ifdef _CS_INITTAB_NAME |
| 6592 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 6593 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6594 | #ifdef _CS_LFS64_CFLAGS |
| 6595 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 6596 | #endif |
| 6597 | #ifdef _CS_LFS64_LDFLAGS |
| 6598 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 6599 | #endif |
| 6600 | #ifdef _CS_LFS64_LIBS |
| 6601 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 6602 | #endif |
| 6603 | #ifdef _CS_LFS64_LINTFLAGS |
| 6604 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 6605 | #endif |
| 6606 | #ifdef _CS_LFS_CFLAGS |
| 6607 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 6608 | #endif |
| 6609 | #ifdef _CS_LFS_LDFLAGS |
| 6610 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6611 | #endif |
| 6612 | #ifdef _CS_LFS_LIBS |
| 6613 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6614 | #endif |
| 6615 | #ifdef _CS_LFS_LINTFLAGS |
| 6616 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6617 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6618 | #ifdef _CS_MACHINE |
| 6619 | {"CS_MACHINE", _CS_MACHINE}, |
| 6620 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6621 | #ifdef _CS_PATH |
| 6622 | {"CS_PATH", _CS_PATH}, |
| 6623 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6624 | #ifdef _CS_RELEASE |
| 6625 | {"CS_RELEASE", _CS_RELEASE}, |
| 6626 | #endif |
| 6627 | #ifdef _CS_SRPC_DOMAIN |
| 6628 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6629 | #endif |
| 6630 | #ifdef _CS_SYSNAME |
| 6631 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6632 | #endif |
| 6633 | #ifdef _CS_VERSION |
| 6634 | {"CS_VERSION", _CS_VERSION}, |
| 6635 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6636 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6637 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6638 | #endif |
| 6639 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6640 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6641 | #endif |
| 6642 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6643 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6644 | #endif |
| 6645 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6646 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6647 | #endif |
| 6648 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6649 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6650 | #endif |
| 6651 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6652 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6653 | #endif |
| 6654 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6655 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6656 | #endif |
| 6657 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6658 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6659 | #endif |
| 6660 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6661 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6662 | #endif |
| 6663 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6664 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6665 | #endif |
| 6666 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6667 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6668 | #endif |
| 6669 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6670 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6671 | #endif |
| 6672 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6673 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6674 | #endif |
| 6675 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6676 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6677 | #endif |
| 6678 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6679 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6680 | #endif |
| 6681 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6682 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6683 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6684 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6685 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6686 | #endif |
| 6687 | #ifdef _MIPS_CS_BASE |
| 6688 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6689 | #endif |
| 6690 | #ifdef _MIPS_CS_HOSTID |
| 6691 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6692 | #endif |
| 6693 | #ifdef _MIPS_CS_HW_NAME |
| 6694 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6695 | #endif |
| 6696 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6697 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6698 | #endif |
| 6699 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6700 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6701 | #endif |
| 6702 | #ifdef _MIPS_CS_OSREL_MIN |
| 6703 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6704 | #endif |
| 6705 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6706 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6707 | #endif |
| 6708 | #ifdef _MIPS_CS_OS_NAME |
| 6709 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6710 | #endif |
| 6711 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6712 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6713 | #endif |
| 6714 | #ifdef _MIPS_CS_PROCESSORS |
| 6715 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6716 | #endif |
| 6717 | #ifdef _MIPS_CS_SERIAL |
| 6718 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6719 | #endif |
| 6720 | #ifdef _MIPS_CS_VENDOR |
| 6721 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6722 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6723 | }; |
| 6724 | |
| 6725 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6726 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6727 | { |
| 6728 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6729 | sizeof(posix_constants_confstr) |
| 6730 | / sizeof(struct constdef)); |
| 6731 | } |
| 6732 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6733 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6734 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6735 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6736 | |
| 6737 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6738 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6739 | { |
| 6740 | PyObject *result = NULL; |
| 6741 | int name; |
| 6742 | char buffer[64]; |
| 6743 | |
| 6744 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 6745 | int len = confstr(name, buffer, sizeof(buffer)); |
| 6746 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6747 | errno = 0; |
| 6748 | if (len == 0) { |
| 6749 | if (errno != 0) |
| 6750 | posix_error(); |
| 6751 | else |
| 6752 | result = PyString_FromString(""); |
| 6753 | } |
| 6754 | else { |
| 6755 | if (len >= sizeof(buffer)) { |
| 6756 | result = PyString_FromStringAndSize(NULL, len); |
| 6757 | if (result != NULL) |
| 6758 | confstr(name, PyString_AS_STRING(result), len+1); |
| 6759 | } |
| 6760 | else |
| 6761 | result = PyString_FromString(buffer); |
| 6762 | } |
| 6763 | } |
| 6764 | return result; |
| 6765 | } |
| 6766 | #endif |
| 6767 | |
| 6768 | |
| 6769 | #ifdef HAVE_SYSCONF |
| 6770 | static struct constdef posix_constants_sysconf[] = { |
| 6771 | #ifdef _SC_2_CHAR_TERM |
| 6772 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6773 | #endif |
| 6774 | #ifdef _SC_2_C_BIND |
| 6775 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6776 | #endif |
| 6777 | #ifdef _SC_2_C_DEV |
| 6778 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6779 | #endif |
| 6780 | #ifdef _SC_2_C_VERSION |
| 6781 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6782 | #endif |
| 6783 | #ifdef _SC_2_FORT_DEV |
| 6784 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6785 | #endif |
| 6786 | #ifdef _SC_2_FORT_RUN |
| 6787 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6788 | #endif |
| 6789 | #ifdef _SC_2_LOCALEDEF |
| 6790 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6791 | #endif |
| 6792 | #ifdef _SC_2_SW_DEV |
| 6793 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6794 | #endif |
| 6795 | #ifdef _SC_2_UPE |
| 6796 | {"SC_2_UPE", _SC_2_UPE}, |
| 6797 | #endif |
| 6798 | #ifdef _SC_2_VERSION |
| 6799 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6800 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6801 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6802 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6803 | #endif |
| 6804 | #ifdef _SC_ACL |
| 6805 | {"SC_ACL", _SC_ACL}, |
| 6806 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6807 | #ifdef _SC_AIO_LISTIO_MAX |
| 6808 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6809 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6810 | #ifdef _SC_AIO_MAX |
| 6811 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6812 | #endif |
| 6813 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6814 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6815 | #endif |
| 6816 | #ifdef _SC_ARG_MAX |
| 6817 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6818 | #endif |
| 6819 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6820 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6821 | #endif |
| 6822 | #ifdef _SC_ATEXIT_MAX |
| 6823 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6824 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6825 | #ifdef _SC_AUDIT |
| 6826 | {"SC_AUDIT", _SC_AUDIT}, |
| 6827 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6828 | #ifdef _SC_AVPHYS_PAGES |
| 6829 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6830 | #endif |
| 6831 | #ifdef _SC_BC_BASE_MAX |
| 6832 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6833 | #endif |
| 6834 | #ifdef _SC_BC_DIM_MAX |
| 6835 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6836 | #endif |
| 6837 | #ifdef _SC_BC_SCALE_MAX |
| 6838 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6839 | #endif |
| 6840 | #ifdef _SC_BC_STRING_MAX |
| 6841 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6842 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6843 | #ifdef _SC_CAP |
| 6844 | {"SC_CAP", _SC_CAP}, |
| 6845 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6846 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6847 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6848 | #endif |
| 6849 | #ifdef _SC_CHAR_BIT |
| 6850 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6851 | #endif |
| 6852 | #ifdef _SC_CHAR_MAX |
| 6853 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6854 | #endif |
| 6855 | #ifdef _SC_CHAR_MIN |
| 6856 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6857 | #endif |
| 6858 | #ifdef _SC_CHILD_MAX |
| 6859 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6860 | #endif |
| 6861 | #ifdef _SC_CLK_TCK |
| 6862 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6863 | #endif |
| 6864 | #ifdef _SC_COHER_BLKSZ |
| 6865 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6866 | #endif |
| 6867 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6868 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6869 | #endif |
| 6870 | #ifdef _SC_DCACHE_ASSOC |
| 6871 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6872 | #endif |
| 6873 | #ifdef _SC_DCACHE_BLKSZ |
| 6874 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6875 | #endif |
| 6876 | #ifdef _SC_DCACHE_LINESZ |
| 6877 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6878 | #endif |
| 6879 | #ifdef _SC_DCACHE_SZ |
| 6880 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6881 | #endif |
| 6882 | #ifdef _SC_DCACHE_TBLKSZ |
| 6883 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6884 | #endif |
| 6885 | #ifdef _SC_DELAYTIMER_MAX |
| 6886 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6887 | #endif |
| 6888 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6889 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6890 | #endif |
| 6891 | #ifdef _SC_EXPR_NEST_MAX |
| 6892 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6893 | #endif |
| 6894 | #ifdef _SC_FSYNC |
| 6895 | {"SC_FSYNC", _SC_FSYNC}, |
| 6896 | #endif |
| 6897 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6898 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6899 | #endif |
| 6900 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6901 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6902 | #endif |
| 6903 | #ifdef _SC_ICACHE_ASSOC |
| 6904 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6905 | #endif |
| 6906 | #ifdef _SC_ICACHE_BLKSZ |
| 6907 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6908 | #endif |
| 6909 | #ifdef _SC_ICACHE_LINESZ |
| 6910 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6911 | #endif |
| 6912 | #ifdef _SC_ICACHE_SZ |
| 6913 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6914 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6915 | #ifdef _SC_INF |
| 6916 | {"SC_INF", _SC_INF}, |
| 6917 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6918 | #ifdef _SC_INT_MAX |
| 6919 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6920 | #endif |
| 6921 | #ifdef _SC_INT_MIN |
| 6922 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6923 | #endif |
| 6924 | #ifdef _SC_IOV_MAX |
| 6925 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6926 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6927 | #ifdef _SC_IP_SECOPTS |
| 6928 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6929 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6930 | #ifdef _SC_JOB_CONTROL |
| 6931 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6932 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6933 | #ifdef _SC_KERN_POINTERS |
| 6934 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6935 | #endif |
| 6936 | #ifdef _SC_KERN_SIM |
| 6937 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6938 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6939 | #ifdef _SC_LINE_MAX |
| 6940 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6941 | #endif |
| 6942 | #ifdef _SC_LOGIN_NAME_MAX |
| 6943 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6944 | #endif |
| 6945 | #ifdef _SC_LOGNAME_MAX |
| 6946 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6947 | #endif |
| 6948 | #ifdef _SC_LONG_BIT |
| 6949 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6950 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6951 | #ifdef _SC_MAC |
| 6952 | {"SC_MAC", _SC_MAC}, |
| 6953 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6954 | #ifdef _SC_MAPPED_FILES |
| 6955 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6956 | #endif |
| 6957 | #ifdef _SC_MAXPID |
| 6958 | {"SC_MAXPID", _SC_MAXPID}, |
| 6959 | #endif |
| 6960 | #ifdef _SC_MB_LEN_MAX |
| 6961 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6962 | #endif |
| 6963 | #ifdef _SC_MEMLOCK |
| 6964 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6965 | #endif |
| 6966 | #ifdef _SC_MEMLOCK_RANGE |
| 6967 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6968 | #endif |
| 6969 | #ifdef _SC_MEMORY_PROTECTION |
| 6970 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6971 | #endif |
| 6972 | #ifdef _SC_MESSAGE_PASSING |
| 6973 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6974 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6975 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6976 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6977 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6978 | #ifdef _SC_MQ_OPEN_MAX |
| 6979 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6980 | #endif |
| 6981 | #ifdef _SC_MQ_PRIO_MAX |
| 6982 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6983 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6984 | #ifdef _SC_NACLS_MAX |
| 6985 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6986 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6987 | #ifdef _SC_NGROUPS_MAX |
| 6988 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6989 | #endif |
| 6990 | #ifdef _SC_NL_ARGMAX |
| 6991 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6992 | #endif |
| 6993 | #ifdef _SC_NL_LANGMAX |
| 6994 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6995 | #endif |
| 6996 | #ifdef _SC_NL_MSGMAX |
| 6997 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6998 | #endif |
| 6999 | #ifdef _SC_NL_NMAX |
| 7000 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 7001 | #endif |
| 7002 | #ifdef _SC_NL_SETMAX |
| 7003 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 7004 | #endif |
| 7005 | #ifdef _SC_NL_TEXTMAX |
| 7006 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 7007 | #endif |
| 7008 | #ifdef _SC_NPROCESSORS_CONF |
| 7009 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 7010 | #endif |
| 7011 | #ifdef _SC_NPROCESSORS_ONLN |
| 7012 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 7013 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7014 | #ifdef _SC_NPROC_CONF |
| 7015 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 7016 | #endif |
| 7017 | #ifdef _SC_NPROC_ONLN |
| 7018 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 7019 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7020 | #ifdef _SC_NZERO |
| 7021 | {"SC_NZERO", _SC_NZERO}, |
| 7022 | #endif |
| 7023 | #ifdef _SC_OPEN_MAX |
| 7024 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 7025 | #endif |
| 7026 | #ifdef _SC_PAGESIZE |
| 7027 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 7028 | #endif |
| 7029 | #ifdef _SC_PAGE_SIZE |
| 7030 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 7031 | #endif |
| 7032 | #ifdef _SC_PASS_MAX |
| 7033 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 7034 | #endif |
| 7035 | #ifdef _SC_PHYS_PAGES |
| 7036 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 7037 | #endif |
| 7038 | #ifdef _SC_PII |
| 7039 | {"SC_PII", _SC_PII}, |
| 7040 | #endif |
| 7041 | #ifdef _SC_PII_INTERNET |
| 7042 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 7043 | #endif |
| 7044 | #ifdef _SC_PII_INTERNET_DGRAM |
| 7045 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 7046 | #endif |
| 7047 | #ifdef _SC_PII_INTERNET_STREAM |
| 7048 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 7049 | #endif |
| 7050 | #ifdef _SC_PII_OSI |
| 7051 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 7052 | #endif |
| 7053 | #ifdef _SC_PII_OSI_CLTS |
| 7054 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 7055 | #endif |
| 7056 | #ifdef _SC_PII_OSI_COTS |
| 7057 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 7058 | #endif |
| 7059 | #ifdef _SC_PII_OSI_M |
| 7060 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 7061 | #endif |
| 7062 | #ifdef _SC_PII_SOCKET |
| 7063 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 7064 | #endif |
| 7065 | #ifdef _SC_PII_XTI |
| 7066 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 7067 | #endif |
| 7068 | #ifdef _SC_POLL |
| 7069 | {"SC_POLL", _SC_POLL}, |
| 7070 | #endif |
| 7071 | #ifdef _SC_PRIORITIZED_IO |
| 7072 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 7073 | #endif |
| 7074 | #ifdef _SC_PRIORITY_SCHEDULING |
| 7075 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 7076 | #endif |
| 7077 | #ifdef _SC_REALTIME_SIGNALS |
| 7078 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 7079 | #endif |
| 7080 | #ifdef _SC_RE_DUP_MAX |
| 7081 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 7082 | #endif |
| 7083 | #ifdef _SC_RTSIG_MAX |
| 7084 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 7085 | #endif |
| 7086 | #ifdef _SC_SAVED_IDS |
| 7087 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 7088 | #endif |
| 7089 | #ifdef _SC_SCHAR_MAX |
| 7090 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 7091 | #endif |
| 7092 | #ifdef _SC_SCHAR_MIN |
| 7093 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 7094 | #endif |
| 7095 | #ifdef _SC_SELECT |
| 7096 | {"SC_SELECT", _SC_SELECT}, |
| 7097 | #endif |
| 7098 | #ifdef _SC_SEMAPHORES |
| 7099 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 7100 | #endif |
| 7101 | #ifdef _SC_SEM_NSEMS_MAX |
| 7102 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 7103 | #endif |
| 7104 | #ifdef _SC_SEM_VALUE_MAX |
| 7105 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 7106 | #endif |
| 7107 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 7108 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 7109 | #endif |
| 7110 | #ifdef _SC_SHRT_MAX |
| 7111 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 7112 | #endif |
| 7113 | #ifdef _SC_SHRT_MIN |
| 7114 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 7115 | #endif |
| 7116 | #ifdef _SC_SIGQUEUE_MAX |
| 7117 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 7118 | #endif |
| 7119 | #ifdef _SC_SIGRT_MAX |
| 7120 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 7121 | #endif |
| 7122 | #ifdef _SC_SIGRT_MIN |
| 7123 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 7124 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7125 | #ifdef _SC_SOFTPOWER |
| 7126 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 7127 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7128 | #ifdef _SC_SPLIT_CACHE |
| 7129 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 7130 | #endif |
| 7131 | #ifdef _SC_SSIZE_MAX |
| 7132 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 7133 | #endif |
| 7134 | #ifdef _SC_STACK_PROT |
| 7135 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 7136 | #endif |
| 7137 | #ifdef _SC_STREAM_MAX |
| 7138 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 7139 | #endif |
| 7140 | #ifdef _SC_SYNCHRONIZED_IO |
| 7141 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 7142 | #endif |
| 7143 | #ifdef _SC_THREADS |
| 7144 | {"SC_THREADS", _SC_THREADS}, |
| 7145 | #endif |
| 7146 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 7147 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 7148 | #endif |
| 7149 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 7150 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 7151 | #endif |
| 7152 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 7153 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 7154 | #endif |
| 7155 | #ifdef _SC_THREAD_KEYS_MAX |
| 7156 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 7157 | #endif |
| 7158 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 7159 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 7160 | #endif |
| 7161 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 7162 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 7163 | #endif |
| 7164 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 7165 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 7166 | #endif |
| 7167 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 7168 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 7169 | #endif |
| 7170 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 7171 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 7172 | #endif |
| 7173 | #ifdef _SC_THREAD_STACK_MIN |
| 7174 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 7175 | #endif |
| 7176 | #ifdef _SC_THREAD_THREADS_MAX |
| 7177 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 7178 | #endif |
| 7179 | #ifdef _SC_TIMERS |
| 7180 | {"SC_TIMERS", _SC_TIMERS}, |
| 7181 | #endif |
| 7182 | #ifdef _SC_TIMER_MAX |
| 7183 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 7184 | #endif |
| 7185 | #ifdef _SC_TTY_NAME_MAX |
| 7186 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 7187 | #endif |
| 7188 | #ifdef _SC_TZNAME_MAX |
| 7189 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 7190 | #endif |
| 7191 | #ifdef _SC_T_IOV_MAX |
| 7192 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 7193 | #endif |
| 7194 | #ifdef _SC_UCHAR_MAX |
| 7195 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 7196 | #endif |
| 7197 | #ifdef _SC_UINT_MAX |
| 7198 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 7199 | #endif |
| 7200 | #ifdef _SC_UIO_MAXIOV |
| 7201 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 7202 | #endif |
| 7203 | #ifdef _SC_ULONG_MAX |
| 7204 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 7205 | #endif |
| 7206 | #ifdef _SC_USHRT_MAX |
| 7207 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 7208 | #endif |
| 7209 | #ifdef _SC_VERSION |
| 7210 | {"SC_VERSION", _SC_VERSION}, |
| 7211 | #endif |
| 7212 | #ifdef _SC_WORD_BIT |
| 7213 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 7214 | #endif |
| 7215 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 7216 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 7217 | #endif |
| 7218 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 7219 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 7220 | #endif |
| 7221 | #ifdef _SC_XBS5_LP64_OFF64 |
| 7222 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 7223 | #endif |
| 7224 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 7225 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 7226 | #endif |
| 7227 | #ifdef _SC_XOPEN_CRYPT |
| 7228 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 7229 | #endif |
| 7230 | #ifdef _SC_XOPEN_ENH_I18N |
| 7231 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 7232 | #endif |
| 7233 | #ifdef _SC_XOPEN_LEGACY |
| 7234 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 7235 | #endif |
| 7236 | #ifdef _SC_XOPEN_REALTIME |
| 7237 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 7238 | #endif |
| 7239 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 7240 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 7241 | #endif |
| 7242 | #ifdef _SC_XOPEN_SHM |
| 7243 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 7244 | #endif |
| 7245 | #ifdef _SC_XOPEN_UNIX |
| 7246 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 7247 | #endif |
| 7248 | #ifdef _SC_XOPEN_VERSION |
| 7249 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 7250 | #endif |
| 7251 | #ifdef _SC_XOPEN_XCU_VERSION |
| 7252 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 7253 | #endif |
| 7254 | #ifdef _SC_XOPEN_XPG2 |
| 7255 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 7256 | #endif |
| 7257 | #ifdef _SC_XOPEN_XPG3 |
| 7258 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 7259 | #endif |
| 7260 | #ifdef _SC_XOPEN_XPG4 |
| 7261 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 7262 | #endif |
| 7263 | }; |
| 7264 | |
| 7265 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7266 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7267 | { |
| 7268 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 7269 | sizeof(posix_constants_sysconf) |
| 7270 | / sizeof(struct constdef)); |
| 7271 | } |
| 7272 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7273 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7274 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7275 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7276 | |
| 7277 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7278 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7279 | { |
| 7280 | PyObject *result = NULL; |
| 7281 | int name; |
| 7282 | |
| 7283 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 7284 | int value; |
| 7285 | |
| 7286 | errno = 0; |
| 7287 | value = sysconf(name); |
| 7288 | if (value == -1 && errno != 0) |
| 7289 | posix_error(); |
| 7290 | else |
| 7291 | result = PyInt_FromLong(value); |
| 7292 | } |
| 7293 | return result; |
| 7294 | } |
| 7295 | #endif |
| 7296 | |
| 7297 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7298 | /* This code is used to ensure that the tables of configuration value names |
| 7299 | * are in sorted order as required by conv_confname(), and also to build the |
| 7300 | * the exported dictionaries that are used to publish information about the |
| 7301 | * names available on the host platform. |
| 7302 | * |
| 7303 | * Sorting the table at runtime ensures that the table is properly ordered |
| 7304 | * when used, even for platforms we're not able to test on. It also makes |
| 7305 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7306 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7307 | |
| 7308 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7309 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7310 | { |
| 7311 | const struct constdef *c1 = |
| 7312 | (const struct constdef *) v1; |
| 7313 | const struct constdef *c2 = |
| 7314 | (const struct constdef *) v2; |
| 7315 | |
| 7316 | return strcmp(c1->name, c2->name); |
| 7317 | } |
| 7318 | |
| 7319 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7320 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7321 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7322 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7323 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7324 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7325 | |
| 7326 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 7327 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7328 | if (d == NULL) |
| 7329 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7330 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7331 | for (i=0; i < tablesize; ++i) { |
| 7332 | PyObject *o = PyInt_FromLong(table[i].value); |
| 7333 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 7334 | Py_XDECREF(o); |
| 7335 | Py_DECREF(d); |
| 7336 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7337 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7338 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7339 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7340 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7341 | } |
| 7342 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7343 | /* Return -1 on failure, 0 on success. */ |
| 7344 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7345 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7346 | { |
| 7347 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7348 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7349 | sizeof(posix_constants_pathconf) |
| 7350 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7351 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7352 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7353 | #endif |
| 7354 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7355 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7356 | sizeof(posix_constants_confstr) |
| 7357 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7358 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7359 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7360 | #endif |
| 7361 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7362 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7363 | sizeof(posix_constants_sysconf) |
| 7364 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7365 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7366 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7367 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7368 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7369 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7370 | |
| 7371 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7372 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7373 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7374 | 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] | 7375 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7376 | |
| 7377 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7378 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7379 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7380 | abort(); |
| 7381 | /*NOTREACHED*/ |
| 7382 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 7383 | return NULL; |
| 7384 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7385 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7386 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7387 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7388 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 7389 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7390 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7391 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 7392 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 7393 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 7394 | application (if any) its extension is associated.\n\ |
| 7395 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 7396 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7397 | \n\ |
| 7398 | startfile returns as soon as the associated application is launched.\n\ |
| 7399 | There is no option to wait for the application to close, and no way\n\ |
| 7400 | to retrieve the application's exit status.\n\ |
| 7401 | \n\ |
| 7402 | The filepath is relative to the current directory. If you want to use\n\ |
| 7403 | 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] | 7404 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7405 | |
| 7406 | static PyObject * |
| 7407 | win32_startfile(PyObject *self, PyObject *args) |
| 7408 | { |
| 7409 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7410 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7411 | HINSTANCE rc; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7412 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 7413 | Py_FileSystemDefaultEncoding, &filepath, |
| 7414 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7415 | return NULL; |
| 7416 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7417 | rc = ShellExecute((HWND)0, operation, filepath, |
| 7418 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7419 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 7420 | if (rc <= (HINSTANCE)32) { |
| 7421 | PyObject *errval = win32_error("startfile", filepath); |
| 7422 | PyMem_Free(filepath); |
| 7423 | return errval; |
| 7424 | } |
| 7425 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7426 | Py_INCREF(Py_None); |
| 7427 | return Py_None; |
| 7428 | } |
| 7429 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7430 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7431 | #ifdef HAVE_GETLOADAVG |
| 7432 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 7433 | "getloadavg() -> (float, float, float)\n\n\ |
| 7434 | Return the number of processes in the system run queue averaged over\n\ |
| 7435 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 7436 | was unobtainable"); |
| 7437 | |
| 7438 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7439 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7440 | { |
| 7441 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7442 | if (getloadavg(loadavg, 3)!=3) { |
| 7443 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 7444 | return NULL; |
| 7445 | } else |
| 7446 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 7447 | } |
| 7448 | #endif |
| 7449 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7450 | #ifdef MS_WINDOWS |
| 7451 | |
| 7452 | PyDoc_STRVAR(win32_urandom__doc__, |
| 7453 | "urandom(n) -> str\n\n\ |
| 7454 | Return a string of n random bytes suitable for cryptographic use."); |
| 7455 | |
| 7456 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 7457 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 7458 | DWORD dwFlags ); |
| 7459 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 7460 | BYTE *pbBuffer ); |
| 7461 | |
| 7462 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
| 7463 | static HCRYPTPROV hCryptProv = 0; |
| 7464 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7465 | static PyObject* |
| 7466 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7467 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7468 | int howMany; |
| 7469 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7470 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7471 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 7472 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7473 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 7474 | if (howMany < 0) |
| 7475 | return PyErr_Format(PyExc_ValueError, |
| 7476 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7477 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7478 | if (hCryptProv == 0) { |
| 7479 | HINSTANCE hAdvAPI32 = NULL; |
| 7480 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7481 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7482 | /* Obtain handle to the DLL containing CryptoAPI |
| 7483 | This should not fail */ |
| 7484 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 7485 | if(hAdvAPI32 == NULL) |
| 7486 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7487 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7488 | /* Obtain pointers to the CryptoAPI functions |
| 7489 | This will fail on some early versions of Win95 */ |
| 7490 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 7491 | hAdvAPI32, |
| 7492 | "CryptAcquireContextA"); |
| 7493 | if (pCryptAcquireContext == NULL) |
| 7494 | return PyErr_Format(PyExc_NotImplementedError, |
| 7495 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7496 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7497 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 7498 | hAdvAPI32, "CryptGenRandom"); |
| 7499 | if (pCryptAcquireContext == NULL) |
| 7500 | return PyErr_Format(PyExc_NotImplementedError, |
| 7501 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7502 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7503 | /* Acquire context */ |
| 7504 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 7505 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 7506 | return win32_error("CryptAcquireContext", NULL); |
| 7507 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7508 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7509 | /* Allocate bytes */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7510 | result = PyString_FromStringAndSize(NULL, howMany); |
| 7511 | if (result != NULL) { |
| 7512 | /* Get random data */ |
| 7513 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 7514 | PyString_AS_STRING(result))) { |
| 7515 | Py_DECREF(result); |
| 7516 | return win32_error("CryptGenRandom", NULL); |
| 7517 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7518 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7519 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7520 | } |
| 7521 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7522 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7523 | static PyMethodDef posix_methods[] = { |
| 7524 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 7525 | #ifdef HAVE_TTYNAME |
| 7526 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 7527 | #endif |
| 7528 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 7529 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7530 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7531 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7532 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7533 | #ifdef HAVE_LCHOWN |
| 7534 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 7535 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7536 | #ifdef HAVE_CHROOT |
| 7537 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 7538 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7539 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7540 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7541 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7542 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7543 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7544 | #ifdef Py_USING_UNICODE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7545 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7546 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7547 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7548 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7549 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7550 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7551 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7552 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7553 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7554 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7555 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7556 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7557 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7558 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7559 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7560 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7561 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7562 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7563 | {"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] | 7564 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7565 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7566 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7567 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7568 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7569 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7570 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7571 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7572 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7573 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7574 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7575 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7576 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7577 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7578 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7579 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7580 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7581 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7582 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7583 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7584 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7585 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7586 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7587 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7588 | #if defined(PYOS_OS2) |
| 7589 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 7590 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 7591 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7592 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7593 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7594 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7595 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7596 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7597 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7598 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7599 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7600 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7601 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7602 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7603 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7604 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7605 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7606 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7607 | #endif /* HAVE_GETEGID */ |
| 7608 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7609 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7610 | #endif /* HAVE_GETEUID */ |
| 7611 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7612 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7613 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7614 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7615 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7616 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7617 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7618 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7619 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7620 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7621 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7622 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7623 | #endif /* HAVE_GETPPID */ |
| 7624 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7625 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7626 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7627 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7628 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7629 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7630 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7631 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7632 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7633 | #ifdef HAVE_KILLPG |
| 7634 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7635 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7636 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7637 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7638 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7639 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7640 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7641 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7642 | {"popen2", win32_popen2, METH_VARARGS}, |
| 7643 | {"popen3", win32_popen3, METH_VARARGS}, |
| 7644 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7645 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7646 | #else |
| 7647 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7648 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 7649 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 7650 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 7651 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7652 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7653 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7654 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7655 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7656 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7657 | #ifdef HAVE_SETEUID |
| 7658 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7659 | #endif /* HAVE_SETEUID */ |
| 7660 | #ifdef HAVE_SETEGID |
| 7661 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7662 | #endif /* HAVE_SETEGID */ |
| 7663 | #ifdef HAVE_SETREUID |
| 7664 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7665 | #endif /* HAVE_SETREUID */ |
| 7666 | #ifdef HAVE_SETREGID |
| 7667 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7668 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7669 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7670 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7671 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7672 | #ifdef HAVE_SETGROUPS |
| 7673 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 7674 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7675 | #ifdef HAVE_GETPGID |
| 7676 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7677 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7678 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7679 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7680 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7681 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7682 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7683 | #endif /* HAVE_WAIT */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7684 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7685 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7686 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7687 | #ifdef HAVE_GETSID |
| 7688 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 7689 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7690 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7691 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7692 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7693 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7694 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7695 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7696 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7697 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7698 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7699 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7700 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7701 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7702 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7703 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 7704 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7705 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7706 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7707 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7708 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7709 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 7710 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7711 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7712 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7713 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7714 | #endif |
| 7715 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7716 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7717 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7718 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7719 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7720 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7721 | #ifdef HAVE_DEVICE_MACROS |
| 7722 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7723 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7724 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7725 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7726 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7727 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7728 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7729 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7730 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7731 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7732 | #ifdef HAVE_UNSETENV |
| 7733 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7734 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7735 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7736 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7737 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7738 | #ifdef HAVE_FCHDIR |
| 7739 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7740 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7741 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7742 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7743 | #endif |
| 7744 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7745 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7746 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7747 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7748 | #ifdef WCOREDUMP |
| 7749 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7750 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7751 | #ifdef WIFCONTINUED |
| 7752 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7753 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7754 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7755 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7756 | #endif /* WIFSTOPPED */ |
| 7757 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7758 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7759 | #endif /* WIFSIGNALED */ |
| 7760 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7761 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7762 | #endif /* WIFEXITED */ |
| 7763 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7764 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7765 | #endif /* WEXITSTATUS */ |
| 7766 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7767 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7768 | #endif /* WTERMSIG */ |
| 7769 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7770 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7771 | #endif /* WSTOPSIG */ |
| 7772 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7773 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7774 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7775 | #endif |
| 7776 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7777 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7778 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 7779 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7780 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7781 | #endif |
| 7782 | #ifdef HAVE_TEMPNAM |
| 7783 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 7784 | #endif |
| 7785 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7786 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7787 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7788 | #ifdef HAVE_CONFSTR |
| 7789 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7790 | #endif |
| 7791 | #ifdef HAVE_SYSCONF |
| 7792 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7793 | #endif |
| 7794 | #ifdef HAVE_FPATHCONF |
| 7795 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7796 | #endif |
| 7797 | #ifdef HAVE_PATHCONF |
| 7798 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7799 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7800 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7801 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7802 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7803 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7804 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7805 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7806 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7807 | #ifdef MS_WINDOWS |
| 7808 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 7809 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7810 | {NULL, NULL} /* Sentinel */ |
| 7811 | }; |
| 7812 | |
| 7813 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7814 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7815 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7816 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7817 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7818 | } |
| 7819 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7820 | #if defined(PYOS_OS2) |
| 7821 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7822 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7823 | { |
| 7824 | APIRET rc; |
| 7825 | ULONG values[QSV_MAX+1]; |
| 7826 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7827 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7828 | |
| 7829 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7830 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7831 | Py_END_ALLOW_THREADS |
| 7832 | |
| 7833 | if (rc != NO_ERROR) { |
| 7834 | os2_error(rc); |
| 7835 | return -1; |
| 7836 | } |
| 7837 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7838 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7839 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7840 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7841 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7842 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7843 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7844 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7845 | |
| 7846 | switch (values[QSV_VERSION_MINOR]) { |
| 7847 | case 0: ver = "2.00"; break; |
| 7848 | case 10: ver = "2.10"; break; |
| 7849 | case 11: ver = "2.11"; break; |
| 7850 | case 30: ver = "3.00"; break; |
| 7851 | case 40: ver = "4.00"; break; |
| 7852 | case 50: ver = "5.00"; break; |
| 7853 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7854 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7855 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7856 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7857 | ver = &tmp[0]; |
| 7858 | } |
| 7859 | |
| 7860 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7861 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7862 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7863 | |
| 7864 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7865 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7866 | tmp[1] = ':'; |
| 7867 | tmp[2] = '\0'; |
| 7868 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7869 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7870 | } |
| 7871 | #endif |
| 7872 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7873 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7874 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7875 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7876 | #ifdef F_OK |
| 7877 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7878 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7879 | #ifdef R_OK |
| 7880 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7881 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7882 | #ifdef W_OK |
| 7883 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7884 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7885 | #ifdef X_OK |
| 7886 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7887 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7888 | #ifdef NGROUPS_MAX |
| 7889 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7890 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7891 | #ifdef TMP_MAX |
| 7892 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7893 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7894 | #ifdef WCONTINUED |
| 7895 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7896 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7897 | #ifdef WNOHANG |
| 7898 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7899 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7900 | #ifdef WUNTRACED |
| 7901 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7902 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7903 | #ifdef O_RDONLY |
| 7904 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7905 | #endif |
| 7906 | #ifdef O_WRONLY |
| 7907 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7908 | #endif |
| 7909 | #ifdef O_RDWR |
| 7910 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7911 | #endif |
| 7912 | #ifdef O_NDELAY |
| 7913 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7914 | #endif |
| 7915 | #ifdef O_NONBLOCK |
| 7916 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7917 | #endif |
| 7918 | #ifdef O_APPEND |
| 7919 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7920 | #endif |
| 7921 | #ifdef O_DSYNC |
| 7922 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7923 | #endif |
| 7924 | #ifdef O_RSYNC |
| 7925 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7926 | #endif |
| 7927 | #ifdef O_SYNC |
| 7928 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7929 | #endif |
| 7930 | #ifdef O_NOCTTY |
| 7931 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7932 | #endif |
| 7933 | #ifdef O_CREAT |
| 7934 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7935 | #endif |
| 7936 | #ifdef O_EXCL |
| 7937 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7938 | #endif |
| 7939 | #ifdef O_TRUNC |
| 7940 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7941 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7942 | #ifdef O_BINARY |
| 7943 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7944 | #endif |
| 7945 | #ifdef O_TEXT |
| 7946 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7947 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7948 | #ifdef O_LARGEFILE |
| 7949 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7950 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7951 | #ifdef O_SHLOCK |
| 7952 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 7953 | #endif |
| 7954 | #ifdef O_EXLOCK |
| 7955 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 7956 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7957 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7958 | /* MS Windows */ |
| 7959 | #ifdef O_NOINHERIT |
| 7960 | /* Don't inherit in child processes. */ |
| 7961 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7962 | #endif |
| 7963 | #ifdef _O_SHORT_LIVED |
| 7964 | /* Optimize for short life (keep in memory). */ |
| 7965 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7966 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7967 | #endif |
| 7968 | #ifdef O_TEMPORARY |
| 7969 | /* Automatically delete when last handle is closed. */ |
| 7970 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7971 | #endif |
| 7972 | #ifdef O_RANDOM |
| 7973 | /* Optimize for random access. */ |
| 7974 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7975 | #endif |
| 7976 | #ifdef O_SEQUENTIAL |
| 7977 | /* Optimize for sequential access. */ |
| 7978 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7979 | #endif |
| 7980 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7981 | /* GNU extensions. */ |
| 7982 | #ifdef O_DIRECT |
| 7983 | /* Direct disk access. */ |
| 7984 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7985 | #endif |
| 7986 | #ifdef O_DIRECTORY |
| 7987 | /* Must be a directory. */ |
| 7988 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7989 | #endif |
| 7990 | #ifdef O_NOFOLLOW |
| 7991 | /* Do not follow links. */ |
| 7992 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7993 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7994 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7995 | /* These come from sysexits.h */ |
| 7996 | #ifdef EX_OK |
| 7997 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7998 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7999 | #ifdef EX_USAGE |
| 8000 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8001 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8002 | #ifdef EX_DATAERR |
| 8003 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8004 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8005 | #ifdef EX_NOINPUT |
| 8006 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8007 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8008 | #ifdef EX_NOUSER |
| 8009 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8010 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8011 | #ifdef EX_NOHOST |
| 8012 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8013 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8014 | #ifdef EX_UNAVAILABLE |
| 8015 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8016 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8017 | #ifdef EX_SOFTWARE |
| 8018 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8019 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8020 | #ifdef EX_OSERR |
| 8021 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8022 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8023 | #ifdef EX_OSFILE |
| 8024 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8025 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8026 | #ifdef EX_CANTCREAT |
| 8027 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8028 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8029 | #ifdef EX_IOERR |
| 8030 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8031 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8032 | #ifdef EX_TEMPFAIL |
| 8033 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8034 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8035 | #ifdef EX_PROTOCOL |
| 8036 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8037 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8038 | #ifdef EX_NOPERM |
| 8039 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8040 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8041 | #ifdef EX_CONFIG |
| 8042 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8043 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8044 | #ifdef EX_NOTFOUND |
| 8045 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8046 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8047 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8048 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8049 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 8050 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 8051 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 8052 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 8053 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 8054 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 8055 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 8056 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 8057 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 8058 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 8059 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 8060 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 8061 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 8062 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 8063 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 8064 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 8065 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 8066 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 8067 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 8068 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 8069 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 8070 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 8071 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 8072 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 8073 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 8074 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 8075 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8076 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8077 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8078 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8079 | #if defined(PYOS_OS2) |
| 8080 | if (insertvalues(d)) return -1; |
| 8081 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8082 | return 0; |
| 8083 | } |
| 8084 | |
| 8085 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8086 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8087 | #define INITFUNC initnt |
| 8088 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8089 | |
| 8090 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8091 | #define INITFUNC initos2 |
| 8092 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8093 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8094 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8095 | #define INITFUNC initposix |
| 8096 | #define MODNAME "posix" |
| 8097 | #endif |
| 8098 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 8099 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 8100 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8101 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8102 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8103 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8104 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8105 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8106 | posix__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 8107 | if (m == NULL) |
| 8108 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8109 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8110 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8111 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8112 | Py_XINCREF(v); |
| 8113 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8114 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8115 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8116 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8117 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8118 | return; |
| 8119 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8120 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8121 | return; |
| 8122 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8123 | Py_INCREF(PyExc_OSError); |
| 8124 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8125 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8126 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 8127 | if (posix_putenv_garbage == NULL) |
| 8128 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8129 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8130 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 8131 | stat_result_desc.name = MODNAME ".stat_result"; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 8132 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 8133 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 8134 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8135 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 8136 | structseq_new = StatResultType.tp_new; |
| 8137 | StatResultType.tp_new = statresult_new; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8138 | Py_INCREF((PyObject*) &StatResultType); |
| 8139 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8140 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 8141 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8142 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8143 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 8144 | PyModule_AddObject(m, "statvfs_result", |
| 8145 | (PyObject*) &StatVFSResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8146 | } |