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 | #include <signal.h> |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 58 | #ifdef HAVE_FCNTL_H |
| 59 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 60 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 62 | #ifdef HAVE_GRP_H |
| 63 | #include <grp.h> |
| 64 | #endif |
| 65 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 66 | #ifdef HAVE_SYSEXITS_H |
| 67 | #include <sysexits.h> |
| 68 | #endif /* HAVE_SYSEXITS_H */ |
| 69 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 70 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 71 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 72 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 73 | #include <process.h> |
| 74 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 75 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 76 | #define HAVE_GETCWD 1 |
| 77 | #define HAVE_OPENDIR 1 |
| 78 | #define HAVE_SYSTEM 1 |
| 79 | #if defined(__OS2__) |
| 80 | #define HAVE_EXECV 1 |
| 81 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 82 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 83 | #include <process.h> |
| 84 | #else |
| 85 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 86 | #define HAVE_EXECV 1 |
| 87 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 88 | #define HAVE_OPENDIR 1 |
| 89 | #define HAVE_PIPE 1 |
| 90 | #define HAVE_POPEN 1 |
| 91 | #define HAVE_SYSTEM 1 |
| 92 | #define HAVE_WAIT 1 |
| 93 | #else |
| 94 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 95 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 96 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 97 | #define HAVE_EXECV 1 |
| 98 | #define HAVE_PIPE 1 |
| 99 | #define HAVE_POPEN 1 |
| 100 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 101 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 102 | #define HAVE_FSYNC 1 |
| 103 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 104 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 105 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 106 | /* 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] | 107 | #else /* all other compilers */ |
| 108 | /* Unix functions that the configure script doesn't check for */ |
| 109 | #define HAVE_EXECV 1 |
| 110 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 111 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 112 | #define HAVE_FORK1 1 |
| 113 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 114 | #define HAVE_GETCWD 1 |
| 115 | #define HAVE_GETEGID 1 |
| 116 | #define HAVE_GETEUID 1 |
| 117 | #define HAVE_GETGID 1 |
| 118 | #define HAVE_GETPPID 1 |
| 119 | #define HAVE_GETUID 1 |
| 120 | #define HAVE_KILL 1 |
| 121 | #define HAVE_OPENDIR 1 |
| 122 | #define HAVE_PIPE 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 123 | #ifndef __rtems__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 124 | #define HAVE_POPEN 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 125 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 126 | #define HAVE_SYSTEM 1 |
| 127 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 128 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 129 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 130 | #endif /* _MSC_VER */ |
| 131 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 132 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 133 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 134 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 135 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 136 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 137 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 138 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 139 | (default) */ |
| 140 | extern char *ctermid_r(char *); |
| 141 | #endif |
| 142 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 143 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 144 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 145 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 146 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 147 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 148 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 149 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 150 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 151 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 152 | #endif |
| 153 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 154 | extern int chdir(char *); |
| 155 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 156 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 157 | extern int chdir(const char *); |
| 158 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 159 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 160 | #ifdef __BORLANDC__ |
| 161 | extern int chmod(const char *, int); |
| 162 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 163 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 164 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 165 | extern int chown(const char *, uid_t, gid_t); |
| 166 | extern char *getcwd(char *, int); |
| 167 | extern char *strerror(int); |
| 168 | extern int link(const char *, const char *); |
| 169 | extern int rename(const char *, const char *); |
| 170 | extern int stat(const char *, struct stat *); |
| 171 | extern int unlink(const char *); |
| 172 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 173 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 174 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 175 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 176 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 177 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 178 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 179 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 181 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 182 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 183 | #ifdef HAVE_UTIME_H |
| 184 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 185 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 187 | #ifdef HAVE_SYS_UTIME_H |
| 188 | #include <sys/utime.h> |
| 189 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 190 | #endif /* HAVE_SYS_UTIME_H */ |
| 191 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 192 | #ifdef HAVE_SYS_TIMES_H |
| 193 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 194 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 195 | |
| 196 | #ifdef HAVE_SYS_PARAM_H |
| 197 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 198 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 199 | |
| 200 | #ifdef HAVE_SYS_UTSNAME_H |
| 201 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 202 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 204 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 206 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 207 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 208 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 209 | #include <direct.h> |
| 210 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 211 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 212 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 213 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 214 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 215 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 216 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 217 | #endif |
| 218 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 219 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 220 | #endif |
| 221 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 223 | #endif |
| 224 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 225 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 226 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 227 | #include <direct.h> |
| 228 | #include <io.h> |
| 229 | #include <process.h> |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 230 | #include "osdefs.h" |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 231 | #define WIN32_LEAN_AND_MEAN |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 233 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 234 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 235 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 236 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 237 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 238 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 240 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 241 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 242 | #ifndef MAXPATHLEN |
| 243 | #define MAXPATHLEN 1024 |
| 244 | #endif /* MAXPATHLEN */ |
| 245 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 246 | #ifdef UNION_WAIT |
| 247 | /* Emulate some macros on systems that have a union instead of macros */ |
| 248 | |
| 249 | #ifndef WIFEXITED |
| 250 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 251 | #endif |
| 252 | |
| 253 | #ifndef WEXITSTATUS |
| 254 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 255 | #endif |
| 256 | |
| 257 | #ifndef WTERMSIG |
| 258 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 259 | #endif |
| 260 | |
| 261 | #endif /* UNION_WAIT */ |
| 262 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 263 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 264 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 265 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 266 | #define USE_CTERMID_R |
| 267 | #endif |
| 268 | |
| 269 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 270 | #define USE_TMPNAM_R |
| 271 | #endif |
| 272 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 273 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 274 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 275 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 276 | # define STAT _stati64 |
| 277 | # define FSTAT _fstati64 |
| 278 | # define STRUCT_STAT struct _stati64 |
| 279 | #else |
| 280 | # define STAT stat |
| 281 | # define FSTAT fstat |
| 282 | # define STRUCT_STAT struct stat |
| 283 | #endif |
| 284 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 285 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 286 | #include <sys/mkdev.h> |
| 287 | #else |
| 288 | #if defined(MAJOR_IN_SYSMACROS) |
| 289 | #include <sys/sysmacros.h> |
| 290 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 291 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 292 | #include <sys/mkdev.h> |
| 293 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 294 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 296 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 297 | #ifdef WITH_NEXT_FRAMEWORK |
| 298 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 299 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 300 | */ |
| 301 | #include <crt_externs.h> |
| 302 | static char **environ; |
| 303 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 304 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 305 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 306 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 307 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 308 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 309 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 310 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 311 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 312 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 313 | if (d == NULL) |
| 314 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 315 | #ifdef WITH_NEXT_FRAMEWORK |
| 316 | if (environ == NULL) |
| 317 | environ = *_NSGetEnviron(); |
| 318 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 319 | if (environ == NULL) |
| 320 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 321 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 322 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 323 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 324 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 325 | char *p = strchr(*e, '='); |
| 326 | if (p == NULL) |
| 327 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 328 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 329 | if (k == NULL) { |
| 330 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 331 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 332 | } |
| 333 | v = PyString_FromString(p+1); |
| 334 | if (v == NULL) { |
| 335 | PyErr_Clear(); |
| 336 | Py_DECREF(k); |
| 337 | continue; |
| 338 | } |
| 339 | if (PyDict_GetItem(d, k) == NULL) { |
| 340 | if (PyDict_SetItem(d, k, v) != 0) |
| 341 | PyErr_Clear(); |
| 342 | } |
| 343 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 344 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 345 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 346 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 347 | { |
| 348 | APIRET rc; |
| 349 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 350 | |
| 351 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 352 | 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] | 353 | PyObject *v = PyString_FromString(buffer); |
| 354 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 355 | Py_DECREF(v); |
| 356 | } |
| 357 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 358 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 359 | PyObject *v = PyString_FromString(buffer); |
| 360 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 361 | Py_DECREF(v); |
| 362 | } |
| 363 | } |
| 364 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 365 | return d; |
| 366 | } |
| 367 | |
| 368 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 369 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 370 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 371 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 372 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 373 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 374 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 375 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 376 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 377 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 378 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 379 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 382 | #ifdef Py_WIN_WIDE_FILENAMES |
| 383 | static PyObject * |
| 384 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 385 | { |
| 386 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 387 | } |
| 388 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 389 | |
| 390 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 391 | static PyObject * |
| 392 | posix_error_with_allocated_filename(char* name) |
| 393 | { |
| 394 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 395 | PyMem_Free(name); |
| 396 | return rc; |
| 397 | } |
| 398 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 399 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 400 | static PyObject * |
| 401 | win32_error(char* function, char* filename) |
| 402 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 403 | /* XXX We should pass the function name along in the future. |
| 404 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 405 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 406 | Windows error object, which is non-trivial. |
| 407 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 408 | errno = GetLastError(); |
| 409 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 410 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 411 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 412 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 413 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 414 | |
| 415 | #ifdef Py_WIN_WIDE_FILENAMES |
| 416 | static PyObject * |
| 417 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 418 | { |
| 419 | /* XXX - see win32_error for comments on 'function' */ |
| 420 | errno = GetLastError(); |
| 421 | if (filename) |
| 422 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 423 | else |
| 424 | return PyErr_SetFromWindowsErr(errno); |
| 425 | } |
| 426 | |
| 427 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 428 | { |
| 429 | /* XXX Perhaps we should make this API an alias of |
| 430 | PyObject_Unicode() instead ?! */ |
| 431 | if (PyUnicode_CheckExact(obj)) { |
| 432 | Py_INCREF(obj); |
| 433 | return obj; |
| 434 | } |
| 435 | if (PyUnicode_Check(obj)) { |
| 436 | /* For a Unicode subtype that's not a Unicode object, |
| 437 | return a true Unicode object with the same data. */ |
| 438 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 439 | PyUnicode_GET_SIZE(obj)); |
| 440 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 441 | return PyUnicode_FromEncodedObject(obj, |
| 442 | Py_FileSystemDefaultEncoding, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 443 | "strict"); |
| 444 | } |
| 445 | |
| 446 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 447 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 448 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 449 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 450 | #if defined(PYOS_OS2) |
| 451 | /********************************************************************** |
| 452 | * Helper Function to Trim and Format OS/2 Messages |
| 453 | **********************************************************************/ |
| 454 | static void |
| 455 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 456 | { |
| 457 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 458 | |
| 459 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 460 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 461 | |
| 462 | while (lastc > msgbuf && isspace(*lastc)) |
| 463 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 464 | } |
| 465 | |
| 466 | /* Add Optional Reason Text */ |
| 467 | if (reason) { |
| 468 | strcat(msgbuf, " : "); |
| 469 | strcat(msgbuf, reason); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /********************************************************************** |
| 474 | * Decode an OS/2 Operating System Error Code |
| 475 | * |
| 476 | * A convenience function to lookup an OS/2 error code and return a |
| 477 | * text message we can use to raise a Python exception. |
| 478 | * |
| 479 | * Notes: |
| 480 | * The messages for errors returned from the OS/2 kernel reside in |
| 481 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 482 | * |
| 483 | **********************************************************************/ |
| 484 | static char * |
| 485 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 486 | { |
| 487 | APIRET rc; |
| 488 | ULONG msglen; |
| 489 | |
| 490 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 491 | Py_BEGIN_ALLOW_THREADS |
| 492 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 493 | errorcode, "oso001.msg", &msglen); |
| 494 | Py_END_ALLOW_THREADS |
| 495 | |
| 496 | if (rc == NO_ERROR) |
| 497 | os2_formatmsg(msgbuf, msglen, reason); |
| 498 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 499 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 500 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 501 | |
| 502 | return msgbuf; |
| 503 | } |
| 504 | |
| 505 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 506 | errors are not in a global variable e.g. 'errno' nor are |
| 507 | they congruent with posix error numbers. */ |
| 508 | |
| 509 | static PyObject * os2_error(int code) |
| 510 | { |
| 511 | char text[1024]; |
| 512 | PyObject *v; |
| 513 | |
| 514 | os2_strerror(text, sizeof(text), code, ""); |
| 515 | |
| 516 | v = Py_BuildValue("(is)", code, text); |
| 517 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 518 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 519 | Py_DECREF(v); |
| 520 | } |
| 521 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 522 | } |
| 523 | |
| 524 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 525 | |
| 526 | /* POSIX generic methods */ |
| 527 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 528 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 529 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 530 | { |
| 531 | int fd; |
| 532 | int res; |
| 533 | fd = PyObject_AsFileDescriptor(fdobj); |
| 534 | if (fd < 0) |
| 535 | return NULL; |
| 536 | Py_BEGIN_ALLOW_THREADS |
| 537 | res = (*func)(fd); |
| 538 | Py_END_ALLOW_THREADS |
| 539 | if (res < 0) |
| 540 | return posix_error(); |
| 541 | Py_INCREF(Py_None); |
| 542 | return Py_None; |
| 543 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 544 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 545 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 546 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 547 | unicode_file_names(void) |
| 548 | { |
| 549 | static int canusewide = -1; |
| 550 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 551 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 552 | the Windows NT family. */ |
| 553 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 554 | } |
| 555 | return canusewide; |
| 556 | } |
| 557 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 558 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 559 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 560 | posix_1str(PyObject *args, char *format, int (*func)(const char*), |
| 561 | char *wformat, int (*wfunc)(const Py_UNICODE*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 562 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 563 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 564 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 565 | #ifdef Py_WIN_WIDE_FILENAMES |
| 566 | if (unicode_file_names()) { |
| 567 | PyUnicodeObject *po; |
| 568 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 569 | Py_BEGIN_ALLOW_THREADS |
| 570 | /* PyUnicode_AS_UNICODE OK without thread |
| 571 | lock as it is a simple dereference. */ |
| 572 | res = (*wfunc)(PyUnicode_AS_UNICODE(po)); |
| 573 | Py_END_ALLOW_THREADS |
| 574 | if (res < 0) |
Mark Hammond | d389036 | 2002-10-03 07:24:48 +0000 | [diff] [blame] | 575 | return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 576 | Py_INCREF(Py_None); |
| 577 | return Py_None; |
| 578 | } |
| 579 | /* Drop the argument parsing error as narrow |
| 580 | strings are also valid. */ |
| 581 | PyErr_Clear(); |
| 582 | } |
| 583 | #else |
| 584 | /* Platforms that don't support Unicode filenames |
| 585 | shouldn't be passing these extra params */ |
| 586 | assert(wformat==NULL && wfunc == NULL); |
| 587 | #endif |
| 588 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 589 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 590 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 591 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 592 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 593 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 594 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 595 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 596 | return posix_error_with_allocated_filename(path1); |
| 597 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 598 | Py_INCREF(Py_None); |
| 599 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 602 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 603 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 604 | char *format, |
| 605 | int (*func)(const char *, const char *), |
| 606 | char *wformat, |
| 607 | int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 608 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 609 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 610 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 611 | #ifdef Py_WIN_WIDE_FILENAMES |
| 612 | if (unicode_file_names()) { |
| 613 | PyObject *po1; |
| 614 | PyObject *po2; |
| 615 | if (PyArg_ParseTuple(args, wformat, &po1, &po2)) { |
| 616 | if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) { |
| 617 | PyObject *wpath1; |
| 618 | PyObject *wpath2; |
| 619 | wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1); |
| 620 | wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2); |
| 621 | if (!wpath1 || !wpath2) { |
| 622 | Py_XDECREF(wpath1); |
| 623 | Py_XDECREF(wpath2); |
| 624 | return NULL; |
| 625 | } |
| 626 | Py_BEGIN_ALLOW_THREADS |
| 627 | /* PyUnicode_AS_UNICODE OK without thread |
| 628 | lock as it is a simple dereference. */ |
| 629 | res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1), |
| 630 | PyUnicode_AS_UNICODE(wpath2)); |
| 631 | Py_END_ALLOW_THREADS |
| 632 | Py_XDECREF(wpath1); |
| 633 | Py_XDECREF(wpath2); |
| 634 | if (res != 0) |
| 635 | return posix_error(); |
| 636 | Py_INCREF(Py_None); |
| 637 | return Py_None; |
| 638 | } |
| 639 | /* Else flow through as neither is Unicode. */ |
| 640 | } |
| 641 | /* Drop the argument parsing error as narrow |
| 642 | strings are also valid. */ |
| 643 | PyErr_Clear(); |
| 644 | } |
| 645 | #else |
| 646 | /* Platforms that don't support Unicode filenames |
| 647 | shouldn't be passing these extra params */ |
| 648 | assert(wformat==NULL && wfunc == NULL); |
| 649 | #endif |
| 650 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 651 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 652 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 653 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 654 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 655 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 656 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 657 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 658 | PyMem_Free(path1); |
| 659 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 660 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 661 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 662 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 663 | Py_INCREF(Py_None); |
| 664 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 667 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 668 | "stat_result: Result from stat or lstat.\n\n\ |
| 669 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 670 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 671 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 672 | \n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 673 | 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] | 674 | they are available as attributes only.\n\ |
| 675 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 676 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 677 | |
| 678 | static PyStructSequence_Field stat_result_fields[] = { |
| 679 | {"st_mode", "protection bits"}, |
| 680 | {"st_ino", "inode"}, |
| 681 | {"st_dev", "device"}, |
| 682 | {"st_nlink", "number of hard links"}, |
| 683 | {"st_uid", "user ID of owner"}, |
| 684 | {"st_gid", "group ID of owner"}, |
| 685 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 686 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 687 | {NULL, "integer time of last access"}, |
| 688 | {NULL, "integer time of last modification"}, |
| 689 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 690 | {"st_atime", "time of last access"}, |
| 691 | {"st_mtime", "time of last modification"}, |
| 692 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 693 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 694 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 695 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 696 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 697 | {"st_blocks", "number of blocks allocated"}, |
| 698 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 699 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 700 | {"st_rdev", "device type (if inode device)"}, |
| 701 | #endif |
| 702 | {0} |
| 703 | }; |
| 704 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 705 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 706 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 707 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 708 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 709 | #endif |
| 710 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 711 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 712 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 713 | #else |
| 714 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 715 | #endif |
| 716 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 717 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 718 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 719 | #else |
| 720 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 721 | #endif |
| 722 | |
| 723 | static PyStructSequence_Desc stat_result_desc = { |
| 724 | "stat_result", /* name */ |
| 725 | stat_result__doc__, /* doc */ |
| 726 | stat_result_fields, |
| 727 | 10 |
| 728 | }; |
| 729 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 730 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 731 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 732 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 733 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 734 | 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] | 735 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 736 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 737 | |
| 738 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 739 | {"f_bsize", }, |
| 740 | {"f_frsize", }, |
| 741 | {"f_blocks", }, |
| 742 | {"f_bfree", }, |
| 743 | {"f_bavail", }, |
| 744 | {"f_files", }, |
| 745 | {"f_ffree", }, |
| 746 | {"f_favail", }, |
| 747 | {"f_flag", }, |
| 748 | {"f_namemax",}, |
| 749 | {0} |
| 750 | }; |
| 751 | |
| 752 | static PyStructSequence_Desc statvfs_result_desc = { |
| 753 | "statvfs_result", /* name */ |
| 754 | statvfs_result__doc__, /* doc */ |
| 755 | statvfs_result_fields, |
| 756 | 10 |
| 757 | }; |
| 758 | |
| 759 | static PyTypeObject StatResultType; |
| 760 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 761 | static newfunc structseq_new; |
| 762 | |
| 763 | static PyObject * |
| 764 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 765 | { |
| 766 | PyStructSequence *result; |
| 767 | int i; |
| 768 | |
| 769 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 770 | if (!result) |
| 771 | return NULL; |
| 772 | /* If we have been initialized from a tuple, |
| 773 | st_?time might be set to None. Initialize it |
| 774 | from the int slots. */ |
| 775 | for (i = 7; i <= 9; i++) { |
| 776 | if (result->ob_item[i+3] == Py_None) { |
| 777 | Py_DECREF(Py_None); |
| 778 | Py_INCREF(result->ob_item[i]); |
| 779 | result->ob_item[i+3] = result->ob_item[i]; |
| 780 | } |
| 781 | } |
| 782 | return (PyObject*)result; |
| 783 | } |
| 784 | |
| 785 | |
| 786 | |
| 787 | /* If true, st_?time is float. */ |
| 788 | static int _stat_float_times = 0; |
| 789 | |
| 790 | PyDoc_STRVAR(stat_float_times__doc__, |
| 791 | "stat_float_times([newval]) -> oldval\n\n\ |
| 792 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 793 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 794 | future calls return ints. \n\ |
| 795 | If newval is omitted, return the current setting.\n"); |
| 796 | |
| 797 | static PyObject* |
| 798 | stat_float_times(PyObject* self, PyObject *args) |
| 799 | { |
| 800 | int newval = -1; |
| 801 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 802 | return NULL; |
| 803 | if (newval == -1) |
| 804 | /* Return old value */ |
| 805 | return PyBool_FromLong(_stat_float_times); |
| 806 | _stat_float_times = newval; |
| 807 | Py_INCREF(Py_None); |
| 808 | return Py_None; |
| 809 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 810 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 811 | static void |
| 812 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 813 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 814 | PyObject *fval,*ival; |
| 815 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 816 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 817 | #else |
| 818 | ival = PyInt_FromLong((long)sec); |
| 819 | #endif |
| 820 | if (_stat_float_times) { |
| 821 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 822 | } else { |
| 823 | fval = ival; |
| 824 | Py_INCREF(fval); |
| 825 | } |
| 826 | PyStructSequence_SET_ITEM(v, index, ival); |
| 827 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 830 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 831 | (used by posix_stat() and posix_fstat()) */ |
| 832 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 833 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 834 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 835 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 836 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 837 | if (v == NULL) |
| 838 | return NULL; |
| 839 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 840 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 841 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 842 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 843 | PyLong_FromLongLong((PY_LONG_LONG)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 844 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 845 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 846 | #endif |
| 847 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 848 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 849 | PyLong_FromLongLong((PY_LONG_LONG)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 850 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 851 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 852 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 853 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 854 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 855 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 856 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 857 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 858 | PyLong_FromLongLong((PY_LONG_LONG)st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 859 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 860 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 861 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 862 | |
| 863 | #ifdef HAVE_STAT_TV_NSEC |
| 864 | ansec = st.st_atim.tv_nsec; |
| 865 | mnsec = st.st_mtim.tv_nsec; |
| 866 | cnsec = st.st_ctim.tv_nsec; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 867 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 868 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 869 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 870 | fill_time(v, 7, st.st_atime, ansec); |
| 871 | fill_time(v, 8, st.st_mtime, mnsec); |
| 872 | fill_time(v, 9, st.st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 873 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 874 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 875 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 876 | PyInt_FromLong((long)st.st_blksize)); |
| 877 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 878 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 879 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 880 | PyInt_FromLong((long)st.st_blocks)); |
| 881 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 882 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 883 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 884 | PyInt_FromLong((long)st.st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 885 | #endif |
| 886 | |
| 887 | if (PyErr_Occurred()) { |
| 888 | Py_DECREF(v); |
| 889 | return NULL; |
| 890 | } |
| 891 | |
| 892 | return v; |
| 893 | } |
| 894 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 895 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 896 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 897 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 898 | #ifdef __VMS |
| 899 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 900 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 901 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 902 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 903 | char *wformat, |
| 904 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 905 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 906 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 907 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 908 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 909 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 910 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 911 | #ifdef MS_WINDOWS |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 912 | int pathlen; |
| 913 | char pathcopy[MAX_PATH]; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 914 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 915 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 916 | |
| 917 | #ifdef Py_WIN_WIDE_FILENAMES |
| 918 | /* If on wide-character-capable OS see if argument |
| 919 | is Unicode and if so use wide API. */ |
| 920 | if (unicode_file_names()) { |
| 921 | PyUnicodeObject *po; |
| 922 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 923 | Py_UNICODE wpath[MAX_PATH+1]; |
| 924 | pathlen = wcslen(PyUnicode_AS_UNICODE(po)); |
| 925 | /* the library call can blow up if the file name is too long! */ |
| 926 | if (pathlen > MAX_PATH) { |
| 927 | errno = ENAMETOOLONG; |
| 928 | return posix_error(); |
| 929 | } |
| 930 | wcscpy(wpath, PyUnicode_AS_UNICODE(po)); |
| 931 | /* Remove trailing slash or backslash, unless it's the current |
| 932 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 933 | */ |
| 934 | if (pathlen > 0 && |
| 935 | (wpath[pathlen-1]== L'\\' || wpath[pathlen-1] == L'/')) { |
| 936 | /* It does end with a slash -- exempt the root drive cases. */ |
| 937 | /* XXX UNC root drives should also be exempted? */ |
| 938 | if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':')) |
| 939 | /* leave it alone */; |
| 940 | else { |
| 941 | /* nuke the trailing backslash */ |
| 942 | wpath[pathlen-1] = L'\0'; |
| 943 | } |
| 944 | } |
| 945 | Py_BEGIN_ALLOW_THREADS |
| 946 | /* PyUnicode_AS_UNICODE result OK without |
| 947 | thread lock as it is a simple dereference. */ |
| 948 | res = wstatfunc(wpath, &st); |
| 949 | Py_END_ALLOW_THREADS |
| 950 | if (res != 0) |
| 951 | return posix_error_with_unicode_filename(wpath); |
| 952 | return _pystat_fromstructstat(st); |
| 953 | } |
| 954 | /* Drop the argument parsing error as narrow strings |
| 955 | are also valid. */ |
| 956 | PyErr_Clear(); |
| 957 | } |
| 958 | #endif |
| 959 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 960 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 961 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 962 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 963 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 964 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 965 | #ifdef MS_WINDOWS |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 966 | pathlen = strlen(path); |
| 967 | /* the library call can blow up if the file name is too long! */ |
| 968 | if (pathlen > MAX_PATH) { |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 969 | PyMem_Free(pathfree); |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 970 | errno = ENAMETOOLONG; |
| 971 | return posix_error(); |
| 972 | } |
| 973 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 974 | /* Remove trailing slash or backslash, unless it's the current |
| 975 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 976 | */ |
| 977 | if (pathlen > 0 && |
| 978 | (path[pathlen-1]== '\\' || path[pathlen-1] == '/')) { |
| 979 | /* It does end with a slash -- exempt the root drive cases. */ |
| 980 | /* XXX UNC root drives should also be exempted? */ |
| 981 | if (pathlen == 1 || (pathlen == 3 && path[1] == ':')) |
| 982 | /* leave it alone */; |
| 983 | else { |
| 984 | /* nuke the trailing backslash */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 985 | strncpy(pathcopy, path, pathlen); |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 986 | pathcopy[pathlen-1] = '\0'; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 987 | path = pathcopy; |
| 988 | } |
| 989 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 990 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 991 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 992 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 993 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 994 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 995 | if (res != 0) |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 996 | return posix_error_with_allocated_filename(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 997 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 998 | PyMem_Free(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 999 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | /* POSIX methods */ |
| 1004 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1005 | PyDoc_STRVAR(posix_access__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1006 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1007 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1008 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1009 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1010 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1011 | 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] | 1012 | |
| 1013 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1014 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1015 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1016 | char *path; |
| 1017 | int mode; |
| 1018 | int res; |
| 1019 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1020 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1021 | if (unicode_file_names()) { |
| 1022 | PyUnicodeObject *po; |
| 1023 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1024 | Py_BEGIN_ALLOW_THREADS |
| 1025 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1026 | it is a simple dereference. */ |
| 1027 | res = _waccess(PyUnicode_AS_UNICODE(po), mode); |
| 1028 | Py_END_ALLOW_THREADS |
| 1029 | return(PyBool_FromLong(res == 0)); |
| 1030 | } |
| 1031 | /* Drop the argument parsing error as narrow strings |
| 1032 | are also valid. */ |
| 1033 | PyErr_Clear(); |
| 1034 | } |
| 1035 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1036 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1037 | return NULL; |
| 1038 | Py_BEGIN_ALLOW_THREADS |
| 1039 | res = access(path, mode); |
| 1040 | Py_END_ALLOW_THREADS |
Guido van Rossum | 674deb2 | 2002-09-01 15:06:28 +0000 | [diff] [blame] | 1041 | return(PyBool_FromLong(res == 0)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1044 | #ifndef F_OK |
| 1045 | #define F_OK 0 |
| 1046 | #endif |
| 1047 | #ifndef R_OK |
| 1048 | #define R_OK 4 |
| 1049 | #endif |
| 1050 | #ifndef W_OK |
| 1051 | #define W_OK 2 |
| 1052 | #endif |
| 1053 | #ifndef X_OK |
| 1054 | #define X_OK 1 |
| 1055 | #endif |
| 1056 | |
| 1057 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1058 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1059 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1060 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1061 | |
| 1062 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1063 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1064 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1065 | int id; |
| 1066 | char *ret; |
| 1067 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1068 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1069 | return NULL; |
| 1070 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1071 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1072 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1073 | if (id == 0) { |
| 1074 | ret = ttyname(); |
| 1075 | } |
| 1076 | else { |
| 1077 | ret = NULL; |
| 1078 | } |
| 1079 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1080 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1081 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1082 | if (ret == NULL) |
| 1083 | return(posix_error()); |
| 1084 | return(PyString_FromString(ret)); |
| 1085 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1086 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1087 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1088 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1089 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1090 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1091 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1092 | |
| 1093 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1094 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1095 | { |
| 1096 | char *ret; |
| 1097 | char buffer[L_ctermid]; |
| 1098 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1099 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1100 | ret = ctermid_r(buffer); |
| 1101 | #else |
| 1102 | ret = ctermid(buffer); |
| 1103 | #endif |
| 1104 | if (ret == NULL) |
| 1105 | return(posix_error()); |
| 1106 | return(PyString_FromString(buffer)); |
| 1107 | } |
| 1108 | #endif |
| 1109 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1110 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1111 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1112 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1113 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1114 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1115 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1116 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1117 | #ifdef MS_WINDOWS |
| 1118 | return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir); |
| 1119 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1120 | return posix_1str(args, "et:chdir", _chdir2, NULL, NULL); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1121 | #elif defined(__VMS) |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1122 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1123 | NULL, NULL); |
| 1124 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1125 | return posix_1str(args, "et:chdir", chdir, NULL, NULL); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1126 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1129 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1130 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1131 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1132 | 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] | 1133 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1134 | |
| 1135 | static PyObject * |
| 1136 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1137 | { |
| 1138 | return posix_fildes(fdobj, fchdir); |
| 1139 | } |
| 1140 | #endif /* HAVE_FCHDIR */ |
| 1141 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1142 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1143 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1144 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1145 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1146 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1147 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1148 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1149 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1150 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1151 | int i; |
| 1152 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1153 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1154 | if (unicode_file_names()) { |
| 1155 | PyUnicodeObject *po; |
| 1156 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1157 | Py_BEGIN_ALLOW_THREADS |
| 1158 | res = _wchmod(PyUnicode_AS_UNICODE(po), i); |
| 1159 | Py_END_ALLOW_THREADS |
| 1160 | if (res < 0) |
| 1161 | return posix_error_with_unicode_filename( |
| 1162 | PyUnicode_AS_UNICODE(po)); |
| 1163 | Py_INCREF(Py_None); |
| 1164 | return Py_None; |
| 1165 | } |
| 1166 | /* Drop the argument parsing error as narrow strings |
| 1167 | are also valid. */ |
| 1168 | PyErr_Clear(); |
| 1169 | } |
| 1170 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1171 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1172 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1173 | return NULL; |
| 1174 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1175 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1176 | Py_END_ALLOW_THREADS |
| 1177 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1178 | return posix_error_with_allocated_filename(path); |
| 1179 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1180 | Py_INCREF(Py_None); |
| 1181 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1184 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1185 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1186 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1187 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1188 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1189 | |
| 1190 | static PyObject * |
| 1191 | posix_chroot(PyObject *self, PyObject *args) |
| 1192 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1193 | return posix_1str(args, "et:chroot", chroot, NULL, NULL); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1194 | } |
| 1195 | #endif |
| 1196 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1197 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1198 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1199 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1200 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1201 | |
| 1202 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1203 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1204 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1205 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1206 | } |
| 1207 | #endif /* HAVE_FSYNC */ |
| 1208 | |
| 1209 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1210 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1211 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1212 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1213 | #endif |
| 1214 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1215 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1216 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1217 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1218 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1219 | |
| 1220 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1221 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1222 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1223 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1224 | } |
| 1225 | #endif /* HAVE_FDATASYNC */ |
| 1226 | |
| 1227 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1228 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1229 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1230 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1231 | 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] | 1232 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1233 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1234 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1235 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1236 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1237 | int uid, gid; |
| 1238 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1239 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1240 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1241 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1242 | return NULL; |
| 1243 | Py_BEGIN_ALLOW_THREADS |
| 1244 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1245 | Py_END_ALLOW_THREADS |
| 1246 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1247 | return posix_error_with_allocated_filename(path); |
| 1248 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1249 | Py_INCREF(Py_None); |
| 1250 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1251 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1252 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1253 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1254 | #ifdef HAVE_LCHOWN |
| 1255 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1256 | "lchown(path, uid, gid)\n\n\ |
| 1257 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1258 | This function will not follow symbolic links."); |
| 1259 | |
| 1260 | static PyObject * |
| 1261 | posix_lchown(PyObject *self, PyObject *args) |
| 1262 | { |
| 1263 | char *path = NULL; |
| 1264 | int uid, gid; |
| 1265 | int res; |
| 1266 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1267 | Py_FileSystemDefaultEncoding, &path, |
| 1268 | &uid, &gid)) |
| 1269 | return NULL; |
| 1270 | Py_BEGIN_ALLOW_THREADS |
| 1271 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1272 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1273 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1274 | return posix_error_with_allocated_filename(path); |
| 1275 | PyMem_Free(path); |
| 1276 | Py_INCREF(Py_None); |
| 1277 | return Py_None; |
| 1278 | } |
| 1279 | #endif /* HAVE_LCHOWN */ |
| 1280 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1281 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1282 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1283 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1284 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1285 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1286 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1287 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1288 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1289 | { |
| 1290 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1291 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1292 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1293 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1294 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1295 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1296 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1297 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1298 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1299 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1300 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1301 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1302 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1303 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1304 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1305 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1306 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1307 | "getcwdu() -> path\n\n\ |
| 1308 | Return a unicode string representing the current working directory."); |
| 1309 | |
| 1310 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1311 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1312 | { |
| 1313 | char buf[1026]; |
| 1314 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1315 | |
| 1316 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1317 | if (unicode_file_names()) { |
| 1318 | wchar_t *wres; |
| 1319 | wchar_t wbuf[1026]; |
| 1320 | Py_BEGIN_ALLOW_THREADS |
| 1321 | wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]); |
| 1322 | Py_END_ALLOW_THREADS |
| 1323 | if (wres == NULL) |
| 1324 | return posix_error(); |
| 1325 | return PyUnicode_FromWideChar(wbuf, wcslen(wbuf)); |
| 1326 | } |
| 1327 | #endif |
| 1328 | |
| 1329 | Py_BEGIN_ALLOW_THREADS |
| 1330 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1331 | res = _getcwd2(buf, sizeof buf); |
| 1332 | #else |
| 1333 | res = getcwd(buf, sizeof buf); |
| 1334 | #endif |
| 1335 | Py_END_ALLOW_THREADS |
| 1336 | if (res == NULL) |
| 1337 | return posix_error(); |
| 1338 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1339 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1340 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1341 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1342 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1343 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1344 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1345 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1346 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1347 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1348 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1349 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1350 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1351 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1352 | return posix_2str(args, "etet:link", link, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1353 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1354 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1355 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1356 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1357 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1358 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1359 | Return a list containing the names of the entries in the directory.\n\ |
| 1360 | \n\ |
| 1361 | path: path of directory to list\n\ |
| 1362 | \n\ |
| 1363 | 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] | 1364 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1365 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1366 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1367 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1368 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1369 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1370 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1371 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1372 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1373 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1374 | HANDLE hFindFile; |
| 1375 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1376 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 1377 | char namebuf[MAX_PATH*2+5]; |
| 1378 | char *bufptr = namebuf; |
| 1379 | int len = sizeof(namebuf)/sizeof(namebuf[0]); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1380 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1381 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1382 | /* If on wide-character-capable OS see if argument |
| 1383 | is Unicode and if so use wide API. */ |
| 1384 | if (unicode_file_names()) { |
| 1385 | PyUnicodeObject *po; |
| 1386 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1387 | WIN32_FIND_DATAW wFileData; |
| 1388 | Py_UNICODE wnamebuf[MAX_PATH*2+5]; |
| 1389 | Py_UNICODE wch; |
| 1390 | wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH); |
| 1391 | wnamebuf[MAX_PATH] = L'\0'; |
| 1392 | len = wcslen(wnamebuf); |
| 1393 | wch = (len > 0) ? wnamebuf[len-1] : L'\0'; |
| 1394 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1395 | wnamebuf[len++] = L'/'; |
| 1396 | wcscpy(wnamebuf + len, L"*.*"); |
| 1397 | if ((d = PyList_New(0)) == NULL) |
| 1398 | return NULL; |
| 1399 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1400 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1401 | errno = GetLastError(); |
| 1402 | if (errno == ERROR_FILE_NOT_FOUND) { |
| 1403 | return d; |
| 1404 | } |
| 1405 | Py_DECREF(d); |
| 1406 | return win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1407 | } |
| 1408 | do { |
| 1409 | if (wFileData.cFileName[0] == L'.' && |
| 1410 | (wFileData.cFileName[1] == L'\0' || |
| 1411 | wFileData.cFileName[1] == L'.' && |
| 1412 | wFileData.cFileName[2] == L'\0')) |
| 1413 | continue; |
| 1414 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1415 | if (v == NULL) { |
| 1416 | Py_DECREF(d); |
| 1417 | d = NULL; |
| 1418 | break; |
| 1419 | } |
| 1420 | if (PyList_Append(d, v) != 0) { |
| 1421 | Py_DECREF(v); |
| 1422 | Py_DECREF(d); |
| 1423 | d = NULL; |
| 1424 | break; |
| 1425 | } |
| 1426 | Py_DECREF(v); |
| 1427 | } while (FindNextFileW(hFindFile, &wFileData) == TRUE); |
| 1428 | |
| 1429 | if (FindClose(hFindFile) == FALSE) { |
| 1430 | Py_DECREF(d); |
| 1431 | return win32_error_unicode("FindClose", wnamebuf); |
| 1432 | } |
| 1433 | return d; |
| 1434 | } |
| 1435 | /* Drop the argument parsing error as narrow strings |
| 1436 | are also valid. */ |
| 1437 | PyErr_Clear(); |
| 1438 | } |
| 1439 | #endif |
| 1440 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1441 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1442 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1443 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1444 | if (len > 0) { |
| 1445 | char ch = namebuf[len-1]; |
| 1446 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 1447 | namebuf[len++] = '/'; |
| 1448 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1449 | strcpy(namebuf + len, "*.*"); |
| 1450 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1451 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1452 | return NULL; |
| 1453 | |
| 1454 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 1455 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1456 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 1457 | if (errno == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1458 | return d; |
| 1459 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1460 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1461 | } |
| 1462 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1463 | if (FileData.cFileName[0] == '.' && |
| 1464 | (FileData.cFileName[1] == '\0' || |
| 1465 | FileData.cFileName[1] == '.' && |
| 1466 | FileData.cFileName[2] == '\0')) |
| 1467 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1468 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1469 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 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 | if (PyList_Append(d, v) != 0) { |
| 1475 | Py_DECREF(v); |
| 1476 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1477 | d = NULL; |
| 1478 | break; |
| 1479 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1480 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1481 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 1482 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1483 | if (FindClose(hFindFile) == FALSE) { |
| 1484 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1485 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1486 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1487 | |
| 1488 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1489 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1490 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1491 | |
| 1492 | #ifndef MAX_PATH |
| 1493 | #define MAX_PATH CCHMAXPATH |
| 1494 | #endif |
| 1495 | char *name, *pt; |
| 1496 | int len; |
| 1497 | PyObject *d, *v; |
| 1498 | char namebuf[MAX_PATH+5]; |
| 1499 | HDIR hdir = 1; |
| 1500 | ULONG srchcnt = 1; |
| 1501 | FILEFINDBUF3 ep; |
| 1502 | APIRET rc; |
| 1503 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1504 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1505 | return NULL; |
| 1506 | if (len >= MAX_PATH) { |
| 1507 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1508 | return NULL; |
| 1509 | } |
| 1510 | strcpy(namebuf, name); |
| 1511 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1512 | if (*pt == ALTSEP) |
| 1513 | *pt = SEP; |
| 1514 | if (namebuf[len-1] != SEP) |
| 1515 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1516 | strcpy(namebuf + len, "*.*"); |
| 1517 | |
| 1518 | if ((d = PyList_New(0)) == NULL) |
| 1519 | return NULL; |
| 1520 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1521 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1522 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1523 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1524 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1525 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1526 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1527 | |
| 1528 | if (rc != NO_ERROR) { |
| 1529 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1530 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1533 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1534 | do { |
| 1535 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1536 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1537 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1538 | |
| 1539 | strcpy(namebuf, ep.achName); |
| 1540 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1541 | /* Leave Case of Name Alone -- In Native Form */ |
| 1542 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1543 | |
| 1544 | v = PyString_FromString(namebuf); |
| 1545 | if (v == NULL) { |
| 1546 | Py_DECREF(d); |
| 1547 | d = NULL; |
| 1548 | break; |
| 1549 | } |
| 1550 | if (PyList_Append(d, v) != 0) { |
| 1551 | Py_DECREF(v); |
| 1552 | Py_DECREF(d); |
| 1553 | d = NULL; |
| 1554 | break; |
| 1555 | } |
| 1556 | Py_DECREF(v); |
| 1557 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1558 | } |
| 1559 | |
| 1560 | return d; |
| 1561 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1562 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1563 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1564 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1565 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1566 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1567 | int arg_is_unicode = 1; |
| 1568 | |
| 1569 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 1570 | arg_is_unicode = 0; |
| 1571 | PyErr_Clear(); |
| 1572 | } |
| 1573 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1574 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1575 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1576 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1577 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1578 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1579 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1580 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1581 | return NULL; |
| 1582 | } |
| 1583 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1584 | if (ep->d_name[0] == '.' && |
| 1585 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1586 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1587 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1588 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1589 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1590 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1591 | d = NULL; |
| 1592 | break; |
| 1593 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1594 | #ifdef Py_USING_UNICODE |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1595 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1596 | PyObject *w; |
| 1597 | |
| 1598 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1599 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1600 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 1601 | if (w != NULL) { |
| 1602 | Py_DECREF(v); |
| 1603 | v = w; |
| 1604 | } |
| 1605 | else { |
| 1606 | /* fall back to the original byte string, as |
| 1607 | discussed in patch #683592 */ |
| 1608 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1609 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1610 | } |
| 1611 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1612 | if (PyList_Append(d, v) != 0) { |
| 1613 | Py_DECREF(v); |
| 1614 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1615 | d = NULL; |
| 1616 | break; |
| 1617 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1618 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1619 | } |
| 1620 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1621 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1622 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1623 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1624 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1625 | #endif /* which OS */ |
| 1626 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1627 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1628 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1629 | /* A helper function for abspath on win32 */ |
| 1630 | static PyObject * |
| 1631 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1632 | { |
| 1633 | /* assume encoded strings wont more than double no of chars */ |
| 1634 | char inbuf[MAX_PATH*2]; |
| 1635 | char *inbufp = inbuf; |
| 1636 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); |
| 1637 | char outbuf[MAX_PATH*2]; |
| 1638 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1639 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1640 | if (unicode_file_names()) { |
| 1641 | PyUnicodeObject *po; |
| 1642 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 1643 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 1644 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1645 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1646 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 1647 | woutbuf, &wtemp)) |
| 1648 | return win32_error("GetFullPathName", ""); |
| 1649 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 1650 | } |
| 1651 | /* Drop the argument parsing error as narrow strings |
| 1652 | are also valid. */ |
| 1653 | PyErr_Clear(); |
| 1654 | } |
| 1655 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1656 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1657 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1658 | &insize)) |
| 1659 | return NULL; |
| 1660 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1661 | outbuf, &temp)) |
| 1662 | return win32_error("GetFullPathName", inbuf); |
| 1663 | return PyString_FromString(outbuf); |
| 1664 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1665 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1666 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1667 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1668 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1669 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1670 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1671 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1672 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1673 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1674 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1675 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1676 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1677 | |
| 1678 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1679 | if (unicode_file_names()) { |
| 1680 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 1681 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1682 | Py_BEGIN_ALLOW_THREADS |
| 1683 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1684 | it is a simple dereference. */ |
| 1685 | res = _wmkdir(PyUnicode_AS_UNICODE(po)); |
| 1686 | Py_END_ALLOW_THREADS |
| 1687 | if (res < 0) |
| 1688 | return posix_error(); |
| 1689 | Py_INCREF(Py_None); |
| 1690 | return Py_None; |
| 1691 | } |
| 1692 | /* Drop the argument parsing error as narrow strings |
| 1693 | are also valid. */ |
| 1694 | PyErr_Clear(); |
| 1695 | } |
| 1696 | #endif |
| 1697 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1698 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1699 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1700 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1701 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1702 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1703 | res = mkdir(path); |
| 1704 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1705 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1706 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1707 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1708 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1709 | return posix_error_with_allocated_filename(path); |
| 1710 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1711 | Py_INCREF(Py_None); |
| 1712 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1715 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1716 | #ifdef HAVE_NICE |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1717 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) |
| 1718 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) |
| 1719 | #include <sys/resource.h> |
| 1720 | #endif |
| 1721 | #endif |
| 1722 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1723 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1724 | "nice(inc) -> new_priority\n\n\ |
| 1725 | 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] | 1726 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1727 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1728 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1729 | { |
| 1730 | int increment, value; |
| 1731 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1732 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1733 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1734 | |
| 1735 | /* There are two flavours of 'nice': one that returns the new |
| 1736 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1737 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 1738 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1739 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1740 | If we are of the nice family that returns the new priority, we |
| 1741 | need to clear errno before the call, and check if errno is filled |
| 1742 | before calling posix_error() on a returnvalue of -1, because the |
| 1743 | -1 may be the actual new priority! */ |
| 1744 | |
| 1745 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1746 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1747 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1748 | if (value == 0) |
| 1749 | value = getpriority(PRIO_PROCESS, 0); |
| 1750 | #endif |
| 1751 | if (value == -1 && errno != 0) |
| 1752 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1753 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1754 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1755 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1756 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1757 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1758 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1759 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1760 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1761 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1762 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1763 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1764 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1765 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1766 | #ifdef MS_WINDOWS |
| 1767 | return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename); |
| 1768 | #else |
| 1769 | return posix_2str(args, "etet:rename", rename, NULL, NULL); |
| 1770 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1773 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1774 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1775 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1776 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1777 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1778 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1779 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1780 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1781 | #ifdef MS_WINDOWS |
| 1782 | return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir); |
| 1783 | #else |
| 1784 | return posix_1str(args, "et:rmdir", rmdir, NULL, NULL); |
| 1785 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1788 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1789 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1790 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1791 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1792 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1793 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1794 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1795 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1796 | #ifdef MS_WINDOWS |
| 1797 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64); |
| 1798 | #else |
| 1799 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 1800 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1803 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1804 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1805 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1806 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1807 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1808 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1809 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1810 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1811 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1812 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1813 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1814 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1815 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1816 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1817 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1818 | Py_END_ALLOW_THREADS |
| 1819 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1820 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1821 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1822 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1823 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1824 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1825 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1826 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1827 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1828 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1829 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1830 | { |
| 1831 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1832 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1833 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 1834 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1835 | if (i < 0) |
| 1836 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1837 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1840 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1841 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1842 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1843 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1844 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1845 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1846 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1847 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1848 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1849 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1850 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1851 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1852 | #ifdef MS_WINDOWS |
| 1853 | return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink); |
| 1854 | #else |
| 1855 | return posix_1str(args, "et:remove", unlink, NULL, NULL); |
| 1856 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1859 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1860 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1861 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1862 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1863 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1864 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1865 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1866 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1867 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1868 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1869 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1870 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1871 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1872 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1873 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1874 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1875 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1876 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1877 | u.sysname, |
| 1878 | u.nodename, |
| 1879 | u.release, |
| 1880 | u.version, |
| 1881 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1882 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1883 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1884 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1885 | static int |
| 1886 | extract_time(PyObject *t, long* sec, long* usec) |
| 1887 | { |
| 1888 | long intval; |
| 1889 | if (PyFloat_Check(t)) { |
| 1890 | double tval = PyFloat_AsDouble(t); |
| 1891 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 1892 | if (!intobj) |
| 1893 | return -1; |
| 1894 | intval = PyInt_AsLong(intobj); |
| 1895 | Py_DECREF(intobj); |
| 1896 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 1897 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1898 | if (*usec < 0) |
| 1899 | /* If rounding gave us a negative number, |
| 1900 | truncate. */ |
| 1901 | *usec = 0; |
| 1902 | return 0; |
| 1903 | } |
| 1904 | intval = PyInt_AsLong(t); |
| 1905 | if (intval == -1 && PyErr_Occurred()) |
| 1906 | return -1; |
| 1907 | *sec = intval; |
| 1908 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 1909 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1910 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1911 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1912 | PyDoc_STRVAR(posix_utime__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1913 | "utime(path, (atime, utime))\n\ |
| 1914 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1915 | 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] | 1916 | 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] | 1917 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1918 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1919 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1920 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1921 | char *path; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1922 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1923 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1924 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1925 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1926 | #if defined(HAVE_UTIMES) |
| 1927 | struct timeval buf[2]; |
| 1928 | #define ATIME buf[0].tv_sec |
| 1929 | #define MTIME buf[1].tv_sec |
| 1930 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1931 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1932 | struct utimbuf buf; |
| 1933 | #define ATIME buf.actime |
| 1934 | #define MTIME buf.modtime |
| 1935 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1936 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1937 | time_t buf[2]; |
| 1938 | #define ATIME buf[0] |
| 1939 | #define MTIME buf[1] |
| 1940 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1941 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1942 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1943 | int have_unicode_filename = 0; |
| 1944 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1945 | PyUnicodeObject *obwpath; |
| 1946 | wchar_t *wpath; |
| 1947 | if (unicode_file_names()) { |
| 1948 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 1949 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 1950 | have_unicode_filename = 1; |
| 1951 | } else |
| 1952 | /* Drop the argument parsing error as narrow strings |
| 1953 | are also valid. */ |
| 1954 | PyErr_Clear(); |
| 1955 | } |
| 1956 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1957 | |
| 1958 | if (!have_unicode_filename && \ |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 1959 | !PyArg_ParseTuple(args, "etO:utime", |
| 1960 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1961 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1962 | if (arg == Py_None) { |
| 1963 | /* optional time values not given */ |
| 1964 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1965 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1966 | if (have_unicode_filename) |
| 1967 | res = _wutime(wpath, NULL); |
| 1968 | else |
| 1969 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1970 | res = utime(path, NULL); |
| 1971 | Py_END_ALLOW_THREADS |
| 1972 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1973 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1974 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1975 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1976 | return NULL; |
| 1977 | } |
| 1978 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1979 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 1980 | &atime, &ausec) == -1) |
| 1981 | return NULL; |
| 1982 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 1983 | &mtime, &musec) == -1) |
| 1984 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1985 | ATIME = atime; |
| 1986 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1987 | #ifdef HAVE_UTIMES |
| 1988 | buf[0].tv_usec = ausec; |
| 1989 | buf[1].tv_usec = musec; |
| 1990 | Py_BEGIN_ALLOW_THREADS |
| 1991 | res = utimes(path, buf); |
| 1992 | Py_END_ALLOW_THREADS |
| 1993 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1994 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1995 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1996 | if (have_unicode_filename) |
| 1997 | /* utime is OK with utimbuf, but _wutime insists |
| 1998 | on _utimbuf (the msvc headers assert the |
| 1999 | underscore version is ansi) */ |
| 2000 | res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG); |
| 2001 | else |
| 2002 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2003 | res = utime(path, UTIME_ARG); |
| 2004 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2005 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2006 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2007 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 2008 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2009 | Py_INCREF(Py_None); |
| 2010 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2011 | #undef UTIME_ARG |
| 2012 | #undef ATIME |
| 2013 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2016 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2017 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2018 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2019 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2020 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2021 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2022 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2023 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2024 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2025 | { |
| 2026 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2027 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2028 | return NULL; |
| 2029 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2030 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2033 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2034 | static void |
| 2035 | free_string_array(char **array, int count) |
| 2036 | { |
| 2037 | int i; |
| 2038 | for (i = 0; i < count; i++) |
| 2039 | PyMem_Free(array[i]); |
| 2040 | PyMem_DEL(array); |
| 2041 | } |
| 2042 | #endif |
| 2043 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2044 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2045 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2046 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2047 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2048 | Execute an executable path with arguments, replacing current process.\n\ |
| 2049 | \n\ |
| 2050 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2051 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2052 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2053 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2054 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2055 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2056 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2057 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2058 | char **argvlist; |
| 2059 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2060 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2061 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2062 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2063 | argv is a list or tuple of strings. */ |
| 2064 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2065 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2066 | Py_FileSystemDefaultEncoding, |
| 2067 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2068 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2069 | if (PyList_Check(argv)) { |
| 2070 | argc = PyList_Size(argv); |
| 2071 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2072 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2073 | else if (PyTuple_Check(argv)) { |
| 2074 | argc = PyTuple_Size(argv); |
| 2075 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2076 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2077 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2078 | 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] | 2079 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2080 | return NULL; |
| 2081 | } |
| 2082 | |
| 2083 | if (argc == 0) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2084 | 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] | 2085 | PyMem_Free(path); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2086 | return NULL; |
| 2087 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2088 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2089 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2090 | if (argvlist == NULL) { |
| 2091 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2092 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2093 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2094 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2095 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2096 | Py_FileSystemDefaultEncoding, |
| 2097 | &argvlist[i])) { |
| 2098 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2099 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2100 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2101 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2102 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2103 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2104 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2105 | } |
| 2106 | argvlist[argc] = NULL; |
| 2107 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2108 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2109 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2110 | /* If we get here it's definitely an error */ |
| 2111 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2112 | free_string_array(argvlist, argc); |
| 2113 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2114 | return posix_error(); |
| 2115 | } |
| 2116 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2117 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2118 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2119 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2120 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2121 | \n\ |
| 2122 | path: path of executable file\n\ |
| 2123 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2124 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2125 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2126 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2127 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2128 | { |
| 2129 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2130 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2131 | char **argvlist; |
| 2132 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2133 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2134 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2135 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2136 | int lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2137 | |
| 2138 | /* execve has three arguments: (path, argv, env), where |
| 2139 | argv is a list or tuple of strings and env is a dictionary |
| 2140 | like posix.environ. */ |
| 2141 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2142 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2143 | Py_FileSystemDefaultEncoding, |
| 2144 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2145 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2146 | if (PyList_Check(argv)) { |
| 2147 | argc = PyList_Size(argv); |
| 2148 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2149 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2150 | else if (PyTuple_Check(argv)) { |
| 2151 | argc = PyTuple_Size(argv); |
| 2152 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2153 | } |
| 2154 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2155 | PyErr_SetString(PyExc_TypeError, |
| 2156 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2157 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2158 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2159 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2160 | PyErr_SetString(PyExc_TypeError, |
| 2161 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2162 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2165 | if (argc == 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2166 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2167 | "execve() arg 2 must not be empty"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2168 | goto fail_0; |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2171 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2172 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2173 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2174 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2175 | } |
| 2176 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2177 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2178 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2179 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2180 | &argvlist[i])) |
| 2181 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2182 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2183 | goto fail_1; |
| 2184 | } |
| 2185 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2186 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2187 | argvlist[argc] = NULL; |
| 2188 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2189 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2190 | if (i < 0) |
| 2191 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2192 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2193 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2194 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2195 | goto fail_1; |
| 2196 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2197 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2198 | keys = PyMapping_Keys(env); |
| 2199 | vals = PyMapping_Values(env); |
| 2200 | if (!keys || !vals) |
| 2201 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2202 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2203 | PyErr_SetString(PyExc_TypeError, |
| 2204 | "execve(): env.keys() or env.values() is not a list"); |
| 2205 | goto fail_2; |
| 2206 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2207 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2208 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2209 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2210 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2211 | |
| 2212 | key = PyList_GetItem(keys, pos); |
| 2213 | val = PyList_GetItem(vals, pos); |
| 2214 | if (!key || !val) |
| 2215 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2216 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2217 | if (!PyArg_Parse( |
| 2218 | key, |
| 2219 | "s;execve() arg 3 contains a non-string key", |
| 2220 | &k) || |
| 2221 | !PyArg_Parse( |
| 2222 | val, |
| 2223 | "s;execve() arg 3 contains a non-string value", |
| 2224 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2225 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2226 | goto fail_2; |
| 2227 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2228 | |
| 2229 | #if defined(PYOS_OS2) |
| 2230 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2231 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2232 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2233 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2234 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2235 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2236 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2237 | goto fail_2; |
| 2238 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2239 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2240 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2241 | #if defined(PYOS_OS2) |
| 2242 | } |
| 2243 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2244 | } |
| 2245 | envlist[envc] = 0; |
| 2246 | |
| 2247 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2248 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2249 | /* If we get here it's definitely an error */ |
| 2250 | |
| 2251 | (void) posix_error(); |
| 2252 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2253 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2254 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2255 | PyMem_DEL(envlist[envc]); |
| 2256 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2257 | fail_1: |
| 2258 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2259 | Py_XDECREF(vals); |
| 2260 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2261 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2262 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2263 | return NULL; |
| 2264 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2265 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2266 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2267 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2268 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2269 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2270 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2271 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2272 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2273 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2274 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2275 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2276 | |
| 2277 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2278 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2279 | { |
| 2280 | char *path; |
| 2281 | PyObject *argv; |
| 2282 | char **argvlist; |
| 2283 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2284 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2285 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2286 | |
| 2287 | /* spawnv has three arguments: (mode, path, argv), where |
| 2288 | argv is a list or tuple of strings. */ |
| 2289 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2290 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2291 | Py_FileSystemDefaultEncoding, |
| 2292 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2293 | return NULL; |
| 2294 | if (PyList_Check(argv)) { |
| 2295 | argc = PyList_Size(argv); |
| 2296 | getitem = PyList_GetItem; |
| 2297 | } |
| 2298 | else if (PyTuple_Check(argv)) { |
| 2299 | argc = PyTuple_Size(argv); |
| 2300 | getitem = PyTuple_GetItem; |
| 2301 | } |
| 2302 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2303 | PyErr_SetString(PyExc_TypeError, |
| 2304 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2305 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2306 | return NULL; |
| 2307 | } |
| 2308 | |
| 2309 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2310 | if (argvlist == NULL) { |
| 2311 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2312 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2313 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2314 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2315 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2316 | Py_FileSystemDefaultEncoding, |
| 2317 | &argvlist[i])) { |
| 2318 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2319 | PyErr_SetString( |
| 2320 | PyExc_TypeError, |
| 2321 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2322 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2323 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2324 | } |
| 2325 | } |
| 2326 | argvlist[argc] = NULL; |
| 2327 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2328 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2329 | Py_BEGIN_ALLOW_THREADS |
| 2330 | spawnval = spawnv(mode, path, argvlist); |
| 2331 | Py_END_ALLOW_THREADS |
| 2332 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2333 | if (mode == _OLD_P_OVERLAY) |
| 2334 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2335 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2336 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2337 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2338 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2339 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2340 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2341 | free_string_array(argvlist, argc); |
| 2342 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2343 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2344 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2345 | return posix_error(); |
| 2346 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2347 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2348 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2349 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2350 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2351 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2355 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2356 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2357 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2358 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2359 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2360 | path: path of executable file\n\ |
| 2361 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2362 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2363 | |
| 2364 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2365 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2366 | { |
| 2367 | char *path; |
| 2368 | PyObject *argv, *env; |
| 2369 | char **argvlist; |
| 2370 | char **envlist; |
| 2371 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2372 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2373 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2374 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2375 | int lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2376 | |
| 2377 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 2378 | argv is a list or tuple of strings and env is a dictionary |
| 2379 | like posix.environ. */ |
| 2380 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2381 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 2382 | Py_FileSystemDefaultEncoding, |
| 2383 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2384 | return NULL; |
| 2385 | if (PyList_Check(argv)) { |
| 2386 | argc = PyList_Size(argv); |
| 2387 | getitem = PyList_GetItem; |
| 2388 | } |
| 2389 | else if (PyTuple_Check(argv)) { |
| 2390 | argc = PyTuple_Size(argv); |
| 2391 | getitem = PyTuple_GetItem; |
| 2392 | } |
| 2393 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2394 | PyErr_SetString(PyExc_TypeError, |
| 2395 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2396 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2397 | } |
| 2398 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2399 | PyErr_SetString(PyExc_TypeError, |
| 2400 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2401 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | argvlist = PyMem_NEW(char *, argc+1); |
| 2405 | if (argvlist == NULL) { |
| 2406 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2407 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2408 | } |
| 2409 | for (i = 0; i < argc; i++) { |
| 2410 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2411 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2412 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2413 | &argvlist[i])) |
| 2414 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2415 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2416 | goto fail_1; |
| 2417 | } |
| 2418 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2419 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2420 | argvlist[argc] = NULL; |
| 2421 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2422 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2423 | if (i < 0) |
| 2424 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2425 | envlist = PyMem_NEW(char *, i + 1); |
| 2426 | if (envlist == NULL) { |
| 2427 | PyErr_NoMemory(); |
| 2428 | goto fail_1; |
| 2429 | } |
| 2430 | envc = 0; |
| 2431 | keys = PyMapping_Keys(env); |
| 2432 | vals = PyMapping_Values(env); |
| 2433 | if (!keys || !vals) |
| 2434 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2435 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2436 | PyErr_SetString(PyExc_TypeError, |
| 2437 | "spawnve(): env.keys() or env.values() is not a list"); |
| 2438 | goto fail_2; |
| 2439 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2440 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2441 | for (pos = 0; pos < i; pos++) { |
| 2442 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2443 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2444 | |
| 2445 | key = PyList_GetItem(keys, pos); |
| 2446 | val = PyList_GetItem(vals, pos); |
| 2447 | if (!key || !val) |
| 2448 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2449 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2450 | if (!PyArg_Parse( |
| 2451 | key, |
| 2452 | "s;spawnve() arg 3 contains a non-string key", |
| 2453 | &k) || |
| 2454 | !PyArg_Parse( |
| 2455 | val, |
| 2456 | "s;spawnve() arg 3 contains a non-string value", |
| 2457 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2458 | { |
| 2459 | goto fail_2; |
| 2460 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2461 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2462 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2463 | if (p == NULL) { |
| 2464 | PyErr_NoMemory(); |
| 2465 | goto fail_2; |
| 2466 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2467 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2468 | envlist[envc++] = p; |
| 2469 | } |
| 2470 | envlist[envc] = 0; |
| 2471 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2472 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2473 | Py_BEGIN_ALLOW_THREADS |
| 2474 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2475 | Py_END_ALLOW_THREADS |
| 2476 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2477 | if (mode == _OLD_P_OVERLAY) |
| 2478 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2479 | |
| 2480 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2481 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2482 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2483 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2484 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2485 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2486 | (void) posix_error(); |
| 2487 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2488 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2489 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2490 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2491 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2492 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2493 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2494 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2495 | while (--envc >= 0) |
| 2496 | PyMem_DEL(envlist[envc]); |
| 2497 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2498 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2499 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2500 | Py_XDECREF(vals); |
| 2501 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2502 | fail_0: |
| 2503 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2504 | return res; |
| 2505 | } |
| 2506 | #endif /* HAVE_SPAWNV */ |
| 2507 | |
| 2508 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2509 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2510 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2511 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2512 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 2513 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2514 | 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] | 2515 | |
| 2516 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2517 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2518 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2519 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2520 | if (pid == -1) |
| 2521 | return posix_error(); |
| 2522 | PyOS_AfterFork(); |
| 2523 | return PyInt_FromLong((long)pid); |
| 2524 | } |
| 2525 | #endif |
| 2526 | |
| 2527 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2528 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2529 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2530 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2531 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2532 | 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] | 2533 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2534 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2535 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2536 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2537 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2538 | if (pid == -1) |
| 2539 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2540 | if (pid == 0) |
| 2541 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2542 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2543 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2544 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2545 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2546 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 2547 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 2548 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2549 | #define DEV_PTY_FILE "/dev/ptc" |
| 2550 | #define HAVE_DEV_PTMX |
| 2551 | #else |
| 2552 | #define DEV_PTY_FILE "/dev/ptmx" |
| 2553 | #endif |
| 2554 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2555 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2556 | #ifdef HAVE_PTY_H |
| 2557 | #include <pty.h> |
| 2558 | #else |
| 2559 | #ifdef HAVE_LIBUTIL_H |
| 2560 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2561 | #endif /* HAVE_LIBUTIL_H */ |
| 2562 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 2563 | #ifdef HAVE_STROPTS_H |
| 2564 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2565 | #endif |
| 2566 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2567 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2568 | #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] | 2569 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2570 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2571 | 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] | 2572 | |
| 2573 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2574 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2575 | { |
| 2576 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2577 | #ifndef HAVE_OPENPTY |
| 2578 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2579 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2580 | #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] | 2581 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2582 | #ifdef sun |
| 2583 | extern char *ptsname(); |
| 2584 | #endif |
| 2585 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2586 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2587 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2588 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 2589 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2590 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2591 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 2592 | if (slave_name == NULL) |
| 2593 | return posix_error(); |
| 2594 | |
| 2595 | slave_fd = open(slave_name, O_RDWR); |
| 2596 | if (slave_fd < 0) |
| 2597 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2598 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2599 | 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] | 2600 | if (master_fd < 0) |
| 2601 | return posix_error(); |
| 2602 | sig_saved = signal(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2603 | /* change permission of slave */ |
| 2604 | if (grantpt(master_fd) < 0) { |
| 2605 | signal(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2606 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2607 | } |
| 2608 | /* unlock slave */ |
| 2609 | if (unlockpt(master_fd) < 0) { |
| 2610 | signal(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2611 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2612 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2613 | signal(SIGCHLD, sig_saved); |
| 2614 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 2615 | if (slave_name == NULL) |
| 2616 | return posix_error(); |
| 2617 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 2618 | if (slave_fd < 0) |
| 2619 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2620 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2621 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 2622 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 2623 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2624 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 2625 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2626 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 2627 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2628 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2629 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2630 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2631 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2632 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2633 | |
| 2634 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2635 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2636 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2637 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 2638 | 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] | 2639 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2640 | |
| 2641 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2642 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2643 | { |
| 2644 | int master_fd, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2645 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2646 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 2647 | if (pid == -1) |
| 2648 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2649 | if (pid == 0) |
| 2650 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2651 | return Py_BuildValue("(ii)", pid, master_fd); |
| 2652 | } |
| 2653 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2654 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2655 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2656 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2657 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2658 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2659 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2660 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2661 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2662 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2663 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2664 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2665 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2666 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2667 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2668 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2669 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2670 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2671 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2672 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2673 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2674 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2675 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2676 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2677 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2678 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2679 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2680 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2681 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2682 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2683 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2684 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2685 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2686 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2687 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2688 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2689 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2690 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2691 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2692 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2693 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2694 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2695 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2696 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2697 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2698 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2699 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2700 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2701 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2704 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2705 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2706 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2707 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2708 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2709 | |
| 2710 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2711 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2712 | { |
| 2713 | PyObject *result = NULL; |
| 2714 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2715 | #ifdef NGROUPS_MAX |
| 2716 | #define MAX_GROUPS NGROUPS_MAX |
| 2717 | #else |
| 2718 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 2719 | #define MAX_GROUPS 64 |
| 2720 | #endif |
| 2721 | gid_t grouplist[MAX_GROUPS]; |
| 2722 | int n; |
| 2723 | |
| 2724 | n = getgroups(MAX_GROUPS, grouplist); |
| 2725 | if (n < 0) |
| 2726 | posix_error(); |
| 2727 | else { |
| 2728 | result = PyList_New(n); |
| 2729 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2730 | int i; |
| 2731 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2732 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2733 | if (o == NULL) { |
| 2734 | Py_DECREF(result); |
| 2735 | result = NULL; |
| 2736 | break; |
| 2737 | } |
| 2738 | PyList_SET_ITEM(result, i, o); |
| 2739 | } |
| 2740 | } |
| 2741 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2742 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2743 | return result; |
| 2744 | } |
| 2745 | #endif |
| 2746 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 2747 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 2748 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2749 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 2750 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 2751 | |
| 2752 | static PyObject * |
| 2753 | posix_getpgid(PyObject *self, PyObject *args) |
| 2754 | { |
| 2755 | int pid, pgid; |
| 2756 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 2757 | return NULL; |
| 2758 | pgid = getpgid(pid); |
| 2759 | if (pgid < 0) |
| 2760 | return posix_error(); |
| 2761 | return PyInt_FromLong((long)pgid); |
| 2762 | } |
| 2763 | #endif /* HAVE_GETPGID */ |
| 2764 | |
| 2765 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2766 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2767 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2768 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2769 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2770 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2771 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2772 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2773 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2774 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2775 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2776 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2777 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2778 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2779 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2780 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2781 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2782 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2783 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2784 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2785 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2786 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2787 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2788 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2789 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2790 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2791 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2792 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2793 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2794 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2795 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2796 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2797 | Py_INCREF(Py_None); |
| 2798 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2801 | #endif /* HAVE_SETPGRP */ |
| 2802 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2803 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2804 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2805 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2806 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2807 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2808 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2809 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2810 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2811 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2812 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2813 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2814 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2815 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2816 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2817 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2818 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2819 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2820 | |
| 2821 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2822 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2823 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2824 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2825 | char *name; |
| 2826 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2827 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2828 | errno = 0; |
| 2829 | name = getlogin(); |
| 2830 | if (name == NULL) { |
| 2831 | if (errno) |
| 2832 | posix_error(); |
| 2833 | else |
| 2834 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 2835 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2836 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2837 | else |
| 2838 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2839 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2840 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2841 | return result; |
| 2842 | } |
| 2843 | #endif |
| 2844 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2845 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2846 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2847 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2848 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2849 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2850 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2851 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2852 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2853 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2854 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2855 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2856 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2857 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2858 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2859 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2860 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2861 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2862 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2863 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2864 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2865 | { |
| 2866 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2867 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2868 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2869 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2870 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 2871 | APIRET rc; |
| 2872 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2873 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2874 | |
| 2875 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 2876 | APIRET rc; |
| 2877 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2878 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2879 | |
| 2880 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2881 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2882 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2883 | if (kill(pid, sig) == -1) |
| 2884 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2885 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2886 | Py_INCREF(Py_None); |
| 2887 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2888 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2889 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2890 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 2891 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2892 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2893 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2894 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 2895 | |
| 2896 | static PyObject * |
| 2897 | posix_killpg(PyObject *self, PyObject *args) |
| 2898 | { |
| 2899 | int pgid, sig; |
| 2900 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 2901 | return NULL; |
| 2902 | if (killpg(pgid, sig) == -1) |
| 2903 | return posix_error(); |
| 2904 | Py_INCREF(Py_None); |
| 2905 | return Py_None; |
| 2906 | } |
| 2907 | #endif |
| 2908 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2909 | #ifdef HAVE_PLOCK |
| 2910 | |
| 2911 | #ifdef HAVE_SYS_LOCK_H |
| 2912 | #include <sys/lock.h> |
| 2913 | #endif |
| 2914 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2915 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2916 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2917 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2918 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2919 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2920 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2921 | { |
| 2922 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2923 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2924 | return NULL; |
| 2925 | if (plock(op) == -1) |
| 2926 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2927 | Py_INCREF(Py_None); |
| 2928 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2929 | } |
| 2930 | #endif |
| 2931 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2932 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2933 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2934 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2935 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2936 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2937 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2938 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2939 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2940 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2941 | async_system(const char *command) |
| 2942 | { |
| 2943 | char *p, errormsg[256], args[1024]; |
| 2944 | RESULTCODES rcodes; |
| 2945 | APIRET rc; |
| 2946 | char *shell = getenv("COMSPEC"); |
| 2947 | if (!shell) |
| 2948 | shell = "cmd"; |
| 2949 | |
| 2950 | strcpy(args, shell); |
| 2951 | p = &args[ strlen(args)+1 ]; |
| 2952 | strcpy(p, "/c "); |
| 2953 | strcat(p, command); |
| 2954 | p += strlen(p) + 1; |
| 2955 | *p = '\0'; |
| 2956 | |
| 2957 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2958 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2959 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2960 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2961 | &rcodes, shell); |
| 2962 | return rc; |
| 2963 | } |
| 2964 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2965 | static FILE * |
| 2966 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2967 | { |
| 2968 | HFILE rhan, whan; |
| 2969 | FILE *retfd = NULL; |
| 2970 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 2971 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2972 | if (rc != NO_ERROR) { |
| 2973 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2974 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2975 | } |
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 (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 2978 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2979 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2980 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2981 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2982 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2983 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 2984 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
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 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2989 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 2990 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2991 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2992 | if (rc == NO_ERROR) |
| 2993 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
| 2994 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2995 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 2996 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2997 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2998 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 2999 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3000 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3001 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 3002 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3003 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3004 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 3005 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3006 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3007 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3010 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 3011 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3012 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3013 | if (rc == NO_ERROR) |
| 3014 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
| 3015 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3016 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 3017 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3018 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3019 | } else { |
| 3020 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3021 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3022 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3026 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3027 | { |
| 3028 | char *name; |
| 3029 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3030 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3031 | FILE *fp; |
| 3032 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3033 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3034 | return NULL; |
| 3035 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3036 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3037 | Py_END_ALLOW_THREADS |
| 3038 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3039 | return os2_error(err); |
| 3040 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3041 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 3042 | if (f != NULL) |
| 3043 | PyFile_SetBufSize(f, bufsize); |
| 3044 | return f; |
| 3045 | } |
| 3046 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3047 | #elif defined(PYCC_GCC) |
| 3048 | |
| 3049 | /* standard posix version of popen() support */ |
| 3050 | static PyObject * |
| 3051 | posix_popen(PyObject *self, PyObject *args) |
| 3052 | { |
| 3053 | char *name; |
| 3054 | char *mode = "r"; |
| 3055 | int bufsize = -1; |
| 3056 | FILE *fp; |
| 3057 | PyObject *f; |
| 3058 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 3059 | return NULL; |
| 3060 | Py_BEGIN_ALLOW_THREADS |
| 3061 | fp = popen(name, mode); |
| 3062 | Py_END_ALLOW_THREADS |
| 3063 | if (fp == NULL) |
| 3064 | return posix_error(); |
| 3065 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 3066 | if (f != NULL) |
| 3067 | PyFile_SetBufSize(f, bufsize); |
| 3068 | return f; |
| 3069 | } |
| 3070 | |
| 3071 | /* fork() under OS/2 has lots'o'warts |
| 3072 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 3073 | * most of this code is a ripoff of the win32 code, but using the |
| 3074 | * capabilities of EMX's C library routines |
| 3075 | */ |
| 3076 | |
| 3077 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 3078 | #define POPEN_1 1 |
| 3079 | #define POPEN_2 2 |
| 3080 | #define POPEN_3 3 |
| 3081 | #define POPEN_4 4 |
| 3082 | |
| 3083 | static PyObject *_PyPopen(char *, int, int, int); |
| 3084 | static int _PyPclose(FILE *file); |
| 3085 | |
| 3086 | /* |
| 3087 | * Internal dictionary mapping popen* file pointers to process handles, |
| 3088 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3089 | * for more information on this dictionary's use. |
| 3090 | */ |
| 3091 | static PyObject *_PyPopenProcs = NULL; |
| 3092 | |
| 3093 | /* os2emx version of popen2() |
| 3094 | * |
| 3095 | * The result of this function is a pipe (file) connected to the |
| 3096 | * process's stdin, and a pipe connected to the process's stdout. |
| 3097 | */ |
| 3098 | |
| 3099 | static PyObject * |
| 3100 | os2emx_popen2(PyObject *self, PyObject *args) |
| 3101 | { |
| 3102 | PyObject *f; |
| 3103 | int tm=0; |
| 3104 | |
| 3105 | char *cmdstring; |
| 3106 | char *mode = "t"; |
| 3107 | int bufsize = -1; |
| 3108 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 3109 | return NULL; |
| 3110 | |
| 3111 | if (*mode == 't') |
| 3112 | tm = O_TEXT; |
| 3113 | else if (*mode != 'b') { |
| 3114 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3115 | return NULL; |
| 3116 | } else |
| 3117 | tm = O_BINARY; |
| 3118 | |
| 3119 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 3120 | |
| 3121 | return f; |
| 3122 | } |
| 3123 | |
| 3124 | /* |
| 3125 | * Variation on os2emx.popen2 |
| 3126 | * |
| 3127 | * The result of this function is 3 pipes - the process's stdin, |
| 3128 | * stdout and stderr |
| 3129 | */ |
| 3130 | |
| 3131 | static PyObject * |
| 3132 | os2emx_popen3(PyObject *self, PyObject *args) |
| 3133 | { |
| 3134 | PyObject *f; |
| 3135 | int tm = 0; |
| 3136 | |
| 3137 | char *cmdstring; |
| 3138 | char *mode = "t"; |
| 3139 | int bufsize = -1; |
| 3140 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 3141 | return NULL; |
| 3142 | |
| 3143 | if (*mode == 't') |
| 3144 | tm = O_TEXT; |
| 3145 | else if (*mode != 'b') { |
| 3146 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3147 | return NULL; |
| 3148 | } else |
| 3149 | tm = O_BINARY; |
| 3150 | |
| 3151 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 3152 | |
| 3153 | return f; |
| 3154 | } |
| 3155 | |
| 3156 | /* |
| 3157 | * Variation on os2emx.popen2 |
| 3158 | * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3159 | * The result of this function is 2 pipes - the processes stdin, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3160 | * and stdout+stderr combined as a single pipe. |
| 3161 | */ |
| 3162 | |
| 3163 | static PyObject * |
| 3164 | os2emx_popen4(PyObject *self, PyObject *args) |
| 3165 | { |
| 3166 | PyObject *f; |
| 3167 | int tm = 0; |
| 3168 | |
| 3169 | char *cmdstring; |
| 3170 | char *mode = "t"; |
| 3171 | int bufsize = -1; |
| 3172 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 3173 | return NULL; |
| 3174 | |
| 3175 | if (*mode == 't') |
| 3176 | tm = O_TEXT; |
| 3177 | else if (*mode != 'b') { |
| 3178 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3179 | return NULL; |
| 3180 | } else |
| 3181 | tm = O_BINARY; |
| 3182 | |
| 3183 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 3184 | |
| 3185 | return f; |
| 3186 | } |
| 3187 | |
| 3188 | /* a couple of structures for convenient handling of multiple |
| 3189 | * file handles and pipes |
| 3190 | */ |
| 3191 | struct file_ref |
| 3192 | { |
| 3193 | int handle; |
| 3194 | int flags; |
| 3195 | }; |
| 3196 | |
| 3197 | struct pipe_ref |
| 3198 | { |
| 3199 | int rd; |
| 3200 | int wr; |
| 3201 | }; |
| 3202 | |
| 3203 | /* The following code is derived from the win32 code */ |
| 3204 | |
| 3205 | static PyObject * |
| 3206 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 3207 | { |
| 3208 | struct file_ref stdio[3]; |
| 3209 | struct pipe_ref p_fd[3]; |
| 3210 | FILE *p_s[3]; |
| 3211 | int file_count, i, pipe_err, pipe_pid; |
| 3212 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 3213 | PyObject *f, *p_f[3]; |
| 3214 | |
| 3215 | /* file modes for subsequent fdopen's on pipe handles */ |
| 3216 | if (mode == O_TEXT) |
| 3217 | { |
| 3218 | rd_mode = "rt"; |
| 3219 | wr_mode = "wt"; |
| 3220 | } |
| 3221 | else |
| 3222 | { |
| 3223 | rd_mode = "rb"; |
| 3224 | wr_mode = "wb"; |
| 3225 | } |
| 3226 | |
| 3227 | /* prepare shell references */ |
| 3228 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 3229 | if ((shell = getenv("COMSPEC")) == NULL) |
| 3230 | { |
| 3231 | errno = ENOENT; |
| 3232 | return posix_error(); |
| 3233 | } |
| 3234 | |
| 3235 | sh_name = _getname(shell); |
| 3236 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 3237 | opt = "/c"; |
| 3238 | else |
| 3239 | opt = "-c"; |
| 3240 | |
| 3241 | /* save current stdio fds + their flags, and set not inheritable */ |
| 3242 | i = pipe_err = 0; |
| 3243 | while (pipe_err >= 0 && i < 3) |
| 3244 | { |
| 3245 | pipe_err = stdio[i].handle = dup(i); |
| 3246 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 3247 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 3248 | i++; |
| 3249 | } |
| 3250 | if (pipe_err < 0) |
| 3251 | { |
| 3252 | /* didn't get them all saved - clean up and bail out */ |
| 3253 | int saved_err = errno; |
| 3254 | while (i-- > 0) |
| 3255 | { |
| 3256 | close(stdio[i].handle); |
| 3257 | } |
| 3258 | errno = saved_err; |
| 3259 | return posix_error(); |
| 3260 | } |
| 3261 | |
| 3262 | /* create pipe ends */ |
| 3263 | file_count = 2; |
| 3264 | if (n == POPEN_3) |
| 3265 | file_count = 3; |
| 3266 | i = pipe_err = 0; |
| 3267 | while ((pipe_err == 0) && (i < file_count)) |
| 3268 | pipe_err = pipe((int *)&p_fd[i++]); |
| 3269 | if (pipe_err < 0) |
| 3270 | { |
| 3271 | /* didn't get them all made - clean up and bail out */ |
| 3272 | while (i-- > 0) |
| 3273 | { |
| 3274 | close(p_fd[i].wr); |
| 3275 | close(p_fd[i].rd); |
| 3276 | } |
| 3277 | errno = EPIPE; |
| 3278 | return posix_error(); |
| 3279 | } |
| 3280 | |
| 3281 | /* change the actual standard IO streams over temporarily, |
| 3282 | * making the retained pipe ends non-inheritable |
| 3283 | */ |
| 3284 | pipe_err = 0; |
| 3285 | |
| 3286 | /* - stdin */ |
| 3287 | if (dup2(p_fd[0].rd, 0) == 0) |
| 3288 | { |
| 3289 | close(p_fd[0].rd); |
| 3290 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 3291 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 3292 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 3293 | { |
| 3294 | close(p_fd[0].wr); |
| 3295 | pipe_err = -1; |
| 3296 | } |
| 3297 | } |
| 3298 | else |
| 3299 | { |
| 3300 | pipe_err = -1; |
| 3301 | } |
| 3302 | |
| 3303 | /* - stdout */ |
| 3304 | if (pipe_err == 0) |
| 3305 | { |
| 3306 | if (dup2(p_fd[1].wr, 1) == 1) |
| 3307 | { |
| 3308 | close(p_fd[1].wr); |
| 3309 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 3310 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 3311 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 3312 | { |
| 3313 | close(p_fd[1].rd); |
| 3314 | pipe_err = -1; |
| 3315 | } |
| 3316 | } |
| 3317 | else |
| 3318 | { |
| 3319 | pipe_err = -1; |
| 3320 | } |
| 3321 | } |
| 3322 | |
| 3323 | /* - stderr, as required */ |
| 3324 | if (pipe_err == 0) |
| 3325 | switch (n) |
| 3326 | { |
| 3327 | case POPEN_3: |
| 3328 | { |
| 3329 | if (dup2(p_fd[2].wr, 2) == 2) |
| 3330 | { |
| 3331 | close(p_fd[2].wr); |
| 3332 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 3333 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 3334 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 3335 | { |
| 3336 | close(p_fd[2].rd); |
| 3337 | pipe_err = -1; |
| 3338 | } |
| 3339 | } |
| 3340 | else |
| 3341 | { |
| 3342 | pipe_err = -1; |
| 3343 | } |
| 3344 | break; |
| 3345 | } |
| 3346 | |
| 3347 | case POPEN_4: |
| 3348 | { |
| 3349 | if (dup2(1, 2) != 2) |
| 3350 | { |
| 3351 | pipe_err = -1; |
| 3352 | } |
| 3353 | break; |
| 3354 | } |
| 3355 | } |
| 3356 | |
| 3357 | /* spawn the child process */ |
| 3358 | if (pipe_err == 0) |
| 3359 | { |
| 3360 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 3361 | if (pipe_pid == -1) |
| 3362 | { |
| 3363 | pipe_err = -1; |
| 3364 | } |
| 3365 | else |
| 3366 | { |
| 3367 | /* save the PID into the FILE structure |
| 3368 | * NOTE: this implementation doesn't actually |
| 3369 | * take advantage of this, but do it for |
| 3370 | * completeness - AIM Apr01 |
| 3371 | */ |
| 3372 | for (i = 0; i < file_count; i++) |
| 3373 | p_s[i]->_pid = pipe_pid; |
| 3374 | } |
| 3375 | } |
| 3376 | |
| 3377 | /* reset standard IO to normal */ |
| 3378 | for (i = 0; i < 3; i++) |
| 3379 | { |
| 3380 | dup2(stdio[i].handle, i); |
| 3381 | fcntl(i, F_SETFD, stdio[i].flags); |
| 3382 | close(stdio[i].handle); |
| 3383 | } |
| 3384 | |
| 3385 | /* if any remnant problems, clean up and bail out */ |
| 3386 | if (pipe_err < 0) |
| 3387 | { |
| 3388 | for (i = 0; i < 3; i++) |
| 3389 | { |
| 3390 | close(p_fd[i].rd); |
| 3391 | close(p_fd[i].wr); |
| 3392 | } |
| 3393 | errno = EPIPE; |
| 3394 | return posix_error_with_filename(cmdstring); |
| 3395 | } |
| 3396 | |
| 3397 | /* build tuple of file objects to return */ |
| 3398 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 3399 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3400 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3401 | PyFile_SetBufSize(p_f[1], bufsize); |
| 3402 | if (n == POPEN_3) |
| 3403 | { |
| 3404 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3405 | PyFile_SetBufSize(p_f[0], bufsize); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3406 | f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3407 | } |
| 3408 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3409 | f = PyTuple_Pack(2, p_f[0], p_f[1]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3410 | |
| 3411 | /* |
| 3412 | * Insert the files we've created into the process dictionary |
| 3413 | * all referencing the list with the process handle and the |
| 3414 | * initial number of files (see description below in _PyPclose). |
| 3415 | * Since if _PyPclose later tried to wait on a process when all |
| 3416 | * handles weren't closed, it could create a deadlock with the |
| 3417 | * child, we spend some energy here to try to ensure that we |
| 3418 | * either insert all file handles into the dictionary or none |
| 3419 | * at all. It's a little clumsy with the various popen modes |
| 3420 | * and variable number of files involved. |
| 3421 | */ |
| 3422 | if (!_PyPopenProcs) |
| 3423 | { |
| 3424 | _PyPopenProcs = PyDict_New(); |
| 3425 | } |
| 3426 | |
| 3427 | if (_PyPopenProcs) |
| 3428 | { |
| 3429 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 3430 | int ins_rc[3]; |
| 3431 | |
| 3432 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 3433 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 3434 | |
| 3435 | procObj = PyList_New(2); |
| 3436 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 3437 | intObj = PyInt_FromLong((long) file_count); |
| 3438 | |
| 3439 | if (procObj && pidObj && intObj) |
| 3440 | { |
| 3441 | PyList_SetItem(procObj, 0, pidObj); |
| 3442 | PyList_SetItem(procObj, 1, intObj); |
| 3443 | |
| 3444 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 3445 | if (fileObj[0]) |
| 3446 | { |
| 3447 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 3448 | fileObj[0], |
| 3449 | procObj); |
| 3450 | } |
| 3451 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 3452 | if (fileObj[1]) |
| 3453 | { |
| 3454 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 3455 | fileObj[1], |
| 3456 | procObj); |
| 3457 | } |
| 3458 | if (file_count >= 3) |
| 3459 | { |
| 3460 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 3461 | if (fileObj[2]) |
| 3462 | { |
| 3463 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 3464 | fileObj[2], |
| 3465 | procObj); |
| 3466 | } |
| 3467 | } |
| 3468 | |
| 3469 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 3470 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 3471 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 3472 | { |
| 3473 | /* Something failed - remove any dictionary |
| 3474 | * entries that did make it. |
| 3475 | */ |
| 3476 | if (!ins_rc[0] && fileObj[0]) |
| 3477 | { |
| 3478 | PyDict_DelItem(_PyPopenProcs, |
| 3479 | fileObj[0]); |
| 3480 | } |
| 3481 | if (!ins_rc[1] && fileObj[1]) |
| 3482 | { |
| 3483 | PyDict_DelItem(_PyPopenProcs, |
| 3484 | fileObj[1]); |
| 3485 | } |
| 3486 | if (!ins_rc[2] && fileObj[2]) |
| 3487 | { |
| 3488 | PyDict_DelItem(_PyPopenProcs, |
| 3489 | fileObj[2]); |
| 3490 | } |
| 3491 | } |
| 3492 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3493 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3494 | /* |
| 3495 | * Clean up our localized references for the dictionary keys |
| 3496 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 3497 | * that got placed in the dictionary. |
| 3498 | */ |
| 3499 | Py_XDECREF(procObj); |
| 3500 | Py_XDECREF(fileObj[0]); |
| 3501 | Py_XDECREF(fileObj[1]); |
| 3502 | Py_XDECREF(fileObj[2]); |
| 3503 | } |
| 3504 | |
| 3505 | /* Child is launched. */ |
| 3506 | return f; |
| 3507 | } |
| 3508 | |
| 3509 | /* |
| 3510 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 3511 | * exit code for the child process and return as a result of the close. |
| 3512 | * |
| 3513 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 3514 | * input file pointer to information about the process that was |
| 3515 | * originally created by the popen* call that created the file pointer. |
| 3516 | * The dictionary uses the file pointer as a key (with one entry |
| 3517 | * inserted for each file returned by the original popen* call) and a |
| 3518 | * single list object as the value for all files from a single call. |
| 3519 | * The list object contains the Win32 process handle at [0], and a file |
| 3520 | * count at [1], which is initialized to the total number of file |
| 3521 | * handles using that list. |
| 3522 | * |
| 3523 | * This function closes whichever handle it is passed, and decrements |
| 3524 | * the file count in the dictionary for the process handle pointed to |
| 3525 | * by this file. On the last close (when the file count reaches zero), |
| 3526 | * this function will wait for the child process and then return its |
| 3527 | * exit code as the result of the close() operation. This permits the |
| 3528 | * files to be closed in any order - it is always the close() of the |
| 3529 | * final handle that will return the exit code. |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3530 | * |
| 3531 | * NOTE: This function is currently called with the GIL released. |
| 3532 | * hence we use the GILState API to manage our state. |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3533 | */ |
| 3534 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3535 | static int _PyPclose(FILE *file) |
| 3536 | { |
| 3537 | int result; |
| 3538 | int exit_code; |
| 3539 | int pipe_pid; |
| 3540 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 3541 | int file_count; |
| 3542 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3543 | PyGILState_STATE state; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3544 | #endif |
| 3545 | |
| 3546 | /* Close the file handle first, to ensure it can't block the |
| 3547 | * child from exiting if it's the last handle. |
| 3548 | */ |
| 3549 | result = fclose(file); |
| 3550 | |
| 3551 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3552 | state = PyGILState_Ensure(); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3553 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3554 | if (_PyPopenProcs) |
| 3555 | { |
| 3556 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 3557 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 3558 | fileObj)) != NULL && |
| 3559 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 3560 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 3561 | { |
| 3562 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 3563 | file_count = (int) PyInt_AsLong(intObj); |
| 3564 | |
| 3565 | if (file_count > 1) |
| 3566 | { |
| 3567 | /* Still other files referencing process */ |
| 3568 | file_count--; |
| 3569 | PyList_SetItem(procObj,1, |
| 3570 | PyInt_FromLong((long) file_count)); |
| 3571 | } |
| 3572 | else |
| 3573 | { |
| 3574 | /* Last file for this process */ |
| 3575 | if (result != EOF && |
| 3576 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 3577 | { |
| 3578 | /* extract exit status */ |
| 3579 | if (WIFEXITED(exit_code)) |
| 3580 | { |
| 3581 | result = WEXITSTATUS(exit_code); |
| 3582 | } |
| 3583 | else |
| 3584 | { |
| 3585 | errno = EPIPE; |
| 3586 | result = -1; |
| 3587 | } |
| 3588 | } |
| 3589 | else |
| 3590 | { |
| 3591 | /* Indicate failure - this will cause the file object |
| 3592 | * to raise an I/O error and translate the last |
| 3593 | * error code from errno. We do have a problem with |
| 3594 | * last errors that overlap the normal errno table, |
| 3595 | * but that's a consistent problem with the file object. |
| 3596 | */ |
| 3597 | result = -1; |
| 3598 | } |
| 3599 | } |
| 3600 | |
| 3601 | /* Remove this file pointer from dictionary */ |
| 3602 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 3603 | |
| 3604 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 3605 | { |
| 3606 | Py_DECREF(_PyPopenProcs); |
| 3607 | _PyPopenProcs = NULL; |
| 3608 | } |
| 3609 | |
| 3610 | } /* if object retrieval ok */ |
| 3611 | |
| 3612 | Py_XDECREF(fileObj); |
| 3613 | } /* if _PyPopenProcs */ |
| 3614 | |
| 3615 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3616 | PyGILState_Release(state); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3617 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3618 | return result; |
| 3619 | } |
| 3620 | |
| 3621 | #endif /* PYCC_??? */ |
| 3622 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3623 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3624 | |
| 3625 | /* |
| 3626 | * Portable 'popen' replacement for Win32. |
| 3627 | * |
| 3628 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 3629 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3630 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3631 | */ |
| 3632 | |
| 3633 | #include <malloc.h> |
| 3634 | #include <io.h> |
| 3635 | #include <fcntl.h> |
| 3636 | |
| 3637 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 3638 | #define POPEN_1 1 |
| 3639 | #define POPEN_2 2 |
| 3640 | #define POPEN_3 3 |
| 3641 | #define POPEN_4 4 |
| 3642 | |
| 3643 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3644 | static int _PyPclose(FILE *file); |
| 3645 | |
| 3646 | /* |
| 3647 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3648 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3649 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3650 | */ |
| 3651 | static PyObject *_PyPopenProcs = NULL; |
| 3652 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3653 | |
| 3654 | /* popen that works from a GUI. |
| 3655 | * |
| 3656 | * The result of this function is a pipe (file) connected to the |
| 3657 | * processes stdin or stdout, depending on the requested mode. |
| 3658 | */ |
| 3659 | |
| 3660 | static PyObject * |
| 3661 | posix_popen(PyObject *self, PyObject *args) |
| 3662 | { |
Raymond Hettinger | b5cb665 | 2003-09-01 22:25:41 +0000 | [diff] [blame] | 3663 | PyObject *f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3664 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3665 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3666 | char *cmdstring; |
| 3667 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3668 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3669 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3670 | return NULL; |
| 3671 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3672 | if (*mode == 'r') |
| 3673 | tm = _O_RDONLY; |
| 3674 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3675 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3676 | return NULL; |
| 3677 | } else |
| 3678 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3679 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3680 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3681 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3682 | return NULL; |
| 3683 | } |
| 3684 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3685 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3686 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3687 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3688 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3689 | else |
| 3690 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 3691 | |
| 3692 | return f; |
| 3693 | } |
| 3694 | |
| 3695 | /* Variation on win32pipe.popen |
| 3696 | * |
| 3697 | * The result of this function is a pipe (file) connected to the |
| 3698 | * process's stdin, and a pipe connected to the process's stdout. |
| 3699 | */ |
| 3700 | |
| 3701 | static PyObject * |
| 3702 | win32_popen2(PyObject *self, PyObject *args) |
| 3703 | { |
| 3704 | PyObject *f; |
| 3705 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3706 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3707 | char *cmdstring; |
| 3708 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3709 | int bufsize = -1; |
| 3710 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3711 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3712 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3713 | if (*mode == 't') |
| 3714 | tm = _O_TEXT; |
| 3715 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3716 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3717 | return NULL; |
| 3718 | } else |
| 3719 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3720 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3721 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3722 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3723 | return NULL; |
| 3724 | } |
| 3725 | |
| 3726 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3727 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3728 | return f; |
| 3729 | } |
| 3730 | |
| 3731 | /* |
| 3732 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3733 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3734 | * The result of this function is 3 pipes - the process's stdin, |
| 3735 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3736 | */ |
| 3737 | |
| 3738 | static PyObject * |
| 3739 | win32_popen3(PyObject *self, PyObject *args) |
| 3740 | { |
| 3741 | PyObject *f; |
| 3742 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3743 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3744 | char *cmdstring; |
| 3745 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3746 | int bufsize = -1; |
| 3747 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3748 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3749 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3750 | if (*mode == 't') |
| 3751 | tm = _O_TEXT; |
| 3752 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3753 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3754 | return NULL; |
| 3755 | } else |
| 3756 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3757 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3758 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3759 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3760 | return NULL; |
| 3761 | } |
| 3762 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3763 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3764 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3765 | return f; |
| 3766 | } |
| 3767 | |
| 3768 | /* |
| 3769 | * Variation on win32pipe.popen |
| 3770 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3771 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3772 | * and stdout+stderr combined as a single pipe. |
| 3773 | */ |
| 3774 | |
| 3775 | static PyObject * |
| 3776 | win32_popen4(PyObject *self, PyObject *args) |
| 3777 | { |
| 3778 | PyObject *f; |
| 3779 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3780 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3781 | char *cmdstring; |
| 3782 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3783 | int bufsize = -1; |
| 3784 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3785 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3786 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3787 | if (*mode == 't') |
| 3788 | tm = _O_TEXT; |
| 3789 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3790 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3791 | return NULL; |
| 3792 | } else |
| 3793 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3794 | |
| 3795 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3796 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3797 | return NULL; |
| 3798 | } |
| 3799 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3800 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3801 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3802 | return f; |
| 3803 | } |
| 3804 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3805 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3806 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3807 | HANDLE hStdin, |
| 3808 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3809 | HANDLE hStderr, |
| 3810 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3811 | { |
| 3812 | PROCESS_INFORMATION piProcInfo; |
| 3813 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3814 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3815 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3816 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3817 | int i; |
| 3818 | int x; |
| 3819 | |
| 3820 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3821 | char *comshell; |
| 3822 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 3823 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3824 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 3825 | return x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3826 | |
| 3827 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 3828 | * then use the w9xpopen hack. |
| 3829 | */ |
| 3830 | comshell = s1 + x; |
| 3831 | while (comshell >= s1 && *comshell != '\\') |
| 3832 | --comshell; |
| 3833 | ++comshell; |
| 3834 | |
| 3835 | if (GetVersion() < 0x80000000 && |
| 3836 | _stricmp(comshell, "command.com") != 0) { |
| 3837 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3838 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 3839 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3840 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 3841 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3842 | } |
| 3843 | else { |
| 3844 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3845 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 3846 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3847 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3848 | char modulepath[_MAX_PATH]; |
| 3849 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3850 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 3851 | for (i = x = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3852 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3853 | x = i+1; |
| 3854 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3855 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3856 | strncat(modulepath, |
| 3857 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3858 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 3859 | -strlen(modulepath)); |
| 3860 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3861 | /* Eeek - file-not-found - possibly an embedding |
| 3862 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3863 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3864 | strncpy(modulepath, |
| 3865 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3866 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 3867 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 3868 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3869 | strncat(modulepath, |
| 3870 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3871 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 3872 | -strlen(modulepath)); |
| 3873 | /* No where else to look - raise an easily identifiable |
| 3874 | error, rather than leaving Windows to report |
| 3875 | "file not found" - as the user is probably blissfully |
| 3876 | unaware this shim EXE is used, and it will confuse them. |
| 3877 | (well, it confused me for a while ;-) |
| 3878 | */ |
| 3879 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3880 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3881 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3882 | "for popen to work with your shell " |
| 3883 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3884 | szConsoleSpawn); |
| 3885 | return FALSE; |
| 3886 | } |
| 3887 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3888 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3889 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3890 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3891 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 3892 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3893 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 3894 | /* To maintain correct argument passing semantics, |
| 3895 | we pass the command-line as it stands, and allow |
| 3896 | quoting to be applied. w9xpopen.exe will then |
| 3897 | use its argv vector, and re-quote the necessary |
| 3898 | args for the ultimate child process. |
| 3899 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 3900 | PyOS_snprintf( |
| 3901 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 3902 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3903 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3904 | s1, |
| 3905 | s3, |
| 3906 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3907 | /* Not passing CREATE_NEW_CONSOLE has been known to |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3908 | cause random failures on win9x. Specifically a |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3909 | dialog: |
| 3910 | "Your program accessed mem currently in use at xxx" |
| 3911 | and a hopeful warning about the stability of your |
| 3912 | system. |
| 3913 | Cost is Ctrl+C wont kill children, but anyone |
| 3914 | who cares can have a go! |
| 3915 | */ |
| 3916 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3917 | } |
| 3918 | } |
| 3919 | |
| 3920 | /* Could be an else here to try cmd.exe / command.com in the path |
| 3921 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3922 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3923 | PyErr_SetString(PyExc_RuntimeError, |
| 3924 | "Cannot locate a COMSPEC environment variable to " |
| 3925 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3926 | return FALSE; |
| 3927 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3928 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3929 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 3930 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 3931 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 3932 | siStartInfo.hStdInput = hStdin; |
| 3933 | siStartInfo.hStdOutput = hStdout; |
| 3934 | siStartInfo.hStdError = hStderr; |
| 3935 | siStartInfo.wShowWindow = SW_HIDE; |
| 3936 | |
| 3937 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3938 | s2, |
| 3939 | NULL, |
| 3940 | NULL, |
| 3941 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3942 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3943 | NULL, |
| 3944 | NULL, |
| 3945 | &siStartInfo, |
| 3946 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3947 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3948 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3949 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3950 | /* Return process handle */ |
| 3951 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3952 | return TRUE; |
| 3953 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3954 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3955 | return FALSE; |
| 3956 | } |
| 3957 | |
| 3958 | /* The following code is based off of KB: Q190351 */ |
| 3959 | |
| 3960 | static PyObject * |
| 3961 | _PyPopen(char *cmdstring, int mode, int n) |
| 3962 | { |
| 3963 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 3964 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3965 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3966 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3967 | SECURITY_ATTRIBUTES saAttr; |
| 3968 | BOOL fSuccess; |
| 3969 | int fd1, fd2, fd3; |
| 3970 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3971 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3972 | PyObject *f; |
| 3973 | |
| 3974 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 3975 | saAttr.bInheritHandle = TRUE; |
| 3976 | saAttr.lpSecurityDescriptor = NULL; |
| 3977 | |
| 3978 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 3979 | return win32_error("CreatePipe", NULL); |
| 3980 | |
| 3981 | /* Create new output read handle and the input write handle. Set |
| 3982 | * the inheritance properties to FALSE. Otherwise, the child inherits |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 3983 | * these handles; resulting in non-closeable handles to the pipes |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3984 | * being created. */ |
| 3985 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3986 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 3987 | FALSE, |
| 3988 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3989 | if (!fSuccess) |
| 3990 | return win32_error("DuplicateHandle", NULL); |
| 3991 | |
| 3992 | /* Close the inheritable version of ChildStdin |
| 3993 | that we're using. */ |
| 3994 | CloseHandle(hChildStdinWr); |
| 3995 | |
| 3996 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 3997 | return win32_error("CreatePipe", NULL); |
| 3998 | |
| 3999 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4000 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 4001 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4002 | if (!fSuccess) |
| 4003 | return win32_error("DuplicateHandle", NULL); |
| 4004 | |
| 4005 | /* Close the inheritable version of ChildStdout |
| 4006 | that we're using. */ |
| 4007 | CloseHandle(hChildStdoutRd); |
| 4008 | |
| 4009 | if (n != POPEN_4) { |
| 4010 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 4011 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4012 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 4013 | hChildStderrRd, |
| 4014 | GetCurrentProcess(), |
| 4015 | &hChildStderrRdDup, 0, |
| 4016 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4017 | if (!fSuccess) |
| 4018 | return win32_error("DuplicateHandle", NULL); |
| 4019 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 4020 | CloseHandle(hChildStderrRd); |
| 4021 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4022 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4023 | switch (n) { |
| 4024 | case POPEN_1: |
| 4025 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 4026 | case _O_WRONLY | _O_TEXT: |
| 4027 | /* Case for writing to child Stdin in text mode. */ |
| 4028 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4029 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4030 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4031 | PyFile_SetBufSize(f, 0); |
| 4032 | /* We don't care about these pipes anymore, so close them. */ |
| 4033 | CloseHandle(hChildStdoutRdDup); |
| 4034 | CloseHandle(hChildStderrRdDup); |
| 4035 | break; |
| 4036 | |
| 4037 | case _O_RDONLY | _O_TEXT: |
| 4038 | /* Case for reading from child Stdout in text mode. */ |
| 4039 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4040 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4041 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4042 | PyFile_SetBufSize(f, 0); |
| 4043 | /* We don't care about these pipes anymore, so close them. */ |
| 4044 | CloseHandle(hChildStdinWrDup); |
| 4045 | CloseHandle(hChildStderrRdDup); |
| 4046 | break; |
| 4047 | |
| 4048 | case _O_RDONLY | _O_BINARY: |
| 4049 | /* Case for readinig from child Stdout in binary mode. */ |
| 4050 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4051 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4052 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4053 | PyFile_SetBufSize(f, 0); |
| 4054 | /* We don't care about these pipes anymore, so close them. */ |
| 4055 | CloseHandle(hChildStdinWrDup); |
| 4056 | CloseHandle(hChildStderrRdDup); |
| 4057 | break; |
| 4058 | |
| 4059 | case _O_WRONLY | _O_BINARY: |
| 4060 | /* Case for writing to child Stdin in binary mode. */ |
| 4061 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4062 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4063 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4064 | PyFile_SetBufSize(f, 0); |
| 4065 | /* We don't care about these pipes anymore, so close them. */ |
| 4066 | CloseHandle(hChildStdoutRdDup); |
| 4067 | CloseHandle(hChildStderrRdDup); |
| 4068 | break; |
| 4069 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4070 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4071 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4072 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4073 | case POPEN_2: |
| 4074 | case POPEN_4: |
| 4075 | { |
| 4076 | char *m1, *m2; |
| 4077 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4078 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4079 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4080 | m1 = "r"; |
| 4081 | m2 = "w"; |
| 4082 | } else { |
| 4083 | m1 = "rb"; |
| 4084 | m2 = "wb"; |
| 4085 | } |
| 4086 | |
| 4087 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4088 | f1 = _fdopen(fd1, m2); |
| 4089 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4090 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4091 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4092 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4093 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4094 | PyFile_SetBufSize(p2, 0); |
| 4095 | |
| 4096 | if (n != 4) |
| 4097 | CloseHandle(hChildStderrRdDup); |
| 4098 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4099 | f = PyTuple_Pack(2,p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4100 | Py_XDECREF(p1); |
| 4101 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4102 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4103 | break; |
| 4104 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4105 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4106 | case POPEN_3: |
| 4107 | { |
| 4108 | char *m1, *m2; |
| 4109 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4110 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4111 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4112 | m1 = "r"; |
| 4113 | m2 = "w"; |
| 4114 | } else { |
| 4115 | m1 = "rb"; |
| 4116 | m2 = "wb"; |
| 4117 | } |
| 4118 | |
| 4119 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4120 | f1 = _fdopen(fd1, m2); |
| 4121 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4122 | f2 = _fdopen(fd2, m1); |
| 4123 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 4124 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4125 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4126 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 4127 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4128 | PyFile_SetBufSize(p1, 0); |
| 4129 | PyFile_SetBufSize(p2, 0); |
| 4130 | PyFile_SetBufSize(p3, 0); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4131 | f = PyTuple_Pack(3,p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4132 | Py_XDECREF(p1); |
| 4133 | Py_XDECREF(p2); |
| 4134 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4135 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4136 | break; |
| 4137 | } |
| 4138 | } |
| 4139 | |
| 4140 | if (n == POPEN_4) { |
| 4141 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4142 | hChildStdinRd, |
| 4143 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4144 | hChildStdoutWr, |
| 4145 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4146 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4147 | } |
| 4148 | else { |
| 4149 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4150 | hChildStdinRd, |
| 4151 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4152 | hChildStderrWr, |
| 4153 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4154 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4157 | /* |
| 4158 | * Insert the files we've created into the process dictionary |
| 4159 | * all referencing the list with the process handle and the |
| 4160 | * initial number of files (see description below in _PyPclose). |
| 4161 | * Since if _PyPclose later tried to wait on a process when all |
| 4162 | * handles weren't closed, it could create a deadlock with the |
| 4163 | * child, we spend some energy here to try to ensure that we |
| 4164 | * either insert all file handles into the dictionary or none |
| 4165 | * at all. It's a little clumsy with the various popen modes |
| 4166 | * and variable number of files involved. |
| 4167 | */ |
| 4168 | if (!_PyPopenProcs) { |
| 4169 | _PyPopenProcs = PyDict_New(); |
| 4170 | } |
| 4171 | |
| 4172 | if (_PyPopenProcs) { |
| 4173 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 4174 | int ins_rc[3]; |
| 4175 | |
| 4176 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4177 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4178 | |
| 4179 | procObj = PyList_New(2); |
| 4180 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 4181 | intObj = PyInt_FromLong(file_count); |
| 4182 | |
| 4183 | if (procObj && hProcessObj && intObj) { |
| 4184 | PyList_SetItem(procObj,0,hProcessObj); |
| 4185 | PyList_SetItem(procObj,1,intObj); |
| 4186 | |
| 4187 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 4188 | if (fileObj[0]) { |
| 4189 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4190 | fileObj[0], |
| 4191 | procObj); |
| 4192 | } |
| 4193 | if (file_count >= 2) { |
| 4194 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 4195 | if (fileObj[1]) { |
| 4196 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4197 | fileObj[1], |
| 4198 | procObj); |
| 4199 | } |
| 4200 | } |
| 4201 | if (file_count >= 3) { |
| 4202 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 4203 | if (fileObj[2]) { |
| 4204 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4205 | fileObj[2], |
| 4206 | procObj); |
| 4207 | } |
| 4208 | } |
| 4209 | |
| 4210 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4211 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4212 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 4213 | /* Something failed - remove any dictionary |
| 4214 | * entries that did make it. |
| 4215 | */ |
| 4216 | if (!ins_rc[0] && fileObj[0]) { |
| 4217 | PyDict_DelItem(_PyPopenProcs, |
| 4218 | fileObj[0]); |
| 4219 | } |
| 4220 | if (!ins_rc[1] && fileObj[1]) { |
| 4221 | PyDict_DelItem(_PyPopenProcs, |
| 4222 | fileObj[1]); |
| 4223 | } |
| 4224 | if (!ins_rc[2] && fileObj[2]) { |
| 4225 | PyDict_DelItem(_PyPopenProcs, |
| 4226 | fileObj[2]); |
| 4227 | } |
| 4228 | } |
| 4229 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4230 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4231 | /* |
| 4232 | * Clean up our localized references for the dictionary keys |
| 4233 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4234 | * that got placed in the dictionary. |
| 4235 | */ |
| 4236 | Py_XDECREF(procObj); |
| 4237 | Py_XDECREF(fileObj[0]); |
| 4238 | Py_XDECREF(fileObj[1]); |
| 4239 | Py_XDECREF(fileObj[2]); |
| 4240 | } |
| 4241 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4242 | /* Child is launched. Close the parents copy of those pipe |
| 4243 | * handles that only the child should have open. You need to |
| 4244 | * make sure that no handles to the write end of the output pipe |
| 4245 | * are maintained in this process or else the pipe will not close |
| 4246 | * when the child process exits and the ReadFile will hang. */ |
| 4247 | |
| 4248 | if (!CloseHandle(hChildStdinRd)) |
| 4249 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4250 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4251 | if (!CloseHandle(hChildStdoutWr)) |
| 4252 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4253 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4254 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 4255 | return win32_error("CloseHandle", NULL); |
| 4256 | |
| 4257 | return f; |
| 4258 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4259 | |
| 4260 | /* |
| 4261 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4262 | * 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] | 4263 | * |
| 4264 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4265 | * input file pointer to information about the process that was |
| 4266 | * originally created by the popen* call that created the file pointer. |
| 4267 | * The dictionary uses the file pointer as a key (with one entry |
| 4268 | * inserted for each file returned by the original popen* call) and a |
| 4269 | * single list object as the value for all files from a single call. |
| 4270 | * The list object contains the Win32 process handle at [0], and a file |
| 4271 | * count at [1], which is initialized to the total number of file |
| 4272 | * handles using that list. |
| 4273 | * |
| 4274 | * This function closes whichever handle it is passed, and decrements |
| 4275 | * the file count in the dictionary for the process handle pointed to |
| 4276 | * by this file. On the last close (when the file count reaches zero), |
| 4277 | * this function will wait for the child process and then return its |
| 4278 | * exit code as the result of the close() operation. This permits the |
| 4279 | * files to be closed in any order - it is always the close() of the |
| 4280 | * final handle that will return the exit code. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4281 | * |
| 4282 | * NOTE: This function is currently called with the GIL released. |
| 4283 | * hence we use the GILState API to manage our state. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4284 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4285 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4286 | static int _PyPclose(FILE *file) |
| 4287 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4288 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4289 | DWORD exit_code; |
| 4290 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4291 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 4292 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4293 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4294 | PyGILState_STATE state; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4295 | #endif |
| 4296 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4297 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4298 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4299 | */ |
| 4300 | result = fclose(file); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4301 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4302 | state = PyGILState_Ensure(); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4303 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4304 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4305 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4306 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4307 | fileObj)) != NULL && |
| 4308 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 4309 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 4310 | |
| 4311 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 4312 | file_count = PyInt_AsLong(intObj); |
| 4313 | |
| 4314 | if (file_count > 1) { |
| 4315 | /* Still other files referencing process */ |
| 4316 | file_count--; |
| 4317 | PyList_SetItem(procObj,1, |
| 4318 | PyInt_FromLong(file_count)); |
| 4319 | } else { |
| 4320 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4321 | if (result != EOF && |
| 4322 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 4323 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4324 | /* Possible truncation here in 16-bit environments, but |
| 4325 | * real exit codes are just the lower byte in any event. |
| 4326 | */ |
| 4327 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4328 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4329 | /* Indicate failure - this will cause the file object |
| 4330 | * to raise an I/O error and translate the last Win32 |
| 4331 | * error code from errno. We do have a problem with |
| 4332 | * last errors that overlap the normal errno table, |
| 4333 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4334 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4335 | if (result != EOF) { |
| 4336 | /* If the error wasn't from the fclose(), then |
| 4337 | * set errno for the file object error handling. |
| 4338 | */ |
| 4339 | errno = GetLastError(); |
| 4340 | } |
| 4341 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4342 | } |
| 4343 | |
| 4344 | /* Free up the native handle at this point */ |
| 4345 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4346 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4347 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4348 | /* Remove this file pointer from dictionary */ |
| 4349 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4350 | |
| 4351 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 4352 | Py_DECREF(_PyPopenProcs); |
| 4353 | _PyPopenProcs = NULL; |
| 4354 | } |
| 4355 | |
| 4356 | } /* if object retrieval ok */ |
| 4357 | |
| 4358 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4359 | } /* if _PyPopenProcs */ |
| 4360 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4361 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4362 | PyGILState_Release(state); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4363 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4364 | return result; |
| 4365 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4366 | |
| 4367 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4368 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4369 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4370 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4371 | char *name; |
| 4372 | char *mode = "r"; |
| 4373 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4374 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4375 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4376 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4377 | return NULL; |
Martin v. Löwis | 9ad853b | 2003-10-31 10:01:53 +0000 | [diff] [blame] | 4378 | /* Strip mode of binary or text modifiers */ |
| 4379 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) |
| 4380 | mode = "r"; |
| 4381 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) |
| 4382 | mode = "w"; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4383 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4384 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4385 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4386 | if (fp == NULL) |
| 4387 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4388 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4389 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4390 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4391 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4392 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4393 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4394 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4395 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4396 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4397 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4398 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4399 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4400 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4401 | Set the current process's user id."); |
| 4402 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4403 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4404 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4405 | { |
| 4406 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4407 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4408 | return NULL; |
| 4409 | if (setuid(uid) < 0) |
| 4410 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4411 | Py_INCREF(Py_None); |
| 4412 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4413 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4414 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4415 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4416 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4417 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4418 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4419 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4420 | Set the current process's effective user id."); |
| 4421 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4422 | static PyObject * |
| 4423 | posix_seteuid (PyObject *self, PyObject *args) |
| 4424 | { |
| 4425 | int euid; |
| 4426 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 4427 | return NULL; |
| 4428 | } else if (seteuid(euid) < 0) { |
| 4429 | return posix_error(); |
| 4430 | } else { |
| 4431 | Py_INCREF(Py_None); |
| 4432 | return Py_None; |
| 4433 | } |
| 4434 | } |
| 4435 | #endif /* HAVE_SETEUID */ |
| 4436 | |
| 4437 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4438 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4439 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4440 | Set the current process's effective group id."); |
| 4441 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4442 | static PyObject * |
| 4443 | posix_setegid (PyObject *self, PyObject *args) |
| 4444 | { |
| 4445 | int egid; |
| 4446 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 4447 | return NULL; |
| 4448 | } else if (setegid(egid) < 0) { |
| 4449 | return posix_error(); |
| 4450 | } else { |
| 4451 | Py_INCREF(Py_None); |
| 4452 | return Py_None; |
| 4453 | } |
| 4454 | } |
| 4455 | #endif /* HAVE_SETEGID */ |
| 4456 | |
| 4457 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4458 | PyDoc_STRVAR(posix_setreuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4459 | "seteuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4460 | Set the current process's real and effective user ids."); |
| 4461 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4462 | static PyObject * |
| 4463 | posix_setreuid (PyObject *self, PyObject *args) |
| 4464 | { |
| 4465 | int ruid, euid; |
| 4466 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4467 | return NULL; |
| 4468 | } else if (setreuid(ruid, euid) < 0) { |
| 4469 | return posix_error(); |
| 4470 | } else { |
| 4471 | Py_INCREF(Py_None); |
| 4472 | return Py_None; |
| 4473 | } |
| 4474 | } |
| 4475 | #endif /* HAVE_SETREUID */ |
| 4476 | |
| 4477 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4478 | PyDoc_STRVAR(posix_setregid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4479 | "setegid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4480 | Set the current process's real and effective group ids."); |
| 4481 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4482 | static PyObject * |
| 4483 | posix_setregid (PyObject *self, PyObject *args) |
| 4484 | { |
| 4485 | int rgid, egid; |
| 4486 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 4487 | return NULL; |
| 4488 | } else if (setregid(rgid, egid) < 0) { |
| 4489 | return posix_error(); |
| 4490 | } else { |
| 4491 | Py_INCREF(Py_None); |
| 4492 | return Py_None; |
| 4493 | } |
| 4494 | } |
| 4495 | #endif /* HAVE_SETREGID */ |
| 4496 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4497 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4498 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4499 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4500 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4501 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4502 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4503 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4504 | { |
| 4505 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4506 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4507 | return NULL; |
| 4508 | if (setgid(gid) < 0) |
| 4509 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4510 | Py_INCREF(Py_None); |
| 4511 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4512 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4513 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4514 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4515 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4516 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4517 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4518 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4519 | |
| 4520 | static PyObject * |
| 4521 | posix_setgroups(PyObject *self, PyObject *args) |
| 4522 | { |
| 4523 | PyObject *groups; |
| 4524 | int i, len; |
| 4525 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4526 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4527 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 4528 | return NULL; |
| 4529 | if (!PySequence_Check(groups)) { |
| 4530 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4531 | return NULL; |
| 4532 | } |
| 4533 | len = PySequence_Size(groups); |
| 4534 | if (len > MAX_GROUPS) { |
| 4535 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4536 | return NULL; |
| 4537 | } |
| 4538 | for(i = 0; i < len; i++) { |
| 4539 | PyObject *elem; |
| 4540 | elem = PySequence_GetItem(groups, i); |
| 4541 | if (!elem) |
| 4542 | return NULL; |
| 4543 | if (!PyInt_Check(elem)) { |
| 4544 | PyErr_SetString(PyExc_TypeError, |
| 4545 | "groups must be integers"); |
| 4546 | Py_DECREF(elem); |
| 4547 | return NULL; |
| 4548 | } |
| 4549 | /* XXX: check that value fits into gid_t. */ |
| 4550 | grouplist[i] = PyInt_AsLong(elem); |
| 4551 | Py_DECREF(elem); |
| 4552 | } |
| 4553 | |
| 4554 | if (setgroups(len, grouplist) < 0) |
| 4555 | return posix_error(); |
| 4556 | Py_INCREF(Py_None); |
| 4557 | return Py_None; |
| 4558 | } |
| 4559 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4560 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4561 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4562 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4563 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4564 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4565 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4566 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4567 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4568 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4569 | int pid, options; |
| 4570 | #ifdef UNION_WAIT |
| 4571 | union wait status; |
| 4572 | #define status_i (status.w_status) |
| 4573 | #else |
| 4574 | int status; |
| 4575 | #define status_i status |
| 4576 | #endif |
| 4577 | status_i = 0; |
| 4578 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4579 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4580 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4581 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4582 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4583 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4584 | if (pid == -1) |
| 4585 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4586 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4587 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4588 | } |
| 4589 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4590 | #elif defined(HAVE_CWAIT) |
| 4591 | |
| 4592 | /* 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] | 4593 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4594 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4595 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4596 | |
| 4597 | static PyObject * |
| 4598 | posix_waitpid(PyObject *self, PyObject *args) |
| 4599 | { |
| 4600 | int pid, options; |
| 4601 | int status; |
| 4602 | |
| 4603 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4604 | return NULL; |
| 4605 | Py_BEGIN_ALLOW_THREADS |
| 4606 | pid = _cwait(&status, pid, options); |
| 4607 | Py_END_ALLOW_THREADS |
| 4608 | if (pid == -1) |
| 4609 | return posix_error(); |
| 4610 | else |
| 4611 | /* shift the status left a byte so this is more like the |
| 4612 | POSIX waitpid */ |
| 4613 | return Py_BuildValue("ii", pid, status << 8); |
| 4614 | } |
| 4615 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4616 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4617 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4618 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4619 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4620 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4621 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4622 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4623 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4624 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4625 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4626 | #ifdef UNION_WAIT |
| 4627 | union wait status; |
| 4628 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4629 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4630 | int status; |
| 4631 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4632 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4633 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4634 | status_i = 0; |
| 4635 | Py_BEGIN_ALLOW_THREADS |
| 4636 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4637 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4638 | if (pid == -1) |
| 4639 | return posix_error(); |
| 4640 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4641 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4642 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4643 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4644 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4645 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4646 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4647 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4648 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4649 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4650 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4651 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4652 | posix_lstat(PyObject *self, PyObject *args) |
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 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4655 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4656 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4657 | #ifdef MS_WINDOWS |
Mark Hammond | 7edd0a9 | 2003-08-06 02:46:58 +0000 | [diff] [blame] | 4658 | return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", _wstati64); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4659 | #else |
| 4660 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 4661 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4662 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4663 | } |
| 4664 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4665 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4666 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4667 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4668 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4669 | 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] | 4670 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4671 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4672 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4673 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4674 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4675 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4676 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4677 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4678 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4679 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4680 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4681 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4682 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 4683 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4684 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4685 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4686 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4687 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4688 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4689 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4690 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4691 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4692 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4693 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4694 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4695 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4696 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4697 | return posix_2str(args, "etet:symlink", symlink, NULL, NULL); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4698 | } |
| 4699 | #endif /* HAVE_SYMLINK */ |
| 4700 | |
| 4701 | |
| 4702 | #ifdef HAVE_TIMES |
| 4703 | #ifndef HZ |
| 4704 | #define HZ 60 /* Universal constant :-) */ |
| 4705 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4706 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4707 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4708 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4709 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4710 | { |
| 4711 | ULONG value = 0; |
| 4712 | |
| 4713 | Py_BEGIN_ALLOW_THREADS |
| 4714 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4715 | Py_END_ALLOW_THREADS |
| 4716 | |
| 4717 | return value; |
| 4718 | } |
| 4719 | |
| 4720 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4721 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4722 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4723 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4724 | return Py_BuildValue("ddddd", |
| 4725 | (double)0 /* t.tms_utime / HZ */, |
| 4726 | (double)0 /* t.tms_stime / HZ */, |
| 4727 | (double)0 /* t.tms_cutime / HZ */, |
| 4728 | (double)0 /* t.tms_cstime / HZ */, |
| 4729 | (double)system_uptime() / 1000); |
| 4730 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4731 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4732 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4733 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4734 | { |
| 4735 | struct tms t; |
| 4736 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4737 | errno = 0; |
| 4738 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4739 | if (c == (clock_t) -1) |
| 4740 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4741 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4742 | (double)t.tms_utime / HZ, |
| 4743 | (double)t.tms_stime / HZ, |
| 4744 | (double)t.tms_cutime / HZ, |
| 4745 | (double)t.tms_cstime / HZ, |
| 4746 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4747 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4748 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4749 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4750 | |
| 4751 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4752 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4753 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4754 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4755 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4756 | { |
| 4757 | FILETIME create, exit, kernel, user; |
| 4758 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4759 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4760 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4761 | /* The fields of a FILETIME structure are the hi and lo part |
| 4762 | of a 64-bit value expressed in 100 nanosecond units. |
| 4763 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4764 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4765 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4766 | return Py_BuildValue( |
| 4767 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4768 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4769 | kernel.dwLowDateTime*1e-7), |
| 4770 | (double)(user.dwHighDateTime*429.4967296 + |
| 4771 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4772 | (double)0, |
| 4773 | (double)0, |
| 4774 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4775 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4776 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4777 | |
| 4778 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4779 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4780 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4781 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4782 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4783 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4784 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4785 | #ifdef HAVE_GETSID |
| 4786 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4787 | "getsid(pid) -> sid\n\n\ |
| 4788 | Call the system call getsid()."); |
| 4789 | |
| 4790 | static PyObject * |
| 4791 | posix_getsid(PyObject *self, PyObject *args) |
| 4792 | { |
| 4793 | int pid, sid; |
| 4794 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 4795 | return NULL; |
| 4796 | sid = getsid(pid); |
| 4797 | if (sid < 0) |
| 4798 | return posix_error(); |
| 4799 | return PyInt_FromLong((long)sid); |
| 4800 | } |
| 4801 | #endif /* HAVE_GETSID */ |
| 4802 | |
| 4803 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4804 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4805 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4806 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4807 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4808 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4809 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4810 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4811 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4812 | if (setsid() < 0) |
| 4813 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4814 | Py_INCREF(Py_None); |
| 4815 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4816 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4817 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4818 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4819 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4820 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4821 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4822 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4823 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4824 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4825 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4826 | { |
| 4827 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4828 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4829 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4830 | if (setpgid(pid, pgrp) < 0) |
| 4831 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4832 | Py_INCREF(Py_None); |
| 4833 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4834 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4835 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4836 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4837 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4838 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4839 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4840 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4841 | 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] | 4842 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4843 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4844 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4845 | { |
| 4846 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4847 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4848 | return NULL; |
| 4849 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4850 | if (pgid < 0) |
| 4851 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4852 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4853 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4854 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4855 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4856 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4857 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4858 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4859 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4860 | 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] | 4861 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4862 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4863 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4864 | { |
| 4865 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4866 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4867 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4868 | if (tcsetpgrp(fd, pgid) < 0) |
| 4869 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4870 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4871 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4872 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4873 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4874 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4875 | /* Functions acting on file descriptors */ |
| 4876 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4877 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4878 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4879 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4880 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4881 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4882 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4883 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4884 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4885 | int flag; |
| 4886 | int mode = 0777; |
| 4887 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4888 | |
| 4889 | #ifdef MS_WINDOWS |
| 4890 | if (unicode_file_names()) { |
| 4891 | PyUnicodeObject *po; |
| 4892 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4893 | Py_BEGIN_ALLOW_THREADS |
| 4894 | /* PyUnicode_AS_UNICODE OK without thread |
| 4895 | lock as it is a simple dereference. */ |
| 4896 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4897 | Py_END_ALLOW_THREADS |
| 4898 | if (fd < 0) |
| 4899 | return posix_error(); |
| 4900 | return PyInt_FromLong((long)fd); |
| 4901 | } |
| 4902 | /* Drop the argument parsing error as narrow strings |
| 4903 | are also valid. */ |
| 4904 | PyErr_Clear(); |
| 4905 | } |
| 4906 | #endif |
| 4907 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4908 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4909 | Py_FileSystemDefaultEncoding, &file, |
| 4910 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4911 | return NULL; |
| 4912 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4913 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4914 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4915 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4916 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4917 | return posix_error_with_allocated_filename(file); |
| 4918 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4919 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4920 | } |
| 4921 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4922 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4923 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4924 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4925 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4926 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4927 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4928 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4929 | { |
| 4930 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4931 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4932 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4933 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4934 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4935 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4936 | if (res < 0) |
| 4937 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4938 | Py_INCREF(Py_None); |
| 4939 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4940 | } |
| 4941 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4942 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4943 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4944 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4945 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4946 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4947 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4948 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4949 | { |
| 4950 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4951 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4952 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4953 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4954 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4955 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4956 | if (fd < 0) |
| 4957 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4958 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4959 | } |
| 4960 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4961 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4962 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 4963 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4964 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4965 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4966 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4967 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4968 | { |
| 4969 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4970 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4971 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4972 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4973 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4974 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4975 | if (res < 0) |
| 4976 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4977 | Py_INCREF(Py_None); |
| 4978 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4979 | } |
| 4980 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4981 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4982 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4983 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4984 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4985 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4986 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4987 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4988 | { |
| 4989 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4990 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 4991 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4992 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4993 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4994 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4995 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4996 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4997 | return NULL; |
| 4998 | #ifdef SEEK_SET |
| 4999 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5000 | switch (how) { |
| 5001 | case 0: how = SEEK_SET; break; |
| 5002 | case 1: how = SEEK_CUR; break; |
| 5003 | case 2: how = SEEK_END; break; |
| 5004 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5005 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5006 | |
| 5007 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5008 | pos = PyInt_AsLong(posobj); |
| 5009 | #else |
| 5010 | pos = PyLong_Check(posobj) ? |
| 5011 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 5012 | #endif |
| 5013 | if (PyErr_Occurred()) |
| 5014 | return NULL; |
| 5015 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5016 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5017 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5018 | res = _lseeki64(fd, pos, how); |
| 5019 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5020 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5021 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5022 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5023 | if (res < 0) |
| 5024 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5025 | |
| 5026 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5027 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5028 | #else |
| 5029 | return PyLong_FromLongLong(res); |
| 5030 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5031 | } |
| 5032 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5033 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5034 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5035 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5036 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5037 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5038 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5039 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5040 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5041 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5042 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5043 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5044 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5045 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5046 | if (buffer == NULL) |
| 5047 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5048 | Py_BEGIN_ALLOW_THREADS |
| 5049 | n = read(fd, PyString_AsString(buffer), size); |
| 5050 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5051 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5052 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5053 | return posix_error(); |
| 5054 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5055 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5056 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5057 | return buffer; |
| 5058 | } |
| 5059 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5060 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5061 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5062 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5063 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5064 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5065 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5066 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5067 | { |
| 5068 | int fd, size; |
| 5069 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5070 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5071 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5072 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5073 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5074 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5075 | if (size < 0) |
| 5076 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5077 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5078 | } |
| 5079 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5080 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5081 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5082 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5083 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5084 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5085 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5086 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5087 | { |
| 5088 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5089 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5090 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5091 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5092 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5093 | #ifdef __VMS |
| 5094 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5095 | fsync(fd); |
| 5096 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5097 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5098 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5099 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5100 | if (res != 0) |
| 5101 | return posix_error(); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5102 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5103 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5104 | } |
| 5105 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5106 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5107 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5108 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5109 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5110 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5111 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5112 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5113 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5114 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5115 | char *mode = "r"; |
| 5116 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5117 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5118 | PyObject *f; |
| 5119 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5120 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5121 | |
Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 5122 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 5123 | PyErr_Format(PyExc_ValueError, |
| 5124 | "invalid file mode '%s'", mode); |
| 5125 | return NULL; |
| 5126 | } |
| 5127 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5128 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5129 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5130 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5131 | if (fp == NULL) |
| 5132 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 5133 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5134 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5135 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5136 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5137 | } |
| 5138 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5139 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5140 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5141 | 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] | 5142 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5143 | |
| 5144 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5145 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5146 | { |
| 5147 | int fd; |
| 5148 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5149 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5150 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5151 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5152 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5153 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5154 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5155 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5156 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5157 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5158 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5159 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5160 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5161 | #if defined(PYOS_OS2) |
| 5162 | HFILE read, write; |
| 5163 | APIRET rc; |
| 5164 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5165 | Py_BEGIN_ALLOW_THREADS |
| 5166 | rc = DosCreatePipe( &read, &write, 4096); |
| 5167 | Py_END_ALLOW_THREADS |
| 5168 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5169 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5170 | |
| 5171 | return Py_BuildValue("(ii)", read, write); |
| 5172 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5173 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5174 | int fds[2]; |
| 5175 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5176 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5177 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5178 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5179 | if (res != 0) |
| 5180 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5181 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5182 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5183 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5184 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5185 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5186 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5187 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5188 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5189 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5190 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5191 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5192 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5193 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5194 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5195 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5196 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5197 | #endif /* HAVE_PIPE */ |
| 5198 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5199 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5200 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5201 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5202 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5203 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5204 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5205 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5206 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5207 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5208 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5209 | int mode = 0666; |
| 5210 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5211 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5212 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5213 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5214 | res = mkfifo(filename, mode); |
| 5215 | Py_END_ALLOW_THREADS |
| 5216 | if (res < 0) |
| 5217 | return posix_error(); |
| 5218 | Py_INCREF(Py_None); |
| 5219 | return Py_None; |
| 5220 | } |
| 5221 | #endif |
| 5222 | |
| 5223 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5224 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5225 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5226 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5227 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5228 | named filename. mode specifies both the permissions to use and the\n\ |
| 5229 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5230 | 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] | 5231 | device defines the newly created device special file (probably using\n\ |
| 5232 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5233 | |
| 5234 | |
| 5235 | static PyObject * |
| 5236 | posix_mknod(PyObject *self, PyObject *args) |
| 5237 | { |
| 5238 | char *filename; |
| 5239 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5240 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5241 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5242 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5243 | return NULL; |
| 5244 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5245 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5246 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5247 | if (res < 0) |
| 5248 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5249 | Py_INCREF(Py_None); |
| 5250 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5251 | } |
| 5252 | #endif |
| 5253 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5254 | #ifdef HAVE_DEVICE_MACROS |
| 5255 | PyDoc_STRVAR(posix_major__doc__, |
| 5256 | "major(device) -> major number\n\ |
| 5257 | Extracts a device major number from a raw device number."); |
| 5258 | |
| 5259 | static PyObject * |
| 5260 | posix_major(PyObject *self, PyObject *args) |
| 5261 | { |
| 5262 | int device; |
| 5263 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5264 | return NULL; |
| 5265 | return PyInt_FromLong((long)major(device)); |
| 5266 | } |
| 5267 | |
| 5268 | PyDoc_STRVAR(posix_minor__doc__, |
| 5269 | "minor(device) -> minor number\n\ |
| 5270 | Extracts a device minor number from a raw device number."); |
| 5271 | |
| 5272 | static PyObject * |
| 5273 | posix_minor(PyObject *self, PyObject *args) |
| 5274 | { |
| 5275 | int device; |
| 5276 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5277 | return NULL; |
| 5278 | return PyInt_FromLong((long)minor(device)); |
| 5279 | } |
| 5280 | |
| 5281 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5282 | "makedev(major, minor) -> device number\n\ |
| 5283 | Composes a raw device number from the major and minor device numbers."); |
| 5284 | |
| 5285 | static PyObject * |
| 5286 | posix_makedev(PyObject *self, PyObject *args) |
| 5287 | { |
| 5288 | int major, minor; |
| 5289 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5290 | return NULL; |
| 5291 | return PyInt_FromLong((long)makedev(major, minor)); |
| 5292 | } |
| 5293 | #endif /* device macros */ |
| 5294 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5295 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5296 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5297 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5298 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5299 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5300 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5301 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5302 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5303 | { |
| 5304 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5305 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5306 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5307 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5308 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5309 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5310 | return NULL; |
| 5311 | |
| 5312 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5313 | length = PyInt_AsLong(lenobj); |
| 5314 | #else |
| 5315 | length = PyLong_Check(lenobj) ? |
| 5316 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 5317 | #endif |
| 5318 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5319 | return NULL; |
| 5320 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5321 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5322 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5323 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5324 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5325 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5326 | return NULL; |
| 5327 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5328 | Py_INCREF(Py_None); |
| 5329 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5330 | } |
| 5331 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5332 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5333 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5334 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5335 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5336 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5337 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5338 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5339 | * get re-set with another call for the same key. */ |
| 5340 | static PyObject *posix_putenv_garbage; |
| 5341 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5342 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5343 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5344 | { |
| 5345 | char *s1, *s2; |
| 5346 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5347 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5348 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5349 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5350 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5351 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5352 | |
| 5353 | #if defined(PYOS_OS2) |
| 5354 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5355 | APIRET rc; |
| 5356 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5357 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5358 | if (rc != NO_ERROR) |
| 5359 | return os2_error(rc); |
| 5360 | |
| 5361 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5362 | APIRET rc; |
| 5363 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5364 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5365 | if (rc != NO_ERROR) |
| 5366 | return os2_error(rc); |
| 5367 | } else { |
| 5368 | #endif |
| 5369 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5370 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5371 | len = strlen(s1) + strlen(s2) + 2; |
| 5372 | /* len includes space for a trailing \0; the size arg to |
| 5373 | PyString_FromStringAndSize does not count that */ |
| 5374 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5375 | if (newstr == NULL) |
| 5376 | return PyErr_NoMemory(); |
| 5377 | new = PyString_AS_STRING(newstr); |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5378 | PyOS_snprintf(new, len, "%s=%s", s1, s2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5379 | if (putenv(new)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5380 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5381 | posix_error(); |
| 5382 | return NULL; |
| 5383 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5384 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5385 | * this will cause previous value to be collected. This has to |
| 5386 | * happen after the real putenv() call because the old value |
| 5387 | * was still accessible until then. */ |
| 5388 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5389 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5390 | /* really not much we can do; just leak */ |
| 5391 | PyErr_Clear(); |
| 5392 | } |
| 5393 | else { |
| 5394 | Py_DECREF(newstr); |
| 5395 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5396 | |
| 5397 | #if defined(PYOS_OS2) |
| 5398 | } |
| 5399 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5400 | Py_INCREF(Py_None); |
| 5401 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5402 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5403 | #endif /* putenv */ |
| 5404 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5405 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5406 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5407 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5408 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5409 | |
| 5410 | static PyObject * |
| 5411 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5412 | { |
| 5413 | char *s1; |
| 5414 | |
| 5415 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5416 | return NULL; |
| 5417 | |
| 5418 | unsetenv(s1); |
| 5419 | |
| 5420 | /* Remove the key from posix_putenv_garbage; |
| 5421 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5422 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5423 | * old value was still accessible until then. |
| 5424 | */ |
| 5425 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5426 | PyTuple_GET_ITEM(args, 0))) { |
| 5427 | /* really not much we can do; just leak */ |
| 5428 | PyErr_Clear(); |
| 5429 | } |
| 5430 | |
| 5431 | Py_INCREF(Py_None); |
| 5432 | return Py_None; |
| 5433 | } |
| 5434 | #endif /* unsetenv */ |
| 5435 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5436 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5437 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5438 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5439 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5440 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5441 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5442 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5443 | { |
| 5444 | int code; |
| 5445 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5446 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5447 | return NULL; |
| 5448 | message = strerror(code); |
| 5449 | if (message == NULL) { |
| 5450 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5451 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5452 | return NULL; |
| 5453 | } |
| 5454 | return PyString_FromString(message); |
| 5455 | } |
| 5456 | #endif /* strerror */ |
| 5457 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5458 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5459 | #ifdef HAVE_SYS_WAIT_H |
| 5460 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5461 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5462 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5463 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5464 | 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] | 5465 | |
| 5466 | static PyObject * |
| 5467 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5468 | { |
| 5469 | #ifdef UNION_WAIT |
| 5470 | union wait status; |
| 5471 | #define status_i (status.w_status) |
| 5472 | #else |
| 5473 | int status; |
| 5474 | #define status_i status |
| 5475 | #endif |
| 5476 | status_i = 0; |
| 5477 | |
| 5478 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i)) |
| 5479 | { |
| 5480 | return NULL; |
| 5481 | } |
| 5482 | |
| 5483 | return PyBool_FromLong(WCOREDUMP(status)); |
| 5484 | #undef status_i |
| 5485 | } |
| 5486 | #endif /* WCOREDUMP */ |
| 5487 | |
| 5488 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5489 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5490 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5491 | 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] | 5492 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5493 | |
| 5494 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5495 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5496 | { |
| 5497 | #ifdef UNION_WAIT |
| 5498 | union wait status; |
| 5499 | #define status_i (status.w_status) |
| 5500 | #else |
| 5501 | int status; |
| 5502 | #define status_i status |
| 5503 | #endif |
| 5504 | status_i = 0; |
| 5505 | |
| 5506 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i)) |
| 5507 | { |
| 5508 | return NULL; |
| 5509 | } |
| 5510 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5511 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5512 | #undef status_i |
| 5513 | } |
| 5514 | #endif /* WIFCONTINUED */ |
| 5515 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5516 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5517 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5518 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5519 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5520 | |
| 5521 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5522 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5523 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5524 | #ifdef UNION_WAIT |
| 5525 | union wait status; |
| 5526 | #define status_i (status.w_status) |
| 5527 | #else |
| 5528 | int status; |
| 5529 | #define status_i status |
| 5530 | #endif |
| 5531 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5532 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5533 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5534 | { |
| 5535 | return NULL; |
| 5536 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5537 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5538 | return PyBool_FromLong(WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5539 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5540 | } |
| 5541 | #endif /* WIFSTOPPED */ |
| 5542 | |
| 5543 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5544 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5545 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5546 | 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] | 5547 | |
| 5548 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5549 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5550 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5551 | #ifdef UNION_WAIT |
| 5552 | union wait status; |
| 5553 | #define status_i (status.w_status) |
| 5554 | #else |
| 5555 | int status; |
| 5556 | #define status_i status |
| 5557 | #endif |
| 5558 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5559 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5560 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5561 | { |
| 5562 | return NULL; |
| 5563 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5564 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5565 | return PyBool_FromLong(WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5566 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5567 | } |
| 5568 | #endif /* WIFSIGNALED */ |
| 5569 | |
| 5570 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5571 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5572 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5573 | 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] | 5574 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5575 | |
| 5576 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5577 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5578 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5579 | #ifdef UNION_WAIT |
| 5580 | union wait status; |
| 5581 | #define status_i (status.w_status) |
| 5582 | #else |
| 5583 | int status; |
| 5584 | #define status_i status |
| 5585 | #endif |
| 5586 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5587 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5588 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5589 | { |
| 5590 | return NULL; |
| 5591 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5592 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5593 | return PyBool_FromLong(WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5594 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5595 | } |
| 5596 | #endif /* WIFEXITED */ |
| 5597 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5598 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5599 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5600 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5601 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5602 | |
| 5603 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5604 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5605 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5606 | #ifdef UNION_WAIT |
| 5607 | union wait status; |
| 5608 | #define status_i (status.w_status) |
| 5609 | #else |
| 5610 | int status; |
| 5611 | #define status_i status |
| 5612 | #endif |
| 5613 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5614 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5615 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5616 | { |
| 5617 | return NULL; |
| 5618 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5619 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5620 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5621 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5622 | } |
| 5623 | #endif /* WEXITSTATUS */ |
| 5624 | |
| 5625 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5626 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5627 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5628 | 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] | 5629 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5630 | |
| 5631 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5632 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5633 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5634 | #ifdef UNION_WAIT |
| 5635 | union wait status; |
| 5636 | #define status_i (status.w_status) |
| 5637 | #else |
| 5638 | int status; |
| 5639 | #define status_i status |
| 5640 | #endif |
| 5641 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5642 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5643 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5644 | { |
| 5645 | return NULL; |
| 5646 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5647 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5648 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5649 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5650 | } |
| 5651 | #endif /* WTERMSIG */ |
| 5652 | |
| 5653 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5654 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5655 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5656 | Return the signal that stopped the process that provided\n\ |
| 5657 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5658 | |
| 5659 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5660 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5661 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5662 | #ifdef UNION_WAIT |
| 5663 | union wait status; |
| 5664 | #define status_i (status.w_status) |
| 5665 | #else |
| 5666 | int status; |
| 5667 | #define status_i status |
| 5668 | #endif |
| 5669 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5670 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5671 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5672 | { |
| 5673 | return NULL; |
| 5674 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5675 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5676 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5677 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5678 | } |
| 5679 | #endif /* WSTOPSIG */ |
| 5680 | |
| 5681 | #endif /* HAVE_SYS_WAIT_H */ |
| 5682 | |
| 5683 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5684 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5685 | #ifdef _SCO_DS |
| 5686 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5687 | needed definitions in sys/statvfs.h */ |
| 5688 | #define _SVID3 |
| 5689 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5690 | #include <sys/statvfs.h> |
| 5691 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5692 | static PyObject* |
| 5693 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5694 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5695 | if (v == NULL) |
| 5696 | return NULL; |
| 5697 | |
| 5698 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5699 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5700 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 5701 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 5702 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 5703 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 5704 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 5705 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 5706 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 5707 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5708 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5709 | #else |
| 5710 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5711 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5712 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5713 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5714 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5715 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5716 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5717 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5718 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5719 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5720 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5721 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5722 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5723 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5724 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5725 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5726 | #endif |
| 5727 | |
| 5728 | return v; |
| 5729 | } |
| 5730 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5731 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5732 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5733 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5734 | |
| 5735 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5736 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5737 | { |
| 5738 | int fd, res; |
| 5739 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5740 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5741 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5742 | return NULL; |
| 5743 | Py_BEGIN_ALLOW_THREADS |
| 5744 | res = fstatvfs(fd, &st); |
| 5745 | Py_END_ALLOW_THREADS |
| 5746 | if (res != 0) |
| 5747 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5748 | |
| 5749 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5750 | } |
| 5751 | #endif /* HAVE_FSTATVFS */ |
| 5752 | |
| 5753 | |
| 5754 | #if defined(HAVE_STATVFS) |
| 5755 | #include <sys/statvfs.h> |
| 5756 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5757 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5758 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5759 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5760 | |
| 5761 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5762 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5763 | { |
| 5764 | char *path; |
| 5765 | int res; |
| 5766 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5767 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5768 | return NULL; |
| 5769 | Py_BEGIN_ALLOW_THREADS |
| 5770 | res = statvfs(path, &st); |
| 5771 | Py_END_ALLOW_THREADS |
| 5772 | if (res != 0) |
| 5773 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5774 | |
| 5775 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5776 | } |
| 5777 | #endif /* HAVE_STATVFS */ |
| 5778 | |
| 5779 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5780 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5781 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5782 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5783 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 5784 | 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] | 5785 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5786 | |
| 5787 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5788 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5789 | { |
| 5790 | PyObject *result = NULL; |
| 5791 | char *dir = NULL; |
| 5792 | char *pfx = NULL; |
| 5793 | char *name; |
| 5794 | |
| 5795 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 5796 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5797 | |
| 5798 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5799 | "tempnam is a potential security risk to your program") < 0) |
| 5800 | return NULL; |
| 5801 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5802 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5803 | name = _tempnam(dir, pfx); |
| 5804 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5805 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5806 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5807 | if (name == NULL) |
| 5808 | return PyErr_NoMemory(); |
| 5809 | result = PyString_FromString(name); |
| 5810 | free(name); |
| 5811 | return result; |
| 5812 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 5813 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5814 | |
| 5815 | |
| 5816 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5817 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5818 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5819 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5820 | |
| 5821 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5822 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5823 | { |
| 5824 | FILE *fp; |
| 5825 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5826 | fp = tmpfile(); |
| 5827 | if (fp == NULL) |
| 5828 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 5829 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5830 | } |
| 5831 | #endif |
| 5832 | |
| 5833 | |
| 5834 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5835 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5836 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5837 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5838 | |
| 5839 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5840 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5841 | { |
| 5842 | char buffer[L_tmpnam]; |
| 5843 | char *name; |
| 5844 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5845 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5846 | "tmpnam is a potential security risk to your program") < 0) |
| 5847 | return NULL; |
| 5848 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5849 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5850 | name = tmpnam_r(buffer); |
| 5851 | #else |
| 5852 | name = tmpnam(buffer); |
| 5853 | #endif |
| 5854 | if (name == NULL) { |
| 5855 | PyErr_SetObject(PyExc_OSError, |
| 5856 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5857 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5858 | "unexpected NULL from tmpnam_r" |
| 5859 | #else |
| 5860 | "unexpected NULL from tmpnam" |
| 5861 | #endif |
| 5862 | )); |
| 5863 | return NULL; |
| 5864 | } |
| 5865 | return PyString_FromString(buffer); |
| 5866 | } |
| 5867 | #endif |
| 5868 | |
| 5869 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5870 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5871 | * It maps strings representing configuration variable names to |
| 5872 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5873 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5874 | * rarely-used constants. There are three separate tables that use |
| 5875 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5876 | * |
| 5877 | * This code is always included, even if none of the interfaces that |
| 5878 | * need it are included. The #if hackery needed to avoid it would be |
| 5879 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5880 | */ |
| 5881 | struct constdef { |
| 5882 | char *name; |
| 5883 | long value; |
| 5884 | }; |
| 5885 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5886 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5887 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 5888 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5889 | { |
| 5890 | if (PyInt_Check(arg)) { |
| 5891 | *valuep = PyInt_AS_LONG(arg); |
| 5892 | return 1; |
| 5893 | } |
| 5894 | if (PyString_Check(arg)) { |
| 5895 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5896 | size_t lo = 0; |
| 5897 | size_t mid; |
| 5898 | size_t hi = tablesize; |
| 5899 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5900 | char *confname = PyString_AS_STRING(arg); |
| 5901 | while (lo < hi) { |
| 5902 | mid = (lo + hi) / 2; |
| 5903 | cmp = strcmp(confname, table[mid].name); |
| 5904 | if (cmp < 0) |
| 5905 | hi = mid; |
| 5906 | else if (cmp > 0) |
| 5907 | lo = mid + 1; |
| 5908 | else { |
| 5909 | *valuep = table[mid].value; |
| 5910 | return 1; |
| 5911 | } |
| 5912 | } |
| 5913 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 5914 | } |
| 5915 | else |
| 5916 | PyErr_SetString(PyExc_TypeError, |
| 5917 | "configuration names must be strings or integers"); |
| 5918 | return 0; |
| 5919 | } |
| 5920 | |
| 5921 | |
| 5922 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5923 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5924 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5925 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5926 | #endif |
| 5927 | #ifdef _PC_ABI_ASYNC_IO |
| 5928 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5929 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5930 | #ifdef _PC_ASYNC_IO |
| 5931 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5932 | #endif |
| 5933 | #ifdef _PC_CHOWN_RESTRICTED |
| 5934 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5935 | #endif |
| 5936 | #ifdef _PC_FILESIZEBITS |
| 5937 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5938 | #endif |
| 5939 | #ifdef _PC_LAST |
| 5940 | {"PC_LAST", _PC_LAST}, |
| 5941 | #endif |
| 5942 | #ifdef _PC_LINK_MAX |
| 5943 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5944 | #endif |
| 5945 | #ifdef _PC_MAX_CANON |
| 5946 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5947 | #endif |
| 5948 | #ifdef _PC_MAX_INPUT |
| 5949 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5950 | #endif |
| 5951 | #ifdef _PC_NAME_MAX |
| 5952 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5953 | #endif |
| 5954 | #ifdef _PC_NO_TRUNC |
| 5955 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5956 | #endif |
| 5957 | #ifdef _PC_PATH_MAX |
| 5958 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5959 | #endif |
| 5960 | #ifdef _PC_PIPE_BUF |
| 5961 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5962 | #endif |
| 5963 | #ifdef _PC_PRIO_IO |
| 5964 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5965 | #endif |
| 5966 | #ifdef _PC_SOCK_MAXBUF |
| 5967 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5968 | #endif |
| 5969 | #ifdef _PC_SYNC_IO |
| 5970 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5971 | #endif |
| 5972 | #ifdef _PC_VDISABLE |
| 5973 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5974 | #endif |
| 5975 | }; |
| 5976 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5977 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5978 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5979 | { |
| 5980 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5981 | sizeof(posix_constants_pathconf) |
| 5982 | / sizeof(struct constdef)); |
| 5983 | } |
| 5984 | #endif |
| 5985 | |
| 5986 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5987 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5988 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5989 | 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] | 5990 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5991 | |
| 5992 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5993 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5994 | { |
| 5995 | PyObject *result = NULL; |
| 5996 | int name, fd; |
| 5997 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5998 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5999 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6000 | long limit; |
| 6001 | |
| 6002 | errno = 0; |
| 6003 | limit = fpathconf(fd, name); |
| 6004 | if (limit == -1 && errno != 0) |
| 6005 | posix_error(); |
| 6006 | else |
| 6007 | result = PyInt_FromLong(limit); |
| 6008 | } |
| 6009 | return result; |
| 6010 | } |
| 6011 | #endif |
| 6012 | |
| 6013 | |
| 6014 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6015 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6016 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6017 | 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] | 6018 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6019 | |
| 6020 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6021 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6022 | { |
| 6023 | PyObject *result = NULL; |
| 6024 | int name; |
| 6025 | char *path; |
| 6026 | |
| 6027 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 6028 | conv_path_confname, &name)) { |
| 6029 | long limit; |
| 6030 | |
| 6031 | errno = 0; |
| 6032 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6033 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6034 | if (errno == EINVAL) |
| 6035 | /* could be a path or name problem */ |
| 6036 | posix_error(); |
| 6037 | else |
| 6038 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6039 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6040 | else |
| 6041 | result = PyInt_FromLong(limit); |
| 6042 | } |
| 6043 | return result; |
| 6044 | } |
| 6045 | #endif |
| 6046 | |
| 6047 | #ifdef HAVE_CONFSTR |
| 6048 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6049 | #ifdef _CS_ARCHITECTURE |
| 6050 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 6051 | #endif |
| 6052 | #ifdef _CS_HOSTNAME |
| 6053 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 6054 | #endif |
| 6055 | #ifdef _CS_HW_PROVIDER |
| 6056 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 6057 | #endif |
| 6058 | #ifdef _CS_HW_SERIAL |
| 6059 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 6060 | #endif |
| 6061 | #ifdef _CS_INITTAB_NAME |
| 6062 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 6063 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6064 | #ifdef _CS_LFS64_CFLAGS |
| 6065 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 6066 | #endif |
| 6067 | #ifdef _CS_LFS64_LDFLAGS |
| 6068 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 6069 | #endif |
| 6070 | #ifdef _CS_LFS64_LIBS |
| 6071 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 6072 | #endif |
| 6073 | #ifdef _CS_LFS64_LINTFLAGS |
| 6074 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 6075 | #endif |
| 6076 | #ifdef _CS_LFS_CFLAGS |
| 6077 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 6078 | #endif |
| 6079 | #ifdef _CS_LFS_LDFLAGS |
| 6080 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6081 | #endif |
| 6082 | #ifdef _CS_LFS_LIBS |
| 6083 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6084 | #endif |
| 6085 | #ifdef _CS_LFS_LINTFLAGS |
| 6086 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6087 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6088 | #ifdef _CS_MACHINE |
| 6089 | {"CS_MACHINE", _CS_MACHINE}, |
| 6090 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6091 | #ifdef _CS_PATH |
| 6092 | {"CS_PATH", _CS_PATH}, |
| 6093 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6094 | #ifdef _CS_RELEASE |
| 6095 | {"CS_RELEASE", _CS_RELEASE}, |
| 6096 | #endif |
| 6097 | #ifdef _CS_SRPC_DOMAIN |
| 6098 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6099 | #endif |
| 6100 | #ifdef _CS_SYSNAME |
| 6101 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6102 | #endif |
| 6103 | #ifdef _CS_VERSION |
| 6104 | {"CS_VERSION", _CS_VERSION}, |
| 6105 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6106 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6107 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6108 | #endif |
| 6109 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6110 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6111 | #endif |
| 6112 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6113 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6114 | #endif |
| 6115 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6116 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6117 | #endif |
| 6118 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6119 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6120 | #endif |
| 6121 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6122 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6123 | #endif |
| 6124 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6125 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6126 | #endif |
| 6127 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6128 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6129 | #endif |
| 6130 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6131 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6132 | #endif |
| 6133 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6134 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6135 | #endif |
| 6136 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6137 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6138 | #endif |
| 6139 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6140 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6141 | #endif |
| 6142 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6143 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6144 | #endif |
| 6145 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6146 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6147 | #endif |
| 6148 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6149 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6150 | #endif |
| 6151 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6152 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6153 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6154 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6155 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6156 | #endif |
| 6157 | #ifdef _MIPS_CS_BASE |
| 6158 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6159 | #endif |
| 6160 | #ifdef _MIPS_CS_HOSTID |
| 6161 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6162 | #endif |
| 6163 | #ifdef _MIPS_CS_HW_NAME |
| 6164 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6165 | #endif |
| 6166 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6167 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6168 | #endif |
| 6169 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6170 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6171 | #endif |
| 6172 | #ifdef _MIPS_CS_OSREL_MIN |
| 6173 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6174 | #endif |
| 6175 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6176 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6177 | #endif |
| 6178 | #ifdef _MIPS_CS_OS_NAME |
| 6179 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6180 | #endif |
| 6181 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6182 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6183 | #endif |
| 6184 | #ifdef _MIPS_CS_PROCESSORS |
| 6185 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6186 | #endif |
| 6187 | #ifdef _MIPS_CS_SERIAL |
| 6188 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6189 | #endif |
| 6190 | #ifdef _MIPS_CS_VENDOR |
| 6191 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6192 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6193 | }; |
| 6194 | |
| 6195 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6196 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6197 | { |
| 6198 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6199 | sizeof(posix_constants_confstr) |
| 6200 | / sizeof(struct constdef)); |
| 6201 | } |
| 6202 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6203 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6204 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6205 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6206 | |
| 6207 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6208 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6209 | { |
| 6210 | PyObject *result = NULL; |
| 6211 | int name; |
| 6212 | char buffer[64]; |
| 6213 | |
| 6214 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 6215 | int len = confstr(name, buffer, sizeof(buffer)); |
| 6216 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6217 | errno = 0; |
| 6218 | if (len == 0) { |
| 6219 | if (errno != 0) |
| 6220 | posix_error(); |
| 6221 | else |
| 6222 | result = PyString_FromString(""); |
| 6223 | } |
| 6224 | else { |
| 6225 | if (len >= sizeof(buffer)) { |
| 6226 | result = PyString_FromStringAndSize(NULL, len); |
| 6227 | if (result != NULL) |
| 6228 | confstr(name, PyString_AS_STRING(result), len+1); |
| 6229 | } |
| 6230 | else |
| 6231 | result = PyString_FromString(buffer); |
| 6232 | } |
| 6233 | } |
| 6234 | return result; |
| 6235 | } |
| 6236 | #endif |
| 6237 | |
| 6238 | |
| 6239 | #ifdef HAVE_SYSCONF |
| 6240 | static struct constdef posix_constants_sysconf[] = { |
| 6241 | #ifdef _SC_2_CHAR_TERM |
| 6242 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6243 | #endif |
| 6244 | #ifdef _SC_2_C_BIND |
| 6245 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6246 | #endif |
| 6247 | #ifdef _SC_2_C_DEV |
| 6248 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6249 | #endif |
| 6250 | #ifdef _SC_2_C_VERSION |
| 6251 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6252 | #endif |
| 6253 | #ifdef _SC_2_FORT_DEV |
| 6254 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6255 | #endif |
| 6256 | #ifdef _SC_2_FORT_RUN |
| 6257 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6258 | #endif |
| 6259 | #ifdef _SC_2_LOCALEDEF |
| 6260 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6261 | #endif |
| 6262 | #ifdef _SC_2_SW_DEV |
| 6263 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6264 | #endif |
| 6265 | #ifdef _SC_2_UPE |
| 6266 | {"SC_2_UPE", _SC_2_UPE}, |
| 6267 | #endif |
| 6268 | #ifdef _SC_2_VERSION |
| 6269 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6270 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6271 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6272 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6273 | #endif |
| 6274 | #ifdef _SC_ACL |
| 6275 | {"SC_ACL", _SC_ACL}, |
| 6276 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6277 | #ifdef _SC_AIO_LISTIO_MAX |
| 6278 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6279 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6280 | #ifdef _SC_AIO_MAX |
| 6281 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6282 | #endif |
| 6283 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6284 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6285 | #endif |
| 6286 | #ifdef _SC_ARG_MAX |
| 6287 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6288 | #endif |
| 6289 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6290 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6291 | #endif |
| 6292 | #ifdef _SC_ATEXIT_MAX |
| 6293 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6294 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6295 | #ifdef _SC_AUDIT |
| 6296 | {"SC_AUDIT", _SC_AUDIT}, |
| 6297 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6298 | #ifdef _SC_AVPHYS_PAGES |
| 6299 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6300 | #endif |
| 6301 | #ifdef _SC_BC_BASE_MAX |
| 6302 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6303 | #endif |
| 6304 | #ifdef _SC_BC_DIM_MAX |
| 6305 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6306 | #endif |
| 6307 | #ifdef _SC_BC_SCALE_MAX |
| 6308 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6309 | #endif |
| 6310 | #ifdef _SC_BC_STRING_MAX |
| 6311 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6312 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6313 | #ifdef _SC_CAP |
| 6314 | {"SC_CAP", _SC_CAP}, |
| 6315 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6316 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6317 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6318 | #endif |
| 6319 | #ifdef _SC_CHAR_BIT |
| 6320 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6321 | #endif |
| 6322 | #ifdef _SC_CHAR_MAX |
| 6323 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6324 | #endif |
| 6325 | #ifdef _SC_CHAR_MIN |
| 6326 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6327 | #endif |
| 6328 | #ifdef _SC_CHILD_MAX |
| 6329 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6330 | #endif |
| 6331 | #ifdef _SC_CLK_TCK |
| 6332 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6333 | #endif |
| 6334 | #ifdef _SC_COHER_BLKSZ |
| 6335 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6336 | #endif |
| 6337 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6338 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6339 | #endif |
| 6340 | #ifdef _SC_DCACHE_ASSOC |
| 6341 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6342 | #endif |
| 6343 | #ifdef _SC_DCACHE_BLKSZ |
| 6344 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6345 | #endif |
| 6346 | #ifdef _SC_DCACHE_LINESZ |
| 6347 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6348 | #endif |
| 6349 | #ifdef _SC_DCACHE_SZ |
| 6350 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6351 | #endif |
| 6352 | #ifdef _SC_DCACHE_TBLKSZ |
| 6353 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6354 | #endif |
| 6355 | #ifdef _SC_DELAYTIMER_MAX |
| 6356 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6357 | #endif |
| 6358 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6359 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6360 | #endif |
| 6361 | #ifdef _SC_EXPR_NEST_MAX |
| 6362 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6363 | #endif |
| 6364 | #ifdef _SC_FSYNC |
| 6365 | {"SC_FSYNC", _SC_FSYNC}, |
| 6366 | #endif |
| 6367 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6368 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6369 | #endif |
| 6370 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6371 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6372 | #endif |
| 6373 | #ifdef _SC_ICACHE_ASSOC |
| 6374 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6375 | #endif |
| 6376 | #ifdef _SC_ICACHE_BLKSZ |
| 6377 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6378 | #endif |
| 6379 | #ifdef _SC_ICACHE_LINESZ |
| 6380 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6381 | #endif |
| 6382 | #ifdef _SC_ICACHE_SZ |
| 6383 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6384 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6385 | #ifdef _SC_INF |
| 6386 | {"SC_INF", _SC_INF}, |
| 6387 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6388 | #ifdef _SC_INT_MAX |
| 6389 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6390 | #endif |
| 6391 | #ifdef _SC_INT_MIN |
| 6392 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6393 | #endif |
| 6394 | #ifdef _SC_IOV_MAX |
| 6395 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6396 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6397 | #ifdef _SC_IP_SECOPTS |
| 6398 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6399 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6400 | #ifdef _SC_JOB_CONTROL |
| 6401 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6402 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6403 | #ifdef _SC_KERN_POINTERS |
| 6404 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6405 | #endif |
| 6406 | #ifdef _SC_KERN_SIM |
| 6407 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6408 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6409 | #ifdef _SC_LINE_MAX |
| 6410 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6411 | #endif |
| 6412 | #ifdef _SC_LOGIN_NAME_MAX |
| 6413 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6414 | #endif |
| 6415 | #ifdef _SC_LOGNAME_MAX |
| 6416 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6417 | #endif |
| 6418 | #ifdef _SC_LONG_BIT |
| 6419 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6420 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6421 | #ifdef _SC_MAC |
| 6422 | {"SC_MAC", _SC_MAC}, |
| 6423 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6424 | #ifdef _SC_MAPPED_FILES |
| 6425 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6426 | #endif |
| 6427 | #ifdef _SC_MAXPID |
| 6428 | {"SC_MAXPID", _SC_MAXPID}, |
| 6429 | #endif |
| 6430 | #ifdef _SC_MB_LEN_MAX |
| 6431 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6432 | #endif |
| 6433 | #ifdef _SC_MEMLOCK |
| 6434 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6435 | #endif |
| 6436 | #ifdef _SC_MEMLOCK_RANGE |
| 6437 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6438 | #endif |
| 6439 | #ifdef _SC_MEMORY_PROTECTION |
| 6440 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6441 | #endif |
| 6442 | #ifdef _SC_MESSAGE_PASSING |
| 6443 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6444 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6445 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6446 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6447 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6448 | #ifdef _SC_MQ_OPEN_MAX |
| 6449 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6450 | #endif |
| 6451 | #ifdef _SC_MQ_PRIO_MAX |
| 6452 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6453 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6454 | #ifdef _SC_NACLS_MAX |
| 6455 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6456 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6457 | #ifdef _SC_NGROUPS_MAX |
| 6458 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6459 | #endif |
| 6460 | #ifdef _SC_NL_ARGMAX |
| 6461 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6462 | #endif |
| 6463 | #ifdef _SC_NL_LANGMAX |
| 6464 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6465 | #endif |
| 6466 | #ifdef _SC_NL_MSGMAX |
| 6467 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6468 | #endif |
| 6469 | #ifdef _SC_NL_NMAX |
| 6470 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6471 | #endif |
| 6472 | #ifdef _SC_NL_SETMAX |
| 6473 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6474 | #endif |
| 6475 | #ifdef _SC_NL_TEXTMAX |
| 6476 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6477 | #endif |
| 6478 | #ifdef _SC_NPROCESSORS_CONF |
| 6479 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6480 | #endif |
| 6481 | #ifdef _SC_NPROCESSORS_ONLN |
| 6482 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6483 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6484 | #ifdef _SC_NPROC_CONF |
| 6485 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6486 | #endif |
| 6487 | #ifdef _SC_NPROC_ONLN |
| 6488 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6489 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6490 | #ifdef _SC_NZERO |
| 6491 | {"SC_NZERO", _SC_NZERO}, |
| 6492 | #endif |
| 6493 | #ifdef _SC_OPEN_MAX |
| 6494 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6495 | #endif |
| 6496 | #ifdef _SC_PAGESIZE |
| 6497 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6498 | #endif |
| 6499 | #ifdef _SC_PAGE_SIZE |
| 6500 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6501 | #endif |
| 6502 | #ifdef _SC_PASS_MAX |
| 6503 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6504 | #endif |
| 6505 | #ifdef _SC_PHYS_PAGES |
| 6506 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6507 | #endif |
| 6508 | #ifdef _SC_PII |
| 6509 | {"SC_PII", _SC_PII}, |
| 6510 | #endif |
| 6511 | #ifdef _SC_PII_INTERNET |
| 6512 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6513 | #endif |
| 6514 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6515 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6516 | #endif |
| 6517 | #ifdef _SC_PII_INTERNET_STREAM |
| 6518 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6519 | #endif |
| 6520 | #ifdef _SC_PII_OSI |
| 6521 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6522 | #endif |
| 6523 | #ifdef _SC_PII_OSI_CLTS |
| 6524 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6525 | #endif |
| 6526 | #ifdef _SC_PII_OSI_COTS |
| 6527 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6528 | #endif |
| 6529 | #ifdef _SC_PII_OSI_M |
| 6530 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6531 | #endif |
| 6532 | #ifdef _SC_PII_SOCKET |
| 6533 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6534 | #endif |
| 6535 | #ifdef _SC_PII_XTI |
| 6536 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6537 | #endif |
| 6538 | #ifdef _SC_POLL |
| 6539 | {"SC_POLL", _SC_POLL}, |
| 6540 | #endif |
| 6541 | #ifdef _SC_PRIORITIZED_IO |
| 6542 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6543 | #endif |
| 6544 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6545 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6546 | #endif |
| 6547 | #ifdef _SC_REALTIME_SIGNALS |
| 6548 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6549 | #endif |
| 6550 | #ifdef _SC_RE_DUP_MAX |
| 6551 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6552 | #endif |
| 6553 | #ifdef _SC_RTSIG_MAX |
| 6554 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6555 | #endif |
| 6556 | #ifdef _SC_SAVED_IDS |
| 6557 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6558 | #endif |
| 6559 | #ifdef _SC_SCHAR_MAX |
| 6560 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6561 | #endif |
| 6562 | #ifdef _SC_SCHAR_MIN |
| 6563 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6564 | #endif |
| 6565 | #ifdef _SC_SELECT |
| 6566 | {"SC_SELECT", _SC_SELECT}, |
| 6567 | #endif |
| 6568 | #ifdef _SC_SEMAPHORES |
| 6569 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6570 | #endif |
| 6571 | #ifdef _SC_SEM_NSEMS_MAX |
| 6572 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6573 | #endif |
| 6574 | #ifdef _SC_SEM_VALUE_MAX |
| 6575 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6576 | #endif |
| 6577 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6578 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6579 | #endif |
| 6580 | #ifdef _SC_SHRT_MAX |
| 6581 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6582 | #endif |
| 6583 | #ifdef _SC_SHRT_MIN |
| 6584 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6585 | #endif |
| 6586 | #ifdef _SC_SIGQUEUE_MAX |
| 6587 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6588 | #endif |
| 6589 | #ifdef _SC_SIGRT_MAX |
| 6590 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6591 | #endif |
| 6592 | #ifdef _SC_SIGRT_MIN |
| 6593 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6594 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6595 | #ifdef _SC_SOFTPOWER |
| 6596 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6597 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6598 | #ifdef _SC_SPLIT_CACHE |
| 6599 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6600 | #endif |
| 6601 | #ifdef _SC_SSIZE_MAX |
| 6602 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6603 | #endif |
| 6604 | #ifdef _SC_STACK_PROT |
| 6605 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6606 | #endif |
| 6607 | #ifdef _SC_STREAM_MAX |
| 6608 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6609 | #endif |
| 6610 | #ifdef _SC_SYNCHRONIZED_IO |
| 6611 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6612 | #endif |
| 6613 | #ifdef _SC_THREADS |
| 6614 | {"SC_THREADS", _SC_THREADS}, |
| 6615 | #endif |
| 6616 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6617 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6618 | #endif |
| 6619 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6620 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6621 | #endif |
| 6622 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6623 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6624 | #endif |
| 6625 | #ifdef _SC_THREAD_KEYS_MAX |
| 6626 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6627 | #endif |
| 6628 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6629 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6630 | #endif |
| 6631 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6632 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6633 | #endif |
| 6634 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6635 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6636 | #endif |
| 6637 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6638 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6639 | #endif |
| 6640 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6641 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6642 | #endif |
| 6643 | #ifdef _SC_THREAD_STACK_MIN |
| 6644 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6645 | #endif |
| 6646 | #ifdef _SC_THREAD_THREADS_MAX |
| 6647 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6648 | #endif |
| 6649 | #ifdef _SC_TIMERS |
| 6650 | {"SC_TIMERS", _SC_TIMERS}, |
| 6651 | #endif |
| 6652 | #ifdef _SC_TIMER_MAX |
| 6653 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6654 | #endif |
| 6655 | #ifdef _SC_TTY_NAME_MAX |
| 6656 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6657 | #endif |
| 6658 | #ifdef _SC_TZNAME_MAX |
| 6659 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6660 | #endif |
| 6661 | #ifdef _SC_T_IOV_MAX |
| 6662 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6663 | #endif |
| 6664 | #ifdef _SC_UCHAR_MAX |
| 6665 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6666 | #endif |
| 6667 | #ifdef _SC_UINT_MAX |
| 6668 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6669 | #endif |
| 6670 | #ifdef _SC_UIO_MAXIOV |
| 6671 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6672 | #endif |
| 6673 | #ifdef _SC_ULONG_MAX |
| 6674 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6675 | #endif |
| 6676 | #ifdef _SC_USHRT_MAX |
| 6677 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6678 | #endif |
| 6679 | #ifdef _SC_VERSION |
| 6680 | {"SC_VERSION", _SC_VERSION}, |
| 6681 | #endif |
| 6682 | #ifdef _SC_WORD_BIT |
| 6683 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6684 | #endif |
| 6685 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6686 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6687 | #endif |
| 6688 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6689 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6690 | #endif |
| 6691 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6692 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6693 | #endif |
| 6694 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6695 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6696 | #endif |
| 6697 | #ifdef _SC_XOPEN_CRYPT |
| 6698 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6699 | #endif |
| 6700 | #ifdef _SC_XOPEN_ENH_I18N |
| 6701 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6702 | #endif |
| 6703 | #ifdef _SC_XOPEN_LEGACY |
| 6704 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6705 | #endif |
| 6706 | #ifdef _SC_XOPEN_REALTIME |
| 6707 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6708 | #endif |
| 6709 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6710 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6711 | #endif |
| 6712 | #ifdef _SC_XOPEN_SHM |
| 6713 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6714 | #endif |
| 6715 | #ifdef _SC_XOPEN_UNIX |
| 6716 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6717 | #endif |
| 6718 | #ifdef _SC_XOPEN_VERSION |
| 6719 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6720 | #endif |
| 6721 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6722 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6723 | #endif |
| 6724 | #ifdef _SC_XOPEN_XPG2 |
| 6725 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6726 | #endif |
| 6727 | #ifdef _SC_XOPEN_XPG3 |
| 6728 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6729 | #endif |
| 6730 | #ifdef _SC_XOPEN_XPG4 |
| 6731 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6732 | #endif |
| 6733 | }; |
| 6734 | |
| 6735 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6736 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6737 | { |
| 6738 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6739 | sizeof(posix_constants_sysconf) |
| 6740 | / sizeof(struct constdef)); |
| 6741 | } |
| 6742 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6743 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6744 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6745 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6746 | |
| 6747 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6748 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6749 | { |
| 6750 | PyObject *result = NULL; |
| 6751 | int name; |
| 6752 | |
| 6753 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6754 | int value; |
| 6755 | |
| 6756 | errno = 0; |
| 6757 | value = sysconf(name); |
| 6758 | if (value == -1 && errno != 0) |
| 6759 | posix_error(); |
| 6760 | else |
| 6761 | result = PyInt_FromLong(value); |
| 6762 | } |
| 6763 | return result; |
| 6764 | } |
| 6765 | #endif |
| 6766 | |
| 6767 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6768 | /* This code is used to ensure that the tables of configuration value names |
| 6769 | * are in sorted order as required by conv_confname(), and also to build the |
| 6770 | * the exported dictionaries that are used to publish information about the |
| 6771 | * names available on the host platform. |
| 6772 | * |
| 6773 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6774 | * when used, even for platforms we're not able to test on. It also makes |
| 6775 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6776 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6777 | |
| 6778 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6779 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6780 | { |
| 6781 | const struct constdef *c1 = |
| 6782 | (const struct constdef *) v1; |
| 6783 | const struct constdef *c2 = |
| 6784 | (const struct constdef *) v2; |
| 6785 | |
| 6786 | return strcmp(c1->name, c2->name); |
| 6787 | } |
| 6788 | |
| 6789 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6790 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6791 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6792 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6793 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6794 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6795 | |
| 6796 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6797 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6798 | if (d == NULL) |
| 6799 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6800 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6801 | for (i=0; i < tablesize; ++i) { |
| 6802 | PyObject *o = PyInt_FromLong(table[i].value); |
| 6803 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6804 | Py_XDECREF(o); |
| 6805 | Py_DECREF(d); |
| 6806 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6807 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6808 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6809 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6810 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6811 | } |
| 6812 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6813 | /* Return -1 on failure, 0 on success. */ |
| 6814 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6815 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6816 | { |
| 6817 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6818 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6819 | sizeof(posix_constants_pathconf) |
| 6820 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6821 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6822 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6823 | #endif |
| 6824 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6825 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6826 | sizeof(posix_constants_confstr) |
| 6827 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6828 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6829 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6830 | #endif |
| 6831 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6832 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6833 | sizeof(posix_constants_sysconf) |
| 6834 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6835 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6836 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6837 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6838 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6839 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6840 | |
| 6841 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6842 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6843 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6844 | 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] | 6845 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6846 | |
| 6847 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6848 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6849 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6850 | abort(); |
| 6851 | /*NOTREACHED*/ |
| 6852 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6853 | return NULL; |
| 6854 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6855 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6856 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6857 | PyDoc_STRVAR(win32_startfile__doc__, |
| 6858 | "startfile(filepath) - Start a file with its associated application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6859 | \n\ |
| 6860 | This acts like double-clicking the file in Explorer, or giving the file\n\ |
| 6861 | name as an argument to the DOS \"start\" command: the file is opened\n\ |
| 6862 | with whatever application (if any) its extension is associated.\n\ |
| 6863 | \n\ |
| 6864 | startfile returns as soon as the associated application is launched.\n\ |
| 6865 | There is no option to wait for the application to close, and no way\n\ |
| 6866 | to retrieve the application's exit status.\n\ |
| 6867 | \n\ |
| 6868 | The filepath is relative to the current directory. If you want to use\n\ |
| 6869 | 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] | 6870 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6871 | |
| 6872 | static PyObject * |
| 6873 | win32_startfile(PyObject *self, PyObject *args) |
| 6874 | { |
| 6875 | char *filepath; |
| 6876 | HINSTANCE rc; |
| 6877 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) |
| 6878 | return NULL; |
| 6879 | Py_BEGIN_ALLOW_THREADS |
| 6880 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); |
| 6881 | Py_END_ALLOW_THREADS |
| 6882 | if (rc <= (HINSTANCE)32) |
| 6883 | return win32_error("startfile", filepath); |
| 6884 | Py_INCREF(Py_None); |
| 6885 | return Py_None; |
| 6886 | } |
| 6887 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6888 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6889 | #ifdef HAVE_GETLOADAVG |
| 6890 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6891 | "getloadavg() -> (float, float, float)\n\n\ |
| 6892 | Return the number of processes in the system run queue averaged over\n\ |
| 6893 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6894 | was unobtainable"); |
| 6895 | |
| 6896 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6897 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6898 | { |
| 6899 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6900 | if (getloadavg(loadavg, 3)!=3) { |
| 6901 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6902 | return NULL; |
| 6903 | } else |
| 6904 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6905 | } |
| 6906 | #endif |
| 6907 | |
| 6908 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6909 | static PyMethodDef posix_methods[] = { |
| 6910 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 6911 | #ifdef HAVE_TTYNAME |
| 6912 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 6913 | #endif |
| 6914 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 6915 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6916 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6917 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6918 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 6919 | #ifdef HAVE_LCHOWN |
| 6920 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 6921 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 6922 | #ifdef HAVE_CHROOT |
| 6923 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 6924 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6925 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6926 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6927 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6928 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6929 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 6930 | #ifdef Py_USING_UNICODE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6931 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6932 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 6933 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6934 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6935 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6936 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6937 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 6938 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 6939 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6940 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6941 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6942 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6943 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6944 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6945 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6946 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 6947 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 6948 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 6949 | {"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] | 6950 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6951 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6952 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6953 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6954 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6955 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6956 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6957 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6958 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6959 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6960 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 6961 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 6962 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6963 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6964 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6965 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6966 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6967 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6968 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 6969 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6970 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6971 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6972 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 6973 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6974 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6975 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6976 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6977 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6978 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6979 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6980 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6981 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6982 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6983 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6984 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6985 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6986 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6987 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6988 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6989 | #endif /* HAVE_GETEGID */ |
| 6990 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6991 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6992 | #endif /* HAVE_GETEUID */ |
| 6993 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6994 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6995 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6996 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6997 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6998 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6999 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7000 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7001 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7002 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7003 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7004 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7005 | #endif /* HAVE_GETPPID */ |
| 7006 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7007 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7008 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7009 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7010 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7011 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7012 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7013 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7014 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7015 | #ifdef HAVE_KILLPG |
| 7016 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7017 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7018 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7019 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7020 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7021 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7022 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7023 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7024 | {"popen2", win32_popen2, METH_VARARGS}, |
| 7025 | {"popen3", win32_popen3, METH_VARARGS}, |
| 7026 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7027 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7028 | #else |
| 7029 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7030 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 7031 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 7032 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 7033 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7034 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7035 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7036 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7037 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7038 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7039 | #ifdef HAVE_SETEUID |
| 7040 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7041 | #endif /* HAVE_SETEUID */ |
| 7042 | #ifdef HAVE_SETEGID |
| 7043 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7044 | #endif /* HAVE_SETEGID */ |
| 7045 | #ifdef HAVE_SETREUID |
| 7046 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7047 | #endif /* HAVE_SETREUID */ |
| 7048 | #ifdef HAVE_SETREGID |
| 7049 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7050 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7051 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7052 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7053 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7054 | #ifdef HAVE_SETGROUPS |
| 7055 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 7056 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7057 | #ifdef HAVE_GETPGID |
| 7058 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7059 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7060 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7061 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7062 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7063 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7064 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7065 | #endif /* HAVE_WAIT */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7066 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7067 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7068 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7069 | #ifdef HAVE_GETSID |
| 7070 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 7071 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7072 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7073 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7074 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7075 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7076 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7077 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7078 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7079 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7080 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7081 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7082 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7083 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7084 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7085 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 7086 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7087 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7088 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7089 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7090 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7091 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 7092 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7093 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7094 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7095 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7096 | #endif |
| 7097 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7098 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7099 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7100 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7101 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7102 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7103 | #ifdef HAVE_DEVICE_MACROS |
| 7104 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7105 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7106 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7107 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7108 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7109 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7110 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7111 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7112 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7113 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7114 | #ifdef HAVE_UNSETENV |
| 7115 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7116 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7117 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7118 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7119 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7120 | #ifdef HAVE_FCHDIR |
| 7121 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7122 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7123 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7124 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7125 | #endif |
| 7126 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7127 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7128 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7129 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7130 | #ifdef WCOREDUMP |
| 7131 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7132 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7133 | #ifdef WIFCONTINUED |
| 7134 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7135 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7136 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7137 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7138 | #endif /* WIFSTOPPED */ |
| 7139 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7140 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7141 | #endif /* WIFSIGNALED */ |
| 7142 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7143 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7144 | #endif /* WIFEXITED */ |
| 7145 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7146 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7147 | #endif /* WEXITSTATUS */ |
| 7148 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7149 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7150 | #endif /* WTERMSIG */ |
| 7151 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7152 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7153 | #endif /* WSTOPSIG */ |
| 7154 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7155 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7156 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7157 | #endif |
| 7158 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7159 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7160 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 7161 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7162 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7163 | #endif |
| 7164 | #ifdef HAVE_TEMPNAM |
| 7165 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 7166 | #endif |
| 7167 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7168 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7169 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7170 | #ifdef HAVE_CONFSTR |
| 7171 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7172 | #endif |
| 7173 | #ifdef HAVE_SYSCONF |
| 7174 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7175 | #endif |
| 7176 | #ifdef HAVE_FPATHCONF |
| 7177 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7178 | #endif |
| 7179 | #ifdef HAVE_PATHCONF |
| 7180 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7181 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7182 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7183 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7184 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7185 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7186 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7187 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7188 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7189 | {NULL, NULL} /* Sentinel */ |
| 7190 | }; |
| 7191 | |
| 7192 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7193 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7194 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7195 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7196 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7197 | } |
| 7198 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7199 | #if defined(PYOS_OS2) |
| 7200 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7201 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7202 | { |
| 7203 | APIRET rc; |
| 7204 | ULONG values[QSV_MAX+1]; |
| 7205 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7206 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7207 | |
| 7208 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7209 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7210 | Py_END_ALLOW_THREADS |
| 7211 | |
| 7212 | if (rc != NO_ERROR) { |
| 7213 | os2_error(rc); |
| 7214 | return -1; |
| 7215 | } |
| 7216 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7217 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7218 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7219 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7220 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7221 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7222 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7223 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7224 | |
| 7225 | switch (values[QSV_VERSION_MINOR]) { |
| 7226 | case 0: ver = "2.00"; break; |
| 7227 | case 10: ver = "2.10"; break; |
| 7228 | case 11: ver = "2.11"; break; |
| 7229 | case 30: ver = "3.00"; break; |
| 7230 | case 40: ver = "4.00"; break; |
| 7231 | case 50: ver = "5.00"; break; |
| 7232 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7233 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7234 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7235 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7236 | ver = &tmp[0]; |
| 7237 | } |
| 7238 | |
| 7239 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7240 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7241 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7242 | |
| 7243 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7244 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7245 | tmp[1] = ':'; |
| 7246 | tmp[2] = '\0'; |
| 7247 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7248 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7249 | } |
| 7250 | #endif |
| 7251 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7252 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7253 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7254 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7255 | #ifdef F_OK |
| 7256 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7257 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7258 | #ifdef R_OK |
| 7259 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7260 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7261 | #ifdef W_OK |
| 7262 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7263 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7264 | #ifdef X_OK |
| 7265 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7266 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7267 | #ifdef NGROUPS_MAX |
| 7268 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7269 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7270 | #ifdef TMP_MAX |
| 7271 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7272 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7273 | #ifdef WCONTINUED |
| 7274 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7275 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7276 | #ifdef WNOHANG |
| 7277 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7278 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7279 | #ifdef WUNTRACED |
| 7280 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7281 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7282 | #ifdef O_RDONLY |
| 7283 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7284 | #endif |
| 7285 | #ifdef O_WRONLY |
| 7286 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7287 | #endif |
| 7288 | #ifdef O_RDWR |
| 7289 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7290 | #endif |
| 7291 | #ifdef O_NDELAY |
| 7292 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7293 | #endif |
| 7294 | #ifdef O_NONBLOCK |
| 7295 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7296 | #endif |
| 7297 | #ifdef O_APPEND |
| 7298 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7299 | #endif |
| 7300 | #ifdef O_DSYNC |
| 7301 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7302 | #endif |
| 7303 | #ifdef O_RSYNC |
| 7304 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7305 | #endif |
| 7306 | #ifdef O_SYNC |
| 7307 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7308 | #endif |
| 7309 | #ifdef O_NOCTTY |
| 7310 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7311 | #endif |
| 7312 | #ifdef O_CREAT |
| 7313 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7314 | #endif |
| 7315 | #ifdef O_EXCL |
| 7316 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7317 | #endif |
| 7318 | #ifdef O_TRUNC |
| 7319 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7320 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7321 | #ifdef O_BINARY |
| 7322 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7323 | #endif |
| 7324 | #ifdef O_TEXT |
| 7325 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7326 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7327 | #ifdef O_LARGEFILE |
| 7328 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7329 | #endif |
| 7330 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7331 | /* MS Windows */ |
| 7332 | #ifdef O_NOINHERIT |
| 7333 | /* Don't inherit in child processes. */ |
| 7334 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7335 | #endif |
| 7336 | #ifdef _O_SHORT_LIVED |
| 7337 | /* Optimize for short life (keep in memory). */ |
| 7338 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7339 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7340 | #endif |
| 7341 | #ifdef O_TEMPORARY |
| 7342 | /* Automatically delete when last handle is closed. */ |
| 7343 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7344 | #endif |
| 7345 | #ifdef O_RANDOM |
| 7346 | /* Optimize for random access. */ |
| 7347 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7348 | #endif |
| 7349 | #ifdef O_SEQUENTIAL |
| 7350 | /* Optimize for sequential access. */ |
| 7351 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7352 | #endif |
| 7353 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7354 | /* GNU extensions. */ |
| 7355 | #ifdef O_DIRECT |
| 7356 | /* Direct disk access. */ |
| 7357 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7358 | #endif |
| 7359 | #ifdef O_DIRECTORY |
| 7360 | /* Must be a directory. */ |
| 7361 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7362 | #endif |
| 7363 | #ifdef O_NOFOLLOW |
| 7364 | /* Do not follow links. */ |
| 7365 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7366 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7367 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7368 | /* These come from sysexits.h */ |
| 7369 | #ifdef EX_OK |
| 7370 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7371 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7372 | #ifdef EX_USAGE |
| 7373 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7374 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7375 | #ifdef EX_DATAERR |
| 7376 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7377 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7378 | #ifdef EX_NOINPUT |
| 7379 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7380 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7381 | #ifdef EX_NOUSER |
| 7382 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7383 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7384 | #ifdef EX_NOHOST |
| 7385 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7386 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7387 | #ifdef EX_UNAVAILABLE |
| 7388 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7389 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7390 | #ifdef EX_SOFTWARE |
| 7391 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7392 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7393 | #ifdef EX_OSERR |
| 7394 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7395 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7396 | #ifdef EX_OSFILE |
| 7397 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7398 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7399 | #ifdef EX_CANTCREAT |
| 7400 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7401 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7402 | #ifdef EX_IOERR |
| 7403 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7404 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7405 | #ifdef EX_TEMPFAIL |
| 7406 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7407 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7408 | #ifdef EX_PROTOCOL |
| 7409 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7410 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7411 | #ifdef EX_NOPERM |
| 7412 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7413 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7414 | #ifdef EX_CONFIG |
| 7415 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7416 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7417 | #ifdef EX_NOTFOUND |
| 7418 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7419 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7420 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7421 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7422 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7423 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7424 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7425 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7426 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7427 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7428 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7429 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7430 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7431 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7432 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7433 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7434 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7435 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7436 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7437 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7438 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7439 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7440 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7441 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7442 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7443 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7444 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7445 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7446 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7447 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7448 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7449 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7450 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7451 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7452 | #if defined(PYOS_OS2) |
| 7453 | if (insertvalues(d)) return -1; |
| 7454 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7455 | return 0; |
| 7456 | } |
| 7457 | |
| 7458 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7459 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7460 | #define INITFUNC initnt |
| 7461 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7462 | |
| 7463 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7464 | #define INITFUNC initos2 |
| 7465 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7466 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7467 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7468 | #define INITFUNC initposix |
| 7469 | #define MODNAME "posix" |
| 7470 | #endif |
| 7471 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7472 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7473 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7474 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7475 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7476 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7477 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7478 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7479 | posix__doc__); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7480 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7481 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7482 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7483 | Py_XINCREF(v); |
| 7484 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7485 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7486 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7487 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7488 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7489 | return; |
| 7490 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7491 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7492 | return; |
| 7493 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7494 | Py_INCREF(PyExc_OSError); |
| 7495 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7496 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7497 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7498 | if (posix_putenv_garbage == NULL) |
| 7499 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7500 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7501 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7502 | stat_result_desc.name = MODNAME ".stat_result"; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7503 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7504 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7505 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7506 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7507 | structseq_new = StatResultType.tp_new; |
| 7508 | StatResultType.tp_new = statresult_new; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7509 | Py_INCREF((PyObject*) &StatResultType); |
| 7510 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7511 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7512 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7513 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7514 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7515 | PyModule_AddObject(m, "statvfs_result", |
| 7516 | (PyObject*) &StatVFSResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7517 | } |