| Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ | 
|  | 3 |  | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2.  In that case the | 
|  | 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few | 
|  | 6 | functions are either unimplemented or implemented differently.  The source | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used.  Different compilers define their own feature | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'.  For OS/2, the compiler | 
|  | 10 | independent macro PYOS_OS2 should be defined.  On OS/2 the default | 
|  | 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP).  PYCC_GCC is used | 
|  | 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 14 | /* See also ../Dos/dosmodule.c */ | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 15 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 16 | #include "Python.h" | 
|  | 17 | #include "structseq.h" | 
|  | 18 |  | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 19 | #if defined(__VMS) | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 20 | #    include <unixio.h> | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 21 | #endif /* defined(__VMS) */ | 
|  | 22 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 23 | PyDoc_STRVAR(posix__doc__, | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 24 | "This module provides access to operating system functionality that is\n\ | 
|  | 25 | standardized by the C Standard and the POSIX standard (a thinly\n\ | 
|  | 26 | disguised Unix interface).  Refer to the library manual and\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 27 | corresponding Unix manual entries for more information on calls."); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 |  | 
| Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 29 | #ifndef Py_USING_UNICODE | 
|  | 30 | /* This is used in signatures of functions. */ | 
|  | 31 | #define Py_UNICODE void | 
|  | 32 | #endif | 
|  | 33 |  | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 34 | #if defined(PYOS_OS2) | 
|  | 35 | #define  INCL_DOS | 
|  | 36 | #define  INCL_DOSERRORS | 
|  | 37 | #define  INCL_DOSPROCESS | 
|  | 38 | #define  INCL_NOPMAPI | 
|  | 39 | #include <os2.h> | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 40 | #if defined(PYCC_GCC) | 
|  | 41 | #include <ctype.h> | 
|  | 42 | #include <io.h> | 
|  | 43 | #include <stdio.h> | 
|  | 44 | #include <process.h> | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 45 | #endif | 
| Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 46 | #include "osdefs.h" | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 47 | #endif | 
|  | 48 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 49 | #include <sys/types.h> | 
|  | 50 | #include <sys/stat.h> | 
| Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 51 |  | 
| Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 52 | #ifdef HAVE_SYS_WAIT_H | 
|  | 53 | #include <sys/wait.h>		/* For WNOHANG */ | 
|  | 54 | #endif | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 55 |  | 
| Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 56 | #include <signal.h> | 
| Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 57 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 58 | #ifdef HAVE_FCNTL_H | 
|  | 59 | #include <fcntl.h> | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 60 | #endif /* HAVE_FCNTL_H */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 61 |  | 
| Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 62 | #ifdef HAVE_GRP_H | 
|  | 63 | #include <grp.h> | 
|  | 64 | #endif | 
|  | 65 |  | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 66 | #ifdef HAVE_SYSEXITS_H | 
|  | 67 | #include <sysexits.h> | 
|  | 68 | #endif /* HAVE_SYSEXITS_H */ | 
|  | 69 |  | 
| 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 |  | 
| Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 895 | #ifdef MS_WINDOWS | 
|  | 896 |  | 
|  | 897 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, | 
|  | 898 | where / can be used in place of \ and the trailing slash is optional. | 
|  | 899 | Both SERVER and SHARE must have at least one character. | 
|  | 900 | */ | 
|  | 901 |  | 
|  | 902 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') | 
|  | 903 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') | 
|  | 904 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) | 
|  | 905 |  | 
|  | 906 | static BOOL | 
|  | 907 | IsUNCRootA(char *path, int pathlen) | 
|  | 908 | { | 
|  | 909 | #define ISSLASH ISSLASHA | 
|  | 910 |  | 
|  | 911 | int i, share; | 
|  | 912 |  | 
|  | 913 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) | 
|  | 914 | /* minimum UNCRoot is \\x\y */ | 
|  | 915 | return FALSE; | 
|  | 916 | for (i = 2; i < pathlen ; i++) | 
|  | 917 | if (ISSLASH(path[i])) break; | 
|  | 918 | if (i == 2 || i == pathlen) | 
|  | 919 | /* do not allow \\\SHARE or \\SERVER */ | 
|  | 920 | return FALSE; | 
|  | 921 | share = i+1; | 
|  | 922 | for (i = share; i < pathlen; i++) | 
|  | 923 | if (ISSLASH(path[i])) break; | 
|  | 924 | return (i != share && (i == pathlen || i == pathlen-1)); | 
|  | 925 |  | 
|  | 926 | #undef ISSLASH | 
|  | 927 | } | 
|  | 928 |  | 
|  | 929 | #ifdef Py_WIN_WIDE_FILENAMES | 
|  | 930 | static BOOL | 
|  | 931 | IsUNCRootW(Py_UNICODE *path, int pathlen) | 
|  | 932 | { | 
|  | 933 | #define ISSLASH ISSLASHW | 
|  | 934 |  | 
|  | 935 | int i, share; | 
|  | 936 |  | 
|  | 937 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) | 
|  | 938 | /* minimum UNCRoot is \\x\y */ | 
|  | 939 | return FALSE; | 
|  | 940 | for (i = 2; i < pathlen ; i++) | 
|  | 941 | if (ISSLASH(path[i])) break; | 
|  | 942 | if (i == 2 || i == pathlen) | 
|  | 943 | /* do not allow \\\SHARE or \\SERVER */ | 
|  | 944 | return FALSE; | 
|  | 945 | share = i+1; | 
|  | 946 | for (i = share; i < pathlen; i++) | 
|  | 947 | if (ISSLASH(path[i])) break; | 
|  | 948 | return (i != share && (i == pathlen || i == pathlen-1)); | 
|  | 949 |  | 
|  | 950 | #undef ISSLASH | 
|  | 951 | } | 
|  | 952 | #endif /* Py_WIN_WIDE_FILENAMES */ | 
|  | 953 | #endif /* MS_WINDOWS */ | 
|  | 954 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 955 | static PyObject * | 
| Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 956 | posix_do_stat(PyObject *self, PyObject *args, | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 957 | char *format, | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 958 | #ifdef __VMS | 
|  | 959 | int (*statfunc)(const char *, STRUCT_STAT *, ...), | 
|  | 960 | #else | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 961 | int (*statfunc)(const char *, STRUCT_STAT *), | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 962 | #endif | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 963 | char *wformat, | 
|  | 964 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 965 | { | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 966 | STRUCT_STAT st; | 
| Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 967 | char *path = NULL;	/* pass this to stat; do not free() it */ | 
|  | 968 | char *pathfree = NULL;  /* this memory must be free'd */ | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 969 | int res; | 
| Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 970 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 971 | #ifdef MS_WINDOWS | 
| Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 972 | int pathlen; | 
|  | 973 | char pathcopy[MAX_PATH]; | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 974 | #endif /* MS_WINDOWS */ | 
| Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 975 |  | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 976 |  | 
|  | 977 | #ifdef Py_WIN_WIDE_FILENAMES | 
|  | 978 | /* If on wide-character-capable OS see if argument | 
|  | 979 | is Unicode and if so use wide API.  */ | 
|  | 980 | if (unicode_file_names()) { | 
|  | 981 | PyUnicodeObject *po; | 
|  | 982 | if (PyArg_ParseTuple(args, wformat, &po)) { | 
|  | 983 | Py_UNICODE wpath[MAX_PATH+1]; | 
|  | 984 | pathlen = wcslen(PyUnicode_AS_UNICODE(po)); | 
|  | 985 | /* the library call can blow up if the file name is too long! */ | 
|  | 986 | if (pathlen > MAX_PATH) { | 
|  | 987 | errno = ENAMETOOLONG; | 
|  | 988 | return posix_error(); | 
|  | 989 | } | 
|  | 990 | wcscpy(wpath, PyUnicode_AS_UNICODE(po)); | 
|  | 991 | /* Remove trailing slash or backslash, unless it's the current | 
|  | 992 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). | 
|  | 993 | */ | 
| Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 994 | if (pathlen > 0) { | 
|  | 995 | if (ISSLASHW(wpath[pathlen-1])) { | 
|  | 996 | /* It does end with a slash -- exempt the root drive cases. */ | 
|  | 997 | if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':') || | 
|  | 998 | IsUNCRootW(wpath, pathlen)) | 
|  | 999 | /* leave it alone */; | 
|  | 1000 | else { | 
|  | 1001 | /* nuke the trailing backslash */ | 
|  | 1002 | wpath[pathlen-1] = L'\0'; | 
|  | 1003 | } | 
|  | 1004 | } | 
|  | 1005 | else if (ISSLASHW(wpath[1]) && pathlen < ARRAYSIZE(wpath)-1 && | 
|  | 1006 | IsUNCRootW(wpath, pathlen)) { | 
|  | 1007 | /* UNC root w/o trailing slash: add one when there's room */ | 
|  | 1008 | wpath[pathlen++] = L'\\'; | 
|  | 1009 | wpath[pathlen] = L'\0'; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1010 | } | 
|  | 1011 | } | 
|  | 1012 | Py_BEGIN_ALLOW_THREADS | 
|  | 1013 | /* PyUnicode_AS_UNICODE result OK without | 
|  | 1014 | thread lock as it is a simple dereference. */ | 
|  | 1015 | res = wstatfunc(wpath, &st); | 
|  | 1016 | Py_END_ALLOW_THREADS | 
|  | 1017 | if (res != 0) | 
|  | 1018 | return posix_error_with_unicode_filename(wpath); | 
|  | 1019 | return _pystat_fromstructstat(st); | 
|  | 1020 | } | 
|  | 1021 | /* Drop the argument parsing error as narrow strings | 
|  | 1022 | are also valid. */ | 
|  | 1023 | PyErr_Clear(); | 
|  | 1024 | } | 
|  | 1025 | #endif | 
|  | 1026 |  | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1027 | if (!PyArg_ParseTuple(args, format, | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1028 | Py_FileSystemDefaultEncoding, &path)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1029 | return NULL; | 
| Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1030 | pathfree = path; | 
| Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1031 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1032 | #ifdef MS_WINDOWS | 
| Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1033 | pathlen = strlen(path); | 
|  | 1034 | /* the library call can blow up if the file name is too long! */ | 
|  | 1035 | if (pathlen > MAX_PATH) { | 
| Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1036 | PyMem_Free(pathfree); | 
| Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1037 | errno = ENAMETOOLONG; | 
|  | 1038 | return posix_error(); | 
|  | 1039 | } | 
|  | 1040 |  | 
| Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1041 | /* Remove trailing slash or backslash, unless it's the current | 
|  | 1042 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). | 
|  | 1043 | */ | 
| Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1044 | if (pathlen > 0) { | 
|  | 1045 | if (ISSLASHA(path[pathlen-1])) { | 
|  | 1046 | /* It does end with a slash -- exempt the root drive cases. */ | 
|  | 1047 | if (pathlen == 1 || (pathlen == 3 && path[1] == ':') || | 
|  | 1048 | IsUNCRootA(path, pathlen)) | 
|  | 1049 | /* leave it alone */; | 
|  | 1050 | else { | 
|  | 1051 | /* nuke the trailing backslash */ | 
|  | 1052 | strncpy(pathcopy, path, pathlen); | 
|  | 1053 | pathcopy[pathlen-1] = '\0'; | 
|  | 1054 | path = pathcopy; | 
|  | 1055 | } | 
|  | 1056 | } | 
|  | 1057 | else if (ISSLASHA(path[1]) && pathlen < ARRAYSIZE(pathcopy)-1 && | 
|  | 1058 | IsUNCRootA(path, pathlen)) { | 
|  | 1059 | /* UNC root w/o trailing slash: add one when there's room */ | 
| Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1060 | strncpy(pathcopy, path, pathlen); | 
| Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1061 | pathcopy[pathlen++] = '\\'; | 
|  | 1062 | pathcopy[pathlen] = '\0'; | 
| Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1063 | path = pathcopy; | 
|  | 1064 | } | 
|  | 1065 | } | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1066 | #endif /* MS_WINDOWS */ | 
| Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1067 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1068 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1069 | res = (*statfunc)(path, &st); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1070 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1071 | if (res != 0) | 
| Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1072 | return posix_error_with_allocated_filename(pathfree); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1073 |  | 
| Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1074 | PyMem_Free(pathfree); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1075 | return _pystat_fromstructstat(st); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1076 | } | 
|  | 1077 |  | 
|  | 1078 |  | 
|  | 1079 | /* POSIX methods */ | 
|  | 1080 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1081 | PyDoc_STRVAR(posix_access__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1082 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ | 
| Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1083 | Use the real uid/gid to test for access to a path.  Note that most\n\ | 
|  | 1084 | operations will use the effective uid/gid, therefore this routine can\n\ | 
|  | 1085 | be used in a suid/sgid environment to test if the invoking user has the\n\ | 
|  | 1086 | specified access to the path.  The mode argument can be F_OK to test\n\ | 
|  | 1087 | 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] | 1088 |  | 
|  | 1089 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1090 | posix_access(PyObject *self, PyObject *args) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1091 | { | 
| Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1092 | char *path; | 
|  | 1093 | int mode; | 
|  | 1094 | int res; | 
|  | 1095 |  | 
| Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1096 | #ifdef Py_WIN_WIDE_FILENAMES | 
|  | 1097 | if (unicode_file_names()) { | 
|  | 1098 | PyUnicodeObject *po; | 
|  | 1099 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { | 
|  | 1100 | Py_BEGIN_ALLOW_THREADS | 
|  | 1101 | /* PyUnicode_AS_UNICODE OK without thread lock as | 
|  | 1102 | it is a simple dereference. */ | 
|  | 1103 | res = _waccess(PyUnicode_AS_UNICODE(po), mode); | 
|  | 1104 | Py_END_ALLOW_THREADS | 
|  | 1105 | return(PyBool_FromLong(res == 0)); | 
|  | 1106 | } | 
|  | 1107 | /* Drop the argument parsing error as narrow strings | 
|  | 1108 | are also valid. */ | 
|  | 1109 | PyErr_Clear(); | 
|  | 1110 | } | 
|  | 1111 | #endif | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1112 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) | 
| Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1113 | return NULL; | 
|  | 1114 | Py_BEGIN_ALLOW_THREADS | 
|  | 1115 | res = access(path, mode); | 
|  | 1116 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 674deb2 | 2002-09-01 15:06:28 +0000 | [diff] [blame] | 1117 | return(PyBool_FromLong(res == 0)); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1118 | } | 
|  | 1119 |  | 
| Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1120 | #ifndef F_OK | 
|  | 1121 | #define F_OK 0 | 
|  | 1122 | #endif | 
|  | 1123 | #ifndef R_OK | 
|  | 1124 | #define R_OK 4 | 
|  | 1125 | #endif | 
|  | 1126 | #ifndef W_OK | 
|  | 1127 | #define W_OK 2 | 
|  | 1128 | #endif | 
|  | 1129 | #ifndef X_OK | 
|  | 1130 | #define X_OK 1 | 
|  | 1131 | #endif | 
|  | 1132 |  | 
|  | 1133 | #ifdef HAVE_TTYNAME | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1134 | PyDoc_STRVAR(posix_ttyname__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1135 | "ttyname(fd) -> string\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1136 | Return the name of the terminal device connected to 'fd'."); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1137 |  | 
|  | 1138 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1139 | posix_ttyname(PyObject *self, PyObject *args) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1140 | { | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1141 | int id; | 
|  | 1142 | char *ret; | 
|  | 1143 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1144 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1145 | return NULL; | 
|  | 1146 |  | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1147 | #if defined(__VMS) | 
| Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1148 | /* file descriptor 0 only, the default input device (stdin) */ | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1149 | if (id == 0) { | 
|  | 1150 | ret = ttyname(); | 
|  | 1151 | } | 
|  | 1152 | else { | 
|  | 1153 | ret = NULL; | 
|  | 1154 | } | 
|  | 1155 | #else | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1156 | ret = ttyname(id); | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1157 | #endif | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1158 | if (ret == NULL) | 
|  | 1159 | return(posix_error()); | 
|  | 1160 | return(PyString_FromString(ret)); | 
|  | 1161 | } | 
| Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1162 | #endif | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1163 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1164 | #ifdef HAVE_CTERMID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1165 | PyDoc_STRVAR(posix_ctermid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1166 | "ctermid() -> string\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1167 | Return the name of the controlling terminal for this process."); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1168 |  | 
|  | 1169 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1170 | posix_ctermid(PyObject *self, PyObject *noargs) | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1171 | { | 
|  | 1172 | char *ret; | 
|  | 1173 | char buffer[L_ctermid]; | 
|  | 1174 |  | 
| Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1175 | #ifdef USE_CTERMID_R | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1176 | ret = ctermid_r(buffer); | 
|  | 1177 | #else | 
|  | 1178 | ret = ctermid(buffer); | 
|  | 1179 | #endif | 
|  | 1180 | if (ret == NULL) | 
|  | 1181 | return(posix_error()); | 
|  | 1182 | return(PyString_FromString(buffer)); | 
|  | 1183 | } | 
|  | 1184 | #endif | 
|  | 1185 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1186 | PyDoc_STRVAR(posix_chdir__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1187 | "chdir(path)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1188 | Change the current working directory to the specified path."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1189 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1190 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1191 | posix_chdir(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1192 | { | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1193 | #ifdef MS_WINDOWS | 
|  | 1194 | return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir); | 
|  | 1195 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) | 
|  | 1196 | return posix_1str(args, "et:chdir", _chdir2, NULL, NULL); | 
| Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1197 | #elif defined(__VMS) | 
| Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1198 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir, | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1199 | NULL, NULL); | 
|  | 1200 | #else | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1201 | return posix_1str(args, "et:chdir", chdir, NULL, NULL); | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1202 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1203 | } | 
|  | 1204 |  | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1205 | #ifdef HAVE_FCHDIR | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1206 | PyDoc_STRVAR(posix_fchdir__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1207 | "fchdir(fildes)\n\n\ | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1208 | 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] | 1209 | opened on a directory, not a file."); | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1210 |  | 
|  | 1211 | static PyObject * | 
|  | 1212 | posix_fchdir(PyObject *self, PyObject *fdobj) | 
|  | 1213 | { | 
|  | 1214 | return posix_fildes(fdobj, fchdir); | 
|  | 1215 | } | 
|  | 1216 | #endif /* HAVE_FCHDIR */ | 
|  | 1217 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1218 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1219 | PyDoc_STRVAR(posix_chmod__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1220 | "chmod(path, mode)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1221 | Change the access permissions of a file."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1222 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1223 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1224 | posix_chmod(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1225 | { | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1226 | char *path = NULL; | 
| Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1227 | int i; | 
|  | 1228 | int res; | 
| Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1229 | #ifdef Py_WIN_WIDE_FILENAMES | 
|  | 1230 | if (unicode_file_names()) { | 
|  | 1231 | PyUnicodeObject *po; | 
|  | 1232 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { | 
|  | 1233 | Py_BEGIN_ALLOW_THREADS | 
|  | 1234 | res = _wchmod(PyUnicode_AS_UNICODE(po), i); | 
|  | 1235 | Py_END_ALLOW_THREADS | 
|  | 1236 | if (res < 0) | 
|  | 1237 | return posix_error_with_unicode_filename( | 
|  | 1238 | PyUnicode_AS_UNICODE(po)); | 
|  | 1239 | Py_INCREF(Py_None); | 
|  | 1240 | return Py_None; | 
|  | 1241 | } | 
|  | 1242 | /* Drop the argument parsing error as narrow strings | 
|  | 1243 | are also valid. */ | 
|  | 1244 | PyErr_Clear(); | 
|  | 1245 | } | 
|  | 1246 | #endif /* Py_WIN_WIDE_FILENAMES */ | 
|  | 1247 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1248 | &path, &i)) | 
| Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1249 | return NULL; | 
|  | 1250 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1251 | res = chmod(path, i); | 
| Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1252 | Py_END_ALLOW_THREADS | 
|  | 1253 | if (res < 0) | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1254 | return posix_error_with_allocated_filename(path); | 
|  | 1255 | PyMem_Free(path); | 
| Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1256 | Py_INCREF(Py_None); | 
|  | 1257 | return Py_None; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1258 | } | 
|  | 1259 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1260 |  | 
| Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1261 | #ifdef HAVE_CHROOT | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1262 | PyDoc_STRVAR(posix_chroot__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1263 | "chroot(path)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1264 | Change root directory to path."); | 
| Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1265 |  | 
|  | 1266 | static PyObject * | 
|  | 1267 | posix_chroot(PyObject *self, PyObject *args) | 
|  | 1268 | { | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1269 | return posix_1str(args, "et:chroot", chroot, NULL, NULL); | 
| Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1270 | } | 
|  | 1271 | #endif | 
|  | 1272 |  | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1273 | #ifdef HAVE_FSYNC | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1274 | PyDoc_STRVAR(posix_fsync__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1275 | "fsync(fildes)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1276 | force write of file with filedescriptor to disk."); | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1277 |  | 
|  | 1278 | static PyObject * | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1279 | posix_fsync(PyObject *self, PyObject *fdobj) | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1280 | { | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1281 | return posix_fildes(fdobj, fsync); | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1282 | } | 
|  | 1283 | #endif /* HAVE_FSYNC */ | 
|  | 1284 |  | 
|  | 1285 | #ifdef HAVE_FDATASYNC | 
| Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1286 |  | 
| Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1287 | #ifdef __hpux | 
| Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1288 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ | 
|  | 1289 | #endif | 
|  | 1290 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1291 | PyDoc_STRVAR(posix_fdatasync__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1292 | "fdatasync(fildes)\n\n\ | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1293 | force write of file with filedescriptor to disk.\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1294 | does not force update of metadata."); | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1295 |  | 
|  | 1296 | static PyObject * | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1297 | posix_fdatasync(PyObject *self, PyObject *fdobj) | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1298 | { | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1299 | return posix_fildes(fdobj, fdatasync); | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1300 | } | 
|  | 1301 | #endif /* HAVE_FDATASYNC */ | 
|  | 1302 |  | 
|  | 1303 |  | 
| Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1304 | #ifdef HAVE_CHOWN | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1305 | PyDoc_STRVAR(posix_chown__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1306 | "chown(path, uid, gid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1307 | 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] | 1308 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1309 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1310 | posix_chown(PyObject *self, PyObject *args) | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1311 | { | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1312 | char *path = NULL; | 
| Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1313 | int uid, gid; | 
|  | 1314 | int res; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1315 | if (!PyArg_ParseTuple(args, "etii:chown", | 
|  | 1316 | Py_FileSystemDefaultEncoding, &path, | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1317 | &uid, &gid)) | 
| Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1318 | return NULL; | 
|  | 1319 | Py_BEGIN_ALLOW_THREADS | 
|  | 1320 | res = chown(path, (uid_t) uid, (gid_t) gid); | 
|  | 1321 | Py_END_ALLOW_THREADS | 
|  | 1322 | if (res < 0) | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1323 | return posix_error_with_allocated_filename(path); | 
|  | 1324 | PyMem_Free(path); | 
| Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1325 | Py_INCREF(Py_None); | 
|  | 1326 | return Py_None; | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1327 | } | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1328 | #endif /* HAVE_CHOWN */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1329 |  | 
| Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1330 | #ifdef HAVE_LCHOWN | 
|  | 1331 | PyDoc_STRVAR(posix_lchown__doc__, | 
|  | 1332 | "lchown(path, uid, gid)\n\n\ | 
|  | 1333 | Change the owner and group id of path to the numeric uid and gid.\n\ | 
|  | 1334 | This function will not follow symbolic links."); | 
|  | 1335 |  | 
|  | 1336 | static PyObject * | 
|  | 1337 | posix_lchown(PyObject *self, PyObject *args) | 
|  | 1338 | { | 
|  | 1339 | char *path = NULL; | 
|  | 1340 | int uid, gid; | 
|  | 1341 | int res; | 
|  | 1342 | if (!PyArg_ParseTuple(args, "etii:lchown", | 
|  | 1343 | Py_FileSystemDefaultEncoding, &path, | 
|  | 1344 | &uid, &gid)) | 
|  | 1345 | return NULL; | 
|  | 1346 | Py_BEGIN_ALLOW_THREADS | 
|  | 1347 | res = lchown(path, (uid_t) uid, (gid_t) gid); | 
|  | 1348 | Py_END_ALLOW_THREADS | 
| Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1349 | if (res < 0) | 
| Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1350 | return posix_error_with_allocated_filename(path); | 
|  | 1351 | PyMem_Free(path); | 
|  | 1352 | Py_INCREF(Py_None); | 
|  | 1353 | return Py_None; | 
|  | 1354 | } | 
|  | 1355 | #endif /* HAVE_LCHOWN */ | 
|  | 1356 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1357 |  | 
| Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1358 | #ifdef HAVE_GETCWD | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1359 | PyDoc_STRVAR(posix_getcwd__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1360 | "getcwd() -> path\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1361 | Return a string representing the current working directory."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1362 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1363 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1364 | posix_getcwd(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1365 | { | 
|  | 1366 | char buf[1026]; | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1367 | char *res; | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1368 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1369 | Py_BEGIN_ALLOW_THREADS | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1370 | #if defined(PYOS_OS2) && defined(PYCC_GCC) | 
|  | 1371 | res = _getcwd2(buf, sizeof buf); | 
| Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1372 | #else | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1373 | res = getcwd(buf, sizeof buf); | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1374 | #endif | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1375 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1376 | if (res == NULL) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1377 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1378 | return PyString_FromString(buf); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1379 | } | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1380 |  | 
| Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1381 | #ifdef Py_USING_UNICODE | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1382 | PyDoc_STRVAR(posix_getcwdu__doc__, | 
|  | 1383 | "getcwdu() -> path\n\n\ | 
|  | 1384 | Return a unicode string representing the current working directory."); | 
|  | 1385 |  | 
|  | 1386 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1387 | posix_getcwdu(PyObject *self, PyObject *noargs) | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1388 | { | 
|  | 1389 | char buf[1026]; | 
|  | 1390 | char *res; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1391 |  | 
|  | 1392 | #ifdef Py_WIN_WIDE_FILENAMES | 
|  | 1393 | if (unicode_file_names()) { | 
|  | 1394 | wchar_t *wres; | 
|  | 1395 | wchar_t wbuf[1026]; | 
|  | 1396 | Py_BEGIN_ALLOW_THREADS | 
|  | 1397 | wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]); | 
|  | 1398 | Py_END_ALLOW_THREADS | 
|  | 1399 | if (wres == NULL) | 
|  | 1400 | return posix_error(); | 
|  | 1401 | return PyUnicode_FromWideChar(wbuf, wcslen(wbuf)); | 
|  | 1402 | } | 
|  | 1403 | #endif | 
|  | 1404 |  | 
|  | 1405 | Py_BEGIN_ALLOW_THREADS | 
|  | 1406 | #if defined(PYOS_OS2) && defined(PYCC_GCC) | 
|  | 1407 | res = _getcwd2(buf, sizeof buf); | 
|  | 1408 | #else | 
|  | 1409 | res = getcwd(buf, sizeof buf); | 
|  | 1410 | #endif | 
|  | 1411 | Py_END_ALLOW_THREADS | 
|  | 1412 | if (res == NULL) | 
|  | 1413 | return posix_error(); | 
|  | 1414 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); | 
|  | 1415 | } | 
| Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1416 | #endif | 
| Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1417 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1418 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1419 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1420 | #ifdef HAVE_LINK | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1421 | PyDoc_STRVAR(posix_link__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1422 | "link(src, dst)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1423 | Create a hard link to a file."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1424 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1425 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1426 | posix_link(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1427 | { | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1428 | return posix_2str(args, "etet:link", link, NULL, NULL); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1429 | } | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1430 | #endif /* HAVE_LINK */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1431 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1432 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1433 | PyDoc_STRVAR(posix_listdir__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1434 | "listdir(path) -> list_of_strings\n\n\ | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1435 | Return a list containing the names of the entries in the directory.\n\ | 
|  | 1436 | \n\ | 
|  | 1437 | path: path of directory to list\n\ | 
|  | 1438 | \n\ | 
|  | 1439 | 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] | 1440 | entries '.' and '..' even if they are present in the directory."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1441 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1442 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1443 | posix_listdir(PyObject *self, PyObject *args) | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1444 | { | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1445 | /* XXX Should redo this putting the (now four) versions of opendir | 
| Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1446 | in separate files instead of having them all here... */ | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1447 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1448 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1449 | PyObject *d, *v; | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1450 | HANDLE hFindFile; | 
|  | 1451 | WIN32_FIND_DATA FileData; | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1452 | /* MAX_PATH characters could mean a bigger encoded string */ | 
|  | 1453 | char namebuf[MAX_PATH*2+5]; | 
|  | 1454 | char *bufptr = namebuf; | 
|  | 1455 | int len = sizeof(namebuf)/sizeof(namebuf[0]); | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1456 |  | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1457 | #ifdef Py_WIN_WIDE_FILENAMES | 
|  | 1458 | /* If on wide-character-capable OS see if argument | 
|  | 1459 | is Unicode and if so use wide API.  */ | 
|  | 1460 | if (unicode_file_names()) { | 
|  | 1461 | PyUnicodeObject *po; | 
|  | 1462 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { | 
|  | 1463 | WIN32_FIND_DATAW wFileData; | 
|  | 1464 | Py_UNICODE wnamebuf[MAX_PATH*2+5]; | 
|  | 1465 | Py_UNICODE wch; | 
|  | 1466 | wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH); | 
|  | 1467 | wnamebuf[MAX_PATH] = L'\0'; | 
|  | 1468 | len = wcslen(wnamebuf); | 
|  | 1469 | wch = (len > 0) ? wnamebuf[len-1] : L'\0'; | 
|  | 1470 | if (wch != L'/' && wch != L'\\' && wch != L':') | 
|  | 1471 | wnamebuf[len++] = L'/'; | 
|  | 1472 | wcscpy(wnamebuf + len, L"*.*"); | 
|  | 1473 | if ((d = PyList_New(0)) == NULL) | 
|  | 1474 | return NULL; | 
|  | 1475 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); | 
|  | 1476 | if (hFindFile == INVALID_HANDLE_VALUE) { | 
|  | 1477 | errno = GetLastError(); | 
|  | 1478 | if (errno == ERROR_FILE_NOT_FOUND) { | 
|  | 1479 | return d; | 
|  | 1480 | } | 
|  | 1481 | Py_DECREF(d); | 
|  | 1482 | return win32_error_unicode("FindFirstFileW", wnamebuf); | 
|  | 1483 | } | 
|  | 1484 | do { | 
|  | 1485 | if (wFileData.cFileName[0] == L'.' && | 
|  | 1486 | (wFileData.cFileName[1] == L'\0' || | 
|  | 1487 | wFileData.cFileName[1] == L'.' && | 
|  | 1488 | wFileData.cFileName[2] == L'\0')) | 
|  | 1489 | continue; | 
|  | 1490 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); | 
|  | 1491 | if (v == NULL) { | 
|  | 1492 | Py_DECREF(d); | 
|  | 1493 | d = NULL; | 
|  | 1494 | break; | 
|  | 1495 | } | 
|  | 1496 | if (PyList_Append(d, v) != 0) { | 
|  | 1497 | Py_DECREF(v); | 
|  | 1498 | Py_DECREF(d); | 
|  | 1499 | d = NULL; | 
|  | 1500 | break; | 
|  | 1501 | } | 
|  | 1502 | Py_DECREF(v); | 
|  | 1503 | } while (FindNextFileW(hFindFile, &wFileData) == TRUE); | 
|  | 1504 |  | 
|  | 1505 | if (FindClose(hFindFile) == FALSE) { | 
|  | 1506 | Py_DECREF(d); | 
|  | 1507 | return win32_error_unicode("FindClose", wnamebuf); | 
|  | 1508 | } | 
|  | 1509 | return d; | 
|  | 1510 | } | 
|  | 1511 | /* Drop the argument parsing error as narrow strings | 
|  | 1512 | are also valid. */ | 
|  | 1513 | PyErr_Clear(); | 
|  | 1514 | } | 
|  | 1515 | #endif | 
|  | 1516 |  | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1517 | if (!PyArg_ParseTuple(args, "et#:listdir", | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1518 | Py_FileSystemDefaultEncoding, &bufptr, &len)) | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1519 | return NULL; | 
| Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1520 | if (len > 0) { | 
|  | 1521 | char ch = namebuf[len-1]; | 
|  | 1522 | if (ch != SEP && ch != ALTSEP && ch != ':') | 
|  | 1523 | namebuf[len++] = '/'; | 
|  | 1524 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1525 | strcpy(namebuf + len, "*.*"); | 
|  | 1526 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1527 | if ((d = PyList_New(0)) == NULL) | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1528 | return NULL; | 
|  | 1529 |  | 
|  | 1530 | hFindFile = FindFirstFile(namebuf, &FileData); | 
|  | 1531 | if (hFindFile == INVALID_HANDLE_VALUE) { | 
|  | 1532 | errno = GetLastError(); | 
| Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 1533 | if (errno == ERROR_FILE_NOT_FOUND) | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1534 | return d; | 
|  | 1535 | Py_DECREF(d); | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1536 | return win32_error("FindFirstFile", namebuf); | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1537 | } | 
|  | 1538 | do { | 
| Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1539 | if (FileData.cFileName[0] == '.' && | 
|  | 1540 | (FileData.cFileName[1] == '\0' || | 
|  | 1541 | FileData.cFileName[1] == '.' && | 
|  | 1542 | FileData.cFileName[2] == '\0')) | 
|  | 1543 | continue; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1544 | v = PyString_FromString(FileData.cFileName); | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1545 | if (v == NULL) { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1546 | Py_DECREF(d); | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1547 | d = NULL; | 
|  | 1548 | break; | 
|  | 1549 | } | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1550 | if (PyList_Append(d, v) != 0) { | 
|  | 1551 | Py_DECREF(v); | 
|  | 1552 | Py_DECREF(d); | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1553 | d = NULL; | 
|  | 1554 | break; | 
|  | 1555 | } | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1556 | Py_DECREF(v); | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1557 | } while (FindNextFile(hFindFile, &FileData) == TRUE); | 
|  | 1558 |  | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1559 | if (FindClose(hFindFile) == FALSE) { | 
|  | 1560 | Py_DECREF(d); | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1561 | return win32_error("FindClose", namebuf); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1562 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1563 |  | 
|  | 1564 | return d; | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1565 |  | 
| Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1566 | #elif defined(PYOS_OS2) | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1567 |  | 
|  | 1568 | #ifndef MAX_PATH | 
|  | 1569 | #define MAX_PATH    CCHMAXPATH | 
|  | 1570 | #endif | 
|  | 1571 | char *name, *pt; | 
|  | 1572 | int len; | 
|  | 1573 | PyObject *d, *v; | 
|  | 1574 | char namebuf[MAX_PATH+5]; | 
|  | 1575 | HDIR  hdir = 1; | 
|  | 1576 | ULONG srchcnt = 1; | 
|  | 1577 | FILEFINDBUF3   ep; | 
|  | 1578 | APIRET rc; | 
|  | 1579 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1580 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1581 | return NULL; | 
|  | 1582 | if (len >= MAX_PATH) { | 
|  | 1583 | PyErr_SetString(PyExc_ValueError, "path too long"); | 
|  | 1584 | return NULL; | 
|  | 1585 | } | 
|  | 1586 | strcpy(namebuf, name); | 
|  | 1587 | for (pt = namebuf; *pt; pt++) | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1588 | if (*pt == ALTSEP) | 
|  | 1589 | *pt = SEP; | 
|  | 1590 | if (namebuf[len-1] != SEP) | 
|  | 1591 | namebuf[len++] = SEP; | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1592 | strcpy(namebuf + len, "*.*"); | 
|  | 1593 |  | 
|  | 1594 | if ((d = PyList_New(0)) == NULL) | 
|  | 1595 | return NULL; | 
|  | 1596 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1597 | rc = DosFindFirst(namebuf,         /* Wildcard Pattern to Match */ | 
|  | 1598 | &hdir,           /* Handle to Use While Search Directory */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1599 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1600 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ | 
|  | 1601 | &srchcnt,        /* Max and Actual Count of Entries Per Iteration */ | 
|  | 1602 | FIL_STANDARD);   /* Format of Entry (EAs or Not) */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1603 |  | 
|  | 1604 | if (rc != NO_ERROR) { | 
|  | 1605 | errno = ENOENT; | 
| Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1606 | return posix_error_with_filename(name); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1607 | } | 
|  | 1608 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1609 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1610 | do { | 
|  | 1611 | if (ep.achName[0] == '.' | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1612 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1613 | continue; /* Skip Over "." and ".." Names */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1614 |  | 
|  | 1615 | strcpy(namebuf, ep.achName); | 
|  | 1616 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1617 | /* Leave Case of Name Alone -- In Native Form */ | 
|  | 1618 | /* (Removed Forced Lowercasing Code) */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1619 |  | 
|  | 1620 | v = PyString_FromString(namebuf); | 
|  | 1621 | if (v == NULL) { | 
|  | 1622 | Py_DECREF(d); | 
|  | 1623 | d = NULL; | 
|  | 1624 | break; | 
|  | 1625 | } | 
|  | 1626 | if (PyList_Append(d, v) != 0) { | 
|  | 1627 | Py_DECREF(v); | 
|  | 1628 | Py_DECREF(d); | 
|  | 1629 | d = NULL; | 
|  | 1630 | break; | 
|  | 1631 | } | 
|  | 1632 | Py_DECREF(v); | 
|  | 1633 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); | 
|  | 1634 | } | 
|  | 1635 |  | 
|  | 1636 | return d; | 
|  | 1637 | #else | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1638 |  | 
| Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1639 | char *name = NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1640 | PyObject *d, *v; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1641 | DIR *dirp; | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1642 | struct dirent *ep; | 
| Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1643 | int arg_is_unicode = 1; | 
|  | 1644 |  | 
|  | 1645 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { | 
|  | 1646 | arg_is_unicode = 0; | 
|  | 1647 | PyErr_Clear(); | 
|  | 1648 | } | 
|  | 1649 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1650 | return NULL; | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1651 | if ((dirp = opendir(name)) == NULL) { | 
| Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1652 | return posix_error_with_allocated_filename(name); | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1653 | } | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1654 | if ((d = PyList_New(0)) == NULL) { | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1655 | closedir(dirp); | 
| Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1656 | PyMem_Free(name); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1657 | return NULL; | 
|  | 1658 | } | 
|  | 1659 | while ((ep = readdir(dirp)) != NULL) { | 
| Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1660 | if (ep->d_name[0] == '.' && | 
|  | 1661 | (NAMLEN(ep) == 1 || | 
| Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1662 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) | 
| Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1663 | continue; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1664 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1665 | if (v == NULL) { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1666 | Py_DECREF(d); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1667 | d = NULL; | 
|  | 1668 | break; | 
|  | 1669 | } | 
| Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1670 | #ifdef Py_USING_UNICODE | 
| Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1671 | if (arg_is_unicode) { | 
| Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1672 | PyObject *w; | 
|  | 1673 |  | 
|  | 1674 | w = PyUnicode_FromEncodedObject(v, | 
| Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1675 | Py_FileSystemDefaultEncoding, | 
| Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1676 | "strict"); | 
| Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 1677 | if (w != NULL) { | 
|  | 1678 | Py_DECREF(v); | 
|  | 1679 | v = w; | 
|  | 1680 | } | 
|  | 1681 | else { | 
|  | 1682 | /* fall back to the original byte string, as | 
|  | 1683 | discussed in patch #683592 */ | 
|  | 1684 | PyErr_Clear(); | 
| Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1685 | } | 
| Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1686 | } | 
|  | 1687 | #endif | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1688 | if (PyList_Append(d, v) != 0) { | 
|  | 1689 | Py_DECREF(v); | 
|  | 1690 | Py_DECREF(d); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1691 | d = NULL; | 
|  | 1692 | break; | 
|  | 1693 | } | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1694 | Py_DECREF(v); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1695 | } | 
|  | 1696 | closedir(dirp); | 
| Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1697 | PyMem_Free(name); | 
| Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1698 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1699 | return d; | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1700 |  | 
| Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1701 | #endif /* which OS */ | 
|  | 1702 | }  /* end of posix_listdir */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1703 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1704 | #ifdef MS_WINDOWS | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1705 | /* A helper function for abspath on win32 */ | 
|  | 1706 | static PyObject * | 
|  | 1707 | posix__getfullpathname(PyObject *self, PyObject *args) | 
|  | 1708 | { | 
|  | 1709 | /* assume encoded strings wont more than double no of chars */ | 
|  | 1710 | char inbuf[MAX_PATH*2]; | 
|  | 1711 | char *inbufp = inbuf; | 
|  | 1712 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); | 
|  | 1713 | char outbuf[MAX_PATH*2]; | 
|  | 1714 | char *temp; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1715 | #ifdef Py_WIN_WIDE_FILENAMES | 
|  | 1716 | if (unicode_file_names()) { | 
|  | 1717 | PyUnicodeObject *po; | 
|  | 1718 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { | 
|  | 1719 | Py_UNICODE woutbuf[MAX_PATH*2]; | 
|  | 1720 | Py_UNICODE *wtemp; | 
| Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1721 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1722 | sizeof(woutbuf)/sizeof(woutbuf[0]), | 
|  | 1723 | woutbuf, &wtemp)) | 
|  | 1724 | return win32_error("GetFullPathName", ""); | 
|  | 1725 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); | 
|  | 1726 | } | 
|  | 1727 | /* Drop the argument parsing error as narrow strings | 
|  | 1728 | are also valid. */ | 
|  | 1729 | PyErr_Clear(); | 
|  | 1730 | } | 
|  | 1731 | #endif | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1732 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", | 
|  | 1733 | Py_FileSystemDefaultEncoding, &inbufp, | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1734 | &insize)) | 
|  | 1735 | return NULL; | 
|  | 1736 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), | 
|  | 1737 | outbuf, &temp)) | 
|  | 1738 | return win32_error("GetFullPathName", inbuf); | 
|  | 1739 | return PyString_FromString(outbuf); | 
|  | 1740 | } /* end of posix__getfullpathname */ | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1741 | #endif /* MS_WINDOWS */ | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1742 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1743 | PyDoc_STRVAR(posix_mkdir__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1744 | "mkdir(path [, mode=0777])\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1745 | Create a directory."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1746 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1747 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1748 | posix_mkdir(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1749 | { | 
| Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1750 | int res; | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1751 | char *path = NULL; | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1752 | int mode = 0777; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1753 |  | 
|  | 1754 | #ifdef Py_WIN_WIDE_FILENAMES | 
|  | 1755 | if (unicode_file_names()) { | 
|  | 1756 | PyUnicodeObject *po; | 
| Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 1757 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1758 | Py_BEGIN_ALLOW_THREADS | 
|  | 1759 | /* PyUnicode_AS_UNICODE OK without thread lock as | 
|  | 1760 | it is a simple dereference. */ | 
|  | 1761 | res = _wmkdir(PyUnicode_AS_UNICODE(po)); | 
|  | 1762 | Py_END_ALLOW_THREADS | 
|  | 1763 | if (res < 0) | 
|  | 1764 | return posix_error(); | 
|  | 1765 | Py_INCREF(Py_None); | 
|  | 1766 | return Py_None; | 
|  | 1767 | } | 
|  | 1768 | /* Drop the argument parsing error as narrow strings | 
|  | 1769 | are also valid. */ | 
|  | 1770 | PyErr_Clear(); | 
|  | 1771 | } | 
|  | 1772 | #endif | 
|  | 1773 |  | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1774 | if (!PyArg_ParseTuple(args, "et|i:mkdir", | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1775 | Py_FileSystemDefaultEncoding, &path, &mode)) | 
| Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1776 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1777 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1778 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1779 | res = mkdir(path); | 
|  | 1780 | #else | 
| Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1781 | res = mkdir(path, mode); | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1782 | #endif | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1783 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1784 | if (res < 0) | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1785 | return posix_error_with_allocated_filename(path); | 
|  | 1786 | PyMem_Free(path); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1787 | Py_INCREF(Py_None); | 
|  | 1788 | return Py_None; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1789 | } | 
|  | 1790 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1791 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1792 | #ifdef HAVE_NICE | 
| Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1793 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) | 
|  | 1794 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) | 
|  | 1795 | #include <sys/resource.h> | 
|  | 1796 | #endif | 
|  | 1797 | #endif | 
|  | 1798 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1799 | PyDoc_STRVAR(posix_nice__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1800 | "nice(inc) -> new_priority\n\n\ | 
|  | 1801 | 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] | 1802 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1803 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1804 | posix_nice(PyObject *self, PyObject *args) | 
| Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1805 | { | 
|  | 1806 | int increment, value; | 
|  | 1807 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1808 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) | 
| Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1809 | return NULL; | 
| Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1810 |  | 
|  | 1811 | /* There are two flavours of 'nice': one that returns the new | 
|  | 1812 | priority (as required by almost all standards out there) and the | 
| Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1813 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices | 
|  | 1814 | the use of getpriority() to get the new priority. | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1815 |  | 
| Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1816 | If we are of the nice family that returns the new priority, we | 
|  | 1817 | need to clear errno before the call, and check if errno is filled | 
|  | 1818 | before calling posix_error() on a returnvalue of -1, because the | 
|  | 1819 | -1 may be the actual new priority! */ | 
|  | 1820 |  | 
|  | 1821 | errno = 0; | 
| Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1822 | value = nice(increment); | 
| Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1823 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) | 
| Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1824 | if (value == 0) | 
|  | 1825 | value = getpriority(PRIO_PROCESS, 0); | 
|  | 1826 | #endif | 
|  | 1827 | if (value == -1 && errno != 0) | 
|  | 1828 | /* either nice() or getpriority() returned an error */ | 
| Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1829 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1830 | return PyInt_FromLong((long) value); | 
| Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1831 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1832 | #endif /* HAVE_NICE */ | 
| Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1833 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1834 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1835 | PyDoc_STRVAR(posix_rename__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1836 | "rename(old, new)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1837 | Rename a file or directory."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1838 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1839 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1840 | posix_rename(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1841 | { | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1842 | #ifdef MS_WINDOWS | 
|  | 1843 | return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename); | 
|  | 1844 | #else | 
|  | 1845 | return posix_2str(args, "etet:rename", rename, NULL, NULL); | 
|  | 1846 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1847 | } | 
|  | 1848 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1849 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1850 | PyDoc_STRVAR(posix_rmdir__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1851 | "rmdir(path)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1852 | Remove a directory."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1853 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1854 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1855 | posix_rmdir(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1856 | { | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1857 | #ifdef MS_WINDOWS | 
|  | 1858 | return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir); | 
|  | 1859 | #else | 
|  | 1860 | return posix_1str(args, "et:rmdir", rmdir, NULL, NULL); | 
|  | 1861 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1862 | } | 
|  | 1863 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1864 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1865 | PyDoc_STRVAR(posix_stat__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1866 | "stat(path) -> stat result\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1867 | Perform a stat system call on the given path."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1868 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1869 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1870 | posix_stat(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1871 | { | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1872 | #ifdef MS_WINDOWS | 
|  | 1873 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64); | 
|  | 1874 | #else | 
|  | 1875 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); | 
|  | 1876 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1877 | } | 
|  | 1878 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1879 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1880 | #ifdef HAVE_SYSTEM | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1881 | PyDoc_STRVAR(posix_system__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1882 | "system(command) -> exit_status\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1883 | Execute the command (a string) in a subshell."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1884 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1885 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1886 | posix_system(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1887 | { | 
| Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1888 | char *command; | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1889 | long sts; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1890 | if (!PyArg_ParseTuple(args, "s:system", &command)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1891 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1892 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1893 | sts = system(command); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1894 | Py_END_ALLOW_THREADS | 
|  | 1895 | return PyInt_FromLong(sts); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1896 | } | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1897 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1898 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1899 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1900 | PyDoc_STRVAR(posix_umask__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1901 | "umask(new_mask) -> old_mask\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1902 | Set the current numeric umask and return the previous umask."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1903 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1904 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1905 | posix_umask(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1906 | { | 
|  | 1907 | int i; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1908 | if (!PyArg_ParseTuple(args, "i:umask", &i)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1909 | return NULL; | 
| Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 1910 | i = (int)umask(i); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1911 | if (i < 0) | 
|  | 1912 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1913 | return PyInt_FromLong((long)i); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1914 | } | 
|  | 1915 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1916 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1917 | PyDoc_STRVAR(posix_unlink__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1918 | "unlink(path)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1919 | Remove a file (same as remove(path))."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1920 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1921 | PyDoc_STRVAR(posix_remove__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1922 | "remove(path)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1923 | Remove a file (same as unlink(path))."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1924 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1925 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1926 | posix_unlink(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1927 | { | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1928 | #ifdef MS_WINDOWS | 
|  | 1929 | return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink); | 
|  | 1930 | #else | 
|  | 1931 | return posix_1str(args, "et:remove", unlink, NULL, NULL); | 
|  | 1932 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1933 | } | 
|  | 1934 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1935 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1936 | #ifdef HAVE_UNAME | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1937 | PyDoc_STRVAR(posix_uname__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1938 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1939 | Return a tuple identifying the current operating system."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1940 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1941 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1942 | posix_uname(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1943 | { | 
| Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1944 | struct utsname u; | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1945 | int res; | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1946 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1947 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1948 | res = uname(&u); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1949 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1950 | if (res < 0) | 
| Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1951 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1952 | return Py_BuildValue("(sssss)", | 
| Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1953 | u.sysname, | 
|  | 1954 | u.nodename, | 
|  | 1955 | u.release, | 
|  | 1956 | u.version, | 
|  | 1957 | u.machine); | 
| Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1958 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1959 | #endif /* HAVE_UNAME */ | 
| Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1960 |  | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1961 | static int | 
|  | 1962 | extract_time(PyObject *t, long* sec, long* usec) | 
|  | 1963 | { | 
|  | 1964 | long intval; | 
|  | 1965 | if (PyFloat_Check(t)) { | 
|  | 1966 | double tval = PyFloat_AsDouble(t); | 
|  | 1967 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); | 
|  | 1968 | if (!intobj) | 
|  | 1969 | return -1; | 
|  | 1970 | intval = PyInt_AsLong(intobj); | 
|  | 1971 | Py_DECREF(intobj); | 
|  | 1972 | *sec = intval; | 
| Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 1973 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1974 | if (*usec < 0) | 
|  | 1975 | /* If rounding gave us a negative number, | 
|  | 1976 | truncate.  */ | 
|  | 1977 | *usec = 0; | 
|  | 1978 | return 0; | 
|  | 1979 | } | 
|  | 1980 | intval = PyInt_AsLong(t); | 
|  | 1981 | if (intval == -1 && PyErr_Occurred()) | 
|  | 1982 | return -1; | 
|  | 1983 | *sec = intval; | 
|  | 1984 | *usec = 0; | 
| Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 1985 | return 0; | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1986 | } | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1987 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1988 | PyDoc_STRVAR(posix_utime__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1989 | "utime(path, (atime, utime))\n\ | 
|  | 1990 | utime(path, None)\n\n\ | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1991 | 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] | 1992 | 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] | 1993 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1994 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1995 | posix_utime(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1996 | { | 
| Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1997 | char *path; | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1998 | long atime, mtime, ausec, musec; | 
| Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1999 | int res; | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2000 | PyObject* arg; | 
| Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2001 |  | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2002 | #if defined(HAVE_UTIMES) | 
|  | 2003 | struct timeval buf[2]; | 
|  | 2004 | #define ATIME buf[0].tv_sec | 
|  | 2005 | #define MTIME buf[1].tv_sec | 
|  | 2006 | #elif defined(HAVE_UTIME_H) | 
| Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2007 | /* XXX should define struct utimbuf instead, above */ | 
| Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2008 | struct utimbuf buf; | 
|  | 2009 | #define ATIME buf.actime | 
|  | 2010 | #define MTIME buf.modtime | 
|  | 2011 | #define UTIME_ARG &buf | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2012 | #else /* HAVE_UTIMES */ | 
| Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2013 | time_t buf[2]; | 
|  | 2014 | #define ATIME buf[0] | 
|  | 2015 | #define MTIME buf[1] | 
|  | 2016 | #define UTIME_ARG buf | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2017 | #endif /* HAVE_UTIMES */ | 
| Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2018 |  | 
| Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2019 | int have_unicode_filename = 0; | 
|  | 2020 | #ifdef Py_WIN_WIDE_FILENAMES | 
|  | 2021 | PyUnicodeObject *obwpath; | 
|  | 2022 | wchar_t *wpath; | 
|  | 2023 | if (unicode_file_names()) { | 
|  | 2024 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { | 
|  | 2025 | wpath = PyUnicode_AS_UNICODE(obwpath); | 
|  | 2026 | have_unicode_filename = 1; | 
|  | 2027 | } else | 
|  | 2028 | /* Drop the argument parsing error as narrow strings | 
|  | 2029 | are also valid. */ | 
|  | 2030 | PyErr_Clear(); | 
|  | 2031 | } | 
|  | 2032 | #endif /* Py_WIN_WIDE_FILENAMES */ | 
|  | 2033 |  | 
|  | 2034 | if (!have_unicode_filename && \ | 
| Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2035 | !PyArg_ParseTuple(args, "etO:utime", | 
|  | 2036 | Py_FileSystemDefaultEncoding, &path, &arg)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2037 | return NULL; | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2038 | if (arg == Py_None) { | 
|  | 2039 | /* optional time values not given */ | 
|  | 2040 | Py_BEGIN_ALLOW_THREADS | 
| Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2041 | #ifdef Py_WIN_WIDE_FILENAMES | 
|  | 2042 | if (have_unicode_filename) | 
|  | 2043 | res = _wutime(wpath, NULL); | 
|  | 2044 | else | 
|  | 2045 | #endif /* Py_WIN_WIDE_FILENAMES */ | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2046 | res = utime(path, NULL); | 
|  | 2047 | Py_END_ALLOW_THREADS | 
|  | 2048 | } | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2049 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2050 | PyErr_SetString(PyExc_TypeError, | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2051 | "utime() arg 2 must be a tuple (atime, mtime)"); | 
| Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame^] | 2052 | PyMem_Free(path); | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2053 | return NULL; | 
|  | 2054 | } | 
|  | 2055 | else { | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2056 | if (extract_time(PyTuple_GET_ITEM(arg, 0), | 
| Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame^] | 2057 | &atime, &ausec) == -1) { | 
|  | 2058 | PyMem_Free(path); | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2059 | return NULL; | 
| Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame^] | 2060 | } | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2061 | if (extract_time(PyTuple_GET_ITEM(arg, 1), | 
| Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame^] | 2062 | &mtime, &musec) == -1) { | 
|  | 2063 | PyMem_Free(path); | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2064 | return NULL; | 
| Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame^] | 2065 | } | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2066 | ATIME = atime; | 
|  | 2067 | MTIME = mtime; | 
| Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2068 | #ifdef HAVE_UTIMES | 
|  | 2069 | buf[0].tv_usec = ausec; | 
|  | 2070 | buf[1].tv_usec = musec; | 
|  | 2071 | Py_BEGIN_ALLOW_THREADS | 
|  | 2072 | res = utimes(path, buf); | 
|  | 2073 | Py_END_ALLOW_THREADS | 
|  | 2074 | #else | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2075 | Py_BEGIN_ALLOW_THREADS | 
| Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2076 | #ifdef Py_WIN_WIDE_FILENAMES | 
|  | 2077 | if (have_unicode_filename) | 
|  | 2078 | /* utime is OK with utimbuf, but _wutime insists | 
|  | 2079 | on _utimbuf (the msvc headers assert the | 
|  | 2080 | underscore version is ansi) */ | 
|  | 2081 | res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG); | 
|  | 2082 | else | 
|  | 2083 | #endif /* Py_WIN_WIDE_FILENAMES */ | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2084 | res = utime(path, UTIME_ARG); | 
|  | 2085 | Py_END_ALLOW_THREADS | 
| Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2086 | #endif /* HAVE_UTIMES */ | 
| Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2087 | } | 
| Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2088 | if (res < 0) { | 
|  | 2089 | #ifdef Py_WIN_WIDE_FILENAMES | 
| Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame^] | 2090 | if (have_unicode_filename) { | 
|  | 2091 | PyMem_Free(path); | 
| Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2092 | return posix_error_with_unicode_filename(wpath); | 
| Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame^] | 2093 | } | 
| Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2094 | #endif /* Py_WIN_WIDE_FILENAMES */ | 
| Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame^] | 2095 | return posix_error_with_allocated_filename(path); | 
| Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2096 | } | 
| Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame^] | 2097 | PyMem_Free(path); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2098 | Py_INCREF(Py_None); | 
|  | 2099 | return Py_None; | 
| Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2100 | #undef UTIME_ARG | 
|  | 2101 | #undef ATIME | 
|  | 2102 | #undef MTIME | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2103 | } | 
|  | 2104 |  | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2105 |  | 
| Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2106 | /* Process operations */ | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2107 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2108 | PyDoc_STRVAR(posix__exit__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2109 | "_exit(status)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2110 | Exit to the system with specified status, without normal exit processing."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2111 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2112 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2113 | posix__exit(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2114 | { | 
|  | 2115 | int sts; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2116 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2117 | return NULL; | 
|  | 2118 | _exit(sts); | 
| Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2119 | return NULL; /* Make gcc -Wall happy */ | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2120 | } | 
|  | 2121 |  | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2122 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) | 
|  | 2123 | static void | 
|  | 2124 | free_string_array(char **array, int count) | 
|  | 2125 | { | 
|  | 2126 | int i; | 
|  | 2127 | for (i = 0; i < count; i++) | 
|  | 2128 | PyMem_Free(array[i]); | 
|  | 2129 | PyMem_DEL(array); | 
|  | 2130 | } | 
|  | 2131 | #endif | 
|  | 2132 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2133 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2134 | #ifdef HAVE_EXECV | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2135 | PyDoc_STRVAR(posix_execv__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2136 | "execv(path, args)\n\n\ | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2137 | Execute an executable path with arguments, replacing current process.\n\ | 
|  | 2138 | \n\ | 
|  | 2139 | path: path of executable file\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2140 | args: tuple or list of strings"); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2141 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2142 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2143 | posix_execv(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2144 | { | 
| Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2145 | char *path; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2146 | PyObject *argv; | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2147 | char **argvlist; | 
|  | 2148 | int i, argc; | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2149 | PyObject *(*getitem)(PyObject *, int); | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2150 |  | 
| Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2151 | /* execv has two arguments: (path, argv), where | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2152 | argv is a list or tuple of strings. */ | 
|  | 2153 |  | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2154 | if (!PyArg_ParseTuple(args, "etO:execv", | 
|  | 2155 | Py_FileSystemDefaultEncoding, | 
|  | 2156 | &path, &argv)) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2157 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2158 | if (PyList_Check(argv)) { | 
|  | 2159 | argc = PyList_Size(argv); | 
|  | 2160 | getitem = PyList_GetItem; | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2161 | } | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2162 | else if (PyTuple_Check(argv)) { | 
|  | 2163 | argc = PyTuple_Size(argv); | 
|  | 2164 | getitem = PyTuple_GetItem; | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2165 | } | 
| Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2166 | else { | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2167 | 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] | 2168 | PyMem_Free(path); | 
| Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2169 | return NULL; | 
|  | 2170 | } | 
|  | 2171 |  | 
|  | 2172 | if (argc == 0) { | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2173 | 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] | 2174 | PyMem_Free(path); | 
| Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2175 | return NULL; | 
|  | 2176 | } | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2177 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2178 | argvlist = PyMem_NEW(char *, argc+1); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2179 | if (argvlist == NULL) { | 
|  | 2180 | PyMem_Free(path); | 
| Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2181 | return PyErr_NoMemory(); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2182 | } | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2183 | for (i = 0; i < argc; i++) { | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2184 | if (!PyArg_Parse((*getitem)(argv, i), "et", | 
|  | 2185 | Py_FileSystemDefaultEncoding, | 
|  | 2186 | &argvlist[i])) { | 
|  | 2187 | free_string_array(argvlist, i); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2188 | PyErr_SetString(PyExc_TypeError, | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2189 | "execv() arg 2 must contain only strings"); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2190 | PyMem_Free(path); | 
| Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2191 | return NULL; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2192 |  | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2193 | } | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2194 | } | 
|  | 2195 | argvlist[argc] = NULL; | 
|  | 2196 |  | 
| Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2197 | execv(path, argvlist); | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2198 |  | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2199 | /* If we get here it's definitely an error */ | 
|  | 2200 |  | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2201 | free_string_array(argvlist, argc); | 
|  | 2202 | PyMem_Free(path); | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2203 | return posix_error(); | 
|  | 2204 | } | 
|  | 2205 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2206 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2207 | PyDoc_STRVAR(posix_execve__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2208 | "execve(path, args, env)\n\n\ | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2209 | Execute a path with arguments and environment, replacing current process.\n\ | 
|  | 2210 | \n\ | 
|  | 2211 | path: path of executable file\n\ | 
|  | 2212 | args: tuple or list of arguments\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2213 | env: dictionary of strings mapping to strings"); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2214 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2215 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2216 | posix_execve(PyObject *self, PyObject *args) | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2217 | { | 
|  | 2218 | char *path; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2219 | PyObject *argv, *env; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2220 | char **argvlist; | 
|  | 2221 | char **envlist; | 
| Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2222 | PyObject *key, *val, *keys=NULL, *vals=NULL; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2223 | int i, pos, argc, envc; | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2224 | PyObject *(*getitem)(PyObject *, int); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2225 | int lastarg = 0; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2226 |  | 
|  | 2227 | /* execve has three arguments: (path, argv, env), where | 
|  | 2228 | argv is a list or tuple of strings and env is a dictionary | 
|  | 2229 | like posix.environ. */ | 
|  | 2230 |  | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2231 | if (!PyArg_ParseTuple(args, "etOO:execve", | 
|  | 2232 | Py_FileSystemDefaultEncoding, | 
|  | 2233 | &path, &argv, &env)) | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2234 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2235 | if (PyList_Check(argv)) { | 
|  | 2236 | argc = PyList_Size(argv); | 
|  | 2237 | getitem = PyList_GetItem; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2238 | } | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2239 | else if (PyTuple_Check(argv)) { | 
|  | 2240 | argc = PyTuple_Size(argv); | 
|  | 2241 | getitem = PyTuple_GetItem; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2242 | } | 
|  | 2243 | else { | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2244 | PyErr_SetString(PyExc_TypeError, | 
|  | 2245 | "execve() arg 2 must be a tuple or list"); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2246 | goto fail_0; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2247 | } | 
| Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2248 | if (!PyMapping_Check(env)) { | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2249 | PyErr_SetString(PyExc_TypeError, | 
|  | 2250 | "execve() arg 3 must be a mapping object"); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2251 | goto fail_0; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2252 | } | 
|  | 2253 |  | 
| Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2254 | if (argc == 0) { | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2255 | PyErr_SetString(PyExc_ValueError, | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2256 | "execve() arg 2 must not be empty"); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2257 | goto fail_0; | 
| Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2258 | } | 
|  | 2259 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2260 | argvlist = PyMem_NEW(char *, argc+1); | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2261 | if (argvlist == NULL) { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2262 | PyErr_NoMemory(); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2263 | goto fail_0; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2264 | } | 
|  | 2265 | for (i = 0; i < argc; i++) { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2266 | if (!PyArg_Parse((*getitem)(argv, i), | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2267 | "et;execve() arg 2 must contain only strings", | 
| Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2268 | Py_FileSystemDefaultEncoding, | 
| Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2269 | &argvlist[i])) | 
|  | 2270 | { | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2271 | lastarg = i; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2272 | goto fail_1; | 
|  | 2273 | } | 
|  | 2274 | } | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2275 | lastarg = argc; | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2276 | argvlist[argc] = NULL; | 
|  | 2277 |  | 
| Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2278 | i = PyMapping_Size(env); | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2279 | if (i < 0) | 
|  | 2280 | goto fail_1; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2281 | envlist = PyMem_NEW(char *, i + 1); | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2282 | if (envlist == NULL) { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2283 | PyErr_NoMemory(); | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2284 | goto fail_1; | 
|  | 2285 | } | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2286 | envc = 0; | 
| Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2287 | keys = PyMapping_Keys(env); | 
|  | 2288 | vals = PyMapping_Values(env); | 
|  | 2289 | if (!keys || !vals) | 
|  | 2290 | goto fail_2; | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2291 | if (!PyList_Check(keys) || !PyList_Check(vals)) { | 
|  | 2292 | PyErr_SetString(PyExc_TypeError, | 
|  | 2293 | "execve(): env.keys() or env.values() is not a list"); | 
|  | 2294 | goto fail_2; | 
|  | 2295 | } | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2296 |  | 
| Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2297 | for (pos = 0; pos < i; pos++) { | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2298 | char *p, *k, *v; | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2299 | size_t len; | 
| Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2300 |  | 
|  | 2301 | key = PyList_GetItem(keys, pos); | 
|  | 2302 | val = PyList_GetItem(vals, pos); | 
|  | 2303 | if (!key || !val) | 
|  | 2304 | goto fail_2; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2305 |  | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2306 | if (!PyArg_Parse( | 
|  | 2307 | key, | 
|  | 2308 | "s;execve() arg 3 contains a non-string key", | 
|  | 2309 | &k) || | 
|  | 2310 | !PyArg_Parse( | 
|  | 2311 | val, | 
|  | 2312 | "s;execve() arg 3 contains a non-string value", | 
|  | 2313 | &v)) | 
| Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2314 | { | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2315 | goto fail_2; | 
|  | 2316 | } | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2317 |  | 
|  | 2318 | #if defined(PYOS_OS2) | 
|  | 2319 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ | 
|  | 2320 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { | 
|  | 2321 | #endif | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2322 | len = PyString_Size(key) + PyString_Size(val) + 2; | 
|  | 2323 | p = PyMem_NEW(char, len); | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2324 | if (p == NULL) { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2325 | PyErr_NoMemory(); | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2326 | goto fail_2; | 
|  | 2327 | } | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2328 | PyOS_snprintf(p, len, "%s=%s", k, v); | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2329 | envlist[envc++] = p; | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2330 | #if defined(PYOS_OS2) | 
|  | 2331 | } | 
|  | 2332 | #endif | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2333 | } | 
|  | 2334 | envlist[envc] = 0; | 
|  | 2335 |  | 
|  | 2336 | execve(path, argvlist, envlist); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2337 |  | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2338 | /* If we get here it's definitely an error */ | 
|  | 2339 |  | 
|  | 2340 | (void) posix_error(); | 
|  | 2341 |  | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2342 | fail_2: | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2343 | while (--envc >= 0) | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2344 | PyMem_DEL(envlist[envc]); | 
|  | 2345 | PyMem_DEL(envlist); | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2346 | fail_1: | 
|  | 2347 | free_string_array(argvlist, lastarg); | 
| Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2348 | Py_XDECREF(vals); | 
|  | 2349 | Py_XDECREF(keys); | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2350 | fail_0: | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2351 | PyMem_Free(path); | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2352 | return NULL; | 
|  | 2353 | } | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2354 | #endif /* HAVE_EXECV */ | 
| Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2355 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2356 |  | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2357 | #ifdef HAVE_SPAWNV | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2358 | PyDoc_STRVAR(posix_spawnv__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2359 | "spawnv(mode, path, args)\n\n\ | 
| Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2360 | Execute the program 'path' in a new process.\n\ | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2361 | \n\ | 
| Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2362 | mode: mode of process creation\n\ | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2363 | path: path of executable file\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2364 | args: tuple or list of strings"); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2365 |  | 
|  | 2366 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2367 | posix_spawnv(PyObject *self, PyObject *args) | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2368 | { | 
|  | 2369 | char *path; | 
|  | 2370 | PyObject *argv; | 
|  | 2371 | char **argvlist; | 
|  | 2372 | int mode, i, argc; | 
| 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); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2375 |  | 
|  | 2376 | /* spawnv has three arguments: (mode, path, argv), where | 
|  | 2377 | argv is a list or tuple of strings. */ | 
|  | 2378 |  | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2379 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, | 
|  | 2380 | Py_FileSystemDefaultEncoding, | 
|  | 2381 | &path, &argv)) | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2382 | return NULL; | 
|  | 2383 | if (PyList_Check(argv)) { | 
|  | 2384 | argc = PyList_Size(argv); | 
|  | 2385 | getitem = PyList_GetItem; | 
|  | 2386 | } | 
|  | 2387 | else if (PyTuple_Check(argv)) { | 
|  | 2388 | argc = PyTuple_Size(argv); | 
|  | 2389 | getitem = PyTuple_GetItem; | 
|  | 2390 | } | 
|  | 2391 | else { | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2392 | PyErr_SetString(PyExc_TypeError, | 
|  | 2393 | "spawnv() arg 2 must be a tuple or list"); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2394 | PyMem_Free(path); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2395 | return NULL; | 
|  | 2396 | } | 
|  | 2397 |  | 
|  | 2398 | argvlist = PyMem_NEW(char *, argc+1); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2399 | if (argvlist == NULL) { | 
|  | 2400 | PyMem_Free(path); | 
| Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2401 | return PyErr_NoMemory(); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2402 | } | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2403 | for (i = 0; i < argc; i++) { | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2404 | if (!PyArg_Parse((*getitem)(argv, i), "et", | 
|  | 2405 | Py_FileSystemDefaultEncoding, | 
|  | 2406 | &argvlist[i])) { | 
|  | 2407 | free_string_array(argvlist, i); | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2408 | PyErr_SetString( | 
|  | 2409 | PyExc_TypeError, | 
|  | 2410 | "spawnv() arg 2 must contain only strings"); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2411 | PyMem_Free(path); | 
| Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2412 | return NULL; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2413 | } | 
|  | 2414 | } | 
|  | 2415 | argvlist[argc] = NULL; | 
|  | 2416 |  | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2417 | #if defined(PYOS_OS2) && defined(PYCC_GCC) | 
|  | 2418 | Py_BEGIN_ALLOW_THREADS | 
|  | 2419 | spawnval = spawnv(mode, path, argvlist); | 
|  | 2420 | Py_END_ALLOW_THREADS | 
|  | 2421 | #else | 
| Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2422 | if (mode == _OLD_P_OVERLAY) | 
|  | 2423 | mode = _P_OVERLAY; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2424 |  | 
| Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2425 | Py_BEGIN_ALLOW_THREADS | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2426 | spawnval = _spawnv(mode, path, argvlist); | 
| Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2427 | Py_END_ALLOW_THREADS | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2428 | #endif | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2429 |  | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2430 | free_string_array(argvlist, argc); | 
|  | 2431 | PyMem_Free(path); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2432 |  | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2433 | if (spawnval == -1) | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2434 | return posix_error(); | 
|  | 2435 | else | 
| Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2436 | #if SIZEOF_LONG == SIZEOF_VOID_P | 
|  | 2437 | return Py_BuildValue("l", (long) spawnval); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2438 | #else | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2439 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2440 | #endif | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2441 | } | 
|  | 2442 |  | 
|  | 2443 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2444 | PyDoc_STRVAR(posix_spawnve__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2445 | "spawnve(mode, path, args, env)\n\n\ | 
| Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2446 | Execute the program 'path' in a new process.\n\ | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2447 | \n\ | 
| Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2448 | mode: mode of process creation\n\ | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2449 | path: path of executable file\n\ | 
|  | 2450 | args: tuple or list of arguments\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2451 | env: dictionary of strings mapping to strings"); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2452 |  | 
|  | 2453 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2454 | posix_spawnve(PyObject *self, PyObject *args) | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2455 | { | 
|  | 2456 | char *path; | 
|  | 2457 | PyObject *argv, *env; | 
|  | 2458 | char **argvlist; | 
|  | 2459 | char **envlist; | 
|  | 2460 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; | 
|  | 2461 | int mode, i, pos, argc, envc; | 
| Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2462 | Py_intptr_t spawnval; | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2463 | PyObject *(*getitem)(PyObject *, int); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2464 | int lastarg = 0; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2465 |  | 
|  | 2466 | /* spawnve has four arguments: (mode, path, argv, env), where | 
|  | 2467 | argv is a list or tuple of strings and env is a dictionary | 
|  | 2468 | like posix.environ. */ | 
|  | 2469 |  | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2470 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, | 
|  | 2471 | Py_FileSystemDefaultEncoding, | 
|  | 2472 | &path, &argv, &env)) | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2473 | return NULL; | 
|  | 2474 | if (PyList_Check(argv)) { | 
|  | 2475 | argc = PyList_Size(argv); | 
|  | 2476 | getitem = PyList_GetItem; | 
|  | 2477 | } | 
|  | 2478 | else if (PyTuple_Check(argv)) { | 
|  | 2479 | argc = PyTuple_Size(argv); | 
|  | 2480 | getitem = PyTuple_GetItem; | 
|  | 2481 | } | 
|  | 2482 | else { | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2483 | PyErr_SetString(PyExc_TypeError, | 
|  | 2484 | "spawnve() arg 2 must be a tuple or list"); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2485 | goto fail_0; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2486 | } | 
|  | 2487 | if (!PyMapping_Check(env)) { | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2488 | PyErr_SetString(PyExc_TypeError, | 
|  | 2489 | "spawnve() arg 3 must be a mapping object"); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2490 | goto fail_0; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2491 | } | 
|  | 2492 |  | 
|  | 2493 | argvlist = PyMem_NEW(char *, argc+1); | 
|  | 2494 | if (argvlist == NULL) { | 
|  | 2495 | PyErr_NoMemory(); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2496 | goto fail_0; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2497 | } | 
|  | 2498 | for (i = 0; i < argc; i++) { | 
|  | 2499 | if (!PyArg_Parse((*getitem)(argv, i), | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2500 | "et;spawnve() arg 2 must contain only strings", | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2501 | Py_FileSystemDefaultEncoding, | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2502 | &argvlist[i])) | 
|  | 2503 | { | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2504 | lastarg = i; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2505 | goto fail_1; | 
|  | 2506 | } | 
|  | 2507 | } | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2508 | lastarg = argc; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2509 | argvlist[argc] = NULL; | 
|  | 2510 |  | 
| Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2511 | i = PyMapping_Size(env); | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2512 | if (i < 0) | 
|  | 2513 | goto fail_1; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2514 | envlist = PyMem_NEW(char *, i + 1); | 
|  | 2515 | if (envlist == NULL) { | 
|  | 2516 | PyErr_NoMemory(); | 
|  | 2517 | goto fail_1; | 
|  | 2518 | } | 
|  | 2519 | envc = 0; | 
|  | 2520 | keys = PyMapping_Keys(env); | 
|  | 2521 | vals = PyMapping_Values(env); | 
|  | 2522 | if (!keys || !vals) | 
|  | 2523 | goto fail_2; | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2524 | if (!PyList_Check(keys) || !PyList_Check(vals)) { | 
|  | 2525 | PyErr_SetString(PyExc_TypeError, | 
|  | 2526 | "spawnve(): env.keys() or env.values() is not a list"); | 
|  | 2527 | goto fail_2; | 
|  | 2528 | } | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2529 |  | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2530 | for (pos = 0; pos < i; pos++) { | 
|  | 2531 | char *p, *k, *v; | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2532 | size_t len; | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2533 |  | 
|  | 2534 | key = PyList_GetItem(keys, pos); | 
|  | 2535 | val = PyList_GetItem(vals, pos); | 
|  | 2536 | if (!key || !val) | 
|  | 2537 | goto fail_2; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2538 |  | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2539 | if (!PyArg_Parse( | 
|  | 2540 | key, | 
|  | 2541 | "s;spawnve() arg 3 contains a non-string key", | 
|  | 2542 | &k) || | 
|  | 2543 | !PyArg_Parse( | 
|  | 2544 | val, | 
|  | 2545 | "s;spawnve() arg 3 contains a non-string value", | 
|  | 2546 | &v)) | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2547 | { | 
|  | 2548 | goto fail_2; | 
|  | 2549 | } | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2550 | len = PyString_Size(key) + PyString_Size(val) + 2; | 
|  | 2551 | p = PyMem_NEW(char, len); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2552 | if (p == NULL) { | 
|  | 2553 | PyErr_NoMemory(); | 
|  | 2554 | goto fail_2; | 
|  | 2555 | } | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2556 | PyOS_snprintf(p, len, "%s=%s", k, v); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2557 | envlist[envc++] = p; | 
|  | 2558 | } | 
|  | 2559 | envlist[envc] = 0; | 
|  | 2560 |  | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2561 | #if defined(PYOS_OS2) && defined(PYCC_GCC) | 
|  | 2562 | Py_BEGIN_ALLOW_THREADS | 
|  | 2563 | spawnval = spawnve(mode, path, argvlist, envlist); | 
|  | 2564 | Py_END_ALLOW_THREADS | 
|  | 2565 | #else | 
| Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2566 | if (mode == _OLD_P_OVERLAY) | 
|  | 2567 | mode = _P_OVERLAY; | 
| Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2568 |  | 
|  | 2569 | Py_BEGIN_ALLOW_THREADS | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2570 | spawnval = _spawnve(mode, path, argvlist, envlist); | 
| Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2571 | Py_END_ALLOW_THREADS | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2572 | #endif | 
| Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2573 |  | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2574 | if (spawnval == -1) | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2575 | (void) posix_error(); | 
|  | 2576 | else | 
| Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2577 | #if SIZEOF_LONG == SIZEOF_VOID_P | 
|  | 2578 | res = Py_BuildValue("l", (long) spawnval); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2579 | #else | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2580 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2581 | #endif | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2582 |  | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2583 | fail_2: | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2584 | while (--envc >= 0) | 
|  | 2585 | PyMem_DEL(envlist[envc]); | 
|  | 2586 | PyMem_DEL(envlist); | 
| Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2587 | fail_1: | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2588 | free_string_array(argvlist, lastarg); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2589 | Py_XDECREF(vals); | 
|  | 2590 | Py_XDECREF(keys); | 
| Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2591 | fail_0: | 
|  | 2592 | PyMem_Free(path); | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2593 | return res; | 
|  | 2594 | } | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 2595 |  | 
|  | 2596 | /* OS/2 supports spawnvp & spawnvpe natively */ | 
|  | 2597 | #if defined(PYOS_OS2) | 
|  | 2598 | PyDoc_STRVAR(posix_spawnvp__doc__, | 
|  | 2599 | "spawnvp(mode, file, args)\n\n\ | 
|  | 2600 | Execute the program 'file' in a new process, using the environment\n\ | 
|  | 2601 | search path to find the file.\n\ | 
|  | 2602 | \n\ | 
|  | 2603 | mode: mode of process creation\n\ | 
|  | 2604 | file: executable file name\n\ | 
|  | 2605 | args: tuple or list of strings"); | 
|  | 2606 |  | 
|  | 2607 | static PyObject * | 
|  | 2608 | posix_spawnvp(PyObject *self, PyObject *args) | 
|  | 2609 | { | 
|  | 2610 | char *path; | 
|  | 2611 | PyObject *argv; | 
|  | 2612 | char **argvlist; | 
|  | 2613 | int mode, i, argc; | 
|  | 2614 | Py_intptr_t spawnval; | 
|  | 2615 | PyObject *(*getitem)(PyObject *, int); | 
|  | 2616 |  | 
|  | 2617 | /* spawnvp has three arguments: (mode, path, argv), where | 
|  | 2618 | argv is a list or tuple of strings. */ | 
|  | 2619 |  | 
|  | 2620 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, | 
|  | 2621 | Py_FileSystemDefaultEncoding, | 
|  | 2622 | &path, &argv)) | 
|  | 2623 | return NULL; | 
|  | 2624 | if (PyList_Check(argv)) { | 
|  | 2625 | argc = PyList_Size(argv); | 
|  | 2626 | getitem = PyList_GetItem; | 
|  | 2627 | } | 
|  | 2628 | else if (PyTuple_Check(argv)) { | 
|  | 2629 | argc = PyTuple_Size(argv); | 
|  | 2630 | getitem = PyTuple_GetItem; | 
|  | 2631 | } | 
|  | 2632 | else { | 
|  | 2633 | PyErr_SetString(PyExc_TypeError, | 
|  | 2634 | "spawnvp() arg 2 must be a tuple or list"); | 
|  | 2635 | PyMem_Free(path); | 
|  | 2636 | return NULL; | 
|  | 2637 | } | 
|  | 2638 |  | 
|  | 2639 | argvlist = PyMem_NEW(char *, argc+1); | 
|  | 2640 | if (argvlist == NULL) { | 
|  | 2641 | PyMem_Free(path); | 
|  | 2642 | return PyErr_NoMemory(); | 
|  | 2643 | } | 
|  | 2644 | for (i = 0; i < argc; i++) { | 
|  | 2645 | if (!PyArg_Parse((*getitem)(argv, i), "et", | 
|  | 2646 | Py_FileSystemDefaultEncoding, | 
|  | 2647 | &argvlist[i])) { | 
|  | 2648 | free_string_array(argvlist, i); | 
|  | 2649 | PyErr_SetString( | 
|  | 2650 | PyExc_TypeError, | 
|  | 2651 | "spawnvp() arg 2 must contain only strings"); | 
|  | 2652 | PyMem_Free(path); | 
|  | 2653 | return NULL; | 
|  | 2654 | } | 
|  | 2655 | } | 
|  | 2656 | argvlist[argc] = NULL; | 
|  | 2657 |  | 
|  | 2658 | Py_BEGIN_ALLOW_THREADS | 
|  | 2659 | #if defined(PYCC_GCC) | 
|  | 2660 | spawnval = spawnvp(mode, path, argvlist); | 
|  | 2661 | #else | 
|  | 2662 | spawnval = _spawnvp(mode, path, argvlist); | 
|  | 2663 | #endif | 
|  | 2664 | Py_END_ALLOW_THREADS | 
|  | 2665 |  | 
|  | 2666 | free_string_array(argvlist, argc); | 
|  | 2667 | PyMem_Free(path); | 
|  | 2668 |  | 
|  | 2669 | if (spawnval == -1) | 
|  | 2670 | return posix_error(); | 
|  | 2671 | else | 
|  | 2672 | return Py_BuildValue("l", (long) spawnval); | 
|  | 2673 | } | 
|  | 2674 |  | 
|  | 2675 |  | 
|  | 2676 | PyDoc_STRVAR(posix_spawnvpe__doc__, | 
|  | 2677 | "spawnvpe(mode, file, args, env)\n\n\ | 
|  | 2678 | Execute the program 'file' in a new process, using the environment\n\ | 
|  | 2679 | search path to find the file.\n\ | 
|  | 2680 | \n\ | 
|  | 2681 | mode: mode of process creation\n\ | 
|  | 2682 | file: executable file name\n\ | 
|  | 2683 | args: tuple or list of arguments\n\ | 
|  | 2684 | env: dictionary of strings mapping to strings"); | 
|  | 2685 |  | 
|  | 2686 | static PyObject * | 
|  | 2687 | posix_spawnvpe(PyObject *self, PyObject *args) | 
|  | 2688 | { | 
|  | 2689 | char *path; | 
|  | 2690 | PyObject *argv, *env; | 
|  | 2691 | char **argvlist; | 
|  | 2692 | char **envlist; | 
|  | 2693 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; | 
|  | 2694 | int mode, i, pos, argc, envc; | 
|  | 2695 | Py_intptr_t spawnval; | 
|  | 2696 | PyObject *(*getitem)(PyObject *, int); | 
|  | 2697 | int lastarg = 0; | 
|  | 2698 |  | 
|  | 2699 | /* spawnvpe has four arguments: (mode, path, argv, env), where | 
|  | 2700 | argv is a list or tuple of strings and env is a dictionary | 
|  | 2701 | like posix.environ. */ | 
|  | 2702 |  | 
|  | 2703 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, | 
|  | 2704 | Py_FileSystemDefaultEncoding, | 
|  | 2705 | &path, &argv, &env)) | 
|  | 2706 | return NULL; | 
|  | 2707 | if (PyList_Check(argv)) { | 
|  | 2708 | argc = PyList_Size(argv); | 
|  | 2709 | getitem = PyList_GetItem; | 
|  | 2710 | } | 
|  | 2711 | else if (PyTuple_Check(argv)) { | 
|  | 2712 | argc = PyTuple_Size(argv); | 
|  | 2713 | getitem = PyTuple_GetItem; | 
|  | 2714 | } | 
|  | 2715 | else { | 
|  | 2716 | PyErr_SetString(PyExc_TypeError, | 
|  | 2717 | "spawnvpe() arg 2 must be a tuple or list"); | 
|  | 2718 | goto fail_0; | 
|  | 2719 | } | 
|  | 2720 | if (!PyMapping_Check(env)) { | 
|  | 2721 | PyErr_SetString(PyExc_TypeError, | 
|  | 2722 | "spawnvpe() arg 3 must be a mapping object"); | 
|  | 2723 | goto fail_0; | 
|  | 2724 | } | 
|  | 2725 |  | 
|  | 2726 | argvlist = PyMem_NEW(char *, argc+1); | 
|  | 2727 | if (argvlist == NULL) { | 
|  | 2728 | PyErr_NoMemory(); | 
|  | 2729 | goto fail_0; | 
|  | 2730 | } | 
|  | 2731 | for (i = 0; i < argc; i++) { | 
|  | 2732 | if (!PyArg_Parse((*getitem)(argv, i), | 
|  | 2733 | "et;spawnvpe() arg 2 must contain only strings", | 
|  | 2734 | Py_FileSystemDefaultEncoding, | 
|  | 2735 | &argvlist[i])) | 
|  | 2736 | { | 
|  | 2737 | lastarg = i; | 
|  | 2738 | goto fail_1; | 
|  | 2739 | } | 
|  | 2740 | } | 
|  | 2741 | lastarg = argc; | 
|  | 2742 | argvlist[argc] = NULL; | 
|  | 2743 |  | 
|  | 2744 | i = PyMapping_Size(env); | 
|  | 2745 | if (i < 0) | 
|  | 2746 | goto fail_1; | 
|  | 2747 | envlist = PyMem_NEW(char *, i + 1); | 
|  | 2748 | if (envlist == NULL) { | 
|  | 2749 | PyErr_NoMemory(); | 
|  | 2750 | goto fail_1; | 
|  | 2751 | } | 
|  | 2752 | envc = 0; | 
|  | 2753 | keys = PyMapping_Keys(env); | 
|  | 2754 | vals = PyMapping_Values(env); | 
|  | 2755 | if (!keys || !vals) | 
|  | 2756 | goto fail_2; | 
|  | 2757 | if (!PyList_Check(keys) || !PyList_Check(vals)) { | 
|  | 2758 | PyErr_SetString(PyExc_TypeError, | 
|  | 2759 | "spawnvpe(): env.keys() or env.values() is not a list"); | 
|  | 2760 | goto fail_2; | 
|  | 2761 | } | 
|  | 2762 |  | 
|  | 2763 | for (pos = 0; pos < i; pos++) { | 
|  | 2764 | char *p, *k, *v; | 
|  | 2765 | size_t len; | 
|  | 2766 |  | 
|  | 2767 | key = PyList_GetItem(keys, pos); | 
|  | 2768 | val = PyList_GetItem(vals, pos); | 
|  | 2769 | if (!key || !val) | 
|  | 2770 | goto fail_2; | 
|  | 2771 |  | 
|  | 2772 | if (!PyArg_Parse( | 
|  | 2773 | key, | 
|  | 2774 | "s;spawnvpe() arg 3 contains a non-string key", | 
|  | 2775 | &k) || | 
|  | 2776 | !PyArg_Parse( | 
|  | 2777 | val, | 
|  | 2778 | "s;spawnvpe() arg 3 contains a non-string value", | 
|  | 2779 | &v)) | 
|  | 2780 | { | 
|  | 2781 | goto fail_2; | 
|  | 2782 | } | 
|  | 2783 | len = PyString_Size(key) + PyString_Size(val) + 2; | 
|  | 2784 | p = PyMem_NEW(char, len); | 
|  | 2785 | if (p == NULL) { | 
|  | 2786 | PyErr_NoMemory(); | 
|  | 2787 | goto fail_2; | 
|  | 2788 | } | 
|  | 2789 | PyOS_snprintf(p, len, "%s=%s", k, v); | 
|  | 2790 | envlist[envc++] = p; | 
|  | 2791 | } | 
|  | 2792 | envlist[envc] = 0; | 
|  | 2793 |  | 
|  | 2794 | Py_BEGIN_ALLOW_THREADS | 
|  | 2795 | #if defined(PYCC_GCC) | 
|  | 2796 | spawnval = spawnve(mode, path, argvlist, envlist); | 
|  | 2797 | #else | 
|  | 2798 | spawnval = _spawnve(mode, path, argvlist, envlist); | 
|  | 2799 | #endif | 
|  | 2800 | Py_END_ALLOW_THREADS | 
|  | 2801 |  | 
|  | 2802 | if (spawnval == -1) | 
|  | 2803 | (void) posix_error(); | 
|  | 2804 | else | 
|  | 2805 | res = Py_BuildValue("l", (long) spawnval); | 
|  | 2806 |  | 
|  | 2807 | fail_2: | 
|  | 2808 | while (--envc >= 0) | 
|  | 2809 | PyMem_DEL(envlist[envc]); | 
|  | 2810 | PyMem_DEL(envlist); | 
|  | 2811 | fail_1: | 
|  | 2812 | free_string_array(argvlist, lastarg); | 
|  | 2813 | Py_XDECREF(vals); | 
|  | 2814 | Py_XDECREF(keys); | 
|  | 2815 | fail_0: | 
|  | 2816 | PyMem_Free(path); | 
|  | 2817 | return res; | 
|  | 2818 | } | 
|  | 2819 | #endif /* PYOS_OS2 */ | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2820 | #endif /* HAVE_SPAWNV */ | 
|  | 2821 |  | 
|  | 2822 |  | 
| Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2823 | #ifdef HAVE_FORK1 | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2824 | PyDoc_STRVAR(posix_fork1__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2825 | "fork1() -> pid\n\n\ | 
| Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2826 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ | 
|  | 2827 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2828 | 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] | 2829 |  | 
|  | 2830 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2831 | posix_fork1(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2832 | { | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2833 | int pid = fork1(); | 
| Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2834 | if (pid == -1) | 
|  | 2835 | return posix_error(); | 
|  | 2836 | PyOS_AfterFork(); | 
|  | 2837 | return PyInt_FromLong((long)pid); | 
|  | 2838 | } | 
|  | 2839 | #endif | 
|  | 2840 |  | 
|  | 2841 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2842 | #ifdef HAVE_FORK | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2843 | PyDoc_STRVAR(posix_fork__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2844 | "fork() -> pid\n\n\ | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2845 | Fork a child process.\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2846 | 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] | 2847 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2848 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2849 | posix_fork(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2850 | { | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2851 | int pid = fork(); | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2852 | if (pid == -1) | 
|  | 2853 | return posix_error(); | 
| Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2854 | if (pid == 0) | 
|  | 2855 | PyOS_AfterFork(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2856 | return PyInt_FromLong((long)pid); | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2857 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2858 | #endif | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2859 |  | 
| Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2860 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ | 
| Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 2861 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ | 
|  | 2862 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) | 
| Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2863 | #define DEV_PTY_FILE "/dev/ptc" | 
|  | 2864 | #define HAVE_DEV_PTMX | 
|  | 2865 | #else | 
|  | 2866 | #define DEV_PTY_FILE "/dev/ptmx" | 
|  | 2867 | #endif | 
|  | 2868 |  | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2869 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2870 | #ifdef HAVE_PTY_H | 
|  | 2871 | #include <pty.h> | 
|  | 2872 | #else | 
|  | 2873 | #ifdef HAVE_LIBUTIL_H | 
|  | 2874 | #include <libutil.h> | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2875 | #endif /* HAVE_LIBUTIL_H */ | 
|  | 2876 | #endif /* HAVE_PTY_H */ | 
| Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 2877 | #ifdef HAVE_STROPTS_H | 
|  | 2878 | #include <stropts.h> | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2879 | #endif | 
|  | 2880 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2881 |  | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2882 | #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] | 2883 | PyDoc_STRVAR(posix_openpty__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2884 | "openpty() -> (master_fd, slave_fd)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2885 | 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] | 2886 |  | 
|  | 2887 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2888 | posix_openpty(PyObject *self, PyObject *noargs) | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2889 | { | 
|  | 2890 | int master_fd, slave_fd; | 
| Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2891 | #ifndef HAVE_OPENPTY | 
|  | 2892 | char * slave_name; | 
| Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2893 | #endif | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2894 | #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] | 2895 | PyOS_sighandler_t sig_saved; | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2896 | #ifdef sun | 
|  | 2897 | extern char *ptsname(); | 
|  | 2898 | #endif | 
|  | 2899 | #endif | 
| Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2900 |  | 
| Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2901 | #ifdef HAVE_OPENPTY | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2902 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) | 
|  | 2903 | return posix_error(); | 
| Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2904 | #elif defined(HAVE__GETPTY) | 
| Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2905 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); | 
|  | 2906 | if (slave_name == NULL) | 
|  | 2907 | return posix_error(); | 
|  | 2908 |  | 
|  | 2909 | slave_fd = open(slave_name, O_RDWR); | 
|  | 2910 | if (slave_fd < 0) | 
|  | 2911 | return posix_error(); | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2912 | #else | 
| Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2913 | 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] | 2914 | if (master_fd < 0) | 
|  | 2915 | return posix_error(); | 
|  | 2916 | sig_saved = signal(SIGCHLD, SIG_DFL); | 
| Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2917 | /* change permission of slave */ | 
|  | 2918 | if (grantpt(master_fd) < 0) { | 
|  | 2919 | signal(SIGCHLD, sig_saved); | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2920 | return posix_error(); | 
| Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2921 | } | 
|  | 2922 | /* unlock slave */ | 
|  | 2923 | if (unlockpt(master_fd) < 0) { | 
|  | 2924 | signal(SIGCHLD, sig_saved); | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2925 | return posix_error(); | 
| Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2926 | } | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2927 | signal(SIGCHLD, sig_saved); | 
|  | 2928 | slave_name = ptsname(master_fd); /* get name of slave */ | 
|  | 2929 | if (slave_name == NULL) | 
|  | 2930 | return posix_error(); | 
|  | 2931 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ | 
|  | 2932 | if (slave_fd < 0) | 
|  | 2933 | return posix_error(); | 
| Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2934 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2935 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ | 
|  | 2936 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ | 
| Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 2937 | #ifndef __hpux | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2938 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ | 
| Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 2939 | #endif /* __hpux */ | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2940 | #endif /* HAVE_CYGWIN */ | 
| Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 2941 | #endif /* HAVE_OPENPTY */ | 
| Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2942 |  | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2943 | return Py_BuildValue("(ii)", master_fd, slave_fd); | 
| Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2944 |  | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2945 | } | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2946 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2947 |  | 
|  | 2948 | #ifdef HAVE_FORKPTY | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2949 | PyDoc_STRVAR(posix_forkpty__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2950 | "forkpty() -> (pid, master_fd)\n\n\ | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2951 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ | 
|  | 2952 | 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] | 2953 | To both, return fd of newly opened pseudo-terminal.\n"); | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2954 |  | 
|  | 2955 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2956 | posix_forkpty(PyObject *self, PyObject *noargs) | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2957 | { | 
|  | 2958 | int master_fd, pid; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2959 |  | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2960 | pid = forkpty(&master_fd, NULL, NULL, NULL); | 
|  | 2961 | if (pid == -1) | 
|  | 2962 | return posix_error(); | 
| Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2963 | if (pid == 0) | 
|  | 2964 | PyOS_AfterFork(); | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2965 | return Py_BuildValue("(ii)", pid, master_fd); | 
|  | 2966 | } | 
|  | 2967 | #endif | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2968 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2969 | #ifdef HAVE_GETEGID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2970 | PyDoc_STRVAR(posix_getegid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2971 | "getegid() -> egid\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2972 | Return the current process's effective group id."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2973 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2974 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2975 | posix_getegid(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2976 | { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2977 | return PyInt_FromLong((long)getegid()); | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2978 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2979 | #endif | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2980 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2981 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2982 | #ifdef HAVE_GETEUID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2983 | PyDoc_STRVAR(posix_geteuid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2984 | "geteuid() -> euid\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2985 | Return the current process's effective user id."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2986 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2987 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2988 | posix_geteuid(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2989 | { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2990 | return PyInt_FromLong((long)geteuid()); | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2991 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2992 | #endif | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2993 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2994 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2995 | #ifdef HAVE_GETGID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2996 | PyDoc_STRVAR(posix_getgid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2997 | "getgid() -> gid\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2998 | Return the current process's group id."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2999 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3000 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3001 | posix_getgid(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3002 | { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3003 | return PyInt_FromLong((long)getgid()); | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3004 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3005 | #endif | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3006 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3007 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3008 | PyDoc_STRVAR(posix_getpid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3009 | "getpid() -> pid\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3010 | Return the current process id"); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3011 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3012 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3013 | posix_getpid(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3014 | { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3015 | return PyInt_FromLong((long)getpid()); | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3016 | } | 
|  | 3017 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3018 |  | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3019 | #ifdef HAVE_GETGROUPS | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3020 | PyDoc_STRVAR(posix_getgroups__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3021 | "getgroups() -> list of group IDs\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3022 | Return list of supplemental group IDs for the process."); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3023 |  | 
|  | 3024 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3025 | posix_getgroups(PyObject *self, PyObject *noargs) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3026 | { | 
|  | 3027 | PyObject *result = NULL; | 
|  | 3028 |  | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3029 | #ifdef NGROUPS_MAX | 
|  | 3030 | #define MAX_GROUPS NGROUPS_MAX | 
|  | 3031 | #else | 
|  | 3032 | /* defined to be 16 on Solaris7, so this should be a small number */ | 
|  | 3033 | #define MAX_GROUPS 64 | 
|  | 3034 | #endif | 
|  | 3035 | gid_t grouplist[MAX_GROUPS]; | 
|  | 3036 | int n; | 
|  | 3037 |  | 
|  | 3038 | n = getgroups(MAX_GROUPS, grouplist); | 
|  | 3039 | if (n < 0) | 
|  | 3040 | posix_error(); | 
|  | 3041 | else { | 
|  | 3042 | result = PyList_New(n); | 
|  | 3043 | if (result != NULL) { | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3044 | int i; | 
|  | 3045 | for (i = 0; i < n; ++i) { | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3046 | PyObject *o = PyInt_FromLong((long)grouplist[i]); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3047 | if (o == NULL) { | 
|  | 3048 | Py_DECREF(result); | 
|  | 3049 | result = NULL; | 
|  | 3050 | break; | 
|  | 3051 | } | 
|  | 3052 | PyList_SET_ITEM(result, i, o); | 
|  | 3053 | } | 
|  | 3054 | } | 
|  | 3055 | } | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3056 |  | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3057 | return result; | 
|  | 3058 | } | 
|  | 3059 | #endif | 
|  | 3060 |  | 
| Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3061 | #ifdef HAVE_GETPGID | 
| Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3062 | PyDoc_STRVAR(posix_getpgid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3063 | "getpgid(pid) -> pgid\n\n\ | 
| Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3064 | Call the system call getpgid()."); | 
| Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3065 |  | 
|  | 3066 | static PyObject * | 
|  | 3067 | posix_getpgid(PyObject *self, PyObject *args) | 
|  | 3068 | { | 
|  | 3069 | int pid, pgid; | 
|  | 3070 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) | 
|  | 3071 | return NULL; | 
|  | 3072 | pgid = getpgid(pid); | 
|  | 3073 | if (pgid < 0) | 
|  | 3074 | return posix_error(); | 
|  | 3075 | return PyInt_FromLong((long)pgid); | 
|  | 3076 | } | 
|  | 3077 | #endif /* HAVE_GETPGID */ | 
|  | 3078 |  | 
|  | 3079 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3080 | #ifdef HAVE_GETPGRP | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3081 | PyDoc_STRVAR(posix_getpgrp__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3082 | "getpgrp() -> pgrp\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3083 | Return the current process group id."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3084 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3085 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3086 | posix_getpgrp(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3087 | { | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3088 | #ifdef GETPGRP_HAVE_ARG | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3089 | return PyInt_FromLong((long)getpgrp(0)); | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3090 | #else /* GETPGRP_HAVE_ARG */ | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3091 | return PyInt_FromLong((long)getpgrp()); | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3092 | #endif /* GETPGRP_HAVE_ARG */ | 
| Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3093 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3094 | #endif /* HAVE_GETPGRP */ | 
| Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3095 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3096 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3097 | #ifdef HAVE_SETPGRP | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3098 | PyDoc_STRVAR(posix_setpgrp__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3099 | "setpgrp()\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3100 | Make this process a session leader."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3101 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3102 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3103 | posix_setpgrp(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3104 | { | 
| Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3105 | #ifdef SETPGRP_HAVE_ARG | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3106 | if (setpgrp(0, 0) < 0) | 
| Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3107 | #else /* SETPGRP_HAVE_ARG */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3108 | if (setpgrp() < 0) | 
| Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3109 | #endif /* SETPGRP_HAVE_ARG */ | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3110 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3111 | Py_INCREF(Py_None); | 
|  | 3112 | return Py_None; | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3113 | } | 
|  | 3114 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3115 | #endif /* HAVE_SETPGRP */ | 
|  | 3116 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3117 | #ifdef HAVE_GETPPID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3118 | PyDoc_STRVAR(posix_getppid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3119 | "getppid() -> ppid\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3120 | Return the parent's process id."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3121 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3122 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3123 | posix_getppid(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3124 | { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3125 | return PyInt_FromLong((long)getppid()); | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3126 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3127 | #endif | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3128 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3129 |  | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3130 | #ifdef HAVE_GETLOGIN | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3131 | PyDoc_STRVAR(posix_getlogin__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3132 | "getlogin() -> string\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3133 | Return the actual login name."); | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3134 |  | 
|  | 3135 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3136 | posix_getlogin(PyObject *self, PyObject *noargs) | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3137 | { | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3138 | PyObject *result = NULL; | 
| Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3139 | char *name; | 
|  | 3140 | int old_errno = errno; | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3141 |  | 
| Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3142 | errno = 0; | 
|  | 3143 | name = getlogin(); | 
|  | 3144 | if (name == NULL) { | 
|  | 3145 | if (errno) | 
|  | 3146 | posix_error(); | 
|  | 3147 | else | 
|  | 3148 | PyErr_SetString(PyExc_OSError, | 
| Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3149 | "unable to determine login name"); | 
| Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3150 | } | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3151 | else | 
|  | 3152 | result = PyString_FromString(name); | 
| Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3153 | errno = old_errno; | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3154 |  | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3155 | return result; | 
|  | 3156 | } | 
|  | 3157 | #endif | 
|  | 3158 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3159 | #ifdef HAVE_GETUID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3160 | PyDoc_STRVAR(posix_getuid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3161 | "getuid() -> uid\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3162 | Return the current process's user id."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3163 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3164 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3165 | posix_getuid(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3166 | { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3167 | return PyInt_FromLong((long)getuid()); | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3168 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3169 | #endif | 
| Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3170 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3171 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3172 | #ifdef HAVE_KILL | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3173 | PyDoc_STRVAR(posix_kill__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3174 | "kill(pid, sig)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3175 | Kill a process with a signal."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3176 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3177 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3178 | posix_kill(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3179 | { | 
|  | 3180 | int pid, sig; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3181 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3182 | return NULL; | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3183 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3184 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { | 
|  | 3185 | APIRET rc; | 
|  | 3186 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3187 | return os2_error(rc); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3188 |  | 
|  | 3189 | } else if (sig == XCPT_SIGNAL_KILLPROC) { | 
|  | 3190 | APIRET rc; | 
|  | 3191 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3192 | return os2_error(rc); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3193 |  | 
|  | 3194 | } else | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3195 | return NULL; /* Unrecognized Signal Requested */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3196 | #else | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3197 | if (kill(pid, sig) == -1) | 
|  | 3198 | return posix_error(); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3199 | #endif | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3200 | Py_INCREF(Py_None); | 
|  | 3201 | return Py_None; | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3202 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3203 | #endif | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3204 |  | 
| Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3205 | #ifdef HAVE_KILLPG | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3206 | PyDoc_STRVAR(posix_killpg__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3207 | "killpg(pgid, sig)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3208 | Kill a process group with a signal."); | 
| Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3209 |  | 
|  | 3210 | static PyObject * | 
|  | 3211 | posix_killpg(PyObject *self, PyObject *args) | 
|  | 3212 | { | 
|  | 3213 | int pgid, sig; | 
|  | 3214 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) | 
|  | 3215 | return NULL; | 
|  | 3216 | if (killpg(pgid, sig) == -1) | 
|  | 3217 | return posix_error(); | 
|  | 3218 | Py_INCREF(Py_None); | 
|  | 3219 | return Py_None; | 
|  | 3220 | } | 
|  | 3221 | #endif | 
|  | 3222 |  | 
| Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3223 | #ifdef HAVE_PLOCK | 
|  | 3224 |  | 
|  | 3225 | #ifdef HAVE_SYS_LOCK_H | 
|  | 3226 | #include <sys/lock.h> | 
|  | 3227 | #endif | 
|  | 3228 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3229 | PyDoc_STRVAR(posix_plock__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3230 | "plock(op)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3231 | Lock program segments into memory."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3232 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3233 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3234 | posix_plock(PyObject *self, PyObject *args) | 
| Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3235 | { | 
|  | 3236 | int op; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3237 | if (!PyArg_ParseTuple(args, "i:plock", &op)) | 
| Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3238 | return NULL; | 
|  | 3239 | if (plock(op) == -1) | 
|  | 3240 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3241 | Py_INCREF(Py_None); | 
|  | 3242 | return Py_None; | 
| Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3243 | } | 
|  | 3244 | #endif | 
|  | 3245 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3246 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3247 | #ifdef HAVE_POPEN | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3248 | PyDoc_STRVAR(posix_popen__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3249 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3250 | Open a pipe to/from a command returning a file object."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3251 |  | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3252 | #if defined(PYOS_OS2) | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3253 | #if defined(PYCC_VACPP) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3254 | static int | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3255 | async_system(const char *command) | 
|  | 3256 | { | 
|  | 3257 | char        *p, errormsg[256], args[1024]; | 
|  | 3258 | RESULTCODES  rcodes; | 
|  | 3259 | APIRET       rc; | 
|  | 3260 | char        *shell = getenv("COMSPEC"); | 
|  | 3261 | if (!shell) | 
|  | 3262 | shell = "cmd"; | 
|  | 3263 |  | 
|  | 3264 | strcpy(args, shell); | 
|  | 3265 | p = &args[ strlen(args)+1 ]; | 
|  | 3266 | strcpy(p, "/c "); | 
|  | 3267 | strcat(p, command); | 
|  | 3268 | p += strlen(p) + 1; | 
|  | 3269 | *p = '\0'; | 
|  | 3270 |  | 
|  | 3271 | rc = DosExecPgm(errormsg, sizeof(errormsg), | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3272 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3273 | args, | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3274 | NULL,       /* Inherit Parent's Environment */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3275 | &rcodes, shell); | 
|  | 3276 | return rc; | 
|  | 3277 | } | 
|  | 3278 |  | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3279 | static FILE * | 
|  | 3280 | popen(const char *command, const char *mode, int pipesize, int *err) | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3281 | { | 
|  | 3282 | HFILE    rhan, whan; | 
|  | 3283 | FILE    *retfd = NULL; | 
|  | 3284 | APIRET   rc = DosCreatePipe(&rhan, &whan, pipesize); | 
|  | 3285 |  | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3286 | if (rc != NO_ERROR) { | 
|  | 3287 | *err = rc; | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3288 | return NULL; /* ERROR - Unable to Create Anon Pipe */ | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3289 | } | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3290 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3291 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ | 
|  | 3292 | int oldfd = dup(1);      /* Save STDOUT Handle in Another Handle */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3293 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3294 | DosEnterCritSec();      /* Stop Other Threads While Changing Handles */ | 
|  | 3295 | close(1);                /* Make STDOUT Available for Reallocation */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3296 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3297 | if (dup2(whan, 1) == 0) {      /* Connect STDOUT to Pipe Write Side */ | 
|  | 3298 | DosClose(whan);            /* Close Now-Unused Pipe Write Handle */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3299 |  | 
| Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3300 | rc = async_system(command); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3301 | } | 
|  | 3302 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3303 | dup2(oldfd, 1);          /* Reconnect STDOUT to Original Handle */ | 
|  | 3304 | DosExitCritSec();        /* Now Allow Other Threads to Run */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3305 |  | 
| Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3306 | if (rc == NO_ERROR) | 
|  | 3307 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ | 
|  | 3308 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3309 | close(oldfd);            /* And Close Saved STDOUT Handle */ | 
|  | 3310 | return retfd;            /* Return fd of Pipe or NULL if Error */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3311 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3312 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ | 
|  | 3313 | int oldfd = dup(0);      /* Save STDIN Handle in Another Handle */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3314 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3315 | DosEnterCritSec();      /* Stop Other Threads While Changing Handles */ | 
|  | 3316 | close(0);                /* Make STDIN Available for Reallocation */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3317 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3318 | if (dup2(rhan, 0) == 0)     { /* Connect STDIN to Pipe Read Side */ | 
|  | 3319 | DosClose(rhan);           /* Close Now-Unused Pipe Read Handle */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3320 |  | 
| Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3321 | rc = async_system(command); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3322 | } | 
|  | 3323 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3324 | dup2(oldfd, 0);          /* Reconnect STDIN to Original Handle */ | 
|  | 3325 | DosExitCritSec();        /* Now Allow Other Threads to Run */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3326 |  | 
| Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3327 | if (rc == NO_ERROR) | 
|  | 3328 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ | 
|  | 3329 |  | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3330 | close(oldfd);            /* And Close Saved STDIN Handle */ | 
|  | 3331 | return retfd;            /* Return fd of Pipe or NULL if Error */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3332 |  | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3333 | } else { | 
|  | 3334 | *err = ERROR_INVALID_ACCESS; | 
| Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3335 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3336 | } | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3337 | } | 
|  | 3338 |  | 
|  | 3339 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3340 | posix_popen(PyObject *self, PyObject *args) | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3341 | { | 
|  | 3342 | char *name; | 
|  | 3343 | char *mode = "r"; | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3344 | int   err, bufsize = -1; | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3345 | FILE *fp; | 
|  | 3346 | PyObject *f; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3347 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3348 | return NULL; | 
|  | 3349 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3350 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3351 | Py_END_ALLOW_THREADS | 
|  | 3352 | if (fp == NULL) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3353 | return os2_error(err); | 
|  | 3354 |  | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3355 | f = PyFile_FromFile(fp, name, mode, fclose); | 
|  | 3356 | if (f != NULL) | 
|  | 3357 | PyFile_SetBufSize(f, bufsize); | 
|  | 3358 | return f; | 
|  | 3359 | } | 
|  | 3360 |  | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3361 | #elif defined(PYCC_GCC) | 
|  | 3362 |  | 
|  | 3363 | /* standard posix version of popen() support */ | 
|  | 3364 | static PyObject * | 
|  | 3365 | posix_popen(PyObject *self, PyObject *args) | 
|  | 3366 | { | 
|  | 3367 | char *name; | 
|  | 3368 | char *mode = "r"; | 
|  | 3369 | int bufsize = -1; | 
|  | 3370 | FILE *fp; | 
|  | 3371 | PyObject *f; | 
|  | 3372 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) | 
|  | 3373 | return NULL; | 
|  | 3374 | Py_BEGIN_ALLOW_THREADS | 
|  | 3375 | fp = popen(name, mode); | 
|  | 3376 | Py_END_ALLOW_THREADS | 
|  | 3377 | if (fp == NULL) | 
|  | 3378 | return posix_error(); | 
|  | 3379 | f = PyFile_FromFile(fp, name, mode, pclose); | 
|  | 3380 | if (f != NULL) | 
|  | 3381 | PyFile_SetBufSize(f, bufsize); | 
|  | 3382 | return f; | 
|  | 3383 | } | 
|  | 3384 |  | 
|  | 3385 | /* fork() under OS/2 has lots'o'warts | 
|  | 3386 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() | 
|  | 3387 | * most of this code is a ripoff of the win32 code, but using the | 
|  | 3388 | * capabilities of EMX's C library routines | 
|  | 3389 | */ | 
|  | 3390 |  | 
|  | 3391 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ | 
|  | 3392 | #define POPEN_1 1 | 
|  | 3393 | #define POPEN_2 2 | 
|  | 3394 | #define POPEN_3 3 | 
|  | 3395 | #define POPEN_4 4 | 
|  | 3396 |  | 
|  | 3397 | static PyObject *_PyPopen(char *, int, int, int); | 
|  | 3398 | static int _PyPclose(FILE *file); | 
|  | 3399 |  | 
|  | 3400 | /* | 
|  | 3401 | * Internal dictionary mapping popen* file pointers to process handles, | 
|  | 3402 | * for use when retrieving the process exit code.  See _PyPclose() below | 
|  | 3403 | * for more information on this dictionary's use. | 
|  | 3404 | */ | 
|  | 3405 | static PyObject *_PyPopenProcs = NULL; | 
|  | 3406 |  | 
|  | 3407 | /* os2emx version of popen2() | 
|  | 3408 | * | 
|  | 3409 | * The result of this function is a pipe (file) connected to the | 
|  | 3410 | * process's stdin, and a pipe connected to the process's stdout. | 
|  | 3411 | */ | 
|  | 3412 |  | 
|  | 3413 | static PyObject * | 
|  | 3414 | os2emx_popen2(PyObject *self, PyObject  *args) | 
|  | 3415 | { | 
|  | 3416 | PyObject *f; | 
|  | 3417 | int tm=0; | 
|  | 3418 |  | 
|  | 3419 | char *cmdstring; | 
|  | 3420 | char *mode = "t"; | 
|  | 3421 | int bufsize = -1; | 
|  | 3422 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) | 
|  | 3423 | return NULL; | 
|  | 3424 |  | 
|  | 3425 | if (*mode == 't') | 
|  | 3426 | tm = O_TEXT; | 
|  | 3427 | else if (*mode != 'b') { | 
|  | 3428 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); | 
|  | 3429 | return NULL; | 
|  | 3430 | } else | 
|  | 3431 | tm = O_BINARY; | 
|  | 3432 |  | 
|  | 3433 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); | 
|  | 3434 |  | 
|  | 3435 | return f; | 
|  | 3436 | } | 
|  | 3437 |  | 
|  | 3438 | /* | 
|  | 3439 | * Variation on os2emx.popen2 | 
|  | 3440 | * | 
|  | 3441 | * The result of this function is 3 pipes - the process's stdin, | 
|  | 3442 | * stdout and stderr | 
|  | 3443 | */ | 
|  | 3444 |  | 
|  | 3445 | static PyObject * | 
|  | 3446 | os2emx_popen3(PyObject *self, PyObject *args) | 
|  | 3447 | { | 
|  | 3448 | PyObject *f; | 
|  | 3449 | int tm = 0; | 
|  | 3450 |  | 
|  | 3451 | char *cmdstring; | 
|  | 3452 | char *mode = "t"; | 
|  | 3453 | int bufsize = -1; | 
|  | 3454 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) | 
|  | 3455 | return NULL; | 
|  | 3456 |  | 
|  | 3457 | if (*mode == 't') | 
|  | 3458 | tm = O_TEXT; | 
|  | 3459 | else if (*mode != 'b') { | 
|  | 3460 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); | 
|  | 3461 | return NULL; | 
|  | 3462 | } else | 
|  | 3463 | tm = O_BINARY; | 
|  | 3464 |  | 
|  | 3465 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); | 
|  | 3466 |  | 
|  | 3467 | return f; | 
|  | 3468 | } | 
|  | 3469 |  | 
|  | 3470 | /* | 
|  | 3471 | * Variation on os2emx.popen2 | 
|  | 3472 | * | 
| Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3473 | * The result of this function is 2 pipes - the processes stdin, | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3474 | * and stdout+stderr combined as a single pipe. | 
|  | 3475 | */ | 
|  | 3476 |  | 
|  | 3477 | static PyObject * | 
|  | 3478 | os2emx_popen4(PyObject *self, PyObject  *args) | 
|  | 3479 | { | 
|  | 3480 | PyObject *f; | 
|  | 3481 | int tm = 0; | 
|  | 3482 |  | 
|  | 3483 | char *cmdstring; | 
|  | 3484 | char *mode = "t"; | 
|  | 3485 | int bufsize = -1; | 
|  | 3486 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) | 
|  | 3487 | return NULL; | 
|  | 3488 |  | 
|  | 3489 | if (*mode == 't') | 
|  | 3490 | tm = O_TEXT; | 
|  | 3491 | else if (*mode != 'b') { | 
|  | 3492 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); | 
|  | 3493 | return NULL; | 
|  | 3494 | } else | 
|  | 3495 | tm = O_BINARY; | 
|  | 3496 |  | 
|  | 3497 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); | 
|  | 3498 |  | 
|  | 3499 | return f; | 
|  | 3500 | } | 
|  | 3501 |  | 
|  | 3502 | /* a couple of structures for convenient handling of multiple | 
|  | 3503 | * file handles and pipes | 
|  | 3504 | */ | 
|  | 3505 | struct file_ref | 
|  | 3506 | { | 
|  | 3507 | int handle; | 
|  | 3508 | int flags; | 
|  | 3509 | }; | 
|  | 3510 |  | 
|  | 3511 | struct pipe_ref | 
|  | 3512 | { | 
|  | 3513 | int rd; | 
|  | 3514 | int wr; | 
|  | 3515 | }; | 
|  | 3516 |  | 
|  | 3517 | /* The following code is derived from the win32 code */ | 
|  | 3518 |  | 
|  | 3519 | static PyObject * | 
|  | 3520 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) | 
|  | 3521 | { | 
|  | 3522 | struct file_ref stdio[3]; | 
|  | 3523 | struct pipe_ref p_fd[3]; | 
|  | 3524 | FILE *p_s[3]; | 
|  | 3525 | int file_count, i, pipe_err, pipe_pid; | 
|  | 3526 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; | 
|  | 3527 | PyObject *f, *p_f[3]; | 
|  | 3528 |  | 
|  | 3529 | /* file modes for subsequent fdopen's on pipe handles */ | 
|  | 3530 | if (mode == O_TEXT) | 
|  | 3531 | { | 
|  | 3532 | rd_mode = "rt"; | 
|  | 3533 | wr_mode = "wt"; | 
|  | 3534 | } | 
|  | 3535 | else | 
|  | 3536 | { | 
|  | 3537 | rd_mode = "rb"; | 
|  | 3538 | wr_mode = "wb"; | 
|  | 3539 | } | 
|  | 3540 |  | 
|  | 3541 | /* prepare shell references */ | 
|  | 3542 | if ((shell = getenv("EMXSHELL")) == NULL) | 
|  | 3543 | if ((shell = getenv("COMSPEC")) == NULL) | 
|  | 3544 | { | 
|  | 3545 | errno = ENOENT; | 
|  | 3546 | return posix_error(); | 
|  | 3547 | } | 
|  | 3548 |  | 
|  | 3549 | sh_name = _getname(shell); | 
|  | 3550 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) | 
|  | 3551 | opt = "/c"; | 
|  | 3552 | else | 
|  | 3553 | opt = "-c"; | 
|  | 3554 |  | 
|  | 3555 | /* save current stdio fds + their flags, and set not inheritable */ | 
|  | 3556 | i = pipe_err = 0; | 
|  | 3557 | while (pipe_err >= 0 && i < 3) | 
|  | 3558 | { | 
|  | 3559 | pipe_err = stdio[i].handle = dup(i); | 
|  | 3560 | stdio[i].flags = fcntl(i, F_GETFD, 0); | 
|  | 3561 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); | 
|  | 3562 | i++; | 
|  | 3563 | } | 
|  | 3564 | if (pipe_err < 0) | 
|  | 3565 | { | 
|  | 3566 | /* didn't get them all saved - clean up and bail out */ | 
|  | 3567 | int saved_err = errno; | 
|  | 3568 | while (i-- > 0) | 
|  | 3569 | { | 
|  | 3570 | close(stdio[i].handle); | 
|  | 3571 | } | 
|  | 3572 | errno = saved_err; | 
|  | 3573 | return posix_error(); | 
|  | 3574 | } | 
|  | 3575 |  | 
|  | 3576 | /* create pipe ends */ | 
|  | 3577 | file_count = 2; | 
|  | 3578 | if (n == POPEN_3) | 
|  | 3579 | file_count = 3; | 
|  | 3580 | i = pipe_err = 0; | 
|  | 3581 | while ((pipe_err == 0) && (i < file_count)) | 
|  | 3582 | pipe_err = pipe((int *)&p_fd[i++]); | 
|  | 3583 | if (pipe_err < 0) | 
|  | 3584 | { | 
|  | 3585 | /* didn't get them all made - clean up and bail out */ | 
|  | 3586 | while (i-- > 0) | 
|  | 3587 | { | 
|  | 3588 | close(p_fd[i].wr); | 
|  | 3589 | close(p_fd[i].rd); | 
|  | 3590 | } | 
|  | 3591 | errno = EPIPE; | 
|  | 3592 | return posix_error(); | 
|  | 3593 | } | 
|  | 3594 |  | 
|  | 3595 | /* change the actual standard IO streams over temporarily, | 
|  | 3596 | * making the retained pipe ends non-inheritable | 
|  | 3597 | */ | 
|  | 3598 | pipe_err = 0; | 
|  | 3599 |  | 
|  | 3600 | /* - stdin */ | 
|  | 3601 | if (dup2(p_fd[0].rd, 0) == 0) | 
|  | 3602 | { | 
|  | 3603 | close(p_fd[0].rd); | 
|  | 3604 | i = fcntl(p_fd[0].wr, F_GETFD, 0); | 
|  | 3605 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); | 
|  | 3606 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) | 
|  | 3607 | { | 
|  | 3608 | close(p_fd[0].wr); | 
|  | 3609 | pipe_err = -1; | 
|  | 3610 | } | 
|  | 3611 | } | 
|  | 3612 | else | 
|  | 3613 | { | 
|  | 3614 | pipe_err = -1; | 
|  | 3615 | } | 
|  | 3616 |  | 
|  | 3617 | /* - stdout */ | 
|  | 3618 | if (pipe_err == 0) | 
|  | 3619 | { | 
|  | 3620 | if (dup2(p_fd[1].wr, 1) == 1) | 
|  | 3621 | { | 
|  | 3622 | close(p_fd[1].wr); | 
|  | 3623 | i = fcntl(p_fd[1].rd, F_GETFD, 0); | 
|  | 3624 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); | 
|  | 3625 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) | 
|  | 3626 | { | 
|  | 3627 | close(p_fd[1].rd); | 
|  | 3628 | pipe_err = -1; | 
|  | 3629 | } | 
|  | 3630 | } | 
|  | 3631 | else | 
|  | 3632 | { | 
|  | 3633 | pipe_err = -1; | 
|  | 3634 | } | 
|  | 3635 | } | 
|  | 3636 |  | 
|  | 3637 | /* - stderr, as required */ | 
|  | 3638 | if (pipe_err == 0) | 
|  | 3639 | switch (n) | 
|  | 3640 | { | 
|  | 3641 | case POPEN_3: | 
|  | 3642 | { | 
|  | 3643 | if (dup2(p_fd[2].wr, 2) == 2) | 
|  | 3644 | { | 
|  | 3645 | close(p_fd[2].wr); | 
|  | 3646 | i = fcntl(p_fd[2].rd, F_GETFD, 0); | 
|  | 3647 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); | 
|  | 3648 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) | 
|  | 3649 | { | 
|  | 3650 | close(p_fd[2].rd); | 
|  | 3651 | pipe_err = -1; | 
|  | 3652 | } | 
|  | 3653 | } | 
|  | 3654 | else | 
|  | 3655 | { | 
|  | 3656 | pipe_err = -1; | 
|  | 3657 | } | 
|  | 3658 | break; | 
|  | 3659 | } | 
|  | 3660 |  | 
|  | 3661 | case POPEN_4: | 
|  | 3662 | { | 
|  | 3663 | if (dup2(1, 2) != 2) | 
|  | 3664 | { | 
|  | 3665 | pipe_err = -1; | 
|  | 3666 | } | 
|  | 3667 | break; | 
|  | 3668 | } | 
|  | 3669 | } | 
|  | 3670 |  | 
|  | 3671 | /* spawn the child process */ | 
|  | 3672 | if (pipe_err == 0) | 
|  | 3673 | { | 
|  | 3674 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); | 
|  | 3675 | if (pipe_pid == -1) | 
|  | 3676 | { | 
|  | 3677 | pipe_err = -1; | 
|  | 3678 | } | 
|  | 3679 | else | 
|  | 3680 | { | 
|  | 3681 | /* save the PID into the FILE structure | 
|  | 3682 | * NOTE: this implementation doesn't actually | 
|  | 3683 | * take advantage of this, but do it for | 
|  | 3684 | * completeness - AIM Apr01 | 
|  | 3685 | */ | 
|  | 3686 | for (i = 0; i < file_count; i++) | 
|  | 3687 | p_s[i]->_pid = pipe_pid; | 
|  | 3688 | } | 
|  | 3689 | } | 
|  | 3690 |  | 
|  | 3691 | /* reset standard IO to normal */ | 
|  | 3692 | for (i = 0; i < 3; i++) | 
|  | 3693 | { | 
|  | 3694 | dup2(stdio[i].handle, i); | 
|  | 3695 | fcntl(i, F_SETFD, stdio[i].flags); | 
|  | 3696 | close(stdio[i].handle); | 
|  | 3697 | } | 
|  | 3698 |  | 
|  | 3699 | /* if any remnant problems, clean up and bail out */ | 
|  | 3700 | if (pipe_err < 0) | 
|  | 3701 | { | 
|  | 3702 | for (i = 0; i < 3; i++) | 
|  | 3703 | { | 
|  | 3704 | close(p_fd[i].rd); | 
|  | 3705 | close(p_fd[i].wr); | 
|  | 3706 | } | 
|  | 3707 | errno = EPIPE; | 
|  | 3708 | return posix_error_with_filename(cmdstring); | 
|  | 3709 | } | 
|  | 3710 |  | 
|  | 3711 | /* build tuple of file objects to return */ | 
|  | 3712 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) | 
|  | 3713 | PyFile_SetBufSize(p_f[0], bufsize); | 
|  | 3714 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) | 
|  | 3715 | PyFile_SetBufSize(p_f[1], bufsize); | 
|  | 3716 | if (n == POPEN_3) | 
|  | 3717 | { | 
|  | 3718 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) | 
|  | 3719 | PyFile_SetBufSize(p_f[0], bufsize); | 
| Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3720 | 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] | 3721 | } | 
|  | 3722 | else | 
| Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3723 | f = PyTuple_Pack(2, p_f[0], p_f[1]); | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3724 |  | 
|  | 3725 | /* | 
|  | 3726 | * Insert the files we've created into the process dictionary | 
|  | 3727 | * all referencing the list with the process handle and the | 
|  | 3728 | * initial number of files (see description below in _PyPclose). | 
|  | 3729 | * Since if _PyPclose later tried to wait on a process when all | 
|  | 3730 | * handles weren't closed, it could create a deadlock with the | 
|  | 3731 | * child, we spend some energy here to try to ensure that we | 
|  | 3732 | * either insert all file handles into the dictionary or none | 
|  | 3733 | * at all.  It's a little clumsy with the various popen modes | 
|  | 3734 | * and variable number of files involved. | 
|  | 3735 | */ | 
|  | 3736 | if (!_PyPopenProcs) | 
|  | 3737 | { | 
|  | 3738 | _PyPopenProcs = PyDict_New(); | 
|  | 3739 | } | 
|  | 3740 |  | 
|  | 3741 | if (_PyPopenProcs) | 
|  | 3742 | { | 
|  | 3743 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; | 
|  | 3744 | int ins_rc[3]; | 
|  | 3745 |  | 
|  | 3746 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; | 
|  | 3747 | ins_rc[0]  = ins_rc[1]  = ins_rc[2]  = 0; | 
|  | 3748 |  | 
|  | 3749 | procObj = PyList_New(2); | 
|  | 3750 | pidObj = PyInt_FromLong((long) pipe_pid); | 
|  | 3751 | intObj = PyInt_FromLong((long) file_count); | 
|  | 3752 |  | 
|  | 3753 | if (procObj && pidObj && intObj) | 
|  | 3754 | { | 
|  | 3755 | PyList_SetItem(procObj, 0, pidObj); | 
|  | 3756 | PyList_SetItem(procObj, 1, intObj); | 
|  | 3757 |  | 
|  | 3758 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); | 
|  | 3759 | if (fileObj[0]) | 
|  | 3760 | { | 
|  | 3761 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, | 
|  | 3762 | fileObj[0], | 
|  | 3763 | procObj); | 
|  | 3764 | } | 
|  | 3765 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); | 
|  | 3766 | if (fileObj[1]) | 
|  | 3767 | { | 
|  | 3768 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, | 
|  | 3769 | fileObj[1], | 
|  | 3770 | procObj); | 
|  | 3771 | } | 
|  | 3772 | if (file_count >= 3) | 
|  | 3773 | { | 
|  | 3774 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); | 
|  | 3775 | if (fileObj[2]) | 
|  | 3776 | { | 
|  | 3777 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, | 
|  | 3778 | fileObj[2], | 
|  | 3779 | procObj); | 
|  | 3780 | } | 
|  | 3781 | } | 
|  | 3782 |  | 
|  | 3783 | if (ins_rc[0] < 0 || !fileObj[0] || | 
|  | 3784 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || | 
|  | 3785 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) | 
|  | 3786 | { | 
|  | 3787 | /* Something failed - remove any dictionary | 
|  | 3788 | * entries that did make it. | 
|  | 3789 | */ | 
|  | 3790 | if (!ins_rc[0] && fileObj[0]) | 
|  | 3791 | { | 
|  | 3792 | PyDict_DelItem(_PyPopenProcs, | 
|  | 3793 | fileObj[0]); | 
|  | 3794 | } | 
|  | 3795 | if (!ins_rc[1] && fileObj[1]) | 
|  | 3796 | { | 
|  | 3797 | PyDict_DelItem(_PyPopenProcs, | 
|  | 3798 | fileObj[1]); | 
|  | 3799 | } | 
|  | 3800 | if (!ins_rc[2] && fileObj[2]) | 
|  | 3801 | { | 
|  | 3802 | PyDict_DelItem(_PyPopenProcs, | 
|  | 3803 | fileObj[2]); | 
|  | 3804 | } | 
|  | 3805 | } | 
|  | 3806 | } | 
| Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3807 |  | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3808 | /* | 
|  | 3809 | * Clean up our localized references for the dictionary keys | 
|  | 3810 | * and value since PyDict_SetItem will Py_INCREF any copies | 
|  | 3811 | * that got placed in the dictionary. | 
|  | 3812 | */ | 
|  | 3813 | Py_XDECREF(procObj); | 
|  | 3814 | Py_XDECREF(fileObj[0]); | 
|  | 3815 | Py_XDECREF(fileObj[1]); | 
|  | 3816 | Py_XDECREF(fileObj[2]); | 
|  | 3817 | } | 
|  | 3818 |  | 
|  | 3819 | /* Child is launched. */ | 
|  | 3820 | return f; | 
|  | 3821 | } | 
|  | 3822 |  | 
|  | 3823 | /* | 
|  | 3824 | * Wrapper for fclose() to use for popen* files, so we can retrieve the | 
|  | 3825 | * exit code for the child process and return as a result of the close. | 
|  | 3826 | * | 
|  | 3827 | * This function uses the _PyPopenProcs dictionary in order to map the | 
|  | 3828 | * input file pointer to information about the process that was | 
|  | 3829 | * originally created by the popen* call that created the file pointer. | 
|  | 3830 | * The dictionary uses the file pointer as a key (with one entry | 
|  | 3831 | * inserted for each file returned by the original popen* call) and a | 
|  | 3832 | * single list object as the value for all files from a single call. | 
|  | 3833 | * The list object contains the Win32 process handle at [0], and a file | 
|  | 3834 | * count at [1], which is initialized to the total number of file | 
|  | 3835 | * handles using that list. | 
|  | 3836 | * | 
|  | 3837 | * This function closes whichever handle it is passed, and decrements | 
|  | 3838 | * the file count in the dictionary for the process handle pointed to | 
|  | 3839 | * by this file.  On the last close (when the file count reaches zero), | 
|  | 3840 | * this function will wait for the child process and then return its | 
|  | 3841 | * exit code as the result of the close() operation.  This permits the | 
|  | 3842 | * files to be closed in any order - it is always the close() of the | 
|  | 3843 | * final handle that will return the exit code. | 
| Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3844 | * | 
|  | 3845 | * NOTE: This function is currently called with the GIL released. | 
|  | 3846 | * hence we use the GILState API to manage our state. | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3847 | */ | 
|  | 3848 |  | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3849 | static int _PyPclose(FILE *file) | 
|  | 3850 | { | 
|  | 3851 | int result; | 
|  | 3852 | int exit_code; | 
|  | 3853 | int pipe_pid; | 
|  | 3854 | PyObject *procObj, *pidObj, *intObj, *fileObj; | 
|  | 3855 | int file_count; | 
|  | 3856 | #ifdef WITH_THREAD | 
| Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3857 | PyGILState_STATE state; | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3858 | #endif | 
|  | 3859 |  | 
|  | 3860 | /* Close the file handle first, to ensure it can't block the | 
|  | 3861 | * child from exiting if it's the last handle. | 
|  | 3862 | */ | 
|  | 3863 | result = fclose(file); | 
|  | 3864 |  | 
|  | 3865 | #ifdef WITH_THREAD | 
| Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3866 | state = PyGILState_Ensure(); | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3867 | #endif | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3868 | if (_PyPopenProcs) | 
|  | 3869 | { | 
|  | 3870 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && | 
|  | 3871 | (procObj = PyDict_GetItem(_PyPopenProcs, | 
|  | 3872 | fileObj)) != NULL && | 
|  | 3873 | (pidObj = PyList_GetItem(procObj,0)) != NULL && | 
|  | 3874 | (intObj = PyList_GetItem(procObj,1)) != NULL) | 
|  | 3875 | { | 
|  | 3876 | pipe_pid = (int) PyInt_AsLong(pidObj); | 
|  | 3877 | file_count = (int) PyInt_AsLong(intObj); | 
|  | 3878 |  | 
|  | 3879 | if (file_count > 1) | 
|  | 3880 | { | 
|  | 3881 | /* Still other files referencing process */ | 
|  | 3882 | file_count--; | 
|  | 3883 | PyList_SetItem(procObj,1, | 
|  | 3884 | PyInt_FromLong((long) file_count)); | 
|  | 3885 | } | 
|  | 3886 | else | 
|  | 3887 | { | 
|  | 3888 | /* Last file for this process */ | 
|  | 3889 | if (result != EOF && | 
|  | 3890 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) | 
|  | 3891 | { | 
|  | 3892 | /* extract exit status */ | 
|  | 3893 | if (WIFEXITED(exit_code)) | 
|  | 3894 | { | 
|  | 3895 | result = WEXITSTATUS(exit_code); | 
|  | 3896 | } | 
|  | 3897 | else | 
|  | 3898 | { | 
|  | 3899 | errno = EPIPE; | 
|  | 3900 | result = -1; | 
|  | 3901 | } | 
|  | 3902 | } | 
|  | 3903 | else | 
|  | 3904 | { | 
|  | 3905 | /* Indicate failure - this will cause the file object | 
|  | 3906 | * to raise an I/O error and translate the last | 
|  | 3907 | * error code from errno.  We do have a problem with | 
|  | 3908 | * last errors that overlap the normal errno table, | 
|  | 3909 | * but that's a consistent problem with the file object. | 
|  | 3910 | */ | 
|  | 3911 | result = -1; | 
|  | 3912 | } | 
|  | 3913 | } | 
|  | 3914 |  | 
|  | 3915 | /* Remove this file pointer from dictionary */ | 
|  | 3916 | PyDict_DelItem(_PyPopenProcs, fileObj); | 
|  | 3917 |  | 
|  | 3918 | if (PyDict_Size(_PyPopenProcs) == 0) | 
|  | 3919 | { | 
|  | 3920 | Py_DECREF(_PyPopenProcs); | 
|  | 3921 | _PyPopenProcs = NULL; | 
|  | 3922 | } | 
|  | 3923 |  | 
|  | 3924 | } /* if object retrieval ok */ | 
|  | 3925 |  | 
|  | 3926 | Py_XDECREF(fileObj); | 
|  | 3927 | } /* if _PyPopenProcs */ | 
|  | 3928 |  | 
|  | 3929 | #ifdef WITH_THREAD | 
| Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3930 | PyGILState_Release(state); | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3931 | #endif | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3932 | return result; | 
|  | 3933 | } | 
|  | 3934 |  | 
|  | 3935 | #endif /* PYCC_??? */ | 
|  | 3936 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3937 | #elif defined(MS_WINDOWS) | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3938 |  | 
|  | 3939 | /* | 
|  | 3940 | * Portable 'popen' replacement for Win32. | 
|  | 3941 | * | 
|  | 3942 | * Written by Bill Tutt <billtut@microsoft.com>.  Minor tweaks | 
|  | 3943 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3944 | * Return code handling by David Bolen <db3l@fitlinxx.com>. | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3945 | */ | 
|  | 3946 |  | 
|  | 3947 | #include <malloc.h> | 
|  | 3948 | #include <io.h> | 
|  | 3949 | #include <fcntl.h> | 
|  | 3950 |  | 
|  | 3951 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ | 
|  | 3952 | #define POPEN_1 1 | 
|  | 3953 | #define POPEN_2 2 | 
|  | 3954 | #define POPEN_3 3 | 
|  | 3955 | #define POPEN_4 4 | 
|  | 3956 |  | 
|  | 3957 | static PyObject *_PyPopen(char *, int, int); | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3958 | static int _PyPclose(FILE *file); | 
|  | 3959 |  | 
|  | 3960 | /* | 
|  | 3961 | * Internal dictionary mapping popen* file pointers to process handles, | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3962 | * for use when retrieving the process exit code.  See _PyPclose() below | 
|  | 3963 | * for more information on this dictionary's use. | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3964 | */ | 
|  | 3965 | static PyObject *_PyPopenProcs = NULL; | 
|  | 3966 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3967 |  | 
|  | 3968 | /* popen that works from a GUI. | 
|  | 3969 | * | 
|  | 3970 | * The result of this function is a pipe (file) connected to the | 
|  | 3971 | * processes stdin or stdout, depending on the requested mode. | 
|  | 3972 | */ | 
|  | 3973 |  | 
|  | 3974 | static PyObject * | 
|  | 3975 | posix_popen(PyObject *self, PyObject *args) | 
|  | 3976 | { | 
| Raymond Hettinger | b5cb665 | 2003-09-01 22:25:41 +0000 | [diff] [blame] | 3977 | PyObject *f; | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3978 | int tm = 0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3979 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3980 | char *cmdstring; | 
|  | 3981 | char *mode = "r"; | 
| Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3982 | int bufsize = -1; | 
| Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3983 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3984 | return NULL; | 
|  | 3985 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3986 | if (*mode == 'r') | 
|  | 3987 | tm = _O_RDONLY; | 
|  | 3988 | else if (*mode != 'w') { | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3989 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3990 | return NULL; | 
|  | 3991 | } else | 
|  | 3992 | tm = _O_WRONLY; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3993 |  | 
| Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3994 | if (bufsize != -1) { | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3995 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); | 
| Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3996 | return NULL; | 
|  | 3997 | } | 
|  | 3998 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3999 | if (*(mode+1) == 't') | 
| Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4000 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4001 | else if (*(mode+1) == 'b') | 
| Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4002 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4003 | else | 
|  | 4004 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); | 
|  | 4005 |  | 
|  | 4006 | return f; | 
|  | 4007 | } | 
|  | 4008 |  | 
|  | 4009 | /* Variation on win32pipe.popen | 
|  | 4010 | * | 
|  | 4011 | * The result of this function is a pipe (file) connected to the | 
|  | 4012 | * process's stdin, and a pipe connected to the process's stdout. | 
|  | 4013 | */ | 
|  | 4014 |  | 
|  | 4015 | static PyObject * | 
|  | 4016 | win32_popen2(PyObject *self, PyObject  *args) | 
|  | 4017 | { | 
|  | 4018 | PyObject *f; | 
|  | 4019 | int tm=0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4020 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4021 | char *cmdstring; | 
|  | 4022 | char *mode = "t"; | 
| Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4023 | int bufsize = -1; | 
|  | 4024 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4025 | return NULL; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4026 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4027 | if (*mode == 't') | 
|  | 4028 | tm = _O_TEXT; | 
|  | 4029 | else if (*mode != 'b') { | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4030 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4031 | return NULL; | 
|  | 4032 | } else | 
|  | 4033 | tm = _O_BINARY; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4034 |  | 
| Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4035 | if (bufsize != -1) { | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4036 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); | 
| Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4037 | return NULL; | 
|  | 4038 | } | 
|  | 4039 |  | 
|  | 4040 | f = _PyPopen(cmdstring, tm, POPEN_2); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4041 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4042 | return f; | 
|  | 4043 | } | 
|  | 4044 |  | 
|  | 4045 | /* | 
|  | 4046 | * Variation on <om win32pipe.popen> | 
| Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4047 | * | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4048 | * The result of this function is 3 pipes - the process's stdin, | 
|  | 4049 | * stdout and stderr | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4050 | */ | 
|  | 4051 |  | 
|  | 4052 | static PyObject * | 
|  | 4053 | win32_popen3(PyObject *self, PyObject *args) | 
|  | 4054 | { | 
|  | 4055 | PyObject *f; | 
|  | 4056 | int tm = 0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4057 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4058 | char *cmdstring; | 
|  | 4059 | char *mode = "t"; | 
| Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4060 | int bufsize = -1; | 
|  | 4061 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4062 | return NULL; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4063 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4064 | if (*mode == 't') | 
|  | 4065 | tm = _O_TEXT; | 
|  | 4066 | else if (*mode != 'b') { | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4067 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4068 | return NULL; | 
|  | 4069 | } else | 
|  | 4070 | tm = _O_BINARY; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4071 |  | 
| Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4072 | if (bufsize != -1) { | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4073 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); | 
| Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4074 | return NULL; | 
|  | 4075 | } | 
|  | 4076 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4077 | f = _PyPopen(cmdstring, tm, POPEN_3); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4078 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4079 | return f; | 
|  | 4080 | } | 
|  | 4081 |  | 
|  | 4082 | /* | 
|  | 4083 | * Variation on win32pipe.popen | 
|  | 4084 | * | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4085 | * The result of this function is 2 pipes - the processes stdin, | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4086 | * and stdout+stderr combined as a single pipe. | 
|  | 4087 | */ | 
|  | 4088 |  | 
|  | 4089 | static PyObject * | 
|  | 4090 | win32_popen4(PyObject *self, PyObject  *args) | 
|  | 4091 | { | 
|  | 4092 | PyObject *f; | 
|  | 4093 | int tm = 0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4094 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4095 | char *cmdstring; | 
|  | 4096 | char *mode = "t"; | 
| Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4097 | int bufsize = -1; | 
|  | 4098 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4099 | return NULL; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4100 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4101 | if (*mode == 't') | 
|  | 4102 | tm = _O_TEXT; | 
|  | 4103 | else if (*mode != 'b') { | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4104 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4105 | return NULL; | 
|  | 4106 | } else | 
|  | 4107 | tm = _O_BINARY; | 
| Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4108 |  | 
|  | 4109 | if (bufsize != -1) { | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4110 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); | 
| Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4111 | return NULL; | 
|  | 4112 | } | 
|  | 4113 |  | 
| Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4114 | f = _PyPopen(cmdstring, tm, POPEN_4); | 
| Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4115 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4116 | return f; | 
|  | 4117 | } | 
|  | 4118 |  | 
| Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4119 | static BOOL | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4120 | _PyPopenCreateProcess(char *cmdstring, | 
| Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4121 | HANDLE hStdin, | 
|  | 4122 | HANDLE hStdout, | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4123 | HANDLE hStderr, | 
|  | 4124 | HANDLE *hProcess) | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4125 | { | 
|  | 4126 | PROCESS_INFORMATION piProcInfo; | 
|  | 4127 | STARTUPINFO siStartInfo; | 
| Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4128 | DWORD dwProcessFlags = 0;  /* no NEW_CONSOLE by default for Ctrl+C handling */ | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4129 | char *s1,*s2, *s3 = " /c "; | 
| Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4130 | const char *szConsoleSpawn = "w9xpopen.exe"; | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4131 | int i; | 
|  | 4132 | int x; | 
|  | 4133 |  | 
|  | 4134 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { | 
| Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4135 | char *comshell; | 
|  | 4136 |  | 
| Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4137 | s1 = (char *)alloca(i); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4138 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) | 
|  | 4139 | return x; | 
| Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4140 |  | 
|  | 4141 | /* Explicitly check if we are using COMMAND.COM.  If we are | 
|  | 4142 | * then use the w9xpopen hack. | 
|  | 4143 | */ | 
|  | 4144 | comshell = s1 + x; | 
|  | 4145 | while (comshell >= s1 && *comshell != '\\') | 
|  | 4146 | --comshell; | 
|  | 4147 | ++comshell; | 
|  | 4148 |  | 
|  | 4149 | if (GetVersion() < 0x80000000 && | 
|  | 4150 | _stricmp(comshell, "command.com") != 0) { | 
|  | 4151 | /* NT/2000 and not using command.com. */ | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4152 | x = i + strlen(s3) + strlen(cmdstring) + 1; | 
| Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4153 | s2 = (char *)alloca(x); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4154 | ZeroMemory(s2, x); | 
| Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4155 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4156 | } | 
|  | 4157 | else { | 
|  | 4158 | /* | 
| Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4159 | * Oh gag, we're on Win9x or using COMMAND.COM. Use | 
|  | 4160 | * the workaround listed in KB: Q150956 | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4161 | */ | 
| Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4162 | char modulepath[_MAX_PATH]; | 
|  | 4163 | struct stat statinfo; | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4164 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); | 
|  | 4165 | for (i = x = 0; modulepath[i]; i++) | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4166 | if (modulepath[i] == SEP) | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4167 | x = i+1; | 
|  | 4168 | modulepath[x] = '\0'; | 
| Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4169 | /* Create the full-name to w9xpopen, so we can test it exists */ | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4170 | strncat(modulepath, | 
|  | 4171 | szConsoleSpawn, | 
| Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4172 | (sizeof(modulepath)/sizeof(modulepath[0])) | 
|  | 4173 | -strlen(modulepath)); | 
|  | 4174 | if (stat(modulepath, &statinfo) != 0) { | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4175 | /* Eeek - file-not-found - possibly an embedding | 
|  | 4176 | situation - see if we can locate it in sys.prefix | 
| Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4177 | */ | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4178 | strncpy(modulepath, | 
|  | 4179 | Py_GetExecPrefix(), | 
| Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4180 | sizeof(modulepath)/sizeof(modulepath[0])); | 
|  | 4181 | if (modulepath[strlen(modulepath)-1] != '\\') | 
|  | 4182 | strcat(modulepath, "\\"); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4183 | strncat(modulepath, | 
|  | 4184 | szConsoleSpawn, | 
| Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4185 | (sizeof(modulepath)/sizeof(modulepath[0])) | 
|  | 4186 | -strlen(modulepath)); | 
|  | 4187 | /* No where else to look - raise an easily identifiable | 
|  | 4188 | error, rather than leaving Windows to report | 
|  | 4189 | "file not found" - as the user is probably blissfully | 
|  | 4190 | unaware this shim EXE is used, and it will confuse them. | 
|  | 4191 | (well, it confused me for a while ;-) | 
|  | 4192 | */ | 
|  | 4193 | if (stat(modulepath, &statinfo) != 0) { | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4194 | PyErr_Format(PyExc_RuntimeError, | 
| Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4195 | "Can not locate '%s' which is needed " | 
| Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4196 | "for popen to work with your shell " | 
|  | 4197 | "or platform.", | 
| Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4198 | szConsoleSpawn); | 
|  | 4199 | return FALSE; | 
|  | 4200 | } | 
|  | 4201 | } | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4202 | x = i + strlen(s3) + strlen(cmdstring) + 1 + | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4203 | strlen(modulepath) + | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4204 | strlen(szConsoleSpawn) + 1; | 
| Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4205 |  | 
| Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4206 | s2 = (char *)alloca(x); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4207 | ZeroMemory(s2, x); | 
| Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4208 | /* To maintain correct argument passing semantics, | 
|  | 4209 | we pass the command-line as it stands, and allow | 
|  | 4210 | quoting to be applied.  w9xpopen.exe will then | 
|  | 4211 | use its argv vector, and re-quote the necessary | 
|  | 4212 | args for the ultimate child process. | 
|  | 4213 | */ | 
| Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4214 | PyOS_snprintf( | 
|  | 4215 | s2, x, | 
| Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4216 | "\"%s\" %s%s%s", | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4217 | modulepath, | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4218 | s1, | 
|  | 4219 | s3, | 
|  | 4220 | cmdstring); | 
| Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4221 | /* Not passing CREATE_NEW_CONSOLE has been known to | 
| Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4222 | cause random failures on win9x.  Specifically a | 
| Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4223 | dialog: | 
|  | 4224 | "Your program accessed mem currently in use at xxx" | 
|  | 4225 | and a hopeful warning about the stability of your | 
|  | 4226 | system. | 
|  | 4227 | Cost is Ctrl+C wont kill children, but anyone | 
|  | 4228 | who cares can have a go! | 
|  | 4229 | */ | 
|  | 4230 | dwProcessFlags |= CREATE_NEW_CONSOLE; | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4231 | } | 
|  | 4232 | } | 
|  | 4233 |  | 
|  | 4234 | /* Could be an else here to try cmd.exe / command.com in the path | 
|  | 4235 | Now we'll just error out.. */ | 
| Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4236 | else { | 
| Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4237 | PyErr_SetString(PyExc_RuntimeError, | 
|  | 4238 | "Cannot locate a COMSPEC environment variable to " | 
|  | 4239 | "use as the shell"); | 
| Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4240 | return FALSE; | 
|  | 4241 | } | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4242 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4243 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); | 
|  | 4244 | siStartInfo.cb = sizeof(STARTUPINFO); | 
|  | 4245 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; | 
|  | 4246 | siStartInfo.hStdInput = hStdin; | 
|  | 4247 | siStartInfo.hStdOutput = hStdout; | 
|  | 4248 | siStartInfo.hStdError = hStderr; | 
|  | 4249 | siStartInfo.wShowWindow = SW_HIDE; | 
|  | 4250 |  | 
|  | 4251 | if (CreateProcess(NULL, | 
| Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4252 | s2, | 
|  | 4253 | NULL, | 
|  | 4254 | NULL, | 
|  | 4255 | TRUE, | 
| Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4256 | dwProcessFlags, | 
| Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4257 | NULL, | 
|  | 4258 | NULL, | 
|  | 4259 | &siStartInfo, | 
|  | 4260 | &piProcInfo) ) { | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4261 | /* Close the handles now so anyone waiting is woken. */ | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4262 | CloseHandle(piProcInfo.hThread); | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4263 |  | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4264 | /* Return process handle */ | 
|  | 4265 | *hProcess = piProcInfo.hProcess; | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4266 | return TRUE; | 
|  | 4267 | } | 
| Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4268 | win32_error("CreateProcess", s2); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4269 | return FALSE; | 
|  | 4270 | } | 
|  | 4271 |  | 
|  | 4272 | /* The following code is based off of KB: Q190351 */ | 
|  | 4273 |  | 
|  | 4274 | static PyObject * | 
|  | 4275 | _PyPopen(char *cmdstring, int mode, int n) | 
|  | 4276 | { | 
|  | 4277 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, | 
|  | 4278 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4279 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4280 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4281 | SECURITY_ATTRIBUTES saAttr; | 
|  | 4282 | BOOL fSuccess; | 
|  | 4283 | int fd1, fd2, fd3; | 
|  | 4284 | FILE *f1, *f2, *f3; | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4285 | long file_count; | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4286 | PyObject *f; | 
|  | 4287 |  | 
|  | 4288 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); | 
|  | 4289 | saAttr.bInheritHandle = TRUE; | 
|  | 4290 | saAttr.lpSecurityDescriptor = NULL; | 
|  | 4291 |  | 
|  | 4292 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) | 
|  | 4293 | return win32_error("CreatePipe", NULL); | 
|  | 4294 |  | 
|  | 4295 | /* Create new output read handle and the input write handle. Set | 
|  | 4296 | * the inheritance properties to FALSE. Otherwise, the child inherits | 
| Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 4297 | * these handles; resulting in non-closeable handles to the pipes | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4298 | * being created. */ | 
|  | 4299 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, | 
| Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4300 | GetCurrentProcess(), &hChildStdinWrDup, 0, | 
|  | 4301 | FALSE, | 
|  | 4302 | DUPLICATE_SAME_ACCESS); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4303 | if (!fSuccess) | 
|  | 4304 | return win32_error("DuplicateHandle", NULL); | 
|  | 4305 |  | 
|  | 4306 | /* Close the inheritable version of ChildStdin | 
|  | 4307 | that we're using. */ | 
|  | 4308 | CloseHandle(hChildStdinWr); | 
|  | 4309 |  | 
|  | 4310 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) | 
|  | 4311 | return win32_error("CreatePipe", NULL); | 
|  | 4312 |  | 
|  | 4313 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, | 
| Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4314 | GetCurrentProcess(), &hChildStdoutRdDup, 0, | 
|  | 4315 | FALSE, DUPLICATE_SAME_ACCESS); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4316 | if (!fSuccess) | 
|  | 4317 | return win32_error("DuplicateHandle", NULL); | 
|  | 4318 |  | 
|  | 4319 | /* Close the inheritable version of ChildStdout | 
|  | 4320 | that we're using. */ | 
|  | 4321 | CloseHandle(hChildStdoutRd); | 
|  | 4322 |  | 
|  | 4323 | if (n != POPEN_4) { | 
|  | 4324 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) | 
|  | 4325 | return win32_error("CreatePipe", NULL); | 
| Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4326 | fSuccess = DuplicateHandle(GetCurrentProcess(), | 
|  | 4327 | hChildStderrRd, | 
|  | 4328 | GetCurrentProcess(), | 
|  | 4329 | &hChildStderrRdDup, 0, | 
|  | 4330 | FALSE, DUPLICATE_SAME_ACCESS); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4331 | if (!fSuccess) | 
|  | 4332 | return win32_error("DuplicateHandle", NULL); | 
|  | 4333 | /* Close the inheritable version of ChildStdErr that we're using. */ | 
|  | 4334 | CloseHandle(hChildStderrRd); | 
|  | 4335 | } | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4336 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4337 | switch (n) { | 
|  | 4338 | case POPEN_1: | 
|  | 4339 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { | 
|  | 4340 | case _O_WRONLY | _O_TEXT: | 
|  | 4341 | /* Case for writing to child Stdin in text mode. */ | 
|  | 4342 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); | 
|  | 4343 | f1 = _fdopen(fd1, "w"); | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4344 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4345 | PyFile_SetBufSize(f, 0); | 
|  | 4346 | /* We don't care about these pipes anymore, so close them. */ | 
|  | 4347 | CloseHandle(hChildStdoutRdDup); | 
|  | 4348 | CloseHandle(hChildStderrRdDup); | 
|  | 4349 | break; | 
|  | 4350 |  | 
|  | 4351 | case _O_RDONLY | _O_TEXT: | 
|  | 4352 | /* Case for reading from child Stdout in text mode. */ | 
|  | 4353 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); | 
|  | 4354 | f1 = _fdopen(fd1, "r"); | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4355 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4356 | PyFile_SetBufSize(f, 0); | 
|  | 4357 | /* We don't care about these pipes anymore, so close them. */ | 
|  | 4358 | CloseHandle(hChildStdinWrDup); | 
|  | 4359 | CloseHandle(hChildStderrRdDup); | 
|  | 4360 | break; | 
|  | 4361 |  | 
|  | 4362 | case _O_RDONLY | _O_BINARY: | 
|  | 4363 | /* Case for readinig from child Stdout in binary mode. */ | 
|  | 4364 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); | 
|  | 4365 | f1 = _fdopen(fd1, "rb"); | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4366 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4367 | PyFile_SetBufSize(f, 0); | 
|  | 4368 | /* We don't care about these pipes anymore, so close them. */ | 
|  | 4369 | CloseHandle(hChildStdinWrDup); | 
|  | 4370 | CloseHandle(hChildStderrRdDup); | 
|  | 4371 | break; | 
|  | 4372 |  | 
|  | 4373 | case _O_WRONLY | _O_BINARY: | 
|  | 4374 | /* Case for writing to child Stdin in binary mode. */ | 
|  | 4375 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); | 
|  | 4376 | f1 = _fdopen(fd1, "wb"); | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4377 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4378 | PyFile_SetBufSize(f, 0); | 
|  | 4379 | /* We don't care about these pipes anymore, so close them. */ | 
|  | 4380 | CloseHandle(hChildStdoutRdDup); | 
|  | 4381 | CloseHandle(hChildStderrRdDup); | 
|  | 4382 | break; | 
|  | 4383 | } | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4384 | file_count = 1; | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4385 | break; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4386 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4387 | case POPEN_2: | 
|  | 4388 | case POPEN_4: | 
|  | 4389 | { | 
|  | 4390 | char *m1, *m2; | 
|  | 4391 | PyObject *p1, *p2; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4392 |  | 
| Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4393 | if (mode & _O_TEXT) { | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4394 | m1 = "r"; | 
|  | 4395 | m2 = "w"; | 
|  | 4396 | } else { | 
|  | 4397 | m1 = "rb"; | 
|  | 4398 | m2 = "wb"; | 
|  | 4399 | } | 
|  | 4400 |  | 
|  | 4401 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); | 
|  | 4402 | f1 = _fdopen(fd1, m2); | 
|  | 4403 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); | 
|  | 4404 | f2 = _fdopen(fd2, m1); | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4405 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4406 | PyFile_SetBufSize(p1, 0); | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4407 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4408 | PyFile_SetBufSize(p2, 0); | 
|  | 4409 |  | 
|  | 4410 | if (n != 4) | 
|  | 4411 | CloseHandle(hChildStderrRdDup); | 
|  | 4412 |  | 
| Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4413 | f = PyTuple_Pack(2,p1,p2); | 
| Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4414 | Py_XDECREF(p1); | 
|  | 4415 | Py_XDECREF(p2); | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4416 | file_count = 2; | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4417 | break; | 
|  | 4418 | } | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4419 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4420 | case POPEN_3: | 
|  | 4421 | { | 
|  | 4422 | char *m1, *m2; | 
|  | 4423 | PyObject *p1, *p2, *p3; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4424 |  | 
| Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4425 | if (mode & _O_TEXT) { | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4426 | m1 = "r"; | 
|  | 4427 | m2 = "w"; | 
|  | 4428 | } else { | 
|  | 4429 | m1 = "rb"; | 
|  | 4430 | m2 = "wb"; | 
|  | 4431 | } | 
|  | 4432 |  | 
|  | 4433 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); | 
|  | 4434 | f1 = _fdopen(fd1, m2); | 
|  | 4435 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); | 
|  | 4436 | f2 = _fdopen(fd2, m1); | 
|  | 4437 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); | 
|  | 4438 | f3 = _fdopen(fd3, m1); | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4439 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4440 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); | 
|  | 4441 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4442 | PyFile_SetBufSize(p1, 0); | 
|  | 4443 | PyFile_SetBufSize(p2, 0); | 
|  | 4444 | PyFile_SetBufSize(p3, 0); | 
| Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4445 | f = PyTuple_Pack(3,p1,p2,p3); | 
| Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4446 | Py_XDECREF(p1); | 
|  | 4447 | Py_XDECREF(p2); | 
|  | 4448 | Py_XDECREF(p3); | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4449 | file_count = 3; | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4450 | break; | 
|  | 4451 | } | 
|  | 4452 | } | 
|  | 4453 |  | 
|  | 4454 | if (n == POPEN_4) { | 
|  | 4455 | if (!_PyPopenCreateProcess(cmdstring, | 
| Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4456 | hChildStdinRd, | 
|  | 4457 | hChildStdoutWr, | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4458 | hChildStdoutWr, | 
|  | 4459 | &hProcess)) | 
| Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4460 | return NULL; | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4461 | } | 
|  | 4462 | else { | 
|  | 4463 | if (!_PyPopenCreateProcess(cmdstring, | 
| Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4464 | hChildStdinRd, | 
|  | 4465 | hChildStdoutWr, | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4466 | hChildStderrWr, | 
|  | 4467 | &hProcess)) | 
| Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4468 | return NULL; | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4469 | } | 
|  | 4470 |  | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4471 | /* | 
|  | 4472 | * Insert the files we've created into the process dictionary | 
|  | 4473 | * all referencing the list with the process handle and the | 
|  | 4474 | * initial number of files (see description below in _PyPclose). | 
|  | 4475 | * Since if _PyPclose later tried to wait on a process when all | 
|  | 4476 | * handles weren't closed, it could create a deadlock with the | 
|  | 4477 | * child, we spend some energy here to try to ensure that we | 
|  | 4478 | * either insert all file handles into the dictionary or none | 
|  | 4479 | * at all.  It's a little clumsy with the various popen modes | 
|  | 4480 | * and variable number of files involved. | 
|  | 4481 | */ | 
|  | 4482 | if (!_PyPopenProcs) { | 
|  | 4483 | _PyPopenProcs = PyDict_New(); | 
|  | 4484 | } | 
|  | 4485 |  | 
|  | 4486 | if (_PyPopenProcs) { | 
|  | 4487 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; | 
|  | 4488 | int ins_rc[3]; | 
|  | 4489 |  | 
|  | 4490 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; | 
|  | 4491 | ins_rc[0]  = ins_rc[1]  = ins_rc[2]  = 0; | 
|  | 4492 |  | 
|  | 4493 | procObj = PyList_New(2); | 
|  | 4494 | hProcessObj = PyLong_FromVoidPtr(hProcess); | 
|  | 4495 | intObj = PyInt_FromLong(file_count); | 
|  | 4496 |  | 
|  | 4497 | if (procObj && hProcessObj && intObj) { | 
|  | 4498 | PyList_SetItem(procObj,0,hProcessObj); | 
|  | 4499 | PyList_SetItem(procObj,1,intObj); | 
|  | 4500 |  | 
|  | 4501 | fileObj[0] = PyLong_FromVoidPtr(f1); | 
|  | 4502 | if (fileObj[0]) { | 
|  | 4503 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, | 
|  | 4504 | fileObj[0], | 
|  | 4505 | procObj); | 
|  | 4506 | } | 
|  | 4507 | if (file_count >= 2) { | 
|  | 4508 | fileObj[1] = PyLong_FromVoidPtr(f2); | 
|  | 4509 | if (fileObj[1]) { | 
|  | 4510 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, | 
|  | 4511 | fileObj[1], | 
|  | 4512 | procObj); | 
|  | 4513 | } | 
|  | 4514 | } | 
|  | 4515 | if (file_count >= 3) { | 
|  | 4516 | fileObj[2] = PyLong_FromVoidPtr(f3); | 
|  | 4517 | if (fileObj[2]) { | 
|  | 4518 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, | 
|  | 4519 | fileObj[2], | 
|  | 4520 | procObj); | 
|  | 4521 | } | 
|  | 4522 | } | 
|  | 4523 |  | 
|  | 4524 | if (ins_rc[0] < 0 || !fileObj[0] || | 
|  | 4525 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || | 
|  | 4526 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { | 
|  | 4527 | /* Something failed - remove any dictionary | 
|  | 4528 | * entries that did make it. | 
|  | 4529 | */ | 
|  | 4530 | if (!ins_rc[0] && fileObj[0]) { | 
|  | 4531 | PyDict_DelItem(_PyPopenProcs, | 
|  | 4532 | fileObj[0]); | 
|  | 4533 | } | 
|  | 4534 | if (!ins_rc[1] && fileObj[1]) { | 
|  | 4535 | PyDict_DelItem(_PyPopenProcs, | 
|  | 4536 | fileObj[1]); | 
|  | 4537 | } | 
|  | 4538 | if (!ins_rc[2] && fileObj[2]) { | 
|  | 4539 | PyDict_DelItem(_PyPopenProcs, | 
|  | 4540 | fileObj[2]); | 
|  | 4541 | } | 
|  | 4542 | } | 
|  | 4543 | } | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4544 |  | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4545 | /* | 
|  | 4546 | * Clean up our localized references for the dictionary keys | 
|  | 4547 | * and value since PyDict_SetItem will Py_INCREF any copies | 
|  | 4548 | * that got placed in the dictionary. | 
|  | 4549 | */ | 
|  | 4550 | Py_XDECREF(procObj); | 
|  | 4551 | Py_XDECREF(fileObj[0]); | 
|  | 4552 | Py_XDECREF(fileObj[1]); | 
|  | 4553 | Py_XDECREF(fileObj[2]); | 
|  | 4554 | } | 
|  | 4555 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4556 | /* Child is launched. Close the parents copy of those pipe | 
|  | 4557 | * handles that only the child should have open.  You need to | 
|  | 4558 | * make sure that no handles to the write end of the output pipe | 
|  | 4559 | * are maintained in this process or else the pipe will not close | 
|  | 4560 | * when the child process exits and the ReadFile will hang. */ | 
|  | 4561 |  | 
|  | 4562 | if (!CloseHandle(hChildStdinRd)) | 
|  | 4563 | return win32_error("CloseHandle", NULL); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4564 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4565 | if (!CloseHandle(hChildStdoutWr)) | 
|  | 4566 | return win32_error("CloseHandle", NULL); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4567 |  | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4568 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) | 
|  | 4569 | return win32_error("CloseHandle", NULL); | 
|  | 4570 |  | 
|  | 4571 | return f; | 
|  | 4572 | } | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4573 |  | 
|  | 4574 | /* | 
|  | 4575 | * Wrapper for fclose() to use for popen* files, so we can retrieve the | 
|  | 4576 | * 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] | 4577 | * | 
|  | 4578 | * This function uses the _PyPopenProcs dictionary in order to map the | 
|  | 4579 | * input file pointer to information about the process that was | 
|  | 4580 | * originally created by the popen* call that created the file pointer. | 
|  | 4581 | * The dictionary uses the file pointer as a key (with one entry | 
|  | 4582 | * inserted for each file returned by the original popen* call) and a | 
|  | 4583 | * single list object as the value for all files from a single call. | 
|  | 4584 | * The list object contains the Win32 process handle at [0], and a file | 
|  | 4585 | * count at [1], which is initialized to the total number of file | 
|  | 4586 | * handles using that list. | 
|  | 4587 | * | 
|  | 4588 | * This function closes whichever handle it is passed, and decrements | 
|  | 4589 | * the file count in the dictionary for the process handle pointed to | 
|  | 4590 | * by this file.  On the last close (when the file count reaches zero), | 
|  | 4591 | * this function will wait for the child process and then return its | 
|  | 4592 | * exit code as the result of the close() operation.  This permits the | 
|  | 4593 | * files to be closed in any order - it is always the close() of the | 
|  | 4594 | * final handle that will return the exit code. | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4595 | * | 
|  | 4596 | * NOTE: This function is currently called with the GIL released. | 
|  | 4597 | * hence we use the GILState API to manage our state. | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4598 | */ | 
| Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4599 |  | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4600 | static int _PyPclose(FILE *file) | 
|  | 4601 | { | 
| Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4602 | int result; | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4603 | DWORD exit_code; | 
|  | 4604 | HANDLE hProcess; | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4605 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; | 
|  | 4606 | long file_count; | 
| Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4607 | #ifdef WITH_THREAD | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4608 | PyGILState_STATE state; | 
| Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4609 | #endif | 
|  | 4610 |  | 
| Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4611 | /* Close the file handle first, to ensure it can't block the | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4612 | * child from exiting if it's the last handle. | 
| Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4613 | */ | 
|  | 4614 | result = fclose(file); | 
| Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4615 | #ifdef WITH_THREAD | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4616 | state = PyGILState_Ensure(); | 
| Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4617 | #endif | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4618 | if (_PyPopenProcs) { | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4619 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && | 
|  | 4620 | (procObj = PyDict_GetItem(_PyPopenProcs, | 
|  | 4621 | fileObj)) != NULL && | 
|  | 4622 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && | 
|  | 4623 | (intObj = PyList_GetItem(procObj,1)) != NULL) { | 
|  | 4624 |  | 
|  | 4625 | hProcess = PyLong_AsVoidPtr(hProcessObj); | 
|  | 4626 | file_count = PyInt_AsLong(intObj); | 
|  | 4627 |  | 
|  | 4628 | if (file_count > 1) { | 
|  | 4629 | /* Still other files referencing process */ | 
|  | 4630 | file_count--; | 
|  | 4631 | PyList_SetItem(procObj,1, | 
|  | 4632 | PyInt_FromLong(file_count)); | 
|  | 4633 | } else { | 
|  | 4634 | /* Last file for this process */ | 
| Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4635 | if (result != EOF && | 
|  | 4636 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && | 
|  | 4637 | GetExitCodeProcess(hProcess, &exit_code)) { | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4638 | /* Possible truncation here in 16-bit environments, but | 
|  | 4639 | * real exit codes are just the lower byte in any event. | 
|  | 4640 | */ | 
|  | 4641 | result = exit_code; | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4642 | } else { | 
| Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4643 | /* Indicate failure - this will cause the file object | 
|  | 4644 | * to raise an I/O error and translate the last Win32 | 
|  | 4645 | * error code from errno.  We do have a problem with | 
|  | 4646 | * last errors that overlap the normal errno table, | 
|  | 4647 | * but that's a consistent problem with the file object. | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4648 | */ | 
| Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4649 | if (result != EOF) { | 
|  | 4650 | /* If the error wasn't from the fclose(), then | 
|  | 4651 | * set errno for the file object error handling. | 
|  | 4652 | */ | 
|  | 4653 | errno = GetLastError(); | 
|  | 4654 | } | 
|  | 4655 | result = -1; | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4656 | } | 
|  | 4657 |  | 
|  | 4658 | /* Free up the native handle at this point */ | 
|  | 4659 | CloseHandle(hProcess); | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4660 | } | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4661 |  | 
| Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4662 | /* Remove this file pointer from dictionary */ | 
|  | 4663 | PyDict_DelItem(_PyPopenProcs, fileObj); | 
|  | 4664 |  | 
|  | 4665 | if (PyDict_Size(_PyPopenProcs) == 0) { | 
|  | 4666 | Py_DECREF(_PyPopenProcs); | 
|  | 4667 | _PyPopenProcs = NULL; | 
|  | 4668 | } | 
|  | 4669 |  | 
|  | 4670 | } /* if object retrieval ok */ | 
|  | 4671 |  | 
|  | 4672 | Py_XDECREF(fileObj); | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4673 | } /* if _PyPopenProcs */ | 
|  | 4674 |  | 
| Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4675 | #ifdef WITH_THREAD | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4676 | PyGILState_Release(state); | 
| Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4677 | #endif | 
| Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4678 | return result; | 
|  | 4679 | } | 
| Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4680 |  | 
|  | 4681 | #else /* which OS? */ | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4682 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4683 | posix_popen(PyObject *self, PyObject *args) | 
| Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4684 | { | 
| Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4685 | char *name; | 
|  | 4686 | char *mode = "r"; | 
|  | 4687 | int bufsize = -1; | 
| Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4688 | FILE *fp; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4689 | PyObject *f; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4690 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) | 
| Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4691 | return NULL; | 
| Martin v. Löwis | 9ad853b | 2003-10-31 10:01:53 +0000 | [diff] [blame] | 4692 | /* Strip mode of binary or text modifiers */ | 
|  | 4693 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) | 
|  | 4694 | mode = "r"; | 
|  | 4695 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) | 
|  | 4696 | mode = "w"; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4697 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4698 | fp = popen(name, mode); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4699 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4700 | if (fp == NULL) | 
|  | 4701 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4702 | f = PyFile_FromFile(fp, name, mode, pclose); | 
| Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4703 | if (f != NULL) | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4704 | PyFile_SetBufSize(f, bufsize); | 
| Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4705 | return f; | 
| Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4706 | } | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4707 |  | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4708 | #endif /* PYOS_??? */ | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4709 | #endif /* HAVE_POPEN */ | 
| Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4710 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4711 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4712 | #ifdef HAVE_SETUID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4713 | PyDoc_STRVAR(posix_setuid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4714 | "setuid(uid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4715 | Set the current process's user id."); | 
|  | 4716 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4717 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4718 | posix_setuid(PyObject *self, PyObject *args) | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4719 | { | 
|  | 4720 | int uid; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4721 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4722 | return NULL; | 
|  | 4723 | if (setuid(uid) < 0) | 
|  | 4724 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4725 | Py_INCREF(Py_None); | 
|  | 4726 | return Py_None; | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4727 | } | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4728 | #endif /* HAVE_SETUID */ | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4729 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4730 |  | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4731 | #ifdef HAVE_SETEUID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4732 | PyDoc_STRVAR(posix_seteuid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4733 | "seteuid(uid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4734 | Set the current process's effective user id."); | 
|  | 4735 |  | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4736 | static PyObject * | 
|  | 4737 | posix_seteuid (PyObject *self, PyObject *args) | 
|  | 4738 | { | 
|  | 4739 | int euid; | 
|  | 4740 | if (!PyArg_ParseTuple(args, "i", &euid)) { | 
|  | 4741 | return NULL; | 
|  | 4742 | } else if (seteuid(euid) < 0) { | 
|  | 4743 | return posix_error(); | 
|  | 4744 | } else { | 
|  | 4745 | Py_INCREF(Py_None); | 
|  | 4746 | return Py_None; | 
|  | 4747 | } | 
|  | 4748 | } | 
|  | 4749 | #endif /* HAVE_SETEUID */ | 
|  | 4750 |  | 
|  | 4751 | #ifdef HAVE_SETEGID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4752 | PyDoc_STRVAR(posix_setegid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4753 | "setegid(gid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4754 | Set the current process's effective group id."); | 
|  | 4755 |  | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4756 | static PyObject * | 
|  | 4757 | posix_setegid (PyObject *self, PyObject *args) | 
|  | 4758 | { | 
|  | 4759 | int egid; | 
|  | 4760 | if (!PyArg_ParseTuple(args, "i", &egid)) { | 
|  | 4761 | return NULL; | 
|  | 4762 | } else if (setegid(egid) < 0) { | 
|  | 4763 | return posix_error(); | 
|  | 4764 | } else { | 
|  | 4765 | Py_INCREF(Py_None); | 
|  | 4766 | return Py_None; | 
|  | 4767 | } | 
|  | 4768 | } | 
|  | 4769 | #endif /* HAVE_SETEGID */ | 
|  | 4770 |  | 
|  | 4771 | #ifdef HAVE_SETREUID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4772 | PyDoc_STRVAR(posix_setreuid__doc__, | 
| Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4773 | "setreuid(ruid, euid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4774 | Set the current process's real and effective user ids."); | 
|  | 4775 |  | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4776 | static PyObject * | 
|  | 4777 | posix_setreuid (PyObject *self, PyObject *args) | 
|  | 4778 | { | 
|  | 4779 | int ruid, euid; | 
|  | 4780 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { | 
|  | 4781 | return NULL; | 
|  | 4782 | } else if (setreuid(ruid, euid) < 0) { | 
|  | 4783 | return posix_error(); | 
|  | 4784 | } else { | 
|  | 4785 | Py_INCREF(Py_None); | 
|  | 4786 | return Py_None; | 
|  | 4787 | } | 
|  | 4788 | } | 
|  | 4789 | #endif /* HAVE_SETREUID */ | 
|  | 4790 |  | 
|  | 4791 | #ifdef HAVE_SETREGID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4792 | PyDoc_STRVAR(posix_setregid__doc__, | 
| Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4793 | "setregid(rgid, egid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4794 | Set the current process's real and effective group ids."); | 
|  | 4795 |  | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4796 | static PyObject * | 
|  | 4797 | posix_setregid (PyObject *self, PyObject *args) | 
|  | 4798 | { | 
|  | 4799 | int rgid, egid; | 
|  | 4800 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { | 
|  | 4801 | return NULL; | 
|  | 4802 | } else if (setregid(rgid, egid) < 0) { | 
|  | 4803 | return posix_error(); | 
|  | 4804 | } else { | 
|  | 4805 | Py_INCREF(Py_None); | 
|  | 4806 | return Py_None; | 
|  | 4807 | } | 
|  | 4808 | } | 
|  | 4809 | #endif /* HAVE_SETREGID */ | 
|  | 4810 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4811 | #ifdef HAVE_SETGID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4812 | PyDoc_STRVAR(posix_setgid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4813 | "setgid(gid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4814 | Set the current process's group id."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4815 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4816 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4817 | posix_setgid(PyObject *self, PyObject *args) | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4818 | { | 
|  | 4819 | int gid; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4820 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4821 | return NULL; | 
|  | 4822 | if (setgid(gid) < 0) | 
|  | 4823 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4824 | Py_INCREF(Py_None); | 
|  | 4825 | return Py_None; | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4826 | } | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4827 | #endif /* HAVE_SETGID */ | 
| Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4828 |  | 
| Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4829 | #ifdef HAVE_SETGROUPS | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4830 | PyDoc_STRVAR(posix_setgroups__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4831 | "setgroups(list)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4832 | Set the groups of the current process to list."); | 
| Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4833 |  | 
|  | 4834 | static PyObject * | 
|  | 4835 | posix_setgroups(PyObject *self, PyObject *args) | 
|  | 4836 | { | 
|  | 4837 | PyObject *groups; | 
|  | 4838 | int i, len; | 
|  | 4839 | gid_t grouplist[MAX_GROUPS]; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4840 |  | 
| Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4841 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) | 
|  | 4842 | return NULL; | 
|  | 4843 | if (!PySequence_Check(groups)) { | 
|  | 4844 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); | 
|  | 4845 | return NULL; | 
|  | 4846 | } | 
|  | 4847 | len = PySequence_Size(groups); | 
|  | 4848 | if (len > MAX_GROUPS) { | 
|  | 4849 | PyErr_SetString(PyExc_ValueError, "too many groups"); | 
|  | 4850 | return NULL; | 
|  | 4851 | } | 
|  | 4852 | for(i = 0; i < len; i++) { | 
|  | 4853 | PyObject *elem; | 
|  | 4854 | elem = PySequence_GetItem(groups, i); | 
|  | 4855 | if (!elem) | 
|  | 4856 | return NULL; | 
|  | 4857 | if (!PyInt_Check(elem)) { | 
|  | 4858 | PyErr_SetString(PyExc_TypeError, | 
|  | 4859 | "groups must be integers"); | 
|  | 4860 | Py_DECREF(elem); | 
|  | 4861 | return NULL; | 
|  | 4862 | } | 
|  | 4863 | /* XXX: check that value fits into gid_t. */ | 
|  | 4864 | grouplist[i] = PyInt_AsLong(elem); | 
|  | 4865 | Py_DECREF(elem); | 
|  | 4866 | } | 
|  | 4867 |  | 
|  | 4868 | if (setgroups(len, grouplist) < 0) | 
|  | 4869 | return posix_error(); | 
|  | 4870 | Py_INCREF(Py_None); | 
|  | 4871 | return Py_None; | 
|  | 4872 | } | 
|  | 4873 | #endif /* HAVE_SETGROUPS */ | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4874 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4875 | #ifdef HAVE_WAITPID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4876 | PyDoc_STRVAR(posix_waitpid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4877 | "waitpid(pid, options) -> (pid, status)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4878 | Wait for completion of a given child process."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4879 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4880 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4881 | posix_waitpid(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4882 | { | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4883 | int pid, options; | 
|  | 4884 | #ifdef UNION_WAIT | 
|  | 4885 | union wait status; | 
|  | 4886 | #define status_i (status.w_status) | 
|  | 4887 | #else | 
|  | 4888 | int status; | 
|  | 4889 | #define status_i status | 
|  | 4890 | #endif | 
|  | 4891 | status_i = 0; | 
|  | 4892 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4893 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) | 
| Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4894 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4895 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4896 | pid = waitpid(pid, &status, options); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4897 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4898 | if (pid == -1) | 
|  | 4899 | return posix_error(); | 
| Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4900 | else | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4901 | return Py_BuildValue("ii", pid, status_i); | 
| Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4902 | } | 
|  | 4903 |  | 
| Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4904 | #elif defined(HAVE_CWAIT) | 
|  | 4905 |  | 
|  | 4906 | /* 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] | 4907 | PyDoc_STRVAR(posix_waitpid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4908 | "waitpid(pid, options) -> (pid, status << 8)\n\n" | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4909 | "Wait for completion of a given process.  options is ignored on Windows."); | 
| Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4910 |  | 
|  | 4911 | static PyObject * | 
|  | 4912 | posix_waitpid(PyObject *self, PyObject *args) | 
|  | 4913 | { | 
|  | 4914 | int pid, options; | 
|  | 4915 | int status; | 
|  | 4916 |  | 
|  | 4917 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) | 
|  | 4918 | return NULL; | 
|  | 4919 | Py_BEGIN_ALLOW_THREADS | 
|  | 4920 | pid = _cwait(&status, pid, options); | 
|  | 4921 | Py_END_ALLOW_THREADS | 
|  | 4922 | if (pid == -1) | 
|  | 4923 | return posix_error(); | 
|  | 4924 | else | 
|  | 4925 | /* shift the status left a byte so this is more like the | 
|  | 4926 | POSIX waitpid */ | 
|  | 4927 | return Py_BuildValue("ii", pid, status << 8); | 
|  | 4928 | } | 
|  | 4929 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4930 |  | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4931 | #ifdef HAVE_WAIT | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4932 | PyDoc_STRVAR(posix_wait__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4933 | "wait() -> (pid, status)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4934 | Wait for completion of a child process."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4935 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4936 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4937 | posix_wait(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4938 | { | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4939 | int pid; | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4940 | #ifdef UNION_WAIT | 
|  | 4941 | union wait status; | 
|  | 4942 | #define status_i (status.w_status) | 
| Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4943 | #else | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4944 | int status; | 
|  | 4945 | #define status_i status | 
| Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4946 | #endif | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4947 |  | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4948 | status_i = 0; | 
|  | 4949 | Py_BEGIN_ALLOW_THREADS | 
|  | 4950 | pid = wait(&status); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4951 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4952 | if (pid == -1) | 
|  | 4953 | return posix_error(); | 
|  | 4954 | else | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4955 | return Py_BuildValue("ii", pid, status_i); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4956 | #undef status_i | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4957 | } | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4958 | #endif | 
| Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4959 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4960 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4961 | PyDoc_STRVAR(posix_lstat__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4962 | "lstat(path) -> stat result\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4963 | Like stat(path), but do not follow symbolic links."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4964 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4965 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4966 | posix_lstat(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4967 | { | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4968 | #ifdef HAVE_LSTAT | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4969 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4970 | #else /* !HAVE_LSTAT */ | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4971 | #ifdef MS_WINDOWS | 
| Mark Hammond | 7edd0a9 | 2003-08-06 02:46:58 +0000 | [diff] [blame] | 4972 | return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", _wstati64); | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4973 | #else | 
|  | 4974 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); | 
|  | 4975 | #endif | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4976 | #endif /* !HAVE_LSTAT */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4977 | } | 
|  | 4978 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4979 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4980 | #ifdef HAVE_READLINK | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4981 | PyDoc_STRVAR(posix_readlink__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4982 | "readlink(path) -> path\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4983 | 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] | 4984 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4985 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4986 | posix_readlink(PyObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4987 | { | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4988 | char buf[MAXPATHLEN]; | 
| Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4989 | char *path; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4990 | int n; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4991 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4992 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4993 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4994 | n = readlink(path, buf, (int) sizeof buf); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4995 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4996 | if (n < 0) | 
| Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 4997 | return posix_error_with_filename(path); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4998 | return PyString_FromStringAndSize(buf, n); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4999 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5000 | #endif /* HAVE_READLINK */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5001 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5002 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5003 | #ifdef HAVE_SYMLINK | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5004 | PyDoc_STRVAR(posix_symlink__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5005 | "symlink(src, dst)\n\n\ | 
| Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 5006 | Create a symbolic link pointing to src named dst."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5007 |  | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5008 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5009 | posix_symlink(PyObject *self, PyObject *args) | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5010 | { | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5011 | return posix_2str(args, "etet:symlink", symlink, NULL, NULL); | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5012 | } | 
|  | 5013 | #endif /* HAVE_SYMLINK */ | 
|  | 5014 |  | 
|  | 5015 |  | 
|  | 5016 | #ifdef HAVE_TIMES | 
|  | 5017 | #ifndef HZ | 
|  | 5018 | #define HZ 60 /* Universal constant :-) */ | 
|  | 5019 | #endif /* HZ */ | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5020 |  | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5021 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) | 
|  | 5022 | static long | 
| Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5023 | system_uptime(void) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5024 | { | 
|  | 5025 | ULONG     value = 0; | 
|  | 5026 |  | 
|  | 5027 | Py_BEGIN_ALLOW_THREADS | 
|  | 5028 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); | 
|  | 5029 | Py_END_ALLOW_THREADS | 
|  | 5030 |  | 
|  | 5031 | return value; | 
|  | 5032 | } | 
|  | 5033 |  | 
|  | 5034 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5035 | posix_times(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5036 | { | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5037 | /* Currently Only Uptime is Provided -- Others Later */ | 
|  | 5038 | return Py_BuildValue("ddddd", | 
|  | 5039 | (double)0 /* t.tms_utime / HZ */, | 
|  | 5040 | (double)0 /* t.tms_stime / HZ */, | 
|  | 5041 | (double)0 /* t.tms_cutime / HZ */, | 
|  | 5042 | (double)0 /* t.tms_cstime / HZ */, | 
|  | 5043 | (double)system_uptime() / 1000); | 
|  | 5044 | } | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5045 | #else /* not OS2 */ | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5046 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5047 | posix_times(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5048 | { | 
|  | 5049 | struct tms t; | 
|  | 5050 | clock_t c; | 
| Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5051 | errno = 0; | 
|  | 5052 | c = times(&t); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5053 | if (c == (clock_t) -1) | 
|  | 5054 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5055 | return Py_BuildValue("ddddd", | 
| Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5056 | (double)t.tms_utime / HZ, | 
|  | 5057 | (double)t.tms_stime / HZ, | 
|  | 5058 | (double)t.tms_cutime / HZ, | 
|  | 5059 | (double)t.tms_cstime / HZ, | 
|  | 5060 | (double)c / HZ); | 
| Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5061 | } | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5062 | #endif /* not OS2 */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5063 | #endif /* HAVE_TIMES */ | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5064 |  | 
|  | 5065 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5066 | #ifdef MS_WINDOWS | 
| Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5067 | #define HAVE_TIMES	/* so the method table will pick it up */ | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5068 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5069 | posix_times(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5070 | { | 
|  | 5071 | FILETIME create, exit, kernel, user; | 
|  | 5072 | HANDLE hProc; | 
| Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5073 | hProc = GetCurrentProcess(); | 
| Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5074 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); | 
|  | 5075 | /* The fields of a FILETIME structure are the hi and lo part | 
|  | 5076 | of a 64-bit value expressed in 100 nanosecond units. | 
|  | 5077 | 1e7 is one second in such units; 1e-7 the inverse. | 
|  | 5078 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. | 
|  | 5079 | */ | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5080 | return Py_BuildValue( | 
|  | 5081 | "ddddd", | 
| Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5082 | (double)(kernel.dwHighDateTime*429.4967296 + | 
|  | 5083 | kernel.dwLowDateTime*1e-7), | 
|  | 5084 | (double)(user.dwHighDateTime*429.4967296 + | 
|  | 5085 | user.dwLowDateTime*1e-7), | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5086 | (double)0, | 
|  | 5087 | (double)0, | 
|  | 5088 | (double)0); | 
| Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5089 | } | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5090 | #endif /* MS_WINDOWS */ | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5091 |  | 
|  | 5092 | #ifdef HAVE_TIMES | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5093 | PyDoc_STRVAR(posix_times__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5094 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5095 | Return a tuple of floating point numbers indicating process times."); | 
| Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5096 | #endif | 
| Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5097 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5098 |  | 
| Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5099 | #ifdef HAVE_GETSID | 
|  | 5100 | PyDoc_STRVAR(posix_getsid__doc__, | 
|  | 5101 | "getsid(pid) -> sid\n\n\ | 
|  | 5102 | Call the system call getsid()."); | 
|  | 5103 |  | 
|  | 5104 | static PyObject * | 
|  | 5105 | posix_getsid(PyObject *self, PyObject *args) | 
|  | 5106 | { | 
|  | 5107 | int pid, sid; | 
|  | 5108 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) | 
|  | 5109 | return NULL; | 
|  | 5110 | sid = getsid(pid); | 
|  | 5111 | if (sid < 0) | 
|  | 5112 | return posix_error(); | 
|  | 5113 | return PyInt_FromLong((long)sid); | 
|  | 5114 | } | 
|  | 5115 | #endif /* HAVE_GETSID */ | 
|  | 5116 |  | 
|  | 5117 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5118 | #ifdef HAVE_SETSID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5119 | PyDoc_STRVAR(posix_setsid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5120 | "setsid()\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5121 | Call the system call setsid()."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5122 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5123 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5124 | posix_setsid(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5125 | { | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5126 | if (setsid() < 0) | 
|  | 5127 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5128 | Py_INCREF(Py_None); | 
|  | 5129 | return Py_None; | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5130 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5131 | #endif /* HAVE_SETSID */ | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5132 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5133 | #ifdef HAVE_SETPGID | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5134 | PyDoc_STRVAR(posix_setpgid__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5135 | "setpgid(pid, pgrp)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5136 | Call the system call setpgid()."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5137 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5138 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5139 | posix_setpgid(PyObject *self, PyObject *args) | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5140 | { | 
|  | 5141 | int pid, pgrp; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5142 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5143 | return NULL; | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5144 | if (setpgid(pid, pgrp) < 0) | 
|  | 5145 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5146 | Py_INCREF(Py_None); | 
|  | 5147 | return Py_None; | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5148 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5149 | #endif /* HAVE_SETPGID */ | 
| Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5150 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5151 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5152 | #ifdef HAVE_TCGETPGRP | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5153 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5154 | "tcgetpgrp(fd) -> pgid\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5155 | 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] | 5156 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5157 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5158 | posix_tcgetpgrp(PyObject *self, PyObject *args) | 
| Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5159 | { | 
|  | 5160 | int fd, pgid; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5161 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) | 
| Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5162 | return NULL; | 
|  | 5163 | pgid = tcgetpgrp(fd); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5164 | if (pgid < 0) | 
|  | 5165 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5166 | return PyInt_FromLong((long)pgid); | 
| Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5167 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5168 | #endif /* HAVE_TCGETPGRP */ | 
| Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5169 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5170 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5171 | #ifdef HAVE_TCSETPGRP | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5172 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5173 | "tcsetpgrp(fd, pgid)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5174 | 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] | 5175 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5176 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5177 | posix_tcsetpgrp(PyObject *self, PyObject *args) | 
| Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5178 | { | 
|  | 5179 | int fd, pgid; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5180 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) | 
| Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5181 | return NULL; | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5182 | if (tcsetpgrp(fd, pgid) < 0) | 
|  | 5183 | return posix_error(); | 
| Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5184 | Py_INCREF(Py_None); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5185 | return Py_None; | 
| Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5186 | } | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5187 | #endif /* HAVE_TCSETPGRP */ | 
| Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5188 |  | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5189 | /* Functions acting on file descriptors */ | 
|  | 5190 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5191 | PyDoc_STRVAR(posix_open__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5192 | "open(filename, flag [, mode=0777]) -> fd\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5193 | Open a file (for low level IO)."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5194 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5195 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5196 | posix_open(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5197 | { | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5198 | char *file = NULL; | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5199 | int flag; | 
|  | 5200 | int mode = 0777; | 
|  | 5201 | int fd; | 
| Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5202 |  | 
|  | 5203 | #ifdef MS_WINDOWS | 
|  | 5204 | if (unicode_file_names()) { | 
|  | 5205 | PyUnicodeObject *po; | 
|  | 5206 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { | 
|  | 5207 | Py_BEGIN_ALLOW_THREADS | 
|  | 5208 | /* PyUnicode_AS_UNICODE OK without thread | 
|  | 5209 | lock as it is a simple dereference. */ | 
|  | 5210 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); | 
|  | 5211 | Py_END_ALLOW_THREADS | 
|  | 5212 | if (fd < 0) | 
|  | 5213 | return posix_error(); | 
|  | 5214 | return PyInt_FromLong((long)fd); | 
|  | 5215 | } | 
|  | 5216 | /* Drop the argument parsing error as narrow strings | 
|  | 5217 | are also valid. */ | 
|  | 5218 | PyErr_Clear(); | 
|  | 5219 | } | 
|  | 5220 | #endif | 
|  | 5221 |  | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5222 | if (!PyArg_ParseTuple(args, "eti|i", | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5223 | Py_FileSystemDefaultEncoding, &file, | 
|  | 5224 | &flag, &mode)) | 
| Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5225 | return NULL; | 
|  | 5226 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5227 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5228 | fd = open(file, flag, mode); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5229 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5230 | if (fd < 0) | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5231 | return posix_error_with_allocated_filename(file); | 
|  | 5232 | PyMem_Free(file); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5233 | return PyInt_FromLong((long)fd); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5234 | } | 
|  | 5235 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5236 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5237 | PyDoc_STRVAR(posix_close__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5238 | "close(fd)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5239 | Close a file descriptor (for low level IO)."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5240 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5241 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5242 | posix_close(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5243 | { | 
|  | 5244 | int fd, res; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5245 | if (!PyArg_ParseTuple(args, "i:close", &fd)) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5246 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5247 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5248 | res = close(fd); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5249 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5250 | if (res < 0) | 
|  | 5251 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5252 | Py_INCREF(Py_None); | 
|  | 5253 | return Py_None; | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5254 | } | 
|  | 5255 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5256 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5257 | PyDoc_STRVAR(posix_dup__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5258 | "dup(fd) -> fd2\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5259 | Return a duplicate of a file descriptor."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5260 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5261 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5262 | posix_dup(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5263 | { | 
|  | 5264 | int fd; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5265 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5266 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5267 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5268 | fd = dup(fd); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5269 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5270 | if (fd < 0) | 
|  | 5271 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5272 | return PyInt_FromLong((long)fd); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5273 | } | 
|  | 5274 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5275 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5276 | PyDoc_STRVAR(posix_dup2__doc__, | 
| Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5277 | "dup2(old_fd, new_fd)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5278 | Duplicate file descriptor."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5279 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5280 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5281 | posix_dup2(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5282 | { | 
|  | 5283 | int fd, fd2, res; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5284 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5285 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5286 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5287 | res = dup2(fd, fd2); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5288 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5289 | if (res < 0) | 
|  | 5290 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5291 | Py_INCREF(Py_None); | 
|  | 5292 | return Py_None; | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5293 | } | 
|  | 5294 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5295 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5296 | PyDoc_STRVAR(posix_lseek__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5297 | "lseek(fd, pos, how) -> newpos\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5298 | Set the current position of a file descriptor."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5299 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5300 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5301 | posix_lseek(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5302 | { | 
|  | 5303 | int fd, how; | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5304 | #if defined(MS_WIN64) || defined(MS_WINDOWS) | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5305 | PY_LONG_LONG pos, res; | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5306 | #else | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5307 | off_t pos, res; | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5308 | #endif | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5309 | PyObject *posobj; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5310 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5311 | return NULL; | 
|  | 5312 | #ifdef SEEK_SET | 
|  | 5313 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ | 
|  | 5314 | switch (how) { | 
|  | 5315 | case 0: how = SEEK_SET; break; | 
|  | 5316 | case 1: how = SEEK_CUR; break; | 
|  | 5317 | case 2: how = SEEK_END; break; | 
|  | 5318 | } | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5319 | #endif /* SEEK_END */ | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5320 |  | 
|  | 5321 | #if !defined(HAVE_LARGEFILE_SUPPORT) | 
|  | 5322 | pos = PyInt_AsLong(posobj); | 
|  | 5323 | #else | 
|  | 5324 | pos = PyLong_Check(posobj) ? | 
|  | 5325 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); | 
|  | 5326 | #endif | 
|  | 5327 | if (PyErr_Occurred()) | 
|  | 5328 | return NULL; | 
|  | 5329 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5330 | Py_BEGIN_ALLOW_THREADS | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5331 | #if defined(MS_WIN64) || defined(MS_WINDOWS) | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5332 | res = _lseeki64(fd, pos, how); | 
|  | 5333 | #else | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5334 | res = lseek(fd, pos, how); | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5335 | #endif | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5336 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5337 | if (res < 0) | 
|  | 5338 | return posix_error(); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5339 |  | 
|  | 5340 | #if !defined(HAVE_LARGEFILE_SUPPORT) | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5341 | return PyInt_FromLong(res); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5342 | #else | 
|  | 5343 | return PyLong_FromLongLong(res); | 
|  | 5344 | #endif | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5345 | } | 
|  | 5346 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5347 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5348 | PyDoc_STRVAR(posix_read__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5349 | "read(fd, buffersize) -> string\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5350 | Read a file descriptor."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5351 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5352 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5353 | posix_read(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5354 | { | 
| Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5355 | int fd, size, n; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5356 | PyObject *buffer; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5357 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5358 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5359 | buffer = PyString_FromStringAndSize((char *)NULL, size); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5360 | if (buffer == NULL) | 
|  | 5361 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5362 | Py_BEGIN_ALLOW_THREADS | 
|  | 5363 | n = read(fd, PyString_AsString(buffer), size); | 
|  | 5364 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5365 | if (n < 0) { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5366 | Py_DECREF(buffer); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5367 | return posix_error(); | 
|  | 5368 | } | 
| Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5369 | if (n != size) | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5370 | _PyString_Resize(&buffer, n); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5371 | return buffer; | 
|  | 5372 | } | 
|  | 5373 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5374 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5375 | PyDoc_STRVAR(posix_write__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5376 | "write(fd, string) -> byteswritten\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5377 | Write a string to a file descriptor."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5378 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5379 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5380 | posix_write(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5381 | { | 
|  | 5382 | int fd, size; | 
|  | 5383 | char *buffer; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5384 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5385 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5386 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5387 | size = write(fd, buffer, size); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5388 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5389 | if (size < 0) | 
|  | 5390 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5391 | return PyInt_FromLong((long)size); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5392 | } | 
|  | 5393 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5394 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5395 | PyDoc_STRVAR(posix_fstat__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5396 | "fstat(fd) -> stat result\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5397 | Like stat(), but for an open file descriptor."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5398 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5399 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5400 | posix_fstat(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5401 | { | 
|  | 5402 | int fd; | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5403 | STRUCT_STAT st; | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5404 | int res; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5405 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5406 | return NULL; | 
| Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5407 | #ifdef __VMS | 
|  | 5408 | /* on OpenVMS we must ensure that all bytes are written to the file */ | 
|  | 5409 | fsync(fd); | 
|  | 5410 | #endif | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5411 | Py_BEGIN_ALLOW_THREADS | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5412 | res = FSTAT(fd, &st); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5413 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5414 | if (res != 0) | 
|  | 5415 | return posix_error(); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5416 |  | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5417 | return _pystat_fromstructstat(st); | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5418 | } | 
|  | 5419 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5420 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5421 | PyDoc_STRVAR(posix_fdopen__doc__, | 
| Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5422 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5423 | Return an open file object connected to a file descriptor."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5424 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5425 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5426 | posix_fdopen(PyObject *self, PyObject *args) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5427 | { | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5428 | int fd; | 
| Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5429 | char *mode = "r"; | 
|  | 5430 | int bufsize = -1; | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5431 | FILE *fp; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5432 | PyObject *f; | 
|  | 5433 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5434 | return NULL; | 
| Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5435 |  | 
| Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 5436 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { | 
|  | 5437 | PyErr_Format(PyExc_ValueError, | 
|  | 5438 | "invalid file mode '%s'", mode); | 
|  | 5439 | return NULL; | 
|  | 5440 | } | 
|  | 5441 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5442 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5443 | fp = fdopen(fd, mode); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5444 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5445 | if (fp == NULL) | 
|  | 5446 | return posix_error(); | 
| Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 5447 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); | 
| Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5448 | if (f != NULL) | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5449 | PyFile_SetBufSize(f, bufsize); | 
| Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5450 | return f; | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5451 | } | 
|  | 5452 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5453 | PyDoc_STRVAR(posix_isatty__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5454 | "isatty(fd) -> bool\n\n\ | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5455 | 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] | 5456 | connected to the slave end of a terminal."); | 
| Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5457 |  | 
|  | 5458 | static PyObject * | 
| Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5459 | posix_isatty(PyObject *self, PyObject *args) | 
| Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5460 | { | 
|  | 5461 | int fd; | 
|  | 5462 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) | 
|  | 5463 | return NULL; | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5464 | return PyBool_FromLong(isatty(fd)); | 
| Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5465 | } | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5466 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5467 | #ifdef HAVE_PIPE | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5468 | PyDoc_STRVAR(posix_pipe__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5469 | "pipe() -> (read_end, write_end)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5470 | Create a pipe."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5471 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5472 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5473 | posix_pipe(PyObject *self, PyObject *noargs) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5474 | { | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5475 | #if defined(PYOS_OS2) | 
|  | 5476 | HFILE read, write; | 
|  | 5477 | APIRET rc; | 
|  | 5478 |  | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5479 | Py_BEGIN_ALLOW_THREADS | 
|  | 5480 | rc = DosCreatePipe( &read, &write, 4096); | 
|  | 5481 | Py_END_ALLOW_THREADS | 
|  | 5482 | if (rc != NO_ERROR) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5483 | return os2_error(rc); | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5484 |  | 
|  | 5485 | return Py_BuildValue("(ii)", read, write); | 
|  | 5486 | #else | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5487 | #if !defined(MS_WINDOWS) | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5488 | int fds[2]; | 
|  | 5489 | int res; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5490 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5491 | res = pipe(fds); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5492 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5493 | if (res != 0) | 
|  | 5494 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5495 | return Py_BuildValue("(ii)", fds[0], fds[1]); | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5496 | #else /* MS_WINDOWS */ | 
| Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5497 | HANDLE read, write; | 
| Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5498 | int read_fd, write_fd; | 
| Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5499 | BOOL ok; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5500 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5501 | ok = CreatePipe(&read, &write, NULL, 0); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5502 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5503 | if (!ok) | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5504 | return win32_error("CreatePipe", NULL); | 
| Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5505 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); | 
|  | 5506 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); | 
| Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5507 | return Py_BuildValue("(ii)", read_fd, write_fd); | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5508 | #endif /* MS_WINDOWS */ | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5509 | #endif | 
| Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5510 | } | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5511 | #endif  /* HAVE_PIPE */ | 
|  | 5512 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5513 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5514 | #ifdef HAVE_MKFIFO | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5515 | PyDoc_STRVAR(posix_mkfifo__doc__, | 
| Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5516 | "mkfifo(filename [, mode=0666])\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5517 | Create a FIFO (a POSIX named pipe)."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5518 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5519 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5520 | posix_mkfifo(PyObject *self, PyObject *args) | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5521 | { | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5522 | char *filename; | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5523 | int mode = 0666; | 
|  | 5524 | int res; | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5525 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5526 | return NULL; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5527 | Py_BEGIN_ALLOW_THREADS | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5528 | res = mkfifo(filename, mode); | 
|  | 5529 | Py_END_ALLOW_THREADS | 
|  | 5530 | if (res < 0) | 
|  | 5531 | return posix_error(); | 
|  | 5532 | Py_INCREF(Py_None); | 
|  | 5533 | return Py_None; | 
|  | 5534 | } | 
|  | 5535 | #endif | 
|  | 5536 |  | 
|  | 5537 |  | 
| Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5538 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5539 | PyDoc_STRVAR(posix_mknod__doc__, | 
| Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5540 | "mknod(filename [, mode=0600, device])\n\n\ | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5541 | Create a filesystem node (file, device special file or named pipe)\n\ | 
|  | 5542 | named filename. mode specifies both the permissions to use and the\n\ | 
|  | 5543 | type of node to be created, being combined (bitwise OR) with one of\n\ | 
|  | 5544 | 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] | 5545 | device defines the newly created device special file (probably using\n\ | 
|  | 5546 | os.makedev()), otherwise it is ignored."); | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5547 |  | 
|  | 5548 |  | 
|  | 5549 | static PyObject * | 
|  | 5550 | posix_mknod(PyObject *self, PyObject *args) | 
|  | 5551 | { | 
|  | 5552 | char *filename; | 
|  | 5553 | int mode = 0600; | 
| Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5554 | int device = 0; | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5555 | int res; | 
| Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5556 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5557 | return NULL; | 
|  | 5558 | Py_BEGIN_ALLOW_THREADS | 
| Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5559 | res = mknod(filename, mode, device); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5560 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5561 | if (res < 0) | 
|  | 5562 | return posix_error(); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5563 | Py_INCREF(Py_None); | 
|  | 5564 | return Py_None; | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5565 | } | 
|  | 5566 | #endif | 
|  | 5567 |  | 
| Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5568 | #ifdef HAVE_DEVICE_MACROS | 
|  | 5569 | PyDoc_STRVAR(posix_major__doc__, | 
|  | 5570 | "major(device) -> major number\n\ | 
|  | 5571 | Extracts a device major number from a raw device number."); | 
|  | 5572 |  | 
|  | 5573 | static PyObject * | 
|  | 5574 | posix_major(PyObject *self, PyObject *args) | 
|  | 5575 | { | 
|  | 5576 | int device; | 
|  | 5577 | if (!PyArg_ParseTuple(args, "i:major", &device)) | 
|  | 5578 | return NULL; | 
|  | 5579 | return PyInt_FromLong((long)major(device)); | 
|  | 5580 | } | 
|  | 5581 |  | 
|  | 5582 | PyDoc_STRVAR(posix_minor__doc__, | 
|  | 5583 | "minor(device) -> minor number\n\ | 
|  | 5584 | Extracts a device minor number from a raw device number."); | 
|  | 5585 |  | 
|  | 5586 | static PyObject * | 
|  | 5587 | posix_minor(PyObject *self, PyObject *args) | 
|  | 5588 | { | 
|  | 5589 | int device; | 
|  | 5590 | if (!PyArg_ParseTuple(args, "i:minor", &device)) | 
|  | 5591 | return NULL; | 
|  | 5592 | return PyInt_FromLong((long)minor(device)); | 
|  | 5593 | } | 
|  | 5594 |  | 
|  | 5595 | PyDoc_STRVAR(posix_makedev__doc__, | 
|  | 5596 | "makedev(major, minor) -> device number\n\ | 
|  | 5597 | Composes a raw device number from the major and minor device numbers."); | 
|  | 5598 |  | 
|  | 5599 | static PyObject * | 
|  | 5600 | posix_makedev(PyObject *self, PyObject *args) | 
|  | 5601 | { | 
|  | 5602 | int major, minor; | 
|  | 5603 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) | 
|  | 5604 | return NULL; | 
|  | 5605 | return PyInt_FromLong((long)makedev(major, minor)); | 
|  | 5606 | } | 
|  | 5607 | #endif /* device macros */ | 
|  | 5608 |  | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5609 |  | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5610 | #ifdef HAVE_FTRUNCATE | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5611 | PyDoc_STRVAR(posix_ftruncate__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5612 | "ftruncate(fd, length)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5613 | Truncate a file to a specified length."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5614 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5615 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5616 | posix_ftruncate(PyObject *self, PyObject *args) | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5617 | { | 
|  | 5618 | int fd; | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5619 | off_t length; | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5620 | int res; | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5621 | PyObject *lenobj; | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5622 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5623 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5624 | return NULL; | 
|  | 5625 |  | 
|  | 5626 | #if !defined(HAVE_LARGEFILE_SUPPORT) | 
|  | 5627 | length = PyInt_AsLong(lenobj); | 
|  | 5628 | #else | 
|  | 5629 | length = PyLong_Check(lenobj) ? | 
|  | 5630 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); | 
|  | 5631 | #endif | 
|  | 5632 | if (PyErr_Occurred()) | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5633 | return NULL; | 
|  | 5634 |  | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5635 | Py_BEGIN_ALLOW_THREADS | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5636 | res = ftruncate(fd, length); | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5637 | Py_END_ALLOW_THREADS | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5638 | if (res < 0) { | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5639 | PyErr_SetFromErrno(PyExc_IOError); | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5640 | return NULL; | 
|  | 5641 | } | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5642 | Py_INCREF(Py_None); | 
|  | 5643 | return Py_None; | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5644 | } | 
|  | 5645 | #endif | 
| Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5646 |  | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5647 | #ifdef HAVE_PUTENV | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5648 | PyDoc_STRVAR(posix_putenv__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5649 | "putenv(key, value)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5650 | Change or add an environment variable."); | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5651 |  | 
| Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5652 | /* Save putenv() parameters as values here, so we can collect them when they | 
|  | 5653 | * get re-set with another call for the same key. */ | 
|  | 5654 | static PyObject *posix_putenv_garbage; | 
|  | 5655 |  | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5656 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5657 | posix_putenv(PyObject *self, PyObject *args) | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5658 | { | 
|  | 5659 | char *s1, *s2; | 
|  | 5660 | char *new; | 
| Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5661 | PyObject *newstr; | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5662 | size_t len; | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5663 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5664 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5665 | return NULL; | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5666 |  | 
|  | 5667 | #if defined(PYOS_OS2) | 
|  | 5668 | if (stricmp(s1, "BEGINLIBPATH") == 0) { | 
|  | 5669 | APIRET rc; | 
|  | 5670 |  | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5671 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); | 
|  | 5672 | if (rc != NO_ERROR) | 
|  | 5673 | return os2_error(rc); | 
|  | 5674 |  | 
|  | 5675 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { | 
|  | 5676 | APIRET rc; | 
|  | 5677 |  | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5678 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); | 
|  | 5679 | if (rc != NO_ERROR) | 
|  | 5680 | return os2_error(rc); | 
|  | 5681 | } else { | 
|  | 5682 | #endif | 
|  | 5683 |  | 
| Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5684 | /* XXX This can leak memory -- not easy to fix :-( */ | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5685 | len = strlen(s1) + strlen(s2) + 2; | 
|  | 5686 | /* len includes space for a trailing \0; the size arg to | 
|  | 5687 | PyString_FromStringAndSize does not count that */ | 
|  | 5688 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); | 
| Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5689 | if (newstr == NULL) | 
|  | 5690 | return PyErr_NoMemory(); | 
|  | 5691 | new = PyString_AS_STRING(newstr); | 
| Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5692 | PyOS_snprintf(new, len, "%s=%s", s1, s2); | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5693 | if (putenv(new)) { | 
| Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5694 | Py_DECREF(newstr); | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5695 | posix_error(); | 
|  | 5696 | return NULL; | 
|  | 5697 | } | 
| Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5698 | /* Install the first arg and newstr in posix_putenv_garbage; | 
|  | 5699 | * this will cause previous value to be collected.  This has to | 
|  | 5700 | * happen after the real putenv() call because the old value | 
|  | 5701 | * was still accessible until then. */ | 
|  | 5702 | if (PyDict_SetItem(posix_putenv_garbage, | 
|  | 5703 | PyTuple_GET_ITEM(args, 0), newstr)) { | 
|  | 5704 | /* really not much we can do; just leak */ | 
|  | 5705 | PyErr_Clear(); | 
|  | 5706 | } | 
|  | 5707 | else { | 
|  | 5708 | Py_DECREF(newstr); | 
|  | 5709 | } | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5710 |  | 
|  | 5711 | #if defined(PYOS_OS2) | 
|  | 5712 | } | 
|  | 5713 | #endif | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5714 | Py_INCREF(Py_None); | 
|  | 5715 | return Py_None; | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5716 | } | 
| Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5717 | #endif /* putenv */ | 
|  | 5718 |  | 
| Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5719 | #ifdef HAVE_UNSETENV | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5720 | PyDoc_STRVAR(posix_unsetenv__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5721 | "unsetenv(key)\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5722 | Delete an environment variable."); | 
| Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5723 |  | 
|  | 5724 | static PyObject * | 
|  | 5725 | posix_unsetenv(PyObject *self, PyObject *args) | 
|  | 5726 | { | 
|  | 5727 | char *s1; | 
|  | 5728 |  | 
|  | 5729 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) | 
|  | 5730 | return NULL; | 
|  | 5731 |  | 
|  | 5732 | unsetenv(s1); | 
|  | 5733 |  | 
|  | 5734 | /* Remove the key from posix_putenv_garbage; | 
|  | 5735 | * this will cause it to be collected.  This has to | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5736 | * happen after the real unsetenv() call because the | 
| Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5737 | * old value was still accessible until then. | 
|  | 5738 | */ | 
|  | 5739 | if (PyDict_DelItem(posix_putenv_garbage, | 
|  | 5740 | PyTuple_GET_ITEM(args, 0))) { | 
|  | 5741 | /* really not much we can do; just leak */ | 
|  | 5742 | PyErr_Clear(); | 
|  | 5743 | } | 
|  | 5744 |  | 
|  | 5745 | Py_INCREF(Py_None); | 
|  | 5746 | return Py_None; | 
|  | 5747 | } | 
|  | 5748 | #endif /* unsetenv */ | 
|  | 5749 |  | 
| Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5750 | #ifdef HAVE_STRERROR | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5751 | PyDoc_STRVAR(posix_strerror__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5752 | "strerror(code) -> string\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5753 | Translate an error code to a message string."); | 
| Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5754 |  | 
| Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5755 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5756 | posix_strerror(PyObject *self, PyObject *args) | 
| Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5757 | { | 
|  | 5758 | int code; | 
|  | 5759 | char *message; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5760 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) | 
| Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5761 | return NULL; | 
|  | 5762 | message = strerror(code); | 
|  | 5763 | if (message == NULL) { | 
|  | 5764 | PyErr_SetString(PyExc_ValueError, | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5765 | "strerror() argument out of range"); | 
| Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5766 | return NULL; | 
|  | 5767 | } | 
|  | 5768 | return PyString_FromString(message); | 
|  | 5769 | } | 
|  | 5770 | #endif /* strerror */ | 
|  | 5771 |  | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5772 |  | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5773 | #ifdef HAVE_SYS_WAIT_H | 
|  | 5774 |  | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5775 | #ifdef WCOREDUMP | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5776 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5777 | "WCOREDUMP(status) -> bool\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5778 | 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] | 5779 |  | 
|  | 5780 | static PyObject * | 
|  | 5781 | posix_WCOREDUMP(PyObject *self, PyObject *args) | 
|  | 5782 | { | 
|  | 5783 | #ifdef UNION_WAIT | 
|  | 5784 | union wait status; | 
|  | 5785 | #define status_i (status.w_status) | 
|  | 5786 | #else | 
|  | 5787 | int status; | 
|  | 5788 | #define status_i status | 
|  | 5789 | #endif | 
|  | 5790 | status_i = 0; | 
|  | 5791 |  | 
|  | 5792 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i)) | 
|  | 5793 | { | 
|  | 5794 | return NULL; | 
|  | 5795 | } | 
|  | 5796 |  | 
|  | 5797 | return PyBool_FromLong(WCOREDUMP(status)); | 
|  | 5798 | #undef status_i | 
|  | 5799 | } | 
|  | 5800 | #endif /* WCOREDUMP */ | 
|  | 5801 |  | 
|  | 5802 | #ifdef WIFCONTINUED | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5803 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5804 | "WIFCONTINUED(status) -> bool\n\n\ | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5805 | 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] | 5806 | job control stop."); | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5807 |  | 
|  | 5808 | static PyObject * | 
| Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5809 | posix_WIFCONTINUED(PyObject *self, PyObject *args) | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5810 | { | 
|  | 5811 | #ifdef UNION_WAIT | 
|  | 5812 | union wait status; | 
|  | 5813 | #define status_i (status.w_status) | 
|  | 5814 | #else | 
|  | 5815 | int status; | 
|  | 5816 | #define status_i status | 
|  | 5817 | #endif | 
|  | 5818 | status_i = 0; | 
|  | 5819 |  | 
|  | 5820 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i)) | 
|  | 5821 | { | 
|  | 5822 | return NULL; | 
|  | 5823 | } | 
|  | 5824 |  | 
| Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5825 | return PyBool_FromLong(WIFCONTINUED(status)); | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5826 | #undef status_i | 
|  | 5827 | } | 
|  | 5828 | #endif /* WIFCONTINUED */ | 
|  | 5829 |  | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5830 | #ifdef WIFSTOPPED | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5831 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5832 | "WIFSTOPPED(status) -> bool\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5833 | Return True if the process returning 'status' was stopped."); | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5834 |  | 
|  | 5835 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5836 | posix_WIFSTOPPED(PyObject *self, PyObject *args) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5837 | { | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5838 | #ifdef UNION_WAIT | 
|  | 5839 | union wait status; | 
|  | 5840 | #define status_i (status.w_status) | 
|  | 5841 | #else | 
|  | 5842 | int status; | 
|  | 5843 | #define status_i status | 
|  | 5844 | #endif | 
|  | 5845 | status_i = 0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5846 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5847 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5848 | { | 
|  | 5849 | return NULL; | 
|  | 5850 | } | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5851 |  | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5852 | return PyBool_FromLong(WIFSTOPPED(status)); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5853 | #undef status_i | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5854 | } | 
|  | 5855 | #endif /* WIFSTOPPED */ | 
|  | 5856 |  | 
|  | 5857 | #ifdef WIFSIGNALED | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5858 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5859 | "WIFSIGNALED(status) -> bool\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5860 | 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] | 5861 |  | 
|  | 5862 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5863 | posix_WIFSIGNALED(PyObject *self, PyObject *args) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5864 | { | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5865 | #ifdef UNION_WAIT | 
|  | 5866 | union wait status; | 
|  | 5867 | #define status_i (status.w_status) | 
|  | 5868 | #else | 
|  | 5869 | int status; | 
|  | 5870 | #define status_i status | 
|  | 5871 | #endif | 
|  | 5872 | status_i = 0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5873 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5874 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5875 | { | 
|  | 5876 | return NULL; | 
|  | 5877 | } | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5878 |  | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5879 | return PyBool_FromLong(WIFSIGNALED(status)); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5880 | #undef status_i | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5881 | } | 
|  | 5882 | #endif /* WIFSIGNALED */ | 
|  | 5883 |  | 
|  | 5884 | #ifdef WIFEXITED | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5885 | PyDoc_STRVAR(posix_WIFEXITED__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5886 | "WIFEXITED(status) -> bool\n\n\ | 
| Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5887 | 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] | 5888 | system call."); | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5889 |  | 
|  | 5890 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5891 | posix_WIFEXITED(PyObject *self, PyObject *args) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5892 | { | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5893 | #ifdef UNION_WAIT | 
|  | 5894 | union wait status; | 
|  | 5895 | #define status_i (status.w_status) | 
|  | 5896 | #else | 
|  | 5897 | int status; | 
|  | 5898 | #define status_i status | 
|  | 5899 | #endif | 
|  | 5900 | status_i = 0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5901 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5902 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5903 | { | 
|  | 5904 | return NULL; | 
|  | 5905 | } | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5906 |  | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5907 | return PyBool_FromLong(WIFEXITED(status)); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5908 | #undef status_i | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5909 | } | 
|  | 5910 | #endif /* WIFEXITED */ | 
|  | 5911 |  | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5912 | #ifdef WEXITSTATUS | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5913 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5914 | "WEXITSTATUS(status) -> integer\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5915 | Return the process return code from 'status'."); | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5916 |  | 
|  | 5917 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5918 | posix_WEXITSTATUS(PyObject *self, PyObject *args) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5919 | { | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5920 | #ifdef UNION_WAIT | 
|  | 5921 | union wait status; | 
|  | 5922 | #define status_i (status.w_status) | 
|  | 5923 | #else | 
|  | 5924 | int status; | 
|  | 5925 | #define status_i status | 
|  | 5926 | #endif | 
|  | 5927 | status_i = 0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5928 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5929 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5930 | { | 
|  | 5931 | return NULL; | 
|  | 5932 | } | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5933 |  | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5934 | return Py_BuildValue("i", WEXITSTATUS(status)); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5935 | #undef status_i | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5936 | } | 
|  | 5937 | #endif /* WEXITSTATUS */ | 
|  | 5938 |  | 
|  | 5939 | #ifdef WTERMSIG | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5940 | PyDoc_STRVAR(posix_WTERMSIG__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5941 | "WTERMSIG(status) -> integer\n\n\ | 
| Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5942 | 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] | 5943 | value."); | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5944 |  | 
|  | 5945 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5946 | posix_WTERMSIG(PyObject *self, PyObject *args) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5947 | { | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5948 | #ifdef UNION_WAIT | 
|  | 5949 | union wait status; | 
|  | 5950 | #define status_i (status.w_status) | 
|  | 5951 | #else | 
|  | 5952 | int status; | 
|  | 5953 | #define status_i status | 
|  | 5954 | #endif | 
|  | 5955 | status_i = 0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5956 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5957 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5958 | { | 
|  | 5959 | return NULL; | 
|  | 5960 | } | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5961 |  | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5962 | return Py_BuildValue("i", WTERMSIG(status)); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5963 | #undef status_i | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5964 | } | 
|  | 5965 | #endif /* WTERMSIG */ | 
|  | 5966 |  | 
|  | 5967 | #ifdef WSTOPSIG | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5968 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5969 | "WSTOPSIG(status) -> integer\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5970 | Return the signal that stopped the process that provided\n\ | 
|  | 5971 | the 'status' value."); | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5972 |  | 
|  | 5973 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5974 | posix_WSTOPSIG(PyObject *self, PyObject *args) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5975 | { | 
| Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5976 | #ifdef UNION_WAIT | 
|  | 5977 | union wait status; | 
|  | 5978 | #define status_i (status.w_status) | 
|  | 5979 | #else | 
|  | 5980 | int status; | 
|  | 5981 | #define status_i status | 
|  | 5982 | #endif | 
|  | 5983 | status_i = 0; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5984 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5985 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5986 | { | 
|  | 5987 | return NULL; | 
|  | 5988 | } | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5989 |  | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5990 | return Py_BuildValue("i", WSTOPSIG(status)); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5991 | #undef status_i | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5992 | } | 
|  | 5993 | #endif /* WSTOPSIG */ | 
|  | 5994 |  | 
|  | 5995 | #endif /* HAVE_SYS_WAIT_H */ | 
|  | 5996 |  | 
|  | 5997 |  | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5998 | #if defined(HAVE_FSTATVFS) | 
| Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5999 | #ifdef _SCO_DS | 
|  | 6000 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the | 
|  | 6001 | needed definitions in sys/statvfs.h */ | 
|  | 6002 | #define _SVID3 | 
|  | 6003 | #endif | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6004 | #include <sys/statvfs.h> | 
|  | 6005 |  | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6006 | static PyObject* | 
|  | 6007 | _pystatvfs_fromstructstatvfs(struct statvfs st) { | 
|  | 6008 | PyObject *v = PyStructSequence_New(&StatVFSResultType); | 
|  | 6009 | if (v == NULL) | 
|  | 6010 | return NULL; | 
|  | 6011 |  | 
|  | 6012 | #if !defined(HAVE_LARGEFILE_SUPPORT) | 
|  | 6013 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); | 
|  | 6014 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); | 
|  | 6015 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); | 
|  | 6016 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); | 
|  | 6017 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); | 
|  | 6018 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); | 
|  | 6019 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); | 
|  | 6020 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); | 
|  | 6021 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); | 
|  | 6022 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); | 
|  | 6023 | #else | 
|  | 6024 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); | 
|  | 6025 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6026 | PyStructSequence_SET_ITEM(v, 2, | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6027 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6028 | PyStructSequence_SET_ITEM(v, 3, | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6029 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6030 | PyStructSequence_SET_ITEM(v, 4, | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6031 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6032 | PyStructSequence_SET_ITEM(v, 5, | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6033 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6034 | PyStructSequence_SET_ITEM(v, 6, | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6035 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6036 | PyStructSequence_SET_ITEM(v, 7, | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6037 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6038 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); | 
|  | 6039 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); | 
|  | 6040 | #endif | 
|  | 6041 |  | 
|  | 6042 | return v; | 
|  | 6043 | } | 
|  | 6044 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6045 | PyDoc_STRVAR(posix_fstatvfs__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6046 | "fstatvfs(fd) -> statvfs result\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6047 | Perform an fstatvfs system call on the given fd."); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6048 |  | 
|  | 6049 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6050 | posix_fstatvfs(PyObject *self, PyObject *args) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6051 | { | 
|  | 6052 | int fd, res; | 
|  | 6053 | struct statvfs st; | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6054 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6055 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6056 | return NULL; | 
|  | 6057 | Py_BEGIN_ALLOW_THREADS | 
|  | 6058 | res = fstatvfs(fd, &st); | 
|  | 6059 | Py_END_ALLOW_THREADS | 
|  | 6060 | if (res != 0) | 
|  | 6061 | return posix_error(); | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6062 |  | 
|  | 6063 | return _pystatvfs_fromstructstatvfs(st); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6064 | } | 
|  | 6065 | #endif /* HAVE_FSTATVFS */ | 
|  | 6066 |  | 
|  | 6067 |  | 
|  | 6068 | #if defined(HAVE_STATVFS) | 
|  | 6069 | #include <sys/statvfs.h> | 
|  | 6070 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6071 | PyDoc_STRVAR(posix_statvfs__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6072 | "statvfs(path) -> statvfs result\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6073 | Perform a statvfs system call on the given path."); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6074 |  | 
|  | 6075 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6076 | posix_statvfs(PyObject *self, PyObject *args) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6077 | { | 
|  | 6078 | char *path; | 
|  | 6079 | int res; | 
|  | 6080 | struct statvfs st; | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6081 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6082 | return NULL; | 
|  | 6083 | Py_BEGIN_ALLOW_THREADS | 
|  | 6084 | res = statvfs(path, &st); | 
|  | 6085 | Py_END_ALLOW_THREADS | 
|  | 6086 | if (res != 0) | 
|  | 6087 | return posix_error_with_filename(path); | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6088 |  | 
|  | 6089 | return _pystatvfs_fromstructstatvfs(st); | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6090 | } | 
|  | 6091 | #endif /* HAVE_STATVFS */ | 
|  | 6092 |  | 
|  | 6093 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6094 | #ifdef HAVE_TEMPNAM | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6095 | PyDoc_STRVAR(posix_tempnam__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6096 | "tempnam([dir[, prefix]]) -> string\n\n\ | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6097 | Return a unique name for a temporary file.\n\ | 
| Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 6098 | 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] | 6099 | or None if not needed."); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6100 |  | 
|  | 6101 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6102 | posix_tempnam(PyObject *self, PyObject *args) | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6103 | { | 
|  | 6104 | PyObject *result = NULL; | 
|  | 6105 | char *dir = NULL; | 
|  | 6106 | char *pfx = NULL; | 
|  | 6107 | char *name; | 
|  | 6108 |  | 
|  | 6109 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) | 
|  | 6110 | return NULL; | 
| Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6111 |  | 
|  | 6112 | if (PyErr_Warn(PyExc_RuntimeWarning, | 
|  | 6113 | "tempnam is a potential security risk to your program") < 0) | 
|  | 6114 | return NULL; | 
|  | 6115 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6116 | #ifdef MS_WINDOWS | 
| Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6117 | name = _tempnam(dir, pfx); | 
|  | 6118 | #else | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6119 | name = tempnam(dir, pfx); | 
| Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6120 | #endif | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6121 | if (name == NULL) | 
|  | 6122 | return PyErr_NoMemory(); | 
|  | 6123 | result = PyString_FromString(name); | 
|  | 6124 | free(name); | 
|  | 6125 | return result; | 
|  | 6126 | } | 
| Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 6127 | #endif | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6128 |  | 
|  | 6129 |  | 
|  | 6130 | #ifdef HAVE_TMPFILE | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6131 | PyDoc_STRVAR(posix_tmpfile__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6132 | "tmpfile() -> file object\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6133 | Create a temporary file with no directory entries."); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6134 |  | 
|  | 6135 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6136 | posix_tmpfile(PyObject *self, PyObject *noargs) | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6137 | { | 
|  | 6138 | FILE *fp; | 
|  | 6139 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6140 | fp = tmpfile(); | 
|  | 6141 | if (fp == NULL) | 
|  | 6142 | return posix_error(); | 
| Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 6143 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6144 | } | 
|  | 6145 | #endif | 
|  | 6146 |  | 
|  | 6147 |  | 
|  | 6148 | #ifdef HAVE_TMPNAM | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6149 | PyDoc_STRVAR(posix_tmpnam__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6150 | "tmpnam() -> string\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6151 | Return a unique name for a temporary file."); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6152 |  | 
|  | 6153 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6154 | posix_tmpnam(PyObject *self, PyObject *noargs) | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6155 | { | 
|  | 6156 | char buffer[L_tmpnam]; | 
|  | 6157 | char *name; | 
|  | 6158 |  | 
| Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6159 | if (PyErr_Warn(PyExc_RuntimeWarning, | 
|  | 6160 | "tmpnam is a potential security risk to your program") < 0) | 
|  | 6161 | return NULL; | 
|  | 6162 |  | 
| Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6163 | #ifdef USE_TMPNAM_R | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6164 | name = tmpnam_r(buffer); | 
|  | 6165 | #else | 
|  | 6166 | name = tmpnam(buffer); | 
|  | 6167 | #endif | 
|  | 6168 | if (name == NULL) { | 
|  | 6169 | PyErr_SetObject(PyExc_OSError, | 
|  | 6170 | Py_BuildValue("is", 0, | 
| Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6171 | #ifdef USE_TMPNAM_R | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6172 | "unexpected NULL from tmpnam_r" | 
|  | 6173 | #else | 
|  | 6174 | "unexpected NULL from tmpnam" | 
|  | 6175 | #endif | 
|  | 6176 | )); | 
|  | 6177 | return NULL; | 
|  | 6178 | } | 
|  | 6179 | return PyString_FromString(buffer); | 
|  | 6180 | } | 
|  | 6181 | #endif | 
|  | 6182 |  | 
|  | 6183 |  | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6184 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). | 
|  | 6185 | * It maps strings representing configuration variable names to | 
|  | 6186 | * integer values, allowing those functions to be called with the | 
| Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6187 | * magic names instead of polluting the module's namespace with tons of | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6188 | * rarely-used constants.  There are three separate tables that use | 
|  | 6189 | * these definitions. | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6190 | * | 
|  | 6191 | * This code is always included, even if none of the interfaces that | 
|  | 6192 | * need it are included.  The #if hackery needed to avoid it would be | 
|  | 6193 | * sufficiently pervasive that it's not worth the loss of readability. | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6194 | */ | 
|  | 6195 | struct constdef { | 
|  | 6196 | char *name; | 
|  | 6197 | long value; | 
|  | 6198 | }; | 
|  | 6199 |  | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6200 | static int | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6201 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, | 
|  | 6202 | size_t tablesize) | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6203 | { | 
|  | 6204 | if (PyInt_Check(arg)) { | 
|  | 6205 | *valuep = PyInt_AS_LONG(arg); | 
|  | 6206 | return 1; | 
|  | 6207 | } | 
|  | 6208 | if (PyString_Check(arg)) { | 
|  | 6209 | /* look up the value in the table using a binary search */ | 
| Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6210 | size_t lo = 0; | 
|  | 6211 | size_t mid; | 
|  | 6212 | size_t hi = tablesize; | 
|  | 6213 | int cmp; | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6214 | char *confname = PyString_AS_STRING(arg); | 
|  | 6215 | while (lo < hi) { | 
|  | 6216 | mid = (lo + hi) / 2; | 
|  | 6217 | cmp = strcmp(confname, table[mid].name); | 
|  | 6218 | if (cmp < 0) | 
|  | 6219 | hi = mid; | 
|  | 6220 | else if (cmp > 0) | 
|  | 6221 | lo = mid + 1; | 
|  | 6222 | else { | 
|  | 6223 | *valuep = table[mid].value; | 
|  | 6224 | return 1; | 
|  | 6225 | } | 
|  | 6226 | } | 
|  | 6227 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); | 
|  | 6228 | } | 
|  | 6229 | else | 
|  | 6230 | PyErr_SetString(PyExc_TypeError, | 
|  | 6231 | "configuration names must be strings or integers"); | 
|  | 6232 | return 0; | 
|  | 6233 | } | 
|  | 6234 |  | 
|  | 6235 |  | 
|  | 6236 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) | 
|  | 6237 | static struct constdef  posix_constants_pathconf[] = { | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6238 | #ifdef _PC_ABI_AIO_XFER_MAX | 
|  | 6239 | {"PC_ABI_AIO_XFER_MAX",	_PC_ABI_AIO_XFER_MAX}, | 
|  | 6240 | #endif | 
|  | 6241 | #ifdef _PC_ABI_ASYNC_IO | 
|  | 6242 | {"PC_ABI_ASYNC_IO",	_PC_ABI_ASYNC_IO}, | 
|  | 6243 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6244 | #ifdef _PC_ASYNC_IO | 
|  | 6245 | {"PC_ASYNC_IO",	_PC_ASYNC_IO}, | 
|  | 6246 | #endif | 
|  | 6247 | #ifdef _PC_CHOWN_RESTRICTED | 
|  | 6248 | {"PC_CHOWN_RESTRICTED",	_PC_CHOWN_RESTRICTED}, | 
|  | 6249 | #endif | 
|  | 6250 | #ifdef _PC_FILESIZEBITS | 
|  | 6251 | {"PC_FILESIZEBITS",	_PC_FILESIZEBITS}, | 
|  | 6252 | #endif | 
|  | 6253 | #ifdef _PC_LAST | 
|  | 6254 | {"PC_LAST",	_PC_LAST}, | 
|  | 6255 | #endif | 
|  | 6256 | #ifdef _PC_LINK_MAX | 
|  | 6257 | {"PC_LINK_MAX",	_PC_LINK_MAX}, | 
|  | 6258 | #endif | 
|  | 6259 | #ifdef _PC_MAX_CANON | 
|  | 6260 | {"PC_MAX_CANON",	_PC_MAX_CANON}, | 
|  | 6261 | #endif | 
|  | 6262 | #ifdef _PC_MAX_INPUT | 
|  | 6263 | {"PC_MAX_INPUT",	_PC_MAX_INPUT}, | 
|  | 6264 | #endif | 
|  | 6265 | #ifdef _PC_NAME_MAX | 
|  | 6266 | {"PC_NAME_MAX",	_PC_NAME_MAX}, | 
|  | 6267 | #endif | 
|  | 6268 | #ifdef _PC_NO_TRUNC | 
|  | 6269 | {"PC_NO_TRUNC",	_PC_NO_TRUNC}, | 
|  | 6270 | #endif | 
|  | 6271 | #ifdef _PC_PATH_MAX | 
|  | 6272 | {"PC_PATH_MAX",	_PC_PATH_MAX}, | 
|  | 6273 | #endif | 
|  | 6274 | #ifdef _PC_PIPE_BUF | 
|  | 6275 | {"PC_PIPE_BUF",	_PC_PIPE_BUF}, | 
|  | 6276 | #endif | 
|  | 6277 | #ifdef _PC_PRIO_IO | 
|  | 6278 | {"PC_PRIO_IO",	_PC_PRIO_IO}, | 
|  | 6279 | #endif | 
|  | 6280 | #ifdef _PC_SOCK_MAXBUF | 
|  | 6281 | {"PC_SOCK_MAXBUF",	_PC_SOCK_MAXBUF}, | 
|  | 6282 | #endif | 
|  | 6283 | #ifdef _PC_SYNC_IO | 
|  | 6284 | {"PC_SYNC_IO",	_PC_SYNC_IO}, | 
|  | 6285 | #endif | 
|  | 6286 | #ifdef _PC_VDISABLE | 
|  | 6287 | {"PC_VDISABLE",	_PC_VDISABLE}, | 
|  | 6288 | #endif | 
|  | 6289 | }; | 
|  | 6290 |  | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6291 | static int | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6292 | conv_path_confname(PyObject *arg, int *valuep) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6293 | { | 
|  | 6294 | return conv_confname(arg, valuep, posix_constants_pathconf, | 
|  | 6295 | sizeof(posix_constants_pathconf) | 
|  | 6296 | / sizeof(struct constdef)); | 
|  | 6297 | } | 
|  | 6298 | #endif | 
|  | 6299 |  | 
|  | 6300 | #ifdef HAVE_FPATHCONF | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6301 | PyDoc_STRVAR(posix_fpathconf__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6302 | "fpathconf(fd, name) -> integer\n\n\ | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6303 | 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] | 6304 | If there is no limit, return -1."); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6305 |  | 
|  | 6306 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6307 | posix_fpathconf(PyObject *self, PyObject *args) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6308 | { | 
|  | 6309 | PyObject *result = NULL; | 
|  | 6310 | int name, fd; | 
|  | 6311 |  | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6312 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, | 
|  | 6313 | conv_path_confname, &name)) { | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6314 | long limit; | 
|  | 6315 |  | 
|  | 6316 | errno = 0; | 
|  | 6317 | limit = fpathconf(fd, name); | 
|  | 6318 | if (limit == -1 && errno != 0) | 
|  | 6319 | posix_error(); | 
|  | 6320 | else | 
|  | 6321 | result = PyInt_FromLong(limit); | 
|  | 6322 | } | 
|  | 6323 | return result; | 
|  | 6324 | } | 
|  | 6325 | #endif | 
|  | 6326 |  | 
|  | 6327 |  | 
|  | 6328 | #ifdef HAVE_PATHCONF | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6329 | PyDoc_STRVAR(posix_pathconf__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6330 | "pathconf(path, name) -> integer\n\n\ | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6331 | 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] | 6332 | If there is no limit, return -1."); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6333 |  | 
|  | 6334 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6335 | posix_pathconf(PyObject *self, PyObject *args) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6336 | { | 
|  | 6337 | PyObject *result = NULL; | 
|  | 6338 | int name; | 
|  | 6339 | char *path; | 
|  | 6340 |  | 
|  | 6341 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, | 
|  | 6342 | conv_path_confname, &name)) { | 
|  | 6343 | long limit; | 
|  | 6344 |  | 
|  | 6345 | errno = 0; | 
|  | 6346 | limit = pathconf(path, name); | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6347 | if (limit == -1 && errno != 0) { | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6348 | if (errno == EINVAL) | 
|  | 6349 | /* could be a path or name problem */ | 
|  | 6350 | posix_error(); | 
|  | 6351 | else | 
|  | 6352 | posix_error_with_filename(path); | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6353 | } | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6354 | else | 
|  | 6355 | result = PyInt_FromLong(limit); | 
|  | 6356 | } | 
|  | 6357 | return result; | 
|  | 6358 | } | 
|  | 6359 | #endif | 
|  | 6360 |  | 
|  | 6361 | #ifdef HAVE_CONFSTR | 
|  | 6362 | static struct constdef posix_constants_confstr[] = { | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6363 | #ifdef _CS_ARCHITECTURE | 
|  | 6364 | {"CS_ARCHITECTURE",	_CS_ARCHITECTURE}, | 
|  | 6365 | #endif | 
|  | 6366 | #ifdef _CS_HOSTNAME | 
|  | 6367 | {"CS_HOSTNAME",	_CS_HOSTNAME}, | 
|  | 6368 | #endif | 
|  | 6369 | #ifdef _CS_HW_PROVIDER | 
|  | 6370 | {"CS_HW_PROVIDER",	_CS_HW_PROVIDER}, | 
|  | 6371 | #endif | 
|  | 6372 | #ifdef _CS_HW_SERIAL | 
|  | 6373 | {"CS_HW_SERIAL",	_CS_HW_SERIAL}, | 
|  | 6374 | #endif | 
|  | 6375 | #ifdef _CS_INITTAB_NAME | 
|  | 6376 | {"CS_INITTAB_NAME",	_CS_INITTAB_NAME}, | 
|  | 6377 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6378 | #ifdef _CS_LFS64_CFLAGS | 
|  | 6379 | {"CS_LFS64_CFLAGS",	_CS_LFS64_CFLAGS}, | 
|  | 6380 | #endif | 
|  | 6381 | #ifdef _CS_LFS64_LDFLAGS | 
|  | 6382 | {"CS_LFS64_LDFLAGS",	_CS_LFS64_LDFLAGS}, | 
|  | 6383 | #endif | 
|  | 6384 | #ifdef _CS_LFS64_LIBS | 
|  | 6385 | {"CS_LFS64_LIBS",	_CS_LFS64_LIBS}, | 
|  | 6386 | #endif | 
|  | 6387 | #ifdef _CS_LFS64_LINTFLAGS | 
|  | 6388 | {"CS_LFS64_LINTFLAGS",	_CS_LFS64_LINTFLAGS}, | 
|  | 6389 | #endif | 
|  | 6390 | #ifdef _CS_LFS_CFLAGS | 
|  | 6391 | {"CS_LFS_CFLAGS",	_CS_LFS_CFLAGS}, | 
|  | 6392 | #endif | 
|  | 6393 | #ifdef _CS_LFS_LDFLAGS | 
|  | 6394 | {"CS_LFS_LDFLAGS",	_CS_LFS_LDFLAGS}, | 
|  | 6395 | #endif | 
|  | 6396 | #ifdef _CS_LFS_LIBS | 
|  | 6397 | {"CS_LFS_LIBS",	_CS_LFS_LIBS}, | 
|  | 6398 | #endif | 
|  | 6399 | #ifdef _CS_LFS_LINTFLAGS | 
|  | 6400 | {"CS_LFS_LINTFLAGS",	_CS_LFS_LINTFLAGS}, | 
|  | 6401 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6402 | #ifdef _CS_MACHINE | 
|  | 6403 | {"CS_MACHINE",	_CS_MACHINE}, | 
|  | 6404 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6405 | #ifdef _CS_PATH | 
|  | 6406 | {"CS_PATH",	_CS_PATH}, | 
|  | 6407 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6408 | #ifdef _CS_RELEASE | 
|  | 6409 | {"CS_RELEASE",	_CS_RELEASE}, | 
|  | 6410 | #endif | 
|  | 6411 | #ifdef _CS_SRPC_DOMAIN | 
|  | 6412 | {"CS_SRPC_DOMAIN",	_CS_SRPC_DOMAIN}, | 
|  | 6413 | #endif | 
|  | 6414 | #ifdef _CS_SYSNAME | 
|  | 6415 | {"CS_SYSNAME",	_CS_SYSNAME}, | 
|  | 6416 | #endif | 
|  | 6417 | #ifdef _CS_VERSION | 
|  | 6418 | {"CS_VERSION",	_CS_VERSION}, | 
|  | 6419 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6420 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS | 
|  | 6421 | {"CS_XBS5_ILP32_OFF32_CFLAGS",	_CS_XBS5_ILP32_OFF32_CFLAGS}, | 
|  | 6422 | #endif | 
|  | 6423 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS | 
|  | 6424 | {"CS_XBS5_ILP32_OFF32_LDFLAGS",	_CS_XBS5_ILP32_OFF32_LDFLAGS}, | 
|  | 6425 | #endif | 
|  | 6426 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS | 
|  | 6427 | {"CS_XBS5_ILP32_OFF32_LIBS",	_CS_XBS5_ILP32_OFF32_LIBS}, | 
|  | 6428 | #endif | 
|  | 6429 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS | 
|  | 6430 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS",	_CS_XBS5_ILP32_OFF32_LINTFLAGS}, | 
|  | 6431 | #endif | 
|  | 6432 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS | 
|  | 6433 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS",	_CS_XBS5_ILP32_OFFBIG_CFLAGS}, | 
|  | 6434 | #endif | 
|  | 6435 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS | 
|  | 6436 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS",	_CS_XBS5_ILP32_OFFBIG_LDFLAGS}, | 
|  | 6437 | #endif | 
|  | 6438 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS | 
|  | 6439 | {"CS_XBS5_ILP32_OFFBIG_LIBS",	_CS_XBS5_ILP32_OFFBIG_LIBS}, | 
|  | 6440 | #endif | 
|  | 6441 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS | 
|  | 6442 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS",	_CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, | 
|  | 6443 | #endif | 
|  | 6444 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS | 
|  | 6445 | {"CS_XBS5_LP64_OFF64_CFLAGS",	_CS_XBS5_LP64_OFF64_CFLAGS}, | 
|  | 6446 | #endif | 
|  | 6447 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS | 
|  | 6448 | {"CS_XBS5_LP64_OFF64_LDFLAGS",	_CS_XBS5_LP64_OFF64_LDFLAGS}, | 
|  | 6449 | #endif | 
|  | 6450 | #ifdef _CS_XBS5_LP64_OFF64_LIBS | 
|  | 6451 | {"CS_XBS5_LP64_OFF64_LIBS",	_CS_XBS5_LP64_OFF64_LIBS}, | 
|  | 6452 | #endif | 
|  | 6453 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS | 
|  | 6454 | {"CS_XBS5_LP64_OFF64_LINTFLAGS",	_CS_XBS5_LP64_OFF64_LINTFLAGS}, | 
|  | 6455 | #endif | 
|  | 6456 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS | 
|  | 6457 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS",	_CS_XBS5_LPBIG_OFFBIG_CFLAGS}, | 
|  | 6458 | #endif | 
|  | 6459 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS | 
|  | 6460 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS",	_CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, | 
|  | 6461 | #endif | 
|  | 6462 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS | 
|  | 6463 | {"CS_XBS5_LPBIG_OFFBIG_LIBS",	_CS_XBS5_LPBIG_OFFBIG_LIBS}, | 
|  | 6464 | #endif | 
|  | 6465 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS | 
|  | 6466 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS",	_CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, | 
|  | 6467 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6468 | #ifdef _MIPS_CS_AVAIL_PROCESSORS | 
|  | 6469 | {"MIPS_CS_AVAIL_PROCESSORS",	_MIPS_CS_AVAIL_PROCESSORS}, | 
|  | 6470 | #endif | 
|  | 6471 | #ifdef _MIPS_CS_BASE | 
|  | 6472 | {"MIPS_CS_BASE",	_MIPS_CS_BASE}, | 
|  | 6473 | #endif | 
|  | 6474 | #ifdef _MIPS_CS_HOSTID | 
|  | 6475 | {"MIPS_CS_HOSTID",	_MIPS_CS_HOSTID}, | 
|  | 6476 | #endif | 
|  | 6477 | #ifdef _MIPS_CS_HW_NAME | 
|  | 6478 | {"MIPS_CS_HW_NAME",	_MIPS_CS_HW_NAME}, | 
|  | 6479 | #endif | 
|  | 6480 | #ifdef _MIPS_CS_NUM_PROCESSORS | 
|  | 6481 | {"MIPS_CS_NUM_PROCESSORS",	_MIPS_CS_NUM_PROCESSORS}, | 
|  | 6482 | #endif | 
|  | 6483 | #ifdef _MIPS_CS_OSREL_MAJ | 
|  | 6484 | {"MIPS_CS_OSREL_MAJ",	_MIPS_CS_OSREL_MAJ}, | 
|  | 6485 | #endif | 
|  | 6486 | #ifdef _MIPS_CS_OSREL_MIN | 
|  | 6487 | {"MIPS_CS_OSREL_MIN",	_MIPS_CS_OSREL_MIN}, | 
|  | 6488 | #endif | 
|  | 6489 | #ifdef _MIPS_CS_OSREL_PATCH | 
|  | 6490 | {"MIPS_CS_OSREL_PATCH",	_MIPS_CS_OSREL_PATCH}, | 
|  | 6491 | #endif | 
|  | 6492 | #ifdef _MIPS_CS_OS_NAME | 
|  | 6493 | {"MIPS_CS_OS_NAME",	_MIPS_CS_OS_NAME}, | 
|  | 6494 | #endif | 
|  | 6495 | #ifdef _MIPS_CS_OS_PROVIDER | 
|  | 6496 | {"MIPS_CS_OS_PROVIDER",	_MIPS_CS_OS_PROVIDER}, | 
|  | 6497 | #endif | 
|  | 6498 | #ifdef _MIPS_CS_PROCESSORS | 
|  | 6499 | {"MIPS_CS_PROCESSORS",	_MIPS_CS_PROCESSORS}, | 
|  | 6500 | #endif | 
|  | 6501 | #ifdef _MIPS_CS_SERIAL | 
|  | 6502 | {"MIPS_CS_SERIAL",	_MIPS_CS_SERIAL}, | 
|  | 6503 | #endif | 
|  | 6504 | #ifdef _MIPS_CS_VENDOR | 
|  | 6505 | {"MIPS_CS_VENDOR",	_MIPS_CS_VENDOR}, | 
|  | 6506 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6507 | }; | 
|  | 6508 |  | 
|  | 6509 | static int | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6510 | conv_confstr_confname(PyObject *arg, int *valuep) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6511 | { | 
|  | 6512 | return conv_confname(arg, valuep, posix_constants_confstr, | 
|  | 6513 | sizeof(posix_constants_confstr) | 
|  | 6514 | / sizeof(struct constdef)); | 
|  | 6515 | } | 
|  | 6516 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6517 | PyDoc_STRVAR(posix_confstr__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6518 | "confstr(name) -> string\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6519 | Return a string-valued system configuration variable."); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6520 |  | 
|  | 6521 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6522 | posix_confstr(PyObject *self, PyObject *args) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6523 | { | 
|  | 6524 | PyObject *result = NULL; | 
|  | 6525 | int name; | 
|  | 6526 | char buffer[64]; | 
|  | 6527 |  | 
|  | 6528 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { | 
|  | 6529 | int len = confstr(name, buffer, sizeof(buffer)); | 
|  | 6530 |  | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6531 | errno = 0; | 
|  | 6532 | if (len == 0) { | 
|  | 6533 | if (errno != 0) | 
|  | 6534 | posix_error(); | 
|  | 6535 | else | 
|  | 6536 | result = PyString_FromString(""); | 
|  | 6537 | } | 
|  | 6538 | else { | 
|  | 6539 | if (len >= sizeof(buffer)) { | 
|  | 6540 | result = PyString_FromStringAndSize(NULL, len); | 
|  | 6541 | if (result != NULL) | 
|  | 6542 | confstr(name, PyString_AS_STRING(result), len+1); | 
|  | 6543 | } | 
|  | 6544 | else | 
|  | 6545 | result = PyString_FromString(buffer); | 
|  | 6546 | } | 
|  | 6547 | } | 
|  | 6548 | return result; | 
|  | 6549 | } | 
|  | 6550 | #endif | 
|  | 6551 |  | 
|  | 6552 |  | 
|  | 6553 | #ifdef HAVE_SYSCONF | 
|  | 6554 | static struct constdef posix_constants_sysconf[] = { | 
|  | 6555 | #ifdef _SC_2_CHAR_TERM | 
|  | 6556 | {"SC_2_CHAR_TERM",	_SC_2_CHAR_TERM}, | 
|  | 6557 | #endif | 
|  | 6558 | #ifdef _SC_2_C_BIND | 
|  | 6559 | {"SC_2_C_BIND",	_SC_2_C_BIND}, | 
|  | 6560 | #endif | 
|  | 6561 | #ifdef _SC_2_C_DEV | 
|  | 6562 | {"SC_2_C_DEV",	_SC_2_C_DEV}, | 
|  | 6563 | #endif | 
|  | 6564 | #ifdef _SC_2_C_VERSION | 
|  | 6565 | {"SC_2_C_VERSION",	_SC_2_C_VERSION}, | 
|  | 6566 | #endif | 
|  | 6567 | #ifdef _SC_2_FORT_DEV | 
|  | 6568 | {"SC_2_FORT_DEV",	_SC_2_FORT_DEV}, | 
|  | 6569 | #endif | 
|  | 6570 | #ifdef _SC_2_FORT_RUN | 
|  | 6571 | {"SC_2_FORT_RUN",	_SC_2_FORT_RUN}, | 
|  | 6572 | #endif | 
|  | 6573 | #ifdef _SC_2_LOCALEDEF | 
|  | 6574 | {"SC_2_LOCALEDEF",	_SC_2_LOCALEDEF}, | 
|  | 6575 | #endif | 
|  | 6576 | #ifdef _SC_2_SW_DEV | 
|  | 6577 | {"SC_2_SW_DEV",	_SC_2_SW_DEV}, | 
|  | 6578 | #endif | 
|  | 6579 | #ifdef _SC_2_UPE | 
|  | 6580 | {"SC_2_UPE",	_SC_2_UPE}, | 
|  | 6581 | #endif | 
|  | 6582 | #ifdef _SC_2_VERSION | 
|  | 6583 | {"SC_2_VERSION",	_SC_2_VERSION}, | 
|  | 6584 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6585 | #ifdef _SC_ABI_ASYNCHRONOUS_IO | 
|  | 6586 | {"SC_ABI_ASYNCHRONOUS_IO",	_SC_ABI_ASYNCHRONOUS_IO}, | 
|  | 6587 | #endif | 
|  | 6588 | #ifdef _SC_ACL | 
|  | 6589 | {"SC_ACL",	_SC_ACL}, | 
|  | 6590 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6591 | #ifdef _SC_AIO_LISTIO_MAX | 
|  | 6592 | {"SC_AIO_LISTIO_MAX",	_SC_AIO_LISTIO_MAX}, | 
|  | 6593 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6594 | #ifdef _SC_AIO_MAX | 
|  | 6595 | {"SC_AIO_MAX",	_SC_AIO_MAX}, | 
|  | 6596 | #endif | 
|  | 6597 | #ifdef _SC_AIO_PRIO_DELTA_MAX | 
|  | 6598 | {"SC_AIO_PRIO_DELTA_MAX",	_SC_AIO_PRIO_DELTA_MAX}, | 
|  | 6599 | #endif | 
|  | 6600 | #ifdef _SC_ARG_MAX | 
|  | 6601 | {"SC_ARG_MAX",	_SC_ARG_MAX}, | 
|  | 6602 | #endif | 
|  | 6603 | #ifdef _SC_ASYNCHRONOUS_IO | 
|  | 6604 | {"SC_ASYNCHRONOUS_IO",	_SC_ASYNCHRONOUS_IO}, | 
|  | 6605 | #endif | 
|  | 6606 | #ifdef _SC_ATEXIT_MAX | 
|  | 6607 | {"SC_ATEXIT_MAX",	_SC_ATEXIT_MAX}, | 
|  | 6608 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6609 | #ifdef _SC_AUDIT | 
|  | 6610 | {"SC_AUDIT",	_SC_AUDIT}, | 
|  | 6611 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6612 | #ifdef _SC_AVPHYS_PAGES | 
|  | 6613 | {"SC_AVPHYS_PAGES",	_SC_AVPHYS_PAGES}, | 
|  | 6614 | #endif | 
|  | 6615 | #ifdef _SC_BC_BASE_MAX | 
|  | 6616 | {"SC_BC_BASE_MAX",	_SC_BC_BASE_MAX}, | 
|  | 6617 | #endif | 
|  | 6618 | #ifdef _SC_BC_DIM_MAX | 
|  | 6619 | {"SC_BC_DIM_MAX",	_SC_BC_DIM_MAX}, | 
|  | 6620 | #endif | 
|  | 6621 | #ifdef _SC_BC_SCALE_MAX | 
|  | 6622 | {"SC_BC_SCALE_MAX",	_SC_BC_SCALE_MAX}, | 
|  | 6623 | #endif | 
|  | 6624 | #ifdef _SC_BC_STRING_MAX | 
|  | 6625 | {"SC_BC_STRING_MAX",	_SC_BC_STRING_MAX}, | 
|  | 6626 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6627 | #ifdef _SC_CAP | 
|  | 6628 | {"SC_CAP",	_SC_CAP}, | 
|  | 6629 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6630 | #ifdef _SC_CHARCLASS_NAME_MAX | 
|  | 6631 | {"SC_CHARCLASS_NAME_MAX",	_SC_CHARCLASS_NAME_MAX}, | 
|  | 6632 | #endif | 
|  | 6633 | #ifdef _SC_CHAR_BIT | 
|  | 6634 | {"SC_CHAR_BIT",	_SC_CHAR_BIT}, | 
|  | 6635 | #endif | 
|  | 6636 | #ifdef _SC_CHAR_MAX | 
|  | 6637 | {"SC_CHAR_MAX",	_SC_CHAR_MAX}, | 
|  | 6638 | #endif | 
|  | 6639 | #ifdef _SC_CHAR_MIN | 
|  | 6640 | {"SC_CHAR_MIN",	_SC_CHAR_MIN}, | 
|  | 6641 | #endif | 
|  | 6642 | #ifdef _SC_CHILD_MAX | 
|  | 6643 | {"SC_CHILD_MAX",	_SC_CHILD_MAX}, | 
|  | 6644 | #endif | 
|  | 6645 | #ifdef _SC_CLK_TCK | 
|  | 6646 | {"SC_CLK_TCK",	_SC_CLK_TCK}, | 
|  | 6647 | #endif | 
|  | 6648 | #ifdef _SC_COHER_BLKSZ | 
|  | 6649 | {"SC_COHER_BLKSZ",	_SC_COHER_BLKSZ}, | 
|  | 6650 | #endif | 
|  | 6651 | #ifdef _SC_COLL_WEIGHTS_MAX | 
|  | 6652 | {"SC_COLL_WEIGHTS_MAX",	_SC_COLL_WEIGHTS_MAX}, | 
|  | 6653 | #endif | 
|  | 6654 | #ifdef _SC_DCACHE_ASSOC | 
|  | 6655 | {"SC_DCACHE_ASSOC",	_SC_DCACHE_ASSOC}, | 
|  | 6656 | #endif | 
|  | 6657 | #ifdef _SC_DCACHE_BLKSZ | 
|  | 6658 | {"SC_DCACHE_BLKSZ",	_SC_DCACHE_BLKSZ}, | 
|  | 6659 | #endif | 
|  | 6660 | #ifdef _SC_DCACHE_LINESZ | 
|  | 6661 | {"SC_DCACHE_LINESZ",	_SC_DCACHE_LINESZ}, | 
|  | 6662 | #endif | 
|  | 6663 | #ifdef _SC_DCACHE_SZ | 
|  | 6664 | {"SC_DCACHE_SZ",	_SC_DCACHE_SZ}, | 
|  | 6665 | #endif | 
|  | 6666 | #ifdef _SC_DCACHE_TBLKSZ | 
|  | 6667 | {"SC_DCACHE_TBLKSZ",	_SC_DCACHE_TBLKSZ}, | 
|  | 6668 | #endif | 
|  | 6669 | #ifdef _SC_DELAYTIMER_MAX | 
|  | 6670 | {"SC_DELAYTIMER_MAX",	_SC_DELAYTIMER_MAX}, | 
|  | 6671 | #endif | 
|  | 6672 | #ifdef _SC_EQUIV_CLASS_MAX | 
|  | 6673 | {"SC_EQUIV_CLASS_MAX",	_SC_EQUIV_CLASS_MAX}, | 
|  | 6674 | #endif | 
|  | 6675 | #ifdef _SC_EXPR_NEST_MAX | 
|  | 6676 | {"SC_EXPR_NEST_MAX",	_SC_EXPR_NEST_MAX}, | 
|  | 6677 | #endif | 
|  | 6678 | #ifdef _SC_FSYNC | 
|  | 6679 | {"SC_FSYNC",	_SC_FSYNC}, | 
|  | 6680 | #endif | 
|  | 6681 | #ifdef _SC_GETGR_R_SIZE_MAX | 
|  | 6682 | {"SC_GETGR_R_SIZE_MAX",	_SC_GETGR_R_SIZE_MAX}, | 
|  | 6683 | #endif | 
|  | 6684 | #ifdef _SC_GETPW_R_SIZE_MAX | 
|  | 6685 | {"SC_GETPW_R_SIZE_MAX",	_SC_GETPW_R_SIZE_MAX}, | 
|  | 6686 | #endif | 
|  | 6687 | #ifdef _SC_ICACHE_ASSOC | 
|  | 6688 | {"SC_ICACHE_ASSOC",	_SC_ICACHE_ASSOC}, | 
|  | 6689 | #endif | 
|  | 6690 | #ifdef _SC_ICACHE_BLKSZ | 
|  | 6691 | {"SC_ICACHE_BLKSZ",	_SC_ICACHE_BLKSZ}, | 
|  | 6692 | #endif | 
|  | 6693 | #ifdef _SC_ICACHE_LINESZ | 
|  | 6694 | {"SC_ICACHE_LINESZ",	_SC_ICACHE_LINESZ}, | 
|  | 6695 | #endif | 
|  | 6696 | #ifdef _SC_ICACHE_SZ | 
|  | 6697 | {"SC_ICACHE_SZ",	_SC_ICACHE_SZ}, | 
|  | 6698 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6699 | #ifdef _SC_INF | 
|  | 6700 | {"SC_INF",	_SC_INF}, | 
|  | 6701 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6702 | #ifdef _SC_INT_MAX | 
|  | 6703 | {"SC_INT_MAX",	_SC_INT_MAX}, | 
|  | 6704 | #endif | 
|  | 6705 | #ifdef _SC_INT_MIN | 
|  | 6706 | {"SC_INT_MIN",	_SC_INT_MIN}, | 
|  | 6707 | #endif | 
|  | 6708 | #ifdef _SC_IOV_MAX | 
|  | 6709 | {"SC_IOV_MAX",	_SC_IOV_MAX}, | 
|  | 6710 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6711 | #ifdef _SC_IP_SECOPTS | 
|  | 6712 | {"SC_IP_SECOPTS",	_SC_IP_SECOPTS}, | 
|  | 6713 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6714 | #ifdef _SC_JOB_CONTROL | 
|  | 6715 | {"SC_JOB_CONTROL",	_SC_JOB_CONTROL}, | 
|  | 6716 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6717 | #ifdef _SC_KERN_POINTERS | 
|  | 6718 | {"SC_KERN_POINTERS",	_SC_KERN_POINTERS}, | 
|  | 6719 | #endif | 
|  | 6720 | #ifdef _SC_KERN_SIM | 
|  | 6721 | {"SC_KERN_SIM",	_SC_KERN_SIM}, | 
|  | 6722 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6723 | #ifdef _SC_LINE_MAX | 
|  | 6724 | {"SC_LINE_MAX",	_SC_LINE_MAX}, | 
|  | 6725 | #endif | 
|  | 6726 | #ifdef _SC_LOGIN_NAME_MAX | 
|  | 6727 | {"SC_LOGIN_NAME_MAX",	_SC_LOGIN_NAME_MAX}, | 
|  | 6728 | #endif | 
|  | 6729 | #ifdef _SC_LOGNAME_MAX | 
|  | 6730 | {"SC_LOGNAME_MAX",	_SC_LOGNAME_MAX}, | 
|  | 6731 | #endif | 
|  | 6732 | #ifdef _SC_LONG_BIT | 
|  | 6733 | {"SC_LONG_BIT",	_SC_LONG_BIT}, | 
|  | 6734 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6735 | #ifdef _SC_MAC | 
|  | 6736 | {"SC_MAC",	_SC_MAC}, | 
|  | 6737 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6738 | #ifdef _SC_MAPPED_FILES | 
|  | 6739 | {"SC_MAPPED_FILES",	_SC_MAPPED_FILES}, | 
|  | 6740 | #endif | 
|  | 6741 | #ifdef _SC_MAXPID | 
|  | 6742 | {"SC_MAXPID",	_SC_MAXPID}, | 
|  | 6743 | #endif | 
|  | 6744 | #ifdef _SC_MB_LEN_MAX | 
|  | 6745 | {"SC_MB_LEN_MAX",	_SC_MB_LEN_MAX}, | 
|  | 6746 | #endif | 
|  | 6747 | #ifdef _SC_MEMLOCK | 
|  | 6748 | {"SC_MEMLOCK",	_SC_MEMLOCK}, | 
|  | 6749 | #endif | 
|  | 6750 | #ifdef _SC_MEMLOCK_RANGE | 
|  | 6751 | {"SC_MEMLOCK_RANGE",	_SC_MEMLOCK_RANGE}, | 
|  | 6752 | #endif | 
|  | 6753 | #ifdef _SC_MEMORY_PROTECTION | 
|  | 6754 | {"SC_MEMORY_PROTECTION",	_SC_MEMORY_PROTECTION}, | 
|  | 6755 | #endif | 
|  | 6756 | #ifdef _SC_MESSAGE_PASSING | 
|  | 6757 | {"SC_MESSAGE_PASSING",	_SC_MESSAGE_PASSING}, | 
|  | 6758 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6759 | #ifdef _SC_MMAP_FIXED_ALIGNMENT | 
|  | 6760 | {"SC_MMAP_FIXED_ALIGNMENT",	_SC_MMAP_FIXED_ALIGNMENT}, | 
|  | 6761 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6762 | #ifdef _SC_MQ_OPEN_MAX | 
|  | 6763 | {"SC_MQ_OPEN_MAX",	_SC_MQ_OPEN_MAX}, | 
|  | 6764 | #endif | 
|  | 6765 | #ifdef _SC_MQ_PRIO_MAX | 
|  | 6766 | {"SC_MQ_PRIO_MAX",	_SC_MQ_PRIO_MAX}, | 
|  | 6767 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6768 | #ifdef _SC_NACLS_MAX | 
|  | 6769 | {"SC_NACLS_MAX",	_SC_NACLS_MAX}, | 
|  | 6770 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6771 | #ifdef _SC_NGROUPS_MAX | 
|  | 6772 | {"SC_NGROUPS_MAX",	_SC_NGROUPS_MAX}, | 
|  | 6773 | #endif | 
|  | 6774 | #ifdef _SC_NL_ARGMAX | 
|  | 6775 | {"SC_NL_ARGMAX",	_SC_NL_ARGMAX}, | 
|  | 6776 | #endif | 
|  | 6777 | #ifdef _SC_NL_LANGMAX | 
|  | 6778 | {"SC_NL_LANGMAX",	_SC_NL_LANGMAX}, | 
|  | 6779 | #endif | 
|  | 6780 | #ifdef _SC_NL_MSGMAX | 
|  | 6781 | {"SC_NL_MSGMAX",	_SC_NL_MSGMAX}, | 
|  | 6782 | #endif | 
|  | 6783 | #ifdef _SC_NL_NMAX | 
|  | 6784 | {"SC_NL_NMAX",	_SC_NL_NMAX}, | 
|  | 6785 | #endif | 
|  | 6786 | #ifdef _SC_NL_SETMAX | 
|  | 6787 | {"SC_NL_SETMAX",	_SC_NL_SETMAX}, | 
|  | 6788 | #endif | 
|  | 6789 | #ifdef _SC_NL_TEXTMAX | 
|  | 6790 | {"SC_NL_TEXTMAX",	_SC_NL_TEXTMAX}, | 
|  | 6791 | #endif | 
|  | 6792 | #ifdef _SC_NPROCESSORS_CONF | 
|  | 6793 | {"SC_NPROCESSORS_CONF",	_SC_NPROCESSORS_CONF}, | 
|  | 6794 | #endif | 
|  | 6795 | #ifdef _SC_NPROCESSORS_ONLN | 
|  | 6796 | {"SC_NPROCESSORS_ONLN",	_SC_NPROCESSORS_ONLN}, | 
|  | 6797 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6798 | #ifdef _SC_NPROC_CONF | 
|  | 6799 | {"SC_NPROC_CONF",	_SC_NPROC_CONF}, | 
|  | 6800 | #endif | 
|  | 6801 | #ifdef _SC_NPROC_ONLN | 
|  | 6802 | {"SC_NPROC_ONLN",	_SC_NPROC_ONLN}, | 
|  | 6803 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6804 | #ifdef _SC_NZERO | 
|  | 6805 | {"SC_NZERO",	_SC_NZERO}, | 
|  | 6806 | #endif | 
|  | 6807 | #ifdef _SC_OPEN_MAX | 
|  | 6808 | {"SC_OPEN_MAX",	_SC_OPEN_MAX}, | 
|  | 6809 | #endif | 
|  | 6810 | #ifdef _SC_PAGESIZE | 
|  | 6811 | {"SC_PAGESIZE",	_SC_PAGESIZE}, | 
|  | 6812 | #endif | 
|  | 6813 | #ifdef _SC_PAGE_SIZE | 
|  | 6814 | {"SC_PAGE_SIZE",	_SC_PAGE_SIZE}, | 
|  | 6815 | #endif | 
|  | 6816 | #ifdef _SC_PASS_MAX | 
|  | 6817 | {"SC_PASS_MAX",	_SC_PASS_MAX}, | 
|  | 6818 | #endif | 
|  | 6819 | #ifdef _SC_PHYS_PAGES | 
|  | 6820 | {"SC_PHYS_PAGES",	_SC_PHYS_PAGES}, | 
|  | 6821 | #endif | 
|  | 6822 | #ifdef _SC_PII | 
|  | 6823 | {"SC_PII",	_SC_PII}, | 
|  | 6824 | #endif | 
|  | 6825 | #ifdef _SC_PII_INTERNET | 
|  | 6826 | {"SC_PII_INTERNET",	_SC_PII_INTERNET}, | 
|  | 6827 | #endif | 
|  | 6828 | #ifdef _SC_PII_INTERNET_DGRAM | 
|  | 6829 | {"SC_PII_INTERNET_DGRAM",	_SC_PII_INTERNET_DGRAM}, | 
|  | 6830 | #endif | 
|  | 6831 | #ifdef _SC_PII_INTERNET_STREAM | 
|  | 6832 | {"SC_PII_INTERNET_STREAM",	_SC_PII_INTERNET_STREAM}, | 
|  | 6833 | #endif | 
|  | 6834 | #ifdef _SC_PII_OSI | 
|  | 6835 | {"SC_PII_OSI",	_SC_PII_OSI}, | 
|  | 6836 | #endif | 
|  | 6837 | #ifdef _SC_PII_OSI_CLTS | 
|  | 6838 | {"SC_PII_OSI_CLTS",	_SC_PII_OSI_CLTS}, | 
|  | 6839 | #endif | 
|  | 6840 | #ifdef _SC_PII_OSI_COTS | 
|  | 6841 | {"SC_PII_OSI_COTS",	_SC_PII_OSI_COTS}, | 
|  | 6842 | #endif | 
|  | 6843 | #ifdef _SC_PII_OSI_M | 
|  | 6844 | {"SC_PII_OSI_M",	_SC_PII_OSI_M}, | 
|  | 6845 | #endif | 
|  | 6846 | #ifdef _SC_PII_SOCKET | 
|  | 6847 | {"SC_PII_SOCKET",	_SC_PII_SOCKET}, | 
|  | 6848 | #endif | 
|  | 6849 | #ifdef _SC_PII_XTI | 
|  | 6850 | {"SC_PII_XTI",	_SC_PII_XTI}, | 
|  | 6851 | #endif | 
|  | 6852 | #ifdef _SC_POLL | 
|  | 6853 | {"SC_POLL",	_SC_POLL}, | 
|  | 6854 | #endif | 
|  | 6855 | #ifdef _SC_PRIORITIZED_IO | 
|  | 6856 | {"SC_PRIORITIZED_IO",	_SC_PRIORITIZED_IO}, | 
|  | 6857 | #endif | 
|  | 6858 | #ifdef _SC_PRIORITY_SCHEDULING | 
|  | 6859 | {"SC_PRIORITY_SCHEDULING",	_SC_PRIORITY_SCHEDULING}, | 
|  | 6860 | #endif | 
|  | 6861 | #ifdef _SC_REALTIME_SIGNALS | 
|  | 6862 | {"SC_REALTIME_SIGNALS",	_SC_REALTIME_SIGNALS}, | 
|  | 6863 | #endif | 
|  | 6864 | #ifdef _SC_RE_DUP_MAX | 
|  | 6865 | {"SC_RE_DUP_MAX",	_SC_RE_DUP_MAX}, | 
|  | 6866 | #endif | 
|  | 6867 | #ifdef _SC_RTSIG_MAX | 
|  | 6868 | {"SC_RTSIG_MAX",	_SC_RTSIG_MAX}, | 
|  | 6869 | #endif | 
|  | 6870 | #ifdef _SC_SAVED_IDS | 
|  | 6871 | {"SC_SAVED_IDS",	_SC_SAVED_IDS}, | 
|  | 6872 | #endif | 
|  | 6873 | #ifdef _SC_SCHAR_MAX | 
|  | 6874 | {"SC_SCHAR_MAX",	_SC_SCHAR_MAX}, | 
|  | 6875 | #endif | 
|  | 6876 | #ifdef _SC_SCHAR_MIN | 
|  | 6877 | {"SC_SCHAR_MIN",	_SC_SCHAR_MIN}, | 
|  | 6878 | #endif | 
|  | 6879 | #ifdef _SC_SELECT | 
|  | 6880 | {"SC_SELECT",	_SC_SELECT}, | 
|  | 6881 | #endif | 
|  | 6882 | #ifdef _SC_SEMAPHORES | 
|  | 6883 | {"SC_SEMAPHORES",	_SC_SEMAPHORES}, | 
|  | 6884 | #endif | 
|  | 6885 | #ifdef _SC_SEM_NSEMS_MAX | 
|  | 6886 | {"SC_SEM_NSEMS_MAX",	_SC_SEM_NSEMS_MAX}, | 
|  | 6887 | #endif | 
|  | 6888 | #ifdef _SC_SEM_VALUE_MAX | 
|  | 6889 | {"SC_SEM_VALUE_MAX",	_SC_SEM_VALUE_MAX}, | 
|  | 6890 | #endif | 
|  | 6891 | #ifdef _SC_SHARED_MEMORY_OBJECTS | 
|  | 6892 | {"SC_SHARED_MEMORY_OBJECTS",	_SC_SHARED_MEMORY_OBJECTS}, | 
|  | 6893 | #endif | 
|  | 6894 | #ifdef _SC_SHRT_MAX | 
|  | 6895 | {"SC_SHRT_MAX",	_SC_SHRT_MAX}, | 
|  | 6896 | #endif | 
|  | 6897 | #ifdef _SC_SHRT_MIN | 
|  | 6898 | {"SC_SHRT_MIN",	_SC_SHRT_MIN}, | 
|  | 6899 | #endif | 
|  | 6900 | #ifdef _SC_SIGQUEUE_MAX | 
|  | 6901 | {"SC_SIGQUEUE_MAX",	_SC_SIGQUEUE_MAX}, | 
|  | 6902 | #endif | 
|  | 6903 | #ifdef _SC_SIGRT_MAX | 
|  | 6904 | {"SC_SIGRT_MAX",	_SC_SIGRT_MAX}, | 
|  | 6905 | #endif | 
|  | 6906 | #ifdef _SC_SIGRT_MIN | 
|  | 6907 | {"SC_SIGRT_MIN",	_SC_SIGRT_MIN}, | 
|  | 6908 | #endif | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6909 | #ifdef _SC_SOFTPOWER | 
|  | 6910 | {"SC_SOFTPOWER",	_SC_SOFTPOWER}, | 
|  | 6911 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6912 | #ifdef _SC_SPLIT_CACHE | 
|  | 6913 | {"SC_SPLIT_CACHE",	_SC_SPLIT_CACHE}, | 
|  | 6914 | #endif | 
|  | 6915 | #ifdef _SC_SSIZE_MAX | 
|  | 6916 | {"SC_SSIZE_MAX",	_SC_SSIZE_MAX}, | 
|  | 6917 | #endif | 
|  | 6918 | #ifdef _SC_STACK_PROT | 
|  | 6919 | {"SC_STACK_PROT",	_SC_STACK_PROT}, | 
|  | 6920 | #endif | 
|  | 6921 | #ifdef _SC_STREAM_MAX | 
|  | 6922 | {"SC_STREAM_MAX",	_SC_STREAM_MAX}, | 
|  | 6923 | #endif | 
|  | 6924 | #ifdef _SC_SYNCHRONIZED_IO | 
|  | 6925 | {"SC_SYNCHRONIZED_IO",	_SC_SYNCHRONIZED_IO}, | 
|  | 6926 | #endif | 
|  | 6927 | #ifdef _SC_THREADS | 
|  | 6928 | {"SC_THREADS",	_SC_THREADS}, | 
|  | 6929 | #endif | 
|  | 6930 | #ifdef _SC_THREAD_ATTR_STACKADDR | 
|  | 6931 | {"SC_THREAD_ATTR_STACKADDR",	_SC_THREAD_ATTR_STACKADDR}, | 
|  | 6932 | #endif | 
|  | 6933 | #ifdef _SC_THREAD_ATTR_STACKSIZE | 
|  | 6934 | {"SC_THREAD_ATTR_STACKSIZE",	_SC_THREAD_ATTR_STACKSIZE}, | 
|  | 6935 | #endif | 
|  | 6936 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS | 
|  | 6937 | {"SC_THREAD_DESTRUCTOR_ITERATIONS",	_SC_THREAD_DESTRUCTOR_ITERATIONS}, | 
|  | 6938 | #endif | 
|  | 6939 | #ifdef _SC_THREAD_KEYS_MAX | 
|  | 6940 | {"SC_THREAD_KEYS_MAX",	_SC_THREAD_KEYS_MAX}, | 
|  | 6941 | #endif | 
|  | 6942 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING | 
|  | 6943 | {"SC_THREAD_PRIORITY_SCHEDULING",	_SC_THREAD_PRIORITY_SCHEDULING}, | 
|  | 6944 | #endif | 
|  | 6945 | #ifdef _SC_THREAD_PRIO_INHERIT | 
|  | 6946 | {"SC_THREAD_PRIO_INHERIT",	_SC_THREAD_PRIO_INHERIT}, | 
|  | 6947 | #endif | 
|  | 6948 | #ifdef _SC_THREAD_PRIO_PROTECT | 
|  | 6949 | {"SC_THREAD_PRIO_PROTECT",	_SC_THREAD_PRIO_PROTECT}, | 
|  | 6950 | #endif | 
|  | 6951 | #ifdef _SC_THREAD_PROCESS_SHARED | 
|  | 6952 | {"SC_THREAD_PROCESS_SHARED",	_SC_THREAD_PROCESS_SHARED}, | 
|  | 6953 | #endif | 
|  | 6954 | #ifdef _SC_THREAD_SAFE_FUNCTIONS | 
|  | 6955 | {"SC_THREAD_SAFE_FUNCTIONS",	_SC_THREAD_SAFE_FUNCTIONS}, | 
|  | 6956 | #endif | 
|  | 6957 | #ifdef _SC_THREAD_STACK_MIN | 
|  | 6958 | {"SC_THREAD_STACK_MIN",	_SC_THREAD_STACK_MIN}, | 
|  | 6959 | #endif | 
|  | 6960 | #ifdef _SC_THREAD_THREADS_MAX | 
|  | 6961 | {"SC_THREAD_THREADS_MAX",	_SC_THREAD_THREADS_MAX}, | 
|  | 6962 | #endif | 
|  | 6963 | #ifdef _SC_TIMERS | 
|  | 6964 | {"SC_TIMERS",	_SC_TIMERS}, | 
|  | 6965 | #endif | 
|  | 6966 | #ifdef _SC_TIMER_MAX | 
|  | 6967 | {"SC_TIMER_MAX",	_SC_TIMER_MAX}, | 
|  | 6968 | #endif | 
|  | 6969 | #ifdef _SC_TTY_NAME_MAX | 
|  | 6970 | {"SC_TTY_NAME_MAX",	_SC_TTY_NAME_MAX}, | 
|  | 6971 | #endif | 
|  | 6972 | #ifdef _SC_TZNAME_MAX | 
|  | 6973 | {"SC_TZNAME_MAX",	_SC_TZNAME_MAX}, | 
|  | 6974 | #endif | 
|  | 6975 | #ifdef _SC_T_IOV_MAX | 
|  | 6976 | {"SC_T_IOV_MAX",	_SC_T_IOV_MAX}, | 
|  | 6977 | #endif | 
|  | 6978 | #ifdef _SC_UCHAR_MAX | 
|  | 6979 | {"SC_UCHAR_MAX",	_SC_UCHAR_MAX}, | 
|  | 6980 | #endif | 
|  | 6981 | #ifdef _SC_UINT_MAX | 
|  | 6982 | {"SC_UINT_MAX",	_SC_UINT_MAX}, | 
|  | 6983 | #endif | 
|  | 6984 | #ifdef _SC_UIO_MAXIOV | 
|  | 6985 | {"SC_UIO_MAXIOV",	_SC_UIO_MAXIOV}, | 
|  | 6986 | #endif | 
|  | 6987 | #ifdef _SC_ULONG_MAX | 
|  | 6988 | {"SC_ULONG_MAX",	_SC_ULONG_MAX}, | 
|  | 6989 | #endif | 
|  | 6990 | #ifdef _SC_USHRT_MAX | 
|  | 6991 | {"SC_USHRT_MAX",	_SC_USHRT_MAX}, | 
|  | 6992 | #endif | 
|  | 6993 | #ifdef _SC_VERSION | 
|  | 6994 | {"SC_VERSION",	_SC_VERSION}, | 
|  | 6995 | #endif | 
|  | 6996 | #ifdef _SC_WORD_BIT | 
|  | 6997 | {"SC_WORD_BIT",	_SC_WORD_BIT}, | 
|  | 6998 | #endif | 
|  | 6999 | #ifdef _SC_XBS5_ILP32_OFF32 | 
|  | 7000 | {"SC_XBS5_ILP32_OFF32",	_SC_XBS5_ILP32_OFF32}, | 
|  | 7001 | #endif | 
|  | 7002 | #ifdef _SC_XBS5_ILP32_OFFBIG | 
|  | 7003 | {"SC_XBS5_ILP32_OFFBIG",	_SC_XBS5_ILP32_OFFBIG}, | 
|  | 7004 | #endif | 
|  | 7005 | #ifdef _SC_XBS5_LP64_OFF64 | 
|  | 7006 | {"SC_XBS5_LP64_OFF64",	_SC_XBS5_LP64_OFF64}, | 
|  | 7007 | #endif | 
|  | 7008 | #ifdef _SC_XBS5_LPBIG_OFFBIG | 
|  | 7009 | {"SC_XBS5_LPBIG_OFFBIG",	_SC_XBS5_LPBIG_OFFBIG}, | 
|  | 7010 | #endif | 
|  | 7011 | #ifdef _SC_XOPEN_CRYPT | 
|  | 7012 | {"SC_XOPEN_CRYPT",	_SC_XOPEN_CRYPT}, | 
|  | 7013 | #endif | 
|  | 7014 | #ifdef _SC_XOPEN_ENH_I18N | 
|  | 7015 | {"SC_XOPEN_ENH_I18N",	_SC_XOPEN_ENH_I18N}, | 
|  | 7016 | #endif | 
|  | 7017 | #ifdef _SC_XOPEN_LEGACY | 
|  | 7018 | {"SC_XOPEN_LEGACY",	_SC_XOPEN_LEGACY}, | 
|  | 7019 | #endif | 
|  | 7020 | #ifdef _SC_XOPEN_REALTIME | 
|  | 7021 | {"SC_XOPEN_REALTIME",	_SC_XOPEN_REALTIME}, | 
|  | 7022 | #endif | 
|  | 7023 | #ifdef _SC_XOPEN_REALTIME_THREADS | 
|  | 7024 | {"SC_XOPEN_REALTIME_THREADS",	_SC_XOPEN_REALTIME_THREADS}, | 
|  | 7025 | #endif | 
|  | 7026 | #ifdef _SC_XOPEN_SHM | 
|  | 7027 | {"SC_XOPEN_SHM",	_SC_XOPEN_SHM}, | 
|  | 7028 | #endif | 
|  | 7029 | #ifdef _SC_XOPEN_UNIX | 
|  | 7030 | {"SC_XOPEN_UNIX",	_SC_XOPEN_UNIX}, | 
|  | 7031 | #endif | 
|  | 7032 | #ifdef _SC_XOPEN_VERSION | 
|  | 7033 | {"SC_XOPEN_VERSION",	_SC_XOPEN_VERSION}, | 
|  | 7034 | #endif | 
|  | 7035 | #ifdef _SC_XOPEN_XCU_VERSION | 
|  | 7036 | {"SC_XOPEN_XCU_VERSION",	_SC_XOPEN_XCU_VERSION}, | 
|  | 7037 | #endif | 
|  | 7038 | #ifdef _SC_XOPEN_XPG2 | 
|  | 7039 | {"SC_XOPEN_XPG2",	_SC_XOPEN_XPG2}, | 
|  | 7040 | #endif | 
|  | 7041 | #ifdef _SC_XOPEN_XPG3 | 
|  | 7042 | {"SC_XOPEN_XPG3",	_SC_XOPEN_XPG3}, | 
|  | 7043 | #endif | 
|  | 7044 | #ifdef _SC_XOPEN_XPG4 | 
|  | 7045 | {"SC_XOPEN_XPG4",	_SC_XOPEN_XPG4}, | 
|  | 7046 | #endif | 
|  | 7047 | }; | 
|  | 7048 |  | 
|  | 7049 | static int | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7050 | conv_sysconf_confname(PyObject *arg, int *valuep) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7051 | { | 
|  | 7052 | return conv_confname(arg, valuep, posix_constants_sysconf, | 
|  | 7053 | sizeof(posix_constants_sysconf) | 
|  | 7054 | / sizeof(struct constdef)); | 
|  | 7055 | } | 
|  | 7056 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7057 | PyDoc_STRVAR(posix_sysconf__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7058 | "sysconf(name) -> integer\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7059 | Return an integer-valued system configuration variable."); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7060 |  | 
|  | 7061 | static PyObject * | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7062 | posix_sysconf(PyObject *self, PyObject *args) | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7063 | { | 
|  | 7064 | PyObject *result = NULL; | 
|  | 7065 | int name; | 
|  | 7066 |  | 
|  | 7067 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { | 
|  | 7068 | int value; | 
|  | 7069 |  | 
|  | 7070 | errno = 0; | 
|  | 7071 | value = sysconf(name); | 
|  | 7072 | if (value == -1 && errno != 0) | 
|  | 7073 | posix_error(); | 
|  | 7074 | else | 
|  | 7075 | result = PyInt_FromLong(value); | 
|  | 7076 | } | 
|  | 7077 | return result; | 
|  | 7078 | } | 
|  | 7079 | #endif | 
|  | 7080 |  | 
|  | 7081 |  | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7082 | /* This code is used to ensure that the tables of configuration value names | 
|  | 7083 | * are in sorted order as required by conv_confname(), and also to build the | 
|  | 7084 | * the exported dictionaries that are used to publish information about the | 
|  | 7085 | * names available on the host platform. | 
|  | 7086 | * | 
|  | 7087 | * Sorting the table at runtime ensures that the table is properly ordered | 
|  | 7088 | * when used, even for platforms we're not able to test on.  It also makes | 
|  | 7089 | * it easier to add additional entries to the tables. | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7090 | */ | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7091 |  | 
|  | 7092 | static int | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7093 | cmp_constdefs(const void *v1,  const void *v2) | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7094 | { | 
|  | 7095 | const struct constdef *c1 = | 
|  | 7096 | (const struct constdef *) v1; | 
|  | 7097 | const struct constdef *c2 = | 
|  | 7098 | (const struct constdef *) v2; | 
|  | 7099 |  | 
|  | 7100 | return strcmp(c1->name, c2->name); | 
|  | 7101 | } | 
|  | 7102 |  | 
|  | 7103 | static int | 
| Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7104 | setup_confname_table(struct constdef *table, size_t tablesize, | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7105 | char *tablename, PyObject *module) | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7106 | { | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7107 | PyObject *d = NULL; | 
| Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7108 | size_t i; | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7109 |  | 
|  | 7110 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); | 
|  | 7111 | d = PyDict_New(); | 
| Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7112 | if (d == NULL) | 
|  | 7113 | return -1; | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7114 |  | 
| Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7115 | for (i=0; i < tablesize; ++i) { | 
|  | 7116 | PyObject *o = PyInt_FromLong(table[i].value); | 
|  | 7117 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { | 
|  | 7118 | Py_XDECREF(o); | 
|  | 7119 | Py_DECREF(d); | 
|  | 7120 | return -1; | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7121 | } | 
| Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7122 | Py_DECREF(o); | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7123 | } | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7124 | return PyModule_AddObject(module, tablename, d); | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7125 | } | 
|  | 7126 |  | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7127 | /* Return -1 on failure, 0 on success. */ | 
|  | 7128 | static int | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7129 | setup_confname_tables(PyObject *module) | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7130 | { | 
|  | 7131 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7132 | if (setup_confname_table(posix_constants_pathconf, | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7133 | sizeof(posix_constants_pathconf) | 
|  | 7134 | / sizeof(struct constdef), | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7135 | "pathconf_names", module)) | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7136 | return -1; | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7137 | #endif | 
|  | 7138 | #ifdef HAVE_CONFSTR | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7139 | if (setup_confname_table(posix_constants_confstr, | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7140 | sizeof(posix_constants_confstr) | 
|  | 7141 | / sizeof(struct constdef), | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7142 | "confstr_names", module)) | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7143 | return -1; | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7144 | #endif | 
|  | 7145 | #ifdef HAVE_SYSCONF | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7146 | if (setup_confname_table(posix_constants_sysconf, | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7147 | sizeof(posix_constants_sysconf) | 
|  | 7148 | / sizeof(struct constdef), | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7149 | "sysconf_names", module)) | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7150 | return -1; | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7151 | #endif | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7152 | return 0; | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7153 | } | 
| Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7154 |  | 
|  | 7155 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7156 | PyDoc_STRVAR(posix_abort__doc__, | 
| Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7157 | "abort() -> does not return!\n\n\ | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7158 | 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] | 7159 | in the hardest way possible on the hosting operating system."); | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7160 |  | 
|  | 7161 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7162 | posix_abort(PyObject *self, PyObject *noargs) | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7163 | { | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7164 | abort(); | 
|  | 7165 | /*NOTREACHED*/ | 
|  | 7166 | Py_FatalError("abort() called from Python code didn't abort!"); | 
|  | 7167 | return NULL; | 
|  | 7168 | } | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7169 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7170 | #ifdef MS_WINDOWS | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7171 | PyDoc_STRVAR(win32_startfile__doc__, | 
|  | 7172 | "startfile(filepath) - Start a file with its associated application.\n\ | 
| Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7173 | \n\ | 
|  | 7174 | This acts like double-clicking the file in Explorer, or giving the file\n\ | 
|  | 7175 | name as an argument to the DOS \"start\" command:  the file is opened\n\ | 
|  | 7176 | with whatever application (if any) its extension is associated.\n\ | 
|  | 7177 | \n\ | 
|  | 7178 | startfile returns as soon as the associated application is launched.\n\ | 
|  | 7179 | There is no option to wait for the application to close, and no way\n\ | 
|  | 7180 | to retrieve the application's exit status.\n\ | 
|  | 7181 | \n\ | 
|  | 7182 | The filepath is relative to the current directory.  If you want to use\n\ | 
|  | 7183 | 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] | 7184 | the underlying Win32 ShellExecute function doesn't work if it is."); | 
| Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7185 |  | 
|  | 7186 | static PyObject * | 
|  | 7187 | win32_startfile(PyObject *self, PyObject *args) | 
|  | 7188 | { | 
|  | 7189 | char *filepath; | 
|  | 7190 | HINSTANCE rc; | 
|  | 7191 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) | 
|  | 7192 | return NULL; | 
|  | 7193 | Py_BEGIN_ALLOW_THREADS | 
|  | 7194 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); | 
|  | 7195 | Py_END_ALLOW_THREADS | 
|  | 7196 | if (rc <= (HINSTANCE)32) | 
|  | 7197 | return win32_error("startfile", filepath); | 
|  | 7198 | Py_INCREF(Py_None); | 
|  | 7199 | return Py_None; | 
|  | 7200 | } | 
|  | 7201 | #endif | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7202 |  | 
| Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7203 | #ifdef HAVE_GETLOADAVG | 
|  | 7204 | PyDoc_STRVAR(posix_getloadavg__doc__, | 
|  | 7205 | "getloadavg() -> (float, float, float)\n\n\ | 
|  | 7206 | Return the number of processes in the system run queue averaged over\n\ | 
|  | 7207 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ | 
|  | 7208 | was unobtainable"); | 
|  | 7209 |  | 
|  | 7210 | static PyObject * | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7211 | posix_getloadavg(PyObject *self, PyObject *noargs) | 
| Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7212 | { | 
|  | 7213 | double loadavg[3]; | 
| Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7214 | if (getloadavg(loadavg, 3)!=3) { | 
|  | 7215 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); | 
|  | 7216 | return NULL; | 
|  | 7217 | } else | 
|  | 7218 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); | 
|  | 7219 | } | 
|  | 7220 | #endif | 
|  | 7221 |  | 
|  | 7222 |  | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7223 | static PyMethodDef posix_methods[] = { | 
|  | 7224 | {"access",	posix_access, METH_VARARGS, posix_access__doc__}, | 
|  | 7225 | #ifdef HAVE_TTYNAME | 
|  | 7226 | {"ttyname",	posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, | 
|  | 7227 | #endif | 
|  | 7228 | {"chdir",	posix_chdir, METH_VARARGS, posix_chdir__doc__}, | 
|  | 7229 | {"chmod",	posix_chmod, METH_VARARGS, posix_chmod__doc__}, | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7230 | #ifdef HAVE_CHOWN | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7231 | {"chown",	posix_chown, METH_VARARGS, posix_chown__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7232 | #endif /* HAVE_CHOWN */ | 
| Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7233 | #ifdef HAVE_LCHOWN | 
|  | 7234 | {"lchown",	posix_lchown, METH_VARARGS, posix_lchown__doc__}, | 
|  | 7235 | #endif /* HAVE_LCHOWN */ | 
| Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7236 | #ifdef HAVE_CHROOT | 
|  | 7237 | {"chroot",	posix_chroot, METH_VARARGS, posix_chroot__doc__}, | 
|  | 7238 | #endif | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7239 | #ifdef HAVE_CTERMID | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7240 | {"ctermid",	posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7241 | #endif | 
| Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7242 | #ifdef HAVE_GETCWD | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7243 | {"getcwd",	posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, | 
| Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7244 | #ifdef Py_USING_UNICODE | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7245 | {"getcwdu",	posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, | 
| Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7246 | #endif | 
| Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7247 | #endif | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7248 | #ifdef HAVE_LINK | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7249 | {"link",	posix_link, METH_VARARGS, posix_link__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7250 | #endif /* HAVE_LINK */ | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7251 | {"listdir",	posix_listdir, METH_VARARGS, posix_listdir__doc__}, | 
|  | 7252 | {"lstat",	posix_lstat, METH_VARARGS, posix_lstat__doc__}, | 
|  | 7253 | {"mkdir",	posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7254 | #ifdef HAVE_NICE | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7255 | {"nice",	posix_nice, METH_VARARGS, posix_nice__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7256 | #endif /* HAVE_NICE */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7257 | #ifdef HAVE_READLINK | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7258 | {"readlink",	posix_readlink, METH_VARARGS, posix_readlink__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7259 | #endif /* HAVE_READLINK */ | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7260 | {"rename",	posix_rename, METH_VARARGS, posix_rename__doc__}, | 
|  | 7261 | {"rmdir",	posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, | 
|  | 7262 | {"stat",	posix_stat, METH_VARARGS, posix_stat__doc__}, | 
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7263 | {"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] | 7264 | #ifdef HAVE_SYMLINK | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7265 | {"symlink",	posix_symlink, METH_VARARGS, posix_symlink__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7266 | #endif /* HAVE_SYMLINK */ | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7267 | #ifdef HAVE_SYSTEM | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7268 | {"system",	posix_system, METH_VARARGS, posix_system__doc__}, | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7269 | #endif | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7270 | {"umask",	posix_umask, METH_VARARGS, posix_umask__doc__}, | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7271 | #ifdef HAVE_UNAME | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7272 | {"uname",	posix_uname, METH_NOARGS, posix_uname__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7273 | #endif /* HAVE_UNAME */ | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7274 | {"unlink",	posix_unlink, METH_VARARGS, posix_unlink__doc__}, | 
|  | 7275 | {"remove",	posix_unlink, METH_VARARGS, posix_remove__doc__}, | 
|  | 7276 | {"utime",	posix_utime, METH_VARARGS, posix_utime__doc__}, | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7277 | #ifdef HAVE_TIMES | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7278 | {"times",	posix_times, METH_NOARGS, posix_times__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7279 | #endif /* HAVE_TIMES */ | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7280 | {"_exit",	posix__exit, METH_VARARGS, posix__exit__doc__}, | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7281 | #ifdef HAVE_EXECV | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7282 | {"execv",	posix_execv, METH_VARARGS, posix_execv__doc__}, | 
|  | 7283 | {"execve",	posix_execve, METH_VARARGS, posix_execve__doc__}, | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7284 | #endif /* HAVE_EXECV */ | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7285 | #ifdef HAVE_SPAWNV | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7286 | {"spawnv",	posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, | 
|  | 7287 | {"spawnve",	posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, | 
| Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7288 | #if defined(PYOS_OS2) | 
|  | 7289 | {"spawnvp",	posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, | 
|  | 7290 | {"spawnvpe",	posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, | 
|  | 7291 | #endif /* PYOS_OS2 */ | 
| Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7292 | #endif /* HAVE_SPAWNV */ | 
| Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7293 | #ifdef HAVE_FORK1 | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7294 | {"fork1",       posix_fork1, METH_NOARGS, posix_fork1__doc__}, | 
| Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7295 | #endif /* HAVE_FORK1 */ | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7296 | #ifdef HAVE_FORK | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7297 | {"fork",	posix_fork, METH_NOARGS, posix_fork__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7298 | #endif /* HAVE_FORK */ | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7299 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7300 | {"openpty",	posix_openpty, METH_NOARGS, posix_openpty__doc__}, | 
| Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7301 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7302 | #ifdef HAVE_FORKPTY | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7303 | {"forkpty",	posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, | 
| Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7304 | #endif /* HAVE_FORKPTY */ | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7305 | #ifdef HAVE_GETEGID | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7306 | {"getegid",	posix_getegid, METH_NOARGS, posix_getegid__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7307 | #endif /* HAVE_GETEGID */ | 
|  | 7308 | #ifdef HAVE_GETEUID | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7309 | {"geteuid",	posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7310 | #endif /* HAVE_GETEUID */ | 
|  | 7311 | #ifdef HAVE_GETGID | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7312 | {"getgid",	posix_getgid, METH_NOARGS, posix_getgid__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7313 | #endif /* HAVE_GETGID */ | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7314 | #ifdef HAVE_GETGROUPS | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7315 | {"getgroups",	posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7316 | #endif | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7317 | {"getpid",	posix_getpid, METH_NOARGS, posix_getpid__doc__}, | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7318 | #ifdef HAVE_GETPGRP | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7319 | {"getpgrp",	posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7320 | #endif /* HAVE_GETPGRP */ | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7321 | #ifdef HAVE_GETPPID | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7322 | {"getppid",	posix_getppid, METH_NOARGS, posix_getppid__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7323 | #endif /* HAVE_GETPPID */ | 
|  | 7324 | #ifdef HAVE_GETUID | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7325 | {"getuid",	posix_getuid, METH_NOARGS, posix_getuid__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7326 | #endif /* HAVE_GETUID */ | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7327 | #ifdef HAVE_GETLOGIN | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7328 | {"getlogin",	posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, | 
| Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7329 | #endif | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7330 | #ifdef HAVE_KILL | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7331 | {"kill",	posix_kill, METH_VARARGS, posix_kill__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7332 | #endif /* HAVE_KILL */ | 
| Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7333 | #ifdef HAVE_KILLPG | 
|  | 7334 | {"killpg",	posix_killpg, METH_VARARGS, posix_killpg__doc__}, | 
|  | 7335 | #endif /* HAVE_KILLPG */ | 
| Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7336 | #ifdef HAVE_PLOCK | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7337 | {"plock",	posix_plock, METH_VARARGS, posix_plock__doc__}, | 
| Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7338 | #endif /* HAVE_PLOCK */ | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7339 | #ifdef HAVE_POPEN | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7340 | {"popen",	posix_popen, METH_VARARGS, posix_popen__doc__}, | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7341 | #ifdef MS_WINDOWS | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7342 | {"popen2",	win32_popen2, METH_VARARGS}, | 
|  | 7343 | {"popen3",	win32_popen3, METH_VARARGS}, | 
|  | 7344 | {"popen4",	win32_popen4, METH_VARARGS}, | 
| Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7345 | {"startfile",	win32_startfile, METH_VARARGS, win32_startfile__doc__}, | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7346 | #else | 
|  | 7347 | #if defined(PYOS_OS2) && defined(PYCC_GCC) | 
|  | 7348 | {"popen2",	os2emx_popen2, METH_VARARGS}, | 
|  | 7349 | {"popen3",	os2emx_popen3, METH_VARARGS}, | 
|  | 7350 | {"popen4",	os2emx_popen4, METH_VARARGS}, | 
|  | 7351 | #endif | 
| Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7352 | #endif | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7353 | #endif /* HAVE_POPEN */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7354 | #ifdef HAVE_SETUID | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7355 | {"setuid",	posix_setuid, METH_VARARGS, posix_setuid__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7356 | #endif /* HAVE_SETUID */ | 
| Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7357 | #ifdef HAVE_SETEUID | 
|  | 7358 | {"seteuid",	posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, | 
|  | 7359 | #endif /* HAVE_SETEUID */ | 
|  | 7360 | #ifdef HAVE_SETEGID | 
|  | 7361 | {"setegid",	posix_setegid, METH_VARARGS, posix_setegid__doc__}, | 
|  | 7362 | #endif /* HAVE_SETEGID */ | 
|  | 7363 | #ifdef HAVE_SETREUID | 
|  | 7364 | {"setreuid",	posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, | 
|  | 7365 | #endif /* HAVE_SETREUID */ | 
|  | 7366 | #ifdef HAVE_SETREGID | 
|  | 7367 | {"setregid",	posix_setregid,	METH_VARARGS, posix_setregid__doc__}, | 
|  | 7368 | #endif /* HAVE_SETREGID */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7369 | #ifdef HAVE_SETGID | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7370 | {"setgid",	posix_setgid, METH_VARARGS, posix_setgid__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7371 | #endif /* HAVE_SETGID */ | 
| Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7372 | #ifdef HAVE_SETGROUPS | 
|  | 7373 | {"setgroups",	posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, | 
|  | 7374 | #endif /* HAVE_SETGROUPS */ | 
| Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7375 | #ifdef HAVE_GETPGID | 
|  | 7376 | {"getpgid",	posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, | 
|  | 7377 | #endif /* HAVE_GETPGID */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7378 | #ifdef HAVE_SETPGRP | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7379 | {"setpgrp",	posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7380 | #endif /* HAVE_SETPGRP */ | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7381 | #ifdef HAVE_WAIT | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7382 | {"wait",	posix_wait, METH_NOARGS, posix_wait__doc__}, | 
| Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7383 | #endif /* HAVE_WAIT */ | 
| Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7384 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7385 | {"waitpid",	posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7386 | #endif /* HAVE_WAITPID */ | 
| Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7387 | #ifdef HAVE_GETSID | 
|  | 7388 | {"getsid",	posix_getsid, METH_VARARGS, posix_getsid__doc__}, | 
|  | 7389 | #endif /* HAVE_GETSID */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7390 | #ifdef HAVE_SETSID | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7391 | {"setsid",	posix_setsid, METH_NOARGS, posix_setsid__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7392 | #endif /* HAVE_SETSID */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7393 | #ifdef HAVE_SETPGID | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7394 | {"setpgid",	posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7395 | #endif /* HAVE_SETPGID */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7396 | #ifdef HAVE_TCGETPGRP | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7397 | {"tcgetpgrp",	posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7398 | #endif /* HAVE_TCGETPGRP */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7399 | #ifdef HAVE_TCSETPGRP | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7400 | {"tcsetpgrp",	posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, | 
| Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7401 | #endif /* HAVE_TCSETPGRP */ | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7402 | {"open",	posix_open, METH_VARARGS, posix_open__doc__}, | 
|  | 7403 | {"close",	posix_close, METH_VARARGS, posix_close__doc__}, | 
|  | 7404 | {"dup",		posix_dup, METH_VARARGS, posix_dup__doc__}, | 
|  | 7405 | {"dup2",	posix_dup2, METH_VARARGS, posix_dup2__doc__}, | 
|  | 7406 | {"lseek",	posix_lseek, METH_VARARGS, posix_lseek__doc__}, | 
|  | 7407 | {"read",	posix_read, METH_VARARGS, posix_read__doc__}, | 
|  | 7408 | {"write",	posix_write, METH_VARARGS, posix_write__doc__}, | 
|  | 7409 | {"fstat",	posix_fstat, METH_VARARGS, posix_fstat__doc__}, | 
|  | 7410 | {"fdopen",	posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, | 
| Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7411 | {"isatty",	posix_isatty, METH_VARARGS, posix_isatty__doc__}, | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7412 | #ifdef HAVE_PIPE | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7413 | {"pipe",	posix_pipe, METH_NOARGS, posix_pipe__doc__}, | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7414 | #endif | 
|  | 7415 | #ifdef HAVE_MKFIFO | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7416 | {"mkfifo",	posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7417 | #endif | 
| Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7418 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) | 
| Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7419 | {"mknod",	posix_mknod, METH_VARARGS, posix_mknod__doc__}, | 
|  | 7420 | #endif | 
| Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7421 | #ifdef HAVE_DEVICE_MACROS | 
|  | 7422 | {"major",	posix_major, METH_VARARGS, posix_major__doc__}, | 
|  | 7423 | {"minor",	posix_minor, METH_VARARGS, posix_minor__doc__}, | 
|  | 7424 | {"makedev",	posix_makedev, METH_VARARGS, posix_makedev__doc__}, | 
|  | 7425 | #endif | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7426 | #ifdef HAVE_FTRUNCATE | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7427 | {"ftruncate",	posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, | 
| Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7428 | #endif | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7429 | #ifdef HAVE_PUTENV | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7430 | {"putenv",	posix_putenv, METH_VARARGS, posix_putenv__doc__}, | 
| Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7431 | #endif | 
| Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7432 | #ifdef HAVE_UNSETENV | 
|  | 7433 | {"unsetenv",	posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, | 
|  | 7434 | #endif | 
| Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7435 | #ifdef HAVE_STRERROR | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7436 | {"strerror",	posix_strerror, METH_VARARGS, posix_strerror__doc__}, | 
| Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7437 | #endif | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7438 | #ifdef HAVE_FCHDIR | 
|  | 7439 | {"fchdir",	posix_fchdir, METH_O, posix_fchdir__doc__}, | 
|  | 7440 | #endif | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7441 | #ifdef HAVE_FSYNC | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7442 | {"fsync",       posix_fsync, METH_O, posix_fsync__doc__}, | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7443 | #endif | 
|  | 7444 | #ifdef HAVE_FDATASYNC | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7445 | {"fdatasync",   posix_fdatasync,  METH_O, posix_fdatasync__doc__}, | 
| Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7446 | #endif | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7447 | #ifdef HAVE_SYS_WAIT_H | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7448 | #ifdef WCOREDUMP | 
|  | 7449 | {"WCOREDUMP",	posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, | 
|  | 7450 | #endif /* WCOREDUMP */ | 
| Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7451 | #ifdef WIFCONTINUED | 
|  | 7452 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, | 
|  | 7453 | #endif /* WIFCONTINUED */ | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7454 | #ifdef WIFSTOPPED | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7455 | {"WIFSTOPPED",	posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7456 | #endif /* WIFSTOPPED */ | 
|  | 7457 | #ifdef WIFSIGNALED | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7458 | {"WIFSIGNALED",	posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7459 | #endif /* WIFSIGNALED */ | 
|  | 7460 | #ifdef WIFEXITED | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7461 | {"WIFEXITED",	posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7462 | #endif /* WIFEXITED */ | 
|  | 7463 | #ifdef WEXITSTATUS | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7464 | {"WEXITSTATUS",	posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7465 | #endif /* WEXITSTATUS */ | 
|  | 7466 | #ifdef WTERMSIG | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7467 | {"WTERMSIG",	posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7468 | #endif /* WTERMSIG */ | 
|  | 7469 | #ifdef WSTOPSIG | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7470 | {"WSTOPSIG",	posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, | 
| Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7471 | #endif /* WSTOPSIG */ | 
|  | 7472 | #endif /* HAVE_SYS_WAIT_H */ | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7473 | #ifdef HAVE_FSTATVFS | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7474 | {"fstatvfs",	posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7475 | #endif | 
|  | 7476 | #ifdef HAVE_STATVFS | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7477 | {"statvfs",	posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7478 | #endif | 
| Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 7479 | #ifdef HAVE_TMPFILE | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7480 | {"tmpfile",	posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7481 | #endif | 
|  | 7482 | #ifdef HAVE_TEMPNAM | 
|  | 7483 | {"tempnam",	posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, | 
|  | 7484 | #endif | 
|  | 7485 | #ifdef HAVE_TMPNAM | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7486 | {"tmpnam",	posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7487 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7488 | #ifdef HAVE_CONFSTR | 
|  | 7489 | {"confstr",	posix_confstr, METH_VARARGS, posix_confstr__doc__}, | 
|  | 7490 | #endif | 
|  | 7491 | #ifdef HAVE_SYSCONF | 
|  | 7492 | {"sysconf",	posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, | 
|  | 7493 | #endif | 
|  | 7494 | #ifdef HAVE_FPATHCONF | 
|  | 7495 | {"fpathconf",	posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, | 
|  | 7496 | #endif | 
|  | 7497 | #ifdef HAVE_PATHCONF | 
|  | 7498 | {"pathconf",	posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, | 
|  | 7499 | #endif | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7500 | {"abort",	posix_abort, METH_NOARGS, posix_abort__doc__}, | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7501 | #ifdef MS_WINDOWS | 
| Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7502 | {"_getfullpathname",	posix__getfullpathname, METH_VARARGS, NULL}, | 
|  | 7503 | #endif | 
| Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7504 | #ifdef HAVE_GETLOADAVG | 
| Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7505 | {"getloadavg",	posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, | 
| Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7506 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7507 | {NULL,		NULL}		 /* Sentinel */ | 
|  | 7508 | }; | 
|  | 7509 |  | 
|  | 7510 |  | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7511 | static int | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7512 | ins(PyObject *module, char *symbol, long value) | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7513 | { | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7514 | return PyModule_AddIntConstant(module, symbol, value); | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7515 | } | 
|  | 7516 |  | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7517 | #if defined(PYOS_OS2) | 
|  | 7518 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7519 | static int insertvalues(PyObject *module) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7520 | { | 
|  | 7521 | APIRET    rc; | 
|  | 7522 | ULONG     values[QSV_MAX+1]; | 
|  | 7523 | PyObject *v; | 
| Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7524 | char     *ver, tmp[50]; | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7525 |  | 
|  | 7526 | Py_BEGIN_ALLOW_THREADS | 
| Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7527 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7528 | Py_END_ALLOW_THREADS | 
|  | 7529 |  | 
|  | 7530 | if (rc != NO_ERROR) { | 
|  | 7531 | os2_error(rc); | 
|  | 7532 | return -1; | 
|  | 7533 | } | 
|  | 7534 |  | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7535 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; | 
|  | 7536 | if (ins(module, "memkernel",    values[QSV_TOTRESMEM])) return -1; | 
|  | 7537 | if (ins(module, "memvirtual",   values[QSV_TOTAVAILMEM])) return -1; | 
|  | 7538 | if (ins(module, "maxpathlen",   values[QSV_MAX_PATH_LENGTH])) return -1; | 
|  | 7539 | if (ins(module, "maxnamelen",   values[QSV_MAX_COMP_LENGTH])) return -1; | 
|  | 7540 | if (ins(module, "revision",     values[QSV_VERSION_REVISION])) return -1; | 
|  | 7541 | if (ins(module, "timeslice",    values[QSV_MIN_SLICE])) return -1; | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7542 |  | 
|  | 7543 | switch (values[QSV_VERSION_MINOR]) { | 
|  | 7544 | case 0:  ver = "2.00"; break; | 
|  | 7545 | case 10: ver = "2.10"; break; | 
|  | 7546 | case 11: ver = "2.11"; break; | 
|  | 7547 | case 30: ver = "3.00"; break; | 
|  | 7548 | case 40: ver = "4.00"; break; | 
|  | 7549 | case 50: ver = "5.00"; break; | 
|  | 7550 | default: | 
| Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7551 | PyOS_snprintf(tmp, sizeof(tmp), | 
|  | 7552 | "%d-%d", values[QSV_VERSION_MAJOR], | 
|  | 7553 | values[QSV_VERSION_MINOR]); | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7554 | ver = &tmp[0]; | 
|  | 7555 | } | 
|  | 7556 |  | 
|  | 7557 | /* Add Indicator of the Version of the Operating System */ | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7558 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7559 | return -1; | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7560 |  | 
|  | 7561 | /* Add Indicator of Which Drive was Used to Boot the System */ | 
|  | 7562 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; | 
|  | 7563 | tmp[1] = ':'; | 
|  | 7564 | tmp[2] = '\0'; | 
|  | 7565 |  | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7566 | return PyModule_AddStringConstant(module, "bootdrive", tmp); | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7567 | } | 
|  | 7568 | #endif | 
|  | 7569 |  | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7570 | static int | 
| Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7571 | all_ins(PyObject *d) | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7572 | { | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7573 | #ifdef F_OK | 
|  | 7574 | if (ins(d, "F_OK", (long)F_OK)) return -1; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7575 | #endif | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7576 | #ifdef R_OK | 
|  | 7577 | if (ins(d, "R_OK", (long)R_OK)) return -1; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7578 | #endif | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7579 | #ifdef W_OK | 
|  | 7580 | if (ins(d, "W_OK", (long)W_OK)) return -1; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7581 | #endif | 
| Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7582 | #ifdef X_OK | 
|  | 7583 | if (ins(d, "X_OK", (long)X_OK)) return -1; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7584 | #endif | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7585 | #ifdef NGROUPS_MAX | 
|  | 7586 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; | 
|  | 7587 | #endif | 
| Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7588 | #ifdef TMP_MAX | 
|  | 7589 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; | 
|  | 7590 | #endif | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7591 | #ifdef WCONTINUED | 
|  | 7592 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; | 
|  | 7593 | #endif | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7594 | #ifdef WNOHANG | 
|  | 7595 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7596 | #endif | 
| Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7597 | #ifdef WUNTRACED | 
|  | 7598 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; | 
|  | 7599 | #endif | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7600 | #ifdef O_RDONLY | 
|  | 7601 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; | 
|  | 7602 | #endif | 
|  | 7603 | #ifdef O_WRONLY | 
|  | 7604 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; | 
|  | 7605 | #endif | 
|  | 7606 | #ifdef O_RDWR | 
|  | 7607 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; | 
|  | 7608 | #endif | 
|  | 7609 | #ifdef O_NDELAY | 
|  | 7610 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; | 
|  | 7611 | #endif | 
|  | 7612 | #ifdef O_NONBLOCK | 
|  | 7613 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; | 
|  | 7614 | #endif | 
|  | 7615 | #ifdef O_APPEND | 
|  | 7616 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; | 
|  | 7617 | #endif | 
|  | 7618 | #ifdef O_DSYNC | 
|  | 7619 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; | 
|  | 7620 | #endif | 
|  | 7621 | #ifdef O_RSYNC | 
|  | 7622 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; | 
|  | 7623 | #endif | 
|  | 7624 | #ifdef O_SYNC | 
|  | 7625 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; | 
|  | 7626 | #endif | 
|  | 7627 | #ifdef O_NOCTTY | 
|  | 7628 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; | 
|  | 7629 | #endif | 
|  | 7630 | #ifdef O_CREAT | 
|  | 7631 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; | 
|  | 7632 | #endif | 
|  | 7633 | #ifdef O_EXCL | 
|  | 7634 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; | 
|  | 7635 | #endif | 
|  | 7636 | #ifdef O_TRUNC | 
|  | 7637 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; | 
|  | 7638 | #endif | 
| Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7639 | #ifdef O_BINARY | 
|  | 7640 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; | 
|  | 7641 | #endif | 
|  | 7642 | #ifdef O_TEXT | 
|  | 7643 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; | 
|  | 7644 | #endif | 
| Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7645 | #ifdef O_LARGEFILE | 
|  | 7646 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; | 
|  | 7647 | #endif | 
|  | 7648 |  | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7649 | /* MS Windows */ | 
|  | 7650 | #ifdef O_NOINHERIT | 
|  | 7651 | /* Don't inherit in child processes. */ | 
|  | 7652 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; | 
|  | 7653 | #endif | 
|  | 7654 | #ifdef _O_SHORT_LIVED | 
|  | 7655 | /* Optimize for short life (keep in memory). */ | 
|  | 7656 | /* MS forgot to define this one with a non-underscore form too. */ | 
|  | 7657 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; | 
|  | 7658 | #endif | 
|  | 7659 | #ifdef O_TEMPORARY | 
|  | 7660 | /* Automatically delete when last handle is closed. */ | 
|  | 7661 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; | 
|  | 7662 | #endif | 
|  | 7663 | #ifdef O_RANDOM | 
|  | 7664 | /* Optimize for random access. */ | 
|  | 7665 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; | 
|  | 7666 | #endif | 
|  | 7667 | #ifdef O_SEQUENTIAL | 
|  | 7668 | /* Optimize for sequential access. */ | 
|  | 7669 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; | 
|  | 7670 | #endif | 
|  | 7671 |  | 
| Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7672 | /* GNU extensions. */ | 
|  | 7673 | #ifdef O_DIRECT | 
|  | 7674 | /* Direct disk access. */ | 
|  | 7675 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; | 
|  | 7676 | #endif | 
|  | 7677 | #ifdef O_DIRECTORY | 
|  | 7678 | /* Must be a directory.	 */ | 
|  | 7679 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; | 
|  | 7680 | #endif | 
|  | 7681 | #ifdef O_NOFOLLOW | 
|  | 7682 | /* Do not follow links.	 */ | 
|  | 7683 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; | 
|  | 7684 | #endif | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7685 |  | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7686 | /* These come from sysexits.h */ | 
|  | 7687 | #ifdef EX_OK | 
|  | 7688 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7689 | #endif /* EX_OK */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7690 | #ifdef EX_USAGE | 
|  | 7691 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7692 | #endif /* EX_USAGE */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7693 | #ifdef EX_DATAERR | 
|  | 7694 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7695 | #endif /* EX_DATAERR */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7696 | #ifdef EX_NOINPUT | 
|  | 7697 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7698 | #endif /* EX_NOINPUT */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7699 | #ifdef EX_NOUSER | 
|  | 7700 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7701 | #endif /* EX_NOUSER */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7702 | #ifdef EX_NOHOST | 
|  | 7703 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7704 | #endif /* EX_NOHOST */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7705 | #ifdef EX_UNAVAILABLE | 
|  | 7706 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7707 | #endif /* EX_UNAVAILABLE */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7708 | #ifdef EX_SOFTWARE | 
|  | 7709 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7710 | #endif /* EX_SOFTWARE */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7711 | #ifdef EX_OSERR | 
|  | 7712 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7713 | #endif /* EX_OSERR */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7714 | #ifdef EX_OSFILE | 
|  | 7715 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7716 | #endif /* EX_OSFILE */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7717 | #ifdef EX_CANTCREAT | 
|  | 7718 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7719 | #endif /* EX_CANTCREAT */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7720 | #ifdef EX_IOERR | 
|  | 7721 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7722 | #endif /* EX_IOERR */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7723 | #ifdef EX_TEMPFAIL | 
|  | 7724 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7725 | #endif /* EX_TEMPFAIL */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7726 | #ifdef EX_PROTOCOL | 
|  | 7727 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7728 | #endif /* EX_PROTOCOL */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7729 | #ifdef EX_NOPERM | 
|  | 7730 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7731 | #endif /* EX_NOPERM */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7732 | #ifdef EX_CONFIG | 
|  | 7733 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7734 | #endif /* EX_CONFIG */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7735 | #ifdef EX_NOTFOUND | 
|  | 7736 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; | 
| Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7737 | #endif /* EX_NOTFOUND */ | 
| Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7738 |  | 
| Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7739 | #ifdef HAVE_SPAWNV | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7740 | #if defined(PYOS_OS2) && defined(PYCC_GCC) | 
|  | 7741 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; | 
|  | 7742 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; | 
|  | 7743 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; | 
|  | 7744 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; | 
|  | 7745 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; | 
|  | 7746 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; | 
|  | 7747 | if (ins(d, "P_PM", (long)P_PM)) return -1; | 
|  | 7748 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; | 
|  | 7749 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; | 
|  | 7750 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; | 
|  | 7751 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; | 
|  | 7752 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; | 
|  | 7753 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; | 
|  | 7754 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; | 
|  | 7755 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; | 
|  | 7756 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; | 
|  | 7757 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; | 
|  | 7758 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; | 
|  | 7759 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; | 
|  | 7760 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; | 
|  | 7761 | #else | 
| Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7762 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; | 
|  | 7763 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; | 
|  | 7764 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; | 
|  | 7765 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; | 
|  | 7766 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; | 
| Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7767 | #endif | 
| Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7768 | #endif | 
| Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7769 |  | 
| Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7770 | #if defined(PYOS_OS2) | 
|  | 7771 | if (insertvalues(d)) return -1; | 
|  | 7772 | #endif | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7773 | return 0; | 
|  | 7774 | } | 
|  | 7775 |  | 
|  | 7776 |  | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7777 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) | 
| Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7778 | #define INITFUNC initnt | 
|  | 7779 | #define MODNAME "nt" | 
| Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7780 |  | 
|  | 7781 | #elif defined(PYOS_OS2) | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7782 | #define INITFUNC initos2 | 
|  | 7783 | #define MODNAME "os2" | 
| Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7784 |  | 
| Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7785 | #else | 
| Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7786 | #define INITFUNC initposix | 
|  | 7787 | #define MODNAME "posix" | 
|  | 7788 | #endif | 
|  | 7789 |  | 
| Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7790 | PyMODINIT_FUNC | 
| Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7791 | INITFUNC(void) | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7792 | { | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7793 | PyObject *m, *v; | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7794 |  | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7795 | m = Py_InitModule3(MODNAME, | 
| Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7796 | posix_methods, | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7797 | posix__doc__); | 
| Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7798 |  | 
| Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7799 | /* Initialize environ dictionary */ | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7800 | v = convertenviron(); | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7801 | Py_XINCREF(v); | 
|  | 7802 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) | 
| Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7803 | return; | 
| Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7804 | Py_DECREF(v); | 
| Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7805 |  | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7806 | if (all_ins(m)) | 
| Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7807 | return; | 
|  | 7808 |  | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7809 | if (setup_confname_tables(m)) | 
| Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7810 | return; | 
|  | 7811 |  | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7812 | Py_INCREF(PyExc_OSError); | 
|  | 7813 | PyModule_AddObject(m, "error", PyExc_OSError); | 
| Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7814 |  | 
| Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7815 | #ifdef HAVE_PUTENV | 
| Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7816 | if (posix_putenv_garbage == NULL) | 
|  | 7817 | posix_putenv_garbage = PyDict_New(); | 
| Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7818 | #endif | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7819 |  | 
| Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7820 | stat_result_desc.name = MODNAME ".stat_result"; | 
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7821 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; | 
|  | 7822 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; | 
|  | 7823 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7824 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); | 
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7825 | structseq_new = StatResultType.tp_new; | 
|  | 7826 | StatResultType.tp_new = statresult_new; | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7827 | Py_INCREF((PyObject*) &StatResultType); | 
|  | 7828 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7829 |  | 
| Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7830 | statvfs_result_desc.name = MODNAME ".statvfs_result"; | 
| Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7831 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); | 
| Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7832 | Py_INCREF((PyObject*) &StatVFSResultType); | 
|  | 7833 | PyModule_AddObject(m, "statvfs_result", | 
|  | 7834 | (PyObject*) &StatVFSResultType); | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7835 | } |