Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 14 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 15 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 16 | #define PY_SSIZE_T_CLEAN |
| 17 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 18 | #include "Python.h" |
| 19 | #include "structseq.h" |
| 20 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 21 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 22 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 23 | #endif /* defined(__VMS) */ |
| 24 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 25 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 26 | "This module provides access to operating system functionality that is\n\ |
| 27 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 28 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 29 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 30 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 31 | #ifndef Py_USING_UNICODE |
| 32 | /* This is used in signatures of functions. */ |
| 33 | #define Py_UNICODE void |
| 34 | #endif |
| 35 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 36 | #if defined(PYOS_OS2) |
| 37 | #define INCL_DOS |
| 38 | #define INCL_DOSERRORS |
| 39 | #define INCL_DOSPROCESS |
| 40 | #define INCL_NOPMAPI |
| 41 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 42 | #if defined(PYCC_GCC) |
| 43 | #include <ctype.h> |
| 44 | #include <io.h> |
| 45 | #include <stdio.h> |
| 46 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 47 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 48 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 49 | #endif |
| 50 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 51 | #include <sys/types.h> |
| 52 | #include <sys/stat.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 54 | #ifdef HAVE_SYS_WAIT_H |
| 55 | #include <sys/wait.h> /* For WNOHANG */ |
| 56 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 58 | #include <signal.h> |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 60 | #ifdef HAVE_FCNTL_H |
| 61 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 62 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 64 | #ifdef HAVE_GRP_H |
| 65 | #include <grp.h> |
| 66 | #endif |
| 67 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 68 | #ifdef HAVE_SYSEXITS_H |
| 69 | #include <sysexits.h> |
| 70 | #endif /* HAVE_SYSEXITS_H */ |
| 71 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 72 | #ifdef HAVE_SYS_LOADAVG_H |
| 73 | #include <sys/loadavg.h> |
| 74 | #endif |
| 75 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 76 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 77 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 78 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 79 | #include <process.h> |
| 80 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 81 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 82 | #define HAVE_GETCWD 1 |
| 83 | #define HAVE_OPENDIR 1 |
| 84 | #define HAVE_SYSTEM 1 |
| 85 | #if defined(__OS2__) |
| 86 | #define HAVE_EXECV 1 |
| 87 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 88 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 89 | #include <process.h> |
| 90 | #else |
| 91 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 92 | #define HAVE_EXECV 1 |
| 93 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 94 | #define HAVE_OPENDIR 1 |
| 95 | #define HAVE_PIPE 1 |
| 96 | #define HAVE_POPEN 1 |
| 97 | #define HAVE_SYSTEM 1 |
| 98 | #define HAVE_WAIT 1 |
| 99 | #else |
| 100 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 101 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 102 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 103 | #define HAVE_EXECV 1 |
| 104 | #define HAVE_PIPE 1 |
| 105 | #define HAVE_POPEN 1 |
| 106 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 107 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 108 | #define HAVE_FSYNC 1 |
| 109 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 110 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 111 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 112 | /* 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] | 113 | #else /* all other compilers */ |
| 114 | /* Unix functions that the configure script doesn't check for */ |
| 115 | #define HAVE_EXECV 1 |
| 116 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 117 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 118 | #define HAVE_FORK1 1 |
| 119 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 120 | #define HAVE_GETCWD 1 |
| 121 | #define HAVE_GETEGID 1 |
| 122 | #define HAVE_GETEUID 1 |
| 123 | #define HAVE_GETGID 1 |
| 124 | #define HAVE_GETPPID 1 |
| 125 | #define HAVE_GETUID 1 |
| 126 | #define HAVE_KILL 1 |
| 127 | #define HAVE_OPENDIR 1 |
| 128 | #define HAVE_PIPE 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 129 | #ifndef __rtems__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 130 | #define HAVE_POPEN 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 131 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 132 | #define HAVE_SYSTEM 1 |
| 133 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 134 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 135 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 136 | #endif /* _MSC_VER */ |
| 137 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 138 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 139 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 140 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 141 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 142 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 143 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 144 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 145 | (default) */ |
| 146 | extern char *ctermid_r(char *); |
| 147 | #endif |
| 148 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 149 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 150 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 151 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 152 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 153 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 154 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 155 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 156 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 157 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 158 | #endif |
| 159 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 160 | extern int chdir(char *); |
| 161 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 162 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 163 | extern int chdir(const char *); |
| 164 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 165 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 166 | #ifdef __BORLANDC__ |
| 167 | extern int chmod(const char *, int); |
| 168 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 170 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 171 | extern int chown(const char *, uid_t, gid_t); |
| 172 | extern char *getcwd(char *, int); |
| 173 | extern char *strerror(int); |
| 174 | extern int link(const char *, const char *); |
| 175 | extern int rename(const char *, const char *); |
| 176 | extern int stat(const char *, struct stat *); |
| 177 | extern int unlink(const char *); |
| 178 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 179 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 180 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 181 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 182 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 183 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 184 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 185 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 187 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 188 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 189 | #ifdef HAVE_UTIME_H |
| 190 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 191 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 193 | #ifdef HAVE_SYS_UTIME_H |
| 194 | #include <sys/utime.h> |
| 195 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 196 | #endif /* HAVE_SYS_UTIME_H */ |
| 197 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 198 | #ifdef HAVE_SYS_TIMES_H |
| 199 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 200 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 201 | |
| 202 | #ifdef HAVE_SYS_PARAM_H |
| 203 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 204 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | |
| 206 | #ifdef HAVE_SYS_UTSNAME_H |
| 207 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 208 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 210 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 211 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 212 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 213 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 214 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 215 | #include <direct.h> |
| 216 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 217 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 218 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 219 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 220 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 221 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 223 | #endif |
| 224 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 225 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 226 | #endif |
| 227 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 229 | #endif |
| 230 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 231 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 232 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 233 | #include <direct.h> |
| 234 | #include <io.h> |
| 235 | #include <process.h> |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 236 | #include "osdefs.h" |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 237 | #define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 238 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 239 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 240 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 241 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 242 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 244 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 246 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 247 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 248 | #ifndef MAXPATHLEN |
| 249 | #define MAXPATHLEN 1024 |
| 250 | #endif /* MAXPATHLEN */ |
| 251 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 252 | #ifdef UNION_WAIT |
| 253 | /* Emulate some macros on systems that have a union instead of macros */ |
| 254 | |
| 255 | #ifndef WIFEXITED |
| 256 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 257 | #endif |
| 258 | |
| 259 | #ifndef WEXITSTATUS |
| 260 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 261 | #endif |
| 262 | |
| 263 | #ifndef WTERMSIG |
| 264 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 265 | #endif |
| 266 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 267 | #define WAIT_TYPE union wait |
| 268 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 269 | |
| 270 | #else /* !UNION_WAIT */ |
| 271 | #define WAIT_TYPE int |
| 272 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 273 | #endif /* UNION_WAIT */ |
| 274 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 275 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 276 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 277 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 278 | #define USE_CTERMID_R |
| 279 | #endif |
| 280 | |
| 281 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 282 | #define USE_TMPNAM_R |
| 283 | #endif |
| 284 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 285 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 286 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 287 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 288 | # define STAT win32_stat |
| 289 | # define FSTAT win32_fstat |
| 290 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 291 | #else |
| 292 | # define STAT stat |
| 293 | # define FSTAT fstat |
| 294 | # define STRUCT_STAT struct stat |
| 295 | #endif |
| 296 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 297 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 298 | #include <sys/mkdev.h> |
| 299 | #else |
| 300 | #if defined(MAJOR_IN_SYSMACROS) |
| 301 | #include <sys/sysmacros.h> |
| 302 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 303 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 304 | #include <sys/mkdev.h> |
| 305 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 306 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 307 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 308 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 309 | #ifdef WITH_NEXT_FRAMEWORK |
| 310 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 311 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 312 | */ |
| 313 | #include <crt_externs.h> |
| 314 | static char **environ; |
| 315 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 316 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 317 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 319 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 320 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 321 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 322 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 323 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 324 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 325 | if (d == NULL) |
| 326 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 327 | #ifdef WITH_NEXT_FRAMEWORK |
| 328 | if (environ == NULL) |
| 329 | environ = *_NSGetEnviron(); |
| 330 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 331 | if (environ == NULL) |
| 332 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 333 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 334 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 335 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 336 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 337 | char *p = strchr(*e, '='); |
| 338 | if (p == NULL) |
| 339 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 340 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 341 | if (k == NULL) { |
| 342 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 343 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 344 | } |
| 345 | v = PyString_FromString(p+1); |
| 346 | if (v == NULL) { |
| 347 | PyErr_Clear(); |
| 348 | Py_DECREF(k); |
| 349 | continue; |
| 350 | } |
| 351 | if (PyDict_GetItem(d, k) == NULL) { |
| 352 | if (PyDict_SetItem(d, k, v) != 0) |
| 353 | PyErr_Clear(); |
| 354 | } |
| 355 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 356 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 357 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 358 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 359 | { |
| 360 | APIRET rc; |
| 361 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 362 | |
| 363 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 364 | 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] | 365 | PyObject *v = PyString_FromString(buffer); |
| 366 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 367 | Py_DECREF(v); |
| 368 | } |
| 369 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 370 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 371 | PyObject *v = PyString_FromString(buffer); |
| 372 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 373 | Py_DECREF(v); |
| 374 | } |
| 375 | } |
| 376 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 377 | return d; |
| 378 | } |
| 379 | |
| 380 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 381 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 382 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 383 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 384 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 385 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 386 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 387 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 388 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 389 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 390 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 391 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 394 | #ifdef Py_WIN_WIDE_FILENAMES |
| 395 | static PyObject * |
| 396 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 397 | { |
| 398 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 399 | } |
| 400 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 401 | |
| 402 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 403 | static PyObject * |
| 404 | posix_error_with_allocated_filename(char* name) |
| 405 | { |
| 406 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 407 | PyMem_Free(name); |
| 408 | return rc; |
| 409 | } |
| 410 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 411 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 412 | static PyObject * |
| 413 | win32_error(char* function, char* filename) |
| 414 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 415 | /* XXX We should pass the function name along in the future. |
| 416 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 417 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 418 | Windows error object, which is non-trivial. |
| 419 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 420 | errno = GetLastError(); |
| 421 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 422 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 423 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 424 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 425 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 426 | |
| 427 | #ifdef Py_WIN_WIDE_FILENAMES |
| 428 | static PyObject * |
| 429 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 430 | { |
| 431 | /* XXX - see win32_error for comments on 'function' */ |
| 432 | errno = GetLastError(); |
| 433 | if (filename) |
| 434 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 435 | else |
| 436 | return PyErr_SetFromWindowsErr(errno); |
| 437 | } |
| 438 | |
| 439 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 440 | { |
| 441 | /* XXX Perhaps we should make this API an alias of |
| 442 | PyObject_Unicode() instead ?! */ |
| 443 | if (PyUnicode_CheckExact(obj)) { |
| 444 | Py_INCREF(obj); |
| 445 | return obj; |
| 446 | } |
| 447 | if (PyUnicode_Check(obj)) { |
| 448 | /* For a Unicode subtype that's not a Unicode object, |
| 449 | return a true Unicode object with the same data. */ |
| 450 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 451 | PyUnicode_GET_SIZE(obj)); |
| 452 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 453 | return PyUnicode_FromEncodedObject(obj, |
| 454 | Py_FileSystemDefaultEncoding, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 455 | "strict"); |
| 456 | } |
| 457 | |
| 458 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 459 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 460 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 461 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 462 | #if defined(PYOS_OS2) |
| 463 | /********************************************************************** |
| 464 | * Helper Function to Trim and Format OS/2 Messages |
| 465 | **********************************************************************/ |
| 466 | static void |
| 467 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 468 | { |
| 469 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 470 | |
| 471 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 472 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 473 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 474 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 475 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 476 | } |
| 477 | |
| 478 | /* Add Optional Reason Text */ |
| 479 | if (reason) { |
| 480 | strcat(msgbuf, " : "); |
| 481 | strcat(msgbuf, reason); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /********************************************************************** |
| 486 | * Decode an OS/2 Operating System Error Code |
| 487 | * |
| 488 | * A convenience function to lookup an OS/2 error code and return a |
| 489 | * text message we can use to raise a Python exception. |
| 490 | * |
| 491 | * Notes: |
| 492 | * The messages for errors returned from the OS/2 kernel reside in |
| 493 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 494 | * |
| 495 | **********************************************************************/ |
| 496 | static char * |
| 497 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 498 | { |
| 499 | APIRET rc; |
| 500 | ULONG msglen; |
| 501 | |
| 502 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 503 | Py_BEGIN_ALLOW_THREADS |
| 504 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 505 | errorcode, "oso001.msg", &msglen); |
| 506 | Py_END_ALLOW_THREADS |
| 507 | |
| 508 | if (rc == NO_ERROR) |
| 509 | os2_formatmsg(msgbuf, msglen, reason); |
| 510 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 511 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 512 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 513 | |
| 514 | return msgbuf; |
| 515 | } |
| 516 | |
| 517 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 518 | errors are not in a global variable e.g. 'errno' nor are |
| 519 | they congruent with posix error numbers. */ |
| 520 | |
| 521 | static PyObject * os2_error(int code) |
| 522 | { |
| 523 | char text[1024]; |
| 524 | PyObject *v; |
| 525 | |
| 526 | os2_strerror(text, sizeof(text), code, ""); |
| 527 | |
| 528 | v = Py_BuildValue("(is)", code, text); |
| 529 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 530 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 531 | Py_DECREF(v); |
| 532 | } |
| 533 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 534 | } |
| 535 | |
| 536 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 537 | |
| 538 | /* POSIX generic methods */ |
| 539 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 540 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 541 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 542 | { |
| 543 | int fd; |
| 544 | int res; |
| 545 | fd = PyObject_AsFileDescriptor(fdobj); |
| 546 | if (fd < 0) |
| 547 | return NULL; |
| 548 | Py_BEGIN_ALLOW_THREADS |
| 549 | res = (*func)(fd); |
| 550 | Py_END_ALLOW_THREADS |
| 551 | if (res < 0) |
| 552 | return posix_error(); |
| 553 | Py_INCREF(Py_None); |
| 554 | return Py_None; |
| 555 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 556 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 557 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 558 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 559 | unicode_file_names(void) |
| 560 | { |
| 561 | static int canusewide = -1; |
| 562 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 563 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 564 | the Windows NT family. */ |
| 565 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 566 | } |
| 567 | return canusewide; |
| 568 | } |
| 569 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 570 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 571 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 572 | posix_1str(PyObject *args, char *format, int (*func)(const char*), |
| 573 | char *wformat, int (*wfunc)(const Py_UNICODE*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 574 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 575 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 576 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 577 | #ifdef Py_WIN_WIDE_FILENAMES |
| 578 | if (unicode_file_names()) { |
| 579 | PyUnicodeObject *po; |
| 580 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 581 | Py_BEGIN_ALLOW_THREADS |
| 582 | /* PyUnicode_AS_UNICODE OK without thread |
| 583 | lock as it is a simple dereference. */ |
| 584 | res = (*wfunc)(PyUnicode_AS_UNICODE(po)); |
| 585 | Py_END_ALLOW_THREADS |
| 586 | if (res < 0) |
Mark Hammond | d389036 | 2002-10-03 07:24:48 +0000 | [diff] [blame] | 587 | return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 588 | Py_INCREF(Py_None); |
| 589 | return Py_None; |
| 590 | } |
| 591 | /* Drop the argument parsing error as narrow |
| 592 | strings are also valid. */ |
| 593 | PyErr_Clear(); |
| 594 | } |
| 595 | #else |
| 596 | /* Platforms that don't support Unicode filenames |
| 597 | shouldn't be passing these extra params */ |
| 598 | assert(wformat==NULL && wfunc == NULL); |
| 599 | #endif |
| 600 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 601 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 602 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 603 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 604 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 605 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 606 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 607 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 608 | return posix_error_with_allocated_filename(path1); |
| 609 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 610 | Py_INCREF(Py_None); |
| 611 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 614 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 615 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 616 | char *format, |
| 617 | int (*func)(const char *, const char *), |
| 618 | char *wformat, |
| 619 | int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 620 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 621 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 622 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 623 | #ifdef Py_WIN_WIDE_FILENAMES |
| 624 | if (unicode_file_names()) { |
| 625 | PyObject *po1; |
| 626 | PyObject *po2; |
| 627 | if (PyArg_ParseTuple(args, wformat, &po1, &po2)) { |
| 628 | if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) { |
| 629 | PyObject *wpath1; |
| 630 | PyObject *wpath2; |
| 631 | wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1); |
| 632 | wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2); |
| 633 | if (!wpath1 || !wpath2) { |
| 634 | Py_XDECREF(wpath1); |
| 635 | Py_XDECREF(wpath2); |
| 636 | return NULL; |
| 637 | } |
| 638 | Py_BEGIN_ALLOW_THREADS |
| 639 | /* PyUnicode_AS_UNICODE OK without thread |
| 640 | lock as it is a simple dereference. */ |
| 641 | res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1), |
| 642 | PyUnicode_AS_UNICODE(wpath2)); |
| 643 | Py_END_ALLOW_THREADS |
| 644 | Py_XDECREF(wpath1); |
| 645 | Py_XDECREF(wpath2); |
| 646 | if (res != 0) |
| 647 | return posix_error(); |
| 648 | Py_INCREF(Py_None); |
| 649 | return Py_None; |
| 650 | } |
| 651 | /* Else flow through as neither is Unicode. */ |
| 652 | } |
| 653 | /* Drop the argument parsing error as narrow |
| 654 | strings are also valid. */ |
| 655 | PyErr_Clear(); |
| 656 | } |
| 657 | #else |
| 658 | /* Platforms that don't support Unicode filenames |
| 659 | shouldn't be passing these extra params */ |
| 660 | assert(wformat==NULL && wfunc == NULL); |
| 661 | #endif |
| 662 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 663 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 664 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 665 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 666 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 667 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 668 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 669 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 670 | PyMem_Free(path1); |
| 671 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 672 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 673 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 674 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 675 | Py_INCREF(Py_None); |
| 676 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 679 | #ifdef MS_WINDOWS |
| 680 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 681 | - time stamps are restricted to second resolution |
| 682 | - file modification times suffer from forth-and-back conversions between |
| 683 | UTC and local time |
| 684 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 685 | */ |
| 686 | #define HAVE_STAT_NSEC 1 |
| 687 | |
| 688 | struct win32_stat{ |
| 689 | int st_dev; |
| 690 | __int64 st_ino; |
| 691 | unsigned short st_mode; |
| 692 | int st_nlink; |
| 693 | int st_uid; |
| 694 | int st_gid; |
| 695 | int st_rdev; |
| 696 | __int64 st_size; |
| 697 | int st_atime; |
| 698 | int st_atime_nsec; |
| 699 | int st_mtime; |
| 700 | int st_mtime_nsec; |
| 701 | int st_ctime; |
| 702 | int st_ctime_nsec; |
| 703 | }; |
| 704 | |
| 705 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 706 | |
| 707 | static void |
| 708 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 709 | { |
| 710 | /* XXX endianness */ |
| 711 | __int64 in = *(__int64*)in_ptr; |
| 712 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 713 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 714 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 715 | } |
| 716 | |
| 717 | /* Below, we *know* that ugo+r is 0444 */ |
| 718 | #if _S_IREAD != 0400 |
| 719 | #error Unsupported C library |
| 720 | #endif |
| 721 | static int |
| 722 | attributes_to_mode(DWORD attr) |
| 723 | { |
| 724 | int m = 0; |
| 725 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 726 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 727 | else |
| 728 | m |= _S_IFREG; |
| 729 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 730 | m |= 0444; |
| 731 | else |
| 732 | m |= 0666; |
| 733 | return m; |
| 734 | } |
| 735 | |
| 736 | static int |
| 737 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 738 | { |
| 739 | memset(result, 0, sizeof(*result)); |
| 740 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 741 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 742 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 743 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 744 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static int |
| 750 | win32_stat(const char* path, struct win32_stat *result) |
| 751 | { |
| 752 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 753 | int code; |
| 754 | char *dot; |
| 755 | /* XXX not supported on Win95 and NT 3.x */ |
| 756 | if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
| 757 | /* Protocol violation: we explicitly clear errno, instead of |
| 758 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 759 | errno = 0; |
| 760 | return -1; |
| 761 | } |
| 762 | code = attribute_data_to_stat(&info, result); |
| 763 | if (code != 0) |
| 764 | return code; |
| 765 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 766 | dot = strrchr(path, '.'); |
| 767 | if (dot) { |
| 768 | if (stricmp(dot, ".bat") == 0 || |
| 769 | stricmp(dot, ".cmd") == 0 || |
| 770 | stricmp(dot, ".exe") == 0 || |
| 771 | stricmp(dot, ".com") == 0) |
| 772 | result->st_mode |= 0111; |
| 773 | } |
| 774 | return code; |
| 775 | } |
| 776 | |
| 777 | static int |
| 778 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 779 | { |
| 780 | int code; |
| 781 | const wchar_t *dot; |
| 782 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 783 | /* XXX not supported on Win95 and NT 3.x */ |
| 784 | if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
| 785 | /* Protocol violation: we explicitly clear errno, instead of |
| 786 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 787 | errno = 0; |
| 788 | return -1; |
| 789 | } |
| 790 | code = attribute_data_to_stat(&info, result); |
| 791 | if (code < 0) |
| 792 | return code; |
| 793 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 794 | dot = wcsrchr(path, '.'); |
| 795 | if (dot) { |
| 796 | if (_wcsicmp(dot, L".bat") == 0 || |
| 797 | _wcsicmp(dot, L".cmd") == 0 || |
| 798 | _wcsicmp(dot, L".exe") == 0 || |
| 799 | _wcsicmp(dot, L".com") == 0) |
| 800 | result->st_mode |= 0111; |
| 801 | } |
| 802 | return code; |
| 803 | } |
| 804 | |
| 805 | static int |
| 806 | win32_fstat(int file_number, struct win32_stat *result) |
| 807 | { |
| 808 | BY_HANDLE_FILE_INFORMATION info; |
| 809 | HANDLE h; |
| 810 | int type; |
| 811 | |
| 812 | h = (HANDLE)_get_osfhandle(file_number); |
| 813 | |
| 814 | /* Protocol violation: we explicitly clear errno, instead of |
| 815 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 816 | errno = 0; |
| 817 | |
| 818 | if (h == INVALID_HANDLE_VALUE) { |
| 819 | /* This is really a C library error (invalid file handle). |
| 820 | We set the Win32 error to the closes one matching. */ |
| 821 | SetLastError(ERROR_INVALID_HANDLE); |
| 822 | return -1; |
| 823 | } |
| 824 | memset(result, 0, sizeof(*result)); |
| 825 | |
| 826 | type = GetFileType(h); |
| 827 | if (type == FILE_TYPE_UNKNOWN) { |
| 828 | DWORD error = GetLastError(); |
| 829 | if (error != 0) { |
| 830 | return -1; |
| 831 | } |
| 832 | /* else: valid but unknown file */ |
| 833 | } |
| 834 | |
| 835 | if (type != FILE_TYPE_DISK) { |
| 836 | if (type == FILE_TYPE_CHAR) |
| 837 | result->st_mode = _S_IFCHR; |
| 838 | else if (type == FILE_TYPE_PIPE) |
| 839 | result->st_mode = _S_IFIFO; |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | if (!GetFileInformationByHandle(h, &info)) { |
| 844 | return -1; |
| 845 | } |
| 846 | |
| 847 | /* similar to stat() */ |
| 848 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 849 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 850 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 851 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 852 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 853 | /* specific to fstat() */ |
| 854 | result->st_nlink = info.nNumberOfLinks; |
| 855 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | #endif /* MS_WINDOWS */ |
| 860 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 861 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 862 | "stat_result: Result from stat or lstat.\n\n\ |
| 863 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 864 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 865 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 866 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 867 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 868 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 869 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 870 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 871 | |
| 872 | static PyStructSequence_Field stat_result_fields[] = { |
| 873 | {"st_mode", "protection bits"}, |
| 874 | {"st_ino", "inode"}, |
| 875 | {"st_dev", "device"}, |
| 876 | {"st_nlink", "number of hard links"}, |
| 877 | {"st_uid", "user ID of owner"}, |
| 878 | {"st_gid", "group ID of owner"}, |
| 879 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 880 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 881 | {NULL, "integer time of last access"}, |
| 882 | {NULL, "integer time of last modification"}, |
| 883 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 884 | {"st_atime", "time of last access"}, |
| 885 | {"st_mtime", "time of last modification"}, |
| 886 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 887 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 888 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 889 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 890 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 891 | {"st_blocks", "number of blocks allocated"}, |
| 892 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 893 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 894 | {"st_rdev", "device type (if inode device)"}, |
| 895 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 896 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 897 | {"st_flags", "user defined flags for file"}, |
| 898 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 899 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 900 | {"st_gen", "generation number"}, |
| 901 | #endif |
| 902 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 903 | {"st_birthtime", "time of creation"}, |
| 904 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 905 | {0} |
| 906 | }; |
| 907 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 908 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 909 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 910 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 911 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 912 | #endif |
| 913 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 914 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 915 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 916 | #else |
| 917 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 918 | #endif |
| 919 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 920 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 921 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 922 | #else |
| 923 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 924 | #endif |
| 925 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 926 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 927 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 928 | #else |
| 929 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 930 | #endif |
| 931 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 932 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 933 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 934 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 935 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 936 | #endif |
| 937 | |
| 938 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 939 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 940 | #else |
| 941 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 942 | #endif |
| 943 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 944 | static PyStructSequence_Desc stat_result_desc = { |
| 945 | "stat_result", /* name */ |
| 946 | stat_result__doc__, /* doc */ |
| 947 | stat_result_fields, |
| 948 | 10 |
| 949 | }; |
| 950 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 951 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 952 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 953 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 954 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 955 | 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] | 956 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 957 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 958 | |
| 959 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 960 | {"f_bsize", }, |
| 961 | {"f_frsize", }, |
| 962 | {"f_blocks", }, |
| 963 | {"f_bfree", }, |
| 964 | {"f_bavail", }, |
| 965 | {"f_files", }, |
| 966 | {"f_ffree", }, |
| 967 | {"f_favail", }, |
| 968 | {"f_flag", }, |
| 969 | {"f_namemax",}, |
| 970 | {0} |
| 971 | }; |
| 972 | |
| 973 | static PyStructSequence_Desc statvfs_result_desc = { |
| 974 | "statvfs_result", /* name */ |
| 975 | statvfs_result__doc__, /* doc */ |
| 976 | statvfs_result_fields, |
| 977 | 10 |
| 978 | }; |
| 979 | |
| 980 | static PyTypeObject StatResultType; |
| 981 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 982 | static newfunc structseq_new; |
| 983 | |
| 984 | static PyObject * |
| 985 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 986 | { |
| 987 | PyStructSequence *result; |
| 988 | int i; |
| 989 | |
| 990 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 991 | if (!result) |
| 992 | return NULL; |
| 993 | /* If we have been initialized from a tuple, |
| 994 | st_?time might be set to None. Initialize it |
| 995 | from the int slots. */ |
| 996 | for (i = 7; i <= 9; i++) { |
| 997 | if (result->ob_item[i+3] == Py_None) { |
| 998 | Py_DECREF(Py_None); |
| 999 | Py_INCREF(result->ob_item[i]); |
| 1000 | result->ob_item[i+3] = result->ob_item[i]; |
| 1001 | } |
| 1002 | } |
| 1003 | return (PyObject*)result; |
| 1004 | } |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1009 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1010 | |
| 1011 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1012 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1013 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1014 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1015 | future calls return ints. \n\ |
| 1016 | If newval is omitted, return the current setting.\n"); |
| 1017 | |
| 1018 | static PyObject* |
| 1019 | stat_float_times(PyObject* self, PyObject *args) |
| 1020 | { |
| 1021 | int newval = -1; |
| 1022 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1023 | return NULL; |
| 1024 | if (newval == -1) |
| 1025 | /* Return old value */ |
| 1026 | return PyBool_FromLong(_stat_float_times); |
| 1027 | _stat_float_times = newval; |
| 1028 | Py_INCREF(Py_None); |
| 1029 | return Py_None; |
| 1030 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1031 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1032 | static void |
| 1033 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1034 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1035 | PyObject *fval,*ival; |
| 1036 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1037 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1038 | #else |
| 1039 | ival = PyInt_FromLong((long)sec); |
| 1040 | #endif |
| 1041 | if (_stat_float_times) { |
| 1042 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1043 | } else { |
| 1044 | fval = ival; |
| 1045 | Py_INCREF(fval); |
| 1046 | } |
| 1047 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1048 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1051 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1052 | (used by posix_stat() and posix_fstat()) */ |
| 1053 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1054 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1055 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1056 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1057 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1058 | if (v == NULL) |
| 1059 | return NULL; |
| 1060 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1061 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1062 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1063 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1064 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1065 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1066 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1067 | #endif |
| 1068 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1069 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1070 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1071 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1072 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1073 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1074 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); |
| 1075 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); |
| 1076 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1077 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1078 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1079 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1080 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1081 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1082 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1083 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1084 | #if defined(HAVE_STAT_TV_NSEC) |
| 1085 | ansec = st->st_atim.tv_nsec; |
| 1086 | mnsec = st->st_mtim.tv_nsec; |
| 1087 | cnsec = st->st_ctim.tv_nsec; |
| 1088 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1089 | ansec = st->st_atimespec.tv_nsec; |
| 1090 | mnsec = st->st_mtimespec.tv_nsec; |
| 1091 | cnsec = st->st_ctimespec.tv_nsec; |
| 1092 | #elif defined(HAVE_STAT_NSEC) |
| 1093 | ansec = st->st_atime_nsec; |
| 1094 | mnsec = st->st_mtime_nsec; |
| 1095 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1096 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1097 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1098 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1099 | fill_time(v, 7, st->st_atime, ansec); |
| 1100 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1101 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1102 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1103 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1104 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1105 | PyInt_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1106 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1107 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1108 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1109 | PyInt_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1110 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1111 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1112 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1113 | PyInt_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1114 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1115 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1116 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1117 | PyInt_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1118 | #endif |
| 1119 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1120 | { |
| 1121 | PyObject *val; |
| 1122 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1123 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1124 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1125 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1126 | #else |
| 1127 | bnsec = 0; |
| 1128 | #endif |
| 1129 | if (_stat_float_times) { |
| 1130 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1131 | } else { |
| 1132 | val = PyInt_FromLong((long)bsec); |
| 1133 | } |
| 1134 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1135 | val); |
| 1136 | } |
| 1137 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1138 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1139 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1140 | PyInt_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1141 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1142 | |
| 1143 | if (PyErr_Occurred()) { |
| 1144 | Py_DECREF(v); |
| 1145 | return NULL; |
| 1146 | } |
| 1147 | |
| 1148 | return v; |
| 1149 | } |
| 1150 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1151 | #ifdef MS_WINDOWS |
| 1152 | |
| 1153 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1154 | where / can be used in place of \ and the trailing slash is optional. |
| 1155 | Both SERVER and SHARE must have at least one character. |
| 1156 | */ |
| 1157 | |
| 1158 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1159 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
| 1160 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
| 1161 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1162 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1163 | IsUNCRootA(char *path, int pathlen) |
| 1164 | { |
| 1165 | #define ISSLASH ISSLASHA |
| 1166 | |
| 1167 | int i, share; |
| 1168 | |
| 1169 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1170 | /* minimum UNCRoot is \\x\y */ |
| 1171 | return FALSE; |
| 1172 | for (i = 2; i < pathlen ; i++) |
| 1173 | if (ISSLASH(path[i])) break; |
| 1174 | if (i == 2 || i == pathlen) |
| 1175 | /* do not allow \\\SHARE or \\SERVER */ |
| 1176 | return FALSE; |
| 1177 | share = i+1; |
| 1178 | for (i = share; i < pathlen; i++) |
| 1179 | if (ISSLASH(path[i])) break; |
| 1180 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1181 | |
| 1182 | #undef ISSLASH |
| 1183 | } |
| 1184 | |
| 1185 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1186 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1187 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1188 | { |
| 1189 | #define ISSLASH ISSLASHW |
| 1190 | |
| 1191 | int i, share; |
| 1192 | |
| 1193 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1194 | /* minimum UNCRoot is \\x\y */ |
| 1195 | return FALSE; |
| 1196 | for (i = 2; i < pathlen ; i++) |
| 1197 | if (ISSLASH(path[i])) break; |
| 1198 | if (i == 2 || i == pathlen) |
| 1199 | /* do not allow \\\SHARE or \\SERVER */ |
| 1200 | return FALSE; |
| 1201 | share = i+1; |
| 1202 | for (i = share; i < pathlen; i++) |
| 1203 | if (ISSLASH(path[i])) break; |
| 1204 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1205 | |
| 1206 | #undef ISSLASH |
| 1207 | } |
| 1208 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1209 | #endif /* MS_WINDOWS */ |
| 1210 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1211 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1212 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1213 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1214 | #ifdef __VMS |
| 1215 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1216 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1217 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1218 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1219 | char *wformat, |
| 1220 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1221 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1222 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1223 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1224 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1225 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1226 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1227 | |
| 1228 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1229 | /* If on wide-character-capable OS see if argument |
| 1230 | is Unicode and if so use wide API. */ |
| 1231 | if (unicode_file_names()) { |
| 1232 | PyUnicodeObject *po; |
| 1233 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1234 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1235 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1236 | Py_BEGIN_ALLOW_THREADS |
| 1237 | /* PyUnicode_AS_UNICODE result OK without |
| 1238 | thread lock as it is a simple dereference. */ |
| 1239 | res = wstatfunc(wpath, &st); |
| 1240 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1241 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1242 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1243 | return win32_error_unicode("stat", wpath); |
| 1244 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1245 | } |
| 1246 | /* Drop the argument parsing error as narrow strings |
| 1247 | are also valid. */ |
| 1248 | PyErr_Clear(); |
| 1249 | } |
| 1250 | #endif |
| 1251 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1252 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1253 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1254 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1255 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1256 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1257 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1258 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1259 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1260 | |
| 1261 | if (res != 0) { |
| 1262 | #ifdef MS_WINDOWS |
| 1263 | result = win32_error("stat", pathfree); |
| 1264 | #else |
| 1265 | result = posix_error_with_filename(pathfree); |
| 1266 | #endif |
| 1267 | } |
| 1268 | else |
| 1269 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1270 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1271 | PyMem_Free(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1272 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1275 | /* POSIX methods */ |
| 1276 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1277 | PyDoc_STRVAR(posix_access__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1278 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1279 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1280 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1281 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1282 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1283 | 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] | 1284 | |
| 1285 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1286 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1287 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1288 | char *path; |
| 1289 | int mode; |
| 1290 | int res; |
| 1291 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1292 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1293 | if (unicode_file_names()) { |
| 1294 | PyUnicodeObject *po; |
| 1295 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1296 | Py_BEGIN_ALLOW_THREADS |
| 1297 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1298 | it is a simple dereference. */ |
| 1299 | res = _waccess(PyUnicode_AS_UNICODE(po), mode); |
| 1300 | Py_END_ALLOW_THREADS |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1301 | return PyBool_FromLong(res == 0); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1302 | } |
| 1303 | /* Drop the argument parsing error as narrow strings |
| 1304 | are also valid. */ |
| 1305 | PyErr_Clear(); |
| 1306 | } |
| 1307 | #endif |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame] | 1308 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1309 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1310 | return NULL; |
| 1311 | Py_BEGIN_ALLOW_THREADS |
| 1312 | res = access(path, mode); |
| 1313 | Py_END_ALLOW_THREADS |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 1314 | PyMem_Free(path); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1315 | return PyBool_FromLong(res == 0); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1318 | #ifndef F_OK |
| 1319 | #define F_OK 0 |
| 1320 | #endif |
| 1321 | #ifndef R_OK |
| 1322 | #define R_OK 4 |
| 1323 | #endif |
| 1324 | #ifndef W_OK |
| 1325 | #define W_OK 2 |
| 1326 | #endif |
| 1327 | #ifndef X_OK |
| 1328 | #define X_OK 1 |
| 1329 | #endif |
| 1330 | |
| 1331 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1332 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1333 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1334 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1335 | |
| 1336 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1337 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1338 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1339 | int id; |
| 1340 | char *ret; |
| 1341 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1342 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1343 | return NULL; |
| 1344 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1345 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1346 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1347 | if (id == 0) { |
| 1348 | ret = ttyname(); |
| 1349 | } |
| 1350 | else { |
| 1351 | ret = NULL; |
| 1352 | } |
| 1353 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1354 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1355 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1356 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1357 | return posix_error(); |
| 1358 | return PyString_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1359 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1360 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1361 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1362 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1363 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1364 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1365 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1366 | |
| 1367 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1368 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1369 | { |
| 1370 | char *ret; |
| 1371 | char buffer[L_ctermid]; |
| 1372 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1373 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1374 | ret = ctermid_r(buffer); |
| 1375 | #else |
| 1376 | ret = ctermid(buffer); |
| 1377 | #endif |
| 1378 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1379 | return posix_error(); |
| 1380 | return PyString_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1381 | } |
| 1382 | #endif |
| 1383 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1384 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1385 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1386 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1387 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1388 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1389 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1390 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1391 | #ifdef MS_WINDOWS |
| 1392 | return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir); |
| 1393 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1394 | return posix_1str(args, "et:chdir", _chdir2, NULL, NULL); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1395 | #elif defined(__VMS) |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1396 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1397 | NULL, NULL); |
| 1398 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1399 | return posix_1str(args, "et:chdir", chdir, NULL, NULL); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1400 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1403 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1404 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1405 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1406 | 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] | 1407 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1408 | |
| 1409 | static PyObject * |
| 1410 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1411 | { |
| 1412 | return posix_fildes(fdobj, fchdir); |
| 1413 | } |
| 1414 | #endif /* HAVE_FCHDIR */ |
| 1415 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1416 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1417 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1418 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1419 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1420 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1421 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1422 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1423 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1424 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1425 | int i; |
| 1426 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1427 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1428 | if (unicode_file_names()) { |
| 1429 | PyUnicodeObject *po; |
| 1430 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1431 | Py_BEGIN_ALLOW_THREADS |
| 1432 | res = _wchmod(PyUnicode_AS_UNICODE(po), i); |
| 1433 | Py_END_ALLOW_THREADS |
| 1434 | if (res < 0) |
| 1435 | return posix_error_with_unicode_filename( |
| 1436 | PyUnicode_AS_UNICODE(po)); |
| 1437 | Py_INCREF(Py_None); |
| 1438 | return Py_None; |
| 1439 | } |
| 1440 | /* Drop the argument parsing error as narrow strings |
| 1441 | are also valid. */ |
| 1442 | PyErr_Clear(); |
| 1443 | } |
| 1444 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1445 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1446 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1447 | return NULL; |
| 1448 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1449 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1450 | Py_END_ALLOW_THREADS |
| 1451 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1452 | return posix_error_with_allocated_filename(path); |
| 1453 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1454 | Py_INCREF(Py_None); |
| 1455 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1458 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1459 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1460 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1461 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1462 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1463 | |
| 1464 | static PyObject * |
| 1465 | posix_chroot(PyObject *self, PyObject *args) |
| 1466 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1467 | return posix_1str(args, "et:chroot", chroot, NULL, NULL); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1468 | } |
| 1469 | #endif |
| 1470 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1471 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1472 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1473 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1474 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1475 | |
| 1476 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1477 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1478 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1479 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1480 | } |
| 1481 | #endif /* HAVE_FSYNC */ |
| 1482 | |
| 1483 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1484 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1485 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1486 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1487 | #endif |
| 1488 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1489 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1490 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1491 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1492 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1493 | |
| 1494 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1495 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1496 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1497 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1498 | } |
| 1499 | #endif /* HAVE_FDATASYNC */ |
| 1500 | |
| 1501 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1502 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1503 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1504 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1505 | 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] | 1506 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1507 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1508 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1509 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1510 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1511 | int uid, gid; |
| 1512 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1513 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1514 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1515 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1516 | return NULL; |
| 1517 | Py_BEGIN_ALLOW_THREADS |
| 1518 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1519 | Py_END_ALLOW_THREADS |
| 1520 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1521 | return posix_error_with_allocated_filename(path); |
| 1522 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1523 | Py_INCREF(Py_None); |
| 1524 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1525 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1526 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1527 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1528 | #ifdef HAVE_LCHOWN |
| 1529 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1530 | "lchown(path, uid, gid)\n\n\ |
| 1531 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1532 | This function will not follow symbolic links."); |
| 1533 | |
| 1534 | static PyObject * |
| 1535 | posix_lchown(PyObject *self, PyObject *args) |
| 1536 | { |
| 1537 | char *path = NULL; |
| 1538 | int uid, gid; |
| 1539 | int res; |
| 1540 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1541 | Py_FileSystemDefaultEncoding, &path, |
| 1542 | &uid, &gid)) |
| 1543 | return NULL; |
| 1544 | Py_BEGIN_ALLOW_THREADS |
| 1545 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1546 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1547 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1548 | return posix_error_with_allocated_filename(path); |
| 1549 | PyMem_Free(path); |
| 1550 | Py_INCREF(Py_None); |
| 1551 | return Py_None; |
| 1552 | } |
| 1553 | #endif /* HAVE_LCHOWN */ |
| 1554 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1555 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1556 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1557 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1558 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1559 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1560 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1561 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1562 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1563 | { |
| 1564 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1565 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1566 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1567 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1568 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1569 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1570 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1571 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1572 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1573 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1574 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1575 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1576 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1577 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1578 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1579 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1580 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1581 | "getcwdu() -> path\n\n\ |
| 1582 | Return a unicode string representing the current working directory."); |
| 1583 | |
| 1584 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1585 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1586 | { |
| 1587 | char buf[1026]; |
| 1588 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1589 | |
| 1590 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1591 | if (unicode_file_names()) { |
| 1592 | wchar_t *wres; |
| 1593 | wchar_t wbuf[1026]; |
| 1594 | Py_BEGIN_ALLOW_THREADS |
| 1595 | wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]); |
| 1596 | Py_END_ALLOW_THREADS |
| 1597 | if (wres == NULL) |
| 1598 | return posix_error(); |
| 1599 | return PyUnicode_FromWideChar(wbuf, wcslen(wbuf)); |
| 1600 | } |
| 1601 | #endif |
| 1602 | |
| 1603 | Py_BEGIN_ALLOW_THREADS |
| 1604 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1605 | res = _getcwd2(buf, sizeof buf); |
| 1606 | #else |
| 1607 | res = getcwd(buf, sizeof buf); |
| 1608 | #endif |
| 1609 | Py_END_ALLOW_THREADS |
| 1610 | if (res == NULL) |
| 1611 | return posix_error(); |
| 1612 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1613 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1614 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1615 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1616 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1617 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1618 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1619 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1620 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1621 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1622 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1623 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1624 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1625 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1626 | return posix_2str(args, "etet:link", link, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1627 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1628 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1629 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1630 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1631 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1632 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1633 | Return a list containing the names of the entries in the directory.\n\ |
| 1634 | \n\ |
| 1635 | path: path of directory to list\n\ |
| 1636 | \n\ |
| 1637 | 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] | 1638 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1639 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1640 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1641 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1642 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1643 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1644 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1645 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1646 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1647 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1648 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1649 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1650 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1651 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 1652 | char namebuf[MAX_PATH*2+5]; |
| 1653 | char *bufptr = namebuf; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1654 | Py_ssize_t len = sizeof(namebuf)/sizeof(namebuf[0]); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1655 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1656 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1657 | /* If on wide-character-capable OS see if argument |
| 1658 | is Unicode and if so use wide API. */ |
| 1659 | if (unicode_file_names()) { |
| 1660 | PyUnicodeObject *po; |
| 1661 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1662 | WIN32_FIND_DATAW wFileData; |
| 1663 | Py_UNICODE wnamebuf[MAX_PATH*2+5]; |
| 1664 | Py_UNICODE wch; |
| 1665 | wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH); |
| 1666 | wnamebuf[MAX_PATH] = L'\0'; |
| 1667 | len = wcslen(wnamebuf); |
| 1668 | wch = (len > 0) ? wnamebuf[len-1] : L'\0'; |
| 1669 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1670 | wnamebuf[len++] = L'/'; |
| 1671 | wcscpy(wnamebuf + len, L"*.*"); |
| 1672 | if ((d = PyList_New(0)) == NULL) |
| 1673 | return NULL; |
| 1674 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1675 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1676 | errno = GetLastError(); |
| 1677 | if (errno == ERROR_FILE_NOT_FOUND) { |
| 1678 | return d; |
| 1679 | } |
| 1680 | Py_DECREF(d); |
| 1681 | return win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1682 | } |
| 1683 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1684 | /* Skip over . and .. */ |
| 1685 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 1686 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 1687 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1688 | if (v == NULL) { |
| 1689 | Py_DECREF(d); |
| 1690 | d = NULL; |
| 1691 | break; |
| 1692 | } |
| 1693 | if (PyList_Append(d, v) != 0) { |
| 1694 | Py_DECREF(v); |
| 1695 | Py_DECREF(d); |
| 1696 | d = NULL; |
| 1697 | break; |
| 1698 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1699 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1700 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1701 | Py_BEGIN_ALLOW_THREADS |
| 1702 | result = FindNextFileW(hFindFile, &wFileData); |
| 1703 | Py_END_ALLOW_THREADS |
| 1704 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1705 | |
| 1706 | if (FindClose(hFindFile) == FALSE) { |
| 1707 | Py_DECREF(d); |
| 1708 | return win32_error_unicode("FindClose", wnamebuf); |
| 1709 | } |
| 1710 | return d; |
| 1711 | } |
| 1712 | /* Drop the argument parsing error as narrow strings |
| 1713 | are also valid. */ |
| 1714 | PyErr_Clear(); |
| 1715 | } |
| 1716 | #endif |
| 1717 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1718 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1719 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1720 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1721 | if (len > 0) { |
| 1722 | char ch = namebuf[len-1]; |
| 1723 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 1724 | namebuf[len++] = '/'; |
| 1725 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1726 | strcpy(namebuf + len, "*.*"); |
| 1727 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1728 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1729 | return NULL; |
| 1730 | |
| 1731 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 1732 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1733 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 1734 | if (errno == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1735 | return d; |
| 1736 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1737 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1738 | } |
| 1739 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1740 | /* Skip over . and .. */ |
| 1741 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 1742 | strcmp(FileData.cFileName, "..") != 0) { |
| 1743 | v = PyString_FromString(FileData.cFileName); |
| 1744 | if (v == NULL) { |
| 1745 | Py_DECREF(d); |
| 1746 | d = NULL; |
| 1747 | break; |
| 1748 | } |
| 1749 | if (PyList_Append(d, v) != 0) { |
| 1750 | Py_DECREF(v); |
| 1751 | Py_DECREF(d); |
| 1752 | d = NULL; |
| 1753 | break; |
| 1754 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1755 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1756 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1757 | Py_BEGIN_ALLOW_THREADS |
| 1758 | result = FindNextFile(hFindFile, &FileData); |
| 1759 | Py_END_ALLOW_THREADS |
| 1760 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1761 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1762 | if (FindClose(hFindFile) == FALSE) { |
| 1763 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1764 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1765 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1766 | |
| 1767 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1768 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1769 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1770 | |
| 1771 | #ifndef MAX_PATH |
| 1772 | #define MAX_PATH CCHMAXPATH |
| 1773 | #endif |
| 1774 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 1775 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1776 | PyObject *d, *v; |
| 1777 | char namebuf[MAX_PATH+5]; |
| 1778 | HDIR hdir = 1; |
| 1779 | ULONG srchcnt = 1; |
| 1780 | FILEFINDBUF3 ep; |
| 1781 | APIRET rc; |
| 1782 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1783 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1784 | return NULL; |
| 1785 | if (len >= MAX_PATH) { |
| 1786 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1787 | return NULL; |
| 1788 | } |
| 1789 | strcpy(namebuf, name); |
| 1790 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1791 | if (*pt == ALTSEP) |
| 1792 | *pt = SEP; |
| 1793 | if (namebuf[len-1] != SEP) |
| 1794 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1795 | strcpy(namebuf + len, "*.*"); |
| 1796 | |
| 1797 | if ((d = PyList_New(0)) == NULL) |
| 1798 | return NULL; |
| 1799 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1800 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1801 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1802 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1803 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1804 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1805 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1806 | |
| 1807 | if (rc != NO_ERROR) { |
| 1808 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1809 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1812 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1813 | do { |
| 1814 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1815 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1816 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1817 | |
| 1818 | strcpy(namebuf, ep.achName); |
| 1819 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1820 | /* Leave Case of Name Alone -- In Native Form */ |
| 1821 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1822 | |
| 1823 | v = PyString_FromString(namebuf); |
| 1824 | if (v == NULL) { |
| 1825 | Py_DECREF(d); |
| 1826 | d = NULL; |
| 1827 | break; |
| 1828 | } |
| 1829 | if (PyList_Append(d, v) != 0) { |
| 1830 | Py_DECREF(v); |
| 1831 | Py_DECREF(d); |
| 1832 | d = NULL; |
| 1833 | break; |
| 1834 | } |
| 1835 | Py_DECREF(v); |
| 1836 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1837 | } |
| 1838 | |
| 1839 | return d; |
| 1840 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1841 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1842 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1843 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1844 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1845 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1846 | int arg_is_unicode = 1; |
| 1847 | |
| 1848 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 1849 | arg_is_unicode = 0; |
| 1850 | PyErr_Clear(); |
| 1851 | } |
| 1852 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1853 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1854 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1855 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1856 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1857 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1858 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1859 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1860 | return NULL; |
| 1861 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1862 | for (;;) { |
| 1863 | Py_BEGIN_ALLOW_THREADS |
| 1864 | ep = readdir(dirp); |
| 1865 | Py_END_ALLOW_THREADS |
| 1866 | if (ep == NULL) |
| 1867 | break; |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1868 | if (ep->d_name[0] == '.' && |
| 1869 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1870 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1871 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1872 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1873 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1874 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1875 | d = NULL; |
| 1876 | break; |
| 1877 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1878 | #ifdef Py_USING_UNICODE |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1879 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1880 | PyObject *w; |
| 1881 | |
| 1882 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1883 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1884 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 1885 | if (w != NULL) { |
| 1886 | Py_DECREF(v); |
| 1887 | v = w; |
| 1888 | } |
| 1889 | else { |
| 1890 | /* fall back to the original byte string, as |
| 1891 | discussed in patch #683592 */ |
| 1892 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1893 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1894 | } |
| 1895 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1896 | if (PyList_Append(d, v) != 0) { |
| 1897 | Py_DECREF(v); |
| 1898 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1899 | d = NULL; |
| 1900 | break; |
| 1901 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1902 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1903 | } |
| 1904 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1905 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1906 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1907 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1908 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1909 | #endif /* which OS */ |
| 1910 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1911 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1912 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1913 | /* A helper function for abspath on win32 */ |
| 1914 | static PyObject * |
| 1915 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1916 | { |
| 1917 | /* assume encoded strings wont more than double no of chars */ |
| 1918 | char inbuf[MAX_PATH*2]; |
| 1919 | char *inbufp = inbuf; |
Tim Peters | 67d70eb | 2006-03-01 04:35:45 +0000 | [diff] [blame] | 1920 | Py_ssize_t insize = sizeof(inbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1921 | char outbuf[MAX_PATH*2]; |
| 1922 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1923 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1924 | if (unicode_file_names()) { |
| 1925 | PyUnicodeObject *po; |
| 1926 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 1927 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 1928 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1929 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1930 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 1931 | woutbuf, &wtemp)) |
| 1932 | return win32_error("GetFullPathName", ""); |
| 1933 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 1934 | } |
| 1935 | /* Drop the argument parsing error as narrow strings |
| 1936 | are also valid. */ |
| 1937 | PyErr_Clear(); |
| 1938 | } |
| 1939 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1940 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1941 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1942 | &insize)) |
| 1943 | return NULL; |
| 1944 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1945 | outbuf, &temp)) |
| 1946 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 1947 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 1948 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 1949 | Py_FileSystemDefaultEncoding, NULL); |
| 1950 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1951 | return PyString_FromString(outbuf); |
| 1952 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1953 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1954 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1955 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1956 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1957 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1958 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1959 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1960 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1961 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1962 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1963 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1964 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1965 | |
| 1966 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1967 | if (unicode_file_names()) { |
| 1968 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 1969 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1970 | Py_BEGIN_ALLOW_THREADS |
| 1971 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1972 | it is a simple dereference. */ |
| 1973 | res = _wmkdir(PyUnicode_AS_UNICODE(po)); |
| 1974 | Py_END_ALLOW_THREADS |
| 1975 | if (res < 0) |
| 1976 | return posix_error(); |
| 1977 | Py_INCREF(Py_None); |
| 1978 | return Py_None; |
| 1979 | } |
| 1980 | /* Drop the argument parsing error as narrow strings |
| 1981 | are also valid. */ |
| 1982 | PyErr_Clear(); |
| 1983 | } |
| 1984 | #endif |
| 1985 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1986 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1987 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1988 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1989 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1990 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1991 | res = mkdir(path); |
| 1992 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1993 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1994 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1995 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1996 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1997 | return posix_error_with_allocated_filename(path); |
| 1998 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1999 | Py_INCREF(Py_None); |
| 2000 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2003 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2004 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2005 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2006 | #include <sys/resource.h> |
| 2007 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2008 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2009 | |
| 2010 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2011 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2012 | "nice(inc) -> new_priority\n\n\ |
| 2013 | 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] | 2014 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2015 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2016 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2017 | { |
| 2018 | int increment, value; |
| 2019 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2020 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2021 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2022 | |
| 2023 | /* There are two flavours of 'nice': one that returns the new |
| 2024 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2025 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2026 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2027 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2028 | If we are of the nice family that returns the new priority, we |
| 2029 | need to clear errno before the call, and check if errno is filled |
| 2030 | before calling posix_error() on a returnvalue of -1, because the |
| 2031 | -1 may be the actual new priority! */ |
| 2032 | |
| 2033 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2034 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2035 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2036 | if (value == 0) |
| 2037 | value = getpriority(PRIO_PROCESS, 0); |
| 2038 | #endif |
| 2039 | if (value == -1 && errno != 0) |
| 2040 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2041 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2042 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2043 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2044 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2045 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2046 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2047 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2048 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2049 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2050 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2051 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2052 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2053 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2054 | #ifdef MS_WINDOWS |
| 2055 | return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename); |
| 2056 | #else |
| 2057 | return posix_2str(args, "etet:rename", rename, NULL, NULL); |
| 2058 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2061 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2062 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2063 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2064 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2065 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2066 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2067 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2068 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2069 | #ifdef MS_WINDOWS |
| 2070 | return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir); |
| 2071 | #else |
| 2072 | return posix_1str(args, "et:rmdir", rmdir, NULL, NULL); |
| 2073 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2076 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2077 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2078 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2079 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2080 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2081 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2082 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2083 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2084 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2085 | 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] | 2086 | #else |
| 2087 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 2088 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2091 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2092 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2093 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2094 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2095 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2096 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2097 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2098 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2099 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2100 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2101 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2102 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2103 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2104 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2105 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2106 | Py_END_ALLOW_THREADS |
| 2107 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2108 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2109 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2110 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2111 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2112 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2113 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2114 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2115 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2116 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2117 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2118 | { |
| 2119 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2120 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2121 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2122 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2123 | if (i < 0) |
| 2124 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2125 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2128 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2129 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2130 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2131 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2132 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2133 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2134 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2135 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2136 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2137 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2138 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2139 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2140 | #ifdef MS_WINDOWS |
| 2141 | return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink); |
| 2142 | #else |
| 2143 | return posix_1str(args, "et:remove", unlink, NULL, NULL); |
| 2144 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2147 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2148 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2149 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2150 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2151 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2152 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2153 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2154 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2155 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2156 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2157 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2158 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2159 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2160 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2161 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2162 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2163 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2164 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2165 | u.sysname, |
| 2166 | u.nodename, |
| 2167 | u.release, |
| 2168 | u.version, |
| 2169 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2170 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2171 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2172 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2173 | static int |
| 2174 | extract_time(PyObject *t, long* sec, long* usec) |
| 2175 | { |
| 2176 | long intval; |
| 2177 | if (PyFloat_Check(t)) { |
| 2178 | double tval = PyFloat_AsDouble(t); |
| 2179 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 2180 | if (!intobj) |
| 2181 | return -1; |
| 2182 | intval = PyInt_AsLong(intobj); |
| 2183 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2184 | if (intval == -1 && PyErr_Occurred()) |
| 2185 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2186 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2187 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2188 | if (*usec < 0) |
| 2189 | /* If rounding gave us a negative number, |
| 2190 | truncate. */ |
| 2191 | *usec = 0; |
| 2192 | return 0; |
| 2193 | } |
| 2194 | intval = PyInt_AsLong(t); |
| 2195 | if (intval == -1 && PyErr_Occurred()) |
| 2196 | return -1; |
| 2197 | *sec = intval; |
| 2198 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2199 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2200 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2201 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2202 | PyDoc_STRVAR(posix_utime__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2203 | "utime(path, (atime, utime))\n\ |
| 2204 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2205 | 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] | 2206 | 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] | 2207 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2208 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2209 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2210 | { |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2211 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2212 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2213 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2214 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2215 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2216 | #if defined(HAVE_UTIMES) |
| 2217 | struct timeval buf[2]; |
| 2218 | #define ATIME buf[0].tv_sec |
| 2219 | #define MTIME buf[1].tv_sec |
| 2220 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2221 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2222 | struct utimbuf buf; |
| 2223 | #define ATIME buf.actime |
| 2224 | #define MTIME buf.modtime |
| 2225 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2226 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2227 | time_t buf[2]; |
| 2228 | #define ATIME buf[0] |
| 2229 | #define MTIME buf[1] |
| 2230 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2231 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2232 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2233 | int have_unicode_filename = 0; |
| 2234 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2235 | PyUnicodeObject *obwpath; |
| 2236 | wchar_t *wpath; |
| 2237 | if (unicode_file_names()) { |
| 2238 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2239 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2240 | have_unicode_filename = 1; |
| 2241 | } else |
| 2242 | /* Drop the argument parsing error as narrow strings |
| 2243 | are also valid. */ |
| 2244 | PyErr_Clear(); |
| 2245 | } |
| 2246 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 2247 | |
| 2248 | if (!have_unicode_filename && \ |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2249 | !PyArg_ParseTuple(args, "etO:utime", |
| 2250 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2251 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2252 | if (arg == Py_None) { |
| 2253 | /* optional time values not given */ |
| 2254 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2255 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2256 | if (have_unicode_filename) |
| 2257 | res = _wutime(wpath, NULL); |
| 2258 | else |
| 2259 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2260 | res = utime(path, NULL); |
| 2261 | Py_END_ALLOW_THREADS |
| 2262 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2263 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2264 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2265 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2266 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2267 | return NULL; |
| 2268 | } |
| 2269 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2270 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2271 | &atime, &ausec) == -1) { |
| 2272 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2273 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2274 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2275 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2276 | &mtime, &musec) == -1) { |
| 2277 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2278 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2279 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2280 | ATIME = atime; |
| 2281 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2282 | #ifdef HAVE_UTIMES |
| 2283 | buf[0].tv_usec = ausec; |
| 2284 | buf[1].tv_usec = musec; |
| 2285 | Py_BEGIN_ALLOW_THREADS |
| 2286 | res = utimes(path, buf); |
| 2287 | Py_END_ALLOW_THREADS |
| 2288 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2289 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2290 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2291 | if (have_unicode_filename) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 2292 | /* utime is OK with utimbuf, but _wutime insists |
| 2293 | on _utimbuf (the msvc headers assert the |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2294 | underscore version is ansi) */ |
| 2295 | res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG); |
| 2296 | else |
| 2297 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2298 | res = utime(path, UTIME_ARG); |
| 2299 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2300 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2301 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2302 | if (res < 0) { |
| 2303 | #ifdef Py_WIN_WIDE_FILENAMES |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2304 | if (have_unicode_filename) |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2305 | return posix_error_with_unicode_filename(wpath); |
| 2306 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2307 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2308 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2309 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2310 | Py_INCREF(Py_None); |
| 2311 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2312 | #undef UTIME_ARG |
| 2313 | #undef ATIME |
| 2314 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2317 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2318 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2319 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2320 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2321 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2322 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2323 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2324 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2325 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2326 | { |
| 2327 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2328 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2329 | return NULL; |
| 2330 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2331 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2334 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2335 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2336 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2337 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2338 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2339 | for (i = 0; i < count; i++) |
| 2340 | PyMem_Free(array[i]); |
| 2341 | PyMem_DEL(array); |
| 2342 | } |
| 2343 | #endif |
| 2344 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2345 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2346 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2347 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2348 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2349 | Execute an executable path with arguments, replacing current process.\n\ |
| 2350 | \n\ |
| 2351 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2352 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2353 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2354 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2355 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2356 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2357 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2358 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2359 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2360 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2361 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2362 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2363 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2364 | argv is a list or tuple of strings. */ |
| 2365 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2366 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2367 | Py_FileSystemDefaultEncoding, |
| 2368 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2369 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2370 | if (PyList_Check(argv)) { |
| 2371 | argc = PyList_Size(argv); |
| 2372 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2373 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2374 | else if (PyTuple_Check(argv)) { |
| 2375 | argc = PyTuple_Size(argv); |
| 2376 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2377 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2378 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2379 | 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] | 2380 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2381 | return NULL; |
| 2382 | } |
| 2383 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2384 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2385 | if (argvlist == NULL) { |
| 2386 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2387 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2388 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2389 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2390 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2391 | Py_FileSystemDefaultEncoding, |
| 2392 | &argvlist[i])) { |
| 2393 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2394 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2395 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2396 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2397 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2398 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2399 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2400 | } |
| 2401 | argvlist[argc] = NULL; |
| 2402 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2403 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2404 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2405 | /* If we get here it's definitely an error */ |
| 2406 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2407 | free_string_array(argvlist, argc); |
| 2408 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2409 | return posix_error(); |
| 2410 | } |
| 2411 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2412 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2413 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2414 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2415 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2416 | \n\ |
| 2417 | path: path of executable file\n\ |
| 2418 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2419 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2420 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2421 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2422 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2423 | { |
| 2424 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2425 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2426 | char **argvlist; |
| 2427 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2428 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2429 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2430 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2431 | int lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2432 | |
| 2433 | /* execve has three arguments: (path, argv, env), where |
| 2434 | argv is a list or tuple of strings and env is a dictionary |
| 2435 | like posix.environ. */ |
| 2436 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2437 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2438 | Py_FileSystemDefaultEncoding, |
| 2439 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2440 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2441 | if (PyList_Check(argv)) { |
| 2442 | argc = PyList_Size(argv); |
| 2443 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2444 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2445 | else if (PyTuple_Check(argv)) { |
| 2446 | argc = PyTuple_Size(argv); |
| 2447 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2448 | } |
| 2449 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2450 | PyErr_SetString(PyExc_TypeError, |
| 2451 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2452 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2453 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2454 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2455 | PyErr_SetString(PyExc_TypeError, |
| 2456 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2457 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2460 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2461 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2462 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2463 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2464 | } |
| 2465 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2466 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2467 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2468 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2469 | &argvlist[i])) |
| 2470 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2471 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2472 | goto fail_1; |
| 2473 | } |
| 2474 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2475 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2476 | argvlist[argc] = NULL; |
| 2477 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2478 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2479 | if (i < 0) |
| 2480 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2481 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2482 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2483 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2484 | goto fail_1; |
| 2485 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2486 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2487 | keys = PyMapping_Keys(env); |
| 2488 | vals = PyMapping_Values(env); |
| 2489 | if (!keys || !vals) |
| 2490 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2491 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2492 | PyErr_SetString(PyExc_TypeError, |
| 2493 | "execve(): env.keys() or env.values() is not a list"); |
| 2494 | goto fail_2; |
| 2495 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2496 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2497 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2498 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2499 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2500 | |
| 2501 | key = PyList_GetItem(keys, pos); |
| 2502 | val = PyList_GetItem(vals, pos); |
| 2503 | if (!key || !val) |
| 2504 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2505 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2506 | if (!PyArg_Parse( |
| 2507 | key, |
| 2508 | "s;execve() arg 3 contains a non-string key", |
| 2509 | &k) || |
| 2510 | !PyArg_Parse( |
| 2511 | val, |
| 2512 | "s;execve() arg 3 contains a non-string value", |
| 2513 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2514 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2515 | goto fail_2; |
| 2516 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2517 | |
| 2518 | #if defined(PYOS_OS2) |
| 2519 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2520 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2521 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2522 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2523 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2524 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2525 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2526 | goto fail_2; |
| 2527 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2528 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2529 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2530 | #if defined(PYOS_OS2) |
| 2531 | } |
| 2532 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2533 | } |
| 2534 | envlist[envc] = 0; |
| 2535 | |
| 2536 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2537 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2538 | /* If we get here it's definitely an error */ |
| 2539 | |
| 2540 | (void) posix_error(); |
| 2541 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2542 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2543 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2544 | PyMem_DEL(envlist[envc]); |
| 2545 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2546 | fail_1: |
| 2547 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2548 | Py_XDECREF(vals); |
| 2549 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2550 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2551 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2552 | return NULL; |
| 2553 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2554 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2555 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2556 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2557 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2558 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2559 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2560 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2561 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2562 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2563 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2564 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2565 | |
| 2566 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2567 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2568 | { |
| 2569 | char *path; |
| 2570 | PyObject *argv; |
| 2571 | char **argvlist; |
| 2572 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2573 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2574 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2575 | |
| 2576 | /* spawnv has three arguments: (mode, path, argv), where |
| 2577 | argv is a list or tuple of strings. */ |
| 2578 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2579 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2580 | Py_FileSystemDefaultEncoding, |
| 2581 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2582 | return NULL; |
| 2583 | if (PyList_Check(argv)) { |
| 2584 | argc = PyList_Size(argv); |
| 2585 | getitem = PyList_GetItem; |
| 2586 | } |
| 2587 | else if (PyTuple_Check(argv)) { |
| 2588 | argc = PyTuple_Size(argv); |
| 2589 | getitem = PyTuple_GetItem; |
| 2590 | } |
| 2591 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2592 | PyErr_SetString(PyExc_TypeError, |
| 2593 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2594 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2595 | return NULL; |
| 2596 | } |
| 2597 | |
| 2598 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2599 | if (argvlist == NULL) { |
| 2600 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2601 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2602 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2603 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2604 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2605 | Py_FileSystemDefaultEncoding, |
| 2606 | &argvlist[i])) { |
| 2607 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2608 | PyErr_SetString( |
| 2609 | PyExc_TypeError, |
| 2610 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2611 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2612 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2613 | } |
| 2614 | } |
| 2615 | argvlist[argc] = NULL; |
| 2616 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2617 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2618 | Py_BEGIN_ALLOW_THREADS |
| 2619 | spawnval = spawnv(mode, path, argvlist); |
| 2620 | Py_END_ALLOW_THREADS |
| 2621 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2622 | if (mode == _OLD_P_OVERLAY) |
| 2623 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2624 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2625 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2626 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2627 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2628 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2629 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2630 | free_string_array(argvlist, argc); |
| 2631 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2632 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2633 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2634 | return posix_error(); |
| 2635 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2636 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2637 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2638 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2639 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2640 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2644 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2645 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2646 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2647 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2648 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2649 | path: path of executable file\n\ |
| 2650 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2651 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2652 | |
| 2653 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2654 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2655 | { |
| 2656 | char *path; |
| 2657 | PyObject *argv, *env; |
| 2658 | char **argvlist; |
| 2659 | char **envlist; |
| 2660 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2661 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2662 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2663 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2664 | int lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2665 | |
| 2666 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 2667 | argv is a list or tuple of strings and env is a dictionary |
| 2668 | like posix.environ. */ |
| 2669 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2670 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 2671 | Py_FileSystemDefaultEncoding, |
| 2672 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2673 | return NULL; |
| 2674 | if (PyList_Check(argv)) { |
| 2675 | argc = PyList_Size(argv); |
| 2676 | getitem = PyList_GetItem; |
| 2677 | } |
| 2678 | else if (PyTuple_Check(argv)) { |
| 2679 | argc = PyTuple_Size(argv); |
| 2680 | getitem = PyTuple_GetItem; |
| 2681 | } |
| 2682 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2683 | PyErr_SetString(PyExc_TypeError, |
| 2684 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2685 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2686 | } |
| 2687 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2688 | PyErr_SetString(PyExc_TypeError, |
| 2689 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2690 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
| 2693 | argvlist = PyMem_NEW(char *, argc+1); |
| 2694 | if (argvlist == NULL) { |
| 2695 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2696 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2697 | } |
| 2698 | for (i = 0; i < argc; i++) { |
| 2699 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2700 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2701 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2702 | &argvlist[i])) |
| 2703 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2704 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2705 | goto fail_1; |
| 2706 | } |
| 2707 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2708 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2709 | argvlist[argc] = NULL; |
| 2710 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2711 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2712 | if (i < 0) |
| 2713 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2714 | envlist = PyMem_NEW(char *, i + 1); |
| 2715 | if (envlist == NULL) { |
| 2716 | PyErr_NoMemory(); |
| 2717 | goto fail_1; |
| 2718 | } |
| 2719 | envc = 0; |
| 2720 | keys = PyMapping_Keys(env); |
| 2721 | vals = PyMapping_Values(env); |
| 2722 | if (!keys || !vals) |
| 2723 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2724 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2725 | PyErr_SetString(PyExc_TypeError, |
| 2726 | "spawnve(): env.keys() or env.values() is not a list"); |
| 2727 | goto fail_2; |
| 2728 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2729 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2730 | for (pos = 0; pos < i; pos++) { |
| 2731 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2732 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2733 | |
| 2734 | key = PyList_GetItem(keys, pos); |
| 2735 | val = PyList_GetItem(vals, pos); |
| 2736 | if (!key || !val) |
| 2737 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2738 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2739 | if (!PyArg_Parse( |
| 2740 | key, |
| 2741 | "s;spawnve() arg 3 contains a non-string key", |
| 2742 | &k) || |
| 2743 | !PyArg_Parse( |
| 2744 | val, |
| 2745 | "s;spawnve() arg 3 contains a non-string value", |
| 2746 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2747 | { |
| 2748 | goto fail_2; |
| 2749 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2750 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2751 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2752 | if (p == NULL) { |
| 2753 | PyErr_NoMemory(); |
| 2754 | goto fail_2; |
| 2755 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2756 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2757 | envlist[envc++] = p; |
| 2758 | } |
| 2759 | envlist[envc] = 0; |
| 2760 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2761 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2762 | Py_BEGIN_ALLOW_THREADS |
| 2763 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2764 | Py_END_ALLOW_THREADS |
| 2765 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2766 | if (mode == _OLD_P_OVERLAY) |
| 2767 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2768 | |
| 2769 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2770 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2771 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2772 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2773 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2774 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2775 | (void) posix_error(); |
| 2776 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2777 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2778 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2779 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2780 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2781 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2782 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2783 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2784 | while (--envc >= 0) |
| 2785 | PyMem_DEL(envlist[envc]); |
| 2786 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2787 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2788 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2789 | Py_XDECREF(vals); |
| 2790 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2791 | fail_0: |
| 2792 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2793 | return res; |
| 2794 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2795 | |
| 2796 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 2797 | #if defined(PYOS_OS2) |
| 2798 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 2799 | "spawnvp(mode, file, args)\n\n\ |
| 2800 | Execute the program 'file' in a new process, using the environment\n\ |
| 2801 | search path to find the file.\n\ |
| 2802 | \n\ |
| 2803 | mode: mode of process creation\n\ |
| 2804 | file: executable file name\n\ |
| 2805 | args: tuple or list of strings"); |
| 2806 | |
| 2807 | static PyObject * |
| 2808 | posix_spawnvp(PyObject *self, PyObject *args) |
| 2809 | { |
| 2810 | char *path; |
| 2811 | PyObject *argv; |
| 2812 | char **argvlist; |
| 2813 | int mode, i, argc; |
| 2814 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2815 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2816 | |
| 2817 | /* spawnvp has three arguments: (mode, path, argv), where |
| 2818 | argv is a list or tuple of strings. */ |
| 2819 | |
| 2820 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 2821 | Py_FileSystemDefaultEncoding, |
| 2822 | &path, &argv)) |
| 2823 | return NULL; |
| 2824 | if (PyList_Check(argv)) { |
| 2825 | argc = PyList_Size(argv); |
| 2826 | getitem = PyList_GetItem; |
| 2827 | } |
| 2828 | else if (PyTuple_Check(argv)) { |
| 2829 | argc = PyTuple_Size(argv); |
| 2830 | getitem = PyTuple_GetItem; |
| 2831 | } |
| 2832 | else { |
| 2833 | PyErr_SetString(PyExc_TypeError, |
| 2834 | "spawnvp() arg 2 must be a tuple or list"); |
| 2835 | PyMem_Free(path); |
| 2836 | return NULL; |
| 2837 | } |
| 2838 | |
| 2839 | argvlist = PyMem_NEW(char *, argc+1); |
| 2840 | if (argvlist == NULL) { |
| 2841 | PyMem_Free(path); |
| 2842 | return PyErr_NoMemory(); |
| 2843 | } |
| 2844 | for (i = 0; i < argc; i++) { |
| 2845 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2846 | Py_FileSystemDefaultEncoding, |
| 2847 | &argvlist[i])) { |
| 2848 | free_string_array(argvlist, i); |
| 2849 | PyErr_SetString( |
| 2850 | PyExc_TypeError, |
| 2851 | "spawnvp() arg 2 must contain only strings"); |
| 2852 | PyMem_Free(path); |
| 2853 | return NULL; |
| 2854 | } |
| 2855 | } |
| 2856 | argvlist[argc] = NULL; |
| 2857 | |
| 2858 | Py_BEGIN_ALLOW_THREADS |
| 2859 | #if defined(PYCC_GCC) |
| 2860 | spawnval = spawnvp(mode, path, argvlist); |
| 2861 | #else |
| 2862 | spawnval = _spawnvp(mode, path, argvlist); |
| 2863 | #endif |
| 2864 | Py_END_ALLOW_THREADS |
| 2865 | |
| 2866 | free_string_array(argvlist, argc); |
| 2867 | PyMem_Free(path); |
| 2868 | |
| 2869 | if (spawnval == -1) |
| 2870 | return posix_error(); |
| 2871 | else |
| 2872 | return Py_BuildValue("l", (long) spawnval); |
| 2873 | } |
| 2874 | |
| 2875 | |
| 2876 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 2877 | "spawnvpe(mode, file, args, env)\n\n\ |
| 2878 | Execute the program 'file' in a new process, using the environment\n\ |
| 2879 | search path to find the file.\n\ |
| 2880 | \n\ |
| 2881 | mode: mode of process creation\n\ |
| 2882 | file: executable file name\n\ |
| 2883 | args: tuple or list of arguments\n\ |
| 2884 | env: dictionary of strings mapping to strings"); |
| 2885 | |
| 2886 | static PyObject * |
| 2887 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 2888 | { |
| 2889 | char *path; |
| 2890 | PyObject *argv, *env; |
| 2891 | char **argvlist; |
| 2892 | char **envlist; |
| 2893 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2894 | int mode, i, pos, argc, envc; |
| 2895 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2896 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2897 | int lastarg = 0; |
| 2898 | |
| 2899 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 2900 | argv is a list or tuple of strings and env is a dictionary |
| 2901 | like posix.environ. */ |
| 2902 | |
| 2903 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 2904 | Py_FileSystemDefaultEncoding, |
| 2905 | &path, &argv, &env)) |
| 2906 | return NULL; |
| 2907 | if (PyList_Check(argv)) { |
| 2908 | argc = PyList_Size(argv); |
| 2909 | getitem = PyList_GetItem; |
| 2910 | } |
| 2911 | else if (PyTuple_Check(argv)) { |
| 2912 | argc = PyTuple_Size(argv); |
| 2913 | getitem = PyTuple_GetItem; |
| 2914 | } |
| 2915 | else { |
| 2916 | PyErr_SetString(PyExc_TypeError, |
| 2917 | "spawnvpe() arg 2 must be a tuple or list"); |
| 2918 | goto fail_0; |
| 2919 | } |
| 2920 | if (!PyMapping_Check(env)) { |
| 2921 | PyErr_SetString(PyExc_TypeError, |
| 2922 | "spawnvpe() arg 3 must be a mapping object"); |
| 2923 | goto fail_0; |
| 2924 | } |
| 2925 | |
| 2926 | argvlist = PyMem_NEW(char *, argc+1); |
| 2927 | if (argvlist == NULL) { |
| 2928 | PyErr_NoMemory(); |
| 2929 | goto fail_0; |
| 2930 | } |
| 2931 | for (i = 0; i < argc; i++) { |
| 2932 | if (!PyArg_Parse((*getitem)(argv, i), |
| 2933 | "et;spawnvpe() arg 2 must contain only strings", |
| 2934 | Py_FileSystemDefaultEncoding, |
| 2935 | &argvlist[i])) |
| 2936 | { |
| 2937 | lastarg = i; |
| 2938 | goto fail_1; |
| 2939 | } |
| 2940 | } |
| 2941 | lastarg = argc; |
| 2942 | argvlist[argc] = NULL; |
| 2943 | |
| 2944 | i = PyMapping_Size(env); |
| 2945 | if (i < 0) |
| 2946 | goto fail_1; |
| 2947 | envlist = PyMem_NEW(char *, i + 1); |
| 2948 | if (envlist == NULL) { |
| 2949 | PyErr_NoMemory(); |
| 2950 | goto fail_1; |
| 2951 | } |
| 2952 | envc = 0; |
| 2953 | keys = PyMapping_Keys(env); |
| 2954 | vals = PyMapping_Values(env); |
| 2955 | if (!keys || !vals) |
| 2956 | goto fail_2; |
| 2957 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2958 | PyErr_SetString(PyExc_TypeError, |
| 2959 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 2960 | goto fail_2; |
| 2961 | } |
| 2962 | |
| 2963 | for (pos = 0; pos < i; pos++) { |
| 2964 | char *p, *k, *v; |
| 2965 | size_t len; |
| 2966 | |
| 2967 | key = PyList_GetItem(keys, pos); |
| 2968 | val = PyList_GetItem(vals, pos); |
| 2969 | if (!key || !val) |
| 2970 | goto fail_2; |
| 2971 | |
| 2972 | if (!PyArg_Parse( |
| 2973 | key, |
| 2974 | "s;spawnvpe() arg 3 contains a non-string key", |
| 2975 | &k) || |
| 2976 | !PyArg_Parse( |
| 2977 | val, |
| 2978 | "s;spawnvpe() arg 3 contains a non-string value", |
| 2979 | &v)) |
| 2980 | { |
| 2981 | goto fail_2; |
| 2982 | } |
| 2983 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2984 | p = PyMem_NEW(char, len); |
| 2985 | if (p == NULL) { |
| 2986 | PyErr_NoMemory(); |
| 2987 | goto fail_2; |
| 2988 | } |
| 2989 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 2990 | envlist[envc++] = p; |
| 2991 | } |
| 2992 | envlist[envc] = 0; |
| 2993 | |
| 2994 | Py_BEGIN_ALLOW_THREADS |
| 2995 | #if defined(PYCC_GCC) |
| 2996 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2997 | #else |
| 2998 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 2999 | #endif |
| 3000 | Py_END_ALLOW_THREADS |
| 3001 | |
| 3002 | if (spawnval == -1) |
| 3003 | (void) posix_error(); |
| 3004 | else |
| 3005 | res = Py_BuildValue("l", (long) spawnval); |
| 3006 | |
| 3007 | fail_2: |
| 3008 | while (--envc >= 0) |
| 3009 | PyMem_DEL(envlist[envc]); |
| 3010 | PyMem_DEL(envlist); |
| 3011 | fail_1: |
| 3012 | free_string_array(argvlist, lastarg); |
| 3013 | Py_XDECREF(vals); |
| 3014 | Py_XDECREF(keys); |
| 3015 | fail_0: |
| 3016 | PyMem_Free(path); |
| 3017 | return res; |
| 3018 | } |
| 3019 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3020 | #endif /* HAVE_SPAWNV */ |
| 3021 | |
| 3022 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3023 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3024 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3025 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3026 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3027 | \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 | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3029 | |
| 3030 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3031 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3032 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3033 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3034 | if (pid == -1) |
| 3035 | return posix_error(); |
| 3036 | PyOS_AfterFork(); |
| 3037 | return PyInt_FromLong((long)pid); |
| 3038 | } |
| 3039 | #endif |
| 3040 | |
| 3041 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3042 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3043 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3044 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3045 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3046 | 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] | 3047 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3048 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3049 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3050 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3051 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3052 | if (pid == -1) |
| 3053 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3054 | if (pid == 0) |
| 3055 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3056 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3057 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3058 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3059 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3060 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3061 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3062 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3063 | #define DEV_PTY_FILE "/dev/ptc" |
| 3064 | #define HAVE_DEV_PTMX |
| 3065 | #else |
| 3066 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3067 | #endif |
| 3068 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3069 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3070 | #ifdef HAVE_PTY_H |
| 3071 | #include <pty.h> |
| 3072 | #else |
| 3073 | #ifdef HAVE_LIBUTIL_H |
| 3074 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3075 | #endif /* HAVE_LIBUTIL_H */ |
| 3076 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3077 | #ifdef HAVE_STROPTS_H |
| 3078 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3079 | #endif |
| 3080 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3081 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3082 | #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] | 3083 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3084 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3085 | 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] | 3086 | |
| 3087 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3088 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3089 | { |
| 3090 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3091 | #ifndef HAVE_OPENPTY |
| 3092 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3093 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3094 | #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] | 3095 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3096 | #ifdef sun |
| 3097 | extern char *ptsname(); |
| 3098 | #endif |
| 3099 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3100 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3101 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3102 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3103 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3104 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3105 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3106 | if (slave_name == NULL) |
| 3107 | return posix_error(); |
| 3108 | |
| 3109 | slave_fd = open(slave_name, O_RDWR); |
| 3110 | if (slave_fd < 0) |
| 3111 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3112 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3113 | 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] | 3114 | if (master_fd < 0) |
| 3115 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3116 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3117 | /* change permission of slave */ |
| 3118 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3119 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3120 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3121 | } |
| 3122 | /* unlock slave */ |
| 3123 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3124 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3125 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3126 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3127 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3128 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3129 | if (slave_name == NULL) |
| 3130 | return posix_error(); |
| 3131 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3132 | if (slave_fd < 0) |
| 3133 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3134 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3135 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3136 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3137 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3138 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3139 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3140 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3141 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3142 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3143 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3144 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3145 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3146 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3147 | |
| 3148 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3149 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3150 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3151 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3152 | 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] | 3153 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3154 | |
| 3155 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3156 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3157 | { |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 3158 | int master_fd = -1, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3159 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3160 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3161 | if (pid == -1) |
| 3162 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3163 | if (pid == 0) |
| 3164 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3165 | return Py_BuildValue("(ii)", pid, master_fd); |
| 3166 | } |
| 3167 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3168 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3169 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3170 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3171 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3172 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3173 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3174 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3175 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3176 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3177 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3178 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3179 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3180 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3181 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3182 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3183 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3184 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3185 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3186 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3187 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3188 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3189 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3190 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3191 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3192 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3193 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3194 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3195 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3196 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3197 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3198 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3199 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3200 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3201 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3202 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3203 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3204 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3205 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3206 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3207 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3208 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3209 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3210 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3211 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3212 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3213 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3214 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3215 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3218 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3219 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3220 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3221 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3222 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3223 | |
| 3224 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3225 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3226 | { |
| 3227 | PyObject *result = NULL; |
| 3228 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3229 | #ifdef NGROUPS_MAX |
| 3230 | #define MAX_GROUPS NGROUPS_MAX |
| 3231 | #else |
| 3232 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3233 | #define MAX_GROUPS 64 |
| 3234 | #endif |
| 3235 | gid_t grouplist[MAX_GROUPS]; |
| 3236 | int n; |
| 3237 | |
| 3238 | n = getgroups(MAX_GROUPS, grouplist); |
| 3239 | if (n < 0) |
| 3240 | posix_error(); |
| 3241 | else { |
| 3242 | result = PyList_New(n); |
| 3243 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3244 | int i; |
| 3245 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3246 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3247 | if (o == NULL) { |
| 3248 | Py_DECREF(result); |
| 3249 | result = NULL; |
| 3250 | break; |
| 3251 | } |
| 3252 | PyList_SET_ITEM(result, i, o); |
| 3253 | } |
| 3254 | } |
| 3255 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3256 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3257 | return result; |
| 3258 | } |
| 3259 | #endif |
| 3260 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3261 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3262 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3263 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3264 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3265 | |
| 3266 | static PyObject * |
| 3267 | posix_getpgid(PyObject *self, PyObject *args) |
| 3268 | { |
| 3269 | int pid, pgid; |
| 3270 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3271 | return NULL; |
| 3272 | pgid = getpgid(pid); |
| 3273 | if (pgid < 0) |
| 3274 | return posix_error(); |
| 3275 | return PyInt_FromLong((long)pgid); |
| 3276 | } |
| 3277 | #endif /* HAVE_GETPGID */ |
| 3278 | |
| 3279 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3280 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3281 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3282 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3283 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3284 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3285 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3286 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3287 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3288 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3289 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3290 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3291 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3292 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3293 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3294 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3295 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3296 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3297 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3298 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3299 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3300 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3301 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3302 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3303 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3304 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3305 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3306 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3307 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3308 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3309 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3310 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3311 | Py_INCREF(Py_None); |
| 3312 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3315 | #endif /* HAVE_SETPGRP */ |
| 3316 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3317 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3318 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3319 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3320 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3321 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3322 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3323 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3324 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3325 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3326 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3327 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3328 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3329 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3330 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3331 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3332 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3333 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3334 | |
| 3335 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3336 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3337 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3338 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3339 | char *name; |
| 3340 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3341 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3342 | errno = 0; |
| 3343 | name = getlogin(); |
| 3344 | if (name == NULL) { |
| 3345 | if (errno) |
| 3346 | posix_error(); |
| 3347 | else |
| 3348 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3349 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3350 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3351 | else |
| 3352 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3353 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3354 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3355 | return result; |
| 3356 | } |
| 3357 | #endif |
| 3358 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3359 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3360 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3361 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3362 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3363 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3364 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3365 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3366 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3367 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3368 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3369 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3370 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3371 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3372 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3373 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3374 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3375 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3376 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3377 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3378 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3379 | { |
| 3380 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3381 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3382 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3383 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3384 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3385 | APIRET rc; |
| 3386 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3387 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3388 | |
| 3389 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3390 | APIRET rc; |
| 3391 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3392 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3393 | |
| 3394 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3395 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3396 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3397 | if (kill(pid, sig) == -1) |
| 3398 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3399 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3400 | Py_INCREF(Py_None); |
| 3401 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3402 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3403 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3404 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3405 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3406 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3407 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3408 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3409 | |
| 3410 | static PyObject * |
| 3411 | posix_killpg(PyObject *self, PyObject *args) |
| 3412 | { |
| 3413 | int pgid, sig; |
| 3414 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3415 | return NULL; |
| 3416 | if (killpg(pgid, sig) == -1) |
| 3417 | return posix_error(); |
| 3418 | Py_INCREF(Py_None); |
| 3419 | return Py_None; |
| 3420 | } |
| 3421 | #endif |
| 3422 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3423 | #ifdef HAVE_PLOCK |
| 3424 | |
| 3425 | #ifdef HAVE_SYS_LOCK_H |
| 3426 | #include <sys/lock.h> |
| 3427 | #endif |
| 3428 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3429 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3430 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3431 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3432 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3433 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3434 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3435 | { |
| 3436 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3437 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3438 | return NULL; |
| 3439 | if (plock(op) == -1) |
| 3440 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3441 | Py_INCREF(Py_None); |
| 3442 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3443 | } |
| 3444 | #endif |
| 3445 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3446 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3447 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3448 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3449 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3450 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3451 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3452 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3453 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3454 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3455 | async_system(const char *command) |
| 3456 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3457 | char errormsg[256], args[1024]; |
| 3458 | RESULTCODES rcodes; |
| 3459 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3460 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3461 | char *shell = getenv("COMSPEC"); |
| 3462 | if (!shell) |
| 3463 | shell = "cmd"; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3464 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3465 | /* avoid overflowing the argument buffer */ |
| 3466 | if (strlen(shell) + 3 + strlen(command) >= 1024) |
| 3467 | return ERROR_NOT_ENOUGH_MEMORY |
| 3468 | |
| 3469 | args[0] = '\0'; |
| 3470 | strcat(args, shell); |
| 3471 | strcat(args, "/c "); |
| 3472 | strcat(args, command); |
| 3473 | |
| 3474 | /* execute asynchronously, inheriting the environment */ |
| 3475 | rc = DosExecPgm(errormsg, |
| 3476 | sizeof(errormsg), |
| 3477 | EXEC_ASYNC, |
| 3478 | args, |
| 3479 | NULL, |
| 3480 | &rcodes, |
| 3481 | shell); |
| 3482 | return rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3483 | } |
| 3484 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3485 | static FILE * |
| 3486 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3487 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3488 | int oldfd, tgtfd; |
| 3489 | HFILE pipeh[2]; |
| 3490 | APIRET rc; |
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 | /* mode determines which of stdin or stdout is reconnected to |
| 3493 | * the pipe to the child |
| 3494 | */ |
| 3495 | if (strchr(mode, 'r') != NULL) { |
| 3496 | tgt_fd = 1; /* stdout */ |
| 3497 | } else if (strchr(mode, 'w')) { |
| 3498 | tgt_fd = 0; /* stdin */ |
| 3499 | } else { |
| 3500 | *err = ERROR_INVALID_ACCESS; |
| 3501 | return NULL; |
| 3502 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3503 | |
Andrew MacIntyre | a3be258 | 2004-12-18 09:51:05 +0000 | [diff] [blame] | 3504 | /* setup the pipe */ |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3505 | if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) { |
| 3506 | *err = rc; |
| 3507 | return NULL; |
| 3508 | } |
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 | /* prevent other threads accessing stdio */ |
| 3511 | DosEnterCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3512 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3513 | /* reconnect stdio and execute child */ |
| 3514 | oldfd = dup(tgtfd); |
| 3515 | close(tgtfd); |
| 3516 | if (dup2(pipeh[tgtfd], tgtfd) == 0) { |
| 3517 | DosClose(pipeh[tgtfd]); |
| 3518 | rc = async_system(command); |
| 3519 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3520 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3521 | /* restore stdio */ |
| 3522 | dup2(oldfd, tgtfd); |
| 3523 | close(oldfd); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3524 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3525 | /* allow other threads access to stdio */ |
| 3526 | DosExitCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3527 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3528 | /* if execution of child was successful return file stream */ |
| 3529 | if (rc == NO_ERROR) |
| 3530 | return fdopen(pipeh[1 - tgtfd], mode); |
| 3531 | else { |
| 3532 | DosClose(pipeh[1 - tgtfd]); |
| 3533 | *err = rc; |
| 3534 | return NULL; |
| 3535 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
| 3538 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3539 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3540 | { |
| 3541 | char *name; |
| 3542 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3543 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3544 | FILE *fp; |
| 3545 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3546 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3547 | return NULL; |
| 3548 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3549 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3550 | Py_END_ALLOW_THREADS |
| 3551 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3552 | return os2_error(err); |
| 3553 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3554 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 3555 | if (f != NULL) |
| 3556 | PyFile_SetBufSize(f, bufsize); |
| 3557 | return f; |
| 3558 | } |
| 3559 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3560 | #elif defined(PYCC_GCC) |
| 3561 | |
| 3562 | /* standard posix version of popen() support */ |
| 3563 | static PyObject * |
| 3564 | posix_popen(PyObject *self, PyObject *args) |
| 3565 | { |
| 3566 | char *name; |
| 3567 | char *mode = "r"; |
| 3568 | int bufsize = -1; |
| 3569 | FILE *fp; |
| 3570 | PyObject *f; |
| 3571 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 3572 | return NULL; |
| 3573 | Py_BEGIN_ALLOW_THREADS |
| 3574 | fp = popen(name, mode); |
| 3575 | Py_END_ALLOW_THREADS |
| 3576 | if (fp == NULL) |
| 3577 | return posix_error(); |
| 3578 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 3579 | if (f != NULL) |
| 3580 | PyFile_SetBufSize(f, bufsize); |
| 3581 | return f; |
| 3582 | } |
| 3583 | |
| 3584 | /* fork() under OS/2 has lots'o'warts |
| 3585 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 3586 | * most of this code is a ripoff of the win32 code, but using the |
| 3587 | * capabilities of EMX's C library routines |
| 3588 | */ |
| 3589 | |
| 3590 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 3591 | #define POPEN_1 1 |
| 3592 | #define POPEN_2 2 |
| 3593 | #define POPEN_3 3 |
| 3594 | #define POPEN_4 4 |
| 3595 | |
| 3596 | static PyObject *_PyPopen(char *, int, int, int); |
| 3597 | static int _PyPclose(FILE *file); |
| 3598 | |
| 3599 | /* |
| 3600 | * Internal dictionary mapping popen* file pointers to process handles, |
| 3601 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3602 | * for more information on this dictionary's use. |
| 3603 | */ |
| 3604 | static PyObject *_PyPopenProcs = NULL; |
| 3605 | |
| 3606 | /* os2emx version of popen2() |
| 3607 | * |
| 3608 | * The result of this function is a pipe (file) connected to the |
| 3609 | * process's stdin, and a pipe connected to the process's stdout. |
| 3610 | */ |
| 3611 | |
| 3612 | static PyObject * |
| 3613 | os2emx_popen2(PyObject *self, PyObject *args) |
| 3614 | { |
| 3615 | PyObject *f; |
| 3616 | int tm=0; |
| 3617 | |
| 3618 | char *cmdstring; |
| 3619 | char *mode = "t"; |
| 3620 | int bufsize = -1; |
| 3621 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 3622 | return NULL; |
| 3623 | |
| 3624 | if (*mode == 't') |
| 3625 | tm = O_TEXT; |
| 3626 | else if (*mode != 'b') { |
| 3627 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3628 | return NULL; |
| 3629 | } else |
| 3630 | tm = O_BINARY; |
| 3631 | |
| 3632 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 3633 | |
| 3634 | return f; |
| 3635 | } |
| 3636 | |
| 3637 | /* |
| 3638 | * Variation on os2emx.popen2 |
| 3639 | * |
| 3640 | * The result of this function is 3 pipes - the process's stdin, |
| 3641 | * stdout and stderr |
| 3642 | */ |
| 3643 | |
| 3644 | static PyObject * |
| 3645 | os2emx_popen3(PyObject *self, PyObject *args) |
| 3646 | { |
| 3647 | PyObject *f; |
| 3648 | int tm = 0; |
| 3649 | |
| 3650 | char *cmdstring; |
| 3651 | char *mode = "t"; |
| 3652 | int bufsize = -1; |
| 3653 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 3654 | return NULL; |
| 3655 | |
| 3656 | if (*mode == 't') |
| 3657 | tm = O_TEXT; |
| 3658 | else if (*mode != 'b') { |
| 3659 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3660 | return NULL; |
| 3661 | } else |
| 3662 | tm = O_BINARY; |
| 3663 | |
| 3664 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 3665 | |
| 3666 | return f; |
| 3667 | } |
| 3668 | |
| 3669 | /* |
| 3670 | * Variation on os2emx.popen2 |
| 3671 | * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3672 | * The result of this function is 2 pipes - the processes stdin, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3673 | * and stdout+stderr combined as a single pipe. |
| 3674 | */ |
| 3675 | |
| 3676 | static PyObject * |
| 3677 | os2emx_popen4(PyObject *self, PyObject *args) |
| 3678 | { |
| 3679 | PyObject *f; |
| 3680 | int tm = 0; |
| 3681 | |
| 3682 | char *cmdstring; |
| 3683 | char *mode = "t"; |
| 3684 | int bufsize = -1; |
| 3685 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 3686 | return NULL; |
| 3687 | |
| 3688 | if (*mode == 't') |
| 3689 | tm = O_TEXT; |
| 3690 | else if (*mode != 'b') { |
| 3691 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3692 | return NULL; |
| 3693 | } else |
| 3694 | tm = O_BINARY; |
| 3695 | |
| 3696 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 3697 | |
| 3698 | return f; |
| 3699 | } |
| 3700 | |
| 3701 | /* a couple of structures for convenient handling of multiple |
| 3702 | * file handles and pipes |
| 3703 | */ |
| 3704 | struct file_ref |
| 3705 | { |
| 3706 | int handle; |
| 3707 | int flags; |
| 3708 | }; |
| 3709 | |
| 3710 | struct pipe_ref |
| 3711 | { |
| 3712 | int rd; |
| 3713 | int wr; |
| 3714 | }; |
| 3715 | |
| 3716 | /* The following code is derived from the win32 code */ |
| 3717 | |
| 3718 | static PyObject * |
| 3719 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 3720 | { |
| 3721 | struct file_ref stdio[3]; |
| 3722 | struct pipe_ref p_fd[3]; |
| 3723 | FILE *p_s[3]; |
| 3724 | int file_count, i, pipe_err, pipe_pid; |
| 3725 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 3726 | PyObject *f, *p_f[3]; |
| 3727 | |
| 3728 | /* file modes for subsequent fdopen's on pipe handles */ |
| 3729 | if (mode == O_TEXT) |
| 3730 | { |
| 3731 | rd_mode = "rt"; |
| 3732 | wr_mode = "wt"; |
| 3733 | } |
| 3734 | else |
| 3735 | { |
| 3736 | rd_mode = "rb"; |
| 3737 | wr_mode = "wb"; |
| 3738 | } |
| 3739 | |
| 3740 | /* prepare shell references */ |
| 3741 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 3742 | if ((shell = getenv("COMSPEC")) == NULL) |
| 3743 | { |
| 3744 | errno = ENOENT; |
| 3745 | return posix_error(); |
| 3746 | } |
| 3747 | |
| 3748 | sh_name = _getname(shell); |
| 3749 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 3750 | opt = "/c"; |
| 3751 | else |
| 3752 | opt = "-c"; |
| 3753 | |
| 3754 | /* save current stdio fds + their flags, and set not inheritable */ |
| 3755 | i = pipe_err = 0; |
| 3756 | while (pipe_err >= 0 && i < 3) |
| 3757 | { |
| 3758 | pipe_err = stdio[i].handle = dup(i); |
| 3759 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 3760 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 3761 | i++; |
| 3762 | } |
| 3763 | if (pipe_err < 0) |
| 3764 | { |
| 3765 | /* didn't get them all saved - clean up and bail out */ |
| 3766 | int saved_err = errno; |
| 3767 | while (i-- > 0) |
| 3768 | { |
| 3769 | close(stdio[i].handle); |
| 3770 | } |
| 3771 | errno = saved_err; |
| 3772 | return posix_error(); |
| 3773 | } |
| 3774 | |
| 3775 | /* create pipe ends */ |
| 3776 | file_count = 2; |
| 3777 | if (n == POPEN_3) |
| 3778 | file_count = 3; |
| 3779 | i = pipe_err = 0; |
| 3780 | while ((pipe_err == 0) && (i < file_count)) |
| 3781 | pipe_err = pipe((int *)&p_fd[i++]); |
| 3782 | if (pipe_err < 0) |
| 3783 | { |
| 3784 | /* didn't get them all made - clean up and bail out */ |
| 3785 | while (i-- > 0) |
| 3786 | { |
| 3787 | close(p_fd[i].wr); |
| 3788 | close(p_fd[i].rd); |
| 3789 | } |
| 3790 | errno = EPIPE; |
| 3791 | return posix_error(); |
| 3792 | } |
| 3793 | |
| 3794 | /* change the actual standard IO streams over temporarily, |
| 3795 | * making the retained pipe ends non-inheritable |
| 3796 | */ |
| 3797 | pipe_err = 0; |
| 3798 | |
| 3799 | /* - stdin */ |
| 3800 | if (dup2(p_fd[0].rd, 0) == 0) |
| 3801 | { |
| 3802 | close(p_fd[0].rd); |
| 3803 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 3804 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 3805 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 3806 | { |
| 3807 | close(p_fd[0].wr); |
| 3808 | pipe_err = -1; |
| 3809 | } |
| 3810 | } |
| 3811 | else |
| 3812 | { |
| 3813 | pipe_err = -1; |
| 3814 | } |
| 3815 | |
| 3816 | /* - stdout */ |
| 3817 | if (pipe_err == 0) |
| 3818 | { |
| 3819 | if (dup2(p_fd[1].wr, 1) == 1) |
| 3820 | { |
| 3821 | close(p_fd[1].wr); |
| 3822 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 3823 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 3824 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 3825 | { |
| 3826 | close(p_fd[1].rd); |
| 3827 | pipe_err = -1; |
| 3828 | } |
| 3829 | } |
| 3830 | else |
| 3831 | { |
| 3832 | pipe_err = -1; |
| 3833 | } |
| 3834 | } |
| 3835 | |
| 3836 | /* - stderr, as required */ |
| 3837 | if (pipe_err == 0) |
| 3838 | switch (n) |
| 3839 | { |
| 3840 | case POPEN_3: |
| 3841 | { |
| 3842 | if (dup2(p_fd[2].wr, 2) == 2) |
| 3843 | { |
| 3844 | close(p_fd[2].wr); |
| 3845 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 3846 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 3847 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 3848 | { |
| 3849 | close(p_fd[2].rd); |
| 3850 | pipe_err = -1; |
| 3851 | } |
| 3852 | } |
| 3853 | else |
| 3854 | { |
| 3855 | pipe_err = -1; |
| 3856 | } |
| 3857 | break; |
| 3858 | } |
| 3859 | |
| 3860 | case POPEN_4: |
| 3861 | { |
| 3862 | if (dup2(1, 2) != 2) |
| 3863 | { |
| 3864 | pipe_err = -1; |
| 3865 | } |
| 3866 | break; |
| 3867 | } |
| 3868 | } |
| 3869 | |
| 3870 | /* spawn the child process */ |
| 3871 | if (pipe_err == 0) |
| 3872 | { |
| 3873 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 3874 | if (pipe_pid == -1) |
| 3875 | { |
| 3876 | pipe_err = -1; |
| 3877 | } |
| 3878 | else |
| 3879 | { |
| 3880 | /* save the PID into the FILE structure |
| 3881 | * NOTE: this implementation doesn't actually |
| 3882 | * take advantage of this, but do it for |
| 3883 | * completeness - AIM Apr01 |
| 3884 | */ |
| 3885 | for (i = 0; i < file_count; i++) |
| 3886 | p_s[i]->_pid = pipe_pid; |
| 3887 | } |
| 3888 | } |
| 3889 | |
| 3890 | /* reset standard IO to normal */ |
| 3891 | for (i = 0; i < 3; i++) |
| 3892 | { |
| 3893 | dup2(stdio[i].handle, i); |
| 3894 | fcntl(i, F_SETFD, stdio[i].flags); |
| 3895 | close(stdio[i].handle); |
| 3896 | } |
| 3897 | |
| 3898 | /* if any remnant problems, clean up and bail out */ |
| 3899 | if (pipe_err < 0) |
| 3900 | { |
| 3901 | for (i = 0; i < 3; i++) |
| 3902 | { |
| 3903 | close(p_fd[i].rd); |
| 3904 | close(p_fd[i].wr); |
| 3905 | } |
| 3906 | errno = EPIPE; |
| 3907 | return posix_error_with_filename(cmdstring); |
| 3908 | } |
| 3909 | |
| 3910 | /* build tuple of file objects to return */ |
| 3911 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 3912 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3913 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3914 | PyFile_SetBufSize(p_f[1], bufsize); |
| 3915 | if (n == POPEN_3) |
| 3916 | { |
| 3917 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3918 | PyFile_SetBufSize(p_f[0], bufsize); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3919 | 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] | 3920 | } |
| 3921 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3922 | f = PyTuple_Pack(2, p_f[0], p_f[1]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3923 | |
| 3924 | /* |
| 3925 | * Insert the files we've created into the process dictionary |
| 3926 | * all referencing the list with the process handle and the |
| 3927 | * initial number of files (see description below in _PyPclose). |
| 3928 | * Since if _PyPclose later tried to wait on a process when all |
| 3929 | * handles weren't closed, it could create a deadlock with the |
| 3930 | * child, we spend some energy here to try to ensure that we |
| 3931 | * either insert all file handles into the dictionary or none |
| 3932 | * at all. It's a little clumsy with the various popen modes |
| 3933 | * and variable number of files involved. |
| 3934 | */ |
| 3935 | if (!_PyPopenProcs) |
| 3936 | { |
| 3937 | _PyPopenProcs = PyDict_New(); |
| 3938 | } |
| 3939 | |
| 3940 | if (_PyPopenProcs) |
| 3941 | { |
| 3942 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 3943 | int ins_rc[3]; |
| 3944 | |
| 3945 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 3946 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 3947 | |
| 3948 | procObj = PyList_New(2); |
| 3949 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 3950 | intObj = PyInt_FromLong((long) file_count); |
| 3951 | |
| 3952 | if (procObj && pidObj && intObj) |
| 3953 | { |
| 3954 | PyList_SetItem(procObj, 0, pidObj); |
| 3955 | PyList_SetItem(procObj, 1, intObj); |
| 3956 | |
| 3957 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 3958 | if (fileObj[0]) |
| 3959 | { |
| 3960 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 3961 | fileObj[0], |
| 3962 | procObj); |
| 3963 | } |
| 3964 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 3965 | if (fileObj[1]) |
| 3966 | { |
| 3967 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 3968 | fileObj[1], |
| 3969 | procObj); |
| 3970 | } |
| 3971 | if (file_count >= 3) |
| 3972 | { |
| 3973 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 3974 | if (fileObj[2]) |
| 3975 | { |
| 3976 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 3977 | fileObj[2], |
| 3978 | procObj); |
| 3979 | } |
| 3980 | } |
| 3981 | |
| 3982 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 3983 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 3984 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 3985 | { |
| 3986 | /* Something failed - remove any dictionary |
| 3987 | * entries that did make it. |
| 3988 | */ |
| 3989 | if (!ins_rc[0] && fileObj[0]) |
| 3990 | { |
| 3991 | PyDict_DelItem(_PyPopenProcs, |
| 3992 | fileObj[0]); |
| 3993 | } |
| 3994 | if (!ins_rc[1] && fileObj[1]) |
| 3995 | { |
| 3996 | PyDict_DelItem(_PyPopenProcs, |
| 3997 | fileObj[1]); |
| 3998 | } |
| 3999 | if (!ins_rc[2] && fileObj[2]) |
| 4000 | { |
| 4001 | PyDict_DelItem(_PyPopenProcs, |
| 4002 | fileObj[2]); |
| 4003 | } |
| 4004 | } |
| 4005 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4006 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4007 | /* |
| 4008 | * Clean up our localized references for the dictionary keys |
| 4009 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4010 | * that got placed in the dictionary. |
| 4011 | */ |
| 4012 | Py_XDECREF(procObj); |
| 4013 | Py_XDECREF(fileObj[0]); |
| 4014 | Py_XDECREF(fileObj[1]); |
| 4015 | Py_XDECREF(fileObj[2]); |
| 4016 | } |
| 4017 | |
| 4018 | /* Child is launched. */ |
| 4019 | return f; |
| 4020 | } |
| 4021 | |
| 4022 | /* |
| 4023 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4024 | * exit code for the child process and return as a result of the close. |
| 4025 | * |
| 4026 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4027 | * input file pointer to information about the process that was |
| 4028 | * originally created by the popen* call that created the file pointer. |
| 4029 | * The dictionary uses the file pointer as a key (with one entry |
| 4030 | * inserted for each file returned by the original popen* call) and a |
| 4031 | * single list object as the value for all files from a single call. |
| 4032 | * The list object contains the Win32 process handle at [0], and a file |
| 4033 | * count at [1], which is initialized to the total number of file |
| 4034 | * handles using that list. |
| 4035 | * |
| 4036 | * This function closes whichever handle it is passed, and decrements |
| 4037 | * the file count in the dictionary for the process handle pointed to |
| 4038 | * by this file. On the last close (when the file count reaches zero), |
| 4039 | * this function will wait for the child process and then return its |
| 4040 | * exit code as the result of the close() operation. This permits the |
| 4041 | * files to be closed in any order - it is always the close() of the |
| 4042 | * final handle that will return the exit code. |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4043 | * |
| 4044 | * NOTE: This function is currently called with the GIL released. |
| 4045 | * hence we use the GILState API to manage our state. |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4046 | */ |
| 4047 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4048 | static int _PyPclose(FILE *file) |
| 4049 | { |
| 4050 | int result; |
| 4051 | int exit_code; |
| 4052 | int pipe_pid; |
| 4053 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 4054 | int file_count; |
| 4055 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4056 | PyGILState_STATE state; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4057 | #endif |
| 4058 | |
| 4059 | /* Close the file handle first, to ensure it can't block the |
| 4060 | * child from exiting if it's the last handle. |
| 4061 | */ |
| 4062 | result = fclose(file); |
| 4063 | |
| 4064 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4065 | state = PyGILState_Ensure(); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4066 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4067 | if (_PyPopenProcs) |
| 4068 | { |
| 4069 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4070 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4071 | fileObj)) != NULL && |
| 4072 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 4073 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 4074 | { |
| 4075 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 4076 | file_count = (int) PyInt_AsLong(intObj); |
| 4077 | |
| 4078 | if (file_count > 1) |
| 4079 | { |
| 4080 | /* Still other files referencing process */ |
| 4081 | file_count--; |
| 4082 | PyList_SetItem(procObj,1, |
| 4083 | PyInt_FromLong((long) file_count)); |
| 4084 | } |
| 4085 | else |
| 4086 | { |
| 4087 | /* Last file for this process */ |
| 4088 | if (result != EOF && |
| 4089 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 4090 | { |
| 4091 | /* extract exit status */ |
| 4092 | if (WIFEXITED(exit_code)) |
| 4093 | { |
| 4094 | result = WEXITSTATUS(exit_code); |
| 4095 | } |
| 4096 | else |
| 4097 | { |
| 4098 | errno = EPIPE; |
| 4099 | result = -1; |
| 4100 | } |
| 4101 | } |
| 4102 | else |
| 4103 | { |
| 4104 | /* Indicate failure - this will cause the file object |
| 4105 | * to raise an I/O error and translate the last |
| 4106 | * error code from errno. We do have a problem with |
| 4107 | * last errors that overlap the normal errno table, |
| 4108 | * but that's a consistent problem with the file object. |
| 4109 | */ |
| 4110 | result = -1; |
| 4111 | } |
| 4112 | } |
| 4113 | |
| 4114 | /* Remove this file pointer from dictionary */ |
| 4115 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4116 | |
| 4117 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 4118 | { |
| 4119 | Py_DECREF(_PyPopenProcs); |
| 4120 | _PyPopenProcs = NULL; |
| 4121 | } |
| 4122 | |
| 4123 | } /* if object retrieval ok */ |
| 4124 | |
| 4125 | Py_XDECREF(fileObj); |
| 4126 | } /* if _PyPopenProcs */ |
| 4127 | |
| 4128 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4129 | PyGILState_Release(state); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4130 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4131 | return result; |
| 4132 | } |
| 4133 | |
| 4134 | #endif /* PYCC_??? */ |
| 4135 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4136 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4137 | |
| 4138 | /* |
| 4139 | * Portable 'popen' replacement for Win32. |
| 4140 | * |
| 4141 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 4142 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4143 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4144 | */ |
| 4145 | |
| 4146 | #include <malloc.h> |
| 4147 | #include <io.h> |
| 4148 | #include <fcntl.h> |
| 4149 | |
| 4150 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 4151 | #define POPEN_1 1 |
| 4152 | #define POPEN_2 2 |
| 4153 | #define POPEN_3 3 |
| 4154 | #define POPEN_4 4 |
| 4155 | |
| 4156 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4157 | static int _PyPclose(FILE *file); |
| 4158 | |
| 4159 | /* |
| 4160 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4161 | * for use when retrieving the process exit code. See _PyPclose() below |
| 4162 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4163 | */ |
| 4164 | static PyObject *_PyPopenProcs = NULL; |
| 4165 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4166 | |
| 4167 | /* popen that works from a GUI. |
| 4168 | * |
| 4169 | * The result of this function is a pipe (file) connected to the |
| 4170 | * processes stdin or stdout, depending on the requested mode. |
| 4171 | */ |
| 4172 | |
| 4173 | static PyObject * |
| 4174 | posix_popen(PyObject *self, PyObject *args) |
| 4175 | { |
Raymond Hettinger | b5cb665 | 2003-09-01 22:25:41 +0000 | [diff] [blame] | 4176 | PyObject *f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4177 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4178 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4179 | char *cmdstring; |
| 4180 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4181 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4182 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4183 | return NULL; |
| 4184 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4185 | if (*mode == 'r') |
| 4186 | tm = _O_RDONLY; |
| 4187 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4188 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4189 | return NULL; |
| 4190 | } else |
| 4191 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4192 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4193 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4194 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4195 | return NULL; |
| 4196 | } |
| 4197 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4198 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4199 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4200 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4201 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4202 | else |
| 4203 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 4204 | |
| 4205 | return f; |
| 4206 | } |
| 4207 | |
| 4208 | /* Variation on win32pipe.popen |
| 4209 | * |
| 4210 | * The result of this function is a pipe (file) connected to the |
| 4211 | * process's stdin, and a pipe connected to the process's stdout. |
| 4212 | */ |
| 4213 | |
| 4214 | static PyObject * |
| 4215 | win32_popen2(PyObject *self, PyObject *args) |
| 4216 | { |
| 4217 | PyObject *f; |
| 4218 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4219 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4220 | char *cmdstring; |
| 4221 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4222 | int bufsize = -1; |
| 4223 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4224 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4225 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4226 | if (*mode == 't') |
| 4227 | tm = _O_TEXT; |
| 4228 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4229 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4230 | return NULL; |
| 4231 | } else |
| 4232 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4233 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4234 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4235 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4236 | return NULL; |
| 4237 | } |
| 4238 | |
| 4239 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4240 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4241 | return f; |
| 4242 | } |
| 4243 | |
| 4244 | /* |
| 4245 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4246 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4247 | * The result of this function is 3 pipes - the process's stdin, |
| 4248 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4249 | */ |
| 4250 | |
| 4251 | static PyObject * |
| 4252 | win32_popen3(PyObject *self, PyObject *args) |
| 4253 | { |
| 4254 | PyObject *f; |
| 4255 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4256 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4257 | char *cmdstring; |
| 4258 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4259 | int bufsize = -1; |
| 4260 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4261 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4262 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4263 | if (*mode == 't') |
| 4264 | tm = _O_TEXT; |
| 4265 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4266 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4267 | return NULL; |
| 4268 | } else |
| 4269 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4270 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4271 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4272 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4273 | return NULL; |
| 4274 | } |
| 4275 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4276 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4277 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4278 | return f; |
| 4279 | } |
| 4280 | |
| 4281 | /* |
| 4282 | * Variation on win32pipe.popen |
| 4283 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4284 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4285 | * and stdout+stderr combined as a single pipe. |
| 4286 | */ |
| 4287 | |
| 4288 | static PyObject * |
| 4289 | win32_popen4(PyObject *self, PyObject *args) |
| 4290 | { |
| 4291 | PyObject *f; |
| 4292 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4293 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4294 | char *cmdstring; |
| 4295 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4296 | int bufsize = -1; |
| 4297 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4298 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4299 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4300 | if (*mode == 't') |
| 4301 | tm = _O_TEXT; |
| 4302 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4303 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4304 | return NULL; |
| 4305 | } else |
| 4306 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4307 | |
| 4308 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4309 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4310 | return NULL; |
| 4311 | } |
| 4312 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4313 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4314 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4315 | return f; |
| 4316 | } |
| 4317 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4318 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4319 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4320 | HANDLE hStdin, |
| 4321 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4322 | HANDLE hStderr, |
| 4323 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4324 | { |
| 4325 | PROCESS_INFORMATION piProcInfo; |
| 4326 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4327 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4328 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4329 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4330 | int i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4331 | Py_ssize_t x; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4332 | |
| 4333 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4334 | char *comshell; |
| 4335 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4336 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4337 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4338 | /* x < i, so x fits into an integer */ |
| 4339 | return (int)x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4340 | |
| 4341 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 4342 | * then use the w9xpopen hack. |
| 4343 | */ |
| 4344 | comshell = s1 + x; |
| 4345 | while (comshell >= s1 && *comshell != '\\') |
| 4346 | --comshell; |
| 4347 | ++comshell; |
| 4348 | |
| 4349 | if (GetVersion() < 0x80000000 && |
| 4350 | _stricmp(comshell, "command.com") != 0) { |
| 4351 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4352 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4353 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4354 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4355 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4356 | } |
| 4357 | else { |
| 4358 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4359 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 4360 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4361 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4362 | char modulepath[_MAX_PATH]; |
| 4363 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4364 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 4365 | for (i = x = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4366 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4367 | x = i+1; |
| 4368 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4369 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4370 | strncat(modulepath, |
| 4371 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4372 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4373 | -strlen(modulepath)); |
| 4374 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4375 | /* Eeek - file-not-found - possibly an embedding |
| 4376 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4377 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4378 | strncpy(modulepath, |
| 4379 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4380 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 4381 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 4382 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4383 | strncat(modulepath, |
| 4384 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4385 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4386 | -strlen(modulepath)); |
| 4387 | /* No where else to look - raise an easily identifiable |
| 4388 | error, rather than leaving Windows to report |
| 4389 | "file not found" - as the user is probably blissfully |
| 4390 | unaware this shim EXE is used, and it will confuse them. |
| 4391 | (well, it confused me for a while ;-) |
| 4392 | */ |
| 4393 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4394 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4395 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4396 | "for popen to work with your shell " |
| 4397 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4398 | szConsoleSpawn); |
| 4399 | return FALSE; |
| 4400 | } |
| 4401 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4402 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4403 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4404 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4405 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4406 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4407 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4408 | /* To maintain correct argument passing semantics, |
| 4409 | we pass the command-line as it stands, and allow |
| 4410 | quoting to be applied. w9xpopen.exe will then |
| 4411 | use its argv vector, and re-quote the necessary |
| 4412 | args for the ultimate child process. |
| 4413 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4414 | PyOS_snprintf( |
| 4415 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4416 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4417 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4418 | s1, |
| 4419 | s3, |
| 4420 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4421 | /* Not passing CREATE_NEW_CONSOLE has been known to |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4422 | cause random failures on win9x. Specifically a |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4423 | dialog: |
| 4424 | "Your program accessed mem currently in use at xxx" |
| 4425 | and a hopeful warning about the stability of your |
| 4426 | system. |
| 4427 | Cost is Ctrl+C wont kill children, but anyone |
| 4428 | who cares can have a go! |
| 4429 | */ |
| 4430 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4431 | } |
| 4432 | } |
| 4433 | |
| 4434 | /* Could be an else here to try cmd.exe / command.com in the path |
| 4435 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4436 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4437 | PyErr_SetString(PyExc_RuntimeError, |
| 4438 | "Cannot locate a COMSPEC environment variable to " |
| 4439 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4440 | return FALSE; |
| 4441 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4442 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4443 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 4444 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 4445 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 4446 | siStartInfo.hStdInput = hStdin; |
| 4447 | siStartInfo.hStdOutput = hStdout; |
| 4448 | siStartInfo.hStdError = hStderr; |
| 4449 | siStartInfo.wShowWindow = SW_HIDE; |
| 4450 | |
| 4451 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4452 | s2, |
| 4453 | NULL, |
| 4454 | NULL, |
| 4455 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4456 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4457 | NULL, |
| 4458 | NULL, |
| 4459 | &siStartInfo, |
| 4460 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4461 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4462 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4463 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4464 | /* Return process handle */ |
| 4465 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4466 | return TRUE; |
| 4467 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4468 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4469 | return FALSE; |
| 4470 | } |
| 4471 | |
| 4472 | /* The following code is based off of KB: Q190351 */ |
| 4473 | |
| 4474 | static PyObject * |
| 4475 | _PyPopen(char *cmdstring, int mode, int n) |
| 4476 | { |
| 4477 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 4478 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4479 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4480 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4481 | SECURITY_ATTRIBUTES saAttr; |
| 4482 | BOOL fSuccess; |
| 4483 | int fd1, fd2, fd3; |
| 4484 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4485 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4486 | PyObject *f; |
| 4487 | |
| 4488 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 4489 | saAttr.bInheritHandle = TRUE; |
| 4490 | saAttr.lpSecurityDescriptor = NULL; |
| 4491 | |
| 4492 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 4493 | return win32_error("CreatePipe", NULL); |
| 4494 | |
| 4495 | /* Create new output read handle and the input write handle. Set |
| 4496 | * the inheritance properties to FALSE. Otherwise, the child inherits |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 4497 | * these handles; resulting in non-closeable handles to the pipes |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4498 | * being created. */ |
| 4499 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4500 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 4501 | FALSE, |
| 4502 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4503 | if (!fSuccess) |
| 4504 | return win32_error("DuplicateHandle", NULL); |
| 4505 | |
| 4506 | /* Close the inheritable version of ChildStdin |
| 4507 | that we're using. */ |
| 4508 | CloseHandle(hChildStdinWr); |
| 4509 | |
| 4510 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 4511 | return win32_error("CreatePipe", NULL); |
| 4512 | |
| 4513 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4514 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 4515 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4516 | if (!fSuccess) |
| 4517 | return win32_error("DuplicateHandle", NULL); |
| 4518 | |
| 4519 | /* Close the inheritable version of ChildStdout |
| 4520 | that we're using. */ |
| 4521 | CloseHandle(hChildStdoutRd); |
| 4522 | |
| 4523 | if (n != POPEN_4) { |
| 4524 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 4525 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4526 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 4527 | hChildStderrRd, |
| 4528 | GetCurrentProcess(), |
| 4529 | &hChildStderrRdDup, 0, |
| 4530 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4531 | if (!fSuccess) |
| 4532 | return win32_error("DuplicateHandle", NULL); |
| 4533 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 4534 | CloseHandle(hChildStderrRd); |
| 4535 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4536 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4537 | switch (n) { |
| 4538 | case POPEN_1: |
| 4539 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 4540 | case _O_WRONLY | _O_TEXT: |
| 4541 | /* Case for writing to child Stdin in text mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4542 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4543 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4544 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4545 | PyFile_SetBufSize(f, 0); |
| 4546 | /* We don't care about these pipes anymore, so close them. */ |
| 4547 | CloseHandle(hChildStdoutRdDup); |
| 4548 | CloseHandle(hChildStderrRdDup); |
| 4549 | break; |
| 4550 | |
| 4551 | case _O_RDONLY | _O_TEXT: |
| 4552 | /* Case for reading from child Stdout in text mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4553 | fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4554 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4555 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4556 | PyFile_SetBufSize(f, 0); |
| 4557 | /* We don't care about these pipes anymore, so close them. */ |
| 4558 | CloseHandle(hChildStdinWrDup); |
| 4559 | CloseHandle(hChildStderrRdDup); |
| 4560 | break; |
| 4561 | |
| 4562 | case _O_RDONLY | _O_BINARY: |
| 4563 | /* Case for readinig from child Stdout in binary mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4564 | fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4565 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4566 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4567 | PyFile_SetBufSize(f, 0); |
| 4568 | /* We don't care about these pipes anymore, so close them. */ |
| 4569 | CloseHandle(hChildStdinWrDup); |
| 4570 | CloseHandle(hChildStderrRdDup); |
| 4571 | break; |
| 4572 | |
| 4573 | case _O_WRONLY | _O_BINARY: |
| 4574 | /* Case for writing to child Stdin in binary mode. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4575 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4576 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4577 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4578 | PyFile_SetBufSize(f, 0); |
| 4579 | /* We don't care about these pipes anymore, so close them. */ |
| 4580 | CloseHandle(hChildStdoutRdDup); |
| 4581 | CloseHandle(hChildStderrRdDup); |
| 4582 | break; |
| 4583 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4584 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4585 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4586 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4587 | case POPEN_2: |
| 4588 | case POPEN_4: |
| 4589 | { |
| 4590 | char *m1, *m2; |
| 4591 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4592 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4593 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4594 | m1 = "r"; |
| 4595 | m2 = "w"; |
| 4596 | } else { |
| 4597 | m1 = "rb"; |
| 4598 | m2 = "wb"; |
| 4599 | } |
| 4600 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4601 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4602 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4603 | fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4604 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4605 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4606 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4607 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4608 | PyFile_SetBufSize(p2, 0); |
| 4609 | |
| 4610 | if (n != 4) |
| 4611 | CloseHandle(hChildStderrRdDup); |
| 4612 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4613 | f = PyTuple_Pack(2,p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4614 | Py_XDECREF(p1); |
| 4615 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4616 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4617 | break; |
| 4618 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4619 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4620 | case POPEN_3: |
| 4621 | { |
| 4622 | char *m1, *m2; |
| 4623 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4624 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4625 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4626 | m1 = "r"; |
| 4627 | m2 = "w"; |
| 4628 | } else { |
| 4629 | m1 = "rb"; |
| 4630 | m2 = "wb"; |
| 4631 | } |
| 4632 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4633 | fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4634 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4635 | fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4636 | f2 = _fdopen(fd2, m1); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4637 | fd3 = _open_osfhandle((intptr_t)hChildStderrRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4638 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4639 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4640 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 4641 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4642 | PyFile_SetBufSize(p1, 0); |
| 4643 | PyFile_SetBufSize(p2, 0); |
| 4644 | PyFile_SetBufSize(p3, 0); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4645 | f = PyTuple_Pack(3,p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4646 | Py_XDECREF(p1); |
| 4647 | Py_XDECREF(p2); |
| 4648 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4649 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4650 | break; |
| 4651 | } |
| 4652 | } |
| 4653 | |
| 4654 | if (n == POPEN_4) { |
| 4655 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4656 | hChildStdinRd, |
| 4657 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4658 | hChildStdoutWr, |
| 4659 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4660 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4661 | } |
| 4662 | else { |
| 4663 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4664 | hChildStdinRd, |
| 4665 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4666 | hChildStderrWr, |
| 4667 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4668 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4669 | } |
| 4670 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4671 | /* |
| 4672 | * Insert the files we've created into the process dictionary |
| 4673 | * all referencing the list with the process handle and the |
| 4674 | * initial number of files (see description below in _PyPclose). |
| 4675 | * Since if _PyPclose later tried to wait on a process when all |
| 4676 | * handles weren't closed, it could create a deadlock with the |
| 4677 | * child, we spend some energy here to try to ensure that we |
| 4678 | * either insert all file handles into the dictionary or none |
| 4679 | * at all. It's a little clumsy with the various popen modes |
| 4680 | * and variable number of files involved. |
| 4681 | */ |
| 4682 | if (!_PyPopenProcs) { |
| 4683 | _PyPopenProcs = PyDict_New(); |
| 4684 | } |
| 4685 | |
| 4686 | if (_PyPopenProcs) { |
| 4687 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 4688 | int ins_rc[3]; |
| 4689 | |
| 4690 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4691 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4692 | |
| 4693 | procObj = PyList_New(2); |
| 4694 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 4695 | intObj = PyInt_FromLong(file_count); |
| 4696 | |
| 4697 | if (procObj && hProcessObj && intObj) { |
| 4698 | PyList_SetItem(procObj,0,hProcessObj); |
| 4699 | PyList_SetItem(procObj,1,intObj); |
| 4700 | |
| 4701 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 4702 | if (fileObj[0]) { |
| 4703 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4704 | fileObj[0], |
| 4705 | procObj); |
| 4706 | } |
| 4707 | if (file_count >= 2) { |
| 4708 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 4709 | if (fileObj[1]) { |
| 4710 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4711 | fileObj[1], |
| 4712 | procObj); |
| 4713 | } |
| 4714 | } |
| 4715 | if (file_count >= 3) { |
| 4716 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 4717 | if (fileObj[2]) { |
| 4718 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4719 | fileObj[2], |
| 4720 | procObj); |
| 4721 | } |
| 4722 | } |
| 4723 | |
| 4724 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4725 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4726 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 4727 | /* Something failed - remove any dictionary |
| 4728 | * entries that did make it. |
| 4729 | */ |
| 4730 | if (!ins_rc[0] && fileObj[0]) { |
| 4731 | PyDict_DelItem(_PyPopenProcs, |
| 4732 | fileObj[0]); |
| 4733 | } |
| 4734 | if (!ins_rc[1] && fileObj[1]) { |
| 4735 | PyDict_DelItem(_PyPopenProcs, |
| 4736 | fileObj[1]); |
| 4737 | } |
| 4738 | if (!ins_rc[2] && fileObj[2]) { |
| 4739 | PyDict_DelItem(_PyPopenProcs, |
| 4740 | fileObj[2]); |
| 4741 | } |
| 4742 | } |
| 4743 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4744 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4745 | /* |
| 4746 | * Clean up our localized references for the dictionary keys |
| 4747 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4748 | * that got placed in the dictionary. |
| 4749 | */ |
| 4750 | Py_XDECREF(procObj); |
| 4751 | Py_XDECREF(fileObj[0]); |
| 4752 | Py_XDECREF(fileObj[1]); |
| 4753 | Py_XDECREF(fileObj[2]); |
| 4754 | } |
| 4755 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4756 | /* Child is launched. Close the parents copy of those pipe |
| 4757 | * handles that only the child should have open. You need to |
| 4758 | * make sure that no handles to the write end of the output pipe |
| 4759 | * are maintained in this process or else the pipe will not close |
| 4760 | * when the child process exits and the ReadFile will hang. */ |
| 4761 | |
| 4762 | if (!CloseHandle(hChildStdinRd)) |
| 4763 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4764 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4765 | if (!CloseHandle(hChildStdoutWr)) |
| 4766 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4767 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4768 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 4769 | return win32_error("CloseHandle", NULL); |
| 4770 | |
| 4771 | return f; |
| 4772 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4773 | |
| 4774 | /* |
| 4775 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4776 | * 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] | 4777 | * |
| 4778 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4779 | * input file pointer to information about the process that was |
| 4780 | * originally created by the popen* call that created the file pointer. |
| 4781 | * The dictionary uses the file pointer as a key (with one entry |
| 4782 | * inserted for each file returned by the original popen* call) and a |
| 4783 | * single list object as the value for all files from a single call. |
| 4784 | * The list object contains the Win32 process handle at [0], and a file |
| 4785 | * count at [1], which is initialized to the total number of file |
| 4786 | * handles using that list. |
| 4787 | * |
| 4788 | * This function closes whichever handle it is passed, and decrements |
| 4789 | * the file count in the dictionary for the process handle pointed to |
| 4790 | * by this file. On the last close (when the file count reaches zero), |
| 4791 | * this function will wait for the child process and then return its |
| 4792 | * exit code as the result of the close() operation. This permits the |
| 4793 | * files to be closed in any order - it is always the close() of the |
| 4794 | * final handle that will return the exit code. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4795 | * |
| 4796 | * NOTE: This function is currently called with the GIL released. |
| 4797 | * hence we use the GILState API to manage our state. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4798 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4799 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4800 | static int _PyPclose(FILE *file) |
| 4801 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4802 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4803 | DWORD exit_code; |
| 4804 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4805 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 4806 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4807 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4808 | PyGILState_STATE state; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4809 | #endif |
| 4810 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4811 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4812 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4813 | */ |
| 4814 | result = fclose(file); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4815 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4816 | state = PyGILState_Ensure(); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4817 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4818 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4819 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4820 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4821 | fileObj)) != NULL && |
| 4822 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 4823 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 4824 | |
| 4825 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 4826 | file_count = PyInt_AsLong(intObj); |
| 4827 | |
| 4828 | if (file_count > 1) { |
| 4829 | /* Still other files referencing process */ |
| 4830 | file_count--; |
| 4831 | PyList_SetItem(procObj,1, |
| 4832 | PyInt_FromLong(file_count)); |
| 4833 | } else { |
| 4834 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4835 | if (result != EOF && |
| 4836 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 4837 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4838 | /* Possible truncation here in 16-bit environments, but |
| 4839 | * real exit codes are just the lower byte in any event. |
| 4840 | */ |
| 4841 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4842 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4843 | /* Indicate failure - this will cause the file object |
| 4844 | * to raise an I/O error and translate the last Win32 |
| 4845 | * error code from errno. We do have a problem with |
| 4846 | * last errors that overlap the normal errno table, |
| 4847 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4848 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4849 | if (result != EOF) { |
| 4850 | /* If the error wasn't from the fclose(), then |
| 4851 | * set errno for the file object error handling. |
| 4852 | */ |
| 4853 | errno = GetLastError(); |
| 4854 | } |
| 4855 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | /* Free up the native handle at this point */ |
| 4859 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4860 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4861 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4862 | /* Remove this file pointer from dictionary */ |
| 4863 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4864 | |
| 4865 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 4866 | Py_DECREF(_PyPopenProcs); |
| 4867 | _PyPopenProcs = NULL; |
| 4868 | } |
| 4869 | |
| 4870 | } /* if object retrieval ok */ |
| 4871 | |
| 4872 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4873 | } /* if _PyPopenProcs */ |
| 4874 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4875 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4876 | PyGILState_Release(state); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4877 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4878 | return result; |
| 4879 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4880 | |
| 4881 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4882 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4883 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4884 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4885 | char *name; |
| 4886 | char *mode = "r"; |
| 4887 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4888 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4889 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4890 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4891 | return NULL; |
Martin v. Löwis | 9ad853b | 2003-10-31 10:01:53 +0000 | [diff] [blame] | 4892 | /* Strip mode of binary or text modifiers */ |
| 4893 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) |
| 4894 | mode = "r"; |
| 4895 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) |
| 4896 | mode = "w"; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4897 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4898 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4899 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4900 | if (fp == NULL) |
| 4901 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4902 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4903 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4904 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4905 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4906 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4907 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4908 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4909 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4910 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4911 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4912 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4913 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4914 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4915 | Set the current process's user id."); |
| 4916 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4917 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4918 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4919 | { |
| 4920 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4921 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4922 | return NULL; |
| 4923 | if (setuid(uid) < 0) |
| 4924 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4925 | Py_INCREF(Py_None); |
| 4926 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4927 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4928 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4929 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4930 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4931 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4932 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4933 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4934 | Set the current process's effective user id."); |
| 4935 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4936 | static PyObject * |
| 4937 | posix_seteuid (PyObject *self, PyObject *args) |
| 4938 | { |
| 4939 | int euid; |
| 4940 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 4941 | return NULL; |
| 4942 | } else if (seteuid(euid) < 0) { |
| 4943 | return posix_error(); |
| 4944 | } else { |
| 4945 | Py_INCREF(Py_None); |
| 4946 | return Py_None; |
| 4947 | } |
| 4948 | } |
| 4949 | #endif /* HAVE_SETEUID */ |
| 4950 | |
| 4951 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4952 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4953 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4954 | Set the current process's effective group id."); |
| 4955 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4956 | static PyObject * |
| 4957 | posix_setegid (PyObject *self, PyObject *args) |
| 4958 | { |
| 4959 | int egid; |
| 4960 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 4961 | return NULL; |
| 4962 | } else if (setegid(egid) < 0) { |
| 4963 | return posix_error(); |
| 4964 | } else { |
| 4965 | Py_INCREF(Py_None); |
| 4966 | return Py_None; |
| 4967 | } |
| 4968 | } |
| 4969 | #endif /* HAVE_SETEGID */ |
| 4970 | |
| 4971 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4972 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4973 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4974 | Set the current process's real and effective user ids."); |
| 4975 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4976 | static PyObject * |
| 4977 | posix_setreuid (PyObject *self, PyObject *args) |
| 4978 | { |
| 4979 | int ruid, euid; |
| 4980 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4981 | return NULL; |
| 4982 | } else if (setreuid(ruid, euid) < 0) { |
| 4983 | return posix_error(); |
| 4984 | } else { |
| 4985 | Py_INCREF(Py_None); |
| 4986 | return Py_None; |
| 4987 | } |
| 4988 | } |
| 4989 | #endif /* HAVE_SETREUID */ |
| 4990 | |
| 4991 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4992 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4993 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4994 | Set the current process's real and effective group ids."); |
| 4995 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4996 | static PyObject * |
| 4997 | posix_setregid (PyObject *self, PyObject *args) |
| 4998 | { |
| 4999 | int rgid, egid; |
| 5000 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 5001 | return NULL; |
| 5002 | } else if (setregid(rgid, egid) < 0) { |
| 5003 | return posix_error(); |
| 5004 | } else { |
| 5005 | Py_INCREF(Py_None); |
| 5006 | return Py_None; |
| 5007 | } |
| 5008 | } |
| 5009 | #endif /* HAVE_SETREGID */ |
| 5010 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5011 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5012 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5013 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5014 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5015 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5016 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5017 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5018 | { |
| 5019 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5020 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5021 | return NULL; |
| 5022 | if (setgid(gid) < 0) |
| 5023 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5024 | Py_INCREF(Py_None); |
| 5025 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5026 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5027 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5028 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5029 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5030 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5031 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5032 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5033 | |
| 5034 | static PyObject * |
| 5035 | posix_setgroups(PyObject *self, PyObject *args) |
| 5036 | { |
| 5037 | PyObject *groups; |
| 5038 | int i, len; |
| 5039 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5040 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5041 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 5042 | return NULL; |
| 5043 | if (!PySequence_Check(groups)) { |
| 5044 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 5045 | return NULL; |
| 5046 | } |
| 5047 | len = PySequence_Size(groups); |
| 5048 | if (len > MAX_GROUPS) { |
| 5049 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 5050 | return NULL; |
| 5051 | } |
| 5052 | for(i = 0; i < len; i++) { |
| 5053 | PyObject *elem; |
| 5054 | elem = PySequence_GetItem(groups, i); |
| 5055 | if (!elem) |
| 5056 | return NULL; |
| 5057 | if (!PyInt_Check(elem)) { |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 5058 | if (!PyLong_Check(elem)) { |
| 5059 | PyErr_SetString(PyExc_TypeError, |
| 5060 | "groups must be integers"); |
| 5061 | Py_DECREF(elem); |
| 5062 | return NULL; |
| 5063 | } else { |
| 5064 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 5065 | if (PyErr_Occurred()) { |
| 5066 | PyErr_SetString(PyExc_TypeError, |
| 5067 | "group id too big"); |
| 5068 | Py_DECREF(elem); |
| 5069 | return NULL; |
| 5070 | } |
| 5071 | grouplist[i] = x; |
| 5072 | /* read back the value to see if it fitted in gid_t */ |
| 5073 | if (grouplist[i] != x) { |
| 5074 | PyErr_SetString(PyExc_TypeError, |
| 5075 | "group id too big"); |
| 5076 | Py_DECREF(elem); |
| 5077 | return NULL; |
| 5078 | } |
| 5079 | } |
| 5080 | } else { |
| 5081 | long x = PyInt_AsLong(elem); |
| 5082 | grouplist[i] = x; |
| 5083 | if (grouplist[i] != x) { |
| 5084 | PyErr_SetString(PyExc_TypeError, |
| 5085 | "group id too big"); |
| 5086 | Py_DECREF(elem); |
| 5087 | return NULL; |
| 5088 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5089 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5090 | Py_DECREF(elem); |
| 5091 | } |
| 5092 | |
| 5093 | if (setgroups(len, grouplist) < 0) |
| 5094 | return posix_error(); |
| 5095 | Py_INCREF(Py_None); |
| 5096 | return Py_None; |
| 5097 | } |
| 5098 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5099 | |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 5100 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5101 | static PyObject * |
| 5102 | wait_helper(int pid, int status, struct rusage *ru) |
| 5103 | { |
| 5104 | PyObject *result; |
| 5105 | static PyObject *struct_rusage; |
| 5106 | |
| 5107 | if (pid == -1) |
| 5108 | return posix_error(); |
| 5109 | |
| 5110 | if (struct_rusage == NULL) { |
| 5111 | PyObject *m = PyImport_ImportModule("resource"); |
| 5112 | if (m == NULL) |
| 5113 | return NULL; |
| 5114 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 5115 | Py_DECREF(m); |
| 5116 | if (struct_rusage == NULL) |
| 5117 | return NULL; |
| 5118 | } |
| 5119 | |
| 5120 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 5121 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 5122 | if (!result) |
| 5123 | return NULL; |
| 5124 | |
| 5125 | #ifndef doubletime |
| 5126 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 5127 | #endif |
| 5128 | |
| 5129 | PyStructSequence_SET_ITEM(result, 0, |
| 5130 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 5131 | PyStructSequence_SET_ITEM(result, 1, |
| 5132 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 5133 | #define SET_INT(result, index, value)\ |
| 5134 | PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value)) |
| 5135 | SET_INT(result, 2, ru->ru_maxrss); |
| 5136 | SET_INT(result, 3, ru->ru_ixrss); |
| 5137 | SET_INT(result, 4, ru->ru_idrss); |
| 5138 | SET_INT(result, 5, ru->ru_isrss); |
| 5139 | SET_INT(result, 6, ru->ru_minflt); |
| 5140 | SET_INT(result, 7, ru->ru_majflt); |
| 5141 | SET_INT(result, 8, ru->ru_nswap); |
| 5142 | SET_INT(result, 9, ru->ru_inblock); |
| 5143 | SET_INT(result, 10, ru->ru_oublock); |
| 5144 | SET_INT(result, 11, ru->ru_msgsnd); |
| 5145 | SET_INT(result, 12, ru->ru_msgrcv); |
| 5146 | SET_INT(result, 13, ru->ru_nsignals); |
| 5147 | SET_INT(result, 14, ru->ru_nvcsw); |
| 5148 | SET_INT(result, 15, ru->ru_nivcsw); |
| 5149 | #undef SET_INT |
| 5150 | |
| 5151 | if (PyErr_Occurred()) { |
| 5152 | Py_DECREF(result); |
| 5153 | return NULL; |
| 5154 | } |
| 5155 | |
Neal Norwitz | 9b00a56 | 2006-03-20 08:47:12 +0000 | [diff] [blame] | 5156 | return Py_BuildValue("iiN", pid, status, result); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5157 | } |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 5158 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5159 | |
| 5160 | #ifdef HAVE_WAIT3 |
| 5161 | PyDoc_STRVAR(posix_wait3__doc__, |
| 5162 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 5163 | Wait for completion of a child process."); |
| 5164 | |
| 5165 | static PyObject * |
| 5166 | posix_wait3(PyObject *self, PyObject *args) |
| 5167 | { |
| 5168 | int pid, options; |
| 5169 | struct rusage ru; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5170 | WAIT_TYPE status; |
| 5171 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5172 | |
| 5173 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 5174 | return NULL; |
| 5175 | |
| 5176 | Py_BEGIN_ALLOW_THREADS |
| 5177 | pid = wait3(&status, options, &ru); |
| 5178 | Py_END_ALLOW_THREADS |
| 5179 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5180 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5181 | } |
| 5182 | #endif /* HAVE_WAIT3 */ |
| 5183 | |
| 5184 | #ifdef HAVE_WAIT4 |
| 5185 | PyDoc_STRVAR(posix_wait4__doc__, |
| 5186 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 5187 | Wait for completion of a given child process."); |
| 5188 | |
| 5189 | static PyObject * |
| 5190 | posix_wait4(PyObject *self, PyObject *args) |
| 5191 | { |
| 5192 | int pid, options; |
| 5193 | struct rusage ru; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5194 | WAIT_TYPE status; |
| 5195 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5196 | |
| 5197 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 5198 | return NULL; |
| 5199 | |
| 5200 | Py_BEGIN_ALLOW_THREADS |
| 5201 | pid = wait4(pid, &status, options, &ru); |
| 5202 | Py_END_ALLOW_THREADS |
| 5203 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5204 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5205 | } |
| 5206 | #endif /* HAVE_WAIT4 */ |
| 5207 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5208 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5209 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5210 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5211 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5212 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5213 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5214 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5215 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5216 | int pid, options; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5217 | WAIT_TYPE status; |
| 5218 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5219 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5220 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5221 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5222 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 5223 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5224 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5225 | if (pid == -1) |
| 5226 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5227 | |
| 5228 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5229 | } |
| 5230 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5231 | #elif defined(HAVE_CWAIT) |
| 5232 | |
| 5233 | /* 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] | 5234 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5235 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5236 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5237 | |
| 5238 | static PyObject * |
| 5239 | posix_waitpid(PyObject *self, PyObject *args) |
| 5240 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5241 | intptr_t pid; |
| 5242 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5243 | |
| 5244 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 5245 | return NULL; |
| 5246 | Py_BEGIN_ALLOW_THREADS |
| 5247 | pid = _cwait(&status, pid, options); |
| 5248 | Py_END_ALLOW_THREADS |
| 5249 | if (pid == -1) |
| 5250 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5251 | |
| 5252 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 5253 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5254 | } |
| 5255 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5256 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5257 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5258 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5259 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5260 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5261 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5262 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5263 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5264 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5265 | int pid; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5266 | WAIT_TYPE status; |
| 5267 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5268 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5269 | Py_BEGIN_ALLOW_THREADS |
| 5270 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5271 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5272 | if (pid == -1) |
| 5273 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5274 | |
| 5275 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5276 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5277 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5278 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5279 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5280 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5281 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5282 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5283 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5284 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5285 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5286 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5287 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5288 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5289 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5290 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5291 | 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] | 5292 | #else |
| 5293 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 5294 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5295 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5296 | } |
| 5297 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5298 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5299 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5300 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5301 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5302 | 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] | 5303 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5304 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5305 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5306 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5307 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 5308 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5309 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5310 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5311 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5312 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 5313 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5314 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5315 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 5316 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5317 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5318 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5319 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5320 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5321 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5322 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5323 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5324 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 5325 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5326 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5327 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5328 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5329 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5330 | return posix_2str(args, "etet:symlink", symlink, NULL, NULL); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5331 | } |
| 5332 | #endif /* HAVE_SYMLINK */ |
| 5333 | |
| 5334 | |
| 5335 | #ifdef HAVE_TIMES |
| 5336 | #ifndef HZ |
| 5337 | #define HZ 60 /* Universal constant :-) */ |
| 5338 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5339 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5340 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 5341 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5342 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5343 | { |
| 5344 | ULONG value = 0; |
| 5345 | |
| 5346 | Py_BEGIN_ALLOW_THREADS |
| 5347 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 5348 | Py_END_ALLOW_THREADS |
| 5349 | |
| 5350 | return value; |
| 5351 | } |
| 5352 | |
| 5353 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5354 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5355 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5356 | /* Currently Only Uptime is Provided -- Others Later */ |
| 5357 | return Py_BuildValue("ddddd", |
| 5358 | (double)0 /* t.tms_utime / HZ */, |
| 5359 | (double)0 /* t.tms_stime / HZ */, |
| 5360 | (double)0 /* t.tms_cutime / HZ */, |
| 5361 | (double)0 /* t.tms_cstime / HZ */, |
| 5362 | (double)system_uptime() / 1000); |
| 5363 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5364 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5365 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5366 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5367 | { |
| 5368 | struct tms t; |
| 5369 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5370 | errno = 0; |
| 5371 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5372 | if (c == (clock_t) -1) |
| 5373 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5374 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5375 | (double)t.tms_utime / HZ, |
| 5376 | (double)t.tms_stime / HZ, |
| 5377 | (double)t.tms_cutime / HZ, |
| 5378 | (double)t.tms_cstime / HZ, |
| 5379 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5380 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5381 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5382 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5383 | |
| 5384 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5385 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5386 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5387 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5388 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5389 | { |
| 5390 | FILETIME create, exit, kernel, user; |
| 5391 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5392 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5393 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 5394 | /* The fields of a FILETIME structure are the hi and lo part |
| 5395 | of a 64-bit value expressed in 100 nanosecond units. |
| 5396 | 1e7 is one second in such units; 1e-7 the inverse. |
| 5397 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 5398 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5399 | return Py_BuildValue( |
| 5400 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5401 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 5402 | kernel.dwLowDateTime*1e-7), |
| 5403 | (double)(user.dwHighDateTime*429.4967296 + |
| 5404 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5405 | (double)0, |
| 5406 | (double)0, |
| 5407 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5408 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5409 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5410 | |
| 5411 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5412 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5413 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5414 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5415 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5416 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5417 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5418 | #ifdef HAVE_GETSID |
| 5419 | PyDoc_STRVAR(posix_getsid__doc__, |
| 5420 | "getsid(pid) -> sid\n\n\ |
| 5421 | Call the system call getsid()."); |
| 5422 | |
| 5423 | static PyObject * |
| 5424 | posix_getsid(PyObject *self, PyObject *args) |
| 5425 | { |
| 5426 | int pid, sid; |
| 5427 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 5428 | return NULL; |
| 5429 | sid = getsid(pid); |
| 5430 | if (sid < 0) |
| 5431 | return posix_error(); |
| 5432 | return PyInt_FromLong((long)sid); |
| 5433 | } |
| 5434 | #endif /* HAVE_GETSID */ |
| 5435 | |
| 5436 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5437 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5438 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5439 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5440 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5441 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5442 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5443 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5444 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5445 | if (setsid() < 0) |
| 5446 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5447 | Py_INCREF(Py_None); |
| 5448 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5449 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5450 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5451 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5452 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5453 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5454 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5455 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5456 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5457 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5458 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5459 | { |
| 5460 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5461 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5462 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5463 | if (setpgid(pid, pgrp) < 0) |
| 5464 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5465 | Py_INCREF(Py_None); |
| 5466 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5467 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5468 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5469 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5470 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5471 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5472 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5473 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5474 | 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] | 5475 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5476 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5477 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5478 | { |
| 5479 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5480 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5481 | return NULL; |
| 5482 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5483 | if (pgid < 0) |
| 5484 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5485 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5486 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5487 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5488 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5489 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5490 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5491 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5492 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5493 | 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] | 5494 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5495 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5496 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5497 | { |
| 5498 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5499 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5500 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5501 | if (tcsetpgrp(fd, pgid) < 0) |
| 5502 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5503 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5504 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5505 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5506 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5507 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5508 | /* Functions acting on file descriptors */ |
| 5509 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5510 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5511 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5512 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5513 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5514 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5515 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5516 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5517 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5518 | int flag; |
| 5519 | int mode = 0777; |
| 5520 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5521 | |
| 5522 | #ifdef MS_WINDOWS |
| 5523 | if (unicode_file_names()) { |
| 5524 | PyUnicodeObject *po; |
| 5525 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 5526 | Py_BEGIN_ALLOW_THREADS |
| 5527 | /* PyUnicode_AS_UNICODE OK without thread |
| 5528 | lock as it is a simple dereference. */ |
| 5529 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 5530 | Py_END_ALLOW_THREADS |
| 5531 | if (fd < 0) |
| 5532 | return posix_error(); |
| 5533 | return PyInt_FromLong((long)fd); |
| 5534 | } |
| 5535 | /* Drop the argument parsing error as narrow strings |
| 5536 | are also valid. */ |
| 5537 | PyErr_Clear(); |
| 5538 | } |
| 5539 | #endif |
| 5540 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5541 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5542 | Py_FileSystemDefaultEncoding, &file, |
| 5543 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5544 | return NULL; |
| 5545 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5546 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5547 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5548 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5549 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5550 | return posix_error_with_allocated_filename(file); |
| 5551 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5552 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5553 | } |
| 5554 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5555 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5556 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5557 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5558 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5559 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5560 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5561 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5562 | { |
| 5563 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5564 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5565 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5566 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5567 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5568 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5569 | if (res < 0) |
| 5570 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5571 | Py_INCREF(Py_None); |
| 5572 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5573 | } |
| 5574 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5575 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5576 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5577 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5578 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5579 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5580 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5581 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5582 | { |
| 5583 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5584 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5585 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5586 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5587 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5588 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5589 | if (fd < 0) |
| 5590 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5591 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5592 | } |
| 5593 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5594 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5595 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5596 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5597 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5598 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5599 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5600 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5601 | { |
| 5602 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5603 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5604 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5605 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5606 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5607 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5608 | if (res < 0) |
| 5609 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5610 | Py_INCREF(Py_None); |
| 5611 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5614 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5615 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5616 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5617 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5618 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5619 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5620 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5621 | { |
| 5622 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5623 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5624 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5625 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5626 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5627 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5628 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5629 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5630 | return NULL; |
| 5631 | #ifdef SEEK_SET |
| 5632 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5633 | switch (how) { |
| 5634 | case 0: how = SEEK_SET; break; |
| 5635 | case 1: how = SEEK_CUR; break; |
| 5636 | case 2: how = SEEK_END; break; |
| 5637 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5638 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5639 | |
| 5640 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5641 | pos = PyInt_AsLong(posobj); |
| 5642 | #else |
| 5643 | pos = PyLong_Check(posobj) ? |
| 5644 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 5645 | #endif |
| 5646 | if (PyErr_Occurred()) |
| 5647 | return NULL; |
| 5648 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5649 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5650 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5651 | res = _lseeki64(fd, pos, how); |
| 5652 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5653 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5654 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5655 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5656 | if (res < 0) |
| 5657 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5658 | |
| 5659 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5660 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5661 | #else |
| 5662 | return PyLong_FromLongLong(res); |
| 5663 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5664 | } |
| 5665 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5666 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5667 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5668 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5669 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5670 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5671 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5672 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5673 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5674 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5675 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5676 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5677 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 5678 | if (size < 0) { |
| 5679 | errno = EINVAL; |
| 5680 | return posix_error(); |
| 5681 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5682 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5683 | if (buffer == NULL) |
| 5684 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5685 | Py_BEGIN_ALLOW_THREADS |
| 5686 | n = read(fd, PyString_AsString(buffer), size); |
| 5687 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5688 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5689 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5690 | return posix_error(); |
| 5691 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5692 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5693 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5694 | return buffer; |
| 5695 | } |
| 5696 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5697 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5698 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5699 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5700 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5701 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5702 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5703 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5704 | { |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5705 | int fd; |
| 5706 | Py_ssize_t size; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5707 | char *buffer; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5708 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5709 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5710 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5711 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5712 | size = write(fd, buffer, (size_t)size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5713 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5714 | if (size < 0) |
| 5715 | return posix_error(); |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5716 | return PyInt_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5717 | } |
| 5718 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5719 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5720 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5721 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5722 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5723 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5724 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5725 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5726 | { |
| 5727 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5728 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5729 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5730 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5731 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5732 | #ifdef __VMS |
| 5733 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5734 | fsync(fd); |
| 5735 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5736 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5737 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5738 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5739 | if (res != 0) { |
| 5740 | #ifdef MS_WINDOWS |
| 5741 | return win32_error("fstat", NULL); |
| 5742 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5743 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5744 | #endif |
| 5745 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5746 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5747 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5748 | } |
| 5749 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5750 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5751 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5752 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5753 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5754 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5755 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5756 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5757 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5758 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5759 | char *mode = "r"; |
| 5760 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5761 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5762 | PyObject *f; |
| 5763 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5764 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5765 | |
Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 5766 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 5767 | PyErr_Format(PyExc_ValueError, |
| 5768 | "invalid file mode '%s'", mode); |
| 5769 | return NULL; |
| 5770 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5771 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 5772 | #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H) |
Georg Brandl | 54a188a | 2006-03-31 20:00:11 +0000 | [diff] [blame] | 5773 | if (mode[0] == 'a') { |
| 5774 | /* try to make sure the O_APPEND flag is set */ |
| 5775 | int flags; |
| 5776 | flags = fcntl(fd, F_GETFL); |
| 5777 | if (flags != -1) |
| 5778 | fcntl(fd, F_SETFL, flags | O_APPEND); |
| 5779 | fp = fdopen(fd, mode); |
Thomas Wouters | 2a9a6b0 | 2006-03-31 22:38:19 +0000 | [diff] [blame] | 5780 | if (fp == NULL && flags != -1) |
Georg Brandl | 54a188a | 2006-03-31 20:00:11 +0000 | [diff] [blame] | 5781 | /* restore old mode if fdopen failed */ |
| 5782 | fcntl(fd, F_SETFL, flags); |
| 5783 | } else { |
| 5784 | fp = fdopen(fd, mode); |
| 5785 | } |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 5786 | #else |
| 5787 | fp = fdopen(fd, mode); |
| 5788 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5789 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5790 | if (fp == NULL) |
| 5791 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 5792 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5793 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5794 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5795 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5796 | } |
| 5797 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5798 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5799 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5800 | 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] | 5801 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5802 | |
| 5803 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5804 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5805 | { |
| 5806 | int fd; |
| 5807 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5808 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5809 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5810 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5811 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5812 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5813 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5814 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5815 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5816 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5817 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5818 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5819 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5820 | #if defined(PYOS_OS2) |
| 5821 | HFILE read, write; |
| 5822 | APIRET rc; |
| 5823 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5824 | Py_BEGIN_ALLOW_THREADS |
| 5825 | rc = DosCreatePipe( &read, &write, 4096); |
| 5826 | Py_END_ALLOW_THREADS |
| 5827 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5828 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5829 | |
| 5830 | return Py_BuildValue("(ii)", read, write); |
| 5831 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5832 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5833 | int fds[2]; |
| 5834 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5835 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5836 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5837 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5838 | if (res != 0) |
| 5839 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5840 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5841 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5842 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5843 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5844 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5845 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5846 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5847 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5848 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5849 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5850 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5851 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5852 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5853 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5854 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5855 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5856 | #endif /* HAVE_PIPE */ |
| 5857 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5858 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5859 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5860 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5861 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5862 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5863 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5864 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5865 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5866 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5867 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5868 | int mode = 0666; |
| 5869 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5870 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5871 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5872 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5873 | res = mkfifo(filename, mode); |
| 5874 | Py_END_ALLOW_THREADS |
| 5875 | if (res < 0) |
| 5876 | return posix_error(); |
| 5877 | Py_INCREF(Py_None); |
| 5878 | return Py_None; |
| 5879 | } |
| 5880 | #endif |
| 5881 | |
| 5882 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5883 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5884 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5885 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5886 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5887 | named filename. mode specifies both the permissions to use and the\n\ |
| 5888 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5889 | 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] | 5890 | device defines the newly created device special file (probably using\n\ |
| 5891 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5892 | |
| 5893 | |
| 5894 | static PyObject * |
| 5895 | posix_mknod(PyObject *self, PyObject *args) |
| 5896 | { |
| 5897 | char *filename; |
| 5898 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5899 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5900 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5901 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5902 | return NULL; |
| 5903 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5904 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5905 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5906 | if (res < 0) |
| 5907 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5908 | Py_INCREF(Py_None); |
| 5909 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5910 | } |
| 5911 | #endif |
| 5912 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5913 | #ifdef HAVE_DEVICE_MACROS |
| 5914 | PyDoc_STRVAR(posix_major__doc__, |
| 5915 | "major(device) -> major number\n\ |
| 5916 | Extracts a device major number from a raw device number."); |
| 5917 | |
| 5918 | static PyObject * |
| 5919 | posix_major(PyObject *self, PyObject *args) |
| 5920 | { |
| 5921 | int device; |
| 5922 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5923 | return NULL; |
| 5924 | return PyInt_FromLong((long)major(device)); |
| 5925 | } |
| 5926 | |
| 5927 | PyDoc_STRVAR(posix_minor__doc__, |
| 5928 | "minor(device) -> minor number\n\ |
| 5929 | Extracts a device minor number from a raw device number."); |
| 5930 | |
| 5931 | static PyObject * |
| 5932 | posix_minor(PyObject *self, PyObject *args) |
| 5933 | { |
| 5934 | int device; |
| 5935 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5936 | return NULL; |
| 5937 | return PyInt_FromLong((long)minor(device)); |
| 5938 | } |
| 5939 | |
| 5940 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5941 | "makedev(major, minor) -> device number\n\ |
| 5942 | Composes a raw device number from the major and minor device numbers."); |
| 5943 | |
| 5944 | static PyObject * |
| 5945 | posix_makedev(PyObject *self, PyObject *args) |
| 5946 | { |
| 5947 | int major, minor; |
| 5948 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5949 | return NULL; |
| 5950 | return PyInt_FromLong((long)makedev(major, minor)); |
| 5951 | } |
| 5952 | #endif /* device macros */ |
| 5953 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5954 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5955 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5956 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5957 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5958 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5959 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5960 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5961 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5962 | { |
| 5963 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5964 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5965 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5966 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5967 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5968 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5969 | return NULL; |
| 5970 | |
| 5971 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5972 | length = PyInt_AsLong(lenobj); |
| 5973 | #else |
| 5974 | length = PyLong_Check(lenobj) ? |
| 5975 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 5976 | #endif |
| 5977 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5978 | return NULL; |
| 5979 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5980 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5981 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5982 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5983 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5984 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5985 | return NULL; |
| 5986 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5987 | Py_INCREF(Py_None); |
| 5988 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5989 | } |
| 5990 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5991 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5992 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5993 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5994 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5995 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5996 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5997 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5998 | * get re-set with another call for the same key. */ |
| 5999 | static PyObject *posix_putenv_garbage; |
| 6000 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6001 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6002 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6003 | { |
| 6004 | char *s1, *s2; |
| 6005 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6006 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 6007 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6008 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6009 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6010 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6011 | |
| 6012 | #if defined(PYOS_OS2) |
| 6013 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 6014 | APIRET rc; |
| 6015 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6016 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 6017 | if (rc != NO_ERROR) |
| 6018 | return os2_error(rc); |
| 6019 | |
| 6020 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 6021 | APIRET rc; |
| 6022 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6023 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 6024 | if (rc != NO_ERROR) |
| 6025 | return os2_error(rc); |
| 6026 | } else { |
| 6027 | #endif |
| 6028 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6029 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 6030 | len = strlen(s1) + strlen(s2) + 2; |
| 6031 | /* len includes space for a trailing \0; the size arg to |
| 6032 | PyString_FromStringAndSize does not count that */ |
| 6033 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6034 | if (newstr == NULL) |
| 6035 | return PyErr_NoMemory(); |
| 6036 | new = PyString_AS_STRING(newstr); |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 6037 | PyOS_snprintf(new, len, "%s=%s", s1, s2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6038 | if (putenv(new)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 6039 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6040 | posix_error(); |
| 6041 | return NULL; |
| 6042 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6043 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 6044 | * this will cause previous value to be collected. This has to |
| 6045 | * happen after the real putenv() call because the old value |
| 6046 | * was still accessible until then. */ |
| 6047 | if (PyDict_SetItem(posix_putenv_garbage, |
| 6048 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 6049 | /* really not much we can do; just leak */ |
| 6050 | PyErr_Clear(); |
| 6051 | } |
| 6052 | else { |
| 6053 | Py_DECREF(newstr); |
| 6054 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6055 | |
| 6056 | #if defined(PYOS_OS2) |
| 6057 | } |
| 6058 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6059 | Py_INCREF(Py_None); |
| 6060 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6061 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6062 | #endif /* putenv */ |
| 6063 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6064 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6065 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6066 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6067 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6068 | |
| 6069 | static PyObject * |
| 6070 | posix_unsetenv(PyObject *self, PyObject *args) |
| 6071 | { |
| 6072 | char *s1; |
| 6073 | |
| 6074 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 6075 | return NULL; |
| 6076 | |
| 6077 | unsetenv(s1); |
| 6078 | |
| 6079 | /* Remove the key from posix_putenv_garbage; |
| 6080 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6081 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6082 | * old value was still accessible until then. |
| 6083 | */ |
| 6084 | if (PyDict_DelItem(posix_putenv_garbage, |
| 6085 | PyTuple_GET_ITEM(args, 0))) { |
| 6086 | /* really not much we can do; just leak */ |
| 6087 | PyErr_Clear(); |
| 6088 | } |
| 6089 | |
| 6090 | Py_INCREF(Py_None); |
| 6091 | return Py_None; |
| 6092 | } |
| 6093 | #endif /* unsetenv */ |
| 6094 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6095 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6096 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6097 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6098 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6099 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6100 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6101 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6102 | { |
| 6103 | int code; |
| 6104 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6105 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6106 | return NULL; |
| 6107 | message = strerror(code); |
| 6108 | if (message == NULL) { |
| 6109 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 6110 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6111 | return NULL; |
| 6112 | } |
| 6113 | return PyString_FromString(message); |
| 6114 | } |
| 6115 | #endif /* strerror */ |
| 6116 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6117 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6118 | #ifdef HAVE_SYS_WAIT_H |
| 6119 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6120 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6121 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6122 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6123 | 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] | 6124 | |
| 6125 | static PyObject * |
| 6126 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 6127 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6128 | WAIT_TYPE status; |
| 6129 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6130 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6131 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6132 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6133 | |
| 6134 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6135 | } |
| 6136 | #endif /* WCOREDUMP */ |
| 6137 | |
| 6138 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6139 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6140 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6141 | 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] | 6142 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6143 | |
| 6144 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6145 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6146 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6147 | WAIT_TYPE status; |
| 6148 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6149 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6150 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6151 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6152 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6153 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6154 | } |
| 6155 | #endif /* WIFCONTINUED */ |
| 6156 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6157 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6158 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6159 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6160 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6161 | |
| 6162 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6163 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6164 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6165 | WAIT_TYPE status; |
| 6166 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6167 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6168 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6169 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6170 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6171 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6172 | } |
| 6173 | #endif /* WIFSTOPPED */ |
| 6174 | |
| 6175 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6176 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6177 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6178 | 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] | 6179 | |
| 6180 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6181 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6182 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6183 | WAIT_TYPE status; |
| 6184 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6185 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6186 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6187 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6188 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6189 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6190 | } |
| 6191 | #endif /* WIFSIGNALED */ |
| 6192 | |
| 6193 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6194 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6195 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6196 | 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] | 6197 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6198 | |
| 6199 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6200 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6201 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6202 | WAIT_TYPE status; |
| 6203 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6204 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6205 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6206 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6207 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6208 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6209 | } |
| 6210 | #endif /* WIFEXITED */ |
| 6211 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6212 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6213 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6214 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6215 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6216 | |
| 6217 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6218 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6219 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6220 | WAIT_TYPE status; |
| 6221 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6222 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6223 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6224 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6225 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6226 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 6227 | } |
| 6228 | #endif /* WEXITSTATUS */ |
| 6229 | |
| 6230 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6231 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6232 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6233 | 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] | 6234 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6235 | |
| 6236 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6237 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6238 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6239 | WAIT_TYPE status; |
| 6240 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6241 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6242 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6243 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6244 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6245 | return Py_BuildValue("i", WTERMSIG(status)); |
| 6246 | } |
| 6247 | #endif /* WTERMSIG */ |
| 6248 | |
| 6249 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6250 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6251 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6252 | Return the signal that stopped the process that provided\n\ |
| 6253 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6254 | |
| 6255 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6256 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6257 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6258 | WAIT_TYPE status; |
| 6259 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6260 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6261 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6262 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6263 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6264 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 6265 | } |
| 6266 | #endif /* WSTOPSIG */ |
| 6267 | |
| 6268 | #endif /* HAVE_SYS_WAIT_H */ |
| 6269 | |
| 6270 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6271 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 6272 | #ifdef _SCO_DS |
| 6273 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 6274 | needed definitions in sys/statvfs.h */ |
| 6275 | #define _SVID3 |
| 6276 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6277 | #include <sys/statvfs.h> |
| 6278 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6279 | static PyObject* |
| 6280 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 6281 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 6282 | if (v == NULL) |
| 6283 | return NULL; |
| 6284 | |
| 6285 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6286 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6287 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 6288 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 6289 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 6290 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 6291 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 6292 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 6293 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 6294 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6295 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6296 | #else |
| 6297 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6298 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6299 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6300 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6301 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6302 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6303 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6304 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6305 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6306 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6307 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6308 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6309 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6310 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6311 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6312 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6313 | #endif |
| 6314 | |
| 6315 | return v; |
| 6316 | } |
| 6317 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6318 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6319 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6320 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6321 | |
| 6322 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6323 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6324 | { |
| 6325 | int fd, res; |
| 6326 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6327 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6328 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6329 | return NULL; |
| 6330 | Py_BEGIN_ALLOW_THREADS |
| 6331 | res = fstatvfs(fd, &st); |
| 6332 | Py_END_ALLOW_THREADS |
| 6333 | if (res != 0) |
| 6334 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6335 | |
| 6336 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6337 | } |
| 6338 | #endif /* HAVE_FSTATVFS */ |
| 6339 | |
| 6340 | |
| 6341 | #if defined(HAVE_STATVFS) |
| 6342 | #include <sys/statvfs.h> |
| 6343 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6344 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6345 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6346 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6347 | |
| 6348 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6349 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6350 | { |
| 6351 | char *path; |
| 6352 | int res; |
| 6353 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6354 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6355 | return NULL; |
| 6356 | Py_BEGIN_ALLOW_THREADS |
| 6357 | res = statvfs(path, &st); |
| 6358 | Py_END_ALLOW_THREADS |
| 6359 | if (res != 0) |
| 6360 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6361 | |
| 6362 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6363 | } |
| 6364 | #endif /* HAVE_STATVFS */ |
| 6365 | |
| 6366 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6367 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6368 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6369 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6370 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 6371 | 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] | 6372 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6373 | |
| 6374 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6375 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6376 | { |
| 6377 | PyObject *result = NULL; |
| 6378 | char *dir = NULL; |
| 6379 | char *pfx = NULL; |
| 6380 | char *name; |
| 6381 | |
| 6382 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 6383 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6384 | |
| 6385 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6386 | "tempnam is a potential security risk to your program") < 0) |
| 6387 | return NULL; |
| 6388 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6389 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6390 | name = _tempnam(dir, pfx); |
| 6391 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6392 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6393 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6394 | if (name == NULL) |
| 6395 | return PyErr_NoMemory(); |
| 6396 | result = PyString_FromString(name); |
| 6397 | free(name); |
| 6398 | return result; |
| 6399 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 6400 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6401 | |
| 6402 | |
| 6403 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6404 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6405 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6406 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6407 | |
| 6408 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6409 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6410 | { |
| 6411 | FILE *fp; |
| 6412 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6413 | fp = tmpfile(); |
| 6414 | if (fp == NULL) |
| 6415 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 6416 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6417 | } |
| 6418 | #endif |
| 6419 | |
| 6420 | |
| 6421 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6422 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6423 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6424 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6425 | |
| 6426 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6427 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6428 | { |
| 6429 | char buffer[L_tmpnam]; |
| 6430 | char *name; |
| 6431 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6432 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6433 | "tmpnam is a potential security risk to your program") < 0) |
| 6434 | return NULL; |
| 6435 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6436 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6437 | name = tmpnam_r(buffer); |
| 6438 | #else |
| 6439 | name = tmpnam(buffer); |
| 6440 | #endif |
| 6441 | if (name == NULL) { |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 6442 | PyObject *err = Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6443 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6444 | "unexpected NULL from tmpnam_r" |
| 6445 | #else |
| 6446 | "unexpected NULL from tmpnam" |
| 6447 | #endif |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 6448 | ); |
| 6449 | PyErr_SetObject(PyExc_OSError, err); |
| 6450 | Py_XDECREF(err); |
| 6451 | return NULL; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6452 | } |
| 6453 | return PyString_FromString(buffer); |
| 6454 | } |
| 6455 | #endif |
| 6456 | |
| 6457 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6458 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 6459 | * It maps strings representing configuration variable names to |
| 6460 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6461 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6462 | * rarely-used constants. There are three separate tables that use |
| 6463 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6464 | * |
| 6465 | * This code is always included, even if none of the interfaces that |
| 6466 | * need it are included. The #if hackery needed to avoid it would be |
| 6467 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6468 | */ |
| 6469 | struct constdef { |
| 6470 | char *name; |
| 6471 | long value; |
| 6472 | }; |
| 6473 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6474 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6475 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 6476 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6477 | { |
| 6478 | if (PyInt_Check(arg)) { |
| 6479 | *valuep = PyInt_AS_LONG(arg); |
| 6480 | return 1; |
| 6481 | } |
| 6482 | if (PyString_Check(arg)) { |
| 6483 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6484 | size_t lo = 0; |
| 6485 | size_t mid; |
| 6486 | size_t hi = tablesize; |
| 6487 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6488 | char *confname = PyString_AS_STRING(arg); |
| 6489 | while (lo < hi) { |
| 6490 | mid = (lo + hi) / 2; |
| 6491 | cmp = strcmp(confname, table[mid].name); |
| 6492 | if (cmp < 0) |
| 6493 | hi = mid; |
| 6494 | else if (cmp > 0) |
| 6495 | lo = mid + 1; |
| 6496 | else { |
| 6497 | *valuep = table[mid].value; |
| 6498 | return 1; |
| 6499 | } |
| 6500 | } |
| 6501 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 6502 | } |
| 6503 | else |
| 6504 | PyErr_SetString(PyExc_TypeError, |
| 6505 | "configuration names must be strings or integers"); |
| 6506 | return 0; |
| 6507 | } |
| 6508 | |
| 6509 | |
| 6510 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 6511 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6512 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 6513 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 6514 | #endif |
| 6515 | #ifdef _PC_ABI_ASYNC_IO |
| 6516 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 6517 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6518 | #ifdef _PC_ASYNC_IO |
| 6519 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 6520 | #endif |
| 6521 | #ifdef _PC_CHOWN_RESTRICTED |
| 6522 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 6523 | #endif |
| 6524 | #ifdef _PC_FILESIZEBITS |
| 6525 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 6526 | #endif |
| 6527 | #ifdef _PC_LAST |
| 6528 | {"PC_LAST", _PC_LAST}, |
| 6529 | #endif |
| 6530 | #ifdef _PC_LINK_MAX |
| 6531 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 6532 | #endif |
| 6533 | #ifdef _PC_MAX_CANON |
| 6534 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 6535 | #endif |
| 6536 | #ifdef _PC_MAX_INPUT |
| 6537 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 6538 | #endif |
| 6539 | #ifdef _PC_NAME_MAX |
| 6540 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 6541 | #endif |
| 6542 | #ifdef _PC_NO_TRUNC |
| 6543 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 6544 | #endif |
| 6545 | #ifdef _PC_PATH_MAX |
| 6546 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 6547 | #endif |
| 6548 | #ifdef _PC_PIPE_BUF |
| 6549 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 6550 | #endif |
| 6551 | #ifdef _PC_PRIO_IO |
| 6552 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 6553 | #endif |
| 6554 | #ifdef _PC_SOCK_MAXBUF |
| 6555 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 6556 | #endif |
| 6557 | #ifdef _PC_SYNC_IO |
| 6558 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 6559 | #endif |
| 6560 | #ifdef _PC_VDISABLE |
| 6561 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 6562 | #endif |
| 6563 | }; |
| 6564 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6565 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6566 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6567 | { |
| 6568 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 6569 | sizeof(posix_constants_pathconf) |
| 6570 | / sizeof(struct constdef)); |
| 6571 | } |
| 6572 | #endif |
| 6573 | |
| 6574 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6575 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6576 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6577 | 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] | 6578 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6579 | |
| 6580 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6581 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6582 | { |
| 6583 | PyObject *result = NULL; |
| 6584 | int name, fd; |
| 6585 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6586 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 6587 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6588 | long limit; |
| 6589 | |
| 6590 | errno = 0; |
| 6591 | limit = fpathconf(fd, name); |
| 6592 | if (limit == -1 && errno != 0) |
| 6593 | posix_error(); |
| 6594 | else |
| 6595 | result = PyInt_FromLong(limit); |
| 6596 | } |
| 6597 | return result; |
| 6598 | } |
| 6599 | #endif |
| 6600 | |
| 6601 | |
| 6602 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6603 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6604 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6605 | 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] | 6606 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6607 | |
| 6608 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6609 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6610 | { |
| 6611 | PyObject *result = NULL; |
| 6612 | int name; |
| 6613 | char *path; |
| 6614 | |
| 6615 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 6616 | conv_path_confname, &name)) { |
| 6617 | long limit; |
| 6618 | |
| 6619 | errno = 0; |
| 6620 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6621 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6622 | if (errno == EINVAL) |
| 6623 | /* could be a path or name problem */ |
| 6624 | posix_error(); |
| 6625 | else |
| 6626 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6627 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6628 | else |
| 6629 | result = PyInt_FromLong(limit); |
| 6630 | } |
| 6631 | return result; |
| 6632 | } |
| 6633 | #endif |
| 6634 | |
| 6635 | #ifdef HAVE_CONFSTR |
| 6636 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6637 | #ifdef _CS_ARCHITECTURE |
| 6638 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 6639 | #endif |
| 6640 | #ifdef _CS_HOSTNAME |
| 6641 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 6642 | #endif |
| 6643 | #ifdef _CS_HW_PROVIDER |
| 6644 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 6645 | #endif |
| 6646 | #ifdef _CS_HW_SERIAL |
| 6647 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 6648 | #endif |
| 6649 | #ifdef _CS_INITTAB_NAME |
| 6650 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 6651 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6652 | #ifdef _CS_LFS64_CFLAGS |
| 6653 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 6654 | #endif |
| 6655 | #ifdef _CS_LFS64_LDFLAGS |
| 6656 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 6657 | #endif |
| 6658 | #ifdef _CS_LFS64_LIBS |
| 6659 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 6660 | #endif |
| 6661 | #ifdef _CS_LFS64_LINTFLAGS |
| 6662 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 6663 | #endif |
| 6664 | #ifdef _CS_LFS_CFLAGS |
| 6665 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 6666 | #endif |
| 6667 | #ifdef _CS_LFS_LDFLAGS |
| 6668 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6669 | #endif |
| 6670 | #ifdef _CS_LFS_LIBS |
| 6671 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6672 | #endif |
| 6673 | #ifdef _CS_LFS_LINTFLAGS |
| 6674 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6675 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6676 | #ifdef _CS_MACHINE |
| 6677 | {"CS_MACHINE", _CS_MACHINE}, |
| 6678 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6679 | #ifdef _CS_PATH |
| 6680 | {"CS_PATH", _CS_PATH}, |
| 6681 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6682 | #ifdef _CS_RELEASE |
| 6683 | {"CS_RELEASE", _CS_RELEASE}, |
| 6684 | #endif |
| 6685 | #ifdef _CS_SRPC_DOMAIN |
| 6686 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6687 | #endif |
| 6688 | #ifdef _CS_SYSNAME |
| 6689 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6690 | #endif |
| 6691 | #ifdef _CS_VERSION |
| 6692 | {"CS_VERSION", _CS_VERSION}, |
| 6693 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6694 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6695 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6696 | #endif |
| 6697 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6698 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6699 | #endif |
| 6700 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6701 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6702 | #endif |
| 6703 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6704 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6705 | #endif |
| 6706 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6707 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6708 | #endif |
| 6709 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6710 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6711 | #endif |
| 6712 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6713 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6714 | #endif |
| 6715 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6716 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6717 | #endif |
| 6718 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6719 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6720 | #endif |
| 6721 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6722 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6723 | #endif |
| 6724 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6725 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6726 | #endif |
| 6727 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6728 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6729 | #endif |
| 6730 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6731 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6732 | #endif |
| 6733 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6734 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6735 | #endif |
| 6736 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6737 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6738 | #endif |
| 6739 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6740 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6741 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6742 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6743 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6744 | #endif |
| 6745 | #ifdef _MIPS_CS_BASE |
| 6746 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6747 | #endif |
| 6748 | #ifdef _MIPS_CS_HOSTID |
| 6749 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6750 | #endif |
| 6751 | #ifdef _MIPS_CS_HW_NAME |
| 6752 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6753 | #endif |
| 6754 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6755 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6756 | #endif |
| 6757 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6758 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6759 | #endif |
| 6760 | #ifdef _MIPS_CS_OSREL_MIN |
| 6761 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6762 | #endif |
| 6763 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6764 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6765 | #endif |
| 6766 | #ifdef _MIPS_CS_OS_NAME |
| 6767 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6768 | #endif |
| 6769 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6770 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6771 | #endif |
| 6772 | #ifdef _MIPS_CS_PROCESSORS |
| 6773 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6774 | #endif |
| 6775 | #ifdef _MIPS_CS_SERIAL |
| 6776 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6777 | #endif |
| 6778 | #ifdef _MIPS_CS_VENDOR |
| 6779 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6780 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6781 | }; |
| 6782 | |
| 6783 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6784 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6785 | { |
| 6786 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6787 | sizeof(posix_constants_confstr) |
| 6788 | / sizeof(struct constdef)); |
| 6789 | } |
| 6790 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6791 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6792 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6793 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6794 | |
| 6795 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6796 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6797 | { |
| 6798 | PyObject *result = NULL; |
| 6799 | int name; |
| 6800 | char buffer[64]; |
| 6801 | |
| 6802 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 6803 | int len = confstr(name, buffer, sizeof(buffer)); |
| 6804 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6805 | errno = 0; |
| 6806 | if (len == 0) { |
| 6807 | if (errno != 0) |
| 6808 | posix_error(); |
| 6809 | else |
| 6810 | result = PyString_FromString(""); |
| 6811 | } |
| 6812 | else { |
| 6813 | if (len >= sizeof(buffer)) { |
| 6814 | result = PyString_FromStringAndSize(NULL, len); |
| 6815 | if (result != NULL) |
| 6816 | confstr(name, PyString_AS_STRING(result), len+1); |
| 6817 | } |
| 6818 | else |
| 6819 | result = PyString_FromString(buffer); |
| 6820 | } |
| 6821 | } |
| 6822 | return result; |
| 6823 | } |
| 6824 | #endif |
| 6825 | |
| 6826 | |
| 6827 | #ifdef HAVE_SYSCONF |
| 6828 | static struct constdef posix_constants_sysconf[] = { |
| 6829 | #ifdef _SC_2_CHAR_TERM |
| 6830 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6831 | #endif |
| 6832 | #ifdef _SC_2_C_BIND |
| 6833 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6834 | #endif |
| 6835 | #ifdef _SC_2_C_DEV |
| 6836 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6837 | #endif |
| 6838 | #ifdef _SC_2_C_VERSION |
| 6839 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6840 | #endif |
| 6841 | #ifdef _SC_2_FORT_DEV |
| 6842 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6843 | #endif |
| 6844 | #ifdef _SC_2_FORT_RUN |
| 6845 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6846 | #endif |
| 6847 | #ifdef _SC_2_LOCALEDEF |
| 6848 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6849 | #endif |
| 6850 | #ifdef _SC_2_SW_DEV |
| 6851 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6852 | #endif |
| 6853 | #ifdef _SC_2_UPE |
| 6854 | {"SC_2_UPE", _SC_2_UPE}, |
| 6855 | #endif |
| 6856 | #ifdef _SC_2_VERSION |
| 6857 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6858 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6859 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6860 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6861 | #endif |
| 6862 | #ifdef _SC_ACL |
| 6863 | {"SC_ACL", _SC_ACL}, |
| 6864 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6865 | #ifdef _SC_AIO_LISTIO_MAX |
| 6866 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6867 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6868 | #ifdef _SC_AIO_MAX |
| 6869 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6870 | #endif |
| 6871 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6872 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6873 | #endif |
| 6874 | #ifdef _SC_ARG_MAX |
| 6875 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6876 | #endif |
| 6877 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6878 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6879 | #endif |
| 6880 | #ifdef _SC_ATEXIT_MAX |
| 6881 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6882 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6883 | #ifdef _SC_AUDIT |
| 6884 | {"SC_AUDIT", _SC_AUDIT}, |
| 6885 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6886 | #ifdef _SC_AVPHYS_PAGES |
| 6887 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6888 | #endif |
| 6889 | #ifdef _SC_BC_BASE_MAX |
| 6890 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6891 | #endif |
| 6892 | #ifdef _SC_BC_DIM_MAX |
| 6893 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6894 | #endif |
| 6895 | #ifdef _SC_BC_SCALE_MAX |
| 6896 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6897 | #endif |
| 6898 | #ifdef _SC_BC_STRING_MAX |
| 6899 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6900 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6901 | #ifdef _SC_CAP |
| 6902 | {"SC_CAP", _SC_CAP}, |
| 6903 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6904 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6905 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6906 | #endif |
| 6907 | #ifdef _SC_CHAR_BIT |
| 6908 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6909 | #endif |
| 6910 | #ifdef _SC_CHAR_MAX |
| 6911 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6912 | #endif |
| 6913 | #ifdef _SC_CHAR_MIN |
| 6914 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6915 | #endif |
| 6916 | #ifdef _SC_CHILD_MAX |
| 6917 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6918 | #endif |
| 6919 | #ifdef _SC_CLK_TCK |
| 6920 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6921 | #endif |
| 6922 | #ifdef _SC_COHER_BLKSZ |
| 6923 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6924 | #endif |
| 6925 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6926 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6927 | #endif |
| 6928 | #ifdef _SC_DCACHE_ASSOC |
| 6929 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6930 | #endif |
| 6931 | #ifdef _SC_DCACHE_BLKSZ |
| 6932 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6933 | #endif |
| 6934 | #ifdef _SC_DCACHE_LINESZ |
| 6935 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6936 | #endif |
| 6937 | #ifdef _SC_DCACHE_SZ |
| 6938 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6939 | #endif |
| 6940 | #ifdef _SC_DCACHE_TBLKSZ |
| 6941 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6942 | #endif |
| 6943 | #ifdef _SC_DELAYTIMER_MAX |
| 6944 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6945 | #endif |
| 6946 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6947 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6948 | #endif |
| 6949 | #ifdef _SC_EXPR_NEST_MAX |
| 6950 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6951 | #endif |
| 6952 | #ifdef _SC_FSYNC |
| 6953 | {"SC_FSYNC", _SC_FSYNC}, |
| 6954 | #endif |
| 6955 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6956 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6957 | #endif |
| 6958 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6959 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6960 | #endif |
| 6961 | #ifdef _SC_ICACHE_ASSOC |
| 6962 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6963 | #endif |
| 6964 | #ifdef _SC_ICACHE_BLKSZ |
| 6965 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6966 | #endif |
| 6967 | #ifdef _SC_ICACHE_LINESZ |
| 6968 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6969 | #endif |
| 6970 | #ifdef _SC_ICACHE_SZ |
| 6971 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6972 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6973 | #ifdef _SC_INF |
| 6974 | {"SC_INF", _SC_INF}, |
| 6975 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6976 | #ifdef _SC_INT_MAX |
| 6977 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6978 | #endif |
| 6979 | #ifdef _SC_INT_MIN |
| 6980 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6981 | #endif |
| 6982 | #ifdef _SC_IOV_MAX |
| 6983 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6984 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6985 | #ifdef _SC_IP_SECOPTS |
| 6986 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6987 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6988 | #ifdef _SC_JOB_CONTROL |
| 6989 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6990 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6991 | #ifdef _SC_KERN_POINTERS |
| 6992 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6993 | #endif |
| 6994 | #ifdef _SC_KERN_SIM |
| 6995 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6996 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6997 | #ifdef _SC_LINE_MAX |
| 6998 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6999 | #endif |
| 7000 | #ifdef _SC_LOGIN_NAME_MAX |
| 7001 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 7002 | #endif |
| 7003 | #ifdef _SC_LOGNAME_MAX |
| 7004 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 7005 | #endif |
| 7006 | #ifdef _SC_LONG_BIT |
| 7007 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 7008 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7009 | #ifdef _SC_MAC |
| 7010 | {"SC_MAC", _SC_MAC}, |
| 7011 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7012 | #ifdef _SC_MAPPED_FILES |
| 7013 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 7014 | #endif |
| 7015 | #ifdef _SC_MAXPID |
| 7016 | {"SC_MAXPID", _SC_MAXPID}, |
| 7017 | #endif |
| 7018 | #ifdef _SC_MB_LEN_MAX |
| 7019 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 7020 | #endif |
| 7021 | #ifdef _SC_MEMLOCK |
| 7022 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 7023 | #endif |
| 7024 | #ifdef _SC_MEMLOCK_RANGE |
| 7025 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 7026 | #endif |
| 7027 | #ifdef _SC_MEMORY_PROTECTION |
| 7028 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 7029 | #endif |
| 7030 | #ifdef _SC_MESSAGE_PASSING |
| 7031 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 7032 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7033 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 7034 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 7035 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7036 | #ifdef _SC_MQ_OPEN_MAX |
| 7037 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 7038 | #endif |
| 7039 | #ifdef _SC_MQ_PRIO_MAX |
| 7040 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 7041 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7042 | #ifdef _SC_NACLS_MAX |
| 7043 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 7044 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7045 | #ifdef _SC_NGROUPS_MAX |
| 7046 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 7047 | #endif |
| 7048 | #ifdef _SC_NL_ARGMAX |
| 7049 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 7050 | #endif |
| 7051 | #ifdef _SC_NL_LANGMAX |
| 7052 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 7053 | #endif |
| 7054 | #ifdef _SC_NL_MSGMAX |
| 7055 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 7056 | #endif |
| 7057 | #ifdef _SC_NL_NMAX |
| 7058 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 7059 | #endif |
| 7060 | #ifdef _SC_NL_SETMAX |
| 7061 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 7062 | #endif |
| 7063 | #ifdef _SC_NL_TEXTMAX |
| 7064 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 7065 | #endif |
| 7066 | #ifdef _SC_NPROCESSORS_CONF |
| 7067 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 7068 | #endif |
| 7069 | #ifdef _SC_NPROCESSORS_ONLN |
| 7070 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 7071 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7072 | #ifdef _SC_NPROC_CONF |
| 7073 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 7074 | #endif |
| 7075 | #ifdef _SC_NPROC_ONLN |
| 7076 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 7077 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7078 | #ifdef _SC_NZERO |
| 7079 | {"SC_NZERO", _SC_NZERO}, |
| 7080 | #endif |
| 7081 | #ifdef _SC_OPEN_MAX |
| 7082 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 7083 | #endif |
| 7084 | #ifdef _SC_PAGESIZE |
| 7085 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 7086 | #endif |
| 7087 | #ifdef _SC_PAGE_SIZE |
| 7088 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 7089 | #endif |
| 7090 | #ifdef _SC_PASS_MAX |
| 7091 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 7092 | #endif |
| 7093 | #ifdef _SC_PHYS_PAGES |
| 7094 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 7095 | #endif |
| 7096 | #ifdef _SC_PII |
| 7097 | {"SC_PII", _SC_PII}, |
| 7098 | #endif |
| 7099 | #ifdef _SC_PII_INTERNET |
| 7100 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 7101 | #endif |
| 7102 | #ifdef _SC_PII_INTERNET_DGRAM |
| 7103 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 7104 | #endif |
| 7105 | #ifdef _SC_PII_INTERNET_STREAM |
| 7106 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 7107 | #endif |
| 7108 | #ifdef _SC_PII_OSI |
| 7109 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 7110 | #endif |
| 7111 | #ifdef _SC_PII_OSI_CLTS |
| 7112 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 7113 | #endif |
| 7114 | #ifdef _SC_PII_OSI_COTS |
| 7115 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 7116 | #endif |
| 7117 | #ifdef _SC_PII_OSI_M |
| 7118 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 7119 | #endif |
| 7120 | #ifdef _SC_PII_SOCKET |
| 7121 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 7122 | #endif |
| 7123 | #ifdef _SC_PII_XTI |
| 7124 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 7125 | #endif |
| 7126 | #ifdef _SC_POLL |
| 7127 | {"SC_POLL", _SC_POLL}, |
| 7128 | #endif |
| 7129 | #ifdef _SC_PRIORITIZED_IO |
| 7130 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 7131 | #endif |
| 7132 | #ifdef _SC_PRIORITY_SCHEDULING |
| 7133 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 7134 | #endif |
| 7135 | #ifdef _SC_REALTIME_SIGNALS |
| 7136 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 7137 | #endif |
| 7138 | #ifdef _SC_RE_DUP_MAX |
| 7139 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 7140 | #endif |
| 7141 | #ifdef _SC_RTSIG_MAX |
| 7142 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 7143 | #endif |
| 7144 | #ifdef _SC_SAVED_IDS |
| 7145 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 7146 | #endif |
| 7147 | #ifdef _SC_SCHAR_MAX |
| 7148 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 7149 | #endif |
| 7150 | #ifdef _SC_SCHAR_MIN |
| 7151 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 7152 | #endif |
| 7153 | #ifdef _SC_SELECT |
| 7154 | {"SC_SELECT", _SC_SELECT}, |
| 7155 | #endif |
| 7156 | #ifdef _SC_SEMAPHORES |
| 7157 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 7158 | #endif |
| 7159 | #ifdef _SC_SEM_NSEMS_MAX |
| 7160 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 7161 | #endif |
| 7162 | #ifdef _SC_SEM_VALUE_MAX |
| 7163 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 7164 | #endif |
| 7165 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 7166 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 7167 | #endif |
| 7168 | #ifdef _SC_SHRT_MAX |
| 7169 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 7170 | #endif |
| 7171 | #ifdef _SC_SHRT_MIN |
| 7172 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 7173 | #endif |
| 7174 | #ifdef _SC_SIGQUEUE_MAX |
| 7175 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 7176 | #endif |
| 7177 | #ifdef _SC_SIGRT_MAX |
| 7178 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 7179 | #endif |
| 7180 | #ifdef _SC_SIGRT_MIN |
| 7181 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 7182 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7183 | #ifdef _SC_SOFTPOWER |
| 7184 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 7185 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7186 | #ifdef _SC_SPLIT_CACHE |
| 7187 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 7188 | #endif |
| 7189 | #ifdef _SC_SSIZE_MAX |
| 7190 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 7191 | #endif |
| 7192 | #ifdef _SC_STACK_PROT |
| 7193 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 7194 | #endif |
| 7195 | #ifdef _SC_STREAM_MAX |
| 7196 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 7197 | #endif |
| 7198 | #ifdef _SC_SYNCHRONIZED_IO |
| 7199 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 7200 | #endif |
| 7201 | #ifdef _SC_THREADS |
| 7202 | {"SC_THREADS", _SC_THREADS}, |
| 7203 | #endif |
| 7204 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 7205 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 7206 | #endif |
| 7207 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 7208 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 7209 | #endif |
| 7210 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 7211 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 7212 | #endif |
| 7213 | #ifdef _SC_THREAD_KEYS_MAX |
| 7214 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 7215 | #endif |
| 7216 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 7217 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 7218 | #endif |
| 7219 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 7220 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 7221 | #endif |
| 7222 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 7223 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 7224 | #endif |
| 7225 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 7226 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 7227 | #endif |
| 7228 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 7229 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 7230 | #endif |
| 7231 | #ifdef _SC_THREAD_STACK_MIN |
| 7232 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 7233 | #endif |
| 7234 | #ifdef _SC_THREAD_THREADS_MAX |
| 7235 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 7236 | #endif |
| 7237 | #ifdef _SC_TIMERS |
| 7238 | {"SC_TIMERS", _SC_TIMERS}, |
| 7239 | #endif |
| 7240 | #ifdef _SC_TIMER_MAX |
| 7241 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 7242 | #endif |
| 7243 | #ifdef _SC_TTY_NAME_MAX |
| 7244 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 7245 | #endif |
| 7246 | #ifdef _SC_TZNAME_MAX |
| 7247 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 7248 | #endif |
| 7249 | #ifdef _SC_T_IOV_MAX |
| 7250 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 7251 | #endif |
| 7252 | #ifdef _SC_UCHAR_MAX |
| 7253 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 7254 | #endif |
| 7255 | #ifdef _SC_UINT_MAX |
| 7256 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 7257 | #endif |
| 7258 | #ifdef _SC_UIO_MAXIOV |
| 7259 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 7260 | #endif |
| 7261 | #ifdef _SC_ULONG_MAX |
| 7262 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 7263 | #endif |
| 7264 | #ifdef _SC_USHRT_MAX |
| 7265 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 7266 | #endif |
| 7267 | #ifdef _SC_VERSION |
| 7268 | {"SC_VERSION", _SC_VERSION}, |
| 7269 | #endif |
| 7270 | #ifdef _SC_WORD_BIT |
| 7271 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 7272 | #endif |
| 7273 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 7274 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 7275 | #endif |
| 7276 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 7277 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 7278 | #endif |
| 7279 | #ifdef _SC_XBS5_LP64_OFF64 |
| 7280 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 7281 | #endif |
| 7282 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 7283 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 7284 | #endif |
| 7285 | #ifdef _SC_XOPEN_CRYPT |
| 7286 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 7287 | #endif |
| 7288 | #ifdef _SC_XOPEN_ENH_I18N |
| 7289 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 7290 | #endif |
| 7291 | #ifdef _SC_XOPEN_LEGACY |
| 7292 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 7293 | #endif |
| 7294 | #ifdef _SC_XOPEN_REALTIME |
| 7295 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 7296 | #endif |
| 7297 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 7298 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 7299 | #endif |
| 7300 | #ifdef _SC_XOPEN_SHM |
| 7301 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 7302 | #endif |
| 7303 | #ifdef _SC_XOPEN_UNIX |
| 7304 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 7305 | #endif |
| 7306 | #ifdef _SC_XOPEN_VERSION |
| 7307 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 7308 | #endif |
| 7309 | #ifdef _SC_XOPEN_XCU_VERSION |
| 7310 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 7311 | #endif |
| 7312 | #ifdef _SC_XOPEN_XPG2 |
| 7313 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 7314 | #endif |
| 7315 | #ifdef _SC_XOPEN_XPG3 |
| 7316 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 7317 | #endif |
| 7318 | #ifdef _SC_XOPEN_XPG4 |
| 7319 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 7320 | #endif |
| 7321 | }; |
| 7322 | |
| 7323 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7324 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7325 | { |
| 7326 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 7327 | sizeof(posix_constants_sysconf) |
| 7328 | / sizeof(struct constdef)); |
| 7329 | } |
| 7330 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7331 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7332 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7333 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7334 | |
| 7335 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7336 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7337 | { |
| 7338 | PyObject *result = NULL; |
| 7339 | int name; |
| 7340 | |
| 7341 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 7342 | int value; |
| 7343 | |
| 7344 | errno = 0; |
| 7345 | value = sysconf(name); |
| 7346 | if (value == -1 && errno != 0) |
| 7347 | posix_error(); |
| 7348 | else |
| 7349 | result = PyInt_FromLong(value); |
| 7350 | } |
| 7351 | return result; |
| 7352 | } |
| 7353 | #endif |
| 7354 | |
| 7355 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7356 | /* This code is used to ensure that the tables of configuration value names |
| 7357 | * are in sorted order as required by conv_confname(), and also to build the |
| 7358 | * the exported dictionaries that are used to publish information about the |
| 7359 | * names available on the host platform. |
| 7360 | * |
| 7361 | * Sorting the table at runtime ensures that the table is properly ordered |
| 7362 | * when used, even for platforms we're not able to test on. It also makes |
| 7363 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7364 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7365 | |
| 7366 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7367 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7368 | { |
| 7369 | const struct constdef *c1 = |
| 7370 | (const struct constdef *) v1; |
| 7371 | const struct constdef *c2 = |
| 7372 | (const struct constdef *) v2; |
| 7373 | |
| 7374 | return strcmp(c1->name, c2->name); |
| 7375 | } |
| 7376 | |
| 7377 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7378 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7379 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7380 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7381 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7382 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7383 | |
| 7384 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 7385 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7386 | if (d == NULL) |
| 7387 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7388 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7389 | for (i=0; i < tablesize; ++i) { |
| 7390 | PyObject *o = PyInt_FromLong(table[i].value); |
| 7391 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 7392 | Py_XDECREF(o); |
| 7393 | Py_DECREF(d); |
| 7394 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7395 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7396 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7397 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7398 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7399 | } |
| 7400 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7401 | /* Return -1 on failure, 0 on success. */ |
| 7402 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7403 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7404 | { |
| 7405 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7406 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7407 | sizeof(posix_constants_pathconf) |
| 7408 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7409 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7410 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7411 | #endif |
| 7412 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7413 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7414 | sizeof(posix_constants_confstr) |
| 7415 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7416 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7417 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7418 | #endif |
| 7419 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7420 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7421 | sizeof(posix_constants_sysconf) |
| 7422 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7423 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7424 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7425 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7426 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7427 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7428 | |
| 7429 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7430 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7431 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7432 | 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] | 7433 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7434 | |
| 7435 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7436 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7437 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7438 | abort(); |
| 7439 | /*NOTREACHED*/ |
| 7440 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 7441 | return NULL; |
| 7442 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7443 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7444 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7445 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7446 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 7447 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7448 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7449 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 7450 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 7451 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 7452 | application (if any) its extension is associated.\n\ |
| 7453 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 7454 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7455 | \n\ |
| 7456 | startfile returns as soon as the associated application is launched.\n\ |
| 7457 | There is no option to wait for the application to close, and no way\n\ |
| 7458 | to retrieve the application's exit status.\n\ |
| 7459 | \n\ |
| 7460 | The filepath is relative to the current directory. If you want to use\n\ |
| 7461 | 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] | 7462 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7463 | |
| 7464 | static PyObject * |
| 7465 | win32_startfile(PyObject *self, PyObject *args) |
| 7466 | { |
| 7467 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7468 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7469 | HINSTANCE rc; |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7470 | #ifdef Py_WIN_WIDE_FILENAMES |
| 7471 | if (unicode_file_names()) { |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame^] | 7472 | PyObject *unipath, *woperation = NULL; |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7473 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 7474 | &unipath, &operation)) { |
| 7475 | PyErr_Clear(); |
| 7476 | goto normal; |
| 7477 | } |
| 7478 | |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame^] | 7479 | |
| 7480 | if (operation) { |
| 7481 | woperation = PyUnicode_DecodeASCII(operation, |
| 7482 | strlen(operation), NULL); |
| 7483 | if (!woperation) { |
| 7484 | PyErr_Clear(); |
| 7485 | operation = NULL; |
| 7486 | goto normal; |
| 7487 | } |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7488 | } |
| 7489 | |
| 7490 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame^] | 7491 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7492 | PyUnicode_AS_UNICODE(unipath), |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7493 | NULL, NULL, SW_SHOWNORMAL); |
| 7494 | Py_END_ALLOW_THREADS |
| 7495 | |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame^] | 7496 | Py_XDECREF(woperation); |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7497 | if (rc <= (HINSTANCE)32) { |
| 7498 | PyObject *errval = win32_error_unicode("startfile", |
| 7499 | PyUnicode_AS_UNICODE(unipath)); |
| 7500 | return errval; |
| 7501 | } |
| 7502 | Py_INCREF(Py_None); |
| 7503 | return Py_None; |
| 7504 | } |
| 7505 | #endif |
| 7506 | |
| 7507 | normal: |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7508 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 7509 | Py_FileSystemDefaultEncoding, &filepath, |
| 7510 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7511 | return NULL; |
| 7512 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7513 | rc = ShellExecute((HWND)0, operation, filepath, |
| 7514 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7515 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 7516 | if (rc <= (HINSTANCE)32) { |
| 7517 | PyObject *errval = win32_error("startfile", filepath); |
| 7518 | PyMem_Free(filepath); |
| 7519 | return errval; |
| 7520 | } |
| 7521 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7522 | Py_INCREF(Py_None); |
| 7523 | return Py_None; |
| 7524 | } |
| 7525 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7526 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7527 | #ifdef HAVE_GETLOADAVG |
| 7528 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 7529 | "getloadavg() -> (float, float, float)\n\n\ |
| 7530 | Return the number of processes in the system run queue averaged over\n\ |
| 7531 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 7532 | was unobtainable"); |
| 7533 | |
| 7534 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7535 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7536 | { |
| 7537 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7538 | if (getloadavg(loadavg, 3)!=3) { |
| 7539 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 7540 | return NULL; |
| 7541 | } else |
| 7542 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 7543 | } |
| 7544 | #endif |
| 7545 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7546 | #ifdef MS_WINDOWS |
| 7547 | |
| 7548 | PyDoc_STRVAR(win32_urandom__doc__, |
| 7549 | "urandom(n) -> str\n\n\ |
| 7550 | Return a string of n random bytes suitable for cryptographic use."); |
| 7551 | |
| 7552 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 7553 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 7554 | DWORD dwFlags ); |
| 7555 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 7556 | BYTE *pbBuffer ); |
| 7557 | |
| 7558 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
| 7559 | static HCRYPTPROV hCryptProv = 0; |
| 7560 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7561 | static PyObject* |
| 7562 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7563 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7564 | int howMany; |
| 7565 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7566 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7567 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 7568 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7569 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 7570 | if (howMany < 0) |
| 7571 | return PyErr_Format(PyExc_ValueError, |
| 7572 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7573 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7574 | if (hCryptProv == 0) { |
| 7575 | HINSTANCE hAdvAPI32 = NULL; |
| 7576 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7577 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7578 | /* Obtain handle to the DLL containing CryptoAPI |
| 7579 | This should not fail */ |
| 7580 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 7581 | if(hAdvAPI32 == NULL) |
| 7582 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7583 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7584 | /* Obtain pointers to the CryptoAPI functions |
| 7585 | This will fail on some early versions of Win95 */ |
| 7586 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 7587 | hAdvAPI32, |
| 7588 | "CryptAcquireContextA"); |
| 7589 | if (pCryptAcquireContext == NULL) |
| 7590 | return PyErr_Format(PyExc_NotImplementedError, |
| 7591 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7592 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7593 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 7594 | hAdvAPI32, "CryptGenRandom"); |
| 7595 | if (pCryptAcquireContext == NULL) |
| 7596 | return PyErr_Format(PyExc_NotImplementedError, |
| 7597 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7598 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7599 | /* Acquire context */ |
| 7600 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 7601 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 7602 | return win32_error("CryptAcquireContext", NULL); |
| 7603 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7604 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7605 | /* Allocate bytes */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7606 | result = PyString_FromStringAndSize(NULL, howMany); |
| 7607 | if (result != NULL) { |
| 7608 | /* Get random data */ |
| 7609 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 7610 | PyString_AS_STRING(result))) { |
| 7611 | Py_DECREF(result); |
| 7612 | return win32_error("CryptGenRandom", NULL); |
| 7613 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7614 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7615 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7616 | } |
| 7617 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7618 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7619 | static PyMethodDef posix_methods[] = { |
| 7620 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 7621 | #ifdef HAVE_TTYNAME |
| 7622 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 7623 | #endif |
| 7624 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 7625 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7626 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7627 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7628 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7629 | #ifdef HAVE_LCHOWN |
| 7630 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 7631 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7632 | #ifdef HAVE_CHROOT |
| 7633 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 7634 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7635 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7636 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7637 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7638 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7639 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7640 | #ifdef Py_USING_UNICODE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7641 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7642 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7643 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7644 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7645 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7646 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7647 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7648 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7649 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7650 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7651 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7652 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7653 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7654 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7655 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7656 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7657 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7658 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7659 | {"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] | 7660 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7661 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7662 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7663 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7664 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7665 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7666 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7667 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7668 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7669 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7670 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7671 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7672 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7673 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7674 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7675 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7676 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7677 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7678 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7679 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7680 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7681 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7682 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7683 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7684 | #if defined(PYOS_OS2) |
| 7685 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 7686 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 7687 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7688 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7689 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7690 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7691 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7692 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7693 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7694 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7695 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7696 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7697 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7698 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7699 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7700 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7701 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7702 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7703 | #endif /* HAVE_GETEGID */ |
| 7704 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7705 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7706 | #endif /* HAVE_GETEUID */ |
| 7707 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7708 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7709 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7710 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7711 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7712 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7713 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7714 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7715 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7716 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7717 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7718 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7719 | #endif /* HAVE_GETPPID */ |
| 7720 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7721 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7722 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7723 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7724 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7725 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7726 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7727 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7728 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7729 | #ifdef HAVE_KILLPG |
| 7730 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7731 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7732 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7733 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7734 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7735 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7736 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7737 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7738 | {"popen2", win32_popen2, METH_VARARGS}, |
| 7739 | {"popen3", win32_popen3, METH_VARARGS}, |
| 7740 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7741 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7742 | #else |
| 7743 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7744 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 7745 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 7746 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 7747 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7748 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7749 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7750 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7751 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7752 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7753 | #ifdef HAVE_SETEUID |
| 7754 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7755 | #endif /* HAVE_SETEUID */ |
| 7756 | #ifdef HAVE_SETEGID |
| 7757 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7758 | #endif /* HAVE_SETEGID */ |
| 7759 | #ifdef HAVE_SETREUID |
| 7760 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7761 | #endif /* HAVE_SETREUID */ |
| 7762 | #ifdef HAVE_SETREGID |
| 7763 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7764 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7765 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7766 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7767 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7768 | #ifdef HAVE_SETGROUPS |
| 7769 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 7770 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7771 | #ifdef HAVE_GETPGID |
| 7772 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7773 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7774 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7775 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7776 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7777 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7778 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7779 | #endif /* HAVE_WAIT */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 7780 | #ifdef HAVE_WAIT3 |
| 7781 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 7782 | #endif /* HAVE_WAIT3 */ |
| 7783 | #ifdef HAVE_WAIT4 |
| 7784 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 7785 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7786 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7787 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7788 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7789 | #ifdef HAVE_GETSID |
| 7790 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 7791 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7792 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7793 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7794 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7795 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7796 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7797 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7798 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7799 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7800 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7801 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7802 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7803 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7804 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7805 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 7806 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7807 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7808 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7809 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7810 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7811 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 7812 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7813 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7814 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7815 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7816 | #endif |
| 7817 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7818 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7819 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7820 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7821 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7822 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7823 | #ifdef HAVE_DEVICE_MACROS |
| 7824 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7825 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7826 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7827 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7828 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7829 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7830 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7831 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7832 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7833 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7834 | #ifdef HAVE_UNSETENV |
| 7835 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7836 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7837 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7838 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7839 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7840 | #ifdef HAVE_FCHDIR |
| 7841 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7842 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7843 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7844 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7845 | #endif |
| 7846 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7847 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7848 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7849 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7850 | #ifdef WCOREDUMP |
| 7851 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7852 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7853 | #ifdef WIFCONTINUED |
| 7854 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7855 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7856 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7857 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7858 | #endif /* WIFSTOPPED */ |
| 7859 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7860 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7861 | #endif /* WIFSIGNALED */ |
| 7862 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7863 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7864 | #endif /* WIFEXITED */ |
| 7865 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7866 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7867 | #endif /* WEXITSTATUS */ |
| 7868 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7869 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7870 | #endif /* WTERMSIG */ |
| 7871 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7872 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7873 | #endif /* WSTOPSIG */ |
| 7874 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7875 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7876 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7877 | #endif |
| 7878 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7879 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7880 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 7881 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7882 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7883 | #endif |
| 7884 | #ifdef HAVE_TEMPNAM |
| 7885 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 7886 | #endif |
| 7887 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7888 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7889 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7890 | #ifdef HAVE_CONFSTR |
| 7891 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7892 | #endif |
| 7893 | #ifdef HAVE_SYSCONF |
| 7894 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7895 | #endif |
| 7896 | #ifdef HAVE_FPATHCONF |
| 7897 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7898 | #endif |
| 7899 | #ifdef HAVE_PATHCONF |
| 7900 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7901 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7902 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7903 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7904 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7905 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7906 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7907 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7908 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7909 | #ifdef MS_WINDOWS |
| 7910 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 7911 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7912 | {NULL, NULL} /* Sentinel */ |
| 7913 | }; |
| 7914 | |
| 7915 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7916 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7917 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7918 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7919 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7920 | } |
| 7921 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7922 | #if defined(PYOS_OS2) |
| 7923 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7924 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7925 | { |
| 7926 | APIRET rc; |
| 7927 | ULONG values[QSV_MAX+1]; |
| 7928 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7929 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7930 | |
| 7931 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7932 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7933 | Py_END_ALLOW_THREADS |
| 7934 | |
| 7935 | if (rc != NO_ERROR) { |
| 7936 | os2_error(rc); |
| 7937 | return -1; |
| 7938 | } |
| 7939 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7940 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7941 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7942 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7943 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7944 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7945 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7946 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7947 | |
| 7948 | switch (values[QSV_VERSION_MINOR]) { |
| 7949 | case 0: ver = "2.00"; break; |
| 7950 | case 10: ver = "2.10"; break; |
| 7951 | case 11: ver = "2.11"; break; |
| 7952 | case 30: ver = "3.00"; break; |
| 7953 | case 40: ver = "4.00"; break; |
| 7954 | case 50: ver = "5.00"; break; |
| 7955 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7956 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7957 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7958 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7959 | ver = &tmp[0]; |
| 7960 | } |
| 7961 | |
| 7962 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7963 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7964 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7965 | |
| 7966 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7967 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7968 | tmp[1] = ':'; |
| 7969 | tmp[2] = '\0'; |
| 7970 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7971 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7972 | } |
| 7973 | #endif |
| 7974 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7975 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7976 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7977 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7978 | #ifdef F_OK |
| 7979 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7980 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7981 | #ifdef R_OK |
| 7982 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7983 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7984 | #ifdef W_OK |
| 7985 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7986 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7987 | #ifdef X_OK |
| 7988 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7989 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7990 | #ifdef NGROUPS_MAX |
| 7991 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7992 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7993 | #ifdef TMP_MAX |
| 7994 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7995 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7996 | #ifdef WCONTINUED |
| 7997 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7998 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7999 | #ifdef WNOHANG |
| 8000 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8001 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8002 | #ifdef WUNTRACED |
| 8003 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 8004 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8005 | #ifdef O_RDONLY |
| 8006 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 8007 | #endif |
| 8008 | #ifdef O_WRONLY |
| 8009 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 8010 | #endif |
| 8011 | #ifdef O_RDWR |
| 8012 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 8013 | #endif |
| 8014 | #ifdef O_NDELAY |
| 8015 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 8016 | #endif |
| 8017 | #ifdef O_NONBLOCK |
| 8018 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 8019 | #endif |
| 8020 | #ifdef O_APPEND |
| 8021 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 8022 | #endif |
| 8023 | #ifdef O_DSYNC |
| 8024 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 8025 | #endif |
| 8026 | #ifdef O_RSYNC |
| 8027 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 8028 | #endif |
| 8029 | #ifdef O_SYNC |
| 8030 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 8031 | #endif |
| 8032 | #ifdef O_NOCTTY |
| 8033 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 8034 | #endif |
| 8035 | #ifdef O_CREAT |
| 8036 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 8037 | #endif |
| 8038 | #ifdef O_EXCL |
| 8039 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 8040 | #endif |
| 8041 | #ifdef O_TRUNC |
| 8042 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 8043 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 8044 | #ifdef O_BINARY |
| 8045 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 8046 | #endif |
| 8047 | #ifdef O_TEXT |
| 8048 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 8049 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8050 | #ifdef O_LARGEFILE |
| 8051 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 8052 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 8053 | #ifdef O_SHLOCK |
| 8054 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 8055 | #endif |
| 8056 | #ifdef O_EXLOCK |
| 8057 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 8058 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8059 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8060 | /* MS Windows */ |
| 8061 | #ifdef O_NOINHERIT |
| 8062 | /* Don't inherit in child processes. */ |
| 8063 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 8064 | #endif |
| 8065 | #ifdef _O_SHORT_LIVED |
| 8066 | /* Optimize for short life (keep in memory). */ |
| 8067 | /* MS forgot to define this one with a non-underscore form too. */ |
| 8068 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 8069 | #endif |
| 8070 | #ifdef O_TEMPORARY |
| 8071 | /* Automatically delete when last handle is closed. */ |
| 8072 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 8073 | #endif |
| 8074 | #ifdef O_RANDOM |
| 8075 | /* Optimize for random access. */ |
| 8076 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 8077 | #endif |
| 8078 | #ifdef O_SEQUENTIAL |
| 8079 | /* Optimize for sequential access. */ |
| 8080 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 8081 | #endif |
| 8082 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8083 | /* GNU extensions. */ |
| 8084 | #ifdef O_DIRECT |
| 8085 | /* Direct disk access. */ |
| 8086 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 8087 | #endif |
| 8088 | #ifdef O_DIRECTORY |
| 8089 | /* Must be a directory. */ |
| 8090 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 8091 | #endif |
| 8092 | #ifdef O_NOFOLLOW |
| 8093 | /* Do not follow links. */ |
| 8094 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 8095 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8096 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8097 | /* These come from sysexits.h */ |
| 8098 | #ifdef EX_OK |
| 8099 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8100 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8101 | #ifdef EX_USAGE |
| 8102 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8103 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8104 | #ifdef EX_DATAERR |
| 8105 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8106 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8107 | #ifdef EX_NOINPUT |
| 8108 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8109 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8110 | #ifdef EX_NOUSER |
| 8111 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8112 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8113 | #ifdef EX_NOHOST |
| 8114 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8115 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8116 | #ifdef EX_UNAVAILABLE |
| 8117 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8118 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8119 | #ifdef EX_SOFTWARE |
| 8120 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8121 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8122 | #ifdef EX_OSERR |
| 8123 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8124 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8125 | #ifdef EX_OSFILE |
| 8126 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8127 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8128 | #ifdef EX_CANTCREAT |
| 8129 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8130 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8131 | #ifdef EX_IOERR |
| 8132 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8133 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8134 | #ifdef EX_TEMPFAIL |
| 8135 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8136 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8137 | #ifdef EX_PROTOCOL |
| 8138 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8139 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8140 | #ifdef EX_NOPERM |
| 8141 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8142 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8143 | #ifdef EX_CONFIG |
| 8144 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8145 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8146 | #ifdef EX_NOTFOUND |
| 8147 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8148 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8149 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8150 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8151 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 8152 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 8153 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 8154 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 8155 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 8156 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 8157 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 8158 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 8159 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 8160 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 8161 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 8162 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 8163 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 8164 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 8165 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 8166 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 8167 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 8168 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 8169 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 8170 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 8171 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 8172 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 8173 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 8174 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 8175 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 8176 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 8177 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8178 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8179 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8180 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8181 | #if defined(PYOS_OS2) |
| 8182 | if (insertvalues(d)) return -1; |
| 8183 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8184 | return 0; |
| 8185 | } |
| 8186 | |
| 8187 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8188 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8189 | #define INITFUNC initnt |
| 8190 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8191 | |
| 8192 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8193 | #define INITFUNC initos2 |
| 8194 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8195 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8196 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8197 | #define INITFUNC initposix |
| 8198 | #define MODNAME "posix" |
| 8199 | #endif |
| 8200 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 8201 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 8202 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8203 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8204 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8205 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8206 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8207 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8208 | posix__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 8209 | if (m == NULL) |
| 8210 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8211 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8212 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8213 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8214 | Py_XINCREF(v); |
| 8215 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8216 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8217 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8218 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8219 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8220 | return; |
| 8221 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8222 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8223 | return; |
| 8224 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8225 | Py_INCREF(PyExc_OSError); |
| 8226 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8227 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8228 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 8229 | if (posix_putenv_garbage == NULL) |
| 8230 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8231 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8232 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 8233 | stat_result_desc.name = MODNAME ".stat_result"; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 8234 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 8235 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 8236 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8237 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 8238 | structseq_new = StatResultType.tp_new; |
| 8239 | StatResultType.tp_new = statresult_new; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8240 | Py_INCREF((PyObject*) &StatResultType); |
| 8241 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8242 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 8243 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8244 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8245 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 8246 | PyModule_AddObject(m, "statvfs_result", |
| 8247 | (PyObject*) &StatVFSResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8248 | } |