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> |
| 45 | #include "osdefs.h" |
| 46 | #endif |
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 | #ifdef HAVE_SIGNAL_H |
| 57 | #include <signal.h> |
| 58 | #endif |
| 59 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 60 | #ifdef HAVE_FCNTL_H |
| 61 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 62 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 64 | #ifdef HAVE_GRP_H |
| 65 | #include <grp.h> |
| 66 | #endif |
| 67 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 68 | #ifdef HAVE_SYSEXITS_H |
| 69 | #include <sysexits.h> |
| 70 | #endif /* HAVE_SYSEXITS_H */ |
| 71 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 72 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 73 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 74 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 75 | #include <process.h> |
| 76 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 77 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 78 | #define HAVE_GETCWD 1 |
| 79 | #define HAVE_OPENDIR 1 |
| 80 | #define HAVE_SYSTEM 1 |
| 81 | #if defined(__OS2__) |
| 82 | #define HAVE_EXECV 1 |
| 83 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 84 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 85 | #include <process.h> |
| 86 | #else |
| 87 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 88 | #define HAVE_EXECV 1 |
| 89 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 90 | #define HAVE_OPENDIR 1 |
| 91 | #define HAVE_PIPE 1 |
| 92 | #define HAVE_POPEN 1 |
| 93 | #define HAVE_SYSTEM 1 |
| 94 | #define HAVE_WAIT 1 |
| 95 | #else |
| 96 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 97 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 98 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 99 | #define HAVE_EXECV 1 |
| 100 | #define HAVE_PIPE 1 |
| 101 | #define HAVE_POPEN 1 |
| 102 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 103 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 104 | #define HAVE_FSYNC 1 |
| 105 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 106 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 107 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 108 | /* 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] | 109 | #else /* all other compilers */ |
| 110 | /* Unix functions that the configure script doesn't check for */ |
| 111 | #define HAVE_EXECV 1 |
| 112 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 113 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 114 | #define HAVE_FORK1 1 |
| 115 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 116 | #define HAVE_GETCWD 1 |
| 117 | #define HAVE_GETEGID 1 |
| 118 | #define HAVE_GETEUID 1 |
| 119 | #define HAVE_GETGID 1 |
| 120 | #define HAVE_GETPPID 1 |
| 121 | #define HAVE_GETUID 1 |
| 122 | #define HAVE_KILL 1 |
| 123 | #define HAVE_OPENDIR 1 |
| 124 | #define HAVE_PIPE 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 125 | #ifndef __rtems__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 126 | #define HAVE_POPEN 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 127 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 128 | #define HAVE_SYSTEM 1 |
| 129 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 130 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 131 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 132 | #endif /* _MSC_VER */ |
| 133 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 134 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 135 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 137 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 139 | #if defined(sun) && !defined(__SVR4) |
| 140 | /* SunOS 4.1.4 doesn't have prototypes for these: */ |
| 141 | extern int rename(const char *, const char *); |
| 142 | extern int pclose(FILE *); |
| 143 | extern int fclose(FILE *); |
Thomas Wouters | 0f954a4 | 2001-02-15 08:46:56 +0000 | [diff] [blame] | 144 | extern int fsync(int); |
| 145 | extern int lstat(const char *, struct stat *); |
| 146 | extern int symlink(const char *, const char *); |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 147 | #endif |
| 148 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 149 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 150 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 151 | (default) */ |
| 152 | extern char *ctermid_r(char *); |
| 153 | #endif |
| 154 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 155 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 156 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 157 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 158 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 159 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 160 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 161 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 162 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 163 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 164 | #endif |
| 165 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 166 | extern int chdir(char *); |
| 167 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 168 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int chdir(const char *); |
| 170 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 171 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 172 | #ifdef __BORLANDC__ |
| 173 | extern int chmod(const char *, int); |
| 174 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 175 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 176 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 177 | extern int chown(const char *, uid_t, gid_t); |
| 178 | extern char *getcwd(char *, int); |
| 179 | extern char *strerror(int); |
| 180 | extern int link(const char *, const char *); |
| 181 | extern int rename(const char *, const char *); |
| 182 | extern int stat(const char *, struct stat *); |
| 183 | extern int unlink(const char *); |
| 184 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 185 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 186 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 187 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 188 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 189 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 190 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 191 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 193 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 194 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 195 | #ifdef HAVE_UTIME_H |
| 196 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 197 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 199 | #ifdef HAVE_SYS_UTIME_H |
| 200 | #include <sys/utime.h> |
| 201 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 202 | #endif /* HAVE_SYS_UTIME_H */ |
| 203 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 204 | #ifdef HAVE_SYS_TIMES_H |
| 205 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 206 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 207 | |
| 208 | #ifdef HAVE_SYS_PARAM_H |
| 209 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 210 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 211 | |
| 212 | #ifdef HAVE_SYS_UTSNAME_H |
| 213 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 214 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 216 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 217 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 218 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 219 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 220 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 221 | #include <direct.h> |
| 222 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 223 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 225 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 226 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 227 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 229 | #endif |
| 230 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 231 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 232 | #endif |
| 233 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 235 | #endif |
| 236 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 237 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 238 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | #include <direct.h> |
| 240 | #include <io.h> |
| 241 | #include <process.h> |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 242 | #include "osdefs.h" |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 243 | #define WIN32_LEAN_AND_MEAN |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 244 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 245 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 246 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 247 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 248 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 250 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 252 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 253 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 254 | #ifndef MAXPATHLEN |
| 255 | #define MAXPATHLEN 1024 |
| 256 | #endif /* MAXPATHLEN */ |
| 257 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 258 | #ifdef UNION_WAIT |
| 259 | /* Emulate some macros on systems that have a union instead of macros */ |
| 260 | |
| 261 | #ifndef WIFEXITED |
| 262 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 263 | #endif |
| 264 | |
| 265 | #ifndef WEXITSTATUS |
| 266 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 267 | #endif |
| 268 | |
| 269 | #ifndef WTERMSIG |
| 270 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 271 | #endif |
| 272 | |
| 273 | #endif /* UNION_WAIT */ |
| 274 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 275 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 276 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 277 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 278 | #define USE_CTERMID_R |
| 279 | #endif |
| 280 | |
| 281 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 282 | #define USE_TMPNAM_R |
| 283 | #endif |
| 284 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 285 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 286 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 287 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 288 | # define STAT _stati64 |
| 289 | # define FSTAT _fstati64 |
| 290 | # define STRUCT_STAT struct _stati64 |
| 291 | #else |
| 292 | # define STAT stat |
| 293 | # define FSTAT fstat |
| 294 | # define STRUCT_STAT struct stat |
| 295 | #endif |
| 296 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 297 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 298 | #include <sys/mkdev.h> |
| 299 | #else |
| 300 | #if defined(MAJOR_IN_SYSMACROS) |
| 301 | #include <sys/sysmacros.h> |
| 302 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 303 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 304 | #include <sys/mkdev.h> |
| 305 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 306 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 307 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 308 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 309 | #ifdef WITH_NEXT_FRAMEWORK |
| 310 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 311 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 312 | */ |
| 313 | #include <crt_externs.h> |
| 314 | static char **environ; |
| 315 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 316 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 317 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 319 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 320 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 321 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 322 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 323 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 324 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 325 | if (d == NULL) |
| 326 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 327 | #ifdef WITH_NEXT_FRAMEWORK |
| 328 | if (environ == NULL) |
| 329 | environ = *_NSGetEnviron(); |
| 330 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 331 | if (environ == NULL) |
| 332 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 333 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 334 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 335 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 336 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 337 | char *p = strchr(*e, '='); |
| 338 | if (p == NULL) |
| 339 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 340 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 341 | if (k == NULL) { |
| 342 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 343 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 344 | } |
| 345 | v = PyString_FromString(p+1); |
| 346 | if (v == NULL) { |
| 347 | PyErr_Clear(); |
| 348 | Py_DECREF(k); |
| 349 | continue; |
| 350 | } |
| 351 | if (PyDict_GetItem(d, k) == NULL) { |
| 352 | if (PyDict_SetItem(d, k, v) != 0) |
| 353 | PyErr_Clear(); |
| 354 | } |
| 355 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 356 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 357 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 358 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 359 | { |
| 360 | APIRET rc; |
| 361 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 362 | |
| 363 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 364 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 365 | PyObject *v = PyString_FromString(buffer); |
| 366 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 367 | Py_DECREF(v); |
| 368 | } |
| 369 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 370 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 371 | PyObject *v = PyString_FromString(buffer); |
| 372 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 373 | Py_DECREF(v); |
| 374 | } |
| 375 | } |
| 376 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 377 | return d; |
| 378 | } |
| 379 | |
| 380 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 381 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 382 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 383 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 384 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 385 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 386 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 387 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 388 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 389 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 390 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 391 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 394 | #ifdef Py_WIN_WIDE_FILENAMES |
| 395 | static PyObject * |
| 396 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 397 | { |
| 398 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 399 | } |
| 400 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 401 | |
| 402 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 403 | static PyObject * |
| 404 | posix_error_with_allocated_filename(char* name) |
| 405 | { |
| 406 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 407 | PyMem_Free(name); |
| 408 | return rc; |
| 409 | } |
| 410 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 411 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 412 | static PyObject * |
| 413 | win32_error(char* function, char* filename) |
| 414 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 415 | /* XXX We should pass the function name along in the future. |
| 416 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 417 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 418 | Windows error object, which is non-trivial. |
| 419 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 420 | errno = GetLastError(); |
| 421 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 422 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 423 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 424 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 425 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 426 | |
| 427 | #ifdef Py_WIN_WIDE_FILENAMES |
| 428 | static PyObject * |
| 429 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 430 | { |
| 431 | /* XXX - see win32_error for comments on 'function' */ |
| 432 | errno = GetLastError(); |
| 433 | if (filename) |
| 434 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 435 | else |
| 436 | return PyErr_SetFromWindowsErr(errno); |
| 437 | } |
| 438 | |
| 439 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 440 | { |
| 441 | /* XXX Perhaps we should make this API an alias of |
| 442 | PyObject_Unicode() instead ?! */ |
| 443 | if (PyUnicode_CheckExact(obj)) { |
| 444 | Py_INCREF(obj); |
| 445 | return obj; |
| 446 | } |
| 447 | if (PyUnicode_Check(obj)) { |
| 448 | /* For a Unicode subtype that's not a Unicode object, |
| 449 | return a true Unicode object with the same data. */ |
| 450 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 451 | PyUnicode_GET_SIZE(obj)); |
| 452 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 453 | return PyUnicode_FromEncodedObject(obj, |
| 454 | Py_FileSystemDefaultEncoding, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 455 | "strict"); |
| 456 | } |
| 457 | |
| 458 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 459 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 460 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 461 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 462 | #if defined(PYOS_OS2) |
| 463 | /********************************************************************** |
| 464 | * Helper Function to Trim and Format OS/2 Messages |
| 465 | **********************************************************************/ |
| 466 | static void |
| 467 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 468 | { |
| 469 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 470 | |
| 471 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 472 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 473 | |
| 474 | while (lastc > msgbuf && isspace(*lastc)) |
| 475 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 476 | } |
| 477 | |
| 478 | /* Add Optional Reason Text */ |
| 479 | if (reason) { |
| 480 | strcat(msgbuf, " : "); |
| 481 | strcat(msgbuf, reason); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /********************************************************************** |
| 486 | * Decode an OS/2 Operating System Error Code |
| 487 | * |
| 488 | * A convenience function to lookup an OS/2 error code and return a |
| 489 | * text message we can use to raise a Python exception. |
| 490 | * |
| 491 | * Notes: |
| 492 | * The messages for errors returned from the OS/2 kernel reside in |
| 493 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 494 | * |
| 495 | **********************************************************************/ |
| 496 | static char * |
| 497 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 498 | { |
| 499 | APIRET rc; |
| 500 | ULONG msglen; |
| 501 | |
| 502 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 503 | Py_BEGIN_ALLOW_THREADS |
| 504 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 505 | errorcode, "oso001.msg", &msglen); |
| 506 | Py_END_ALLOW_THREADS |
| 507 | |
| 508 | if (rc == NO_ERROR) |
| 509 | os2_formatmsg(msgbuf, msglen, reason); |
| 510 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 511 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 512 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 513 | |
| 514 | return msgbuf; |
| 515 | } |
| 516 | |
| 517 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 518 | errors are not in a global variable e.g. 'errno' nor are |
| 519 | they congruent with posix error numbers. */ |
| 520 | |
| 521 | static PyObject * os2_error(int code) |
| 522 | { |
| 523 | char text[1024]; |
| 524 | PyObject *v; |
| 525 | |
| 526 | os2_strerror(text, sizeof(text), code, ""); |
| 527 | |
| 528 | v = Py_BuildValue("(is)", code, text); |
| 529 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 530 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 531 | Py_DECREF(v); |
| 532 | } |
| 533 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 534 | } |
| 535 | |
| 536 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 537 | |
| 538 | /* POSIX generic methods */ |
| 539 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 540 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 541 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 542 | { |
| 543 | int fd; |
| 544 | int res; |
| 545 | fd = PyObject_AsFileDescriptor(fdobj); |
| 546 | if (fd < 0) |
| 547 | return NULL; |
| 548 | Py_BEGIN_ALLOW_THREADS |
| 549 | res = (*func)(fd); |
| 550 | Py_END_ALLOW_THREADS |
| 551 | if (res < 0) |
| 552 | return posix_error(); |
| 553 | Py_INCREF(Py_None); |
| 554 | return Py_None; |
| 555 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 556 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 557 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 558 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 559 | unicode_file_names(void) |
| 560 | { |
| 561 | static int canusewide = -1; |
| 562 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 563 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 564 | the Windows NT family. */ |
| 565 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 566 | } |
| 567 | return canusewide; |
| 568 | } |
| 569 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 570 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 571 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 572 | posix_1str(PyObject *args, char *format, int (*func)(const char*), |
| 573 | char *wformat, int (*wfunc)(const Py_UNICODE*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 574 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 575 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 576 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 577 | #ifdef Py_WIN_WIDE_FILENAMES |
| 578 | if (unicode_file_names()) { |
| 579 | PyUnicodeObject *po; |
| 580 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 581 | Py_BEGIN_ALLOW_THREADS |
| 582 | /* PyUnicode_AS_UNICODE OK without thread |
| 583 | lock as it is a simple dereference. */ |
| 584 | res = (*wfunc)(PyUnicode_AS_UNICODE(po)); |
| 585 | Py_END_ALLOW_THREADS |
| 586 | if (res < 0) |
Mark Hammond | d389036 | 2002-10-03 07:24:48 +0000 | [diff] [blame] | 587 | return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 588 | Py_INCREF(Py_None); |
| 589 | return Py_None; |
| 590 | } |
| 591 | /* Drop the argument parsing error as narrow |
| 592 | strings are also valid. */ |
| 593 | PyErr_Clear(); |
| 594 | } |
| 595 | #else |
| 596 | /* Platforms that don't support Unicode filenames |
| 597 | shouldn't be passing these extra params */ |
| 598 | assert(wformat==NULL && wfunc == NULL); |
| 599 | #endif |
| 600 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 601 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 602 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 603 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 604 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 605 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 606 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 607 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 608 | return posix_error_with_allocated_filename(path1); |
| 609 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 610 | Py_INCREF(Py_None); |
| 611 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 614 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 615 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 616 | char *format, |
| 617 | int (*func)(const char *, const char *), |
| 618 | char *wformat, |
| 619 | int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 620 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 621 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 622 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 623 | #ifdef Py_WIN_WIDE_FILENAMES |
| 624 | if (unicode_file_names()) { |
| 625 | PyObject *po1; |
| 626 | PyObject *po2; |
| 627 | if (PyArg_ParseTuple(args, wformat, &po1, &po2)) { |
| 628 | if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) { |
| 629 | PyObject *wpath1; |
| 630 | PyObject *wpath2; |
| 631 | wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1); |
| 632 | wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2); |
| 633 | if (!wpath1 || !wpath2) { |
| 634 | Py_XDECREF(wpath1); |
| 635 | Py_XDECREF(wpath2); |
| 636 | return NULL; |
| 637 | } |
| 638 | Py_BEGIN_ALLOW_THREADS |
| 639 | /* PyUnicode_AS_UNICODE OK without thread |
| 640 | lock as it is a simple dereference. */ |
| 641 | res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1), |
| 642 | PyUnicode_AS_UNICODE(wpath2)); |
| 643 | Py_END_ALLOW_THREADS |
| 644 | Py_XDECREF(wpath1); |
| 645 | Py_XDECREF(wpath2); |
| 646 | if (res != 0) |
| 647 | return posix_error(); |
| 648 | Py_INCREF(Py_None); |
| 649 | return Py_None; |
| 650 | } |
| 651 | /* Else flow through as neither is Unicode. */ |
| 652 | } |
| 653 | /* Drop the argument parsing error as narrow |
| 654 | strings are also valid. */ |
| 655 | PyErr_Clear(); |
| 656 | } |
| 657 | #else |
| 658 | /* Platforms that don't support Unicode filenames |
| 659 | shouldn't be passing these extra params */ |
| 660 | assert(wformat==NULL && wfunc == NULL); |
| 661 | #endif |
| 662 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 663 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 664 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 665 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 666 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 667 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 668 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 669 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 670 | PyMem_Free(path1); |
| 671 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 672 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 673 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 674 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 675 | Py_INCREF(Py_None); |
| 676 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 679 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 680 | "stat_result: Result from stat or lstat.\n\n\ |
| 681 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 682 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 683 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 684 | \n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 685 | Posix/windows: If your platform supports st_blksize, st_blocks, or st_rdev,\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 686 | they are available as attributes only.\n\ |
| 687 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 688 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 689 | |
| 690 | static PyStructSequence_Field stat_result_fields[] = { |
| 691 | {"st_mode", "protection bits"}, |
| 692 | {"st_ino", "inode"}, |
| 693 | {"st_dev", "device"}, |
| 694 | {"st_nlink", "number of hard links"}, |
| 695 | {"st_uid", "user ID of owner"}, |
| 696 | {"st_gid", "group ID of owner"}, |
| 697 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 698 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 699 | {NULL, "integer time of last access"}, |
| 700 | {NULL, "integer time of last modification"}, |
| 701 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 702 | {"st_atime", "time of last access"}, |
| 703 | {"st_mtime", "time of last modification"}, |
| 704 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 705 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 706 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 707 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 708 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 709 | {"st_blocks", "number of blocks allocated"}, |
| 710 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 711 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 712 | {"st_rdev", "device type (if inode device)"}, |
| 713 | #endif |
| 714 | {0} |
| 715 | }; |
| 716 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 717 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 718 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 719 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 720 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 721 | #endif |
| 722 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 723 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 724 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 725 | #else |
| 726 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 727 | #endif |
| 728 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 729 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 730 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 731 | #else |
| 732 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 733 | #endif |
| 734 | |
| 735 | static PyStructSequence_Desc stat_result_desc = { |
| 736 | "stat_result", /* name */ |
| 737 | stat_result__doc__, /* doc */ |
| 738 | stat_result_fields, |
| 739 | 10 |
| 740 | }; |
| 741 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 742 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 743 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 744 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 745 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 746 | 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] | 747 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 748 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 749 | |
| 750 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 751 | {"f_bsize", }, |
| 752 | {"f_frsize", }, |
| 753 | {"f_blocks", }, |
| 754 | {"f_bfree", }, |
| 755 | {"f_bavail", }, |
| 756 | {"f_files", }, |
| 757 | {"f_ffree", }, |
| 758 | {"f_favail", }, |
| 759 | {"f_flag", }, |
| 760 | {"f_namemax",}, |
| 761 | {0} |
| 762 | }; |
| 763 | |
| 764 | static PyStructSequence_Desc statvfs_result_desc = { |
| 765 | "statvfs_result", /* name */ |
| 766 | statvfs_result__doc__, /* doc */ |
| 767 | statvfs_result_fields, |
| 768 | 10 |
| 769 | }; |
| 770 | |
| 771 | static PyTypeObject StatResultType; |
| 772 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 773 | static newfunc structseq_new; |
| 774 | |
| 775 | static PyObject * |
| 776 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 777 | { |
| 778 | PyStructSequence *result; |
| 779 | int i; |
| 780 | |
| 781 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 782 | if (!result) |
| 783 | return NULL; |
| 784 | /* If we have been initialized from a tuple, |
| 785 | st_?time might be set to None. Initialize it |
| 786 | from the int slots. */ |
| 787 | for (i = 7; i <= 9; i++) { |
| 788 | if (result->ob_item[i+3] == Py_None) { |
| 789 | Py_DECREF(Py_None); |
| 790 | Py_INCREF(result->ob_item[i]); |
| 791 | result->ob_item[i+3] = result->ob_item[i]; |
| 792 | } |
| 793 | } |
| 794 | return (PyObject*)result; |
| 795 | } |
| 796 | |
| 797 | |
| 798 | |
| 799 | /* If true, st_?time is float. */ |
| 800 | static int _stat_float_times = 0; |
| 801 | |
| 802 | PyDoc_STRVAR(stat_float_times__doc__, |
| 803 | "stat_float_times([newval]) -> oldval\n\n\ |
| 804 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 805 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 806 | future calls return ints. \n\ |
| 807 | If newval is omitted, return the current setting.\n"); |
| 808 | |
| 809 | static PyObject* |
| 810 | stat_float_times(PyObject* self, PyObject *args) |
| 811 | { |
| 812 | int newval = -1; |
| 813 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 814 | return NULL; |
| 815 | if (newval == -1) |
| 816 | /* Return old value */ |
| 817 | return PyBool_FromLong(_stat_float_times); |
| 818 | _stat_float_times = newval; |
| 819 | Py_INCREF(Py_None); |
| 820 | return Py_None; |
| 821 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 822 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 823 | static void |
| 824 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 825 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 826 | PyObject *fval,*ival; |
| 827 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 828 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 829 | #else |
| 830 | ival = PyInt_FromLong((long)sec); |
| 831 | #endif |
| 832 | if (_stat_float_times) { |
| 833 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 834 | } else { |
| 835 | fval = ival; |
| 836 | Py_INCREF(fval); |
| 837 | } |
| 838 | PyStructSequence_SET_ITEM(v, index, ival); |
| 839 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 842 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 843 | (used by posix_stat() and posix_fstat()) */ |
| 844 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 845 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 846 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 847 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 848 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 849 | if (v == NULL) |
| 850 | return NULL; |
| 851 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 852 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 853 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 854 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 855 | PyLong_FromLongLong((PY_LONG_LONG)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 856 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 857 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 858 | #endif |
| 859 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 860 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 861 | PyLong_FromLongLong((PY_LONG_LONG)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 862 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 863 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 864 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 865 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 866 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 867 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 868 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 869 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 870 | PyLong_FromLongLong((PY_LONG_LONG)st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 871 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 872 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 873 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 874 | |
| 875 | #ifdef HAVE_STAT_TV_NSEC |
| 876 | ansec = st.st_atim.tv_nsec; |
| 877 | mnsec = st.st_mtim.tv_nsec; |
| 878 | cnsec = st.st_ctim.tv_nsec; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 879 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 880 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 881 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 882 | fill_time(v, 7, st.st_atime, ansec); |
| 883 | fill_time(v, 8, st.st_mtime, mnsec); |
| 884 | fill_time(v, 9, st.st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 885 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 886 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 887 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 888 | PyInt_FromLong((long)st.st_blksize)); |
| 889 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 890 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 891 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 892 | PyInt_FromLong((long)st.st_blocks)); |
| 893 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 894 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 895 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 896 | PyInt_FromLong((long)st.st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 897 | #endif |
| 898 | |
| 899 | if (PyErr_Occurred()) { |
| 900 | Py_DECREF(v); |
| 901 | return NULL; |
| 902 | } |
| 903 | |
| 904 | return v; |
| 905 | } |
| 906 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 907 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 908 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 909 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 910 | #ifdef __VMS |
| 911 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 912 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 913 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 914 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 915 | char *wformat, |
| 916 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 917 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 918 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 919 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 920 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 921 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 922 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 923 | #ifdef MS_WINDOWS |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 924 | int pathlen; |
| 925 | char pathcopy[MAX_PATH]; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 926 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 927 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 928 | |
| 929 | #ifdef Py_WIN_WIDE_FILENAMES |
| 930 | /* If on wide-character-capable OS see if argument |
| 931 | is Unicode and if so use wide API. */ |
| 932 | if (unicode_file_names()) { |
| 933 | PyUnicodeObject *po; |
| 934 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 935 | Py_UNICODE wpath[MAX_PATH+1]; |
| 936 | pathlen = wcslen(PyUnicode_AS_UNICODE(po)); |
| 937 | /* the library call can blow up if the file name is too long! */ |
| 938 | if (pathlen > MAX_PATH) { |
| 939 | errno = ENAMETOOLONG; |
| 940 | return posix_error(); |
| 941 | } |
| 942 | wcscpy(wpath, PyUnicode_AS_UNICODE(po)); |
| 943 | /* Remove trailing slash or backslash, unless it's the current |
| 944 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 945 | */ |
| 946 | if (pathlen > 0 && |
| 947 | (wpath[pathlen-1]== L'\\' || wpath[pathlen-1] == L'/')) { |
| 948 | /* It does end with a slash -- exempt the root drive cases. */ |
| 949 | /* XXX UNC root drives should also be exempted? */ |
| 950 | if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':')) |
| 951 | /* leave it alone */; |
| 952 | else { |
| 953 | /* nuke the trailing backslash */ |
| 954 | wpath[pathlen-1] = L'\0'; |
| 955 | } |
| 956 | } |
| 957 | Py_BEGIN_ALLOW_THREADS |
| 958 | /* PyUnicode_AS_UNICODE result OK without |
| 959 | thread lock as it is a simple dereference. */ |
| 960 | res = wstatfunc(wpath, &st); |
| 961 | Py_END_ALLOW_THREADS |
| 962 | if (res != 0) |
| 963 | return posix_error_with_unicode_filename(wpath); |
| 964 | return _pystat_fromstructstat(st); |
| 965 | } |
| 966 | /* Drop the argument parsing error as narrow strings |
| 967 | are also valid. */ |
| 968 | PyErr_Clear(); |
| 969 | } |
| 970 | #endif |
| 971 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 972 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 973 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 974 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 975 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 976 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 977 | #ifdef MS_WINDOWS |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 978 | pathlen = strlen(path); |
| 979 | /* the library call can blow up if the file name is too long! */ |
| 980 | if (pathlen > MAX_PATH) { |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 981 | PyMem_Free(pathfree); |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 982 | errno = ENAMETOOLONG; |
| 983 | return posix_error(); |
| 984 | } |
| 985 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 986 | /* Remove trailing slash or backslash, unless it's the current |
| 987 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 988 | */ |
| 989 | if (pathlen > 0 && |
| 990 | (path[pathlen-1]== '\\' || path[pathlen-1] == '/')) { |
| 991 | /* It does end with a slash -- exempt the root drive cases. */ |
| 992 | /* XXX UNC root drives should also be exempted? */ |
| 993 | if (pathlen == 1 || (pathlen == 3 && path[1] == ':')) |
| 994 | /* leave it alone */; |
| 995 | else { |
| 996 | /* nuke the trailing backslash */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 997 | strncpy(pathcopy, path, pathlen); |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 998 | pathcopy[pathlen-1] = '\0'; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 999 | path = pathcopy; |
| 1000 | } |
| 1001 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1002 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1003 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1004 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1005 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1006 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1007 | if (res != 0) |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1008 | return posix_error_with_allocated_filename(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1009 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1010 | PyMem_Free(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1011 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | |
| 1015 | /* POSIX methods */ |
| 1016 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1017 | PyDoc_STRVAR(posix_access__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1018 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1019 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1020 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1021 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1022 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1023 | 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] | 1024 | |
| 1025 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1026 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1027 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1028 | char *path; |
| 1029 | int mode; |
| 1030 | int res; |
| 1031 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1032 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1033 | if (unicode_file_names()) { |
| 1034 | PyUnicodeObject *po; |
| 1035 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1036 | Py_BEGIN_ALLOW_THREADS |
| 1037 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1038 | it is a simple dereference. */ |
| 1039 | res = _waccess(PyUnicode_AS_UNICODE(po), mode); |
| 1040 | Py_END_ALLOW_THREADS |
| 1041 | return(PyBool_FromLong(res == 0)); |
| 1042 | } |
| 1043 | /* Drop the argument parsing error as narrow strings |
| 1044 | are also valid. */ |
| 1045 | PyErr_Clear(); |
| 1046 | } |
| 1047 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1048 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1049 | return NULL; |
| 1050 | Py_BEGIN_ALLOW_THREADS |
| 1051 | res = access(path, mode); |
| 1052 | Py_END_ALLOW_THREADS |
Guido van Rossum | 674deb2 | 2002-09-01 15:06:28 +0000 | [diff] [blame] | 1053 | return(PyBool_FromLong(res == 0)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1056 | #ifndef F_OK |
| 1057 | #define F_OK 0 |
| 1058 | #endif |
| 1059 | #ifndef R_OK |
| 1060 | #define R_OK 4 |
| 1061 | #endif |
| 1062 | #ifndef W_OK |
| 1063 | #define W_OK 2 |
| 1064 | #endif |
| 1065 | #ifndef X_OK |
| 1066 | #define X_OK 1 |
| 1067 | #endif |
| 1068 | |
| 1069 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1070 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1071 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1072 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1073 | |
| 1074 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1075 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1076 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1077 | int id; |
| 1078 | char *ret; |
| 1079 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1080 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1081 | return NULL; |
| 1082 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1083 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1084 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1085 | if (id == 0) { |
| 1086 | ret = ttyname(); |
| 1087 | } |
| 1088 | else { |
| 1089 | ret = NULL; |
| 1090 | } |
| 1091 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1092 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1093 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1094 | if (ret == NULL) |
| 1095 | return(posix_error()); |
| 1096 | return(PyString_FromString(ret)); |
| 1097 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1098 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1099 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1100 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1101 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1102 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1103 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1104 | |
| 1105 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1106 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1107 | { |
| 1108 | char *ret; |
| 1109 | char buffer[L_ctermid]; |
| 1110 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1111 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1112 | ret = ctermid_r(buffer); |
| 1113 | #else |
| 1114 | ret = ctermid(buffer); |
| 1115 | #endif |
| 1116 | if (ret == NULL) |
| 1117 | return(posix_error()); |
| 1118 | return(PyString_FromString(buffer)); |
| 1119 | } |
| 1120 | #endif |
| 1121 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1122 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1123 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1124 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1125 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1126 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1127 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1128 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1129 | #ifdef MS_WINDOWS |
| 1130 | return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir); |
| 1131 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1132 | return posix_1str(args, "et:chdir", _chdir2, NULL, NULL); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1133 | #elif defined(__VMS) |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1134 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1135 | NULL, NULL); |
| 1136 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1137 | return posix_1str(args, "et:chdir", chdir, NULL, NULL); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1138 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1141 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1142 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1143 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1144 | 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] | 1145 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1146 | |
| 1147 | static PyObject * |
| 1148 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1149 | { |
| 1150 | return posix_fildes(fdobj, fchdir); |
| 1151 | } |
| 1152 | #endif /* HAVE_FCHDIR */ |
| 1153 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1154 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1155 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1156 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1157 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1158 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1159 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1160 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1161 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1162 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1163 | int i; |
| 1164 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1165 | if (!PyArg_ParseTuple(args, "eti", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1166 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1167 | return NULL; |
| 1168 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1169 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1170 | Py_END_ALLOW_THREADS |
| 1171 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1172 | return posix_error_with_allocated_filename(path); |
| 1173 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1174 | Py_INCREF(Py_None); |
| 1175 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1178 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1179 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1180 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1181 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1182 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1183 | |
| 1184 | static PyObject * |
| 1185 | posix_chroot(PyObject *self, PyObject *args) |
| 1186 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1187 | return posix_1str(args, "et:chroot", chroot, NULL, NULL); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1188 | } |
| 1189 | #endif |
| 1190 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1191 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1192 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1193 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1194 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1195 | |
| 1196 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1197 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1198 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1199 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1200 | } |
| 1201 | #endif /* HAVE_FSYNC */ |
| 1202 | |
| 1203 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1204 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1205 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1206 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1207 | #endif |
| 1208 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1209 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1210 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1211 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1212 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1213 | |
| 1214 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1215 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1216 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1217 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1218 | } |
| 1219 | #endif /* HAVE_FDATASYNC */ |
| 1220 | |
| 1221 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1222 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1223 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1224 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1225 | 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] | 1226 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1227 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1228 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1229 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1230 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1231 | int uid, gid; |
| 1232 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1233 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1234 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1235 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1236 | return NULL; |
| 1237 | Py_BEGIN_ALLOW_THREADS |
| 1238 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1239 | Py_END_ALLOW_THREADS |
| 1240 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1241 | return posix_error_with_allocated_filename(path); |
| 1242 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1243 | Py_INCREF(Py_None); |
| 1244 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1245 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1246 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1247 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1248 | #ifdef HAVE_LCHOWN |
| 1249 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1250 | "lchown(path, uid, gid)\n\n\ |
| 1251 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1252 | This function will not follow symbolic links."); |
| 1253 | |
| 1254 | static PyObject * |
| 1255 | posix_lchown(PyObject *self, PyObject *args) |
| 1256 | { |
| 1257 | char *path = NULL; |
| 1258 | int uid, gid; |
| 1259 | int res; |
| 1260 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1261 | Py_FileSystemDefaultEncoding, &path, |
| 1262 | &uid, &gid)) |
| 1263 | return NULL; |
| 1264 | Py_BEGIN_ALLOW_THREADS |
| 1265 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1266 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1267 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1268 | return posix_error_with_allocated_filename(path); |
| 1269 | PyMem_Free(path); |
| 1270 | Py_INCREF(Py_None); |
| 1271 | return Py_None; |
| 1272 | } |
| 1273 | #endif /* HAVE_LCHOWN */ |
| 1274 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1275 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1276 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1277 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1278 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1279 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1280 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1281 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1282 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1283 | { |
| 1284 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1285 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1286 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1287 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1288 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1289 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1290 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1291 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1292 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1293 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1294 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1295 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1296 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1297 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1298 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1299 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1300 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1301 | "getcwdu() -> path\n\n\ |
| 1302 | Return a unicode string representing the current working directory."); |
| 1303 | |
| 1304 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1305 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1306 | { |
| 1307 | char buf[1026]; |
| 1308 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1309 | |
| 1310 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1311 | if (unicode_file_names()) { |
| 1312 | wchar_t *wres; |
| 1313 | wchar_t wbuf[1026]; |
| 1314 | Py_BEGIN_ALLOW_THREADS |
| 1315 | wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]); |
| 1316 | Py_END_ALLOW_THREADS |
| 1317 | if (wres == NULL) |
| 1318 | return posix_error(); |
| 1319 | return PyUnicode_FromWideChar(wbuf, wcslen(wbuf)); |
| 1320 | } |
| 1321 | #endif |
| 1322 | |
| 1323 | Py_BEGIN_ALLOW_THREADS |
| 1324 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1325 | res = _getcwd2(buf, sizeof buf); |
| 1326 | #else |
| 1327 | res = getcwd(buf, sizeof buf); |
| 1328 | #endif |
| 1329 | Py_END_ALLOW_THREADS |
| 1330 | if (res == NULL) |
| 1331 | return posix_error(); |
| 1332 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1333 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1334 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1335 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1336 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1337 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1338 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1339 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1340 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1341 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1342 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1343 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1344 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1345 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1346 | return posix_2str(args, "etet:link", link, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1347 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1348 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1349 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1350 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1351 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1352 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1353 | Return a list containing the names of the entries in the directory.\n\ |
| 1354 | \n\ |
| 1355 | path: path of directory to list\n\ |
| 1356 | \n\ |
| 1357 | 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] | 1358 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1359 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1360 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1361 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1362 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1363 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1364 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1365 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1366 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1367 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1368 | HANDLE hFindFile; |
| 1369 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1370 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 1371 | char namebuf[MAX_PATH*2+5]; |
| 1372 | char *bufptr = namebuf; |
| 1373 | int len = sizeof(namebuf)/sizeof(namebuf[0]); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1374 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1375 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1376 | /* If on wide-character-capable OS see if argument |
| 1377 | is Unicode and if so use wide API. */ |
| 1378 | if (unicode_file_names()) { |
| 1379 | PyUnicodeObject *po; |
| 1380 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1381 | WIN32_FIND_DATAW wFileData; |
| 1382 | Py_UNICODE wnamebuf[MAX_PATH*2+5]; |
| 1383 | Py_UNICODE wch; |
| 1384 | wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH); |
| 1385 | wnamebuf[MAX_PATH] = L'\0'; |
| 1386 | len = wcslen(wnamebuf); |
| 1387 | wch = (len > 0) ? wnamebuf[len-1] : L'\0'; |
| 1388 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1389 | wnamebuf[len++] = L'/'; |
| 1390 | wcscpy(wnamebuf + len, L"*.*"); |
| 1391 | if ((d = PyList_New(0)) == NULL) |
| 1392 | return NULL; |
| 1393 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1394 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1395 | errno = GetLastError(); |
| 1396 | if (errno == ERROR_FILE_NOT_FOUND) { |
| 1397 | return d; |
| 1398 | } |
| 1399 | Py_DECREF(d); |
| 1400 | return win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1401 | } |
| 1402 | do { |
| 1403 | if (wFileData.cFileName[0] == L'.' && |
| 1404 | (wFileData.cFileName[1] == L'\0' || |
| 1405 | wFileData.cFileName[1] == L'.' && |
| 1406 | wFileData.cFileName[2] == L'\0')) |
| 1407 | continue; |
| 1408 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1409 | if (v == NULL) { |
| 1410 | Py_DECREF(d); |
| 1411 | d = NULL; |
| 1412 | break; |
| 1413 | } |
| 1414 | if (PyList_Append(d, v) != 0) { |
| 1415 | Py_DECREF(v); |
| 1416 | Py_DECREF(d); |
| 1417 | d = NULL; |
| 1418 | break; |
| 1419 | } |
| 1420 | Py_DECREF(v); |
| 1421 | } while (FindNextFileW(hFindFile, &wFileData) == TRUE); |
| 1422 | |
| 1423 | if (FindClose(hFindFile) == FALSE) { |
| 1424 | Py_DECREF(d); |
| 1425 | return win32_error_unicode("FindClose", wnamebuf); |
| 1426 | } |
| 1427 | return d; |
| 1428 | } |
| 1429 | /* Drop the argument parsing error as narrow strings |
| 1430 | are also valid. */ |
| 1431 | PyErr_Clear(); |
| 1432 | } |
| 1433 | #endif |
| 1434 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1435 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1436 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1437 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1438 | if (len > 0) { |
| 1439 | char ch = namebuf[len-1]; |
| 1440 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 1441 | namebuf[len++] = '/'; |
| 1442 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1443 | strcpy(namebuf + len, "*.*"); |
| 1444 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1445 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1446 | return NULL; |
| 1447 | |
| 1448 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 1449 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1450 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 1451 | if (errno == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1452 | return d; |
| 1453 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1454 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1455 | } |
| 1456 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1457 | if (FileData.cFileName[0] == '.' && |
| 1458 | (FileData.cFileName[1] == '\0' || |
| 1459 | FileData.cFileName[1] == '.' && |
| 1460 | FileData.cFileName[2] == '\0')) |
| 1461 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1462 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1463 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1464 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1465 | d = NULL; |
| 1466 | break; |
| 1467 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1468 | if (PyList_Append(d, v) != 0) { |
| 1469 | Py_DECREF(v); |
| 1470 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1471 | d = NULL; |
| 1472 | break; |
| 1473 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1474 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1475 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 1476 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1477 | if (FindClose(hFindFile) == FALSE) { |
| 1478 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1479 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1480 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1481 | |
| 1482 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1483 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1484 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1485 | |
| 1486 | #ifndef MAX_PATH |
| 1487 | #define MAX_PATH CCHMAXPATH |
| 1488 | #endif |
| 1489 | char *name, *pt; |
| 1490 | int len; |
| 1491 | PyObject *d, *v; |
| 1492 | char namebuf[MAX_PATH+5]; |
| 1493 | HDIR hdir = 1; |
| 1494 | ULONG srchcnt = 1; |
| 1495 | FILEFINDBUF3 ep; |
| 1496 | APIRET rc; |
| 1497 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1498 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1499 | return NULL; |
| 1500 | if (len >= MAX_PATH) { |
| 1501 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1502 | return NULL; |
| 1503 | } |
| 1504 | strcpy(namebuf, name); |
| 1505 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1506 | if (*pt == ALTSEP) |
| 1507 | *pt = SEP; |
| 1508 | if (namebuf[len-1] != SEP) |
| 1509 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1510 | strcpy(namebuf + len, "*.*"); |
| 1511 | |
| 1512 | if ((d = PyList_New(0)) == NULL) |
| 1513 | return NULL; |
| 1514 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1515 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1516 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1517 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1518 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1519 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1520 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1521 | |
| 1522 | if (rc != NO_ERROR) { |
| 1523 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1524 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1527 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1528 | do { |
| 1529 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1530 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1531 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1532 | |
| 1533 | strcpy(namebuf, ep.achName); |
| 1534 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1535 | /* Leave Case of Name Alone -- In Native Form */ |
| 1536 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1537 | |
| 1538 | v = PyString_FromString(namebuf); |
| 1539 | if (v == NULL) { |
| 1540 | Py_DECREF(d); |
| 1541 | d = NULL; |
| 1542 | break; |
| 1543 | } |
| 1544 | if (PyList_Append(d, v) != 0) { |
| 1545 | Py_DECREF(v); |
| 1546 | Py_DECREF(d); |
| 1547 | d = NULL; |
| 1548 | break; |
| 1549 | } |
| 1550 | Py_DECREF(v); |
| 1551 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1552 | } |
| 1553 | |
| 1554 | return d; |
| 1555 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1556 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1557 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1558 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1559 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1560 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1561 | int arg_is_unicode = 1; |
| 1562 | |
| 1563 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 1564 | arg_is_unicode = 0; |
| 1565 | PyErr_Clear(); |
| 1566 | } |
| 1567 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1568 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1569 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1570 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1571 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1572 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1573 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1574 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1575 | return NULL; |
| 1576 | } |
| 1577 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1578 | if (ep->d_name[0] == '.' && |
| 1579 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1580 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1581 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1582 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1583 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1584 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1585 | d = NULL; |
| 1586 | break; |
| 1587 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1588 | #ifdef Py_USING_UNICODE |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1589 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1590 | PyObject *w; |
| 1591 | |
| 1592 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1593 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1594 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 1595 | if (w != NULL) { |
| 1596 | Py_DECREF(v); |
| 1597 | v = w; |
| 1598 | } |
| 1599 | else { |
| 1600 | /* fall back to the original byte string, as |
| 1601 | discussed in patch #683592 */ |
| 1602 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1603 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1604 | } |
| 1605 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1606 | if (PyList_Append(d, v) != 0) { |
| 1607 | Py_DECREF(v); |
| 1608 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1609 | d = NULL; |
| 1610 | break; |
| 1611 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1612 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1613 | } |
| 1614 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1615 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1616 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1617 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1618 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1619 | #endif /* which OS */ |
| 1620 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1621 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1622 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1623 | /* A helper function for abspath on win32 */ |
| 1624 | static PyObject * |
| 1625 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1626 | { |
| 1627 | /* assume encoded strings wont more than double no of chars */ |
| 1628 | char inbuf[MAX_PATH*2]; |
| 1629 | char *inbufp = inbuf; |
| 1630 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); |
| 1631 | char outbuf[MAX_PATH*2]; |
| 1632 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1633 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1634 | if (unicode_file_names()) { |
| 1635 | PyUnicodeObject *po; |
| 1636 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 1637 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 1638 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1639 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1640 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 1641 | woutbuf, &wtemp)) |
| 1642 | return win32_error("GetFullPathName", ""); |
| 1643 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 1644 | } |
| 1645 | /* Drop the argument parsing error as narrow strings |
| 1646 | are also valid. */ |
| 1647 | PyErr_Clear(); |
| 1648 | } |
| 1649 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1650 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1651 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1652 | &insize)) |
| 1653 | return NULL; |
| 1654 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1655 | outbuf, &temp)) |
| 1656 | return win32_error("GetFullPathName", inbuf); |
| 1657 | return PyString_FromString(outbuf); |
| 1658 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1659 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1660 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1661 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1662 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1663 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1664 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1665 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1666 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1667 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1668 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1669 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1670 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1671 | |
| 1672 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1673 | if (unicode_file_names()) { |
| 1674 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 1675 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1676 | Py_BEGIN_ALLOW_THREADS |
| 1677 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1678 | it is a simple dereference. */ |
| 1679 | res = _wmkdir(PyUnicode_AS_UNICODE(po)); |
| 1680 | Py_END_ALLOW_THREADS |
| 1681 | if (res < 0) |
| 1682 | return posix_error(); |
| 1683 | Py_INCREF(Py_None); |
| 1684 | return Py_None; |
| 1685 | } |
| 1686 | /* Drop the argument parsing error as narrow strings |
| 1687 | are also valid. */ |
| 1688 | PyErr_Clear(); |
| 1689 | } |
| 1690 | #endif |
| 1691 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1692 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1693 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1694 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1695 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1696 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1697 | res = mkdir(path); |
| 1698 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1699 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1700 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1701 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1702 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1703 | return posix_error_with_allocated_filename(path); |
| 1704 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1705 | Py_INCREF(Py_None); |
| 1706 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1709 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1710 | #ifdef HAVE_NICE |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1711 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) |
| 1712 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) |
| 1713 | #include <sys/resource.h> |
| 1714 | #endif |
| 1715 | #endif |
| 1716 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1717 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1718 | "nice(inc) -> new_priority\n\n\ |
| 1719 | 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] | 1720 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1721 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1722 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1723 | { |
| 1724 | int increment, value; |
| 1725 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1726 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1727 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1728 | |
| 1729 | /* There are two flavours of 'nice': one that returns the new |
| 1730 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1731 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 1732 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1733 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1734 | If we are of the nice family that returns the new priority, we |
| 1735 | need to clear errno before the call, and check if errno is filled |
| 1736 | before calling posix_error() on a returnvalue of -1, because the |
| 1737 | -1 may be the actual new priority! */ |
| 1738 | |
| 1739 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1740 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1741 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1742 | if (value == 0) |
| 1743 | value = getpriority(PRIO_PROCESS, 0); |
| 1744 | #endif |
| 1745 | if (value == -1 && errno != 0) |
| 1746 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1747 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1748 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1749 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1750 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1751 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1752 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1753 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1754 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1755 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1756 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1757 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1758 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1759 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1760 | #ifdef MS_WINDOWS |
| 1761 | return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename); |
| 1762 | #else |
| 1763 | return posix_2str(args, "etet:rename", rename, NULL, NULL); |
| 1764 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1767 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1768 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1769 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1770 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1771 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1772 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1773 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1774 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1775 | #ifdef MS_WINDOWS |
| 1776 | return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir); |
| 1777 | #else |
| 1778 | return posix_1str(args, "et:rmdir", rmdir, NULL, NULL); |
| 1779 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1782 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1783 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1784 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1785 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1786 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1787 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1788 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1789 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1790 | #ifdef MS_WINDOWS |
| 1791 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64); |
| 1792 | #else |
| 1793 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 1794 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1797 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1798 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1799 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1800 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1801 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1802 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1803 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1804 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1805 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1806 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1807 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1808 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1809 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1810 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1811 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1812 | Py_END_ALLOW_THREADS |
| 1813 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1814 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1815 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1816 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1817 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1818 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1819 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1820 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1821 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1822 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1823 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1824 | { |
| 1825 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1826 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1827 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 1828 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1829 | if (i < 0) |
| 1830 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1831 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1834 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1835 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1836 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1837 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1838 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1839 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1840 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1841 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1842 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1843 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1844 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1845 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1846 | #ifdef MS_WINDOWS |
| 1847 | return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink); |
| 1848 | #else |
| 1849 | return posix_1str(args, "et:remove", unlink, NULL, NULL); |
| 1850 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1853 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1854 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1855 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1856 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1857 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1858 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1859 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1860 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1861 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1862 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1863 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1864 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1865 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1866 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1867 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1868 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1869 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1870 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1871 | u.sysname, |
| 1872 | u.nodename, |
| 1873 | u.release, |
| 1874 | u.version, |
| 1875 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1876 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1877 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1878 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1879 | static int |
| 1880 | extract_time(PyObject *t, long* sec, long* usec) |
| 1881 | { |
| 1882 | long intval; |
| 1883 | if (PyFloat_Check(t)) { |
| 1884 | double tval = PyFloat_AsDouble(t); |
| 1885 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 1886 | if (!intobj) |
| 1887 | return -1; |
| 1888 | intval = PyInt_AsLong(intobj); |
| 1889 | Py_DECREF(intobj); |
| 1890 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 1891 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1892 | if (*usec < 0) |
| 1893 | /* If rounding gave us a negative number, |
| 1894 | truncate. */ |
| 1895 | *usec = 0; |
| 1896 | return 0; |
| 1897 | } |
| 1898 | intval = PyInt_AsLong(t); |
| 1899 | if (intval == -1 && PyErr_Occurred()) |
| 1900 | return -1; |
| 1901 | *sec = intval; |
| 1902 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 1903 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1904 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1905 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1906 | PyDoc_STRVAR(posix_utime__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1907 | "utime(path, (atime, utime))\n\ |
| 1908 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1909 | 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] | 1910 | 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] | 1911 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1912 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1913 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1914 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1915 | char *path; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1916 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1917 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1918 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1919 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1920 | #if defined(HAVE_UTIMES) |
| 1921 | struct timeval buf[2]; |
| 1922 | #define ATIME buf[0].tv_sec |
| 1923 | #define MTIME buf[1].tv_sec |
| 1924 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1925 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1926 | struct utimbuf buf; |
| 1927 | #define ATIME buf.actime |
| 1928 | #define MTIME buf.modtime |
| 1929 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1930 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1931 | time_t buf[2]; |
| 1932 | #define ATIME buf[0] |
| 1933 | #define MTIME buf[1] |
| 1934 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1935 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1936 | |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1937 | if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1938 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1939 | if (arg == Py_None) { |
| 1940 | /* optional time values not given */ |
| 1941 | Py_BEGIN_ALLOW_THREADS |
| 1942 | res = utime(path, NULL); |
| 1943 | Py_END_ALLOW_THREADS |
| 1944 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1945 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1946 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1947 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1948 | return NULL; |
| 1949 | } |
| 1950 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1951 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 1952 | &atime, &ausec) == -1) |
| 1953 | return NULL; |
| 1954 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 1955 | &mtime, &musec) == -1) |
| 1956 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1957 | ATIME = atime; |
| 1958 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1959 | #ifdef HAVE_UTIMES |
| 1960 | buf[0].tv_usec = ausec; |
| 1961 | buf[1].tv_usec = musec; |
| 1962 | Py_BEGIN_ALLOW_THREADS |
| 1963 | res = utimes(path, buf); |
| 1964 | Py_END_ALLOW_THREADS |
| 1965 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1966 | Py_BEGIN_ALLOW_THREADS |
| 1967 | res = utime(path, UTIME_ARG); |
| 1968 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1969 | #endif |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1970 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1971 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1972 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1973 | Py_INCREF(Py_None); |
| 1974 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1975 | #undef UTIME_ARG |
| 1976 | #undef ATIME |
| 1977 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1980 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1981 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1982 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1983 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1984 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1985 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1986 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1987 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1988 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1989 | { |
| 1990 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1991 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1992 | return NULL; |
| 1993 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1994 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1997 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 1998 | static void |
| 1999 | free_string_array(char **array, int count) |
| 2000 | { |
| 2001 | int i; |
| 2002 | for (i = 0; i < count; i++) |
| 2003 | PyMem_Free(array[i]); |
| 2004 | PyMem_DEL(array); |
| 2005 | } |
| 2006 | #endif |
| 2007 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2008 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2009 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2010 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2011 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2012 | Execute an executable path with arguments, replacing current process.\n\ |
| 2013 | \n\ |
| 2014 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2015 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2016 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2017 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2018 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2019 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2020 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2021 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2022 | char **argvlist; |
| 2023 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2024 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2025 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2026 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2027 | argv is a list or tuple of strings. */ |
| 2028 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2029 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2030 | Py_FileSystemDefaultEncoding, |
| 2031 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2032 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2033 | if (PyList_Check(argv)) { |
| 2034 | argc = PyList_Size(argv); |
| 2035 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2036 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2037 | else if (PyTuple_Check(argv)) { |
| 2038 | argc = PyTuple_Size(argv); |
| 2039 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2040 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2041 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2042 | 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] | 2043 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2044 | return NULL; |
| 2045 | } |
| 2046 | |
| 2047 | if (argc == 0) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2048 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2049 | PyMem_Free(path); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2050 | return NULL; |
| 2051 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2052 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2053 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2054 | if (argvlist == NULL) { |
| 2055 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2056 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2057 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2058 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2059 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2060 | Py_FileSystemDefaultEncoding, |
| 2061 | &argvlist[i])) { |
| 2062 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2063 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2064 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2065 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2066 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2067 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2068 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2069 | } |
| 2070 | argvlist[argc] = NULL; |
| 2071 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2072 | #ifdef BAD_EXEC_PROTOTYPES |
| 2073 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2074 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2075 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2076 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2077 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2078 | /* If we get here it's definitely an error */ |
| 2079 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2080 | free_string_array(argvlist, argc); |
| 2081 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2082 | return posix_error(); |
| 2083 | } |
| 2084 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2085 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2086 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2087 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2088 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2089 | \n\ |
| 2090 | path: path of executable file\n\ |
| 2091 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2092 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2093 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2094 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2095 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2096 | { |
| 2097 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2098 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2099 | char **argvlist; |
| 2100 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2101 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2102 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2103 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2104 | int lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2105 | |
| 2106 | /* execve has three arguments: (path, argv, env), where |
| 2107 | argv is a list or tuple of strings and env is a dictionary |
| 2108 | like posix.environ. */ |
| 2109 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2110 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2111 | Py_FileSystemDefaultEncoding, |
| 2112 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2113 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2114 | if (PyList_Check(argv)) { |
| 2115 | argc = PyList_Size(argv); |
| 2116 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2117 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2118 | else if (PyTuple_Check(argv)) { |
| 2119 | argc = PyTuple_Size(argv); |
| 2120 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2121 | } |
| 2122 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2123 | PyErr_SetString(PyExc_TypeError, |
| 2124 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2125 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2126 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2127 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2128 | PyErr_SetString(PyExc_TypeError, |
| 2129 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2130 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2133 | if (argc == 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2134 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2135 | "execve() arg 2 must not be empty"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2136 | goto fail_0; |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2139 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2140 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2141 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2142 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2143 | } |
| 2144 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2145 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2146 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2147 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2148 | &argvlist[i])) |
| 2149 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2150 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2151 | goto fail_1; |
| 2152 | } |
| 2153 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2154 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2155 | argvlist[argc] = NULL; |
| 2156 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2157 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2158 | if (i < 0) |
| 2159 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2160 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2161 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2162 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2163 | goto fail_1; |
| 2164 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2165 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2166 | keys = PyMapping_Keys(env); |
| 2167 | vals = PyMapping_Values(env); |
| 2168 | if (!keys || !vals) |
| 2169 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2170 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2171 | PyErr_SetString(PyExc_TypeError, |
| 2172 | "execve(): env.keys() or env.values() is not a list"); |
| 2173 | goto fail_2; |
| 2174 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2175 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2176 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2177 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2178 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2179 | |
| 2180 | key = PyList_GetItem(keys, pos); |
| 2181 | val = PyList_GetItem(vals, pos); |
| 2182 | if (!key || !val) |
| 2183 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2184 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2185 | if (!PyArg_Parse( |
| 2186 | key, |
| 2187 | "s;execve() arg 3 contains a non-string key", |
| 2188 | &k) || |
| 2189 | !PyArg_Parse( |
| 2190 | val, |
| 2191 | "s;execve() arg 3 contains a non-string value", |
| 2192 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2193 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2194 | goto fail_2; |
| 2195 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2196 | |
| 2197 | #if defined(PYOS_OS2) |
| 2198 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2199 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2200 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2201 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2202 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2203 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2204 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2205 | goto fail_2; |
| 2206 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2207 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2208 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2209 | #if defined(PYOS_OS2) |
| 2210 | } |
| 2211 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2212 | } |
| 2213 | envlist[envc] = 0; |
| 2214 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2215 | |
| 2216 | #ifdef BAD_EXEC_PROTOTYPES |
| 2217 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2218 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2219 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2220 | #endif /* BAD_EXEC_PROTOTYPES */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2221 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2222 | /* If we get here it's definitely an error */ |
| 2223 | |
| 2224 | (void) posix_error(); |
| 2225 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2226 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2227 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2228 | PyMem_DEL(envlist[envc]); |
| 2229 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2230 | fail_1: |
| 2231 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2232 | Py_XDECREF(vals); |
| 2233 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2234 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2235 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2236 | return NULL; |
| 2237 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2238 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2239 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2240 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2241 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2242 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2243 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2244 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2245 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2246 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2247 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2248 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2249 | |
| 2250 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2251 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2252 | { |
| 2253 | char *path; |
| 2254 | PyObject *argv; |
| 2255 | char **argvlist; |
| 2256 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2257 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2258 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2259 | |
| 2260 | /* spawnv has three arguments: (mode, path, argv), where |
| 2261 | argv is a list or tuple of strings. */ |
| 2262 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2263 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2264 | Py_FileSystemDefaultEncoding, |
| 2265 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2266 | return NULL; |
| 2267 | if (PyList_Check(argv)) { |
| 2268 | argc = PyList_Size(argv); |
| 2269 | getitem = PyList_GetItem; |
| 2270 | } |
| 2271 | else if (PyTuple_Check(argv)) { |
| 2272 | argc = PyTuple_Size(argv); |
| 2273 | getitem = PyTuple_GetItem; |
| 2274 | } |
| 2275 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2276 | PyErr_SetString(PyExc_TypeError, |
| 2277 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2278 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2279 | return NULL; |
| 2280 | } |
| 2281 | |
| 2282 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2283 | if (argvlist == NULL) { |
| 2284 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2285 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2286 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2287 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2288 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2289 | Py_FileSystemDefaultEncoding, |
| 2290 | &argvlist[i])) { |
| 2291 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2292 | PyErr_SetString( |
| 2293 | PyExc_TypeError, |
| 2294 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2295 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2296 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2297 | } |
| 2298 | } |
| 2299 | argvlist[argc] = NULL; |
| 2300 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2301 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2302 | Py_BEGIN_ALLOW_THREADS |
| 2303 | spawnval = spawnv(mode, path, argvlist); |
| 2304 | Py_END_ALLOW_THREADS |
| 2305 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2306 | if (mode == _OLD_P_OVERLAY) |
| 2307 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2308 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2309 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2310 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2311 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2312 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2313 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2314 | free_string_array(argvlist, argc); |
| 2315 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2316 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2317 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2318 | return posix_error(); |
| 2319 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2320 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2321 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2322 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2323 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2324 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2328 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2329 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2330 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2331 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2332 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2333 | path: path of executable file\n\ |
| 2334 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2335 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2336 | |
| 2337 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2338 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2339 | { |
| 2340 | char *path; |
| 2341 | PyObject *argv, *env; |
| 2342 | char **argvlist; |
| 2343 | char **envlist; |
| 2344 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2345 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2346 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2347 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2348 | int lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2349 | |
| 2350 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 2351 | argv is a list or tuple of strings and env is a dictionary |
| 2352 | like posix.environ. */ |
| 2353 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2354 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 2355 | Py_FileSystemDefaultEncoding, |
| 2356 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2357 | return NULL; |
| 2358 | if (PyList_Check(argv)) { |
| 2359 | argc = PyList_Size(argv); |
| 2360 | getitem = PyList_GetItem; |
| 2361 | } |
| 2362 | else if (PyTuple_Check(argv)) { |
| 2363 | argc = PyTuple_Size(argv); |
| 2364 | getitem = PyTuple_GetItem; |
| 2365 | } |
| 2366 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2367 | PyErr_SetString(PyExc_TypeError, |
| 2368 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2369 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2370 | } |
| 2371 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2372 | PyErr_SetString(PyExc_TypeError, |
| 2373 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2374 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | argvlist = PyMem_NEW(char *, argc+1); |
| 2378 | if (argvlist == NULL) { |
| 2379 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2380 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2381 | } |
| 2382 | for (i = 0; i < argc; i++) { |
| 2383 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2384 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2385 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2386 | &argvlist[i])) |
| 2387 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2388 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2389 | goto fail_1; |
| 2390 | } |
| 2391 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2392 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2393 | argvlist[argc] = NULL; |
| 2394 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2395 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2396 | if (i < 0) |
| 2397 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2398 | envlist = PyMem_NEW(char *, i + 1); |
| 2399 | if (envlist == NULL) { |
| 2400 | PyErr_NoMemory(); |
| 2401 | goto fail_1; |
| 2402 | } |
| 2403 | envc = 0; |
| 2404 | keys = PyMapping_Keys(env); |
| 2405 | vals = PyMapping_Values(env); |
| 2406 | if (!keys || !vals) |
| 2407 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2408 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2409 | PyErr_SetString(PyExc_TypeError, |
| 2410 | "spawnve(): env.keys() or env.values() is not a list"); |
| 2411 | goto fail_2; |
| 2412 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2413 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2414 | for (pos = 0; pos < i; pos++) { |
| 2415 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2416 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2417 | |
| 2418 | key = PyList_GetItem(keys, pos); |
| 2419 | val = PyList_GetItem(vals, pos); |
| 2420 | if (!key || !val) |
| 2421 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2422 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2423 | if (!PyArg_Parse( |
| 2424 | key, |
| 2425 | "s;spawnve() arg 3 contains a non-string key", |
| 2426 | &k) || |
| 2427 | !PyArg_Parse( |
| 2428 | val, |
| 2429 | "s;spawnve() arg 3 contains a non-string value", |
| 2430 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2431 | { |
| 2432 | goto fail_2; |
| 2433 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2434 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2435 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2436 | if (p == NULL) { |
| 2437 | PyErr_NoMemory(); |
| 2438 | goto fail_2; |
| 2439 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2440 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2441 | envlist[envc++] = p; |
| 2442 | } |
| 2443 | envlist[envc] = 0; |
| 2444 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2445 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2446 | Py_BEGIN_ALLOW_THREADS |
| 2447 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2448 | Py_END_ALLOW_THREADS |
| 2449 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2450 | if (mode == _OLD_P_OVERLAY) |
| 2451 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2452 | |
| 2453 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2454 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2455 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2456 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2457 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2458 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2459 | (void) posix_error(); |
| 2460 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2461 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2462 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2463 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2464 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2465 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2466 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2467 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2468 | while (--envc >= 0) |
| 2469 | PyMem_DEL(envlist[envc]); |
| 2470 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2471 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2472 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2473 | Py_XDECREF(vals); |
| 2474 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2475 | fail_0: |
| 2476 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2477 | return res; |
| 2478 | } |
| 2479 | #endif /* HAVE_SPAWNV */ |
| 2480 | |
| 2481 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2482 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2483 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2484 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2485 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 2486 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2487 | 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] | 2488 | |
| 2489 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2490 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2491 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2492 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2493 | if (pid == -1) |
| 2494 | return posix_error(); |
| 2495 | PyOS_AfterFork(); |
| 2496 | return PyInt_FromLong((long)pid); |
| 2497 | } |
| 2498 | #endif |
| 2499 | |
| 2500 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2501 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2502 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2503 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2504 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2505 | 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] | 2506 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2507 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2508 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2509 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2510 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2511 | if (pid == -1) |
| 2512 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2513 | if (pid == 0) |
| 2514 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2515 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2516 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2517 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2518 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2519 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 2520 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 2521 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2522 | #define DEV_PTY_FILE "/dev/ptc" |
| 2523 | #define HAVE_DEV_PTMX |
| 2524 | #else |
| 2525 | #define DEV_PTY_FILE "/dev/ptmx" |
| 2526 | #endif |
| 2527 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2528 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2529 | #ifdef HAVE_PTY_H |
| 2530 | #include <pty.h> |
| 2531 | #else |
| 2532 | #ifdef HAVE_LIBUTIL_H |
| 2533 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2534 | #endif /* HAVE_LIBUTIL_H */ |
| 2535 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 2536 | #ifdef HAVE_STROPTS_H |
| 2537 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2538 | #endif |
| 2539 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2540 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2541 | #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] | 2542 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2543 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2544 | 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] | 2545 | |
| 2546 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2547 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2548 | { |
| 2549 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2550 | #ifndef HAVE_OPENPTY |
| 2551 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2552 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2553 | #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] | 2554 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2555 | #ifdef sun |
| 2556 | extern char *ptsname(); |
| 2557 | #endif |
| 2558 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2559 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2560 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2561 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 2562 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2563 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2564 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 2565 | if (slave_name == NULL) |
| 2566 | return posix_error(); |
| 2567 | |
| 2568 | slave_fd = open(slave_name, O_RDWR); |
| 2569 | if (slave_fd < 0) |
| 2570 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2571 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2572 | 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] | 2573 | if (master_fd < 0) |
| 2574 | return posix_error(); |
| 2575 | sig_saved = signal(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2576 | /* change permission of slave */ |
| 2577 | if (grantpt(master_fd) < 0) { |
| 2578 | signal(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2579 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2580 | } |
| 2581 | /* unlock slave */ |
| 2582 | if (unlockpt(master_fd) < 0) { |
| 2583 | signal(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2584 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2585 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2586 | signal(SIGCHLD, sig_saved); |
| 2587 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 2588 | if (slave_name == NULL) |
| 2589 | return posix_error(); |
| 2590 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 2591 | if (slave_fd < 0) |
| 2592 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2593 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2594 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 2595 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 2596 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2597 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 2598 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2599 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 2600 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2601 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2602 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2603 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2604 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2605 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2606 | |
| 2607 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2608 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2609 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2610 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 2611 | 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] | 2612 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2613 | |
| 2614 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2615 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2616 | { |
| 2617 | int master_fd, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2618 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2619 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 2620 | if (pid == -1) |
| 2621 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2622 | if (pid == 0) |
| 2623 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2624 | return Py_BuildValue("(ii)", pid, master_fd); |
| 2625 | } |
| 2626 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2627 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2628 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2629 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2630 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2631 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2632 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2633 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2634 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2635 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2636 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2637 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2638 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2639 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2640 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2641 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2642 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2643 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2644 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2645 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2646 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2647 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2648 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2649 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2650 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2651 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2652 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2653 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2654 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2655 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2656 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2657 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2658 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2659 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2660 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2661 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2662 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2663 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2664 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2665 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2666 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2667 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2668 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2669 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2670 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2671 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2672 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2673 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2674 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2677 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2678 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2679 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2680 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2681 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2682 | |
| 2683 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2684 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2685 | { |
| 2686 | PyObject *result = NULL; |
| 2687 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2688 | #ifdef NGROUPS_MAX |
| 2689 | #define MAX_GROUPS NGROUPS_MAX |
| 2690 | #else |
| 2691 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 2692 | #define MAX_GROUPS 64 |
| 2693 | #endif |
| 2694 | gid_t grouplist[MAX_GROUPS]; |
| 2695 | int n; |
| 2696 | |
| 2697 | n = getgroups(MAX_GROUPS, grouplist); |
| 2698 | if (n < 0) |
| 2699 | posix_error(); |
| 2700 | else { |
| 2701 | result = PyList_New(n); |
| 2702 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2703 | int i; |
| 2704 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2705 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2706 | if (o == NULL) { |
| 2707 | Py_DECREF(result); |
| 2708 | result = NULL; |
| 2709 | break; |
| 2710 | } |
| 2711 | PyList_SET_ITEM(result, i, o); |
| 2712 | } |
| 2713 | } |
| 2714 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2715 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2716 | return result; |
| 2717 | } |
| 2718 | #endif |
| 2719 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 2720 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 2721 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2722 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 2723 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 2724 | |
| 2725 | static PyObject * |
| 2726 | posix_getpgid(PyObject *self, PyObject *args) |
| 2727 | { |
| 2728 | int pid, pgid; |
| 2729 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 2730 | return NULL; |
| 2731 | pgid = getpgid(pid); |
| 2732 | if (pgid < 0) |
| 2733 | return posix_error(); |
| 2734 | return PyInt_FromLong((long)pgid); |
| 2735 | } |
| 2736 | #endif /* HAVE_GETPGID */ |
| 2737 | |
| 2738 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2739 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2740 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2741 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2742 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2743 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2744 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2745 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2746 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2747 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2748 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2749 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2750 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2751 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2752 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2753 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2754 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2755 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2756 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2757 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2758 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2759 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2760 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2761 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2762 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2763 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2764 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2765 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2766 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2767 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2768 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2769 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2770 | Py_INCREF(Py_None); |
| 2771 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2774 | #endif /* HAVE_SETPGRP */ |
| 2775 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2776 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2777 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2778 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2779 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2780 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2781 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2782 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2783 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2784 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2785 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2786 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2787 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2788 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2789 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2790 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2791 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2792 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2793 | |
| 2794 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2795 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2796 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2797 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2798 | char *name; |
| 2799 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2800 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2801 | errno = 0; |
| 2802 | name = getlogin(); |
| 2803 | if (name == NULL) { |
| 2804 | if (errno) |
| 2805 | posix_error(); |
| 2806 | else |
| 2807 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 2808 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2809 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2810 | else |
| 2811 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2812 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2813 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2814 | return result; |
| 2815 | } |
| 2816 | #endif |
| 2817 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2818 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2819 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2820 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2821 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2822 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2823 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2824 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2825 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2826 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2827 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2828 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2829 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2830 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2831 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2832 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2833 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2834 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2835 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2836 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2837 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2838 | { |
| 2839 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2840 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2841 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2842 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2843 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 2844 | APIRET rc; |
| 2845 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2846 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2847 | |
| 2848 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 2849 | APIRET rc; |
| 2850 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2851 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2852 | |
| 2853 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2854 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2855 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2856 | if (kill(pid, sig) == -1) |
| 2857 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2858 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2859 | Py_INCREF(Py_None); |
| 2860 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2861 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2862 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2863 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 2864 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2865 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2866 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2867 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 2868 | |
| 2869 | static PyObject * |
| 2870 | posix_killpg(PyObject *self, PyObject *args) |
| 2871 | { |
| 2872 | int pgid, sig; |
| 2873 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 2874 | return NULL; |
| 2875 | if (killpg(pgid, sig) == -1) |
| 2876 | return posix_error(); |
| 2877 | Py_INCREF(Py_None); |
| 2878 | return Py_None; |
| 2879 | } |
| 2880 | #endif |
| 2881 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2882 | #ifdef HAVE_PLOCK |
| 2883 | |
| 2884 | #ifdef HAVE_SYS_LOCK_H |
| 2885 | #include <sys/lock.h> |
| 2886 | #endif |
| 2887 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2888 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2889 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2890 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2891 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2892 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2893 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2894 | { |
| 2895 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2896 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2897 | return NULL; |
| 2898 | if (plock(op) == -1) |
| 2899 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2900 | Py_INCREF(Py_None); |
| 2901 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2902 | } |
| 2903 | #endif |
| 2904 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2905 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2906 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2907 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2908 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2909 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2910 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2911 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2912 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2913 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2914 | async_system(const char *command) |
| 2915 | { |
| 2916 | char *p, errormsg[256], args[1024]; |
| 2917 | RESULTCODES rcodes; |
| 2918 | APIRET rc; |
| 2919 | char *shell = getenv("COMSPEC"); |
| 2920 | if (!shell) |
| 2921 | shell = "cmd"; |
| 2922 | |
| 2923 | strcpy(args, shell); |
| 2924 | p = &args[ strlen(args)+1 ]; |
| 2925 | strcpy(p, "/c "); |
| 2926 | strcat(p, command); |
| 2927 | p += strlen(p) + 1; |
| 2928 | *p = '\0'; |
| 2929 | |
| 2930 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2931 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2932 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2933 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2934 | &rcodes, shell); |
| 2935 | return rc; |
| 2936 | } |
| 2937 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2938 | static FILE * |
| 2939 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2940 | { |
| 2941 | HFILE rhan, whan; |
| 2942 | FILE *retfd = NULL; |
| 2943 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 2944 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2945 | if (rc != NO_ERROR) { |
| 2946 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2947 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2948 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2949 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2950 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 2951 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2952 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2953 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2954 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2955 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2956 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 2957 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2958 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2959 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2962 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 2963 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2964 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2965 | if (rc == NO_ERROR) |
| 2966 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
| 2967 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2968 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 2969 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2970 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2971 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 2972 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2973 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2974 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2975 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2976 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2977 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 2978 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2979 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2980 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2983 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 2984 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2985 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2986 | if (rc == NO_ERROR) |
| 2987 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
| 2988 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2989 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 2990 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2991 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2992 | } else { |
| 2993 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2994 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2995 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
| 2998 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2999 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3000 | { |
| 3001 | char *name; |
| 3002 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3003 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3004 | FILE *fp; |
| 3005 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3006 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3007 | return NULL; |
| 3008 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3009 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3010 | Py_END_ALLOW_THREADS |
| 3011 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3012 | return os2_error(err); |
| 3013 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3014 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 3015 | if (f != NULL) |
| 3016 | PyFile_SetBufSize(f, bufsize); |
| 3017 | return f; |
| 3018 | } |
| 3019 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3020 | #elif defined(PYCC_GCC) |
| 3021 | |
| 3022 | /* standard posix version of popen() support */ |
| 3023 | static PyObject * |
| 3024 | posix_popen(PyObject *self, PyObject *args) |
| 3025 | { |
| 3026 | char *name; |
| 3027 | char *mode = "r"; |
| 3028 | int bufsize = -1; |
| 3029 | FILE *fp; |
| 3030 | PyObject *f; |
| 3031 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 3032 | return NULL; |
| 3033 | Py_BEGIN_ALLOW_THREADS |
| 3034 | fp = popen(name, mode); |
| 3035 | Py_END_ALLOW_THREADS |
| 3036 | if (fp == NULL) |
| 3037 | return posix_error(); |
| 3038 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 3039 | if (f != NULL) |
| 3040 | PyFile_SetBufSize(f, bufsize); |
| 3041 | return f; |
| 3042 | } |
| 3043 | |
| 3044 | /* fork() under OS/2 has lots'o'warts |
| 3045 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 3046 | * most of this code is a ripoff of the win32 code, but using the |
| 3047 | * capabilities of EMX's C library routines |
| 3048 | */ |
| 3049 | |
| 3050 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 3051 | #define POPEN_1 1 |
| 3052 | #define POPEN_2 2 |
| 3053 | #define POPEN_3 3 |
| 3054 | #define POPEN_4 4 |
| 3055 | |
| 3056 | static PyObject *_PyPopen(char *, int, int, int); |
| 3057 | static int _PyPclose(FILE *file); |
| 3058 | |
| 3059 | /* |
| 3060 | * Internal dictionary mapping popen* file pointers to process handles, |
| 3061 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3062 | * for more information on this dictionary's use. |
| 3063 | */ |
| 3064 | static PyObject *_PyPopenProcs = NULL; |
| 3065 | |
| 3066 | /* os2emx version of popen2() |
| 3067 | * |
| 3068 | * The result of this function is a pipe (file) connected to the |
| 3069 | * process's stdin, and a pipe connected to the process's stdout. |
| 3070 | */ |
| 3071 | |
| 3072 | static PyObject * |
| 3073 | os2emx_popen2(PyObject *self, PyObject *args) |
| 3074 | { |
| 3075 | PyObject *f; |
| 3076 | int tm=0; |
| 3077 | |
| 3078 | char *cmdstring; |
| 3079 | char *mode = "t"; |
| 3080 | int bufsize = -1; |
| 3081 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 3082 | return NULL; |
| 3083 | |
| 3084 | if (*mode == 't') |
| 3085 | tm = O_TEXT; |
| 3086 | else if (*mode != 'b') { |
| 3087 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3088 | return NULL; |
| 3089 | } else |
| 3090 | tm = O_BINARY; |
| 3091 | |
| 3092 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 3093 | |
| 3094 | return f; |
| 3095 | } |
| 3096 | |
| 3097 | /* |
| 3098 | * Variation on os2emx.popen2 |
| 3099 | * |
| 3100 | * The result of this function is 3 pipes - the process's stdin, |
| 3101 | * stdout and stderr |
| 3102 | */ |
| 3103 | |
| 3104 | static PyObject * |
| 3105 | os2emx_popen3(PyObject *self, PyObject *args) |
| 3106 | { |
| 3107 | PyObject *f; |
| 3108 | int tm = 0; |
| 3109 | |
| 3110 | char *cmdstring; |
| 3111 | char *mode = "t"; |
| 3112 | int bufsize = -1; |
| 3113 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 3114 | return NULL; |
| 3115 | |
| 3116 | if (*mode == 't') |
| 3117 | tm = O_TEXT; |
| 3118 | else if (*mode != 'b') { |
| 3119 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3120 | return NULL; |
| 3121 | } else |
| 3122 | tm = O_BINARY; |
| 3123 | |
| 3124 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 3125 | |
| 3126 | return f; |
| 3127 | } |
| 3128 | |
| 3129 | /* |
| 3130 | * Variation on os2emx.popen2 |
| 3131 | * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3132 | * The result of this function is 2 pipes - the processes stdin, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3133 | * and stdout+stderr combined as a single pipe. |
| 3134 | */ |
| 3135 | |
| 3136 | static PyObject * |
| 3137 | os2emx_popen4(PyObject *self, PyObject *args) |
| 3138 | { |
| 3139 | PyObject *f; |
| 3140 | int tm = 0; |
| 3141 | |
| 3142 | char *cmdstring; |
| 3143 | char *mode = "t"; |
| 3144 | int bufsize = -1; |
| 3145 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 3146 | return NULL; |
| 3147 | |
| 3148 | if (*mode == 't') |
| 3149 | tm = O_TEXT; |
| 3150 | else if (*mode != 'b') { |
| 3151 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3152 | return NULL; |
| 3153 | } else |
| 3154 | tm = O_BINARY; |
| 3155 | |
| 3156 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 3157 | |
| 3158 | return f; |
| 3159 | } |
| 3160 | |
| 3161 | /* a couple of structures for convenient handling of multiple |
| 3162 | * file handles and pipes |
| 3163 | */ |
| 3164 | struct file_ref |
| 3165 | { |
| 3166 | int handle; |
| 3167 | int flags; |
| 3168 | }; |
| 3169 | |
| 3170 | struct pipe_ref |
| 3171 | { |
| 3172 | int rd; |
| 3173 | int wr; |
| 3174 | }; |
| 3175 | |
| 3176 | /* The following code is derived from the win32 code */ |
| 3177 | |
| 3178 | static PyObject * |
| 3179 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 3180 | { |
| 3181 | struct file_ref stdio[3]; |
| 3182 | struct pipe_ref p_fd[3]; |
| 3183 | FILE *p_s[3]; |
| 3184 | int file_count, i, pipe_err, pipe_pid; |
| 3185 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 3186 | PyObject *f, *p_f[3]; |
| 3187 | |
| 3188 | /* file modes for subsequent fdopen's on pipe handles */ |
| 3189 | if (mode == O_TEXT) |
| 3190 | { |
| 3191 | rd_mode = "rt"; |
| 3192 | wr_mode = "wt"; |
| 3193 | } |
| 3194 | else |
| 3195 | { |
| 3196 | rd_mode = "rb"; |
| 3197 | wr_mode = "wb"; |
| 3198 | } |
| 3199 | |
| 3200 | /* prepare shell references */ |
| 3201 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 3202 | if ((shell = getenv("COMSPEC")) == NULL) |
| 3203 | { |
| 3204 | errno = ENOENT; |
| 3205 | return posix_error(); |
| 3206 | } |
| 3207 | |
| 3208 | sh_name = _getname(shell); |
| 3209 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 3210 | opt = "/c"; |
| 3211 | else |
| 3212 | opt = "-c"; |
| 3213 | |
| 3214 | /* save current stdio fds + their flags, and set not inheritable */ |
| 3215 | i = pipe_err = 0; |
| 3216 | while (pipe_err >= 0 && i < 3) |
| 3217 | { |
| 3218 | pipe_err = stdio[i].handle = dup(i); |
| 3219 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 3220 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 3221 | i++; |
| 3222 | } |
| 3223 | if (pipe_err < 0) |
| 3224 | { |
| 3225 | /* didn't get them all saved - clean up and bail out */ |
| 3226 | int saved_err = errno; |
| 3227 | while (i-- > 0) |
| 3228 | { |
| 3229 | close(stdio[i].handle); |
| 3230 | } |
| 3231 | errno = saved_err; |
| 3232 | return posix_error(); |
| 3233 | } |
| 3234 | |
| 3235 | /* create pipe ends */ |
| 3236 | file_count = 2; |
| 3237 | if (n == POPEN_3) |
| 3238 | file_count = 3; |
| 3239 | i = pipe_err = 0; |
| 3240 | while ((pipe_err == 0) && (i < file_count)) |
| 3241 | pipe_err = pipe((int *)&p_fd[i++]); |
| 3242 | if (pipe_err < 0) |
| 3243 | { |
| 3244 | /* didn't get them all made - clean up and bail out */ |
| 3245 | while (i-- > 0) |
| 3246 | { |
| 3247 | close(p_fd[i].wr); |
| 3248 | close(p_fd[i].rd); |
| 3249 | } |
| 3250 | errno = EPIPE; |
| 3251 | return posix_error(); |
| 3252 | } |
| 3253 | |
| 3254 | /* change the actual standard IO streams over temporarily, |
| 3255 | * making the retained pipe ends non-inheritable |
| 3256 | */ |
| 3257 | pipe_err = 0; |
| 3258 | |
| 3259 | /* - stdin */ |
| 3260 | if (dup2(p_fd[0].rd, 0) == 0) |
| 3261 | { |
| 3262 | close(p_fd[0].rd); |
| 3263 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 3264 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 3265 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 3266 | { |
| 3267 | close(p_fd[0].wr); |
| 3268 | pipe_err = -1; |
| 3269 | } |
| 3270 | } |
| 3271 | else |
| 3272 | { |
| 3273 | pipe_err = -1; |
| 3274 | } |
| 3275 | |
| 3276 | /* - stdout */ |
| 3277 | if (pipe_err == 0) |
| 3278 | { |
| 3279 | if (dup2(p_fd[1].wr, 1) == 1) |
| 3280 | { |
| 3281 | close(p_fd[1].wr); |
| 3282 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 3283 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 3284 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 3285 | { |
| 3286 | close(p_fd[1].rd); |
| 3287 | pipe_err = -1; |
| 3288 | } |
| 3289 | } |
| 3290 | else |
| 3291 | { |
| 3292 | pipe_err = -1; |
| 3293 | } |
| 3294 | } |
| 3295 | |
| 3296 | /* - stderr, as required */ |
| 3297 | if (pipe_err == 0) |
| 3298 | switch (n) |
| 3299 | { |
| 3300 | case POPEN_3: |
| 3301 | { |
| 3302 | if (dup2(p_fd[2].wr, 2) == 2) |
| 3303 | { |
| 3304 | close(p_fd[2].wr); |
| 3305 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 3306 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 3307 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 3308 | { |
| 3309 | close(p_fd[2].rd); |
| 3310 | pipe_err = -1; |
| 3311 | } |
| 3312 | } |
| 3313 | else |
| 3314 | { |
| 3315 | pipe_err = -1; |
| 3316 | } |
| 3317 | break; |
| 3318 | } |
| 3319 | |
| 3320 | case POPEN_4: |
| 3321 | { |
| 3322 | if (dup2(1, 2) != 2) |
| 3323 | { |
| 3324 | pipe_err = -1; |
| 3325 | } |
| 3326 | break; |
| 3327 | } |
| 3328 | } |
| 3329 | |
| 3330 | /* spawn the child process */ |
| 3331 | if (pipe_err == 0) |
| 3332 | { |
| 3333 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 3334 | if (pipe_pid == -1) |
| 3335 | { |
| 3336 | pipe_err = -1; |
| 3337 | } |
| 3338 | else |
| 3339 | { |
| 3340 | /* save the PID into the FILE structure |
| 3341 | * NOTE: this implementation doesn't actually |
| 3342 | * take advantage of this, but do it for |
| 3343 | * completeness - AIM Apr01 |
| 3344 | */ |
| 3345 | for (i = 0; i < file_count; i++) |
| 3346 | p_s[i]->_pid = pipe_pid; |
| 3347 | } |
| 3348 | } |
| 3349 | |
| 3350 | /* reset standard IO to normal */ |
| 3351 | for (i = 0; i < 3; i++) |
| 3352 | { |
| 3353 | dup2(stdio[i].handle, i); |
| 3354 | fcntl(i, F_SETFD, stdio[i].flags); |
| 3355 | close(stdio[i].handle); |
| 3356 | } |
| 3357 | |
| 3358 | /* if any remnant problems, clean up and bail out */ |
| 3359 | if (pipe_err < 0) |
| 3360 | { |
| 3361 | for (i = 0; i < 3; i++) |
| 3362 | { |
| 3363 | close(p_fd[i].rd); |
| 3364 | close(p_fd[i].wr); |
| 3365 | } |
| 3366 | errno = EPIPE; |
| 3367 | return posix_error_with_filename(cmdstring); |
| 3368 | } |
| 3369 | |
| 3370 | /* build tuple of file objects to return */ |
| 3371 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 3372 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3373 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3374 | PyFile_SetBufSize(p_f[1], bufsize); |
| 3375 | if (n == POPEN_3) |
| 3376 | { |
| 3377 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3378 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3379 | f = Py_BuildValue("OOO", p_f[0], p_f[1], p_f[2]); |
| 3380 | } |
| 3381 | else |
| 3382 | f = Py_BuildValue("OO", p_f[0], p_f[1]); |
| 3383 | |
| 3384 | /* |
| 3385 | * Insert the files we've created into the process dictionary |
| 3386 | * all referencing the list with the process handle and the |
| 3387 | * initial number of files (see description below in _PyPclose). |
| 3388 | * Since if _PyPclose later tried to wait on a process when all |
| 3389 | * handles weren't closed, it could create a deadlock with the |
| 3390 | * child, we spend some energy here to try to ensure that we |
| 3391 | * either insert all file handles into the dictionary or none |
| 3392 | * at all. It's a little clumsy with the various popen modes |
| 3393 | * and variable number of files involved. |
| 3394 | */ |
| 3395 | if (!_PyPopenProcs) |
| 3396 | { |
| 3397 | _PyPopenProcs = PyDict_New(); |
| 3398 | } |
| 3399 | |
| 3400 | if (_PyPopenProcs) |
| 3401 | { |
| 3402 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 3403 | int ins_rc[3]; |
| 3404 | |
| 3405 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 3406 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 3407 | |
| 3408 | procObj = PyList_New(2); |
| 3409 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 3410 | intObj = PyInt_FromLong((long) file_count); |
| 3411 | |
| 3412 | if (procObj && pidObj && intObj) |
| 3413 | { |
| 3414 | PyList_SetItem(procObj, 0, pidObj); |
| 3415 | PyList_SetItem(procObj, 1, intObj); |
| 3416 | |
| 3417 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 3418 | if (fileObj[0]) |
| 3419 | { |
| 3420 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 3421 | fileObj[0], |
| 3422 | procObj); |
| 3423 | } |
| 3424 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 3425 | if (fileObj[1]) |
| 3426 | { |
| 3427 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 3428 | fileObj[1], |
| 3429 | procObj); |
| 3430 | } |
| 3431 | if (file_count >= 3) |
| 3432 | { |
| 3433 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 3434 | if (fileObj[2]) |
| 3435 | { |
| 3436 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 3437 | fileObj[2], |
| 3438 | procObj); |
| 3439 | } |
| 3440 | } |
| 3441 | |
| 3442 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 3443 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 3444 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 3445 | { |
| 3446 | /* Something failed - remove any dictionary |
| 3447 | * entries that did make it. |
| 3448 | */ |
| 3449 | if (!ins_rc[0] && fileObj[0]) |
| 3450 | { |
| 3451 | PyDict_DelItem(_PyPopenProcs, |
| 3452 | fileObj[0]); |
| 3453 | } |
| 3454 | if (!ins_rc[1] && fileObj[1]) |
| 3455 | { |
| 3456 | PyDict_DelItem(_PyPopenProcs, |
| 3457 | fileObj[1]); |
| 3458 | } |
| 3459 | if (!ins_rc[2] && fileObj[2]) |
| 3460 | { |
| 3461 | PyDict_DelItem(_PyPopenProcs, |
| 3462 | fileObj[2]); |
| 3463 | } |
| 3464 | } |
| 3465 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3466 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3467 | /* |
| 3468 | * Clean up our localized references for the dictionary keys |
| 3469 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 3470 | * that got placed in the dictionary. |
| 3471 | */ |
| 3472 | Py_XDECREF(procObj); |
| 3473 | Py_XDECREF(fileObj[0]); |
| 3474 | Py_XDECREF(fileObj[1]); |
| 3475 | Py_XDECREF(fileObj[2]); |
| 3476 | } |
| 3477 | |
| 3478 | /* Child is launched. */ |
| 3479 | return f; |
| 3480 | } |
| 3481 | |
| 3482 | /* |
| 3483 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 3484 | * exit code for the child process and return as a result of the close. |
| 3485 | * |
| 3486 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 3487 | * input file pointer to information about the process that was |
| 3488 | * originally created by the popen* call that created the file pointer. |
| 3489 | * The dictionary uses the file pointer as a key (with one entry |
| 3490 | * inserted for each file returned by the original popen* call) and a |
| 3491 | * single list object as the value for all files from a single call. |
| 3492 | * The list object contains the Win32 process handle at [0], and a file |
| 3493 | * count at [1], which is initialized to the total number of file |
| 3494 | * handles using that list. |
| 3495 | * |
| 3496 | * This function closes whichever handle it is passed, and decrements |
| 3497 | * the file count in the dictionary for the process handle pointed to |
| 3498 | * by this file. On the last close (when the file count reaches zero), |
| 3499 | * this function will wait for the child process and then return its |
| 3500 | * exit code as the result of the close() operation. This permits the |
| 3501 | * files to be closed in any order - it is always the close() of the |
| 3502 | * final handle that will return the exit code. |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3503 | * |
| 3504 | * NOTE: This function is currently called with the GIL released. |
| 3505 | * hence we use the GILState API to manage our state. |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3506 | */ |
| 3507 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3508 | static int _PyPclose(FILE *file) |
| 3509 | { |
| 3510 | int result; |
| 3511 | int exit_code; |
| 3512 | int pipe_pid; |
| 3513 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 3514 | int file_count; |
| 3515 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3516 | PyGILState_STATE state; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3517 | #endif |
| 3518 | |
| 3519 | /* Close the file handle first, to ensure it can't block the |
| 3520 | * child from exiting if it's the last handle. |
| 3521 | */ |
| 3522 | result = fclose(file); |
| 3523 | |
| 3524 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3525 | state = PyGILState_Ensure(); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3526 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3527 | if (_PyPopenProcs) |
| 3528 | { |
| 3529 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 3530 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 3531 | fileObj)) != NULL && |
| 3532 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 3533 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 3534 | { |
| 3535 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 3536 | file_count = (int) PyInt_AsLong(intObj); |
| 3537 | |
| 3538 | if (file_count > 1) |
| 3539 | { |
| 3540 | /* Still other files referencing process */ |
| 3541 | file_count--; |
| 3542 | PyList_SetItem(procObj,1, |
| 3543 | PyInt_FromLong((long) file_count)); |
| 3544 | } |
| 3545 | else |
| 3546 | { |
| 3547 | /* Last file for this process */ |
| 3548 | if (result != EOF && |
| 3549 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 3550 | { |
| 3551 | /* extract exit status */ |
| 3552 | if (WIFEXITED(exit_code)) |
| 3553 | { |
| 3554 | result = WEXITSTATUS(exit_code); |
| 3555 | } |
| 3556 | else |
| 3557 | { |
| 3558 | errno = EPIPE; |
| 3559 | result = -1; |
| 3560 | } |
| 3561 | } |
| 3562 | else |
| 3563 | { |
| 3564 | /* Indicate failure - this will cause the file object |
| 3565 | * to raise an I/O error and translate the last |
| 3566 | * error code from errno. We do have a problem with |
| 3567 | * last errors that overlap the normal errno table, |
| 3568 | * but that's a consistent problem with the file object. |
| 3569 | */ |
| 3570 | result = -1; |
| 3571 | } |
| 3572 | } |
| 3573 | |
| 3574 | /* Remove this file pointer from dictionary */ |
| 3575 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 3576 | |
| 3577 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 3578 | { |
| 3579 | Py_DECREF(_PyPopenProcs); |
| 3580 | _PyPopenProcs = NULL; |
| 3581 | } |
| 3582 | |
| 3583 | } /* if object retrieval ok */ |
| 3584 | |
| 3585 | Py_XDECREF(fileObj); |
| 3586 | } /* if _PyPopenProcs */ |
| 3587 | |
| 3588 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3589 | PyGILState_Release(state); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3590 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3591 | return result; |
| 3592 | } |
| 3593 | |
| 3594 | #endif /* PYCC_??? */ |
| 3595 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3596 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3597 | |
| 3598 | /* |
| 3599 | * Portable 'popen' replacement for Win32. |
| 3600 | * |
| 3601 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 3602 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3603 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3604 | */ |
| 3605 | |
| 3606 | #include <malloc.h> |
| 3607 | #include <io.h> |
| 3608 | #include <fcntl.h> |
| 3609 | |
| 3610 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 3611 | #define POPEN_1 1 |
| 3612 | #define POPEN_2 2 |
| 3613 | #define POPEN_3 3 |
| 3614 | #define POPEN_4 4 |
| 3615 | |
| 3616 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3617 | static int _PyPclose(FILE *file); |
| 3618 | |
| 3619 | /* |
| 3620 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3621 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3622 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3623 | */ |
| 3624 | static PyObject *_PyPopenProcs = NULL; |
| 3625 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3626 | |
| 3627 | /* popen that works from a GUI. |
| 3628 | * |
| 3629 | * The result of this function is a pipe (file) connected to the |
| 3630 | * processes stdin or stdout, depending on the requested mode. |
| 3631 | */ |
| 3632 | |
| 3633 | static PyObject * |
| 3634 | posix_popen(PyObject *self, PyObject *args) |
| 3635 | { |
Raymond Hettinger | b5cb665 | 2003-09-01 22:25:41 +0000 | [diff] [blame] | 3636 | PyObject *f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3637 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3638 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3639 | char *cmdstring; |
| 3640 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3641 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3642 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3643 | return NULL; |
| 3644 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3645 | if (*mode == 'r') |
| 3646 | tm = _O_RDONLY; |
| 3647 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3648 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3649 | return NULL; |
| 3650 | } else |
| 3651 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3652 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3653 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3654 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3655 | return NULL; |
| 3656 | } |
| 3657 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3658 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3659 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3660 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3661 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3662 | else |
| 3663 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 3664 | |
| 3665 | return f; |
| 3666 | } |
| 3667 | |
| 3668 | /* Variation on win32pipe.popen |
| 3669 | * |
| 3670 | * The result of this function is a pipe (file) connected to the |
| 3671 | * process's stdin, and a pipe connected to the process's stdout. |
| 3672 | */ |
| 3673 | |
| 3674 | static PyObject * |
| 3675 | win32_popen2(PyObject *self, PyObject *args) |
| 3676 | { |
| 3677 | PyObject *f; |
| 3678 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3679 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3680 | char *cmdstring; |
| 3681 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3682 | int bufsize = -1; |
| 3683 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3684 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3685 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3686 | if (*mode == 't') |
| 3687 | tm = _O_TEXT; |
| 3688 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3689 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3690 | return NULL; |
| 3691 | } else |
| 3692 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3693 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3694 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3695 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3696 | return NULL; |
| 3697 | } |
| 3698 | |
| 3699 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3700 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3701 | return f; |
| 3702 | } |
| 3703 | |
| 3704 | /* |
| 3705 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3706 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3707 | * The result of this function is 3 pipes - the process's stdin, |
| 3708 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3709 | */ |
| 3710 | |
| 3711 | static PyObject * |
| 3712 | win32_popen3(PyObject *self, PyObject *args) |
| 3713 | { |
| 3714 | PyObject *f; |
| 3715 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3716 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3717 | char *cmdstring; |
| 3718 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3719 | int bufsize = -1; |
| 3720 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3721 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3722 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3723 | if (*mode == 't') |
| 3724 | tm = _O_TEXT; |
| 3725 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3726 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3727 | return NULL; |
| 3728 | } else |
| 3729 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3730 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3731 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3732 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3733 | return NULL; |
| 3734 | } |
| 3735 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3736 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3737 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3738 | return f; |
| 3739 | } |
| 3740 | |
| 3741 | /* |
| 3742 | * Variation on win32pipe.popen |
| 3743 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3744 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3745 | * and stdout+stderr combined as a single pipe. |
| 3746 | */ |
| 3747 | |
| 3748 | static PyObject * |
| 3749 | win32_popen4(PyObject *self, PyObject *args) |
| 3750 | { |
| 3751 | PyObject *f; |
| 3752 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3753 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3754 | char *cmdstring; |
| 3755 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3756 | int bufsize = -1; |
| 3757 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3758 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3759 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3760 | if (*mode == 't') |
| 3761 | tm = _O_TEXT; |
| 3762 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3763 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3764 | return NULL; |
| 3765 | } else |
| 3766 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3767 | |
| 3768 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3769 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3770 | return NULL; |
| 3771 | } |
| 3772 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3773 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3774 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3775 | return f; |
| 3776 | } |
| 3777 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3778 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3779 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3780 | HANDLE hStdin, |
| 3781 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3782 | HANDLE hStderr, |
| 3783 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3784 | { |
| 3785 | PROCESS_INFORMATION piProcInfo; |
| 3786 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3787 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3788 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3789 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3790 | int i; |
| 3791 | int x; |
| 3792 | |
| 3793 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3794 | char *comshell; |
| 3795 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 3796 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3797 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 3798 | return x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3799 | |
| 3800 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 3801 | * then use the w9xpopen hack. |
| 3802 | */ |
| 3803 | comshell = s1 + x; |
| 3804 | while (comshell >= s1 && *comshell != '\\') |
| 3805 | --comshell; |
| 3806 | ++comshell; |
| 3807 | |
| 3808 | if (GetVersion() < 0x80000000 && |
| 3809 | _stricmp(comshell, "command.com") != 0) { |
| 3810 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3811 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 3812 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3813 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 3814 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3815 | } |
| 3816 | else { |
| 3817 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3818 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 3819 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3820 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3821 | char modulepath[_MAX_PATH]; |
| 3822 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3823 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 3824 | for (i = x = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3825 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3826 | x = i+1; |
| 3827 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3828 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3829 | strncat(modulepath, |
| 3830 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3831 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 3832 | -strlen(modulepath)); |
| 3833 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3834 | /* Eeek - file-not-found - possibly an embedding |
| 3835 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3836 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3837 | strncpy(modulepath, |
| 3838 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3839 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 3840 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 3841 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3842 | strncat(modulepath, |
| 3843 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3844 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 3845 | -strlen(modulepath)); |
| 3846 | /* No where else to look - raise an easily identifiable |
| 3847 | error, rather than leaving Windows to report |
| 3848 | "file not found" - as the user is probably blissfully |
| 3849 | unaware this shim EXE is used, and it will confuse them. |
| 3850 | (well, it confused me for a while ;-) |
| 3851 | */ |
| 3852 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3853 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3854 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3855 | "for popen to work with your shell " |
| 3856 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3857 | szConsoleSpawn); |
| 3858 | return FALSE; |
| 3859 | } |
| 3860 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3861 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3862 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3863 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3864 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 3865 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3866 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 3867 | /* To maintain correct argument passing semantics, |
| 3868 | we pass the command-line as it stands, and allow |
| 3869 | quoting to be applied. w9xpopen.exe will then |
| 3870 | use its argv vector, and re-quote the necessary |
| 3871 | args for the ultimate child process. |
| 3872 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 3873 | PyOS_snprintf( |
| 3874 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 3875 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3876 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3877 | s1, |
| 3878 | s3, |
| 3879 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3880 | /* Not passing CREATE_NEW_CONSOLE has been known to |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3881 | cause random failures on win9x. Specifically a |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3882 | dialog: |
| 3883 | "Your program accessed mem currently in use at xxx" |
| 3884 | and a hopeful warning about the stability of your |
| 3885 | system. |
| 3886 | Cost is Ctrl+C wont kill children, but anyone |
| 3887 | who cares can have a go! |
| 3888 | */ |
| 3889 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3890 | } |
| 3891 | } |
| 3892 | |
| 3893 | /* Could be an else here to try cmd.exe / command.com in the path |
| 3894 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3895 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3896 | PyErr_SetString(PyExc_RuntimeError, |
| 3897 | "Cannot locate a COMSPEC environment variable to " |
| 3898 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3899 | return FALSE; |
| 3900 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3901 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3902 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 3903 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 3904 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 3905 | siStartInfo.hStdInput = hStdin; |
| 3906 | siStartInfo.hStdOutput = hStdout; |
| 3907 | siStartInfo.hStdError = hStderr; |
| 3908 | siStartInfo.wShowWindow = SW_HIDE; |
| 3909 | |
| 3910 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3911 | s2, |
| 3912 | NULL, |
| 3913 | NULL, |
| 3914 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3915 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3916 | NULL, |
| 3917 | NULL, |
| 3918 | &siStartInfo, |
| 3919 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3920 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3921 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3922 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3923 | /* Return process handle */ |
| 3924 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3925 | return TRUE; |
| 3926 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3927 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3928 | return FALSE; |
| 3929 | } |
| 3930 | |
| 3931 | /* The following code is based off of KB: Q190351 */ |
| 3932 | |
| 3933 | static PyObject * |
| 3934 | _PyPopen(char *cmdstring, int mode, int n) |
| 3935 | { |
| 3936 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 3937 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3938 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3939 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3940 | SECURITY_ATTRIBUTES saAttr; |
| 3941 | BOOL fSuccess; |
| 3942 | int fd1, fd2, fd3; |
| 3943 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3944 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3945 | PyObject *f; |
| 3946 | |
| 3947 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 3948 | saAttr.bInheritHandle = TRUE; |
| 3949 | saAttr.lpSecurityDescriptor = NULL; |
| 3950 | |
| 3951 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 3952 | return win32_error("CreatePipe", NULL); |
| 3953 | |
| 3954 | /* Create new output read handle and the input write handle. Set |
| 3955 | * the inheritance properties to FALSE. Otherwise, the child inherits |
| 3956 | * the these handles; resulting in non-closeable handles to the pipes |
| 3957 | * being created. */ |
| 3958 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3959 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 3960 | FALSE, |
| 3961 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3962 | if (!fSuccess) |
| 3963 | return win32_error("DuplicateHandle", NULL); |
| 3964 | |
| 3965 | /* Close the inheritable version of ChildStdin |
| 3966 | that we're using. */ |
| 3967 | CloseHandle(hChildStdinWr); |
| 3968 | |
| 3969 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 3970 | return win32_error("CreatePipe", NULL); |
| 3971 | |
| 3972 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3973 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 3974 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3975 | if (!fSuccess) |
| 3976 | return win32_error("DuplicateHandle", NULL); |
| 3977 | |
| 3978 | /* Close the inheritable version of ChildStdout |
| 3979 | that we're using. */ |
| 3980 | CloseHandle(hChildStdoutRd); |
| 3981 | |
| 3982 | if (n != POPEN_4) { |
| 3983 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 3984 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3985 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 3986 | hChildStderrRd, |
| 3987 | GetCurrentProcess(), |
| 3988 | &hChildStderrRdDup, 0, |
| 3989 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3990 | if (!fSuccess) |
| 3991 | return win32_error("DuplicateHandle", NULL); |
| 3992 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 3993 | CloseHandle(hChildStderrRd); |
| 3994 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3995 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3996 | switch (n) { |
| 3997 | case POPEN_1: |
| 3998 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 3999 | case _O_WRONLY | _O_TEXT: |
| 4000 | /* Case for writing to child Stdin in text mode. */ |
| 4001 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4002 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4003 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4004 | PyFile_SetBufSize(f, 0); |
| 4005 | /* We don't care about these pipes anymore, so close them. */ |
| 4006 | CloseHandle(hChildStdoutRdDup); |
| 4007 | CloseHandle(hChildStderrRdDup); |
| 4008 | break; |
| 4009 | |
| 4010 | case _O_RDONLY | _O_TEXT: |
| 4011 | /* Case for reading from child Stdout in text mode. */ |
| 4012 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4013 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4014 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4015 | PyFile_SetBufSize(f, 0); |
| 4016 | /* We don't care about these pipes anymore, so close them. */ |
| 4017 | CloseHandle(hChildStdinWrDup); |
| 4018 | CloseHandle(hChildStderrRdDup); |
| 4019 | break; |
| 4020 | |
| 4021 | case _O_RDONLY | _O_BINARY: |
| 4022 | /* Case for readinig from child Stdout in binary mode. */ |
| 4023 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4024 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4025 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4026 | PyFile_SetBufSize(f, 0); |
| 4027 | /* We don't care about these pipes anymore, so close them. */ |
| 4028 | CloseHandle(hChildStdinWrDup); |
| 4029 | CloseHandle(hChildStderrRdDup); |
| 4030 | break; |
| 4031 | |
| 4032 | case _O_WRONLY | _O_BINARY: |
| 4033 | /* Case for writing to child Stdin in binary mode. */ |
| 4034 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4035 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4036 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4037 | PyFile_SetBufSize(f, 0); |
| 4038 | /* We don't care about these pipes anymore, so close them. */ |
| 4039 | CloseHandle(hChildStdoutRdDup); |
| 4040 | CloseHandle(hChildStderrRdDup); |
| 4041 | break; |
| 4042 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4043 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4044 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4045 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4046 | case POPEN_2: |
| 4047 | case POPEN_4: |
| 4048 | { |
| 4049 | char *m1, *m2; |
| 4050 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4051 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4052 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4053 | m1 = "r"; |
| 4054 | m2 = "w"; |
| 4055 | } else { |
| 4056 | m1 = "rb"; |
| 4057 | m2 = "wb"; |
| 4058 | } |
| 4059 | |
| 4060 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4061 | f1 = _fdopen(fd1, m2); |
| 4062 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4063 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4064 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4065 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4066 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4067 | PyFile_SetBufSize(p2, 0); |
| 4068 | |
| 4069 | if (n != 4) |
| 4070 | CloseHandle(hChildStderrRdDup); |
| 4071 | |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4072 | f = Py_BuildValue("OO",p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4073 | Py_XDECREF(p1); |
| 4074 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4075 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4076 | break; |
| 4077 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4078 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4079 | case POPEN_3: |
| 4080 | { |
| 4081 | char *m1, *m2; |
| 4082 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4083 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4084 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4085 | m1 = "r"; |
| 4086 | m2 = "w"; |
| 4087 | } else { |
| 4088 | m1 = "rb"; |
| 4089 | m2 = "wb"; |
| 4090 | } |
| 4091 | |
| 4092 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4093 | f1 = _fdopen(fd1, m2); |
| 4094 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4095 | f2 = _fdopen(fd2, m1); |
| 4096 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 4097 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4098 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4099 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 4100 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4101 | PyFile_SetBufSize(p1, 0); |
| 4102 | PyFile_SetBufSize(p2, 0); |
| 4103 | PyFile_SetBufSize(p3, 0); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4104 | f = Py_BuildValue("OOO",p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4105 | Py_XDECREF(p1); |
| 4106 | Py_XDECREF(p2); |
| 4107 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4108 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4109 | break; |
| 4110 | } |
| 4111 | } |
| 4112 | |
| 4113 | if (n == POPEN_4) { |
| 4114 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4115 | hChildStdinRd, |
| 4116 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4117 | hChildStdoutWr, |
| 4118 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4119 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4120 | } |
| 4121 | else { |
| 4122 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4123 | hChildStdinRd, |
| 4124 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4125 | hChildStderrWr, |
| 4126 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4127 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4128 | } |
| 4129 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4130 | /* |
| 4131 | * Insert the files we've created into the process dictionary |
| 4132 | * all referencing the list with the process handle and the |
| 4133 | * initial number of files (see description below in _PyPclose). |
| 4134 | * Since if _PyPclose later tried to wait on a process when all |
| 4135 | * handles weren't closed, it could create a deadlock with the |
| 4136 | * child, we spend some energy here to try to ensure that we |
| 4137 | * either insert all file handles into the dictionary or none |
| 4138 | * at all. It's a little clumsy with the various popen modes |
| 4139 | * and variable number of files involved. |
| 4140 | */ |
| 4141 | if (!_PyPopenProcs) { |
| 4142 | _PyPopenProcs = PyDict_New(); |
| 4143 | } |
| 4144 | |
| 4145 | if (_PyPopenProcs) { |
| 4146 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 4147 | int ins_rc[3]; |
| 4148 | |
| 4149 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4150 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4151 | |
| 4152 | procObj = PyList_New(2); |
| 4153 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 4154 | intObj = PyInt_FromLong(file_count); |
| 4155 | |
| 4156 | if (procObj && hProcessObj && intObj) { |
| 4157 | PyList_SetItem(procObj,0,hProcessObj); |
| 4158 | PyList_SetItem(procObj,1,intObj); |
| 4159 | |
| 4160 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 4161 | if (fileObj[0]) { |
| 4162 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4163 | fileObj[0], |
| 4164 | procObj); |
| 4165 | } |
| 4166 | if (file_count >= 2) { |
| 4167 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 4168 | if (fileObj[1]) { |
| 4169 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4170 | fileObj[1], |
| 4171 | procObj); |
| 4172 | } |
| 4173 | } |
| 4174 | if (file_count >= 3) { |
| 4175 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 4176 | if (fileObj[2]) { |
| 4177 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4178 | fileObj[2], |
| 4179 | procObj); |
| 4180 | } |
| 4181 | } |
| 4182 | |
| 4183 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4184 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4185 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 4186 | /* Something failed - remove any dictionary |
| 4187 | * entries that did make it. |
| 4188 | */ |
| 4189 | if (!ins_rc[0] && fileObj[0]) { |
| 4190 | PyDict_DelItem(_PyPopenProcs, |
| 4191 | fileObj[0]); |
| 4192 | } |
| 4193 | if (!ins_rc[1] && fileObj[1]) { |
| 4194 | PyDict_DelItem(_PyPopenProcs, |
| 4195 | fileObj[1]); |
| 4196 | } |
| 4197 | if (!ins_rc[2] && fileObj[2]) { |
| 4198 | PyDict_DelItem(_PyPopenProcs, |
| 4199 | fileObj[2]); |
| 4200 | } |
| 4201 | } |
| 4202 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4203 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4204 | /* |
| 4205 | * Clean up our localized references for the dictionary keys |
| 4206 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4207 | * that got placed in the dictionary. |
| 4208 | */ |
| 4209 | Py_XDECREF(procObj); |
| 4210 | Py_XDECREF(fileObj[0]); |
| 4211 | Py_XDECREF(fileObj[1]); |
| 4212 | Py_XDECREF(fileObj[2]); |
| 4213 | } |
| 4214 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4215 | /* Child is launched. Close the parents copy of those pipe |
| 4216 | * handles that only the child should have open. You need to |
| 4217 | * make sure that no handles to the write end of the output pipe |
| 4218 | * are maintained in this process or else the pipe will not close |
| 4219 | * when the child process exits and the ReadFile will hang. */ |
| 4220 | |
| 4221 | if (!CloseHandle(hChildStdinRd)) |
| 4222 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4223 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4224 | if (!CloseHandle(hChildStdoutWr)) |
| 4225 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4226 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4227 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 4228 | return win32_error("CloseHandle", NULL); |
| 4229 | |
| 4230 | return f; |
| 4231 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4232 | |
| 4233 | /* |
| 4234 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4235 | * 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] | 4236 | * |
| 4237 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4238 | * input file pointer to information about the process that was |
| 4239 | * originally created by the popen* call that created the file pointer. |
| 4240 | * The dictionary uses the file pointer as a key (with one entry |
| 4241 | * inserted for each file returned by the original popen* call) and a |
| 4242 | * single list object as the value for all files from a single call. |
| 4243 | * The list object contains the Win32 process handle at [0], and a file |
| 4244 | * count at [1], which is initialized to the total number of file |
| 4245 | * handles using that list. |
| 4246 | * |
| 4247 | * This function closes whichever handle it is passed, and decrements |
| 4248 | * the file count in the dictionary for the process handle pointed to |
| 4249 | * by this file. On the last close (when the file count reaches zero), |
| 4250 | * this function will wait for the child process and then return its |
| 4251 | * exit code as the result of the close() operation. This permits the |
| 4252 | * files to be closed in any order - it is always the close() of the |
| 4253 | * final handle that will return the exit code. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4254 | * |
| 4255 | * NOTE: This function is currently called with the GIL released. |
| 4256 | * hence we use the GILState API to manage our state. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4257 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4258 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4259 | static int _PyPclose(FILE *file) |
| 4260 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4261 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4262 | DWORD exit_code; |
| 4263 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4264 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 4265 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4266 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4267 | PyGILState_STATE state; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4268 | #endif |
| 4269 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4270 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4271 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4272 | */ |
| 4273 | result = fclose(file); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4274 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4275 | state = PyGILState_Ensure(); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4276 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4277 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4278 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4279 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4280 | fileObj)) != NULL && |
| 4281 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 4282 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 4283 | |
| 4284 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 4285 | file_count = PyInt_AsLong(intObj); |
| 4286 | |
| 4287 | if (file_count > 1) { |
| 4288 | /* Still other files referencing process */ |
| 4289 | file_count--; |
| 4290 | PyList_SetItem(procObj,1, |
| 4291 | PyInt_FromLong(file_count)); |
| 4292 | } else { |
| 4293 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4294 | if (result != EOF && |
| 4295 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 4296 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4297 | /* Possible truncation here in 16-bit environments, but |
| 4298 | * real exit codes are just the lower byte in any event. |
| 4299 | */ |
| 4300 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4301 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4302 | /* Indicate failure - this will cause the file object |
| 4303 | * to raise an I/O error and translate the last Win32 |
| 4304 | * error code from errno. We do have a problem with |
| 4305 | * last errors that overlap the normal errno table, |
| 4306 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4307 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4308 | if (result != EOF) { |
| 4309 | /* If the error wasn't from the fclose(), then |
| 4310 | * set errno for the file object error handling. |
| 4311 | */ |
| 4312 | errno = GetLastError(); |
| 4313 | } |
| 4314 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
| 4317 | /* Free up the native handle at this point */ |
| 4318 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4319 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4320 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4321 | /* Remove this file pointer from dictionary */ |
| 4322 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4323 | |
| 4324 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 4325 | Py_DECREF(_PyPopenProcs); |
| 4326 | _PyPopenProcs = NULL; |
| 4327 | } |
| 4328 | |
| 4329 | } /* if object retrieval ok */ |
| 4330 | |
| 4331 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4332 | } /* if _PyPopenProcs */ |
| 4333 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4334 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4335 | PyGILState_Release(state); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4336 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4337 | return result; |
| 4338 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4339 | |
| 4340 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4341 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4342 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4343 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4344 | char *name; |
| 4345 | char *mode = "r"; |
| 4346 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4347 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4348 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4349 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4350 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4351 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4352 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4353 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4354 | if (fp == NULL) |
| 4355 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4356 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4357 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4358 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4359 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4360 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4361 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4362 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4363 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4364 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4365 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4366 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4367 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4368 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4369 | Set the current process's user id."); |
| 4370 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4371 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4372 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4373 | { |
| 4374 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4375 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4376 | return NULL; |
| 4377 | if (setuid(uid) < 0) |
| 4378 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4379 | Py_INCREF(Py_None); |
| 4380 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4381 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4382 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4383 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4384 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4385 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4386 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4387 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4388 | Set the current process's effective user id."); |
| 4389 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4390 | static PyObject * |
| 4391 | posix_seteuid (PyObject *self, PyObject *args) |
| 4392 | { |
| 4393 | int euid; |
| 4394 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 4395 | return NULL; |
| 4396 | } else if (seteuid(euid) < 0) { |
| 4397 | return posix_error(); |
| 4398 | } else { |
| 4399 | Py_INCREF(Py_None); |
| 4400 | return Py_None; |
| 4401 | } |
| 4402 | } |
| 4403 | #endif /* HAVE_SETEUID */ |
| 4404 | |
| 4405 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4406 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4407 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4408 | Set the current process's effective group id."); |
| 4409 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4410 | static PyObject * |
| 4411 | posix_setegid (PyObject *self, PyObject *args) |
| 4412 | { |
| 4413 | int egid; |
| 4414 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 4415 | return NULL; |
| 4416 | } else if (setegid(egid) < 0) { |
| 4417 | return posix_error(); |
| 4418 | } else { |
| 4419 | Py_INCREF(Py_None); |
| 4420 | return Py_None; |
| 4421 | } |
| 4422 | } |
| 4423 | #endif /* HAVE_SETEGID */ |
| 4424 | |
| 4425 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4426 | PyDoc_STRVAR(posix_setreuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4427 | "seteuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4428 | Set the current process's real and effective user ids."); |
| 4429 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4430 | static PyObject * |
| 4431 | posix_setreuid (PyObject *self, PyObject *args) |
| 4432 | { |
| 4433 | int ruid, euid; |
| 4434 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4435 | return NULL; |
| 4436 | } else if (setreuid(ruid, euid) < 0) { |
| 4437 | return posix_error(); |
| 4438 | } else { |
| 4439 | Py_INCREF(Py_None); |
| 4440 | return Py_None; |
| 4441 | } |
| 4442 | } |
| 4443 | #endif /* HAVE_SETREUID */ |
| 4444 | |
| 4445 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4446 | PyDoc_STRVAR(posix_setregid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4447 | "setegid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4448 | Set the current process's real and effective group ids."); |
| 4449 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4450 | static PyObject * |
| 4451 | posix_setregid (PyObject *self, PyObject *args) |
| 4452 | { |
| 4453 | int rgid, egid; |
| 4454 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 4455 | return NULL; |
| 4456 | } else if (setregid(rgid, egid) < 0) { |
| 4457 | return posix_error(); |
| 4458 | } else { |
| 4459 | Py_INCREF(Py_None); |
| 4460 | return Py_None; |
| 4461 | } |
| 4462 | } |
| 4463 | #endif /* HAVE_SETREGID */ |
| 4464 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4465 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4466 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4467 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4468 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4469 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4470 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4471 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4472 | { |
| 4473 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4474 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4475 | return NULL; |
| 4476 | if (setgid(gid) < 0) |
| 4477 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4478 | Py_INCREF(Py_None); |
| 4479 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4480 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4481 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4482 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4483 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4484 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4485 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4486 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4487 | |
| 4488 | static PyObject * |
| 4489 | posix_setgroups(PyObject *self, PyObject *args) |
| 4490 | { |
| 4491 | PyObject *groups; |
| 4492 | int i, len; |
| 4493 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4494 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4495 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 4496 | return NULL; |
| 4497 | if (!PySequence_Check(groups)) { |
| 4498 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4499 | return NULL; |
| 4500 | } |
| 4501 | len = PySequence_Size(groups); |
| 4502 | if (len > MAX_GROUPS) { |
| 4503 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4504 | return NULL; |
| 4505 | } |
| 4506 | for(i = 0; i < len; i++) { |
| 4507 | PyObject *elem; |
| 4508 | elem = PySequence_GetItem(groups, i); |
| 4509 | if (!elem) |
| 4510 | return NULL; |
| 4511 | if (!PyInt_Check(elem)) { |
| 4512 | PyErr_SetString(PyExc_TypeError, |
| 4513 | "groups must be integers"); |
| 4514 | Py_DECREF(elem); |
| 4515 | return NULL; |
| 4516 | } |
| 4517 | /* XXX: check that value fits into gid_t. */ |
| 4518 | grouplist[i] = PyInt_AsLong(elem); |
| 4519 | Py_DECREF(elem); |
| 4520 | } |
| 4521 | |
| 4522 | if (setgroups(len, grouplist) < 0) |
| 4523 | return posix_error(); |
| 4524 | Py_INCREF(Py_None); |
| 4525 | return Py_None; |
| 4526 | } |
| 4527 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4528 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4529 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4530 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4531 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4532 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4533 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4534 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4535 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4536 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4537 | int pid, options; |
| 4538 | #ifdef UNION_WAIT |
| 4539 | union wait status; |
| 4540 | #define status_i (status.w_status) |
| 4541 | #else |
| 4542 | int status; |
| 4543 | #define status_i status |
| 4544 | #endif |
| 4545 | status_i = 0; |
| 4546 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4547 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4548 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4549 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4550 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4551 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4552 | if (pid == -1) |
| 4553 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4554 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4555 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4556 | } |
| 4557 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4558 | #elif defined(HAVE_CWAIT) |
| 4559 | |
| 4560 | /* 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] | 4561 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4562 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4563 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4564 | |
| 4565 | static PyObject * |
| 4566 | posix_waitpid(PyObject *self, PyObject *args) |
| 4567 | { |
| 4568 | int pid, options; |
| 4569 | int status; |
| 4570 | |
| 4571 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4572 | return NULL; |
| 4573 | Py_BEGIN_ALLOW_THREADS |
| 4574 | pid = _cwait(&status, pid, options); |
| 4575 | Py_END_ALLOW_THREADS |
| 4576 | if (pid == -1) |
| 4577 | return posix_error(); |
| 4578 | else |
| 4579 | /* shift the status left a byte so this is more like the |
| 4580 | POSIX waitpid */ |
| 4581 | return Py_BuildValue("ii", pid, status << 8); |
| 4582 | } |
| 4583 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4584 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4585 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4586 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4587 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4588 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4589 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4590 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4591 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4592 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4593 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4594 | #ifdef UNION_WAIT |
| 4595 | union wait status; |
| 4596 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4597 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4598 | int status; |
| 4599 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4600 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4601 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4602 | status_i = 0; |
| 4603 | Py_BEGIN_ALLOW_THREADS |
| 4604 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4605 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4606 | if (pid == -1) |
| 4607 | return posix_error(); |
| 4608 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4609 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4610 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4611 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4612 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4613 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4614 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4615 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4616 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4617 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4618 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4619 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4620 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4621 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4622 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4623 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4624 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4625 | #ifdef MS_WINDOWS |
Mark Hammond | 7edd0a9 | 2003-08-06 02:46:58 +0000 | [diff] [blame] | 4626 | return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", _wstati64); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4627 | #else |
| 4628 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 4629 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4630 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4631 | } |
| 4632 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4633 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4634 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4635 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4636 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4637 | 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] | 4638 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4639 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4640 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4641 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4642 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4643 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4644 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4645 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4646 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4647 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4648 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4649 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4650 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 4651 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4652 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4653 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4654 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4655 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4656 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4657 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4658 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4659 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4660 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4661 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4662 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4663 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4664 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4665 | return posix_2str(args, "etet:symlink", symlink, NULL, NULL); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4666 | } |
| 4667 | #endif /* HAVE_SYMLINK */ |
| 4668 | |
| 4669 | |
| 4670 | #ifdef HAVE_TIMES |
| 4671 | #ifndef HZ |
| 4672 | #define HZ 60 /* Universal constant :-) */ |
| 4673 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4674 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4675 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4676 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4677 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4678 | { |
| 4679 | ULONG value = 0; |
| 4680 | |
| 4681 | Py_BEGIN_ALLOW_THREADS |
| 4682 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4683 | Py_END_ALLOW_THREADS |
| 4684 | |
| 4685 | return value; |
| 4686 | } |
| 4687 | |
| 4688 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4689 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4690 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4691 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4692 | return Py_BuildValue("ddddd", |
| 4693 | (double)0 /* t.tms_utime / HZ */, |
| 4694 | (double)0 /* t.tms_stime / HZ */, |
| 4695 | (double)0 /* t.tms_cutime / HZ */, |
| 4696 | (double)0 /* t.tms_cstime / HZ */, |
| 4697 | (double)system_uptime() / 1000); |
| 4698 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4699 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4700 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4701 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4702 | { |
| 4703 | struct tms t; |
| 4704 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4705 | errno = 0; |
| 4706 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4707 | if (c == (clock_t) -1) |
| 4708 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4709 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4710 | (double)t.tms_utime / HZ, |
| 4711 | (double)t.tms_stime / HZ, |
| 4712 | (double)t.tms_cutime / HZ, |
| 4713 | (double)t.tms_cstime / HZ, |
| 4714 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4715 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4716 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4717 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4718 | |
| 4719 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4720 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4721 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4722 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4723 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4724 | { |
| 4725 | FILETIME create, exit, kernel, user; |
| 4726 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4727 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4728 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4729 | /* The fields of a FILETIME structure are the hi and lo part |
| 4730 | of a 64-bit value expressed in 100 nanosecond units. |
| 4731 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4732 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4733 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4734 | return Py_BuildValue( |
| 4735 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4736 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4737 | kernel.dwLowDateTime*1e-7), |
| 4738 | (double)(user.dwHighDateTime*429.4967296 + |
| 4739 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4740 | (double)0, |
| 4741 | (double)0, |
| 4742 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4743 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4744 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4745 | |
| 4746 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4747 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4748 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4749 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4750 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4751 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4752 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4753 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4754 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4755 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4756 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4757 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4758 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4759 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4760 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4761 | if (setsid() < 0) |
| 4762 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4763 | Py_INCREF(Py_None); |
| 4764 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4765 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4766 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4767 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4768 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4769 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4770 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4771 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 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_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4775 | { |
| 4776 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4777 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4778 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4779 | if (setpgid(pid, pgrp) < 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 | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4783 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4784 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4785 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4786 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4787 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4788 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4789 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4790 | 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] | 4791 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4792 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4793 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4794 | { |
| 4795 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4796 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4797 | return NULL; |
| 4798 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4799 | if (pgid < 0) |
| 4800 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4801 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4802 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4803 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4804 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4805 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4806 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4807 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4808 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4809 | 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] | 4810 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4811 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4812 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4813 | { |
| 4814 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4815 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4816 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4817 | if (tcsetpgrp(fd, pgid) < 0) |
| 4818 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4819 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4820 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4821 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4822 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4823 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4824 | /* Functions acting on file descriptors */ |
| 4825 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4826 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4827 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4828 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4829 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4830 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4831 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4832 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4833 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4834 | int flag; |
| 4835 | int mode = 0777; |
| 4836 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4837 | |
| 4838 | #ifdef MS_WINDOWS |
| 4839 | if (unicode_file_names()) { |
| 4840 | PyUnicodeObject *po; |
| 4841 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4842 | Py_BEGIN_ALLOW_THREADS |
| 4843 | /* PyUnicode_AS_UNICODE OK without thread |
| 4844 | lock as it is a simple dereference. */ |
| 4845 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4846 | Py_END_ALLOW_THREADS |
| 4847 | if (fd < 0) |
| 4848 | return posix_error(); |
| 4849 | return PyInt_FromLong((long)fd); |
| 4850 | } |
| 4851 | /* Drop the argument parsing error as narrow strings |
| 4852 | are also valid. */ |
| 4853 | PyErr_Clear(); |
| 4854 | } |
| 4855 | #endif |
| 4856 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4857 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4858 | Py_FileSystemDefaultEncoding, &file, |
| 4859 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4860 | return NULL; |
| 4861 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4862 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4863 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4864 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4865 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4866 | return posix_error_with_allocated_filename(file); |
| 4867 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4868 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4869 | } |
| 4870 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4871 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4872 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4873 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4874 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4875 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4876 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4877 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4878 | { |
| 4879 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4880 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4881 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4882 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4883 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4884 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4885 | if (res < 0) |
| 4886 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4887 | Py_INCREF(Py_None); |
| 4888 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4891 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4892 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4893 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4894 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4895 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4896 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4897 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4898 | { |
| 4899 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4900 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4901 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4902 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4903 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4904 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4905 | if (fd < 0) |
| 4906 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4907 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4910 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4911 | PyDoc_STRVAR(posix_dup2__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4912 | "dup2(fd, fd2)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4913 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4914 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4915 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4916 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4917 | { |
| 4918 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4919 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4920 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4921 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4922 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4923 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4924 | if (res < 0) |
| 4925 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4926 | Py_INCREF(Py_None); |
| 4927 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4928 | } |
| 4929 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4930 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4931 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4932 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4933 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4934 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4935 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4936 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4937 | { |
| 4938 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4939 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 4940 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4941 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4942 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4943 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4944 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4945 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4946 | return NULL; |
| 4947 | #ifdef SEEK_SET |
| 4948 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 4949 | switch (how) { |
| 4950 | case 0: how = SEEK_SET; break; |
| 4951 | case 1: how = SEEK_CUR; break; |
| 4952 | case 2: how = SEEK_END; break; |
| 4953 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4954 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4955 | |
| 4956 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4957 | pos = PyInt_AsLong(posobj); |
| 4958 | #else |
| 4959 | pos = PyLong_Check(posobj) ? |
| 4960 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 4961 | #endif |
| 4962 | if (PyErr_Occurred()) |
| 4963 | return NULL; |
| 4964 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4965 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4966 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4967 | res = _lseeki64(fd, pos, how); |
| 4968 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4969 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4970 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4971 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4972 | if (res < 0) |
| 4973 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4974 | |
| 4975 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4976 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4977 | #else |
| 4978 | return PyLong_FromLongLong(res); |
| 4979 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4982 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4983 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4984 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4985 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4986 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4987 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4988 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4989 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4990 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4991 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4992 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4993 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4994 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4995 | if (buffer == NULL) |
| 4996 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4997 | Py_BEGIN_ALLOW_THREADS |
| 4998 | n = read(fd, PyString_AsString(buffer), size); |
| 4999 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5000 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5001 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5002 | return posix_error(); |
| 5003 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5004 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5005 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5006 | return buffer; |
| 5007 | } |
| 5008 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5009 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5010 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5011 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5012 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5013 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5014 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5015 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5016 | { |
| 5017 | int fd, size; |
| 5018 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5019 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5020 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5021 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5022 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5023 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5024 | if (size < 0) |
| 5025 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5026 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5027 | } |
| 5028 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5029 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5030 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5031 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5032 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5033 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5034 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5035 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5036 | { |
| 5037 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5038 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5039 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5040 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5041 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5042 | #ifdef __VMS |
| 5043 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5044 | fsync(fd); |
| 5045 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5046 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5047 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5048 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5049 | if (res != 0) |
| 5050 | return posix_error(); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5051 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5052 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5053 | } |
| 5054 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5055 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5056 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5057 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5058 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5059 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5060 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5061 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5062 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5063 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5064 | char *mode = "r"; |
| 5065 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5066 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5067 | PyObject *f; |
| 5068 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5069 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5070 | |
Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 5071 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 5072 | PyErr_Format(PyExc_ValueError, |
| 5073 | "invalid file mode '%s'", mode); |
| 5074 | return NULL; |
| 5075 | } |
| 5076 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5077 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5078 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5079 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5080 | if (fp == NULL) |
| 5081 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 5082 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5083 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5084 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5085 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5088 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5089 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5090 | 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] | 5091 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5092 | |
| 5093 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5094 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5095 | { |
| 5096 | int fd; |
| 5097 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5098 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5099 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5100 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5101 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5102 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5103 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5104 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5105 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5106 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5107 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5108 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5109 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5110 | #if defined(PYOS_OS2) |
| 5111 | HFILE read, write; |
| 5112 | APIRET rc; |
| 5113 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5114 | Py_BEGIN_ALLOW_THREADS |
| 5115 | rc = DosCreatePipe( &read, &write, 4096); |
| 5116 | Py_END_ALLOW_THREADS |
| 5117 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5118 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5119 | |
| 5120 | return Py_BuildValue("(ii)", read, write); |
| 5121 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5122 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5123 | int fds[2]; |
| 5124 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5125 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5126 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5127 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5128 | if (res != 0) |
| 5129 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5130 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5131 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5132 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5133 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5134 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5135 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5136 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5137 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5138 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5139 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5140 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5141 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5142 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5143 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5144 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5145 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5146 | #endif /* HAVE_PIPE */ |
| 5147 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5148 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5149 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5150 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5151 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5152 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5153 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5154 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5155 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5156 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5157 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5158 | int mode = 0666; |
| 5159 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5160 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5161 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5162 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5163 | res = mkfifo(filename, mode); |
| 5164 | Py_END_ALLOW_THREADS |
| 5165 | if (res < 0) |
| 5166 | return posix_error(); |
| 5167 | Py_INCREF(Py_None); |
| 5168 | return Py_None; |
| 5169 | } |
| 5170 | #endif |
| 5171 | |
| 5172 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5173 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5174 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5175 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5176 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5177 | named filename. mode specifies both the permissions to use and the\n\ |
| 5178 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5179 | 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] | 5180 | device defines the newly created device special file (probably using\n\ |
| 5181 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5182 | |
| 5183 | |
| 5184 | static PyObject * |
| 5185 | posix_mknod(PyObject *self, PyObject *args) |
| 5186 | { |
| 5187 | char *filename; |
| 5188 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5189 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5190 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5191 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5192 | return NULL; |
| 5193 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5194 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5195 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5196 | if (res < 0) |
| 5197 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5198 | Py_INCREF(Py_None); |
| 5199 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5200 | } |
| 5201 | #endif |
| 5202 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5203 | #ifdef HAVE_DEVICE_MACROS |
| 5204 | PyDoc_STRVAR(posix_major__doc__, |
| 5205 | "major(device) -> major number\n\ |
| 5206 | Extracts a device major number from a raw device number."); |
| 5207 | |
| 5208 | static PyObject * |
| 5209 | posix_major(PyObject *self, PyObject *args) |
| 5210 | { |
| 5211 | int device; |
| 5212 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5213 | return NULL; |
| 5214 | return PyInt_FromLong((long)major(device)); |
| 5215 | } |
| 5216 | |
| 5217 | PyDoc_STRVAR(posix_minor__doc__, |
| 5218 | "minor(device) -> minor number\n\ |
| 5219 | Extracts a device minor number from a raw device number."); |
| 5220 | |
| 5221 | static PyObject * |
| 5222 | posix_minor(PyObject *self, PyObject *args) |
| 5223 | { |
| 5224 | int device; |
| 5225 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5226 | return NULL; |
| 5227 | return PyInt_FromLong((long)minor(device)); |
| 5228 | } |
| 5229 | |
| 5230 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5231 | "makedev(major, minor) -> device number\n\ |
| 5232 | Composes a raw device number from the major and minor device numbers."); |
| 5233 | |
| 5234 | static PyObject * |
| 5235 | posix_makedev(PyObject *self, PyObject *args) |
| 5236 | { |
| 5237 | int major, minor; |
| 5238 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5239 | return NULL; |
| 5240 | return PyInt_FromLong((long)makedev(major, minor)); |
| 5241 | } |
| 5242 | #endif /* device macros */ |
| 5243 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5244 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5245 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5246 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5247 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5248 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5249 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5250 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5251 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5252 | { |
| 5253 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5254 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5255 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5256 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5257 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5258 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5259 | return NULL; |
| 5260 | |
| 5261 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5262 | length = PyInt_AsLong(lenobj); |
| 5263 | #else |
| 5264 | length = PyLong_Check(lenobj) ? |
| 5265 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 5266 | #endif |
| 5267 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5268 | return NULL; |
| 5269 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5270 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5271 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5272 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5273 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5274 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5275 | return NULL; |
| 5276 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5277 | Py_INCREF(Py_None); |
| 5278 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5279 | } |
| 5280 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5281 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5282 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5283 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5284 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5285 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5286 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5287 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5288 | * get re-set with another call for the same key. */ |
| 5289 | static PyObject *posix_putenv_garbage; |
| 5290 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5291 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5292 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5293 | { |
| 5294 | char *s1, *s2; |
| 5295 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5296 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5297 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5298 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5299 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5300 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5301 | |
| 5302 | #if defined(PYOS_OS2) |
| 5303 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5304 | APIRET rc; |
| 5305 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5306 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5307 | if (rc != NO_ERROR) |
| 5308 | return os2_error(rc); |
| 5309 | |
| 5310 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5311 | APIRET rc; |
| 5312 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5313 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5314 | if (rc != NO_ERROR) |
| 5315 | return os2_error(rc); |
| 5316 | } else { |
| 5317 | #endif |
| 5318 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5319 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5320 | len = strlen(s1) + strlen(s2) + 2; |
| 5321 | /* len includes space for a trailing \0; the size arg to |
| 5322 | PyString_FromStringAndSize does not count that */ |
| 5323 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5324 | if (newstr == NULL) |
| 5325 | return PyErr_NoMemory(); |
| 5326 | new = PyString_AS_STRING(newstr); |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5327 | PyOS_snprintf(new, len, "%s=%s", s1, s2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5328 | if (putenv(new)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5329 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5330 | posix_error(); |
| 5331 | return NULL; |
| 5332 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5333 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5334 | * this will cause previous value to be collected. This has to |
| 5335 | * happen after the real putenv() call because the old value |
| 5336 | * was still accessible until then. */ |
| 5337 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5338 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5339 | /* really not much we can do; just leak */ |
| 5340 | PyErr_Clear(); |
| 5341 | } |
| 5342 | else { |
| 5343 | Py_DECREF(newstr); |
| 5344 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5345 | |
| 5346 | #if defined(PYOS_OS2) |
| 5347 | } |
| 5348 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5349 | Py_INCREF(Py_None); |
| 5350 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5351 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5352 | #endif /* putenv */ |
| 5353 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5354 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5355 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5356 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5357 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5358 | |
| 5359 | static PyObject * |
| 5360 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5361 | { |
| 5362 | char *s1; |
| 5363 | |
| 5364 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5365 | return NULL; |
| 5366 | |
| 5367 | unsetenv(s1); |
| 5368 | |
| 5369 | /* Remove the key from posix_putenv_garbage; |
| 5370 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5371 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5372 | * old value was still accessible until then. |
| 5373 | */ |
| 5374 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5375 | PyTuple_GET_ITEM(args, 0))) { |
| 5376 | /* really not much we can do; just leak */ |
| 5377 | PyErr_Clear(); |
| 5378 | } |
| 5379 | |
| 5380 | Py_INCREF(Py_None); |
| 5381 | return Py_None; |
| 5382 | } |
| 5383 | #endif /* unsetenv */ |
| 5384 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5385 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5386 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5387 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5388 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5389 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5390 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5391 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5392 | { |
| 5393 | int code; |
| 5394 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5395 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5396 | return NULL; |
| 5397 | message = strerror(code); |
| 5398 | if (message == NULL) { |
| 5399 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5400 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5401 | return NULL; |
| 5402 | } |
| 5403 | return PyString_FromString(message); |
| 5404 | } |
| 5405 | #endif /* strerror */ |
| 5406 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5407 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5408 | #ifdef HAVE_SYS_WAIT_H |
| 5409 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5410 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5411 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5412 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5413 | 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] | 5414 | |
| 5415 | static PyObject * |
| 5416 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5417 | { |
| 5418 | #ifdef UNION_WAIT |
| 5419 | union wait status; |
| 5420 | #define status_i (status.w_status) |
| 5421 | #else |
| 5422 | int status; |
| 5423 | #define status_i status |
| 5424 | #endif |
| 5425 | status_i = 0; |
| 5426 | |
| 5427 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i)) |
| 5428 | { |
| 5429 | return NULL; |
| 5430 | } |
| 5431 | |
| 5432 | return PyBool_FromLong(WCOREDUMP(status)); |
| 5433 | #undef status_i |
| 5434 | } |
| 5435 | #endif /* WCOREDUMP */ |
| 5436 | |
| 5437 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5438 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5439 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5440 | 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] | 5441 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5442 | |
| 5443 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5444 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5445 | { |
| 5446 | #ifdef UNION_WAIT |
| 5447 | union wait status; |
| 5448 | #define status_i (status.w_status) |
| 5449 | #else |
| 5450 | int status; |
| 5451 | #define status_i status |
| 5452 | #endif |
| 5453 | status_i = 0; |
| 5454 | |
| 5455 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i)) |
| 5456 | { |
| 5457 | return NULL; |
| 5458 | } |
| 5459 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5460 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5461 | #undef status_i |
| 5462 | } |
| 5463 | #endif /* WIFCONTINUED */ |
| 5464 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5465 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5466 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5467 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5468 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5469 | |
| 5470 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5471 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5472 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5473 | #ifdef UNION_WAIT |
| 5474 | union wait status; |
| 5475 | #define status_i (status.w_status) |
| 5476 | #else |
| 5477 | int status; |
| 5478 | #define status_i status |
| 5479 | #endif |
| 5480 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5481 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5482 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5483 | { |
| 5484 | return NULL; |
| 5485 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5486 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5487 | return PyBool_FromLong(WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5488 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5489 | } |
| 5490 | #endif /* WIFSTOPPED */ |
| 5491 | |
| 5492 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5493 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5494 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5495 | 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] | 5496 | |
| 5497 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5498 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5499 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5500 | #ifdef UNION_WAIT |
| 5501 | union wait status; |
| 5502 | #define status_i (status.w_status) |
| 5503 | #else |
| 5504 | int status; |
| 5505 | #define status_i status |
| 5506 | #endif |
| 5507 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5508 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5509 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5510 | { |
| 5511 | return NULL; |
| 5512 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5513 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5514 | return PyBool_FromLong(WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5515 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5516 | } |
| 5517 | #endif /* WIFSIGNALED */ |
| 5518 | |
| 5519 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5520 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5521 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5522 | 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] | 5523 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5524 | |
| 5525 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5526 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5527 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5528 | #ifdef UNION_WAIT |
| 5529 | union wait status; |
| 5530 | #define status_i (status.w_status) |
| 5531 | #else |
| 5532 | int status; |
| 5533 | #define status_i status |
| 5534 | #endif |
| 5535 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5536 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5537 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5538 | { |
| 5539 | return NULL; |
| 5540 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5541 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5542 | return PyBool_FromLong(WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5543 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5544 | } |
| 5545 | #endif /* WIFEXITED */ |
| 5546 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5547 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5548 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5549 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5550 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5551 | |
| 5552 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5553 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5554 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5555 | #ifdef UNION_WAIT |
| 5556 | union wait status; |
| 5557 | #define status_i (status.w_status) |
| 5558 | #else |
| 5559 | int status; |
| 5560 | #define status_i status |
| 5561 | #endif |
| 5562 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5563 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5564 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5565 | { |
| 5566 | return NULL; |
| 5567 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5568 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5569 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5570 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5571 | } |
| 5572 | #endif /* WEXITSTATUS */ |
| 5573 | |
| 5574 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5575 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5576 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5577 | 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] | 5578 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5579 | |
| 5580 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5581 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5582 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5583 | #ifdef UNION_WAIT |
| 5584 | union wait status; |
| 5585 | #define status_i (status.w_status) |
| 5586 | #else |
| 5587 | int status; |
| 5588 | #define status_i status |
| 5589 | #endif |
| 5590 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5591 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5592 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5593 | { |
| 5594 | return NULL; |
| 5595 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5596 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5597 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5598 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5599 | } |
| 5600 | #endif /* WTERMSIG */ |
| 5601 | |
| 5602 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5603 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5604 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5605 | Return the signal that stopped the process that provided\n\ |
| 5606 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5607 | |
| 5608 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5609 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5610 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5611 | #ifdef UNION_WAIT |
| 5612 | union wait status; |
| 5613 | #define status_i (status.w_status) |
| 5614 | #else |
| 5615 | int status; |
| 5616 | #define status_i status |
| 5617 | #endif |
| 5618 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5619 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5620 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5621 | { |
| 5622 | return NULL; |
| 5623 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5624 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5625 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5626 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5627 | } |
| 5628 | #endif /* WSTOPSIG */ |
| 5629 | |
| 5630 | #endif /* HAVE_SYS_WAIT_H */ |
| 5631 | |
| 5632 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5633 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5634 | #ifdef _SCO_DS |
| 5635 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5636 | needed definitions in sys/statvfs.h */ |
| 5637 | #define _SVID3 |
| 5638 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5639 | #include <sys/statvfs.h> |
| 5640 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5641 | static PyObject* |
| 5642 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5643 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5644 | if (v == NULL) |
| 5645 | return NULL; |
| 5646 | |
| 5647 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5648 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5649 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 5650 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 5651 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 5652 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 5653 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 5654 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 5655 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 5656 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5657 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5658 | #else |
| 5659 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5660 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5661 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5662 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5663 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5664 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5665 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5666 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5667 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5668 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5669 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5670 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5671 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5672 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5673 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5674 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5675 | #endif |
| 5676 | |
| 5677 | return v; |
| 5678 | } |
| 5679 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5680 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5681 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5682 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5683 | |
| 5684 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5685 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5686 | { |
| 5687 | int fd, res; |
| 5688 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5689 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5690 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5691 | return NULL; |
| 5692 | Py_BEGIN_ALLOW_THREADS |
| 5693 | res = fstatvfs(fd, &st); |
| 5694 | Py_END_ALLOW_THREADS |
| 5695 | if (res != 0) |
| 5696 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5697 | |
| 5698 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5699 | } |
| 5700 | #endif /* HAVE_FSTATVFS */ |
| 5701 | |
| 5702 | |
| 5703 | #if defined(HAVE_STATVFS) |
| 5704 | #include <sys/statvfs.h> |
| 5705 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5706 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5707 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5708 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5709 | |
| 5710 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5711 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5712 | { |
| 5713 | char *path; |
| 5714 | int res; |
| 5715 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5716 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5717 | return NULL; |
| 5718 | Py_BEGIN_ALLOW_THREADS |
| 5719 | res = statvfs(path, &st); |
| 5720 | Py_END_ALLOW_THREADS |
| 5721 | if (res != 0) |
| 5722 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5723 | |
| 5724 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5725 | } |
| 5726 | #endif /* HAVE_STATVFS */ |
| 5727 | |
| 5728 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5729 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5730 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5731 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5732 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 5733 | 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] | 5734 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5735 | |
| 5736 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5737 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5738 | { |
| 5739 | PyObject *result = NULL; |
| 5740 | char *dir = NULL; |
| 5741 | char *pfx = NULL; |
| 5742 | char *name; |
| 5743 | |
| 5744 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 5745 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5746 | |
| 5747 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5748 | "tempnam is a potential security risk to your program") < 0) |
| 5749 | return NULL; |
| 5750 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5751 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5752 | name = _tempnam(dir, pfx); |
| 5753 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5754 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5755 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5756 | if (name == NULL) |
| 5757 | return PyErr_NoMemory(); |
| 5758 | result = PyString_FromString(name); |
| 5759 | free(name); |
| 5760 | return result; |
| 5761 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 5762 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5763 | |
| 5764 | |
| 5765 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5766 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5767 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5768 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5769 | |
| 5770 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5771 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5772 | { |
| 5773 | FILE *fp; |
| 5774 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5775 | fp = tmpfile(); |
| 5776 | if (fp == NULL) |
| 5777 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 5778 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5779 | } |
| 5780 | #endif |
| 5781 | |
| 5782 | |
| 5783 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5784 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5785 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5786 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5787 | |
| 5788 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5789 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5790 | { |
| 5791 | char buffer[L_tmpnam]; |
| 5792 | char *name; |
| 5793 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5794 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5795 | "tmpnam is a potential security risk to your program") < 0) |
| 5796 | return NULL; |
| 5797 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5798 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5799 | name = tmpnam_r(buffer); |
| 5800 | #else |
| 5801 | name = tmpnam(buffer); |
| 5802 | #endif |
| 5803 | if (name == NULL) { |
| 5804 | PyErr_SetObject(PyExc_OSError, |
| 5805 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5806 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5807 | "unexpected NULL from tmpnam_r" |
| 5808 | #else |
| 5809 | "unexpected NULL from tmpnam" |
| 5810 | #endif |
| 5811 | )); |
| 5812 | return NULL; |
| 5813 | } |
| 5814 | return PyString_FromString(buffer); |
| 5815 | } |
| 5816 | #endif |
| 5817 | |
| 5818 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5819 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5820 | * It maps strings representing configuration variable names to |
| 5821 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5822 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5823 | * rarely-used constants. There are three separate tables that use |
| 5824 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5825 | * |
| 5826 | * This code is always included, even if none of the interfaces that |
| 5827 | * need it are included. The #if hackery needed to avoid it would be |
| 5828 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5829 | */ |
| 5830 | struct constdef { |
| 5831 | char *name; |
| 5832 | long value; |
| 5833 | }; |
| 5834 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5835 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5836 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 5837 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5838 | { |
| 5839 | if (PyInt_Check(arg)) { |
| 5840 | *valuep = PyInt_AS_LONG(arg); |
| 5841 | return 1; |
| 5842 | } |
| 5843 | if (PyString_Check(arg)) { |
| 5844 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5845 | size_t lo = 0; |
| 5846 | size_t mid; |
| 5847 | size_t hi = tablesize; |
| 5848 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5849 | char *confname = PyString_AS_STRING(arg); |
| 5850 | while (lo < hi) { |
| 5851 | mid = (lo + hi) / 2; |
| 5852 | cmp = strcmp(confname, table[mid].name); |
| 5853 | if (cmp < 0) |
| 5854 | hi = mid; |
| 5855 | else if (cmp > 0) |
| 5856 | lo = mid + 1; |
| 5857 | else { |
| 5858 | *valuep = table[mid].value; |
| 5859 | return 1; |
| 5860 | } |
| 5861 | } |
| 5862 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 5863 | } |
| 5864 | else |
| 5865 | PyErr_SetString(PyExc_TypeError, |
| 5866 | "configuration names must be strings or integers"); |
| 5867 | return 0; |
| 5868 | } |
| 5869 | |
| 5870 | |
| 5871 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5872 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5873 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5874 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5875 | #endif |
| 5876 | #ifdef _PC_ABI_ASYNC_IO |
| 5877 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5878 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5879 | #ifdef _PC_ASYNC_IO |
| 5880 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5881 | #endif |
| 5882 | #ifdef _PC_CHOWN_RESTRICTED |
| 5883 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5884 | #endif |
| 5885 | #ifdef _PC_FILESIZEBITS |
| 5886 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5887 | #endif |
| 5888 | #ifdef _PC_LAST |
| 5889 | {"PC_LAST", _PC_LAST}, |
| 5890 | #endif |
| 5891 | #ifdef _PC_LINK_MAX |
| 5892 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5893 | #endif |
| 5894 | #ifdef _PC_MAX_CANON |
| 5895 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5896 | #endif |
| 5897 | #ifdef _PC_MAX_INPUT |
| 5898 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5899 | #endif |
| 5900 | #ifdef _PC_NAME_MAX |
| 5901 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5902 | #endif |
| 5903 | #ifdef _PC_NO_TRUNC |
| 5904 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5905 | #endif |
| 5906 | #ifdef _PC_PATH_MAX |
| 5907 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5908 | #endif |
| 5909 | #ifdef _PC_PIPE_BUF |
| 5910 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5911 | #endif |
| 5912 | #ifdef _PC_PRIO_IO |
| 5913 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5914 | #endif |
| 5915 | #ifdef _PC_SOCK_MAXBUF |
| 5916 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5917 | #endif |
| 5918 | #ifdef _PC_SYNC_IO |
| 5919 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5920 | #endif |
| 5921 | #ifdef _PC_VDISABLE |
| 5922 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5923 | #endif |
| 5924 | }; |
| 5925 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5926 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5927 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5928 | { |
| 5929 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5930 | sizeof(posix_constants_pathconf) |
| 5931 | / sizeof(struct constdef)); |
| 5932 | } |
| 5933 | #endif |
| 5934 | |
| 5935 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5936 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5937 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5938 | 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] | 5939 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5940 | |
| 5941 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5942 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5943 | { |
| 5944 | PyObject *result = NULL; |
| 5945 | int name, fd; |
| 5946 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5947 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5948 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5949 | long limit; |
| 5950 | |
| 5951 | errno = 0; |
| 5952 | limit = fpathconf(fd, name); |
| 5953 | if (limit == -1 && errno != 0) |
| 5954 | posix_error(); |
| 5955 | else |
| 5956 | result = PyInt_FromLong(limit); |
| 5957 | } |
| 5958 | return result; |
| 5959 | } |
| 5960 | #endif |
| 5961 | |
| 5962 | |
| 5963 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5964 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5965 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5966 | 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] | 5967 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5968 | |
| 5969 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5970 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5971 | { |
| 5972 | PyObject *result = NULL; |
| 5973 | int name; |
| 5974 | char *path; |
| 5975 | |
| 5976 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5977 | conv_path_confname, &name)) { |
| 5978 | long limit; |
| 5979 | |
| 5980 | errno = 0; |
| 5981 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5982 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5983 | if (errno == EINVAL) |
| 5984 | /* could be a path or name problem */ |
| 5985 | posix_error(); |
| 5986 | else |
| 5987 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5988 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5989 | else |
| 5990 | result = PyInt_FromLong(limit); |
| 5991 | } |
| 5992 | return result; |
| 5993 | } |
| 5994 | #endif |
| 5995 | |
| 5996 | #ifdef HAVE_CONFSTR |
| 5997 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5998 | #ifdef _CS_ARCHITECTURE |
| 5999 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 6000 | #endif |
| 6001 | #ifdef _CS_HOSTNAME |
| 6002 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 6003 | #endif |
| 6004 | #ifdef _CS_HW_PROVIDER |
| 6005 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 6006 | #endif |
| 6007 | #ifdef _CS_HW_SERIAL |
| 6008 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 6009 | #endif |
| 6010 | #ifdef _CS_INITTAB_NAME |
| 6011 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 6012 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6013 | #ifdef _CS_LFS64_CFLAGS |
| 6014 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 6015 | #endif |
| 6016 | #ifdef _CS_LFS64_LDFLAGS |
| 6017 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 6018 | #endif |
| 6019 | #ifdef _CS_LFS64_LIBS |
| 6020 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 6021 | #endif |
| 6022 | #ifdef _CS_LFS64_LINTFLAGS |
| 6023 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 6024 | #endif |
| 6025 | #ifdef _CS_LFS_CFLAGS |
| 6026 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 6027 | #endif |
| 6028 | #ifdef _CS_LFS_LDFLAGS |
| 6029 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6030 | #endif |
| 6031 | #ifdef _CS_LFS_LIBS |
| 6032 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6033 | #endif |
| 6034 | #ifdef _CS_LFS_LINTFLAGS |
| 6035 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6036 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6037 | #ifdef _CS_MACHINE |
| 6038 | {"CS_MACHINE", _CS_MACHINE}, |
| 6039 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6040 | #ifdef _CS_PATH |
| 6041 | {"CS_PATH", _CS_PATH}, |
| 6042 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6043 | #ifdef _CS_RELEASE |
| 6044 | {"CS_RELEASE", _CS_RELEASE}, |
| 6045 | #endif |
| 6046 | #ifdef _CS_SRPC_DOMAIN |
| 6047 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6048 | #endif |
| 6049 | #ifdef _CS_SYSNAME |
| 6050 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6051 | #endif |
| 6052 | #ifdef _CS_VERSION |
| 6053 | {"CS_VERSION", _CS_VERSION}, |
| 6054 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6055 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6056 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6057 | #endif |
| 6058 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6059 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6060 | #endif |
| 6061 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6062 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6063 | #endif |
| 6064 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6065 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6066 | #endif |
| 6067 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6068 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6069 | #endif |
| 6070 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6071 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6072 | #endif |
| 6073 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6074 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6075 | #endif |
| 6076 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6077 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6078 | #endif |
| 6079 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6080 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6081 | #endif |
| 6082 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6083 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6084 | #endif |
| 6085 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6086 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6087 | #endif |
| 6088 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6089 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6090 | #endif |
| 6091 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6092 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6093 | #endif |
| 6094 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6095 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6096 | #endif |
| 6097 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6098 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6099 | #endif |
| 6100 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6101 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6102 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6103 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6104 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6105 | #endif |
| 6106 | #ifdef _MIPS_CS_BASE |
| 6107 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6108 | #endif |
| 6109 | #ifdef _MIPS_CS_HOSTID |
| 6110 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6111 | #endif |
| 6112 | #ifdef _MIPS_CS_HW_NAME |
| 6113 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6114 | #endif |
| 6115 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6116 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6117 | #endif |
| 6118 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6119 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6120 | #endif |
| 6121 | #ifdef _MIPS_CS_OSREL_MIN |
| 6122 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6123 | #endif |
| 6124 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6125 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6126 | #endif |
| 6127 | #ifdef _MIPS_CS_OS_NAME |
| 6128 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6129 | #endif |
| 6130 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6131 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6132 | #endif |
| 6133 | #ifdef _MIPS_CS_PROCESSORS |
| 6134 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6135 | #endif |
| 6136 | #ifdef _MIPS_CS_SERIAL |
| 6137 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6138 | #endif |
| 6139 | #ifdef _MIPS_CS_VENDOR |
| 6140 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6141 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6142 | }; |
| 6143 | |
| 6144 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6145 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6146 | { |
| 6147 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6148 | sizeof(posix_constants_confstr) |
| 6149 | / sizeof(struct constdef)); |
| 6150 | } |
| 6151 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6152 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6153 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6154 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6155 | |
| 6156 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6157 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6158 | { |
| 6159 | PyObject *result = NULL; |
| 6160 | int name; |
| 6161 | char buffer[64]; |
| 6162 | |
| 6163 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 6164 | int len = confstr(name, buffer, sizeof(buffer)); |
| 6165 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6166 | errno = 0; |
| 6167 | if (len == 0) { |
| 6168 | if (errno != 0) |
| 6169 | posix_error(); |
| 6170 | else |
| 6171 | result = PyString_FromString(""); |
| 6172 | } |
| 6173 | else { |
| 6174 | if (len >= sizeof(buffer)) { |
| 6175 | result = PyString_FromStringAndSize(NULL, len); |
| 6176 | if (result != NULL) |
| 6177 | confstr(name, PyString_AS_STRING(result), len+1); |
| 6178 | } |
| 6179 | else |
| 6180 | result = PyString_FromString(buffer); |
| 6181 | } |
| 6182 | } |
| 6183 | return result; |
| 6184 | } |
| 6185 | #endif |
| 6186 | |
| 6187 | |
| 6188 | #ifdef HAVE_SYSCONF |
| 6189 | static struct constdef posix_constants_sysconf[] = { |
| 6190 | #ifdef _SC_2_CHAR_TERM |
| 6191 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6192 | #endif |
| 6193 | #ifdef _SC_2_C_BIND |
| 6194 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6195 | #endif |
| 6196 | #ifdef _SC_2_C_DEV |
| 6197 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6198 | #endif |
| 6199 | #ifdef _SC_2_C_VERSION |
| 6200 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6201 | #endif |
| 6202 | #ifdef _SC_2_FORT_DEV |
| 6203 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6204 | #endif |
| 6205 | #ifdef _SC_2_FORT_RUN |
| 6206 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6207 | #endif |
| 6208 | #ifdef _SC_2_LOCALEDEF |
| 6209 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6210 | #endif |
| 6211 | #ifdef _SC_2_SW_DEV |
| 6212 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6213 | #endif |
| 6214 | #ifdef _SC_2_UPE |
| 6215 | {"SC_2_UPE", _SC_2_UPE}, |
| 6216 | #endif |
| 6217 | #ifdef _SC_2_VERSION |
| 6218 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6219 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6220 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6221 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6222 | #endif |
| 6223 | #ifdef _SC_ACL |
| 6224 | {"SC_ACL", _SC_ACL}, |
| 6225 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6226 | #ifdef _SC_AIO_LISTIO_MAX |
| 6227 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6228 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6229 | #ifdef _SC_AIO_MAX |
| 6230 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6231 | #endif |
| 6232 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6233 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6234 | #endif |
| 6235 | #ifdef _SC_ARG_MAX |
| 6236 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6237 | #endif |
| 6238 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6239 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6240 | #endif |
| 6241 | #ifdef _SC_ATEXIT_MAX |
| 6242 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6243 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6244 | #ifdef _SC_AUDIT |
| 6245 | {"SC_AUDIT", _SC_AUDIT}, |
| 6246 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6247 | #ifdef _SC_AVPHYS_PAGES |
| 6248 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6249 | #endif |
| 6250 | #ifdef _SC_BC_BASE_MAX |
| 6251 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6252 | #endif |
| 6253 | #ifdef _SC_BC_DIM_MAX |
| 6254 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6255 | #endif |
| 6256 | #ifdef _SC_BC_SCALE_MAX |
| 6257 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6258 | #endif |
| 6259 | #ifdef _SC_BC_STRING_MAX |
| 6260 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6261 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6262 | #ifdef _SC_CAP |
| 6263 | {"SC_CAP", _SC_CAP}, |
| 6264 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6265 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6266 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6267 | #endif |
| 6268 | #ifdef _SC_CHAR_BIT |
| 6269 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6270 | #endif |
| 6271 | #ifdef _SC_CHAR_MAX |
| 6272 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6273 | #endif |
| 6274 | #ifdef _SC_CHAR_MIN |
| 6275 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6276 | #endif |
| 6277 | #ifdef _SC_CHILD_MAX |
| 6278 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6279 | #endif |
| 6280 | #ifdef _SC_CLK_TCK |
| 6281 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6282 | #endif |
| 6283 | #ifdef _SC_COHER_BLKSZ |
| 6284 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6285 | #endif |
| 6286 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6287 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6288 | #endif |
| 6289 | #ifdef _SC_DCACHE_ASSOC |
| 6290 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6291 | #endif |
| 6292 | #ifdef _SC_DCACHE_BLKSZ |
| 6293 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6294 | #endif |
| 6295 | #ifdef _SC_DCACHE_LINESZ |
| 6296 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6297 | #endif |
| 6298 | #ifdef _SC_DCACHE_SZ |
| 6299 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6300 | #endif |
| 6301 | #ifdef _SC_DCACHE_TBLKSZ |
| 6302 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6303 | #endif |
| 6304 | #ifdef _SC_DELAYTIMER_MAX |
| 6305 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6306 | #endif |
| 6307 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6308 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6309 | #endif |
| 6310 | #ifdef _SC_EXPR_NEST_MAX |
| 6311 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6312 | #endif |
| 6313 | #ifdef _SC_FSYNC |
| 6314 | {"SC_FSYNC", _SC_FSYNC}, |
| 6315 | #endif |
| 6316 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6317 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6318 | #endif |
| 6319 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6320 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6321 | #endif |
| 6322 | #ifdef _SC_ICACHE_ASSOC |
| 6323 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6324 | #endif |
| 6325 | #ifdef _SC_ICACHE_BLKSZ |
| 6326 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6327 | #endif |
| 6328 | #ifdef _SC_ICACHE_LINESZ |
| 6329 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6330 | #endif |
| 6331 | #ifdef _SC_ICACHE_SZ |
| 6332 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6333 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6334 | #ifdef _SC_INF |
| 6335 | {"SC_INF", _SC_INF}, |
| 6336 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6337 | #ifdef _SC_INT_MAX |
| 6338 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6339 | #endif |
| 6340 | #ifdef _SC_INT_MIN |
| 6341 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6342 | #endif |
| 6343 | #ifdef _SC_IOV_MAX |
| 6344 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6345 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6346 | #ifdef _SC_IP_SECOPTS |
| 6347 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6348 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6349 | #ifdef _SC_JOB_CONTROL |
| 6350 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6351 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6352 | #ifdef _SC_KERN_POINTERS |
| 6353 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6354 | #endif |
| 6355 | #ifdef _SC_KERN_SIM |
| 6356 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6357 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6358 | #ifdef _SC_LINE_MAX |
| 6359 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6360 | #endif |
| 6361 | #ifdef _SC_LOGIN_NAME_MAX |
| 6362 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6363 | #endif |
| 6364 | #ifdef _SC_LOGNAME_MAX |
| 6365 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6366 | #endif |
| 6367 | #ifdef _SC_LONG_BIT |
| 6368 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6369 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6370 | #ifdef _SC_MAC |
| 6371 | {"SC_MAC", _SC_MAC}, |
| 6372 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6373 | #ifdef _SC_MAPPED_FILES |
| 6374 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6375 | #endif |
| 6376 | #ifdef _SC_MAXPID |
| 6377 | {"SC_MAXPID", _SC_MAXPID}, |
| 6378 | #endif |
| 6379 | #ifdef _SC_MB_LEN_MAX |
| 6380 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6381 | #endif |
| 6382 | #ifdef _SC_MEMLOCK |
| 6383 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6384 | #endif |
| 6385 | #ifdef _SC_MEMLOCK_RANGE |
| 6386 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6387 | #endif |
| 6388 | #ifdef _SC_MEMORY_PROTECTION |
| 6389 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6390 | #endif |
| 6391 | #ifdef _SC_MESSAGE_PASSING |
| 6392 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6393 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6394 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6395 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6396 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6397 | #ifdef _SC_MQ_OPEN_MAX |
| 6398 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6399 | #endif |
| 6400 | #ifdef _SC_MQ_PRIO_MAX |
| 6401 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6402 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6403 | #ifdef _SC_NACLS_MAX |
| 6404 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6405 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6406 | #ifdef _SC_NGROUPS_MAX |
| 6407 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6408 | #endif |
| 6409 | #ifdef _SC_NL_ARGMAX |
| 6410 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6411 | #endif |
| 6412 | #ifdef _SC_NL_LANGMAX |
| 6413 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6414 | #endif |
| 6415 | #ifdef _SC_NL_MSGMAX |
| 6416 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6417 | #endif |
| 6418 | #ifdef _SC_NL_NMAX |
| 6419 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6420 | #endif |
| 6421 | #ifdef _SC_NL_SETMAX |
| 6422 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6423 | #endif |
| 6424 | #ifdef _SC_NL_TEXTMAX |
| 6425 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6426 | #endif |
| 6427 | #ifdef _SC_NPROCESSORS_CONF |
| 6428 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6429 | #endif |
| 6430 | #ifdef _SC_NPROCESSORS_ONLN |
| 6431 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6432 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6433 | #ifdef _SC_NPROC_CONF |
| 6434 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6435 | #endif |
| 6436 | #ifdef _SC_NPROC_ONLN |
| 6437 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6438 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6439 | #ifdef _SC_NZERO |
| 6440 | {"SC_NZERO", _SC_NZERO}, |
| 6441 | #endif |
| 6442 | #ifdef _SC_OPEN_MAX |
| 6443 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6444 | #endif |
| 6445 | #ifdef _SC_PAGESIZE |
| 6446 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6447 | #endif |
| 6448 | #ifdef _SC_PAGE_SIZE |
| 6449 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6450 | #endif |
| 6451 | #ifdef _SC_PASS_MAX |
| 6452 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6453 | #endif |
| 6454 | #ifdef _SC_PHYS_PAGES |
| 6455 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6456 | #endif |
| 6457 | #ifdef _SC_PII |
| 6458 | {"SC_PII", _SC_PII}, |
| 6459 | #endif |
| 6460 | #ifdef _SC_PII_INTERNET |
| 6461 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6462 | #endif |
| 6463 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6464 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6465 | #endif |
| 6466 | #ifdef _SC_PII_INTERNET_STREAM |
| 6467 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6468 | #endif |
| 6469 | #ifdef _SC_PII_OSI |
| 6470 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6471 | #endif |
| 6472 | #ifdef _SC_PII_OSI_CLTS |
| 6473 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6474 | #endif |
| 6475 | #ifdef _SC_PII_OSI_COTS |
| 6476 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6477 | #endif |
| 6478 | #ifdef _SC_PII_OSI_M |
| 6479 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6480 | #endif |
| 6481 | #ifdef _SC_PII_SOCKET |
| 6482 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6483 | #endif |
| 6484 | #ifdef _SC_PII_XTI |
| 6485 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6486 | #endif |
| 6487 | #ifdef _SC_POLL |
| 6488 | {"SC_POLL", _SC_POLL}, |
| 6489 | #endif |
| 6490 | #ifdef _SC_PRIORITIZED_IO |
| 6491 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6492 | #endif |
| 6493 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6494 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6495 | #endif |
| 6496 | #ifdef _SC_REALTIME_SIGNALS |
| 6497 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6498 | #endif |
| 6499 | #ifdef _SC_RE_DUP_MAX |
| 6500 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6501 | #endif |
| 6502 | #ifdef _SC_RTSIG_MAX |
| 6503 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6504 | #endif |
| 6505 | #ifdef _SC_SAVED_IDS |
| 6506 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6507 | #endif |
| 6508 | #ifdef _SC_SCHAR_MAX |
| 6509 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6510 | #endif |
| 6511 | #ifdef _SC_SCHAR_MIN |
| 6512 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6513 | #endif |
| 6514 | #ifdef _SC_SELECT |
| 6515 | {"SC_SELECT", _SC_SELECT}, |
| 6516 | #endif |
| 6517 | #ifdef _SC_SEMAPHORES |
| 6518 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6519 | #endif |
| 6520 | #ifdef _SC_SEM_NSEMS_MAX |
| 6521 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6522 | #endif |
| 6523 | #ifdef _SC_SEM_VALUE_MAX |
| 6524 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6525 | #endif |
| 6526 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6527 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6528 | #endif |
| 6529 | #ifdef _SC_SHRT_MAX |
| 6530 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6531 | #endif |
| 6532 | #ifdef _SC_SHRT_MIN |
| 6533 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6534 | #endif |
| 6535 | #ifdef _SC_SIGQUEUE_MAX |
| 6536 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6537 | #endif |
| 6538 | #ifdef _SC_SIGRT_MAX |
| 6539 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6540 | #endif |
| 6541 | #ifdef _SC_SIGRT_MIN |
| 6542 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6543 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6544 | #ifdef _SC_SOFTPOWER |
| 6545 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6546 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6547 | #ifdef _SC_SPLIT_CACHE |
| 6548 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6549 | #endif |
| 6550 | #ifdef _SC_SSIZE_MAX |
| 6551 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6552 | #endif |
| 6553 | #ifdef _SC_STACK_PROT |
| 6554 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6555 | #endif |
| 6556 | #ifdef _SC_STREAM_MAX |
| 6557 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6558 | #endif |
| 6559 | #ifdef _SC_SYNCHRONIZED_IO |
| 6560 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6561 | #endif |
| 6562 | #ifdef _SC_THREADS |
| 6563 | {"SC_THREADS", _SC_THREADS}, |
| 6564 | #endif |
| 6565 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6566 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6567 | #endif |
| 6568 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6569 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6570 | #endif |
| 6571 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6572 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6573 | #endif |
| 6574 | #ifdef _SC_THREAD_KEYS_MAX |
| 6575 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6576 | #endif |
| 6577 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6578 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6579 | #endif |
| 6580 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6581 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6582 | #endif |
| 6583 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6584 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6585 | #endif |
| 6586 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6587 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6588 | #endif |
| 6589 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6590 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6591 | #endif |
| 6592 | #ifdef _SC_THREAD_STACK_MIN |
| 6593 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6594 | #endif |
| 6595 | #ifdef _SC_THREAD_THREADS_MAX |
| 6596 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6597 | #endif |
| 6598 | #ifdef _SC_TIMERS |
| 6599 | {"SC_TIMERS", _SC_TIMERS}, |
| 6600 | #endif |
| 6601 | #ifdef _SC_TIMER_MAX |
| 6602 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6603 | #endif |
| 6604 | #ifdef _SC_TTY_NAME_MAX |
| 6605 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6606 | #endif |
| 6607 | #ifdef _SC_TZNAME_MAX |
| 6608 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6609 | #endif |
| 6610 | #ifdef _SC_T_IOV_MAX |
| 6611 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6612 | #endif |
| 6613 | #ifdef _SC_UCHAR_MAX |
| 6614 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6615 | #endif |
| 6616 | #ifdef _SC_UINT_MAX |
| 6617 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6618 | #endif |
| 6619 | #ifdef _SC_UIO_MAXIOV |
| 6620 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6621 | #endif |
| 6622 | #ifdef _SC_ULONG_MAX |
| 6623 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6624 | #endif |
| 6625 | #ifdef _SC_USHRT_MAX |
| 6626 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6627 | #endif |
| 6628 | #ifdef _SC_VERSION |
| 6629 | {"SC_VERSION", _SC_VERSION}, |
| 6630 | #endif |
| 6631 | #ifdef _SC_WORD_BIT |
| 6632 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6633 | #endif |
| 6634 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6635 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6636 | #endif |
| 6637 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6638 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6639 | #endif |
| 6640 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6641 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6642 | #endif |
| 6643 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6644 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6645 | #endif |
| 6646 | #ifdef _SC_XOPEN_CRYPT |
| 6647 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6648 | #endif |
| 6649 | #ifdef _SC_XOPEN_ENH_I18N |
| 6650 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6651 | #endif |
| 6652 | #ifdef _SC_XOPEN_LEGACY |
| 6653 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6654 | #endif |
| 6655 | #ifdef _SC_XOPEN_REALTIME |
| 6656 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6657 | #endif |
| 6658 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6659 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6660 | #endif |
| 6661 | #ifdef _SC_XOPEN_SHM |
| 6662 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6663 | #endif |
| 6664 | #ifdef _SC_XOPEN_UNIX |
| 6665 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6666 | #endif |
| 6667 | #ifdef _SC_XOPEN_VERSION |
| 6668 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6669 | #endif |
| 6670 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6671 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6672 | #endif |
| 6673 | #ifdef _SC_XOPEN_XPG2 |
| 6674 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6675 | #endif |
| 6676 | #ifdef _SC_XOPEN_XPG3 |
| 6677 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6678 | #endif |
| 6679 | #ifdef _SC_XOPEN_XPG4 |
| 6680 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6681 | #endif |
| 6682 | }; |
| 6683 | |
| 6684 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6685 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6686 | { |
| 6687 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6688 | sizeof(posix_constants_sysconf) |
| 6689 | / sizeof(struct constdef)); |
| 6690 | } |
| 6691 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6692 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6693 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6694 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6695 | |
| 6696 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6697 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6698 | { |
| 6699 | PyObject *result = NULL; |
| 6700 | int name; |
| 6701 | |
| 6702 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6703 | int value; |
| 6704 | |
| 6705 | errno = 0; |
| 6706 | value = sysconf(name); |
| 6707 | if (value == -1 && errno != 0) |
| 6708 | posix_error(); |
| 6709 | else |
| 6710 | result = PyInt_FromLong(value); |
| 6711 | } |
| 6712 | return result; |
| 6713 | } |
| 6714 | #endif |
| 6715 | |
| 6716 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6717 | /* This code is used to ensure that the tables of configuration value names |
| 6718 | * are in sorted order as required by conv_confname(), and also to build the |
| 6719 | * the exported dictionaries that are used to publish information about the |
| 6720 | * names available on the host platform. |
| 6721 | * |
| 6722 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6723 | * when used, even for platforms we're not able to test on. It also makes |
| 6724 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6725 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6726 | |
| 6727 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6728 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6729 | { |
| 6730 | const struct constdef *c1 = |
| 6731 | (const struct constdef *) v1; |
| 6732 | const struct constdef *c2 = |
| 6733 | (const struct constdef *) v2; |
| 6734 | |
| 6735 | return strcmp(c1->name, c2->name); |
| 6736 | } |
| 6737 | |
| 6738 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6739 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6740 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6741 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6742 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6743 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6744 | |
| 6745 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6746 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6747 | if (d == NULL) |
| 6748 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6749 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6750 | for (i=0; i < tablesize; ++i) { |
| 6751 | PyObject *o = PyInt_FromLong(table[i].value); |
| 6752 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6753 | Py_XDECREF(o); |
| 6754 | Py_DECREF(d); |
| 6755 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6756 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6757 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6758 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6759 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6760 | } |
| 6761 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6762 | /* Return -1 on failure, 0 on success. */ |
| 6763 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6764 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6765 | { |
| 6766 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6767 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6768 | sizeof(posix_constants_pathconf) |
| 6769 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6770 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6771 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6772 | #endif |
| 6773 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6774 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6775 | sizeof(posix_constants_confstr) |
| 6776 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6777 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6778 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6779 | #endif |
| 6780 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6781 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6782 | sizeof(posix_constants_sysconf) |
| 6783 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6784 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6785 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6786 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6787 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6788 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6789 | |
| 6790 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6791 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6792 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6793 | 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] | 6794 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6795 | |
| 6796 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6797 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6798 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6799 | abort(); |
| 6800 | /*NOTREACHED*/ |
| 6801 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6802 | return NULL; |
| 6803 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6804 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6805 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6806 | PyDoc_STRVAR(win32_startfile__doc__, |
| 6807 | "startfile(filepath) - Start a file with its associated application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6808 | \n\ |
| 6809 | This acts like double-clicking the file in Explorer, or giving the file\n\ |
| 6810 | name as an argument to the DOS \"start\" command: the file is opened\n\ |
| 6811 | with whatever application (if any) its extension is associated.\n\ |
| 6812 | \n\ |
| 6813 | startfile returns as soon as the associated application is launched.\n\ |
| 6814 | There is no option to wait for the application to close, and no way\n\ |
| 6815 | to retrieve the application's exit status.\n\ |
| 6816 | \n\ |
| 6817 | The filepath is relative to the current directory. If you want to use\n\ |
| 6818 | 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] | 6819 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6820 | |
| 6821 | static PyObject * |
| 6822 | win32_startfile(PyObject *self, PyObject *args) |
| 6823 | { |
| 6824 | char *filepath; |
| 6825 | HINSTANCE rc; |
| 6826 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) |
| 6827 | return NULL; |
| 6828 | Py_BEGIN_ALLOW_THREADS |
| 6829 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); |
| 6830 | Py_END_ALLOW_THREADS |
| 6831 | if (rc <= (HINSTANCE)32) |
| 6832 | return win32_error("startfile", filepath); |
| 6833 | Py_INCREF(Py_None); |
| 6834 | return Py_None; |
| 6835 | } |
| 6836 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6837 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6838 | #ifdef HAVE_GETLOADAVG |
| 6839 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6840 | "getloadavg() -> (float, float, float)\n\n\ |
| 6841 | Return the number of processes in the system run queue averaged over\n\ |
| 6842 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6843 | was unobtainable"); |
| 6844 | |
| 6845 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6846 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6847 | { |
| 6848 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6849 | if (getloadavg(loadavg, 3)!=3) { |
| 6850 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6851 | return NULL; |
| 6852 | } else |
| 6853 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6854 | } |
| 6855 | #endif |
| 6856 | |
| 6857 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6858 | static PyMethodDef posix_methods[] = { |
| 6859 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 6860 | #ifdef HAVE_TTYNAME |
| 6861 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 6862 | #endif |
| 6863 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 6864 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6865 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6866 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6867 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 6868 | #ifdef HAVE_LCHOWN |
| 6869 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 6870 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 6871 | #ifdef HAVE_CHROOT |
| 6872 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 6873 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6874 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6875 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6876 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6877 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6878 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 6879 | #ifdef Py_USING_UNICODE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6880 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6881 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 6882 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6883 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6884 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6885 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6886 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 6887 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 6888 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6889 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6890 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6891 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6892 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6893 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6894 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6895 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 6896 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 6897 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 6898 | {"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] | 6899 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6900 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6901 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6902 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6903 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6904 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6905 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6906 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6907 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6908 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6909 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 6910 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 6911 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6912 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6913 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6914 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6915 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6916 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6917 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 6918 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6919 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6920 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6921 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 6922 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6923 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6924 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6925 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6926 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6927 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6928 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6929 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6930 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6931 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6932 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6933 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6934 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6935 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6936 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6937 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6938 | #endif /* HAVE_GETEGID */ |
| 6939 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6940 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6941 | #endif /* HAVE_GETEUID */ |
| 6942 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6943 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6944 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6945 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6946 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6947 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6948 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6949 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6950 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6951 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6952 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6953 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6954 | #endif /* HAVE_GETPPID */ |
| 6955 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6956 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6957 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6958 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6959 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6960 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6961 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6962 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6963 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6964 | #ifdef HAVE_KILLPG |
| 6965 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 6966 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6967 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6968 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6969 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6970 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6971 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6972 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 6973 | {"popen2", win32_popen2, METH_VARARGS}, |
| 6974 | {"popen3", win32_popen3, METH_VARARGS}, |
| 6975 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6976 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6977 | #else |
| 6978 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 6979 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 6980 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 6981 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 6982 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 6983 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6984 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6985 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6986 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6987 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6988 | #ifdef HAVE_SETEUID |
| 6989 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 6990 | #endif /* HAVE_SETEUID */ |
| 6991 | #ifdef HAVE_SETEGID |
| 6992 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 6993 | #endif /* HAVE_SETEGID */ |
| 6994 | #ifdef HAVE_SETREUID |
| 6995 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 6996 | #endif /* HAVE_SETREUID */ |
| 6997 | #ifdef HAVE_SETREGID |
| 6998 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 6999 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7000 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7001 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7002 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7003 | #ifdef HAVE_SETGROUPS |
| 7004 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 7005 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7006 | #ifdef HAVE_GETPGID |
| 7007 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7008 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7009 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7010 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7011 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7012 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7013 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7014 | #endif /* HAVE_WAIT */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7015 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7016 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7017 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7018 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7019 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7020 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7021 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7022 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7023 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7024 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7025 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7026 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7027 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7028 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7029 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7030 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7031 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 7032 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7033 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7034 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7035 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7036 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7037 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 7038 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7039 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7040 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7041 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7042 | #endif |
| 7043 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7044 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7045 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7046 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7047 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7048 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7049 | #ifdef HAVE_DEVICE_MACROS |
| 7050 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7051 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7052 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7053 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7054 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7055 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7056 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7057 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7058 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7059 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7060 | #ifdef HAVE_UNSETENV |
| 7061 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7062 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7063 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7064 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7065 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7066 | #ifdef HAVE_FCHDIR |
| 7067 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7068 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7069 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7070 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7071 | #endif |
| 7072 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7073 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7074 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7075 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7076 | #ifdef WCOREDUMP |
| 7077 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7078 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7079 | #ifdef WIFCONTINUED |
| 7080 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7081 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7082 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7083 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7084 | #endif /* WIFSTOPPED */ |
| 7085 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7086 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7087 | #endif /* WIFSIGNALED */ |
| 7088 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7089 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7090 | #endif /* WIFEXITED */ |
| 7091 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7092 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7093 | #endif /* WEXITSTATUS */ |
| 7094 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7095 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7096 | #endif /* WTERMSIG */ |
| 7097 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7098 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7099 | #endif /* WSTOPSIG */ |
| 7100 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7101 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7102 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7103 | #endif |
| 7104 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7105 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7106 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 7107 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7108 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7109 | #endif |
| 7110 | #ifdef HAVE_TEMPNAM |
| 7111 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 7112 | #endif |
| 7113 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7114 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7115 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7116 | #ifdef HAVE_CONFSTR |
| 7117 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7118 | #endif |
| 7119 | #ifdef HAVE_SYSCONF |
| 7120 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7121 | #endif |
| 7122 | #ifdef HAVE_FPATHCONF |
| 7123 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7124 | #endif |
| 7125 | #ifdef HAVE_PATHCONF |
| 7126 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7127 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7128 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7129 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7130 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7131 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7132 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7133 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7134 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7135 | {NULL, NULL} /* Sentinel */ |
| 7136 | }; |
| 7137 | |
| 7138 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7139 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7140 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7141 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7142 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7143 | } |
| 7144 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7145 | #if defined(PYOS_OS2) |
| 7146 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7147 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7148 | { |
| 7149 | APIRET rc; |
| 7150 | ULONG values[QSV_MAX+1]; |
| 7151 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7152 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7153 | |
| 7154 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7155 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7156 | Py_END_ALLOW_THREADS |
| 7157 | |
| 7158 | if (rc != NO_ERROR) { |
| 7159 | os2_error(rc); |
| 7160 | return -1; |
| 7161 | } |
| 7162 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7163 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7164 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7165 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7166 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7167 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7168 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7169 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7170 | |
| 7171 | switch (values[QSV_VERSION_MINOR]) { |
| 7172 | case 0: ver = "2.00"; break; |
| 7173 | case 10: ver = "2.10"; break; |
| 7174 | case 11: ver = "2.11"; break; |
| 7175 | case 30: ver = "3.00"; break; |
| 7176 | case 40: ver = "4.00"; break; |
| 7177 | case 50: ver = "5.00"; break; |
| 7178 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7179 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7180 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7181 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7182 | ver = &tmp[0]; |
| 7183 | } |
| 7184 | |
| 7185 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7186 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7187 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7188 | |
| 7189 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7190 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7191 | tmp[1] = ':'; |
| 7192 | tmp[2] = '\0'; |
| 7193 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7194 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7195 | } |
| 7196 | #endif |
| 7197 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7198 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7199 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7200 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7201 | #ifdef F_OK |
| 7202 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7203 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7204 | #ifdef R_OK |
| 7205 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7206 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7207 | #ifdef W_OK |
| 7208 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7209 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7210 | #ifdef X_OK |
| 7211 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7212 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7213 | #ifdef NGROUPS_MAX |
| 7214 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7215 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7216 | #ifdef TMP_MAX |
| 7217 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7218 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7219 | #ifdef WCONTINUED |
| 7220 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7221 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7222 | #ifdef WNOHANG |
| 7223 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7224 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7225 | #ifdef WUNTRACED |
| 7226 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7227 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7228 | #ifdef O_RDONLY |
| 7229 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7230 | #endif |
| 7231 | #ifdef O_WRONLY |
| 7232 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7233 | #endif |
| 7234 | #ifdef O_RDWR |
| 7235 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7236 | #endif |
| 7237 | #ifdef O_NDELAY |
| 7238 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7239 | #endif |
| 7240 | #ifdef O_NONBLOCK |
| 7241 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7242 | #endif |
| 7243 | #ifdef O_APPEND |
| 7244 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7245 | #endif |
| 7246 | #ifdef O_DSYNC |
| 7247 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7248 | #endif |
| 7249 | #ifdef O_RSYNC |
| 7250 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7251 | #endif |
| 7252 | #ifdef O_SYNC |
| 7253 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7254 | #endif |
| 7255 | #ifdef O_NOCTTY |
| 7256 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7257 | #endif |
| 7258 | #ifdef O_CREAT |
| 7259 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7260 | #endif |
| 7261 | #ifdef O_EXCL |
| 7262 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7263 | #endif |
| 7264 | #ifdef O_TRUNC |
| 7265 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7266 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7267 | #ifdef O_BINARY |
| 7268 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7269 | #endif |
| 7270 | #ifdef O_TEXT |
| 7271 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7272 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7273 | #ifdef O_LARGEFILE |
| 7274 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7275 | #endif |
| 7276 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7277 | /* MS Windows */ |
| 7278 | #ifdef O_NOINHERIT |
| 7279 | /* Don't inherit in child processes. */ |
| 7280 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7281 | #endif |
| 7282 | #ifdef _O_SHORT_LIVED |
| 7283 | /* Optimize for short life (keep in memory). */ |
| 7284 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7285 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7286 | #endif |
| 7287 | #ifdef O_TEMPORARY |
| 7288 | /* Automatically delete when last handle is closed. */ |
| 7289 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7290 | #endif |
| 7291 | #ifdef O_RANDOM |
| 7292 | /* Optimize for random access. */ |
| 7293 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7294 | #endif |
| 7295 | #ifdef O_SEQUENTIAL |
| 7296 | /* Optimize for sequential access. */ |
| 7297 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7298 | #endif |
| 7299 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7300 | /* GNU extensions. */ |
| 7301 | #ifdef O_DIRECT |
| 7302 | /* Direct disk access. */ |
| 7303 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7304 | #endif |
| 7305 | #ifdef O_DIRECTORY |
| 7306 | /* Must be a directory. */ |
| 7307 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7308 | #endif |
| 7309 | #ifdef O_NOFOLLOW |
| 7310 | /* Do not follow links. */ |
| 7311 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7312 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7313 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7314 | /* These come from sysexits.h */ |
| 7315 | #ifdef EX_OK |
| 7316 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7317 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7318 | #ifdef EX_USAGE |
| 7319 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7320 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7321 | #ifdef EX_DATAERR |
| 7322 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7323 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7324 | #ifdef EX_NOINPUT |
| 7325 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7326 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7327 | #ifdef EX_NOUSER |
| 7328 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7329 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7330 | #ifdef EX_NOHOST |
| 7331 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7332 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7333 | #ifdef EX_UNAVAILABLE |
| 7334 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7335 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7336 | #ifdef EX_SOFTWARE |
| 7337 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7338 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7339 | #ifdef EX_OSERR |
| 7340 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7341 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7342 | #ifdef EX_OSFILE |
| 7343 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7344 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7345 | #ifdef EX_CANTCREAT |
| 7346 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7347 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7348 | #ifdef EX_IOERR |
| 7349 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7350 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7351 | #ifdef EX_TEMPFAIL |
| 7352 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7353 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7354 | #ifdef EX_PROTOCOL |
| 7355 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7356 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7357 | #ifdef EX_NOPERM |
| 7358 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7359 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7360 | #ifdef EX_CONFIG |
| 7361 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7362 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7363 | #ifdef EX_NOTFOUND |
| 7364 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7365 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7366 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7367 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7368 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7369 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7370 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7371 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7372 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7373 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7374 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7375 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7376 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7377 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7378 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7379 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7380 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7381 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7382 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7383 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7384 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7385 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7386 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7387 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7388 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7389 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7390 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7391 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7392 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7393 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7394 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7395 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7396 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7397 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7398 | #if defined(PYOS_OS2) |
| 7399 | if (insertvalues(d)) return -1; |
| 7400 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7401 | return 0; |
| 7402 | } |
| 7403 | |
| 7404 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7405 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7406 | #define INITFUNC initnt |
| 7407 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7408 | |
| 7409 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7410 | #define INITFUNC initos2 |
| 7411 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7412 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7413 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7414 | #define INITFUNC initposix |
| 7415 | #define MODNAME "posix" |
| 7416 | #endif |
| 7417 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7418 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7419 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7420 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7421 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7422 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7423 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7424 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7425 | posix__doc__); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7426 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7427 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7428 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7429 | Py_XINCREF(v); |
| 7430 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7431 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7432 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7433 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7434 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7435 | return; |
| 7436 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7437 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7438 | return; |
| 7439 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7440 | Py_INCREF(PyExc_OSError); |
| 7441 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7442 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7443 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7444 | if (posix_putenv_garbage == NULL) |
| 7445 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7446 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7447 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7448 | stat_result_desc.name = MODNAME ".stat_result"; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7449 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7450 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7451 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7452 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7453 | structseq_new = StatResultType.tp_new; |
| 7454 | StatResultType.tp_new = statresult_new; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7455 | Py_INCREF((PyObject*) &StatResultType); |
| 7456 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7457 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7458 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7459 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7460 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7461 | PyModule_AddObject(m, "statvfs_result", |
| 7462 | (PyObject*) &StatVFSResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7463 | } |