Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 14 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 15 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 16 | #include "Python.h" |
| 17 | #include "structseq.h" |
| 18 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 19 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 20 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 21 | #endif /* defined(__VMS) */ |
| 22 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 23 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 24 | "This module provides access to operating system functionality that is\n\ |
| 25 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 26 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 27 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 29 | #ifndef Py_USING_UNICODE |
| 30 | /* This is used in signatures of functions. */ |
| 31 | #define Py_UNICODE void |
| 32 | #endif |
| 33 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 34 | #if defined(PYOS_OS2) |
| 35 | #define INCL_DOS |
| 36 | #define INCL_DOSERRORS |
| 37 | #define INCL_DOSPROCESS |
| 38 | #define INCL_NOPMAPI |
| 39 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 40 | #if defined(PYCC_GCC) |
| 41 | #include <ctype.h> |
| 42 | #include <io.h> |
| 43 | #include <stdio.h> |
| 44 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 45 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 46 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 49 | #include <sys/types.h> |
| 50 | #include <sys/stat.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 52 | #ifdef HAVE_SYS_WAIT_H |
| 53 | #include <sys/wait.h> /* For WNOHANG */ |
| 54 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 56 | #include <signal.h> |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 58 | #ifdef HAVE_FCNTL_H |
| 59 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 60 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 62 | #ifdef HAVE_GRP_H |
| 63 | #include <grp.h> |
| 64 | #endif |
| 65 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 66 | #ifdef HAVE_SYSEXITS_H |
| 67 | #include <sysexits.h> |
| 68 | #endif /* HAVE_SYSEXITS_H */ |
| 69 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 70 | #ifdef HAVE_SYS_LOADAVG_H |
| 71 | #include <sys/loadavg.h> |
| 72 | #endif |
| 73 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 74 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 75 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 76 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 77 | #include <process.h> |
| 78 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 79 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 80 | #define HAVE_GETCWD 1 |
| 81 | #define HAVE_OPENDIR 1 |
| 82 | #define HAVE_SYSTEM 1 |
| 83 | #if defined(__OS2__) |
| 84 | #define HAVE_EXECV 1 |
| 85 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 86 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 87 | #include <process.h> |
| 88 | #else |
| 89 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 90 | #define HAVE_EXECV 1 |
| 91 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 92 | #define HAVE_OPENDIR 1 |
| 93 | #define HAVE_PIPE 1 |
| 94 | #define HAVE_POPEN 1 |
| 95 | #define HAVE_SYSTEM 1 |
| 96 | #define HAVE_WAIT 1 |
| 97 | #else |
| 98 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 99 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 100 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 101 | #define HAVE_EXECV 1 |
| 102 | #define HAVE_PIPE 1 |
| 103 | #define HAVE_POPEN 1 |
| 104 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 105 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 106 | #define HAVE_FSYNC 1 |
| 107 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 108 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 109 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 110 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 111 | #else /* all other compilers */ |
| 112 | /* Unix functions that the configure script doesn't check for */ |
| 113 | #define HAVE_EXECV 1 |
| 114 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 115 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 116 | #define HAVE_FORK1 1 |
| 117 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 118 | #define HAVE_GETCWD 1 |
| 119 | #define HAVE_GETEGID 1 |
| 120 | #define HAVE_GETEUID 1 |
| 121 | #define HAVE_GETGID 1 |
| 122 | #define HAVE_GETPPID 1 |
| 123 | #define HAVE_GETUID 1 |
| 124 | #define HAVE_KILL 1 |
| 125 | #define HAVE_OPENDIR 1 |
| 126 | #define HAVE_PIPE 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 127 | #ifndef __rtems__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 128 | #define HAVE_POPEN 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 129 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 130 | #define HAVE_SYSTEM 1 |
| 131 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 132 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 133 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #endif /* _MSC_VER */ |
| 135 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 136 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 137 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 139 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 140 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 141 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 142 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 143 | (default) */ |
| 144 | extern char *ctermid_r(char *); |
| 145 | #endif |
| 146 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 147 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 148 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 149 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 150 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 151 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 152 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 153 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 154 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 155 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 156 | #endif |
| 157 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 158 | extern int chdir(char *); |
| 159 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 160 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 161 | extern int chdir(const char *); |
| 162 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 163 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 164 | #ifdef __BORLANDC__ |
| 165 | extern int chmod(const char *, int); |
| 166 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 167 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 168 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int chown(const char *, uid_t, gid_t); |
| 170 | extern char *getcwd(char *, int); |
| 171 | extern char *strerror(int); |
| 172 | extern int link(const char *, const char *); |
| 173 | extern int rename(const char *, const char *); |
| 174 | extern int stat(const char *, struct stat *); |
| 175 | extern int unlink(const char *); |
| 176 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 177 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 178 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 179 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 180 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 181 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 182 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 183 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 185 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 187 | #ifdef HAVE_UTIME_H |
| 188 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 189 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 191 | #ifdef HAVE_SYS_UTIME_H |
| 192 | #include <sys/utime.h> |
| 193 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 194 | #endif /* HAVE_SYS_UTIME_H */ |
| 195 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 196 | #ifdef HAVE_SYS_TIMES_H |
| 197 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 198 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 199 | |
| 200 | #ifdef HAVE_SYS_PARAM_H |
| 201 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 202 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 203 | |
| 204 | #ifdef HAVE_SYS_UTSNAME_H |
| 205 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 206 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 207 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 208 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 210 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 211 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 212 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 213 | #include <direct.h> |
| 214 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 215 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 216 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 217 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 218 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 219 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 220 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 221 | #endif |
| 222 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 223 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 224 | #endif |
| 225 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 227 | #endif |
| 228 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 230 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 231 | #include <direct.h> |
| 232 | #include <io.h> |
| 233 | #include <process.h> |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 234 | #include "osdefs.h" |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 235 | #define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 236 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 237 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 238 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 239 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 240 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 242 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 244 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 245 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 246 | #ifndef MAXPATHLEN |
| 247 | #define MAXPATHLEN 1024 |
| 248 | #endif /* MAXPATHLEN */ |
| 249 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 250 | #ifdef UNION_WAIT |
| 251 | /* Emulate some macros on systems that have a union instead of macros */ |
| 252 | |
| 253 | #ifndef WIFEXITED |
| 254 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 255 | #endif |
| 256 | |
| 257 | #ifndef WEXITSTATUS |
| 258 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 259 | #endif |
| 260 | |
| 261 | #ifndef WTERMSIG |
| 262 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 263 | #endif |
| 264 | |
| 265 | #endif /* UNION_WAIT */ |
| 266 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 267 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 268 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 269 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 270 | #define USE_CTERMID_R |
| 271 | #endif |
| 272 | |
| 273 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 274 | #define USE_TMPNAM_R |
| 275 | #endif |
| 276 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 277 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 278 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 279 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 280 | # define STAT _stati64 |
| 281 | # define FSTAT _fstati64 |
| 282 | # define STRUCT_STAT struct _stati64 |
| 283 | #else |
| 284 | # define STAT stat |
| 285 | # define FSTAT fstat |
| 286 | # define STRUCT_STAT struct stat |
| 287 | #endif |
| 288 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 289 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 290 | #include <sys/mkdev.h> |
| 291 | #else |
| 292 | #if defined(MAJOR_IN_SYSMACROS) |
| 293 | #include <sys/sysmacros.h> |
| 294 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 295 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 296 | #include <sys/mkdev.h> |
| 297 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 298 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 299 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 300 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 301 | #ifdef WITH_NEXT_FRAMEWORK |
| 302 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 303 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 304 | */ |
| 305 | #include <crt_externs.h> |
| 306 | static char **environ; |
| 307 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 308 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 309 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 310 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 311 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 312 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 313 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 314 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 316 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 317 | if (d == NULL) |
| 318 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 319 | #ifdef WITH_NEXT_FRAMEWORK |
| 320 | if (environ == NULL) |
| 321 | environ = *_NSGetEnviron(); |
| 322 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 323 | if (environ == NULL) |
| 324 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 325 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 326 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 327 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 328 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 329 | char *p = strchr(*e, '='); |
| 330 | if (p == NULL) |
| 331 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 332 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 333 | if (k == NULL) { |
| 334 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 335 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 336 | } |
| 337 | v = PyString_FromString(p+1); |
| 338 | if (v == NULL) { |
| 339 | PyErr_Clear(); |
| 340 | Py_DECREF(k); |
| 341 | continue; |
| 342 | } |
| 343 | if (PyDict_GetItem(d, k) == NULL) { |
| 344 | if (PyDict_SetItem(d, k, v) != 0) |
| 345 | PyErr_Clear(); |
| 346 | } |
| 347 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 348 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 349 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 350 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 351 | { |
| 352 | APIRET rc; |
| 353 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 354 | |
| 355 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 356 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 357 | PyObject *v = PyString_FromString(buffer); |
| 358 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 359 | Py_DECREF(v); |
| 360 | } |
| 361 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 362 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 363 | PyObject *v = PyString_FromString(buffer); |
| 364 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 365 | Py_DECREF(v); |
| 366 | } |
| 367 | } |
| 368 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 369 | return d; |
| 370 | } |
| 371 | |
| 372 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 373 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 374 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 375 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 376 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 377 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 378 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 379 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 380 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 381 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 382 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 383 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 386 | #ifdef Py_WIN_WIDE_FILENAMES |
| 387 | static PyObject * |
| 388 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 389 | { |
| 390 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 391 | } |
| 392 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 393 | |
| 394 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 395 | static PyObject * |
| 396 | posix_error_with_allocated_filename(char* name) |
| 397 | { |
| 398 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 399 | PyMem_Free(name); |
| 400 | return rc; |
| 401 | } |
| 402 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 403 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 404 | static PyObject * |
| 405 | win32_error(char* function, char* filename) |
| 406 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 407 | /* XXX We should pass the function name along in the future. |
| 408 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 409 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 410 | Windows error object, which is non-trivial. |
| 411 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 412 | errno = GetLastError(); |
| 413 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 414 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 415 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 416 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 417 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 418 | |
| 419 | #ifdef Py_WIN_WIDE_FILENAMES |
| 420 | static PyObject * |
| 421 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 422 | { |
| 423 | /* XXX - see win32_error for comments on 'function' */ |
| 424 | errno = GetLastError(); |
| 425 | if (filename) |
| 426 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 427 | else |
| 428 | return PyErr_SetFromWindowsErr(errno); |
| 429 | } |
| 430 | |
| 431 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 432 | { |
| 433 | /* XXX Perhaps we should make this API an alias of |
| 434 | PyObject_Unicode() instead ?! */ |
| 435 | if (PyUnicode_CheckExact(obj)) { |
| 436 | Py_INCREF(obj); |
| 437 | return obj; |
| 438 | } |
| 439 | if (PyUnicode_Check(obj)) { |
| 440 | /* For a Unicode subtype that's not a Unicode object, |
| 441 | return a true Unicode object with the same data. */ |
| 442 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 443 | PyUnicode_GET_SIZE(obj)); |
| 444 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 445 | return PyUnicode_FromEncodedObject(obj, |
| 446 | Py_FileSystemDefaultEncoding, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 447 | "strict"); |
| 448 | } |
| 449 | |
| 450 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 451 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 452 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 453 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 454 | #if defined(PYOS_OS2) |
| 455 | /********************************************************************** |
| 456 | * Helper Function to Trim and Format OS/2 Messages |
| 457 | **********************************************************************/ |
| 458 | static void |
| 459 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 460 | { |
| 461 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 462 | |
| 463 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 464 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 465 | |
| 466 | while (lastc > msgbuf && isspace(*lastc)) |
| 467 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 468 | } |
| 469 | |
| 470 | /* Add Optional Reason Text */ |
| 471 | if (reason) { |
| 472 | strcat(msgbuf, " : "); |
| 473 | strcat(msgbuf, reason); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /********************************************************************** |
| 478 | * Decode an OS/2 Operating System Error Code |
| 479 | * |
| 480 | * A convenience function to lookup an OS/2 error code and return a |
| 481 | * text message we can use to raise a Python exception. |
| 482 | * |
| 483 | * Notes: |
| 484 | * The messages for errors returned from the OS/2 kernel reside in |
| 485 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 486 | * |
| 487 | **********************************************************************/ |
| 488 | static char * |
| 489 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 490 | { |
| 491 | APIRET rc; |
| 492 | ULONG msglen; |
| 493 | |
| 494 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 495 | Py_BEGIN_ALLOW_THREADS |
| 496 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 497 | errorcode, "oso001.msg", &msglen); |
| 498 | Py_END_ALLOW_THREADS |
| 499 | |
| 500 | if (rc == NO_ERROR) |
| 501 | os2_formatmsg(msgbuf, msglen, reason); |
| 502 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 503 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 504 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 505 | |
| 506 | return msgbuf; |
| 507 | } |
| 508 | |
| 509 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 510 | errors are not in a global variable e.g. 'errno' nor are |
| 511 | they congruent with posix error numbers. */ |
| 512 | |
| 513 | static PyObject * os2_error(int code) |
| 514 | { |
| 515 | char text[1024]; |
| 516 | PyObject *v; |
| 517 | |
| 518 | os2_strerror(text, sizeof(text), code, ""); |
| 519 | |
| 520 | v = Py_BuildValue("(is)", code, text); |
| 521 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 522 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 523 | Py_DECREF(v); |
| 524 | } |
| 525 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 526 | } |
| 527 | |
| 528 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 529 | |
| 530 | /* POSIX generic methods */ |
| 531 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 532 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 533 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 534 | { |
| 535 | int fd; |
| 536 | int res; |
| 537 | fd = PyObject_AsFileDescriptor(fdobj); |
| 538 | if (fd < 0) |
| 539 | return NULL; |
| 540 | Py_BEGIN_ALLOW_THREADS |
| 541 | res = (*func)(fd); |
| 542 | Py_END_ALLOW_THREADS |
| 543 | if (res < 0) |
| 544 | return posix_error(); |
| 545 | Py_INCREF(Py_None); |
| 546 | return Py_None; |
| 547 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 548 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 549 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 550 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 551 | unicode_file_names(void) |
| 552 | { |
| 553 | static int canusewide = -1; |
| 554 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 555 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 556 | the Windows NT family. */ |
| 557 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 558 | } |
| 559 | return canusewide; |
| 560 | } |
| 561 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 562 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 563 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 564 | posix_1str(PyObject *args, char *format, int (*func)(const char*), |
| 565 | char *wformat, int (*wfunc)(const Py_UNICODE*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 566 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 567 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 568 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 569 | #ifdef Py_WIN_WIDE_FILENAMES |
| 570 | if (unicode_file_names()) { |
| 571 | PyUnicodeObject *po; |
| 572 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 573 | Py_BEGIN_ALLOW_THREADS |
| 574 | /* PyUnicode_AS_UNICODE OK without thread |
| 575 | lock as it is a simple dereference. */ |
| 576 | res = (*wfunc)(PyUnicode_AS_UNICODE(po)); |
| 577 | Py_END_ALLOW_THREADS |
| 578 | if (res < 0) |
Mark Hammond | d389036 | 2002-10-03 07:24:48 +0000 | [diff] [blame] | 579 | return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 580 | Py_INCREF(Py_None); |
| 581 | return Py_None; |
| 582 | } |
| 583 | /* Drop the argument parsing error as narrow |
| 584 | strings are also valid. */ |
| 585 | PyErr_Clear(); |
| 586 | } |
| 587 | #else |
| 588 | /* Platforms that don't support Unicode filenames |
| 589 | shouldn't be passing these extra params */ |
| 590 | assert(wformat==NULL && wfunc == NULL); |
| 591 | #endif |
| 592 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 593 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 594 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 595 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 596 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 597 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 598 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 599 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 600 | return posix_error_with_allocated_filename(path1); |
| 601 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 602 | Py_INCREF(Py_None); |
| 603 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 606 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 607 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 608 | char *format, |
| 609 | int (*func)(const char *, const char *), |
| 610 | char *wformat, |
| 611 | int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 612 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 613 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 614 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 615 | #ifdef Py_WIN_WIDE_FILENAMES |
| 616 | if (unicode_file_names()) { |
| 617 | PyObject *po1; |
| 618 | PyObject *po2; |
| 619 | if (PyArg_ParseTuple(args, wformat, &po1, &po2)) { |
| 620 | if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) { |
| 621 | PyObject *wpath1; |
| 622 | PyObject *wpath2; |
| 623 | wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1); |
| 624 | wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2); |
| 625 | if (!wpath1 || !wpath2) { |
| 626 | Py_XDECREF(wpath1); |
| 627 | Py_XDECREF(wpath2); |
| 628 | return NULL; |
| 629 | } |
| 630 | Py_BEGIN_ALLOW_THREADS |
| 631 | /* PyUnicode_AS_UNICODE OK without thread |
| 632 | lock as it is a simple dereference. */ |
| 633 | res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1), |
| 634 | PyUnicode_AS_UNICODE(wpath2)); |
| 635 | Py_END_ALLOW_THREADS |
| 636 | Py_XDECREF(wpath1); |
| 637 | Py_XDECREF(wpath2); |
| 638 | if (res != 0) |
| 639 | return posix_error(); |
| 640 | Py_INCREF(Py_None); |
| 641 | return Py_None; |
| 642 | } |
| 643 | /* Else flow through as neither is Unicode. */ |
| 644 | } |
| 645 | /* Drop the argument parsing error as narrow |
| 646 | strings are also valid. */ |
| 647 | PyErr_Clear(); |
| 648 | } |
| 649 | #else |
| 650 | /* Platforms that don't support Unicode filenames |
| 651 | shouldn't be passing these extra params */ |
| 652 | assert(wformat==NULL && wfunc == NULL); |
| 653 | #endif |
| 654 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 655 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 656 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 657 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 658 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 659 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 660 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 661 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 662 | PyMem_Free(path1); |
| 663 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 664 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 665 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 666 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 667 | Py_INCREF(Py_None); |
| 668 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 671 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 672 | "stat_result: Result from stat or lstat.\n\n\ |
| 673 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 674 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 675 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 676 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 677 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 678 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 679 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 680 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 681 | |
| 682 | static PyStructSequence_Field stat_result_fields[] = { |
| 683 | {"st_mode", "protection bits"}, |
| 684 | {"st_ino", "inode"}, |
| 685 | {"st_dev", "device"}, |
| 686 | {"st_nlink", "number of hard links"}, |
| 687 | {"st_uid", "user ID of owner"}, |
| 688 | {"st_gid", "group ID of owner"}, |
| 689 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 690 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 691 | {NULL, "integer time of last access"}, |
| 692 | {NULL, "integer time of last modification"}, |
| 693 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 694 | {"st_atime", "time of last access"}, |
| 695 | {"st_mtime", "time of last modification"}, |
| 696 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 697 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 698 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 699 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 700 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 701 | {"st_blocks", "number of blocks allocated"}, |
| 702 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 703 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 704 | {"st_rdev", "device type (if inode device)"}, |
| 705 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 706 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 707 | {"st_flags", "user defined flags for file"}, |
| 708 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 709 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 710 | {"st_gen", "generation number"}, |
| 711 | #endif |
| 712 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 713 | {"st_birthtime", "time of creation"}, |
| 714 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 715 | {0} |
| 716 | }; |
| 717 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 718 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 719 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 720 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 721 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 722 | #endif |
| 723 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 724 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 725 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 726 | #else |
| 727 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 728 | #endif |
| 729 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 730 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 731 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 732 | #else |
| 733 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 734 | #endif |
| 735 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 736 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 737 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 738 | #else |
| 739 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 740 | #endif |
| 741 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 742 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 743 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 744 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 745 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 746 | #endif |
| 747 | |
| 748 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 749 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 750 | #else |
| 751 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 752 | #endif |
| 753 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 754 | static PyStructSequence_Desc stat_result_desc = { |
| 755 | "stat_result", /* name */ |
| 756 | stat_result__doc__, /* doc */ |
| 757 | stat_result_fields, |
| 758 | 10 |
| 759 | }; |
| 760 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 761 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 762 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 763 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 764 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 765 | 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] | 766 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 767 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 768 | |
| 769 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 770 | {"f_bsize", }, |
| 771 | {"f_frsize", }, |
| 772 | {"f_blocks", }, |
| 773 | {"f_bfree", }, |
| 774 | {"f_bavail", }, |
| 775 | {"f_files", }, |
| 776 | {"f_ffree", }, |
| 777 | {"f_favail", }, |
| 778 | {"f_flag", }, |
| 779 | {"f_namemax",}, |
| 780 | {0} |
| 781 | }; |
| 782 | |
| 783 | static PyStructSequence_Desc statvfs_result_desc = { |
| 784 | "statvfs_result", /* name */ |
| 785 | statvfs_result__doc__, /* doc */ |
| 786 | statvfs_result_fields, |
| 787 | 10 |
| 788 | }; |
| 789 | |
| 790 | static PyTypeObject StatResultType; |
| 791 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 792 | static newfunc structseq_new; |
| 793 | |
| 794 | static PyObject * |
| 795 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 796 | { |
| 797 | PyStructSequence *result; |
| 798 | int i; |
| 799 | |
| 800 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 801 | if (!result) |
| 802 | return NULL; |
| 803 | /* If we have been initialized from a tuple, |
| 804 | st_?time might be set to None. Initialize it |
| 805 | from the int slots. */ |
| 806 | for (i = 7; i <= 9; i++) { |
| 807 | if (result->ob_item[i+3] == Py_None) { |
| 808 | Py_DECREF(Py_None); |
| 809 | Py_INCREF(result->ob_item[i]); |
| 810 | result->ob_item[i+3] = result->ob_item[i]; |
| 811 | } |
| 812 | } |
| 813 | return (PyObject*)result; |
| 814 | } |
| 815 | |
| 816 | |
| 817 | |
| 818 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 819 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 820 | |
| 821 | PyDoc_STRVAR(stat_float_times__doc__, |
| 822 | "stat_float_times([newval]) -> oldval\n\n\ |
| 823 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 824 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 825 | future calls return ints. \n\ |
| 826 | If newval is omitted, return the current setting.\n"); |
| 827 | |
| 828 | static PyObject* |
| 829 | stat_float_times(PyObject* self, PyObject *args) |
| 830 | { |
| 831 | int newval = -1; |
| 832 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 833 | return NULL; |
| 834 | if (newval == -1) |
| 835 | /* Return old value */ |
| 836 | return PyBool_FromLong(_stat_float_times); |
| 837 | _stat_float_times = newval; |
| 838 | Py_INCREF(Py_None); |
| 839 | return Py_None; |
| 840 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 841 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 842 | static void |
| 843 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 844 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 845 | PyObject *fval,*ival; |
| 846 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 847 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 848 | #else |
| 849 | ival = PyInt_FromLong((long)sec); |
| 850 | #endif |
| 851 | if (_stat_float_times) { |
| 852 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 853 | } else { |
| 854 | fval = ival; |
| 855 | Py_INCREF(fval); |
| 856 | } |
| 857 | PyStructSequence_SET_ITEM(v, index, ival); |
| 858 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 861 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 862 | (used by posix_stat() and posix_fstat()) */ |
| 863 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 864 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 865 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 866 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 867 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 868 | if (v == NULL) |
| 869 | return NULL; |
| 870 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 871 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 872 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 873 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 874 | PyLong_FromLongLong((PY_LONG_LONG)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 875 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 876 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 877 | #endif |
| 878 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 879 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 880 | PyLong_FromLongLong((PY_LONG_LONG)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 881 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 882 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 883 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 884 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 885 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 886 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 887 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 888 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 889 | PyLong_FromLongLong((PY_LONG_LONG)st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 890 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 891 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 892 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 893 | |
| 894 | #ifdef HAVE_STAT_TV_NSEC |
| 895 | ansec = st.st_atim.tv_nsec; |
| 896 | mnsec = st.st_mtim.tv_nsec; |
| 897 | cnsec = st.st_ctim.tv_nsec; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 898 | #else |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 899 | #ifdef HAVE_STAT_TV_NSEC2 |
| 900 | ansec = st.st_atimespec.tv_nsec; |
| 901 | mnsec = st.st_mtimespec.tv_nsec; |
| 902 | cnsec = st.st_ctimespec.tv_nsec; |
| 903 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 904 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 905 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 906 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 907 | fill_time(v, 7, st.st_atime, ansec); |
| 908 | fill_time(v, 8, st.st_mtime, mnsec); |
| 909 | fill_time(v, 9, st.st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 910 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 911 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 912 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 913 | PyInt_FromLong((long)st.st_blksize)); |
| 914 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 915 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 916 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 917 | PyInt_FromLong((long)st.st_blocks)); |
| 918 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 919 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 920 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 921 | PyInt_FromLong((long)st.st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 922 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 923 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 924 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 925 | PyInt_FromLong((long)st.st_gen)); |
| 926 | #endif |
| 927 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 928 | { |
| 929 | PyObject *val; |
| 930 | unsigned long bsec,bnsec; |
| 931 | bsec = (long)st.st_birthtime; |
| 932 | #ifdef HAVE_STAT_TV_NSEC2 |
| 933 | bnsec = st.st_birthtimespec.tv_nsec; |
| 934 | #else |
| 935 | bnsec = 0; |
| 936 | #endif |
| 937 | if (_stat_float_times) { |
| 938 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 939 | } else { |
| 940 | val = PyInt_FromLong((long)bsec); |
| 941 | } |
| 942 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 943 | val); |
| 944 | } |
| 945 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 946 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 947 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 948 | PyInt_FromLong((long)st.st_flags)); |
| 949 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 950 | |
| 951 | if (PyErr_Occurred()) { |
| 952 | Py_DECREF(v); |
| 953 | return NULL; |
| 954 | } |
| 955 | |
| 956 | return v; |
| 957 | } |
| 958 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 959 | #ifdef MS_WINDOWS |
| 960 | |
| 961 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 962 | where / can be used in place of \ and the trailing slash is optional. |
| 963 | Both SERVER and SHARE must have at least one character. |
| 964 | */ |
| 965 | |
| 966 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 967 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
| 968 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
| 969 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 970 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 971 | IsUNCRootA(char *path, int pathlen) |
| 972 | { |
| 973 | #define ISSLASH ISSLASHA |
| 974 | |
| 975 | int i, share; |
| 976 | |
| 977 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 978 | /* minimum UNCRoot is \\x\y */ |
| 979 | return FALSE; |
| 980 | for (i = 2; i < pathlen ; i++) |
| 981 | if (ISSLASH(path[i])) break; |
| 982 | if (i == 2 || i == pathlen) |
| 983 | /* do not allow \\\SHARE or \\SERVER */ |
| 984 | return FALSE; |
| 985 | share = i+1; |
| 986 | for (i = share; i < pathlen; i++) |
| 987 | if (ISSLASH(path[i])) break; |
| 988 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 989 | |
| 990 | #undef ISSLASH |
| 991 | } |
| 992 | |
| 993 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 994 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 995 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 996 | { |
| 997 | #define ISSLASH ISSLASHW |
| 998 | |
| 999 | int i, share; |
| 1000 | |
| 1001 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1002 | /* minimum UNCRoot is \\x\y */ |
| 1003 | return FALSE; |
| 1004 | for (i = 2; i < pathlen ; i++) |
| 1005 | if (ISSLASH(path[i])) break; |
| 1006 | if (i == 2 || i == pathlen) |
| 1007 | /* do not allow \\\SHARE or \\SERVER */ |
| 1008 | return FALSE; |
| 1009 | share = i+1; |
| 1010 | for (i = share; i < pathlen; i++) |
| 1011 | if (ISSLASH(path[i])) break; |
| 1012 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1013 | |
| 1014 | #undef ISSLASH |
| 1015 | } |
| 1016 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1017 | #endif /* MS_WINDOWS */ |
| 1018 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1019 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1020 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1021 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1022 | #ifdef __VMS |
| 1023 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1024 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1025 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1026 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1027 | char *wformat, |
| 1028 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1029 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1030 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1031 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1032 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1033 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1034 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1035 | #ifdef MS_WINDOWS |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1036 | int pathlen; |
| 1037 | char pathcopy[MAX_PATH]; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1038 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1039 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1040 | |
| 1041 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1042 | /* If on wide-character-capable OS see if argument |
| 1043 | is Unicode and if so use wide API. */ |
| 1044 | if (unicode_file_names()) { |
| 1045 | PyUnicodeObject *po; |
| 1046 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 1047 | Py_UNICODE wpath[MAX_PATH+1]; |
| 1048 | pathlen = wcslen(PyUnicode_AS_UNICODE(po)); |
| 1049 | /* the library call can blow up if the file name is too long! */ |
| 1050 | if (pathlen > MAX_PATH) { |
| 1051 | errno = ENAMETOOLONG; |
| 1052 | return posix_error(); |
| 1053 | } |
| 1054 | wcscpy(wpath, PyUnicode_AS_UNICODE(po)); |
| 1055 | /* Remove trailing slash or backslash, unless it's the current |
| 1056 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 1057 | */ |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1058 | if (pathlen > 0) { |
| 1059 | if (ISSLASHW(wpath[pathlen-1])) { |
| 1060 | /* It does end with a slash -- exempt the root drive cases. */ |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1061 | if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':') || |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1062 | IsUNCRootW(wpath, pathlen)) |
| 1063 | /* leave it alone */; |
| 1064 | else { |
| 1065 | /* nuke the trailing backslash */ |
| 1066 | wpath[pathlen-1] = L'\0'; |
| 1067 | } |
| 1068 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1069 | else if (ISSLASHW(wpath[1]) && pathlen < ARRAYSIZE(wpath)-1 && |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1070 | IsUNCRootW(wpath, pathlen)) { |
| 1071 | /* UNC root w/o trailing slash: add one when there's room */ |
| 1072 | wpath[pathlen++] = L'\\'; |
| 1073 | wpath[pathlen] = L'\0'; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | Py_BEGIN_ALLOW_THREADS |
| 1077 | /* PyUnicode_AS_UNICODE result OK without |
| 1078 | thread lock as it is a simple dereference. */ |
| 1079 | res = wstatfunc(wpath, &st); |
| 1080 | Py_END_ALLOW_THREADS |
| 1081 | if (res != 0) |
| 1082 | return posix_error_with_unicode_filename(wpath); |
| 1083 | return _pystat_fromstructstat(st); |
| 1084 | } |
| 1085 | /* Drop the argument parsing error as narrow strings |
| 1086 | are also valid. */ |
| 1087 | PyErr_Clear(); |
| 1088 | } |
| 1089 | #endif |
| 1090 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1091 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1092 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1093 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1094 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1095 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1096 | #ifdef MS_WINDOWS |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1097 | pathlen = strlen(path); |
| 1098 | /* the library call can blow up if the file name is too long! */ |
| 1099 | if (pathlen > MAX_PATH) { |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1100 | PyMem_Free(pathfree); |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1101 | errno = ENAMETOOLONG; |
| 1102 | return posix_error(); |
| 1103 | } |
| 1104 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1105 | /* Remove trailing slash or backslash, unless it's the current |
| 1106 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 1107 | */ |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1108 | if (pathlen > 0) { |
| 1109 | if (ISSLASHA(path[pathlen-1])) { |
| 1110 | /* It does end with a slash -- exempt the root drive cases. */ |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1111 | if (pathlen == 1 || (pathlen == 3 && path[1] == ':') || |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1112 | IsUNCRootA(path, pathlen)) |
| 1113 | /* leave it alone */; |
| 1114 | else { |
| 1115 | /* nuke the trailing backslash */ |
| 1116 | strncpy(pathcopy, path, pathlen); |
| 1117 | pathcopy[pathlen-1] = '\0'; |
| 1118 | path = pathcopy; |
| 1119 | } |
| 1120 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1121 | else if (ISSLASHA(path[1]) && pathlen < ARRAYSIZE(pathcopy)-1 && |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1122 | IsUNCRootA(path, pathlen)) { |
| 1123 | /* UNC root w/o trailing slash: add one when there's room */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1124 | strncpy(pathcopy, path, pathlen); |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1125 | pathcopy[pathlen++] = '\\'; |
| 1126 | pathcopy[pathlen] = '\0'; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1127 | path = pathcopy; |
| 1128 | } |
| 1129 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1130 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1131 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1132 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1133 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1134 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1135 | if (res != 0) |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1136 | return posix_error_with_allocated_filename(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1137 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1138 | PyMem_Free(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1139 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | /* POSIX methods */ |
| 1144 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1145 | PyDoc_STRVAR(posix_access__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1146 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1147 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1148 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1149 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1150 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1151 | 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] | 1152 | |
| 1153 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1154 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1155 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1156 | char *path; |
| 1157 | int mode; |
| 1158 | int res; |
| 1159 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1160 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1161 | if (unicode_file_names()) { |
| 1162 | PyUnicodeObject *po; |
| 1163 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1164 | Py_BEGIN_ALLOW_THREADS |
| 1165 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1166 | it is a simple dereference. */ |
| 1167 | res = _waccess(PyUnicode_AS_UNICODE(po), mode); |
| 1168 | Py_END_ALLOW_THREADS |
| 1169 | return(PyBool_FromLong(res == 0)); |
| 1170 | } |
| 1171 | /* Drop the argument parsing error as narrow strings |
| 1172 | are also valid. */ |
| 1173 | PyErr_Clear(); |
| 1174 | } |
| 1175 | #endif |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame] | 1176 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1177 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1178 | return NULL; |
| 1179 | Py_BEGIN_ALLOW_THREADS |
| 1180 | res = access(path, mode); |
| 1181 | Py_END_ALLOW_THREADS |
Guido van Rossum | 674deb2 | 2002-09-01 15:06:28 +0000 | [diff] [blame] | 1182 | return(PyBool_FromLong(res == 0)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1185 | #ifndef F_OK |
| 1186 | #define F_OK 0 |
| 1187 | #endif |
| 1188 | #ifndef R_OK |
| 1189 | #define R_OK 4 |
| 1190 | #endif |
| 1191 | #ifndef W_OK |
| 1192 | #define W_OK 2 |
| 1193 | #endif |
| 1194 | #ifndef X_OK |
| 1195 | #define X_OK 1 |
| 1196 | #endif |
| 1197 | |
| 1198 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1199 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1200 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1201 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1202 | |
| 1203 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1204 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1205 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1206 | int id; |
| 1207 | char *ret; |
| 1208 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1209 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1210 | return NULL; |
| 1211 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1212 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1213 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1214 | if (id == 0) { |
| 1215 | ret = ttyname(); |
| 1216 | } |
| 1217 | else { |
| 1218 | ret = NULL; |
| 1219 | } |
| 1220 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1221 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1222 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1223 | if (ret == NULL) |
| 1224 | return(posix_error()); |
| 1225 | return(PyString_FromString(ret)); |
| 1226 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1227 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1228 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1229 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1230 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1231 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1232 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1233 | |
| 1234 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1235 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1236 | { |
| 1237 | char *ret; |
| 1238 | char buffer[L_ctermid]; |
| 1239 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1240 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1241 | ret = ctermid_r(buffer); |
| 1242 | #else |
| 1243 | ret = ctermid(buffer); |
| 1244 | #endif |
| 1245 | if (ret == NULL) |
| 1246 | return(posix_error()); |
| 1247 | return(PyString_FromString(buffer)); |
| 1248 | } |
| 1249 | #endif |
| 1250 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1251 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1252 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1253 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1254 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1255 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1256 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1257 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1258 | #ifdef MS_WINDOWS |
| 1259 | return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir); |
| 1260 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1261 | return posix_1str(args, "et:chdir", _chdir2, NULL, NULL); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1262 | #elif defined(__VMS) |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1263 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1264 | NULL, NULL); |
| 1265 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1266 | return posix_1str(args, "et:chdir", chdir, NULL, NULL); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1267 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1270 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1271 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1272 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1273 | 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] | 1274 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1275 | |
| 1276 | static PyObject * |
| 1277 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1278 | { |
| 1279 | return posix_fildes(fdobj, fchdir); |
| 1280 | } |
| 1281 | #endif /* HAVE_FCHDIR */ |
| 1282 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1283 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1284 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1285 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1286 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1287 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1288 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1289 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1290 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1291 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1292 | int i; |
| 1293 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1294 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1295 | if (unicode_file_names()) { |
| 1296 | PyUnicodeObject *po; |
| 1297 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1298 | Py_BEGIN_ALLOW_THREADS |
| 1299 | res = _wchmod(PyUnicode_AS_UNICODE(po), i); |
| 1300 | Py_END_ALLOW_THREADS |
| 1301 | if (res < 0) |
| 1302 | return posix_error_with_unicode_filename( |
| 1303 | PyUnicode_AS_UNICODE(po)); |
| 1304 | Py_INCREF(Py_None); |
| 1305 | return Py_None; |
| 1306 | } |
| 1307 | /* Drop the argument parsing error as narrow strings |
| 1308 | are also valid. */ |
| 1309 | PyErr_Clear(); |
| 1310 | } |
| 1311 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1312 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1313 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1314 | return NULL; |
| 1315 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1316 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1317 | Py_END_ALLOW_THREADS |
| 1318 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1319 | return posix_error_with_allocated_filename(path); |
| 1320 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1321 | Py_INCREF(Py_None); |
| 1322 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1325 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1326 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1327 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1328 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1329 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1330 | |
| 1331 | static PyObject * |
| 1332 | posix_chroot(PyObject *self, PyObject *args) |
| 1333 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1334 | return posix_1str(args, "et:chroot", chroot, NULL, NULL); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1335 | } |
| 1336 | #endif |
| 1337 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1338 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1339 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1340 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1341 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1342 | |
| 1343 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1344 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1345 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1346 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1347 | } |
| 1348 | #endif /* HAVE_FSYNC */ |
| 1349 | |
| 1350 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1351 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1352 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1353 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1354 | #endif |
| 1355 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1356 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1357 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1358 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1359 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1360 | |
| 1361 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1362 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1363 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1364 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1365 | } |
| 1366 | #endif /* HAVE_FDATASYNC */ |
| 1367 | |
| 1368 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1369 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1370 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1371 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1372 | 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] | 1373 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1374 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1375 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1376 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1377 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1378 | int uid, gid; |
| 1379 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1380 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1381 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1382 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1383 | return NULL; |
| 1384 | Py_BEGIN_ALLOW_THREADS |
| 1385 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1386 | Py_END_ALLOW_THREADS |
| 1387 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1388 | return posix_error_with_allocated_filename(path); |
| 1389 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1390 | Py_INCREF(Py_None); |
| 1391 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1392 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1393 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1394 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1395 | #ifdef HAVE_LCHOWN |
| 1396 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1397 | "lchown(path, uid, gid)\n\n\ |
| 1398 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1399 | This function will not follow symbolic links."); |
| 1400 | |
| 1401 | static PyObject * |
| 1402 | posix_lchown(PyObject *self, PyObject *args) |
| 1403 | { |
| 1404 | char *path = NULL; |
| 1405 | int uid, gid; |
| 1406 | int res; |
| 1407 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1408 | Py_FileSystemDefaultEncoding, &path, |
| 1409 | &uid, &gid)) |
| 1410 | return NULL; |
| 1411 | Py_BEGIN_ALLOW_THREADS |
| 1412 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1413 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1414 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1415 | return posix_error_with_allocated_filename(path); |
| 1416 | PyMem_Free(path); |
| 1417 | Py_INCREF(Py_None); |
| 1418 | return Py_None; |
| 1419 | } |
| 1420 | #endif /* HAVE_LCHOWN */ |
| 1421 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1422 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1423 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1424 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1425 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1426 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1427 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1428 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1429 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1430 | { |
| 1431 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1432 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1433 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1434 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1435 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1436 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1437 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1438 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1439 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1440 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1441 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1442 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1443 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1444 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1445 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1446 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1447 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1448 | "getcwdu() -> path\n\n\ |
| 1449 | Return a unicode string representing the current working directory."); |
| 1450 | |
| 1451 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1452 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1453 | { |
| 1454 | char buf[1026]; |
| 1455 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1456 | |
| 1457 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1458 | if (unicode_file_names()) { |
| 1459 | wchar_t *wres; |
| 1460 | wchar_t wbuf[1026]; |
| 1461 | Py_BEGIN_ALLOW_THREADS |
| 1462 | wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]); |
| 1463 | Py_END_ALLOW_THREADS |
| 1464 | if (wres == NULL) |
| 1465 | return posix_error(); |
| 1466 | return PyUnicode_FromWideChar(wbuf, wcslen(wbuf)); |
| 1467 | } |
| 1468 | #endif |
| 1469 | |
| 1470 | Py_BEGIN_ALLOW_THREADS |
| 1471 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1472 | res = _getcwd2(buf, sizeof buf); |
| 1473 | #else |
| 1474 | res = getcwd(buf, sizeof buf); |
| 1475 | #endif |
| 1476 | Py_END_ALLOW_THREADS |
| 1477 | if (res == NULL) |
| 1478 | return posix_error(); |
| 1479 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1480 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1481 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1482 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1483 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1484 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1485 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1486 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1487 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1488 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1489 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1490 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1491 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1492 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1493 | return posix_2str(args, "etet:link", link, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1494 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1495 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1496 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1497 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1498 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1499 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1500 | Return a list containing the names of the entries in the directory.\n\ |
| 1501 | \n\ |
| 1502 | path: path of directory to list\n\ |
| 1503 | \n\ |
| 1504 | 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] | 1505 | entries '.' and '..' even if they are present in the directory."); |
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_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1509 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1510 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1511 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1512 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1513 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1514 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1515 | HANDLE hFindFile; |
| 1516 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1517 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 1518 | char namebuf[MAX_PATH*2+5]; |
| 1519 | char *bufptr = namebuf; |
| 1520 | int len = sizeof(namebuf)/sizeof(namebuf[0]); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1521 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1522 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1523 | /* If on wide-character-capable OS see if argument |
| 1524 | is Unicode and if so use wide API. */ |
| 1525 | if (unicode_file_names()) { |
| 1526 | PyUnicodeObject *po; |
| 1527 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1528 | WIN32_FIND_DATAW wFileData; |
| 1529 | Py_UNICODE wnamebuf[MAX_PATH*2+5]; |
| 1530 | Py_UNICODE wch; |
| 1531 | wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH); |
| 1532 | wnamebuf[MAX_PATH] = L'\0'; |
| 1533 | len = wcslen(wnamebuf); |
| 1534 | wch = (len > 0) ? wnamebuf[len-1] : L'\0'; |
| 1535 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1536 | wnamebuf[len++] = L'/'; |
| 1537 | wcscpy(wnamebuf + len, L"*.*"); |
| 1538 | if ((d = PyList_New(0)) == NULL) |
| 1539 | return NULL; |
| 1540 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1541 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1542 | errno = GetLastError(); |
| 1543 | if (errno == ERROR_FILE_NOT_FOUND) { |
| 1544 | return d; |
| 1545 | } |
| 1546 | Py_DECREF(d); |
| 1547 | return win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1548 | } |
| 1549 | do { |
| 1550 | if (wFileData.cFileName[0] == L'.' && |
| 1551 | (wFileData.cFileName[1] == L'\0' || |
| 1552 | wFileData.cFileName[1] == L'.' && |
| 1553 | wFileData.cFileName[2] == L'\0')) |
| 1554 | continue; |
| 1555 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1556 | if (v == NULL) { |
| 1557 | Py_DECREF(d); |
| 1558 | d = NULL; |
| 1559 | break; |
| 1560 | } |
| 1561 | if (PyList_Append(d, v) != 0) { |
| 1562 | Py_DECREF(v); |
| 1563 | Py_DECREF(d); |
| 1564 | d = NULL; |
| 1565 | break; |
| 1566 | } |
| 1567 | Py_DECREF(v); |
| 1568 | } while (FindNextFileW(hFindFile, &wFileData) == TRUE); |
| 1569 | |
| 1570 | if (FindClose(hFindFile) == FALSE) { |
| 1571 | Py_DECREF(d); |
| 1572 | return win32_error_unicode("FindClose", wnamebuf); |
| 1573 | } |
| 1574 | return d; |
| 1575 | } |
| 1576 | /* Drop the argument parsing error as narrow strings |
| 1577 | are also valid. */ |
| 1578 | PyErr_Clear(); |
| 1579 | } |
| 1580 | #endif |
| 1581 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1582 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1583 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1584 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1585 | if (len > 0) { |
| 1586 | char ch = namebuf[len-1]; |
| 1587 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 1588 | namebuf[len++] = '/'; |
| 1589 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1590 | strcpy(namebuf + len, "*.*"); |
| 1591 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1592 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1593 | return NULL; |
| 1594 | |
| 1595 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 1596 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1597 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 1598 | if (errno == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1599 | return d; |
| 1600 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1601 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1602 | } |
| 1603 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1604 | if (FileData.cFileName[0] == '.' && |
| 1605 | (FileData.cFileName[1] == '\0' || |
| 1606 | FileData.cFileName[1] == '.' && |
| 1607 | FileData.cFileName[2] == '\0')) |
| 1608 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1609 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1610 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1611 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1612 | d = NULL; |
| 1613 | break; |
| 1614 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1615 | if (PyList_Append(d, v) != 0) { |
| 1616 | Py_DECREF(v); |
| 1617 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1618 | d = NULL; |
| 1619 | break; |
| 1620 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1621 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1622 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 1623 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1624 | if (FindClose(hFindFile) == FALSE) { |
| 1625 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1626 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1627 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1628 | |
| 1629 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1630 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1631 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1632 | |
| 1633 | #ifndef MAX_PATH |
| 1634 | #define MAX_PATH CCHMAXPATH |
| 1635 | #endif |
| 1636 | char *name, *pt; |
| 1637 | int len; |
| 1638 | PyObject *d, *v; |
| 1639 | char namebuf[MAX_PATH+5]; |
| 1640 | HDIR hdir = 1; |
| 1641 | ULONG srchcnt = 1; |
| 1642 | FILEFINDBUF3 ep; |
| 1643 | APIRET rc; |
| 1644 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1645 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1646 | return NULL; |
| 1647 | if (len >= MAX_PATH) { |
| 1648 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1649 | return NULL; |
| 1650 | } |
| 1651 | strcpy(namebuf, name); |
| 1652 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1653 | if (*pt == ALTSEP) |
| 1654 | *pt = SEP; |
| 1655 | if (namebuf[len-1] != SEP) |
| 1656 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1657 | strcpy(namebuf + len, "*.*"); |
| 1658 | |
| 1659 | if ((d = PyList_New(0)) == NULL) |
| 1660 | return NULL; |
| 1661 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1662 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1663 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1664 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1665 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1666 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1667 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1668 | |
| 1669 | if (rc != NO_ERROR) { |
| 1670 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1671 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1674 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1675 | do { |
| 1676 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1677 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1678 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1679 | |
| 1680 | strcpy(namebuf, ep.achName); |
| 1681 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1682 | /* Leave Case of Name Alone -- In Native Form */ |
| 1683 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1684 | |
| 1685 | v = PyString_FromString(namebuf); |
| 1686 | if (v == NULL) { |
| 1687 | Py_DECREF(d); |
| 1688 | d = NULL; |
| 1689 | break; |
| 1690 | } |
| 1691 | if (PyList_Append(d, v) != 0) { |
| 1692 | Py_DECREF(v); |
| 1693 | Py_DECREF(d); |
| 1694 | d = NULL; |
| 1695 | break; |
| 1696 | } |
| 1697 | Py_DECREF(v); |
| 1698 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1699 | } |
| 1700 | |
| 1701 | return d; |
| 1702 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1703 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1704 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1705 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1706 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1707 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1708 | int arg_is_unicode = 1; |
| 1709 | |
| 1710 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 1711 | arg_is_unicode = 0; |
| 1712 | PyErr_Clear(); |
| 1713 | } |
| 1714 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1715 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1716 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1717 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1718 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1719 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1720 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1721 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1722 | return NULL; |
| 1723 | } |
| 1724 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1725 | if (ep->d_name[0] == '.' && |
| 1726 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1727 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1728 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1729 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1730 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1731 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1732 | d = NULL; |
| 1733 | break; |
| 1734 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1735 | #ifdef Py_USING_UNICODE |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1736 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1737 | PyObject *w; |
| 1738 | |
| 1739 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1740 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1741 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 1742 | if (w != NULL) { |
| 1743 | Py_DECREF(v); |
| 1744 | v = w; |
| 1745 | } |
| 1746 | else { |
| 1747 | /* fall back to the original byte string, as |
| 1748 | discussed in patch #683592 */ |
| 1749 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1750 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1751 | } |
| 1752 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1753 | if (PyList_Append(d, v) != 0) { |
| 1754 | Py_DECREF(v); |
| 1755 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1756 | d = NULL; |
| 1757 | break; |
| 1758 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1759 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1760 | } |
| 1761 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1762 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1763 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1764 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1765 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1766 | #endif /* which OS */ |
| 1767 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1768 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1769 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1770 | /* A helper function for abspath on win32 */ |
| 1771 | static PyObject * |
| 1772 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1773 | { |
| 1774 | /* assume encoded strings wont more than double no of chars */ |
| 1775 | char inbuf[MAX_PATH*2]; |
| 1776 | char *inbufp = inbuf; |
| 1777 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); |
| 1778 | char outbuf[MAX_PATH*2]; |
| 1779 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1780 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1781 | if (unicode_file_names()) { |
| 1782 | PyUnicodeObject *po; |
| 1783 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 1784 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 1785 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1786 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1787 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 1788 | woutbuf, &wtemp)) |
| 1789 | return win32_error("GetFullPathName", ""); |
| 1790 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 1791 | } |
| 1792 | /* Drop the argument parsing error as narrow strings |
| 1793 | are also valid. */ |
| 1794 | PyErr_Clear(); |
| 1795 | } |
| 1796 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1797 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1798 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1799 | &insize)) |
| 1800 | return NULL; |
| 1801 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1802 | outbuf, &temp)) |
| 1803 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 1804 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 1805 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 1806 | Py_FileSystemDefaultEncoding, NULL); |
| 1807 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1808 | return PyString_FromString(outbuf); |
| 1809 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1810 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1811 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1812 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1813 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1814 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1815 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1816 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1817 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1818 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1819 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1820 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1821 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1822 | |
| 1823 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1824 | if (unicode_file_names()) { |
| 1825 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 1826 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1827 | Py_BEGIN_ALLOW_THREADS |
| 1828 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1829 | it is a simple dereference. */ |
| 1830 | res = _wmkdir(PyUnicode_AS_UNICODE(po)); |
| 1831 | Py_END_ALLOW_THREADS |
| 1832 | if (res < 0) |
| 1833 | return posix_error(); |
| 1834 | Py_INCREF(Py_None); |
| 1835 | return Py_None; |
| 1836 | } |
| 1837 | /* Drop the argument parsing error as narrow strings |
| 1838 | are also valid. */ |
| 1839 | PyErr_Clear(); |
| 1840 | } |
| 1841 | #endif |
| 1842 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1843 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1844 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1845 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1846 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1847 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1848 | res = mkdir(path); |
| 1849 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1850 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1851 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1852 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1853 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1854 | return posix_error_with_allocated_filename(path); |
| 1855 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1856 | Py_INCREF(Py_None); |
| 1857 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1860 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1861 | #ifdef HAVE_NICE |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1862 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) |
| 1863 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) |
| 1864 | #include <sys/resource.h> |
| 1865 | #endif |
| 1866 | #endif |
| 1867 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1868 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1869 | "nice(inc) -> new_priority\n\n\ |
| 1870 | 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] | 1871 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1872 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1873 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1874 | { |
| 1875 | int increment, value; |
| 1876 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1877 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1878 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1879 | |
| 1880 | /* There are two flavours of 'nice': one that returns the new |
| 1881 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1882 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 1883 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1884 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1885 | If we are of the nice family that returns the new priority, we |
| 1886 | need to clear errno before the call, and check if errno is filled |
| 1887 | before calling posix_error() on a returnvalue of -1, because the |
| 1888 | -1 may be the actual new priority! */ |
| 1889 | |
| 1890 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1891 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1892 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1893 | if (value == 0) |
| 1894 | value = getpriority(PRIO_PROCESS, 0); |
| 1895 | #endif |
| 1896 | if (value == -1 && errno != 0) |
| 1897 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1898 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1899 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1900 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1901 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1902 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1903 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1904 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1905 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1906 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1907 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1908 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1909 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1910 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1911 | #ifdef MS_WINDOWS |
| 1912 | return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename); |
| 1913 | #else |
| 1914 | return posix_2str(args, "etet:rename", rename, NULL, NULL); |
| 1915 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1918 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1919 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1920 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1921 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1922 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1923 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1924 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1925 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1926 | #ifdef MS_WINDOWS |
| 1927 | return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir); |
| 1928 | #else |
| 1929 | return posix_1str(args, "et:rmdir", rmdir, NULL, NULL); |
| 1930 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1933 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1934 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1935 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1936 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1937 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1938 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1939 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1940 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1941 | #ifdef MS_WINDOWS |
| 1942 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64); |
| 1943 | #else |
| 1944 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 1945 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1948 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1949 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1950 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1951 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1952 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1953 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1954 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1955 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1956 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1957 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1958 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1959 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1960 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1961 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1962 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1963 | Py_END_ALLOW_THREADS |
| 1964 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1965 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1966 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1967 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1968 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1969 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1970 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1971 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1972 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1973 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1974 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1975 | { |
| 1976 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1977 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1978 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 1979 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1980 | if (i < 0) |
| 1981 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1982 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1985 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1986 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1987 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1988 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1990 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1991 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1992 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1993 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1994 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1995 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1996 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1997 | #ifdef MS_WINDOWS |
| 1998 | return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink); |
| 1999 | #else |
| 2000 | return posix_1str(args, "et:remove", unlink, NULL, NULL); |
| 2001 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2004 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2005 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2006 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2007 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2008 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2009 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2010 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2011 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2012 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2013 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2014 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2015 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2016 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2017 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2018 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2019 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2020 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2021 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2022 | u.sysname, |
| 2023 | u.nodename, |
| 2024 | u.release, |
| 2025 | u.version, |
| 2026 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2027 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2028 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2029 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2030 | static int |
| 2031 | extract_time(PyObject *t, long* sec, long* usec) |
| 2032 | { |
| 2033 | long intval; |
| 2034 | if (PyFloat_Check(t)) { |
| 2035 | double tval = PyFloat_AsDouble(t); |
| 2036 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 2037 | if (!intobj) |
| 2038 | return -1; |
| 2039 | intval = PyInt_AsLong(intobj); |
| 2040 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2041 | if (intval == -1 && PyErr_Occurred()) |
| 2042 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2043 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2044 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2045 | if (*usec < 0) |
| 2046 | /* If rounding gave us a negative number, |
| 2047 | truncate. */ |
| 2048 | *usec = 0; |
| 2049 | return 0; |
| 2050 | } |
| 2051 | intval = PyInt_AsLong(t); |
| 2052 | if (intval == -1 && PyErr_Occurred()) |
| 2053 | return -1; |
| 2054 | *sec = intval; |
| 2055 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2056 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2057 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2058 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2059 | PyDoc_STRVAR(posix_utime__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2060 | "utime(path, (atime, utime))\n\ |
| 2061 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2062 | 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] | 2063 | 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] | 2064 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2065 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2066 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2067 | { |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2068 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2069 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2070 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2071 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2072 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2073 | #if defined(HAVE_UTIMES) |
| 2074 | struct timeval buf[2]; |
| 2075 | #define ATIME buf[0].tv_sec |
| 2076 | #define MTIME buf[1].tv_sec |
| 2077 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2078 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2079 | struct utimbuf buf; |
| 2080 | #define ATIME buf.actime |
| 2081 | #define MTIME buf.modtime |
| 2082 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2083 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2084 | time_t buf[2]; |
| 2085 | #define ATIME buf[0] |
| 2086 | #define MTIME buf[1] |
| 2087 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2088 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2089 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2090 | int have_unicode_filename = 0; |
| 2091 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2092 | PyUnicodeObject *obwpath; |
| 2093 | wchar_t *wpath; |
| 2094 | if (unicode_file_names()) { |
| 2095 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2096 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2097 | have_unicode_filename = 1; |
| 2098 | } else |
| 2099 | /* Drop the argument parsing error as narrow strings |
| 2100 | are also valid. */ |
| 2101 | PyErr_Clear(); |
| 2102 | } |
| 2103 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 2104 | |
| 2105 | if (!have_unicode_filename && \ |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2106 | !PyArg_ParseTuple(args, "etO:utime", |
| 2107 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2108 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2109 | if (arg == Py_None) { |
| 2110 | /* optional time values not given */ |
| 2111 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2112 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2113 | if (have_unicode_filename) |
| 2114 | res = _wutime(wpath, NULL); |
| 2115 | else |
| 2116 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2117 | res = utime(path, NULL); |
| 2118 | Py_END_ALLOW_THREADS |
| 2119 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2120 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2121 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2122 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2123 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2124 | return NULL; |
| 2125 | } |
| 2126 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2127 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2128 | &atime, &ausec) == -1) { |
| 2129 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2130 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2131 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2132 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2133 | &mtime, &musec) == -1) { |
| 2134 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2135 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2136 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2137 | ATIME = atime; |
| 2138 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2139 | #ifdef HAVE_UTIMES |
| 2140 | buf[0].tv_usec = ausec; |
| 2141 | buf[1].tv_usec = musec; |
| 2142 | Py_BEGIN_ALLOW_THREADS |
| 2143 | res = utimes(path, buf); |
| 2144 | Py_END_ALLOW_THREADS |
| 2145 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2146 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2147 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2148 | if (have_unicode_filename) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 2149 | /* utime is OK with utimbuf, but _wutime insists |
| 2150 | on _utimbuf (the msvc headers assert the |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2151 | underscore version is ansi) */ |
| 2152 | res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG); |
| 2153 | else |
| 2154 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2155 | res = utime(path, UTIME_ARG); |
| 2156 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2157 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2158 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2159 | if (res < 0) { |
| 2160 | #ifdef Py_WIN_WIDE_FILENAMES |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2161 | if (have_unicode_filename) |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2162 | return posix_error_with_unicode_filename(wpath); |
| 2163 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2164 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2165 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2166 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2167 | Py_INCREF(Py_None); |
| 2168 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2169 | #undef UTIME_ARG |
| 2170 | #undef ATIME |
| 2171 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2174 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2175 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2176 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2177 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2178 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2179 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2180 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2181 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2182 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2183 | { |
| 2184 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2185 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2186 | return NULL; |
| 2187 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2188 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2191 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2192 | static void |
| 2193 | free_string_array(char **array, int count) |
| 2194 | { |
| 2195 | int i; |
| 2196 | for (i = 0; i < count; i++) |
| 2197 | PyMem_Free(array[i]); |
| 2198 | PyMem_DEL(array); |
| 2199 | } |
| 2200 | #endif |
| 2201 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2202 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2203 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2204 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2205 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2206 | Execute an executable path with arguments, replacing current process.\n\ |
| 2207 | \n\ |
| 2208 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2209 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2210 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2211 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2212 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2213 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2214 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2215 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2216 | char **argvlist; |
| 2217 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2218 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2219 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2220 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2221 | argv is a list or tuple of strings. */ |
| 2222 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2223 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2224 | Py_FileSystemDefaultEncoding, |
| 2225 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2226 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2227 | if (PyList_Check(argv)) { |
| 2228 | argc = PyList_Size(argv); |
| 2229 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2230 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2231 | else if (PyTuple_Check(argv)) { |
| 2232 | argc = PyTuple_Size(argv); |
| 2233 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2234 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2235 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2236 | 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] | 2237 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2238 | return NULL; |
| 2239 | } |
| 2240 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2241 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2242 | if (argvlist == NULL) { |
| 2243 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2244 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2245 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2246 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2247 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2248 | Py_FileSystemDefaultEncoding, |
| 2249 | &argvlist[i])) { |
| 2250 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2251 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2252 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2253 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2254 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2255 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2256 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2257 | } |
| 2258 | argvlist[argc] = NULL; |
| 2259 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2260 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2261 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2262 | /* If we get here it's definitely an error */ |
| 2263 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2264 | free_string_array(argvlist, argc); |
| 2265 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2266 | return posix_error(); |
| 2267 | } |
| 2268 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2269 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2270 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2271 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2272 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2273 | \n\ |
| 2274 | path: path of executable file\n\ |
| 2275 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2276 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2277 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2278 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2279 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2280 | { |
| 2281 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2282 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2283 | char **argvlist; |
| 2284 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2285 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2286 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2287 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2288 | int lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2289 | |
| 2290 | /* execve has three arguments: (path, argv, env), where |
| 2291 | argv is a list or tuple of strings and env is a dictionary |
| 2292 | like posix.environ. */ |
| 2293 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2294 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2295 | Py_FileSystemDefaultEncoding, |
| 2296 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2297 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2298 | if (PyList_Check(argv)) { |
| 2299 | argc = PyList_Size(argv); |
| 2300 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2301 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2302 | else if (PyTuple_Check(argv)) { |
| 2303 | argc = PyTuple_Size(argv); |
| 2304 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2305 | } |
| 2306 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2307 | PyErr_SetString(PyExc_TypeError, |
| 2308 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2309 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2310 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2311 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2312 | PyErr_SetString(PyExc_TypeError, |
| 2313 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2314 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2317 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2318 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2319 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2320 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2321 | } |
| 2322 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2323 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2324 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2325 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2326 | &argvlist[i])) |
| 2327 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2328 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2329 | goto fail_1; |
| 2330 | } |
| 2331 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2332 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2333 | argvlist[argc] = NULL; |
| 2334 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2335 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2336 | if (i < 0) |
| 2337 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2338 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2339 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2340 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2341 | goto fail_1; |
| 2342 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2343 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2344 | keys = PyMapping_Keys(env); |
| 2345 | vals = PyMapping_Values(env); |
| 2346 | if (!keys || !vals) |
| 2347 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2348 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2349 | PyErr_SetString(PyExc_TypeError, |
| 2350 | "execve(): env.keys() or env.values() is not a list"); |
| 2351 | goto fail_2; |
| 2352 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2353 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2354 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2355 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2356 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2357 | |
| 2358 | key = PyList_GetItem(keys, pos); |
| 2359 | val = PyList_GetItem(vals, pos); |
| 2360 | if (!key || !val) |
| 2361 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2362 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2363 | if (!PyArg_Parse( |
| 2364 | key, |
| 2365 | "s;execve() arg 3 contains a non-string key", |
| 2366 | &k) || |
| 2367 | !PyArg_Parse( |
| 2368 | val, |
| 2369 | "s;execve() arg 3 contains a non-string value", |
| 2370 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2371 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2372 | goto fail_2; |
| 2373 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2374 | |
| 2375 | #if defined(PYOS_OS2) |
| 2376 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2377 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2378 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2379 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2380 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2381 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2382 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2383 | goto fail_2; |
| 2384 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2385 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2386 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2387 | #if defined(PYOS_OS2) |
| 2388 | } |
| 2389 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2390 | } |
| 2391 | envlist[envc] = 0; |
| 2392 | |
| 2393 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2394 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2395 | /* If we get here it's definitely an error */ |
| 2396 | |
| 2397 | (void) posix_error(); |
| 2398 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2399 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2400 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2401 | PyMem_DEL(envlist[envc]); |
| 2402 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2403 | fail_1: |
| 2404 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2405 | Py_XDECREF(vals); |
| 2406 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2407 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2408 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2409 | return NULL; |
| 2410 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2411 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2412 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2413 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2414 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2415 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2416 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2417 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2418 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2419 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2420 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2421 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2422 | |
| 2423 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2424 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2425 | { |
| 2426 | char *path; |
| 2427 | PyObject *argv; |
| 2428 | char **argvlist; |
| 2429 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2430 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2431 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2432 | |
| 2433 | /* spawnv has three arguments: (mode, path, argv), where |
| 2434 | argv is a list or tuple of strings. */ |
| 2435 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2436 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2437 | Py_FileSystemDefaultEncoding, |
| 2438 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2439 | return NULL; |
| 2440 | if (PyList_Check(argv)) { |
| 2441 | argc = PyList_Size(argv); |
| 2442 | getitem = PyList_GetItem; |
| 2443 | } |
| 2444 | else if (PyTuple_Check(argv)) { |
| 2445 | argc = PyTuple_Size(argv); |
| 2446 | getitem = PyTuple_GetItem; |
| 2447 | } |
| 2448 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2449 | PyErr_SetString(PyExc_TypeError, |
| 2450 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2451 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2452 | return NULL; |
| 2453 | } |
| 2454 | |
| 2455 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2456 | if (argvlist == NULL) { |
| 2457 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2458 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2459 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2460 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2461 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2462 | Py_FileSystemDefaultEncoding, |
| 2463 | &argvlist[i])) { |
| 2464 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2465 | PyErr_SetString( |
| 2466 | PyExc_TypeError, |
| 2467 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2468 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2469 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2470 | } |
| 2471 | } |
| 2472 | argvlist[argc] = NULL; |
| 2473 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2474 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2475 | Py_BEGIN_ALLOW_THREADS |
| 2476 | spawnval = spawnv(mode, path, argvlist); |
| 2477 | Py_END_ALLOW_THREADS |
| 2478 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2479 | if (mode == _OLD_P_OVERLAY) |
| 2480 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2481 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2482 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2483 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2484 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2485 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2486 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2487 | free_string_array(argvlist, argc); |
| 2488 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2489 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2490 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2491 | return posix_error(); |
| 2492 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2493 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2494 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2495 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2496 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2497 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
| 2500 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2501 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2502 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2503 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2504 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2505 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2506 | path: path of executable file\n\ |
| 2507 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2508 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2509 | |
| 2510 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2511 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2512 | { |
| 2513 | char *path; |
| 2514 | PyObject *argv, *env; |
| 2515 | char **argvlist; |
| 2516 | char **envlist; |
| 2517 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2518 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2519 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2520 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2521 | int lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2522 | |
| 2523 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 2524 | argv is a list or tuple of strings and env is a dictionary |
| 2525 | like posix.environ. */ |
| 2526 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2527 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 2528 | Py_FileSystemDefaultEncoding, |
| 2529 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2530 | return NULL; |
| 2531 | if (PyList_Check(argv)) { |
| 2532 | argc = PyList_Size(argv); |
| 2533 | getitem = PyList_GetItem; |
| 2534 | } |
| 2535 | else if (PyTuple_Check(argv)) { |
| 2536 | argc = PyTuple_Size(argv); |
| 2537 | getitem = PyTuple_GetItem; |
| 2538 | } |
| 2539 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2540 | PyErr_SetString(PyExc_TypeError, |
| 2541 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2542 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2543 | } |
| 2544 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2545 | PyErr_SetString(PyExc_TypeError, |
| 2546 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2547 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
| 2550 | argvlist = PyMem_NEW(char *, argc+1); |
| 2551 | if (argvlist == NULL) { |
| 2552 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2553 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2554 | } |
| 2555 | for (i = 0; i < argc; i++) { |
| 2556 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2557 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2558 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2559 | &argvlist[i])) |
| 2560 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2561 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2562 | goto fail_1; |
| 2563 | } |
| 2564 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2565 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2566 | argvlist[argc] = NULL; |
| 2567 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2568 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2569 | if (i < 0) |
| 2570 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2571 | envlist = PyMem_NEW(char *, i + 1); |
| 2572 | if (envlist == NULL) { |
| 2573 | PyErr_NoMemory(); |
| 2574 | goto fail_1; |
| 2575 | } |
| 2576 | envc = 0; |
| 2577 | keys = PyMapping_Keys(env); |
| 2578 | vals = PyMapping_Values(env); |
| 2579 | if (!keys || !vals) |
| 2580 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2581 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2582 | PyErr_SetString(PyExc_TypeError, |
| 2583 | "spawnve(): env.keys() or env.values() is not a list"); |
| 2584 | goto fail_2; |
| 2585 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2586 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2587 | for (pos = 0; pos < i; pos++) { |
| 2588 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2589 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2590 | |
| 2591 | key = PyList_GetItem(keys, pos); |
| 2592 | val = PyList_GetItem(vals, pos); |
| 2593 | if (!key || !val) |
| 2594 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2595 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2596 | if (!PyArg_Parse( |
| 2597 | key, |
| 2598 | "s;spawnve() arg 3 contains a non-string key", |
| 2599 | &k) || |
| 2600 | !PyArg_Parse( |
| 2601 | val, |
| 2602 | "s;spawnve() arg 3 contains a non-string value", |
| 2603 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2604 | { |
| 2605 | goto fail_2; |
| 2606 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2607 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2608 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2609 | if (p == NULL) { |
| 2610 | PyErr_NoMemory(); |
| 2611 | goto fail_2; |
| 2612 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2613 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2614 | envlist[envc++] = p; |
| 2615 | } |
| 2616 | envlist[envc] = 0; |
| 2617 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2618 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2619 | Py_BEGIN_ALLOW_THREADS |
| 2620 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2621 | Py_END_ALLOW_THREADS |
| 2622 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2623 | if (mode == _OLD_P_OVERLAY) |
| 2624 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2625 | |
| 2626 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2627 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2628 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2629 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2630 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2631 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2632 | (void) posix_error(); |
| 2633 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2634 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2635 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2636 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2637 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2638 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2639 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2640 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2641 | while (--envc >= 0) |
| 2642 | PyMem_DEL(envlist[envc]); |
| 2643 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2644 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2645 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2646 | Py_XDECREF(vals); |
| 2647 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2648 | fail_0: |
| 2649 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2650 | return res; |
| 2651 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2652 | |
| 2653 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 2654 | #if defined(PYOS_OS2) |
| 2655 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 2656 | "spawnvp(mode, file, args)\n\n\ |
| 2657 | Execute the program 'file' in a new process, using the environment\n\ |
| 2658 | search path to find the file.\n\ |
| 2659 | \n\ |
| 2660 | mode: mode of process creation\n\ |
| 2661 | file: executable file name\n\ |
| 2662 | args: tuple or list of strings"); |
| 2663 | |
| 2664 | static PyObject * |
| 2665 | posix_spawnvp(PyObject *self, PyObject *args) |
| 2666 | { |
| 2667 | char *path; |
| 2668 | PyObject *argv; |
| 2669 | char **argvlist; |
| 2670 | int mode, i, argc; |
| 2671 | Py_intptr_t spawnval; |
| 2672 | PyObject *(*getitem)(PyObject *, int); |
| 2673 | |
| 2674 | /* spawnvp has three arguments: (mode, path, argv), where |
| 2675 | argv is a list or tuple of strings. */ |
| 2676 | |
| 2677 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 2678 | Py_FileSystemDefaultEncoding, |
| 2679 | &path, &argv)) |
| 2680 | return NULL; |
| 2681 | if (PyList_Check(argv)) { |
| 2682 | argc = PyList_Size(argv); |
| 2683 | getitem = PyList_GetItem; |
| 2684 | } |
| 2685 | else if (PyTuple_Check(argv)) { |
| 2686 | argc = PyTuple_Size(argv); |
| 2687 | getitem = PyTuple_GetItem; |
| 2688 | } |
| 2689 | else { |
| 2690 | PyErr_SetString(PyExc_TypeError, |
| 2691 | "spawnvp() arg 2 must be a tuple or list"); |
| 2692 | PyMem_Free(path); |
| 2693 | return NULL; |
| 2694 | } |
| 2695 | |
| 2696 | argvlist = PyMem_NEW(char *, argc+1); |
| 2697 | if (argvlist == NULL) { |
| 2698 | PyMem_Free(path); |
| 2699 | return PyErr_NoMemory(); |
| 2700 | } |
| 2701 | for (i = 0; i < argc; i++) { |
| 2702 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2703 | Py_FileSystemDefaultEncoding, |
| 2704 | &argvlist[i])) { |
| 2705 | free_string_array(argvlist, i); |
| 2706 | PyErr_SetString( |
| 2707 | PyExc_TypeError, |
| 2708 | "spawnvp() arg 2 must contain only strings"); |
| 2709 | PyMem_Free(path); |
| 2710 | return NULL; |
| 2711 | } |
| 2712 | } |
| 2713 | argvlist[argc] = NULL; |
| 2714 | |
| 2715 | Py_BEGIN_ALLOW_THREADS |
| 2716 | #if defined(PYCC_GCC) |
| 2717 | spawnval = spawnvp(mode, path, argvlist); |
| 2718 | #else |
| 2719 | spawnval = _spawnvp(mode, path, argvlist); |
| 2720 | #endif |
| 2721 | Py_END_ALLOW_THREADS |
| 2722 | |
| 2723 | free_string_array(argvlist, argc); |
| 2724 | PyMem_Free(path); |
| 2725 | |
| 2726 | if (spawnval == -1) |
| 2727 | return posix_error(); |
| 2728 | else |
| 2729 | return Py_BuildValue("l", (long) spawnval); |
| 2730 | } |
| 2731 | |
| 2732 | |
| 2733 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 2734 | "spawnvpe(mode, file, args, env)\n\n\ |
| 2735 | Execute the program 'file' in a new process, using the environment\n\ |
| 2736 | search path to find the file.\n\ |
| 2737 | \n\ |
| 2738 | mode: mode of process creation\n\ |
| 2739 | file: executable file name\n\ |
| 2740 | args: tuple or list of arguments\n\ |
| 2741 | env: dictionary of strings mapping to strings"); |
| 2742 | |
| 2743 | static PyObject * |
| 2744 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 2745 | { |
| 2746 | char *path; |
| 2747 | PyObject *argv, *env; |
| 2748 | char **argvlist; |
| 2749 | char **envlist; |
| 2750 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2751 | int mode, i, pos, argc, envc; |
| 2752 | Py_intptr_t spawnval; |
| 2753 | PyObject *(*getitem)(PyObject *, int); |
| 2754 | int lastarg = 0; |
| 2755 | |
| 2756 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 2757 | argv is a list or tuple of strings and env is a dictionary |
| 2758 | like posix.environ. */ |
| 2759 | |
| 2760 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 2761 | Py_FileSystemDefaultEncoding, |
| 2762 | &path, &argv, &env)) |
| 2763 | return NULL; |
| 2764 | if (PyList_Check(argv)) { |
| 2765 | argc = PyList_Size(argv); |
| 2766 | getitem = PyList_GetItem; |
| 2767 | } |
| 2768 | else if (PyTuple_Check(argv)) { |
| 2769 | argc = PyTuple_Size(argv); |
| 2770 | getitem = PyTuple_GetItem; |
| 2771 | } |
| 2772 | else { |
| 2773 | PyErr_SetString(PyExc_TypeError, |
| 2774 | "spawnvpe() arg 2 must be a tuple or list"); |
| 2775 | goto fail_0; |
| 2776 | } |
| 2777 | if (!PyMapping_Check(env)) { |
| 2778 | PyErr_SetString(PyExc_TypeError, |
| 2779 | "spawnvpe() arg 3 must be a mapping object"); |
| 2780 | goto fail_0; |
| 2781 | } |
| 2782 | |
| 2783 | argvlist = PyMem_NEW(char *, argc+1); |
| 2784 | if (argvlist == NULL) { |
| 2785 | PyErr_NoMemory(); |
| 2786 | goto fail_0; |
| 2787 | } |
| 2788 | for (i = 0; i < argc; i++) { |
| 2789 | if (!PyArg_Parse((*getitem)(argv, i), |
| 2790 | "et;spawnvpe() arg 2 must contain only strings", |
| 2791 | Py_FileSystemDefaultEncoding, |
| 2792 | &argvlist[i])) |
| 2793 | { |
| 2794 | lastarg = i; |
| 2795 | goto fail_1; |
| 2796 | } |
| 2797 | } |
| 2798 | lastarg = argc; |
| 2799 | argvlist[argc] = NULL; |
| 2800 | |
| 2801 | i = PyMapping_Size(env); |
| 2802 | if (i < 0) |
| 2803 | goto fail_1; |
| 2804 | envlist = PyMem_NEW(char *, i + 1); |
| 2805 | if (envlist == NULL) { |
| 2806 | PyErr_NoMemory(); |
| 2807 | goto fail_1; |
| 2808 | } |
| 2809 | envc = 0; |
| 2810 | keys = PyMapping_Keys(env); |
| 2811 | vals = PyMapping_Values(env); |
| 2812 | if (!keys || !vals) |
| 2813 | goto fail_2; |
| 2814 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2815 | PyErr_SetString(PyExc_TypeError, |
| 2816 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 2817 | goto fail_2; |
| 2818 | } |
| 2819 | |
| 2820 | for (pos = 0; pos < i; pos++) { |
| 2821 | char *p, *k, *v; |
| 2822 | size_t len; |
| 2823 | |
| 2824 | key = PyList_GetItem(keys, pos); |
| 2825 | val = PyList_GetItem(vals, pos); |
| 2826 | if (!key || !val) |
| 2827 | goto fail_2; |
| 2828 | |
| 2829 | if (!PyArg_Parse( |
| 2830 | key, |
| 2831 | "s;spawnvpe() arg 3 contains a non-string key", |
| 2832 | &k) || |
| 2833 | !PyArg_Parse( |
| 2834 | val, |
| 2835 | "s;spawnvpe() arg 3 contains a non-string value", |
| 2836 | &v)) |
| 2837 | { |
| 2838 | goto fail_2; |
| 2839 | } |
| 2840 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2841 | p = PyMem_NEW(char, len); |
| 2842 | if (p == NULL) { |
| 2843 | PyErr_NoMemory(); |
| 2844 | goto fail_2; |
| 2845 | } |
| 2846 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 2847 | envlist[envc++] = p; |
| 2848 | } |
| 2849 | envlist[envc] = 0; |
| 2850 | |
| 2851 | Py_BEGIN_ALLOW_THREADS |
| 2852 | #if defined(PYCC_GCC) |
| 2853 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2854 | #else |
| 2855 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 2856 | #endif |
| 2857 | Py_END_ALLOW_THREADS |
| 2858 | |
| 2859 | if (spawnval == -1) |
| 2860 | (void) posix_error(); |
| 2861 | else |
| 2862 | res = Py_BuildValue("l", (long) spawnval); |
| 2863 | |
| 2864 | fail_2: |
| 2865 | while (--envc >= 0) |
| 2866 | PyMem_DEL(envlist[envc]); |
| 2867 | PyMem_DEL(envlist); |
| 2868 | fail_1: |
| 2869 | free_string_array(argvlist, lastarg); |
| 2870 | Py_XDECREF(vals); |
| 2871 | Py_XDECREF(keys); |
| 2872 | fail_0: |
| 2873 | PyMem_Free(path); |
| 2874 | return res; |
| 2875 | } |
| 2876 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2877 | #endif /* HAVE_SPAWNV */ |
| 2878 | |
| 2879 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2880 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2881 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2882 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2883 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 2884 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2885 | 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] | 2886 | |
| 2887 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2888 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2889 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2890 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2891 | if (pid == -1) |
| 2892 | return posix_error(); |
| 2893 | PyOS_AfterFork(); |
| 2894 | return PyInt_FromLong((long)pid); |
| 2895 | } |
| 2896 | #endif |
| 2897 | |
| 2898 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2899 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2900 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2901 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2902 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2903 | 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] | 2904 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2905 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2906 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2907 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2908 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2909 | if (pid == -1) |
| 2910 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2911 | if (pid == 0) |
| 2912 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2913 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2914 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2915 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2916 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2917 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 2918 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 2919 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2920 | #define DEV_PTY_FILE "/dev/ptc" |
| 2921 | #define HAVE_DEV_PTMX |
| 2922 | #else |
| 2923 | #define DEV_PTY_FILE "/dev/ptmx" |
| 2924 | #endif |
| 2925 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2926 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2927 | #ifdef HAVE_PTY_H |
| 2928 | #include <pty.h> |
| 2929 | #else |
| 2930 | #ifdef HAVE_LIBUTIL_H |
| 2931 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2932 | #endif /* HAVE_LIBUTIL_H */ |
| 2933 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 2934 | #ifdef HAVE_STROPTS_H |
| 2935 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2936 | #endif |
| 2937 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2938 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2939 | #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] | 2940 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2941 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2942 | 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] | 2943 | |
| 2944 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2945 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2946 | { |
| 2947 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2948 | #ifndef HAVE_OPENPTY |
| 2949 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2950 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2951 | #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] | 2952 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2953 | #ifdef sun |
| 2954 | extern char *ptsname(); |
| 2955 | #endif |
| 2956 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2957 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2958 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2959 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 2960 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2961 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2962 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 2963 | if (slave_name == NULL) |
| 2964 | return posix_error(); |
| 2965 | |
| 2966 | slave_fd = open(slave_name, O_RDWR); |
| 2967 | if (slave_fd < 0) |
| 2968 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2969 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2970 | 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] | 2971 | if (master_fd < 0) |
| 2972 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 2973 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2974 | /* change permission of slave */ |
| 2975 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 2976 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2977 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2978 | } |
| 2979 | /* unlock slave */ |
| 2980 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 2981 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2982 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2983 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 2984 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2985 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 2986 | if (slave_name == NULL) |
| 2987 | return posix_error(); |
| 2988 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 2989 | if (slave_fd < 0) |
| 2990 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2991 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2992 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 2993 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 2994 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2995 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 2996 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2997 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 2998 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2999 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3000 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3001 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3002 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3003 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3004 | |
| 3005 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3006 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3007 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3008 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3009 | 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] | 3010 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3011 | |
| 3012 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3013 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3014 | { |
| 3015 | int master_fd, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3016 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3017 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3018 | if (pid == -1) |
| 3019 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3020 | if (pid == 0) |
| 3021 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3022 | return Py_BuildValue("(ii)", pid, master_fd); |
| 3023 | } |
| 3024 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3025 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3026 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3027 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3028 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3029 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3030 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3031 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3032 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3033 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3034 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3035 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3036 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3037 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3038 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3039 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3040 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3041 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3042 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3043 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3044 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3045 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3046 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3047 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3048 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3049 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3050 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3051 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3052 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3053 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3054 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3055 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3056 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3057 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3058 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3059 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3060 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3061 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3062 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3063 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3064 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3065 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3066 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3067 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3068 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3069 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3070 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3071 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3072 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3075 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3076 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3077 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3078 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3079 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3080 | |
| 3081 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3082 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3083 | { |
| 3084 | PyObject *result = NULL; |
| 3085 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3086 | #ifdef NGROUPS_MAX |
| 3087 | #define MAX_GROUPS NGROUPS_MAX |
| 3088 | #else |
| 3089 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3090 | #define MAX_GROUPS 64 |
| 3091 | #endif |
| 3092 | gid_t grouplist[MAX_GROUPS]; |
| 3093 | int n; |
| 3094 | |
| 3095 | n = getgroups(MAX_GROUPS, grouplist); |
| 3096 | if (n < 0) |
| 3097 | posix_error(); |
| 3098 | else { |
| 3099 | result = PyList_New(n); |
| 3100 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3101 | int i; |
| 3102 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3103 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3104 | if (o == NULL) { |
| 3105 | Py_DECREF(result); |
| 3106 | result = NULL; |
| 3107 | break; |
| 3108 | } |
| 3109 | PyList_SET_ITEM(result, i, o); |
| 3110 | } |
| 3111 | } |
| 3112 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3113 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3114 | return result; |
| 3115 | } |
| 3116 | #endif |
| 3117 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3118 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3119 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3120 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3121 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3122 | |
| 3123 | static PyObject * |
| 3124 | posix_getpgid(PyObject *self, PyObject *args) |
| 3125 | { |
| 3126 | int pid, pgid; |
| 3127 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3128 | return NULL; |
| 3129 | pgid = getpgid(pid); |
| 3130 | if (pgid < 0) |
| 3131 | return posix_error(); |
| 3132 | return PyInt_FromLong((long)pgid); |
| 3133 | } |
| 3134 | #endif /* HAVE_GETPGID */ |
| 3135 | |
| 3136 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3137 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3138 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3139 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3140 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3141 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3142 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3143 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3144 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3145 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3146 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3147 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3148 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3149 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3150 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3151 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3152 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3153 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3154 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3155 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3156 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3157 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3158 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3159 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3160 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3161 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3162 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3163 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3164 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3165 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3166 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3167 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3168 | Py_INCREF(Py_None); |
| 3169 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3172 | #endif /* HAVE_SETPGRP */ |
| 3173 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3174 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3175 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3176 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3177 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3178 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3179 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3180 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3181 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3182 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3183 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3184 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3185 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3186 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3187 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3188 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3189 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3190 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3191 | |
| 3192 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3193 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3194 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3195 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3196 | char *name; |
| 3197 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3198 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3199 | errno = 0; |
| 3200 | name = getlogin(); |
| 3201 | if (name == NULL) { |
| 3202 | if (errno) |
| 3203 | posix_error(); |
| 3204 | else |
| 3205 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3206 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3207 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3208 | else |
| 3209 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3210 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3211 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3212 | return result; |
| 3213 | } |
| 3214 | #endif |
| 3215 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3216 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3217 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3218 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3219 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3220 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3221 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3222 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3223 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3224 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3225 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3226 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3227 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3228 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3229 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3230 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3231 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3232 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3233 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3234 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3235 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3236 | { |
| 3237 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3238 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3239 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3240 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3241 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3242 | APIRET rc; |
| 3243 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3244 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3245 | |
| 3246 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3247 | APIRET rc; |
| 3248 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3249 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3250 | |
| 3251 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3252 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3253 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3254 | if (kill(pid, sig) == -1) |
| 3255 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3256 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3257 | Py_INCREF(Py_None); |
| 3258 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3259 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3260 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3261 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3262 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3263 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3264 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3265 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3266 | |
| 3267 | static PyObject * |
| 3268 | posix_killpg(PyObject *self, PyObject *args) |
| 3269 | { |
| 3270 | int pgid, sig; |
| 3271 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3272 | return NULL; |
| 3273 | if (killpg(pgid, sig) == -1) |
| 3274 | return posix_error(); |
| 3275 | Py_INCREF(Py_None); |
| 3276 | return Py_None; |
| 3277 | } |
| 3278 | #endif |
| 3279 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3280 | #ifdef HAVE_PLOCK |
| 3281 | |
| 3282 | #ifdef HAVE_SYS_LOCK_H |
| 3283 | #include <sys/lock.h> |
| 3284 | #endif |
| 3285 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3286 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3287 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3288 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3289 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3290 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3291 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3292 | { |
| 3293 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3294 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3295 | return NULL; |
| 3296 | if (plock(op) == -1) |
| 3297 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3298 | Py_INCREF(Py_None); |
| 3299 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3300 | } |
| 3301 | #endif |
| 3302 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3303 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3304 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3305 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3306 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3307 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3308 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3309 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3310 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3311 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3312 | async_system(const char *command) |
| 3313 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3314 | char errormsg[256], args[1024]; |
| 3315 | RESULTCODES rcodes; |
| 3316 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3317 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3318 | char *shell = getenv("COMSPEC"); |
| 3319 | if (!shell) |
| 3320 | shell = "cmd"; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3321 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3322 | /* avoid overflowing the argument buffer */ |
| 3323 | if (strlen(shell) + 3 + strlen(command) >= 1024) |
| 3324 | return ERROR_NOT_ENOUGH_MEMORY |
| 3325 | |
| 3326 | args[0] = '\0'; |
| 3327 | strcat(args, shell); |
| 3328 | strcat(args, "/c "); |
| 3329 | strcat(args, command); |
| 3330 | |
| 3331 | /* execute asynchronously, inheriting the environment */ |
| 3332 | rc = DosExecPgm(errormsg, |
| 3333 | sizeof(errormsg), |
| 3334 | EXEC_ASYNC, |
| 3335 | args, |
| 3336 | NULL, |
| 3337 | &rcodes, |
| 3338 | shell); |
| 3339 | return rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3342 | static FILE * |
| 3343 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3344 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3345 | int oldfd, tgtfd; |
| 3346 | HFILE pipeh[2]; |
| 3347 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3348 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3349 | /* mode determines which of stdin or stdout is reconnected to |
| 3350 | * the pipe to the child |
| 3351 | */ |
| 3352 | if (strchr(mode, 'r') != NULL) { |
| 3353 | tgt_fd = 1; /* stdout */ |
| 3354 | } else if (strchr(mode, 'w')) { |
| 3355 | tgt_fd = 0; /* stdin */ |
| 3356 | } else { |
| 3357 | *err = ERROR_INVALID_ACCESS; |
| 3358 | return NULL; |
| 3359 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3360 | |
Andrew MacIntyre | a3be258 | 2004-12-18 09:51:05 +0000 | [diff] [blame] | 3361 | /* setup the pipe */ |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3362 | if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) { |
| 3363 | *err = rc; |
| 3364 | return NULL; |
| 3365 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3366 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3367 | /* prevent other threads accessing stdio */ |
| 3368 | DosEnterCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3369 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3370 | /* reconnect stdio and execute child */ |
| 3371 | oldfd = dup(tgtfd); |
| 3372 | close(tgtfd); |
| 3373 | if (dup2(pipeh[tgtfd], tgtfd) == 0) { |
| 3374 | DosClose(pipeh[tgtfd]); |
| 3375 | rc = async_system(command); |
| 3376 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3377 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3378 | /* restore stdio */ |
| 3379 | dup2(oldfd, tgtfd); |
| 3380 | close(oldfd); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3381 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3382 | /* allow other threads access to stdio */ |
| 3383 | DosExitCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3384 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3385 | /* if execution of child was successful return file stream */ |
| 3386 | if (rc == NO_ERROR) |
| 3387 | return fdopen(pipeh[1 - tgtfd], mode); |
| 3388 | else { |
| 3389 | DosClose(pipeh[1 - tgtfd]); |
| 3390 | *err = rc; |
| 3391 | return NULL; |
| 3392 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3393 | } |
| 3394 | |
| 3395 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3396 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3397 | { |
| 3398 | char *name; |
| 3399 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3400 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3401 | FILE *fp; |
| 3402 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3403 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3404 | return NULL; |
| 3405 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3406 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3407 | Py_END_ALLOW_THREADS |
| 3408 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3409 | return os2_error(err); |
| 3410 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3411 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 3412 | if (f != NULL) |
| 3413 | PyFile_SetBufSize(f, bufsize); |
| 3414 | return f; |
| 3415 | } |
| 3416 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3417 | #elif defined(PYCC_GCC) |
| 3418 | |
| 3419 | /* standard posix version of popen() support */ |
| 3420 | static PyObject * |
| 3421 | posix_popen(PyObject *self, PyObject *args) |
| 3422 | { |
| 3423 | char *name; |
| 3424 | char *mode = "r"; |
| 3425 | int bufsize = -1; |
| 3426 | FILE *fp; |
| 3427 | PyObject *f; |
| 3428 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 3429 | return NULL; |
| 3430 | Py_BEGIN_ALLOW_THREADS |
| 3431 | fp = popen(name, mode); |
| 3432 | Py_END_ALLOW_THREADS |
| 3433 | if (fp == NULL) |
| 3434 | return posix_error(); |
| 3435 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 3436 | if (f != NULL) |
| 3437 | PyFile_SetBufSize(f, bufsize); |
| 3438 | return f; |
| 3439 | } |
| 3440 | |
| 3441 | /* fork() under OS/2 has lots'o'warts |
| 3442 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 3443 | * most of this code is a ripoff of the win32 code, but using the |
| 3444 | * capabilities of EMX's C library routines |
| 3445 | */ |
| 3446 | |
| 3447 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 3448 | #define POPEN_1 1 |
| 3449 | #define POPEN_2 2 |
| 3450 | #define POPEN_3 3 |
| 3451 | #define POPEN_4 4 |
| 3452 | |
| 3453 | static PyObject *_PyPopen(char *, int, int, int); |
| 3454 | static int _PyPclose(FILE *file); |
| 3455 | |
| 3456 | /* |
| 3457 | * Internal dictionary mapping popen* file pointers to process handles, |
| 3458 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3459 | * for more information on this dictionary's use. |
| 3460 | */ |
| 3461 | static PyObject *_PyPopenProcs = NULL; |
| 3462 | |
| 3463 | /* os2emx version of popen2() |
| 3464 | * |
| 3465 | * The result of this function is a pipe (file) connected to the |
| 3466 | * process's stdin, and a pipe connected to the process's stdout. |
| 3467 | */ |
| 3468 | |
| 3469 | static PyObject * |
| 3470 | os2emx_popen2(PyObject *self, PyObject *args) |
| 3471 | { |
| 3472 | PyObject *f; |
| 3473 | int tm=0; |
| 3474 | |
| 3475 | char *cmdstring; |
| 3476 | char *mode = "t"; |
| 3477 | int bufsize = -1; |
| 3478 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 3479 | return NULL; |
| 3480 | |
| 3481 | if (*mode == 't') |
| 3482 | tm = O_TEXT; |
| 3483 | else if (*mode != 'b') { |
| 3484 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3485 | return NULL; |
| 3486 | } else |
| 3487 | tm = O_BINARY; |
| 3488 | |
| 3489 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 3490 | |
| 3491 | return f; |
| 3492 | } |
| 3493 | |
| 3494 | /* |
| 3495 | * Variation on os2emx.popen2 |
| 3496 | * |
| 3497 | * The result of this function is 3 pipes - the process's stdin, |
| 3498 | * stdout and stderr |
| 3499 | */ |
| 3500 | |
| 3501 | static PyObject * |
| 3502 | os2emx_popen3(PyObject *self, PyObject *args) |
| 3503 | { |
| 3504 | PyObject *f; |
| 3505 | int tm = 0; |
| 3506 | |
| 3507 | char *cmdstring; |
| 3508 | char *mode = "t"; |
| 3509 | int bufsize = -1; |
| 3510 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 3511 | return NULL; |
| 3512 | |
| 3513 | if (*mode == 't') |
| 3514 | tm = O_TEXT; |
| 3515 | else if (*mode != 'b') { |
| 3516 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3517 | return NULL; |
| 3518 | } else |
| 3519 | tm = O_BINARY; |
| 3520 | |
| 3521 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 3522 | |
| 3523 | return f; |
| 3524 | } |
| 3525 | |
| 3526 | /* |
| 3527 | * Variation on os2emx.popen2 |
| 3528 | * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3529 | * The result of this function is 2 pipes - the processes stdin, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3530 | * and stdout+stderr combined as a single pipe. |
| 3531 | */ |
| 3532 | |
| 3533 | static PyObject * |
| 3534 | os2emx_popen4(PyObject *self, PyObject *args) |
| 3535 | { |
| 3536 | PyObject *f; |
| 3537 | int tm = 0; |
| 3538 | |
| 3539 | char *cmdstring; |
| 3540 | char *mode = "t"; |
| 3541 | int bufsize = -1; |
| 3542 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 3543 | return NULL; |
| 3544 | |
| 3545 | if (*mode == 't') |
| 3546 | tm = O_TEXT; |
| 3547 | else if (*mode != 'b') { |
| 3548 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3549 | return NULL; |
| 3550 | } else |
| 3551 | tm = O_BINARY; |
| 3552 | |
| 3553 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 3554 | |
| 3555 | return f; |
| 3556 | } |
| 3557 | |
| 3558 | /* a couple of structures for convenient handling of multiple |
| 3559 | * file handles and pipes |
| 3560 | */ |
| 3561 | struct file_ref |
| 3562 | { |
| 3563 | int handle; |
| 3564 | int flags; |
| 3565 | }; |
| 3566 | |
| 3567 | struct pipe_ref |
| 3568 | { |
| 3569 | int rd; |
| 3570 | int wr; |
| 3571 | }; |
| 3572 | |
| 3573 | /* The following code is derived from the win32 code */ |
| 3574 | |
| 3575 | static PyObject * |
| 3576 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 3577 | { |
| 3578 | struct file_ref stdio[3]; |
| 3579 | struct pipe_ref p_fd[3]; |
| 3580 | FILE *p_s[3]; |
| 3581 | int file_count, i, pipe_err, pipe_pid; |
| 3582 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 3583 | PyObject *f, *p_f[3]; |
| 3584 | |
| 3585 | /* file modes for subsequent fdopen's on pipe handles */ |
| 3586 | if (mode == O_TEXT) |
| 3587 | { |
| 3588 | rd_mode = "rt"; |
| 3589 | wr_mode = "wt"; |
| 3590 | } |
| 3591 | else |
| 3592 | { |
| 3593 | rd_mode = "rb"; |
| 3594 | wr_mode = "wb"; |
| 3595 | } |
| 3596 | |
| 3597 | /* prepare shell references */ |
| 3598 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 3599 | if ((shell = getenv("COMSPEC")) == NULL) |
| 3600 | { |
| 3601 | errno = ENOENT; |
| 3602 | return posix_error(); |
| 3603 | } |
| 3604 | |
| 3605 | sh_name = _getname(shell); |
| 3606 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 3607 | opt = "/c"; |
| 3608 | else |
| 3609 | opt = "-c"; |
| 3610 | |
| 3611 | /* save current stdio fds + their flags, and set not inheritable */ |
| 3612 | i = pipe_err = 0; |
| 3613 | while (pipe_err >= 0 && i < 3) |
| 3614 | { |
| 3615 | pipe_err = stdio[i].handle = dup(i); |
| 3616 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 3617 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 3618 | i++; |
| 3619 | } |
| 3620 | if (pipe_err < 0) |
| 3621 | { |
| 3622 | /* didn't get them all saved - clean up and bail out */ |
| 3623 | int saved_err = errno; |
| 3624 | while (i-- > 0) |
| 3625 | { |
| 3626 | close(stdio[i].handle); |
| 3627 | } |
| 3628 | errno = saved_err; |
| 3629 | return posix_error(); |
| 3630 | } |
| 3631 | |
| 3632 | /* create pipe ends */ |
| 3633 | file_count = 2; |
| 3634 | if (n == POPEN_3) |
| 3635 | file_count = 3; |
| 3636 | i = pipe_err = 0; |
| 3637 | while ((pipe_err == 0) && (i < file_count)) |
| 3638 | pipe_err = pipe((int *)&p_fd[i++]); |
| 3639 | if (pipe_err < 0) |
| 3640 | { |
| 3641 | /* didn't get them all made - clean up and bail out */ |
| 3642 | while (i-- > 0) |
| 3643 | { |
| 3644 | close(p_fd[i].wr); |
| 3645 | close(p_fd[i].rd); |
| 3646 | } |
| 3647 | errno = EPIPE; |
| 3648 | return posix_error(); |
| 3649 | } |
| 3650 | |
| 3651 | /* change the actual standard IO streams over temporarily, |
| 3652 | * making the retained pipe ends non-inheritable |
| 3653 | */ |
| 3654 | pipe_err = 0; |
| 3655 | |
| 3656 | /* - stdin */ |
| 3657 | if (dup2(p_fd[0].rd, 0) == 0) |
| 3658 | { |
| 3659 | close(p_fd[0].rd); |
| 3660 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 3661 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 3662 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 3663 | { |
| 3664 | close(p_fd[0].wr); |
| 3665 | pipe_err = -1; |
| 3666 | } |
| 3667 | } |
| 3668 | else |
| 3669 | { |
| 3670 | pipe_err = -1; |
| 3671 | } |
| 3672 | |
| 3673 | /* - stdout */ |
| 3674 | if (pipe_err == 0) |
| 3675 | { |
| 3676 | if (dup2(p_fd[1].wr, 1) == 1) |
| 3677 | { |
| 3678 | close(p_fd[1].wr); |
| 3679 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 3680 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 3681 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 3682 | { |
| 3683 | close(p_fd[1].rd); |
| 3684 | pipe_err = -1; |
| 3685 | } |
| 3686 | } |
| 3687 | else |
| 3688 | { |
| 3689 | pipe_err = -1; |
| 3690 | } |
| 3691 | } |
| 3692 | |
| 3693 | /* - stderr, as required */ |
| 3694 | if (pipe_err == 0) |
| 3695 | switch (n) |
| 3696 | { |
| 3697 | case POPEN_3: |
| 3698 | { |
| 3699 | if (dup2(p_fd[2].wr, 2) == 2) |
| 3700 | { |
| 3701 | close(p_fd[2].wr); |
| 3702 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 3703 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 3704 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 3705 | { |
| 3706 | close(p_fd[2].rd); |
| 3707 | pipe_err = -1; |
| 3708 | } |
| 3709 | } |
| 3710 | else |
| 3711 | { |
| 3712 | pipe_err = -1; |
| 3713 | } |
| 3714 | break; |
| 3715 | } |
| 3716 | |
| 3717 | case POPEN_4: |
| 3718 | { |
| 3719 | if (dup2(1, 2) != 2) |
| 3720 | { |
| 3721 | pipe_err = -1; |
| 3722 | } |
| 3723 | break; |
| 3724 | } |
| 3725 | } |
| 3726 | |
| 3727 | /* spawn the child process */ |
| 3728 | if (pipe_err == 0) |
| 3729 | { |
| 3730 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 3731 | if (pipe_pid == -1) |
| 3732 | { |
| 3733 | pipe_err = -1; |
| 3734 | } |
| 3735 | else |
| 3736 | { |
| 3737 | /* save the PID into the FILE structure |
| 3738 | * NOTE: this implementation doesn't actually |
| 3739 | * take advantage of this, but do it for |
| 3740 | * completeness - AIM Apr01 |
| 3741 | */ |
| 3742 | for (i = 0; i < file_count; i++) |
| 3743 | p_s[i]->_pid = pipe_pid; |
| 3744 | } |
| 3745 | } |
| 3746 | |
| 3747 | /* reset standard IO to normal */ |
| 3748 | for (i = 0; i < 3; i++) |
| 3749 | { |
| 3750 | dup2(stdio[i].handle, i); |
| 3751 | fcntl(i, F_SETFD, stdio[i].flags); |
| 3752 | close(stdio[i].handle); |
| 3753 | } |
| 3754 | |
| 3755 | /* if any remnant problems, clean up and bail out */ |
| 3756 | if (pipe_err < 0) |
| 3757 | { |
| 3758 | for (i = 0; i < 3; i++) |
| 3759 | { |
| 3760 | close(p_fd[i].rd); |
| 3761 | close(p_fd[i].wr); |
| 3762 | } |
| 3763 | errno = EPIPE; |
| 3764 | return posix_error_with_filename(cmdstring); |
| 3765 | } |
| 3766 | |
| 3767 | /* build tuple of file objects to return */ |
| 3768 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 3769 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3770 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3771 | PyFile_SetBufSize(p_f[1], bufsize); |
| 3772 | if (n == POPEN_3) |
| 3773 | { |
| 3774 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3775 | PyFile_SetBufSize(p_f[0], bufsize); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3776 | 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] | 3777 | } |
| 3778 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3779 | f = PyTuple_Pack(2, p_f[0], p_f[1]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3780 | |
| 3781 | /* |
| 3782 | * Insert the files we've created into the process dictionary |
| 3783 | * all referencing the list with the process handle and the |
| 3784 | * initial number of files (see description below in _PyPclose). |
| 3785 | * Since if _PyPclose later tried to wait on a process when all |
| 3786 | * handles weren't closed, it could create a deadlock with the |
| 3787 | * child, we spend some energy here to try to ensure that we |
| 3788 | * either insert all file handles into the dictionary or none |
| 3789 | * at all. It's a little clumsy with the various popen modes |
| 3790 | * and variable number of files involved. |
| 3791 | */ |
| 3792 | if (!_PyPopenProcs) |
| 3793 | { |
| 3794 | _PyPopenProcs = PyDict_New(); |
| 3795 | } |
| 3796 | |
| 3797 | if (_PyPopenProcs) |
| 3798 | { |
| 3799 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 3800 | int ins_rc[3]; |
| 3801 | |
| 3802 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 3803 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 3804 | |
| 3805 | procObj = PyList_New(2); |
| 3806 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 3807 | intObj = PyInt_FromLong((long) file_count); |
| 3808 | |
| 3809 | if (procObj && pidObj && intObj) |
| 3810 | { |
| 3811 | PyList_SetItem(procObj, 0, pidObj); |
| 3812 | PyList_SetItem(procObj, 1, intObj); |
| 3813 | |
| 3814 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 3815 | if (fileObj[0]) |
| 3816 | { |
| 3817 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 3818 | fileObj[0], |
| 3819 | procObj); |
| 3820 | } |
| 3821 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 3822 | if (fileObj[1]) |
| 3823 | { |
| 3824 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 3825 | fileObj[1], |
| 3826 | procObj); |
| 3827 | } |
| 3828 | if (file_count >= 3) |
| 3829 | { |
| 3830 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 3831 | if (fileObj[2]) |
| 3832 | { |
| 3833 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 3834 | fileObj[2], |
| 3835 | procObj); |
| 3836 | } |
| 3837 | } |
| 3838 | |
| 3839 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 3840 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 3841 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 3842 | { |
| 3843 | /* Something failed - remove any dictionary |
| 3844 | * entries that did make it. |
| 3845 | */ |
| 3846 | if (!ins_rc[0] && fileObj[0]) |
| 3847 | { |
| 3848 | PyDict_DelItem(_PyPopenProcs, |
| 3849 | fileObj[0]); |
| 3850 | } |
| 3851 | if (!ins_rc[1] && fileObj[1]) |
| 3852 | { |
| 3853 | PyDict_DelItem(_PyPopenProcs, |
| 3854 | fileObj[1]); |
| 3855 | } |
| 3856 | if (!ins_rc[2] && fileObj[2]) |
| 3857 | { |
| 3858 | PyDict_DelItem(_PyPopenProcs, |
| 3859 | fileObj[2]); |
| 3860 | } |
| 3861 | } |
| 3862 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3863 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3864 | /* |
| 3865 | * Clean up our localized references for the dictionary keys |
| 3866 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 3867 | * that got placed in the dictionary. |
| 3868 | */ |
| 3869 | Py_XDECREF(procObj); |
| 3870 | Py_XDECREF(fileObj[0]); |
| 3871 | Py_XDECREF(fileObj[1]); |
| 3872 | Py_XDECREF(fileObj[2]); |
| 3873 | } |
| 3874 | |
| 3875 | /* Child is launched. */ |
| 3876 | return f; |
| 3877 | } |
| 3878 | |
| 3879 | /* |
| 3880 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 3881 | * exit code for the child process and return as a result of the close. |
| 3882 | * |
| 3883 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 3884 | * input file pointer to information about the process that was |
| 3885 | * originally created by the popen* call that created the file pointer. |
| 3886 | * The dictionary uses the file pointer as a key (with one entry |
| 3887 | * inserted for each file returned by the original popen* call) and a |
| 3888 | * single list object as the value for all files from a single call. |
| 3889 | * The list object contains the Win32 process handle at [0], and a file |
| 3890 | * count at [1], which is initialized to the total number of file |
| 3891 | * handles using that list. |
| 3892 | * |
| 3893 | * This function closes whichever handle it is passed, and decrements |
| 3894 | * the file count in the dictionary for the process handle pointed to |
| 3895 | * by this file. On the last close (when the file count reaches zero), |
| 3896 | * this function will wait for the child process and then return its |
| 3897 | * exit code as the result of the close() operation. This permits the |
| 3898 | * files to be closed in any order - it is always the close() of the |
| 3899 | * final handle that will return the exit code. |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3900 | * |
| 3901 | * NOTE: This function is currently called with the GIL released. |
| 3902 | * hence we use the GILState API to manage our state. |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3903 | */ |
| 3904 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3905 | static int _PyPclose(FILE *file) |
| 3906 | { |
| 3907 | int result; |
| 3908 | int exit_code; |
| 3909 | int pipe_pid; |
| 3910 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 3911 | int file_count; |
| 3912 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3913 | PyGILState_STATE state; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3914 | #endif |
| 3915 | |
| 3916 | /* Close the file handle first, to ensure it can't block the |
| 3917 | * child from exiting if it's the last handle. |
| 3918 | */ |
| 3919 | result = fclose(file); |
| 3920 | |
| 3921 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3922 | state = PyGILState_Ensure(); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3923 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3924 | if (_PyPopenProcs) |
| 3925 | { |
| 3926 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 3927 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 3928 | fileObj)) != NULL && |
| 3929 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 3930 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 3931 | { |
| 3932 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 3933 | file_count = (int) PyInt_AsLong(intObj); |
| 3934 | |
| 3935 | if (file_count > 1) |
| 3936 | { |
| 3937 | /* Still other files referencing process */ |
| 3938 | file_count--; |
| 3939 | PyList_SetItem(procObj,1, |
| 3940 | PyInt_FromLong((long) file_count)); |
| 3941 | } |
| 3942 | else |
| 3943 | { |
| 3944 | /* Last file for this process */ |
| 3945 | if (result != EOF && |
| 3946 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 3947 | { |
| 3948 | /* extract exit status */ |
| 3949 | if (WIFEXITED(exit_code)) |
| 3950 | { |
| 3951 | result = WEXITSTATUS(exit_code); |
| 3952 | } |
| 3953 | else |
| 3954 | { |
| 3955 | errno = EPIPE; |
| 3956 | result = -1; |
| 3957 | } |
| 3958 | } |
| 3959 | else |
| 3960 | { |
| 3961 | /* Indicate failure - this will cause the file object |
| 3962 | * to raise an I/O error and translate the last |
| 3963 | * error code from errno. We do have a problem with |
| 3964 | * last errors that overlap the normal errno table, |
| 3965 | * but that's a consistent problem with the file object. |
| 3966 | */ |
| 3967 | result = -1; |
| 3968 | } |
| 3969 | } |
| 3970 | |
| 3971 | /* Remove this file pointer from dictionary */ |
| 3972 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 3973 | |
| 3974 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 3975 | { |
| 3976 | Py_DECREF(_PyPopenProcs); |
| 3977 | _PyPopenProcs = NULL; |
| 3978 | } |
| 3979 | |
| 3980 | } /* if object retrieval ok */ |
| 3981 | |
| 3982 | Py_XDECREF(fileObj); |
| 3983 | } /* if _PyPopenProcs */ |
| 3984 | |
| 3985 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3986 | PyGILState_Release(state); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3987 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3988 | return result; |
| 3989 | } |
| 3990 | |
| 3991 | #endif /* PYCC_??? */ |
| 3992 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3993 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3994 | |
| 3995 | /* |
| 3996 | * Portable 'popen' replacement for Win32. |
| 3997 | * |
| 3998 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 3999 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4000 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4001 | */ |
| 4002 | |
| 4003 | #include <malloc.h> |
| 4004 | #include <io.h> |
| 4005 | #include <fcntl.h> |
| 4006 | |
| 4007 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 4008 | #define POPEN_1 1 |
| 4009 | #define POPEN_2 2 |
| 4010 | #define POPEN_3 3 |
| 4011 | #define POPEN_4 4 |
| 4012 | |
| 4013 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4014 | static int _PyPclose(FILE *file); |
| 4015 | |
| 4016 | /* |
| 4017 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4018 | * for use when retrieving the process exit code. See _PyPclose() below |
| 4019 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4020 | */ |
| 4021 | static PyObject *_PyPopenProcs = NULL; |
| 4022 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4023 | |
| 4024 | /* popen that works from a GUI. |
| 4025 | * |
| 4026 | * The result of this function is a pipe (file) connected to the |
| 4027 | * processes stdin or stdout, depending on the requested mode. |
| 4028 | */ |
| 4029 | |
| 4030 | static PyObject * |
| 4031 | posix_popen(PyObject *self, PyObject *args) |
| 4032 | { |
Raymond Hettinger | b5cb665 | 2003-09-01 22:25:41 +0000 | [diff] [blame] | 4033 | PyObject *f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4034 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4035 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4036 | char *cmdstring; |
| 4037 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4038 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4039 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4040 | return NULL; |
| 4041 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4042 | if (*mode == 'r') |
| 4043 | tm = _O_RDONLY; |
| 4044 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4045 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4046 | return NULL; |
| 4047 | } else |
| 4048 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4049 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4050 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4051 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4052 | return NULL; |
| 4053 | } |
| 4054 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4055 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4056 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4057 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4058 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4059 | else |
| 4060 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 4061 | |
| 4062 | return f; |
| 4063 | } |
| 4064 | |
| 4065 | /* Variation on win32pipe.popen |
| 4066 | * |
| 4067 | * The result of this function is a pipe (file) connected to the |
| 4068 | * process's stdin, and a pipe connected to the process's stdout. |
| 4069 | */ |
| 4070 | |
| 4071 | static PyObject * |
| 4072 | win32_popen2(PyObject *self, PyObject *args) |
| 4073 | { |
| 4074 | PyObject *f; |
| 4075 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4076 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4077 | char *cmdstring; |
| 4078 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4079 | int bufsize = -1; |
| 4080 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4081 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4082 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4083 | if (*mode == 't') |
| 4084 | tm = _O_TEXT; |
| 4085 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4086 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4087 | return NULL; |
| 4088 | } else |
| 4089 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4090 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4091 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4092 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4093 | return NULL; |
| 4094 | } |
| 4095 | |
| 4096 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4097 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4098 | return f; |
| 4099 | } |
| 4100 | |
| 4101 | /* |
| 4102 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4103 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4104 | * The result of this function is 3 pipes - the process's stdin, |
| 4105 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4106 | */ |
| 4107 | |
| 4108 | static PyObject * |
| 4109 | win32_popen3(PyObject *self, PyObject *args) |
| 4110 | { |
| 4111 | PyObject *f; |
| 4112 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4113 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4114 | char *cmdstring; |
| 4115 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4116 | int bufsize = -1; |
| 4117 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4118 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4119 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4120 | if (*mode == 't') |
| 4121 | tm = _O_TEXT; |
| 4122 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4123 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4124 | return NULL; |
| 4125 | } else |
| 4126 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4127 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4128 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4129 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4130 | return NULL; |
| 4131 | } |
| 4132 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4133 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4134 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4135 | return f; |
| 4136 | } |
| 4137 | |
| 4138 | /* |
| 4139 | * Variation on win32pipe.popen |
| 4140 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4141 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4142 | * and stdout+stderr combined as a single pipe. |
| 4143 | */ |
| 4144 | |
| 4145 | static PyObject * |
| 4146 | win32_popen4(PyObject *self, PyObject *args) |
| 4147 | { |
| 4148 | PyObject *f; |
| 4149 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4150 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4151 | char *cmdstring; |
| 4152 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4153 | int bufsize = -1; |
| 4154 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4155 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4156 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4157 | if (*mode == 't') |
| 4158 | tm = _O_TEXT; |
| 4159 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4160 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4161 | return NULL; |
| 4162 | } else |
| 4163 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4164 | |
| 4165 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4166 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4167 | return NULL; |
| 4168 | } |
| 4169 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4170 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4171 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4172 | return f; |
| 4173 | } |
| 4174 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4175 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4176 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4177 | HANDLE hStdin, |
| 4178 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4179 | HANDLE hStderr, |
| 4180 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4181 | { |
| 4182 | PROCESS_INFORMATION piProcInfo; |
| 4183 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4184 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4185 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4186 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4187 | int i; |
| 4188 | int x; |
| 4189 | |
| 4190 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4191 | char *comshell; |
| 4192 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4193 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4194 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 4195 | return x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4196 | |
| 4197 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 4198 | * then use the w9xpopen hack. |
| 4199 | */ |
| 4200 | comshell = s1 + x; |
| 4201 | while (comshell >= s1 && *comshell != '\\') |
| 4202 | --comshell; |
| 4203 | ++comshell; |
| 4204 | |
| 4205 | if (GetVersion() < 0x80000000 && |
| 4206 | _stricmp(comshell, "command.com") != 0) { |
| 4207 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4208 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4209 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4210 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4211 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4212 | } |
| 4213 | else { |
| 4214 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4215 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 4216 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4217 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4218 | char modulepath[_MAX_PATH]; |
| 4219 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4220 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 4221 | for (i = x = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4222 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4223 | x = i+1; |
| 4224 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4225 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4226 | strncat(modulepath, |
| 4227 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4228 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4229 | -strlen(modulepath)); |
| 4230 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4231 | /* Eeek - file-not-found - possibly an embedding |
| 4232 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4233 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4234 | strncpy(modulepath, |
| 4235 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4236 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 4237 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 4238 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4239 | strncat(modulepath, |
| 4240 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4241 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4242 | -strlen(modulepath)); |
| 4243 | /* No where else to look - raise an easily identifiable |
| 4244 | error, rather than leaving Windows to report |
| 4245 | "file not found" - as the user is probably blissfully |
| 4246 | unaware this shim EXE is used, and it will confuse them. |
| 4247 | (well, it confused me for a while ;-) |
| 4248 | */ |
| 4249 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4250 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4251 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4252 | "for popen to work with your shell " |
| 4253 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4254 | szConsoleSpawn); |
| 4255 | return FALSE; |
| 4256 | } |
| 4257 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4258 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4259 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4260 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4261 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4262 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4263 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4264 | /* To maintain correct argument passing semantics, |
| 4265 | we pass the command-line as it stands, and allow |
| 4266 | quoting to be applied. w9xpopen.exe will then |
| 4267 | use its argv vector, and re-quote the necessary |
| 4268 | args for the ultimate child process. |
| 4269 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4270 | PyOS_snprintf( |
| 4271 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4272 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4273 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4274 | s1, |
| 4275 | s3, |
| 4276 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4277 | /* Not passing CREATE_NEW_CONSOLE has been known to |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4278 | cause random failures on win9x. Specifically a |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4279 | dialog: |
| 4280 | "Your program accessed mem currently in use at xxx" |
| 4281 | and a hopeful warning about the stability of your |
| 4282 | system. |
| 4283 | Cost is Ctrl+C wont kill children, but anyone |
| 4284 | who cares can have a go! |
| 4285 | */ |
| 4286 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4287 | } |
| 4288 | } |
| 4289 | |
| 4290 | /* Could be an else here to try cmd.exe / command.com in the path |
| 4291 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4292 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4293 | PyErr_SetString(PyExc_RuntimeError, |
| 4294 | "Cannot locate a COMSPEC environment variable to " |
| 4295 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4296 | return FALSE; |
| 4297 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4298 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4299 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 4300 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 4301 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 4302 | siStartInfo.hStdInput = hStdin; |
| 4303 | siStartInfo.hStdOutput = hStdout; |
| 4304 | siStartInfo.hStdError = hStderr; |
| 4305 | siStartInfo.wShowWindow = SW_HIDE; |
| 4306 | |
| 4307 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4308 | s2, |
| 4309 | NULL, |
| 4310 | NULL, |
| 4311 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4312 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4313 | NULL, |
| 4314 | NULL, |
| 4315 | &siStartInfo, |
| 4316 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4317 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4318 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4319 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4320 | /* Return process handle */ |
| 4321 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4322 | return TRUE; |
| 4323 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4324 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4325 | return FALSE; |
| 4326 | } |
| 4327 | |
| 4328 | /* The following code is based off of KB: Q190351 */ |
| 4329 | |
| 4330 | static PyObject * |
| 4331 | _PyPopen(char *cmdstring, int mode, int n) |
| 4332 | { |
| 4333 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 4334 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4335 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4336 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4337 | SECURITY_ATTRIBUTES saAttr; |
| 4338 | BOOL fSuccess; |
| 4339 | int fd1, fd2, fd3; |
| 4340 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4341 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4342 | PyObject *f; |
| 4343 | |
| 4344 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 4345 | saAttr.bInheritHandle = TRUE; |
| 4346 | saAttr.lpSecurityDescriptor = NULL; |
| 4347 | |
| 4348 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 4349 | return win32_error("CreatePipe", NULL); |
| 4350 | |
| 4351 | /* Create new output read handle and the input write handle. Set |
| 4352 | * the inheritance properties to FALSE. Otherwise, the child inherits |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 4353 | * these handles; resulting in non-closeable handles to the pipes |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4354 | * being created. */ |
| 4355 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4356 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 4357 | FALSE, |
| 4358 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4359 | if (!fSuccess) |
| 4360 | return win32_error("DuplicateHandle", NULL); |
| 4361 | |
| 4362 | /* Close the inheritable version of ChildStdin |
| 4363 | that we're using. */ |
| 4364 | CloseHandle(hChildStdinWr); |
| 4365 | |
| 4366 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 4367 | return win32_error("CreatePipe", NULL); |
| 4368 | |
| 4369 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4370 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 4371 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4372 | if (!fSuccess) |
| 4373 | return win32_error("DuplicateHandle", NULL); |
| 4374 | |
| 4375 | /* Close the inheritable version of ChildStdout |
| 4376 | that we're using. */ |
| 4377 | CloseHandle(hChildStdoutRd); |
| 4378 | |
| 4379 | if (n != POPEN_4) { |
| 4380 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 4381 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4382 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 4383 | hChildStderrRd, |
| 4384 | GetCurrentProcess(), |
| 4385 | &hChildStderrRdDup, 0, |
| 4386 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4387 | if (!fSuccess) |
| 4388 | return win32_error("DuplicateHandle", NULL); |
| 4389 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 4390 | CloseHandle(hChildStderrRd); |
| 4391 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4392 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4393 | switch (n) { |
| 4394 | case POPEN_1: |
| 4395 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 4396 | case _O_WRONLY | _O_TEXT: |
| 4397 | /* Case for writing to child Stdin in text mode. */ |
| 4398 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4399 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4400 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4401 | PyFile_SetBufSize(f, 0); |
| 4402 | /* We don't care about these pipes anymore, so close them. */ |
| 4403 | CloseHandle(hChildStdoutRdDup); |
| 4404 | CloseHandle(hChildStderrRdDup); |
| 4405 | break; |
| 4406 | |
| 4407 | case _O_RDONLY | _O_TEXT: |
| 4408 | /* Case for reading from child Stdout in text mode. */ |
| 4409 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4410 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4411 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4412 | PyFile_SetBufSize(f, 0); |
| 4413 | /* We don't care about these pipes anymore, so close them. */ |
| 4414 | CloseHandle(hChildStdinWrDup); |
| 4415 | CloseHandle(hChildStderrRdDup); |
| 4416 | break; |
| 4417 | |
| 4418 | case _O_RDONLY | _O_BINARY: |
| 4419 | /* Case for readinig from child Stdout in binary mode. */ |
| 4420 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4421 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4422 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4423 | PyFile_SetBufSize(f, 0); |
| 4424 | /* We don't care about these pipes anymore, so close them. */ |
| 4425 | CloseHandle(hChildStdinWrDup); |
| 4426 | CloseHandle(hChildStderrRdDup); |
| 4427 | break; |
| 4428 | |
| 4429 | case _O_WRONLY | _O_BINARY: |
| 4430 | /* Case for writing to child Stdin in binary mode. */ |
| 4431 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4432 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4433 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4434 | PyFile_SetBufSize(f, 0); |
| 4435 | /* We don't care about these pipes anymore, so close them. */ |
| 4436 | CloseHandle(hChildStdoutRdDup); |
| 4437 | CloseHandle(hChildStderrRdDup); |
| 4438 | break; |
| 4439 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4440 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4441 | break; |
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 | case POPEN_2: |
| 4444 | case POPEN_4: |
| 4445 | { |
| 4446 | char *m1, *m2; |
| 4447 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4448 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4449 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4450 | m1 = "r"; |
| 4451 | m2 = "w"; |
| 4452 | } else { |
| 4453 | m1 = "rb"; |
| 4454 | m2 = "wb"; |
| 4455 | } |
| 4456 | |
| 4457 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4458 | f1 = _fdopen(fd1, m2); |
| 4459 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4460 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4461 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4462 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4463 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4464 | PyFile_SetBufSize(p2, 0); |
| 4465 | |
| 4466 | if (n != 4) |
| 4467 | CloseHandle(hChildStderrRdDup); |
| 4468 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4469 | f = PyTuple_Pack(2,p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4470 | Py_XDECREF(p1); |
| 4471 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4472 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4473 | break; |
| 4474 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4475 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4476 | case POPEN_3: |
| 4477 | { |
| 4478 | char *m1, *m2; |
| 4479 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4480 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4481 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4482 | m1 = "r"; |
| 4483 | m2 = "w"; |
| 4484 | } else { |
| 4485 | m1 = "rb"; |
| 4486 | m2 = "wb"; |
| 4487 | } |
| 4488 | |
| 4489 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4490 | f1 = _fdopen(fd1, m2); |
| 4491 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4492 | f2 = _fdopen(fd2, m1); |
| 4493 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 4494 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4495 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4496 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 4497 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4498 | PyFile_SetBufSize(p1, 0); |
| 4499 | PyFile_SetBufSize(p2, 0); |
| 4500 | PyFile_SetBufSize(p3, 0); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4501 | f = PyTuple_Pack(3,p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4502 | Py_XDECREF(p1); |
| 4503 | Py_XDECREF(p2); |
| 4504 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4505 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4506 | break; |
| 4507 | } |
| 4508 | } |
| 4509 | |
| 4510 | if (n == POPEN_4) { |
| 4511 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4512 | hChildStdinRd, |
| 4513 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4514 | hChildStdoutWr, |
| 4515 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4516 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4517 | } |
| 4518 | else { |
| 4519 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4520 | hChildStdinRd, |
| 4521 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4522 | hChildStderrWr, |
| 4523 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4524 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4527 | /* |
| 4528 | * Insert the files we've created into the process dictionary |
| 4529 | * all referencing the list with the process handle and the |
| 4530 | * initial number of files (see description below in _PyPclose). |
| 4531 | * Since if _PyPclose later tried to wait on a process when all |
| 4532 | * handles weren't closed, it could create a deadlock with the |
| 4533 | * child, we spend some energy here to try to ensure that we |
| 4534 | * either insert all file handles into the dictionary or none |
| 4535 | * at all. It's a little clumsy with the various popen modes |
| 4536 | * and variable number of files involved. |
| 4537 | */ |
| 4538 | if (!_PyPopenProcs) { |
| 4539 | _PyPopenProcs = PyDict_New(); |
| 4540 | } |
| 4541 | |
| 4542 | if (_PyPopenProcs) { |
| 4543 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 4544 | int ins_rc[3]; |
| 4545 | |
| 4546 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4547 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4548 | |
| 4549 | procObj = PyList_New(2); |
| 4550 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 4551 | intObj = PyInt_FromLong(file_count); |
| 4552 | |
| 4553 | if (procObj && hProcessObj && intObj) { |
| 4554 | PyList_SetItem(procObj,0,hProcessObj); |
| 4555 | PyList_SetItem(procObj,1,intObj); |
| 4556 | |
| 4557 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 4558 | if (fileObj[0]) { |
| 4559 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4560 | fileObj[0], |
| 4561 | procObj); |
| 4562 | } |
| 4563 | if (file_count >= 2) { |
| 4564 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 4565 | if (fileObj[1]) { |
| 4566 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4567 | fileObj[1], |
| 4568 | procObj); |
| 4569 | } |
| 4570 | } |
| 4571 | if (file_count >= 3) { |
| 4572 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 4573 | if (fileObj[2]) { |
| 4574 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4575 | fileObj[2], |
| 4576 | procObj); |
| 4577 | } |
| 4578 | } |
| 4579 | |
| 4580 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4581 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4582 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 4583 | /* Something failed - remove any dictionary |
| 4584 | * entries that did make it. |
| 4585 | */ |
| 4586 | if (!ins_rc[0] && fileObj[0]) { |
| 4587 | PyDict_DelItem(_PyPopenProcs, |
| 4588 | fileObj[0]); |
| 4589 | } |
| 4590 | if (!ins_rc[1] && fileObj[1]) { |
| 4591 | PyDict_DelItem(_PyPopenProcs, |
| 4592 | fileObj[1]); |
| 4593 | } |
| 4594 | if (!ins_rc[2] && fileObj[2]) { |
| 4595 | PyDict_DelItem(_PyPopenProcs, |
| 4596 | fileObj[2]); |
| 4597 | } |
| 4598 | } |
| 4599 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4600 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4601 | /* |
| 4602 | * Clean up our localized references for the dictionary keys |
| 4603 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4604 | * that got placed in the dictionary. |
| 4605 | */ |
| 4606 | Py_XDECREF(procObj); |
| 4607 | Py_XDECREF(fileObj[0]); |
| 4608 | Py_XDECREF(fileObj[1]); |
| 4609 | Py_XDECREF(fileObj[2]); |
| 4610 | } |
| 4611 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4612 | /* Child is launched. Close the parents copy of those pipe |
| 4613 | * handles that only the child should have open. You need to |
| 4614 | * make sure that no handles to the write end of the output pipe |
| 4615 | * are maintained in this process or else the pipe will not close |
| 4616 | * when the child process exits and the ReadFile will hang. */ |
| 4617 | |
| 4618 | if (!CloseHandle(hChildStdinRd)) |
| 4619 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4620 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4621 | if (!CloseHandle(hChildStdoutWr)) |
| 4622 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4623 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4624 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 4625 | return win32_error("CloseHandle", NULL); |
| 4626 | |
| 4627 | return f; |
| 4628 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4629 | |
| 4630 | /* |
| 4631 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4632 | * 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] | 4633 | * |
| 4634 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4635 | * input file pointer to information about the process that was |
| 4636 | * originally created by the popen* call that created the file pointer. |
| 4637 | * The dictionary uses the file pointer as a key (with one entry |
| 4638 | * inserted for each file returned by the original popen* call) and a |
| 4639 | * single list object as the value for all files from a single call. |
| 4640 | * The list object contains the Win32 process handle at [0], and a file |
| 4641 | * count at [1], which is initialized to the total number of file |
| 4642 | * handles using that list. |
| 4643 | * |
| 4644 | * This function closes whichever handle it is passed, and decrements |
| 4645 | * the file count in the dictionary for the process handle pointed to |
| 4646 | * by this file. On the last close (when the file count reaches zero), |
| 4647 | * this function will wait for the child process and then return its |
| 4648 | * exit code as the result of the close() operation. This permits the |
| 4649 | * files to be closed in any order - it is always the close() of the |
| 4650 | * final handle that will return the exit code. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4651 | * |
| 4652 | * NOTE: This function is currently called with the GIL released. |
| 4653 | * hence we use the GILState API to manage our state. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4654 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4655 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4656 | static int _PyPclose(FILE *file) |
| 4657 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4658 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4659 | DWORD exit_code; |
| 4660 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4661 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 4662 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4663 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4664 | PyGILState_STATE state; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4665 | #endif |
| 4666 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4667 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4668 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4669 | */ |
| 4670 | result = fclose(file); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4671 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4672 | state = PyGILState_Ensure(); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4673 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4674 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4675 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4676 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4677 | fileObj)) != NULL && |
| 4678 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 4679 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 4680 | |
| 4681 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 4682 | file_count = PyInt_AsLong(intObj); |
| 4683 | |
| 4684 | if (file_count > 1) { |
| 4685 | /* Still other files referencing process */ |
| 4686 | file_count--; |
| 4687 | PyList_SetItem(procObj,1, |
| 4688 | PyInt_FromLong(file_count)); |
| 4689 | } else { |
| 4690 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4691 | if (result != EOF && |
| 4692 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 4693 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4694 | /* Possible truncation here in 16-bit environments, but |
| 4695 | * real exit codes are just the lower byte in any event. |
| 4696 | */ |
| 4697 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4698 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4699 | /* Indicate failure - this will cause the file object |
| 4700 | * to raise an I/O error and translate the last Win32 |
| 4701 | * error code from errno. We do have a problem with |
| 4702 | * last errors that overlap the normal errno table, |
| 4703 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4704 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4705 | if (result != EOF) { |
| 4706 | /* If the error wasn't from the fclose(), then |
| 4707 | * set errno for the file object error handling. |
| 4708 | */ |
| 4709 | errno = GetLastError(); |
| 4710 | } |
| 4711 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4712 | } |
| 4713 | |
| 4714 | /* Free up the native handle at this point */ |
| 4715 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4716 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4717 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4718 | /* Remove this file pointer from dictionary */ |
| 4719 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4720 | |
| 4721 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 4722 | Py_DECREF(_PyPopenProcs); |
| 4723 | _PyPopenProcs = NULL; |
| 4724 | } |
| 4725 | |
| 4726 | } /* if object retrieval ok */ |
| 4727 | |
| 4728 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4729 | } /* if _PyPopenProcs */ |
| 4730 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4731 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4732 | PyGILState_Release(state); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4733 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4734 | return result; |
| 4735 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4736 | |
| 4737 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4738 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4739 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4740 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4741 | char *name; |
| 4742 | char *mode = "r"; |
| 4743 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4744 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4745 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4746 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4747 | return NULL; |
Martin v. Löwis | 9ad853b | 2003-10-31 10:01:53 +0000 | [diff] [blame] | 4748 | /* Strip mode of binary or text modifiers */ |
| 4749 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) |
| 4750 | mode = "r"; |
| 4751 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) |
| 4752 | mode = "w"; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4753 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4754 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4755 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4756 | if (fp == NULL) |
| 4757 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4758 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4759 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4760 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4761 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4762 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4763 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4764 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4765 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4766 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4767 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4768 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4769 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4770 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4771 | Set the current process's user id."); |
| 4772 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4773 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4774 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4775 | { |
| 4776 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4777 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4778 | return NULL; |
| 4779 | if (setuid(uid) < 0) |
| 4780 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4781 | Py_INCREF(Py_None); |
| 4782 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4783 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4784 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4785 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4786 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4787 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4788 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4789 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4790 | Set the current process's effective user id."); |
| 4791 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4792 | static PyObject * |
| 4793 | posix_seteuid (PyObject *self, PyObject *args) |
| 4794 | { |
| 4795 | int euid; |
| 4796 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 4797 | return NULL; |
| 4798 | } else if (seteuid(euid) < 0) { |
| 4799 | return posix_error(); |
| 4800 | } else { |
| 4801 | Py_INCREF(Py_None); |
| 4802 | return Py_None; |
| 4803 | } |
| 4804 | } |
| 4805 | #endif /* HAVE_SETEUID */ |
| 4806 | |
| 4807 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4808 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4809 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4810 | Set the current process's effective group id."); |
| 4811 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4812 | static PyObject * |
| 4813 | posix_setegid (PyObject *self, PyObject *args) |
| 4814 | { |
| 4815 | int egid; |
| 4816 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 4817 | return NULL; |
| 4818 | } else if (setegid(egid) < 0) { |
| 4819 | return posix_error(); |
| 4820 | } else { |
| 4821 | Py_INCREF(Py_None); |
| 4822 | return Py_None; |
| 4823 | } |
| 4824 | } |
| 4825 | #endif /* HAVE_SETEGID */ |
| 4826 | |
| 4827 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4828 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4829 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4830 | Set the current process's real and effective user ids."); |
| 4831 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4832 | static PyObject * |
| 4833 | posix_setreuid (PyObject *self, PyObject *args) |
| 4834 | { |
| 4835 | int ruid, euid; |
| 4836 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4837 | return NULL; |
| 4838 | } else if (setreuid(ruid, euid) < 0) { |
| 4839 | return posix_error(); |
| 4840 | } else { |
| 4841 | Py_INCREF(Py_None); |
| 4842 | return Py_None; |
| 4843 | } |
| 4844 | } |
| 4845 | #endif /* HAVE_SETREUID */ |
| 4846 | |
| 4847 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4848 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4849 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4850 | Set the current process's real and effective group ids."); |
| 4851 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4852 | static PyObject * |
| 4853 | posix_setregid (PyObject *self, PyObject *args) |
| 4854 | { |
| 4855 | int rgid, egid; |
| 4856 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 4857 | return NULL; |
| 4858 | } else if (setregid(rgid, egid) < 0) { |
| 4859 | return posix_error(); |
| 4860 | } else { |
| 4861 | Py_INCREF(Py_None); |
| 4862 | return Py_None; |
| 4863 | } |
| 4864 | } |
| 4865 | #endif /* HAVE_SETREGID */ |
| 4866 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4867 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4868 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4869 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4870 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4871 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4872 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4873 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4874 | { |
| 4875 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4876 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4877 | return NULL; |
| 4878 | if (setgid(gid) < 0) |
| 4879 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4880 | Py_INCREF(Py_None); |
| 4881 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4882 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4883 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4884 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4885 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4886 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4887 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4888 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4889 | |
| 4890 | static PyObject * |
| 4891 | posix_setgroups(PyObject *self, PyObject *args) |
| 4892 | { |
| 4893 | PyObject *groups; |
| 4894 | int i, len; |
| 4895 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4896 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4897 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 4898 | return NULL; |
| 4899 | if (!PySequence_Check(groups)) { |
| 4900 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4901 | return NULL; |
| 4902 | } |
| 4903 | len = PySequence_Size(groups); |
| 4904 | if (len > MAX_GROUPS) { |
| 4905 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4906 | return NULL; |
| 4907 | } |
| 4908 | for(i = 0; i < len; i++) { |
| 4909 | PyObject *elem; |
| 4910 | elem = PySequence_GetItem(groups, i); |
| 4911 | if (!elem) |
| 4912 | return NULL; |
| 4913 | if (!PyInt_Check(elem)) { |
| 4914 | PyErr_SetString(PyExc_TypeError, |
| 4915 | "groups must be integers"); |
| 4916 | Py_DECREF(elem); |
| 4917 | return NULL; |
| 4918 | } |
| 4919 | /* XXX: check that value fits into gid_t. */ |
| 4920 | grouplist[i] = PyInt_AsLong(elem); |
| 4921 | Py_DECREF(elem); |
| 4922 | } |
| 4923 | |
| 4924 | if (setgroups(len, grouplist) < 0) |
| 4925 | return posix_error(); |
| 4926 | Py_INCREF(Py_None); |
| 4927 | return Py_None; |
| 4928 | } |
| 4929 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4930 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4931 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4932 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4933 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4934 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4935 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4936 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4937 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4938 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4939 | int pid, options; |
| 4940 | #ifdef UNION_WAIT |
| 4941 | union wait status; |
| 4942 | #define status_i (status.w_status) |
| 4943 | #else |
| 4944 | int status; |
| 4945 | #define status_i status |
| 4946 | #endif |
| 4947 | status_i = 0; |
| 4948 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4949 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4950 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4951 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4952 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4953 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4954 | if (pid == -1) |
| 4955 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4956 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4957 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4960 | #elif defined(HAVE_CWAIT) |
| 4961 | |
| 4962 | /* 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] | 4963 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4964 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4965 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4966 | |
| 4967 | static PyObject * |
| 4968 | posix_waitpid(PyObject *self, PyObject *args) |
| 4969 | { |
| 4970 | int pid, options; |
| 4971 | int status; |
| 4972 | |
| 4973 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4974 | return NULL; |
| 4975 | Py_BEGIN_ALLOW_THREADS |
| 4976 | pid = _cwait(&status, pid, options); |
| 4977 | Py_END_ALLOW_THREADS |
| 4978 | if (pid == -1) |
| 4979 | return posix_error(); |
| 4980 | else |
| 4981 | /* shift the status left a byte so this is more like the |
| 4982 | POSIX waitpid */ |
| 4983 | return Py_BuildValue("ii", pid, status << 8); |
| 4984 | } |
| 4985 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4986 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4987 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4988 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4989 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4990 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4991 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4992 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4993 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4994 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4995 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4996 | #ifdef UNION_WAIT |
| 4997 | union wait status; |
| 4998 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4999 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5000 | int status; |
| 5001 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 5002 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5003 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5004 | status_i = 0; |
| 5005 | Py_BEGIN_ALLOW_THREADS |
| 5006 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5007 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5008 | if (pid == -1) |
| 5009 | return posix_error(); |
| 5010 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5011 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5012 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5013 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5014 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5015 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5016 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5017 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5018 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5019 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5020 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5021 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5022 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5023 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5024 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5025 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5026 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5027 | #ifdef MS_WINDOWS |
Mark Hammond | 7edd0a9 | 2003-08-06 02:46:58 +0000 | [diff] [blame] | 5028 | return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", _wstati64); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5029 | #else |
| 5030 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 5031 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5032 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5033 | } |
| 5034 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5035 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5036 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5037 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5038 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5039 | 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] | 5040 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5041 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5042 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5043 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5044 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 5045 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5046 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5047 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5048 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5049 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 5050 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5051 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5052 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 5053 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5054 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5055 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5056 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5057 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5058 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5059 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5060 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5061 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 5062 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5063 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5064 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5065 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5066 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5067 | return posix_2str(args, "etet:symlink", symlink, NULL, NULL); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5068 | } |
| 5069 | #endif /* HAVE_SYMLINK */ |
| 5070 | |
| 5071 | |
| 5072 | #ifdef HAVE_TIMES |
| 5073 | #ifndef HZ |
| 5074 | #define HZ 60 /* Universal constant :-) */ |
| 5075 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5076 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5077 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 5078 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5079 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5080 | { |
| 5081 | ULONG value = 0; |
| 5082 | |
| 5083 | Py_BEGIN_ALLOW_THREADS |
| 5084 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 5085 | Py_END_ALLOW_THREADS |
| 5086 | |
| 5087 | return value; |
| 5088 | } |
| 5089 | |
| 5090 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5091 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5092 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5093 | /* Currently Only Uptime is Provided -- Others Later */ |
| 5094 | return Py_BuildValue("ddddd", |
| 5095 | (double)0 /* t.tms_utime / HZ */, |
| 5096 | (double)0 /* t.tms_stime / HZ */, |
| 5097 | (double)0 /* t.tms_cutime / HZ */, |
| 5098 | (double)0 /* t.tms_cstime / HZ */, |
| 5099 | (double)system_uptime() / 1000); |
| 5100 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5101 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5102 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5103 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5104 | { |
| 5105 | struct tms t; |
| 5106 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5107 | errno = 0; |
| 5108 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5109 | if (c == (clock_t) -1) |
| 5110 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5111 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5112 | (double)t.tms_utime / HZ, |
| 5113 | (double)t.tms_stime / HZ, |
| 5114 | (double)t.tms_cutime / HZ, |
| 5115 | (double)t.tms_cstime / HZ, |
| 5116 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5117 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5118 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5119 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5120 | |
| 5121 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5122 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5123 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5124 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5125 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5126 | { |
| 5127 | FILETIME create, exit, kernel, user; |
| 5128 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5129 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5130 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 5131 | /* The fields of a FILETIME structure are the hi and lo part |
| 5132 | of a 64-bit value expressed in 100 nanosecond units. |
| 5133 | 1e7 is one second in such units; 1e-7 the inverse. |
| 5134 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 5135 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5136 | return Py_BuildValue( |
| 5137 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5138 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 5139 | kernel.dwLowDateTime*1e-7), |
| 5140 | (double)(user.dwHighDateTime*429.4967296 + |
| 5141 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5142 | (double)0, |
| 5143 | (double)0, |
| 5144 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5145 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5146 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5147 | |
| 5148 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5149 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5150 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5151 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5152 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5153 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5154 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5155 | #ifdef HAVE_GETSID |
| 5156 | PyDoc_STRVAR(posix_getsid__doc__, |
| 5157 | "getsid(pid) -> sid\n\n\ |
| 5158 | Call the system call getsid()."); |
| 5159 | |
| 5160 | static PyObject * |
| 5161 | posix_getsid(PyObject *self, PyObject *args) |
| 5162 | { |
| 5163 | int pid, sid; |
| 5164 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 5165 | return NULL; |
| 5166 | sid = getsid(pid); |
| 5167 | if (sid < 0) |
| 5168 | return posix_error(); |
| 5169 | return PyInt_FromLong((long)sid); |
| 5170 | } |
| 5171 | #endif /* HAVE_GETSID */ |
| 5172 | |
| 5173 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5174 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5175 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5176 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5177 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5178 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5179 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5180 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5181 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5182 | if (setsid() < 0) |
| 5183 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5184 | Py_INCREF(Py_None); |
| 5185 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5186 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5187 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5188 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5189 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5190 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5191 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5192 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5193 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5194 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5195 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5196 | { |
| 5197 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5198 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5199 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5200 | if (setpgid(pid, pgrp) < 0) |
| 5201 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5202 | Py_INCREF(Py_None); |
| 5203 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5204 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5205 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5206 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5207 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5208 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5209 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5210 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5211 | 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] | 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_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5215 | { |
| 5216 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5217 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5218 | return NULL; |
| 5219 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5220 | if (pgid < 0) |
| 5221 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5222 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5223 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5224 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5225 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5226 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5227 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5228 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5229 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5230 | 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] | 5231 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5232 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5233 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5234 | { |
| 5235 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5236 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5237 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5238 | if (tcsetpgrp(fd, pgid) < 0) |
| 5239 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5240 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5241 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5242 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5243 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5244 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5245 | /* Functions acting on file descriptors */ |
| 5246 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5247 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5248 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5249 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5250 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5251 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5252 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5253 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5254 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5255 | int flag; |
| 5256 | int mode = 0777; |
| 5257 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5258 | |
| 5259 | #ifdef MS_WINDOWS |
| 5260 | if (unicode_file_names()) { |
| 5261 | PyUnicodeObject *po; |
| 5262 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 5263 | Py_BEGIN_ALLOW_THREADS |
| 5264 | /* PyUnicode_AS_UNICODE OK without thread |
| 5265 | lock as it is a simple dereference. */ |
| 5266 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 5267 | Py_END_ALLOW_THREADS |
| 5268 | if (fd < 0) |
| 5269 | return posix_error(); |
| 5270 | return PyInt_FromLong((long)fd); |
| 5271 | } |
| 5272 | /* Drop the argument parsing error as narrow strings |
| 5273 | are also valid. */ |
| 5274 | PyErr_Clear(); |
| 5275 | } |
| 5276 | #endif |
| 5277 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5278 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5279 | Py_FileSystemDefaultEncoding, &file, |
| 5280 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5281 | return NULL; |
| 5282 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5283 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5284 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5285 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5286 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5287 | return posix_error_with_allocated_filename(file); |
| 5288 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5289 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5290 | } |
| 5291 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5292 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5293 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5294 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5295 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5296 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5297 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5298 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5299 | { |
| 5300 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5301 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5302 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5303 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5304 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5305 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5306 | if (res < 0) |
| 5307 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5308 | Py_INCREF(Py_None); |
| 5309 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5310 | } |
| 5311 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5312 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5313 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5314 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5315 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5316 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5317 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5318 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5319 | { |
| 5320 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5321 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5322 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5323 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5324 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5325 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5326 | if (fd < 0) |
| 5327 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5328 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5329 | } |
| 5330 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5331 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5332 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5333 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5334 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5335 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5336 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5337 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5338 | { |
| 5339 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5340 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5341 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5342 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5343 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5344 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5345 | if (res < 0) |
| 5346 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5347 | Py_INCREF(Py_None); |
| 5348 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5349 | } |
| 5350 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5351 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5352 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5353 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5354 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5355 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5356 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5357 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5358 | { |
| 5359 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5360 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5361 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5362 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5363 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5364 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5365 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5366 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5367 | return NULL; |
| 5368 | #ifdef SEEK_SET |
| 5369 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5370 | switch (how) { |
| 5371 | case 0: how = SEEK_SET; break; |
| 5372 | case 1: how = SEEK_CUR; break; |
| 5373 | case 2: how = SEEK_END; break; |
| 5374 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5375 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5376 | |
| 5377 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5378 | pos = PyInt_AsLong(posobj); |
| 5379 | #else |
| 5380 | pos = PyLong_Check(posobj) ? |
| 5381 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 5382 | #endif |
| 5383 | if (PyErr_Occurred()) |
| 5384 | return NULL; |
| 5385 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5386 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5387 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5388 | res = _lseeki64(fd, pos, how); |
| 5389 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5390 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5391 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5392 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5393 | if (res < 0) |
| 5394 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5395 | |
| 5396 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5397 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5398 | #else |
| 5399 | return PyLong_FromLongLong(res); |
| 5400 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5401 | } |
| 5402 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5403 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5404 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5405 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5406 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5407 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5408 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5409 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5410 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5411 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5412 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5413 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5414 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 5415 | if (size < 0) { |
| 5416 | errno = EINVAL; |
| 5417 | return posix_error(); |
| 5418 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5419 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5420 | if (buffer == NULL) |
| 5421 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5422 | Py_BEGIN_ALLOW_THREADS |
| 5423 | n = read(fd, PyString_AsString(buffer), size); |
| 5424 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5425 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5426 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5427 | return posix_error(); |
| 5428 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5429 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5430 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5431 | return buffer; |
| 5432 | } |
| 5433 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5434 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5435 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5436 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5437 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5438 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5439 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5440 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5441 | { |
| 5442 | int fd, size; |
| 5443 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5444 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5445 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5446 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5447 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5448 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5449 | if (size < 0) |
| 5450 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5451 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5454 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5455 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5456 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5457 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5458 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5459 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5460 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5461 | { |
| 5462 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5463 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5464 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5465 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5466 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5467 | #ifdef __VMS |
| 5468 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5469 | fsync(fd); |
| 5470 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5471 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5472 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5473 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5474 | if (res != 0) |
| 5475 | return posix_error(); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5476 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5477 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5478 | } |
| 5479 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5480 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5481 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5482 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5483 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5484 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5485 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5486 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5487 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5488 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5489 | char *mode = "r"; |
| 5490 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5491 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5492 | PyObject *f; |
| 5493 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5494 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5495 | |
Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 5496 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 5497 | PyErr_Format(PyExc_ValueError, |
| 5498 | "invalid file mode '%s'", mode); |
| 5499 | return NULL; |
| 5500 | } |
| 5501 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5502 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5503 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5504 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5505 | if (fp == NULL) |
| 5506 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 5507 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5508 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5509 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5510 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5511 | } |
| 5512 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5513 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5514 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5515 | 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] | 5516 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5517 | |
| 5518 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5519 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5520 | { |
| 5521 | int fd; |
| 5522 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5523 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5524 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5525 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5526 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5527 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5528 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5529 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5530 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5531 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5532 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5533 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5534 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5535 | #if defined(PYOS_OS2) |
| 5536 | HFILE read, write; |
| 5537 | APIRET rc; |
| 5538 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5539 | Py_BEGIN_ALLOW_THREADS |
| 5540 | rc = DosCreatePipe( &read, &write, 4096); |
| 5541 | Py_END_ALLOW_THREADS |
| 5542 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5543 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5544 | |
| 5545 | return Py_BuildValue("(ii)", read, write); |
| 5546 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5547 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5548 | int fds[2]; |
| 5549 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5550 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5551 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5552 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5553 | if (res != 0) |
| 5554 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5555 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5556 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5557 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5558 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5559 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5560 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5561 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5562 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5563 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5564 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5565 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5566 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5567 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5568 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5569 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5570 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5571 | #endif /* HAVE_PIPE */ |
| 5572 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5573 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5574 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5575 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5576 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5577 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5578 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5579 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5580 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5581 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5582 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5583 | int mode = 0666; |
| 5584 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5585 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5586 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5587 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5588 | res = mkfifo(filename, mode); |
| 5589 | Py_END_ALLOW_THREADS |
| 5590 | if (res < 0) |
| 5591 | return posix_error(); |
| 5592 | Py_INCREF(Py_None); |
| 5593 | return Py_None; |
| 5594 | } |
| 5595 | #endif |
| 5596 | |
| 5597 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5598 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5599 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5600 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5601 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5602 | named filename. mode specifies both the permissions to use and the\n\ |
| 5603 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5604 | 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] | 5605 | device defines the newly created device special file (probably using\n\ |
| 5606 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5607 | |
| 5608 | |
| 5609 | static PyObject * |
| 5610 | posix_mknod(PyObject *self, PyObject *args) |
| 5611 | { |
| 5612 | char *filename; |
| 5613 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5614 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5615 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5616 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5617 | return NULL; |
| 5618 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5619 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5620 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5621 | if (res < 0) |
| 5622 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5623 | Py_INCREF(Py_None); |
| 5624 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5625 | } |
| 5626 | #endif |
| 5627 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5628 | #ifdef HAVE_DEVICE_MACROS |
| 5629 | PyDoc_STRVAR(posix_major__doc__, |
| 5630 | "major(device) -> major number\n\ |
| 5631 | Extracts a device major number from a raw device number."); |
| 5632 | |
| 5633 | static PyObject * |
| 5634 | posix_major(PyObject *self, PyObject *args) |
| 5635 | { |
| 5636 | int device; |
| 5637 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5638 | return NULL; |
| 5639 | return PyInt_FromLong((long)major(device)); |
| 5640 | } |
| 5641 | |
| 5642 | PyDoc_STRVAR(posix_minor__doc__, |
| 5643 | "minor(device) -> minor number\n\ |
| 5644 | Extracts a device minor number from a raw device number."); |
| 5645 | |
| 5646 | static PyObject * |
| 5647 | posix_minor(PyObject *self, PyObject *args) |
| 5648 | { |
| 5649 | int device; |
| 5650 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5651 | return NULL; |
| 5652 | return PyInt_FromLong((long)minor(device)); |
| 5653 | } |
| 5654 | |
| 5655 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5656 | "makedev(major, minor) -> device number\n\ |
| 5657 | Composes a raw device number from the major and minor device numbers."); |
| 5658 | |
| 5659 | static PyObject * |
| 5660 | posix_makedev(PyObject *self, PyObject *args) |
| 5661 | { |
| 5662 | int major, minor; |
| 5663 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5664 | return NULL; |
| 5665 | return PyInt_FromLong((long)makedev(major, minor)); |
| 5666 | } |
| 5667 | #endif /* device macros */ |
| 5668 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5669 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5670 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5671 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5672 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5673 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5674 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5675 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5676 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5677 | { |
| 5678 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5679 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5680 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5681 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5682 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5683 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5684 | return NULL; |
| 5685 | |
| 5686 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5687 | length = PyInt_AsLong(lenobj); |
| 5688 | #else |
| 5689 | length = PyLong_Check(lenobj) ? |
| 5690 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 5691 | #endif |
| 5692 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5693 | return NULL; |
| 5694 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5695 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5696 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5697 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5698 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5699 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5700 | return NULL; |
| 5701 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5702 | Py_INCREF(Py_None); |
| 5703 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5704 | } |
| 5705 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5706 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5707 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5708 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5709 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5710 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5711 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5712 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5713 | * get re-set with another call for the same key. */ |
| 5714 | static PyObject *posix_putenv_garbage; |
| 5715 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5716 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5717 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5718 | { |
| 5719 | char *s1, *s2; |
| 5720 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5721 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5722 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5723 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5724 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5725 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5726 | |
| 5727 | #if defined(PYOS_OS2) |
| 5728 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5729 | APIRET rc; |
| 5730 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5731 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5732 | if (rc != NO_ERROR) |
| 5733 | return os2_error(rc); |
| 5734 | |
| 5735 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5736 | APIRET rc; |
| 5737 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5738 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5739 | if (rc != NO_ERROR) |
| 5740 | return os2_error(rc); |
| 5741 | } else { |
| 5742 | #endif |
| 5743 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5744 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5745 | len = strlen(s1) + strlen(s2) + 2; |
| 5746 | /* len includes space for a trailing \0; the size arg to |
| 5747 | PyString_FromStringAndSize does not count that */ |
| 5748 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5749 | if (newstr == NULL) |
| 5750 | return PyErr_NoMemory(); |
| 5751 | new = PyString_AS_STRING(newstr); |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5752 | PyOS_snprintf(new, len, "%s=%s", s1, s2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5753 | if (putenv(new)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5754 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5755 | posix_error(); |
| 5756 | return NULL; |
| 5757 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5758 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5759 | * this will cause previous value to be collected. This has to |
| 5760 | * happen after the real putenv() call because the old value |
| 5761 | * was still accessible until then. */ |
| 5762 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5763 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5764 | /* really not much we can do; just leak */ |
| 5765 | PyErr_Clear(); |
| 5766 | } |
| 5767 | else { |
| 5768 | Py_DECREF(newstr); |
| 5769 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5770 | |
| 5771 | #if defined(PYOS_OS2) |
| 5772 | } |
| 5773 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5774 | Py_INCREF(Py_None); |
| 5775 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5776 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5777 | #endif /* putenv */ |
| 5778 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5779 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5780 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5781 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5782 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5783 | |
| 5784 | static PyObject * |
| 5785 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5786 | { |
| 5787 | char *s1; |
| 5788 | |
| 5789 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5790 | return NULL; |
| 5791 | |
| 5792 | unsetenv(s1); |
| 5793 | |
| 5794 | /* Remove the key from posix_putenv_garbage; |
| 5795 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5796 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5797 | * old value was still accessible until then. |
| 5798 | */ |
| 5799 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5800 | PyTuple_GET_ITEM(args, 0))) { |
| 5801 | /* really not much we can do; just leak */ |
| 5802 | PyErr_Clear(); |
| 5803 | } |
| 5804 | |
| 5805 | Py_INCREF(Py_None); |
| 5806 | return Py_None; |
| 5807 | } |
| 5808 | #endif /* unsetenv */ |
| 5809 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5810 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5811 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5812 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5813 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5814 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5815 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5816 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5817 | { |
| 5818 | int code; |
| 5819 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5820 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5821 | return NULL; |
| 5822 | message = strerror(code); |
| 5823 | if (message == NULL) { |
| 5824 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5825 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5826 | return NULL; |
| 5827 | } |
| 5828 | return PyString_FromString(message); |
| 5829 | } |
| 5830 | #endif /* strerror */ |
| 5831 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5832 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5833 | #ifdef HAVE_SYS_WAIT_H |
| 5834 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5835 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5836 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5837 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5838 | 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] | 5839 | |
| 5840 | static PyObject * |
| 5841 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5842 | { |
| 5843 | #ifdef UNION_WAIT |
| 5844 | union wait status; |
| 5845 | #define status_i (status.w_status) |
| 5846 | #else |
| 5847 | int status; |
| 5848 | #define status_i status |
| 5849 | #endif |
| 5850 | status_i = 0; |
| 5851 | |
| 5852 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i)) |
| 5853 | { |
| 5854 | return NULL; |
| 5855 | } |
| 5856 | |
| 5857 | return PyBool_FromLong(WCOREDUMP(status)); |
| 5858 | #undef status_i |
| 5859 | } |
| 5860 | #endif /* WCOREDUMP */ |
| 5861 | |
| 5862 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5863 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5864 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5865 | 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] | 5866 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5867 | |
| 5868 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5869 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5870 | { |
| 5871 | #ifdef UNION_WAIT |
| 5872 | union wait status; |
| 5873 | #define status_i (status.w_status) |
| 5874 | #else |
| 5875 | int status; |
| 5876 | #define status_i status |
| 5877 | #endif |
| 5878 | status_i = 0; |
| 5879 | |
| 5880 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i)) |
| 5881 | { |
| 5882 | return NULL; |
| 5883 | } |
| 5884 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5885 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5886 | #undef status_i |
| 5887 | } |
| 5888 | #endif /* WIFCONTINUED */ |
| 5889 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5890 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5891 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5892 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5893 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5894 | |
| 5895 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5896 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5897 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5898 | #ifdef UNION_WAIT |
| 5899 | union wait status; |
| 5900 | #define status_i (status.w_status) |
| 5901 | #else |
| 5902 | int status; |
| 5903 | #define status_i status |
| 5904 | #endif |
| 5905 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5906 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5907 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5908 | { |
| 5909 | return NULL; |
| 5910 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5911 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5912 | return PyBool_FromLong(WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5913 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5914 | } |
| 5915 | #endif /* WIFSTOPPED */ |
| 5916 | |
| 5917 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5918 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5919 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5920 | 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] | 5921 | |
| 5922 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5923 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5924 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5925 | #ifdef UNION_WAIT |
| 5926 | union wait status; |
| 5927 | #define status_i (status.w_status) |
| 5928 | #else |
| 5929 | int status; |
| 5930 | #define status_i status |
| 5931 | #endif |
| 5932 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5933 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5934 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5935 | { |
| 5936 | return NULL; |
| 5937 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5938 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5939 | return PyBool_FromLong(WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5940 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5941 | } |
| 5942 | #endif /* WIFSIGNALED */ |
| 5943 | |
| 5944 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5945 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5946 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5947 | 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] | 5948 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5949 | |
| 5950 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5951 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5952 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5953 | #ifdef UNION_WAIT |
| 5954 | union wait status; |
| 5955 | #define status_i (status.w_status) |
| 5956 | #else |
| 5957 | int status; |
| 5958 | #define status_i status |
| 5959 | #endif |
| 5960 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5961 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5962 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5963 | { |
| 5964 | return NULL; |
| 5965 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5966 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5967 | return PyBool_FromLong(WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5968 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5969 | } |
| 5970 | #endif /* WIFEXITED */ |
| 5971 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5972 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5973 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5974 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5975 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5976 | |
| 5977 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5978 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5979 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5980 | #ifdef UNION_WAIT |
| 5981 | union wait status; |
| 5982 | #define status_i (status.w_status) |
| 5983 | #else |
| 5984 | int status; |
| 5985 | #define status_i status |
| 5986 | #endif |
| 5987 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5988 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5989 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5990 | { |
| 5991 | return NULL; |
| 5992 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5993 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5994 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5995 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5996 | } |
| 5997 | #endif /* WEXITSTATUS */ |
| 5998 | |
| 5999 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6000 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6001 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6002 | 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] | 6003 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6004 | |
| 6005 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6006 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6007 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6008 | #ifdef UNION_WAIT |
| 6009 | union wait status; |
| 6010 | #define status_i (status.w_status) |
| 6011 | #else |
| 6012 | int status; |
| 6013 | #define status_i status |
| 6014 | #endif |
| 6015 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6016 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6017 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6018 | { |
| 6019 | return NULL; |
| 6020 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6021 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6022 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6023 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6024 | } |
| 6025 | #endif /* WTERMSIG */ |
| 6026 | |
| 6027 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6028 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6029 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6030 | Return the signal that stopped the process that provided\n\ |
| 6031 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6032 | |
| 6033 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6034 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6035 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6036 | #ifdef UNION_WAIT |
| 6037 | union wait status; |
| 6038 | #define status_i (status.w_status) |
| 6039 | #else |
| 6040 | int status; |
| 6041 | #define status_i status |
| 6042 | #endif |
| 6043 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6044 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6045 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6046 | { |
| 6047 | return NULL; |
| 6048 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6049 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6050 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6051 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6052 | } |
| 6053 | #endif /* WSTOPSIG */ |
| 6054 | |
| 6055 | #endif /* HAVE_SYS_WAIT_H */ |
| 6056 | |
| 6057 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6058 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 6059 | #ifdef _SCO_DS |
| 6060 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 6061 | needed definitions in sys/statvfs.h */ |
| 6062 | #define _SVID3 |
| 6063 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6064 | #include <sys/statvfs.h> |
| 6065 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6066 | static PyObject* |
| 6067 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 6068 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 6069 | if (v == NULL) |
| 6070 | return NULL; |
| 6071 | |
| 6072 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6073 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6074 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 6075 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 6076 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 6077 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 6078 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 6079 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 6080 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 6081 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6082 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6083 | #else |
| 6084 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6085 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6086 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6087 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6088 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6089 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6090 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6091 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6092 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6093 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6094 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6095 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6096 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6097 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6098 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6099 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6100 | #endif |
| 6101 | |
| 6102 | return v; |
| 6103 | } |
| 6104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6105 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6106 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6107 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6108 | |
| 6109 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6110 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6111 | { |
| 6112 | int fd, res; |
| 6113 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6114 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6115 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6116 | return NULL; |
| 6117 | Py_BEGIN_ALLOW_THREADS |
| 6118 | res = fstatvfs(fd, &st); |
| 6119 | Py_END_ALLOW_THREADS |
| 6120 | if (res != 0) |
| 6121 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6122 | |
| 6123 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6124 | } |
| 6125 | #endif /* HAVE_FSTATVFS */ |
| 6126 | |
| 6127 | |
| 6128 | #if defined(HAVE_STATVFS) |
| 6129 | #include <sys/statvfs.h> |
| 6130 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6131 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6132 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6133 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6134 | |
| 6135 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6136 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6137 | { |
| 6138 | char *path; |
| 6139 | int res; |
| 6140 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6141 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6142 | return NULL; |
| 6143 | Py_BEGIN_ALLOW_THREADS |
| 6144 | res = statvfs(path, &st); |
| 6145 | Py_END_ALLOW_THREADS |
| 6146 | if (res != 0) |
| 6147 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6148 | |
| 6149 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6150 | } |
| 6151 | #endif /* HAVE_STATVFS */ |
| 6152 | |
| 6153 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6154 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6155 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6156 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6157 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 6158 | 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] | 6159 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6160 | |
| 6161 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6162 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6163 | { |
| 6164 | PyObject *result = NULL; |
| 6165 | char *dir = NULL; |
| 6166 | char *pfx = NULL; |
| 6167 | char *name; |
| 6168 | |
| 6169 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 6170 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6171 | |
| 6172 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6173 | "tempnam is a potential security risk to your program") < 0) |
| 6174 | return NULL; |
| 6175 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6176 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6177 | name = _tempnam(dir, pfx); |
| 6178 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6179 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6180 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6181 | if (name == NULL) |
| 6182 | return PyErr_NoMemory(); |
| 6183 | result = PyString_FromString(name); |
| 6184 | free(name); |
| 6185 | return result; |
| 6186 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 6187 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6188 | |
| 6189 | |
| 6190 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6191 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6192 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6193 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6194 | |
| 6195 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6196 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6197 | { |
| 6198 | FILE *fp; |
| 6199 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6200 | fp = tmpfile(); |
| 6201 | if (fp == NULL) |
| 6202 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 6203 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6204 | } |
| 6205 | #endif |
| 6206 | |
| 6207 | |
| 6208 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6209 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6210 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6211 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6212 | |
| 6213 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6214 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6215 | { |
| 6216 | char buffer[L_tmpnam]; |
| 6217 | char *name; |
| 6218 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6219 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6220 | "tmpnam is a potential security risk to your program") < 0) |
| 6221 | return NULL; |
| 6222 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6223 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6224 | name = tmpnam_r(buffer); |
| 6225 | #else |
| 6226 | name = tmpnam(buffer); |
| 6227 | #endif |
| 6228 | if (name == NULL) { |
| 6229 | PyErr_SetObject(PyExc_OSError, |
| 6230 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6231 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6232 | "unexpected NULL from tmpnam_r" |
| 6233 | #else |
| 6234 | "unexpected NULL from tmpnam" |
| 6235 | #endif |
| 6236 | )); |
| 6237 | return NULL; |
| 6238 | } |
| 6239 | return PyString_FromString(buffer); |
| 6240 | } |
| 6241 | #endif |
| 6242 | |
| 6243 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6244 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 6245 | * It maps strings representing configuration variable names to |
| 6246 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6247 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6248 | * rarely-used constants. There are three separate tables that use |
| 6249 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6250 | * |
| 6251 | * This code is always included, even if none of the interfaces that |
| 6252 | * need it are included. The #if hackery needed to avoid it would be |
| 6253 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6254 | */ |
| 6255 | struct constdef { |
| 6256 | char *name; |
| 6257 | long value; |
| 6258 | }; |
| 6259 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6260 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6261 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 6262 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6263 | { |
| 6264 | if (PyInt_Check(arg)) { |
| 6265 | *valuep = PyInt_AS_LONG(arg); |
| 6266 | return 1; |
| 6267 | } |
| 6268 | if (PyString_Check(arg)) { |
| 6269 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6270 | size_t lo = 0; |
| 6271 | size_t mid; |
| 6272 | size_t hi = tablesize; |
| 6273 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6274 | char *confname = PyString_AS_STRING(arg); |
| 6275 | while (lo < hi) { |
| 6276 | mid = (lo + hi) / 2; |
| 6277 | cmp = strcmp(confname, table[mid].name); |
| 6278 | if (cmp < 0) |
| 6279 | hi = mid; |
| 6280 | else if (cmp > 0) |
| 6281 | lo = mid + 1; |
| 6282 | else { |
| 6283 | *valuep = table[mid].value; |
| 6284 | return 1; |
| 6285 | } |
| 6286 | } |
| 6287 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 6288 | } |
| 6289 | else |
| 6290 | PyErr_SetString(PyExc_TypeError, |
| 6291 | "configuration names must be strings or integers"); |
| 6292 | return 0; |
| 6293 | } |
| 6294 | |
| 6295 | |
| 6296 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 6297 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6298 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 6299 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 6300 | #endif |
| 6301 | #ifdef _PC_ABI_ASYNC_IO |
| 6302 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 6303 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6304 | #ifdef _PC_ASYNC_IO |
| 6305 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 6306 | #endif |
| 6307 | #ifdef _PC_CHOWN_RESTRICTED |
| 6308 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 6309 | #endif |
| 6310 | #ifdef _PC_FILESIZEBITS |
| 6311 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 6312 | #endif |
| 6313 | #ifdef _PC_LAST |
| 6314 | {"PC_LAST", _PC_LAST}, |
| 6315 | #endif |
| 6316 | #ifdef _PC_LINK_MAX |
| 6317 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 6318 | #endif |
| 6319 | #ifdef _PC_MAX_CANON |
| 6320 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 6321 | #endif |
| 6322 | #ifdef _PC_MAX_INPUT |
| 6323 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 6324 | #endif |
| 6325 | #ifdef _PC_NAME_MAX |
| 6326 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 6327 | #endif |
| 6328 | #ifdef _PC_NO_TRUNC |
| 6329 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 6330 | #endif |
| 6331 | #ifdef _PC_PATH_MAX |
| 6332 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 6333 | #endif |
| 6334 | #ifdef _PC_PIPE_BUF |
| 6335 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 6336 | #endif |
| 6337 | #ifdef _PC_PRIO_IO |
| 6338 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 6339 | #endif |
| 6340 | #ifdef _PC_SOCK_MAXBUF |
| 6341 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 6342 | #endif |
| 6343 | #ifdef _PC_SYNC_IO |
| 6344 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 6345 | #endif |
| 6346 | #ifdef _PC_VDISABLE |
| 6347 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 6348 | #endif |
| 6349 | }; |
| 6350 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6351 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6352 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6353 | { |
| 6354 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 6355 | sizeof(posix_constants_pathconf) |
| 6356 | / sizeof(struct constdef)); |
| 6357 | } |
| 6358 | #endif |
| 6359 | |
| 6360 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6361 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6362 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6363 | 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] | 6364 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6365 | |
| 6366 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6367 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6368 | { |
| 6369 | PyObject *result = NULL; |
| 6370 | int name, fd; |
| 6371 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6372 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 6373 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6374 | long limit; |
| 6375 | |
| 6376 | errno = 0; |
| 6377 | limit = fpathconf(fd, name); |
| 6378 | if (limit == -1 && errno != 0) |
| 6379 | posix_error(); |
| 6380 | else |
| 6381 | result = PyInt_FromLong(limit); |
| 6382 | } |
| 6383 | return result; |
| 6384 | } |
| 6385 | #endif |
| 6386 | |
| 6387 | |
| 6388 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6389 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6390 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6391 | 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] | 6392 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6393 | |
| 6394 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6395 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6396 | { |
| 6397 | PyObject *result = NULL; |
| 6398 | int name; |
| 6399 | char *path; |
| 6400 | |
| 6401 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 6402 | conv_path_confname, &name)) { |
| 6403 | long limit; |
| 6404 | |
| 6405 | errno = 0; |
| 6406 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6407 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6408 | if (errno == EINVAL) |
| 6409 | /* could be a path or name problem */ |
| 6410 | posix_error(); |
| 6411 | else |
| 6412 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6413 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6414 | else |
| 6415 | result = PyInt_FromLong(limit); |
| 6416 | } |
| 6417 | return result; |
| 6418 | } |
| 6419 | #endif |
| 6420 | |
| 6421 | #ifdef HAVE_CONFSTR |
| 6422 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6423 | #ifdef _CS_ARCHITECTURE |
| 6424 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 6425 | #endif |
| 6426 | #ifdef _CS_HOSTNAME |
| 6427 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 6428 | #endif |
| 6429 | #ifdef _CS_HW_PROVIDER |
| 6430 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 6431 | #endif |
| 6432 | #ifdef _CS_HW_SERIAL |
| 6433 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 6434 | #endif |
| 6435 | #ifdef _CS_INITTAB_NAME |
| 6436 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 6437 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6438 | #ifdef _CS_LFS64_CFLAGS |
| 6439 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 6440 | #endif |
| 6441 | #ifdef _CS_LFS64_LDFLAGS |
| 6442 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 6443 | #endif |
| 6444 | #ifdef _CS_LFS64_LIBS |
| 6445 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 6446 | #endif |
| 6447 | #ifdef _CS_LFS64_LINTFLAGS |
| 6448 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 6449 | #endif |
| 6450 | #ifdef _CS_LFS_CFLAGS |
| 6451 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 6452 | #endif |
| 6453 | #ifdef _CS_LFS_LDFLAGS |
| 6454 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6455 | #endif |
| 6456 | #ifdef _CS_LFS_LIBS |
| 6457 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6458 | #endif |
| 6459 | #ifdef _CS_LFS_LINTFLAGS |
| 6460 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6461 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6462 | #ifdef _CS_MACHINE |
| 6463 | {"CS_MACHINE", _CS_MACHINE}, |
| 6464 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6465 | #ifdef _CS_PATH |
| 6466 | {"CS_PATH", _CS_PATH}, |
| 6467 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6468 | #ifdef _CS_RELEASE |
| 6469 | {"CS_RELEASE", _CS_RELEASE}, |
| 6470 | #endif |
| 6471 | #ifdef _CS_SRPC_DOMAIN |
| 6472 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6473 | #endif |
| 6474 | #ifdef _CS_SYSNAME |
| 6475 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6476 | #endif |
| 6477 | #ifdef _CS_VERSION |
| 6478 | {"CS_VERSION", _CS_VERSION}, |
| 6479 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6480 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6481 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6482 | #endif |
| 6483 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6484 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6485 | #endif |
| 6486 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6487 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6488 | #endif |
| 6489 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6490 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6491 | #endif |
| 6492 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6493 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6494 | #endif |
| 6495 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6496 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6497 | #endif |
| 6498 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6499 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6500 | #endif |
| 6501 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6502 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6503 | #endif |
| 6504 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6505 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6506 | #endif |
| 6507 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6508 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6509 | #endif |
| 6510 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6511 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6512 | #endif |
| 6513 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6514 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6515 | #endif |
| 6516 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6517 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6518 | #endif |
| 6519 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6520 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6521 | #endif |
| 6522 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6523 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6524 | #endif |
| 6525 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6526 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6527 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6528 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6529 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6530 | #endif |
| 6531 | #ifdef _MIPS_CS_BASE |
| 6532 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6533 | #endif |
| 6534 | #ifdef _MIPS_CS_HOSTID |
| 6535 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6536 | #endif |
| 6537 | #ifdef _MIPS_CS_HW_NAME |
| 6538 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6539 | #endif |
| 6540 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6541 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6542 | #endif |
| 6543 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6544 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6545 | #endif |
| 6546 | #ifdef _MIPS_CS_OSREL_MIN |
| 6547 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6548 | #endif |
| 6549 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6550 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6551 | #endif |
| 6552 | #ifdef _MIPS_CS_OS_NAME |
| 6553 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6554 | #endif |
| 6555 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6556 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6557 | #endif |
| 6558 | #ifdef _MIPS_CS_PROCESSORS |
| 6559 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6560 | #endif |
| 6561 | #ifdef _MIPS_CS_SERIAL |
| 6562 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6563 | #endif |
| 6564 | #ifdef _MIPS_CS_VENDOR |
| 6565 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6566 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6567 | }; |
| 6568 | |
| 6569 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6570 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6571 | { |
| 6572 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6573 | sizeof(posix_constants_confstr) |
| 6574 | / sizeof(struct constdef)); |
| 6575 | } |
| 6576 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6577 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6578 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6579 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6580 | |
| 6581 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6582 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6583 | { |
| 6584 | PyObject *result = NULL; |
| 6585 | int name; |
| 6586 | char buffer[64]; |
| 6587 | |
| 6588 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 6589 | int len = confstr(name, buffer, sizeof(buffer)); |
| 6590 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6591 | errno = 0; |
| 6592 | if (len == 0) { |
| 6593 | if (errno != 0) |
| 6594 | posix_error(); |
| 6595 | else |
| 6596 | result = PyString_FromString(""); |
| 6597 | } |
| 6598 | else { |
| 6599 | if (len >= sizeof(buffer)) { |
| 6600 | result = PyString_FromStringAndSize(NULL, len); |
| 6601 | if (result != NULL) |
| 6602 | confstr(name, PyString_AS_STRING(result), len+1); |
| 6603 | } |
| 6604 | else |
| 6605 | result = PyString_FromString(buffer); |
| 6606 | } |
| 6607 | } |
| 6608 | return result; |
| 6609 | } |
| 6610 | #endif |
| 6611 | |
| 6612 | |
| 6613 | #ifdef HAVE_SYSCONF |
| 6614 | static struct constdef posix_constants_sysconf[] = { |
| 6615 | #ifdef _SC_2_CHAR_TERM |
| 6616 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6617 | #endif |
| 6618 | #ifdef _SC_2_C_BIND |
| 6619 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6620 | #endif |
| 6621 | #ifdef _SC_2_C_DEV |
| 6622 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6623 | #endif |
| 6624 | #ifdef _SC_2_C_VERSION |
| 6625 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6626 | #endif |
| 6627 | #ifdef _SC_2_FORT_DEV |
| 6628 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6629 | #endif |
| 6630 | #ifdef _SC_2_FORT_RUN |
| 6631 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6632 | #endif |
| 6633 | #ifdef _SC_2_LOCALEDEF |
| 6634 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6635 | #endif |
| 6636 | #ifdef _SC_2_SW_DEV |
| 6637 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6638 | #endif |
| 6639 | #ifdef _SC_2_UPE |
| 6640 | {"SC_2_UPE", _SC_2_UPE}, |
| 6641 | #endif |
| 6642 | #ifdef _SC_2_VERSION |
| 6643 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6644 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6645 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6646 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6647 | #endif |
| 6648 | #ifdef _SC_ACL |
| 6649 | {"SC_ACL", _SC_ACL}, |
| 6650 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6651 | #ifdef _SC_AIO_LISTIO_MAX |
| 6652 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6653 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6654 | #ifdef _SC_AIO_MAX |
| 6655 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6656 | #endif |
| 6657 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6658 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6659 | #endif |
| 6660 | #ifdef _SC_ARG_MAX |
| 6661 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6662 | #endif |
| 6663 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6664 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6665 | #endif |
| 6666 | #ifdef _SC_ATEXIT_MAX |
| 6667 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6668 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6669 | #ifdef _SC_AUDIT |
| 6670 | {"SC_AUDIT", _SC_AUDIT}, |
| 6671 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6672 | #ifdef _SC_AVPHYS_PAGES |
| 6673 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6674 | #endif |
| 6675 | #ifdef _SC_BC_BASE_MAX |
| 6676 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6677 | #endif |
| 6678 | #ifdef _SC_BC_DIM_MAX |
| 6679 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6680 | #endif |
| 6681 | #ifdef _SC_BC_SCALE_MAX |
| 6682 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6683 | #endif |
| 6684 | #ifdef _SC_BC_STRING_MAX |
| 6685 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6686 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6687 | #ifdef _SC_CAP |
| 6688 | {"SC_CAP", _SC_CAP}, |
| 6689 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6690 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6691 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6692 | #endif |
| 6693 | #ifdef _SC_CHAR_BIT |
| 6694 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6695 | #endif |
| 6696 | #ifdef _SC_CHAR_MAX |
| 6697 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6698 | #endif |
| 6699 | #ifdef _SC_CHAR_MIN |
| 6700 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6701 | #endif |
| 6702 | #ifdef _SC_CHILD_MAX |
| 6703 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6704 | #endif |
| 6705 | #ifdef _SC_CLK_TCK |
| 6706 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6707 | #endif |
| 6708 | #ifdef _SC_COHER_BLKSZ |
| 6709 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6710 | #endif |
| 6711 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6712 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6713 | #endif |
| 6714 | #ifdef _SC_DCACHE_ASSOC |
| 6715 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6716 | #endif |
| 6717 | #ifdef _SC_DCACHE_BLKSZ |
| 6718 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6719 | #endif |
| 6720 | #ifdef _SC_DCACHE_LINESZ |
| 6721 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6722 | #endif |
| 6723 | #ifdef _SC_DCACHE_SZ |
| 6724 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6725 | #endif |
| 6726 | #ifdef _SC_DCACHE_TBLKSZ |
| 6727 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6728 | #endif |
| 6729 | #ifdef _SC_DELAYTIMER_MAX |
| 6730 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6731 | #endif |
| 6732 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6733 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6734 | #endif |
| 6735 | #ifdef _SC_EXPR_NEST_MAX |
| 6736 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6737 | #endif |
| 6738 | #ifdef _SC_FSYNC |
| 6739 | {"SC_FSYNC", _SC_FSYNC}, |
| 6740 | #endif |
| 6741 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6742 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6743 | #endif |
| 6744 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6745 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6746 | #endif |
| 6747 | #ifdef _SC_ICACHE_ASSOC |
| 6748 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6749 | #endif |
| 6750 | #ifdef _SC_ICACHE_BLKSZ |
| 6751 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6752 | #endif |
| 6753 | #ifdef _SC_ICACHE_LINESZ |
| 6754 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6755 | #endif |
| 6756 | #ifdef _SC_ICACHE_SZ |
| 6757 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6758 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6759 | #ifdef _SC_INF |
| 6760 | {"SC_INF", _SC_INF}, |
| 6761 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6762 | #ifdef _SC_INT_MAX |
| 6763 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6764 | #endif |
| 6765 | #ifdef _SC_INT_MIN |
| 6766 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6767 | #endif |
| 6768 | #ifdef _SC_IOV_MAX |
| 6769 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6770 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6771 | #ifdef _SC_IP_SECOPTS |
| 6772 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6773 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6774 | #ifdef _SC_JOB_CONTROL |
| 6775 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6776 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6777 | #ifdef _SC_KERN_POINTERS |
| 6778 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6779 | #endif |
| 6780 | #ifdef _SC_KERN_SIM |
| 6781 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6782 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6783 | #ifdef _SC_LINE_MAX |
| 6784 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6785 | #endif |
| 6786 | #ifdef _SC_LOGIN_NAME_MAX |
| 6787 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6788 | #endif |
| 6789 | #ifdef _SC_LOGNAME_MAX |
| 6790 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6791 | #endif |
| 6792 | #ifdef _SC_LONG_BIT |
| 6793 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6794 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6795 | #ifdef _SC_MAC |
| 6796 | {"SC_MAC", _SC_MAC}, |
| 6797 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6798 | #ifdef _SC_MAPPED_FILES |
| 6799 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6800 | #endif |
| 6801 | #ifdef _SC_MAXPID |
| 6802 | {"SC_MAXPID", _SC_MAXPID}, |
| 6803 | #endif |
| 6804 | #ifdef _SC_MB_LEN_MAX |
| 6805 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6806 | #endif |
| 6807 | #ifdef _SC_MEMLOCK |
| 6808 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6809 | #endif |
| 6810 | #ifdef _SC_MEMLOCK_RANGE |
| 6811 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6812 | #endif |
| 6813 | #ifdef _SC_MEMORY_PROTECTION |
| 6814 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6815 | #endif |
| 6816 | #ifdef _SC_MESSAGE_PASSING |
| 6817 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6818 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6819 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6820 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6821 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6822 | #ifdef _SC_MQ_OPEN_MAX |
| 6823 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6824 | #endif |
| 6825 | #ifdef _SC_MQ_PRIO_MAX |
| 6826 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6827 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6828 | #ifdef _SC_NACLS_MAX |
| 6829 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6830 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6831 | #ifdef _SC_NGROUPS_MAX |
| 6832 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6833 | #endif |
| 6834 | #ifdef _SC_NL_ARGMAX |
| 6835 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6836 | #endif |
| 6837 | #ifdef _SC_NL_LANGMAX |
| 6838 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6839 | #endif |
| 6840 | #ifdef _SC_NL_MSGMAX |
| 6841 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6842 | #endif |
| 6843 | #ifdef _SC_NL_NMAX |
| 6844 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6845 | #endif |
| 6846 | #ifdef _SC_NL_SETMAX |
| 6847 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6848 | #endif |
| 6849 | #ifdef _SC_NL_TEXTMAX |
| 6850 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6851 | #endif |
| 6852 | #ifdef _SC_NPROCESSORS_CONF |
| 6853 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6854 | #endif |
| 6855 | #ifdef _SC_NPROCESSORS_ONLN |
| 6856 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6857 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6858 | #ifdef _SC_NPROC_CONF |
| 6859 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6860 | #endif |
| 6861 | #ifdef _SC_NPROC_ONLN |
| 6862 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6863 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6864 | #ifdef _SC_NZERO |
| 6865 | {"SC_NZERO", _SC_NZERO}, |
| 6866 | #endif |
| 6867 | #ifdef _SC_OPEN_MAX |
| 6868 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6869 | #endif |
| 6870 | #ifdef _SC_PAGESIZE |
| 6871 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6872 | #endif |
| 6873 | #ifdef _SC_PAGE_SIZE |
| 6874 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6875 | #endif |
| 6876 | #ifdef _SC_PASS_MAX |
| 6877 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6878 | #endif |
| 6879 | #ifdef _SC_PHYS_PAGES |
| 6880 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6881 | #endif |
| 6882 | #ifdef _SC_PII |
| 6883 | {"SC_PII", _SC_PII}, |
| 6884 | #endif |
| 6885 | #ifdef _SC_PII_INTERNET |
| 6886 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6887 | #endif |
| 6888 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6889 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6890 | #endif |
| 6891 | #ifdef _SC_PII_INTERNET_STREAM |
| 6892 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6893 | #endif |
| 6894 | #ifdef _SC_PII_OSI |
| 6895 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6896 | #endif |
| 6897 | #ifdef _SC_PII_OSI_CLTS |
| 6898 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6899 | #endif |
| 6900 | #ifdef _SC_PII_OSI_COTS |
| 6901 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6902 | #endif |
| 6903 | #ifdef _SC_PII_OSI_M |
| 6904 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6905 | #endif |
| 6906 | #ifdef _SC_PII_SOCKET |
| 6907 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6908 | #endif |
| 6909 | #ifdef _SC_PII_XTI |
| 6910 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6911 | #endif |
| 6912 | #ifdef _SC_POLL |
| 6913 | {"SC_POLL", _SC_POLL}, |
| 6914 | #endif |
| 6915 | #ifdef _SC_PRIORITIZED_IO |
| 6916 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6917 | #endif |
| 6918 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6919 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6920 | #endif |
| 6921 | #ifdef _SC_REALTIME_SIGNALS |
| 6922 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6923 | #endif |
| 6924 | #ifdef _SC_RE_DUP_MAX |
| 6925 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6926 | #endif |
| 6927 | #ifdef _SC_RTSIG_MAX |
| 6928 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6929 | #endif |
| 6930 | #ifdef _SC_SAVED_IDS |
| 6931 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6932 | #endif |
| 6933 | #ifdef _SC_SCHAR_MAX |
| 6934 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6935 | #endif |
| 6936 | #ifdef _SC_SCHAR_MIN |
| 6937 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6938 | #endif |
| 6939 | #ifdef _SC_SELECT |
| 6940 | {"SC_SELECT", _SC_SELECT}, |
| 6941 | #endif |
| 6942 | #ifdef _SC_SEMAPHORES |
| 6943 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6944 | #endif |
| 6945 | #ifdef _SC_SEM_NSEMS_MAX |
| 6946 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6947 | #endif |
| 6948 | #ifdef _SC_SEM_VALUE_MAX |
| 6949 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6950 | #endif |
| 6951 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6952 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6953 | #endif |
| 6954 | #ifdef _SC_SHRT_MAX |
| 6955 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6956 | #endif |
| 6957 | #ifdef _SC_SHRT_MIN |
| 6958 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6959 | #endif |
| 6960 | #ifdef _SC_SIGQUEUE_MAX |
| 6961 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6962 | #endif |
| 6963 | #ifdef _SC_SIGRT_MAX |
| 6964 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6965 | #endif |
| 6966 | #ifdef _SC_SIGRT_MIN |
| 6967 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6968 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6969 | #ifdef _SC_SOFTPOWER |
| 6970 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6971 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6972 | #ifdef _SC_SPLIT_CACHE |
| 6973 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6974 | #endif |
| 6975 | #ifdef _SC_SSIZE_MAX |
| 6976 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6977 | #endif |
| 6978 | #ifdef _SC_STACK_PROT |
| 6979 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6980 | #endif |
| 6981 | #ifdef _SC_STREAM_MAX |
| 6982 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6983 | #endif |
| 6984 | #ifdef _SC_SYNCHRONIZED_IO |
| 6985 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6986 | #endif |
| 6987 | #ifdef _SC_THREADS |
| 6988 | {"SC_THREADS", _SC_THREADS}, |
| 6989 | #endif |
| 6990 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6991 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6992 | #endif |
| 6993 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6994 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6995 | #endif |
| 6996 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6997 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6998 | #endif |
| 6999 | #ifdef _SC_THREAD_KEYS_MAX |
| 7000 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 7001 | #endif |
| 7002 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 7003 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 7004 | #endif |
| 7005 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 7006 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 7007 | #endif |
| 7008 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 7009 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 7010 | #endif |
| 7011 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 7012 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 7013 | #endif |
| 7014 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 7015 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 7016 | #endif |
| 7017 | #ifdef _SC_THREAD_STACK_MIN |
| 7018 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 7019 | #endif |
| 7020 | #ifdef _SC_THREAD_THREADS_MAX |
| 7021 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 7022 | #endif |
| 7023 | #ifdef _SC_TIMERS |
| 7024 | {"SC_TIMERS", _SC_TIMERS}, |
| 7025 | #endif |
| 7026 | #ifdef _SC_TIMER_MAX |
| 7027 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 7028 | #endif |
| 7029 | #ifdef _SC_TTY_NAME_MAX |
| 7030 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 7031 | #endif |
| 7032 | #ifdef _SC_TZNAME_MAX |
| 7033 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 7034 | #endif |
| 7035 | #ifdef _SC_T_IOV_MAX |
| 7036 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 7037 | #endif |
| 7038 | #ifdef _SC_UCHAR_MAX |
| 7039 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 7040 | #endif |
| 7041 | #ifdef _SC_UINT_MAX |
| 7042 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 7043 | #endif |
| 7044 | #ifdef _SC_UIO_MAXIOV |
| 7045 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 7046 | #endif |
| 7047 | #ifdef _SC_ULONG_MAX |
| 7048 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 7049 | #endif |
| 7050 | #ifdef _SC_USHRT_MAX |
| 7051 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 7052 | #endif |
| 7053 | #ifdef _SC_VERSION |
| 7054 | {"SC_VERSION", _SC_VERSION}, |
| 7055 | #endif |
| 7056 | #ifdef _SC_WORD_BIT |
| 7057 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 7058 | #endif |
| 7059 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 7060 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 7061 | #endif |
| 7062 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 7063 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 7064 | #endif |
| 7065 | #ifdef _SC_XBS5_LP64_OFF64 |
| 7066 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 7067 | #endif |
| 7068 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 7069 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 7070 | #endif |
| 7071 | #ifdef _SC_XOPEN_CRYPT |
| 7072 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 7073 | #endif |
| 7074 | #ifdef _SC_XOPEN_ENH_I18N |
| 7075 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 7076 | #endif |
| 7077 | #ifdef _SC_XOPEN_LEGACY |
| 7078 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 7079 | #endif |
| 7080 | #ifdef _SC_XOPEN_REALTIME |
| 7081 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 7082 | #endif |
| 7083 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 7084 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 7085 | #endif |
| 7086 | #ifdef _SC_XOPEN_SHM |
| 7087 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 7088 | #endif |
| 7089 | #ifdef _SC_XOPEN_UNIX |
| 7090 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 7091 | #endif |
| 7092 | #ifdef _SC_XOPEN_VERSION |
| 7093 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 7094 | #endif |
| 7095 | #ifdef _SC_XOPEN_XCU_VERSION |
| 7096 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 7097 | #endif |
| 7098 | #ifdef _SC_XOPEN_XPG2 |
| 7099 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 7100 | #endif |
| 7101 | #ifdef _SC_XOPEN_XPG3 |
| 7102 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 7103 | #endif |
| 7104 | #ifdef _SC_XOPEN_XPG4 |
| 7105 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 7106 | #endif |
| 7107 | }; |
| 7108 | |
| 7109 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7110 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7111 | { |
| 7112 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 7113 | sizeof(posix_constants_sysconf) |
| 7114 | / sizeof(struct constdef)); |
| 7115 | } |
| 7116 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7117 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7118 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7119 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7120 | |
| 7121 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7122 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7123 | { |
| 7124 | PyObject *result = NULL; |
| 7125 | int name; |
| 7126 | |
| 7127 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 7128 | int value; |
| 7129 | |
| 7130 | errno = 0; |
| 7131 | value = sysconf(name); |
| 7132 | if (value == -1 && errno != 0) |
| 7133 | posix_error(); |
| 7134 | else |
| 7135 | result = PyInt_FromLong(value); |
| 7136 | } |
| 7137 | return result; |
| 7138 | } |
| 7139 | #endif |
| 7140 | |
| 7141 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7142 | /* This code is used to ensure that the tables of configuration value names |
| 7143 | * are in sorted order as required by conv_confname(), and also to build the |
| 7144 | * the exported dictionaries that are used to publish information about the |
| 7145 | * names available on the host platform. |
| 7146 | * |
| 7147 | * Sorting the table at runtime ensures that the table is properly ordered |
| 7148 | * when used, even for platforms we're not able to test on. It also makes |
| 7149 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7150 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7151 | |
| 7152 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7153 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7154 | { |
| 7155 | const struct constdef *c1 = |
| 7156 | (const struct constdef *) v1; |
| 7157 | const struct constdef *c2 = |
| 7158 | (const struct constdef *) v2; |
| 7159 | |
| 7160 | return strcmp(c1->name, c2->name); |
| 7161 | } |
| 7162 | |
| 7163 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7164 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7165 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7166 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7167 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7168 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7169 | |
| 7170 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 7171 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7172 | if (d == NULL) |
| 7173 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7174 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7175 | for (i=0; i < tablesize; ++i) { |
| 7176 | PyObject *o = PyInt_FromLong(table[i].value); |
| 7177 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 7178 | Py_XDECREF(o); |
| 7179 | Py_DECREF(d); |
| 7180 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7181 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7182 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7183 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7184 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7185 | } |
| 7186 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7187 | /* Return -1 on failure, 0 on success. */ |
| 7188 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7189 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7190 | { |
| 7191 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7192 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7193 | sizeof(posix_constants_pathconf) |
| 7194 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7195 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7196 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7197 | #endif |
| 7198 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7199 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7200 | sizeof(posix_constants_confstr) |
| 7201 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7202 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7203 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7204 | #endif |
| 7205 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7206 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7207 | sizeof(posix_constants_sysconf) |
| 7208 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7209 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7210 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7211 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7212 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7213 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7214 | |
| 7215 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7216 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7217 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7218 | 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] | 7219 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7220 | |
| 7221 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7222 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7223 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7224 | abort(); |
| 7225 | /*NOTREACHED*/ |
| 7226 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 7227 | return NULL; |
| 7228 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7229 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7230 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7231 | PyDoc_STRVAR(win32_startfile__doc__, |
| 7232 | "startfile(filepath) - Start a file with its associated application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7233 | \n\ |
| 7234 | This acts like double-clicking the file in Explorer, or giving the file\n\ |
| 7235 | name as an argument to the DOS \"start\" command: the file is opened\n\ |
| 7236 | with whatever application (if any) its extension is associated.\n\ |
| 7237 | \n\ |
| 7238 | startfile returns as soon as the associated application is launched.\n\ |
| 7239 | There is no option to wait for the application to close, and no way\n\ |
| 7240 | to retrieve the application's exit status.\n\ |
| 7241 | \n\ |
| 7242 | The filepath is relative to the current directory. If you want to use\n\ |
| 7243 | 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] | 7244 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7245 | |
| 7246 | static PyObject * |
| 7247 | win32_startfile(PyObject *self, PyObject *args) |
| 7248 | { |
| 7249 | char *filepath; |
| 7250 | HINSTANCE rc; |
| 7251 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) |
| 7252 | return NULL; |
| 7253 | Py_BEGIN_ALLOW_THREADS |
| 7254 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); |
| 7255 | Py_END_ALLOW_THREADS |
| 7256 | if (rc <= (HINSTANCE)32) |
| 7257 | return win32_error("startfile", filepath); |
| 7258 | Py_INCREF(Py_None); |
| 7259 | return Py_None; |
| 7260 | } |
| 7261 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7262 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7263 | #ifdef HAVE_GETLOADAVG |
| 7264 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 7265 | "getloadavg() -> (float, float, float)\n\n\ |
| 7266 | Return the number of processes in the system run queue averaged over\n\ |
| 7267 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 7268 | was unobtainable"); |
| 7269 | |
| 7270 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7271 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7272 | { |
| 7273 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7274 | if (getloadavg(loadavg, 3)!=3) { |
| 7275 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 7276 | return NULL; |
| 7277 | } else |
| 7278 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 7279 | } |
| 7280 | #endif |
| 7281 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7282 | #ifdef MS_WINDOWS |
| 7283 | |
| 7284 | PyDoc_STRVAR(win32_urandom__doc__, |
| 7285 | "urandom(n) -> str\n\n\ |
| 7286 | Return a string of n random bytes suitable for cryptographic use."); |
| 7287 | |
| 7288 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 7289 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 7290 | DWORD dwFlags ); |
| 7291 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 7292 | BYTE *pbBuffer ); |
| 7293 | |
| 7294 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
| 7295 | static HCRYPTPROV hCryptProv = 0; |
| 7296 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7297 | static PyObject* |
| 7298 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7299 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7300 | int howMany; |
| 7301 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7302 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7303 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 7304 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7305 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 7306 | if (howMany < 0) |
| 7307 | return PyErr_Format(PyExc_ValueError, |
| 7308 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7309 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7310 | if (hCryptProv == 0) { |
| 7311 | HINSTANCE hAdvAPI32 = NULL; |
| 7312 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7313 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7314 | /* Obtain handle to the DLL containing CryptoAPI |
| 7315 | This should not fail */ |
| 7316 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 7317 | if(hAdvAPI32 == NULL) |
| 7318 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7319 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7320 | /* Obtain pointers to the CryptoAPI functions |
| 7321 | This will fail on some early versions of Win95 */ |
| 7322 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 7323 | hAdvAPI32, |
| 7324 | "CryptAcquireContextA"); |
| 7325 | if (pCryptAcquireContext == NULL) |
| 7326 | return PyErr_Format(PyExc_NotImplementedError, |
| 7327 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7328 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7329 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 7330 | hAdvAPI32, "CryptGenRandom"); |
| 7331 | if (pCryptAcquireContext == NULL) |
| 7332 | return PyErr_Format(PyExc_NotImplementedError, |
| 7333 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7334 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7335 | /* Acquire context */ |
| 7336 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 7337 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 7338 | return win32_error("CryptAcquireContext", NULL); |
| 7339 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7340 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7341 | /* Allocate bytes */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7342 | result = PyString_FromStringAndSize(NULL, howMany); |
| 7343 | if (result != NULL) { |
| 7344 | /* Get random data */ |
| 7345 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 7346 | PyString_AS_STRING(result))) { |
| 7347 | Py_DECREF(result); |
| 7348 | return win32_error("CryptGenRandom", NULL); |
| 7349 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7350 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7351 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7352 | } |
| 7353 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7354 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7355 | static PyMethodDef posix_methods[] = { |
| 7356 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 7357 | #ifdef HAVE_TTYNAME |
| 7358 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 7359 | #endif |
| 7360 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 7361 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7362 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7363 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7364 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7365 | #ifdef HAVE_LCHOWN |
| 7366 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 7367 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7368 | #ifdef HAVE_CHROOT |
| 7369 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 7370 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7371 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7372 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7373 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7374 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7375 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7376 | #ifdef Py_USING_UNICODE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7377 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7378 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7379 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7380 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7381 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7382 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7383 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7384 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7385 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7386 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7387 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7388 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7389 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7390 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7391 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7392 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7393 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7394 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7395 | {"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] | 7396 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7397 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7398 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7399 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7400 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7401 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7402 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7403 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7404 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7405 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7406 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7407 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7408 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7409 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7410 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7411 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7412 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7413 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7414 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7415 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7416 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7417 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7418 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7419 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7420 | #if defined(PYOS_OS2) |
| 7421 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 7422 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 7423 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7424 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7425 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7426 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7427 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7428 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7429 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7430 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7431 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7432 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7433 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7434 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7435 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7436 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7437 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7438 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7439 | #endif /* HAVE_GETEGID */ |
| 7440 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7441 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7442 | #endif /* HAVE_GETEUID */ |
| 7443 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7444 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7445 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7446 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7447 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7448 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7449 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7450 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7451 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7452 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7453 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7454 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7455 | #endif /* HAVE_GETPPID */ |
| 7456 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7457 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7458 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7459 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7460 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7461 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7462 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7463 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7464 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7465 | #ifdef HAVE_KILLPG |
| 7466 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7467 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7468 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7469 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7470 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7471 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7472 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7473 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7474 | {"popen2", win32_popen2, METH_VARARGS}, |
| 7475 | {"popen3", win32_popen3, METH_VARARGS}, |
| 7476 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7477 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7478 | #else |
| 7479 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7480 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 7481 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 7482 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 7483 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7484 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7485 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7486 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7487 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7488 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7489 | #ifdef HAVE_SETEUID |
| 7490 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7491 | #endif /* HAVE_SETEUID */ |
| 7492 | #ifdef HAVE_SETEGID |
| 7493 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7494 | #endif /* HAVE_SETEGID */ |
| 7495 | #ifdef HAVE_SETREUID |
| 7496 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7497 | #endif /* HAVE_SETREUID */ |
| 7498 | #ifdef HAVE_SETREGID |
| 7499 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7500 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7501 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7502 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7503 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7504 | #ifdef HAVE_SETGROUPS |
| 7505 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 7506 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7507 | #ifdef HAVE_GETPGID |
| 7508 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7509 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7510 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7511 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7512 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7513 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7514 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7515 | #endif /* HAVE_WAIT */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7516 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7517 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7518 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7519 | #ifdef HAVE_GETSID |
| 7520 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 7521 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7522 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7523 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7524 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7525 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7526 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7527 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7528 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7529 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7530 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7531 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7532 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7533 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7534 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7535 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 7536 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7537 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7538 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7539 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7540 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7541 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 7542 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7543 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7544 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7545 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7546 | #endif |
| 7547 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7548 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7549 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7550 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7551 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7552 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7553 | #ifdef HAVE_DEVICE_MACROS |
| 7554 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7555 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7556 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7557 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7558 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7559 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7560 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7561 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7562 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7563 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7564 | #ifdef HAVE_UNSETENV |
| 7565 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7566 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7567 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7568 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7569 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7570 | #ifdef HAVE_FCHDIR |
| 7571 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7572 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7573 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7574 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7575 | #endif |
| 7576 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7577 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7578 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7579 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7580 | #ifdef WCOREDUMP |
| 7581 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7582 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7583 | #ifdef WIFCONTINUED |
| 7584 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7585 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7586 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7587 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7588 | #endif /* WIFSTOPPED */ |
| 7589 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7590 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7591 | #endif /* WIFSIGNALED */ |
| 7592 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7593 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7594 | #endif /* WIFEXITED */ |
| 7595 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7596 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7597 | #endif /* WEXITSTATUS */ |
| 7598 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7599 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7600 | #endif /* WTERMSIG */ |
| 7601 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7602 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7603 | #endif /* WSTOPSIG */ |
| 7604 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7605 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7606 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7607 | #endif |
| 7608 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7609 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7610 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 7611 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7612 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7613 | #endif |
| 7614 | #ifdef HAVE_TEMPNAM |
| 7615 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 7616 | #endif |
| 7617 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7618 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7619 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7620 | #ifdef HAVE_CONFSTR |
| 7621 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7622 | #endif |
| 7623 | #ifdef HAVE_SYSCONF |
| 7624 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7625 | #endif |
| 7626 | #ifdef HAVE_FPATHCONF |
| 7627 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7628 | #endif |
| 7629 | #ifdef HAVE_PATHCONF |
| 7630 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7631 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7632 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7633 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7634 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7635 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7636 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7637 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7638 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7639 | #ifdef MS_WINDOWS |
| 7640 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 7641 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7642 | {NULL, NULL} /* Sentinel */ |
| 7643 | }; |
| 7644 | |
| 7645 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7646 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7647 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7648 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7649 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7650 | } |
| 7651 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7652 | #if defined(PYOS_OS2) |
| 7653 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7654 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7655 | { |
| 7656 | APIRET rc; |
| 7657 | ULONG values[QSV_MAX+1]; |
| 7658 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7659 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7660 | |
| 7661 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7662 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7663 | Py_END_ALLOW_THREADS |
| 7664 | |
| 7665 | if (rc != NO_ERROR) { |
| 7666 | os2_error(rc); |
| 7667 | return -1; |
| 7668 | } |
| 7669 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7670 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7671 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7672 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7673 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7674 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7675 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7676 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7677 | |
| 7678 | switch (values[QSV_VERSION_MINOR]) { |
| 7679 | case 0: ver = "2.00"; break; |
| 7680 | case 10: ver = "2.10"; break; |
| 7681 | case 11: ver = "2.11"; break; |
| 7682 | case 30: ver = "3.00"; break; |
| 7683 | case 40: ver = "4.00"; break; |
| 7684 | case 50: ver = "5.00"; break; |
| 7685 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7686 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7687 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7688 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7689 | ver = &tmp[0]; |
| 7690 | } |
| 7691 | |
| 7692 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7693 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7694 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7695 | |
| 7696 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7697 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7698 | tmp[1] = ':'; |
| 7699 | tmp[2] = '\0'; |
| 7700 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7701 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7702 | } |
| 7703 | #endif |
| 7704 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7705 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7706 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7707 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7708 | #ifdef F_OK |
| 7709 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7710 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7711 | #ifdef R_OK |
| 7712 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7713 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7714 | #ifdef W_OK |
| 7715 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7716 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7717 | #ifdef X_OK |
| 7718 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7719 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7720 | #ifdef NGROUPS_MAX |
| 7721 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7722 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7723 | #ifdef TMP_MAX |
| 7724 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7725 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7726 | #ifdef WCONTINUED |
| 7727 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7728 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7729 | #ifdef WNOHANG |
| 7730 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7731 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7732 | #ifdef WUNTRACED |
| 7733 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7734 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7735 | #ifdef O_RDONLY |
| 7736 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7737 | #endif |
| 7738 | #ifdef O_WRONLY |
| 7739 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7740 | #endif |
| 7741 | #ifdef O_RDWR |
| 7742 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7743 | #endif |
| 7744 | #ifdef O_NDELAY |
| 7745 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7746 | #endif |
| 7747 | #ifdef O_NONBLOCK |
| 7748 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7749 | #endif |
| 7750 | #ifdef O_APPEND |
| 7751 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7752 | #endif |
| 7753 | #ifdef O_DSYNC |
| 7754 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7755 | #endif |
| 7756 | #ifdef O_RSYNC |
| 7757 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7758 | #endif |
| 7759 | #ifdef O_SYNC |
| 7760 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7761 | #endif |
| 7762 | #ifdef O_NOCTTY |
| 7763 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7764 | #endif |
| 7765 | #ifdef O_CREAT |
| 7766 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7767 | #endif |
| 7768 | #ifdef O_EXCL |
| 7769 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7770 | #endif |
| 7771 | #ifdef O_TRUNC |
| 7772 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7773 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7774 | #ifdef O_BINARY |
| 7775 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7776 | #endif |
| 7777 | #ifdef O_TEXT |
| 7778 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7779 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7780 | #ifdef O_LARGEFILE |
| 7781 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7782 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7783 | #ifdef O_SHLOCK |
| 7784 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 7785 | #endif |
| 7786 | #ifdef O_EXLOCK |
| 7787 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 7788 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7789 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7790 | /* MS Windows */ |
| 7791 | #ifdef O_NOINHERIT |
| 7792 | /* Don't inherit in child processes. */ |
| 7793 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7794 | #endif |
| 7795 | #ifdef _O_SHORT_LIVED |
| 7796 | /* Optimize for short life (keep in memory). */ |
| 7797 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7798 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7799 | #endif |
| 7800 | #ifdef O_TEMPORARY |
| 7801 | /* Automatically delete when last handle is closed. */ |
| 7802 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7803 | #endif |
| 7804 | #ifdef O_RANDOM |
| 7805 | /* Optimize for random access. */ |
| 7806 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7807 | #endif |
| 7808 | #ifdef O_SEQUENTIAL |
| 7809 | /* Optimize for sequential access. */ |
| 7810 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7811 | #endif |
| 7812 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7813 | /* GNU extensions. */ |
| 7814 | #ifdef O_DIRECT |
| 7815 | /* Direct disk access. */ |
| 7816 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7817 | #endif |
| 7818 | #ifdef O_DIRECTORY |
| 7819 | /* Must be a directory. */ |
| 7820 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7821 | #endif |
| 7822 | #ifdef O_NOFOLLOW |
| 7823 | /* Do not follow links. */ |
| 7824 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7825 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7826 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7827 | /* These come from sysexits.h */ |
| 7828 | #ifdef EX_OK |
| 7829 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7830 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7831 | #ifdef EX_USAGE |
| 7832 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7833 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7834 | #ifdef EX_DATAERR |
| 7835 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7836 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7837 | #ifdef EX_NOINPUT |
| 7838 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7839 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7840 | #ifdef EX_NOUSER |
| 7841 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7842 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7843 | #ifdef EX_NOHOST |
| 7844 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7845 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7846 | #ifdef EX_UNAVAILABLE |
| 7847 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7848 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7849 | #ifdef EX_SOFTWARE |
| 7850 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7851 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7852 | #ifdef EX_OSERR |
| 7853 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7854 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7855 | #ifdef EX_OSFILE |
| 7856 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7857 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7858 | #ifdef EX_CANTCREAT |
| 7859 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7860 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7861 | #ifdef EX_IOERR |
| 7862 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7863 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7864 | #ifdef EX_TEMPFAIL |
| 7865 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7866 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7867 | #ifdef EX_PROTOCOL |
| 7868 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7869 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7870 | #ifdef EX_NOPERM |
| 7871 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7872 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7873 | #ifdef EX_CONFIG |
| 7874 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7875 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7876 | #ifdef EX_NOTFOUND |
| 7877 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7878 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7879 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7880 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7881 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7882 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7883 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7884 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7885 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7886 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7887 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7888 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7889 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7890 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7891 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7892 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7893 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7894 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7895 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7896 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7897 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7898 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7899 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7900 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7901 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7902 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7903 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7904 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7905 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7906 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7907 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7908 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7909 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7910 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7911 | #if defined(PYOS_OS2) |
| 7912 | if (insertvalues(d)) return -1; |
| 7913 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7914 | return 0; |
| 7915 | } |
| 7916 | |
| 7917 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7918 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7919 | #define INITFUNC initnt |
| 7920 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7921 | |
| 7922 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7923 | #define INITFUNC initos2 |
| 7924 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7925 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7926 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7927 | #define INITFUNC initposix |
| 7928 | #define MODNAME "posix" |
| 7929 | #endif |
| 7930 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7931 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7932 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7933 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7934 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7935 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7936 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7937 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7938 | posix__doc__); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7939 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7940 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7941 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7942 | Py_XINCREF(v); |
| 7943 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7944 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7945 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7946 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7947 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7948 | return; |
| 7949 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7950 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7951 | return; |
| 7952 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7953 | Py_INCREF(PyExc_OSError); |
| 7954 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7955 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7956 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7957 | if (posix_putenv_garbage == NULL) |
| 7958 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7959 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7960 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7961 | stat_result_desc.name = MODNAME ".stat_result"; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7962 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7963 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7964 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7965 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7966 | structseq_new = StatResultType.tp_new; |
| 7967 | StatResultType.tp_new = statresult_new; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7968 | Py_INCREF((PyObject*) &StatResultType); |
| 7969 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7970 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7971 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7972 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7973 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7974 | PyModule_AddObject(m, "statvfs_result", |
| 7975 | (PyObject*) &StatVFSResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7976 | } |