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 |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WIN32' 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 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 16 | static char posix__doc__ [] = |
| 17 | "This module provides access to operating system functionality that is\n\ |
| 18 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 19 | disguised Unix interface). Refer to the library manual and\n\ |
| 20 | corresponding Unix manual entries for more information on calls."; |
| 21 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 22 | #include "Python.h" |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 23 | #include "structseq.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 25 | #if defined(PYOS_OS2) |
| 26 | #define INCL_DOS |
| 27 | #define INCL_DOSERRORS |
| 28 | #define INCL_DOSPROCESS |
| 29 | #define INCL_NOPMAPI |
| 30 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 31 | #if defined(PYCC_GCC) |
| 32 | #include <ctype.h> |
| 33 | #include <io.h> |
| 34 | #include <stdio.h> |
| 35 | #include <process.h> |
| 36 | #include "osdefs.h" |
| 37 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 40 | #include <sys/types.h> |
| 41 | #include <sys/stat.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 43 | #ifdef HAVE_SYS_WAIT_H |
| 44 | #include <sys/wait.h> /* For WNOHANG */ |
| 45 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 47 | #ifdef HAVE_SIGNAL_H |
| 48 | #include <signal.h> |
| 49 | #endif |
| 50 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 51 | #ifdef HAVE_FCNTL_H |
| 52 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 53 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 55 | #ifdef HAVE_GRP_H |
| 56 | #include <grp.h> |
| 57 | #endif |
| 58 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 59 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 60 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 61 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 62 | #include <process.h> |
| 63 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 64 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 65 | #define HAVE_GETCWD 1 |
| 66 | #define HAVE_OPENDIR 1 |
| 67 | #define HAVE_SYSTEM 1 |
| 68 | #if defined(__OS2__) |
| 69 | #define HAVE_EXECV 1 |
| 70 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 71 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 72 | #include <process.h> |
| 73 | #else |
| 74 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 75 | #define HAVE_EXECV 1 |
| 76 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 77 | #define HAVE_OPENDIR 1 |
| 78 | #define HAVE_PIPE 1 |
| 79 | #define HAVE_POPEN 1 |
| 80 | #define HAVE_SYSTEM 1 |
| 81 | #define HAVE_WAIT 1 |
| 82 | #else |
| 83 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 84 | #define HAVE_GETCWD 1 |
| 85 | #ifdef MS_WIN32 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 86 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 87 | #define HAVE_EXECV 1 |
| 88 | #define HAVE_PIPE 1 |
| 89 | #define HAVE_POPEN 1 |
| 90 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 91 | #define HAVE_CWAIT 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 92 | #else /* 16-bit Windows */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 93 | #endif /* !MS_WIN32 */ |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 94 | #else |
| 95 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 96 | /* Everything needed is defined in PC/os2emx/pyconfig.h */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 97 | #else /* all other compilers */ |
| 98 | /* Unix functions that the configure script doesn't check for */ |
| 99 | #define HAVE_EXECV 1 |
| 100 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 101 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 102 | #define HAVE_FORK1 1 |
| 103 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 104 | #define HAVE_GETCWD 1 |
| 105 | #define HAVE_GETEGID 1 |
| 106 | #define HAVE_GETEUID 1 |
| 107 | #define HAVE_GETGID 1 |
| 108 | #define HAVE_GETPPID 1 |
| 109 | #define HAVE_GETUID 1 |
| 110 | #define HAVE_KILL 1 |
| 111 | #define HAVE_OPENDIR 1 |
| 112 | #define HAVE_PIPE 1 |
| 113 | #define HAVE_POPEN 1 |
| 114 | #define HAVE_SYSTEM 1 |
| 115 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 116 | #define HAVE_TTYNAME 1 |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 117 | #endif /* PYOS_OS2 && PYCC_GCC */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 118 | #endif /* _MSC_VER */ |
| 119 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 120 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 121 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 122 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 123 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 125 | #if defined(sun) && !defined(__SVR4) |
| 126 | /* SunOS 4.1.4 doesn't have prototypes for these: */ |
| 127 | extern int rename(const char *, const char *); |
| 128 | extern int pclose(FILE *); |
| 129 | extern int fclose(FILE *); |
Thomas Wouters | 0f954a4 | 2001-02-15 08:46:56 +0000 | [diff] [blame] | 130 | extern int fsync(int); |
| 131 | extern int lstat(const char *, struct stat *); |
| 132 | extern int symlink(const char *, const char *); |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 133 | #endif |
| 134 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 135 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 136 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 137 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 138 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 139 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 140 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 141 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 142 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 143 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 144 | #endif |
| 145 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 146 | extern int chdir(char *); |
| 147 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 148 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 149 | extern int chdir(const char *); |
| 150 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 151 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 152 | #ifdef __BORLANDC__ |
| 153 | extern int chmod(const char *, int); |
| 154 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 155 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 156 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 157 | extern int chown(const char *, uid_t, gid_t); |
| 158 | extern char *getcwd(char *, int); |
| 159 | extern char *strerror(int); |
| 160 | extern int link(const char *, const char *); |
| 161 | extern int rename(const char *, const char *); |
| 162 | extern int stat(const char *, struct stat *); |
| 163 | extern int unlink(const char *); |
| 164 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 165 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 166 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 167 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 168 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 170 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 171 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 173 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 175 | #ifdef HAVE_UTIME_H |
| 176 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 177 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 179 | #ifdef HAVE_SYS_UTIME_H |
| 180 | #include <sys/utime.h> |
| 181 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 182 | #endif /* HAVE_SYS_UTIME_H */ |
| 183 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 184 | #ifdef HAVE_SYS_TIMES_H |
| 185 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 186 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 187 | |
| 188 | #ifdef HAVE_SYS_PARAM_H |
| 189 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 190 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 191 | |
| 192 | #ifdef HAVE_SYS_UTSNAME_H |
| 193 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 194 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 195 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 196 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 197 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 198 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 199 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 200 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 201 | #include <direct.h> |
| 202 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 203 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 204 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 205 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 206 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 207 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 208 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 209 | #endif |
| 210 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 211 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 212 | #endif |
| 213 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 214 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 215 | #endif |
| 216 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 217 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 218 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 219 | #include <direct.h> |
| 220 | #include <io.h> |
| 221 | #include <process.h> |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 222 | #include "osdefs.h" |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 223 | #define WINDOWS_LEAN_AND_MEAN |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | #include <windows.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 225 | #ifdef MS_WIN32 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 226 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 227 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 228 | #else /* 16-bit Windows */ |
| 229 | #include <dos.h> |
| 230 | #include <ctype.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 231 | #endif /* MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 232 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 234 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 236 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 237 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 238 | #ifndef MAXPATHLEN |
| 239 | #define MAXPATHLEN 1024 |
| 240 | #endif /* MAXPATHLEN */ |
| 241 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 242 | #ifdef UNION_WAIT |
| 243 | /* Emulate some macros on systems that have a union instead of macros */ |
| 244 | |
| 245 | #ifndef WIFEXITED |
| 246 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 247 | #endif |
| 248 | |
| 249 | #ifndef WEXITSTATUS |
| 250 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 251 | #endif |
| 252 | |
| 253 | #ifndef WTERMSIG |
| 254 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 255 | #endif |
| 256 | |
| 257 | #endif /* UNION_WAIT */ |
| 258 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 259 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 260 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 261 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 262 | #define USE_CTERMID_R |
| 263 | #endif |
| 264 | |
| 265 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 266 | #define USE_TMPNAM_R |
| 267 | #endif |
| 268 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 269 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 270 | #undef STAT |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 271 | #if defined(MS_WIN64) || defined(MS_WIN32) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 272 | # define STAT _stati64 |
| 273 | # define FSTAT _fstati64 |
| 274 | # define STRUCT_STAT struct _stati64 |
| 275 | #else |
| 276 | # define STAT stat |
| 277 | # define FSTAT fstat |
| 278 | # define STRUCT_STAT struct stat |
| 279 | #endif |
| 280 | |
| 281 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 282 | /* Return a dictionary corresponding to the POSIX environment table */ |
| 283 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 284 | #if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 285 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 286 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 287 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 288 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 289 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 290 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 291 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 292 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 293 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 294 | if (d == NULL) |
| 295 | return NULL; |
| 296 | if (environ == NULL) |
| 297 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 298 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 299 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 300 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 301 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 302 | char *p = strchr(*e, '='); |
| 303 | if (p == NULL) |
| 304 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 305 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 306 | if (k == NULL) { |
| 307 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 308 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 309 | } |
| 310 | v = PyString_FromString(p+1); |
| 311 | if (v == NULL) { |
| 312 | PyErr_Clear(); |
| 313 | Py_DECREF(k); |
| 314 | continue; |
| 315 | } |
| 316 | if (PyDict_GetItem(d, k) == NULL) { |
| 317 | if (PyDict_SetItem(d, k, v) != 0) |
| 318 | PyErr_Clear(); |
| 319 | } |
| 320 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 321 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 322 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 323 | #if defined(PYOS_OS2) |
| 324 | { |
| 325 | APIRET rc; |
| 326 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 327 | |
| 328 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 329 | 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] | 330 | PyObject *v = PyString_FromString(buffer); |
| 331 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 332 | Py_DECREF(v); |
| 333 | } |
| 334 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 335 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 336 | PyObject *v = PyString_FromString(buffer); |
| 337 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 338 | Py_DECREF(v); |
| 339 | } |
| 340 | } |
| 341 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 342 | return d; |
| 343 | } |
| 344 | |
| 345 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 346 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 347 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 348 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 349 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 350 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 351 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 352 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 353 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 354 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 355 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 356 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 359 | static PyObject * |
| 360 | posix_error_with_allocated_filename(char* name) |
| 361 | { |
| 362 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 363 | PyMem_Free(name); |
| 364 | return rc; |
| 365 | } |
| 366 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 367 | #ifdef MS_WIN32 |
| 368 | static PyObject * |
| 369 | win32_error(char* function, char* filename) |
| 370 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 371 | /* XXX We should pass the function name along in the future. |
| 372 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 373 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 374 | Windows error object, which is non-trivial. |
| 375 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 376 | errno = GetLastError(); |
| 377 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 378 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 379 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 380 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 381 | } |
| 382 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 383 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 384 | #if defined(PYOS_OS2) |
| 385 | /********************************************************************** |
| 386 | * Helper Function to Trim and Format OS/2 Messages |
| 387 | **********************************************************************/ |
| 388 | static void |
| 389 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 390 | { |
| 391 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 392 | |
| 393 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 394 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 395 | |
| 396 | while (lastc > msgbuf && isspace(*lastc)) |
| 397 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 398 | } |
| 399 | |
| 400 | /* Add Optional Reason Text */ |
| 401 | if (reason) { |
| 402 | strcat(msgbuf, " : "); |
| 403 | strcat(msgbuf, reason); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /********************************************************************** |
| 408 | * Decode an OS/2 Operating System Error Code |
| 409 | * |
| 410 | * A convenience function to lookup an OS/2 error code and return a |
| 411 | * text message we can use to raise a Python exception. |
| 412 | * |
| 413 | * Notes: |
| 414 | * The messages for errors returned from the OS/2 kernel reside in |
| 415 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 416 | * |
| 417 | **********************************************************************/ |
| 418 | static char * |
| 419 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 420 | { |
| 421 | APIRET rc; |
| 422 | ULONG msglen; |
| 423 | |
| 424 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 425 | Py_BEGIN_ALLOW_THREADS |
| 426 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 427 | errorcode, "oso001.msg", &msglen); |
| 428 | Py_END_ALLOW_THREADS |
| 429 | |
| 430 | if (rc == NO_ERROR) |
| 431 | os2_formatmsg(msgbuf, msglen, reason); |
| 432 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 433 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 434 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 435 | |
| 436 | return msgbuf; |
| 437 | } |
| 438 | |
| 439 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 440 | errors are not in a global variable e.g. 'errno' nor are |
| 441 | they congruent with posix error numbers. */ |
| 442 | |
| 443 | static PyObject * os2_error(int code) |
| 444 | { |
| 445 | char text[1024]; |
| 446 | PyObject *v; |
| 447 | |
| 448 | os2_strerror(text, sizeof(text), code, ""); |
| 449 | |
| 450 | v = Py_BuildValue("(is)", code, text); |
| 451 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 452 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 453 | Py_DECREF(v); |
| 454 | } |
| 455 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 456 | } |
| 457 | |
| 458 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 459 | |
| 460 | /* POSIX generic methods */ |
| 461 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 462 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 463 | posix_int(PyObject *args, char *format, int (*func)(int)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 464 | { |
| 465 | int fd; |
| 466 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 467 | if (!PyArg_ParseTuple(args, format, &fd)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 468 | return NULL; |
| 469 | Py_BEGIN_ALLOW_THREADS |
| 470 | res = (*func)(fd); |
| 471 | Py_END_ALLOW_THREADS |
| 472 | if (res < 0) |
| 473 | return posix_error(); |
| 474 | Py_INCREF(Py_None); |
| 475 | return Py_None; |
| 476 | } |
| 477 | |
| 478 | |
| 479 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 480 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 481 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 482 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 483 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 484 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 485 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 486 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 487 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 488 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 489 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 490 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 491 | return posix_error_with_allocated_filename(path1); |
| 492 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 493 | Py_INCREF(Py_None); |
| 494 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 497 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 498 | posix_2str(PyObject *args, char *format, |
| 499 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 500 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 501 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 502 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 503 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 504 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 505 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 506 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 507 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 508 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 509 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 510 | PyMem_Free(path1); |
| 511 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 512 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 513 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 514 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 515 | Py_INCREF(Py_None); |
| 516 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 519 | static char stat_result__doc__[] = |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 520 | "stat_result: Result from stat or lstat.\n\n\ |
| 521 | This object may be accessed either as a tuple of\n\ |
| 522 | (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 523 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 524 | \n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 525 | 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] | 526 | they are available as attributes only.\n\ |
| 527 | \n\ |
| 528 | See os.stat for more information.\n"; |
| 529 | |
| 530 | static PyStructSequence_Field stat_result_fields[] = { |
| 531 | {"st_mode", "protection bits"}, |
| 532 | {"st_ino", "inode"}, |
| 533 | {"st_dev", "device"}, |
| 534 | {"st_nlink", "number of hard links"}, |
| 535 | {"st_uid", "user ID of owner"}, |
| 536 | {"st_gid", "group ID of owner"}, |
| 537 | {"st_size", "total size, in bytes"}, |
| 538 | {"st_atime", "time of last access"}, |
| 539 | {"st_mtime", "time of last modification"}, |
| 540 | {"st_ctime", "time of last change"}, |
| 541 | #ifdef HAVE_ST_BLKSIZE |
| 542 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 543 | #endif |
| 544 | #ifdef HAVE_ST_BLOCKS |
| 545 | {"st_blocks", "number of blocks allocated"}, |
| 546 | #endif |
| 547 | #ifdef HAVE_ST_RDEV |
| 548 | {"st_rdev", "device type (if inode device)"}, |
| 549 | #endif |
| 550 | {0} |
| 551 | }; |
| 552 | |
| 553 | #ifdef HAVE_ST_BLKSIZE |
| 554 | #define ST_BLKSIZE_IDX 10 |
| 555 | #else |
| 556 | #define ST_BLKSIZE_IDX 9 |
| 557 | #endif |
| 558 | |
| 559 | #ifdef HAVE_ST_BLOCKS |
| 560 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 561 | #else |
| 562 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 563 | #endif |
| 564 | |
| 565 | #ifdef HAVE_ST_RDEV |
| 566 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 567 | #else |
| 568 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 569 | #endif |
| 570 | |
| 571 | static PyStructSequence_Desc stat_result_desc = { |
| 572 | "stat_result", /* name */ |
| 573 | stat_result__doc__, /* doc */ |
| 574 | stat_result_fields, |
| 575 | 10 |
| 576 | }; |
| 577 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 578 | static char statvfs_result__doc__[] = |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 579 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 580 | This object may be accessed either as a tuple of\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 581 | (bsize,frsize,blocks,bfree,bavail,files,ffree,favail,flag,namemax),\n\ |
| 582 | 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] | 583 | \n\ |
| 584 | See os.statvfs for more information.\n"; |
| 585 | |
| 586 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 587 | {"f_bsize", }, |
| 588 | {"f_frsize", }, |
| 589 | {"f_blocks", }, |
| 590 | {"f_bfree", }, |
| 591 | {"f_bavail", }, |
| 592 | {"f_files", }, |
| 593 | {"f_ffree", }, |
| 594 | {"f_favail", }, |
| 595 | {"f_flag", }, |
| 596 | {"f_namemax",}, |
| 597 | {0} |
| 598 | }; |
| 599 | |
| 600 | static PyStructSequence_Desc statvfs_result_desc = { |
| 601 | "statvfs_result", /* name */ |
| 602 | statvfs_result__doc__, /* doc */ |
| 603 | statvfs_result_fields, |
| 604 | 10 |
| 605 | }; |
| 606 | |
| 607 | static PyTypeObject StatResultType; |
| 608 | static PyTypeObject StatVFSResultType; |
| 609 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 610 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 611 | (used by posix_stat() and posix_fstat()) */ |
| 612 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 613 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 614 | { |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 615 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 616 | if (v == NULL) |
| 617 | return NULL; |
| 618 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 619 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 620 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 621 | PyStructSequence_SET_ITEM(v, 1, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 622 | PyLong_FromLongLong((LONG_LONG)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 623 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 624 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 625 | #endif |
| 626 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 627 | PyStructSequence_SET_ITEM(v, 2, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 628 | PyLong_FromLongLong((LONG_LONG)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 629 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 630 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 631 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 632 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 633 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 634 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 635 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 636 | PyStructSequence_SET_ITEM(v, 6, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 637 | PyLong_FromLongLong((LONG_LONG)st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 638 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 639 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 640 | #endif |
| 641 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 642 | PyStructSequence_SET_ITEM(v, 7, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 643 | PyLong_FromLongLong((LONG_LONG)st.st_atime)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 644 | PyStructSequence_SET_ITEM(v, 8, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 645 | PyLong_FromLongLong((LONG_LONG)st.st_mtime)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 646 | PyStructSequence_SET_ITEM(v, 9, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 647 | PyLong_FromLongLong((LONG_LONG)st.st_ctime)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 648 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 649 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long)st.st_atime)); |
| 650 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long)st.st_mtime)); |
| 651 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long)st.st_ctime)); |
| 652 | #endif |
| 653 | |
| 654 | #ifdef HAVE_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 655 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 656 | PyInt_FromLong((long)st.st_blksize)); |
| 657 | #endif |
| 658 | #ifdef HAVE_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 659 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 660 | PyInt_FromLong((long)st.st_blocks)); |
| 661 | #endif |
| 662 | #ifdef HAVE_ST_RDEV |
| 663 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 664 | PyInt_FromLong((long)st.st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 665 | #endif |
| 666 | |
| 667 | if (PyErr_Occurred()) { |
| 668 | Py_DECREF(v); |
| 669 | return NULL; |
| 670 | } |
| 671 | |
| 672 | return v; |
| 673 | } |
| 674 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 675 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 676 | posix_do_stat(PyObject *self, PyObject *args, char *format, |
| 677 | int (*statfunc)(const char *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 678 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 679 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 680 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 681 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 682 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 683 | |
| 684 | #ifdef MS_WIN32 |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 685 | int pathlen; |
| 686 | char pathcopy[MAX_PATH]; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 687 | #endif /* MS_WIN32 */ |
| 688 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 689 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 690 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 691 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 692 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 693 | |
| 694 | #ifdef MS_WIN32 |
| 695 | pathlen = strlen(path); |
| 696 | /* the library call can blow up if the file name is too long! */ |
| 697 | if (pathlen > MAX_PATH) { |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 698 | PyMem_Free(pathfree); |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 699 | errno = ENAMETOOLONG; |
| 700 | return posix_error(); |
| 701 | } |
| 702 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 703 | /* Remove trailing slash or backslash, unless it's the current |
| 704 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 705 | */ |
| 706 | if (pathlen > 0 && |
| 707 | (path[pathlen-1]== '\\' || path[pathlen-1] == '/')) { |
| 708 | /* It does end with a slash -- exempt the root drive cases. */ |
| 709 | /* XXX UNC root drives should also be exempted? */ |
| 710 | if (pathlen == 1 || (pathlen == 3 && path[1] == ':')) |
| 711 | /* leave it alone */; |
| 712 | else { |
| 713 | /* nuke the trailing backslash */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 714 | strncpy(pathcopy, path, pathlen); |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 715 | pathcopy[pathlen-1] = '\0'; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 716 | path = pathcopy; |
| 717 | } |
| 718 | } |
| 719 | #endif /* MS_WIN32 */ |
| 720 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 721 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 722 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 723 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 724 | if (res != 0) |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 725 | return posix_error_with_allocated_filename(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 726 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 727 | PyMem_Free(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 728 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | |
| 732 | /* POSIX methods */ |
| 733 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 734 | static char posix_access__doc__[] = |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 735 | "access(path, mode) -> 1 if granted, 0 otherwise\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 736 | Test for access to a file."; |
| 737 | |
| 738 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 739 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 740 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 741 | char *path; |
| 742 | int mode; |
| 743 | int res; |
| 744 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 745 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 746 | return NULL; |
| 747 | Py_BEGIN_ALLOW_THREADS |
| 748 | res = access(path, mode); |
| 749 | Py_END_ALLOW_THREADS |
| 750 | return(PyInt_FromLong(res == 0 ? 1L : 0L)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 753 | #ifndef F_OK |
| 754 | #define F_OK 0 |
| 755 | #endif |
| 756 | #ifndef R_OK |
| 757 | #define R_OK 4 |
| 758 | #endif |
| 759 | #ifndef W_OK |
| 760 | #define W_OK 2 |
| 761 | #endif |
| 762 | #ifndef X_OK |
| 763 | #define X_OK 1 |
| 764 | #endif |
| 765 | |
| 766 | #ifdef HAVE_TTYNAME |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 767 | static char posix_ttyname__doc__[] = |
Guido van Rossum | 61eeb04 | 1999-02-22 15:29:15 +0000 | [diff] [blame] | 768 | "ttyname(fd) -> String\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 769 | Return the name of the terminal device connected to 'fd'."; |
| 770 | |
| 771 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 772 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 773 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 774 | int id; |
| 775 | char *ret; |
| 776 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 777 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 778 | return NULL; |
| 779 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 780 | ret = ttyname(id); |
| 781 | if (ret == NULL) |
| 782 | return(posix_error()); |
| 783 | return(PyString_FromString(ret)); |
| 784 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 785 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 786 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 787 | #ifdef HAVE_CTERMID |
| 788 | static char posix_ctermid__doc__[] = |
| 789 | "ctermid() -> String\n\ |
| 790 | Return the name of the controlling terminal for this process."; |
| 791 | |
| 792 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 793 | posix_ctermid(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 794 | { |
| 795 | char *ret; |
| 796 | char buffer[L_ctermid]; |
| 797 | |
| 798 | if (!PyArg_ParseTuple(args, ":ctermid")) |
| 799 | return NULL; |
| 800 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 801 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 802 | ret = ctermid_r(buffer); |
| 803 | #else |
| 804 | ret = ctermid(buffer); |
| 805 | #endif |
| 806 | if (ret == NULL) |
| 807 | return(posix_error()); |
| 808 | return(PyString_FromString(buffer)); |
| 809 | } |
| 810 | #endif |
| 811 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 812 | static char posix_chdir__doc__[] = |
| 813 | "chdir(path) -> None\n\ |
| 814 | Change the current working directory to the specified path."; |
| 815 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 816 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 817 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 818 | { |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 819 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 820 | return posix_1str(args, "et:chdir", _chdir2); |
| 821 | #else |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 822 | return posix_1str(args, "et:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 823 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 826 | |
| 827 | static char posix_chmod__doc__[] = |
| 828 | "chmod(path, mode) -> None\n\ |
| 829 | Change the access permissions of a file."; |
| 830 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 831 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 832 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 833 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 834 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 835 | int i; |
| 836 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 837 | if (!PyArg_ParseTuple(args, "eti", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 838 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 839 | return NULL; |
| 840 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 841 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 842 | Py_END_ALLOW_THREADS |
| 843 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 844 | return posix_error_with_allocated_filename(path); |
| 845 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 846 | Py_INCREF(Py_None); |
| 847 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 850 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 851 | #ifdef HAVE_CHROOT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 852 | static char posix_chroot__doc__[] = |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 853 | "chroot(path) -> None\n\ |
| 854 | Change root directory to path."; |
| 855 | |
| 856 | static PyObject * |
| 857 | posix_chroot(PyObject *self, PyObject *args) |
| 858 | { |
| 859 | return posix_1str(args, "et:chroot", chroot); |
| 860 | } |
| 861 | #endif |
| 862 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 863 | #ifdef HAVE_FSYNC |
| 864 | static char posix_fsync__doc__[] = |
| 865 | "fsync(fildes) -> None\n\ |
| 866 | force write of file with filedescriptor to disk."; |
| 867 | |
| 868 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 869 | posix_fsync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 870 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 871 | return posix_int(args, "i:fsync", fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 872 | } |
| 873 | #endif /* HAVE_FSYNC */ |
| 874 | |
| 875 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 876 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 877 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 878 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 879 | #endif |
| 880 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 881 | static char posix_fdatasync__doc__[] = |
| 882 | "fdatasync(fildes) -> None\n\ |
| 883 | force write of file with filedescriptor to disk.\n\ |
| 884 | does not force update of metadata."; |
| 885 | |
| 886 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 887 | posix_fdatasync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 888 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 889 | return posix_int(args, "i:fdatasync", fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 890 | } |
| 891 | #endif /* HAVE_FDATASYNC */ |
| 892 | |
| 893 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 894 | #ifdef HAVE_CHOWN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 895 | static char posix_chown__doc__[] = |
| 896 | "chown(path, uid, gid) -> None\n\ |
| 897 | Change the owner and group id of path to the numeric uid and gid."; |
| 898 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 899 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 900 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 901 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 902 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 903 | int uid, gid; |
| 904 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 905 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 906 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 907 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 908 | return NULL; |
| 909 | Py_BEGIN_ALLOW_THREADS |
| 910 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 911 | Py_END_ALLOW_THREADS |
| 912 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 913 | return posix_error_with_allocated_filename(path); |
| 914 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 915 | Py_INCREF(Py_None); |
| 916 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 917 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 918 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 919 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 920 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 921 | #ifdef HAVE_GETCWD |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 922 | static char posix_getcwd__doc__[] = |
| 923 | "getcwd() -> path\n\ |
| 924 | Return a string representing the current working directory."; |
| 925 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 926 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 927 | posix_getcwd(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 928 | { |
| 929 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 930 | char *res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 931 | if (!PyArg_ParseTuple(args, ":getcwd")) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 932 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 933 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 934 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 935 | res = _getcwd2(buf, sizeof buf); |
| 936 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 937 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 938 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 939 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 940 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 941 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 942 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 943 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 944 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 945 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 946 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 947 | #ifdef HAVE_LINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 948 | static char posix_link__doc__[] = |
| 949 | "link(src, dst) -> None\n\ |
| 950 | Create a hard link to a file."; |
| 951 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 952 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 953 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 954 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 955 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 956 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 957 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 958 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 959 | |
| 960 | static char posix_listdir__doc__[] = |
| 961 | "listdir(path) -> list_of_strings\n\ |
| 962 | Return a list containing the names of the entries in the directory.\n\ |
| 963 | \n\ |
| 964 | path: path of directory to list\n\ |
| 965 | \n\ |
| 966 | The list is in arbitrary order. It does not include the special\n\ |
| 967 | entries '.' and '..' even if they are present in the directory."; |
| 968 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 969 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 970 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 971 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 972 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 973 | in separate files instead of having them all here... */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 974 | #if defined(MS_WIN32) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 975 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 976 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 977 | HANDLE hFindFile; |
| 978 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 979 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 980 | char namebuf[MAX_PATH*2+5]; |
| 981 | char *bufptr = namebuf; |
| 982 | int len = sizeof(namebuf)/sizeof(namebuf[0]); |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 983 | char ch; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 984 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 985 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 986 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 987 | return NULL; |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 988 | ch = namebuf[len-1]; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 989 | if (ch != SEP && ch != ALTSEP && ch != ':') |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 990 | namebuf[len++] = '/'; |
| 991 | strcpy(namebuf + len, "*.*"); |
| 992 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 993 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 994 | return NULL; |
| 995 | |
| 996 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 997 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 998 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 999 | if (errno == ERROR_FILE_NOT_FOUND) |
| 1000 | return PyList_New(0); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1001 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1002 | } |
| 1003 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1004 | if (FileData.cFileName[0] == '.' && |
| 1005 | (FileData.cFileName[1] == '\0' || |
| 1006 | FileData.cFileName[1] == '.' && |
| 1007 | FileData.cFileName[2] == '\0')) |
| 1008 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1009 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1010 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1011 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1012 | d = NULL; |
| 1013 | break; |
| 1014 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1015 | if (PyList_Append(d, v) != 0) { |
| 1016 | Py_DECREF(v); |
| 1017 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1018 | d = NULL; |
| 1019 | break; |
| 1020 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1021 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1022 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 1023 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1024 | if (FindClose(hFindFile) == FALSE) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1025 | return win32_error("FindClose", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1026 | |
| 1027 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1028 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1029 | #elif defined(_MSC_VER) /* 16-bit Windows */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1030 | |
| 1031 | #ifndef MAX_PATH |
| 1032 | #define MAX_PATH 250 |
| 1033 | #endif |
| 1034 | char *name, *pt; |
| 1035 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1036 | PyObject *d, *v; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1037 | char namebuf[MAX_PATH+5]; |
| 1038 | struct _find_t ep; |
| 1039 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1040 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1041 | return NULL; |
| 1042 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1043 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1044 | return NULL; |
| 1045 | } |
| 1046 | strcpy(namebuf, name); |
| 1047 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1048 | if (*pt == ALTSEP) |
| 1049 | *pt = SEP; |
| 1050 | if (namebuf[len-1] != SEP) |
| 1051 | namebuf[len++] = SEP; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1052 | strcpy(namebuf + len, "*.*"); |
| 1053 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1054 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1055 | return NULL; |
| 1056 | |
| 1057 | if (_dos_findfirst(namebuf, _A_RDONLY | |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1058 | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0) |
| 1059 | { |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1060 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1061 | return posix_error_with_filename(name); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1062 | } |
| 1063 | do { |
| 1064 | if (ep.name[0] == '.' && |
| 1065 | (ep.name[1] == '\0' || |
| 1066 | ep.name[1] == '.' && |
| 1067 | ep.name[2] == '\0')) |
| 1068 | continue; |
| 1069 | strcpy(namebuf, ep.name); |
| 1070 | for (pt = namebuf; *pt; pt++) |
| 1071 | if (isupper(*pt)) |
| 1072 | *pt = tolower(*pt); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1073 | v = PyString_FromString(namebuf); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1074 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1075 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1076 | d = NULL; |
| 1077 | break; |
| 1078 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1079 | if (PyList_Append(d, v) != 0) { |
| 1080 | Py_DECREF(v); |
| 1081 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1082 | d = NULL; |
| 1083 | break; |
| 1084 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1085 | Py_DECREF(v); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1086 | } while (_dos_findnext(&ep) == 0); |
| 1087 | |
| 1088 | return d; |
| 1089 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1090 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1091 | |
| 1092 | #ifndef MAX_PATH |
| 1093 | #define MAX_PATH CCHMAXPATH |
| 1094 | #endif |
| 1095 | char *name, *pt; |
| 1096 | int len; |
| 1097 | PyObject *d, *v; |
| 1098 | char namebuf[MAX_PATH+5]; |
| 1099 | HDIR hdir = 1; |
| 1100 | ULONG srchcnt = 1; |
| 1101 | FILEFINDBUF3 ep; |
| 1102 | APIRET rc; |
| 1103 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1104 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1105 | return NULL; |
| 1106 | if (len >= MAX_PATH) { |
| 1107 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1108 | return NULL; |
| 1109 | } |
| 1110 | strcpy(namebuf, name); |
| 1111 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1112 | if (*pt == ALTSEP) |
| 1113 | *pt = SEP; |
| 1114 | if (namebuf[len-1] != SEP) |
| 1115 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1116 | strcpy(namebuf + len, "*.*"); |
| 1117 | |
| 1118 | if ((d = PyList_New(0)) == NULL) |
| 1119 | return NULL; |
| 1120 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1121 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1122 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1123 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1124 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1125 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1126 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1127 | |
| 1128 | if (rc != NO_ERROR) { |
| 1129 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1130 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1133 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1134 | do { |
| 1135 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1136 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1137 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1138 | |
| 1139 | strcpy(namebuf, ep.achName); |
| 1140 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1141 | /* Leave Case of Name Alone -- In Native Form */ |
| 1142 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1143 | |
| 1144 | v = PyString_FromString(namebuf); |
| 1145 | if (v == NULL) { |
| 1146 | Py_DECREF(d); |
| 1147 | d = NULL; |
| 1148 | break; |
| 1149 | } |
| 1150 | if (PyList_Append(d, v) != 0) { |
| 1151 | Py_DECREF(v); |
| 1152 | Py_DECREF(d); |
| 1153 | d = NULL; |
| 1154 | break; |
| 1155 | } |
| 1156 | Py_DECREF(v); |
| 1157 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1158 | } |
| 1159 | |
| 1160 | return d; |
| 1161 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1162 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1163 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1164 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1165 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1166 | struct dirent *ep; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1167 | if (!PyArg_ParseTuple(args, "s:listdir", &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1168 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1169 | if ((dirp = opendir(name)) == NULL) { |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1170 | return posix_error_with_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1171 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1172 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1173 | closedir(dirp); |
| 1174 | return NULL; |
| 1175 | } |
| 1176 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1177 | if (ep->d_name[0] == '.' && |
| 1178 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1179 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1180 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1181 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1182 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1183 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1184 | d = NULL; |
| 1185 | break; |
| 1186 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1187 | if (PyList_Append(d, v) != 0) { |
| 1188 | Py_DECREF(v); |
| 1189 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1190 | d = NULL; |
| 1191 | break; |
| 1192 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1193 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1194 | } |
| 1195 | closedir(dirp); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1196 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1197 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1198 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1199 | #endif /* which OS */ |
| 1200 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1201 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1202 | #ifdef MS_WIN32 |
| 1203 | /* A helper function for abspath on win32 */ |
| 1204 | static PyObject * |
| 1205 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1206 | { |
| 1207 | /* assume encoded strings wont more than double no of chars */ |
| 1208 | char inbuf[MAX_PATH*2]; |
| 1209 | char *inbufp = inbuf; |
| 1210 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); |
| 1211 | char outbuf[MAX_PATH*2]; |
| 1212 | char *temp; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1213 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1214 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1215 | &insize)) |
| 1216 | return NULL; |
| 1217 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1218 | outbuf, &temp)) |
| 1219 | return win32_error("GetFullPathName", inbuf); |
| 1220 | return PyString_FromString(outbuf); |
| 1221 | } /* end of posix__getfullpathname */ |
| 1222 | #endif /* MS_WIN32 */ |
| 1223 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1224 | static char posix_mkdir__doc__[] = |
| 1225 | "mkdir(path [, mode=0777]) -> None\n\ |
| 1226 | Create a directory."; |
| 1227 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1228 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1229 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1230 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1231 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1232 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1233 | int mode = 0777; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1234 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1235 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1236 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1237 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1238 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1239 | res = mkdir(path); |
| 1240 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1241 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1242 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1243 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1244 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1245 | return posix_error_with_allocated_filename(path); |
| 1246 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1247 | Py_INCREF(Py_None); |
| 1248 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1251 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1252 | #ifdef HAVE_NICE |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1253 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) |
| 1254 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) |
| 1255 | #include <sys/resource.h> |
| 1256 | #endif |
| 1257 | #endif |
| 1258 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1259 | static char posix_nice__doc__[] = |
| 1260 | "nice(inc) -> new_priority\n\ |
| 1261 | Decrease the priority of process and return new priority."; |
| 1262 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1263 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1264 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1265 | { |
| 1266 | int increment, value; |
| 1267 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1268 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1269 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1270 | |
| 1271 | /* There are two flavours of 'nice': one that returns the new |
| 1272 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1273 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 1274 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1275 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1276 | If we are of the nice family that returns the new priority, we |
| 1277 | need to clear errno before the call, and check if errno is filled |
| 1278 | before calling posix_error() on a returnvalue of -1, because the |
| 1279 | -1 may be the actual new priority! */ |
| 1280 | |
| 1281 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1282 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1283 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1284 | if (value == 0) |
| 1285 | value = getpriority(PRIO_PROCESS, 0); |
| 1286 | #endif |
| 1287 | if (value == -1 && errno != 0) |
| 1288 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1289 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1290 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1291 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1292 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1293 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1294 | |
| 1295 | static char posix_rename__doc__[] = |
| 1296 | "rename(old, new) -> None\n\ |
| 1297 | Rename a file or directory."; |
| 1298 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1299 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1300 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1301 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1302 | return posix_2str(args, "etet:rename", rename); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1305 | |
| 1306 | static char posix_rmdir__doc__[] = |
| 1307 | "rmdir(path) -> None\n\ |
| 1308 | Remove a directory."; |
| 1309 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1310 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1311 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1312 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1313 | return posix_1str(args, "et:rmdir", rmdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1316 | |
| 1317 | static char posix_stat__doc__[] = |
Fred Drake | 193a3f6 | 2002-03-12 21:38:49 +0000 | [diff] [blame] | 1318 | "stat(path) -> (st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid,\n\ |
| 1319 | st_size, st_atime, st_mtime, st_ctime)\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1320 | Perform a stat system call on the given path."; |
| 1321 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1322 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1323 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1324 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1325 | return posix_do_stat(self, args, "et:stat", STAT); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1328 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1329 | #ifdef HAVE_SYSTEM |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1330 | static char posix_system__doc__[] = |
| 1331 | "system(command) -> exit_status\n\ |
| 1332 | Execute the command (a string) in a subshell."; |
| 1333 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1334 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1335 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1336 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1337 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1338 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1339 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1340 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1341 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1342 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1343 | Py_END_ALLOW_THREADS |
| 1344 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1345 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1346 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1347 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1348 | |
| 1349 | static char posix_umask__doc__[] = |
| 1350 | "umask(new_mask) -> old_mask\n\ |
| 1351 | Set the current numeric umask and return the previous umask."; |
| 1352 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1353 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1354 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1355 | { |
| 1356 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1357 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1358 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 1359 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1360 | if (i < 0) |
| 1361 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1362 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1365 | |
| 1366 | static char posix_unlink__doc__[] = |
| 1367 | "unlink(path) -> None\n\ |
| 1368 | Remove a file (same as remove(path))."; |
| 1369 | |
| 1370 | static char posix_remove__doc__[] = |
| 1371 | "remove(path) -> None\n\ |
| 1372 | Remove a file (same as unlink(path))."; |
| 1373 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1374 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1375 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1376 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1377 | return posix_1str(args, "et:remove", unlink); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1380 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1381 | #ifdef HAVE_UNAME |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1382 | static char posix_uname__doc__[] = |
| 1383 | "uname() -> (sysname, nodename, release, version, machine)\n\ |
| 1384 | Return a tuple identifying the current operating system."; |
| 1385 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1386 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1387 | posix_uname(PyObject *self, PyObject *args) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1388 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1389 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1390 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1391 | if (!PyArg_ParseTuple(args, ":uname")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1392 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1393 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1394 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1395 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1396 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1397 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1398 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1399 | u.sysname, |
| 1400 | u.nodename, |
| 1401 | u.release, |
| 1402 | u.version, |
| 1403 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1404 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1405 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1406 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1407 | |
| 1408 | static char posix_utime__doc__[] = |
| 1409 | "utime(path, (atime, utime)) -> None\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1410 | utime(path, None) -> None\n\ |
| 1411 | Set the access and modified time of the file to the given values. If the\n\ |
| 1412 | 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] | 1413 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1414 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1415 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1416 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1417 | char *path; |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 1418 | long atime, mtime; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1419 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1420 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1421 | |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1422 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1423 | #ifdef HAVE_UTIME_H |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1424 | struct utimbuf buf; |
| 1425 | #define ATIME buf.actime |
| 1426 | #define MTIME buf.modtime |
| 1427 | #define UTIME_ARG &buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1428 | #else /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1429 | time_t buf[2]; |
| 1430 | #define ATIME buf[0] |
| 1431 | #define MTIME buf[1] |
| 1432 | #define UTIME_ARG buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1433 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1434 | |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1435 | if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1436 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1437 | if (arg == Py_None) { |
| 1438 | /* optional time values not given */ |
| 1439 | Py_BEGIN_ALLOW_THREADS |
| 1440 | res = utime(path, NULL); |
| 1441 | Py_END_ALLOW_THREADS |
| 1442 | } |
| 1443 | else if (!PyArg_Parse(arg, "(ll)", &atime, &mtime)) { |
| 1444 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1445 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1446 | return NULL; |
| 1447 | } |
| 1448 | else { |
| 1449 | ATIME = atime; |
| 1450 | MTIME = mtime; |
| 1451 | Py_BEGIN_ALLOW_THREADS |
| 1452 | res = utime(path, UTIME_ARG); |
| 1453 | Py_END_ALLOW_THREADS |
| 1454 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1455 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1456 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1457 | Py_INCREF(Py_None); |
| 1458 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1459 | #undef UTIME_ARG |
| 1460 | #undef ATIME |
| 1461 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1464 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1465 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1466 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1467 | static char posix__exit__doc__[] = |
| 1468 | "_exit(status)\n\ |
| 1469 | Exit to the system with specified status, without normal exit processing."; |
| 1470 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1471 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1472 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1473 | { |
| 1474 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1475 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1476 | return NULL; |
| 1477 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1478 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1481 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1482 | #ifdef HAVE_EXECV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1483 | static char posix_execv__doc__[] = |
| 1484 | "execv(path, args)\n\ |
| 1485 | Execute an executable path with arguments, replacing current process.\n\ |
| 1486 | \n\ |
| 1487 | path: path of executable file\n\ |
| 1488 | args: tuple or list of strings"; |
| 1489 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1490 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1491 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1492 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1493 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1494 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1495 | char **argvlist; |
| 1496 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1497 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1498 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1499 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1500 | argv is a list or tuple of strings. */ |
| 1501 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1502 | if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1503 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1504 | if (PyList_Check(argv)) { |
| 1505 | argc = PyList_Size(argv); |
| 1506 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1507 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1508 | else if (PyTuple_Check(argv)) { |
| 1509 | argc = PyTuple_Size(argv); |
| 1510 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1511 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1512 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1513 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1514 | return NULL; |
| 1515 | } |
| 1516 | |
| 1517 | if (argc == 0) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1518 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1519 | return NULL; |
| 1520 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1521 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1522 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1523 | if (argvlist == NULL) |
| 1524 | return NULL; |
| 1525 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1526 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1527 | PyMem_DEL(argvlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1528 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1529 | "execv() arg 2 must contain only strings"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1530 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1531 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1532 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1533 | } |
| 1534 | argvlist[argc] = NULL; |
| 1535 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1536 | #ifdef BAD_EXEC_PROTOTYPES |
| 1537 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1538 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1539 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1540 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1541 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1542 | /* If we get here it's definitely an error */ |
| 1543 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1544 | PyMem_DEL(argvlist); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1545 | return posix_error(); |
| 1546 | } |
| 1547 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1548 | |
| 1549 | static char posix_execve__doc__[] = |
| 1550 | "execve(path, args, env)\n\ |
| 1551 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1552 | \n\ |
| 1553 | path: path of executable file\n\ |
| 1554 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1555 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1556 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1557 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1558 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1559 | { |
| 1560 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1561 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1562 | char **argvlist; |
| 1563 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1564 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1565 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1566 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1567 | |
| 1568 | /* execve has three arguments: (path, argv, env), where |
| 1569 | argv is a list or tuple of strings and env is a dictionary |
| 1570 | like posix.environ. */ |
| 1571 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1572 | if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1573 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1574 | if (PyList_Check(argv)) { |
| 1575 | argc = PyList_Size(argv); |
| 1576 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1577 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1578 | else if (PyTuple_Check(argv)) { |
| 1579 | argc = PyTuple_Size(argv); |
| 1580 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1581 | } |
| 1582 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1583 | PyErr_SetString(PyExc_TypeError, "execve() arg 2 must be a tuple or list"); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1584 | return NULL; |
| 1585 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1586 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1587 | PyErr_SetString(PyExc_TypeError, "execve() arg 3 must be a mapping object"); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1588 | return NULL; |
| 1589 | } |
| 1590 | |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1591 | if (argc == 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1592 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1593 | "execve() arg 2 must not be empty"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1594 | return NULL; |
| 1595 | } |
| 1596 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1597 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1598 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1599 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1600 | return NULL; |
| 1601 | } |
| 1602 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1603 | if (!PyArg_Parse((*getitem)(argv, i), |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1604 | "s;execve() arg 2 must contain only strings", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1605 | &argvlist[i])) |
| 1606 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1607 | goto fail_1; |
| 1608 | } |
| 1609 | } |
| 1610 | argvlist[argc] = NULL; |
| 1611 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1612 | i = PyMapping_Size(env); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1613 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1614 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1615 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1616 | goto fail_1; |
| 1617 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1618 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1619 | keys = PyMapping_Keys(env); |
| 1620 | vals = PyMapping_Values(env); |
| 1621 | if (!keys || !vals) |
| 1622 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1623 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1624 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1625 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1626 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1627 | |
| 1628 | key = PyList_GetItem(keys, pos); |
| 1629 | val = PyList_GetItem(vals, pos); |
| 1630 | if (!key || !val) |
| 1631 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1632 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1633 | if (!PyArg_Parse(key, "s;execve() arg 3 contains a non-string key", &k) || |
| 1634 | !PyArg_Parse(val, "s;execve() arg 3 contains a non-string value", &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1635 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1636 | goto fail_2; |
| 1637 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1638 | |
| 1639 | #if defined(PYOS_OS2) |
| 1640 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 1641 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 1642 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1643 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 1644 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1645 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1646 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1647 | goto fail_2; |
| 1648 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1649 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1650 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1651 | #if defined(PYOS_OS2) |
| 1652 | } |
| 1653 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1654 | } |
| 1655 | envlist[envc] = 0; |
| 1656 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1657 | |
| 1658 | #ifdef BAD_EXEC_PROTOTYPES |
| 1659 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1660 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1661 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1662 | #endif /* BAD_EXEC_PROTOTYPES */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1663 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1664 | /* If we get here it's definitely an error */ |
| 1665 | |
| 1666 | (void) posix_error(); |
| 1667 | |
| 1668 | fail_2: |
| 1669 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1670 | PyMem_DEL(envlist[envc]); |
| 1671 | PyMem_DEL(envlist); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1672 | fail_1: |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1673 | PyMem_DEL(argvlist); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1674 | Py_XDECREF(vals); |
| 1675 | Py_XDECREF(keys); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1676 | return NULL; |
| 1677 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1678 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1679 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1680 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1681 | #ifdef HAVE_SPAWNV |
| 1682 | static char posix_spawnv__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1683 | "spawnv(mode, path, args)\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1684 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1685 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1686 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1687 | path: path of executable file\n\ |
| 1688 | args: tuple or list of strings"; |
| 1689 | |
| 1690 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1691 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1692 | { |
| 1693 | char *path; |
| 1694 | PyObject *argv; |
| 1695 | char **argvlist; |
| 1696 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 1697 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1698 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1699 | |
| 1700 | /* spawnv has three arguments: (mode, path, argv), where |
| 1701 | argv is a list or tuple of strings. */ |
| 1702 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1703 | if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1704 | return NULL; |
| 1705 | if (PyList_Check(argv)) { |
| 1706 | argc = PyList_Size(argv); |
| 1707 | getitem = PyList_GetItem; |
| 1708 | } |
| 1709 | else if (PyTuple_Check(argv)) { |
| 1710 | argc = PyTuple_Size(argv); |
| 1711 | getitem = PyTuple_GetItem; |
| 1712 | } |
| 1713 | else { |
Martin v. Löwis | e75f0e4 | 2001-11-24 09:31:44 +0000 | [diff] [blame] | 1714 | PyErr_SetString(PyExc_TypeError, "spawnv() arg 2 must be a tuple or list"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1715 | return NULL; |
| 1716 | } |
| 1717 | |
| 1718 | argvlist = PyMem_NEW(char *, argc+1); |
| 1719 | if (argvlist == NULL) |
| 1720 | return NULL; |
| 1721 | for (i = 0; i < argc; i++) { |
| 1722 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1723 | PyMem_DEL(argvlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1724 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1725 | "spawnv() arg 2 must contain only strings"); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1726 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1727 | } |
| 1728 | } |
| 1729 | argvlist[argc] = NULL; |
| 1730 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1731 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1732 | Py_BEGIN_ALLOW_THREADS |
| 1733 | spawnval = spawnv(mode, path, argvlist); |
| 1734 | Py_END_ALLOW_THREADS |
| 1735 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1736 | if (mode == _OLD_P_OVERLAY) |
| 1737 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1738 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1739 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1740 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1741 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1742 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1743 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1744 | PyMem_DEL(argvlist); |
| 1745 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1746 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1747 | return posix_error(); |
| 1748 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1749 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1750 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1751 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1752 | return Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1753 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | |
| 1757 | static char posix_spawnve__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1758 | "spawnve(mode, path, args, env)\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1759 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1760 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1761 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1762 | path: path of executable file\n\ |
| 1763 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1764 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1765 | |
| 1766 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1767 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1768 | { |
| 1769 | char *path; |
| 1770 | PyObject *argv, *env; |
| 1771 | char **argvlist; |
| 1772 | char **envlist; |
| 1773 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 1774 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 1775 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1776 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1777 | |
| 1778 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 1779 | argv is a list or tuple of strings and env is a dictionary |
| 1780 | like posix.environ. */ |
| 1781 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1782 | if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1783 | return NULL; |
| 1784 | if (PyList_Check(argv)) { |
| 1785 | argc = PyList_Size(argv); |
| 1786 | getitem = PyList_GetItem; |
| 1787 | } |
| 1788 | else if (PyTuple_Check(argv)) { |
| 1789 | argc = PyTuple_Size(argv); |
| 1790 | getitem = PyTuple_GetItem; |
| 1791 | } |
| 1792 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1793 | PyErr_SetString(PyExc_TypeError, "spawnve() arg 2 must be a tuple or list"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1794 | return NULL; |
| 1795 | } |
| 1796 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1797 | PyErr_SetString(PyExc_TypeError, "spawnve() arg 3 must be a mapping object"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1798 | return NULL; |
| 1799 | } |
| 1800 | |
| 1801 | argvlist = PyMem_NEW(char *, argc+1); |
| 1802 | if (argvlist == NULL) { |
| 1803 | PyErr_NoMemory(); |
| 1804 | return NULL; |
| 1805 | } |
| 1806 | for (i = 0; i < argc; i++) { |
| 1807 | if (!PyArg_Parse((*getitem)(argv, i), |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1808 | "s;spawnve() arg 2 must contain only strings", |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1809 | &argvlist[i])) |
| 1810 | { |
| 1811 | goto fail_1; |
| 1812 | } |
| 1813 | } |
| 1814 | argvlist[argc] = NULL; |
| 1815 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1816 | i = PyMapping_Size(env); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1817 | envlist = PyMem_NEW(char *, i + 1); |
| 1818 | if (envlist == NULL) { |
| 1819 | PyErr_NoMemory(); |
| 1820 | goto fail_1; |
| 1821 | } |
| 1822 | envc = 0; |
| 1823 | keys = PyMapping_Keys(env); |
| 1824 | vals = PyMapping_Values(env); |
| 1825 | if (!keys || !vals) |
| 1826 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1827 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1828 | for (pos = 0; pos < i; pos++) { |
| 1829 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1830 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1831 | |
| 1832 | key = PyList_GetItem(keys, pos); |
| 1833 | val = PyList_GetItem(vals, pos); |
| 1834 | if (!key || !val) |
| 1835 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1836 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1837 | if (!PyArg_Parse(key, "s;spawnve() arg 3 contains a non-string key", &k) || |
| 1838 | !PyArg_Parse(val, "s;spawnve() arg 3 contains a non-string value", &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1839 | { |
| 1840 | goto fail_2; |
| 1841 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1842 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 1843 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1844 | if (p == NULL) { |
| 1845 | PyErr_NoMemory(); |
| 1846 | goto fail_2; |
| 1847 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1848 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1849 | envlist[envc++] = p; |
| 1850 | } |
| 1851 | envlist[envc] = 0; |
| 1852 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1853 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1854 | Py_BEGIN_ALLOW_THREADS |
| 1855 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 1856 | Py_END_ALLOW_THREADS |
| 1857 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1858 | if (mode == _OLD_P_OVERLAY) |
| 1859 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1860 | |
| 1861 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1862 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1863 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1864 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1865 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1866 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1867 | (void) posix_error(); |
| 1868 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1869 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1870 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1871 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1872 | res = Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1873 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1874 | |
| 1875 | fail_2: |
| 1876 | while (--envc >= 0) |
| 1877 | PyMem_DEL(envlist[envc]); |
| 1878 | PyMem_DEL(envlist); |
| 1879 | fail_1: |
| 1880 | PyMem_DEL(argvlist); |
| 1881 | Py_XDECREF(vals); |
| 1882 | Py_XDECREF(keys); |
| 1883 | return res; |
| 1884 | } |
| 1885 | #endif /* HAVE_SPAWNV */ |
| 1886 | |
| 1887 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 1888 | #ifdef HAVE_FORK1 |
| 1889 | static char posix_fork1__doc__[] = |
| 1890 | "fork1() -> pid\n\ |
| 1891 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 1892 | \n\ |
| 1893 | Return 0 to child process and PID of child to parent process."; |
| 1894 | |
| 1895 | static PyObject * |
| 1896 | posix_fork1(self, args) |
| 1897 | PyObject *self; |
| 1898 | PyObject *args; |
| 1899 | { |
| 1900 | int pid; |
| 1901 | if (!PyArg_ParseTuple(args, ":fork1")) |
| 1902 | return NULL; |
| 1903 | pid = fork1(); |
| 1904 | if (pid == -1) |
| 1905 | return posix_error(); |
| 1906 | PyOS_AfterFork(); |
| 1907 | return PyInt_FromLong((long)pid); |
| 1908 | } |
| 1909 | #endif |
| 1910 | |
| 1911 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1912 | #ifdef HAVE_FORK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1913 | static char posix_fork__doc__[] = |
| 1914 | "fork() -> pid\n\ |
| 1915 | Fork a child process.\n\ |
| 1916 | \n\ |
| 1917 | Return 0 to child process and PID of child to parent process."; |
| 1918 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1919 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1920 | posix_fork(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1921 | { |
| 1922 | int pid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1923 | if (!PyArg_ParseTuple(args, ":fork")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1924 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1925 | pid = fork(); |
| 1926 | if (pid == -1) |
| 1927 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1928 | if (pid == 0) |
| 1929 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1930 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1931 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1932 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1933 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1934 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) |
| 1935 | #ifdef HAVE_PTY_H |
| 1936 | #include <pty.h> |
| 1937 | #else |
| 1938 | #ifdef HAVE_LIBUTIL_H |
| 1939 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1940 | #endif /* HAVE_LIBUTIL_H */ |
| 1941 | #endif /* HAVE_PTY_H */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1942 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1943 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1944 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1945 | static char posix_openpty__doc__[] = |
| 1946 | "openpty() -> (master_fd, slave_fd)\n\ |
| 1947 | Open a pseudo-terminal, returning open fd's for both master and slave end.\n"; |
| 1948 | |
| 1949 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1950 | posix_openpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1951 | { |
| 1952 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1953 | #ifndef HAVE_OPENPTY |
| 1954 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1955 | #endif |
| 1956 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1957 | if (!PyArg_ParseTuple(args, ":openpty")) |
| 1958 | return NULL; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1959 | |
| 1960 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1961 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 1962 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1963 | #else |
| 1964 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 1965 | if (slave_name == NULL) |
| 1966 | return posix_error(); |
| 1967 | |
| 1968 | slave_fd = open(slave_name, O_RDWR); |
| 1969 | if (slave_fd < 0) |
| 1970 | return posix_error(); |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 1971 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1972 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1973 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1974 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1975 | } |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1976 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1977 | |
| 1978 | #ifdef HAVE_FORKPTY |
| 1979 | static char posix_forkpty__doc__[] = |
| 1980 | "forkpty() -> (pid, master_fd)\n\ |
| 1981 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 1982 | Like fork(), return 0 as pid to child process, and PID of child to parent.\n\ |
| 1983 | To both, return fd of newly opened pseudo-terminal.\n"; |
| 1984 | |
| 1985 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1986 | posix_forkpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1987 | { |
| 1988 | int master_fd, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1989 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1990 | if (!PyArg_ParseTuple(args, ":forkpty")) |
| 1991 | return NULL; |
| 1992 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 1993 | if (pid == -1) |
| 1994 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1995 | if (pid == 0) |
| 1996 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1997 | return Py_BuildValue("(ii)", pid, master_fd); |
| 1998 | } |
| 1999 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2000 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2001 | #ifdef HAVE_GETEGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2002 | static char posix_getegid__doc__[] = |
| 2003 | "getegid() -> egid\n\ |
| 2004 | Return the current process's effective group id."; |
| 2005 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2006 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2007 | posix_getegid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2008 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2009 | if (!PyArg_ParseTuple(args, ":getegid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2010 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2011 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2012 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2013 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2014 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2015 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2016 | #ifdef HAVE_GETEUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2017 | static char posix_geteuid__doc__[] = |
| 2018 | "geteuid() -> euid\n\ |
| 2019 | Return the current process's effective user id."; |
| 2020 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2021 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2022 | posix_geteuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2023 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2024 | if (!PyArg_ParseTuple(args, ":geteuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2025 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2026 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2027 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2028 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2029 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2030 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2031 | #ifdef HAVE_GETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2032 | static char posix_getgid__doc__[] = |
| 2033 | "getgid() -> gid\n\ |
| 2034 | Return the current process's group id."; |
| 2035 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2036 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2037 | posix_getgid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2038 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2039 | if (!PyArg_ParseTuple(args, ":getgid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2040 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2041 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2042 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2043 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2044 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2045 | |
| 2046 | static char posix_getpid__doc__[] = |
| 2047 | "getpid() -> pid\n\ |
| 2048 | Return the current process id"; |
| 2049 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2050 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2051 | posix_getpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2052 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2053 | if (!PyArg_ParseTuple(args, ":getpid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2054 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2055 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2058 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2059 | #ifdef HAVE_GETGROUPS |
| 2060 | static char posix_getgroups__doc__[] = "\ |
| 2061 | getgroups() -> list of group IDs\n\ |
| 2062 | Return list of supplemental group IDs for the process."; |
| 2063 | |
| 2064 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2065 | posix_getgroups(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2066 | { |
| 2067 | PyObject *result = NULL; |
| 2068 | |
| 2069 | if (PyArg_ParseTuple(args, ":getgroups")) { |
| 2070 | #ifdef NGROUPS_MAX |
| 2071 | #define MAX_GROUPS NGROUPS_MAX |
| 2072 | #else |
| 2073 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 2074 | #define MAX_GROUPS 64 |
| 2075 | #endif |
| 2076 | gid_t grouplist[MAX_GROUPS]; |
| 2077 | int n; |
| 2078 | |
| 2079 | n = getgroups(MAX_GROUPS, grouplist); |
| 2080 | if (n < 0) |
| 2081 | posix_error(); |
| 2082 | else { |
| 2083 | result = PyList_New(n); |
| 2084 | if (result != NULL) { |
| 2085 | PyObject *o; |
| 2086 | int i; |
| 2087 | for (i = 0; i < n; ++i) { |
| 2088 | o = PyInt_FromLong((long)grouplist[i]); |
| 2089 | if (o == NULL) { |
| 2090 | Py_DECREF(result); |
| 2091 | result = NULL; |
| 2092 | break; |
| 2093 | } |
| 2094 | PyList_SET_ITEM(result, i, o); |
| 2095 | } |
| 2096 | } |
| 2097 | } |
| 2098 | } |
| 2099 | return result; |
| 2100 | } |
| 2101 | #endif |
| 2102 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2103 | #ifdef HAVE_GETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2104 | static char posix_getpgrp__doc__[] = |
| 2105 | "getpgrp() -> pgrp\n\ |
| 2106 | Return the current process group id."; |
| 2107 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2108 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2109 | posix_getpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2110 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2111 | if (!PyArg_ParseTuple(args, ":getpgrp")) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2112 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2113 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2114 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2115 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2116 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2117 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2118 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2119 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2120 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2121 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2122 | #ifdef HAVE_SETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2123 | static char posix_setpgrp__doc__[] = |
| 2124 | "setpgrp() -> None\n\ |
| 2125 | Make this process a session leader."; |
| 2126 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2127 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2128 | posix_setpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2129 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2130 | if (!PyArg_ParseTuple(args, ":setpgrp")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2131 | return NULL; |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2132 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2133 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2134 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2135 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2136 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2137 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2138 | Py_INCREF(Py_None); |
| 2139 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2142 | #endif /* HAVE_SETPGRP */ |
| 2143 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2144 | #ifdef HAVE_GETPPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2145 | static char posix_getppid__doc__[] = |
| 2146 | "getppid() -> ppid\n\ |
| 2147 | Return the parent's process id."; |
| 2148 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2149 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2150 | posix_getppid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2151 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2152 | if (!PyArg_ParseTuple(args, ":getppid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2153 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2154 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2155 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2156 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2157 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2158 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2159 | #ifdef HAVE_GETLOGIN |
| 2160 | static char posix_getlogin__doc__[] = "\ |
| 2161 | getlogin() -> string\n\ |
| 2162 | Return the actual login name."; |
| 2163 | |
| 2164 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2165 | posix_getlogin(PyObject *self, PyObject *args) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2166 | { |
| 2167 | PyObject *result = NULL; |
| 2168 | |
| 2169 | if (PyArg_ParseTuple(args, ":getlogin")) { |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2170 | char *name; |
| 2171 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2172 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2173 | errno = 0; |
| 2174 | name = getlogin(); |
| 2175 | if (name == NULL) { |
| 2176 | if (errno) |
| 2177 | posix_error(); |
| 2178 | else |
| 2179 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 2180 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2181 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2182 | else |
| 2183 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2184 | errno = old_errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2185 | } |
| 2186 | return result; |
| 2187 | } |
| 2188 | #endif |
| 2189 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2190 | #ifdef HAVE_GETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2191 | static char posix_getuid__doc__[] = |
| 2192 | "getuid() -> uid\n\ |
| 2193 | Return the current process's user id."; |
| 2194 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2195 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2196 | posix_getuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2197 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2198 | if (!PyArg_ParseTuple(args, ":getuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2199 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2200 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2201 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2202 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2203 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2204 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2205 | #ifdef HAVE_KILL |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2206 | static char posix_kill__doc__[] = |
| 2207 | "kill(pid, sig) -> None\n\ |
| 2208 | Kill a process with a signal."; |
| 2209 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2210 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2211 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2212 | { |
| 2213 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2214 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2215 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2216 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2217 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 2218 | APIRET rc; |
| 2219 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2220 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2221 | |
| 2222 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 2223 | APIRET rc; |
| 2224 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2225 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2226 | |
| 2227 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2228 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2229 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2230 | if (kill(pid, sig) == -1) |
| 2231 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2232 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2233 | Py_INCREF(Py_None); |
| 2234 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2235 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2236 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2237 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 2238 | #ifdef HAVE_KILLPG |
| 2239 | static char posix_killpg__doc__[] = |
| 2240 | "killpg(pgid, sig) -> None\n\ |
| 2241 | Kill a process group with a signal."; |
| 2242 | |
| 2243 | static PyObject * |
| 2244 | posix_killpg(PyObject *self, PyObject *args) |
| 2245 | { |
| 2246 | int pgid, sig; |
| 2247 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 2248 | return NULL; |
| 2249 | if (killpg(pgid, sig) == -1) |
| 2250 | return posix_error(); |
| 2251 | Py_INCREF(Py_None); |
| 2252 | return Py_None; |
| 2253 | } |
| 2254 | #endif |
| 2255 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2256 | #ifdef HAVE_PLOCK |
| 2257 | |
| 2258 | #ifdef HAVE_SYS_LOCK_H |
| 2259 | #include <sys/lock.h> |
| 2260 | #endif |
| 2261 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2262 | static char posix_plock__doc__[] = |
| 2263 | "plock(op) -> None\n\ |
| 2264 | Lock program segments into memory."; |
| 2265 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2266 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2267 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2268 | { |
| 2269 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2270 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2271 | return NULL; |
| 2272 | if (plock(op) == -1) |
| 2273 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2274 | Py_INCREF(Py_None); |
| 2275 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2276 | } |
| 2277 | #endif |
| 2278 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2279 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2280 | #ifdef HAVE_POPEN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2281 | static char posix_popen__doc__[] = |
| 2282 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\ |
| 2283 | Open a pipe to/from a command returning a file object."; |
| 2284 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2285 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2286 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2287 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2288 | async_system(const char *command) |
| 2289 | { |
| 2290 | char *p, errormsg[256], args[1024]; |
| 2291 | RESULTCODES rcodes; |
| 2292 | APIRET rc; |
| 2293 | char *shell = getenv("COMSPEC"); |
| 2294 | if (!shell) |
| 2295 | shell = "cmd"; |
| 2296 | |
| 2297 | strcpy(args, shell); |
| 2298 | p = &args[ strlen(args)+1 ]; |
| 2299 | strcpy(p, "/c "); |
| 2300 | strcat(p, command); |
| 2301 | p += strlen(p) + 1; |
| 2302 | *p = '\0'; |
| 2303 | |
| 2304 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2305 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2306 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2307 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2308 | &rcodes, shell); |
| 2309 | return rc; |
| 2310 | } |
| 2311 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2312 | static FILE * |
| 2313 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2314 | { |
| 2315 | HFILE rhan, whan; |
| 2316 | FILE *retfd = NULL; |
| 2317 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 2318 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2319 | if (rc != NO_ERROR) { |
| 2320 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2321 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2322 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2323 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2324 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 2325 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2326 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2327 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2328 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2329 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2330 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 2331 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2332 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2333 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2336 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 2337 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2338 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2339 | if (rc == NO_ERROR) |
| 2340 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
| 2341 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2342 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 2343 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2344 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2345 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 2346 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2347 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2348 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2349 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2350 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2351 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 2352 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2353 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2354 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2357 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 2358 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2359 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2360 | if (rc == NO_ERROR) |
| 2361 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
| 2362 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2363 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 2364 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2365 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2366 | } else { |
| 2367 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2368 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2369 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2373 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2374 | { |
| 2375 | char *name; |
| 2376 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2377 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2378 | FILE *fp; |
| 2379 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2380 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2381 | return NULL; |
| 2382 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2383 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2384 | Py_END_ALLOW_THREADS |
| 2385 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2386 | return os2_error(err); |
| 2387 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2388 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 2389 | if (f != NULL) |
| 2390 | PyFile_SetBufSize(f, bufsize); |
| 2391 | return f; |
| 2392 | } |
| 2393 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2394 | #elif defined(PYCC_GCC) |
| 2395 | |
| 2396 | /* standard posix version of popen() support */ |
| 2397 | static PyObject * |
| 2398 | posix_popen(PyObject *self, PyObject *args) |
| 2399 | { |
| 2400 | char *name; |
| 2401 | char *mode = "r"; |
| 2402 | int bufsize = -1; |
| 2403 | FILE *fp; |
| 2404 | PyObject *f; |
| 2405 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 2406 | return NULL; |
| 2407 | Py_BEGIN_ALLOW_THREADS |
| 2408 | fp = popen(name, mode); |
| 2409 | Py_END_ALLOW_THREADS |
| 2410 | if (fp == NULL) |
| 2411 | return posix_error(); |
| 2412 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 2413 | if (f != NULL) |
| 2414 | PyFile_SetBufSize(f, bufsize); |
| 2415 | return f; |
| 2416 | } |
| 2417 | |
| 2418 | /* fork() under OS/2 has lots'o'warts |
| 2419 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 2420 | * most of this code is a ripoff of the win32 code, but using the |
| 2421 | * capabilities of EMX's C library routines |
| 2422 | */ |
| 2423 | |
| 2424 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 2425 | #define POPEN_1 1 |
| 2426 | #define POPEN_2 2 |
| 2427 | #define POPEN_3 3 |
| 2428 | #define POPEN_4 4 |
| 2429 | |
| 2430 | static PyObject *_PyPopen(char *, int, int, int); |
| 2431 | static int _PyPclose(FILE *file); |
| 2432 | |
| 2433 | /* |
| 2434 | * Internal dictionary mapping popen* file pointers to process handles, |
| 2435 | * for use when retrieving the process exit code. See _PyPclose() below |
| 2436 | * for more information on this dictionary's use. |
| 2437 | */ |
| 2438 | static PyObject *_PyPopenProcs = NULL; |
| 2439 | |
| 2440 | /* os2emx version of popen2() |
| 2441 | * |
| 2442 | * The result of this function is a pipe (file) connected to the |
| 2443 | * process's stdin, and a pipe connected to the process's stdout. |
| 2444 | */ |
| 2445 | |
| 2446 | static PyObject * |
| 2447 | os2emx_popen2(PyObject *self, PyObject *args) |
| 2448 | { |
| 2449 | PyObject *f; |
| 2450 | int tm=0; |
| 2451 | |
| 2452 | char *cmdstring; |
| 2453 | char *mode = "t"; |
| 2454 | int bufsize = -1; |
| 2455 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 2456 | return NULL; |
| 2457 | |
| 2458 | if (*mode == 't') |
| 2459 | tm = O_TEXT; |
| 2460 | else if (*mode != 'b') { |
| 2461 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 2462 | return NULL; |
| 2463 | } else |
| 2464 | tm = O_BINARY; |
| 2465 | |
| 2466 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 2467 | |
| 2468 | return f; |
| 2469 | } |
| 2470 | |
| 2471 | /* |
| 2472 | * Variation on os2emx.popen2 |
| 2473 | * |
| 2474 | * The result of this function is 3 pipes - the process's stdin, |
| 2475 | * stdout and stderr |
| 2476 | */ |
| 2477 | |
| 2478 | static PyObject * |
| 2479 | os2emx_popen3(PyObject *self, PyObject *args) |
| 2480 | { |
| 2481 | PyObject *f; |
| 2482 | int tm = 0; |
| 2483 | |
| 2484 | char *cmdstring; |
| 2485 | char *mode = "t"; |
| 2486 | int bufsize = -1; |
| 2487 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 2488 | return NULL; |
| 2489 | |
| 2490 | if (*mode == 't') |
| 2491 | tm = O_TEXT; |
| 2492 | else if (*mode != 'b') { |
| 2493 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 2494 | return NULL; |
| 2495 | } else |
| 2496 | tm = O_BINARY; |
| 2497 | |
| 2498 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 2499 | |
| 2500 | return f; |
| 2501 | } |
| 2502 | |
| 2503 | /* |
| 2504 | * Variation on os2emx.popen2 |
| 2505 | * |
| 2506 | * The result of this function is 2 pipes - the processes stdin, |
| 2507 | * and stdout+stderr combined as a single pipe. |
| 2508 | */ |
| 2509 | |
| 2510 | static PyObject * |
| 2511 | os2emx_popen4(PyObject *self, PyObject *args) |
| 2512 | { |
| 2513 | PyObject *f; |
| 2514 | int tm = 0; |
| 2515 | |
| 2516 | char *cmdstring; |
| 2517 | char *mode = "t"; |
| 2518 | int bufsize = -1; |
| 2519 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 2520 | return NULL; |
| 2521 | |
| 2522 | if (*mode == 't') |
| 2523 | tm = O_TEXT; |
| 2524 | else if (*mode != 'b') { |
| 2525 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 2526 | return NULL; |
| 2527 | } else |
| 2528 | tm = O_BINARY; |
| 2529 | |
| 2530 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 2531 | |
| 2532 | return f; |
| 2533 | } |
| 2534 | |
| 2535 | /* a couple of structures for convenient handling of multiple |
| 2536 | * file handles and pipes |
| 2537 | */ |
| 2538 | struct file_ref |
| 2539 | { |
| 2540 | int handle; |
| 2541 | int flags; |
| 2542 | }; |
| 2543 | |
| 2544 | struct pipe_ref |
| 2545 | { |
| 2546 | int rd; |
| 2547 | int wr; |
| 2548 | }; |
| 2549 | |
| 2550 | /* The following code is derived from the win32 code */ |
| 2551 | |
| 2552 | static PyObject * |
| 2553 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 2554 | { |
| 2555 | struct file_ref stdio[3]; |
| 2556 | struct pipe_ref p_fd[3]; |
| 2557 | FILE *p_s[3]; |
| 2558 | int file_count, i, pipe_err, pipe_pid; |
| 2559 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 2560 | PyObject *f, *p_f[3]; |
| 2561 | |
| 2562 | /* file modes for subsequent fdopen's on pipe handles */ |
| 2563 | if (mode == O_TEXT) |
| 2564 | { |
| 2565 | rd_mode = "rt"; |
| 2566 | wr_mode = "wt"; |
| 2567 | } |
| 2568 | else |
| 2569 | { |
| 2570 | rd_mode = "rb"; |
| 2571 | wr_mode = "wb"; |
| 2572 | } |
| 2573 | |
| 2574 | /* prepare shell references */ |
| 2575 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 2576 | if ((shell = getenv("COMSPEC")) == NULL) |
| 2577 | { |
| 2578 | errno = ENOENT; |
| 2579 | return posix_error(); |
| 2580 | } |
| 2581 | |
| 2582 | sh_name = _getname(shell); |
| 2583 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 2584 | opt = "/c"; |
| 2585 | else |
| 2586 | opt = "-c"; |
| 2587 | |
| 2588 | /* save current stdio fds + their flags, and set not inheritable */ |
| 2589 | i = pipe_err = 0; |
| 2590 | while (pipe_err >= 0 && i < 3) |
| 2591 | { |
| 2592 | pipe_err = stdio[i].handle = dup(i); |
| 2593 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 2594 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 2595 | i++; |
| 2596 | } |
| 2597 | if (pipe_err < 0) |
| 2598 | { |
| 2599 | /* didn't get them all saved - clean up and bail out */ |
| 2600 | int saved_err = errno; |
| 2601 | while (i-- > 0) |
| 2602 | { |
| 2603 | close(stdio[i].handle); |
| 2604 | } |
| 2605 | errno = saved_err; |
| 2606 | return posix_error(); |
| 2607 | } |
| 2608 | |
| 2609 | /* create pipe ends */ |
| 2610 | file_count = 2; |
| 2611 | if (n == POPEN_3) |
| 2612 | file_count = 3; |
| 2613 | i = pipe_err = 0; |
| 2614 | while ((pipe_err == 0) && (i < file_count)) |
| 2615 | pipe_err = pipe((int *)&p_fd[i++]); |
| 2616 | if (pipe_err < 0) |
| 2617 | { |
| 2618 | /* didn't get them all made - clean up and bail out */ |
| 2619 | while (i-- > 0) |
| 2620 | { |
| 2621 | close(p_fd[i].wr); |
| 2622 | close(p_fd[i].rd); |
| 2623 | } |
| 2624 | errno = EPIPE; |
| 2625 | return posix_error(); |
| 2626 | } |
| 2627 | |
| 2628 | /* change the actual standard IO streams over temporarily, |
| 2629 | * making the retained pipe ends non-inheritable |
| 2630 | */ |
| 2631 | pipe_err = 0; |
| 2632 | |
| 2633 | /* - stdin */ |
| 2634 | if (dup2(p_fd[0].rd, 0) == 0) |
| 2635 | { |
| 2636 | close(p_fd[0].rd); |
| 2637 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 2638 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 2639 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 2640 | { |
| 2641 | close(p_fd[0].wr); |
| 2642 | pipe_err = -1; |
| 2643 | } |
| 2644 | } |
| 2645 | else |
| 2646 | { |
| 2647 | pipe_err = -1; |
| 2648 | } |
| 2649 | |
| 2650 | /* - stdout */ |
| 2651 | if (pipe_err == 0) |
| 2652 | { |
| 2653 | if (dup2(p_fd[1].wr, 1) == 1) |
| 2654 | { |
| 2655 | close(p_fd[1].wr); |
| 2656 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 2657 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 2658 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 2659 | { |
| 2660 | close(p_fd[1].rd); |
| 2661 | pipe_err = -1; |
| 2662 | } |
| 2663 | } |
| 2664 | else |
| 2665 | { |
| 2666 | pipe_err = -1; |
| 2667 | } |
| 2668 | } |
| 2669 | |
| 2670 | /* - stderr, as required */ |
| 2671 | if (pipe_err == 0) |
| 2672 | switch (n) |
| 2673 | { |
| 2674 | case POPEN_3: |
| 2675 | { |
| 2676 | if (dup2(p_fd[2].wr, 2) == 2) |
| 2677 | { |
| 2678 | close(p_fd[2].wr); |
| 2679 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 2680 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 2681 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 2682 | { |
| 2683 | close(p_fd[2].rd); |
| 2684 | pipe_err = -1; |
| 2685 | } |
| 2686 | } |
| 2687 | else |
| 2688 | { |
| 2689 | pipe_err = -1; |
| 2690 | } |
| 2691 | break; |
| 2692 | } |
| 2693 | |
| 2694 | case POPEN_4: |
| 2695 | { |
| 2696 | if (dup2(1, 2) != 2) |
| 2697 | { |
| 2698 | pipe_err = -1; |
| 2699 | } |
| 2700 | break; |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | /* spawn the child process */ |
| 2705 | if (pipe_err == 0) |
| 2706 | { |
| 2707 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 2708 | if (pipe_pid == -1) |
| 2709 | { |
| 2710 | pipe_err = -1; |
| 2711 | } |
| 2712 | else |
| 2713 | { |
| 2714 | /* save the PID into the FILE structure |
| 2715 | * NOTE: this implementation doesn't actually |
| 2716 | * take advantage of this, but do it for |
| 2717 | * completeness - AIM Apr01 |
| 2718 | */ |
| 2719 | for (i = 0; i < file_count; i++) |
| 2720 | p_s[i]->_pid = pipe_pid; |
| 2721 | } |
| 2722 | } |
| 2723 | |
| 2724 | /* reset standard IO to normal */ |
| 2725 | for (i = 0; i < 3; i++) |
| 2726 | { |
| 2727 | dup2(stdio[i].handle, i); |
| 2728 | fcntl(i, F_SETFD, stdio[i].flags); |
| 2729 | close(stdio[i].handle); |
| 2730 | } |
| 2731 | |
| 2732 | /* if any remnant problems, clean up and bail out */ |
| 2733 | if (pipe_err < 0) |
| 2734 | { |
| 2735 | for (i = 0; i < 3; i++) |
| 2736 | { |
| 2737 | close(p_fd[i].rd); |
| 2738 | close(p_fd[i].wr); |
| 2739 | } |
| 2740 | errno = EPIPE; |
| 2741 | return posix_error_with_filename(cmdstring); |
| 2742 | } |
| 2743 | |
| 2744 | /* build tuple of file objects to return */ |
| 2745 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 2746 | PyFile_SetBufSize(p_f[0], bufsize); |
| 2747 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 2748 | PyFile_SetBufSize(p_f[1], bufsize); |
| 2749 | if (n == POPEN_3) |
| 2750 | { |
| 2751 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 2752 | PyFile_SetBufSize(p_f[0], bufsize); |
| 2753 | f = Py_BuildValue("OOO", p_f[0], p_f[1], p_f[2]); |
| 2754 | } |
| 2755 | else |
| 2756 | f = Py_BuildValue("OO", p_f[0], p_f[1]); |
| 2757 | |
| 2758 | /* |
| 2759 | * Insert the files we've created into the process dictionary |
| 2760 | * all referencing the list with the process handle and the |
| 2761 | * initial number of files (see description below in _PyPclose). |
| 2762 | * Since if _PyPclose later tried to wait on a process when all |
| 2763 | * handles weren't closed, it could create a deadlock with the |
| 2764 | * child, we spend some energy here to try to ensure that we |
| 2765 | * either insert all file handles into the dictionary or none |
| 2766 | * at all. It's a little clumsy with the various popen modes |
| 2767 | * and variable number of files involved. |
| 2768 | */ |
| 2769 | if (!_PyPopenProcs) |
| 2770 | { |
| 2771 | _PyPopenProcs = PyDict_New(); |
| 2772 | } |
| 2773 | |
| 2774 | if (_PyPopenProcs) |
| 2775 | { |
| 2776 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 2777 | int ins_rc[3]; |
| 2778 | |
| 2779 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 2780 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 2781 | |
| 2782 | procObj = PyList_New(2); |
| 2783 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 2784 | intObj = PyInt_FromLong((long) file_count); |
| 2785 | |
| 2786 | if (procObj && pidObj && intObj) |
| 2787 | { |
| 2788 | PyList_SetItem(procObj, 0, pidObj); |
| 2789 | PyList_SetItem(procObj, 1, intObj); |
| 2790 | |
| 2791 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 2792 | if (fileObj[0]) |
| 2793 | { |
| 2794 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 2795 | fileObj[0], |
| 2796 | procObj); |
| 2797 | } |
| 2798 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 2799 | if (fileObj[1]) |
| 2800 | { |
| 2801 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 2802 | fileObj[1], |
| 2803 | procObj); |
| 2804 | } |
| 2805 | if (file_count >= 3) |
| 2806 | { |
| 2807 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 2808 | if (fileObj[2]) |
| 2809 | { |
| 2810 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 2811 | fileObj[2], |
| 2812 | procObj); |
| 2813 | } |
| 2814 | } |
| 2815 | |
| 2816 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 2817 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 2818 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 2819 | { |
| 2820 | /* Something failed - remove any dictionary |
| 2821 | * entries that did make it. |
| 2822 | */ |
| 2823 | if (!ins_rc[0] && fileObj[0]) |
| 2824 | { |
| 2825 | PyDict_DelItem(_PyPopenProcs, |
| 2826 | fileObj[0]); |
| 2827 | } |
| 2828 | if (!ins_rc[1] && fileObj[1]) |
| 2829 | { |
| 2830 | PyDict_DelItem(_PyPopenProcs, |
| 2831 | fileObj[1]); |
| 2832 | } |
| 2833 | if (!ins_rc[2] && fileObj[2]) |
| 2834 | { |
| 2835 | PyDict_DelItem(_PyPopenProcs, |
| 2836 | fileObj[2]); |
| 2837 | } |
| 2838 | } |
| 2839 | } |
| 2840 | |
| 2841 | /* |
| 2842 | * Clean up our localized references for the dictionary keys |
| 2843 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 2844 | * that got placed in the dictionary. |
| 2845 | */ |
| 2846 | Py_XDECREF(procObj); |
| 2847 | Py_XDECREF(fileObj[0]); |
| 2848 | Py_XDECREF(fileObj[1]); |
| 2849 | Py_XDECREF(fileObj[2]); |
| 2850 | } |
| 2851 | |
| 2852 | /* Child is launched. */ |
| 2853 | return f; |
| 2854 | } |
| 2855 | |
| 2856 | /* |
| 2857 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 2858 | * exit code for the child process and return as a result of the close. |
| 2859 | * |
| 2860 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 2861 | * input file pointer to information about the process that was |
| 2862 | * originally created by the popen* call that created the file pointer. |
| 2863 | * The dictionary uses the file pointer as a key (with one entry |
| 2864 | * inserted for each file returned by the original popen* call) and a |
| 2865 | * single list object as the value for all files from a single call. |
| 2866 | * The list object contains the Win32 process handle at [0], and a file |
| 2867 | * count at [1], which is initialized to the total number of file |
| 2868 | * handles using that list. |
| 2869 | * |
| 2870 | * This function closes whichever handle it is passed, and decrements |
| 2871 | * the file count in the dictionary for the process handle pointed to |
| 2872 | * by this file. On the last close (when the file count reaches zero), |
| 2873 | * this function will wait for the child process and then return its |
| 2874 | * exit code as the result of the close() operation. This permits the |
| 2875 | * files to be closed in any order - it is always the close() of the |
| 2876 | * final handle that will return the exit code. |
| 2877 | */ |
| 2878 | |
| 2879 | /* RED_FLAG 31-Aug-2000 Tim |
| 2880 | * This is always called (today!) between a pair of |
| 2881 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 2882 | * macros. So the thread running this has no valid thread state, as |
| 2883 | * far as Python is concerned. However, this calls some Python API |
| 2884 | * functions that cannot be called safely without a valid thread |
| 2885 | * state, in particular PyDict_GetItem. |
| 2886 | * As a temporary hack (although it may last for years ...), we |
| 2887 | * *rely* on not having a valid thread state in this function, in |
| 2888 | * order to create our own "from scratch". |
| 2889 | * This will deadlock if _PyPclose is ever called by a thread |
| 2890 | * holding the global lock. |
| 2891 | * (The OS/2 EMX thread support appears to cover the case where the |
| 2892 | * lock is already held - AIM Apr01) |
| 2893 | */ |
| 2894 | |
| 2895 | static int _PyPclose(FILE *file) |
| 2896 | { |
| 2897 | int result; |
| 2898 | int exit_code; |
| 2899 | int pipe_pid; |
| 2900 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 2901 | int file_count; |
| 2902 | #ifdef WITH_THREAD |
| 2903 | PyInterpreterState* pInterpreterState; |
| 2904 | PyThreadState* pThreadState; |
| 2905 | #endif |
| 2906 | |
| 2907 | /* Close the file handle first, to ensure it can't block the |
| 2908 | * child from exiting if it's the last handle. |
| 2909 | */ |
| 2910 | result = fclose(file); |
| 2911 | |
| 2912 | #ifdef WITH_THREAD |
| 2913 | /* Bootstrap a valid thread state into existence. */ |
| 2914 | pInterpreterState = PyInterpreterState_New(); |
| 2915 | if (!pInterpreterState) { |
| 2916 | /* Well, we're hosed now! We don't have a thread |
| 2917 | * state, so can't call a nice error routine, or raise |
| 2918 | * an exception. Just die. |
| 2919 | */ |
| 2920 | Py_FatalError("unable to allocate interpreter state " |
| 2921 | "when closing popen object."); |
| 2922 | return -1; /* unreachable */ |
| 2923 | } |
| 2924 | pThreadState = PyThreadState_New(pInterpreterState); |
| 2925 | if (!pThreadState) { |
| 2926 | Py_FatalError("unable to allocate thread state " |
| 2927 | "when closing popen object."); |
| 2928 | return -1; /* unreachable */ |
| 2929 | } |
| 2930 | /* Grab the global lock. Note that this will deadlock if the |
| 2931 | * current thread already has the lock! (see RED_FLAG comments |
| 2932 | * before this function) |
| 2933 | */ |
| 2934 | PyEval_RestoreThread(pThreadState); |
| 2935 | #endif |
| 2936 | |
| 2937 | if (_PyPopenProcs) |
| 2938 | { |
| 2939 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 2940 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 2941 | fileObj)) != NULL && |
| 2942 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 2943 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 2944 | { |
| 2945 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 2946 | file_count = (int) PyInt_AsLong(intObj); |
| 2947 | |
| 2948 | if (file_count > 1) |
| 2949 | { |
| 2950 | /* Still other files referencing process */ |
| 2951 | file_count--; |
| 2952 | PyList_SetItem(procObj,1, |
| 2953 | PyInt_FromLong((long) file_count)); |
| 2954 | } |
| 2955 | else |
| 2956 | { |
| 2957 | /* Last file for this process */ |
| 2958 | if (result != EOF && |
| 2959 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 2960 | { |
| 2961 | /* extract exit status */ |
| 2962 | if (WIFEXITED(exit_code)) |
| 2963 | { |
| 2964 | result = WEXITSTATUS(exit_code); |
| 2965 | } |
| 2966 | else |
| 2967 | { |
| 2968 | errno = EPIPE; |
| 2969 | result = -1; |
| 2970 | } |
| 2971 | } |
| 2972 | else |
| 2973 | { |
| 2974 | /* Indicate failure - this will cause the file object |
| 2975 | * to raise an I/O error and translate the last |
| 2976 | * error code from errno. We do have a problem with |
| 2977 | * last errors that overlap the normal errno table, |
| 2978 | * but that's a consistent problem with the file object. |
| 2979 | */ |
| 2980 | result = -1; |
| 2981 | } |
| 2982 | } |
| 2983 | |
| 2984 | /* Remove this file pointer from dictionary */ |
| 2985 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 2986 | |
| 2987 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 2988 | { |
| 2989 | Py_DECREF(_PyPopenProcs); |
| 2990 | _PyPopenProcs = NULL; |
| 2991 | } |
| 2992 | |
| 2993 | } /* if object retrieval ok */ |
| 2994 | |
| 2995 | Py_XDECREF(fileObj); |
| 2996 | } /* if _PyPopenProcs */ |
| 2997 | |
| 2998 | #ifdef WITH_THREAD |
| 2999 | /* Tear down the thread & interpreter states. |
| 3000 | * Note that interpreter state clear & delete functions automatically |
| 3001 | * call the thread clear & delete functions, and indeed insist on |
| 3002 | * doing that themselves. The lock must be held during the clear, but |
| 3003 | * need not be held during the delete. |
| 3004 | */ |
| 3005 | PyInterpreterState_Clear(pInterpreterState); |
| 3006 | PyEval_ReleaseThread(pThreadState); |
| 3007 | PyInterpreterState_Delete(pInterpreterState); |
| 3008 | #endif |
| 3009 | |
| 3010 | return result; |
| 3011 | } |
| 3012 | |
| 3013 | #endif /* PYCC_??? */ |
| 3014 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3015 | #elif defined(MS_WIN32) |
| 3016 | |
| 3017 | /* |
| 3018 | * Portable 'popen' replacement for Win32. |
| 3019 | * |
| 3020 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 3021 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3022 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3023 | */ |
| 3024 | |
| 3025 | #include <malloc.h> |
| 3026 | #include <io.h> |
| 3027 | #include <fcntl.h> |
| 3028 | |
| 3029 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 3030 | #define POPEN_1 1 |
| 3031 | #define POPEN_2 2 |
| 3032 | #define POPEN_3 3 |
| 3033 | #define POPEN_4 4 |
| 3034 | |
| 3035 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3036 | static int _PyPclose(FILE *file); |
| 3037 | |
| 3038 | /* |
| 3039 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3040 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3041 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3042 | */ |
| 3043 | static PyObject *_PyPopenProcs = NULL; |
| 3044 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3045 | |
| 3046 | /* popen that works from a GUI. |
| 3047 | * |
| 3048 | * The result of this function is a pipe (file) connected to the |
| 3049 | * processes stdin or stdout, depending on the requested mode. |
| 3050 | */ |
| 3051 | |
| 3052 | static PyObject * |
| 3053 | posix_popen(PyObject *self, PyObject *args) |
| 3054 | { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3055 | PyObject *f, *s; |
| 3056 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3057 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3058 | char *cmdstring; |
| 3059 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3060 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3061 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3062 | return NULL; |
| 3063 | |
| 3064 | s = PyTuple_New(0); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3065 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3066 | if (*mode == 'r') |
| 3067 | tm = _O_RDONLY; |
| 3068 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3069 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3070 | return NULL; |
| 3071 | } else |
| 3072 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3073 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3074 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3075 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3076 | return NULL; |
| 3077 | } |
| 3078 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3079 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3080 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3081 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3082 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3083 | else |
| 3084 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 3085 | |
| 3086 | return f; |
| 3087 | } |
| 3088 | |
| 3089 | /* Variation on win32pipe.popen |
| 3090 | * |
| 3091 | * The result of this function is a pipe (file) connected to the |
| 3092 | * process's stdin, and a pipe connected to the process's stdout. |
| 3093 | */ |
| 3094 | |
| 3095 | static PyObject * |
| 3096 | win32_popen2(PyObject *self, PyObject *args) |
| 3097 | { |
| 3098 | PyObject *f; |
| 3099 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3100 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3101 | char *cmdstring; |
| 3102 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3103 | int bufsize = -1; |
| 3104 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3105 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3106 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3107 | if (*mode == 't') |
| 3108 | tm = _O_TEXT; |
| 3109 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3110 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3111 | return NULL; |
| 3112 | } else |
| 3113 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3114 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3115 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3116 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3117 | return NULL; |
| 3118 | } |
| 3119 | |
| 3120 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3121 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3122 | return f; |
| 3123 | } |
| 3124 | |
| 3125 | /* |
| 3126 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3127 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3128 | * The result of this function is 3 pipes - the process's stdin, |
| 3129 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3130 | */ |
| 3131 | |
| 3132 | static PyObject * |
| 3133 | win32_popen3(PyObject *self, PyObject *args) |
| 3134 | { |
| 3135 | PyObject *f; |
| 3136 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3137 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3138 | char *cmdstring; |
| 3139 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3140 | int bufsize = -1; |
| 3141 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3142 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3143 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3144 | if (*mode == 't') |
| 3145 | tm = _O_TEXT; |
| 3146 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3147 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3148 | return NULL; |
| 3149 | } else |
| 3150 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3151 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3152 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3153 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3154 | return NULL; |
| 3155 | } |
| 3156 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3157 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3158 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3159 | return f; |
| 3160 | } |
| 3161 | |
| 3162 | /* |
| 3163 | * Variation on win32pipe.popen |
| 3164 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3165 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3166 | * and stdout+stderr combined as a single pipe. |
| 3167 | */ |
| 3168 | |
| 3169 | static PyObject * |
| 3170 | win32_popen4(PyObject *self, PyObject *args) |
| 3171 | { |
| 3172 | PyObject *f; |
| 3173 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3174 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3175 | char *cmdstring; |
| 3176 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3177 | int bufsize = -1; |
| 3178 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3179 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3180 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3181 | if (*mode == 't') |
| 3182 | tm = _O_TEXT; |
| 3183 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3184 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3185 | return NULL; |
| 3186 | } else |
| 3187 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3188 | |
| 3189 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3190 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3191 | return NULL; |
| 3192 | } |
| 3193 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3194 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3195 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3196 | return f; |
| 3197 | } |
| 3198 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3199 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3200 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3201 | HANDLE hStdin, |
| 3202 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3203 | HANDLE hStderr, |
| 3204 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3205 | { |
| 3206 | PROCESS_INFORMATION piProcInfo; |
| 3207 | STARTUPINFO siStartInfo; |
| 3208 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3209 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3210 | int i; |
| 3211 | int x; |
| 3212 | |
| 3213 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3214 | char *comshell; |
| 3215 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3216 | s1 = (char *)_alloca(i); |
| 3217 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 3218 | return x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3219 | |
| 3220 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 3221 | * then use the w9xpopen hack. |
| 3222 | */ |
| 3223 | comshell = s1 + x; |
| 3224 | while (comshell >= s1 && *comshell != '\\') |
| 3225 | --comshell; |
| 3226 | ++comshell; |
| 3227 | |
| 3228 | if (GetVersion() < 0x80000000 && |
| 3229 | _stricmp(comshell, "command.com") != 0) { |
| 3230 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3231 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
| 3232 | s2 = (char *)_alloca(x); |
| 3233 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 3234 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3235 | } |
| 3236 | else { |
| 3237 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3238 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 3239 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3240 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3241 | char modulepath[_MAX_PATH]; |
| 3242 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3243 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 3244 | for (i = x = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3245 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3246 | x = i+1; |
| 3247 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3248 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3249 | strncat(modulepath, |
| 3250 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3251 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 3252 | -strlen(modulepath)); |
| 3253 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3254 | /* Eeek - file-not-found - possibly an embedding |
| 3255 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3256 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3257 | strncpy(modulepath, |
| 3258 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3259 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 3260 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 3261 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3262 | strncat(modulepath, |
| 3263 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3264 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 3265 | -strlen(modulepath)); |
| 3266 | /* No where else to look - raise an easily identifiable |
| 3267 | error, rather than leaving Windows to report |
| 3268 | "file not found" - as the user is probably blissfully |
| 3269 | unaware this shim EXE is used, and it will confuse them. |
| 3270 | (well, it confused me for a while ;-) |
| 3271 | */ |
| 3272 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3273 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3274 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3275 | "for popen to work with your shell " |
| 3276 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3277 | szConsoleSpawn); |
| 3278 | return FALSE; |
| 3279 | } |
| 3280 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3281 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3282 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3283 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3284 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3285 | s2 = (char *)_alloca(x); |
| 3286 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 3287 | PyOS_snprintf( |
| 3288 | s2, x, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3289 | "%s \"%s%s%s\"", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3290 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3291 | s1, |
| 3292 | s3, |
| 3293 | cmdstring); |
| 3294 | } |
| 3295 | } |
| 3296 | |
| 3297 | /* Could be an else here to try cmd.exe / command.com in the path |
| 3298 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3299 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3300 | PyErr_SetString(PyExc_RuntimeError, |
| 3301 | "Cannot locate a COMSPEC environment variable to " |
| 3302 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3303 | return FALSE; |
| 3304 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3305 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3306 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 3307 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 3308 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 3309 | siStartInfo.hStdInput = hStdin; |
| 3310 | siStartInfo.hStdOutput = hStdout; |
| 3311 | siStartInfo.hStdError = hStderr; |
| 3312 | siStartInfo.wShowWindow = SW_HIDE; |
| 3313 | |
| 3314 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3315 | s2, |
| 3316 | NULL, |
| 3317 | NULL, |
| 3318 | TRUE, |
| 3319 | CREATE_NEW_CONSOLE, |
| 3320 | NULL, |
| 3321 | NULL, |
| 3322 | &siStartInfo, |
| 3323 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3324 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3325 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3326 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3327 | /* Return process handle */ |
| 3328 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3329 | return TRUE; |
| 3330 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3331 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3332 | return FALSE; |
| 3333 | } |
| 3334 | |
| 3335 | /* The following code is based off of KB: Q190351 */ |
| 3336 | |
| 3337 | static PyObject * |
| 3338 | _PyPopen(char *cmdstring, int mode, int n) |
| 3339 | { |
| 3340 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 3341 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3342 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3343 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3344 | SECURITY_ATTRIBUTES saAttr; |
| 3345 | BOOL fSuccess; |
| 3346 | int fd1, fd2, fd3; |
| 3347 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3348 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3349 | PyObject *f; |
| 3350 | |
| 3351 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 3352 | saAttr.bInheritHandle = TRUE; |
| 3353 | saAttr.lpSecurityDescriptor = NULL; |
| 3354 | |
| 3355 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 3356 | return win32_error("CreatePipe", NULL); |
| 3357 | |
| 3358 | /* Create new output read handle and the input write handle. Set |
| 3359 | * the inheritance properties to FALSE. Otherwise, the child inherits |
| 3360 | * the these handles; resulting in non-closeable handles to the pipes |
| 3361 | * being created. */ |
| 3362 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3363 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 3364 | FALSE, |
| 3365 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3366 | if (!fSuccess) |
| 3367 | return win32_error("DuplicateHandle", NULL); |
| 3368 | |
| 3369 | /* Close the inheritable version of ChildStdin |
| 3370 | that we're using. */ |
| 3371 | CloseHandle(hChildStdinWr); |
| 3372 | |
| 3373 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 3374 | return win32_error("CreatePipe", NULL); |
| 3375 | |
| 3376 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3377 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 3378 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3379 | if (!fSuccess) |
| 3380 | return win32_error("DuplicateHandle", NULL); |
| 3381 | |
| 3382 | /* Close the inheritable version of ChildStdout |
| 3383 | that we're using. */ |
| 3384 | CloseHandle(hChildStdoutRd); |
| 3385 | |
| 3386 | if (n != POPEN_4) { |
| 3387 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 3388 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3389 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 3390 | hChildStderrRd, |
| 3391 | GetCurrentProcess(), |
| 3392 | &hChildStderrRdDup, 0, |
| 3393 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3394 | if (!fSuccess) |
| 3395 | return win32_error("DuplicateHandle", NULL); |
| 3396 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 3397 | CloseHandle(hChildStderrRd); |
| 3398 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3399 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3400 | switch (n) { |
| 3401 | case POPEN_1: |
| 3402 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 3403 | case _O_WRONLY | _O_TEXT: |
| 3404 | /* Case for writing to child Stdin in text mode. */ |
| 3405 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 3406 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3407 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3408 | PyFile_SetBufSize(f, 0); |
| 3409 | /* We don't care about these pipes anymore, so close them. */ |
| 3410 | CloseHandle(hChildStdoutRdDup); |
| 3411 | CloseHandle(hChildStderrRdDup); |
| 3412 | break; |
| 3413 | |
| 3414 | case _O_RDONLY | _O_TEXT: |
| 3415 | /* Case for reading from child Stdout in text mode. */ |
| 3416 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 3417 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3418 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3419 | PyFile_SetBufSize(f, 0); |
| 3420 | /* We don't care about these pipes anymore, so close them. */ |
| 3421 | CloseHandle(hChildStdinWrDup); |
| 3422 | CloseHandle(hChildStderrRdDup); |
| 3423 | break; |
| 3424 | |
| 3425 | case _O_RDONLY | _O_BINARY: |
| 3426 | /* Case for readinig from child Stdout in binary mode. */ |
| 3427 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 3428 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3429 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3430 | PyFile_SetBufSize(f, 0); |
| 3431 | /* We don't care about these pipes anymore, so close them. */ |
| 3432 | CloseHandle(hChildStdinWrDup); |
| 3433 | CloseHandle(hChildStderrRdDup); |
| 3434 | break; |
| 3435 | |
| 3436 | case _O_WRONLY | _O_BINARY: |
| 3437 | /* Case for writing to child Stdin in binary mode. */ |
| 3438 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 3439 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3440 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3441 | PyFile_SetBufSize(f, 0); |
| 3442 | /* We don't care about these pipes anymore, so close them. */ |
| 3443 | CloseHandle(hChildStdoutRdDup); |
| 3444 | CloseHandle(hChildStderrRdDup); |
| 3445 | break; |
| 3446 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3447 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3448 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3449 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3450 | case POPEN_2: |
| 3451 | case POPEN_4: |
| 3452 | { |
| 3453 | char *m1, *m2; |
| 3454 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3455 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3456 | if (mode && _O_TEXT) { |
| 3457 | m1 = "r"; |
| 3458 | m2 = "w"; |
| 3459 | } else { |
| 3460 | m1 = "rb"; |
| 3461 | m2 = "wb"; |
| 3462 | } |
| 3463 | |
| 3464 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 3465 | f1 = _fdopen(fd1, m2); |
| 3466 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 3467 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3468 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3469 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3470 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3471 | PyFile_SetBufSize(p2, 0); |
| 3472 | |
| 3473 | if (n != 4) |
| 3474 | CloseHandle(hChildStderrRdDup); |
| 3475 | |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3476 | f = Py_BuildValue("OO",p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 3477 | Py_XDECREF(p1); |
| 3478 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3479 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3480 | break; |
| 3481 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3482 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3483 | case POPEN_3: |
| 3484 | { |
| 3485 | char *m1, *m2; |
| 3486 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3487 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3488 | if (mode && _O_TEXT) { |
| 3489 | m1 = "r"; |
| 3490 | m2 = "w"; |
| 3491 | } else { |
| 3492 | m1 = "rb"; |
| 3493 | m2 = "wb"; |
| 3494 | } |
| 3495 | |
| 3496 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 3497 | f1 = _fdopen(fd1, m2); |
| 3498 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 3499 | f2 = _fdopen(fd2, m1); |
| 3500 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 3501 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3502 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3503 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 3504 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3505 | PyFile_SetBufSize(p1, 0); |
| 3506 | PyFile_SetBufSize(p2, 0); |
| 3507 | PyFile_SetBufSize(p3, 0); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3508 | f = Py_BuildValue("OOO",p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 3509 | Py_XDECREF(p1); |
| 3510 | Py_XDECREF(p2); |
| 3511 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3512 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3513 | break; |
| 3514 | } |
| 3515 | } |
| 3516 | |
| 3517 | if (n == POPEN_4) { |
| 3518 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3519 | hChildStdinRd, |
| 3520 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3521 | hChildStdoutWr, |
| 3522 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3523 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3524 | } |
| 3525 | else { |
| 3526 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3527 | hChildStdinRd, |
| 3528 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3529 | hChildStderrWr, |
| 3530 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3531 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3534 | /* |
| 3535 | * Insert the files we've created into the process dictionary |
| 3536 | * all referencing the list with the process handle and the |
| 3537 | * initial number of files (see description below in _PyPclose). |
| 3538 | * Since if _PyPclose later tried to wait on a process when all |
| 3539 | * handles weren't closed, it could create a deadlock with the |
| 3540 | * child, we spend some energy here to try to ensure that we |
| 3541 | * either insert all file handles into the dictionary or none |
| 3542 | * at all. It's a little clumsy with the various popen modes |
| 3543 | * and variable number of files involved. |
| 3544 | */ |
| 3545 | if (!_PyPopenProcs) { |
| 3546 | _PyPopenProcs = PyDict_New(); |
| 3547 | } |
| 3548 | |
| 3549 | if (_PyPopenProcs) { |
| 3550 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 3551 | int ins_rc[3]; |
| 3552 | |
| 3553 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 3554 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 3555 | |
| 3556 | procObj = PyList_New(2); |
| 3557 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 3558 | intObj = PyInt_FromLong(file_count); |
| 3559 | |
| 3560 | if (procObj && hProcessObj && intObj) { |
| 3561 | PyList_SetItem(procObj,0,hProcessObj); |
| 3562 | PyList_SetItem(procObj,1,intObj); |
| 3563 | |
| 3564 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 3565 | if (fileObj[0]) { |
| 3566 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 3567 | fileObj[0], |
| 3568 | procObj); |
| 3569 | } |
| 3570 | if (file_count >= 2) { |
| 3571 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 3572 | if (fileObj[1]) { |
| 3573 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 3574 | fileObj[1], |
| 3575 | procObj); |
| 3576 | } |
| 3577 | } |
| 3578 | if (file_count >= 3) { |
| 3579 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 3580 | if (fileObj[2]) { |
| 3581 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 3582 | fileObj[2], |
| 3583 | procObj); |
| 3584 | } |
| 3585 | } |
| 3586 | |
| 3587 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 3588 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 3589 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 3590 | /* Something failed - remove any dictionary |
| 3591 | * entries that did make it. |
| 3592 | */ |
| 3593 | if (!ins_rc[0] && fileObj[0]) { |
| 3594 | PyDict_DelItem(_PyPopenProcs, |
| 3595 | fileObj[0]); |
| 3596 | } |
| 3597 | if (!ins_rc[1] && fileObj[1]) { |
| 3598 | PyDict_DelItem(_PyPopenProcs, |
| 3599 | fileObj[1]); |
| 3600 | } |
| 3601 | if (!ins_rc[2] && fileObj[2]) { |
| 3602 | PyDict_DelItem(_PyPopenProcs, |
| 3603 | fileObj[2]); |
| 3604 | } |
| 3605 | } |
| 3606 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3607 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3608 | /* |
| 3609 | * Clean up our localized references for the dictionary keys |
| 3610 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 3611 | * that got placed in the dictionary. |
| 3612 | */ |
| 3613 | Py_XDECREF(procObj); |
| 3614 | Py_XDECREF(fileObj[0]); |
| 3615 | Py_XDECREF(fileObj[1]); |
| 3616 | Py_XDECREF(fileObj[2]); |
| 3617 | } |
| 3618 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3619 | /* Child is launched. Close the parents copy of those pipe |
| 3620 | * handles that only the child should have open. You need to |
| 3621 | * make sure that no handles to the write end of the output pipe |
| 3622 | * are maintained in this process or else the pipe will not close |
| 3623 | * when the child process exits and the ReadFile will hang. */ |
| 3624 | |
| 3625 | if (!CloseHandle(hChildStdinRd)) |
| 3626 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3627 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3628 | if (!CloseHandle(hChildStdoutWr)) |
| 3629 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3630 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3631 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 3632 | return win32_error("CloseHandle", NULL); |
| 3633 | |
| 3634 | return f; |
| 3635 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3636 | |
| 3637 | /* |
| 3638 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 3639 | * 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] | 3640 | * |
| 3641 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 3642 | * input file pointer to information about the process that was |
| 3643 | * originally created by the popen* call that created the file pointer. |
| 3644 | * The dictionary uses the file pointer as a key (with one entry |
| 3645 | * inserted for each file returned by the original popen* call) and a |
| 3646 | * single list object as the value for all files from a single call. |
| 3647 | * The list object contains the Win32 process handle at [0], and a file |
| 3648 | * count at [1], which is initialized to the total number of file |
| 3649 | * handles using that list. |
| 3650 | * |
| 3651 | * This function closes whichever handle it is passed, and decrements |
| 3652 | * the file count in the dictionary for the process handle pointed to |
| 3653 | * by this file. On the last close (when the file count reaches zero), |
| 3654 | * this function will wait for the child process and then return its |
| 3655 | * exit code as the result of the close() operation. This permits the |
| 3656 | * files to be closed in any order - it is always the close() of the |
| 3657 | * final handle that will return the exit code. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3658 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3659 | |
| 3660 | /* RED_FLAG 31-Aug-2000 Tim |
| 3661 | * This is always called (today!) between a pair of |
| 3662 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 3663 | * macros. So the thread running this has no valid thread state, as |
| 3664 | * far as Python is concerned. However, this calls some Python API |
| 3665 | * functions that cannot be called safely without a valid thread |
| 3666 | * state, in particular PyDict_GetItem. |
| 3667 | * As a temporary hack (although it may last for years ...), we |
| 3668 | * *rely* on not having a valid thread state in this function, in |
| 3669 | * order to create our own "from scratch". |
| 3670 | * This will deadlock if _PyPclose is ever called by a thread |
| 3671 | * holding the global lock. |
| 3672 | */ |
| 3673 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3674 | static int _PyPclose(FILE *file) |
| 3675 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3676 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3677 | DWORD exit_code; |
| 3678 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3679 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 3680 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3681 | #ifdef WITH_THREAD |
| 3682 | PyInterpreterState* pInterpreterState; |
| 3683 | PyThreadState* pThreadState; |
| 3684 | #endif |
| 3685 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3686 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3687 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3688 | */ |
| 3689 | result = fclose(file); |
| 3690 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3691 | #ifdef WITH_THREAD |
| 3692 | /* Bootstrap a valid thread state into existence. */ |
| 3693 | pInterpreterState = PyInterpreterState_New(); |
| 3694 | if (!pInterpreterState) { |
| 3695 | /* Well, we're hosed now! We don't have a thread |
| 3696 | * state, so can't call a nice error routine, or raise |
| 3697 | * an exception. Just die. |
| 3698 | */ |
| 3699 | Py_FatalError("unable to allocate interpreter state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3700 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3701 | return -1; /* unreachable */ |
| 3702 | } |
| 3703 | pThreadState = PyThreadState_New(pInterpreterState); |
| 3704 | if (!pThreadState) { |
| 3705 | Py_FatalError("unable to allocate thread state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3706 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3707 | return -1; /* unreachable */ |
| 3708 | } |
| 3709 | /* Grab the global lock. Note that this will deadlock if the |
| 3710 | * current thread already has the lock! (see RED_FLAG comments |
| 3711 | * before this function) |
| 3712 | */ |
| 3713 | PyEval_RestoreThread(pThreadState); |
| 3714 | #endif |
| 3715 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3716 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3717 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 3718 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 3719 | fileObj)) != NULL && |
| 3720 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 3721 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 3722 | |
| 3723 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 3724 | file_count = PyInt_AsLong(intObj); |
| 3725 | |
| 3726 | if (file_count > 1) { |
| 3727 | /* Still other files referencing process */ |
| 3728 | file_count--; |
| 3729 | PyList_SetItem(procObj,1, |
| 3730 | PyInt_FromLong(file_count)); |
| 3731 | } else { |
| 3732 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3733 | if (result != EOF && |
| 3734 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 3735 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3736 | /* Possible truncation here in 16-bit environments, but |
| 3737 | * real exit codes are just the lower byte in any event. |
| 3738 | */ |
| 3739 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3740 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3741 | /* Indicate failure - this will cause the file object |
| 3742 | * to raise an I/O error and translate the last Win32 |
| 3743 | * error code from errno. We do have a problem with |
| 3744 | * last errors that overlap the normal errno table, |
| 3745 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3746 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3747 | if (result != EOF) { |
| 3748 | /* If the error wasn't from the fclose(), then |
| 3749 | * set errno for the file object error handling. |
| 3750 | */ |
| 3751 | errno = GetLastError(); |
| 3752 | } |
| 3753 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3754 | } |
| 3755 | |
| 3756 | /* Free up the native handle at this point */ |
| 3757 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3758 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3759 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3760 | /* Remove this file pointer from dictionary */ |
| 3761 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 3762 | |
| 3763 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 3764 | Py_DECREF(_PyPopenProcs); |
| 3765 | _PyPopenProcs = NULL; |
| 3766 | } |
| 3767 | |
| 3768 | } /* if object retrieval ok */ |
| 3769 | |
| 3770 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3771 | } /* if _PyPopenProcs */ |
| 3772 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3773 | #ifdef WITH_THREAD |
| 3774 | /* Tear down the thread & interpreter states. |
| 3775 | * Note that interpreter state clear & delete functions automatically |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 3776 | * call the thread clear & delete functions, and indeed insist on |
| 3777 | * doing that themselves. The lock must be held during the clear, but |
| 3778 | * need not be held during the delete. |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3779 | */ |
| 3780 | PyInterpreterState_Clear(pInterpreterState); |
| 3781 | PyEval_ReleaseThread(pThreadState); |
| 3782 | PyInterpreterState_Delete(pInterpreterState); |
| 3783 | #endif |
| 3784 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3785 | return result; |
| 3786 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 3787 | |
| 3788 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3789 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3790 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3791 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3792 | char *name; |
| 3793 | char *mode = "r"; |
| 3794 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3795 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3796 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3797 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3798 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3799 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3800 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3801 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3802 | if (fp == NULL) |
| 3803 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3804 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3805 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3806 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3807 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3808 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3809 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3810 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3811 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3812 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3813 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3814 | #ifdef HAVE_SETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3815 | static char posix_setuid__doc__[] = |
| 3816 | "setuid(uid) -> None\n\ |
| 3817 | Set the current process's user id."; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3818 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3819 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3820 | { |
| 3821 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3822 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3823 | return NULL; |
| 3824 | if (setuid(uid) < 0) |
| 3825 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3826 | Py_INCREF(Py_None); |
| 3827 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3828 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3829 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3830 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3831 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3832 | #ifdef HAVE_SETEUID |
| 3833 | static char posix_seteuid__doc__[] = |
| 3834 | "seteuid(uid) -> None\n\ |
| 3835 | Set the current process's effective user id."; |
| 3836 | static PyObject * |
| 3837 | posix_seteuid (PyObject *self, PyObject *args) |
| 3838 | { |
| 3839 | int euid; |
| 3840 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 3841 | return NULL; |
| 3842 | } else if (seteuid(euid) < 0) { |
| 3843 | return posix_error(); |
| 3844 | } else { |
| 3845 | Py_INCREF(Py_None); |
| 3846 | return Py_None; |
| 3847 | } |
| 3848 | } |
| 3849 | #endif /* HAVE_SETEUID */ |
| 3850 | |
| 3851 | #ifdef HAVE_SETEGID |
| 3852 | static char posix_setegid__doc__[] = |
| 3853 | "setegid(gid) -> None\n\ |
| 3854 | Set the current process's effective group id."; |
| 3855 | static PyObject * |
| 3856 | posix_setegid (PyObject *self, PyObject *args) |
| 3857 | { |
| 3858 | int egid; |
| 3859 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 3860 | return NULL; |
| 3861 | } else if (setegid(egid) < 0) { |
| 3862 | return posix_error(); |
| 3863 | } else { |
| 3864 | Py_INCREF(Py_None); |
| 3865 | return Py_None; |
| 3866 | } |
| 3867 | } |
| 3868 | #endif /* HAVE_SETEGID */ |
| 3869 | |
| 3870 | #ifdef HAVE_SETREUID |
| 3871 | static char posix_setreuid__doc__[] = |
| 3872 | "seteuid(ruid, euid) -> None\n\ |
| 3873 | Set the current process's real and effective user ids."; |
| 3874 | static PyObject * |
| 3875 | posix_setreuid (PyObject *self, PyObject *args) |
| 3876 | { |
| 3877 | int ruid, euid; |
| 3878 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 3879 | return NULL; |
| 3880 | } else if (setreuid(ruid, euid) < 0) { |
| 3881 | return posix_error(); |
| 3882 | } else { |
| 3883 | Py_INCREF(Py_None); |
| 3884 | return Py_None; |
| 3885 | } |
| 3886 | } |
| 3887 | #endif /* HAVE_SETREUID */ |
| 3888 | |
| 3889 | #ifdef HAVE_SETREGID |
| 3890 | static char posix_setregid__doc__[] = |
| 3891 | "setegid(rgid, egid) -> None\n\ |
| 3892 | Set the current process's real and effective group ids."; |
| 3893 | static PyObject * |
| 3894 | posix_setregid (PyObject *self, PyObject *args) |
| 3895 | { |
| 3896 | int rgid, egid; |
| 3897 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 3898 | return NULL; |
| 3899 | } else if (setregid(rgid, egid) < 0) { |
| 3900 | return posix_error(); |
| 3901 | } else { |
| 3902 | Py_INCREF(Py_None); |
| 3903 | return Py_None; |
| 3904 | } |
| 3905 | } |
| 3906 | #endif /* HAVE_SETREGID */ |
| 3907 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3908 | #ifdef HAVE_SETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3909 | static char posix_setgid__doc__[] = |
| 3910 | "setgid(gid) -> None\n\ |
| 3911 | Set the current process's group id."; |
| 3912 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3913 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3914 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3915 | { |
| 3916 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3917 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3918 | return NULL; |
| 3919 | if (setgid(gid) < 0) |
| 3920 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3921 | Py_INCREF(Py_None); |
| 3922 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3923 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3924 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3925 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 3926 | #ifdef HAVE_SETGROUPS |
| 3927 | static char posix_setgroups__doc__[] = |
| 3928 | "setgroups(list) -> None\n\ |
| 3929 | Set the groups of the current process to list."; |
| 3930 | |
| 3931 | static PyObject * |
| 3932 | posix_setgroups(PyObject *self, PyObject *args) |
| 3933 | { |
| 3934 | PyObject *groups; |
| 3935 | int i, len; |
| 3936 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3937 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 3938 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 3939 | return NULL; |
| 3940 | if (!PySequence_Check(groups)) { |
| 3941 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 3942 | return NULL; |
| 3943 | } |
| 3944 | len = PySequence_Size(groups); |
| 3945 | if (len > MAX_GROUPS) { |
| 3946 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 3947 | return NULL; |
| 3948 | } |
| 3949 | for(i = 0; i < len; i++) { |
| 3950 | PyObject *elem; |
| 3951 | elem = PySequence_GetItem(groups, i); |
| 3952 | if (!elem) |
| 3953 | return NULL; |
| 3954 | if (!PyInt_Check(elem)) { |
| 3955 | PyErr_SetString(PyExc_TypeError, |
| 3956 | "groups must be integers"); |
| 3957 | Py_DECREF(elem); |
| 3958 | return NULL; |
| 3959 | } |
| 3960 | /* XXX: check that value fits into gid_t. */ |
| 3961 | grouplist[i] = PyInt_AsLong(elem); |
| 3962 | Py_DECREF(elem); |
| 3963 | } |
| 3964 | |
| 3965 | if (setgroups(len, grouplist) < 0) |
| 3966 | return posix_error(); |
| 3967 | Py_INCREF(Py_None); |
| 3968 | return Py_None; |
| 3969 | } |
| 3970 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3971 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3972 | #ifdef HAVE_WAITPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3973 | static char posix_waitpid__doc__[] = |
| 3974 | "waitpid(pid, options) -> (pid, status)\n\ |
Guido van Rossum | f377d57 | 2000-12-12 00:37:58 +0000 | [diff] [blame] | 3975 | Wait for completion of a given child process."; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3976 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3977 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3978 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3979 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3980 | int pid, options; |
| 3981 | #ifdef UNION_WAIT |
| 3982 | union wait status; |
| 3983 | #define status_i (status.w_status) |
| 3984 | #else |
| 3985 | int status; |
| 3986 | #define status_i status |
| 3987 | #endif |
| 3988 | status_i = 0; |
| 3989 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3990 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3991 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3992 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 3993 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3994 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3995 | if (pid == -1) |
| 3996 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3997 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3998 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3999 | } |
| 4000 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4001 | #elif defined(HAVE_CWAIT) |
| 4002 | |
| 4003 | /* MS C has a variant of waitpid() that's usable for most purposes. */ |
| 4004 | static char posix_waitpid__doc__[] = |
| 4005 | "waitpid(pid, options) -> (pid, status << 8)\n" |
| 4006 | "Wait for completion of a given process. options is ignored on Windows."; |
| 4007 | |
| 4008 | static PyObject * |
| 4009 | posix_waitpid(PyObject *self, PyObject *args) |
| 4010 | { |
| 4011 | int pid, options; |
| 4012 | int status; |
| 4013 | |
| 4014 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4015 | return NULL; |
| 4016 | Py_BEGIN_ALLOW_THREADS |
| 4017 | pid = _cwait(&status, pid, options); |
| 4018 | Py_END_ALLOW_THREADS |
| 4019 | if (pid == -1) |
| 4020 | return posix_error(); |
| 4021 | else |
| 4022 | /* shift the status left a byte so this is more like the |
| 4023 | POSIX waitpid */ |
| 4024 | return Py_BuildValue("ii", pid, status << 8); |
| 4025 | } |
| 4026 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4027 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4028 | #ifdef HAVE_WAIT |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4029 | static char posix_wait__doc__[] = |
| 4030 | "wait() -> (pid, status)\n\ |
| 4031 | Wait for completion of a child process."; |
| 4032 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4033 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4034 | posix_wait(PyObject *self, PyObject *args) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4035 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4036 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4037 | #ifdef UNION_WAIT |
| 4038 | union wait status; |
| 4039 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4040 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4041 | int status; |
| 4042 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4043 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4044 | if (!PyArg_ParseTuple(args, ":wait")) |
| 4045 | return NULL; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4046 | status_i = 0; |
| 4047 | Py_BEGIN_ALLOW_THREADS |
| 4048 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4049 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4050 | if (pid == -1) |
| 4051 | return posix_error(); |
| 4052 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4053 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4054 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4055 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4056 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4057 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4058 | |
| 4059 | static char posix_lstat__doc__[] = |
Fred Drake | 193a3f6 | 2002-03-12 21:38:49 +0000 | [diff] [blame] | 4060 | "lstat(path) -> (st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid,\n\ |
| 4061 | st_size, st_atime, st_mtime, st_ctime)\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4062 | Like stat(path), but do not follow symbolic links."; |
| 4063 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4064 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4065 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4066 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4067 | #ifdef HAVE_LSTAT |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4068 | return posix_do_stat(self, args, "et:lstat", lstat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4069 | #else /* !HAVE_LSTAT */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4070 | return posix_do_stat(self, args, "et:lstat", STAT); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4071 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4074 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4075 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4076 | static char posix_readlink__doc__[] = |
| 4077 | "readlink(path) -> path\n\ |
| 4078 | Return a string representing the path to which the symbolic link points."; |
| 4079 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4080 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4081 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4082 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4083 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4084 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4085 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4086 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4087 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4088 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4089 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4090 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4091 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 4092 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4093 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4094 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4095 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4096 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4097 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4098 | #ifdef HAVE_SYMLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4099 | static char posix_symlink__doc__[] = |
| 4100 | "symlink(src, dst) -> None\n\ |
| 4101 | Create a symbolic link."; |
| 4102 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4103 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4104 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4105 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4106 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4107 | } |
| 4108 | #endif /* HAVE_SYMLINK */ |
| 4109 | |
| 4110 | |
| 4111 | #ifdef HAVE_TIMES |
| 4112 | #ifndef HZ |
| 4113 | #define HZ 60 /* Universal constant :-) */ |
| 4114 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4115 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4116 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4117 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4118 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4119 | { |
| 4120 | ULONG value = 0; |
| 4121 | |
| 4122 | Py_BEGIN_ALLOW_THREADS |
| 4123 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4124 | Py_END_ALLOW_THREADS |
| 4125 | |
| 4126 | return value; |
| 4127 | } |
| 4128 | |
| 4129 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4130 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4131 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4132 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4133 | return NULL; |
| 4134 | |
| 4135 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4136 | return Py_BuildValue("ddddd", |
| 4137 | (double)0 /* t.tms_utime / HZ */, |
| 4138 | (double)0 /* t.tms_stime / HZ */, |
| 4139 | (double)0 /* t.tms_cutime / HZ */, |
| 4140 | (double)0 /* t.tms_cstime / HZ */, |
| 4141 | (double)system_uptime() / 1000); |
| 4142 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4143 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4144 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4145 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4146 | { |
| 4147 | struct tms t; |
| 4148 | clock_t c; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4149 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4150 | return NULL; |
| 4151 | errno = 0; |
| 4152 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4153 | if (c == (clock_t) -1) |
| 4154 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4155 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4156 | (double)t.tms_utime / HZ, |
| 4157 | (double)t.tms_stime / HZ, |
| 4158 | (double)t.tms_cutime / HZ, |
| 4159 | (double)t.tms_cstime / HZ, |
| 4160 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4161 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4162 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4163 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4164 | |
| 4165 | |
Guido van Rossum | 87755a2 | 1996-09-07 00:59:43 +0000 | [diff] [blame] | 4166 | #ifdef MS_WIN32 |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4167 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4168 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4169 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4170 | { |
| 4171 | FILETIME create, exit, kernel, user; |
| 4172 | HANDLE hProc; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4173 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4174 | return NULL; |
| 4175 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4176 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4177 | /* The fields of a FILETIME structure are the hi and lo part |
| 4178 | of a 64-bit value expressed in 100 nanosecond units. |
| 4179 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4180 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4181 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4182 | return Py_BuildValue( |
| 4183 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4184 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4185 | kernel.dwLowDateTime*1e-7), |
| 4186 | (double)(user.dwHighDateTime*429.4967296 + |
| 4187 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4188 | (double)0, |
| 4189 | (double)0, |
| 4190 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4191 | } |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4192 | #endif /* MS_WIN32 */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4193 | |
| 4194 | #ifdef HAVE_TIMES |
Roger E. Masse | 0318fd6 | 1997-06-05 22:07:58 +0000 | [diff] [blame] | 4195 | static char posix_times__doc__[] = |
| 4196 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\ |
| 4197 | Return a tuple of floating point numbers indicating process times."; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4198 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4199 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4200 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4201 | #ifdef HAVE_SETSID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4202 | static char posix_setsid__doc__[] = |
| 4203 | "setsid() -> None\n\ |
| 4204 | Call the system call setsid()."; |
| 4205 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4206 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4207 | posix_setsid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4208 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4209 | if (!PyArg_ParseTuple(args, ":setsid")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4210 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4211 | if (setsid() < 0) |
| 4212 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4213 | Py_INCREF(Py_None); |
| 4214 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4215 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4216 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4217 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4218 | #ifdef HAVE_SETPGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4219 | static char posix_setpgid__doc__[] = |
| 4220 | "setpgid(pid, pgrp) -> None\n\ |
| 4221 | Call the system call setpgid()."; |
| 4222 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4223 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4224 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4225 | { |
| 4226 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4227 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4228 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4229 | if (setpgid(pid, pgrp) < 0) |
| 4230 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4231 | Py_INCREF(Py_None); |
| 4232 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4233 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4234 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4235 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4236 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4237 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4238 | static char posix_tcgetpgrp__doc__[] = |
| 4239 | "tcgetpgrp(fd) -> pgid\n\ |
| 4240 | Return the process group associated with the terminal given by a fd."; |
| 4241 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4242 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4243 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4244 | { |
| 4245 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4246 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4247 | return NULL; |
| 4248 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4249 | if (pgid < 0) |
| 4250 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4251 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4252 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4253 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4254 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4255 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4256 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4257 | static char posix_tcsetpgrp__doc__[] = |
| 4258 | "tcsetpgrp(fd, pgid) -> None\n\ |
| 4259 | Set the process group associated with the terminal given by a fd."; |
| 4260 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4261 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4262 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4263 | { |
| 4264 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4265 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4266 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4267 | if (tcsetpgrp(fd, pgid) < 0) |
| 4268 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4269 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4270 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4271 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4272 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4273 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4274 | /* Functions acting on file descriptors */ |
| 4275 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4276 | static char posix_open__doc__[] = |
| 4277 | "open(filename, flag [, mode=0777]) -> fd\n\ |
| 4278 | Open a file (for low level IO)."; |
| 4279 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4280 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4281 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4282 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4283 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4284 | int flag; |
| 4285 | int mode = 0777; |
| 4286 | int fd; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4287 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4288 | Py_FileSystemDefaultEncoding, &file, |
| 4289 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4290 | return NULL; |
| 4291 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4292 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4293 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4294 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4295 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4296 | return posix_error_with_allocated_filename(file); |
| 4297 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4298 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4299 | } |
| 4300 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4301 | |
| 4302 | static char posix_close__doc__[] = |
| 4303 | "close(fd) -> None\n\ |
| 4304 | Close a file descriptor (for low level IO)."; |
| 4305 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4306 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4307 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4308 | { |
| 4309 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4310 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4311 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4312 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4313 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4314 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4315 | if (res < 0) |
| 4316 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4317 | Py_INCREF(Py_None); |
| 4318 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4321 | |
| 4322 | static char posix_dup__doc__[] = |
| 4323 | "dup(fd) -> fd2\n\ |
| 4324 | Return a duplicate of a file descriptor."; |
| 4325 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4326 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4327 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4328 | { |
| 4329 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4330 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4331 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4332 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4333 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4334 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4335 | if (fd < 0) |
| 4336 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4337 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4338 | } |
| 4339 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4340 | |
| 4341 | static char posix_dup2__doc__[] = |
| 4342 | "dup2(fd, fd2) -> None\n\ |
| 4343 | Duplicate file descriptor."; |
| 4344 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4345 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4346 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4347 | { |
| 4348 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4349 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4350 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4351 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4352 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4353 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4354 | if (res < 0) |
| 4355 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4356 | Py_INCREF(Py_None); |
| 4357 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4358 | } |
| 4359 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4360 | |
| 4361 | static char posix_lseek__doc__[] = |
| 4362 | "lseek(fd, pos, how) -> newpos\n\ |
| 4363 | Set the current position of a file descriptor."; |
| 4364 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4365 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4366 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4367 | { |
| 4368 | int fd, how; |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 4369 | #if defined(MS_WIN64) || defined(MS_WIN32) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4370 | LONG_LONG pos, res; |
| 4371 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4372 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4373 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4374 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4375 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4376 | return NULL; |
| 4377 | #ifdef SEEK_SET |
| 4378 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 4379 | switch (how) { |
| 4380 | case 0: how = SEEK_SET; break; |
| 4381 | case 1: how = SEEK_CUR; break; |
| 4382 | case 2: how = SEEK_END; break; |
| 4383 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4384 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4385 | |
| 4386 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4387 | pos = PyInt_AsLong(posobj); |
| 4388 | #else |
| 4389 | pos = PyLong_Check(posobj) ? |
| 4390 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 4391 | #endif |
| 4392 | if (PyErr_Occurred()) |
| 4393 | return NULL; |
| 4394 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4395 | Py_BEGIN_ALLOW_THREADS |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 4396 | #if defined(MS_WIN64) || defined(MS_WIN32) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4397 | res = _lseeki64(fd, pos, how); |
| 4398 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4399 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4400 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4401 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4402 | if (res < 0) |
| 4403 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4404 | |
| 4405 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4406 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4407 | #else |
| 4408 | return PyLong_FromLongLong(res); |
| 4409 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4412 | |
| 4413 | static char posix_read__doc__[] = |
| 4414 | "read(fd, buffersize) -> string\n\ |
| 4415 | Read a file descriptor."; |
| 4416 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4417 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4418 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4419 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4420 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4421 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4422 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4423 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4424 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4425 | if (buffer == NULL) |
| 4426 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4427 | Py_BEGIN_ALLOW_THREADS |
| 4428 | n = read(fd, PyString_AsString(buffer), size); |
| 4429 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4430 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4431 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4432 | return posix_error(); |
| 4433 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4434 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4435 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4436 | return buffer; |
| 4437 | } |
| 4438 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4439 | |
| 4440 | static char posix_write__doc__[] = |
| 4441 | "write(fd, string) -> byteswritten\n\ |
| 4442 | Write a string to a file descriptor."; |
| 4443 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4444 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4445 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4446 | { |
| 4447 | int fd, size; |
| 4448 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4449 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4450 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4451 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4452 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4453 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4454 | if (size < 0) |
| 4455 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4456 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4457 | } |
| 4458 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4459 | |
| 4460 | static char posix_fstat__doc__[]= |
| 4461 | "fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
| 4462 | Like stat(), but for an open file descriptor."; |
| 4463 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4464 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4465 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4466 | { |
| 4467 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4468 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4469 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4470 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4471 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4472 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4473 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4474 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4475 | if (res != 0) |
| 4476 | return posix_error(); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4477 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4478 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4481 | |
| 4482 | static char posix_fdopen__doc__[] = |
| 4483 | "fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\ |
| 4484 | Return an open file object connected to a file descriptor."; |
| 4485 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4486 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4487 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4488 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4489 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4490 | char *mode = "r"; |
| 4491 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4492 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4493 | PyObject *f; |
| 4494 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4495 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4496 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4497 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4498 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4499 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4500 | if (fp == NULL) |
| 4501 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4502 | f = PyFile_FromFile(fp, "(fdopen)", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4503 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4504 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4505 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4506 | } |
| 4507 | |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4508 | static char posix_isatty__doc__[] = |
| 4509 | "isatty(fd) -> Boolean\n\ |
| 4510 | Return true if the file descriptor 'fd' is an open file descriptor\n\ |
Thomas Wouters | 12e1595 | 2000-10-03 16:54:24 +0000 | [diff] [blame] | 4511 | connected to the slave end of a terminal."; |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4512 | |
| 4513 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 4514 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4515 | { |
| 4516 | int fd; |
| 4517 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 4518 | return NULL; |
| 4519 | return Py_BuildValue("i", isatty(fd)); |
| 4520 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4521 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4522 | #ifdef HAVE_PIPE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4523 | static char posix_pipe__doc__[] = |
| 4524 | "pipe() -> (read_end, write_end)\n\ |
| 4525 | Create a pipe."; |
| 4526 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4527 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4528 | posix_pipe(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4529 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4530 | #if defined(PYOS_OS2) |
| 4531 | HFILE read, write; |
| 4532 | APIRET rc; |
| 4533 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4534 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4535 | return NULL; |
| 4536 | |
| 4537 | Py_BEGIN_ALLOW_THREADS |
| 4538 | rc = DosCreatePipe( &read, &write, 4096); |
| 4539 | Py_END_ALLOW_THREADS |
| 4540 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4541 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4542 | |
| 4543 | return Py_BuildValue("(ii)", read, write); |
| 4544 | #else |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4545 | #if !defined(MS_WIN32) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4546 | int fds[2]; |
| 4547 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4548 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4549 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4550 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4551 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4552 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4553 | if (res != 0) |
| 4554 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4555 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4556 | #else /* MS_WIN32 */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4557 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4558 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4559 | BOOL ok; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4560 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4561 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4562 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4563 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4564 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4565 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4566 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 4567 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 4568 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4569 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4570 | #endif /* MS_WIN32 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4571 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4572 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4573 | #endif /* HAVE_PIPE */ |
| 4574 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4575 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4576 | #ifdef HAVE_MKFIFO |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4577 | static char posix_mkfifo__doc__[] = |
| 4578 | "mkfifo(file, [, mode=0666]) -> None\n\ |
| 4579 | Create a FIFO (a POSIX named pipe)."; |
| 4580 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4581 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4582 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4583 | { |
| 4584 | char *file; |
| 4585 | int mode = 0666; |
| 4586 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4587 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4588 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4589 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4590 | res = mkfifo(file, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4591 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4592 | if (res < 0) |
| 4593 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4594 | Py_INCREF(Py_None); |
| 4595 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4596 | } |
| 4597 | #endif |
| 4598 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4599 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4600 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4601 | static char posix_ftruncate__doc__[] = |
| 4602 | "ftruncate(fd, length) -> None\n\ |
| 4603 | Truncate a file to a specified length."; |
| 4604 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4605 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4606 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4607 | { |
| 4608 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4609 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4610 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4611 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4612 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4613 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4614 | return NULL; |
| 4615 | |
| 4616 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4617 | length = PyInt_AsLong(lenobj); |
| 4618 | #else |
| 4619 | length = PyLong_Check(lenobj) ? |
| 4620 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 4621 | #endif |
| 4622 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4623 | return NULL; |
| 4624 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4625 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4626 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4627 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4628 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4629 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4630 | return NULL; |
| 4631 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4632 | Py_INCREF(Py_None); |
| 4633 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4634 | } |
| 4635 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4636 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4637 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4638 | static char posix_putenv__doc__[] = |
| 4639 | "putenv(key, value) -> None\n\ |
| 4640 | Change or add an environment variable."; |
| 4641 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4642 | /* Save putenv() parameters as values here, so we can collect them when they |
| 4643 | * get re-set with another call for the same key. */ |
| 4644 | static PyObject *posix_putenv_garbage; |
| 4645 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4646 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4647 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4648 | { |
| 4649 | char *s1, *s2; |
| 4650 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4651 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4652 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4653 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4654 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4655 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4656 | |
| 4657 | #if defined(PYOS_OS2) |
| 4658 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 4659 | APIRET rc; |
| 4660 | |
| 4661 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 4662 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 4663 | |
| 4664 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 4665 | if (rc != NO_ERROR) |
| 4666 | return os2_error(rc); |
| 4667 | |
| 4668 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 4669 | APIRET rc; |
| 4670 | |
| 4671 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 4672 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 4673 | |
| 4674 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 4675 | if (rc != NO_ERROR) |
| 4676 | return os2_error(rc); |
| 4677 | } else { |
| 4678 | #endif |
| 4679 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4680 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4681 | len = strlen(s1) + strlen(s2) + 2; |
| 4682 | /* len includes space for a trailing \0; the size arg to |
| 4683 | PyString_FromStringAndSize does not count that */ |
| 4684 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4685 | if (newstr == NULL) |
| 4686 | return PyErr_NoMemory(); |
| 4687 | new = PyString_AS_STRING(newstr); |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4688 | PyOS_snprintf(new, len, "%s=%s", s1, s2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4689 | if (putenv(new)) { |
| 4690 | posix_error(); |
| 4691 | return NULL; |
| 4692 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4693 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 4694 | * this will cause previous value to be collected. This has to |
| 4695 | * happen after the real putenv() call because the old value |
| 4696 | * was still accessible until then. */ |
| 4697 | if (PyDict_SetItem(posix_putenv_garbage, |
| 4698 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 4699 | /* really not much we can do; just leak */ |
| 4700 | PyErr_Clear(); |
| 4701 | } |
| 4702 | else { |
| 4703 | Py_DECREF(newstr); |
| 4704 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4705 | |
| 4706 | #if defined(PYOS_OS2) |
| 4707 | } |
| 4708 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4709 | Py_INCREF(Py_None); |
| 4710 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4711 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4712 | #endif /* putenv */ |
| 4713 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 4714 | #ifdef HAVE_UNSETENV |
| 4715 | static char posix_unsetenv__doc__[] = |
| 4716 | "unsetenv(key) -> None\n\ |
| 4717 | Delete an environment variable."; |
| 4718 | |
| 4719 | static PyObject * |
| 4720 | posix_unsetenv(PyObject *self, PyObject *args) |
| 4721 | { |
| 4722 | char *s1; |
| 4723 | |
| 4724 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 4725 | return NULL; |
| 4726 | |
| 4727 | unsetenv(s1); |
| 4728 | |
| 4729 | /* Remove the key from posix_putenv_garbage; |
| 4730 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4731 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 4732 | * old value was still accessible until then. |
| 4733 | */ |
| 4734 | if (PyDict_DelItem(posix_putenv_garbage, |
| 4735 | PyTuple_GET_ITEM(args, 0))) { |
| 4736 | /* really not much we can do; just leak */ |
| 4737 | PyErr_Clear(); |
| 4738 | } |
| 4739 | |
| 4740 | Py_INCREF(Py_None); |
| 4741 | return Py_None; |
| 4742 | } |
| 4743 | #endif /* unsetenv */ |
| 4744 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4745 | #ifdef HAVE_STRERROR |
| 4746 | static char posix_strerror__doc__[] = |
| 4747 | "strerror(code) -> string\n\ |
| 4748 | Translate an error code to a message string."; |
| 4749 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 4750 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4751 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4752 | { |
| 4753 | int code; |
| 4754 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4755 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4756 | return NULL; |
| 4757 | message = strerror(code); |
| 4758 | if (message == NULL) { |
| 4759 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4760 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4761 | return NULL; |
| 4762 | } |
| 4763 | return PyString_FromString(message); |
| 4764 | } |
| 4765 | #endif /* strerror */ |
| 4766 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4767 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4768 | #ifdef HAVE_SYS_WAIT_H |
| 4769 | |
| 4770 | #ifdef WIFSTOPPED |
| 4771 | static char posix_WIFSTOPPED__doc__[] = |
| 4772 | "WIFSTOPPED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4773 | Return true if the process returning 'status' was stopped."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4774 | |
| 4775 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4776 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4777 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4778 | #ifdef UNION_WAIT |
| 4779 | union wait status; |
| 4780 | #define status_i (status.w_status) |
| 4781 | #else |
| 4782 | int status; |
| 4783 | #define status_i status |
| 4784 | #endif |
| 4785 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4786 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4787 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4788 | { |
| 4789 | return NULL; |
| 4790 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4791 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4792 | return Py_BuildValue("i", WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4793 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4794 | } |
| 4795 | #endif /* WIFSTOPPED */ |
| 4796 | |
| 4797 | #ifdef WIFSIGNALED |
| 4798 | static char posix_WIFSIGNALED__doc__[] = |
| 4799 | "WIFSIGNALED(status) -> Boolean\n\ |
Guido van Rossum | 3366d1c | 1999-02-23 18:34:43 +0000 | [diff] [blame] | 4800 | 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] | 4801 | |
| 4802 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4803 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4804 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4805 | #ifdef UNION_WAIT |
| 4806 | union wait status; |
| 4807 | #define status_i (status.w_status) |
| 4808 | #else |
| 4809 | int status; |
| 4810 | #define status_i status |
| 4811 | #endif |
| 4812 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4813 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4814 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4815 | { |
| 4816 | return NULL; |
| 4817 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4818 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4819 | return Py_BuildValue("i", WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4820 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4821 | } |
| 4822 | #endif /* WIFSIGNALED */ |
| 4823 | |
| 4824 | #ifdef WIFEXITED |
| 4825 | static char posix_WIFEXITED__doc__[] = |
| 4826 | "WIFEXITED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4827 | Return true if the process returning 'status' exited using the exit()\n\ |
| 4828 | system call."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4829 | |
| 4830 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4831 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4832 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4833 | #ifdef UNION_WAIT |
| 4834 | union wait status; |
| 4835 | #define status_i (status.w_status) |
| 4836 | #else |
| 4837 | int status; |
| 4838 | #define status_i status |
| 4839 | #endif |
| 4840 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4841 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4842 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4843 | { |
| 4844 | return NULL; |
| 4845 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4846 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4847 | return Py_BuildValue("i", WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4848 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4849 | } |
| 4850 | #endif /* WIFEXITED */ |
| 4851 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4852 | #ifdef WEXITSTATUS |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4853 | static char posix_WEXITSTATUS__doc__[] = |
| 4854 | "WEXITSTATUS(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4855 | Return the process return code from 'status'."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4856 | |
| 4857 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4858 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4859 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4860 | #ifdef UNION_WAIT |
| 4861 | union wait status; |
| 4862 | #define status_i (status.w_status) |
| 4863 | #else |
| 4864 | int status; |
| 4865 | #define status_i status |
| 4866 | #endif |
| 4867 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4868 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4869 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4870 | { |
| 4871 | return NULL; |
| 4872 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4873 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4874 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4875 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4876 | } |
| 4877 | #endif /* WEXITSTATUS */ |
| 4878 | |
| 4879 | #ifdef WTERMSIG |
| 4880 | static char posix_WTERMSIG__doc__[] = |
| 4881 | "WTERMSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4882 | Return the signal that terminated the process that provided the 'status'\n\ |
| 4883 | value."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4884 | |
| 4885 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4886 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4887 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4888 | #ifdef UNION_WAIT |
| 4889 | union wait status; |
| 4890 | #define status_i (status.w_status) |
| 4891 | #else |
| 4892 | int status; |
| 4893 | #define status_i status |
| 4894 | #endif |
| 4895 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4896 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4897 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4898 | { |
| 4899 | return NULL; |
| 4900 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4901 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4902 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4903 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4904 | } |
| 4905 | #endif /* WTERMSIG */ |
| 4906 | |
| 4907 | #ifdef WSTOPSIG |
| 4908 | static char posix_WSTOPSIG__doc__[] = |
| 4909 | "WSTOPSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4910 | Return the signal that stopped the process that provided the 'status' value."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4911 | |
| 4912 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4913 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4914 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4915 | #ifdef UNION_WAIT |
| 4916 | union wait status; |
| 4917 | #define status_i (status.w_status) |
| 4918 | #else |
| 4919 | int status; |
| 4920 | #define status_i status |
| 4921 | #endif |
| 4922 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4923 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4924 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4925 | { |
| 4926 | return NULL; |
| 4927 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4928 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4929 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4930 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4931 | } |
| 4932 | #endif /* WSTOPSIG */ |
| 4933 | |
| 4934 | #endif /* HAVE_SYS_WAIT_H */ |
| 4935 | |
| 4936 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4937 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 4938 | #ifdef _SCO_DS |
| 4939 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 4940 | needed definitions in sys/statvfs.h */ |
| 4941 | #define _SVID3 |
| 4942 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4943 | #include <sys/statvfs.h> |
| 4944 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4945 | static PyObject* |
| 4946 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 4947 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 4948 | if (v == NULL) |
| 4949 | return NULL; |
| 4950 | |
| 4951 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4952 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 4953 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 4954 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 4955 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 4956 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 4957 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 4958 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 4959 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 4960 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 4961 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 4962 | #else |
| 4963 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 4964 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4965 | PyStructSequence_SET_ITEM(v, 2, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4966 | PyLong_FromLongLong((LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4967 | PyStructSequence_SET_ITEM(v, 3, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4968 | PyLong_FromLongLong((LONG_LONG) st.f_bfree)); |
| 4969 | PyStructSequence_SET_ITEM(v, 4, |
| 4970 | PyLong_FromLongLong((LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4971 | PyStructSequence_SET_ITEM(v, 5, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4972 | PyLong_FromLongLong((LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4973 | PyStructSequence_SET_ITEM(v, 6, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4974 | PyLong_FromLongLong((LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4975 | PyStructSequence_SET_ITEM(v, 7, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4976 | PyLong_FromLongLong((LONG_LONG) st.f_favail)); |
| 4977 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 4978 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 4979 | #endif |
| 4980 | |
| 4981 | return v; |
| 4982 | } |
| 4983 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4984 | static char posix_fstatvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4985 | "fstatvfs(fd) -> \n\ |
| 4986 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4987 | Perform an fstatvfs system call on the given fd."; |
| 4988 | |
| 4989 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4990 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4991 | { |
| 4992 | int fd, res; |
| 4993 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4994 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4995 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4996 | return NULL; |
| 4997 | Py_BEGIN_ALLOW_THREADS |
| 4998 | res = fstatvfs(fd, &st); |
| 4999 | Py_END_ALLOW_THREADS |
| 5000 | if (res != 0) |
| 5001 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5002 | |
| 5003 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5004 | } |
| 5005 | #endif /* HAVE_FSTATVFS */ |
| 5006 | |
| 5007 | |
| 5008 | #if defined(HAVE_STATVFS) |
| 5009 | #include <sys/statvfs.h> |
| 5010 | |
| 5011 | static char posix_statvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 5012 | "statvfs(path) -> \n\ |
| 5013 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5014 | Perform a statvfs system call on the given path."; |
| 5015 | |
| 5016 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5017 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5018 | { |
| 5019 | char *path; |
| 5020 | int res; |
| 5021 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5022 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5023 | return NULL; |
| 5024 | Py_BEGIN_ALLOW_THREADS |
| 5025 | res = statvfs(path, &st); |
| 5026 | Py_END_ALLOW_THREADS |
| 5027 | if (res != 0) |
| 5028 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5029 | |
| 5030 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5031 | } |
| 5032 | #endif /* HAVE_STATVFS */ |
| 5033 | |
| 5034 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5035 | #ifdef HAVE_TEMPNAM |
| 5036 | static char posix_tempnam__doc__[] = "\ |
| 5037 | tempnam([dir[, prefix]]) -> string\n\ |
| 5038 | Return a unique name for a temporary file.\n\ |
| 5039 | The directory and a short may be specified as strings; they may be omitted\n\ |
| 5040 | or None if not needed."; |
| 5041 | |
| 5042 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5043 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5044 | { |
| 5045 | PyObject *result = NULL; |
| 5046 | char *dir = NULL; |
| 5047 | char *pfx = NULL; |
| 5048 | char *name; |
| 5049 | |
| 5050 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 5051 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5052 | |
| 5053 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5054 | "tempnam is a potential security risk to your program") < 0) |
| 5055 | return NULL; |
| 5056 | |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5057 | #ifdef MS_WIN32 |
| 5058 | name = _tempnam(dir, pfx); |
| 5059 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5060 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5061 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5062 | if (name == NULL) |
| 5063 | return PyErr_NoMemory(); |
| 5064 | result = PyString_FromString(name); |
| 5065 | free(name); |
| 5066 | return result; |
| 5067 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 5068 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5069 | |
| 5070 | |
| 5071 | #ifdef HAVE_TMPFILE |
| 5072 | static char posix_tmpfile__doc__[] = "\ |
| 5073 | tmpfile() -> file object\n\ |
| 5074 | Create a temporary file with no directory entries."; |
| 5075 | |
| 5076 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5077 | posix_tmpfile(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5078 | { |
| 5079 | FILE *fp; |
| 5080 | |
| 5081 | if (!PyArg_ParseTuple(args, ":tmpfile")) |
| 5082 | return NULL; |
| 5083 | fp = tmpfile(); |
| 5084 | if (fp == NULL) |
| 5085 | return posix_error(); |
| 5086 | return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose); |
| 5087 | } |
| 5088 | #endif |
| 5089 | |
| 5090 | |
| 5091 | #ifdef HAVE_TMPNAM |
| 5092 | static char posix_tmpnam__doc__[] = "\ |
| 5093 | tmpnam() -> string\n\ |
| 5094 | Return a unique name for a temporary file."; |
| 5095 | |
| 5096 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5097 | posix_tmpnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5098 | { |
| 5099 | char buffer[L_tmpnam]; |
| 5100 | char *name; |
| 5101 | |
| 5102 | if (!PyArg_ParseTuple(args, ":tmpnam")) |
| 5103 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5104 | |
| 5105 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5106 | "tmpnam is a potential security risk to your program") < 0) |
| 5107 | return NULL; |
| 5108 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5109 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5110 | name = tmpnam_r(buffer); |
| 5111 | #else |
| 5112 | name = tmpnam(buffer); |
| 5113 | #endif |
| 5114 | if (name == NULL) { |
| 5115 | PyErr_SetObject(PyExc_OSError, |
| 5116 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5117 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5118 | "unexpected NULL from tmpnam_r" |
| 5119 | #else |
| 5120 | "unexpected NULL from tmpnam" |
| 5121 | #endif |
| 5122 | )); |
| 5123 | return NULL; |
| 5124 | } |
| 5125 | return PyString_FromString(buffer); |
| 5126 | } |
| 5127 | #endif |
| 5128 | |
| 5129 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5130 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5131 | * It maps strings representing configuration variable names to |
| 5132 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5133 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5134 | * rarely-used constants. There are three separate tables that use |
| 5135 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5136 | * |
| 5137 | * This code is always included, even if none of the interfaces that |
| 5138 | * need it are included. The #if hackery needed to avoid it would be |
| 5139 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5140 | */ |
| 5141 | struct constdef { |
| 5142 | char *name; |
| 5143 | long value; |
| 5144 | }; |
| 5145 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5146 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5147 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 5148 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5149 | { |
| 5150 | if (PyInt_Check(arg)) { |
| 5151 | *valuep = PyInt_AS_LONG(arg); |
| 5152 | return 1; |
| 5153 | } |
| 5154 | if (PyString_Check(arg)) { |
| 5155 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5156 | size_t lo = 0; |
| 5157 | size_t mid; |
| 5158 | size_t hi = tablesize; |
| 5159 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5160 | char *confname = PyString_AS_STRING(arg); |
| 5161 | while (lo < hi) { |
| 5162 | mid = (lo + hi) / 2; |
| 5163 | cmp = strcmp(confname, table[mid].name); |
| 5164 | if (cmp < 0) |
| 5165 | hi = mid; |
| 5166 | else if (cmp > 0) |
| 5167 | lo = mid + 1; |
| 5168 | else { |
| 5169 | *valuep = table[mid].value; |
| 5170 | return 1; |
| 5171 | } |
| 5172 | } |
| 5173 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 5174 | } |
| 5175 | else |
| 5176 | PyErr_SetString(PyExc_TypeError, |
| 5177 | "configuration names must be strings or integers"); |
| 5178 | return 0; |
| 5179 | } |
| 5180 | |
| 5181 | |
| 5182 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5183 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5184 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5185 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5186 | #endif |
| 5187 | #ifdef _PC_ABI_ASYNC_IO |
| 5188 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5189 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5190 | #ifdef _PC_ASYNC_IO |
| 5191 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5192 | #endif |
| 5193 | #ifdef _PC_CHOWN_RESTRICTED |
| 5194 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5195 | #endif |
| 5196 | #ifdef _PC_FILESIZEBITS |
| 5197 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5198 | #endif |
| 5199 | #ifdef _PC_LAST |
| 5200 | {"PC_LAST", _PC_LAST}, |
| 5201 | #endif |
| 5202 | #ifdef _PC_LINK_MAX |
| 5203 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5204 | #endif |
| 5205 | #ifdef _PC_MAX_CANON |
| 5206 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5207 | #endif |
| 5208 | #ifdef _PC_MAX_INPUT |
| 5209 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5210 | #endif |
| 5211 | #ifdef _PC_NAME_MAX |
| 5212 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5213 | #endif |
| 5214 | #ifdef _PC_NO_TRUNC |
| 5215 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5216 | #endif |
| 5217 | #ifdef _PC_PATH_MAX |
| 5218 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5219 | #endif |
| 5220 | #ifdef _PC_PIPE_BUF |
| 5221 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5222 | #endif |
| 5223 | #ifdef _PC_PRIO_IO |
| 5224 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5225 | #endif |
| 5226 | #ifdef _PC_SOCK_MAXBUF |
| 5227 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5228 | #endif |
| 5229 | #ifdef _PC_SYNC_IO |
| 5230 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5231 | #endif |
| 5232 | #ifdef _PC_VDISABLE |
| 5233 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5234 | #endif |
| 5235 | }; |
| 5236 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5237 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5238 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5239 | { |
| 5240 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5241 | sizeof(posix_constants_pathconf) |
| 5242 | / sizeof(struct constdef)); |
| 5243 | } |
| 5244 | #endif |
| 5245 | |
| 5246 | #ifdef HAVE_FPATHCONF |
| 5247 | static char posix_fpathconf__doc__[] = "\ |
| 5248 | fpathconf(fd, name) -> integer\n\ |
| 5249 | Return the configuration limit name for the file descriptor fd.\n\ |
| 5250 | If there is no limit, return -1."; |
| 5251 | |
| 5252 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5253 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5254 | { |
| 5255 | PyObject *result = NULL; |
| 5256 | int name, fd; |
| 5257 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5258 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5259 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5260 | long limit; |
| 5261 | |
| 5262 | errno = 0; |
| 5263 | limit = fpathconf(fd, name); |
| 5264 | if (limit == -1 && errno != 0) |
| 5265 | posix_error(); |
| 5266 | else |
| 5267 | result = PyInt_FromLong(limit); |
| 5268 | } |
| 5269 | return result; |
| 5270 | } |
| 5271 | #endif |
| 5272 | |
| 5273 | |
| 5274 | #ifdef HAVE_PATHCONF |
| 5275 | static char posix_pathconf__doc__[] = "\ |
| 5276 | pathconf(path, name) -> integer\n\ |
| 5277 | Return the configuration limit name for the file or directory path.\n\ |
| 5278 | If there is no limit, return -1."; |
| 5279 | |
| 5280 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5281 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5282 | { |
| 5283 | PyObject *result = NULL; |
| 5284 | int name; |
| 5285 | char *path; |
| 5286 | |
| 5287 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5288 | conv_path_confname, &name)) { |
| 5289 | long limit; |
| 5290 | |
| 5291 | errno = 0; |
| 5292 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5293 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5294 | if (errno == EINVAL) |
| 5295 | /* could be a path or name problem */ |
| 5296 | posix_error(); |
| 5297 | else |
| 5298 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5299 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5300 | else |
| 5301 | result = PyInt_FromLong(limit); |
| 5302 | } |
| 5303 | return result; |
| 5304 | } |
| 5305 | #endif |
| 5306 | |
| 5307 | #ifdef HAVE_CONFSTR |
| 5308 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5309 | #ifdef _CS_ARCHITECTURE |
| 5310 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 5311 | #endif |
| 5312 | #ifdef _CS_HOSTNAME |
| 5313 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 5314 | #endif |
| 5315 | #ifdef _CS_HW_PROVIDER |
| 5316 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 5317 | #endif |
| 5318 | #ifdef _CS_HW_SERIAL |
| 5319 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 5320 | #endif |
| 5321 | #ifdef _CS_INITTAB_NAME |
| 5322 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 5323 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5324 | #ifdef _CS_LFS64_CFLAGS |
| 5325 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 5326 | #endif |
| 5327 | #ifdef _CS_LFS64_LDFLAGS |
| 5328 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 5329 | #endif |
| 5330 | #ifdef _CS_LFS64_LIBS |
| 5331 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 5332 | #endif |
| 5333 | #ifdef _CS_LFS64_LINTFLAGS |
| 5334 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 5335 | #endif |
| 5336 | #ifdef _CS_LFS_CFLAGS |
| 5337 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 5338 | #endif |
| 5339 | #ifdef _CS_LFS_LDFLAGS |
| 5340 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 5341 | #endif |
| 5342 | #ifdef _CS_LFS_LIBS |
| 5343 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 5344 | #endif |
| 5345 | #ifdef _CS_LFS_LINTFLAGS |
| 5346 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 5347 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5348 | #ifdef _CS_MACHINE |
| 5349 | {"CS_MACHINE", _CS_MACHINE}, |
| 5350 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5351 | #ifdef _CS_PATH |
| 5352 | {"CS_PATH", _CS_PATH}, |
| 5353 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5354 | #ifdef _CS_RELEASE |
| 5355 | {"CS_RELEASE", _CS_RELEASE}, |
| 5356 | #endif |
| 5357 | #ifdef _CS_SRPC_DOMAIN |
| 5358 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 5359 | #endif |
| 5360 | #ifdef _CS_SYSNAME |
| 5361 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 5362 | #endif |
| 5363 | #ifdef _CS_VERSION |
| 5364 | {"CS_VERSION", _CS_VERSION}, |
| 5365 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5366 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 5367 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 5368 | #endif |
| 5369 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 5370 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 5371 | #endif |
| 5372 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 5373 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 5374 | #endif |
| 5375 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 5376 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 5377 | #endif |
| 5378 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 5379 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 5380 | #endif |
| 5381 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 5382 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 5383 | #endif |
| 5384 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 5385 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 5386 | #endif |
| 5387 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 5388 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 5389 | #endif |
| 5390 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 5391 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 5392 | #endif |
| 5393 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 5394 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 5395 | #endif |
| 5396 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 5397 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 5398 | #endif |
| 5399 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 5400 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 5401 | #endif |
| 5402 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 5403 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 5404 | #endif |
| 5405 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 5406 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 5407 | #endif |
| 5408 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 5409 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 5410 | #endif |
| 5411 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 5412 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 5413 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5414 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 5415 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 5416 | #endif |
| 5417 | #ifdef _MIPS_CS_BASE |
| 5418 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 5419 | #endif |
| 5420 | #ifdef _MIPS_CS_HOSTID |
| 5421 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 5422 | #endif |
| 5423 | #ifdef _MIPS_CS_HW_NAME |
| 5424 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 5425 | #endif |
| 5426 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 5427 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 5428 | #endif |
| 5429 | #ifdef _MIPS_CS_OSREL_MAJ |
| 5430 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 5431 | #endif |
| 5432 | #ifdef _MIPS_CS_OSREL_MIN |
| 5433 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 5434 | #endif |
| 5435 | #ifdef _MIPS_CS_OSREL_PATCH |
| 5436 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 5437 | #endif |
| 5438 | #ifdef _MIPS_CS_OS_NAME |
| 5439 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 5440 | #endif |
| 5441 | #ifdef _MIPS_CS_OS_PROVIDER |
| 5442 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 5443 | #endif |
| 5444 | #ifdef _MIPS_CS_PROCESSORS |
| 5445 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 5446 | #endif |
| 5447 | #ifdef _MIPS_CS_SERIAL |
| 5448 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 5449 | #endif |
| 5450 | #ifdef _MIPS_CS_VENDOR |
| 5451 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 5452 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5453 | }; |
| 5454 | |
| 5455 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5456 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5457 | { |
| 5458 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 5459 | sizeof(posix_constants_confstr) |
| 5460 | / sizeof(struct constdef)); |
| 5461 | } |
| 5462 | |
| 5463 | static char posix_confstr__doc__[] = "\ |
| 5464 | confstr(name) -> string\n\ |
| 5465 | Return a string-valued system configuration variable."; |
| 5466 | |
| 5467 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5468 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5469 | { |
| 5470 | PyObject *result = NULL; |
| 5471 | int name; |
| 5472 | char buffer[64]; |
| 5473 | |
| 5474 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 5475 | int len = confstr(name, buffer, sizeof(buffer)); |
| 5476 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5477 | errno = 0; |
| 5478 | if (len == 0) { |
| 5479 | if (errno != 0) |
| 5480 | posix_error(); |
| 5481 | else |
| 5482 | result = PyString_FromString(""); |
| 5483 | } |
| 5484 | else { |
| 5485 | if (len >= sizeof(buffer)) { |
| 5486 | result = PyString_FromStringAndSize(NULL, len); |
| 5487 | if (result != NULL) |
| 5488 | confstr(name, PyString_AS_STRING(result), len+1); |
| 5489 | } |
| 5490 | else |
| 5491 | result = PyString_FromString(buffer); |
| 5492 | } |
| 5493 | } |
| 5494 | return result; |
| 5495 | } |
| 5496 | #endif |
| 5497 | |
| 5498 | |
| 5499 | #ifdef HAVE_SYSCONF |
| 5500 | static struct constdef posix_constants_sysconf[] = { |
| 5501 | #ifdef _SC_2_CHAR_TERM |
| 5502 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 5503 | #endif |
| 5504 | #ifdef _SC_2_C_BIND |
| 5505 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 5506 | #endif |
| 5507 | #ifdef _SC_2_C_DEV |
| 5508 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 5509 | #endif |
| 5510 | #ifdef _SC_2_C_VERSION |
| 5511 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 5512 | #endif |
| 5513 | #ifdef _SC_2_FORT_DEV |
| 5514 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 5515 | #endif |
| 5516 | #ifdef _SC_2_FORT_RUN |
| 5517 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 5518 | #endif |
| 5519 | #ifdef _SC_2_LOCALEDEF |
| 5520 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 5521 | #endif |
| 5522 | #ifdef _SC_2_SW_DEV |
| 5523 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 5524 | #endif |
| 5525 | #ifdef _SC_2_UPE |
| 5526 | {"SC_2_UPE", _SC_2_UPE}, |
| 5527 | #endif |
| 5528 | #ifdef _SC_2_VERSION |
| 5529 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 5530 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5531 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 5532 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 5533 | #endif |
| 5534 | #ifdef _SC_ACL |
| 5535 | {"SC_ACL", _SC_ACL}, |
| 5536 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5537 | #ifdef _SC_AIO_LISTIO_MAX |
| 5538 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 5539 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5540 | #ifdef _SC_AIO_MAX |
| 5541 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 5542 | #endif |
| 5543 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 5544 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 5545 | #endif |
| 5546 | #ifdef _SC_ARG_MAX |
| 5547 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 5548 | #endif |
| 5549 | #ifdef _SC_ASYNCHRONOUS_IO |
| 5550 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 5551 | #endif |
| 5552 | #ifdef _SC_ATEXIT_MAX |
| 5553 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 5554 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5555 | #ifdef _SC_AUDIT |
| 5556 | {"SC_AUDIT", _SC_AUDIT}, |
| 5557 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5558 | #ifdef _SC_AVPHYS_PAGES |
| 5559 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 5560 | #endif |
| 5561 | #ifdef _SC_BC_BASE_MAX |
| 5562 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 5563 | #endif |
| 5564 | #ifdef _SC_BC_DIM_MAX |
| 5565 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 5566 | #endif |
| 5567 | #ifdef _SC_BC_SCALE_MAX |
| 5568 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 5569 | #endif |
| 5570 | #ifdef _SC_BC_STRING_MAX |
| 5571 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 5572 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5573 | #ifdef _SC_CAP |
| 5574 | {"SC_CAP", _SC_CAP}, |
| 5575 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5576 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 5577 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 5578 | #endif |
| 5579 | #ifdef _SC_CHAR_BIT |
| 5580 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 5581 | #endif |
| 5582 | #ifdef _SC_CHAR_MAX |
| 5583 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 5584 | #endif |
| 5585 | #ifdef _SC_CHAR_MIN |
| 5586 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 5587 | #endif |
| 5588 | #ifdef _SC_CHILD_MAX |
| 5589 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 5590 | #endif |
| 5591 | #ifdef _SC_CLK_TCK |
| 5592 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 5593 | #endif |
| 5594 | #ifdef _SC_COHER_BLKSZ |
| 5595 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 5596 | #endif |
| 5597 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 5598 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 5599 | #endif |
| 5600 | #ifdef _SC_DCACHE_ASSOC |
| 5601 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 5602 | #endif |
| 5603 | #ifdef _SC_DCACHE_BLKSZ |
| 5604 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 5605 | #endif |
| 5606 | #ifdef _SC_DCACHE_LINESZ |
| 5607 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 5608 | #endif |
| 5609 | #ifdef _SC_DCACHE_SZ |
| 5610 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 5611 | #endif |
| 5612 | #ifdef _SC_DCACHE_TBLKSZ |
| 5613 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 5614 | #endif |
| 5615 | #ifdef _SC_DELAYTIMER_MAX |
| 5616 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 5617 | #endif |
| 5618 | #ifdef _SC_EQUIV_CLASS_MAX |
| 5619 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 5620 | #endif |
| 5621 | #ifdef _SC_EXPR_NEST_MAX |
| 5622 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 5623 | #endif |
| 5624 | #ifdef _SC_FSYNC |
| 5625 | {"SC_FSYNC", _SC_FSYNC}, |
| 5626 | #endif |
| 5627 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 5628 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 5629 | #endif |
| 5630 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 5631 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 5632 | #endif |
| 5633 | #ifdef _SC_ICACHE_ASSOC |
| 5634 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 5635 | #endif |
| 5636 | #ifdef _SC_ICACHE_BLKSZ |
| 5637 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 5638 | #endif |
| 5639 | #ifdef _SC_ICACHE_LINESZ |
| 5640 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 5641 | #endif |
| 5642 | #ifdef _SC_ICACHE_SZ |
| 5643 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 5644 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5645 | #ifdef _SC_INF |
| 5646 | {"SC_INF", _SC_INF}, |
| 5647 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5648 | #ifdef _SC_INT_MAX |
| 5649 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 5650 | #endif |
| 5651 | #ifdef _SC_INT_MIN |
| 5652 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 5653 | #endif |
| 5654 | #ifdef _SC_IOV_MAX |
| 5655 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 5656 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5657 | #ifdef _SC_IP_SECOPTS |
| 5658 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 5659 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5660 | #ifdef _SC_JOB_CONTROL |
| 5661 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 5662 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5663 | #ifdef _SC_KERN_POINTERS |
| 5664 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 5665 | #endif |
| 5666 | #ifdef _SC_KERN_SIM |
| 5667 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 5668 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5669 | #ifdef _SC_LINE_MAX |
| 5670 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 5671 | #endif |
| 5672 | #ifdef _SC_LOGIN_NAME_MAX |
| 5673 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 5674 | #endif |
| 5675 | #ifdef _SC_LOGNAME_MAX |
| 5676 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 5677 | #endif |
| 5678 | #ifdef _SC_LONG_BIT |
| 5679 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 5680 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5681 | #ifdef _SC_MAC |
| 5682 | {"SC_MAC", _SC_MAC}, |
| 5683 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5684 | #ifdef _SC_MAPPED_FILES |
| 5685 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 5686 | #endif |
| 5687 | #ifdef _SC_MAXPID |
| 5688 | {"SC_MAXPID", _SC_MAXPID}, |
| 5689 | #endif |
| 5690 | #ifdef _SC_MB_LEN_MAX |
| 5691 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 5692 | #endif |
| 5693 | #ifdef _SC_MEMLOCK |
| 5694 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 5695 | #endif |
| 5696 | #ifdef _SC_MEMLOCK_RANGE |
| 5697 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 5698 | #endif |
| 5699 | #ifdef _SC_MEMORY_PROTECTION |
| 5700 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 5701 | #endif |
| 5702 | #ifdef _SC_MESSAGE_PASSING |
| 5703 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 5704 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5705 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 5706 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 5707 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5708 | #ifdef _SC_MQ_OPEN_MAX |
| 5709 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 5710 | #endif |
| 5711 | #ifdef _SC_MQ_PRIO_MAX |
| 5712 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 5713 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5714 | #ifdef _SC_NACLS_MAX |
| 5715 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 5716 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5717 | #ifdef _SC_NGROUPS_MAX |
| 5718 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 5719 | #endif |
| 5720 | #ifdef _SC_NL_ARGMAX |
| 5721 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 5722 | #endif |
| 5723 | #ifdef _SC_NL_LANGMAX |
| 5724 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 5725 | #endif |
| 5726 | #ifdef _SC_NL_MSGMAX |
| 5727 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 5728 | #endif |
| 5729 | #ifdef _SC_NL_NMAX |
| 5730 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 5731 | #endif |
| 5732 | #ifdef _SC_NL_SETMAX |
| 5733 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 5734 | #endif |
| 5735 | #ifdef _SC_NL_TEXTMAX |
| 5736 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 5737 | #endif |
| 5738 | #ifdef _SC_NPROCESSORS_CONF |
| 5739 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 5740 | #endif |
| 5741 | #ifdef _SC_NPROCESSORS_ONLN |
| 5742 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 5743 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5744 | #ifdef _SC_NPROC_CONF |
| 5745 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 5746 | #endif |
| 5747 | #ifdef _SC_NPROC_ONLN |
| 5748 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 5749 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5750 | #ifdef _SC_NZERO |
| 5751 | {"SC_NZERO", _SC_NZERO}, |
| 5752 | #endif |
| 5753 | #ifdef _SC_OPEN_MAX |
| 5754 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 5755 | #endif |
| 5756 | #ifdef _SC_PAGESIZE |
| 5757 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 5758 | #endif |
| 5759 | #ifdef _SC_PAGE_SIZE |
| 5760 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 5761 | #endif |
| 5762 | #ifdef _SC_PASS_MAX |
| 5763 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 5764 | #endif |
| 5765 | #ifdef _SC_PHYS_PAGES |
| 5766 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 5767 | #endif |
| 5768 | #ifdef _SC_PII |
| 5769 | {"SC_PII", _SC_PII}, |
| 5770 | #endif |
| 5771 | #ifdef _SC_PII_INTERNET |
| 5772 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 5773 | #endif |
| 5774 | #ifdef _SC_PII_INTERNET_DGRAM |
| 5775 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 5776 | #endif |
| 5777 | #ifdef _SC_PII_INTERNET_STREAM |
| 5778 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 5779 | #endif |
| 5780 | #ifdef _SC_PII_OSI |
| 5781 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 5782 | #endif |
| 5783 | #ifdef _SC_PII_OSI_CLTS |
| 5784 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 5785 | #endif |
| 5786 | #ifdef _SC_PII_OSI_COTS |
| 5787 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 5788 | #endif |
| 5789 | #ifdef _SC_PII_OSI_M |
| 5790 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 5791 | #endif |
| 5792 | #ifdef _SC_PII_SOCKET |
| 5793 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 5794 | #endif |
| 5795 | #ifdef _SC_PII_XTI |
| 5796 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 5797 | #endif |
| 5798 | #ifdef _SC_POLL |
| 5799 | {"SC_POLL", _SC_POLL}, |
| 5800 | #endif |
| 5801 | #ifdef _SC_PRIORITIZED_IO |
| 5802 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 5803 | #endif |
| 5804 | #ifdef _SC_PRIORITY_SCHEDULING |
| 5805 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 5806 | #endif |
| 5807 | #ifdef _SC_REALTIME_SIGNALS |
| 5808 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 5809 | #endif |
| 5810 | #ifdef _SC_RE_DUP_MAX |
| 5811 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 5812 | #endif |
| 5813 | #ifdef _SC_RTSIG_MAX |
| 5814 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 5815 | #endif |
| 5816 | #ifdef _SC_SAVED_IDS |
| 5817 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 5818 | #endif |
| 5819 | #ifdef _SC_SCHAR_MAX |
| 5820 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 5821 | #endif |
| 5822 | #ifdef _SC_SCHAR_MIN |
| 5823 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 5824 | #endif |
| 5825 | #ifdef _SC_SELECT |
| 5826 | {"SC_SELECT", _SC_SELECT}, |
| 5827 | #endif |
| 5828 | #ifdef _SC_SEMAPHORES |
| 5829 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 5830 | #endif |
| 5831 | #ifdef _SC_SEM_NSEMS_MAX |
| 5832 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 5833 | #endif |
| 5834 | #ifdef _SC_SEM_VALUE_MAX |
| 5835 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 5836 | #endif |
| 5837 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 5838 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 5839 | #endif |
| 5840 | #ifdef _SC_SHRT_MAX |
| 5841 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 5842 | #endif |
| 5843 | #ifdef _SC_SHRT_MIN |
| 5844 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 5845 | #endif |
| 5846 | #ifdef _SC_SIGQUEUE_MAX |
| 5847 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 5848 | #endif |
| 5849 | #ifdef _SC_SIGRT_MAX |
| 5850 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 5851 | #endif |
| 5852 | #ifdef _SC_SIGRT_MIN |
| 5853 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 5854 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5855 | #ifdef _SC_SOFTPOWER |
| 5856 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 5857 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5858 | #ifdef _SC_SPLIT_CACHE |
| 5859 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 5860 | #endif |
| 5861 | #ifdef _SC_SSIZE_MAX |
| 5862 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 5863 | #endif |
| 5864 | #ifdef _SC_STACK_PROT |
| 5865 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 5866 | #endif |
| 5867 | #ifdef _SC_STREAM_MAX |
| 5868 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 5869 | #endif |
| 5870 | #ifdef _SC_SYNCHRONIZED_IO |
| 5871 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 5872 | #endif |
| 5873 | #ifdef _SC_THREADS |
| 5874 | {"SC_THREADS", _SC_THREADS}, |
| 5875 | #endif |
| 5876 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 5877 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 5878 | #endif |
| 5879 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 5880 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 5881 | #endif |
| 5882 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 5883 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 5884 | #endif |
| 5885 | #ifdef _SC_THREAD_KEYS_MAX |
| 5886 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 5887 | #endif |
| 5888 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 5889 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 5890 | #endif |
| 5891 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 5892 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 5893 | #endif |
| 5894 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 5895 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 5896 | #endif |
| 5897 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 5898 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 5899 | #endif |
| 5900 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 5901 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 5902 | #endif |
| 5903 | #ifdef _SC_THREAD_STACK_MIN |
| 5904 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 5905 | #endif |
| 5906 | #ifdef _SC_THREAD_THREADS_MAX |
| 5907 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 5908 | #endif |
| 5909 | #ifdef _SC_TIMERS |
| 5910 | {"SC_TIMERS", _SC_TIMERS}, |
| 5911 | #endif |
| 5912 | #ifdef _SC_TIMER_MAX |
| 5913 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 5914 | #endif |
| 5915 | #ifdef _SC_TTY_NAME_MAX |
| 5916 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 5917 | #endif |
| 5918 | #ifdef _SC_TZNAME_MAX |
| 5919 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 5920 | #endif |
| 5921 | #ifdef _SC_T_IOV_MAX |
| 5922 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 5923 | #endif |
| 5924 | #ifdef _SC_UCHAR_MAX |
| 5925 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 5926 | #endif |
| 5927 | #ifdef _SC_UINT_MAX |
| 5928 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 5929 | #endif |
| 5930 | #ifdef _SC_UIO_MAXIOV |
| 5931 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 5932 | #endif |
| 5933 | #ifdef _SC_ULONG_MAX |
| 5934 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 5935 | #endif |
| 5936 | #ifdef _SC_USHRT_MAX |
| 5937 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 5938 | #endif |
| 5939 | #ifdef _SC_VERSION |
| 5940 | {"SC_VERSION", _SC_VERSION}, |
| 5941 | #endif |
| 5942 | #ifdef _SC_WORD_BIT |
| 5943 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 5944 | #endif |
| 5945 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 5946 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 5947 | #endif |
| 5948 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 5949 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 5950 | #endif |
| 5951 | #ifdef _SC_XBS5_LP64_OFF64 |
| 5952 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 5953 | #endif |
| 5954 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 5955 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 5956 | #endif |
| 5957 | #ifdef _SC_XOPEN_CRYPT |
| 5958 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 5959 | #endif |
| 5960 | #ifdef _SC_XOPEN_ENH_I18N |
| 5961 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 5962 | #endif |
| 5963 | #ifdef _SC_XOPEN_LEGACY |
| 5964 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 5965 | #endif |
| 5966 | #ifdef _SC_XOPEN_REALTIME |
| 5967 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 5968 | #endif |
| 5969 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 5970 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 5971 | #endif |
| 5972 | #ifdef _SC_XOPEN_SHM |
| 5973 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 5974 | #endif |
| 5975 | #ifdef _SC_XOPEN_UNIX |
| 5976 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 5977 | #endif |
| 5978 | #ifdef _SC_XOPEN_VERSION |
| 5979 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 5980 | #endif |
| 5981 | #ifdef _SC_XOPEN_XCU_VERSION |
| 5982 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 5983 | #endif |
| 5984 | #ifdef _SC_XOPEN_XPG2 |
| 5985 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 5986 | #endif |
| 5987 | #ifdef _SC_XOPEN_XPG3 |
| 5988 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 5989 | #endif |
| 5990 | #ifdef _SC_XOPEN_XPG4 |
| 5991 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 5992 | #endif |
| 5993 | }; |
| 5994 | |
| 5995 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5996 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5997 | { |
| 5998 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 5999 | sizeof(posix_constants_sysconf) |
| 6000 | / sizeof(struct constdef)); |
| 6001 | } |
| 6002 | |
| 6003 | static char posix_sysconf__doc__[] = "\ |
| 6004 | sysconf(name) -> integer\n\ |
| 6005 | Return an integer-valued system configuration variable."; |
| 6006 | |
| 6007 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6008 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6009 | { |
| 6010 | PyObject *result = NULL; |
| 6011 | int name; |
| 6012 | |
| 6013 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6014 | int value; |
| 6015 | |
| 6016 | errno = 0; |
| 6017 | value = sysconf(name); |
| 6018 | if (value == -1 && errno != 0) |
| 6019 | posix_error(); |
| 6020 | else |
| 6021 | result = PyInt_FromLong(value); |
| 6022 | } |
| 6023 | return result; |
| 6024 | } |
| 6025 | #endif |
| 6026 | |
| 6027 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6028 | /* This code is used to ensure that the tables of configuration value names |
| 6029 | * are in sorted order as required by conv_confname(), and also to build the |
| 6030 | * the exported dictionaries that are used to publish information about the |
| 6031 | * names available on the host platform. |
| 6032 | * |
| 6033 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6034 | * when used, even for platforms we're not able to test on. It also makes |
| 6035 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6036 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6037 | |
| 6038 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6039 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6040 | { |
| 6041 | const struct constdef *c1 = |
| 6042 | (const struct constdef *) v1; |
| 6043 | const struct constdef *c2 = |
| 6044 | (const struct constdef *) v2; |
| 6045 | |
| 6046 | return strcmp(c1->name, c2->name); |
| 6047 | } |
| 6048 | |
| 6049 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6050 | setup_confname_table(struct constdef *table, size_t tablesize, |
| 6051 | char *tablename, PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6052 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6053 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6054 | size_t i; |
| 6055 | int status; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6056 | |
| 6057 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6058 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6059 | if (d == NULL) |
| 6060 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6061 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6062 | for (i=0; i < tablesize; ++i) { |
| 6063 | PyObject *o = PyInt_FromLong(table[i].value); |
| 6064 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6065 | Py_XDECREF(o); |
| 6066 | Py_DECREF(d); |
| 6067 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6068 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6069 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6070 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6071 | status = PyDict_SetItemString(moddict, tablename, d); |
| 6072 | Py_DECREF(d); |
| 6073 | return status; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6074 | } |
| 6075 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6076 | /* Return -1 on failure, 0 on success. */ |
| 6077 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6078 | setup_confname_tables(PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6079 | { |
| 6080 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6081 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6082 | sizeof(posix_constants_pathconf) |
| 6083 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6084 | "pathconf_names", moddict)) |
| 6085 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6086 | #endif |
| 6087 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6088 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6089 | sizeof(posix_constants_confstr) |
| 6090 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6091 | "confstr_names", moddict)) |
| 6092 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6093 | #endif |
| 6094 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6095 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6096 | sizeof(posix_constants_sysconf) |
| 6097 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6098 | "sysconf_names", moddict)) |
| 6099 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6100 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6101 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6102 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6103 | |
| 6104 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6105 | static char posix_abort__doc__[] = "\ |
| 6106 | abort() -> does not return!\n\ |
| 6107 | Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\ |
| 6108 | in the hardest way possible on the hosting operating system."; |
| 6109 | |
| 6110 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6111 | posix_abort(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6112 | { |
| 6113 | if (!PyArg_ParseTuple(args, ":abort")) |
| 6114 | return NULL; |
| 6115 | abort(); |
| 6116 | /*NOTREACHED*/ |
| 6117 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6118 | return NULL; |
| 6119 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6120 | |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6121 | #ifdef MS_WIN32 |
| 6122 | static char win32_startfile__doc__[] = "\ |
| 6123 | startfile(filepath) - Start a file with its associated application.\n\ |
| 6124 | \n\ |
| 6125 | This acts like double-clicking the file in Explorer, or giving the file\n\ |
| 6126 | name as an argument to the DOS \"start\" command: the file is opened\n\ |
| 6127 | with whatever application (if any) its extension is associated.\n\ |
| 6128 | \n\ |
| 6129 | startfile returns as soon as the associated application is launched.\n\ |
| 6130 | There is no option to wait for the application to close, and no way\n\ |
| 6131 | to retrieve the application's exit status.\n\ |
| 6132 | \n\ |
| 6133 | The filepath is relative to the current directory. If you want to use\n\ |
| 6134 | an absolute path, make sure the first character is not a slash (\"/\");\n\ |
| 6135 | the underlying Win32 ShellExecute function doesn't work if it is."; |
| 6136 | |
| 6137 | static PyObject * |
| 6138 | win32_startfile(PyObject *self, PyObject *args) |
| 6139 | { |
| 6140 | char *filepath; |
| 6141 | HINSTANCE rc; |
| 6142 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) |
| 6143 | return NULL; |
| 6144 | Py_BEGIN_ALLOW_THREADS |
| 6145 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); |
| 6146 | Py_END_ALLOW_THREADS |
| 6147 | if (rc <= (HINSTANCE)32) |
| 6148 | return win32_error("startfile", filepath); |
| 6149 | Py_INCREF(Py_None); |
| 6150 | return Py_None; |
| 6151 | } |
| 6152 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6153 | |
| 6154 | static PyMethodDef posix_methods[] = { |
| 6155 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 6156 | #ifdef HAVE_TTYNAME |
| 6157 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 6158 | #endif |
| 6159 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 6160 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6161 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6162 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6163 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 6164 | #ifdef HAVE_CHROOT |
| 6165 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 6166 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6167 | #ifdef HAVE_CTERMID |
| 6168 | {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__}, |
| 6169 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6170 | #ifdef HAVE_GETCWD |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6171 | {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6172 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6173 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6174 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6175 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6176 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 6177 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 6178 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6179 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6180 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6181 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6182 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6183 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6184 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6185 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 6186 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 6187 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6188 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6189 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6190 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6191 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6192 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6193 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6194 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6195 | #ifdef HAVE_UNAME |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6196 | {"uname", posix_uname, METH_VARARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6197 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6198 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 6199 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 6200 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6201 | #ifdef HAVE_TIMES |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6202 | {"times", posix_times, METH_VARARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6203 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6204 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6205 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6206 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 6207 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6208 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6209 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6210 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 6211 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6212 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6213 | #ifdef HAVE_FORK1 |
| 6214 | {"fork1", posix_fork1, METH_VARARGS, posix_fork1__doc__}, |
| 6215 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6216 | #ifdef HAVE_FORK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6217 | {"fork", posix_fork, METH_VARARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6218 | #endif /* HAVE_FORK */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6219 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6220 | {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__}, |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6221 | #endif /* HAVE_OPENPTY || HAVE__GETPTY */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6222 | #ifdef HAVE_FORKPTY |
| 6223 | {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__}, |
| 6224 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6225 | #ifdef HAVE_GETEGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6226 | {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6227 | #endif /* HAVE_GETEGID */ |
| 6228 | #ifdef HAVE_GETEUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6229 | {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6230 | #endif /* HAVE_GETEUID */ |
| 6231 | #ifdef HAVE_GETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6232 | {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6233 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6234 | #ifdef HAVE_GETGROUPS |
| 6235 | {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__}, |
| 6236 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6237 | {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6238 | #ifdef HAVE_GETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6239 | {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6240 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6241 | #ifdef HAVE_GETPPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6242 | {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6243 | #endif /* HAVE_GETPPID */ |
| 6244 | #ifdef HAVE_GETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6245 | {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6246 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6247 | #ifdef HAVE_GETLOGIN |
| 6248 | {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__}, |
| 6249 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6250 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6251 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6252 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6253 | #ifdef HAVE_KILLPG |
| 6254 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 6255 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6256 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6257 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6258 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6259 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6260 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 6261 | #ifdef MS_WIN32 |
| 6262 | {"popen2", win32_popen2, METH_VARARGS}, |
| 6263 | {"popen3", win32_popen3, METH_VARARGS}, |
| 6264 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6265 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6266 | #else |
| 6267 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 6268 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 6269 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 6270 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 6271 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 6272 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6273 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6274 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6275 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6276 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6277 | #ifdef HAVE_SETEUID |
| 6278 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 6279 | #endif /* HAVE_SETEUID */ |
| 6280 | #ifdef HAVE_SETEGID |
| 6281 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 6282 | #endif /* HAVE_SETEGID */ |
| 6283 | #ifdef HAVE_SETREUID |
| 6284 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 6285 | #endif /* HAVE_SETREUID */ |
| 6286 | #ifdef HAVE_SETREGID |
| 6287 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 6288 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6289 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6290 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6291 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6292 | #ifdef HAVE_SETGROUPS |
| 6293 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 6294 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6295 | #ifdef HAVE_SETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6296 | {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6297 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6298 | #ifdef HAVE_WAIT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6299 | {"wait", posix_wait, METH_VARARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6300 | #endif /* HAVE_WAIT */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6301 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6302 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6303 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6304 | #ifdef HAVE_SETSID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6305 | {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6306 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6307 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6308 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6309 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6310 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6311 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6312 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6313 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6314 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6315 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6316 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 6317 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 6318 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 6319 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 6320 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 6321 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 6322 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 6323 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 6324 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6325 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6326 | #ifdef HAVE_PIPE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6327 | {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6328 | #endif |
| 6329 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6330 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6331 | #endif |
| 6332 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6333 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6334 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6335 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6336 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6337 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6338 | #ifdef HAVE_UNSETENV |
| 6339 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 6340 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6341 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6342 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6343 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6344 | #ifdef HAVE_FSYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6345 | {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6346 | #endif |
| 6347 | #ifdef HAVE_FDATASYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6348 | {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6349 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6350 | #ifdef HAVE_SYS_WAIT_H |
| 6351 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6352 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6353 | #endif /* WIFSTOPPED */ |
| 6354 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6355 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6356 | #endif /* WIFSIGNALED */ |
| 6357 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6358 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6359 | #endif /* WIFEXITED */ |
| 6360 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6361 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6362 | #endif /* WEXITSTATUS */ |
| 6363 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6364 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6365 | #endif /* WTERMSIG */ |
| 6366 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6367 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6368 | #endif /* WSTOPSIG */ |
| 6369 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6370 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6371 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6372 | #endif |
| 6373 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6374 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6375 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 6376 | #ifdef HAVE_TMPFILE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6377 | {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__}, |
| 6378 | #endif |
| 6379 | #ifdef HAVE_TEMPNAM |
| 6380 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 6381 | #endif |
| 6382 | #ifdef HAVE_TMPNAM |
| 6383 | {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__}, |
| 6384 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6385 | #ifdef HAVE_CONFSTR |
| 6386 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 6387 | #endif |
| 6388 | #ifdef HAVE_SYSCONF |
| 6389 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 6390 | #endif |
| 6391 | #ifdef HAVE_FPATHCONF |
| 6392 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 6393 | #endif |
| 6394 | #ifdef HAVE_PATHCONF |
| 6395 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 6396 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6397 | {"abort", posix_abort, METH_VARARGS, posix_abort__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 6398 | #ifdef MS_WIN32 |
| 6399 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 6400 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6401 | {NULL, NULL} /* Sentinel */ |
| 6402 | }; |
| 6403 | |
| 6404 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6405 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6406 | ins(PyObject *d, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6407 | { |
| 6408 | PyObject* v = PyInt_FromLong(value); |
| 6409 | if (!v || PyDict_SetItemString(d, symbol, v) < 0) |
| 6410 | return -1; /* triggers fatal error */ |
| 6411 | |
| 6412 | Py_DECREF(v); |
| 6413 | return 0; |
| 6414 | } |
| 6415 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6416 | #if defined(PYOS_OS2) |
| 6417 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
| 6418 | static int insertvalues(PyObject *d) |
| 6419 | { |
| 6420 | APIRET rc; |
| 6421 | ULONG values[QSV_MAX+1]; |
| 6422 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 6423 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6424 | |
| 6425 | Py_BEGIN_ALLOW_THREADS |
| 6426 | rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values)); |
| 6427 | Py_END_ALLOW_THREADS |
| 6428 | |
| 6429 | if (rc != NO_ERROR) { |
| 6430 | os2_error(rc); |
| 6431 | return -1; |
| 6432 | } |
| 6433 | |
| 6434 | if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 6435 | if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 6436 | if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 6437 | if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 6438 | if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 6439 | if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 6440 | if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1; |
| 6441 | |
| 6442 | switch (values[QSV_VERSION_MINOR]) { |
| 6443 | case 0: ver = "2.00"; break; |
| 6444 | case 10: ver = "2.10"; break; |
| 6445 | case 11: ver = "2.11"; break; |
| 6446 | case 30: ver = "3.00"; break; |
| 6447 | case 40: ver = "4.00"; break; |
| 6448 | case 50: ver = "5.00"; break; |
| 6449 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 6450 | PyOS_snprintf(tmp, sizeof(tmp), |
| 6451 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 6452 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6453 | ver = &tmp[0]; |
| 6454 | } |
| 6455 | |
| 6456 | /* Add Indicator of the Version of the Operating System */ |
| 6457 | v = PyString_FromString(ver); |
| 6458 | if (!v || PyDict_SetItemString(d, "version", v) < 0) |
| 6459 | return -1; |
| 6460 | Py_DECREF(v); |
| 6461 | |
| 6462 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 6463 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 6464 | tmp[1] = ':'; |
| 6465 | tmp[2] = '\0'; |
| 6466 | |
| 6467 | v = PyString_FromString(tmp); |
| 6468 | if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0) |
| 6469 | return -1; |
| 6470 | Py_DECREF(v); |
| 6471 | |
| 6472 | return 0; |
| 6473 | } |
| 6474 | #endif |
| 6475 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6476 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6477 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6478 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6479 | #ifdef F_OK |
| 6480 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6481 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6482 | #ifdef R_OK |
| 6483 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6484 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6485 | #ifdef W_OK |
| 6486 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6487 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6488 | #ifdef X_OK |
| 6489 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6490 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6491 | #ifdef NGROUPS_MAX |
| 6492 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 6493 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6494 | #ifdef TMP_MAX |
| 6495 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 6496 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6497 | #ifdef WNOHANG |
| 6498 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6499 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6500 | #ifdef O_RDONLY |
| 6501 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 6502 | #endif |
| 6503 | #ifdef O_WRONLY |
| 6504 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 6505 | #endif |
| 6506 | #ifdef O_RDWR |
| 6507 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 6508 | #endif |
| 6509 | #ifdef O_NDELAY |
| 6510 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 6511 | #endif |
| 6512 | #ifdef O_NONBLOCK |
| 6513 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 6514 | #endif |
| 6515 | #ifdef O_APPEND |
| 6516 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 6517 | #endif |
| 6518 | #ifdef O_DSYNC |
| 6519 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 6520 | #endif |
| 6521 | #ifdef O_RSYNC |
| 6522 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 6523 | #endif |
| 6524 | #ifdef O_SYNC |
| 6525 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 6526 | #endif |
| 6527 | #ifdef O_NOCTTY |
| 6528 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 6529 | #endif |
| 6530 | #ifdef O_CREAT |
| 6531 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 6532 | #endif |
| 6533 | #ifdef O_EXCL |
| 6534 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 6535 | #endif |
| 6536 | #ifdef O_TRUNC |
| 6537 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 6538 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 6539 | #ifdef O_BINARY |
| 6540 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 6541 | #endif |
| 6542 | #ifdef O_TEXT |
| 6543 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 6544 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 6545 | #ifdef O_LARGEFILE |
| 6546 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 6547 | #endif |
| 6548 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6549 | /* MS Windows */ |
| 6550 | #ifdef O_NOINHERIT |
| 6551 | /* Don't inherit in child processes. */ |
| 6552 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 6553 | #endif |
| 6554 | #ifdef _O_SHORT_LIVED |
| 6555 | /* Optimize for short life (keep in memory). */ |
| 6556 | /* MS forgot to define this one with a non-underscore form too. */ |
| 6557 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 6558 | #endif |
| 6559 | #ifdef O_TEMPORARY |
| 6560 | /* Automatically delete when last handle is closed. */ |
| 6561 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 6562 | #endif |
| 6563 | #ifdef O_RANDOM |
| 6564 | /* Optimize for random access. */ |
| 6565 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 6566 | #endif |
| 6567 | #ifdef O_SEQUENTIAL |
| 6568 | /* Optimize for sequential access. */ |
| 6569 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 6570 | #endif |
| 6571 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 6572 | /* GNU extensions. */ |
| 6573 | #ifdef O_DIRECT |
| 6574 | /* Direct disk access. */ |
| 6575 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 6576 | #endif |
| 6577 | #ifdef O_DIRECTORY |
| 6578 | /* Must be a directory. */ |
| 6579 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 6580 | #endif |
| 6581 | #ifdef O_NOFOLLOW |
| 6582 | /* Do not follow links. */ |
| 6583 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 6584 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6585 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 6586 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6587 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 6588 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 6589 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 6590 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 6591 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 6592 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 6593 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 6594 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 6595 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 6596 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 6597 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 6598 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 6599 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 6600 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 6601 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 6602 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 6603 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 6604 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 6605 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 6606 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 6607 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 6608 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 6609 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 6610 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 6611 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 6612 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 6613 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 6614 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6615 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 6616 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6617 | #if defined(PYOS_OS2) |
| 6618 | if (insertvalues(d)) return -1; |
| 6619 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6620 | return 0; |
| 6621 | } |
| 6622 | |
| 6623 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6624 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 6625 | #define INITFUNC initnt |
| 6626 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 6627 | |
| 6628 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6629 | #define INITFUNC initos2 |
| 6630 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 6631 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6632 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 6633 | #define INITFUNC initposix |
| 6634 | #define MODNAME "posix" |
| 6635 | #endif |
| 6636 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 6637 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6638 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6639 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6640 | PyObject *m, *d, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6641 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 6642 | m = Py_InitModule4(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6643 | posix_methods, |
| 6644 | posix__doc__, |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 6645 | (PyObject *)NULL, |
| 6646 | PYTHON_API_VERSION); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6647 | d = PyModule_GetDict(m); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6648 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 6649 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6650 | v = convertenviron(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6651 | if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 6652 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6653 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6654 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6655 | if (all_ins(d)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6656 | return; |
| 6657 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6658 | if (setup_confname_tables(d)) |
| 6659 | return; |
| 6660 | |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 6661 | PyDict_SetItemString(d, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6662 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 6663 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 6664 | if (posix_putenv_garbage == NULL) |
| 6665 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 6666 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6667 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 6668 | stat_result_desc.name = MODNAME ".stat_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6669 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 6670 | PyDict_SetItemString(d, "stat_result", (PyObject*) &StatResultType); |
| 6671 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 6672 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6673 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Guido van Rossum | bb2501f | 2001-12-27 16:23:28 +0000 | [diff] [blame] | 6674 | PyDict_SetItemString(d, "statvfs_result", (PyObject*) &StatVFSResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6675 | } |